Internal parametricity, without an interval

Parametricity is a property of the syntax of type theory implying, e.g., that there is only one function having the type of the polymorphic identity function. Parametricity is usually proven externally, and does not hold internally. Internalising it is difficult because once there is a term witnessing parametricity, it also has to be parametric itself and this results in the appearance of higher dimensional cubes. In previous theories with internal parametricity, either an explicit syntax for higher cubes is present or the theory is extended with a new sort for the interval. In this paper we present a type theory with internal parametricity which is a simple extension of Martin-L\"of type theory: there are a few new type formers, term formers and equations. Geometry is not explicit in this syntax, but emergent: the new operations and equations only refer to objects up to dimension 3. We show that this theory is modelled by presheaves over the BCH cube category. Fibrancy conditions are not needed because we use span-based rather than relational parametricity. We define a gluing model for this theory implying that external parametricity and canonicity hold. The theory can be seen as a special case of a new kind of modal type theory, and it is the simplest setting in which the computational properties of higher observational type theory can be demonstrated.


INTRODUCTION
Parametricity was introduced by Reynolds [Reynolds 1983] as a theory of representation-independence for the polymorphic lambda calculus.The idea is that a polymorphic function has to work uniformly on all types, i.e., it cannot inspect its type arguments, and thus for example there are zero, one and two terms of types ∀ ., ∀ .→ and ∀ .→ → , respectively.This intuition is formalised by relation-preservation: each type is equipped with a relation (called logical relation), and one can prove by induction on the syntax that every term respects the relation corresponding to its type (called the fundamental lemma).Dependent types are expressive enough that they can formulate their own parametricity relations.This was used by [Bernardy et al. 2010] to define a parametricity translation for type theory.We describe this translation below.We expect that the reader is familiar with the syntax of type theory.
The external parametricity translation.By mutual induction on syntactic contexts, substitutions, types and terms, we define the following -P operations.On contexts, we further define operations 0 and 1 and we write when we mean either.On substitutions we mutually prove an equation.
Γ : Con Γ P : Con Γ : Sub Γ P Γ : Sub ∆ Γ P : Sub ∆ P Γ P • ∆ = Γ • P : Ty Γ P : Ty (Γ P , [0 Γ ], [1 Γ ]) The main point of this translation is to compute the logical relation P from a type (the third operation).For a closed type : Ty ⋄, we obtain a homogeneous binary relation P : Ty(⋄, , ), that is, a type depending on two variables of type .For an in a non-empty context, P is a heterogeneous relation which depends on the operations for contexts.Because types can include terms, we need to define -P mutually on terms and substitutions as well.We explain how these operations are defined for each sort.
• Γ P is a context that contains two copies of each type in Γ together with witnesses of their relatedness.The empty context ⋄ stays empty.For a context ending with , we obtain two copies of which are substituted by 0 and 1, respectively; finally we have a witness of relatedness.The projections 0 and 1 return the 0 and 1 components, respectively.
⋄ P := ⋄ (Γ, : ) P := Γ P , 0 : • A substitution is a list of terms, each variable in the codomain context is mapped to some term which we denote ↦ → .On the empty substitution -P is the identity, on a substitution into an extended context ( , ↦ → ) : Sub ∆ (Γ, : ), it is defined pointwise.
U P [ 0 , 1 ] := El 0 ⇒ El 1 ⇒ U (El ) P [ 0 , 1 ] := El ( P $ 0 $ 1 ) • The term P says that respects logical relations: if the relations hold for every dependency in the context, the relation P also holds for the two copies of , depending on the respective copies of Γ.Sometimes P is called the fundamental lemma for the term .Note that when we say "relation" we always mean proof-relevant relation (correspondence, or family of types).
The "hello world" example of parametricity.A term in Tm (⋄, : U, : El ) (El ) can only contain two free variables, and .Using the translation -P , we show that for any such term and closed terms and , the substituted term [ , ] is equal to .This is one way to formalise that ∀ .→ has only one element.In fact, the unary version of the translation is enough, so for this example we restrict ourself to the = 0 case and omit the 1 components (alternatively, we could fill the 1 components using dummy ⊤ arguments).Now P says that if there is a code for a type 0 , a predicate on elements of this type, and an element 0 for which the predicate holds, then the predicate will also hold for [ 0 , 0 ].P : Tm ⋄, 0 : U, 2 : El 0 ⇒ U, 0 : El 0 , 2 : El ( 2 $ 0 ) El ( 2 $ ( [ 0 , 0 ])) Proc.ACM Program.Lang., Vol. 8, No. POPL, Article 78.Publication date: January 2024.
Given a closed type : Tm ⋄ U, a term : Tm ⋄ (El ), we define a predicate $ := Eq El expressing equality to .As holds for by reflexivity, we obtain the following substituted term with the desired type.
P [ , , , refl] : Tm ⋄ (Eq El ( [ , ])) The operation -P can be defined for most well-behaved type theories as a syntactic translation or model construction.However, it only gives parametricity in the empty context.In the above example if we replace the empty context ⋄ by an arbitrary Γ, we only obtain Tm Γ P (Eq El ( [ , ])), which expresses the same equality, but it is only valid in a different (larger) context Γ P which includes the information that the elements in Γ are themselves parametric.Operations that are internal to type theory (such as or application $) do not act on the full context, as can be seen by looking at their inference rules.This is in contrast with -P which takes a term into a completely different context.This is why -P is called an external parametricity translation.
However there is no hope of being able to define the substitution R Γ by induction on the syntax.Type theory has non-parametric models in which the above equality does not hold for any : e.g., models with excluded middle [Booij et al. 2016] or a type-case operator [Boulier et al. 2017].
So it is not a surprise that when trying to define R Γ by induction on the context Γ, we need to extend the syntax by a new operator which we call rel: Once we introduce new terms (such as rel), we have to say how -P acts on them, and it is not clear how to do this.A solution is to turn the -P operations into operators of the syntax and their definitions into conversion rules.Such a -P relation behaves like an identity type that is reflexive and a congruence, but has no transport (it is sometimes called a Bridge type).Before defining our syntax with a Bridge type, we take a detour to understand how iterated usages of -P behave.
Higher cubes in external parametricity.Γ P can be seen as a context of lines, (Γ P ) P as a context of squares, ((Γ P ) P ) P as a context of three-dimensional cubes, and so on.We illustrate this by computing the contents of (⋄, : ) after applying -P to it twice.
Degenerating a line.Using our newly developed geometric intuition, we explain how two substitutions of type Sub Γ P (Γ P ) P , namely R Γ P and (R Γ ) P , differ.They correspond to the two different ways of turning a line into a square: given (⋄, : ) P , R (⋄, : ) P produces the square on the left, (R ⋄, : ) P produces the square on the right.
Symmetry and emergent geometry.In a syntax for internal parametricity, we either need to postulate the existence of an infinite hierarchy of rels from which the substitutions R Γ , (R Γ ) P , (R Γ ) P P , . . .can be obtained, or we need to provide another way to relate R Γ P and (R Γ ) P .We choose the latter:1 we introduce a new substitution S Γ (called symmetry) which satisfies S Γ • R Γ P = (R Γ ) P .Intuitively, symmetry maps to .It turns out that this operation is enough, and there is no need to introduce higher dimensional versions of S Γ (as in [Bernardy and Moulin 2012]) or an extra sort of intervals (as in [Bernardy et al. 2015;Cavallo 2021]).In this paper we define a theory with internal parametricity which does not have explicit geometry in the syntax.Compared to previous theories with internal parametricity, geometry is emergent rather than explicitly built-in.We do have ways to talk about higher dimensional cubes (as we saw when iterating -P on contexts) but this is nothing special: Martin-Löf type theory also has all higher dimensional cubes simply because the identity type can be iterated.E.g. the type Id (Id 01 11 ) transport (Id 01 -) 12 (transport (Id -10 ) 02 20 ) 21 expresses the type of fillers of the following two-dimensional square.We have one two-dimensional operation (S Γ ) and one equation about S Γ that involes three dimensional cubes, but we never mention anything higher than that.
Obtaining a theory from a model.Even if higher cubes are not explicitly built into our syntax, our type theory is informed by an analysis of the cubical set model built on Bezem-Coquand-Huber (BCH) cubes [Bezem et al. 2013].
The BCH cube category can be presented using a finite number of basic operators and equations between them.It is given by the free 2-category generated by the diagram on the left in Figure 1 and five equations relating the 2-cells.This 2-category has one 0-cell * , and can be seen as a 1-category where objects are given by 1-cells from * to itself, and morphisms are given by the 2-cells.In this presentation we have numbered dimensions instead of named dimensions (see [Buchholtz and Morehouse 2017] for a comparison of different presentations).The objects of the cube category are natural numbers given by 0 = id, 1 = suc • id, 2 = suc • suc • id, and so on.Degeneracies are generated by R, e.g.there is one map R from 1 to 0, there are two maps from 2 to 1, namely id suc • R and R • id suc (where -• -denotes horizontal composition).Face maps are similarly generated by 0 and 1, and there is a symmetry operation S.
The category of presheaves over the BCH cube category supports the exact same structure with maps in the other direction (diagram on the right in Figure 1).Here ∀ is precomposition by suc, and the natural transformations are named after their generating 2-cells.This picture can * * be reified into new operations of the type theory: we add a strict morphism from the syntax to itself corresponding to ∀, natural transformations R, 0, 1 and S, and the five extra equations.The new equations and the other operators that we add are those which are justified by this presheaf model: the morphism ∀ strictly respects the substitution calculus (the category with families, CwF), ⊤, Σ, strict identity Eq, Bool and K. Our model does not justify an equation such as ∀(Π ) = Σ Π (∀ ) (∀ ) . . ., but only the analogous isomorphism, so for Π we add new operators and equations expressing this isomorphism.Similarly, ∀U is described by a section-retraction pair.We call the collection of these new operations the global theory, as it involves operations on contexts and substitutions.We directly obtained the global theory from the presheaf model over BCH cubes, thus it is immediately justified by this model.
Local theory.Multi-modal type theory [Gratzer et al. 2021] gives a generic way to construct a type theory from a CwF morphism such as suc.It uses that precomposition ∀ = suc * has a left adjoint suc !(the left Kan extension), and this has a dependent right adjoint.However this theory is still non-local as it introduces extra (un)lock operations on contexts.A presentation of internal parametricity using this method is [Cavallo 2021], where the lock operation of multi-modal type theory becomes context extension with an interval variable.
In our case we can define a version of our theory with only local operations, that is, operations that do not change the context.This is what we call the local theory.It is specified by the exact same data as the global theory, but now ∀ is not a morphism from the syntax to itself, but a morphism from the standard model to itself, internal to presheaves over the syntax.We explain this in detail.
Any category of presheaves has a type-theoretic internal language.For example, when we write : Set in this internal language, externally this means that is a presheaf.Similarly, the internal : → Set means a dependent presheaf over externally.If the base category C of the presheaf model is not only a category, but a CwF, then internally we have Ty : Set and Tm : Ty → Set which externally are defined by the presheaf of types and the dependent presheaf of terms in C.This is the main idea of two-level type theory [Altenkirch et al. 2016;Annenkov et al. 2017].
Now we define a CwF internal to presheaves over C. We call this the internal standard model.Contexts in this model are given by Ty, a type in a context Ω is a function Tm Ω → Ty, a term in context Ω of type is a dependent function ( : Tm Ω) → Tm( ). Context extension is given by Σ, hence we need that Ty is closed under Σ, which in turn needs that C has Σ types.This standard model is a generalisation of the set model (or type model, or metacircular model) [Altenkirch and Kaposi 2016] and is a variant of the telescopic contextualisation of [Bocquet et al. 2023].
The ∀ of the local theory is specified by a CwF-morphism from this standard model to itself.Internally written, it maps contexts to contexts (∀ : Ty → Ty), types to types (∀d : (Tm → Ty) → Tm (∀ ) → Ty), and so on.We add a "d" suffix to the operation on types to distinguish from the one on contexts.Externally, these operations are natural tranformations described as follows. : : Ty (Γ, : ) ∀d : We obtain all of the local theory this way: we start with a strictly democratic CwF C with ⊤, Σ, Eq, Π, U and Bool (we call this the core theory).Internally to presheaves over C, we have the standard model of this core theory.Now ∀, ∀d and the other new operations and equations providing internal parametricity are specified by a core theory morphism from this standard model to itself.Just as in the case of the global theory, this morphism respects the CwF structure, ⊤, Σ, Eq, K and Bool strictly, Π up to an isomorphism, and U up to section-retraction.Note that this only gives a specification of the local theory, and does not directly provide a model of it.We justify the local theory by deriving its syntax from the syntax of the global theory which we localise using R.
The local theory is truly local: it does not mention contexts and it can be described as a secondorder generalised algebraic theory (SOGAT) [Uemura 2019].From this SOGAT we obtain a firstorder GAT in a way that makes sure that all operations are stable under substitution.As far as we know, our local theory is the first non-substructural type theory describing presheaves over BCH cubes.We distinguish the corresponding operations of the local and global theories by writing those of the global theory in brick red colour.
This correspondence holds only up to isomorphism in the model and thus in our theory.
In the external parametricity translation, we have The main reason that we use span-based instead of relation-based parametricity is that in our model this is not an equality, only a logical equivalence. 2We have maps in both directions, but the composite map is not the identity (and neither is the other roundtrip).However, if we replace relations by spans, we do have that the analogous composite map for spans is the identity.The intuition for why this works for spans and not for relations is that presheaves are span-based by nature.Given a presheaf Γ over BCH-cubes , we denote the action on objects Γ : → Set and the action on morphisms -[ -] Γ : Γ → ( , ) → Γ .Now, at levels 0 and Relation-based presheaves are called Reedy fibrant (relative to families of sets as the underlying notion of "fibration") [Kraus and Sattler 2017], and it should be possible to construct a Reedy fibrant presheaf model of internal parametricity, but we leave this for future work.A model of internal parametricity based on refined presheaves similar to Reedy fibrant ones is [Bernardy et al. 2015].
The other roundtrip for the correspondence ∀U ↔ Span is unfortunately not identity in our model.Thus we justify this correspondence up to a section-retraction pair.
Metatheory.As our global theory arose from a presheaf model, it is not surprising that it is modelled by the exact same presheaf category.We extend gluing [Kaposi et al. 2019a] to the global theory, and define a global section functor satisfying the necessary conditions from the syntax to our presheaf model.We know that the syntaxes of the global and local theories are isomorphic, hence, as a result, our local theory satisfies canonicity and has an external parametricity translation.We conjecture that a version of our theory without equality reflection satisfies normalisation.

Structure of the Paper
After summarising related work and our notations, we introduce our local theory in Section 2, and describe some applications including well-known usages of internal parametricity.This section can be understood without prior familiarity with models of type theory or presheaves.For the rest of the paper we try to be as self-contained as possible, and refer to the relevant literature.
In Section 3, we define the global version of the theory as a generalised algebraic theory (GAT).The syntax of the theory is given by the initial algebra (model) which exists for any GAT.We also show that the global theory has a presheaf model.Section 4 shows the isomorphism of the local and global syntaxes.Then in Section 5 we prove that our global theory has a gluing model and as a consequence satisfies canonicity: every closed term of the boolean type is equal to either true or false.By the previous isomorphism this result holds for both the local and global theories.

Related Work
The first type theory with internal parametricity was defined by Bernardy and Moulin [Bernardy and Moulin 2012].It contains a syntax for arbitrary dimensional cubes.The apd operator in our local syntax is very similar to their double bracket operator, but our theory only mentions cubes up to dimension three.Bernardy and Moulin simplified their syntax later using named dimensions [Bernardy and Moulin 2013] and further refined it using a sort of intervals [Bernardy et al. 2015] similar to that of cubical type theories (e.g.[Cohen et al. 2015]).Using the same (BCH) cube category as the first cubical set model of univalence [Bezem et al. 2013], the paper [Bernardy et al. 2015] defines a presheaf model of internal parametricity.It uses a refined notion of dependent presheaf for interpreting types similar to Reedy fibrancy [Kraus and Sattler 2017].Our presheaf model uses ordinary presheaves and avoids the need of Reedy fibrancy by having span-based instead of relational parametricity.[Bernardy et al. 2015] has an equation "SURJ-TYP" the analog of which we were not able to justify in our model.This would correspond to unspan being an isomorphism, not only a section (see (7) in Problem 3.7).It seems that having Reedy fibrant types does not make a difference in this respect.
Cavallo and Harper [Cavallo 2021;Cavallo and Harper 2021] define a cubical type theory with univalence and internal parametricity at the same time.They support a double-presheaf model with cartesian cubes for the identity type and BCH cubes for parametricity.They justify relational parametricity, but the correspondence between the logical relation at U and relation space only holds up to internal equivalence (and hence propositional equality, by univalence), and not definitional section-retraction as in our theory.This is enough to derive the consequences of internal parametricity, however.Van Muylder, Nuyts and Devriese [Muylder et al. 2024] extend Cubical Agda with internal parametricity following Cavallo and Harper.Inside this theory they shallowly embed a "relational observational type theory" in which logical relations are computed as in [Bernardy and Moulin 2012], but it does not feature iterated parametricity.
Nuyts et al. [Nuyts and Devriese 2018;Nuyts et al. 2017] analyse the presheaf model of internal parametricity, and define type theories where the parametricity relation and the (non-univalent) identity type are special cases of a general construction "relatedness".These syntaxes use two different kind of Π types (parametric and non-parametric ones) and there is no proof of canonicity.
Our global syntax is very close to the naive syntax in [Altenkirch and Kaposi 2015].By closely following a model, we make sure that we do not miss any equations, and we manage to prove canonicity, even being "naive" in their sense.
Recent work on cubical type theories [Angiuli et al. 2021;Cohen et al. 2015;Vezzosi et al. 2021] has used different cube categories that contain diagonals and sometimes connections as well, due to their advantages when formulating higher inductive types.The resulting presheaf categories satisfy a different computation rule for ∀ of Π (function extensionality), which is correct homotopically but inappropriate for parametricity.On the other hand, earlier work on cubical homotopy theory used a cube category lacking symmetries, which also fails to have our desired computation rule for ∀ of Π.The BCH cube category is "just right".
Finally, although we explicitly discuss only "binary" parametricity, one can consider -ary parametricity for any natural number , in which case there are possible values for wherever it appears.Unary parametricity is also common in the literature.Nothing that we say should be sensitive to the choice of , and often even our notation can be applied directly in the -ary case.

Metalanguage and Notation
Our metalanguage is extensional type theory with quotients and propositional extensionality (unlike in the above paragraph "Local theory", this extensional type theory is not the outer level of a two-level type theory).Our constructions can be also understood as taking place in a constructive set theory.We use Agda-style notation with implicit arguments usually omitted or written in curly braces {. ..} and we employ implicit coercions and overloaded projections.

:
: We write p 2 = p • p, p 3 = p 2 • p, and natural numbers for De Bruijn indices: = q[p ].We denote an isomorphism between two contexts by : Γ ∆ which means that : Sub Γ ∆ and there is also a −1 : Sub ∆ Γ and both compositions • −1 and −1 • are the identity.Similarly, for types we write to mean that we have terms :

THE LOCAL THEORY AND APPLICATIONS
In this section we list and explain the rules of our local theory with internal parametricity and show how to apply it to derive consequences of parametricity.This section can be understood without previous knowledge of models of type theory.
We list the operations and equations of our core type theory in Figure 2 and the rules providing internal parametricity in Figure 3.The notation can be understood as listing the operations and equations of a second-order generalised algebraic theory (SOGAT) [Bocquet et al. 2023;Uemura 2019].This is why we don't have to list rules about contexts or substitutions.(The external presentation of the core theory is Definition 3.1, the external presentation of the local theory is described in Section 4.1.)We have two sorts, types are denoted , terms are : .In case an operation is a binder, some of its arguments have extra dependencies listed before a ⊢.Some operations have implicit arguments, for example the constructor for Σ types ( -, -) has two implicit inputs and which we omit for readability.Similarly, the equation 1 ( , ) = has four implicit arguments: , , , .We omit the premises of most equations.Note that the rule for Σ types written = ( 1 , 2 ) only makes sense for having a Σ type, so we don't have to add this as an explicit assumption.If one views Figures 2 and 3 as constructors for a syntax, then we have well-typed (intrinsic) terms which are quotiented by conversion.
in this paper can be redone using universe indexing.In that case we would need to index types (and terms) by their level as well.Finally, we have a type of booleans with dependent elimination ite.
Ad Figure 3.We have a nondependent ∀ on types which computes the logical span from a type.We know that nondependent maps (terms : ⊢ : where does not depend on ) preserve logical spans, we call this ap as an homage to the "apply on path" congruence operation in HoTT.For types depending on a variable, we have dependent logical spans ∀d which depend on a base logical span.These are also preserved by terms witnessed by apd (this is our main internal parametricity operation).Then we have projections from ∀ to , in the binary case can be either 0 or 1.The provide the legs of the logical span ∀ .(The dependent version of this projection can be derived using Σ types.)We can build degenerate logical spans using R and we can apply a symmetry operation S to a double-span (two-dimensional span).We explain mk∀Π and unspan below.The , R and S operations are natural with respect to ap and satisfy five additional equations (which reflect the equations of the BCH cube category): (1) the bases of a degenerate span are the same as the element we started with, (2) the two different ways of taking the bases of a double-span are related by S, (3) the two different ways of degenerating a span into a double-span are related by S, (4) double symmetry is identity, (5) we can apply symmetry to a triple span in two different ways: we can swap dimensions 0 and 1, or we can swap dimensions 1 and 2; now first swapping 0 − 1, then 1 − 2, then 0 − 1 is the same as first swapping 1 − 2, then 0 − 1, then 1 − 2. Combined with naturality of S, this ensures that the induced symmetries of an -fold span form the -ary symmetric group.
In addition to ∀, ∀d, ap, apd, , R, S, we have two more operations which concern Π types and the universe (mk∀Π and unspan).We will explain them below.
The core equations say that ap, ∀d and apd are functorial with respect to nondependent maps, ∀ on unit returns unit and ∀, ap on Σ types is pointwise.
The constant equations express that ∀ is a special case of ∀d when the type is actually nondependent.We have an analogous equation relating apd and ap.
Just as Σ, strict identity Eq is strictly preserved.Preservation of refl is automatic by UIP.
Π types are only preserved by ∀d up to isomorphism.However we don't have that ∀(Π( : ). ) is isomorphic to Π( 2 : ∀ ).∀d( .) 2 because we can use Π ( : ). to obtain a function Π( : ).from an element of the first type, but we cannot obtain such a function from an element of the second type.Hence we have to add more information to the second type: functions at the bases that are compatible with the function at the apex.Moreover itself can be dependent.So the final statement is that the obvious map from ∀d( .Π( : ). ) 2 to a and s that make the following diagram commute is an isomorphism.
Just as Π is not preserved up to equality by ∀, U is also not preserved up to equality.There is an obvious map from 2 : ∀U to a span: the apex is ∀d( .El ) 2 , the bases El ( U 2 ), and the legs are 2 : ∀d( .El ) 2 ⊢ d .El 2 2 .This map has a section called unspan.The premises of unspan are universally quantified over , hence include two types 0 and 1 , and two terms 0 and 1 .
Bool, its constructors and eliminator are preserved strictly by ∀ and apd.

Some Derivable Equations
For Σ, it was enough to state that ( -, -) is preserved by apd, the other direction is automatic: Relationship between d and apd: d . 2 (apd( .
The unary version of our theory suffices (thus = 0).We compute the first projection of this term.and we obtain the desired result by choosing the predicate to be ( : ) ⊢ Eq .
Induction for Church-encoded natural numbers.We can also prove results for higher order polymorphic types using mk∀Π.As an example, we prove the induction principle for Church encoded natural numbers following [Wadler 1990].The (uncurried) type of Church natural numbers is A natural number algebra is given by a type , : and : → .A morphism of algebras between ( , , ) and ( , , ) is a function : → such that $ = and $ ( $ ) = ( ) for every .These form a category.We define: The induction principle that we want says that ( , , ) is initial in the category of algebras of , being the unique morphism from to the algebra ( , , ). (This is equivalent to saying that every displayed model over has a section, see [Kaposi et al. 2019b] for a generic proof.)Using binary parametricity, we first show that respects morphisms, that is, $( $ ) = $ .Assuming : we build (( , ), ) : Σ( : × ).→ Σ( : × ).
Compute ap on R using symmetry.One reason for needing symmetry S in our syntax is to compute the ap( .R ) 2 way of turning a witness of a span 2 : ∀ into a witness of a doublespan.Without symmetry, there seems to be no way to compute the value of the closed boolean ap( .R Bool ) true.Having symmetry and the rule

THE GLOBAL THEORY AND ITS PRESHEAF MODEL
In this section, we define our global theory which directly comes from the presheaf model on BCH cubes [Bezem et al. 2013].We assume basic knowledge of working with models of type theory as categories with families (CwFs, [Castellan et al. 2019]) and the CwF of presheaves [Hofmann 1997].
We define the global theory as a generalised algebraic theory (GAT, [Kaposi et al. 2019b]) by saying what a model of this theory is.First we present the core theory externally, this is the common part of the local and global theories.This corresponds to the core theory listed in Figure 2, with the additional structure of strict democracy which is not expressible internally.Definition 3.1 (Core theory, externally).A model of the core theory is a CwF with the following features: • ⊤, Σ with and and strict identity types Eq with uniqueness of identity proofs.This turns the CwF into a finite limit CwF (flCwF, [Kovács and Kaposi 2020]).• Strict democracy, that is, an operation K that turns a context into a type satisfying the following equations.Note that this includes a sort equation.(Non-strict) democracy requires Sub Γ Θ = Tm Γ (K Θ) only up to isomorphism.
In the equation relating K of ⊲ and Σ, q can be used as a substitution because of the previous equations: , and because of the sort equation we have q : Sub (Γ ⊲ K Θ) Θ.
• A hierarchy of universes à la Coquand, that is U : Ty Γ with an isomorphism c : Ty Γ Tm Γ U : El.We don't write universe indices for convenience, but every construction in this paper can be redone in a setting where types, terms and universes are all indexed by levels, and U : Ty +1 Γ. • Bool with dependent elimination ite and two rules (no ).Now we list the components specific to the global theory (c.f. Figure 1).We use brick red colour to distinguish from the operations of the local theory.

Definition 3.2 (Global theory).
A model of the core theory is a model of the global theory if it comes equipped with the following structure: (1) A strict flCwF endomorphism on the model ∀ that also preserves K strictly.We denote the four different maps by ∀ Con , ∀ Sub , ∀ Ty and ∀ Tm or just ∀.Strict preservation of K means ∀(K Θ) = K (∀Θ) and ∀ Sub {Γ }{Θ} = ∀ Tm {Γ }{K Θ}.
(2) Natural transformations from ∀ to the identity functor on the CwF, for = 0 and = 1.
This means for each Γ : Con, a substitution Γ : Sub (∀Γ) Γ with Γ • ∀ = • ∆ .We define the action of on types as follows. : (3) A natural transformation R from the identity to ∀.
(5) The following five equations relating the above three natural transformations: ) has an inverse mk∀Π.Precisely we require the operation mk∀Π with the following equations.
When we quantify over a subscripted variable such as , we mean to quantify over two variables 0 and 1 with types obtained by substituting = 0, 1 in the type of .(7) The map from ∀U to a span has a section called unspan.Precisely we require: We do not require the other roundtrip equation.( 8) ∀ preserves Bool strictly.
We define the BCH cube category as depicted in Figure 1.
Definition 3.3 (Cube category ).The objects of the category are natural numbers, the morphisms are generated by the following quotient inductive set.First of all we have a category, that is, composition and id with the categorical laws, then we have the following constructors and equality constructors.When we write we always mean both a copy for 0 and a copy for 1.
Comment on the definition of .Another way to describe this category is that it is the free symmetric semicartesian strict monoidal category over a cylinder.In particular, the final equation on S is the "braid equation", which together with naturality forms a presentation of the symmetric group; thus the automorphisms of are the permutations of an -element set.This category also has a named variant, in which the objects are finite sets and a morphism from to is a function from to + {0, 1} that is injective on the subset of that is not mapped to inr.
S allows us to swap the first two dimensions, but we would like to swap the first with any other dimension, on both directions.Definition 3.4 (Generalised symmetries in ).We define the following morphisms by induction on natural numbers.
(sym 0 ,1 |id 1 ) : By induction we prove that the generalised symmetries form an isomorphism in : We would like to characterise the morphisms in without equations, so that we can have a powerful case analysis (which will be needed for defining the ∀-preservation of Π and U).We will need a case analysis on a morphism : ( , 1 + ) to know where the 1 in the codomain is coming from: either it is coming from a face map and = • ′ or = 0 + 1 + 1 for some 0 and 1 and the 1 in the codomain comes from the 1 in the domain, with the rest being mapped by some ′ : ( 0 + 1 , ).We generalise so that the 1 in the codomain can be in the middle.P 3.5 (C ).For a morphism : ( , 0 + 1 + 1 ) either (i) there is a and an ′ : ( , 0 + 1 ) such that = (sym 1, 0 |id 1 ) • 0 + 1 • ′ , (ii) or there are 0 , 1 with = 0 + 1 + 1 and ′ :

C
. The first case means that the 1 in the codomain of the morphism comes from a face map , the second case means that the 1 in the codomain comes from a 1 in the domain.
We perform induction by building a displayed model of the quotient inductive set in Definition 3.3, and then using the induction principle of the quotient inductive set [Kaposi et al. 2019b].Displayed morphisms are given by We would like to define • • • • which says our induction motive for • .We match on the induction hypothesis for denoted • : if it is an inl, that is, = (sym 1, 0 |id 1 ) • 0 + 1 • ′ , then we have • = (sym 1, 0 |id 1 ) • 0 + 1 • ′ • , so we still know that the 1 in the codomain of • comes from .If • is an inr (that is, the 1 in the codomain comes from a 1 in the domain of ), then we have to match on • to learn whether that 1 in the codomain of comes from or from the domain of .We then check that -• • -satisfies associativity.
For the rest of the constructors of , we list the computational parts of the displayed model.
The displayed versions of the equations all hold.The displayed model induces a dependent function from ( , 0 + 1 + 1 ) to the sum type by the induction principle of the quotient inductive set .
This case analysis will be used in parts ( 6) and ( 7) of Problem 3.7.
Construction 3.6 (Presheaf model of the core theory).We recall the computational parts of the presheaf model over [Hofmann 1997] providing a model of the core theory (Definition 3.1).
A Γ : Con is a presheaf, that is a family of sets Γ : → Set together with reindexing [ ] Γ : Γ for : Γ and : ( , ( [ ]) with two equations.Finally a term is a dependent natural tansformation : Context extension and the types ⊤, Σ, Eq are defined pointwise and we have strict democracy by The Yoneda embedding is a functor from to presheaves over , we denote it by y : → Con and y : ( , ) → Sub (y ) (y ), and we have the Yoneda lemma yl Γ : Γ → Sub (y ) Γ defined by Bool is pointwise.P 3.7 (P ).The presheaf model extends to the operations and equations of the global theory (Definition 3.2).This justifies the notion of the global theory, which was in turn extracted from this exact presheaf model.

C
. Recall the two diagrams of Figure 1.We define the components in order.(1) ∀ is defined as precomposition with suc: This preserves the flCwF structure strictly, e.g.we have and by the fact that ∀ Sub and ∀ Tm coincide.
(2) is defined as Γ 1+ := 1+ [ ] Γ .This is natural as The five equations follow from their corresponding equations in .For example, ).We do case analysis (Problem 3.5) on the input morphism given by Yoneda y (1 + ): If the morphism selects a projection , then we use the map at the base of the span .If the morphism selects an existing dimension (1 in the middle of 0 + 1 + 1 ), then we use the function at the apex , but we have to apply symmetry to the input to make it well-typed.
).Then we have to apply symmetry at the output again: app

ISOMORPHISM OF THE LOCAL AND GLOBAL SYNTAXES
In this section we construct a model of the local theory using the global syntax and show that it is isomorphic to the local syntax.And similarly, we construct a model of the global theory using the local syntax.

Defining the Local Syntax Using the Global Syntax
The types in the local theory correspond to the types in a fixed context Γ of the core theory.To define the local ∀ from the global ∀, we apply ∀ and apply the substitution that goes back to Γ: All the other operations follow the same idea: The equations of the local theory follow from the corresponding equations in the global theory and some naturality principles.We give an example: unspan is simply unspan and mk∀Π := mk∀Π (R Γ ↑) .

Defining the Global Syntax Using the Local Syntax
We have an operation ∀ on types and we want to extend it to contexts.Since the syntax is contextual, it is natural to do an induction to define ∀.In fact, since we have strict democracy, we have another natural way to procede which is to apply ∀ on the type corresponding to the context, so we need a mutual induction to make the link between these two, by defining an isomorphism between them.
Here we used q : Tm (⋄ ⊲ K Γ) (K Γ) as a substitution Sub (⋄ ⊲ K Γ) Γ, as a consequence of strict democracy.In fact, it is one direction of an isomorphism: We also have to do an induction for substitutions.
We can also prove a naturality property for ∀ : The definition of ∀ on types and terms is enforced: Finally we define: and mk∀Π := mk∀Π (q[∀ Γ • ]) .Once again, unspan is straightforward, and the equations come from the corresponding equations in the local theory.

Roundtrips
The 9 operations of the local syntax coincide with the same ones after globalising and localising them.The proofs essentially rely on manipulations on substitutions and naturality properties.
In addition, the 9 operations of the global syntax coincide with the same ones after localising and globalising them.To prove that the ∀ ′ that we define by induction is the same as the initial ∀, we do a mutual induction proving that ∀ Γ = ( , id).The proof for substitutions is also an induction, the others rely on naturality.

Isomorphism
Subsection 4.1 says that the global syntax Syn is also a model of the local theory.Thus we obtain a map from the local syntax to this global model.Note that it is identity on the core calculus (Figure 2 or Definition 3.1).Similarly, Subsection 4.2 decorates the local syntax with a model of the global syntax providing a morphism from Syn to the local syntax.By induction by Syn and Syn, we prove the compositions of and are identities.The only nontrivial cases of this induction are handled in Subsection 4.3.Thus we obtain e.g. that for all Γ : Con Syn , ( Γ) = Γ, and so on.

GLUING FOR THE GLOBAL THEORY
In this section we extend the gluing proof of [Kaposi et al. 2019a] to the global theory.We first define the notion of weak morphism which also has to respect ∀, then we define the gluing model.Finally we show how to make use of gluing by defining a global section functor from the syntax of the global theory Syn to the presheaf model PSh( ) (Problem 3.7), and applying gluing to this, which shows that our theory enjoys canonicity.

Weak Morphism of Models Respecting ∀
A weak morphism is a functor which has an action on types and terms and preserves instantiation of substitution strictly and we require that the empty context and context extension are preserved up to isomorphism.Furthermore, we require that the functor preserves ∀, , R and S strictly.
Definition 5.1 (Weak morphisms of models of the global theory).Given two models of the global theory C and D, a weak morphism from C to D contains the following functions and satisfies the following equations.We usually omit the subscripts saying which model is meant because it is clear from the context, and we also overload the names for the four maps in .

Maps
Preservation of core structure Preservation of ∀, , R, S : id For any such we can define comparison maps expressing that K, ⊤, Σ, Eq are preserved automatically up to isomorphism, Π and U are preserved in a lax way and Bool in an oplax way.The specification is on the left, the implementations are on the right.
We note that a consequence of preservation of ∀ is that ∀ −1 ⊲ = −1 ⊲ .

The Gluing Displayed Model
A displayed model over a base model is equivalent to a model with a morphism into the base.It can also be seen as the collection of motives and methods for the induction principle of the syntax (the initial model).The components of a displayed model can be computed for any generalised algebraic theory using the methods in [Kaposi et al. 2019b].We mark displayed components with a bullet.For example, displayed contexts are a set indexed over contexts: Con • : Con → Set where Con is the set of contexts of the base model.Displayed types are indexed implicitly over a base context, a displayed context at the base context and a base type: Ty The displayed variants of equations are well-typed because of the corresponding equations in the base model.

C
. Gluing of a weak morphism from C to D gives a displayed model over C.This model construction is a generalisation of logical predicates.A displayed context is a predicate over applied to the context, a displayed type is a dependent predicate over applied to the type.A displayed substitution/term says expresses that of the substitution/term respects the predicates.
The core calculus is glued using the same technique as the standard model: composition and substitution are modelled by substitution, context extension by Σ types.
The type formers K, ⊤, Σ, Eq, Π, U and Bool in the gluing displayed model are defined as follows.K • simply returns its argument context, ⊤ • , Σ • , Eq • are pointwise.Π • expresses that if the predicate holds for an input, it holds for the output.U • gives predicate space, Bool • for a in Bool says that there is a boolean which is equal to when mapped using −1 Bool .The definitions are straightforward, but technical: adjustments using −1 ⊲ and the comparison maps (e.g.Σ , Π ) have to make things match.The definitions below satisfy all the equations of the displayed model.
The displayed versions of the ∀, , R and S operators are defined as follows.
to be in Ty ( (∀Γ)) and ∀Γ • is in Ty (∀( Γ)), but these are equal because respects ∀ strictly.The situation is similar for the other operators.
For mk∀Π • • • • , we need a term in We can directly apply mk∀Π to build an element of this latter type reusing the uncurried versions of • and • , respectively.
Now we curry the resulting term under ∀ to obtain the definition of mk∀Π • : For unspan • • • • , we need a term in which is the span of a predicate.We already have predicates on and with a map from witnesses of the second one to the first one that lies over : The way to make terms in ∀ of a function space is by mk∀Π.The function (the predicate) at the base is essentially • , the predicate at the apex is given by unspan ( where • and • have to be substituted to plug in their dependencies properly.
The equations for the global syntax follow from the preservation properties of the weak morphism.Here is a typical example: This completes the construction of the gluing displayed model.

The Global Section Functor
We define a way to interpret morphisms in in our syntax (see Definition 3.3).The substitution Γ for an : ( , ) is defined mutually with three equations by induction on : denotes the times iteration of ∀ on Γ.The different cases 3 of -: The fact that -Γ preserves the equations for the morphisms in is direct except for the three naturality equations • = suc • , R • suc = • R and S • suc (suc ) = suc (suc ) • S which follow from the = 0 cases of the above three equations.
We also prove by induction on that for any , : Γ • ∀ = ∀ • ∆ .Now we construct the weak morphism on which we will glue for canonicity.This can be seen as a cubical nerve functor.

P 5.3 (G
).We construct a weak morphism G from Syn to PSh( ).
3 An alternative definition of -Γ uses suc Γ = ∀ Γ , Γ = ∀ Γ , and so on.This version generates a global section functor which only satisfies G (∀Γ ) ∀ (G Γ ) up to isomorphism.An isomorphism is enough to build a gluing model, however it is much more tedious than our current, stricter approach.
The functor laws for G follow directly and naturality for G is proven as follows.
It is easy to see that G is a functor, it is more interesting that it preserves substitution of types: A similar proof shows that G preserves term substitution.We define preservation of ⋄ and -⊲ -as follows.In the latter case we replace a metatheoretic pairing operation with a syntactic extended substitution former.
We verify that G preserves ∀ and commutes with , R, S. G commutes with ∀ on contexts G ) , and terms

Preservation of is given by
preservation of R and S are shown analogously.
5.4 Reaping the Fruits T 5.4 (C ).In the local or global syntax, for a : Tm ⋄ Bool, we have = true or = false.

P
. By gluing along the global section functor, we obtain a displayed model over the syntax of the global theory, and given a : Tm Syn ⋄ Bool, by induction of Syn, we get a Tm PSh( ) (G ⋄ ⊲ ⊤) (Σ Bool (Eq (ite q (G true[p]) (G false[p])) (G [p]))), and providing the id ⋄ : G ⋄ ⋄ and the element of the metatheoretic unit set as input we obtain ( : ¾) × if then true else false = .
Given a term in the local syntax : Tm Syn ⋄ Bool, we have : Tm Syn ( ⋄) ( Bool) where is the map from the global syntax to the local syntax defined in Section 4.4.As does not affect the core syntax this is : Tm Syn ⋄ Bool and we learn from canonicity say that = true.Now applying to such an equation we obtain = ( ) = true = true as also does not affect that core calculus.

FUTURE WORK
We presented a type theory with internal parametricity, a presheaf model and a canonicity proof.It can be seen as a baby version of higher observational type theory (HOTT).To obtain HOTT, we plan to add the following additional features to our theory: • a bridge type which can be seen as an indexed version of ∀, • Reedy fibrancy, which replaces spans by relations, • a strictification construction which turns the isomorphism for Π types into a definitional equality (in case of bridge, we also need the same for Σ), • Kan fibrancy, which adds transport and turns the bridge type into a proper identity type.This would also change the correspondence between ∀U and spans into ∀U and equivalences.We would also like to include general (higher) inductive and coinductive types.Concerning the metatheory, we plan to use internal language techniques [Bocquet et al. 2023;Sterling 2022] to obtain a higher level canonicity proof, and extend it to normalisation.
Several of our constructions in this paper follow a generic pattern: most of the global theory and gluing should be derivable from the 2-category in Figure 1 similarly to the way it is done for multimodal type theory [Gratzer 2023].

Fig. 1 .
Fig.1.The generating 1-cells and 2-cells of the BCH cube category as a 2-category (le ).Structure on presheaves over this category (right).The two occurrences of * represent the same object, depicted twice solely for presentation, and the same holds for PSh( ).

Fig. 2 .
Fig. 2. The core theory.See Definition 3.1 for an external description.