使用mtl,派生MonadState似乎可以正确地提升make state操作:
:set -XGeneralizedNewtypeDeriving
import Control.Applicative (Applicative)
import Control.Monad.Trans ( MonadIO, liftIO )
import Control.Monad.State (MonadState, evalStateT, modify, StateT, gets)
data State = State { int:: Int }
newtype St a = St { unSt :: StateT State IO a } deriving (Functor, Applicative, Monad, MonadIO, MonadState State)
let testLift = gets int >> return () :: St ()对于转换器,没有MonadState
:set -XGeneralizedNewtypeDeriving
import Control.Monad.Trans.State.Strict (runStateT, evalStateT, modify, StateT, gets)
import Control.Monad.IO.Class ( MonadIO, liftIO )
import Control.Applicative (Applicative)
data State = State { int:: Int }
newtype St a = St { unSt :: StateT State IO a } deriving (Functor, Applicative, Monad, MonadIO)
let testLift = gets int >> return () :: St ()Couldn't match type `StateT State m0' with `St'
Expected type: St Int
Actual type: StateT State m0 Int
In the return type of a call of `gets'
In the first argument of `(>>)', namely `gets int'
In the expression: gets int >> return () :: St ()我应该怎么做才能让它与transformers一起工作呢?
发布于 2013-07-15 04:54:04
你应该使用mtl来实现这一点。mtl和transformers并不是真正的竞争对手-- mtl depends on转换器!它的存在是为了添加像MonadState这样使用函数依赖的类,这样它们就不必成为核心(Haskell-98)类型定义的一部分。还有一个使用类型族的包(monads-tf),它也与transformers兼容(但实际上,您不应该使用它)。
也许我应该问:为什么你要“从mtl切换到transformers"?
发布于 2013-07-15 02:40:47
MonadState实例位于包monads-tf或monads-fd中,具体取决于您使用的是类型函数还是函数依赖项。我用monads-tf试过了(因为那是我喜欢的包),GHC不能为St派生一个MonadState实例,所以你必须自己写一个。
{-# OPTIONS_GHC -package transformers -package monads-tf -hide-package mtl #-}
{-# LANGUAGE GeneralizedNewtypeDeriving, TypeFamilies #-}
import Control.Monad.Trans.State.Strict (runStateT, evalStateT, modify, StateT)
import Control.Monad.State.Class -- MonadState class
import Control.Monad.IO.Class ( MonadIO, liftIO )
import Control.Applicative (Applicative)
data State = State { int:: Int }
newtype St a = St { unSt :: StateT State IO a } deriving (Functor, Applicative, Monad, MonadIO)
instance MonadState St where
type StateType St = State
get = St get
put x = St (put x)https://stackoverflow.com/questions/17642112
复制相似问题