有什么方法可以恢复足够的懒惰以在单子中喜结连理吗?
我想通过打结来编写一些巧妙的代码(节省我很多时间来实现)。大致是这样的,
n <- myinstr n x
理论上,myinstr
应该运行x
来获取一个值,该值将变成n
。 myinstr
在 State
monad 内运行,会将 n
置于状态中,但这不会影响 x
> 的计算。
我尝试过使用 DoRec
和简单的 mfix
实现,
instance Monad
I want to write a slick bit of code (saving me much time to implement otherwise) by tying the knot. It goes roughly like this,
n <- myinstr n x
where in theory, myinstr
should run x
to get a value, which will become n
. myinstr
, which runs inside a State
monad, will put n
into the state, but this doesn't affect x
's computation.
I've tried using DoRec
and a naiive implementation of mfix
,
instance Monad ???? => MonadFix (MyMonad ????) where
mfix f = fix (\mx -> mx >>= f)
but things freeze. Are there any methods for fixing my code (or methodologies for designing it correctly the first time) or should I write something more straightforward?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有通用的方法可以使任意 monad 成为 MonadFix 的实例。实际的代码取决于 monad,甚至不可能对所有 monad 都有效。
您可以查看各种 monad 以了解它是如何完成的。如果你的 monad 实际上是 State,那么应该已经有一个实例了。
There is no generic way to make an arbitrary monad an instance of
MonadFix
. The actual code depends on the monad, and it's not even possible for all monads.You can look at the various monads to see how it's done. And if your monad is in fact
State
there should already be an instance.