[Haskell-cafe] A question about "monad laws"

askyle valgarv at gmx.net
Tue Mar 11 19:52:57 EDT 2008


>  My favorite presentation of the monad laws is associativity of Kliesli
>  composition:
>  (a1 >=> a2) x = a1 x >>= a2   -- predefined in 6.8 control.monad
>  -- The laws
>  return >=> a    = a
>  a >=> return    = a
>  a >=> (b >=> c) = (a >=> b) >=> c

If you use this presentation you also need the following law:
(a . b) >=> c  = (a >=> c) . b
that is, compatibility with ordinary function composition. I like to call
this "naturality", since it's instrumental in proving return and bind to be
natural transformations.

The law looks less alien if we use a flipped version of (>=>):
(<=<) = flip (>=>)
{- Monad Laws in terms of (<=<) -}
return <=< f  =  f <=< return  =  f          -- Identity
f <=< (g <=< h)  =  (f <=< g) <=< h     -- Associativity
f <=< (g . h)  = (f <=< g) . h                -- Naturality

(which happens to be my favorite presentation of the laws, followed by the
derivations that lead to the 'do' notation, which lead to various 'ah'
moments from unsuspecting FP-challenged friends)

-----
Ariel J. Birnbaum
-- 
View this message in context: http://www.nabble.com/A-question-about-%22monad-laws%22-tp15411587p15975734.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list