我正在尝试使用类型系统来确保X永远不会从monad M中去掉。我希望它的工作方式类似于runST,因为它不可能混合来自不同线程的环境。
data X s = X Int
type M s = State Int
newX :: M s (X s)
newX = X <$> get
eval :: (forall s. M s a) -> a
eval x = evalState x 0但是,以下代码不会导致类型错误:
ghci> x = eval newX
ghci> :t x
x :: X s为什么ST monad中的类似代码会抛出错误,而我的不会?据我所知,M s a中的s应该被绑定,从而使X s中的s成为自由类型变量,从而导致类型检查器中出现错误。
发布于 2020-02-04 22:37:43
要强制类型抽象,必须使用data或newtype,而不是type。
type同义词中未使用的参数根本不起作用:
type M s = State Int所以这些是等价的:
newX :: M s (X s)
newX :: State Int (X s)
eval :: (forall s. M s a) -> a
eval :: State Int a -> aeval实际上并不是更高级别的。
https://stackoverflow.com/questions/60058614
复制相似问题