我有以下newtype
:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype Wrap m a = Wrap {runWrap :: m a}
deriving (Functor, Applicative, Monad, MonadTrans)
我尝试自动派生 MonadTrans
,但出现以下错误:
• Can't make a derived instance of ‘MonadTrans Wrap’
(even with cunning GeneralizedNewtypeDeriving):
cannot eta-reduce the representation type enough
• In the newtype declaration for ‘Wrap’
但是,为 MonadTrans
编写简单的实例就可以了:
instance MonadTrans Wrap where
lift = Wrap
出现这样的错误消息的原因是什么?
最佳答案
GeneralizedNewtypeDeriving
使用类的底层实例来实现 newtype
的类。然而,在这种情况下,这没有任何意义,因为 m
甚至不适合作为 MonadTrans
的实例(回想一下 m::* -> *
,但 MonadTrans
想要 (* -> *) -> * -> *
)。
关于haskell - 无法创建 monad 转换器的派生实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41823706/