高阶函数和ST [英] Higher-order functions and ST
问题描述
我正在玩 http://hackage.haskell.org/packages/archive/vault/0.2.0.0/doc/html/Data-Vault-ST.html ,并希望编写如下功能:
onVault f = runST(f< $> Vault.newKey)
onVault2 f = runST(f< $> Vault .newKey * Vault.newKey)
等。如果我将这些函数替换为不带参数的函数,并调用一个特定的函数而不是f,它可以工作,但这些高阶函数不会输入check。
是否正在进行,我可以修复它? c>和 onVault2
排名2类。
{ - #LANGUAGE Rank2Types# - } - RankNTypes也可以工作
onVault: :(forall s。Key sa - > b) - > b
onVault2 ::(全部密钥s a - >密钥b - > c) - > c
这是因为 runST ::(forall s。ST sa) - >一个
要求传递的行为在状态线程参数 s
中是多态的,这是一种用于保证纯度的类型级别技巧。详情请参阅关于HaskellWiki的ST monad文章。
I'm playing with http://hackage.haskell.org/packages/archive/vault/0.2.0.0/doc/html/Data-Vault-ST.html and want to write functions like the following:
onVault f = runST (f <$> Vault.newKey)
onVault2 f = runST (f <$> Vault.newKey <*> Vault.newKey)
etc. If I replace these functions with ones that take no arguments and call a particular function instead of f, it works, but these higher-order functions will not type check.
What is going on and can I fix it?
You need to give onVault
and onVault2
rank 2 types.
{-# LANGUAGE Rank2Types #-} -- RankNTypes would also work
onVault :: (forall s. Key s a -> b) -> b
onVault2 :: (forall s. Key s a -> Key s b -> c) -> c
This is because runST :: (forall s. ST s a) -> a
requires that the passed action is polymorphic in the state thread parameter s
, which is a type-level trick used to guarantee purity. See the ST monad article on HaskellWiki for details.
这篇关于高阶函数和ST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!