Free theorems were popularized by Wadler in his 1989 paper "Theorems for Free!". In that paper only polymorphism over types is considered. In 2009, Voigtländer illustrates how this can be extended to polymorphism over type constructors with class constraints in his paper "Free Theorems Involving Type Constructor Classes". He uses polymorphism over Monads for his example. In order to learn to apply this theory I will use a very simple exercise only involving Functors. Be aware that our reasoning will be "fast and loose" which is a technical term that means that our results only apply to total values.

The exercise here is to prove that the type A → B and the polymorphic type ∀ κ :Functor. κ A → κ B are isomorphic. The intuition is that all a polymorphic function of type ∀ κ :Functor. κ A → κ B can do is map some internally held function of type A → B . The claim is that the following pair of functions witness the isomorphism:

iso 1 : ( A → B ) → ∀ κ :Functor. κ A → κ B

iso 1 x := Λ κ . λ l . map κ x l

iso 2 : (∀ κ :Functor. κ A → κ B ) → A → B

iso 2 y := y Id

In the above we use Id, which is the identity functor.

To prove this we need to show that both ways of composing these two functions produce the identity function. One way is easy:

iso 2 (iso 1 x ) a =

map Id x a =

x a

So we see that iso 2 (iso 1 x ) = x as required. The other direction is harder.

iso 1 (iso 2 y ) κ l =

map κ ( y Id) l

At this point we get stuck. We need to show that map κ ( y Id) l = y κ l for an arbitrary functor κ and arbitrary l : κ A . To continue the proof, we are going to have to use the parametricity of y : ∀ κ :Functor. κ A → κ B .

Parametricity of y gives us a free theorem which states that given two functors F 1 and F 2 and a functor morphism η : ∀ α . F 1 α → F 2 α we have that:

η B ∘ y F 1 = y F 2 ∘ η A

A functor morphism is better known as a natural transformation. A natural transformation η : ∀ α . F 1 α → F 2 α is required to satisfy the following law: For any types C and D and any function g : C → D , we require that,

map F 2 g ∘ η C = η D ∘ map F 1 g

My proof will proceed in two stages. The first lemma I will prove is:

Lemma 1 y Id = y (Env A ) id A

Here Env A is the environment functor (it is also a monad) aka the reader functor, and id is the identity function. To prove our lemma it suffices to show that y Id a = y (Env A ) id a for an arbitrary a : A. We will use the free theorem to prove this with a natural transformation from (Env A ) to Id defined as

η 1 : ∀ α . Env A α → Id α

η 1 := Λ α . λ h . h a

However, we must first prove that this is a natural transformation; i.e. we need to prove that map Id g ∘ η 1 C = η 1 D ∘ map (Env A ) g :

map Id g ( η 1 C h ) =

g ( h a ) =

( g ∘ h ) a =

η 1 D (map (Env A ) g h )

Now we can apply our free theorem using the natural transformation &eta 1 to prove the lemma.

y Id a =

y Id ( η 1 A id A ) =

η 1 B ( y (Env A ) id A ) =

y (Env A ) id A a

This completes Lemma 1. To finish our proof of the isomorphism we only need to prove Lemma 2:

Lemma 2 map κ ( y (Env A ) id A ) l = y κ l

To prove this we use the free theorem with a new natural transformation from Env A to κ :

η 2 : ∀ α . Env A α → κ α

η 2 := Λ α . λ h . (map κ h l )

Again, we need to show that η 2 is a natural transformation, meaning we need to show that map κ g ∘ η 2 C = η 2 D ∘ map (Env A ) g .

map κ g ( η 2 C h ) =

map κ g (map κ h l ) =

map κ ( g ∘ h ) l =

η 2 D (map (Env A ) g h )

Now we can apply our free theorem using the natural transformation η 2 to prove Lemma 2.

map κ ( y (Env A ) id A ) l =

η 2 B ( y (Env A ) id A ) =

y κ ( η 2 A id A ) =

y κ (map κ id A l) =

y κ l

This completes the proof of the isomorphism. Perhaps it can be simplified by combining the two step into one, but I am satisfied with this two step process.