在使用monad变换器构建monad堆栈来编写库时,我遇到了一个关于它的行为的问题.
以下代码不会传递类型检查器:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Foo (FooM, runFooM, foo) where
import Control.Applicative
import Control.Monad.Reader
newtype FooM m a = FooM { runFooM :: ReaderT Int m a }
deriving (Functor, Applicative, Monad, MonadReader Int)
foo :: FooM m Int
foo = do
x <- ask
return x
Run Code Online (Sandbox Code Playgroud)
错误是:
$ ghc foo.hs
[1 of 1] Compiling Foo ( foo.hs, foo.o )
foo.hs:12:3:
No instance for (Monad m) arising from a do statement
Possible fix:
add (Monad m) to the …Run Code Online (Sandbox Code Playgroud) haskell ×1