将MonadReader实例添加到ReaderT包装器

时间:2013-08-01 20:09:19

标签: haskell

我只是好奇为什么这段代码不能编译。为什么要m为MonadReader ConnState?:

newtype Netbeans m a = Netbeans { unNetbeans :: ReaderT ConnState m a }
                            deriving (Monad, Functor, Applicative, MonadIO, MonadTrans)

instance Monad m => MonadReader ConnState (Netbeans m) where
    ask = Netbeans $ ask
    local f x = Netbeans $ mapReaderT (local f) (unNetbeans x)

错误消息如下(第88行是本地函数定义的行):

src/Vim/Netbeans.hs:88:40:
Could not deduce (MonadReader ConnState m)
  arising from a use of `local'
from the context (Monad m)
  bound by the instance declaration at src/Vim/Netbeans.hs:86:10-54
Possible fix:
  add (MonadReader ConnState m) to the context of
    the type signature for
      local :: (ConnState -> ConnState) -> Netbeans m a -> Netbeans m a
    or the instance declaration
  or add an instance declaration for (MonadReader ConnState m)
In the first argument of `mapReaderT', namely `(local f)'
In the second argument of `($)', namely
  `mapReaderT (local f) (unNetbeans x)'
In the expression: Netbeans $ mapReaderT (local f) (unNetbeans x)

提前致谢。

1 个答案:

答案 0 :(得分:2)

原因是您将local f应用于m转换器内的基本monad ReaderT。相反,它应该看起来像

local f x = Netbeans $ local f (unNetbeans x)
相关问题