Functors are the simplest of our trio of type classes, and also have the simplest laws, so we’ll start with them. There are two laws that well-behaved functors should adhere to, and they are both documented in the standard library documentation. There are two functor laws, identity and composition :

​  ​ -- Identity ​
​  fmap id = id
​  ​ -- Composition ​
​  fmap (f . g) = fmap f . fmap g

The identity law of functors is pretty simple: mapping the identity function shouldn’t change the value of the functor. The implication is that fmap should only ever be a mapping function and shouldn’t change anything in the functor except for the value that it’s mapping. The second law, composition, follows naturally from the first. The law of composition says that it shouldn’t matter whether we compose calls to fmap , or we fmap a composed function. Put simply, these two laws together state that fmap should “do nothing but simply apply the mapping to the value(s) inside the functor.”

Both our Maybe and List implementations satisfied the functor laws, so let’s look at an example of a Functor instance that violates the laws so that we can get a full picture of what sort of behavior they disallow. We’ll create an Outlaw type that contains a value and a counter. Our outlaw will increment the counter every time we call fmap :

​  ​ data ​ ​ Outlaw ​ a = ​ Outlaw ​ ​ Int ​ a ​ deriving ​ (​ Eq ​, ​ Show ​)
​  
​  ​ instance ​ ​ Functor ​ ​ Outlaw ​ ​ where ​
​  fmap f (​ Outlaw ​ cnt val) = ​ Outlaw ​ (cnt + 1) (f val)

Let’s create a sample outlaw and a couple of helper functions to use when we look at testing our functor laws. We’ll use the toUpper function from Data.Char to let us convert a String to uppercase:

​  bang = (<> ​ "!" ​)
​  upcase = map ​ Data ​.​ Char ​.toUpper
​  billyTheKid = ​ Outlaw ​ 0 ​ "bank robber" ​

Now we can test our our identity law. We’ll write a simple function that should return true if our identity law holds:

​  testIdentity =
​  fmap id billyTheKid == id billyTheKid

If we run this, we’ll see that we actually get back False , because our outlaw is violating the functor laws. We can see why if we run each side of our comparison in ghci :

​  λ testIdentity
​  False
​  λ fmap id billyTheKid
​  Outlaw 1 "bank robber"
​  λ id billyTheKid
​  Outlaw 0 "bank robber"

Since our outlaw’s fmap function has the side effect of changing the counter, our functor law fails to hold. We can see a similar problem when we test the second functor law:

​  testComposition =
​  fmap (bang . upcase) billyTheKid == (fmap bang . fmap upcase $ billyTheKid)
​  ​ λ ​ testComposition
​  ​ False ​
​  ​ λ ​ fmap (bang . upcase) billyTheKid
​  ​ Outlaw ​ 1 ​ "BANK ROBBER!" ​
​  ​ λ ​ fmap bang . fmap upcase $ billyTheKid
​  ​ Outlaw ​ 2 ​ "BANK ROBBER!" ​

Just like the case with our identity function test, the side effect of incrementing a counter causes us to get back a different value when we compose the functions before mapping compared to composing the calls to fmap .

The functor laws are useful guidelines that help us write programs that behave consistently and are easy to reason about. Whenever you’re creating an instance of a type class like Functor , Applicative , or Monad , it’s helpful to encode the laws as unit tests so that you can make sure that you’ve written an instance that adheres to the laws. In the rest of this section, we’re going to look at the laws for monads and applicatives. As we go along, try to write some unit tests to ensure that your List instances for these type classes adhere to the laws.