有什么方法可以恢复足够的懒惰以在单子中喜结连理吗?

发布于 2024-12-20 00:11:36 字数 428 浏览 4 评论 0原文

我想通过打结来编写一些巧妙的代码(节省我很多时间来实现)。大致是这样的,

n <- myinstr n x

理论上,myinstr应该运行x来获取一个值,该值将变成nmyinstrState 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-12-27 00:11:36

没有通用的方法可以使任意 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文