Skip to Content
Effective Haskell
book

Effective Haskell

by Rebecca Skinner
July 2023
Intermediate to advanced
670 pages
17h 13m
English
Pragmatic Bookshelf
Content preview from Effective Haskell

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Grokking Functional Programming

Grokking Functional Programming

Michal Plachta
Grokking Algorithms

Grokking Algorithms

Aditya Bhargava
The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

Steve Klabnik, Carol Nichols

Publisher Resources

ISBN: 9798888650400Errata Page