假设我有以下单子变压器堆栈(为简单起见r
,请s
留下()
),
newtype MyMonad m a = MM (ReaderT () (StateT () m a)
如果我想将它用作 haskeline's 的基本 monad InputT
,我需要一个System.Console.Haskeline.MonadException
实例。鉴于这些实例的明显复杂性,我宁愿让编译器为我派生出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
吗?如果没有开放编码,如何实现这个实例?