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
aso that its monadic nature is trivial. When we bind it to a function, there are no monadic effects, it should just passato the function.The unwrapped output of
mis passed toreturnthat rewraps it. The monadic nature remains the same. So it is the same as the original monad.The unwrapped value is passed to
fthen 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.