July 2023
Intermediate to advanced
670 pages
17h 13m
English
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 ...