[Haskell-cafe] Monad laws (Was: Investing in languages (Was: What is your favourite Haskell "aha" moment?))

Alexis King lexi.lambda at gmail.com
Thu Jul 12 07:06:21 UTC 2018

> On Jul 12, 2018, at 01:42, Tom Ellis <tom-lists-haskell-cafe-2017 at jaguarpaw.co.uk> wrote:
>> the monad laws are too hard to read.
> FWIW the monad laws are not hard to *read* if written in this form
>    return >=> f = f
>    f >=> return = f
>    (f >=> g) >=> h = f >=> (g >=> h)
> (Whether they're easy to *understand* in that form is another matter.)

Here is another formulation of the monad laws that is less frequently discussed than either of the ones using bind or Kleisli composition:

    (1) join . return = id
    (2) join . fmap return = id
    (3) join . join = join . map join

These laws map less obviously to the common ones, but I think they are easier to understand (not least because `join` is closer to the “essence” of what a monad is than >>=). (1) and (2) describe the intuitive notion that adding a layer and squashing it should be the identity function, whether you add the new layer on the outside or on the inside. Likewise, (3) states that if you have three layers and squash them all together, it doesn’t matter whether you squash the inner two or outer two together first.

(Credit goes to HTNW on Stack Overflow for explaining this to me. https://stackoverflow.com/a/45829556/465378)

More information about the Haskell-Cafe mailing list