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:
- 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 passa
to the function. - 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.
- 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.
- I don't have an explanation for this law. This does say that the monad must be "almost associative" though