# Parametricity, type equality, and higher-order polymorphism

## Summary (3 min read)

### 1 Type equivalence, isomorphism and equality

- Type equivalence propositions assert that two types are isomorphic.
- Type equivalence and type equality propositions may be used for many of the same applications, but there are subtle differences between them.
- One element of type EQUIV (Int, Bool) (Bool, Int) is two copies of a function that swaps the components of a pair.
- The authors derive these free theorems from the parametricity theorem for a language called Rω (Crary etal., 2002), which extends Girard’s Whereas free theorems for second-order polymorphism quantify over arbitrary relations, they are often instantiated with (the graphs of) functions expressible in the polymorphic λ-calculus (Wadler, 1989).

### 1.1 Contributions.

- The primary contribution of this paper is the correctness of the equality type, which implies correctness properties of a type-safe cast operation that can produce it.
- In addition, the authors use their framework to prove correctness for another equality proposition, which defines type equality as the smallest reflexive relation.
- This paper offers a fully explicit and accessible roadmap to the proof of parametricity for higher-order polymorphism, using the technique of syntactic logical relations,1 and insisting on rigorous definitions.
- Rigorous definitions are not only challenging to get right but important in practice, since their examples demonstrate that the “power” of the meta-logical functions involved in instantiating the free theorems determines the expressiveness of these free theorems.
- Because of their attention to formal details, their development is particularly wellsuited for mechanical verification in proof assistants based on Type Theory (the meta-logic of choice in this paper), such as Coq (http://coq.inria.fr).

### 2 Constructing equivalence and equality types

- In this section the authors give an informal introduction to Rω. Although they use Haskell syntax throughout the section (and all of the code is valid Haskell) their examples are intended to demonstrate Rω programming.
- The important property of datatype R a is that the type index a changes with the data constructor.
- Weirich (2004) defined two different versions of type-safe cast, cast and gcast, shown in Figure 1.
- The authors implementations differ slightly from Weirich’s—namely they use Haskell’s Maybe type to account for potential failure, instead of an error primitive—but the essential structure is the same.
- Again, the case for products has been decorated with type annotations—the first recursive call changes the type of the first component of the product, the second recursive call changes the type of the second component.

### 3.1 The Rω calculus.

- Rω includes type representations Rint, R, R×, R+, and R→which must be fully applied to their arguments.
- Each of these judgments uses a unified environment, Γ, containing bindings for type variables (a:κ) and term variables (x:τ).
- The rest of this typing relation is standard.
- In addition, the authors implicitly identify α-equivalent types, and treat them as syntactically equal in the rest of the paper.

### 3.2 Definition [(Typed-)Generalized Relations]:

- GRelκ The notation P(term × term) stands for the space of binary relations on terms of Rω.
- The authors define this predicate mutually with extensional equality on generalized relations (≡κ) and on Typed-Generalized relations (≡).
- Because their wfGRelκ conditions depend on equality for type GRelκ, the authors cannot include those conditions in the definition of GRelκ itself.
- Stating it in the simpler form would require the logic to include propositional extensionality.
- The operation J×K lifts ρ and π to a relation between products such that the first components of the products belong in ρ̂, and the second in π̂.

### Proof

- As their logical relation implies equivalence, the authors show their result by showing that (e, λx.x) ∈.
- Hence, despite the fact that the authors are showing a theorem about an Fω type, they need morphisms at higher kinds to accept both types and morphisms as arguments and dispatch on their type argument—a novel use of type-dispatching interpretations compared to recent work on free theorems for higher-order polymorphic functions (Voigtländer, 2009).
- If the abstraction theorem remained true when this condition was violated then the authors could derive a false statement.
- The first property that the authors show is that if gcast returns a function then the two types that instantiated gcast must be equal.

### 3.3 Behavioral equivalence

- Is contained in a suitable behavioral equivalence relation for closed terms.
- Intuitively, two terms are behaviorally equivalent if all uses of them produce the same result.

### 4.2 Another definition of type equality

- In this section the authors discuss the properties of another proposition that defines type equality as the smallest reflexive relation.
- Equality is the intersection of all such relations.
- The code shown in Figure 10 includes a definition of the REqual gadt (of which REQUAL is the encoding).
- In the product branch, pattern matching on the result of pcast ra0 ra0’ unifies the types a0 and a0’.
- Because of the integration between this equality predicate and the Haskell type checker, a proof of type REqual t1 t2 is often easier to use than one of type EQUAL t1 t2.

### Proof sketch

- Furthermore, the authors can also show that these two definitions of equality are logically equivalent.
- (For clarity, the authors write these terms in a Church-style variant, where all type abstractions and applications are explicit.).
- Furthermore, by Theorems 4.3 and 4.11, the authors know that i and j form an isomorphism between the two equality types.

### 5.1 Injectivity

- Not all properties of type equalities seem to be expressible as Rω or Fω terms.
- But without such capability, such an injection does not seem possible.
- On the other hand, the authors do not know how to show that the type of inj is uninhabited—they cannot assume the existence of a term inj and derive that (inj, inj) ∈ ∅ by using the fundamental theorem as they can for other empty types.
- Some authors propose that the EQUIV type, which can define injectivity, be used instead.
- In particular, the following code typechecks in GHC.

### 5.2 Relational interpretation and contextual equivalence.

- The authors conjecture that for closed values their behavioral equivalence coincides with contextual equivalence.
- In fact the 5 However, see Washburn and Weirich (Washburn & Weirich, 2005) for a related language that does show parametricity in the presence of such a construct.
- Same problem is open even for System F even without any datatypes or representations.
- A potential solution to this problem would involve modifying the clauses of the definition that correspond to sums (such as the J+K and R operations) by >>-closing them as Pitts suggests (Pitts, 2000; Pitts, 2005).

### 5.3 Representations of polymorphic types and non-termination.

- Rω does not include representations of all types for a good reason.
- This example begins by using gcast to enable a self-application term with a concise type.
- This example demonstrates that the authors cannot extend the relational interpretation to Rid and the proof of the abstraction theorem in a straightforward manner as their proof implies termination.
- K·} However, this definition is not well-formed.

### 5.4 Implicit versus explicit generalization and instantiation

- Hence, it is to be expected that the derived free theorems in this paper will only be “morally” true (Danielsson etal., 2006) in a setting with non-termination.
- Therefore, although the language Rω only contains the specific example of the representation type, the parametricity results in this paper could be extended to languages that include arbitrary gadts.
- The easiest gadts to incorporate in this way are those that, like representation types, have inductive structure.
- Such types do not introduce non-termination, so the necessary extensions to the definitions in this paper are localized.
- Crary and Harper (Crary & Harper, 2007a) and Ahmed (Ahmed, 2006a) describe necessary extensions to to support their inclusion.

Did you find this useful? Give us your feedback

##### Citations

77 citations

### Cites background or result from "Parametricity, type equality, and h..."

...• The result is already exposed by Vytiniotis & Weirich (2010), but it is remarkable that its proof is one line long given our framework....

[...]

...We also show that the assumption that every term satisfies the parametricity condition generated by its type is consistent with the generated logic....

[...]

...Recent works (Takeuti 2004, personal communication; Johann & Voigtländer 2006; Neis et al. 2009; Vytiniotis & Weirich 2010) have developed parametricity results for several such calculi, but not in a common framework....

[...]

65 citations

58 citations

52 citations

### Cites background from "Parametricity, type equality, and h..."

...Vytiniotis and Weirich [2009] use parametricity to show that certain casting functions are equivalent to the identity....

[...]

...Vytiniotis and Weirich [2009] show that this theorem can be deduced from the type of gcast alone, by parametricity....

[...]

##### References

904 citations

### "Parametricity, type equality, and h..." refers methods in this paper

...In this paper, we make the previous arguments about type equality rigorous by deriving free theorems (Reynolds 1983; Wadler 1989) about equality types from Reynolds’s abstraction theorem....

[...]

[...]

773 citations

### "Parametricity, type equality, and h..." refers background or methods in this paper

...Reynolds’s abstraction theorem (also referred to as the “parametricity theorem” (Wadler 1989) or the “fundamental theorem” of logical relations) asserts that every well-typed expression of the polymorphic λ-calculus (System F) (Girard 1972) satisfies a property directly derivable from its type....

[...]

...In this paper, we make the previous arguments about type equality rigorous by deriving free theorems (Reynolds 1983; Wadler 1989) about equality types from Reynolds’s abstraction theorem....

[...]

...Whereas free theorems for second-order polymorphism quantify over arbitrary relations, they are often instantiated with (the graphs of) functions expressible in the polymorphic λ-calculus (Wadler 1989)....

[...]

464 citations

### "Parametricity, type equality, and h..." refers background in this paper

...However, in an expressive enough meta-logic, such as CIC (Paulin-Mohring 1993) or ZF set theory, such a definition is indeed well formed, as there exists a type containing both spaces (e.g. Type in CIC (see Appendix 7), or pure ZF sets in ZF set theory)....

[...]

...However, in an expressive enough meta-logic, such as CIC (Paulin-Mohring 1993) or ZF set theory, such a definition is indeed well formed, as there exists a type containing both spaces (e....

[...]

...Our work was developed in CIC instead of untyped set theory, but there are similarities....

[...]

[...]

349 citations

### "Parametricity, type equality, and h..." refers background in this paper

...Equality is a stronger relation than equivalence as it must be substitutive as well as reflexive, symmetric, and transitive (see Kleene 1967, p. 157)....

[...]

347 citations

### "Parametricity, type equality, and h..." refers methods in this paper

...…types may be implemented in Haskell by a Generalized Algebraic Datatype (gadt) called R a, which represents its type index a (Sheard & Pasalic 2004; Jones et al. 2006). data R a where Rint :: R Int Runit :: R () Rprod :: R a -> R b -> R (a,b) Rsum :: R a -> R b -> R (Either a b) Rarr :: R a -> R…...

[...]

...Although native to Rω , representation types may be implemented in Haskell by a Generalized Algebraic Datatype (gadt) called R a, which represents its type index a (Sheard & Pasalic 2004; Jones et al. 2006)....

[...]

##### Related Papers (5)

##### Frequently Asked Questions (2)

###### Q2. What are the future works in "Parametricity, type equality and higher-order polymorphism" ?

If the authors wished to use these results to reason about Haskell implementations of gcast, they must extend their model to include more—in particular, general recursion and recursive types ( Melliès & Vouillon, 2005 ; Johann & Voigtländer, 2004 ; Appel & McAllester, 2001 ; Ahmed, 2006b ; Crary & Harper, 2007a ). The authors believe that the techniques developed here are independent of those for advanced language features. Thanks to Aaron Bohannon, Jeff Vaughan, Steve Zdancewic, and anonymous reviewers for their feedback and suggestions.