[Haskell-cafe] A question about monad laws

Sebastian Fischer fischer at nii.ac.jp
Tue Jan 18 16:28:30 CET 2011


Hi Kashyap,

> Could someone please help me get a better understanding of the necessity of monads complying with these laws?

Maybe it helps to write them in do-notation. Once written like this,
it becomes clear(er?) that do-notation would be much less intuitive if
the laws would not hold:

Left Unit: return x >>= \y -> f y  =  f x
    do y <- return x
       f y
  = do f x

Right Unit: a >>= \x -> return x  =  a
    do x <- a
       return x
  = do a

Associativity: (a >>= \x -> f x) >>= \y -> g y  =  a >>= \x -> (f x
>>= \y -> g y)
    do y <- do x <- a
               f x
       g y
  = do x <- a
       y <- f x
       g y

So, at least, the monad laws ensure that do-notation behaves as one
would expect.

Sebastian



More information about the Haskell-Cafe mailing list