Parametricity for Nested Types and GADTs

This paper considers parametricity and its consequent free theorems for nested data types. Rather than representing nested types via their Church encodings in a higher-kinded or dependently typed extension of System F, we adopt a functional programming perspective and design a Hindley-Milner-style calculus with primitives for constructing nested types directly as fixpoints. Our calculus can express all nested types appearing in the literature, including truly nested types. At the level of terms, it supports primitive pattern matching, map functions, and fold combinators for nested types. Our main contribution is the construction of a parametric model for our calculus. This is both delicate and challenging. In particular, to ensure the existence of semantic fixpoints interpreting nested types, and thus to establish a suitable Identity Extension Lemma for our calculus, our type system must explicitly track functoriality of types, and cocontinuity conditions on the functors interpreting them must be appropriately threaded throughout the model construction. We also prove that our model satisfies an appropriate Abstraction Theorem, as well as that it verifies all standard consequences of parametricity in the presence of primitive nested types. We give several concrete examples illustrating how our model can be used to derive useful free theorems, including a short cut fusion transformation, for programs over nested types. Finally, we consider generalizing our results to GADTs, and argue that no extension of our parametric model for nested types can give a functorial interpretation of GADTs in terms of left Kan extensions and still be parametric.


Introduction
Algebraic data types (ADTs), both built-in and user-defined, have long been at the core of functional languages such as Haskell, ML, Agda, Epigram, and Idris. ADTs, such as that of natural numbers, can be unindexed. But they can also be indexed over other types. For example, the ADT of lists (here coded in Agda) data List (A : Set) : Set where nil : List A cons : A → List A → List A is indexed over its element type A. The instance of List at index A depends only on itself, and so is independent of List B for any other index B. That is, List, like all other ADTs, defines a family of inductive types, one for each index type. Over time, there has been a notable trend toward data types whose non-regular indexing can capture invariants and other sophisticated properties that can be used for program verification and other applications. A simple example of such a type is given by the type data PTree (A : Set) : Set where pleaf : A → PTree A pnode : PTree (A × A) → PTree A of perfect trees, which can be thought of as constraining lists to have lengths that are powers of 2. A similar data type Nest is given in the canonical paper [BM98] on nested types. The above code makes clear that perfect trees at index type A are defined in terms of perfect trees at index type A × A. This is typical of nested types, one type instance of which can depend on others, so that the entire family of types must actually be defined at once. A nested type thus defines not a family of inductive types, but rather an inductive family of types. Nested types include simple nested types, like perfect trees, none of whose recursive occurrences occur below another type constructor; "deep" nested types [JP20], such as the nested type data PForest (A : Set) : Set where fempty : PForest A fnode : A → PTree (PForest A) → PForest A of perfect forests, whose recursive occurrences appear below type constructors for other nested types; and truly nested types, such as the nested type data Bush (A : Set) : Set where bnil : Bush A bcons : A → Bush (Bush A) → Bush A of bushes, whose recursive occurrences appear below their own type constructors.
Relational parametricity encodes a powerful notion of type-uniformity, or representation independence, for data types in polymorphic languages. It formalizes the intuition that a polymorphic program must act uniformly on all of its possible type instantiations by requiring that every such program preserves all relations between pairs of types at which it is instantiated. Parametricity was originally put forth by Reynolds [Rey83] for System F [Gir72], the calculus at the core of all polymorphic functional languages. It was later popularized as Wadler's "theorems for free" [Wad89], so called because it can deduce properties of programs in such languages solely from their types, i.e., with no knowledge whatsoever of the text of the programs involved. Most of Wadler's free theorems are consequences of naturality for polymorphic list-processing functions. However, parametricity can also derive results that go beyond just naturality, such as correctness for ADTs of the program optimization known as short cut fusion [GLP93,Joh02].
But what about nested types? Does parametricity still hold if such types are added to polymorphic calculi? More practically, can we justifiably reason type-independently about (functions over) nested types in functional languages?
Type-independent reasoning about ADTs in functional languages is usually justified by first representing ADTs by their Church encodings, and then reasoning type-independently about these encodings. This is typically justified by constructing a parametric model -i.e, a model in which polymorphic functions preserve relationsá la Reynolds -for a suitable If Γ is a finite subset of T, Φ is a finite subset of F, α is a finite subset of F 0 disjoint from Φ, and φ k ∈ F k \ Φ, then the set F of well-formed types is given in Definition 2.1. The notation there entails that an application F F 1 . . . F k is allowed only when F is a type variable of arity k, or F is a subexpression of the form µφ k .λα 1 . . . α k .F . Accordingly, an overbar indicates a sequence of types whose length matches the arity of the type applied to it. Requiring that types are always in such η-long normal form avoids having to consider β-conversion at the level of types. In a type Nat α F G, the Nat operator binds all occurrences of the variables in α in F and G; intuitively, Nat α F G represents the type of a natural transformation in α from the functor F to the functor G. In a subexpression µφ k .λα.F , the µ operator binds all occurrences of the variable φ, and the λ operator binds all occurrences of the variables in α, in the body F .
A type constructor context, or non-functorial context, is a finite set Γ of type constructor variables, and a functorial context is a finite set Φ of functorial variables. In Definition 2.1, a judgment of the form Γ; Φ F indicates that the type F is intended to be functorial in the variables in Φ but not necessarily in those in Γ.
Definition 2.1. The formation rules for the set F of (well-formed) types are We write F for ∅; ∅ F . Definition 2.1 ensures that the expected weakening rules for well-formed types hold, i.e., if Γ; Φ F is well-formed, then both Γ, ψ; Φ F and Γ; Φ, φ F are also well-formed. Note that weakening does not change the contexts in which types can be formed. For example, a Nat-type -i.e., a type of the form Nat α F G -can be formed in any functorial context. But since Nat-types contain no functorial variables, we will form them in empty functorial contexts below whenever convenient. Of course, we cannot first weaken the functorial contexts of F and G to Γ; α, β F and Γ; α, β G, and then form Nat α F G in the weakened context Γ; β. If Γ; ∅ F and Γ; ∅ G, then our rules allow formation of the type Γ; ∅ Nat ∅ F G, which represents the System F type Γ F → G. Similarly, if Γ; α F , then our rules allow formation of the type Γ; ∅ Nat α 1 F , which represents the System F type Γ; ∅ ∀α.F . However, some System F types, such as ∀α.(α → α) → α, are not representable in our calculus. Note that, in the rule for µ-types, no variables in Φ appear in the body F of µφ.λα.F . This will be critical to proving the Identity Extension Lemma for our calculus. Definition 2.1 allows the formation of all of the types from Section 1: List α = µβ. 1 + α × β or (µφ.λβ. 1 + β × φβ) α PTree α = (µφ.λβ. β + φ (β × β)) α Forest α = (µφ.λβ. 1 + β × PTree (φβ)) α Bush α = (µφ.λβ. 1 + β × φ (φβ)) α Note that since the body F of a type (µφ.λα.F )G can only be functorial in φ and the variables in α, the representation of List α as the ADT µβ. 1 + α × β cannot be functorial in α. By contrast, if List α is represented as the nested type (µφ.λβ. 1 + β × φβ) α then we can choose α to be a functorial variable or not when forming the type. This observation holds for other ADTs as well; for example, if Tree α γ = µβ.α + β × γ × β, then α, γ; ∅ Tree α γ is well-formed, but ∅; α, γ Tree α γ is not. And it also applies to some non-ADT types, such as GRose φ α = µβ.1 + α × φβ, in which φ and α must both be non-functorial variables. It is in fact possible to allow "extra" 0-ary functorial variables in the body of µ-types (functorial variables of higher arity are the real problem), which would allow the first-order representations of ADTs to be functorial. However, since doing this requires some changes to the formation rule for µ-types, as well as the delicate threading of some additional conditions throughout our model construction, we do not pursue this line of investigation here.
Definition 2.1 allows well-formed types to be functorial in no variables. Functorial variables can also be demoted to non-functorial status: if F [φ :== ψ] is the textual replacement of φ in F , then Γ, ψ k ; Φ F [φ k :== ψ k ] is derivable whenever Γ; Φ, φ k F is. The proof is by induction on the structure of F . In addition to textual replacement, we also have a substitution operation on types.
If Γ; Φ F is a type, if Γ and Φ contain only type variables of arity 0, and if k = 0 for every occurrence of φ k bound by µ in F , then we say that F is first-order ; otherwise we say that F is second-order. Substitution for first-order types is the usual capture-avoiding textual substitution. We write H[α := F ] for the result of substituting F for α in H, and H[α 1 := F 1 , . . . , α k := F k ], or H[α := F ] when convenient, for H[α 1 := F 1 ][α 2 := F 2 , . . . , α k := F k ]. The operation of second-order type substitution along α is given in Definition 2.2, where we adopt a similar notational convention for vectors of types. Of course, (·)[φ 0 := ∅ F ] coincides with first-order substitution. We omit α when convenient, but note that it is not correct to substitute along non-functorial variables.   The idea is that the arguments to φ get substituted for the variables in α in each F replacing an occurrence of φ. It is not hard to see that Γ; Φ H[φ := α F ].
2.2. Terms. To define our term calculus we assume an infinite set V of term variables disjoint from T and F. If Γ is a type constructor context and Φ is a functorial context, then a term context for Γ and Φ is a finite set of bindings of the form x : F , where x ∈ V and Γ; Φ F . We adopt the above conventions for denoting disjoint unions and vectors in term contexts. If ∆ is a term context for Γ and Φ, then the formation rules for the set of well-formed terms over ∆ are as in Figure 1. In the rule there for L α x.t, the L operator binds all occurrences of the type variables in α in the types of x and t, as well as all occurrences of x in t. In the rule for t K s there is one type in K for every functorial variable in α. In the rule for map F ,G H there is one type F and one type G for each functorial variable in φ. Moreover, for each φ k in φ the number of functorial variables in β in the judgments for its corresponding type F and G is k. In the rules for in H and fold F H , the functorial variables in β are fresh with respect to H, and there is one β for every α. Substitution for terms is the obvious extension of the usual capture-avoiding textual substitution, and the rules of Figure 1 ensure that weakening is respected. Below we form L-terms, which contain no free Vol. 17:4   PARAMETRICITY FOR PRIMITIVE NESTED TYPES AND GADTS  23:9 functorial variables, in empty functorial contexts whenever convenient. We similarly form map-, in-, and fold-terms, which contain no free functorial variables or free term variables, in empty functorial contexts and empty term contexts whenever convenient. The "extra" functorial variables γ in the rules for map F ,G H (i.e., those variables not affected by the substitution of φ) deserve comment. They allow us to map polymorphic functions over nested types. Suppose, for example, we want to map the polymorphic function flatten : Nat β (PTree β) (List β) over a list. Even in the absence of extra variables the instance of map required to map each non-functorial monomorphic instantiation of flatten over a list of perfect trees is well-formed: But in the absence of γ, the instance Γ; ∅ | ∅ map PTree β,List β List α : Nat ∅ (Nat β (PTree β) (List β)) (Nat β (List (PTree β)) (List (List β))) required to map the polymorphic flatten function over a list of perfect trees is not: indeed, the functorial contexts for F and G in the rule for map F,G H would have to be empty, but because the polymorphic flatten function is natural in β it cannot possibly have a type of the form Nat ∅ F G as would be required for it to be the function input to map. Untypeability of this instance of map is unsatisfactory in a polymorphic calculus, where we naturally expect to be able to manipulate entire polymorphic functions rather than just their monomorphic instances, but the "extra" variables γ remedy the situation, ensuring that the instance of map needed to map the polymorphic flatten function is typeable as follows: List α : Nat ∅ (Nat γ (PTree γ) (List γ)) (Nat γ (List (PTree γ)) (List (List γ))) Our calculus is expressive enough to define a function reversePTree : Nat α (PTree α) (PTree α) that reverses the order of the leaves in a perfect tree. This function maps, e.g., the perfect tree pnode (pnode (pleaf ((1, 2), (3, 4)))) to the perfect tree pnode (pnode (pleaf ((4, 3), (2, 1)))) i.e., maps ((1, 2), (3, 4)) to ((4, 3), (2, 1)). It can be defined as and pleaf , pnode, swap, and s are the terms PTree α ) ∅ swap) α t )} : Nat α (α + PTree (α × α)) PTree α respectively. Here, if Γ; ∅ | ∆ t : Nat α F G and Γ; ∅ | ∆ s : Nat α G H are terms then the composition s • t of t and s is defined by s • t = Γ; ∅ | ∆ L α x.s α (t α x) : Nat α F H. Our calculus can similarly define a rustle function that changes the placement of the data in a bush. This function maps, e.g., the bush bcons 0 (bcons (bcons 1 (bcons (bcons 2 bnil ) bnil )) (bcons (bcons (bcons 3 (bcons (bcons 4 bnil ) bnil )) bnil ) bnil )) to the bush bcons 4 (bcons (bcons 0 (bcons (bcons 3 bnil ) bnil )) (bcons (bcons (bcons 2 (bcons (bcons 1 bnil ) bnil )) bnil ) bnil )) It can be defined as fold Bush α 1+β×φ(φβ) : Nat ∅ (Nat α (1 + α × Bush (Bush α))) (Bush α)) (Nat α (Bush α) (Bush α)) and bnil , bcons, in −1 1+β×φ(φβ) , balg, and consalg are the terms : Nat α (Bush α) (1 + α × Bush (Bush α)) L α s.case s of { * → bnil α * ; (a, bba) → consalg α (a, bba)} : Nat α (1 + α × Bush(Bush α)) (Bush α) L α (a, bba).case ((in −1 1+β×φ(φβ) ) Bush α bba) of { * → bcons α (a, bnil Bush α * ); (ba, bbba) → case ((in −1 1+β×φ(φβ) ) α ba) of { * → bcons α (a, bcons Bush α (bnil α * , bbba)); (a , bba ) → bcons α (a , bcons Bush α (bcons α (a, bba ), bbba))}} : Nat α (α × Bush(Bush α)) (Bush α) respectively.
Unfortunately, our calculus cannot express types of recursive functions -such as a concatenation function for perfect trees or a zip function for bushes -that take as inputs a nested type and an argument of another type, both of which are parameterized over the same variable. The fundamental issue is that recursion is expressible only via fold, which produces natural transformations in some variables α from µ-types to other functors F . The restrictions on Nat-types entail that F cannot itself be a Nat-type containing α, so, e.g., Nat α (PTree α) (Nat ∅ (PTree α) (PTree (α × α))) is not well-formed. Uncurrying gives Nat α (PTree α × PTree α) (PTree (α × α)), which is well-formed, but fold cannot produce a term of this type because PTree α × PTree α is not a µ-type. Our calculus can, however, express types of recursive functions that take multiple nested types as arguments, provided they are parameterized over disjoint sets of type variables and the return type of the function is parameterized over only the variables occurring in the type of its final argument. Even for ADTs there is a difference between which folds over them we can type when they are viewed as ADTs (i.e., as fixpoints of first-order functors) versus as proper nested types (i.e., as fixpoints of higher-order functors). This is because, in the return type of fold, the arguments of the µ-type must be variables bound by Nat. For ADTs, the µ-type takes no arguments, making it possible to write recursive functions, such as a concatenation function for lists of type α; ∅ Nat ∅ (µβ.1 + α × β) (Nat ∅ (µβ.1 + α × β) (µβ.1 + α × β)). This is not possible for nested types -even when they are semantically equivalent to ADTs.
Interestingly, even some recursive functions of a single proper nested type -e.g., a reverse function for bushes that is a true involution -cannot be expressed as folds because the algebra arguments needed to define them are again recursive functions with types of the same problematic form as the type of, e.g., a zip function for perfect trees. Expressivity of folds for nested types has long been a vexing issue, and this is naturally inherited by our calculus. Adding more expressive recursion combinators could help, but since this is orthogonal to the issue of parametricity in the presence of primitive nested types we do not consider it further here.

Interpreting Types
We denote the category of sets and functions by Set. The category Rel has as its objects triples (A, B, R) where R is a relation between the objects A and B in Set, i.e., a subset of A × B, and has as its morphisms from (A, B, R) to (A , B , R ) pairs (f : A → A , g : B → B ) of morphisms in Set such that (f a, g b) ∈ R whenever (a, b) ∈ R. We write R : Rel(A, B) in place of (A, B, R) when convenient. If R : Rel(A, B) we write π 1 R and π 2 R for the domain A of R and the codomain B of R, respectively. If A : Set, then we write Eq A = (A, A, {(x, x) | x ∈ A}) for the equality relation on A.
The key idea underlying Reynolds' parametricity is to give each type F (α) with one free variable α both an object interpretation F 0 taking sets to sets and a relational interpretation F 1 taking relations R : Rel(A, B) to relations F 1 (R) : Rel(F 0 (A), F 0 (B)), and to interpret each term t(α, x) : F (α) with one free term variable x : G(α) as a map t 0 associating to each set A a function t 0 (A) : G 0 (A) → F 0 (A). These interpretations are to be given inductively on the structures of F and t in such a way that they imply two fundamental theorems. The first is an Identity Extension Lemma, which states that F 1 (Eq A ) = Eq F 0 (A) , and is the essential property that makes a model relationally parametric rather than just induced by a logical relation. The second is an Abstraction Theorem, which states that, for any R : Rel(A, B), The Identity Extension Lemma is similar to the Abstraction Theorem except that it holds for all elements of a type's interpretation, not just those that are interpretations of terms. Similar theorems are expected to hold for types and terms with any number of free variables.
The key to proving the Identity Extension Lemma in our setting (Theorem 4.4) is a familiar "cutting down" of the interpretations of universally quantified types to include only the "parametric" elements; the relevant types in our calculus are the Nat-types. This cutting down requires, as usual, that the set interpretations of types (Section 3.1) are defined simultaneously with their relational interpretations (Section 3.2). While the set interpretations are relatively straightforward, their relation interpretations are less so, mainly because of the cocontinuity conditions required to ensure that they are well-defined. We develop these conditions in Sections 3.1 and 3.2. This separates our set and relational interpretations in space, but otherwise has no impact on the fact that they are given by mutual induction.
3.1. Interpreting Types as Sets. We interpret types in our calculus as ω-cocontinuous functors on locally finitely presentable categories [AR94]. Both Set and Rel are locally finitely presentable categories. Since functor categories of locally finitely presentable categories are again locally finitely presentable, the fixpoints interpreting µ-types in Set and Rel must all exist, and thus the set and relational interpretations of all of the types in Definition 2.1, are well-defined [JP19]. To bootstrap this process, we interpret type variables as ω-cocontinuous functors in Definitions 3.1 and 3.9. If C and D are locally finitely presentable categories, we write [C, D] for the category of ω-cocontinuous functors from C to D.
Definition 3.1. A set environment maps each type variable in T k ∪ F k to an element of [Set k , Set]. A morphism f : ρ → ρ for set environments ρ and ρ with ρ| T = ρ | T maps each type constructor variable ψ k ∈ T to the identity natural transformation on ρψ k = ρ ψ k and each functorial variable φ k ∈ F to a natural transformation from the k-ary functor ρφ k on Set to the k-ary functor ρ φ k on Set. Composition of morphisms on set environments is given componentwise, with the identity morphism mapping each set environment to itself. This gives a category of set environments and morphisms between them, denoted SetEnv.
We can now define our set interpretation. Its action on objects of SetEnv is given in Definition 3.2, and its action on morphisms of SetEnv is given in Definition 3.3. If ρ is a set environment we write Eq ρ for the equality relation environment such that Eq ρ φ = Eq ρφ for every type variable φ; see Definitions 3.4 and 3.9 for the definitions of a relation transformer and a relation environment, and Equation 4.1 for the definition of the relation transformer Eq F on a functor F . Equality relation environments appear in the third clause of Definition 3.2. The relational interpretation also appearing in the third clause of Definition 3.2 is given in Definition 3.11.
Definition 3.2. The set interpretation · Set : F → [SetEnv, Set] is defined by: If ρ ∈ SetEnv and F then we write F Set instead of F Set ρ since the environment is immaterial. The third clause of Definition 3.2 does indeed define a set: local finite presentability of Set and ω-cocontinuity of Γ; α F Set ensure that {η : λA. Γ; α F Set ρ[α := A] ⇒ λA. Γ; α G Set ρ[α := A]} (containing Γ; ∅ Nat α F G Set ρ) can be embedded in the set S=(S 1 ,...,S |α| ) S 1 ,...,S |α| are finite cardinals Also, Γ; ∅ Nat α F G Set is ω-cocontinuous since it is constant (in particular, on ω-directed sets). This is because Definition 3.1 ensures that restrictions to T k of morphisms between set environments are identities. Interpretations of Nat-types ensure that the interpretations Γ F → G Set and Γ ∀α.F Set of the System F arrow types and ∀-types representable in our calculus are as expected in any parametric model.
To make sense of the last clause in Definition 3.2, we need to know that, for each ρ ∈ SetEnv, T Set H,ρ is an ω-cocontinuous endofunctor on [Set k , Set], and thus admits a fixpoint. Since T Set H,ρ is defined in terms of Γ; φ, α H Set , this means that interpretations of types must be such functors, which in turn means that the actions of set interpretations of types on objects and on morphisms in SetEnv are intertwined. Fortunately, we know from [JP19] that, for every Γ; α G, Γ; α G Set is actually in [Set k , Set] where k = |α|. This means that for each Γ; φ k , α H Set , the corresponding operator T Set H can be extended to a functor from SetEnv to [[Set k , Set], [Set k , Set]]. The action of T Set H on an object ρ ∈ SetEnv is given by the higher-order functor T Set H,ρ , whose actions on objects (functors in [Set k , Set]) and morphisms (natural transformations) between them are given in Definition 3.2. Its action on a morphism f : ρ → ρ is the higher-order natural transformation T Set H,f : T Set H,ρ → T Set H,ρ whose action on Note that this action of T Set H,ρ on morphisms appears in the final clause of Definition 3.3 below. It will indeed be well-defined there because the functorial action of Γ; φ, α H Set will already be available from the induction hypothesis on H.
Using T Set H , we can define the functorial action of set interpretation.
Definition 3.3. Let f : ρ → ρ be a morphism between set environments ρ and ρ (so that ρ| T = ρ | T ). The action Γ; Φ F Set f of Γ; Φ F Set on f is given by: The latter equality holds because ρφ and ρ φ are functors and f φ : ρφ → ρ φ is a natural transformation, so the following naturality square commutes: H,ρ is a natural transformation, so the following naturality square commutes: Definitions 3.2 and 3.3 respect weakening, i.e., ensure that a type and its weakenings have the same set interpretations.
We define F R to be F * R and F (α, β) to be F * (α, β).
The last clause of Definition 3.4 expands to: if (a, b) ∈ R implies (α a, β b) ∈ S then (c, d) ∈ F * R implies (F 1 α c, F 2 β d) ∈ F * S. When convenient we identify a 0-ary relation transformer (A, B, R) with R : Rel(A, B), and write π 1 F for F 1 and π 2 F for F 2 . Below we extend these conventions to relation environments in the obvious ways.
Definition 3.5. The category RT k of k-ary relation transformers is given by the following data: • An object of RT k is a relation transformer • Identity morphisms and composition are inherited from the category of functors on Set Definition 3.6 entails that every higher-order relation transformer H is an endofunctor on RT k , where • The action of H on objects is given by Definition 3.7. A morphism σ : H → K between higher-order relation transformers H and K on RT k is a pair σ = (σ 1 , σ 2 ), where σ 1 : H 1 → K 1 and σ 2 : H 2 → K 2 are natural transformations between endofunctors on [Set k , Set] such that ((σ 1 Definition 3.7 entails that a morphism σ between higher-order relation transformers is a natural transformation between endofunctors on RT k whose component at We define a higher-order Rel]]. Now, for any k, any A : Set, and any R : Rel(A, B), let K Set A be the constantly A-valued functor from Set k to Set and K Rel R be the constantly R-valued functor from Rel k to Rel. Also let 0 denote either the initial object of either Set or Rel, as appropriate. Observing that, for every k, K Set 0 is initial in [Set k , Set], and K Rel 0 is initial in [Rel k , Rel], we have that, for each k, K 0 = (K Set 0 , K Set 0 , K Rel 0 ) is initial in RT k . Thus, if T = (T 1 , T 2 , T * ) : RT k → RT k is a higher-order relation transformer on RT k then we can define the relation transformer µT to be lim − →n∈N T n K 0 . It is not hard to see that µT is given explicitly as Moreover, µT really is a fixpoint for T if T is ω-cocontinuous: The isomorphism is given by the morphisms (in 1 , in 2 ) : The latter is always a morphism in RT k , but the former need not be if T is not ω-cocontinuous.
It is worth noting that the third component in Equation (3.3) is the colimit in [Rel k , Rel] of third components of relation transformers, rather than a fixpoint of an endofunctor on [Rel k , Rel]. There is thus an asymmetry between the first two components of µT and its third component, which reflects the important conceptual observation that the third component of a higher-order relation transformer on RT k need not be a functor on all of [Rel k , Rel]. In particular, although we can define T H,ρ F for a relation transformer F in Definition 3.11 below, it is not clear how we could define it for an arbitrary F : [Rel k , Rel].
Definition 3.9. A relation environment maps each type variable in T k ∪ F k to a k-ary relation transformer. A morphism f : ρ → ρ between relation environments ρ and ρ with ρ| T = ρ | T maps each type constructor variable ψ k ∈ T to the identity morphism on ρψ k = ρ ψ k and each functorial variable φ k ∈ F to a morphism from the k-ary relation transformer ρφ to the k-ary relation transformer ρ φ. Composition of morphisms on relation environments is given componentwise, with the identity morphism mapping each relation environment to itself. This gives a category of relation environments and morphisms between them, denoted RelEnv.
We identify a 0-ary relation transformer with the relation (transformer) that is its codomain and consider a relation environment to map a type variable of arity 0 to a relation. We write ρ[α := R] for the relation environment ρ such that ρ α i = R i for i = 1, . . . , k and ρ α = ρα if α ∈ {α 1 , . . . , α k }. If ρ is a relation environment, we write π 1 ρ and π 2 ρ for the set environments mapping each type variable φ to the functors (ρφ) 1 and (ρφ) 2 , respectively.
Definition 3.10. For each k, an ω-cocontinuous environment transformer H is a triple Definition 3.10 entails that every ω-cocontinuous environment transformer H is a ωcocontinuous functor from RelEnv to RT k , where • The action of H on ρ in RelEnv is given by Hρ = (H 1 (π 1 ρ), H 2 (π 2 ρ), H * ρ) • The action of H on morphisms f : ρ → ρ in RelEnv is given by Hf = (H 1 (π 1 f ), H 2 (π 2 f )) Note that the last condition of Definition 3.10 entails that if f : Considering RelEnv as a subcategory of the product Π φ k ∈T∪F RT k , computation of ωdirected colimits in RT k extends componentwise to RelEnv. Recalling from the start of this subsection that Definition 3.11 is given mutually inductively with Definition 3.2 we can now define our relational interpretation. As with the set interpretation, the relational interpretation is given in two parts, in Definitions 3.11 and 3.13.
Definition 3.11. The relational interpretation · Rel : F → [RelEnv, Rel] is defined by The interpretations in Definitions 3.11 and 3.13 below respect weakening, and also ensure that the interpretations Γ F → G Rel and Γ ∀α.F Rel of the System F arrow types and ∀-types representable in our calculus are as expected in any parametric model. As for set interpretations, Γ; ∅ Nat α F G Rel is ω-cocontinuous; indeed, it is constant (on ω-directed sets) because Definition 3.9 ensures that restrictions to T k of morphisms between relational environments are identities. If ρ ∈ RelEnv and F , then we write F Rel instead of F Rel ρ. For the last clause in Definition 3.11 to be well-defined we need T H,ρ to be an ω-cocontinuous higher-order relation transformer on RT k , where k is the arity of φ, so that, by Lemma 3.8, it admits a fixpoint. Since T H,ρ is defined in terms of Γ; φ, α H Rel , this means that relational interpretations of types must be ω-cocontinuous environment transformer from RelEnv to RT 0 , which in turn entails that the actions of relational interpretations of types on objects and on morphisms in RelEnv are intertwined. As for set interpretations, we know from [JP19] that, for every Γ; α F , Γ; α F Rel is actually in [Rel k , Rel] where k = |α|. We first define the actions of each of these functors on morphisms between environments, and then argue that they are well-defined and have the required properties. To do this, we show that T H = (T Set H , T Set H , T Rel H ) is a higher-order ω-cocontinuous environment transformer, as defined by Definition 3.12. A higher-order ω-cocontinuous environment transformer is a triple H = (H 1 , H 2 , H * ), where

P. Johann and E. Ghiorzi
Vol. 17:4 • H * ρ (δ 1 , δ 2 ) is a morphism in RT k whenever (δ 1 , δ 2 ) is a morphism in RT k and, moreover, Definition 3.12 entails that every higher-order ω-cocontinuous environment transformer H is a ω-cocontinuous functor from RelEnv to the category of ω-cocontinuous higher-order relation transformers on RT k , where • The action of H on ρ in RelEnv is given by Note that the last condition of Definition 3.12 entails that if f : is a higher-order ω-cocontinuous environment transformer follows from the analogue for relations of the argument immediately preceding Definition 3.3, together with the observations that the action of T Rel H,ρ on morphisms will be well-defined by Definition 3.13, and the functorial action of Γ; φ, α H Rel will already be available in the final clause of Definition 3.12 from the induction hypothesis on H and Lemma 3.14 below. The action of T H on an object ρ ∈ RelEnv is given by the ωcocontinuous higher-order relation transformer T H,ρ whose actions on objects and morphisms are given in Definition 3.12. The action of T H on a morphism f : ρ → ρ is the morphism T H,f : T H,ρ → T H,ρ between higher-order relation transformers whose action on any F ∈ RT k is the morphism of relation transfomers T H,f F : Using T H , we can define the functorial action of relational interpretation.
Definition 3.13. Let f : ρ → ρ for relation environments ρ and ρ (so that ρ| T = ρ | T ). The action Γ; Φ F Rel f of Γ; Φ F Rel on the morphism f is given exactly as in Definition 3.3, except that all interpretations are relational interpretations and all occurrences of T Set H,f are replaced by T H,f .
, then this is an immediate consequence of Lemma 3.14. For every Γ; Φ F , Γ; Φ F is an ω-cocontinuous environment transformer.
The proof is a straightforward induction on the structure of F , using an appropriate result from [JP19] to deduce ω-cocontinuity of Γ; Φ F in each case, together with Lemma 3.8 and Equation 3.3 for µ-types. We note that, for any relation environment ρ, Γ; Φ F ρ ∈ RT 0 .
We can prove by simultaneous induction that our interpretations of types interact well with demotion of functorial variables to non-functorial ones, along with other useful identities.
Identities analogous to (3.4) through (3.9) hold for relational interpretations as well.

The Identity Extension Lemma
In most treatments of parametricity, equality relations on sets are taken as given -either directly as diagonal relations, or perhaps via reflexive graphs if kinds are also being tracked -and the graph relations used to validate existence of initial algebras are defined in terms of them. We take a different approach here, giving a categorical definition of graph relations for morphisms (i.e., natural transformations) between functors and constructing equality relations as particular graph relations. Our definitions specialize to the usual ones for the graph relation for morphisms between sets and equality relations on sets. In light of its novelty, we spell out our construction in detail. The standard definition of the graph for a morphism f : A → B in Set is the relation f : Rel(A, B) defined by (x, y) ∈ f iff f x = y. This definition naturally generalizes to associate to each natural transformation between k-ary functors on Set a k-ary relation transformer as follows: Definition 4.1. If F, G : Set k → Set and α : F → G is a natural transformation, then the functor α * : Rel k → Rel is defined as follows. Given R 1 : Further, let α ∧ R be the subobject through which h R is factorized by the mono-epi factorization system in Set, as shown in the following diagram: Then α ∧ R : Rel(F A, GB) by construction, so the action of α * on objects can be given by α * (A, B, R) = (F A, GB, ι α ∧ R α ∧ R). Its action on morphisms is given by α * (β, β ) = (F β, Gβ ). The data in Definition 4.1 yield the graph relation transformer for α, denoted α = (F, G, α * ).
We want to show that there exists a morphism : α ∧ R → α ∧ S such that the diagram on the left below commutes. Since (β, β ) : R → S, there exist γ : R → S such that each diagram in the middle commutes. Moreover, since both h C×D • F (β × β ) and (F β × Gβ ) • h A×B make the diagram on the right commute, they must be equal.
We therefore get that the right-hand square in the diagram on the left below commutes, and thus that the entire diagram does as well. Finally, by the left-lifting property of q F ∧ R with respect to ι F ∧ S given by the mono-epi factorization system, there exists an such that the diagram on the right below commutes as desired.
Set then the definition of the graph relation transformer f for f as a natural transformation between 0-ary functors A and B coincides with its standard definition. Graph relation transformers are thus a reasonable extension of graph relations to functors.
The action of a graph relation transformer on a graph relation can be computed explicitly: Proof. Since h A×B is the unique morphism making the bottom triangle of the diagram on the left below commute, and since To prove the IEL, we also need to know that the equality relation transformer preserves equality relations. The equality relation transformer on F : [Set k , Set] is defined to be Lemma 4.3 then gives that, for all A : Set, Graph relation transformers in general, and equality relation transformers in particular, naturally extend to relation environments. Indeed, if ρ, ρ : SetEnv and f : ρ → ρ , then the graph relation environment f is defined pointwise by f φ = f φ for every φ, which entails that π 1 f = ρ and π 2 f = ρ . In particular, the equality relation environment Eq ρ is defined to be id ρ , which entails that Eq ρ φ = Eq ρφ for every φ.
With these definitions in hand, we can state and prove both an Identity Extension Lemma and a Graph Lemma for our calculus. Proof. The proof is by induction on the structure of F . Only the Nat, application, and fixpoint cases are non-routine. • Γ; Φ 0 Rel Eq ρ = 0 Rel = Eq 0 Set = Eq Γ;Φ 0 Set ρ • Γ; Φ 1 Rel Eq ρ = 1 Rel = Eq 1 Set = Eq Γ;Φ 1 Set ρ • By definition, Γ; ∅ Nat α F G Rel Eq ρ is the relation on Γ; ∅ Nat α F G Set ρ relating t and t if, for all R 1 : Rel(A 1 , B 1 ), . . . , R k : To prove that this relation is • The fixpoint case is proven by the sequence of equalities Here, the third equality is by induction hypothesis, the fifth is by Equation 4.2, and the fourth equality is because, for every n ∈ N, the following two statements can be proved by simultaneous induction: for any H, ρ, and A, and for any subformula J of H, The inductive case is proved as follows. We prove Equation 4.3 by the following sequence of equalities: We prove Equation 4.4 by structural induction on J. The only interesting cases, though, are when J = φG and when J = (µψ.λβ.G)K.
-The case J = φG is proved by the sequence of equalities: Here, the second equality is by the induction hypothesis for Equation 4.4 on the Gs, the fourth is by the induction hypothesis for the IEL on the Gs, and the fifth is by Equation 4.3, which we have just proved.
-The case J = (µψ. λβ. G)K is proved by the sequence of equalities and the inductive case is proved by: Here, the second equality holds by the induction hypothesis for Equation 4.5 on m. The fourth equality holds because φ and the variables in α do not appear in G. (We note that this case of the IEL would fail to hold if the functorial context in which the body F of a µ-type µφ.λα.F were formed in Definition 2.1 were extended to contain variables other than φ and the αs. This justifies our design of the µ-type formation rule there.) This concludes the fixpoint case.
Expanding the second equation gives that if x ∈ Γ; Φ F Set ρ then

Interpreting Terms
If ∆ = x 1 : F 1 , . . . , x n : F n is a term context for Γ and Φ, then the interpretations Γ; Φ ∆ Set and Γ; Φ ∆ Rel are defined by

Rel
Every well-formed term Γ; Φ | ∆ t : F then has, for every ρ ∈ SetEnv, a set interpretation Γ; Φ | ∆ t : F Set ρ as the component at ρ of a natural transformation from Γ; Φ ∆ Set to Γ; Φ F Set , and, for every ρ ∈ RelEnv, a relational interpretation Γ; Φ | ∆ t : F Rel ρ as the component at ρ of a natural transformation from Γ; Φ ∆ Rel to Γ; Φ F Rel .
If t is closed, i.e., if ∅; ∅ | ∅ t : F , then we write t : F D instead of ∅; ∅ | ∅ t : F D . The interpretations in Definition 5.1 respect weakening, i.e., a term and its weakenings all have the same set and relational interpretations. Specifically, for any ρ ∈ SetEnv, where π ∆ is the projection Γ; Φ ∆, x : F Set → Γ; Φ ∆ Set . A similar result holds for relational interpretations.
The return type for the semantic fold is Γ; β F D ρ[β := B]. This interpretation gives so it specializes to the standard interpretations of those System F terms that are representable in our calculus. Term interpretation also respects substitution for both functorial and nonfunctorial type variables, as well as term substitution. That is, if Γ, α; Φ | ∆ t : where X is as above Theorem 6.1 gives rise to an entire family of free theorems that are consequences of naturality, and thus do not require the full power of parametricity. In particular, Definition 5.1 alone ensures that standard properties of the initial algebraic constructs map, in, and fold hold. We have, for example, that the interpretation of every map H is a functor, i.e., if Γ; α, γ H, Γ; ∅ | ∆ g : Nat γ F G, and Γ; ∅ | ∆ f : Nat γ G K, then In fact, we have that if Γ; α H, Γ; ∅ F , and Γ; ∅ G, then, for all f ∈ Γ; ∅ Nat ∅ F G Set ρ, Here, we obtain the first equality from the appropriate instance of Definition 5.1, and the second one by noting that λA. Γ; α H Set ρ[α := A] is a functor in A and using map G f to denote the action of the semantic functor G on morphisms f . We also have, e.g., that: • map is a higher-order functor, i.e., if Γ; ψ, γ H, Γ; α, γ, φ K, Γ; β, γ F , Γ; β, γ G,  6.2. The Abstraction Theorem. To get consequences of parametricity that are not merely consequences of naturality, we prove an Abstraction Theorem (Theorem 6.4) for our calculus. As is usual for such theorems, we first prove a more general result (Theorem 6. 2) for open terms, and recover our Abstraction Theorem as a special case of it for closed terms of closed type.
Theorem 6.2. Every well-formed term Γ; Φ | ∆ t : F induces a morphism from Γ; Φ ∆ to Γ; Φ F , i.e., a triple of natural transformations where, for D ∈ {Set, Rel} and for ρ ∈ SetEnv or ρ ∈ RelEnv as appropriate, Moreover, for all ρ : RelEnv, Proof. The proof is by induction on t. It requires showing that set and relational interpretations of term judgments are natural transformations, and that all set interpretations of terms of Nat types satisfy the appropriate equality preservation conditions from Definition 3.2.
For the interesting cases of abstraction, application, map, in, and fold terms, propagating the naturality conditions is quite involved; the latter two especially require some rather delicate diagram chasing. That it is possible provides strong evidence that our development is sensible, natural, and at an appropriate level of abstraction. The only interesting cases are the cases for abstraction, application, map, in, and fold. We omit the others. • Γ; ∅ | ∆ L α x.t : Nat α F G To see that Γ; ∅ | ∆ L α x.t : Nat α F G Set is a natural transformation from Γ; ∅ ∆ Set to Γ; ∅ Nat α F G Set we need to show that, for every ρ : SetEnv, Γ; ∅ | ∆ L α x.t : Nat α F G Set ρ is a morphism in Set from Γ; ∅ ∆ Set ρ to Γ; ∅ Nat α F G Set ρ, and that such a family of morphisms is natural. First, we need to show that, for all A : Set and all d : but this follows easily from the induction hypothesis. That these maps comprise a natural transformation η : Γ; α F Set ρ[α := ] → Γ; α G Set ρ[α := ] is clear since Then the induction hypothesis and (d, Moreover, Γ; ∅ | ∆ L α x.t : Nat α F G Set ρ is trivially natural in ρ, as the functorial action of Γ; ∅ ∆ Set and Γ; ∅ Nat α F G Set on morphisms is the identity.
The induction hypothesis ensures that The top diagram commutes because g and h are natural in ρ by the induction hypothesis. To see that the bottom diagram commutes, we need to show that holds for all η ∈ Γ; ∅ Nat α F G Set ρ and x ∈ Γ; Φ F [α := K] Set ρ, i.e., that for all η ∈ Γ; ∅ Nat α F G Set ρ. But this follows from the naturality of η, which indeed ensures the commutativity of  commutes for all f : B → B ': The following theorem is an immediate consequence of Theorem 6.2: Finally, the Abstraction Theorem is the instantiation of Theorem 6.3 to closed terms of closed type: Theorem 6.4 (Abstraction Theorem). If t : F , then ( t : F Set , t : F Set ) ∈ F Rel .

Free Theorems for Nested Types
In this section we show how Theorem 6.2 and its consequences can be used to prove free theorems. Those in Sections 7.3, 7.4, and 7.5 go beyond mere naturality. We also show that we can extend short cut fusion for lists [GLP93] to nested types, thereby formally proving correctness of the categorically inspired theorem from [JG10]. 7.1. Free Theorem for Type of Polymorphic Bottom. Suppose g : Nat α 1 α and g Set = g : Nat α 1 α Set . Then g Set is a natural transformation from the constantly 1valued functor to the identity functor in Set. In particular, for every S : Set, g Set S : 1 → S. Note, however, that if S = ∅, then there can be no such morphism, so no such natural transformation, and thus no term g : Nat α 1 α, can exist. That is, our calculus admits no (non-terminating) terms with the closed type Nat α 1 α of the polymorphic bottom. 7.2. Free Theorem for Type of Polymorphic Identity. Suppose g : Nat α α α and g Set = g : Nat α α α Set . Then g Set is a natural transformation from the identity functor on Set to itself. If S is any set, if a is any element of S, and if K a : S → S is the constantly a-valued morphism on S, then naturality of g Set gives that g Set S • K a = K a • g Set S , i.e., g Set S a = a, i.e., g Set S = id S . That is, g Set is the identity natural transformation for the identity functor on Set. So every closed term g of closed type Nat α α α always denotes the identity natural transformation for the identity functor on Set, i.e., every closed term g of type Nat α α α denotes the polymorphic identity function. 7.3. Standard Free Theorems for ADTs and Nested Types. We can derive in our calculus even those free theorems for polymorphic functions over ADTs that are not consequences of naturality. We can, e.g., prove the free theorem for filter 's type as follows: Theorem 7.1. If g : A → B, ρ ∈ RelEnv, ρα = (A, B, g ), (a, b) ∈ α; ∅ ∆ Rel ρ, (s • g, s) ∈ α; ∅ Nat ∅ α Bool Rel ρ, and filter = α; ∅ | ∆ t : Nat ∅ (Nat ∅ α Bool ) (Nat ∅ (List α) (List α)) Set for some t, then map List g • filter (π 1 ρ) a (s • g) = filter (π 2 ρ) b s • map List g Proof. By Theorem 6.4, (filter (π 1 ρ) a, filter (π 2 ρ) b) ∈ α; ∅ Nat ∅ (Nat ∅ α Bool ) (Nat ∅ (List α) (List α)) Rel ρ so if (s , s) ∈ α; ∅ Nat ∅ α Bool Rel ρ = ρα → Eq Bool and (xs , xs) ∈ α; ∅ List α Rel ρ then (filter (π 1 ρ) a s xs , filter (π 2 ρ) b s xs) ∈ α; ∅ List α Rel ρ (7.1) If ρα = (A, B, g ), then α; ∅ List α Rel ρ = map List g by Lemma 4.5 and demotion. Moreover, xs = map List g xs and (s , s) ∈ g → Eq Bool , so s = s • g. The result follows from Equation 7.1. A similar proof establishes the analogous result for, say, generalized rose trees.
As in [JG10], replacing 1 with any type ∅; α C generalizes Theorem 7.4 to deliver a more general free theorem whose conclusion is fold H B •ĝ µH in H =ĝ ∅; α K Set B.
Although it is standard to prove that the parametric model constructed verifies the existence of initial algebras, this is unnecessary here since initial algebras are built directly into our model.

Parametricity for GADTs
As discussed in Section 1, type indices for nested types can be any types, including, in the case of truly nested types like that of bushes, types involving the very same nested type that is being defined. But every data constructor for a nested type must still have as its return type exactly the instance being defined. For example, the data constructors for the instance PTree A of the nested type PTree are pleaf :: PTree A and pnode :: PTree (A × A) → PTree A, and the data constructors for the instance Bush A of the truly nested type Bush are bnil :: Bush A and bcons :: Generalized algebraic data types (GADTs) -also known as guarded recursive data types [XCC03] or first-class phantom types [CH03] -generalize nested types by relaxing the above restriction to allow the return types of data constructors to be different instances of the data type than the one being defined. For example, the GADT data Seq (A : Set) : Set where sconst : has data constructors spair and sseq with return types Seq (A × B) and Seq (Nat → A). These types are not only at different instances of Seq from the instance Seq A being defined, but also at different instances from one another. The resulting interdependence of different instances of GADTs means that they can express more constraints than ADTs and nested types. For example, the ADT List expresses the invariant that all of the data in the lists it defines is of the same type, while the nested type PTree expresses this invariant as well as the invariant that all of the lists it defines have lengths that are powers of 2. The GADT Seq enforces even more general well-formedness conditions for sequences of values that simply cannot be expressed with ADTs and nested types alone. GADTs are widely used in modern functional languages, such as Haskell, as well as in proof assistants, such as Agda, that are based on dependent type theories. A natural next step in the line of work reported in this paper is therefore to extend our parametricity results to GADTs. A promising starting point for this endeavor is the observation from [JG08] that the data objects of GADTs can be represented using object-level left Kan extensions over discrete categories. The more recent results of [JP19] further show that adding a carefully designed object-level left Kan extension construct to a calculus supporting primitive nested types preserves the cocontinuity needed for primitive GADTs to have well-defined, properly functorial interpretations. Together this suggests extending the type system in Definition 2.1 with such a left Kan extension construct, and extending the calculus in Figure 1 with corresponding categorically inspired constructs to introduce and eliminate terms of these richer types. This approach exactly mirrors the (entirely standard) approach taken above for product, coproduct, and fixpoint types. In this section we outline the obvious approach to extending our model from Section 5 to a parametric model when some classes of primitive GADTs are incorporated in this manner. However, as we argue at the end of this section, this naive approach fails because the IEL does not hold. Unfortunately, the IEL not holding derails more than just parametricity: it also affects the well-definedness of the term semantics (in particular, the semantics of L-terms).

Left Kan Extensions.
We begin by recalling the definition of a left Kan extension and establishing some useful notation and results for them.
Definition 8.1. If F : Set k → Set and K : Set k → Set h are functors over Set, then the left Kan extension of F along K is a functor Lan K F : Set h → Set together with a natural transformation η : F → (Lan K F )•K such that, for every functor G : Set h → Set and natural transformation γ : F → G • K, there exists a unique natural transformation µ : Lan K F → G such that (µK) • η = γ. This is depicted in the following diagram: An alternative presentation characterizes the left Kan extension (Lan K F, η) in terms of the bijection between natural transformations from F to G • K and natural transformations from Lan K F to G, for which η is the unit. If Agda were to support a primitive Lan for left Kan extensions, we could use this bijection to rewrite the type of each data constructor for the GADT Seq to arrive at the following equivalent representation: In that setting we will denote the morphism mapping the cocone into the colimit ι. We will make good use of both of these representations in Section 8.4 below. More about each of the above three representations of left Kan extensions and the connections between them can be found in, e.g., [Rie16].
8.2. Extending the Calculus. We incorporate GADTs into our calculus by first adding to Definition 2.1 the following type formation rule for Lan-types: Here, the type constructor Lan binds the variables in α, and these variables must always have arity 0. In addition, the vectors K and A must have the same length.
Intuitively, Lan α 0 K F is a syntactic representation of the left Kan extension of the functor in the variables in α denoted by F along the functor in the variables in α denoted by K.
Using Lan-types, we can therefore represent the GADT Seq in our calculus as Seq α := µφ.λβ.β + Lan γ 1 ,γ 2 γ 1 ×γ 2 (φγ 1 × φγ 2 ) β + Lan γ Nat→γ (Nat → φγ) β α where Nat = µα.1 + α and Nat → C abbreviates (Lan ∅ Nat 1) C, which equals lim − →f:Nat→C 1 by Equation 8.1 (here, k = 0). This representation of Seq corresponds exactly to the rewriting in Section 8.1. As explained in Section IV. D of [JP19], the more general construct Lan α 0 K F allowing extensions along vectors of functors as depicted above makes it possible to represent GADTs with two or more type arguments that depend on one another. Such GADTs cannot be represented using just unary Lan-types, i.e, Lan-types of the form (Lan α 0 K F )A. As we will see below, in order to define the term-formation rules for terms of Lan-types we also need to generalize Nat-types to bind type constructor variables not just of arity 0, but of arbitrary arity. Accordingly, we replace the rule for Nat-types in Definition 2.1 with Γ; Φ F Γ; Φ G Γ; ∅ Nat Φ F G Of course, we could have performed this replacement from the outset of the present paper. But since using the above rule instead of the one currently in Definition 2.1 only makes the notation of our calculus heavier without increasing its expressivity, we have chosen not to.
Finally, adding Lan-types to the types of our calculus also requires the extension of Definition 2.2 with the following new clause: We must also extend our term calculus to accommodate GADTs. To this end, we first give introduction and elimination rules appropriate to our generalized Nat-types, replacing the tenth and eleventh rules in Figure 1 with respectively. Next, we add to the term calculus in Figure 1 introduction and elimination rules for terms of Lan-types according to Definition 8.1. This gives the rules Note that both of these rules make essential use of our generalized Nat-types. If Φ were required to be empty then we would not be able to express, e.g., the types and Nat φ,β (Lan γ,δ γ×δ (φγ × φδ))β (φβ) associated with Seq's data constructor spair.

Extending the Type Semantics.
To construct a parametric model of the kind we seek, we must give suitable interpretations of our generalized Nat-types and Lan-types, as well as of terms of both such types, in both Set and Rel. (Note that it is having both Set and Rel interpretations, and having these be interconnected in the specific way parametricity demands, that is at issue here; if we are only interested in having some model of the type calculus, then the one in [JP19] will do.) Recalling that the type constructor variables bound by Nat in the formation rule for generalized Nat-types are interpreted in Set as functors and in Rel as relation transformers, we let k i be the arity of the i th element of the sequence Φ and extend the set and relational interpretations for Nat types from Section 3 as follows: respectively. But in order to guarantee that the IEL, and thus parametricity, continues to hold for the model we are constructing for our extended calculus, we might think we have to cut down the set interpretations of Lan-types by restricting to the subcollection of those interpretations that are well-behaved with respect to the IEL. This would mirror the cutting down we have already seen in the set interpretations of both our original Nat-types and our of generalized Nat-types. However, as we will see in Proposition 8.2 below, this will not be possible if we want the set and relational interpretations of our Lan-types to include the set and relational interpretations of the terms given by the Lan-introduction rule. Since we do indeed want this, the upshot is that the above interpretations of Lan-types are, in fact, the only possibility. We must also define the functorial actions of the above set and relational interpretations of Lan-types on morphisms. If f : ρ → ρ is a morphism of set environments, then or, equivalently by naturality, The functorial action for the relational interpretations of Lan-types is defined analogously. 8.4. Extending the Term Semantics. Recalling the notation for the colimit representation of left Kan extensions from Section 8.1, we can define the set and relational interpretations of the terms from the introduction and elimination rules for Lan types from Section 8.2 by is the unique natural transformation such that as given by the universal property of the left Kan extension. The relational interpretations Γ; ∅ | ∅ K,F : Nat Φ,α F (Lan α K F )K Rel and Γ; ∅ | ∆ ∂ G,K F t : Nat Φ,β (Lan α K F )β G Set can then be defined entirely analogously.
We conclude this subsection by showing, as promised above, that no cutting down of set and relational interpretations of Lan-types by taking subsets is possible. We have: Proposition 8.2. Let F : Set k → Set and K : Set k → Set h be functors. If L : Set h → Set is a functor such that LB for all f : A → B and x ∈ LA, and • η A y ∈ L(KA) for all A : Set and y ∈ F A, then L = Lan K F .
Proof. For all A : Set and z ∈ (Lan K F )A there exist S : Set 0 , f : KS → A, and w ∈ F S such that z = ι S,f w. Thus, z = ι S,f w = (Lan K F )f (η S w) by Equation 8.2. Then by the third assumption above we have that η S w ∈ L(KS), and by the second assumption above we have that z ∈ LA. This gives (Lan K F )A ⊆ LA. Finally, by the first assumption above, we therefore have that LA = (Lan K F )A. ]. An analogous result holds for relational interpretations of Lan-types. This shows that the machinery of this section does not give a well-defined relational semantics and, given the expected properties of the term semantics, this cannot be fixed by restricting the relational semantics of Lan-types from Section 8.3. 8.5. Parametricity and GADTs. Having extended our calculus with both Lan-types and terms of such types, and having given sensible set and relational interpretations for these types and terms, we now need to verify that these interpretations give rise to a parametric model. The first step in this process is to extend Lemma 3.14 to Lan-types by adding a clause for Lan-types to the proof. Unfortunately, however, Lemma 3.14 does not extend to arbitrary Lan-types, as the following example shows. because id 1 is the unique arrow from 1 to 1 in Set. Lemma 3.14 therefore cannot hold.
The problem in Example 8.3 lies in the fact that the type along whose interpretation we extend contains constants, i.e., subtypes constructed from 1. Indeed, if ∅; α K consists only of polynomial (i.e., sum-of-products) types not containing constants (i.e., formed only from +, ×, and the variables in α), then Lemma 3.14 actually does holds for Γ; Φ (Lan α K F )A. This is proved in the following proposition, which covers the case of Lemma 3.14 for such Lan-types.
The requirement that the types in K be constant-free polynomials is quite restrictive. In fact, it precludes the expression of many GADTs commonly used in practice, such as the following GADT Expr of typed expressions: Even when extending along constant-free polynomials, it is unclear how to prove the Identity Extension Lemma for Lan-types or, indeed, whether it holds at all in their presence. To prove the IEL for Nat-types, for example, it is necessary to cut down the semantic interpretation by requiring that the natural transformations in the set interpretation preserve equalities. The impossibility of restricting the semantic interpretations of Lan-types, as shown in Proposition 8.2, suggests that the IEL may fail for them. As noted above, this would derail not just parametricity, but the well-definedness of the term semantics as well. We have been able neither to prove nor disprove the IEL thus far, but our substantial and lengthy investigations into the issue lead us to suspect that it may not actually be possible to define a functorial semantics for Lan-types that gives rise to parametric models for languages supporting even constant-free polynomial primitive GADTs.

Conclusion and Directions for Future Work
We have constructed a parametric model for a calculus providing primitives for constructing nested types directly as fixpoints, rather than representing them via their Church encodings. We have also used the Abstraction Theorem for this model to derive free theorems for nested types. This was not possible before [JP19] because such types were not previously known to have well-defined interpretations in locally finitely presentable categories (here, Set and Rel), and, to our knowledge, no term calculus for them existed either. The key to obtaining our parametric model is the delicate threading of functoriality and its accompanying naturality conditions throughout our model construction.
We were surprised to find that, although GADTs were shown in [JP19] to have appropriately cocontinuous functorial semantics in terms of left Kan extensions, our model construction does not extend to give a parametric model for them. It may be possible to modify the categories of relations and relation transformers so that if (A, B, R) is a relation then π 1 : R → A and π 2 : R → B are always surjective; this would prohibit situations like that in Example 8.3, and might therefore allow us to recover our IEL and, ultimately, an Abstraction Theorem appropriate to GADTs. If this turns out to be possible, then generalizing the resulting model construction to locally λ-presentable categories for λ > ω would make it possible to handle broader classes of GADTs. (As shown in [JP19], λ > ω 1 is required to interpret even common GADTs.) We could even attempt to carry out our construction in locally λ-presentable cartesian closed categories (lpcccs) C whose categories of (abstract) relations, obtained by pullback as in [Jac99], are also lpcccs and are appropriately fibred over C. This would give a framework for constructing parametric models for calculi with primitive GADTs that is based on locally λ-presentable fibrations, for some appropriate definition thereof.
The expressivity of folds for nested types has long been a vexing issue (see, e.g., [BM98]), and this is naturally inherited by the calculus presented here. Since it codes all recursion using standard folds, and since folds for nested types must return natural transformations, many standard functions over nested types cannot be represented in this calculus. Another important direction for future work is therefore to improve the expressivity of our calculus by adding, say, generalized folds [BP99], or Mendler iterators [AMU05], or otherwise extending standard folds to express computations whose results are not natural transformations. In particular, we may wish to add term-level fixpoints as, e.g., in [Pit00]. This would require the categories interpreting types to be not just locally λ-presentable, but also to support some kind of domain structure. At the moment it seems that such an endeavor will have to wait for a generalization of the results presented here to at least locally ω 1 -presentable categories, however: ω-CPO, the most natural category of domains to replace Set, is not locally finitely presentable.