Haskell functors, applicatives, and monads are more than just the functions given by their type classes. Each of these type classes also come with a set of laws that govern what the implementations should look like. While “laws” might sound rigid and formal, in reality, understanding these laws can be very helpful because they can often lead you to better understanding of how to create instances of these type classes in the first place. Many of these laws originated in pure mathematics, but you don’t need to have a strong math background to follow this section. We’ll walk through all of the laws purely using what you’ve learned about Haskell so far in this book. If you encounter unfamiliar math-y sounding terminology, don’t worry about it too much. You can make a quick note of any unfamiliar terms and look them up later if you like. Understanding the terminology used in this section will be a little bit helpful in deepening your understanding, but a lack of familiarity shouldn’t impede your ability to follow along.

There’s nothing in the language that will give you an error if you violate these laws, and sometimes libraries do, so it’s good to think of them more like guidelines that can lead you toward writing better code. Following the laws for functors, applicatives, and monads will make it much easier for you to reason about what your code is doing and will allow you to make use of all of the different functions that work with those type classes. In this section, we’ll go over the laws that govern functors, monads, and applicatives. This section will be useful not only to help you understand the rules that govern how common types like IO work, but also to help you get practice in reading and understanding some of the more formal looking parts of the standard library documentation. If you find yourself having trouble with this section, bookmark it and come back to it after you’ve finished a few more chapters, and read through it again.