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 show them next to the functor identity law, to help make the parallel a bit easier to see:

​  ​ -- Functor Identity law ​
​  fmap id f = id f
​  ​ -- Monad Identity (Left) law ​
​  (>>=) (return a) f = f a
​  ​ -- Monad Identity (Right) law ​
​  (>>=) m return = m

In the functor identity law, we said that we had restrictions around what we chould change when we called fmap . The monad identity laws are similar, telling us what we can change when we call >>= . Unlike our functor, the values inside of our monad can change when we call >>= . These laws also relate to what kind of a value we should get out of calling return .

This all sounds a little bit abstract, but if we think about it in terms of the IO monad, it’s easier to reason about what the laws are trying to say.

Let’s start by looking at one specific manifestation of the first law:

​  return ​ "filename" ​ >>= getcontents = getContents ​ "filename" ​

If we put our filename into an IO action, and then use >>= to pass it to getContents we shouldn’t expect it to behave any differently than if we’d just passed the filename in directly in the first place. This should make sense if you recall back here where we talk about how IO represents some changes that have happened out in the real world. The return function creates a blank slate where we haven’t actually made any changes, and >>= makes sure that our changes happen in order. Looking at it this way, it should make sense that “First do these no changes at all, and then read the file” shouldn’t behave any differently from just reading the file.

The second identity law is pretty similar to the first one. Let’s restate it the same way that we did the first law:

​  getContents ​ "filename" ​ >>= return = getContents ​ "filename" ​

Here we call getContents on our file, and then pass the contents to return with >>= . Our right-hand identity law basically says that the call to return shouldn’t change anything. In IO terms, saying “open the file and then do nothing” should be the same as saying “open the file.”

Our Maybe and List instances also obeyed these identity laws. Just like we did with Functor , let’s look at an example of an outlaw monad that breaks the identity laws. We’ll continue the trend of tracking how often fmap was called by adding a number to our counter every time we call >>= :

​  ​ instance ​ ​ Monad ​ ​ Outlaw ​ ​ where ​
​  return summary = ​ Outlaw ​ 0 summary
​  (​ Outlaw ​ cnt a) >>= f =
​  ​ let ​ (​ Outlaw ​ cnt' v) = f a
​  ​ in ​ ​ Outlaw ​ (cnt + cnt' + 1) v

Like we did before we’ll write a couple of manual test functions to see how the laws hold up with our outlaw monad. We’ll also add a utility function to make it easier to test >>= operations.

​  stoleAHorse :: ​ String ​ -> ​ Outlaw ​ ​ String ​
​  stoleAHorse = return . (<> ​ " and horse robber" ​)
​  
​  testLeftIdentity =
​  (return ​ "robbed a bank" ​ >>= stoleAHorse) == stoleAHorse ​ "robbed a bank" ​
​  
​  testRightIdentity =
​  (billyTheKid >>= return) == billyTheKid

If we load these up into a REPL, we can see the same sorts of failures we had with our outlaw functor—introducing changes to our counter that didn’t come from either of the operands to >>= cause our laws to be violated:

​  λ testLeftIdentity
​  False
​  λ return "robbed a bank" >>= stoleAHorse
​  Outlaw 1 "robbed a bank and horse robber"
​  λ stoleAHorse "robbed a bank"
​  Outlaw 0 "robbed a bank and horse robber"
​  λ testRightIdentity
​  False
​  λ billyTheKid >>= return
​  Outlaw 1 "bank robber"
​  λ billyTheKid
​  Outlaw 0 "bank robber"

Now that we’ve looked at how the first two monad laws work, and when they are violated, let’s move on to the final monad law: associativity. Associativity is related to the order that we evaluate values. The most familiar example of associativity that you’ve probably seen is in arithmetic. Both addition and multiplication are associative, meaning that you can omit or move around parentheses however you want, but division and subtraction aren’t associative, so changing the order of evaluation can change the output:

​  λ (2 + 3) + 4 == 2 + (3 + 4)
​  True
​  λ (2 * 3) * 4 == 2 * (3 * 4)
​  True
​  λ (2 - 3) - 4 == 2 - (3 - 4)
​  False
​  λ (2 / 3) / 4 == 2 / (3 / 4)
​  False

The associativity law for monads is really getting to the same point: the way that we group the monadic actions doesn’t matter in the end, because >>= ensures that they’ll be evaluated in a consistent order. Let’s look at this one in terms of IO actions to make it a little easier to understand:

​  λ openFile "/tmp/example.txt" ReadMode >>= hGetContents >>= putStrLn
​  Hello from "example.txt"
​  
​  λ :{
​  *System.IO| openFile "/tmp/example.txt" ReadMode
​  *System.IO| >>= (\handle -> hGetContents handle >>= putStrLn)
​  *System.IO| :}
​  Hello from "example.txt"

We can’t use \== to compare IO actions, but if we look at what’s happening in these examples, we can still understand what’s going on with the associativity law. In both examples, we’re opening a file handle, then reading the contents and printing them out. The law of associativity says that it doesn’t matter how we group these IO actions. This might seem a little counterintuitive at first; after all, how can it not matter if we try to read the contents of a file and print them out before we open the file? This sort of composability is available thanks to lazy evaluation. If you look back here and remember how IO is evaluated, you’ll understand how the particulars of the way Haskell treats evaluating IO allows it to fulfill this property.