Joh*_*ler 3 polymorphism haskell algebraic-data-types
我有一个data类型,我想存储一个函数:State Foo a -> a.据推测,当创建此类型的实例时,程序将部分应用于evalState计算的初始状态并将结果函数存储在数据结构中.稍后,可以从实例检索该函数,并用于评估Statemonad中的一个或多个计算并获得结果.
-- This doesn't work
data Bar = Bar {
-- other members here
runWithState :: State Foo a -> a
}
==> Not in scope: type variable 'a'
Run Code Online (Sandbox Code Playgroud)
我不能更具体,因为我不知道计算的最终结果是什么,它可能会根据计算产生的结果而改变.
如何使类型检查器与此一起工作?
forall与RankNTypes扩展名一起使用:
{-# LANGUAGE RankNTypes #-}
import Control.Monad.State
type Foo = String
data Bar = Bar {
-- other members here
runWithState :: forall a. State Foo a -> a
}
Run Code Online (Sandbox Code Playgroud)