Previously, I had only a passing familiarity with the idea that “Theorems for free!” can be internalized in dependent type theory. In April, Naïm Favier and Dan Doel introduced me to Jem Lord’s “Easy Parametricity”. While Lord’s paper itself is somewhat over my head, the result is actually pretty easy to put into practice. Let us look at some applications of Lord’s work.
The easiest way to characterize parametricity internally is through an axiom stating that there are only constant functions from a universe of types to one of its types.
For lack of a better name, I will call this postulate c.
postulate
c : ∀ {A : U} (f : U → A) (X Y : U) → f X ≡ f Y
Lord uses a different formulation of parametricity, which states that the function space U → A is equivalent to A in the obvious way.
This can be derived from postulate c, as Lord notes.
jem : ∀ {A : U} → isEquiv (λ (a : A) (X : U) → a)
jem = isoToIsEquiv (iso _ (λ b → b 𝟘) (λ b → funExt λ P → c b 𝟘 P) λ x → refl)
As Lord also notes, we can do the reverse and derive postulate c from jem.
centre : ∀ {A : U} → (f : U → A) → (X Y : U) → f X ≡ f Y
centre f X Y = sym (funExt⁻ jem-ε X) ∙ funExt⁻ jem-ε Y
where
jem-ε : (λ X → jem .equiv-proof f .fst .fst) ≡ f
jem-ε = jem .equiv-proof f .fst .snd
One advantage of jem is that isEquiv is a homotopy proposition.
Since jem is a proposition, the version of postulate c we derived from it must be canonical.
This is why I am calling it centre.
On the other hand, the statement of postulate c does not appear to be a homotopy proposition at first glance.
If A is a higher type, it seems plausible that our arbitrarily chosen postulate c could include non-trivial loops in the resulting value of f X ≡ f Y.
We will come back to this.
The surprise for me was that this relatively trivial aspect of parametricity is actually adequate to derive all the usual parametricity results I am interested in.
The general form of a free theorem is that you have some polymorphic function α : ∀ X : U → … and some particular function f : A → B, and you want to “pass f through α”, which equates a term containing α A with a corresponding term containing α B.
To derive this from our postulate c, we use a construction called the wide pullback.
WPB : ∀ {A B : U} (f : A → B) → U → U
WPB f P = Σ[ x ∈ B ] (P → Σ[ y ∈ A ] f y ≡ x)
This is a pullback of P‐many copies of f.
While it is important that this construction is polymorphic in P, there are only two particular instances we care about:
WPB f 𝟘 ≡ B and WPB f 𝟙 ≡ A.
WPB f 𝟘 ≡ Σ[ x ∈ B ] (𝟘 → Σ[ y ∈ A ] f y ≡ x) ≃ Σ[ x ∈ B ] 𝟙 ≃ B
WPB f 𝟙 ≡ Σ[ x ∈ B ] (𝟙 → Σ[ y ∈ A ] f y ≡ x) ≃ Σ[ y ∈ A ] Σ[ x ∈ B ] f y ≡ x ≃ Σ[ y ∈ A ] 𝟙 ≃ A
By univalence, these equivalences are all identities.
There is a generic function into WPB f P from A:
η : ∀ {P A B : U} {f : A → B} → A → WPB f P
η {P} {f} a = ⟨ f a , λ _ → ⟨ a , refl ⟩ ⟩
Similarly, the first projection is a generic function out of WPB f P to B:
ε : ∀ {P A B : U} {f : A → B} → WPB f P → B
ε {P} {f} ⟨ b , _ ⟩ = b
η {𝟘} is f when WPB f 𝟘 is B, and η {𝟙} is id when WPB f 𝟙 is A.
On the flip side, ε {𝟘} is id when WPB f 𝟘 is B, and ε {𝟙} is f when WPB f 𝟙 is A.
The idea is to compose WPB f with α to get λ P → α (WPB f P), and then compose that with suitable uses of η and ε to get a term whose type is independent of P.
For example, if α : ∀ X : U → [ X ] → [ X ] is reverse or tail, then λ P → map ε ∘ α (WPB f P) ∘ map η : U → [ A ] → [ B ].
The resulting function from U must be constant by postulate c.
Using heterogeneous substitution, one can prove that this function applied to 𝟘 is identical to one side of the free theorem:
map id ∘ α B ∘ map f.
When applied to 𝟙, it is identical to the other side: map f ∘ α A ∘ map id.
Because this function is constant, the two sides must be equal.
Note that univalence was used to provide the type equalities needed for the heterogeneous substitution.
Doel proves a bunch of the usual suspects.
For example, he proves that the polymorphic function (R : U) → R → R is always the identity.
My own interest in parametricity lies in proving the Van Laarhoven representation theorems for lenses and other optics. Doel proves the representation theorem for grates:
wat-equiv : ∀ {A B S T : U}
→ ((F : Func) → (F ⟨ A ⟩ → B) → (F ⟨ S ⟩ → T))
≃ (((S → A) → B) → T)
Notice that in this development, there is no functor composition law stating fmap F (g ∘ f) ≡ (fmap F g ∘ fmap F f).
Instead of a law, it is a theorem derived from parametricity.
One notable result for our purposes is that a parametric function of type ∀ (R : U) (x : R) → x ≡ x must be reflexivity.
This is relevant because the statement of postulate c, when applied to the constant function, has this exact shape.
Therefore, by parametricity itself, c (λ _ → a) X Y ≡ refl.
Furthermore, again by parametricity, every function f : U → A is a constant function.
The gist of it all is that by parametricity, we can prove our arbitrarily chosen postulate c is actually identical to the canonical centre derived from the jem proposition.
This means that the statement of postulate c is itself a homotopy proposition, and thus it is equivalent to Lord’s parametricity proposition.
Regardless of your preferred formulation of the parametricity proposition, it is quite amazing that I can derive all my favourite parametricity results as actual theorems from it. Of course, even better than a parametricity postulate would be giving it a computational interpretation, as was done with univalence. As far as I understand, there are systems proposed out there that realize this, and it is simply a matter of proving these systems have all the metatheoretical niceties one demands from a type theory.
Before closing, I should note that this parametricity postulate contradicts various forms of the law of the excluded middle.
LEM more or less states that there is a function oracle : ∀ (P : U) → isHProp P → 𝟚 that can decide whether a given proposition is true or false.
This, in turn, allows us to decide for any type A whether A is uninhabited, which is a proposition: λ A → oracle (¬ A) (…) : U → 𝟚.
However, parametricity says a function of that type must be constant, and thus cannot decide anything.
I think a fully normalizing type theory with internal parametricity is far more valuable than a non-computational type theory with an undecidable excluded middle axiom, and this is a sign that LEM is not the way to go. Besides, classical mathematics is already better implemented using double‐negated logical connectives, letting you mix and match classical and constructive connectives as needed.