Reputation:
(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?
Upvotes: 10
Views: 567
Reputation: 77384
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.
Upvotes: 15