<<

The Next 700 Syntactical Models of

Simon Boulier Pierre-Marie Pédrot Nicolas Tabareau Inria, France fi[email protected]

Abstract theory such as COQ (The COQ Development Team 2015) A family of syntactic models for the calculus of construc- or AGDA (The AGDA Development Team 2015). However, the definitive answer to those questions is far from being tion with universes (CCω) is described, all of them preserv- ing conversion of the calculus definitionally, and thus giving simple as it involves proving the consistency of a type theory augmented with some logical principles or their negation. rise directly to a program transformation of CCω into itself. Those models are based on the remark that negative type Traditionally, given a type theory described as a syntactic constructors (e.g., dependent product, coinductive types or object, there are two ways to justify it. universes) are underspecified in type theory—which leaves 1. The most standard one, coming all the way back from some freedom on extra intensional specifications. The model , consists in building a model of it, usu- construction can be seen as a compilation phase from a com- ally into theory or a given categorical structure. This plex type theory into a simpler type theory. Such models amounts formally to translating the into some can be used to derive (the negative part of) independence well-behaved quotient. See e.g. (Werner 1997). results with respect to CCω, such as functional extensional- ity, propositional extensionality, univalence or the fact that 2. The most syntactical one consists in showing some good bisimulation on a coinductive type may not coincide with properties of the syntax, usually strong normalization and . They can also be used to add new principles to subject reduction, so as to derive the desired properties on the type theory. the theory, which we illustrate by defining a version of CCω with ad-hoc polymorphism that shows in particular that para- Each approach suffers from its own set of defects. Mod- metricity is not an implicit requirement of type theory. The els of dependent type theory have been widely studied from correctness of some of the models/program transformations a set theoretic point of view (see (Hoffman 1997) for a gen- have been checked in the COQ proof assistant and have been eral overview), giving rise to several families of models, e.g., instrumented as a COQ plugin. categories with families (Dybjer 1996), comprehension cat- Categories and Subject Descriptors F.4.1 [MATHEMATI- egories (Jacobs 1993), . . . But all those models of a type the- CAL AND FORMAL LANGUAGES]: Mathematical ory are built into some other fundational theory, putting back Logic the burden on the consistency on the latter. This is unavoid- able, because of the incompleteness , and may be Keywords Dependent type theory, Program translation actually seen as an advantage when targeting such a stress- tested theory as . Nonetheless, the discrepancy be- 1. Introduction tween the source theory and the target theory can be at the “Can we prove that ∀x. f x = g x → f = g ?”, “What root of subtle but very important mismatches. This is typi- is the difference between P ↔ Q and P = Q, when P cally seen when considering the of conversion and Q are two ?”, “Can we prove that a term which may hold in the source theory, but is lost as soon as of type ∀A. A → A is necessarily the identity ?”. objects are interpreted semantically. Those simple questions are frequently asked to someone Going the syntactical way is not without issues either, who starts teaching the use of a theorem prover based on type as the proofs are often very tedious, not modular, and keep repeating themselves between close flavours of type theory. In this paper, we advocate for a third alternative approach, based on a proof-as-program of type theory, which can somehow be seen as a reconciliation of the two aforementioned kinds of justification.

“To justify a complex type theory, it suffices to compile it into a simpler—already justified—type theory.” Modified Type Name of the translation New principles Translation

Πx : A. B Intensional functions (ext) ¬ functional extensionality [[Πx : A. B]] := (Πx : [[A]]. [[B]]) × B Intensional functions (int) ¬ functional extensionality [[Πx : A. B]] := Πx : [[A]]. [[B]] × B stream A Intensional streams ¬ stream extensionality [[stream A]] := stream [[A]] × B ∗ Ad-hoc intensional propositions ¬ propositional extensionality [[∗]] := ∗ × B

i Ad-hoc intensional types ¬ univalence [[i]] := i × B

Ad-hoc Polymorphism (quote) à la Lisp [[i]] := TYPEi (inductive-recursive type)

parametricity Built-in parametricity [[i]] := λ(A0 A1 : i).A0 → A1 → i

Functional Reactive Programming General Fixpoints [[i]] := stream i Forcing over P (depending on P) more complicated, see (Jaber et al. 2016)

Figure 1. Different Translations of CCω

Essentially, we follow the model approach, but instead of used to derive a new type theory where (the negative part of) targeting an alien system, we use type theory itself and can independence results can be proven, such as functional ex- thus rely on the consistency of it, proved in a syntactical tensionality, propositional extensionality, univalence or the manner. Furthermore, contrarily to recent models of type fact that bisimulation on a coinductive type may not coincide theory into itself (Chapman 2009; Altenkirch and Kaposi with equality (that will be called here “stream extensional- 2016), we do not describe the source theory as a deep em- ity”). bedding in the target one, but rather use a shallow embedding It has to be noticed that obtaining similar results using where all source proofs are translated into the target sys- set-theoretic models is very tedious. Indeed, although most tem in a way that preserves most of the structure. This is no available models negate univalence, finding a model that more than the dependently typed counterpart of more stan- negates functional extensionality or propositional extension- dard logical translations seen as program translations, e.g. ality is much more difficult as those principles are hardcoded Lafont-Streicher-Reus CPS (Lafont et al. 1993) is the pro- in set theory. It becomes even harder when it comes to mod- gramming language equivalent of some variant of Gödel’ els integrating coinductive types, as such model are based double-negation translation. upon the notion of final coalgebra and analysing the connec- This way, several properties from the target theory are in- tion between bisimilulation and equality in this setting is a herited for free, as long as the program transformation pre- very complex task. serves conversion of the calculus definitionally and that the Type-theoretic translations can also be used to add new empty type ⊥ (encoded here as ΠA : i.A) is translated principles to the theory, which we illustrate by defining a into some type equivalent to ⊥. This provides a very simple version of a type theory with ad-hoc polymorphism (i.e., notion of model of type theory, accessible to a broader audi- enriched with the (quote) operator of Lisp) that shows in ence than specialists of category theory and set theory, which particular that parametricity is not an implicit requirement can be used to quickly answer if an is derivable. of type theory. As said, such translations can not be used directly to The systems we study are built upon CCω, a type theory prove the consistency of the type theory under consideration, featuring only dependent products and a denumarable hier- and rely on the consistency of the base type theory itself. archy of universes i. But they can rather be used to study some properties of type theory enriched with new principles. This approach is in Contributions. There are a handful of program translations accordance with the recent work on forcing in type theory in type theory (starting from the model of (Hofmann (Jaber et al. 2012, 2016) and parametricity in type theory 1997)), but they are usually not presented as such. This is (Bernardy et al. 2010; Bernardy and Moulin 2012), which why we believe that giving a proper account of them is can both be seen as particular cases of the general picture worthwhile, as it permits to exhibit the nice properties of described in this paper. those models, most notably the preservation of computation. We provide several translations in this paper. They are We also hope to popularize such a way of building models. based on the remark that negative type constructors (e.g., In this paper, we provide the following contributions, dependent product, coinductive types or universes) are un- which are summarized in Figure 1: derspecified in type theory—which leaves a lot of freedom • we provide a general approach to defining translations of on extra intensional specifications. Such translations can be CCω into itself by adding more intensional properties to negative types • we apply it to dependent products to show formally that preservation ensures consistency of the source theory pro- functional extensionality is not derivable in intensional vided consistency of the target theory. type theory We argue that this approach is strictly sharper than the • we apply it to coinductive types to show that bisimilarity various notions of models from the literature. First, the trans- of streams does not imply in general equality of streams lation is only defined by induction on raw syntax, i.e. it does not depend on any typing assumption and is purely local. • we apply it to the universe, by using a presentation à la Furthermore, preservation of conversion gives a first- Tarski, to show that univalence and even propositional treatment of computation even in an untyped setting. Fi- extensionality are not derivable in CCω nally, the target is pure type theory, so that every assumption • finally, we introduce a more complex interpretation of needed to make the soundness proofs go through has to be universes using induction- to equip CCω with internal, meaning that one cannot rely on pushing back side- ad-hoc polymorphism and a quote operator conditions in the metatheory. We call this approach monistic, in the sense that it only requires type theory as foundations. Coq Formalization. The correctness of the first three pro- Although a few instances of models of type theory from the gram translations have been checked in the COQ proof assis- literature can be described in this formalism, most of them tant and have been instrumented as a COQ plugin. They have cannot, and even those which can are typically not presented been developed using the 8.5 release of COQ (The COQ De- this way and rely on an intermediate structure such as cate- velopment Team 2015) and are available at: gories with families or sets. Examples of models which are https://github.com/CoqHott/Program-translations-CC-omega syntactical but not program translations include the models of (Hofmann 1997) and (Altenkirch 1999). Amongst the few members of our class of models, one can cite the 2. Overview subset type (Paulin-Mohring 1989) and parametricity trans- lations (Bernardy and Lasson 2011), which are quite similar, The systems we study in this paper are built upon CC , a ω as well as the call-by-name forcing translation (Jaber et al. type theory featuring only dependent products, also known 2016). as Π-types. It features a denumerable hierarchy of universes The source theory S may contain more type constructors , and is a close relative to Luo’s ECC (Luo 1989). i or logical properties than T as long as there is a term in T Definition 1 (Typing system). We define two statements corresponding to the translation of those new type construc- mutually recursively as usual. The statement ` Γ means tors or logical properties. In practice, S is often T + axiom that the environment Γ is well formed, while Γ ` M : A and the translation ensures that the axiom can be assumed means that the term M has type A in environment Γ. The consistently. In this case, we also have a trivial translation typing rules are given in Figure 2.  stands for any i or the from T to T + axiom (the injection) and hence the two theo- impredicative universe ∗ when it is considered. ries are equiconsistent. Computational soundness can some- times be refined so that we can deduce strong normalization Before exposing specific translations between type theo- for S from strong normalization for T , although we will not ries, we sketch the general schema followed by those trans- describe this here. This is done by analyzing the preservation S lations. Given two type theories, the source theory and the of reduction steps rather than mere conversion. T t S target theory , we require that any term in is translated In all the translations presented in this paper, we use the [t] T t into a term in by induction over the syntax of , and fact that inhabitants of negative types can only be partially ι that there is an internal operation which coerces a trans- observed from the outside. For instance, dependent functions [[A]] lated type into a type of the target type theory. We write can only be observed using application. In the same way, ι [A] for . Note that the induced type may live in a bigger types can only be observed on a right-hand side of a typing universe, we will use this fact in Section 5.2. The context is judgment, which means that the translation only depends on then translated pointwise as: [[A]] and is oblivious to any additional data contained in [A]. [[·]] := · Figure 1 summarizes how this general scheme can be [[Γ, x : A]] := [[Γ]], x : [[A]] applied to various extensions of CCω to give a computa- tional meaning to new principles available in those exten- The expected theorem is typing soundness which states sions. Those translations are presented in this paper, but for that [[Γ]] ` [M] : [[A]] whenever Γ ` M : A. This requires in parametricity (already described in (Bernardy and Moulin particular to show a form of computational soundness which 2012)) and for functional reactive programming and forc- says that [M] ≡ [N] whenever M ≡ N in order to inter- ing which have already been described in (Jaber et al. 2012, pret the conversion rule transparently as another conversion. 2016)—the former being a particular case of the latter be- Another expected of the translation is consistency cause stream A ' N → A corresponds to forcing on the preservation, saying that the translation of ΠX : i.X is not inhabited. Typing soundness together with consistency Γ, ∆ ::= · | Γ, x : A

A, B, M, N ::= i | x | MN | λx : A. M | Πx : A. B

` Γ i < j Γ ` A : i Γ, x : A ` B : j

Γ ` i : j Γ ` Πx : A. B : max(i,j)

Γ, x : A ` M : B Γ ` Πx : A. B :  Γ ` M :Πx : A. B Γ ` N : A Γ ` M : B Γ ` A :  Γ ` λx : A. M :Πx : A. B Γ ` MN : B{x := N} Γ, x : A ` M : B

Γ ` A :  Γ ` A :  Γ ` M : B Γ ` A :  Γ ` A ≡ B ` · ` Γ, x : A Γ, x : A ` x : A Γ ` M : A

(λx : A. M) N ≡ M{x := N} (congruence rules ommitted)

Figure 2. Typing rules of CCω

pre-order of natural numbers. Those translations can be com- Definition 2. The intensional function translation [·]f from posed to add different principles together. CCω to CCω + Σ + B is defined by induction on terms as follows. Extensions. Depending on the translation, we will use var- [i]f := i ious extensions of the base CCω system, and we will refer [x] := x to them explicitely when needed. Those extensions are sum- f marized in Figure 3. They feature an impredicative sort ∗, [λx : A. M]f := (λx : [[A]]f . [M]f , true) as it exists in COQ, that will be used to distinguish between [MN] := π1 [M] [N] propositional extensionality and univalence. To reason about f f f [Πx : A. B] := (Πx : [[A]] . [[B]] ) × equality in CCω, we use Martin-Löf identity type, that is f f f B ∗ OQ typed in i (but it could be typed in as in C ). We may [[A]]f := [A]f need booleans and negative pairs, also known as Σ-types, [[A]] := [A] with projections1 and, in one case, surjective pairing. Note that f f , which means that for this trans- lation, ιf is the identity function, which is possible because Plan of the paper. In Section 3, we give a first translation [i]f := i. that acts on dependent products in order to realize the nega- 3 (Substitution lemma). For all terms M, N tion of functional extensionality. This shows for instance that and any variable x we have this principle is not provable in CCω. In Section 4, we show that a similar translation can be done on streams in order to [M{x := N}]f ≡ [M]f {x := [N]f } negate the principle of stream extensionality. In Section 5, we provide two translations, one that negates univalence and Proof. By induction on M. even propositional extensionality and one that introduces a notion of ad-hoc polymorhism in the form of a (quote) op- Using the substitution lemma, it is not difficult to derive erator à la Lisp. In Section 6, we show how some transla- the two main properties of the translation. tions have been formalized in the COQ proof assistant and Theorem 4 (Computational soundness). If M ≡ N then describe the COQ plugins that implement those translations. [M]f ≡ [N]f .

3. Intensional Functions Proof. The only non-trivial case is the β-reduction rule, which is proved by the following rewriting steps. In this section, we present a translation that augments ev- [(λx : A. M) N] := π ((λx : [[A]] . [M] ), true)[N] ery dependent function with a boolean value that cannot be f 1 f f f ≡ (λx : [[A]] . [M] )[N] seen externally as the only way to observe a function is by f f f ≡ [M] {x := [N] } applying it, which forgets about the boolean value. In this f f ≡ [M{x := N}] translation, every function coming from a translated term of f CCω will be equipped with the value true. All congruence rules are interpreted as-is.

1 We do not use a more general form of dependent elimination to simplify Theorem 5 (Typing soundness). If Γ ` M : A then the theoretical development in this paper. [[Γ]]f ` [M]f : [[A]]f . Impredicative Universe

A, B, M, N ::= ... | ∗

` Γ Γ ` A :  Γ, x : A ` B : ∗ Γ ` ∗ : i Γ ` Πx : A. B : ∗ Negative Pairs

A, B, M, N ::= ... | Σx : A. B | π1 M | π2 M | (M,N)

Γ ` A : i Γ, x : A ` B : j Γ ` M : A Γ ` N : B{x := M}

Γ ` Σx : A. B : max(i,j) Γ ` (M,N):Σx : A. B Γ ` M :Σx : A. B Γ ` M :Σx : A. B

Γ ` π1 M : A Γ ` π2 M : B{x := π1 M}

π1 (M,N) ≡ M π2 (M,N) ≡ N (π1 M, π2 M) ≡ M

Booleans

A, B, M, N ::= ... | B | true | false | if M ret P then N1 else N2

Γ ` M : B Γ ` P : B → i Γ ` N1 : P true Γ ` N2 : P false

Γ ` if M ret P then N1 else N2 : PM

` Γ ` Γ ` Γ

Γ ` B : i Γ ` true : B Γ ` false : B

if true ret P then N1 else N2 ≡ N1 if false ret P then N1 else N2 ≡ N2

Equality

A, B, M, N ::= ... | reflx | x =A y | J(P, e, t)

0 ` ΓΓ ` A : i Γ ` t, t : A Γ ` t : A 0 Γ ` t =A t : i Γ ` reflt : t =A t

0 0 Γ ` e : t =A t Γ, y : A, q : t =A t ` P : i Γ ` u : P {y := t}{q :=reflt} Γ ` J(P, e, u): P {y := t0}{q := e}

J(P, reflt, u) ≡ u

Figure 3. Extensions to CCω

Proof. By induction on the typing derivation. The only non- Theorem 6 (Consistency preservation). The translation pre- trivial cases are the conversion rule which is interpreted by serves consistency. the above theorem, and rules manipulating products which Proof. Indeed, we have are easily proved.

[[ΠA : i.A]]f := (ΠA : i.A) × B This translation can account for various extensions of which is inhabited if and only if ΠA : i.A is. CCω in a straightforward fashion. For instance, we can in- terpret inductive datatypes functorially, even though we will While being quite simple, this syntactical model already not describe it here. In the end of this section, we assume we implements strictly more than just CCω. Thanks to the ad- have at our disposal equality. ditional boolean worn by functions, it becomes possible to functions, we obtain in a straightforward way a variant for [stream A] := stream [[A]] × B s s intensional streams. Using intensional streams, it is possible [hd M] := hd (π [M] ) s 1 s to prove that stream extensionality (the fact that bisimilarity

[tl M]s := (tl (π1 [M]s), π2 [M]s) coincides with equality for streams) is not provable in CCω.

[stream_corec SM0 M1 N]s := Streams. We assume that our source and target theory are equipped with a type of streams and a of bisimilarity, (stream_corec [S]s [M0]s [M1]s [N]s, true) defined in Figure 5. Given a stream M, it is possible to get [bisim AMN] := bisim [[A]] (π [M] )(π [N] ) s s 1 s 1 s its first (or head) by using the destructor hd M and

[hdb M]s := hdb [M]s to get its tail by using the destructor tl M. The only way to create a stream is by coinduction2 using the constructor [tlb M] := tlb [M] s s stream_corec which produces, for any type S, a from [bisim_corec SM0 M1 P0 P1 N]s := S to stream A given a map S → A and a map S → S. bisim_corec Bisimilarity of streams is defined similarly. (λs0 s1. [S]s (s0, (π2 [P0]s)) (s1, (π2 [P1]s))) (λs0 s1 s. [M0]s (s0, (π2 [P0]s)) (s1, (π2 [P1]s)) s) The translation for intensional streams works by adding (λs s s. [M ] (s , (π [P ] )) (s , (π [P ] )) s) 0 1 1 s 0 2 0 s 1 2 1 s a boolean value to every stream, throwing it away when (π [P ] )(π [P ] )[N] 1 0 s 1 1 s s accessing the head, and threading it when accessing the tail.

[... ]s := ... Definition 9. The intensional stream translation [·]s from [[A]] := [A] s s CCω + stream into CCω + Σ + B + stream is defined by induction on terms as defined in Figure 4, where unspecified Figure 4. Definition of the intensional stream translation cases are defined by commutation of the translation with the corresponding syntax constructor. discriminate between two functions which are extensionally The properties satisfied by [·] are the same as for inten- equal, thus negating functional extensionality. For the rest of s this section we add identity types (Figure 3) in source and sional functions, the proof being similar. target theories. The translation extends directly by functori- Theorem 10 (Typing soundness). Assuming definitional ality. surjective pairing in the target, if Γ ` M : A then Theorem 7. We define functional extensionality funext as [[Γ]]s ` [M]s : [[A]]s. follows. Proof. Essentially the same as for the functional case. Con- funext := Π(A : )(B : )(f g : A → B). version is once again interpreted as conversion, as the sub- (Πx : A. f x = g x) → f = g B A→B stitution lemma is trivial. We only need surjective pairing in order to show that [N] is well-typed in the interpretation of Then there is, in the target system, a closed proof of s bisim_corec, otherwise all rules are straightforward. [[funext → ⊥]]f . Remark 11. The requirement of surjective pairing can be Proof. It is sufficient to instantiate in the premise A and lifted by defining a new dedicated coinductive type in the B by some arbitrary type X and f and g respectively by target system. We choose instead to reuse the same notion (λx : X. x, true) and (λx : X. x, false). This immedi- of bisimilarity for the sake of conceptual simplicity at the ately leads to a proof that true = false by injection and expense of a stronger requirement on the target system. thus a contradiction. Even more directly than for dependent functions, falsity Almost every model of CCω, if not all, validates funext, is translated into itself because the translation does not act so it is consistent with CCω. The previous theorem shows on dependent products, so that we can derive consistency that it is not provable in CCω. preservation directly.

Corollary 8. funext is independent from CCω. Theorem 12 (Consistency preservation). The translation It is possible to define a similar translation by adding preserves consistency. the boolean value “inside” the function [Πx : A. B]f := As coinductive types are negatives types, the only way to Πx : [[A]]f . [[B]]f × B. This translation is very similar so observe them is by using their destructors, which for the case we do not present it in detail. of streams amounts to observing only the head and the tail of a stream (coinductively). Thus, intensional streams allow to 4. Intensional Coinductive Types 2 Coinductive types are defined categorically as terminal coalgebras, so their We now enrich our source theory with coinductive datatypes, universal property can be used to construct maps going into them. This and by using a translation similar to the one of intensional corresponds to the coinduction principle. A, B, M, N ::= ... | stream A | hd M | tl M | stream_corec SM0 M1 N | bisim AMN | hdb M | tlb M | bisim_corec SM0 M1 P0 P1 N

Γ ` S : i Γ ` M0 : S → A Γ ` M1 : S → S Γ ` N : S

Γ ` stream_corec SM0 M1 N : stream A

Γ ` S : stream A → stream A → i Γ ` M0 : T0 Γ ` M1 : T1 Γ ` N : SP0 P1

Γ ` bisim_corec SM0 M1 P0 P1 N : bisim AP0 P1

where T0 := Π(s0 s1 : stream A). S s0 s1 → hd s0 =A hd s1 T1 := Π(s0 s1 : stream A). S s0 s1 → S (tl s0)(tl s1)

Γ ` A : i Γ ` A : i Γ ` M : stream A Γ ` N : stream A Γ ` stream A : i Γ ` bisim AMN : i

Γ ` M : stream A Γ ` M : stream A Γ ` M : bisim AN1 N2 Γ ` M : bisim AN1 N2

Γ ` hd M : A Γ ` tl M : stream A Γ ` hdb M : hd N1 =A hd N2 Γ ` tlb M : bisim A (tl N1)(tl N2)

hd (stream_corec SM0 M1 N) ≡ M0 N tl (stream_corec SM0 M1 N) ≡ stream_corec SM0 M1 (M1 N)

hdb (bisim_corec SM0 M1 P0 P1 N) ≡ M0 P0 P1 N

tlb (bisim_corec SM0 M1 P0 P1 N) ≡ bisim_corec SM0 M1 (tl P0)(tl P1)(M1 P0 P1 N) Figure 5. Coinductive types

negate stream extensionality by comparing two streams that Definition 14. The ad-hoc intensional type translation [·]t share the same elements, but differ on the additional boolean from CCω to CCω + Σ + B is defined by induction on terms value. As previous, the translation extends to identity types as follows. and we have: [i]t := (i × B, true)

Theorem 13. We define stream extensionality streamext [x]t := x as follows. [λx : A. M]t := λx : [[A]]t. [M]t streamext := Π(A : )(s1 s2 : stream A).  [MN]t := [M]t [N]t bisim A s1 s2 → s1 =stream A s2 [Πx : A. B] := (Πx : [[A]] . [[B]] , true) Then there is in the target system a closed proof of t t t [[A]]t := π1 [A]t [[streamext → ⊥]]s. As in previous sections, the translation verifies the substi- Proof. Once again, as in the functional case, intensional tution lemma and computational soundness, from which we equality observes the boolean in the translated streams but derive: bisimilarity does not. Theorem 15 (Typing soundness). If Γ ` M : A then

5. Intensional Types [[Γ]]t ` [M]t : [[A]]t. In this section, we exploit the underspecification of types Proof. By induction on the typing derivation of Γ ` M : CC in ω to give more content to types. Our first translation A. The only interesting cases are the two rules for type · × builds upon the B trick we used in the previous sections, constructors. but our second translation is much more subtle as it allows 1. Case : . Direct from the fact that to implement ad-hoc polymorphism in vanilla type theory. i j

5.1 Ad-hoc intensional types (i × B, true): j × B We define here a translation giving a very naive intensional 2. Case Πx : A. B : j. Direct from the fact that content to types. Just as we did in the function case, we enrich every type with a boolean which is not observable (Πx : [[A]]t. [[B]]t, true): i × B in the source theory, except for intensional equality. Theorem 16 (Consistency preservation). The translation Inductive TYPE0 : 1 := 0 preserves consistency. | Π0 :ΠA : TYPE0. (Elt0 A → TYPE0) → TYPE0 with Elt0 : TYPE0 → 0 := fun 0 Proof. This comes immediately from the fact that | Π0 AB ⇒ Πx : Elt0 A. Elt0 (B x) ... [[ΠA : i.A]]t := ΠA : i × B. π1 A Inductive TYPEi+1 : i+2 := | Ui : TYPEi+1 0 which is inhabited if and only if ΠA : i.A is | Πi+1 :ΠA : TYPE0. (Elt0 A → TYPEi+1) → TYPEi+1 | ... : ... Proposition 17. The above translation can easily accomo- i+1 | Πi+1 :ΠA : TYPEi+1. (Elti+1 A → TYPEi+1) → TYPEi+1 date the presence of an impredicative universe ∗ as long as with Elti+1 : TYPEi+1 → i+1 := fun there is one in the target system, by defining | Ui ⇒ TYPEi | 0 AB ⇒ Πx : A. (B x) [∗] := (∗ × , true). Πi+1 Elt0 Elti+1 t B | ... ⇒ ... i+1 The rules for an impredicative universe from Figure 3 are | Πi+1 AB ⇒ Πx : Elti+1 A. Elti+1 (B x) valid with this interpretation. The typing and reduction rules generated by these def- More generally, the translation can be extended to any initions are formally given in Figure 6. These inductive- type former Φ which is not a universe by defining recursive types respect the usual positivity conditions and

[Φ (M1, ... ,Mn)]t := (Φ ([M1]t, ... , [Mn]t), true). thus do not endanger the consistency of the target system. In the remainder of this section, we assume that we have an There are several points to discuss about codes. First, the equality and an empty type and their translations at hand. definition of codes makes a closed-world assumption on the type constructors from the source theory. Furthermore, as Theorem 18. We define propositional extensionality as: codes reflect faithfully the various operations at our disposal

propext := Π(AB : ∗). (A → B) → (B → A) → A =∗ B. in the source theory, we actually need to hardwire the uni- verse hierarchy in the inductive-recursive definitions by ex-

There is in the target system a proof of [[propext → ⊥]]t. ternally quantifying over the index i ∈ N. In order not to make the translation even more complex as it is, we simplify Proof. Just as in the functional extensionality proof, it is suf- a bit the authorized type formers to prevent a combinatorial ficient to take A and B to be the same underlying proposition explosion. Most notably, the codes for product must be du- X, but with a different boolean. plicated as many times as there are valid combinations of sorts for it. Using the of propositional extensionality in the It is interesting to remark that the resulting model suf- set model (Werner 1997), we get the independence of propo- fers from an expressivity limitation directly linked to the sitional extensionality with CC . ω inductive-recursive construction, that is, it cannot interpret In the same way, it is possible that the negation of univa- an impredicative universe. This would obviously entail a lence holds in the source theory. Using the validity of univa- non-wellfounded loop in the definition of codes, because lence in the simplicial model of (Kapulkin et al. 2012), we ∗ : 0 but all quantifications targetting ∗ would have to refer conclude that univalence is independent from CCω. to every TYPEi defined afterwards. 5.2 Ad-hoc polymorphism In order to define the translation, we need to know the In this section, we push the concept of type intensional- universe level at which a term is being typed at each induc- ity into its utmost consequencies, namely by showing that tive step. Therefore, we will use as a source system CCι , there exists a syntactic model of type theory interpreting a ω a variant of CCω which is slightly less expressive but well quoting operator on types. Such an operator allows to do stratified, defined in Figure 7. It would probably be possible case-analysis on the normal form of any type in the theory, to adapt the translation to the full-blown CCω system, at the which proves that ad-hoc polymorphism is actually compat- cost of a more involved translation. Essentially, the main dif- ible with usual type theory. Although this result looks sur- ference is that we annotate both the sequent and the variables prising, the model is actually quite straightfoward to obtain from the context with the level of their expected sort. We as soon as the target theory is expressive enough. will omit the indices when they are uniquely defined from We will assume here that our target theory features the context. induction-recursion, and we will use it to define the type ι of codes (as in (Dybjer 2000)). Proposition 20. CCω is a subsystem of CCω. Definition 19 (Codes). We define a family of inductive- Proof. It is indeed sufficient to erase all level annotations on ι recursive definitions TYPEi and Elti where i ranges over N colons to translate a well-typed CCω term into a well-typed as follows, using a suggestive syntax. CCω term. j A, B, M, N ::= ... | TYPEi | Elti | Ui | Πi | TYPE_reci

` Γ ` Γ ` Γ ` Γ j ≤ i j Γ ` TYPEi : i+1 Γ ` Elti : TYPEi → i Γ ` Ui : TYPEi+1 Γ ` Πi :ΠA : TYPEj . (Eltj A → TYPEi) → TYPEi ` Γ

Γ ` TYPE_rec0 :ΠP : TYPE0 → j .PΠ0 → ΠA : TYPE0.PA ` Γ

Γ ` TYPE_reci+1 :ΠP : TYPEi+1 → j .P Ui → P 0 → ... → P i → PΠ → ΠA : TYPEi+1.PA  Πi+1 Πi+1 i+1

i where PΠi := Π(A : TYPEi)(B : Elti A → TYPEi).PA → (Πx : Elti A. P (B x)) → P (Πi AB) j P j := Π(A : TYPEj )(B : Eltj A → TYPEi). (Πx : Eltj A. P (B x)) → P (Πi AB) Πi

j Elti (Πi AB) ≡ Πx : Eltj A. Elti (B x) Elti+1 Ui ≡ TYPEi 0 TYPE_rec0 P pΠ (Π0 AB) ≡ pΠ (TYPE_rec0 P pΠ A)(λx : Elt0 A. TYPE_rec0 P pΠ (B x))

TYPE_reci+1 P pU pΠ0 ... pΠi+1 Ui ≡ pU j TYPE_reci+1 P pU pΠ0 ... pΠi+1 (Πi+1 AB) ≡ pΠj AB

(λx : Eltj A. TYPE_reci+1 P pU pΠ0 ... pΠi+1 (B x)) i+1 TYPE_reci+1 P pU pΠ0 ... pΠi+1 (Πi+1 AB) ≡ pΠi+1 AB (TYPE_reci+1 P pU pΠ0 ... pΠi+1 A)

(λx : Elti+1 A. TYPE_reci+1 P pU pΠ0 ... pΠi+1 (B x)) Figure 6. Codes

Γ, ∆ ::= · | Γ, x :i A

A, B, M, N ::= i | x | Mi N | λx :i A. M | Πx :i A. B

` Γ Γ ` A :j+1 j Γ, x :j A ` B :i+1 i j ≤ i Γ ` i :i+2 i+1 Γ ` Πx :j A. B :i+1 i

Γ, x :i A ` M :j B Γ ` Πx :j A. B :i+1 i Γ ` M :i Πx :j A. B Γ ` N :j A Γ ` M :j B Γ ` A :i+1 i

Γ ` λx :j A. M :i Πx :j A. B Γ ` Mj N :i B{x := N} Γ, x :i A ` M :j B

Γ ` A :i+1 i Γ ` A :i+1 i Γ ` M :i B Γ ` A :i+1 i A ≡ B

` · ` Γ, x :i A Γ, x :i A ` x :i A Γ ` M :i A

(λx :i A. M)i N ≡ M{x := N} (congruence rules ommitted)

ι Figure 7. Typing rules of CCω

Contrarily to previous translations, this translation needs [i]T := Ui to be indexed by the universe level. This is because the code j [x] := x for dependent products is not polymorphic in the universe Ti level, which must thus be inserted explicitly during the trans- [λx :j A. M] := λx : [[A]] . [M] Ti Tj Ti lation. Therefore, the index is here to keep track of the cur- [Mj N] := [M] [N] rent universe level. Ti Ti Tj j [Πx :j A. B] := Π [A] (λx : [[A]] . [B] ) Ti i−1 Tj Tj−1 Ti

[[A]] := Elti [A] Ti Ti+1 Definition 21. The ad-hoc polymorphism translation [·] Ti∈ ι N [[·]]T := · from CCω to CCω + TYPEi∈N is defined by induction on [[Γ, x :i A]] := [[Γ]] , x : [[A]] terms as follows. T T Ti j In general, Πi may not be defined so that we arbitrarily fix j ~ 0 [quotei P M]T := Πi := Πi whenever i < j. Note that the context translation is i+1 TYPE_rec (λA : TYPE . Elt ([P ] A)) [M~ ] not indexed, as the corresponding level is hardcoded in each i i i Ti+2 Ti+1 variable from the context. Preservation of typing and reduction is straightfoward, as Proposition 22 (Substitution lemma). For all terms M, N everything has been done to obtain this property. and any variable x s.t. Γ, x : A, ∆ ` M : B, we have i j The type operator can be used to define non-parametric [M{x := N}] ≡ [M] {x := [N] } functions. For example, it becomes possible to prove Tj Tj Ti Σf : (ΠA : .A → A). f =ΠA: .A→A id → ⊥ Proof. By induction on the typing derivation of M. Actually,  we need a much weaker condition than typing on M for the by simply passing a function that is the negation on B and theorem to hold, which amounts to checking that variables the identity on other types. This shows that parametricity is are used at the correct universe level in the axiom case. not built-in in CCω.

Theorem 23 (Typing soundness). If Γ ` M :i A then 6. Formalization and Instrumentation [[Γ]] ` [M] : [[A]] . T Ti Ti This section presents the framework for the COQ formaliza- tion of some of the translations. We then describe the instru- Proof. By induction on the typing derivation of M. Conver- mentation of such translations as a COQ plugin. sion is interpreted by conversion itself, thanks to the substi- tution lemma, and the other rules are staightforward. 6.1 Deep Embedding using De Bruijn Indices We rely on the formalization of Pure Type Systems (PTS) Remark 24. When the source theory only features Π-types given by Siles and Herbelin (Siles and Herbelin 2012). It and universes, it is not clear whether the translation pre- consists in first defining untyped terms and then defining serves consistency. Indeed, we have the conversion and typing rules. Note that we could have also used another approach based on induction-recursion [[ΠA : .A]] ≡ ΠA : TYPE . Elt A i i Ti i i that defines directly well-typed terms as in the recent work of Altenkirch and Kaposi (Altenkirch and Kaposi 2016). but then there is no obvious closed code A made out of However, it appears that this approach is a bit too rigid as it Π-types and universes to give to a term of the above type forbids to use an untyped term during the translation, making in order to recover an inconsistency. For instance, trying to the translation very difficult to define in practice. feed it with the code of the impredicative encoding of falsity Terms are defined by an inductive type, where variables simply gives back the same type. are represented using De Bruijn indices and Sorts is an This phenomenon does not occur as soon as the source inductive type representing the sorts (either i or i and ∗). theory features an empty type ⊥, reflected in the codes as Fi s.t. Elt F ≡ ⊥. In this case, we can instantiate a proof of Inductive Term : Set := i i | Var : → Term the above type with F and we obtain immediately a proof of N i | Sort : Sorts → Term the empty type. | Π : Term → Term → Term Theorem 25 (Consistency preservation). If the source the- | λ : Term → Term → Term ory features an empty type ⊥, the ad-hoc polymorphic trans- | App : Term → Term → Term lation preserves consistency. | Eq : ∀ (A t1 t2 : Term), Term | refl : Term → Term We now turn to show that this translation allows to fully | J : ∀ (AP t1 u t2 p : Term), Term. observe the normal form of types by doing case analysis on The substitution of the n-th De Bruijn variable by N in M any term A : . We define formally what we mean by this i is defined by recursion on M. We write it M [n 7→ N]. Conver- under the notion of type quoting below. sion in the calculus, written M ≡ N, is defined as the reflexive, Definition 26 (Type quoting). A type quoting operator is symmetric and transitive closure of the beta reduction, de- ι given in CCω by the data described in Figure 8. fined as an inductive family. The type system is defined by mutual induction with the well-formedness of contexts as Theorem 27 (Type Quote). The [·] translation gives a Ti Inductive wf : Env → Prop := computational content to type quoting. | wf_nil : nil a | wf_cons : ∀ Γ As , Γ ` A : Sorts → A::Γ a Proof. As all types are represented by codes, the vari- where" Γ a" := (wf Γ) ous quote operators are simply defined in terms of the with typ : Env → Term → Term → Prop := TYPE_rec operators. | cVar : ∀ Γ Av , Γ `v → A ↓ v ⊂ Γ → Γ ` Varv : A A, B, M, N ::= ... | quote0 PNA | quotei+1 PMN0 ... Ni+1 A

Γ ` P : 0 → 0 Γ ` pΠ0 : PΠ0 Γ ` A : 0

Γ ` quote0 P pΠ0 A : PA

Γ ` P : i+1 → i+1 Γ ` pU : P i Γ ` pΠ0 : P 0 ... Γ ` pΠ : P i Γ ` pΠ : PΠ Γ ` A : i+1    Πi+1 i Πi+1 i+1 i+1 

Γ ` quotei+1 P pU pΠ0 ... pΠi+1 A : PA

where PΠi := Π(A : i)(B : A → i).PA → (Πx :i A. P (B x)) → P (Πx :i A. B x) P j := Π(A : j )(B : A → i). (Πx :j A. P (B x)) → P (Πx :j A. B x) Πi 0 quote0 P pΠ0 (Π0 AB) ≡ pΠ0 (quote0 P pΠ0 A)(λx :0 A. quote0 P pΠ0 (B x))

quotei+1 P pU pΠ0 ... pΠi+1 Ui ≡ pU

quotei+1 P pU pΠ0 ... pΠi+1 (Πx :j A. B) ≡ pΠj AB

(λx : A. quotei+1 P pU pΠ0 ... pΠi+1 (B x))

quotei+1 P pU pΠ0 ... pΠi+1 (Πx :i+1 A. B) ≡ pΠi+1 AB (quotei+1 P pU pΠ0 ... pΠi+1 A)

(λx : A. quotei+1 P pU pΠ0 ... pΠi+1 (B x)) Figure 8. Type Quoting

| cSort : ∀ Γ ss ’, Γ a → Axss ’ → Γ ` Sorts : Sorts ’ Theorem tsl_correctness : | cΠ : ∀ Γ ABss ’ s’’, Relss ’ s’’ → Γ ` A : Sorts → (∀ Γ, Γ a → Γt a) ∧ (∀ Γ MA , Γ ` M : A → Γt ` Mt : At). A::Γ ` B : Sorts ’ → Γ ` Π AB : Sorts ’’ 3 | cλ : ∀ Γ ABss ’ s’’ M, Relss ’ s’’ → Γ ` A : Sorts → In the additional materials accompanying this paper , we A::Γ ` B : Sorts ’ → A::Γ ` M : B → Γ ` λ AM : Π AB have formalized the intensional functions translation, the 4 | cApp : ∀ Γ MNAB , Γ ` M : Π AB → Γ ` N : A → intensional streams translation and ad-hoc intensional types Γ ` AppMN : B[0 7→ N] translation. In each case, computational soundness, typing | Cnv : ∀ Γ MABs , A ≡ B → Γ ` B : Sorts → soundness and consistency preservation have been checked. Γ ` M : A → Γ ` M : B ... (* typing rules for equality*) 6.2 Instrumentation as a COQ Plugin where" Γ ` t : A" := (typ Γ tA ). It is quite commonly agreed upon that type theory can be where A ↓ v ⊂ Γ means that the vth variable has type A in difficult to work with formally, which is why proof assis- Γ and Ax and Rel are two inductive families reflecting the tants were implemented in the first place. In particular, it is hierarchy on universes. not very convenient to prove new logical or computational It is straightforward to extend Term to integrate other principles directly through the paper translation, let alone in types such as Σ-types, booleans and streams. the deep embedding formalization. Luckily, our translations rely on a rather generic target theory, so that we can use the Now suppose S is a module defining the PTS for CCω (plus dependent pairs and identity type) and T the same PTS one underlying the COQ proof assistant. extended with booleans. The intensional functions transla- This is where plugins shine in. A COQ plugin is simply tion introduced in Section 3 can be defined directly by in- a program that, given a COQ proof term M, produces the duction on S.Term. translation [M] as another COQ term. This is a shallow em- bedding, and this process is orthogonal to the COQ formal- Fixpoint tsl (t : S.Term): T.Term := ization which uses deep embedding instead. Typically, the matcht with fact that [M] is still well-typed relies on the soudness the- ⇒ | S.Varv T.Varv orem that lives in the metatheory rather than in the object | S.Sorts ⇒ T.Sorts t t theory. | S.Π AB ⇒ T.Σ (Π A B ) Bool | S.λ AM ⇒ T.Pair (λ At Mt) true While there is no real point in implementing model trans- t t | S.AppMN ⇒ T.App (π1 M ) N lations in any formal foundations such as e.g. set theory, t t t | S.EqA t1 t2 ⇒ T.EqA t1 t2 here, the fact that we have an actual proof assistant at hand | S.refle ⇒ T.refle t makes the plugin quite useful. It allows to give the impres- t t t t t t | S.JAP t1 u t2 p ⇒ T.JA P t1 u t2 p sion to the user that she is working transparently in the end where"M t" := (tslM ). 3 https://github.com/CoqHott/Program-translations-CC-omega The typing soundness of the translation amounts to prove 4 Some bureaucratic lemmas about lifting of De Bruijn indices have been the following theorem by mutual induction. admitted for the type system extended with streams. See Readme.md. source theory, while it turns out everything is translated into (∀ AB ,(fg : A → B), (∀ x, fx = gx ) → f = g) → False. the target theory on the fly. In particular, typechecking re- mains decidable by construction, and one can take advantage Such a plugin has been defined for the intensional func- of all of the facilities provided by the host language. This ef- tions translation, the intensional streams translation and ad- fectively prevents the necessity to reimplement a standalone hoc intensional types translation. Code and examples come proof assistant for the source theory. with this article. Writing a plugin can be cumbersome, for two unrelated reasons. One has to know the internals of the COQ system, 7. Future Work which strongly restricts the amount of people able to do As a first step, we wish to improve the plugins which are it. Furthermore, a distinct term-translating program must be only prototypes for the moment. This requires generalizing written for every translation one wishes to internalize, which the program transformations presented in this paper to the implies that each translation requires a distinct plugin. For entire language of COQ—defining it on all inductive types, instance, the forcing plugin described in (Jaber et al. 2016) is coinductive types and records. This way, it will be possible quite different from the ones we implemented for this paper. to work on the translation layer without restriction and still As of today, there is no such thing as a meta-plugin that benefit from the new logical or computional principles avail- would allow to quickly prototype a term translation in COQ. able in this layer. Remark 28. The type theory implemented by COQ and the Regarding the type quote operator, an instrumentation as various extensions of CCω are slightly different, leading to a COQ plugin would require to define induction-recursion small discrepancies between the previously described trans- in COQ. This is the subject of on-going work and has al- lations and their actual instrumentation as a plugin. First, pa- ready been implemented in an experimental branch of COQ rameters do not appear in dependent pairs from the PTS pre- 8.5 developed by Matthieu Sozeau (https://github. sentation, while they do in COQ. Second, and more impor- com/mattam82/coq/tree/IR). However, the extension of tantly, the management of universes in COQ is more complex the syntactic guard condition from inductive to inductive- as it deals with universe variables and a graph of constraints. recursive definitions is still the subject of ongoing research. In the remaining of this section, we describe the com- Another possible line of work would be on the develop- mands provided by a plugin. First, any COQ term also per- ment of a generic COQ plugin that allows users to define taining to the source theory can be translated into the target their own compilation phase with a minimum effort—e.g., theory automatically. Assuming some constant c : A, the by defining only the translation on untyped terms in the deep plugin command embedding—and generate automatically the associated plu- gin for this transformation. This way, it will be possible to Translatec . experiment the frontier of what is provable in type theory at produces a new constant cf : [[A]] that is added to the current a low cost investment. environment and whose body is the translation of the body But the most important is the development of more trans- of c. lations in order to realize and logic principles. For Moreover, it is possible to add new symbols to the system instance, we wonder if it is possible to find a presentation of through a translation. To do so, it is sufficient to provide the setoid model as a program translation. for any such p : A its translation p• : [[A]]. This is reflected in the COQ plugin by the command Acknowledgments Implementp : A. This work has been funded by the CoqHoTT ERC Grant which opens a new goal of type [[A]]. When it is solved, 637339. the resulting term p• is used to automatically extend the translation with a term p : A by defining [p] := p•. References Theoretically, this amounts to considering a typing rela- T. Altenkirch. Extensional equality in intensional type theory. In tion `t in the translation layer defined by extending CIC with Proceedings of LICS, Trento, Italy, July 1999. the axiom Γ ` p : A. We easily get that if Γ ` M : B then t t T. Altenkirch and A. Kaposi. Type theory in type theory using [[Γ]] ` [M] : [[B]] using the typing correctness of the trans- quotient inductive types. In Proceedings of POPL, 2016. lation, which justifies the abovementioned equiconsistency result. J.-P. Bernardy and M. Lasson. Realizability and Parametricity in Pure Type Systems. In Foundations of Software Science In practice, the plugin can thus be used to inhabit new ax- and Computational Structures, volume 6604, pages 108–122, ioms. For instance, for the case of the intensional functions Saarbrücken, Germany, Mar. 2011. translation, we can add the negation of functional extension- ality in the translation layer by providing a term J.-P. Bernardy and G. Moulin. A computational interpretation of parametricity. In Proceedings of LICS, Dubrovnik, Croatia, June Implement neg_fun_ext : 2012. J.-P. Bernardy, P. Jansson, and R. Paterson. Parametricity and 1993. dependent types. In Proceedings of ICFP, Baltimore, Maryland, USA, 2010. C. Kapulkin, P. L. Lumsdaine, and V. Voevodsky. The sim- plicial model of univalent foundations. arXiv preprint J. Chapman. Type theory should eat itself. Electron. Notes Theor. arXiv:1211.2851, 2012. Comput. Sci., 228:21–36, Jan. 2009. ISSN 1571-0661. P. Dybjer. Internal type theory. In S. Berardi and M. Coppo, editors, Y. Lafont, B. Reus, and T. Streicher. Continuation semantics or Types for Proofs and Programs, volume 1158 of LNCS, pages expressing implication by negation. Univ. München, Inst. für 120–134. Springer Berlin Heidelberg, 1996. ISBN 978-3-540- Informatik, 1993. 61780-8. Z. Luo. ECC, an extended calculus of constructions. In Proceed- P. Dybjer. A general formulation of simultaneous inductive- ings of LICS, Pacific Grove, CA, USA, June 1989. recursive definitions in type theory. J. Symb. Log., 65(2):525– 549, 2000. C. Paulin-Mohring. Extraction de programmes dans le Calcul des M. Hoffman. Syntax and semantics of dependent types. In Seman- Constructions. Thèse d’université, Paris 7, Jan. 1989. URL tics and of Computation, pages 241–298, 1997. http://www.lri.fr/~paulin/PUBLIS/these.ps.gz. M. Hofmann. Extensional constructs in intensional type theory. V. Siles and H. Herbelin. Pure type systems conversion is always CPHC/BCS distinguished dissertations. Springer, 1997. ISBN typable. Journal of Functional Programming, 22(2):153–180, 978-3-540-76121-1. Mar 2012. G. Jaber, N. Tabareau, and M. Sozeau. Extending Type Theory with Forcing. In Proceedings of LICS, Dubrovnik, Croatia, June The AGDA Development Team. Agda. 2015. URL http://wiki. 2012. portal.chalmers.se/agda. G. Jaber, G. Lewertoski, P.-M. Pédrot, N. Tabareau, and M. Sozeau. The COQ Development Team. The Coq proof assistant reference The Definitional Side of the Forcing. In Proceedings of LICS, manual. 2015. URL http://coq.inria.fr. Version 8.5. New-York, USA, July 2016. B. Jacobs. Comprehension categories and the semantics of type B. Werner. Sets in types, types in sets. In Theoretical aspects of dependency. Theoretical Computer Science, 107(2):169 – 207, computer software, pages 530–546. Springer, 1997.