从带有变压器堆栈的新类型派生MonadFree

时间:2018-10-07 11:04:22

标签: haskell free-monad newtype deriving

我正试图从MonadFree导出newtype,但我无法解决。我当前的代码是:

newtype ApplicationStack s r p m =
  ApplicationStack { runApplication :: StateT s (ReaderT r p) m }
  deriving (Functor, Applicative, Monad, MonadState s, MonadReader r)

deriving instance MonadFree f p => MonadFree f (ApplicationStack s r p)

我明白了

• Could not deduce (transformers-0.5.5.0:Control.Monad.Trans.Class.MonadTrans
                      (ApplicationStack s r))
    arising from a use of ‘Control.Monad.Free.Class.$dmwrap’
  from the context: MonadFree f p
    bound by the instance declaration
    at src/Application/Commands/Base.hs:41:10-62
• In the expression:
    Control.Monad.Free.Class.$dmwrap @(f) @(ApplicationStack s r p)
  In an equation for ‘Control.Monad.Free.Class.wrap’:
      Control.Monad.Free.Class.wrap
        = Control.Monad.Free.Class.$dmwrap @(f) @(ApplicationStack s r p)
  In the instance declaration for
    ‘MonadFree f (ApplicationStack s r p)’
   |
41 | instance MonadFree f p => MonadFree f (ApplicationStack s r p)
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

如果有人有任何建议,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

definition of MonadFree提供了wrap的默认定义:

wrap :: (m ~ t n, MonadTrans t, MonadFree f n, Functor f) => f (m a) -> m a

GHC尝试使用此定义,但找不到MonadTrans实例。

您可以为MonadTrans定义一个ApplicationStack实例,或者可以指导GHC将{{1}的MonadFree实例基于{{1} {1}}代替。如果您使用的是GHC 8.2或更高版本,则最简单的方法是使用deriving strategies。看起来像

ApplicationStack

使用较早的GHC,需要花些时间才能在正确的模块中激活正确的扩展。