A General Framework for Relational Parametricity

04/30/2018 ∙ by Kristina Sojakova, et al. ∙ 0

Reynolds' original theory of relational parametricity was intended to capture the idea that polymorphically typed System F programs preserve all relations between inputs. But as Reynolds himself later showed, his theory can only be formalized in a meta-theory with an impredicative universe, such as the Calculus of Inductive Constructions. Abstracting from Reynolds' ideas, Dunphy and Reddy developed their well-known framework for parametricity that uses parametric limits in reflexive graph categories and aims to subsume a variety of parametric models. As we observe, however, their theory is not sufficiently general to subsume the very model that inspired parametricity, namely Reynolds' original model, expressed inside type theory. To correct this, we develop an abstract framework for relational parametricity that generalizes the notion of a reflexive graph categories and delivers Reynolds' model as a direct instance in a natural way. This framework is uniform with respect to a choice of meta-theory, which allows us to obtain the well-known PER model of Longo and Moggi as a direct instance in a natural way as well. In addition, we offer two novel relationally parametric models of System F: i) a categorical version of Reynolds' model, where types are functorial on isomorphisms and all polymorphic functions respect the functorial action, and ii) a proof-relevant categorical version of Reynolds' model (after Orsanigo), where, additionally, witnesses of relatedness are themselves suitably related. We show that, unlike previously existing frameworks for parametricity, ours recognizes both of these new models in a natural way. Our framework is thus descriptive, in that it accounts for well-known models, as well as prescriptive, in that it identifies abstract properties that good models of relational parametricity should satisfy and suggests new constructions of such models.



There are no comments yet.


page 1

page 2

page 3

page 4

This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

Reynolds [13] introduced the notion of relational parametricity to model the extensional behavior of programs in System F [6], the formal calculus at the core of all polymorphic functional languages. His goal was to give a type an object interpretation and a relational interpretation , where takes sets to sets and takes relations to relations . A term was to be interpreted as a map associating to each set a function . The interpretations were to be given inductively on the structure of and in such a way that they implied two key theorems: the Identity Extension Lemma, stating that if is the equality relation on then is the equality relation on ; and the Abstraction Theorem, stating that, for any relation , and map arguments related by to results related by . A similar result holds for types and terms with any number of free variables.

In Reynolds’ treatment of relational parametricity, if is the type , for example, then is the set of functions and, for , relates to iff and map arguments related by to results related by . Similarly, if is the type , then consists of those polymorphic functions that take a set and return an element of , and also have the property that for any relation , and are related by . Two such polymorphic functions and are then related by iff for any relation , and are related by . These definitions allow us to deduce interesting properties of (interpretations of) terms solely from their types. For example, for any term , the Abstraction Theorem guarantees that the interpretation of is related to itself by the relational interpretation of . So if we fix a set , fix , and define a relation on by , then must be related to itself by the relational interpretation of applied to . This means that must carry arguments related by to results related by . Since is related to itself by , must be related to itself by , so that must be . That is, must be the polymorphic identity function. Such applications of relational parametricity are useful in many different scenarios, e.g., when proving invariance of polymorphic functions under changes of data representation, equivalences of programs, and “free theorems” [17].

The well-known problem with Reynolds’ treatment of relational parametricity (see [14]) is that the universe of sets is not impredicative, and hence the aforementioned “set” cannot be formed. This issue can be resolved if we instead work in a meta-theory that has an impredicative universe; a natural choice is an extensional version of the Calculus of Inductive Constructions (CIC), i.e., a dependent type theory with a cumulative Russell-style hierarchy of universes , where is impredicative, and extensional identity types. With this adjustment, we have two canonical relationally parametric models of System F: i) the PER model of Longo and Moggi [9], internal to the theory of -sets and realizable functions, and ii) Reynolds’ original model111Since there are no set-theoretic models of System F, by the phrase “Reynolds’ original model” we will always mean the version of his model that is internal to extensional CIC as described above. The need for impredicativity is inherited from Reynolds’ original construction, and is not a new requirement., internal to CIC.

After Reynolds’ original paper, more abstract treatments of his ideas were given by, e.g., Robinson and Rosolini [15], O’Hearn and Tennent [11], Dunphy and Reddy [2], and Ghani et al. [5]. The approach is to use a categorical structure — reflexive graph categories for [15, 11, 2] and fibrations for [5] — to represent sets and relations, and to interpret types as appropriate functors and terms as natural transformations. In particular, [2] aims to “[address] parametricity in all its incarnations”, and similarly for [5]. Surprisingly and significantly, however, Reynolds’ original model does not arise as a direct instance of either framework. This leads us to ask:

What constitutes a good framework for relational parametricity?

Our answer is that such a framework should:

  1. Deliver a relationally parametric model for each instantiation of its parameters, from which it uniformly produces such models. In particular, it should allow a choice of a suitable meta-theory (the Calculus of Inductive Constructions, the theory of -sets, etc.).

  2. Admit the two canonical relationally parametric models mentioned above as direct instances in a natural, uniform way.

  3. Abstractly formulate properties that good models of parametricity for System F should be expected to satisfy.

Criterion 1 ensures that we indeed get a true framework rather than just a reusable blueprint for constructing models of parametricity. Criterion 2 remains unsatisfied for the frameworks of Dunphy and Reddy and of Ghani et al. because Reynolds’ original model formulated syntactically does not satisfy certain strictness conditions imposed by [2, 5]. For example, let and be two types, with object interpretations and and relational interpretations and . The interpretation of the product should be an appropriate product of interpretations; that is, the object interpretation should map a set to and the relational interpretation should map a relation to , with the product of two relations defined in the obvious way. For the Identity Extension Lemma to hold, we need to be the same as . Here, the equality relation on a set maps to the type of proofs of equality between and , so that and are related iff is inhabited, i.e., iff is identical to . By the induction hypothesis, is , and similarly for , so we need to show that is . But this is not necessarily the case since the identity type on a product is in general not identical to the product of identity types, but rather just suitably isomorphic. So the interpretation of is not necessarily an indexed or fibered functor (in the settings of [2] and [5], respectively).

Three ways to fix this problem come to mind. Firstly, we can attempt to change the meta-theory, by, e.g., imposing an additional axiom asserting that two logically equivalent propositions are definitionally equal. We do not pursue this approach here: the goal of our framework is to directly subsume the important models in their natural meta-theories, as per criteria 1 and 2 above, rather than require the user to augment the meta-theory with ad hoc axioms to make the shoe fit. The second possibility is to use the syntactic analogue of strictification, pursued in, e.g., [1]. The idea is that instead of interpreting a closed type as a set (on the object level), we interpret it as a set endowed with a relation that is isomorphic, but not necessarily identical, to the canonical discrete relation . The chosen equality relation on the set — more precisely, on the entire structure — will then be rather than . This allows us to construct in a way that respects all type constructors on the nose, so that the aforementioned issue with not being identical to is avoided. The problem, however, is that there can be many different ways to endow with a discrete relation ; in other words, the type of discrete relations on is not contractible. It is thus unclear whether and how this “discretized” version of Reynolds’ model is equivalent to the original, intended one.

Here we suggest a third approach: we record the isomorphisms witnessing the preservation of the Identity Extension Lemma for each type constructor, and propagate them through the construction. This means, however, that we can no longer interpret a type as a pair of maps and ; indeed, since the domain of is the discrete category , is not required to preserve isomorphisms in . As a result, even if we know that the pair satisfies the Identity Extension Lemma, its reindexing — defined by precomposition — might not. The upshot is that the obvious “2-fibration” corresponding to Reynolds’ original model is not necessarily a fibration at all.

We solve this problem by specifying subcategories and of relevant isomorphisms that form a reflexive graph category with isomorphisms. Abstractly, this structure gives us two face maps (called and in [2]), which represent the domain and codomain projections, and a degeneracy (called in [2]), which represents the equality functor. We interpret a type as a pair of functors and that together comprise a face map- and degeneracy-preserving reflexive graph functor, and interpret each term as a face map- and degeneracy-preserving reflexive graph natural transformation.

Since the domain of is , preserves all relevant isomorphisms between relations, so the reindexing of is now well-defined. Choosing to contain the isomorphism between the two relations and yields the satisfaction of the Identity Extension Lemma for products; other type constructors follow the same pattern. We note that although the preservation of isomorphisms on the relation level is sufficient to carry out the model construction, we formally require the preservation of relevant isomorphisms on the object level, too. This makes the framework more uniform and, moreover, leads to the novel notion of a categorical Reynolds’ model, in which interpretations of types are endowed with a functorial action on isomorphisms and all polymorphic functions respect this action. Furthermore, we go one level higher and use the ideas of Orsanigo [12] (and Ghani et al. [3], which it supersedes) to define a proof-relevant categorical Reynolds’ model, in which, additionally, witnesses of relatedness are themselves suitably related via a yet higher relation.

This “2-parametric” model of course does not arise as an instance of our framework since it requires additional structure — e.g., the concept of a 2-relation — pertaining to the higher notion of parametricity. Nevertheless, we would still like to be able to recognize it as a model parametric in the ordinary sense. Various definitions of parametricity for models of System F exist: [2, 5] are examples of “internal” approaches to parametricity, where a model is considered parametric if it is produced via a specified procedure that bakes in desired features of parametricity such as the Identity Extension Lemma. On the other hand, [4, 10, 15, 7] are examples of “external” approaches to parametricity, in which reflexive graphs of models are used to endow models of interest with enough additional structure that they can reasonably be considered parametric. Surprisingly though, the proof-relevant model we give in Section 6

does not appear to satisfy any of these definitions, and in particular does not satisfy any of the external ones. The ability to construct a classifying reflexive graph seems to rely on an implicit assumption of proof-irrelevance, which we elaborate on in Section 

6. However, we propose a new definition of a relationally parametric model of System F in Section 5 and show that it subsumes not only the two canonical parametric models of System F, but also the two novel ones we give in this paper. In particular, it subsumes the proof-relevant model given in Section 6.

The main contributions of this paper are as follows:

  • We demonstrate that existing frameworks for the functorial semantics of relational parametricity for System F fail to directly subsume both canonical models of relational parametricity for System F.

  • We solve this problem by developing a good abstract framework for relational parametricity that allows a choice of meta-theory, delivers both canonical relationally parametric models of System F as direct instances in a uniform way, and exposes properties that good models of System F parametricity should be expected to satisfy, e.g., guaranteeing that interpretations of terms, not just types, suitably commute with the degeneracy.

  • We give a novel definition of a parametric model of System F, which is a hybrid of the external and internal approaches, and show that it subsumes both canonical models (expressed as instances of our framework).

  • We give two novel relationally parametric models of System F — one of which is proof-relevant and can be seen as parametric in a higher sense (“2-parametric”) — and show that our definition recognizes both of these in a natural way, with the proof-irrelevant model arising as a direct instance of our framework.

Fibrational Preliminaries

We give a brief introduction to fibrations, mainly to settle notation. More details can be found in, e.g.[7].

Definition 1.

Let be a functor. A morphism in is cartesian over in if and, for every in with for some , there is a unique with and . A functor is a fibration if, for every object of and morphism of , there is a cartesian morphism in with codomain over .

If is a fibration then is its total category and is its base category. An object in is over its image , and similarly for morphisms. A morphism is vertical if it is over an identity morphism. We write for the fiber over an object in , i.e., the subcategory of of objects over and morphisms over .

If is a fibration, we call a cartesian morphism over with codomain a cartesian lifting of with codomain with respect to . A cartesian lifting of with codomain with respect to need not be unique, but it is always unique up to vertical isomorphism. We are interested in fibrations in which representative cartesian liftings are specified, or chosen.

Definition 2.

A fibration is cloven if it comes with a choice of cartesian liftings, i.e., with one cartesian lifting of with codomain with respect to regarded as primary amongst all such cartesian liftings for each morphism in and object in .

We emphasize that the choice of cartesian liftings is part of the structure that is given when a fibration is cloven. In this case one uses the phrase “the cartesian lifting” of with codomain to refer to the chosen such lifting, which we denote by . Any time we consider categorical objects (e.g., categories, functors, etc.) with particular structures (e.g., products, adjoints, etc.) in this paper, we intend that those structures are chosen in this sense.

The function mapping each object of to the domain of then extends to a functor mapping each morphism in to the unique morphism such that . The universal property of ensures the existence and uniqueness of . We call the substitution functor along . We will be especially interested in cloven fibrations whose substitution functors are well-behaved:

Definition 3.

A cloven fibration is split if its substitution functors are such that and .

We will later require even more structure of our split fibrations:

Definition 4.

A split fibration has a split generic object if there is an object in , together with a collection of isomorphisms mapping each morphism from to in to an object of the fiber that is natural in , i.e., is such that for every and .

Seely [16] gave a sound categorical semantics of System F in -fibrations (presented as PL-categories). We will make good use of this result below.

2 Reflexive Graph Categories

Although Reynolds himself showed that his original approach to relational parametricity does not work in set theory, we can still use it as a guide for designing an abstract framework for parametricity. Instead of sets and relations, we consider abstract notions of “sets” and “relations”, and require them to be related as follows: i) for any relation , there are two canonical ways of projecting an object out of , corresponding to the domain and codomain operations, ii) for any object , there is a canonical way of turning it into a relation, corresponding to the equality relation on , and iii) if we start with an object , turn it into a relation according to ii), and then project out an object according to i), we get back. This suggests that our abstract relations and the canonical operations on them can be organized into a reflexive graph structure: categories , and functors , such that , as is done in [2].

Since there are no set-theoretic models of System F ([14]), all of the reflexive graph structure identified above must to be internal to some ambient category . In particular, and must be categories internal in , and , , and must be functors internal in . For Reynolds’ original model, the ambient category has types as objects and terms as morphisms. Here, is the universe one level above the impredicative universe ; we will denote simply by below. This ensures that is an object in . To model relations, we introduce:

The type of propositions singles out those types in with the property that any two inhabitants, if they exist, are equal. Propositions can be used to model relations as follows: in Reynolds’ original model, is related to in at most one way under any relation (either or not), so the type of proofs that is a proposition. Conversely, given , we consider and to be related by iff is inhabited.

To see the universe as a category internal to we take its object of objects to be and define its object of morphisms by . We define the category of relations by giving its objects and of objects and morphisms, respectively:

We clearly have two internal functors from to corresponding to the domain and codomain projections, respectively. We also have an internal functor from to that constructs an equality relation with and . Here, the term is defined as usual by -induction and witnesses the fact that respects equality.

These observations motivate the next two definitions, in which we denote the category of categories and functors internal to by , and assume is locally small and has all finite products. (A category is locally small if each of its hom-sets is small, i.e., is a set rather than a proper class.)

Definition 5.

A reflexive graph structure on a category consists of:

  • objects and of

  • distinct arrows for

  • an arrow

such that .

The requirement that the two face maps and are distinct is to ensure that there are enough relations for the notion of relation-preservation to be meaningful. Otherwise, as also observed in [2], we could see any category as supporting a trivial reflexive graph structure whose only relations are the equality ones. For readers familiar with [7], the condition serves a purpose similar to that of the requirement in Definition 8.6.2 of [7] that the fiber category over the terminal object in is the category of relations in the preorder fibration on the fiber category over the terminal object in . Both conditions imply that some relations must be heterogeneous. But while in [7] relations are obtained in a standard way as predicates (given by a preorder fibration) over a product, we do not assume that relations are constructed in any specific way, but rather only that the abstract operations on relations suitably interact. Moreover, since the two face maps and are distinct, any morphism generated by the face maps and the degeneracy must be one of the seven distinct maps , and for . Every such morphism thus has a canonical representation.

Definition 6.

A reflexive graph category (on ) is a reflexive graph structure on .

Example 7 (PER model).

We take the ambient category to be the category of -sets, given in Definition 6.3 of [9]. We construct a reflexive graph category, which we call , as follows. The internal category of “sets” is the category given in Definition 8.4 of [9]. Informally, the objects of are partial equivalence relations on , and the morphisms are realizable functions that respect such relations. To define the internal category of “relations”, we first construct its object of objects. The carrier of this -set is the set of pairs of the form , where and are partial equivalence relations and is a saturated predicate on the product PER . A saturated predicate on a PER is a predicate on such that and imply . To finish the construction of our object of objects for we take any pair as above to be realized by any natural number.

The carrier of the object of morphisms for comprises all pairs of the form

satisfying the condition that, for any , such that , , and holds, holds as well. The first component records the domain and codomain of the morphism and the second component is a pair of equivalence classes under the specified exponential PERs. As in [9], we denote the application of the partial recursive function to a natural number in its domain by . To finish the construction of the object of morphisms for , we take a pair of pairs as above to be realized by a natural number iff and .

We again have two internal functors and from to corresponding to the two projections. We also have an equality functor from to whose action on objects is given by , where iff , and whose action on morphisms is given by

Example 8 (Reynolds’ model).

We obtain a reflexive graph category by taking , , and , and letting and be the functors corresponding to the domain and codomain projections, respectively.

If is a reflexive graph category, then the discrete graph category and the product reflexive graph category for are defined in the obvious ways: has the same objects as but only the identity morphisms, and for . For the latter, the product on the right-hand side is a product of internal categories, which exists because has finite products by assumption.

If is an internal category, we denote by and the objects of representing the objects and morphisms of , respectively. If is an internal functor, we denote by and the arrows of representing the object and morphisms parts of , respectively. Also:

Notation 9.

We will use the following notation with respect to an internal category in :

  • Given a “generalized object” (with arbitrary), we denote by the arrow , where is the arrow representing identity morphisms in .

  • For a “generalized morphism” (with arbitrary), we denote by and the arrows and respectively, where are the arrows representing the source and target operations in .

  • For generalized morphisms such that , we denote by the arrow , where is the arrow representing composition in , its domain is the pullback of the two arrows and , and is the canonical morphism into this pullback.

  • We say that is an isomorphism if there exists a such that , and , . If such a exists, it is necessarily unique and hence will be denoted by .

Given a reflexive graph category axiomatizing the sets and relations, an obvious first attempt at pushing Reynolds’ original idea through is to take the interpretation of a type with free type variables to be a pair , where and are functions giving the “set” and “relation” interpretations of the type . Although as explained in the introduction, this approach will need some tweaking — we will need to endow and with actions on some morphisms — it suggests:

Definition 10.

Let and be reflexive graph categories. A reflexive graph functor is a pair of functors such that and .

Writing for and for , we recall from the introduction that and should be appropriately related via the domain and codomain projections and the equality functor. Since the two face maps now model the projections, and the degeneracy models the equality functor, we end up with the following conditions:

  • for each object in , we have

  • for each object in , we have

We examine what these conditions imply for Reynolds’ model by considering the product of two types and . By the induction hypothesis, and are interpreted as pairs and , where and satisfy i) and ii). The interpretation of a product should be a product of interpretations, i.e., and . It remains to be seen that this interpretation satisfies i) and ii). Fix a relation on and . Condition i) entails that and for some and . Thus has the form , where maps a pair of pairs to . Thus i) is satisfied simply by construction, which leads us to define:

Definition 11.

A reflexive graph functor is face map-preserving if the following diagram in commutes for all :

In Reynolds’ model, condition ii) gives that is for any set , and similarly for . We thus need to show that is . But while the domains and codomains of these two relations agree (all are ), the former maps to , while the latter maps it to . These two types are not necessarily identical, but they are isomorphic (i.e., there are functions back and forth that compose to identity on both sides).

We thus relax condition ii) to allow an isomorphism . In fact, we can require more: since the domains and codomains of and coincide by condition i), we can insist that both projections map the isomorphism to the identity morphism on . This coherence condition is a natural counterpart to the equation , and turns out to be not just a design choice but a necessary requirement: in Reynolds’ model, for instance, the proof that the interpretations of -types (as defined later) suitably commute with the functor depends precisely on the morphisms underlying the maps being identities. This suggests:

Definition 12.

A reflexive graph functor is degeneracy-preserving if the following diagram in commutes up to a given natural isomorphism satisfying the coherence condition for :

As a first approximation, we can try to interpret a type with free type variables as a face map- and degeneracy-preserving reflexive graph functor . Reynolds’ original idea for interpreting terms suggests that the interpretation of a term should be a (vacuously) natural transformation . As observed in [5], the Abstraction Theorem can then be formulated as follows: there is a (vacuously) natural transformation such that, for any object in , we have . To see that this does indeed give what we want, we revisit Reynolds’ model. There, the face maps are the domain and codomain projections and an object in is an -tuple of relations. Denote by and by . Then is a morphism of relations from to and, since and are face map-preserving, and for some and . By definition, gives maps , , together with a map stating precisely that and map related inputs to related outputs. By definition, is and is , so the condition that is implies that the maps underlying and must be and , respectively, and so must indeed map related inputs to related outputs, as witnessed by . Pairing the natural transformations and motivates:

Definition 13.

Let be reflexive graph functors. A reflexive graph natural transformation is a pair of natural transformations and .

The Abstraction Theorem then further suggests defining:

Definition 14.

A reflexive graph natural transformation between two face map-preserving reflexive graph functors is face map-preserving if for any we have

The interpretation of a term should then be a face map-preserving natural transformation from to . We also have the dual notion:

Definition 15.

A reflexive graph natural transformation between two degeneracy-preserving reflexive graph functors and is degeneracy-preserving if for any , we have

Intuitively, the above equation represents the commutativity of the following diagram in the internal category :

There is no explicit analogue of Definition 15 in Reynolds’ model for the following reason: Reynolds’ model (as well as the PER model) is proof-irrelevant, in the precise sense that the functor is faithful, and this condition is sufficient to guarantee that any face map-preserving natural transformation is automatically degeneracy-preserving as well. This may or may not be the case in proof-relevant models (although in the model from Section 6 it is), so we explicitly restrict attention below only to those natural transformations that are face map- and degeneracy-preserving (as also done in [2]), and omit further mention of these properties.

We have the usual laws of identity and composition of reflexive graph functors and natural transformations:

Definition 16.

Given a reflexive graph category , the identity reflexive graph functor is defined as follows:

  • is the identity functor on

Definition 17.

Given two reflexive graph functors and , let be the reflexive graph functor defined as follows:

Definition 18.

Given a reflexive graph functor , the identity reflexive graph natural transformation is defined by .

Definition 19.

Given reflexive graph functors and reflexive graph natural transformations and , let be the reflexive graph natural transformation defined by .

Definition 20.

Given reflexive graph functors and , and a reflexive graph natural transformation , let be the reflexive graph natural transformation defined by .

Definition 21.

Given reflexive graph functors and , and a reflexive graph natural transformation , let be the reflexive graph natural transformation defined by .

One basic example of a reflexive graph functor which will be used often and will end up interpreting type variables is the projection:

Definition 22.

Given a reflexive graph category and , the “-th projection” reflexive graph functor