Haskell 中 Monad 的结合性规则
(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
表达式
f
和\x ->对于大多数目的来说,f x 确实意味着同样的事情。然而,lambda 表达式的范围尽可能向右延伸,即
m >>= (\x -> (fx >>= g))
。如果类型为
m :: m a
,f :: a -> m b
和g :: 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
, andg :: 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 expressions1 + (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.