[Haskell-cafe] Confusion on the third monad law when using lambda
dave at zednenem.com
Wed Jun 17 23:11:30 EDT 2009
On Wed, Jun 17, 2009 at 9:08 PM, Jon Strait<jstrait at moonloop.net> wrote:
> I use and love Haskell, but I just have this nagging concern, that maybe
> someone can help me reason about. If I'm missing something completely
> obvious here and making the wrong assumptions, please be gentle. :)
> I'm reading the third (bind associativity) law for monads in this form:
> m >>= (\x -> k x >>= h) = (m >>= k) >>= h
> Now considering the definition of liftM2:
> liftM2 f m1 m2 = m1 >>= (\x1 -> m2 >>= (\x2 -> return (f x1 x2)))
> Isn't this liftM2 definition in the same form as the LHS of the third law
> equation, with (\x2 -> return (f x1 x2)) being the h function?
The usual convention here is that m, k, and h are variables bound
outside the scope of the law. In other words, the law only applies to
expressions which can be rewritten to have the form
let m = ...
k = ...
h = ...
in m >>= (\x -> k x >>= h)
In the case of liftM2, you'd have to rewrite it as,
liftM2 f m1 m2 = m >>= \x -> k x >>= h
m = m1
k = \x -> m2 >>= \y -> return (x,y)
h = \(x,y) -> return (f x y)
which is awkward, but works perfectly fine with the third law.
Dave Menendez <dave at zednenem.com>
More information about the Haskell-Cafe