如何定义一个单子变压器,即两个任意单子变压器的组成?
我想写一些类似于以下内容的内容:
newtype FooT c d m a = FooT { unFooT :: (c (d m)) a }
instance (MonadTrans c, MonadTrans d) => MonadTrans (FooT c d) where
lift = FooT . lift . lift
但是,此片段不会编译:
Could not deduce (Monad (d m)) arising from a use of ‘lift’
我明白为什么这不会编译;我们不知道任意变压器的应用d M
本身就是一个单月。但是,我不确定最好的方法。 有没有干净的方法来制作这样的工作?据推测,如果我可以沿monad(dm)
的行添加一个约束在实例声明的左侧,但是我不知道该怎么做,因为<代码> m 不绑定。
I want to write something similar to the following:
newtype FooT c d m a = FooT { unFooT :: (c (d m)) a }
instance (MonadTrans c, MonadTrans d) => MonadTrans (FooT c d) where
lift = FooT . lift . lift
However, this snippet will not compile:
Could not deduce (Monad (d m)) arising from a use of ‘lift’
I understand why this won't compile; we don't know that the application of an arbitrary transformer d m
is itself a monad. However, I'm not sure of the best way to proceed.
Is there a clean way to make something like this work? Presumably it would go through if I could add a constraint along the lines of Monad (d m)
to the left-hand-side of the instance declaration, but I don't know how to do so since m
is not bound.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
endenifiedConstraints
GHC扩展程序,这是约束中的
m
与m
中的m
不像lift
中的m
。量化的约束仅表示它说的内容(“对于任何m :: type - &gt; type
,如果monad m
requient requiendmonad(dm)
> ”),在lift
中,正在与特定m
作为参数传递给lift
的特定m
实例化。因此,提升
'sm
不会逃脱其范围。With the
QuantifiedConstraints
GHC extension, this ism
in the constraint is not the samem
as inlift
. The quantified constraint simply means what it says ("for anym :: Type -> Type
, ifMonad m
requireMonad (d m)
"), and inlift
that universal statement is being instantiated with the particularm
being passed as argument tolift
. Thuslift
'sm
does not escape its scope.因为 promensioners 0.6 monadtrans 类型类有一个要求它可以保留它 。
这意味着
monadtrans
是:
变形金刚的组成(
composet
),您调用foot
不需要指定提升,因此代码代码您的问题中提供的版本应适用于 0.6+。已经存在于 derving-trans 中
Since transformers 0.6 the
MonadTrans
type class has had a requirement that it preservesMonad
.This means the definition of
MonadTrans
is:The composition of transformers (
ComposeT
), which you callFooT
doesn't need to specify the lifting, so the code you supplied in your question should work for versions 0.6+.ComposeT
already exists in deriving-trans