为什么我不能通过MonadReader约束致电询问

时间:2018-12-26 17:05:46

标签: haskell monad-transformers

我试图在ask约束的do表示法块中调用MonadReader,但遇到有关刚性类型的错误。据我所知,ask是为所有MonadReader定义的?

bar :: MonadReader r m => m r
bar = do
  i <- ask
  return i

这将返回

• Couldn't match type ‘m’ with ‘ReaderT r m0’
  ‘m’ is a rigid type variable bound by
    the type signature for:
      bar :: forall r (m :: * -> *). MonadReader r m => m r
    at mem.hs:50:8
  Expected type: m r
    Actual type: ReaderT r m0 r
• In a stmt of a 'do' block: i <- ask
  In the expression:
    do { i <- ask;
         return i }
  In an equation for ‘bar’:
      bar
        = do { i <- ask;
               return i }

1 个答案:

答案 0 :(得分:6)

似乎您已经从onCreateView()导入了ask,但是却从Control.Monad.Trans.Reader导入了MonadReaderControl.Monad.Reader中的ask函数是单态的,并且仅与Control.Monad.Trans.Reader一起使用。只需自行导入ReaderT,即可重新导出Control.Monad.Reader,但它提供了ReaderTasklocal的通用版本。