假设我有以下monad转换器堆栈(为简单起见,将r和s保留为() ),
newtype MyMonad m a = MM (ReaderT () (StateT () m a)如果我想使用它作为haskeline的InputT的基单体,我需要一个System.Console.Haskeline.MonadException实例。考虑到这些实例的apparent complexity,我更愿意让编译器用GeneralizedNewtypeDeriving为我派生它。具体地说,我希望下面的代码进行类型检查,
{-# LANGUAGE GeneralizedNewtypeDeriving, StandaloneDeriving, FlexibleContexts #-}
import Control.Monad.State
import Control.Monad.Reader
import Control.Monad.IO.Class
import Control.Applicative
import System.Console.Haskeline.MonadException
newtype MyMonad m a = MM (ReaderT () (StateT () m) a)
deriving (Functor, Applicative, Monad, MonadIO)
deriving instance (MonadException m) => MonadException (MyMonad m) 但不幸的是,这给了我
/home/bgamari/hi.hs:11:1:
Could not deduce (MonadException (StateT () m))
arising from the superclasses of an instance declaration
from the context (MonadIO (MyMonad m), MonadException m)
bound by the instance declaration at /home/bgamari/hi.hs:11:1-66
Possible fix:
add an instance declaration for (MonadException (StateT () m))
In the instance declaration for `MonadException (MyMonad m)'查看为StateT和ReaderT提供的实例,
instance MonadException m => MonadException (ReaderT r m)
instance MonadException m => MonadException (StateT s m)期望编译器推导出StateT实例似乎是完全合理的。我对巧妙的GeneralizedNewtypeDeriving期望太高了吗?如果不对其进行开放编码,如何实现此实例?
发布于 2013-08-20 06:05:13
有两个版本的State monad:strict和lazy。import Control.Monad.State brings in the lazy version,但MonadException的实例似乎是针对严格版本的。
尝试使用import Control.Monad.State.Strict而不是import Control.Monad.State。
https://stackoverflow.com/questions/18323608
复制相似问题