Using the Applicative Laws

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)
 

Get Effective Haskell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.