Lesson 9: Property testing

Contents
  • Generating products and sums
  • Identity
    • Figuring out the type
    • Type ambiguity
    • Eq and Show
    • Integers, and lifted integers
  • Homomorphism
    • Function-like types
    • Ambiguities abound
    • Generating functions
  • Composition
    • Working out three types
    • Lifting generators, generally
    • A type of a higher rank
    • The miscellaneous constraints
  • Interchange
  • Putting it all together

We had a short introduction to one of the Applicative class laws in the previous lesson. But the Applicative class has quite a few laws, so there is still a lot to cover here.

The Applicative laws are stated as follows:

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

This lesson is dedicated to the testing machinery and how to transcribe the laws into code. At the end we will have a function that, given a generator for Applicative-lifted values, will tell us whether each of the four laws is satisfied. Once we have the testing set up, the next lesson will experiment with them and further explore what the properties mean.

We’ll be using the property testing library hedgehog to ensure we write good, useful, law-abiding Applicative instances. There are some new things that will make this a bit more difficult than the other Hedgehog properties we’ve written so far. There are more laws and more types of generators required than before. A new challenge in this lesson is that some of these laws ask us to generate functions.

Sign up for access to the full page, plus the complete archive and all the latest content.