• Aucun résultat trouvé

Eliminating Reflection from Type Theory

N/A
N/A
Protected

Academic year: 2021

Partager "Eliminating Reflection from Type Theory"

Copied!
20
0
0

Texte intégral

(1)

HAL Id: hal-01849166

https://hal.archives-ouvertes.fr/hal-01849166v3

Submitted on 22 Nov 2018

HAL is a multi-disciplinary open access

archive for the deposit and dissemination of

sci-entific research documents, whether they are

pub-lished or not. The documents may come from

teaching and research institutions in France or

abroad, or from public or private research centers.

L’archive ouverte pluridisciplinaire HAL, est

destinée au dépôt et à la diffusion de documents

scientifiques de niveau recherche, publiés ou non,

émanant des établissements d’enseignement et de

recherche français ou étrangers, des laboratoires

publics ou privés.

Eliminating Reflection from Type Theory

Théo Winterhalter, Matthieu Sozeau, Nicolas Tabareau

To cite this version:

Théo Winterhalter, Matthieu Sozeau, Nicolas Tabareau. Eliminating Reflection from Type Theory:

To the Legacy of Martin Hofmann. CPP 2019 - 8th ACM SIGPLAN International Conference on

Certified Programs and Proofs, Jan 2019, Lisbonne, Portugal. pp.91-103, �10.1145/3293880.3294095�.

�hal-01849166v3�

(2)

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

Eliminating Reflection from Type Theory

To the Legacy of Martin Hofmann

Théo Winterhalter

Gallinette Project-Team, Inria Nantes, France theo.winterhalter@inria.fr

Matthieu Sozeau

Pi.R2 Project-Team, Inria and IRIF Paris, France

matthieu.sozeau@inria.fr

Nicolas Tabareau

Gallinette Project-Team, Inria Nantes, France nicolas.tabareau@inria.fr

Abstract

Type theories with equality reflection, such as extensional type theory (ET T), are convenient theories in which to for-malise mathematics, as they make it possible to consider provably equal terms as convertible. Although type-checking is undecidable in this context, variants of ET T have been

implemented, for example inNuPRL and more recently in

Andromeda. The actual objects that can be checked are not proof-terms, but derivations of proof-terms. This suggests that any derivation of ET T can be translated into a typecheck-able proof term of intensional type theory (IT T). However, this result, investigated categorically by Hofmann in 1995, and 10 years later more syntactically by Oury, has never given rise to an effective translation. In this paper, we pro-vide the first effective syntactical translation from ET T to IT T with uniqueness of identity proofs and functional ex-tensionality. This translation has been defined and proven

correct inCoq and yields an executable plugin that translates

a derivation in ET T into an actualCoq typing judgment.

Ad-ditionally, we show how this result is extended in the context of homotopy type theory to a two-level type theory. Keywords dependent types, translation, formalisation

1

Introduction

Type theories with equality reflection, such as extensional type theory (ET T), are convenient theories in which to for-malise mathematics, as they make it possible to consider provably equal terms as convertible, as expressed in the fol-lowing typing rule:

Γ ⊢xe : u =Av

Γ ⊢xu ≡ v : A

(1)

Here, the typeu =A v is Martin-Löf’s identity type with

only one constructorrefl u : u =Au which represents proofs

Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for third-party components of this work must be honored. For all other uses, contact the owner /author(s).

Conference’17, July 2017, Washington, DC, USA © 2018 Copyright held by the owner/author(s). ACM ISBN 978-x-xxxx-xxxx-x/YY/MM. https://doi.org/10.1145/nnnnnnn.nnnnnnn

of equality inside type theory, whereasu ≡ v : A means

thatu and v are convertible in the theory—and can thus

be silently replaced by one another in any term. Several variants of ET T have been considered and implemented, for

example inNuPRL [Allen et al. 2000] and more recently in

Andromeda [Bauer et al. 2016]. The prototypical example of the use of equality reflection is the definition of a coercion

function between two typesA and B that are equal (but not

convertible) by taking a term of typeA and simply returning

it as a term of typeB:

λ A B (e : A = B) (x : A). x : Π A B. A = B → A → B. In intensional type theory (IT T), this term does not

type-check becausex of type A can not be given the type B by

conversion. In ET T, however, equality reflection can be used to turn the witness of equality into a proof of conversion and

thus the type system validates the fact thatx can be given the

typeB. This means that one needs to guess equality proofs

during type-checking, because the witness of equality has been lost at the application of the reflection rule. Guessing it was not so hard in this example but is in general undecidable, as one can for instance encode the halting problem of any Turing machine as an equality in ET T. That is, the actual objects that can be checked in ET T are not terms, but instead derivations of terms. It thus seems natural to wonder whether any derivation of ET T can be translated into a typecheckable term of IT T. And indeed, it is well know that one can find

a corresponding term of the same type in IT T byexplicitly

transporting the termx of type A using the elimination of

internal equality on the witness of equalitye, noted e:

λ A B (e : A = B) (x : A). e∗x : Π A B. A = B → A → B.

This can be seen as a way to make explicit thesilent use

of reflection. Furthermore, by making the use of transport as economic as possible, the corresponding IT T term can be seen as a compact witness of the derivation tree of the original ET T term.

This result has first been investigated categorically in the pioneering work of Hofmann [1995, 1997], by showing that the term model of IT T can be turned into a model of ET T by quotienting this model with propositional equality. However, it is not clear how to extend this categorical construction to an explicit and constructive translation from a derivation in ET T to a term of IT T. In 2005, this result has been in-vestigated more syntactically by Oury [2005]. However, his

(3)

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 presentation does not give rise to an effective translation.

By aneffective translation we mean that it is entirely

con-structive and can be used to deterministicallycompute the

translation of a given ET T typing derivation. Two issues

prevent deriving aneffective translation from Oury’s

presen-tation, and it is the process of actual formalisation of the result in a proof assistant that led us to these discoveries. First, his handling of related contexts is not explicit enough, which we fix by framing the translation using ideas coming from the parametricity translation (section 1.2). Addition-ally, Oury’s proof requires an additional axiom in IT T on top of functional extensionality and uniqueness of identity proofs, that has no clear motivation and can be avoided by considering an annotated syntax (section 2.1).

Contributions. In this paper, we present the first effective syntactical translation from ET T to IT T (assuming unique-ness of identity proofs (UIP) and functional extensionality in IT T). By syntactical translation, we mean an explicit

transla-tion from a derivatransla-tionΓ ⊢xt : T of ETT (the x index testifies

that it is a derivation in ET T) to a contextΓ′, termt′and

typeT′of IT T such thatΓ′⊢t′:T′in IT T. This translation

enjoys the additional property that ifT can be typed in ITT,

i.e., Γ ⊢T , then T′≡T . This means in particular that a

theo-rem proven in ET T but whose statement is also valid in IT T can be automatically transferred to a theorem of IT T. For

instance, one could use alocal extension of theCoq proof

assistant with a reflection rule, without being forced to rely on the reflection in the entire development.

This translation can be seen as a way to build a syntac-tical model of ET T from a model of IT T as described more generally in Boulier et al. [2017] and has been entirely

pro-grammed and formalised inCoq [Coq development team

2017]. For this, we rely onTemplateCoq1[Anand et al. 2018],

which provides a reifier for Coq terms as represented in

Coq’s kernel as well as a formalisation of the type system of Coq. Thus, our formalisation of ETT is just given by adding the reflection rule to a subset of the original type system

of Coq. This allows us to extract concrete Coq terms and

types from a closed derivation of ET T, using a little trick to incorporate Inductive types and induction. We do not treat cumulativity of universes which is an orthogonal feature of Coq’s type theory.

Outline of the Paper. Before going into the technical devel-opment of the translation, we explain its main ingredients and differences with previous works. Then, in Section 2, we define the extensional and intensional type theories we consider. In Section 3, we define the main ingredient of the translation, which is a relation between terms of ET T and terms in IT T. Then, the translation is given in Section 4.

Sec-tion 5 describes theCoq formalisation and Sections 6 and 7

1

https://template-coq.github.io/template-coq/

discuss limitations and related work. The main proofs are given in detail in Appendices B and C.

TheCoq formalisation can be found in https://github.com/

TheoWinterhalter/ett-to-itt.

1.1 On the Need for UIP and Functional

Extensionality.

Our translation targets IT T plus UIP and functional exten-sionality, which correspond to the two following axioms

(where□i denotes the universe of types at leveli):

UIP : Π(A : □i) (x y : A) (e e′:x = y). e = e′

FunExt : Π(A : □i) (B : A → □i) (f д : Π(x : A). B x).

(Π(x : A). f x = д x) → f = д

The first axiom says that any two proofs of the same equality are equal, and the other one says that two (dependent)

func-tions are equal whenever they are pointwise equal2. These

two axioms are perfectly valid statements of IT T and they can be proven in ET T. Indeed, UIP can be shown to be equiv-alent to the Streicher’s axiom K

K : Π(A : □i). Π(x : A). Π(e : x = x). e = reflx

using the elimination on the identity type. But K is provable in ET T by considering the type

Π(A : □i). Π(x y : A). Π(e : x = y). e = reflx

which is well typed (using the reflection rule to show thate

has typex = x) and which can be inhabited by elimination of

the identity type. In the same way, functional extensionality is provable in ET T because

Π(x : A). f x = д x

→ x : A ⊢ f x ≡ д x by reflection

→ (λ(x : A).f x) ≡ (λ(x : A).д x) by congruence of ≡

→ f ≡ д byη-law

→ f = д

Therefore, applying our translation to the proofs of those theorems in ET T gives corresponding proofs of the same theorems in IT T. However, UIP is independent from IT T, as first shown by Hofmann and Streicher using the groupoid model [Hofmann and Streicher 1998], which has recently been extended in the setting of univalent type theory using the simplicial or cubical models [Bezem et al. 2013; Kapulkin

and Lumsdaine 2012]. Similarly, Boulieret al. have shown

that functional extensionality is independent from IT T using a simple syntactical translation [Boulier et al. 2017].

Therefore, our translation provides proofs of axioms in-dependent from IT T, which means that the target of the translation already needs to have both UIP and functional extensionality. Part of our work is to show formally that they are the only axioms required.

2

In Homotopy Type Theory (HoT T) [Univalent Foundations Program 2013], the functional extensionality axiom is stated in a more complete way, using the notion of adjoint equivalences, but this more complete way collapses to our simpler statement in presence of UIP.

(4)

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

1.2 Heterogeneous Equality and the Parametricity

Translation.

The basic idea behind the translation from ET T to IT T is to interpret conversion using the internal notion of

equal-ity,i.e., the identity type. But this means that two terms of

two convertible types that were comparable in ET T become comparable in IT T only up-to the equality between the two types. One possible solution to this problem is to consider a

native heterogeneous equality, such asJohn Major equality

introduced by McBride [2000]. However, to avoid adding additional axioms to IT T as done by Oury [2005], we prefer to encode this heterogeneous equality using the following dependent sums:

tTU u := Σ(p : T = U ). p∗t = u.

During the translation, the same term occurring twice can be translated in two different manners, if the corresponding typing derivations are different. Even the types of the two different translations may be different. However, we have the strong property that any two translations of the same term only differ in places where transports of proof of equal-ity have been injected. To keep track of this property, we

introduce the relationt ∼ t′between two terms of IT T, of

possibly different types. The crux of the proof of the

transla-tion is to guarantee that for every two termst1andt2such

thatΓ ⊢t1:T1,Γ ⊢t2:T2andt1∼t2, there existsp such that

Γ ⊢p : t1T1T2t2. However, during the proof, variables of

different but (propositionally) equal types are introduced and

the context cannot be maintained to be the same for botht1

andt2. Therefore, the translation needs to keep track of this

duplication of variables, plus a proof that they are heteroge-neously equal. This mechanism is similar to what happens in the (relational) internal parametricity translation in IT T introduced by Bernardy et al. [2012] and recently rephrased

in the setting ofTemplateCoq [Anand et al. 2018]. Namely, a

context is not translated as a telescope of variables, but as a telescope of triples consisting of two variables plus a witness that they are in the parametric relation. In our setting, this amounts to consider telescope of triples consisting of two variables plus a witness that they are heterogeneously equal. We can express this by considering the following dependent sums:

Pack A1A2:= Σ(x : A1). Σ(y : A2).xA1A2y.

This presentation inspired by the parametricity translation is crucial in order to get an effective translation, because it is necessary to keep track of the evolution of contexts when doing the translation on open terms. This ingredient is missing in Oury’s work [Oury 2005], which prevents him

from deducing an effective (i.e., constructive and computable)

translation from his theorem.

2

Definitions of Extensional and

Intensional Type Theories

This section presents the common syntax, typing and main properties of ET T and IT T. Our type theories feature a uni-verse hierarchy, dependent products and sums as well as Martin Löf ’s identity types.

2.1 Syntax of ETT and ITT

The common syntax of ET T and IT T is given in Figure 1. It

features: dependent productsΠ(x : A). B, with (annotated)

λ-abstractions and (annotated) applications, negative

depen-dent sumsΣ(x : A). B with (annotated) projections, sorts □i,

identity typesu =A v with reflection and elimination as

well as terms realising UIP and functional extensionality. An-notating terms with otherwise computationally irrelevant typing information is a common practice when studying the syntax of type theory precisely (see [Streicher 1993] for a

similar example). We will writeA → B for Π(_ : A). B the

non-dependent product / function type.

We consider a fixed universe hierarchy without cumula-tivity, which ensures in particular uniqueness of typing (2.2) which is important for the translation.

About Annotations. Although it may look like a technical detail, the use of annotation is more fundamental in ET T than it is in IT T (where it is irrelevant and doesn’t affect the theory). And this is actually one of the main differences between our work (and that of Martin Hofmann [1995] who has a similar presentation) and the work of Oury [2005].

Indeed, by using the standard model where types are inter-preted as cardinals rather than sets, it is possible to see that

the equalitynat → nat= nat → bool is independent from

the theory, it is thus possible to assume it (as an axiom, or for

those that would still not be convinced, simply under aλ that

would introduce this equality). In that context, the identity

mapλ(x : nat). x can be given the type nat → bool and we

thus type(λ(x : nat). x) 0 : bool. Moreover, the β-reduction

of the non-annotated system used by Oury concludes that this expression reduces to 0, but cannot be given the type

bool (as we said, the equality nat → nat= nat → bool is

independent from the theory, so the context is consistent). This means we lack subject reduction in this case (or unique-ness of types, depending on how we see the issue). Our

presentation has a blockedβ-reduction limited to matching

annotations:(λ(x : A).B. t ) @x:A.Bu = t[x ←u], from which

subject reduction and uniqueness of types follow.

Although subtle, this difference is responsible for Oury’s need for an extra axiom. Indeed, to treat the case of equality of applications in his proof, he needs to assume the congru-ence rule for heterogeneous equality of applications, which is not provable when formulated with John Major equality (Fig. 2). Thanks to annotations and our notion of hetero-geneous equality, we can prove this congruence rule for applications.

(5)

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 s ::= □i (i ∈ N) sorts (universes)

T , A, B, t,u,v ::= x | λ(x : A).B.t | t @x:A.Bu | Π(x : A). B | s dependent λ-calculus

| ⟨u;v⟩x:A.B | π1x:A.Bp | π2x:A.Bp | Σ(x : A). B dependent pairs

| reflAu | J(A,u, x.e.P,w,v,p) | u =Av propositional equality

| funext(x : A, B, f , д, e ) | uip(A, u, v, p, q) equality axioms

Γ, ∆ ::= • | Γ, x : A contexts

Figure 1. Common syntax of ETT and ITT

JMAPP

f1 ∀(x:U1).V1∀(x:U2).V2 f2 u1U1U2u2 f1u1V1[x←u1]V2[x←u2] f2u2

Figure 2. Congruence of heteogeneous equality

2.2 The Typing Systems

As usual in dependent type theory, we consider contexts which are telescopes whose declarations may depend on any

variable already introduced. We noteΓ ⊢t : A to say that t

has typeA in context Γ. Γ ⊢ A shall stand for Γ ⊢ A : s for

some sorts and similarly Γ ⊢ A ≡ B stands for Γ ⊢ A ≡ B : s.

We use two relations(s, s′) ∈Ax (written (s, s′) for short)

and(s, s′, s′′) ∈R (written (s, s′, s′′)) to constrain the sorts

in the typing rules for universes, dependent products and dependent sums, as is done in any Pure Type System (PTS). In our case, because we do not have cumulativity, the rules are as follows:

(□i, □i+1) ∈Ax (□i, □j, □max(i, j )) ∈R We give the typing rules of IT T in Figure 3. The rules are standard and we do not explain them. Let us just point out the

conversion rule, which says thatu : A can be given the type

u : B when A ≡ B, i.e., when A and B are convertible. As the notion of conversion is central in our work—the conversion of ET T being translated to an equality in IT T—we provide an exhaustive definition of it, with computational conversion

rules (includingβ-conversion or reduction of the elimination

principle of equality over reflexivity, see Figure 4), however congruence conversion rules can be found in Appendix A (Figure 6). Note that we use Christine Paulin-Möhring’s vari-ant of the J rule rather than Martin-Löf ’s original formulation. Although pretty straightforward, being precise here is very

important, as for instance the congruence rule forλ-terms

is the reason why functional extensionality is derivable in ET T. Congruence of equality terms is a standard extension of congruence to the new principles we add (UIP and functional extensionality).

ET T is thus simply an extension of IT T (we write⊢xfor

the associated typing judgment) with the reflection rule on equality, which axiomatises that propositionally equal terms are convertible (see Equation 1). Note that, as already

men-tioned, in the presence of reflection andJ, UIP is derivable

so we could remove it from ET T, but keeping it allows us to share a common syntax which makes the statements of theorems simpler and does not affect the development.

2.3 General Properties of ITT and ETT

We now state the main properties of both IT T and ET T. We do not detail their proof as they are standard and can be

found in theCoq formalisation.

First, although not explicit in the typing system, weaken-ing is admissible in ET T and IT T.

Lemma 2.1 (Weakening). If Γ ⊢ J and ∆ extends Γ (possibly interleaving variables) then ∆ ⊢ J .

Then, as mentioned above, the use of a non-cumulative

hierarchy allows us to prove that a termt can be given at

most one type in a contextΓ, up-to conversion.

Lemma 2.2 (Uniqueness of typing). If Γ ⊢u : T1and Γ ⊢u :

T2then Γ ⊢T1≡T2.

Finally, an important property of the typing system (seen as a mutual inductive definition) is the possibility to deduce hypotheses from their conclusion, thanks to inversion of typing. Note that it is important here that our syntax is annotated for applications and projections as it provides a richer inversion principle.

Lemma 2.3 (Inversion of typing).

1. If Γ ⊢x : T then (x : A) ∈ Γ and Γ ⊢ A ≡ T .

2. If Γ ⊢ □i :T then Γ ⊢ □i+1 ≡T .

3. If Γ ⊢ Π(x : A). B : T then Γ ⊢ A : s and Γ, x : A ⊢ B : s′

and Γ ⊢s′′≡T for some (s, s′, s′′).

4. If Γ ⊢λ(x : A).B.t : T then Γ ⊢ A : s and Γ, x : A ⊢ B : s′

and Γ,x : A ⊢ t : B and Γ ⊢ Π(x : A). B ≡ T .

5. If Γ ⊢u @x:A.Bv : T then Γ ⊢ A : s and Γ, x : A ⊢

B : s′and Γ ⊢ u : Π(x : A). B and Γ ⊢ v : A and

Γ ⊢B[x ←u] ≡ T .

6. . . . Analogous for the remaining term and type

construc-tors.

Proof. Each case is proven by induction on the derivation (which corresponds to any number of applications of the

conversion rule following one introduction rule). □

3

Relating Translated Expressions

We want to define a relation on terms that equates two terms that are the same up to transport. This begs the question of

(6)

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 Well-formedness of contexts. ⊢ • ⊢Γ Γ ⊢A ⊢Γ,x : A (x < Γ) Types. ⊢Γ Γ ⊢s : s′(s, s ′ ) Γ ⊢A : s Γ,x : A ⊢ B : s ′ Γ ⊢ Π(x : A). B : s′′ (s, s ′, s′′ ) Γ ⊢A : s Γ,x : A ⊢ B : s′ Γ ⊢ Σ(x : A). B : s′′ (s, s ′, s′′ ) Γ ⊢A : s Γ ⊢u : A Γ ⊢v : A Γ ⊢u =Av : s Structural rules. ⊢Γ (x : A) ∈ Γ Γ ⊢x : A Γ ⊢u : A Γ ⊢A ≡ B : s Γ ⊢u : B λ-calculus terms. Γ ⊢A : s Γ,x : A ⊢ B : s′ Γ,x : A ⊢ t : B Γ ⊢λ(x : A).B.t : Π(x : A). B Γ ⊢A : s Γ,x : A ⊢ B : s′ Γ ⊢t : Π(x : A). B Γ ⊢u : A Γ ⊢t @x:A.Bu : B[x ←u] Γ ⊢u : A Γ ⊢A : s Γ,x : A ⊢ B : s′ Γ ⊢v : B[x ←u]

Γ ⊢ ⟨u;v⟩x:A.B :Σ(x : A). B

Γ ⊢p : Σ(x : A). B Γ ⊢π1x:A.Bp : A Γ ⊢p : Σ(x : A). B Γ ⊢π2x:A.Bp : B[x ← π x:A.B 1 p] Equality terms. Γ ⊢A : s Γ ⊢u : A Γ ⊢reflAu : u =Au Γ ⊢e1, e2:u =Av Γ ⊢uip(A, u, v, e1, e2) :e1= e2 Γ ⊢A : s Γ ⊢u,v : A Γ,x : A, e : u =Ax ⊢ P : s′ Γ ⊢p : u =Av Γ ⊢w : P[x ←u, e ← reflAu] Γ ⊢J(A, u, x .e.P, w, v, p) : P [x ← v, e ← p] Γ ⊢ f ,д : Π(x : A). B

Γ ⊢e : Π(x : A). f @x:A.Bx =Bд @x:A.Bx

Γ ⊢funext(x : A, B, f , д, e ) : f = д

Figure 3. Typing rules

what notion of transport is going to be used. Transport can be defined from elimination of equality as follows:

Definition 3.1 (Transport). Given Γ ⊢ p : T1 =s T2 and

Γ ⊢t : T1we define the transport oft along p, written p∗t, as

J(s,T1, X .e. T1 →X, λ(x : T1).T1.x,T2, p) @T1.T2t such that

Γ ⊢p∗t : T2.

However, in order not to confuse the transports added by the translation with the transports that were already present

in the source, we considerp as part of the syntax in the

reasoning. It will be unfolded to its definition only after the complete translation is performed. This idea is not novel as

Hofmann already had aSubst operator that was part of his

IT T (noted T TIin his paper [Hofmann 1995]).

We first define the (purely syntactic) relation⊏ between

ET T terms and IT T terms in Figure 5 stating that the IT T term is simply a decoration of the first term by transports. Its purpose is to state how close to the original term its

transla-tion is. Then, we extend this relatransla-tion to a similarity relatransla-tion∼

on ET T terms by taking its symmetric and transitive closure:

∼B (⊏ ∪ ⊏−1)+

Lemma 3.2 (∼ is an equivalence relation). ∼ is reflexive, symmetric and transitive.

Proof. For reflexivity we proceed by induction on the term. □ The goal is to prove that two terms in this relation, that are well-typed in the target type theory, are heterogeneously equal. As for this notion, we recall the definition we

previ-ously gave:tTU u := Σ(p : T = U ). pt = u. This

defini-tion of heterogeneous equality can be shown to be reflexive, symmetric and transitive. Because of UIP, heterogeneous equality collapses to equality when taken on the same type.

Lemma 3.3. If Γ ⊢e : uAAv then there exists p such that

Γ ⊢p : u =Av.

Proof. This holds thanks to UIP on equality, which implies K,

and so the proof ofA = A can be taken to be reflexivity. □

Note. In particular,  on types corresponds to equality. This is not as trivial as it sounds, one might be concerned about what

happens if we have Γ ⊢e : Ass′ B with two distinct sorts

s and s′. We would thus haves = s, however, for this to be

well-typed, we need to give a common type tos and s′, which

can only be achieved ifs and s′are actually the same sort.

Before we can prove the fundamental lemma stating that two terms in relation are heterogeneously equal, we need to consider another construction. As explained in the intro-duction, when proving the property by induction on terms, we introduce variables in the context that are equal only up-to heterogeneous equality. This phenomenon is similar to what happens in the parametricity translation [Bernardy et al. 2012]. Our fundamental lemma on the decoration

re-lation∼ assumes two related terms of potentially different

(7)

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 Computation. Γ ⊢A : s Γ,x : A ⊢ B : s′ Γ,x : A ⊢ t : B Γ ⊢u : A

Γ ⊢ (λ(x : A).B.t ) @x:A.Bu ≡ t[x ←u] : B[x ←u]

Γ ⊢A : s Γ ⊢u : A Γ,x : A, e : u =Ax ⊢ P : s′ Γ ⊢w : P[x ←u, e ← reflAu]

Γ ⊢J(A, u, x .e.P, w, u, reflAu) ≡ w : P[x ←u, e ← reflAu]

Γ ⊢A : s Γ ⊢u : A Γ,x : A ⊢ B : s′ Γ ⊢v : B[x ←u]

Γ ⊢π1x:A.B⟨u;v⟩x:A.B ≡u : A

Γ ⊢A : s Γ ⊢u : A Γ,x : A ⊢ B : s′ Γ ⊢v : B[x ←u]

Γ ⊢π2x:A.B ⟨u;v⟩x:A.B ≡v : B[x ←u] Conversion.

Γ ⊢t1≡t2:T1 Γ ⊢T1≡T2

Γ ⊢t1≡t2:T2

Figure 4. Main conversion rules (omitting congruence rules)

typesT 1 and T 2 to produce an heterogeneous equality

be-tween them. For induction to go through under binders (e.g. for dependent products and abstractions), we hence need to consider the two terms under different, but heterogeneously equal contexts. Therefore, the context we produce will not only be a telescope of variables, but rather a telescope of triples consisting of two variables of possibly different types, and a witness that they are heterogeneously equal. To make this precise, we define the following macro:

Pack A1A2:= Σ(x : A1). Σ(y : A2).x  y

together with its projections

Proj1p := π . 1p Proj2p := π . 1π . 2p Projep := π . 2π . 2p. We can then extend this notion canonically to contexts of the same length that are well formed using the same sorts:

Pack (Γ1, x : A1) (Γ2, x : A2) :=

(Pack Γ1Γ2),x : Pack (A1[γ1]) (A2[γ2])

Pack • • := •.

When we pack contexts, we also need to apply the correct projections for the types in that context to still make sense.

Assuming two contextsΓ1andΓ2of the same length, we can

define left and right substitutions:

γ1 := [x ← Proj1x | (x : _) ∈ Γ1]

γ2 := [x ← Proj2x | (x : _) ∈ Γ2].

These substitutions implement lifting of terms to packed

contexts:Γ,Pack Γ1Γ2⊢t[γ1] :A[γ1] wheneverΓ, Γ1⊢t : A

(resp.Γ,Pack Γ1Γ2⊢t[γ2] :A[γ2] wheneverΓ, Γ2⊢t : A).

For readability, whenΓ1andΓ2are understood we will

writeΓpforPack Γ1Γ2.

Implicitly, whenever we use the notationPack Γ1 Γ2 it

means that the two contexts are of the same length and well-formed with the same sorts. We can now state the fun-damental lemma.

Lemma 3.4 (Fundamental lemma). Lett1andt2be two terms.

If Γ, Γ1⊢t1:T1and Γ, Γ2⊢t2:T2andt1∼t2then there exists

p such that Γ, Pack Γ1Γ2⊢p : t1[γ1]T1[γ1]T2[γ2]t2[γ2].

Proof. The proof is by induction on the derivation oft1∼t2.

We show the three most interesting cases: • Var

x ∼ x

Ifx belongs to Γ, we apply reflexivity—together with

uniqueness of typing (2.2)—to conclude. Otherwise,

Projex has the expected type (since x[γ1] ≡Proj1x

andx[γ2] ≡Proj2x).

• Application

t1∼t2 A1∼A2 B1∼B2 u1∼u2

t1@x:A1.B1u1∼t2@x:A2.B2u2

We haveΓ, Γ1⊢t1@x:A1.B1u1:T1andΓ, Γ2⊢t2@x:A2.B2

u2 :T2which means by inversion (2.3) that the

sub-terms are well-typed. We apply the induction hypoth-esis and then conclude.

• TransportLeft

t1∼t2

p∗t1∼t2

We haveΓ, Γ1 ⊢p∗t1:T1andΓ, Γ2⊢t2:T2. By

inver-sion (2.3) we haveΓ, Γ1⊢p : T

1 = T1andΓ, Γ1⊢t1:T ′ 1.

By induction hypothesis we havee such that Γ, Γp⊢e :

(8)

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 t1⊏ t2 t1⊏ p∗t2 x ⊏ x A1⊏ A2 B1 ⊏ B2 Π(x : A1). B1⊏ Π(x : A2). B2 A1⊏ A2 B1⊏ B2 Σ(x : A1).B1⊏ Σ(x : A2).B2 A1⊏ A2 u1⊏ u2 v1 ⊏ v2 u1=A1v1⊏ u2=A2v2 s ⊏ s A1 ⊏ A2 B1⊏ B2 t1⊏ t2 λ(x : A1).B1.t1⊏ λ(x : A2).B2.t2 t1⊏ t2 A1 ⊏ A2 B1⊏ B2 u1 ⊏ u2 t1@x:A1.B1u1⊏ t2@x:A2.B2u2 A1⊏ A2 B1 ⊏ B2 t1⊏ t2 u1 ⊏ u2 ⟨t1;u1⟩x:A1.B1 ⊏ ⟨t2;u2⟩x:A2.B2 A1⊏ A2 B1⊏ B2 p1⊏ p2 πx:A1.B1 1 p1⊏ π x:A2.B1 1 p2 A1⊏ A2 B1 ⊏ B2 p1⊏ p2 πx:A1.B1 2 p1 ⊏ π x:A2.B2 2 p2 A1 ⊏ A2 u1⊏ u2 reflA1u1⊏ reflA2u2 A1⊏ A2 B1 ⊏ B2 f1⊏ f2 д1⊏ д2 e1⊏ e2 funext(x : A1, B1, f1, д1, e1) ⊏ funext(x : A2, B2, f2, д2, e2) A1 ⊏ A2 u1⊏ u2 v1⊏ v2 p1⊏ p2 q1⊏ q2 uip(A1,u1,v1, p1, q1) ⊏ uip(A2,u2,v2, p2, q2) A1⊏ A2 u1⊏ u2 P1 ⊏ P2 w1 ⊏ w2 v1 ⊏ v2 p1 ⊏ p2

J(A1,u1, x.e.P1, w1,v1, p1) ⊏ J(A2,u2, x.e.P2, w2,v2, p2)

Figure 5. Relation ⊏

t1[γ1]  t2[γ2] . From transitivity and symmetry we

only need to provide a proof oft1[γ1]  p[γ1]t1[γ1]

which is inhabited by⟨p[γ1] ;refl (p[γ1]t1[γ1])⟩_._.

The complete proof can be found in Appendix B. □

We can also prove that∼ preserves substitution.

Lemma 3.5. Ift1 ∼ t2 andu1 ∼ u2 thent1[x ←u1] ∼

t2[x ←u2].

Proof. We proceed by induction on the derivation oft1 ∼

t2. □

4

Translating ETT to ITT

4.1 The Translation

We now define the translations (let us stress the plural here)

of an extensional judgment. We extend ⊏ canonically to

contexts (Γ ⊏ Γ when they bind the same variables and the

types are in relation for⊏).

Before defining the translation, we define a set

JΓ⊢xt : AK

of typing judgments in IT T associated to a typing judgment

Γ ⊢x t : A in ETT. The idea is that this set describes all

the possible translations that lead to the expected property.

WhenΓ ⊢t : A ∈

JΓ⊢xt : AK, we say thatΓ ⊢t : A realises

Γ ⊢xt : A. The translation will be given by showing that this

set is inhabited by induction on the derivation.

Definition 4.1 (Characterisation of possible translations).

• For any ⊢x Γ we defineJ⊢x ΓKas a set of valid

judg-ments (in IT T) such that⊢ Γ ∈

J⊢x ΓKif and only if

Γ ⊏ Γ.

• Similarly,Γ ⊢t : A ∈JΓ⊢xt : AKiff⊢Γ ∈J⊢xΓKand

A ⊏ A and t ⊏ t.

In order to better master the shape of the produced realiser, we state the following lemma which shows that it has the same head type constructor as the type it realises. This is important for instance for the case of an application, where we do not know a priori if the translated function has a dependent product type, which is required to be able to use the typing rule for application.

Lemma 4.2. We can always choose typesT that have the

same head constructor asT .

Proof. Assume we have Γ ⊢t : T ∈JΓ⊢xt : TK. By definition

of⊏, T ⊏ T means that T is shaped pq ... rT

withT

having the same head constructor asT . By inversion (2.3), the

subterms are typable, includingT

. Actually, from inversion,

we even get that the type ofT

is a universe. Then, using

lemma 3.4 and lemma 3.3, we getΓ ⊢e : T = T

. We conclude

withΓ ⊢et : T

xt : TK. □

Finally, in order for the induction to go through, we need to

know that when we have a realiser of a derivationΓ ⊢xt : T ,

we can pick an arbitrary other type realisingΓ ⊢x T and

still get a new derivation realisingΓ ⊢xt : T with that type.

This is important for instance for the case of an application, where the type of the domain of the translated function may differ from the type of the translated argument. So we need

to be able to change ita posteriori.

Lemma 4.3. When we have Γ ⊢t : T ∈ JΓ ⊢x t : TK and

Γ ⊢T′∈xTK then we also have Γ⊢t′:T

xt : TK

for somet′.

(9)

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880

Proof. By definition we haveT ⊏ T and T ⊏ T′and thus

T ∼ T and T ∼ T′, implyingT ∼ T

by transitivity (3.2). By

lemma 3.4 (in the caseΓ1≡Γ2≡ •) we getΓ ⊢p : T  T

′ for

somep. By lemma 3.3 (and lemma 4.2 to give universes as

types toT and T ′ ) we can assumeΓ ⊢p : T = T ′ . ThenΓ ⊢ p∗t : T ′

is still a translation since⊏ ignores transports. □

We can now define the translation. This is done by mutual induction on context well-formedness, typing and conver-sion derivations. Indeed, in order to be able to produce a realiser by induction, we need to show that every conver-sion in ET T is translated as an heterogeneous equality in IT T.

Theorem 4.4 (Translation).

• If ⊢xΓ then there exists ⊢ Γ ∈J⊢xΓK,

• If Γ ⊢xt : T then for any ⊢ Γ ∈J⊢xΓK there exist t and

T such that Γ ⊢ t : T ∈JΓ⊢xt : TK,

• If Γ ⊢x u ≡ v : A then for any ⊢ Γ ∈ J⊢x ΓK there

existA ⊏ A, A ⊏ A′,u ⊏ u,v ⊏ v and e such that

Γ ⊢e : uAA′v.

Proof. We prove the theorem by induction on the derivation in the extensional type theory. We only show the two most interesting cases of application and conversion. The complete proof is given in Appendix C.

• Application

Γ ⊢xA : s Γ,x : A ⊢xB : s′

Γ ⊢xt : Π(x : A). B Γ ⊢xu : A

Γ ⊢xt @x:A.Bu : B[x ←u]

Using IH together with lemmata 4.2 and 4.3 we get

Γ ⊢A : s and Γ, x : A ⊢ B : s′andΓ ⊢t : Π(x : A). B

andΓ ⊢u : A meaning we can conclude Γ ⊢ t @x:A.Bu :

B[x ←u] ∈JΓ⊢xt @x:A.Bu : B[x ←u]K.

• Conversion

Γ ⊢xu : A Γ ⊢xA ≡ B

Γ ⊢xu : B

By IH and lemma 3.3 we haveΓ ⊢ e : A = B which

impliesΓ ⊢ A ∈

JΓ ⊢x AKby inversion (2.3), thus,

from lemma 4.3 and IH we getΓ ⊢u : A, yielding

Γ ⊢e∗u : B ∈JΓ⊢xu : BK.

4.2 Meta-theoretical Consequences

We can check that all ET T theorems whose type are typable in IT T have proofs in IT T as well:

Corollary 4.5 (Preservation of ITT). If ⊢xt : T and ⊢ T then

there existt such that ⊢ t : T ∈J⊢xt : TK.

Proof. Since ⊢ • ∈ J⊢x •K, by Theorem (4.4), there existst

andT such that ⊢ t : T ∈

J⊢x t : TKBut as⊢T , we have

⊢T ∈JxTK, and, using Lemma 4.3, we obtain⊢t : T ∈

J⊢x

t : TK. □

Corollary 4.6 (Relative consistency). Assuming ITT is

con-sistent, there is no termt such that ⊢xt : Π(A : □0). A.

Proof. Assume such at exists. By the Corollary 4.5, because

⊢Π(A : □0). A, there exists t such that ⊢ t : Π(A : □0). A

which contradicts the assumed consistency of IT T. □

4.3 Optimisations

Up until now, we remained silent about one thing: the size of the translated terms. Indeed, the translated term is a deco-ration of the initial one by transports which appear in many locations. For example, at each application we use a transport by lemma 4.2 to ensure that the term in function position is given a function type. In most cases—in particular when translating IT T terms—this produces unnecessary transports (often by reflexivity) that we wish to avoid.

In order to limit the size explosion, in the above we use a

different version of transport, namelytransport′such that

transport′A

1,A2(p, t ) = t

whenA1=α A2

= p∗t otherwise.

The idea is that we avoidtrivially unnecessary transports (we

do not deal withβ-conversion for instance). We extend this

technique to the different constructors of equality (symmetry, transitivity, . . . ) so that they reduce to reflexivity whenever possible. Take transitivity for instance:

transitivity′(refl u, q)= q

transitivity′(p, refl u) = p

transitivity′(p, q) = transitivity(p,q).

We show thesedefined terms enjoy the same typing rules

as their counterparts and use them instead. In practice it is enough to recover the exact same term when it is typed in IT T.

5

Formalisation with Template-Coq

We have formalised the translation in the setting of

Tem-plateCoq [Anand et al. 2018] in order to have a more precise proof, but also to evidence the fact that the translation is in-deed constructive and can be used to perform computations. TemplateCoq is a Coq library that has a representation of Coq terms as they are in Coq’s kernel (in particular using de Bruijn indices for variables) and a (partial) implementation of the type checking algorithm (not checking guardedness of fixpoints or positivity of inductive types). It comes with

aCoq plugin that permits to quote Coq terms into their

representations, and to produceCoq terms from their

rep-resentation (if they indeed denote well-typed terms). We have integrated our formalisation within that framework in order to ensure our presentations of ET T and IT T are close

toCoq, but also to take advantage of the quoting mechanism

(10)

881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 to produce terms using the interactive mode (in particular

we get to use tactics). Note that we also rely on Mangin

and Sozeau’sEquations [Sozeau 2010] plugin to derive nice

dependent induction principles.

Our formalisation takes full advantage of its easy

interfac-ing withTemplateCoq: we define two theories, namely ETT

and IT T, but IT T enjoys a lot of syntactic sugar by having things such as transport, heterogeneous equality and pack-ing as part of the syntax. The operations regardpack-ing these constructors—in particular the tedious ones—are written in Coq and then quoted to finally be realised in the translation

from IT T toTemplateCoq.

Interoperability with TemplateCoq. The translation we

define from IT T toTemplateCoq is not proven correct, but

it is not really important as it can just be seen as a feature to observe the produced terms in a nicer setting. In any case, TemplateCoq does not yet provide a complete formalisa-tion of CIC rules, as guard checking of recursive definiformalisa-tions and strict positivity of inductive type declarations are not formalised yet.

We also provide a translation fromTemplateCoq to ETT

that we will describe more extensively with the examples (5.4).

5.1 Quick Overview of the Formalisation

The file SAst.v contains the definition of the (common) ab-stract syntax of ET T and IT T in the form of an inductive

definition with de Bruijn indices for variables (like in

Tem-plateCoq). Sorts are defined separately in Sorts.v and we will address them later in Section 5.3.

Inductivesterm :Type:= |sRel (n: nat) |sSort (s :sort)

|sProd (nx :name) (A B:sterm) |sLambda(nx: name) (A B t :sterm) |sApp (u: sterm) (nx: name) (A B v :sterm) |sEq(A u v:sterm)

|sRefl (A u:sterm) |(* ... *) .

The files ITyping.v and XTyping.v define respectively the typing judgments for IT T and ET T, using mutual inductive types. Then, most of the files are focused on the meta-theory of IT T and can be ignored by readers who don’t need to see yet another proof of subject reduction.

The most interesting files are obviously those where the fundamental lemma and the translation are formalised: Fun-damentalLemma.v and Translation.v. For instance, here is the main theorem, as stated in our formalisation:

Theoremcomplete_translation Σ: type_glob Σ -> (forallΓ (h :XTyping.wf Σ Γ),PΓ', Σ|--i Γ'# J Γ K) * (forallΓ t A(h :Σ;;;Γ|-x t:A) Γ' (hΓ:Σ|--i Γ'#J Γ K), PA' t', Σ;;;;Γ'|--- [t'] :A' #J Γ|--- [t] :AK) * (forallΓ u v A(h:Σ;;;Γ |-x u= v: A) Γ' (hΓ:Σ|--i Γ'#J Γ K),

PA' A'' u' v' p', eqtrans Σ Γ A u v Γ' A' A'' u' v' p').

Hereintype_glob Σ refers to the fact that some global

con-text is well-typed, its purpose is detailed in Section 5.2. The

fact that the theorem holds inCoq ensures we can actually

compute a translated term and type out of a derivation in ET T.

5.2 Inductive Types and Recursion

In the proof of Section 4, we didn’t mention anything about inductive types, pattern-matching or recursion as it is a bit technical on paper. In the formalisation, we offer a way to still be able to use them, and we will even show how it works in practice with the examples (5.4).

The main guiding principle is that inductive types and in-duction are orthogonal to the translation, they should more or less be translated to themselves. To realise that easily, we just treat an inductive definition as a way to introduce new constants in the theory, one for the type, one for each constructor, one for its elimination principle, and one equal-ity per computation rule. For instance, the natural numbers can be represented by having the following constants in the context: nat :□0 0 :nat S :nat → nat natrec : ∀P, P 0 → (∀m, P m → P (S m)) → ∀n, P n natrec0:∀P Pz Ps, natrec P PzPs 0= Pz natrecS:∀P Pz Ps n, natrec P PzPs (Sn) = Ps n (natrec P PzPs n)

Here we rely on the reflection rule to obtain the

computa-tional behavior of the eliminatornatrec.

This means for instance that we do not consider inductive types that would only make sense in ET T, but we deem this not to be a restriction and to the best of our knowledge isn’t something that is usually considered in the literature. With that in mind, our translation features a global context of typed constants with the restriction that the types of those constants should be well-formed in IT T. Those constants are thus used as black boxes inside ET T.

With this we are able to recover what we were missing

fromCoq, without having to deal with the trouble of proving

that the translation doesn’t break the guard condition of fixed points, and we are instead relying on a more type-based approach.

5.3 About Universes and Homotopy

The experienced reader might have noticed that our treat-ment of universes (except perhaps for the absence of cumu-lativity) was really superficial and the notion of sorts used is rather orthogonal to our main development. This is even more apparent in the formalisation. Indeed, we didn’t fix a specific universe hierarchy, but instead specify what

proper-ties it should have, in what is reminiscent to a (functional3)

PTS formulation.

3

Meaning the sort of a sort, and the sort of a product are functions, necessary to the uniqueness of types (2.2).

(11)

991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

ClassSorts.notion:= {

sort :Type;

succ :sort-> sort;

prod_sort :sort-> sort-> sort;

sum_sort :sort-> sort-> sort;

eq_sort: sort-> sort;

eq_dec : foralls z: sort, {s =z} + {s <> z} ;

succ_inj :forall s z, succ s= succ z-> s =z

}.

From the notion of sorts, we require functions to get the sort of a sort, the sort of a product from the sorts of its arguments, and (crucially) the sort of an identity type. We also require some measure of decidable equality and injectivity on those. This allows us to instantiate this by a lot of different no-tions including the one presented earlier in the paper or

even its extension with a universeProp of propositions (like

CIC [Bertot and Castéran 2004]). We present here two in-stances that have their own interest.

Type in Type. One of the instances we provide is one with

only one universeType, with the inconsistent typing rule

Type : Type. Although inconsistent, this allows us to

inter-face withTemplateCoq, without the—for the time being—

very time-consuming universe constraint checking.

Homotopy Type System and Two-Level Type Theory. An-other interesting application (or rather instance) of our for-malisation is a translation from Homotopy Type System (HTS) [Voevodsky 2013] to Two-Level Type Theory (2T T) [Al-tenkirch et al. 2016; Annenkov et al. 2017].

HTS and 2T T arise from the incompatibility between UIP— recall it is provable in ET T—and univalence. The idea is to have two distinct notions of equality in the theory, a strict one satisfying UIP, and a fibrant one corresponding to the homotopy type theory equality, possibly satisfying univalence. This actually induces a separation in the types

of the theory: some of them are calledfibrant and the fibrant

or homotopic equality can only be eliminated on those. HTS can be seen as an extension of 2T T with reflection on the strict equality just like ET T is an extension of IT T.

We can recover HTS and 2T T in our setting by taking

Fi andUi as respectively the fibrant and strict universes

of those theories (fori ∈ N), along with the following PTS

rules:

(Fi, Fi+1) ∈Ax (Ui, Ui+1) ∈Ax

(Fi, Fj, Fmax(i, j )) ∈R (Fi, Uj, Umax(i, j )) ∈R (Ui, Fj, Umax(i, j )) ∈R (Ui, Uj, Umax(i, j )) ∈R

and the fact that the sort of the (strict) identity type onA : s

is thestrictified version ofs, i.e., Ui fors = Ui ors = Fi. In

order to have the fibrant equality, one simply needs to do as in Section 5.2.

In short, the translation from HTS to 2T T is basically the same as the one from ET T to IT T we presented in this paper, and this fact is factorised through our formalisation.

5.4 ETT-flavoured Coq: Examples

In this section we demonstrate how our translation can bring

extensionality to the world ofCoq in action. The examples

can be found in plugin_demo.v.

First, a pedestrian approach. We would like to begin by showing how one can write an example step by step before we show how it can be used in practice. For this we use a self-contained example without any inductive types or recursion, illustrating a very simple case of reflection. The term we want to translate is the following:

λ A B e x. x : Π A B. A = B → A → B

This is, in some sense the identity, relying on the equality e : A = B to convert x : A to x : B. Of course, this definition

isn’t accepted inCoq because the conversion doesn’t hold

in IT T.

FailDefinition pseudoid(A B:Type) (e: A= B) (x: A) :B:= x.

However, we still want to be able to write itin some way, in

order to avoid manipulating de Bruijn indices directly. For

this, we use a little trick by first defining aCoq axiom to

represent an ill-typed term:

Axiomcandidate :forall A B(t: A), B.

candidate A B t is a candidate t of type A to inhabit type B. We complete this by adding a notation that is reminiscent

toAgda’s [Norell 2007] hole mechanism.

Notation"'{!' t '!}'":= (candidate_ _t).

We can now write the ET T function withinCoq.

Definitionpseudoid (A B:Type) (e: A= B) (x:A) :B:= {!x !}.

We can then quote the term and its type toTemplateCoq

thanks to theQuote Definitioncommand provided by the

plugin.

QuoteDefinition pseudoid_term:=

ltac:(lett:= evalcompute in pseudoidin exactt).

QuoteDefinition pseudoid_type:=

ltac:(letT:= typeof pseudoidin exactT).

The terms that we get are nowTemplateCoq terms,

repre-sentingCoq syntax. We need to put them in ETT, meaning

adding the annotations, and also removing thecandidate

axiom. This is the purpose of thefullquote function that

we provide in our formalisation.

Definitionpretm_pseudoid :=

Eval lazy infullquote (2^18)Σ[] pseudoid_term empty empty nomap.

Definitiontm_pseudoid:=

Eval lazy in matchpretm_pseudoidwith

| Success t=> t

| Error_ =>sRel0

end.

Definitionprety_pseudoid :=

Eval lazy infullquote (2^18)Σ[] pseudoid_type empty empty nomap.

Definitionty_pseudoid:=

Eval lazy in matchprety_pseudoidwith

| Success t=> t

| Error_ =>sRel 0

end.

tm_pseudoid and ty_pseudoid correspond respectively to

the ET T representation ofpseudoid and its type. We then

produce, using our home-brewed Ltac type-checking tactic,

(12)

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 the corresponding ET T typing derivation (notice the use of

reflection to typecheck).

Lemmatype_pseudoid :Σi;;;[] |-x tm_pseudoid: ty_pseudoid.

Proof.

unfold tm_pseudoid,ty_pseudoid.

ettcheck. cbn.

eapply reflection with(e:= sRel1).

ettcheck.

Defined.

We can then translate this derivation, obtain the translated

term and then convert it toTemplateCoq.

Definition itt_pseudoid:sterm:=

Eval lazy in let '(_ ;t; _) :=

type_translation type_pseudoid istrans_nil

in t.

Definition tc_pseudoid: tsl_result term:=

Eval lazy in

tsl_rec(2 ^ 18)Σ[] itt_pseudoid empty.

Once we have it, weunquote the term to obtain aCoq term

(notice that the only use of reflection has been replaced by a transport).

fun(A B: Type) (e :A =B) (x: A) =>transport e x

: forallA B: Type,A= B-> A ->B

Making a Plugin with TemplateCoq. All of this work

is pretty systematic. Fortunately for us,TemplateCoq also

features a monad to reifyCoq commands which we can use

toprogram the translation steps. As such we have written a

complete procedure, relying onCoq type checkers we wrote

for IT T and ET T, which can generate equality obligations. Thanks to this, the user doesn’t have to know about the details of implementation of the translation, and stay within

theCoq ecosystem.

For instance, our previous example now becomes:

Definition pseudoid(A B:Type) (e: A= B) (x: A) :B:= {! x!}.

Run TemplateProgram (Translate ε"pseudoid").

This produces aCoq termpseudoid' corresponding to the

translation. Notice how the user doesn’t even have to pro-vide any proof of equality or derivations of any sort. The derivation part is handled by our own typechecker while the

obligation part is solved automatically by theCoq obligation

mechanism.

About inductive types. As we promised, our translation is able to handle inductive types. For this consider the inductive type of vectors (or length-indexed lists) below, together with a simple definition (we will remain in IT T for simplicity).

Inductivevec A :nat-> Type:= |vnil :vec A0

|vcons :A ->forall n, vec A n->vec A (S n).

Argumentsvnil{_}.

Argumentsvcons {_} _ _ _.

Definition vv:= vcons1 _vnil.

This time, in order to apply the translation we need to extend

the translation context withnatandvec.

Run TemplateProgram (

Θ<- TranslateConstant ε "nat";; Θ<- TranslateConstant Θ "vec";;

Translate Θ "vv"

).

Here, ε is the empty translation context and the command TranslateConstant enriches it with the types the induc-tives and of their constructors. The translation context then also contains associative tables between our own

represen-tation of constants and those of Coq. Unsurprisingly, the

translatedCoq term is the same as the original term.

Reversal of vectors. Next, we tackle a motivating example: reversal on vectors. Indeed, if you want to implement this operation, the same way you would do it on lists, you end up having a conversion problem:

FailDefinition vrev{A n m} (v: vec A n) (acc:vec A m) :vec A (n+ m) :=

vec_rect A (funn _ =>forallm,vec A m-> vec A(n +m)) (funm acc=> acc)

(funa n_rv m acc=> rv_ (vcons a m acc))

n v m acc.

The recursive call returns a vector of lengthn + S m where

the context expects one of lengthS n + m. In ITT these

types are not convertible. This example is thus a perfect fit for ET T where we can use the fact that these two expressions always compute to the same thing when instantiated with concrete numbers.

Definitionvrev {A n m} (v: vec A n) (acc:vec A m) :vec A (n+ m) :=

vec_rect A (funn _ =>forallm,vec A m-> vec A(n +m)) (funm acc=> acc)

(funa n_rv m acc=> {!rv _ (vcons a m acc) !})

n v m acc. Run TemplateProgram ( Θ<- TranslateConstant ε "nat";; Θ<- TranslateConstant Θ"vec" ;; Θ<- TranslateConstant Θ"Nat.add" ;; Θ<- TranslateConstant Θ"vec_rect" ;; Translate Θ "vrev" ).

This generates four obligations that are all solved

automati-cally. One of them contains a proof ofS n + m = n + S m

while the remaining three correspond to the computation

rules of addition (as mentionned before,add is simply a

con-stant and does not compute in our representation, hence the need for equalities). The returned term is the following, with only one transport remaining (remember our interpretation map removes unnecessary transports).

fun(A :Type) (n m:nat) (v :vec A n) (acc:vec A m) =>

vec_rect A

(funn_ =>forall m,vec A m-> vec A(n+ m)) (funm acc=> acc)

(funa n0 v0 rv m0 acc0 =>

transport(vrev_obligation_3 A n m v acc a n0 v0 rv m0 acc0)

(rv(S m0) (vcons a m0 acc0)))n v m acc

:forall A n m, vec A n-> vec A m-> vec A(n +m)

5.5 Towards an Interfacing between Andromeda

and Coq

Andromeda [Bauer et al. 2016] is a proof assistant implement-ing ET T in a sense that is really close to our formalisation. Aside from a concise nucleus with a basic type theory, most things happen with the declaration of constants with given

(13)

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 types, including equalities to define the computational

be-haviour of eliminators for instance. This is essentially what we do in our formalisation. Furthermore, their theory relies

onType : Type, meaning, our modular handling of universes

can accommodate for this as well.

All in all, it should be possible in the near future to use our

translation to produceCoq terms out of Andromeda

devel-opments. Note that this would not suffer from the difficulties

in generating typing derivations sinceAndromeda does it

for you.

5.6 Composition with other Translations

This translation also enables the formalisation of translations that target ET T rather than IT T and still get mechanised proofs of (relative) consistency by composition with this ET T to IT T translation. This could also be used to implement plu-gins based on the composition of translations. In particular, supposing we have a theory which forms a subset of ET T and whose conversion is decidable. Using this translation, we could formalise it as an embedded domain-specific type theory and provide an automatic translation of well-typed

terms into witnesses inCoq. This would make it possible to

extend conversion with the theory of lists for example. This would provide a simple way to justify the consistency of CoqMT [Jouannaud and Strub 2017] for example, seeing it as an extensional type theory where reflection is restricted to equalities on a specific domain whose theory is decidable.

6

Limitations and Axioms

Currently, the representation of terms and derivations and the computational content of the proof only allow us to deal with the translation of relatively small terms but we hope to improve that in the future. As we have seen, the actual translation involves the computational content of lemmata of inversion, substitution, weakening and equational reasoning and thus cannot be presented as a simple recursive definition on derivations.

As we already mentioned, the axioms K and functional extensionality are both necessary in IT T if we want the trans-lation to be conservative as they are provable in ET T [Hof-mann 1995]. However, one might still be concerned about having axioms as they can for instance hinder canonicity of the system. In that respect, K isn’t really a restriction since it preserves canonicity. The best proof of that is probably Agda itself which natively features K—in fact, one needs to explicitly deactivate it with a flag if they wish to work without.

The case of functional extensionality is trickier. It is still possible to realise the axiom by composing our translation with a setoid interpretation [Altenkirch 1999] which vali-dates it, or by going into a system featuring it, for instance by implementing Observational Type Theory [Altenkirch

et al. 2007] likeEPIGRAM [McBride 2004].

7

Related Works and Conclusion

The seminal works on the precise connection between ET T and IT T go back to Streicher [1993] and Hofmann [1995, 1997]. In particular, the work of Hofmann provides a categor-ical answer to the question of consistency and conservativity of ET T over IT T with UIP and functional extensionality. Ten years later, Oury [2005, 2006] provided a translation from ET T to IT T with UIP and functional extensionality and other axioms (mainly due to technical difficulties). Although a first step towards a move from categorical semantics to a syntactic translation, his work does not stress any constructive aspect of the proof and shows that there merely exist translations in IT T to a typed term in ET T.

van Doorn et al. [2013] have later proposed and formalised a similar translation between a PTS with and without ex-plicit conversion. This does not entail anything about ET T to IT T but we can find similarities in that there is a wit-ness of conversion between any term and itself under an explicit conversion, which internalises irrelevance of explicit conversions. This morally corresponds to a Uniqueness of Conversions principle.

The Program [Sozeau 2007] extension ofCoq performs

a related coercion insertion algorithm, between objects in subsets on the same carrier or in different instances of the same inductive family, assuming a proof-irrelevance axiom. Inserting coercions locally is not as general as the present translation from ET T to IT T which can insert transports in any context.

In this paper we provide the first effective translation from ET T to IT T with UIP and functional extensionality. The

translation has been formalised inCoq using TemplateCoq,

a meta-programming plugin ofCoq. This translation is also

effective in the sense that we can produce in the end aCoq

term using theTemplateCoq denotation machinery. With

ongoing work to extend the translation to the inductive

fragment ofCoq, we are paving the way to an extensional

version of theCoq proof assistant which could be translated

back to its intensional version, allowing the user to navigate between the two modes, and in the end produce a proof term checkable in the intensional fragment.

References

Stuart F. Allen, Robert L. Constable, Richard Eaton, Christoph Kreitz, and Lori Lorigo. 2000. The Nuprl Open Logical Environment. InAutomated Deduction - CADE-17, 17th International Conference on Automated De-duction, Pittsburgh, PA, USA, June 17-20, 2000, Proceedings (Lecture Notes in Computer Science), David A. McAllester (Ed.), Vol. 1831. Springer, 170–176. https://doi.org/10.1007/10721959_12

T. Altenkirch. 1999. Extensional equality in intensional type theory. In Pro-ceedings. 14th Symposium on Logic in Computer Science (Cat. No. PR00158). 412–420. https://doi.org/10.1109/LICS.1999.782636

Thorsten Altenkirch, Paolo Capriotti, and Nicolai Kraus. 2016. Extending Homotopy Type Theory with Strict Equality.CoRR abs/1604.03799 (2016). arXiv:1604.03799 http://arxiv.org/abs/1604.03799

(14)

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430

Thorsten Altenkirch, Conor McBride, and Wouter Swierstra. 2007. Observa-tional equality, now!. InProceedings of the 2007 workshop on Programming languages meets program verification. ACM, 57–68.

Abhishek Anand, Simon Boulier, Cyril Cohen, Matthieu Sozeau, and Nico-las Tabareau. 2018. Towards Certified Meta-Programming with Typed Template-Coq. InInteractive Theorem Proving - 9th International Con-ference, ITP 2018, Held as Part of the Federated Logic ConCon-ference, FloC 2018, Oxford, UK, July 9-12, 2018, Proceedings (Lecture Notes in Computer Science), Jeremy Avigad and Assia Mahboubi (Eds.), Vol. 10895. Springer, 20–39. https://doi.org/10.1007/978-3-319-94821-8_2

Danil Annenkov, Paolo Capriotti, and Nicolai Kraus. 2017. Two-Level Type Theory and Applications.CoRR abs/1705.03307 (2017). arXiv:1705.03307 http://arxiv.org/abs/1705.03307

Andrej Bauer, Gaëtan Gilbert, Philipp G. Haselwarter, Matija Pretnar, and Chris Stone. 2016. The ‘Andromeda’ prover. (2016). http://www. andromeda-prover.org/

Jean-philippe Bernardy, Patrik Jansson, and Ross Paterson. 2012. Proofs for free: Parametricity for dependent types.Journal of Functional Program-ming 22, 2 (2012), 107–152.

Yves Bertot and Pierre Castéran. 2004. Interactive Theorem Proving and Program Development. (2004).

Marc Bezem, Thierry Coquand, and Simon Huber. 2013. A Model of Type Theory in Cubical Sets. (December 2013). http://www.cse.chalmers.se/ ~coquand/mod1.pdf

Simon Boulier, Pierre-Marie Pédrot, and Nicolas Tabareau. 2017. The Next 700 Syntactical Models of Type Theory. InCertified Programs and Proofs – CPP 2017. 182–194.

The Coq development team. 2017.The Coq proof assistant reference manual. LogiCal Project. http://coq.inria.fr Version 8.7.

Martin Hofmann. 1995. Conservativity of equality reflection over inten-sional type theory. InInternational Workshop on Types for Proofs and Programs. Springer, 153–164.

Martin Hofmann. 1997. Extensional constructs in intensional type theory. Springer.

Martin Hofmann and Thomas Streicher. 1998. The Groupoid Interpretation of Type Theory. InTwenty-five years of constructive type theory (Venice, 1995). Oxford Logic Guides, Vol. 36. Oxford Univ. Press, New York, 83– 111. http://www.tcs.informatik.uni-muenchen.de/lehre/SS97/types-vl/ venedig.ps

Jean-Pierre Jouannaud and Pierre-Yves Strub. 2017. Coq without Type Casts: A Complete Proof of Coq Modulo Theory. InLPAR-21, 21st International Conference on Logic for Programming, Artificial Intelligence and Reasoning, Maun, Botswana, May 7-12, 2017 (EPiC Series in Computing), Thomas Eiter and David Sands (Eds.), Vol. 46. EasyChair, 474–489. http://www. easychair.org/publications/paper/340342

Chris Kapulkin and Peter LeFanu Lumsdaine. 2012. The simplicial model of univalent foundations.arXiv preprint arXiv:1211.2851 (2012).

Conor McBride. 2000. Dependently typed functional programs and their proofs. Ph.D. Dissertation. University of Edinburgh.

Conor McBride. 2004. Epigram: Practical programming with dependent types. InInternational School on Advanced Functional Programming. Springer, 130–170.

Ulf Norell. 2007.Towards a practical programming language based on depen-dent type theory. Vol. 32. Citeseer.

Nicolas Oury. 2005. Extensionality in the calculus of constructions. In Inter-national Conference on Theorem Proving in Higher Order Logics. Springer, 278–293.

Nicolas Oury. 2006.Egalité et filtrage avec types dépendants dans le calcul des constructions inductives. Ph.D. Dissertation. http://www.theses.fr/ 2006PA112136 Thèse de doctorat dirigée par Paulin-Mohring, Christine Informatique Paris 11 2006.

Matthieu Sozeau. 2007. Program-ing Finger Trees in Coq. InICFP’07. ACM Press, Freiburg, Germany, 13–24. https://doi.org/10.1145/1291151. 1291156

Matthieu Sozeau. 2010. Equations: A Dependent Pattern-Matching Compiler. InInteractive Theorem Proving, First International Conference, ITP 2010, Edinburgh, UK, July 11-14, 2010. Proceedings (Lecture Notes in Computer Science), Matt Kaufmann and Lawrence C. Paulson (Eds.), Vol. 6172. Springer, 419–434. https://doi.org/10.1007/978-3-642-14052-5_29 Thomas Streicher. 1993.Investigations into intensional type theory. The Univalent Foundations Program. 2013.Homotopy Type Theory: Univalent

Foundations of Mathematics. Institute for Advanced Study.

Floris van Doorn, Herman Geuvers, and Freek Wiedijk. 2013. Explicit convertibility proofs in pure type systems. InProceedings of the Eighth ACM SIGPLAN international workshop on Logical frameworks & meta-languages: theory & practice. ACM, 25–36.

Vladimir Voevodsky. 2013. A simple type system with two identity types. (2013). https://ncatlab.org/homotopytypetheory/files/HTS.pdf

Figure

Figure 3. Typing rules
Figure 4. Main conversion rules (omitting congruence rules)
Figure 5. Relation ⊏
Figure 6. Congruence rules

Références

Documents relatifs

In this paper, we establish a relation between these two approaches and show how proofs in Hoare logic can be interpreted in type theory, yielding a translation of imperative

At the time of starting this work, there were three available back-end provers with different capabilities: Zenon [BDD07], a tableau prover for first-order logic with equality

Since strong forms of extensionality lead to undecidable type-checking, a good trade-off is to extend intensional equality with a decidable first-order theory T , as done in CoqMT

We propose a formal definition of independence and give a proof-theoretic characterization of it by: (1) representing the inference rules of a given type theory and a closed

L’archive ouverte pluridisciplinaire HAL, est destinée au dépôt et à la diffusion de documents scientifiques de niveau recherche, publiés ou non, émanant des

(Received 6 April 2019; revised manuscript received 28 September 2019; published 11 November 2019) We show that nonlinear continuum elasticity can be effective in modeling plastic

L’archive ouverte pluridisciplinaire HAL, est destinée au dépôt et à la diffusion de documents scientifiques de niveau recherche, publiés ou non, émanant des