# Comprehensive Parametric Polymorphism: Categorical Models and Type Theory

• Conference paper
• 1175 Accesses

Part of the Lecture Notes in Computer Science book series (LNTCS,volume 9634)

## Abstract

This paper combines reflexive-graph-category structure for relational parametricity with fibrational models of impredicative polymorphism. To achieve this, we modify the definition of fibrational model of impredicative polymorphism by adding one further ingredient to the structure: comprehension in the sense of Lawvere. Our main result is that such comprehensive models, once further endowed with reflexive-graph-category structure, enjoy the expected consequences of parametricity. This is proved using a type-theoretic presentation of the category-theoretic structure, within which the desired consequences of parametricity are derived. The formalisation requires new techniques because equality relations are not available, and standard arguments that exploit equality need to be reworked.

### Keywords

• Relational Parametricity
• Type Theory
• Term Variable
• Identity Relation
• Relational Context

These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.

## 1 Introduction

According to Strachey , a polymorphic program is parametric if it applies the same uniform algorithm at all instantiations of its type parameters. Reynolds  proposed relational parametricity as a mathematical model of parametric polymorphism. Relational parametricity is a powerful mathematical tool with many useful consequences; see [13, 21, 27] for numerous examples.

The polymorphic lambda-calculus, $$\lambda \mathbf {2}$$, (a.k.a. System F) was introduced independently by Girard  and Reynolds . It serves as a model type theory for (impredicative) polymorphism, and thus provides a significant testing ground for ideas on relational parametricity. In this paper we address the question:

What is the fundamental category-theoretic structure needed to model relational parametricity for $$\lambda \mathbf {2}$$ , which is both (i) minimal, in assuming as little structure as possible; but (ii) strong enough to ensure the expected consequences of parametricity hold?

It is perhaps surprising that this question does not yet have an established answer. On the one hand, category-theoretic models for $$\lambda \mathbf {2}$$ were developed many years ago by Seely . They are studied systematically as $$\lambda \mathbf {2}$$ fibrations in Jacobs . On the other, the fundamental category-theoretic structure needed to model relational parametricity is also known. The crucial ingredient is the notion of reflexive graph category which appeared implicitly in Ma and Reynolds , was used explicitly by O’Hearn and Tennent , and Robinson and Rosolini , and reached maturity in the parametricity graphs of Dunphy and Reddy [7, 8].

To obtain minimal structure for relational parametricity for $$\lambda \mathbf {2}$$, it is natural to combine the structure of $$\lambda \mathbf {2}$$ fibrations with that of parametricity graphs. This results in the notion of $$\lambda \mathbf {2}$$ parametricity graph, which we define in Sect. 3. Sadly, $$\lambda \mathbf {2}$$ parametricity graphs enjoy the expected properties of parametricity only in the special case that the underlying category is well-pointed. (Similar observations, for different but related notions of model, are made in .) Since well-pointedness rules out many categories of interest in semantics (e.g., functor categories) this limits the generality of the theory.

One way of circumventing the restriction to well-pointed categories was proposed by Birkedal and Møgelberg , who developed a more elaborate category-theoretic structure, which overcomes the limitation by modelling Plotkin and Abadi’s logic for parametricity . This method of modelling the combination of $$\lambda \mathbf {2}$$ with an extraneous logic has been refined and simplified by Hermida . Nonetheless, it does not enjoy the simplicity in conception of combining the structure of category-theoretic models of $$\lambda \mathbf {2}$$ with that of parametricity graphs.

To obtain our minimal structure, we retain the original idea of combining parametricity graphs with category-theoretic models of $$\lambda \mathbf {2}$$. However, we implement this in a perhaps unexpected way. We modify the notion of $$\lambda \mathbf {2}$$ model. We ask for $$\lambda \mathbf {2}$$ fibrations to additionally satisfy Lawvere’s comprehension property. Not only are the resulting comprehensive $$\lambda \mathbf {2}$$ fibrations natural in their own right as models of $$\lambda \mathbf {2}$$, but, when combined with parametricity-graph structure to form comprehensive $$\lambda \mathbf {2}$$ parametricity graphs, they do indeed enjoy all expected consequences of parametricity.

Sections 2 and 3 define comprehensive $$\lambda \mathbf {2}$$ fibrations and comprehensive $$\lambda \mathbf {2}$$ parametricity graphs respectively. In Sect. 4, we present a type theory $$\lambda \mathbf {2 R}$$, corresponding to our category-theoretic structure, which provides a simple system for reasoning about parametricity. The type theory $$\lambda \mathbf {2 R}$$ is similar to Dunphy’s System P , and Abadi, Cardelli and Curien’s System R , to which it is compared in Sect. 7.

In Sect. 5, we develop the technical machinery needed to reason in $$\lambda \mathbf {2 R}$$. A key obstacle is that the system does not include equality relations. This means that graph relations, which are a crucial ingredient in standard arguments involving relational parametricity, are not in general definable. In Sect. 5, we instead identify two forms of pseudograph relations, whose subtle interrelationship allows us to establish the consequences we need. One kind of pseudograph relation is immediately definable using the fibrational structure built into the notion of parametricity graph. The other type of pseudograph requires opfibrational structure. We use an impredicative encoding to show that opfibrational structure is definable in $$\lambda \mathbf {2 R}$$, and hence always present in comprehensive $$\lambda \mathbf {2}$$ parametricity graphs. In Sect. 6, we finally apply the technical machinery and establish that the expected consequences of relational parametricity are indeed derivable in $$\lambda \mathbf {2 R}$$, and hence hold in comprehensive $$\lambda \mathbf {2}$$ parametricity graphs.

In summary, the main contributions of this work are:

1. (i)

The definition of comprehensive $$\lambda \mathbf {2}$$ fibrations as models of $$\lambda \mathbf {2}$$.

2. (ii)

The definition of a new category-theoretic notion of model of relational parametricity, obtained by combining parametricity graphs and comprehensive $$\lambda \mathbf {2}$$ fibrations into comprehensive $$\lambda \mathbf {2}$$ parametricity graphs.

3. (iii)

The extraction of $$\lambda \mathbf {2 R}$$ as the type theory intrinsic to comprehensive $$\lambda \mathbf {2}$$ parametricity graphs.

4. (iv)

The derivation of the expected consequences of parametricity in $$\lambda \mathbf {2 R}$$, and hence in comprehensive $$\lambda \mathbf {2}$$ parametricity graphs. This requires novel techniques: establishing the opfibration property of comprehensive $$\lambda \mathbf {2}$$ parametricity graphs, and the use of pseudograph relations.

In the category-theoretic parts of the paper, we assume familiarity with fibred category theory, for which Jacobs  is our main reference. Nevertheless, a substantial portion of the paper is presented in purely type-theoretic terms, and may be read without reference to the accompanying category-theoretic material.

## 2 Comprehensive $$\lambda \mathbf {2}$$ Fibrations

In Fig. 1, we recall the polymorphic $$\lambda$$-calculus $$\lambda \mathbf {2}$$. We use $$x,y,\dots$$ to range over term variables, and $$\alpha , \beta , \dots$$ to range over type variables. Our presentation has four judgements: , stating that $$\varGamma$$ is a well-formed context; , stating that A is a well-formed type in context $$\varGamma$$; , stating that the term t has type A in context $$\varGamma$$; and judgemental equality . We assume $$\beta$$ and $$\eta$$-equalities for both term abstraction, $$\lambda$$, and type abstraction, $$\varLambda$$. Equality is also assumed to be a congruence relation, although the rules guaranteeing this have been omitted from Fig. 1 for brevity.

A minor departure from many presentations of $$\lambda \mathbf {2}$$ is that we interleave type variables and term variables in a single context. This approach is not only natural, but indeed standard when $$\lambda \mathbf {2}$$ is considered in the context of dependent type theory; for example, when derived as an instance of a pure type system . Since there is no dependency of $$\lambda \mathbf {2}$$ types on term variables, such interleaving is syntactically vacuous. Nevertheless, we shall see below that its presence does have semantic implications.

We next recall the standard category-theoretic notion of $$\lambda \mathbf {2}$$ fibration, which models $$\lambda \mathbf {2}$$. We directly restrict the definition to the split case to circumvent coherence issues that would otherwise arise, cf. .

### Definition 1

( $$\lambda \mathbf {2}$$ fibration). A $$\lambda \mathbf {2}$$ fibration is a split fibration $$p: \mathbb {T }\rightarrow \mathbb {C}$$, where the base category $$\mathbb {C}$$ has finite products, and the fibration:

1. (i)

is fibred cartesian closed;

2. (ii)

has a split generic object U [15, Definition 5.2.1] — we write $$\varOmega$$ for $$p\, U$$;

3. (iii)

and has fibred-products along projections in $$\mathbb {C}$$.

Moreover, the reindexing functors given by the splitting are required to preserve the above-specified structure in fibres on the nose.

The above definition differs slightly from [15, Definition 8.4.3(b)] in that we do not include fibred coproducts in condition (iii). These are not needed to model $$\lambda \mathbf {2}$$, and their existence is anyway derivable in parametric models.

In a $$\lambda \mathbf {2}$$ fibration, we write $$\mathbb {T}_X$$ for the fibre category over X. We also use X as a subscript when referring to structure in $$\mathbb {T}_X$$; e.g., $$\mathbf {1}_X$$ is the specified terminal object in $$\mathbb {T}_X$$, and $$\Rightarrow _X$$ is the exponential structure in $$\mathbb {T}_X$$. Given in $$\mathbb {C}$$, we write $$f^*$$ for the reindexing functor $$\mathbb {T}_Y \rightarrow \mathbb {T}_X$$, and for the specified cartesian lifting of f relative to A. We also write $$\prod _\varOmega$$ for the specified right adjoint, given by (iii), to reindexing functors $$\pi _1^* : \mathbb {T}_X \rightarrow \mathbb {T}_{X \times \varOmega }$$.

We recall in outline the semantic interpretation of $$\lambda \mathbf {2}$$ in a $$\lambda \mathbf {2}$$ fibration $$\mathbb {T }\rightarrow \mathbb {C}$$. A context $$\varTheta = \alpha _1, \dots , \alpha _n$$ of type variables is interpreted as the n-fold product $$\llbracket \varTheta \rrbracket = \varOmega ^n$$ in $$\mathbb {C}$$. A type A in type-variable context $$\varTheta$$ is then interpreted as an object $$\llbracket A \rrbracket _\varTheta$$ of $$\mathbb {T}$$ over $$\llbracket \varTheta \rrbracket$$, defined by induction on the structure of A, using cartesian closure for function types, fibred products for universal types, and the reindexing $$(\pi _i)^* \, U$$ of the generic object along the projection to interpret $$\alpha _i$$ over $$\llbracket \varTheta \rrbracket$$. Finally, the interpretation of a term $$\varGamma \vdash t : A$$ is obtained by splitting $$\varGamma$$ into its component contexts: $$\varTheta$$ of type variables, and $$\varDelta$$ of term variables. Then $$\varDelta = x_1\!:\!A_1, \dots , x_m\! : \! A_m$$ is interpreted as the product $$\llbracket \varDelta \rrbracket _\varTheta = \llbracket A_1 \rrbracket _\varTheta \times \dots \times \llbracket A_m \rrbracket _\varTheta$$ in the fibre over $$\llbracket \varTheta \rrbracket$$, and t is interpreted as a morphism .

In the above outline, one sees that the structure of a $$\lambda \mathbf {2}$$ fibration fits uneasily alongside our mixed contexts of interleaved type and term variables, since these have to be separated to define the semantic interpretation. In dependent type theory, where no such separation is possible, a more direct semantic interpretation is achieved using Lawvere’s comprehension property  to model the process of context extension . It is natural to apply the same idea to $$\lambda \mathbf {2}$$.

### Definition 2

(Comprehensive $$\lambda \mathbf {2}$$ fibration). A $$\lambda \mathbf {2}$$ fibration $$p: \mathbb {T }\rightarrow \mathbb {C}$$ is comprehensive if it enjoys the comprehension property [15, Definition 10.4.7]: the terminal-object functor $$X \mapsto \mathbf {1}_X : \mathbb {C} \rightarrow \mathbb {T}$$ has a specified right adjoint $$K : \mathbb {T} \rightarrow ~\mathbb {C}$$.

Requiring a specified right adjoint maintains consistency with our policy of working with split fibrational structure. Given A in $$\mathbb {T}_X$$, we write for the ‘projection’ map obtained by applying p to the counit in $$\mathbb {T}$$.

To show that comprehensive $$\lambda \mathbf {2}$$ fibrations permit a direct, inductive-on-syntax semantic interpretation, we present the interpretation of $$\lambda \mathbf {2}$$ types in detail. A context $${\varGamma }\,\,\mathsf {ctxt}$$ is interpreted as an object $$\llbracket \varGamma \rrbracket$$ of $$\mathbb {C}$$; and a type $$\varGamma \vdash {A}\,\,\mathsf {type}$$ is interpreted as an object $$\llbracket A \rrbracket _\varGamma$$ in $$\mathbb {T}_{\llbracket \varGamma \rrbracket }$$. These are defined by mutual induction, together with maps for every context $$\varGamma$$ containing $$\alpha$$.

\begin{aligned} \llbracket \cdot \rrbracket&= \mathbf {1}&\llbracket {\alpha } \rrbracket _\varGamma&= (\pi ^\alpha _\varGamma )^*\, U&\pi ^\alpha _{{\varGamma },\,{\alpha }}&=\! \pi _2 \\ \llbracket {\varGamma },\,{\alpha } \rrbracket&= \llbracket \varGamma \rrbracket \times \varOmega&\!\llbracket {A \rightarrow B} \rrbracket _\varGamma&= \llbracket A \rrbracket _\varGamma \Rightarrow _{\llbracket \varGamma \rrbracket } \llbracket B \rrbracket _{\varGamma }&\pi ^\alpha _{{\varGamma },\,{\beta }}&=\! \pi ^\alpha _\varGamma \circ \pi _1 ~ (\beta \! \ne \! \alpha ) \\ \llbracket {\varGamma },\,{x : A} \rrbracket&= K \llbracket A \rrbracket _\varGamma&\llbracket \forall {\alpha }.\, A \rrbracket&= {\prod }_\varOmega \, \llbracket A \rrbracket _{{\varGamma },\,{\alpha }}&\pi ^\alpha _{{\varGamma },\,{x : A}}&=\! \pi ^\alpha _\varGamma \circ \kappa _{\llbracket A \rrbracket _\varGamma } \end{aligned}

Having made the above definitions, a term $$\varGamma \vdash t : A$$ is interpreted as a global element in $$\mathbb {T}_{\llbracket \varGamma \rrbracket }$$. The definition, which we omit, is a straightforward induction on the derivation of $$\varGamma \vdash t : A$$.

The appropriateness of comprehensive $$\lambda \mathbf {2}$$ fibrations as a notion of model for $$\lambda \mathbf {2}$$ is supported by soundness and completeness results.

### Theorem 3

(Soundness for $$\lambda \mathbf {2}$$ ). If $$\varGamma \vdash t_1 = t_2 : A$$ then, in every comprehensive $$\lambda \mathbf {2}$$ fibration, we have $$\llbracket t_1 \rrbracket _\varGamma = \llbracket t_2 \rrbracket _\varGamma$$.

### Theorem 4

(Full completeness for $$\lambda \mathbf {2}$$ ). There exists a comprehensive $$\lambda \mathbf {2}$$ fibration satisfying:

1. (i)

for every type $$\varGamma \vdash {A}\,\,\mathsf {type}$$, every global point is the denotation $$\llbracket t \rrbracket _\varGamma$$ of some term $$\varGamma \vdash t : A$$; and

2. (ii)

for all terms $$\varGamma \vdash t_1,t_2 : A$$ satisfying $$\llbracket t_1 \rrbracket _\varGamma = \llbracket t_2 \rrbracket _\varGamma$$, we have $$\varGamma \vdash t_1 = t_2 : A$$.

Theorem 3 is proved by a routine induction on equality derivations, and Theorem 4 by construction of a syntactic model, which has the requisite properties.

## 3 Comprehensive $$\lambda \mathbf {2}$$ Parametricity Graphs

Reflexive graph categories are studied in [7, 8, 16, 19, 24] as a simple category-theoretic structure for modelling relational parametricity. A reflexive graph category consists of a pair of categories, $$\mathbb {V}$$, the vertex category, and $$\mathbb {E}$$, the edge category, together with functors $$\nabla _1, \nabla _2 \,{:}\,\mathbb {E} \rightarrow \mathbb {V}$$ and $$\varDelta \,{:}\,\mathbb {V} \rightarrow \mathbb {E}$$ satisfying $$\nabla _1 \varDelta = \mathrm {id}_{\mathbb {V}} = \nabla _2 \varDelta$$. Informally, one thinks of $$\mathbb {E}$$ as a category whose objects are binary ‘relations’ between objects of $$\mathbb {V}$$. Then $$\nabla _1,\nabla _2$$ are ‘projection’ functors, and $$\varDelta$$ maps an object to its ‘identity relation’.

We shall be guided by the following general thesis. A model of relational parametricity, irrespective of the type theory for which it is considered, should form a reflexive graph category, in the (2-)category of structure-preserving functors between models of the type theory in question. This thesis is supported by the following considerations. Endowing the edge category $$\mathbb {E}$$ with the categorical structure needed to interpret types corresponds to giving types a relational interpretation. The preservation of this structure by the projection functors $$\nabla _1, \nabla _2$$ means that the relational interpretation commutes with the usual non-relational interpretation of types. The preservation of structure by $$\varDelta$$, in combination with the identity property discussed later, corresponds to Reynolds’ identity extension property .

In the context of the present paper, we need to specialise the above recipe to (comprehensive) $$\lambda \mathbf {2}$$ fibrations. A morphism from one (comprehensive) $$\lambda \mathbf {2}$$ fibration $$p' \,{:}\,\mathbb {T}' \rightarrow \mathbb {C}'$$ to another $$p \,{:}\,\mathbb {T} \rightarrow \mathbb {C}$$ is given by a pair of functors, $$H \,{:}\,\mathbb {T}' \rightarrow \mathbb {T}$$ and $$L \,{:}\,\mathbb {C}' \rightarrow \mathbb {C}$$ such that $$p\, H = L \, p'$$, and such that HL preserve all other specified structure (including the choice of cartesian morphisms in the splitting) on the nose. By a reflexive graph of (comprehensive) $$\lambda \mathbf {2}$$ fibrations, we thus mean a pair of (comprehensive) $$\lambda \mathbf {2}$$ fibrations with functors between them:  (1)

where each of the three pairs $$\nabla ^{\mathbb {T}}_1, \nabla ^{\mathbb {C}}_1$$ and $$\nabla ^{\mathbb {T}}_2, \nabla ^{\mathbb {C}}_2$$ and $$\varDelta ^{\mathbb {T}}, \varDelta ^{\mathbb {C}}$$ is a morphism of (comprehensive) $$\lambda \mathbf {2}$$ fibrations, and where each of the triples $$\nabla ^{\mathbb {T}}_1, \nabla ^{\mathbb {T}}_2, \varDelta ^{\mathbb {T}}$$ and $$\nabla ^{\mathbb {C}}_1, \nabla ^{\mathbb {C}}_2, \varDelta ^{\mathbb {C}}$$ is a reflexive graph category. We emphasise that $${p}^{\mathcal {R}} \,{:}\,\mathcal {R}({\mathbb {T}}) \rightarrow \mathcal {R}({\mathbb {C}})$$, in (1), is an arbitrary (comprehensive) $$\lambda \mathbf {2}$$ fibration fitting into the diagram. The notation $$\mathcal {R}({\cdot })$$ is merely mnemonic, and does not imply that $$\mathcal {R}({\mathbb {T}})$$ is obtained using a particular construction from $$\mathbb {T}$$.

One needs to add further conditions to the above structure to ensure that the objects of $$\mathcal {R}({\mathbb {T}})$$ behave sufficiently like relations. In , this was addressed by requiring the fibre category $$\mathcal {R}({\mathbb {T}})_{\mathbf {1}_{\mathcal {R}({\mathbb {C}})}}$$, over the terminal object, to coincide with a particular category of logical relations over $${\mathbb {T}}_{\mathbf {1}_{{\mathbb {C}}}}$$. As well as only being applicable if $${\mathbb {T}}_{\mathbf {1}_{{\mathbb {C}}}}$$ has (sufficient) finite limits, this requirement also has the weakness that it says nothing about other fibres of $${p}^{\mathcal {R}}$$. As a result, the structure is too weak to imply consequences of parametricity in general, see [6, 24] for discussion. To remedy this, we instead need axiomatic structure for a category of relations, in a form that is suitable for being imposed fibrewise on $${p}^{\mathcal {R}}$$. This is provided by Dunphy and Reddy’s notion of parametricity graph [7, 8], which we now recall.

A reflexive graph category $$\nabla _1, \nabla _2 \,{:}\,\mathbb {E} \rightarrow \mathbb {V}, ~ \varDelta \,{:}\,\mathbb {V} \rightarrow \mathbb {E}$$ is said to be relational if the functor $$\langle \nabla _1, \nabla _2 \rangle \,{:}\,\mathbb {E} \rightarrow \mathbb {V} \times \mathbb {V}$$ is faithful. This property allows one to think of morphisms in $$\mathbb {E}$$ as pairs of relation-preserving maps from $$\mathbb {V}$$. Accordingly, we call objects of $$\mathbb {E}$$ relations, we write $$R \,{:}\,A \leftrightarrow B$$ to mean an object R of $$\mathbb {E}$$ with $$\nabla _1 R = A$$ and $$\nabla _2 R = B$$, and we write to mean that there is a (necessarily unique) map in $$\mathbb {E}$$ with $$\nabla _1 h = f$$ and $$\nabla _2 h = g$$. A reflexive graph category satisfies the identity property if, for every in $$\mathbb {E}$$, it holds that $$\nabla _1 h = \nabla _2 h$$. This allows one to think of $$\varDelta A$$ as an identity relation on A (although, cf. Sect. 5 for caveats). In a relational reflexive graph category, the identity property is equivalent to the fullness of the functor $$\varDelta$$. A parametricity graph is a relational reflexive graph category with the identity property, for which the functor $$\langle \nabla _1, \nabla _2 \rangle \,{:}\,\mathbb {E} \rightarrow \mathbb {V} \times \mathbb {V}$$ is a fibration. The fibration property supports the following definition mechanism. Let $$R : A \leftrightarrow B$$ be a relation in $$\mathbb {E}$$. Then, given morphisms and in $$\mathbb {V}$$, reindexing produces an inverse image relation $$[f \!\times \! g]^{-1} R \,{:}\,A' \leftrightarrow B'$$.Footnote 1

The main category-theoretic definition of this paper is a fibrewise adaptation of parametricity graph to the context of comprehensive $$\lambda \mathbf {2}$$ fibrations.

### Definition 5

((Comprehensive) $$\lambda \mathbf {2}$$ parametricity graph). A (comprehensive) $$\lambda \mathbf {2}$$ parametricity graph is a reflexive graph of (comprehensive) $$\lambda \mathbf {2}$$ fibrations, as in (1), that satisfies, for all objects W of $$\mathcal {R}({\mathbb {C}})$$ and X of $$\mathbb {C}$$:

• (Relational) The functor is faithful.

• (Identity property) The functor is full.

• (Fibration) is a cloven fibration.

Moreover, for every in $$\mathcal {R}({\mathbb {C}})$$, we require the commuting square  (where the notation distinguishes reindexing functors determined by p and $${p}^{\mathcal {R}}$$) to give a cleavage-preserving fibred functor from $$\langle \nabla ^{\mathbb {T}}_{1}, \! \nabla ^{\mathbb {T}}_{2} \rangle \! \upharpoonright _{\mathcal {R}({\mathbb {T}})_W}$$ to $$\langle \nabla ^{\mathbb {T}}_1, \! \nabla ^{\mathbb {T}}_2 \rangle \! \upharpoonright _{\mathcal {R}({\mathbb {T}})_{W'}}$$.

This definition could by strengthened by asking for the parametricity-graph fibrations to be split instead of merely cloven. Such a strengthening does not affect any of the results in the sequel, and may seem natural given our use of split fibrations in all previous definitions. Nevertheless, our choice of definition reflects the fact that the weaker cloven assumption is all that is needed to avoid coherence issues arising in the semantic interpretation of the type theory $$\lambda \mathbf {2 R}$$ introduced in Sect. 4 below.

It is Definition 5, with the comprehension property included, that provides our answer to the question highlighted in the introduction. (The definition without comprehension is included for comparison purposes only.)

## 4 A Type System for Relational Reasoning

We define a type system $$\lambda \mathbf {2 R}$$, suggested by the structure of comprehensive $$\lambda \mathbf {2}$$ parametricity graphs. This system is similar, in many respects, to System R of Abadi, Cardelli and Curien  and System P of Dunphy , to which we shall compare it in Sect. 7.

The rules for $$\lambda \mathbf {2 R}$$ are given by Fig. 1 (it extends $$\lambda \mathbf {2}$$) in combination with Fig. 2. The latter adds three new judgements: says that $$\varTheta$$ is a well-defined relational context; says that R is a relation between types $$A_1$$ and $$A_2$$, in relational context $$\varTheta$$; and is a relatedness judgement, asserting that $$t_1 \! : \! A_1$$ is related to $$t_2 \! : \!A_2$$ by the relation R.

Relations, in Fig. 2, are built up from a collection of relation variables $$\rho , \dots$$, which, for clarity, we choose to keep disjoint from type and term variables. In the rules, we make use of three operations $$(\cdot )_1$$, $$(\cdot )_2$$ and $$\langle \cdot \rangle$$, defined in Fig. 3, which implement reflexive graph structure on syntax. The $$(\cdot )_i$$ operations project a relational context to a typing context, whereas the $$\langle \cdot \rangle$$ operation acts in the other direction. In the definition of the latter, we associate a distinct relation variable $$\rho ^\alpha$$ to every type variable $$\alpha$$. Lemma 7 below states how these operations relate typing and relational judgements.

The rules for building relational contexts and relations, in Fig. 2, require some explanation. In adding an assertion $$\alpha \rho \beta$$ to a relational context $$\varTheta$$, all variables $$\alpha , \beta , \rho$$ need to be sufficiently fresh. However, the formulation of $$\lambda \mathbf {2 R}$$ is such that variables on the left-hand side of relations are always manipulated separately from variables on the right. Thus, for example, $$\alpha$$ is sufficiently fresh in $$\alpha \rho \beta$$, as long as $$\alpha$$ does not already occur on the left side $$(\varTheta )_1$$ of $$\varTheta$$. A similar separation principle applies also with respect to the term variables $$x_1,x_2$$ in assertions $$(x_1\!:\!A_1)R(x_2\!:\!A_2)$$. The separation principle means that one needs to be cautious in interpreting assertions of the form $$\alpha \rho \alpha$$ and $$(x\!:\!A)R(x\!:\!A)$$. In such assertions, even though the same variable appears on the left and right, the correct intuition is that these are really two distinct variables. We have chosen not to underline this distinction by requiring the variables to be syntactically different, since doing so would add unnecessary syntactic clutter to the system; for example, it would complicate the definition of the $$\langle \cdot \rangle$$ operation. Instead, we rely on left and right positioning to make the necessary distinctions. This is crucial in the definition of the substitution operations on relations. There are two such operations: $$R[\alpha \rho \beta \mapsto ASB]$$ substitutes, in the relation R, the type A for all left occurrences of $$\alpha$$, the type B for all right occurrences of $$\beta$$ (which may itself be $$\alpha$$), and the relation S for all occurrences of $$\rho$$; similarly, $$S[x \mapsto s, y \mapsto t]$$ substitutes, in the relation S, the term s for all left occurrences of x, and the term t for all right occurrences of y (which may itself be x). Note that relations can indeed contain terms and (hence) type variables, due to the $$[t_1\times t_2]^{-1}R$$ construction, where we consider $$t_1$$ as occurring on the left, and $$t_2$$ on the right.

### Lemma 6

(Substitution lemma)

1. (i)

If $$\varTheta \vdash {A_1RA_2}\,\,\mathsf {rel}$$ and $${\varTheta },\,{\alpha _1\rho \, \alpha _2} \vdash (t_1: B_1)S(t_2: B_2)$$ then $$\varTheta \! \vdash \!(t_1 [\alpha _1\! \mapsto \!A_1]\,{:}\,B_1[\alpha _1\! \mapsto \!A_1])S[\alpha _1\rho \alpha _2 \! \mapsto \! A_1RA_2] (t_2[\alpha _2\! \mapsto \!A_2] \,{:}\,B_2[\alpha _2\! \mapsto \! A_2])$$.

2. (ii)

If $$\varTheta \vdash (t_1: A_1)R(t_2 : A_2)$$ and $${\varTheta },\,{(x_1 : A_1)R(x_2 : A_2)} \vdash (s _1: B_1)S(s_2 : B_2)$$ then $$\varTheta \vdash (s_1[x_1 \mapsto t] : B_1)S[x_1 \mapsto t_1, x_2 \mapsto t_2](s_2[x_2 \mapsto t_2] : B_2)$$.

The relatedness rules of Fig. 2 include the expected rules for relations $$R \rightarrow S$$ and $$\forall {\alpha \rho \beta }.\, R$$, which mimic the analogous type constructions in $$\lambda \mathbf {2}$$. The rules for $$[t_1\times t_2]^{-1}R$$ implement its intended interpretation as an inverse image construction. In addition, a further rule expresses an extensionality principle for relations with respect to judgemental equality. Such an intermixing of relatedness judgements with equality judgements is legitimised by statement (i) of the lemma below.

### Lemma 7

1. (i)

If $$\varTheta \vdash (t_1 : A_1)R(t_2 : A_2)$$ then $$(\varTheta )_i \vdash t_i : A_i$$.

2. (ii)

If $$\varGamma \vdash t : A$$ then $$\langle \varGamma \rangle \,\vdash (t : A)\langle A \rangle (t : A)$$.

Statement (ii) of the lemma asserts that all terms enjoy the characteristic relation-preservation property of relational parametricity. By the extensionality rule, it follows that $$\varGamma \vdash s = t : A$$ implies $$\langle \varGamma \rangle \,\vdash (s : A)\langle A \rangle (t : A)$$. That is, equal terms are parametrically related. Since parametric relatedness captures a form of behavioural equivalence, we can ask also for the converse implication to hold. This is implemented by the parametricity rule in Fig. 2. This rule, in the general form given, is derivable from its empty-context version: $$\vdash (s : A)\langle A \rangle (t : A)$$ implies $$\vdash s = t : A$$. Thus the parametricity rule is equivalent to asking for the relational interpretation of a closed type to act as an identity relation between closed terms—a weak version of Reynold’s identity extension property . We discuss the relational interpretation of open types in Sect. 5.

We outline the semantic interpretation of $$\lambda \mathbf {2 R}$$. Given a comprehensive $$\lambda \mathbf {2}$$ parametricity graph, the contexts, types and terms of $$\lambda \mathbf {2}$$ are interpreted in the comprehensive $$\lambda \mathbf {2}$$ fibration $$p: \mathbb {T} \rightarrow \mathbb {C}$$, as in Sect. 2. In addition, we interpret a relational context $$\varTheta$$ as an object $$\llbracket \varTheta \rrbracket$$ of $$\mathcal {R}({\mathbb {C}})$$, and a syntactic relation $$\varTheta \vdash {ARB}\,\,\mathsf {rel}$$ as a semantic relation $$\llbracket R \rrbracket _\varTheta \,{:}\,\llbracket A \rrbracket _{(\varTheta )_1} \leftrightarrow \llbracket B \rrbracket _{(\varTheta )_2}$$ in $$\mathcal {R}({\mathbb {T}})_{\llbracket \varTheta \rrbracket }$$. The definitions of $$\llbracket \varTheta \rrbracket$$ and $$\llbracket R \rrbracket _\varTheta$$ interpret context extension, function space and universal quantification using the structure of the comprehensive $$\lambda \mathbf {2}$$ fibration $${p}^{\mathcal {R}} : \mathcal {R}({\mathbb {T}}) \rightarrow \mathcal {R}({\mathbb {C}})$$, where relation variables $$\alpha \rho \beta$$ are interpreted using the generic object of $${p}^{\mathcal {R}}$$. For the inverse-image relation $$\varTheta \vdash {A_1([t_1\times t_2]^{-1}R)A_2}\,\,\mathsf {rel}$$, we have that $$\llbracket t_1 \rrbracket _{(\varTheta )_1}$$ and $$\llbracket t_2 \rrbracket _{(\varTheta )_2}$$ determine maps and in $$\mathbb {T}_{\llbracket (\varTheta )_1 \rrbracket }$$ and $$\mathbb {T}_{\llbracket (\varTheta )_2 \rrbracket }$$ respectively. The fibration property of $$\langle \nabla _1^{\mathbb {T}}, \nabla _2^{\mathbb {T}} \rangle \! \upharpoonright _{\mathcal {R}({\mathbb {T}})_{\llbracket \varTheta \rrbracket }}$$ then gives $$\llbracket [t_1\times t_2]^{-1}R \rrbracket : \llbracket A_1 \rrbracket _{(\varTheta )_1} \leftrightarrow \llbracket A_2 \rrbracket _{(\varTheta )_2}$$ as the inverse image of $$\llbracket {R} \rrbracket : \llbracket B_1 \rrbracket _{(\varTheta )_1} \leftrightarrow \llbracket B_2 \rrbracket _{(\varTheta )_2}$$ along these maps.

In the above semantic interpretation, the comprehension property is needed in order to interpret a relational context $$\varTheta$$ as an object $$\llbracket \varTheta \rrbracket$$ of $$\mathbb {\mathcal {R}({C})}$$, and essential use is made of this in the definition of $$\llbracket [t_1\times t_2]^{-1}R \rrbracket$$. Were the comprehension property of models dropped, it would be possible to rejig the semantics to interpret a restricted calculus with inverse-image relations definable only in relational contexts containing no term variables, but not full $$\lambda \mathbf {2 R}$$.

The semantics is supported by soundness and completeness theorems.

### Theorem 8

(Soundness for $$\lambda \mathbf {2 R}$$ ). In every comprehensive $$\lambda \mathbf {2}$$ parametricity graph:

1. (i)

if $$\varGamma \vdash t_1 = t_2 : A$$ then $$\llbracket t_1 \rrbracket _\varGamma = \llbracket t_2 \rrbracket _\varGamma$$; and

2. (ii)

if $$\varTheta \vdash (t_1\! :\! A_1)R(t_2\! : \! A_2)$$ then .

### Theorem 9

(Full completeness for $$\lambda \mathbf {2 R}$$ ). There exists a comprehensive $$\lambda \mathbf {2}$$ parametricity graph satisfying the following.

1. (i)

For every type $$\varGamma \vdash {A}\,\,\mathsf {type}$$, every global point is the denotation $$\llbracket t \rrbracket _\varGamma$$ of some term $$\varGamma \vdash t : A$$.

2. (ii)

For all terms $$\varGamma \vdash t_1,t_2 : A$$ satisfying $$\llbracket t_1 \rrbracket _\varGamma = \llbracket t_2 \rrbracket _\varGamma$$, we have $$\varGamma \vdash t_1 = t_2 :~A$$.

3. (iii)

For every relation $$\varTheta \vdash {A_1RA_2}\,\,\mathsf {type}$$, every global point arises as $$\llbracket t_1 \rrbracket _{({\varTheta })_1} \times \llbracket t_2 \rrbracket _{(\varTheta )_2}$$ for terms $$t_1,t_2$$ such that $$\varTheta \vdash (t_1\! :\! A_1)R(t_2\! : \! A_2)$$.

Theorem 8 is proved by induction on derivations. We highlight that the soundness of the parametricity rule follows from the identity property of comprehensive $$\lambda \mathbf {2}$$ parametricity graphs. Theorem 9 is proved by a term model construction.

## 5 Direct-Image and Pseudograph Relations

As already discussed, the parametricity rule of Fig. 2 interprets the relation $$\langle A \rangle$$ as an identity relation when A is a closed type. When A contains type variables, however, this interpretation is not available. Consider an open type $$\alpha \vdash {A(\alpha )}\,\,\mathsf {type}$$ (where we write $$A(\alpha )$$ to highlight the occurrences of $$\alpha$$ in A). Then we have $$\alpha \rho \alpha \vdash {A(\alpha ) \big (\langle A \rangle (\rho )\big ) A(\alpha )}\,\,\mathsf {rel}$$. However, the independent handling of left and right variables in $$\lambda \mathbf {2 R}$$ (forced by the semantic correspondence with comprehensive $$\lambda \mathbf {2}$$ parametricity graphs), means that the latter relation is equivalent to $$\alpha \rho \beta \vdash {A(\alpha ) \big (\langle A \rangle (\rho )\big ) A(\beta )}\,\,\mathsf {rel}$$; i.e., it is a family (indexed by relations $$\rho$$) of relations between different types. Indeed, the distinctness of left and right type variables means $$\lambda \mathbf {2 R}$$ has no facility for formulating relations between open types and themselves. In particular, $$\lambda \mathbf {2 R}$$ contains no mechanism for defining identity relations on open types. Nonetheless, the relation $$\langle A \rangle$$ can act as a kind of pseudo-identity relation for type A where the parametricity rule can establish equalities from $$\langle A \rangle$$-relatedness in relational contexts of the form $$\langle \varGamma \rangle$$.

Graphs of functions are ubiquitous in standard arguments involving relational parametricity. Since we have only pseudo-identity relations, we correspondingly have only pseudographs available in $$\lambda \mathbf {2 R}$$. Suppose $$\varGamma \vdash f : A \rightarrow B$$. Define:

\begin{aligned} gr_{*}(f) \,\,{:}{=}\,\,[f\times \mathsf {id}_B]^{-1}\langle B \rangle \end{aligned}

Clearly $${\langle \varGamma \rangle } \vdash {A\, gr_{*}(f) \,B}\,\,\mathsf {rel}$$. Its defining property is that $$(x \! :\! A)\, gr_{*}(f) \,(y \! : \!B)$$ holds if and only if $$(fx \! :\! B) \langle B \rangle (y\! : \! B)$$. Mathematically, there is, however, another natural pseudograph relation, for f, between A and B. This is the relation $$gr_{!}(f)\,$$ defined by $$(x \! :\! A) gr_{!}(f) (y \! : \!B)$$ if there exists $$w\!:\!A$$ such that $$(x \! :\! A) \langle A \rangle (w\! : \! A)$$ and $$y = fw$$. Since, by (ii) of Lemma 7, f maps $$\langle A \rangle$$-related values to $$\langle B \rangle$$-related values, $$gr_{!}(f) \subseteq gr_{*}(f)$$. However, because $$\langle A \rangle$$ and $$\langle B \rangle$$ are not identity relations, there is no need for this inclusion to be an equality. We shall need to make use of both forms of pseudograph relation to derive the standard consequences of parametricity. In order to do so, we must first provide a definition of $$gr_{!}(f)$$ in $$\lambda \mathbf {2 R}$$ itself, and establish formal analogues of the informal observations above.

The main construction we need is that of direct-image relations $$[t_1\times t_2]_{!}R$$, dual to inverse-image relations. This is achieved using an impredicative encoding.

### Theorem 10

(Direct-image relations). Using the definition

\begin{aligned}{}[t_1\times t_2]_{!}R \,\,{:}{=}\,\,[i_{B_1}\times i_{B_2}]^{-1}(\forall {\alpha \rho \alpha }.\, ([({-} \circ t_1)\times ({-} \circ t_2)]^{-1}(R \rightarrow \rho )) \rightarrow \rho ) \end{aligned}

where $$i_{B}$$ abbreviates abbreviates $$\lambda {(v_{j} : B_{j} \rightarrow \alpha )}. \lambda ({x_{j} : A_{j}}). v_{j} (t_{1} \, x_{j})$$, $$\lambda \mathbf {2 R}$$ supports the derived rules below.    In fact, these rules are derivable without use of the parametricity rule of $$\lambda \mathbf {2 R}$$.

It is now straightforward to define the second form of pseudograph relation discussed above. Suppose that $$\varGamma \vdash f \,{:}\,A \rightarrow B$$ and define $${\langle \varGamma \rangle } \vdash {A\, gr_{!}(f) \,B}\,\,\mathsf {rel}$$ by:

\begin{aligned} gr_{!}(f)&\,\,{:}{=}\,\,[\mathsf {id}_A\times f]_{!}\langle A \rangle . \end{aligned}

To understand the relationship between the two pseudograph relations we introduce some notation. Given R and S such that $$\varTheta \vdash {ARB}\,\,\mathsf {rel}$$ and $$\varTheta \vdash {ASB}\,\,\mathsf {rel}$$, let $$\varTheta \vdash R \subseteq S$$ abbreviate $${\varTheta },\,{(x : A)R(y : B)} \vdash (x : A)S(y : B)$$.

### Lemma 11

If $$\varGamma \vdash f : A \rightarrow B$$ then:

1. (i)

$${\langle \varGamma \rangle } \vdash gr_{!}(f) \subseteq gr_{*}(f)$$; and

2. (ii)

$${\langle \varGamma \rangle } \vdash (s\!:\!A)\, gr_{*}(f) \,(t\!:\!B)$$ iff $$\varGamma \vdash f\,s \, = \, t \,{:}\,B$$ iff $${\langle \varGamma \rangle } \vdash (s\!:\!A) \, gr_{!}(f) \, (t\!:\!B)$$.

We comment that, in spite of item (ii), the converse inclusion to (i) does not hold in general. Property (ii) applies only in context $${\langle \varGamma \rangle }$$, and thus implies nothing about what happens if further relational assumptions are added.

Theorem 10 has a semantic analogue: direct image relations correspond to opfibrational structure on comprehensive $$\lambda \mathbf {2}$$ parametricity graphs.

### Theorem 12

In any comprehensive $$\lambda \mathbf {2}$$ parametricity graph, for every object W of $$\mathcal {R}({\mathbb {C}})$$, the functor $$\langle \nabla ^{\mathbb {T}}_1, \nabla ^{\mathbb {T}}_2 \rangle \! \upharpoonright _{\mathcal {R}({\mathbb {T}})_W}\,{:}\,\mathcal {R}({\mathbb {T}})_W \rightarrow \mathbb {T}_{\nabla ^{\mathbb {C}}_1 W}\! \times \! \mathbb {T}_{\nabla ^{\mathbb {C}}_2 W}$$ is an opfibration.

## 6 Consequences of Parametricity

System $$\lambda \mathbf {2 R}$$ is strong enough to prove the familiar consequences of parametricity.

### Theorem 13

(Consequences of Parametricity). System $$\lambda \mathbf {2 R}$$ proves:

1. (i)

The unit (terminal) type can be encoded as $$\mathbf {1}= \forall {\alpha }.\, \alpha \rightarrow \alpha$$.

2. (ii)

The product of A and B can be encoded as $$A \times B = \forall {\alpha }.\,(A \rightarrow B \rightarrow \alpha ) \rightarrow \alpha$$.

3. (iii)

The empty (initial) type can be encoded as $$\mathbf {0}= \forall {\alpha }.\, \alpha$$.

4. (iv)

The sum of A and B can be encoded as $$A\! + \!B = \forall {\alpha }.\,(A\! \rightarrow \! \alpha ) \! \rightarrow \! (B\! \rightarrow \! \alpha )\! \rightarrow \!\alpha$$.

5. (v)

Existential types can be encoded as $$\exists {\alpha }.\,T(\alpha ) = \forall {\alpha }.\, (\forall {\beta }.\, (T(\beta ) \rightarrow \alpha )) \rightarrow \alpha$$.

6. (vi)

The type $$\forall {\alpha }.\,(T(\alpha ) \rightarrow \alpha ) \rightarrow \alpha$$ is the carrier of the initial T-algebra for all functorial type expressions $$T(\alpha )$$.

7. (vii)

The type $$\exists {\alpha }.\,(\alpha \rightarrow T(\alpha )) \times \alpha$$ is the carrier of the final T-coalgebra for all functorial type expressions $$T(\alpha )$$.

This result for $$\lambda \mathbf {2 R}$$ implies that analogous category-theoretic properties (which we do not state for lack of space) hold for comprehensive $$\lambda \mathbf {2}$$ parametricity graphs.

The proofs of (i)–(vii) follow the usual ones, see, e.g., , but with graph relations replaced by pseudographs. Pseudograph relations of the form $$gr_{*}(f)$$ suffice in all proofs with the exception of the verification of final coalgebras, where $$gr_{!}(f)$$ is used. In this section, we explain how this difference in the treatment of initial algebras and final coalgebras arises. For lack of space, we focus on the use of pseudograph relations only, and omit the (standard) supporting arguments.

Suppose $${\varGamma },\,{\alpha } \vdash {T}\,\,\mathsf {type}$$. We write $$T(\alpha )$$ to highlight the occurrences of $$\alpha$$ in T, and T(A) for the substitution $$T[\alpha \mapsto A]$$. If $$\alpha$$ occurs only positively in T (i.e., not on the left-hand side of an odd number of arrows) then it is standard that T defines an endofunctor on types. If $$\varGamma ' \vdash f : A \rightarrow B$$, where $$\varGamma '$$ extends $$\varGamma$$, then we use the notation $$\varGamma ' \vdash T(f) : T(A) \rightarrow T(B)$$ for the functorial action of T. This action preserves identities and composition up to judgemental equality. In addition, the corresponding relational substitution preserves pseudo-identity relations; i.e., $$\langle T \rangle \!(\langle A \rangle )$$ (by which we mean the substitution $$\langle T \rangle [\alpha \rho ^\alpha \alpha \mapsto \langle A \rangle ]$$) syntactically coincides with $$\langle T(A) \rangle$$. Also, the functorial action lifts to relations: if $$\varTheta \vdash (f\!:\! A \rightarrow B) (R \rightarrow S) (f' \! : \! A' \rightarrow B')$$, where $$\varTheta$$ extends $$\langle \varGamma \rangle$$, then:

\begin{aligned} \varTheta \vdash (T(f)\!:\! T(A) \rightarrow T(B)) ({\langle T \rangle \!(R)} \rightarrow {\langle T \rangle \!(S)}) (T(f') \! : \! T(A') \rightarrow T(B')). \end{aligned}

Using these facts (which assert that T is a reflexive-graph functor ) one establishes the following properties of the action of $$\langle T \rangle$$ on pseudograph relations.

### Lemma 14

Suppose $$\alpha$$ occurs positively in $${\varGamma },\,{\alpha } \vdash {T}\,\,\mathsf {type}$$ and $$\varGamma ' \vdash f \,{:}\,A \rightarrow B$$, where $$\varGamma '$$ extends $$\varGamma$$.

1. (i)

$${\langle \varGamma ' \rangle } \vdash {\langle T \rangle (gr_{*}(f)) \subseteq gr_{*}(T(f))}\,$$.

2. (ii)

$${\langle \varGamma ' \rangle } \vdash {gr_{!}(T(f))} \subseteq {\langle T \rangle (gr_{!}(f))}\,$$.

Our proof of this lemma closely mirrors the proof of the Graph Lemma in , which exploits the fact that graph relations can be defined either using inverse image, analogously to $$gr_{*}(f)$$, or using direct image, analogously to $$gr_{!}(f)$$.

We now explain how Lemma 14 bears on the proofs of the universal properties of initial algebras and final coalgebras. Given T as above, standard constructions produce a T-algebra and a T-coalgebra that can be shown to be weakly initial and weakly final respectively, without invoking parametricity. The parametricity rule is used to establish the uniqueness part of the universal property. In the initiality and finality arguments, one is led to consider T-algebra and T-coalgebra homomorphisms respectively:  where the diagrams are given by terms, in a context $$\varGamma '$$ extending $$\varGamma$$, which commute up to judgemental equality. Lemma 14 allows one to prove the following crucial properties as consequences of the commutativity of the above diagrams.

\begin{aligned} { \langle \varGamma ' \rangle }&\vdash (a \,{:}\,T(A) \rightarrow A) \, \big (\langle T \rangle (gr_{*}(h)) \rightarrow gr_{*}(h)\big ) \, (b \,{:}\,T(B) \rightarrow B) \\ { \langle \varGamma ' \rangle }&\vdash (a' \,{:}\,A \rightarrow T(A)) \, \big (gr_{!}(h') \rightarrow \, \langle T \rangle (gr_{!}(h'))\big ) \, (b' \,{:}\,B \rightarrow T(B)) \end{aligned}

It is the orientation of the function relations above that necessitates the use of a different type of pseudograph relation in each case. Modulo this subtlety, the remaining proofs of initiality and finality proceed as usual, cf. .

## 7 Related and Further Work

System R of  and System P of  share with $$\lambda \mathbf {2 R}$$ the property of having a syntax in which function space and universal quantification are basic constructions on relations. Indeed $$\lambda \mathbf {2 R}$$ is especially similar to System P, which also has the inverse-image-relation constructor $$[t_1\times t_2]^{-1}R$$. The most significant difference is that, in System P, the formation rule for this construction is restricted: the terms $$t_1, t_2$$ are not allowed to contain free term variables. However, they are permitted to contain so-called indeterminates, which, in the semantics of System P, range over global elements in models. This device allows System P to be used to establish consequences of parametricity in well-pointed models . In $$\lambda \mathbf {2 R}$$, our general arguments for consequences of parametricity make essential use of the possibility for $$t_1$$ and $$t_2$$ to contain free term variables. As already observed in Sect. 4, the comprehension property of our models is crucial to the semantic interpretation of inverse-image relations in such cases.

System R of  departs from $$\lambda \mathbf {2 R}$$ (and System P) in two main ways. The first is that, in System R, every type A has an associated identity relation $$A^*$$.Footnote 2 A key rule of System R (written in our notation) is that $$\varTheta \vdash x \,A^*\, x$$, whenever x : A appears anywhere in relational context $$\varTheta$$. This rule breaks the independence between left and right variables in the relational judgements of $$\lambda \mathbf {2 R}$$. (For example, property (i) of Lemma 7 fails.) The second difference is that System R has an explicit syntax for defining graph relations, rather than the inverse-image construct of $$\lambda \mathbf {2 R}$$ (and System P), which would be more general in that context. Due to the presence of both identity and graph relations, the arguments, in System R, for consequences of parametricity proceed along standard lines . However, System R currently lacks a corresponding semantic story of the kind we have used in this paper in justification of $$\lambda \mathbf {2 R}$$.

In fact, the interplay between models and syntax could be pushed much further than in the present paper. By adding primitive product types to $$\lambda \mathbf {2}$$ and $$\lambda \mathbf {2 R}$$, one can strengthen our full completeness results by obtaining syntactic categories that are initial in an appropriate 2-category of strict structure-preserving morphisms of models. It would be more natural, however, to broaden both the notion of model, by replacing splittings of fibrations with cleavages, and the notion of morphism, by permitting non-strict structure preservation. With such a relaxation, coherence issues arise, but one would expect to obtain (pseudo-) initiality of the syntactic model of $$\lambda \mathbf {2 R}$$ (without any need to extend the syntax with products).

For lack of space we have not presented any concrete models in this paper. In fact, any instance of the more elaborate axiomatic structure from  can be reconstrued (albeit in a nontrivial way) as a comprehensive $$\lambda \mathbf {2}$$ parametricity graph. So our minimal structure at least generalises the known models of parametricity. However, we do not know whether our structure encompasses any genuinely new models of relational parametricity that truly exploit the (potential) added generality of our approach.

The results of the present paper should be contrasted with those of other recent work by first two authors and colleagues [9, 10]. In this paper, we have axiomatised category-theoretic structure modelling relational parametricity for the specific type theory $$\lambda \mathbf {2}$$, where the resulting structure encompasses both ‘syntactic’ and ‘semantic’ models. In contrast, [9, 10] axiomatise the category-theoretic structure required on a ‘semantic’ model for Reynolds’ original set-theoretic definition of relational parametricity  to generalise to the model. Interestingly, the category-theoretic notion of bifibration occurs both as a central ingredient in the axiomatisation of [9, 10], and, in the guise of direct-image relations, as a vital tool in the present paper. A novelty in the present paper is that the bifibrational structure is derived rather than assumed.

From a type-theoretic perspective, one advantage of the approach followed in this paper is that the passage from the original type theory ($$\lambda \mathbf {2}$$) to the relational version ($$\lambda \mathbf {2 R}$$) appears not to depend on specific properties of the former, other than that essential use is made of judgemental equality in the formulation of the parametricity rule. We believe that this potential flexibility may be useful for transferring our methods to dependent type theories, where parametricity is an active area of study [2, 4, 5, 17].

The proof-relevant setting of dependent type theory, however, requires modifications to our semantic framework. In particular the relational property of parametricity graphs must be relaxed. Ongoing work on a higher-dimensional, proof-relevant form of parametricity may show how to remove this requirement.

1. 1.

We use $$(\cdot )^{-1}$$ rather than $$(\cdot )^{*}$$ for reindexing to emphasise that we are in a relational setting: $$\langle \nabla _1, \nabla _2 \rangle$$ is a preorder fibration since it is faithful.

2. 2.

In System P, every type A is itself a relation, which, although called an “identity relation” in , has the properties of the relation $$\langle A \rangle$$ in the present paper.

## References

1. Abadi, M., Cardelli, L., Curien, P.-L.: Formal parametric polymorphism. Theor. Comput. Sci. 121(1&2), 9–58 (1993)

2. Atkey, R., Ghani, N., Johann, P.: A relationally parametric model of dependent type theory. In: Jagannathan, S., Sewell, P. (eds.), POPL, pp. 503–515. ACM (2014)

3. Barendregt, H.: Introduction to generalized type systems. J. Funct. Program. 1(2), 125–154 (1991)

4. Bernardy, J.-P., Coquand, T., Moulin, G.: A presheaf model of parametric type theory. In: Ghica, D.R. (ed.), MFPS, ENTCS, pp. 17–33. Elsevier (2015)

5. Bernardy, J.-P., Jansson, P., Paterson, R.: Proofs for free. J. Funct. Program. 22, 107–152 (2012)

6. Møgelberg, E.: Categorical models for Abadi and Plotkin’s logic for parametricity. Math. Struct. Comput. Sci. 15, 709–772 (2005)

7. Dunphy, B.: Parametricity as a notion of uniformity in reflexive graphs. Ph.D. thesis, University of Illinois (2002)

8. Dunphy, B., Reddy, U.: Parametric limits. In: LICS, pp. 242–251 (2004)

9. Ghani, N., Johann, P., Nordvall Forsberg, F., Orsanigo, F., Revell, T.: Bifibrational functorial semantics of parametric polymorphism. In: Ghica, D.R. (ed.) MFPS, ENTCS, pp. 67–83. Elsevier (2015)

10. Ghani, N., Nordvall Forsberg, F., Orsanigo, F.: Parametric polymorphism — universally. In: de Paiva, V., de Queiroz, R., Moss, L.S., Leivant, D., de Oliveira, A. (eds.) WoLLIC 2015. LNCS, vol. 9160, pp. 81–92. Springer, Heidelberg (2015)

11. Girard, J.-Y.: Interprétation fonctionelle et élimination des coupures dans l’arithmétique d’ordre supérieur. Ph.D. thesis, University of Paris VII (1972)

12. Hermida, C.: Fibrational relational polymorphism (2006). http://maggie.cs.queensu.ca/chermida/papers/FibRelPoly.pdf

13. Hermida, C., Reddy, U., Robinson, E.: Logical relations and parametricity – a Reynolds programme for category theory and programming languages. ENTCS 303, 149–180 (2014)

14. Jacobs, B.: Comprehension categories and the semantics of type dependency. Theor. Comput. Sci. 107(2), 169–207 (1993)

15. Jacobs, B.: Categorical Logic and Type Theory. Elsevier, Amsterdam (1999)

16. Kinoshita, Y., O’Hearn, P.W., Power, J., Takeyama, M., Tennent, R.D.: An axiomatic approach to binary logical relations with applications to data refinement. In: Abadi, M., Ito, T. (eds.) Theoretical Aspects of Computer Software. LNCS, vol. 1281, pp. 191–212. Springer, Heidelberg (1997)

17. Krishnaswami, N.R., Dreyer, D.: Internalizing relational parametricity in the extensional calculus of constructions. In: Ronchi, S., Rocca, D. (eds.) CSL, pp. 432–451 (2013)

18. Lawvere, F.W.: Equality in hyperdoctrines and comprehension schema as an adjoint functor. Appl. Categorical Algebra 17, 1–14 (1970)

19. Ma, Q., Reynolds, J.C.: Types, abstraction, and parametric polymorphism, part 2. In: Brookes, S., Main, M., Melton, A., Mislove, M., Schmidt, D. (eds.) Mathematical Foundations of Programming Semantics. LNCS, vol. 598, pp. 1–40. Springer, Heidelberg (1991)

20. O’Hearn, P.W., Tennent, R.D.: Parametricity and local variables. J. ACM 42(3), 658–709 (1995)

21. Plotkin, G., Abadi, M.: A logic for parametric polymorphism. In: Bezem, M., Groote, J.F. (eds.) Typed Lambda Calculi and Applications. LNCS, vol. 664, pp. 361–375. Springer, Heidelberg (1993)

22. Reynolds, J.: Towards a theory of type structure. In: Robinet, B. (ed.) Programming Symposium. LNCS, vol. 19, pp. 408–425. Springer, Heidelberg (1974)

23. Reynolds, J.: Types, abstraction and parametric polymorphism. In: Mason, R.E.A. (ed.), Information Processing, pp. 513–523 (1983)

24. Robinson, E.P., Rosolini, G.: Reflexive graphs and parametric polymorphism. In: LICS, pp. 364–371. IEEE Computer Society (1994)

25. Seely, R.A.G.: Categorical semantics for higher order polymorphic lambda calculus. J. Symbolic Logic 52, 969–989 (1987)

26. Strachey, C.: Fundamental concepts in programming languages. High. Order Symbolic Comput. 13(1–2), 11–49 (2000)

27. Wadler, P.: Theorems for free! In: Stoy, J.E. (ed.) FPCA, pp. 347–359. ACM (1989)

## Acknowledgements

We thank Bob Atkey, Claudio Hermida, Rasmus Møgelberg and the anonymous reviewers for helpful discussions and comments. This research was supported by EPSRC grants GR/A11731/01, EP/E016146/1, EP/K023837/1 and EP/M016951/1.

## Author information

Authors

### Corresponding author

Correspondence to Fredrik Nordvall Forsberg .

## Rights and permissions

Reprints and Permissions

© 2016 Springer-Verlag Berlin Heidelberg

### Cite this paper

Ghani, N., Nordvall Forsberg, F., Simpson, A. (2016). Comprehensive Parametric Polymorphism: Categorical Models and Type Theory. In: Jacobs, B., Löding, C. (eds) Foundations of Software Science and Computation Structures. FoSSaCS 2016. Lecture Notes in Computer Science(), vol 9634. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-662-49630-5_1