Using the Monad Laws

Although Applicative comes before Monad in our type class hierarchy, the Monad laws are shorter and easier to understand, so let’s focus on them before we conclude this section by looking at the laws for applicatives. There are three monad laws: left and right-hand identity laws, and a law of associativity. Let’s look at how they are shown in the documentation for the standard library, and then we’ll dig into them in detail:

 -- identity (left)
 return a >>= m = m s
 
 -- identity (right)
 m >>= return = m
 
 -- Associativity
 (a >>= b) >>= c = a >>= (​\​x -> b x >>= c)

The similarity between the monad and functor identity laws aren’t as obvious written this way as they could be, so let’s rewrite them slightly, and ...

Get Effective Haskell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.