The last of the type class laws that we’ll look at are the laws for Applicative . Applicative has four laws, and they are a bit trickier than the laws for Functor and Monad . In particular, the names of the laws might sounds a bit more intimidating, but as we’ve seen in the last two sets of laws, we can follow the code and write tests to get a feel for how our particular instances are holding up to the laws without having to delve too deeply into the underlying mathematical theory. Let’s start with the applicative laws as they are documented in the standard library:

​  ​ -- Identity ​
​  pure id <*> v = v
​  ​ -- Composition ​
​  pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
​  ​ -- Homomorphism ​
​  pure f <*> pure x = pure (f x)
​  ​ -- Interchange ​
​  u <*> pure y = pure ($ y) <*> u

The applicative identity law is pretty much like the identity laws for monads and functors. Since we’ve seen two similar examples already, you should be able to work through this example yourself.

The composition law of applicatives looks a little bit more complicated than the composition law for functors, but it’s actually pretty similar. Let’s take a minute to unpack it and try to really understand what’s happening.

Recall that the type for <*> is Applicative f => f (a -> b) -> f a -> f b , and the type for . is (.) :: (b -> c) -> (a --> b) -> a -> c . So when we say pure (.) we’re getting back something like: f ((b -> c) -> (a -> b) -> a -> c) . That means that the u and v in our law need to be functions. So we could rewrite our law so it looks like this:

​  pure ((b -> c) -> (a ->b) -> a -> c)
​  <*> f (b -> c)
​  <*> f (a -> b)
​  <*> f a
​  == f (b -> c)
​  <*> (f (a -> b) <*> f a)

Wow! That’s a lot of arrows! Let’s see if we can simplify this a little bit more so it’s easier to parse.

The first part of this law is that we’re composing two functions inside of our applicative. We’ve got two functions to start with:

​  u :: f (b -> c)
​  v :: f (a -> b)

And we have a single value:

​  w :: f a

We want to get out a value with the type f c . The most obvious way we might do that is to first call v on w with <*> :

​  w' :: f b
​  w' = v <*> w

Then we can call u on w’ :

​  result :: f c
​  result = u <*> w'

If we write it all together as a single expression we get:

​  result :: f c
​  result = u <*> (v <*> w)

If we were dealing with regular functions instead of applicatives, we could write this quite simply:

​  result :: c
​  result = g (f a)

And if we were writing it with functors we might say:

​  result :: f c
​  result = fmap g . fmap f

We might naturally think that it would be nice to compose functions with . instead of calling them one after another. Since we’re working inside of an applicative, we’ll start by lifting composition into our applicative:

​  apCompose :: f ((b -> c) -> (a -> b) -> a -> c)
​  apCompose = pure (.)

Now we can compose our two applicative functions. We start with u :

​  u' :: f ((a -> b) -> a -> c)
​  u' = apCompose <*> u

And next we bring in v :

​  v' :: f (a -> c)
​  v' = u' <*> v

And finally we can apply our newly composed function to get a value:

​  result :: f c
​  result = v' <*> w

If we write all that as a single expression, we get the left-hand side of our composition law:

​  result :: f c
​  result = pure (.) <*> u <*> v <*> w

So our applicative law of composition says that no matter which way we want to go about trying to apply our functions, we should always get the same value back out.

The next applicative law is the law of homomorphism . Homomorphism is one of those words that can sometimes make Haskell sound scary and overly formal, but we’re not going to spend any time on formally defining it. Instead, we’ll just look at the law and show how it follows from the intuition that we’ve built up around how some of these laws work. The law of homomorphism says that:

​  pure f <*> pure x = pure (f x)

To put it in English: lifting a function and a value into our applicative with pure and then applying the function with <*> should give us the same value as just lifting the result of calling the function on the value. This is another law that formalizes the notion that <*> really shouldn’t be doing much except for applying the mapping, and that pure should be giving us some sort of reasonable initial value.

We can use lists as a good example of how this works. Let’s look at an example:

​  ​ λ ​ pure succ <*> pure 1 :: [​ Int ​]
​  [2]
​  ​ -- This is the same as if we'd said ​
​  ​ λ ​ [succ] <*> [1]
​  [2]
​  ​ -- Our monad law says this should be the same ​
​  ​ λ ​ (pure succ <*> pure 1 :: [​ Int ​]) == (pure (succ 1))
​  ​ True ​
​  ​ -- The same thing words with Maybe Int too ​
​  ​ λ ​ pure succ <*> pure 1 :: ​ Maybe ​ ​ Int ​
​  ​ Just ​ 2
​  ​ λ ​ (pure succ <*> pure 1 :: ​ Maybe ​ ​ Int ​) == (pure (succ 1))
​  ​ True ​

These examples show that well-behaved applicatives follow the law of homomorphism, and how following these laws makes our code easy to reason about. Thanks to this law, we can either lift a function and its argument using pure before applying them with (<*>) , or we can use ordinary function application and lift the result with pure afterwards.

The last applicative law is the law of interchange . Interchange is related to commutivity, and the law of interchange provides a way that we can sort of flip the operands of <*> in a predictable way. The law is given as:

​  u <*> pure y = pure ($ y) <*> u

The left-hand side of the law is the usual application with applicatives. The right-hand side might look a bit weird, so let’s break it down.

We’ll start using the left-hand side to fill in some things that we know:

​  result :: f b
​  result = u <*> pure y
​  
​  u :: f (a -> b)
​  y :: a
​  pure y :: f a

So, we know that the right-hand side needs to evaluate to a type of f b :

​  result :: f b
​  result = pure ($ y) <*> u

Let’s take a minute to recall that the type of $ is ($) :: (a -> b) -> a -> b . Since we already know y :: a , then we know that the expression ($ y) must have the type (a -> b) -> b . When we lift that into our applicative with pure we get:

​  pure ($ y) :: f ((a -> b) -> b)

When we apply u to that expression with <*> we’re effectively applying a value of type f (a -> b) to a function of type f ((a -> b) -> b) , giving us a result value of f b .

Let’s look back at the law one more time now that we’ve walked through all of the parts, and then we’ll look at a simple example of it in practice:

​  u <*> pure y = pure ($ y) <*> u

We can demonstrate this law with Maybe :

​  ​ λ ​ ​ let ​ u = pure succ :: ​ Maybe ​ (​ Int ​ -> ​ Int ​)
​  ​ λ ​ u <*> pure 3
​  ​ Just ​ 4
​  ​ λ ​ pure ($ 3) <*> u
​  ​ Just ​ 4
​  ​ λ ​ (u <*> pure 4) == (pure ($ 4) <*> u)
​  ​ True ​

The law of interchange allows us to compose functions in different ways while still being able to reason effectively about the results.

The functor, monad, and applicative laws will make it easier for you to figure out how to write instances for your own types. In fact, you may find that frequently there’s only one implementation that actually follows all of the laws. As you spend more time working with these type classes, having an understanding of the underlying laws will also make it easier to reason about what your code is doing. As you’re working through that section, and the rest of this chapter, keep an eye out for how code we write and decisions that we make are influenced by these laws.