1

我只是好奇为什么这段代码不能编译。为什么要求 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)

提前致谢。

4

1 回答 1

2

原因是您正在变压器内申请local fbase monad 。相反,它应该看起来像mReaderT

local f x = Netbeans $ local f (unNetbeans x)
于 2013-08-01T20:41:21.110 回答