Explanation of Monad laws

Viewed 8640

From a gentle introduction to Haskell, there are the following monad laws. Can anyone intuitively explain what they mean?

return a >>= k             = k a
m >>= return               = m
xs >>= return . f          = fmap f xs
m >>= (\x -> k x >>= h)    = (m >>= k) >>= h

Here is my attempted explanation:

  1. We expect the return function to wrap a so that its monadic nature is trivial. When we bind it to a function, there are no monadic effects, it should just pass a to the function.

  2. The unwrapped output of m is passed to return that rewraps it. The monadic nature remains the same. So it is the same as the original monad.

  3. The unwrapped value is passed to f then rewrapped. The monadic nature remains the same. This is the behavior expected when we transform a normal function into a monadic function.

  4. I don't have an explanation for this law. This does say that the monad must be "almost associative" though.

4 Answers
Related