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

Marcin Szamotulski profunctor at pm.me
Fri Jul 20 14:13:39 UTC 2018


Hi,

And if you draw the diagrams corresponding to the monad laws for join and return they have exactly the same shape as monoid laws (left and righ unitality and associativity), just the Cartesian product is exchanged for functor composition. It's a nice exercise on it's own to observe this fact so I will leave it here.

Regards,
Marcin

Sent from ProtonMail mobile

-------- Original Message --------
On 12 Jul 2018, 09:06, Alexis King wrote:

>> 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)
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20180720/42e9aa9b/attachment.html>


More information about the Haskell-Cafe mailing list