Haskell 中 Monad 的结合性规则

发布于 2024-12-18 23:44:05 字数 298 浏览 4 评论 0原文

(m >>= f) >>>= g = m >>= (\x -> fx >>= g)

f\x->f x 有何不同?

我认为它们是同一类型 a -> m b。但方程右侧的第二个 >>= 似乎将 \x->f x 的类型视为 m b。 出什么问题了?

(m >>= f) >>= g = m >>= (\x -> f x >>= g)

what's different from f and \x->f x ??

I think they're the same type a -> m b. but it seems that the second >>= at right side of equation treats the type of \x->f x as m b.
what's going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

逆夏时光 2024-12-25 23:44:05

表达式 f\x ->对于大多数目的来说,f x 确实意味着同样的事情。然而,lambda 表达式的范围尽可能向右延伸,即 m >>= (\x -> (fx >>= g))

如果类型为m :: m af :: a -> m bg :: b -> m c,那么左边有 (m >>= f) :: m b,右边有 (\x -> fx >; >= g) :: a -> mc.

因此,两个表达式之间的区别只是 (>>=) 运算的执行顺序,就像表达式 1 + (2 + 3)(1 + 2) + 3 仅在执行加法的顺序上有所不同。

单子定律要求,像加法一样,两者的答案应该相同。

The expressions f and \x -> f x do, for most purposes, mean the same thing. However, the scope of a lambda expression extends as far to the right as possible, i.e. m >>= (\x -> (f x >>= g)).

If the types are m :: m a, f :: a -> m b, and g :: b -> m c, then on the left we have (m >>= f) :: m b, and on the right we have (\x -> f x >>= g) :: a -> m c.

So, the difference between the two expressions is just which order the (>>=) operations are performed, much like the expressions 1 + (2 + 3) and (1 + 2) + 3 differ only in the order in which the additions are performed.

The monad laws require that, like addition, the answer should be the same for both.

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