Failure is Not an Option
An Exceptional Type Theory
Pierre-Marie Pédrot1 and Nicolas Tabareau2
1

MPI-SWS, Germany
2
Inria, France

Abstract. We define the exceptional translation, a syntactic translationof the Calculus of Inductive Constructions (CIC) into itself, that covers
full dependent elimination. The new resulting type theory features callby-name exceptions with decidable type-checking and canonicity, but
at the price of inconsistency. Then, noticing parametricity amounts toKreisel’s realizability in this setting, we provide an additional layer on
top of the exceptional translation in order to tame exceptions and ensurethat all exceptions used locally are caught, leading to the parametric exceptional translation which fully preserves consistency. This way, we canconsistently extend the logical expressivity of CIC with independence ofpremises, Markov’s rule, and the negation of function extensionality while
retaining η-expansion. As a byproduct, we also show that Markov’s principle is not provable in CIC. Both translations have been implemented
in a Coq plugin, which we use to formalize the examples.

1

Introduction

Monadic translations constitute a canonical way to add effects to pure functional
languages [1]. Until recently, this technique was not available for type theories
such as CIC because of complex interactions with dependency. In a recent paper [2], we have presented a generic way to extend the monadic translation todependent types, using the weaning translation, as soon as the monad under consideration satisfies a crucial property: being self-algebraic. Indeed, in the same
way that the universe of types i is itself a type (of a higher universe) in typetheory, the type of algebras of a monad T
ΣA : i . T A → A
needs to be itself an algebra of the monad to allow a correct translation of theuniverse. However, in general, the weaning translation does not interpret all of
CIC because dependent elimination needs to be restricted to linear predicates,that is, those that are intuitively call-by-value [3]. In this paper, we study theparticular case of the error monad, and show that its weaning translation canbe simplified and tweaked so that full dependent elimination is valid.

This exceptional translation gives rise to a novel extension of CIC with newcomputational behaviours, namely call-by-name exceptions.3 That is, the typetheory induced by the exceptional translation features new operations to raiseand catch exceptions. This new logical expressivity comes at a cost, as the resulting theory is not consistent anymore, although still being computationally
relevant. This means that it is possible to prove a contradiction, but, thanks to a
weak form of canonicity, only because of an unhandled exception. Furthermore,
the translation allows us to reason directly in CIC on terms of the exceptional
theory, letting us prove, e.g., that assuming some properties on its input, an
exceptional function actually never raises an exception. We thus have a soundlogical framework to prove safety properties about impure dependently-typed
programs.
We then push this technique further by noticing that parametricity provides
a systematic way to describe that a term is not allowed to produce uncaught
exceptions, bridging the gap between Kreisel’s modified realizability [4] and parametricity inside type theory [5]. This parametric exceptional translation ensuresthat no exception reaches toplevel, thus ensuring consistency of the resulting
theory. Pure terms are automatically handled, while it is necessary to show
parametricity manually for terms internally using exceptions. We exploit thiscomputational extension of CIC to show various logical results over CIC.
Contributions.
– We describe the exceptional translation, the first monadic translation for theerror monad for CIC, including strong elimation of inductive types, resulting
in a sound logical framework to reason about impure dependently-typed
programs.
– We use parametricity to extend the exceptional translation, getting a consistent variant dubbed the parametric exceptional translation.
– We show that Markov’s rule is admissible in CIC.
– We show that definitional η-expansion together with the negation of function
extensionality is admissible in CIC.
– We show that there exists a syntactical model of CIC that validates theindependence of premises (which is known to be generally not valid in intuitionistic logic [6]) and use it to recover the recent result of Coquand andMannaa [7], i.e., that Markov’s principle is not provable in CIC.
– We provide a Coq plugin4 that implements both translations and with which
we have formalized all the examples.
Plan of the paper. In Section 2, we describe the exceptional translation and theresulting new computational principles arising from it. In Section 3, we present
the parametric variant of the exceptional translation. Section 4 is devoted to the3

The fact that the resulting exception are call-by-name is explained in detailed in [2]using a call-by-push-value decomposition. Intuitively, it comes from the fact that
CIC is naturally call-by-name.
4
The plugin is available at https://github.com/CoqHott/exceptional-tt.

2

A, B, M, N ::= i | x | M N | λx : A. M | Πx : A. B
Γ, ∆ ::= · | Γ, x : A`ΓΓ`M :Bi<jΓ ` i : j
Γ ` A : iΓ, x : A ` M : B

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

Γ ` A : iA≡BΓ`M :AΓ ` Πx : A. B : iΓ ` M : Πx : A. BΓ ` λx : A. M : Πx : A. B`·

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

` Γ, x : A

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

Fig. 1. Typing rules of CCω

various logical results resulting from the parametric exceptional translations. In
Section 5, we discuss possible extensions of the translation with negative records
and an impredicative universe. Section 6 describes the Coq plugin and illustrates
its use on a concrete example. We discuss related work in Section 7 and conclude
in Section 8.

2

The Exceptional Translation

We define in this section the exceptional translation as a syntactic translation
between type theories. We call the target theory T , upon which we will makevarious assumptions depending on the objects we want to translate.
2.1

Adding Exceptions to CCω

In this section, we describe the exceptional translation over a purely negative
theory, i.e., featuring only universes and dependent functions, called CCω , which
is presented in Figure 1. This theory is a predicative version of the Calculusof Constructions [8], with an infinite hierarchy of universes i instead of one
impredicative sort. We assume from now on that T contains at least CCω itself.
The exceptional translation is a simplification of the weaning translation [2]applied to the error monad. Owing to the fact that it is specifically tailored for
exceptions, this allows to give a more compact presentation of it.
Let E : 0 be a fixed type of exceptions in T . The weaning translation for
the error monad amounts to interpret types as algebras, i.e., as inhabitants of3

the dependent sum ΣA : i . (A + E) → A. In this paper, we take advantage ofthe fact that the algebra morphism restricted to A is always the identity. Thus
every type just comes with a way to interpret failure on this type, i.e. types
are intuitively interpreted as a pair of an A : i with a default (raise) function
A∅ : E → A. In practice, it is slightly more complicated as the universe of types
itself is a type, so its interpretation must comes with a default function. We
overcome this issue by assuming a term typei , representing types that can raise
exceptions. This type comes with two constructors: TypeVali which allows toconstruct a typei from a type and a default function on this type ; and anotherconstructor TypeErri that represents the default function at the level of typei .
Furthermore, typei is equipped with an eliminator type_elimi and thus can bethought of as an inductive definition. For simplicity, we axiomatize it instead ofrequiring inductive types in the target of the translation.
Definition 1. We assume that T features the data below, where i, j indices standfor universe polymorphism.
– Ωi : E →  i– ωi : Πe : E. Ωi e
– typei : j , where i < j– TypeVali : ΠA : i . (E → A) → typei– TypeErri : E → typei– type_elimi,j : ΠP : typei → j .
(Π(A : i ) (A∅ : E → A). P (TypeVali A A∅ )) →(Πe : E. P (TypeErri e)) → ΠT : typei . P T
subject to the following definitional equations:
type_elimi,j P pv p∅ (TypeVali A A∅ ) ≡ pv A A∅
type_elimi,j P pv p∅ (TypeErri e) ≡ p∅ e
The Ω term describes what it means for a type to fail, i.e. it ascribes ameaning to sequents of the form Γ ` M : fail e. In practice, it is irrelevant andcan be chosen to be degenerate, e.g. Ω := λ_ : E. unit.
In what follows, we often leave the universe indices implicit although they
can be retrieved at the cost of more explicit annotations.
Before defining the exceptional translation we need to derive a term El5 thatrecovers the underlying type from an inhabitant of type and Err that lifts thedefault function to this underlying type.
Definition 2. From the data of Definition 1, we derive the following terms.
Eli : typei → i:= λA : typei . type_elim (λT : typei . i )(λ(A0 : i ) (A∅ : E → A0 ). A0 ) Ω A
Erri : ΠA : typei . E → Eli A:= λ(A : typei ) (e : E). type_elim Eli(λ(A0 : i ) (A∅ : E → A0 ). A∅ e) ω A
5

The notation El refers to universes à la Tarski in Martin-Löf type theory.

4[i ]:= TypeVal typei TypeErri

[x]

:= x

[λx : A. M ] := λx : [[A]]. [M ][M N ]:= [M ] [N ][Πx : A. B] := TypeVal (Πx : [[A]]. [[B]]) (λ(e : E) (x : [[A]]). [B]∅ e)[A]∅:= Err [A][[A]]:= El [A]

[[·]]

:= ·

[[Γ, x : A]]:= [[Γ]], x : [[A]]Fig. 2. Exceptional Translation

The exceptional translation is defined in Figure 2. As usual for syntactictranslations [9], the term translation is given by [·] and the type translation,
written [[·]], is derived from it using the function El. There is an additional
macro [·]∅ , defined using Erri , which corresponds to the way to inhabit a given
type from an exception.
Note that we will often slightly abuse the translation and use the [·] and [[·]]
notation as macros acting on the target theory. This is merely for readability
purposes, and the corresponding uses are easily expanded to the actual term.
The following lemma makes explicit how [[·]] and [·]∅ behave on universes andon the dependent function space.
Lemma 3 (Unfoldings). The following definitional equations hold:– [[i ]] ≡ typei– [[Πx : A. B]] ≡ Πx : [[A]]. [[B]]– [i ]∅ e ≡ TypeErri e– [Πx : A. B]∅ e ≡ λx : [[A]]. [B]∅ e
Proof. By unfolding and straightforward reductions.
The soundness of the translation follows from the following properties, which
are fundamental but straightforward to prove.
Theorem 4 (Soundness). The following properties hold.
– [M {x := N }] ≡ [M ]{x := [N ]} (substitution lemma).
– If M ≡ N then [M ] ≡ [N ] (conversion lemma).
– If Γ ` M : A then [[Γ]] ` [M ] : [[A]] (typing soundness).
– If Γ ` A :  then [[Γ]] ` [A]∅ : E → [[A]] (exception soundness).
Proof. The first property is by routine induction on M , the second is direct
by induction on the conversion derivation. The third is by induction on the5typing derivation, the most important rule being i : j , which holds because[i ] ≡ TypeVal typei TypeErri has type typej which is convertible to [[j ]] by
Lemma 3. The last property is a direct application of typing soundness andunfolding of Lemma 3 for universes.
We call TE the theory arising from this interpretation, which is formallydefined in a way similar to standard categorical constructions over dependent
type theory. Terms and contexts of TE are simply terms and contexts of T . A
context Γ is valid is TE whenever its translation [[Γ]] is valid in T . Two terms
M and N are convertible in TE whenever their translations [M ] and [N ] areconvertible in T . Finally, Γ `TE M : A whenever [[Γ]] `T [M ] : [[A]].
That is, it is possible to extend TE with a new constant c of a given type A
by providing an inhabitant cE of the translated type [[A]]. Then the translation isextended with [c] := cE . The potential computational rules satisfied by this new
constant are directly given by the computational rules satisfied by its translation.
In some sense, the new constant c is just syntactic sugar for cE . Using TE ,Theorem 4 can be rephrased in the following way.
Theorem 5. If T interprets CCω then so does TE , that is, the exceptional translation is a syntactic model of CCω .

2.2

Exceptional Inductive Types

The fact that the only effect we consider is raising exceptions does not reallyaffect the negative fragment when compared to our previous work [2], but it
sure shines when it comes to interpreting inductive datatypes. Indeed, as explained in the introduction, the weaning translation only interprets a subset of
CIC, restricting dependent elimination to linear predicates. Furthermore, it also
requires a few syntactic properties of the underlying monad ensuring that positivity criteria are preserved through the translation, which can be sometimes
hard to obtain.
The exceptional translation diverges from the weaning translation precisely
on inductives types. It allows a more compact translation of the latter, while atthe same time providing a complete interpretation of CIC, that is, including fulldependent elimination.
From now on, we assume that the target theory is a predicative restrictionof CIC, i.e. that we can construct in it new inductive datatypes as we do ine.g. Coq [10], but without considering an impredicative universe. That is, all
the inductive types we consider in this section live in . As a matter of fact,
we slightly abuse the usual nomenclature and simply call CIC this predicativefragment in the remainder of the paper. We refrain from describing the generic
typing rules that extend CCω into CIC, as they are fairly standard and would
take up too much space. See for instance Werner’s thesis for a comprehensive
presentation [11].
6[I] := λ(p1 : [[P1 ]]) . . . (pn : [[Pn ]]) (i1 : [[I1 ]]) . . . (im : [[Im ]]).
TypeVal (I • p1 . . . pn i1 . . . im ) (I∅ p1 . . . pn i1 . . . im )•[c1 ] := c1...
[ck ] := c•kFig. 3. Inductive Type Translation

Type and Constructor Translation As explained before, the intuitive interpretation of a type through the exceptional translation is a pair of a type and adefault function from exceptions into that type. In particular, when translatingsome inductive type I, we must come up with a type [[I]] together with a default
function E → [[I]]. As soon as E is inhabited, that means that we need [[I]] to be
inhabited, preferably in a canonical way. The solution is simple: just as for types
where we freely added the exceptional case by means of the TypeErr constructor,
we freely add exceptions to every inductive type.
In practice, there is an elegant and simple way to do this. It just consistsin translating constructors pointwise, while adding a new dedicated constructor
standing for the exceptional case. We now turn to the formal construction.
Definition 6. Let I be an inductive datatype with– parameters p1 : P1 , . . . , pn : Pn ;– indices i1 : I1 , . . . , im : Im ;– constructors
c1 : Π(a1,1 : A1,1 ) . . . (a1,l1 : A1,l1 ). I p1 . . . pn V1,1 . . . V1,m...
ck : Π(ak,1 : Ak,1 ) . . . (ak,lk : Ak,lk ). I p1 . . . pn Vk,1 . . . Vk,m
We define the exceptional translation of I and its constructors in Figure 3,
where I • is the inductive type defined by– parameters p1 : [[P1 ]], . . . , pn : [[Pn ]];– indices i1 : [[I1 ]], . . . , im : [[Im ]];– constructors
c•1 : Π(a1,1 : [[A1,1 ]]) . . . (a1,l1 : [[A1,l1 ]]). I • p1 . . . pn [V1,1 ] . . . [V1,m ]...
c•k : Π(ak,1 : [[Ak,1 ]]) . . . (ak,lk : [[Ak,lk ]]). I • p1 . . . pn [Vk,1 ] . . . [Vk,m ]I∅ : Π(i1 : [[I1 ]]) . . . (im : [[Im ]]). E → I • p1 . . . pn i1 . . . im
where in the recursive calls in the various A, we locally set[[I M1 . . . Mn N1 . . . Nm ]] := I • [M1 ] . . . [Mn ] [N1 ] . . . [Nm ].
Example 7. We give a few representative examples of the inductive translationin Figure 4 in a Coq-like syntax. They were chosen because they are simple
instances of inductive types featuring parameters, indices and recursion in an
orthogonal way. For convenience, we write Σ A (λx : A. B) as Σx : A. B.
7

Ind bool :  :=| true : bool
| false : bool

Ind bool• :  :=| true• : bool•| false• : bool•| bool∅ : E → bool•

Ind list (A : ) :  :=| nil : list A| cons : A → list A → list A

Ind list• (A : [[]]) :  :=| nil• : list• A| cons• : [[A]] → list• A → list• A| list∅ : E → list• A

Ind Σ (A : ) (B : A → ) :  :=| ex : Π(x : A) (y : B x). Σ A B

Ind Σ• (A : [[]]) (B : [[A]] → ) :  :=| ex• : Π(x : [[A]]) (y : [[B x]]). Σ• A B| Σ∅ : E → Σ• A B

Ind eq (A : ) (x : A) : A →  :=| refl : eq A x x

Ind eq• (A : [[]]) (x : [[A]]) : [[A]] →  :=| refl• : eq• A x x| eq∅ : Πy : [[A]]. E → eq• A x y

Fig. 4. Examples of Translations of Inductive Types

Remark 8. The fact the we locally override the translation for recursive callson the [[·]] translation of the type being defined means that we cannot handle
cases where the translation of the type of a constructor actually contains an
instance of [I]. Because of the syntactic positivity criterion, the only possibility
for such a situation to occur in CIC is in the so-called nested inductive definitions.
However, nested inductive types are essentially a programming convenience, as
most nested types can be rewritten in an isomorphic way that is not nested.
~,N~
Lemma 9. If I is given as in Definition 6, we have for any terms M[[I M1 . . . Mn N1 . . . Nm ]] ≡ I • [M1 ] . . . [Mn ] [N1 ] . . . [Nm ].
This justifies a posteriori the simplified local definition we used in the recursive calls of the translation of the constructors.
Theorem 10. For any inductive type I not using nested inductive types, thetranslation from Definition 6 is well-typed and satisfies the positivity criterion.
Proof. Preservation of typing is a consequence of Theorem 4. The restriction
on nested types, which is slightly stronger than the usual positivity criterion of
CIC, is due to the fact that I∅ is not available in the recursive calls and thus
cannot be used to build a term of type type via the TypeVal constructor.
Preservation of the positivity criterion is straightforward, as the shape ofevery constructor ck is preserved, and furthermore by Lemma 3 the structure ofevery argument type is preserved by [[·]] as well. The only additional constructorI∅ does not mention the recursive type and is thus automatically positive.
Corollary 11. Type soundness holds for the translation of inductive types andtheir constructors.
8Pattern-Matching Translation We now turn to the translation of the elimination of inductive terms, that is, pattern matching. Once again, its definition
originates from the fact that we are working with call-by-name exceptions. It
is well-known that in call-by-name, pattern matching implements a delimited
form of call-by-value, by forcing its scrutinee before proceeding, at least up to
the head constructor. Therefore, as soon as the matched term (re-)raises an exception, the whole pattern-matching reraises the same exception. A little carehas to be taken in order to accomodate for the fact that the return type of thepattern-matching depends on the scrutinee, in particular when it is the default
constructor of the inductive type.
In what follows, we use the i1 . . . in notation for clarity, but compact it to ~ifor space reasons, when appropriate.
Definition 12. Assume an inductive I as given in Definition 6. Let Q be thewell-typed pattern-matching defined asmatch M return λ(i1 : I1 ) . . . (im : Im ) (x : I X1 . . . Xn i1 . . . im ). R with| c1 a1,1 . . . a1,l1 ⇒ N1...
| ck ak,1 . . . ak,lk ⇒ Nk
end

where

~ :P
~
Γ`X

~ : I{~~ p := X}~Γ`YΓ ` M : I X1 . . . Xn Y1 . . . Ym

~ p := X},~ x:I X~ ~i ` R : Γ,~i : I{~~ , x := M }Γ ` Q : R{~i := Y

~ 1 ` N1 : R{~i := V
~1 {~~ x := c1 X
~ ~a1 }
Γ, ~a1 : A
p := X},...
~ k ` Nk : R{~i := V~k {~~ x := ck X
~ ~ak }
Γ, ~ak : A
p := X},

then we pose [Q] to be the following pattern-matching.
match [M ] return λ(i1 : [[I1 ]]). . . (im : [[Im ]]) (x : I • [X1 ] . . . [Xn ] i1 . . . im ). [[R]] with| c•1 a1,1 . . . a1,l1 ⇒ [N1 ]...
| c•k ak,1 . . . ak,lk ⇒ [Nk ]| I∅ i1 . . . im e ⇒ [R]∅ {x := I∅ X1 . . . Xn i1 . . . im e} eendLemma 13. With notations and typing assumptions from Definition 12, we
have~ ], x := [M ]}.
[[Γ]] ` [Q] : [[R]]{~i := [YProof. Mostly a consequence of Theorem 4 applied to all of the premises of thepattern-matching rule. The only thing we have to check specifically is that thebranch for the default constructor I∅ is well-typed as~ p := X},~ e : E ` [R] {x := I∅ X
~ ~i e} e : [[R]]{x := I∅ X
~ ~i e}[[Γ]],~i : I{~∅
which is also due to Theorem 4 applied to R.
9Lemma 14. The translation preserves ι-rules.
Proof. Immediate, as the translation preserves the structure of the patterns.
The translation is also applicable to fixpoints, but for the sake of readability
we do not want to fully spell it out, although it is simply defined by congruence(commutation with the syntax). As such, it trivially preserves typing and reduction rules. Note that the Coq plugin presented in Section 6 features a complete
translation of inductive types, pattern-matching and fixpoints. So the interested
reader may experiment with the plugin to see how fixpoints are translated.
Therefore, by summarizing all of the previous properties, we have the following result.
Theorem 15. If T interprets CIC, then so does TE , and thus the exceptionaltranslation is a syntactic model of CIC.
2.3

Flirting with Inconsistency

It is now time to point at the elephant in the room. The exceptional translation
has a lot of nice properties, but it has one grave defect.
Theorem 16. If E is inhabited, then TE is logically inconsistent.
Proof. The empty type is translated as
Ind empty• :  := empty∅ : E → empty•
which is inhabited as soon as E is.
Note that when E is empty, the situation is hardly better, as the translation
is essentially the identity. However, when T satisfies canonicity, the situation is
not totally desperate as TE enjoys the following weaker canonicity lemma.
Lemma 17 (Exceptional Canonicity). Let I be an inductive type with constructors c1 , . . . , cn and assume that T satisfies canonicity. The translationof any closed term `TE M : I evaluates either to a constructor of the formc•i N1 . . . Nli or to the default constructor I∅ e for some e : E.
Proof. Direct application of Theorem 4 and canonicity of T .
A direct consequence of Lemma 17 is that any proof of the empty type isan exception. As we will see in Section 4.1, for some types it is also possible todynamically check whether a term of this type is a correct proof, in the sense thatit does not raise an uncaught exception. This means that while TE is logicallyunsound, it is computationally relevant and can still be used as a dependentlytyped programming language with exceptions, a shift into a realm where we would
have called the weaker canonicity Lemma 17 a progress lemma.
This is not the end of the story, though. Recall that TE only exists throughits embedding [·] into T . In particular, if T is consistent, this means that one
10

can reason about terms of TE directly in T . For instance, it is possible to provein T that assuming some properties about its input, a function in TE never raises
an exception. Hence not only do we have an effectul programming language, but
we also have a sound logical framework allowing to transparently prove safety
properties about impure programs.
It is actually even better than that. We will show in Section 3 that safety
properties can be derived automatically for pure programs, allowing to recovera consistent type theory as long as T is consistent itself.
2.4

Living in an Exceptional WorldWe describe here what TE feels like in direct style. The exceptional theory feature
a new type E which reifies the underlying type E of exceptions in TE . It uses thefact that for E, the default function (here of type E → E) can simply be definedas the identity function. Its translation is given by[E] : [[]] := TypeVal E (λe : E. e).
Then, it is possible to define in TE a function raise : ΠA : . E → A thatraises the provided exception at any type as[raise] := λ(A : type) (e : E). Err A e.
As we have already mentioned, the reader should be aware that the exceptions
arising from this translation are call-by-name. This means that they do not
behave like their usual call-by-value counterpart. In particular, we have in TE
raise (Πx : A. B) e ≡ λx : A. raise B ewhich means that exceptions cannot be caught on Π-types. We can catch themon universes and inductive types though, because in those cases they are freely
added through an extra constructor which one can pattern-match on. For instance, there exists in TE a term
catchbool : ΠP : bool → . P true → P false →(Πe : E. P (raise bool e)) → Πb : bool. P bdefined by[catchbool ] := λP pt pf pe b. match b return λb. El (P b) with| true• ⇒ pt
| false• ⇒ pf
| bool∅ e ⇒ pe eendsatisfying the expected reduction rules on all three cases.
In Section 6, we illustrate the use of the exceptional theory using the Coqplugin to define a simple cast framework as in [12].
11[i ]ε:= λA : [[i ]]. [[A]] → i

[x]ε

:= xε

[λx : A. M ]ε := λ(x : [[A]]) (xε : [[A]]ε x). [M ]ε[M N ]ε:= [M ]ε [N ] [N ]ε[Πx : A. B]ε := λ(f : Πx : [[A]]. [[B]]). Π(x : [[A]]) (xε : [[A]]ε x). [[B]]ε (f x)[[A]]ε:= [A]ε[[·]]ε:= ·[[Γ, x : A]]ε:= [[Γ]]ε , x : [[A]], xε : [[A]]ε x

Fig. 5. Parametricity over Exceptional Translation

3

Kreisel Meets Martin-Löf

It is well-known that Reynolds’ parametricity [13] and Kreisel’s modified realizability [4] are two instances of the broader logical relation techniques. Usually,parametricity is used to derive theorems for free, while realizability constrains
programs. In a surprising turn of events, we use Bernardy’s variant of parametricity on CIC [5] as a realizability trick to evict undesirable behaviours of
TE . This leads to the parametric exceptional translation, which can be seen as
the embodiement of Kreisel’s realizability in type theory. In this section, we firstpresent this translation on the negative fragment, then extend it to CIC andfinally discuss its meta-theoretical properties.
3.1

Exceptional Parametricity in a Negative WorldThe exceptional parametricity translation for terms of CCω is defined in Figure 5.
Intuitively, any type A in TE is turned into a validity predicate Aε : A →  which
encodes the fact that an inhabitant of A is not allowed to generate unhandled
exceptions. For instance, a function is valid if its application to a valid term
produces a valid answer. It does not say anything about the application to invalid
terms though, which amounts to a garbage in, garbage out policy. The translation
then states that every pure term is automatically valid.
This translation is exactly standard parametricity for type theory [5] but
parametrized by the exceptional translation. This means that any occurrence ofa term of the original theory used in the parametricity translation is replacedby its exceptional translation, using [·] or [[·]] depending on whether it is used asa term or as a type. For instance, the translation of an application [M N ]ε isgiven by [M ]ε [N ] [N ]ε instead of just [M ]ε N [N ]ε .
Lemma 18 (Substitution lemma). The translation satisfies the following
conversion: [M {x := N }]ε ≡ [M ]ε {x := [N ], xε := [N ]ε }.
Theorem 19 (Soundness). The two following properties hold.
12

– If M ≡ N then [M ]ε ≡ [N ]ε .
– If Γ ` M : A then [[Γ]]ε ` [M ]ε : [[A]]ε [M ].
Proof. By induction on the derivation.
We can use this result to construct another syntactic model of CCω . Contrarily to usual syntactic models where sequents are straightforwarldy translated tosequents, this model is slightly more subtle as sequents are translated to pairsof sequents instead. This is similar to the usual parametricity translation.
Definition 20. The theory TEp is defined by the following data.
– Terms of TEp are pairs of terms of T .
– Contexts of TEp are pairs of contexts of T .
– `TEp Γ whenever `T [[Γ]] and `T [[Γ]]ε .
– M ≡TEp N whenever [M ] ≡T [N ] and [M ]ε ≡T [N ]ε .
– Γ `TEp M : A whenever [[Γ]] `T [M ] : [[A]] and [[Γ]]ε `T [M ]ε : [[A]]ε [M ].
Once again, Theorem 19 can be rephrased in terms of preservation of theories
and syntactic models.
Theorem 21. If T interprets CCω then so does TEp . That is, the parametric
exceptional translation is a syntactic model of CCω .
This construction preserves definitional η-expansion, as functions are mappedto (slightly more complicated) functions.
Lemma 22. If T satisfies definitional η-expansion, then so does TEp .
Proof. The first component of the translation preserves definitional η-expansion
because functions are mapped to functions. It remains to show that
[λx : A. M x]ε := λ(x : [[A]]) (xε : [[A]]ε x). [M ]ε x xε ≡ [M ]εwhich holds by applying η-expansion twice.
It is interesting to remark that Bernardy-style unary parametricity also leads
to a syntactic model T p that interprets CCω (as well as CIC), using the samekind of glueing construction. Nonetheless, this model is somewhat degeneratefrom the logical point of view. Namely it is a conservative extension of the target
theory. Indeed, if Γ `T p M : A for some Γ, M and A from T , then there we also
have Γ `T M : A, because the first component of the model is the identity, andthe original sequent can be retrieved by the first projection.
This is definitely not the case with the TEp theory, because the first projection
is not the identity. In particular, because of Theorem 16, every sequent in the firstprojection is inhabited, although it is not the case in T itself if it is consistent.
This means that parametricity can actually bring additional expressivity when
it applies to a theory which is not pure, as it is the case here.
13

Ind boolε : bool• →  :=| trueε : boolε true•| falseε : boolε false•
Ind listε (A : type) (Aε : [[A]] → ) : list• A →  :=| nilε : listε A Aε (nil• A)| consε : Π(x : [[A]]) (xε : Aε x) (l : list• A) (lε : listε A Aε l).
listε A Aε (cons• A x l)
Ind eqε (A : type) (Aε : [[A]] → ) (x : [[A]]) (xε : Aε x) :Π(y : [[A]]) (yε : Aε y). eq• A x y →  :=| reflε : reflε A Aε x xε x xε (refl• A x)
Fig. 6. Examples of Parametric Translation of Inductive Types

3.2

Exceptional Parametric Translation of CIC

We now describe the parametricity translation of the positive fragment. The
intuition is that as it stands for an exception, the default constructor is always
invalid, while all other constructors are valid, assuming their arguments are.
Type and Constructor Translation
Definition 23. Let I be an inductive type as given in Definition 6. We define
the exceptional parametricity translation Iε of I as the inductive type defined by:– parameters [[p1 : P1 , . . . , pn : Pn ]]ε ;– indices [[i1 : I1 , . . . , im : Im ]]ε , x : I p1 . . . pn i1 . . . im ;– constructors~ 1 ]] .
c1ε : Π[[~a1 : AεIε p1 p1ε . . . pn pnε [V1,1 ] [V1,1 ]ε . . . [V1,m ] [V1,m ]ε (c•1 p~ ~a1 )...
~ k ]] .
ckε : Π[[~ak : AεIε p1 p1ε . . . pn pnε [Vk,1 ] [Vk,1 ]ε . . . [Vk,m ] [Vk,m ]ε (c•k p~ ~ak ).
and we extend the translation as[I]ε := Iε[c1 ]ε := c1ε...

[ck ]ε := ckε.

Example 24. We give the exceptional parametric inductive translation of ourrunning examples in Figure 6.
Note that contrarily to the negative case, the exceptional parametricity translation on inductive types is not the same thing as the composition of Bernardy’s
parametricity together with the exceptional translation. Indeed, the latter would
also have produced a constructor for the default case from the exceptional inductive translation, whereas our goal is precisely to rule this case out via theadditional realizability-like interpretation.
14

It is also very different from our previous parametric weaning translation [2],which relies on internal parametricity to recover dependent elimination, enforcing by construction that no effectful term exists. Here, effectful terms may be
used in the first component, but they are required after the fact to have no
inconsistent behaviour. Intuitively, parametric weaning produces one pure sequent, while exceptional parametricity produces two, with the first one being
potentially impure and the second one assuring the first one is harmless.
Pattern-matching Translation
Definition 25. Let Q be the pattern-matching defined in Definition 12. We pose[Q]ε to be the pattern-matching~ (x : I • [X1 ] . . . [Xn ] i1 . . . im ).
match [M ]ε return λ[[~i : I]]ε(xε : Iε [X1 ] [X1 ]ε . . . [Xn ] [Xn ]ε i1 i1ε . . . im imε x)[[R]]ε [Qx ]with| c1ε a1,1 a1,1ε . . . a1,l1 a1,l1 ε ⇒ [N1 ]ε...
| ckε ak,1 ak,1ε . . . ak,lk a1,lk ε ⇒ [Nk ]εendwhere Qx is the following pattern-matching
match x return λ(i1 : I1 ) . . . (im : Im ) (x : I X1 . . . Xn i1 . . . im ). R with| c1 a1,1 . . . a1,l1 ⇒ N1...
| ck ak,1 . . . ak,lk ⇒ Nk
end

that is Q where the scrutinee has been turned into the index variable of theparametricity predicate.
Lemma 26. With notations and typing assumptions from Definition 12, wehave~ , x := M }]] [Q].
[[Γ]]ε ` [Q]ε : [[R{~i := YεThe exceptional parametricity translation can be extended to handle fixpoints as well, with a few limitations. Translating generic fixpoints uniformly
is indeed an open problem in standard parametricity, and our variant faces thesame issue. In practice, standard recursors can be automatically translated, andfancy fixpoints may require hand-writing the parametricity proof. We do notdescribe the recursor translation here though, as it is essentially the same as
standard parametricity. Again, the interested reader may test the Coq plugin
exposed in Section 6 to see how recursors are translated.
Packing everything together allows to state the following result.
Theorem 27. If T interprets CIC, then so does TEp , and thus the exceptionalparametricity translation is a syntactic model of CIC.
15

3.3

Meta-Theoretical Properties of TEp

Being built as a syntactic model, TEp inherits a lot of meta-theoretical properties
of T . We list a few of interest below.
Theorem 28. If T is consistent, then so is TEp .
Proof. Assume `TEp M0 : empty for some M0 . Then by definition, there exists
two terms M and Mε such that `T M : empty• and `T Mε : emptyε M . But
emptyε has no constructor, and T is inconsistent.
More generally, the same argument holds for any inductive type.
Theorem 29. If T enjoys canonicity, then so does TEp .
Proof. The exceptional parametricity translation for inductive types has thesame structure as the original type, so any normal form in TEp can be mappedback to a normal form in T .

4Effectively Extending CIC

The parametric exceptional translation allows to extend the logical expressivityof CIC in the following ways, which we develop in the remainder of this section.
We show in Section 4.1 that Markov’s rule is admissible in CIC. We alreadysketched this result in our previous paper [2], but we come back to it in moredetails. More generally, we show a form of conservativity of double-negation
elimination over the type-theoretic version of Π02 formulae.
In Section 4.2, we exhibit a syntactic model of CIC which satisfies definitional
η-expansion for functions but which negates function extensionality. As far as
we know, this was not known.
Finally, in Section 4.3, we show that there exists a model of CIC which
validates the independence of premises. This is a new result, that shows that
CIC can feature traces of classical reasoning while staying computational. We
use this result in Section 4.4 to give an alternative proof of the recent result of
Coquand and Mannaa [7] that Markov’s principle is not provable in CIC.
4.1

Markov’s Rule

We show in this section that CIC is closed under a generalized Markov’s rule.
The technique used here is no more than a dependently-typed variant of Friedman’s trick [14]. Indeed, Friedman’s A-translation amounts to add exceptions tointuitionistic logic, which is precisely what TE does for CIC.
Definition 30. An inductive type in CIC is said to be first-order if all the typesof the arguments of its constructors, in its parameters and in its indices arerecursively first-order.
16

Example 31. The empty, unit and N types are first-order. If P and Q are firstorder then so is Σp : P. Q, P + Q and eq P p0 p1 . Consequently, the CIC
equivalent of Σ01 formulae are in particular first-order.
First-order types enjoy uncommon properties, like the fact that they can beinjected into effectful terms and purified away. This is then used to prove thegeneralized Markov’s Rule.
Lemma 32. For every first-order type p~ : P~ ` Q :  where all P~ are first-order,
there are retractions ιP~ , ιQ and θP~ , θQ s.t.:p~ : P~ ` ιQ : Q → [[Q]]{~p := ιP~ p~}
p~ : P~ ` θQ : [[Q]]{~p := ιP~ p~} → Q + E.
Proof. The ι terms exist because effectful inductive types are a semantical superset of their pure equivalent, and the θ terms are implemented by recursively
forcing the corresponding impure inductive term. One relies on decidability ofequality of first-order type to fix the indices.
Theorem 33 (Generalized Markov’s Rule). For any first-order type P and
first-order predicate Q over P , if `CIC Πp : P. ¬¬ (Q p) then `CIC Πp : P. Q p.
Proof. Let ` M : Πp : P. ¬¬ (Q p). By taking E := Q p and apply the soundness
theorem, one gets a proof
p : P ` [M ] : Πp̂ : [[P ]]. ([[Q p̂]] → empty• ) → empty• .
But empty• ∼= E ≡ Q p, so we can derive from [M ] a term M ] s.t.
p : P ` M ] : Πp̂ : [[P ]]. ([[Q p̂]] → Q p + Q p) → Q p.
The proofterm we were looking for is thus no more than λp : P. M ] (ιP p) θQ .
4.2

Function Intensionality with η-expansion

In a previous paper [9], we already showed that there existed a syntactic model of
CIC that allowed to internally disprove function extensionality. Yet, this model
was clearly not preserving definitional η-expansion on functions, as it was adding
additional structure to abstraction and application (namely a boolean). Thanks
to our new model, we can now demonstrate that counterintuitively, it is possible
to have a consistent type theory that enjoys definitional η-expansion while negating internally function extensionality. In this section we suppose that E := unit,
although any inhabited type of exceptions would work.
By Lemma 22, we know that the parametric exceptional translation preserves
definitional η-expansion. It is thus sufficient to find two functions that are extensionally equal but intensionally distinct in the model. Let us consider to thisend the unit → unit functions
id⊥ := λu : unit. u

id> := λu : unit. tt.
17

Theorem 34. The following sequents are derivable:`TEp Πu : unit. id⊥ u = id> u`TEp id⊥ = id> → empty.

Proof. The main difference between the two functions is that id⊥ preserves
exceptions while id> does not, which we exploit.
The first sequent is provable in CIC by dependent elimination and thus isderivable in TEp by applying the soundness theorem.
To prove the first component of the second sequent, we exhibit a property
that discriminates [id⊥ ] and [id> ], which is, as explained, their evaluation on
the term unit∅ tt. Showing then that this proof is parametric is equivalent toshowing Π(p : [[id⊥ = id> ]]) (pε : [[id⊥ = id> ]]ε p). empty. But pε actually implies [id⊥ ] = [id> ], which we just showed was absurd.
4.3

Independence of Premise

Independence of premise (IP) is a semi-classical principle from first-order logic
whose CIC equivalent can be stated as follows.
Π(A : ) (B : N → ). (¬A → Σn : N. B n) → Σn : N. ¬A → B n(IP)

Although not derivable in intuitionistic logic, it is an admissible rule of HA. The
standard proof of this property is to go through Kreisel’s modified realizability
interpretation of HA [4]. In a nutshell, the interpretation goes as follows: by
induction over a formula A, define a simple type τ (A) of realizers of A together
with a realizability predicate · A over τ (A). Then show that whenever `HA A,
there exists some simply-typed term t : τ (A) s.t. t A. As the interpretation
also implies that there is no t s.t. t ⊥, this gives a sound model of HA, whichcontains more than the latter. Most notably, there is for instance a term ip s.t.
ip(¬A → ∃n. B) → ∃n. ¬A → B

for any A, B. Intriguingly, the computational content of ip did not seem toreceive a fair treatment in the literature. To the best of our knowledge, it has
never been explicitly stated that IP was realizable because of the following “bug”
of Kreisel’s modified realizability.
Lemma 35 (Kreisel’s bug). For every formula A, τ (A) is inhabited. In particular, τ (⊥) := unit.
We show that this is actually not a bug, but a hidden feature of Kreisel’s
modified realizability, which secretly allows to encode exceptions in the realizers.
To this end, we implement IP in TEp by relying internally on paraproofs, i.e.
terms raising exceptions, while ensuring these exceptions never escape outsideof the locally unsafe boundary. The resulting TEp term has essentially the same
computational content as its Kreisel’s realizability counterpart. In this section
we suppose that E := unit, although assuming E to be inhabited is sufficient.
To ease the understanding of the definition, we rely on effectful combinatorsthat can be defined in TE .
18

Definition 36. We define in TE the following terms.
fail : ΠA : . A
[fail] := λA : [[]]. [A]∅ tt
isΣ : ΠA B. (Σx : A. B) → bool[isΣ ] := λA B p. match p with| ex• _ _ ⇒ true•| Σ∅ _ ⇒ false•end

isN : N → bool
[isN ] := fix isN n := match n with| O• ⇒ true•| S• n ⇒ isN n| N∅ _ ⇒ false•end

It is worth insisting that these combinators are not necessarily parametric.
While it can be shown that isΣ and isN actually are, fail is luckily not. The
isΣ and isN functions are used in order to check that a value is actually pureand does not contain exceptions.
Definition 37. We define ip in TE in direct style below, using the available
combinators from Definition 36 and a bit of syntactic sugar.
ip : IP
ip := λ(A : ) (B : N → ) (f : ¬A → Σn : N. B n).
let p := f (fail (¬A)) in
if isΣ N B p then match p with| ex n b ⇒ if isN n then ex _ _ n (λ_ : ¬A. b)else ex _ _ O (fail (¬A → B O))
end else ex _ _ O (fail (¬A → B O))

The intuition behind this term is the following. Given f : ¬A → Σn : N. B n,
we apply it to a dummy function which fails whenever it is used. Owing to thesemantics of negation, we know in the parametricity layer that the only way
for this application to return an exception is that f actually contained a proof
of A and applied fail to it. Therefore, given a true proof of ¬A, we are in aninconsistent setting and thus we are able to do whatever pleases us. The issue
is that we do not have access to such a proof yet, and we do have to provide
a valid integer now. Therefore, we check whether f actually provided us with avalid pair containing a valid integer. If so, this is our answer, otherwise we stuff
a dummy integer value and we postpone the contradiction.
This is essentially the same realizer as the one from Kreisel’s modified realizability, except that we have a fancy type system for realizers. In particular,
because we have dependent types, integers also exist in the logical layer, so thatthey need to be checked for exceptions as well. The only thing that remains tobe proved is that ip also lives in TEp .
Theorem 38. There is a proof of `T [[IP]]ε [ip].
Proof. The proof is straightforward but tedious, so we do not give the full details.
The file IPc.v of the companion Coq plugin contains an explicit proof. The
essential properties that make it go through are the following.
19

– `T Π(n : N• ) (p1 p2 : Nε n). p1 = p2
– `T Πn : N• . [isN ] n = true• ↔ Nε n– `T Π(p q : [[¬A]]). [[¬A]]ε p → [[¬A]]ε q
Corollary 39. We have `TEp IP.
4.4

Non-provability of Markov’s Principle

From this result, one can get a very easy syntactic proof of the independence
result of Markov’s principle from CIC. Markov’s principle is usually stated asΠP : N → bool. ¬¬ (Σn : N. P n = true) → Σn : N. P n = true (MP)An independence result was recently proved by Coquand and Mannaa by asemantic argument [7]. We leverage instead a property from realizability [15]that has been applied to type theory the other way around by Herbelin [16].
Lemma 40. If S is a computable theory containing CIC and enjoying canonicity, then one cannot have both `S IP and `S MP.
Proof. By applying IP to MP, one easily obtains that`S ΠP : N → bool. Σn : N. Πm : N. P m = true → P n = true.
Thus, for every closed P : N → bool, by canonicity there exists a closed nP : N
s.t. `S Πm : N. P m = true → P nP = true. But then one can decide whether
P holds for some n by just computing P nP , so that we effectively obtained anoracle deciding the halting problem (which is expressible in CIC).
Corollary 41. We have 6`CICpE MP and thus also 6`CIC MP.

5

Possible extensions

5.1

Negative Records

Interestingly, the fact that the translation introduces effects has unintented consequences on a few properties of type theory that are often taken for granted.
Namely, because type theory is pure, there is a widespread confusion amongst
type theorists between positive tuples and negative records.
– Positive tuples are defined as a one-constructor inductive type, introducedby this constructor and eliminated by pattern-matching. They do not (andin general cannot, for typing reasons) satisfy definitional η-laws, also known
as surjective pairing.
– Negative records are defined as a record type, introduced by primitive packing and eliminated by projections. They naturally obey definitional η-laws.
20

A, B, M, N ::= . . . | &x : A. B | hM, N i | M.π1 | M.π2
Γ ` A : iΓ, x : A ` B : j

Γ ` &x : A. B : max(i,j)Γ`M :AΓ ` M : &x : A. B

Γ ` M : &x : A. B

Γ ` M.π1 : AΓ ` M.π2 : B{x := M.π1 }Γ, x : A ` B : Γ ` N : B{x := M }Γ ` hM, N i : &x : A. B
hM.π1 , M.π2 i ≡ M

hM, N i.π1 ≡ M

hM, N i.π2 ≡ N

Fig. 7. Negative pairs[&x : A. B] := TypeVal (&x : [[A]]. [[B]]) (λe : E. h[A]∅ e, [B]∅ {x := [A]∅ e} ei)[hM, N i]:= h[M ], [N ]i[M.πi ]:= [M ].πiFig. 8. Exceptional Translation of Negative Pairs

In the remainder of this section, we will focus on the specific case of pairs, but thesame arguments are generalizable to arbitrary records. Positive pairs Σx : A. B
are defined by the inductive type from Figure 4. Negative pairs &x : A. B are
defined as a primitive structure in Figure 7. We use the ampersand notation asa reference to linear logic.
In CIC, it is possible to show that negative and positive pairs are propositionally isomorphic, because positive pairs enjoy dependent elimination. Nonetheless, it is a well-known fact in the programming folklore that in a call-by-namelanguage with effects, the two are sharply distinct. For instance, in presence of
exceptions, assuming ` M : Σx : A. B, one does not have in generalM ≡ ex A B (fst A B M ) (snd A B M )where fst and snd are defined by pattern-matching. Indeed, if M is itself an exception, the two sides can be discriminated by a pattern-matching. Matching on
the left-hand side results in immediate reraising of the exception, while matchingon the right-hand side succeeds as long as the arguments of the constructor are
not forced. Forcefully equating those two terms would then result in a trivial
equational theory.
Such a phenomenon is at work in the exceptional translation. It is actuallypossible to interpret negative pairs through the translation, but in a way thatsignificantly differs from the translation of positive pairs. In this section, we
assume that T contains negative pairs.
Definition 42. The translation of negative pairs is given in Figure 8.
21

It is straightforward to check that the definitions of Figure 8 preserve theconversion and typing rules from Figure 7. The same translation can be extended
to any record. We thus have the following theorem.
Theorem 43. If T has negative records, then so has TE .
It is enlightening to look at the difference between negative and positive pairs
through the translation, because now we have effects that allow to separate themclearly. Indeed, compare[[&x : A. B]] ≡ &x : [[A]]. [[B]]with [[Σx : A. B]] ∼= E + Σx : [[A]]. [[B]].

Clearly, if E is inhabited, then the two types do not even have the same cardinal, assuming A and B are finite. Furthermore, their default inhabitant is not
the same at all. It is defined pointwise for negative pairs, while it is a special
constructor for positive ones. Finally, there is obviously not any chance that[[Σx : A. B]] satisfies definitional surjective pairing in vanilla CIC, as it has twoconstructors. The trick is that the two types are externally distinguishible, but
are not internally so, because TE is a model of CIC + & and thus proves thatthey are propositionally isomorphic.
It is possible to equip negative pairs with a parametricity relation defined
as a primitive record which is the pointwise parametricity relation of each field,
which naturally preserve typing and conversion rules.
Theorem 44. If T has negative records, then so has TEp .
5.2

Impredicative Universe

All the systems we have considered so far are predicative. It is nonetheless possible to implement an impredicative universe ∗ in TE if T features one.
Intuitively, it is sufficient to ask for an inductive type prop living in ifor all i, which is defined just as type, except that its constructor PropVal
corresponding to TypeVal contains elements of ∗ rather than . Then one cansimilarly define El∗ and Err∗ acting on prop rather than type. One then slightly
tweaks the [[·]] macro from Figure 2 by defining it instead as(El∗ [A] if A : ∗[[A]] :=El [A] otherwise
and similarly for type constructors. With this modified translation, one obtains
a soundness theorem for CCω .
Theorem 45. The exceptional translation is a syntactic model of CCω + ∗.
Likewise, the inductive translation is amenable to interpret an impredicative
universe, with one major restriction though.
Theorem 46. The exceptional translation is a syntactic model of CIC + ∗ without the singleton elimination rule.
22

Indeed, the addition of the default constructor disrupts the singleton elimination criterion for all inductive types. Actually, this criterion is very fragile, andeven if TE satisfied it, Keller and Lasson showed that the parametricity translation could not interpret inductive types in ∗ for similar reasons [17], and TEp
would face the same issue.

6

The Exceptional Translation in Practice

6.1

Implementation as a Coq Plugin

The (parametric) exceptional translation is a translation of CIC into itself, whichmeans that we can directly implement it as a Coq plugin. This way, we canuse the translation to extend safely Coq with new logical principles, so thattypechecking remains decidable.
Such a Coq plugin is simply a program that, given a Coq proof term M ,produces the translations [M ] and [M ]ε as Coq terms. For instance, the translations of type list, given in Figures 4 and 6, are obtained by typing the following
commands, which define each one new inductive type in Coq.
Effect Translate list.
Parametricity Translate list.

The first command produces only [list], while the second produces [list]ε . But
the main interest of the translation is that we can exhibit new constructors. For
instance, the raise operation described in Section 2.4 is defined as
Effect Definition Exception : Type := fun E ⇒ TypeVal E E id.
Effect Definition raise : ∀ A, Exception → A := fun E (A : type E) ⇒ Err A.

6.2

Usecase: A Cast Framework

We can use the ability to raise exception to define partial function in the exceptional layer. For instance, given a decidable property (described by the typeclass below), it is then possible to define a cast function from A to Σ (a : A). P a
returning the converted value if the property is satisfied and raising an exception
otherwise (using an inhabitant cast_failed of Exception).
Class Decidable (A : Type) := dec : A + (not A).
Definition cast A (P : A → Type) (a:A) {Hdec : Decidable (P a)} : Σ (a : A). P a:= match dec (P a) with| inl p ⇒ (a ; p)
| inr _ ⇒ raise cast_failed
end.

Using this cast mechanism, it is easy to define a function list_to_pair fromlists to pairs by first converting the list into a list size two, using the impure function cast (list A) (fun l ⇒ List.length l = 2) and then recovering a pair from alist of size two using a pure function.
In the exceptional layer, it is possible to prove the following property
23

Definition list_to_pair_prop A (x y : A) : list_to_pair [x ; y] = (x,y).

in at least two way. One can perfectly prove it by simply raising an exception
at top level, or by reflexivity—using the fact that list_to_pair [x ; y] actually
reduces to (x,y).
However, there is a way to distinguish between those two proofs in the target
theory, here Coq, by stating the following lemma which can only proven for the
proof not raising an exception.
Definition list_to_pair_prop_soundness A x y :list_to_pair_prop• A x y = eq_refl• _ _ _ := eq_refl _.

where underscores represent arguments inferred by Coq.

7

Related Work

Adding dependency to an effectful language. There are numerous works on adding
dependent types in mainstream effectful programming languages. They all mostly
focused on how to appropriately restrict effectful terms from appearing in types.
Indeed, if types only depend on pure terms, the problem of having two different evaluations of the effect of the term (at the level of types and at the level ofterms) disappear. This is the case for instance for Dependent ML of Xi and Pfenning [18], or more recently for Casinghino et al. [19] on how to combine proofs
and programs when programs can be non-terminating. The F ? programming
language of Swamy et al. [20] uses a notion of primitive effects including state,
exceptions, divergence and IO. Each effect is described through a monadic predicate transformer semantics which allows to have a pure core dependent language
to reason on those effects. On a more foundational side, there are two recent andoverlapping lines of work on the description of a dependent call-by-push-value(CBPV) by Ahman et al. [21] and Vákár [22]. Those works also use a purity
restriction for dependency, but using the CBPV language, deals with any effectdescribed in monadic style. On another line of work, Brady advocates for theuse of algebraic effects as an elegant way to allow combing effects more smoothly
than with a monadic approach and gives an implementation in Idris [23].
Adding effects to a dependently-typed language. Nanevski et al. [24] have developed Hoare type theory (HTT) to extend Coq with monadic style effects. To
this end, they provide an axiomatic extension of Coq with a monad in whichto encapsulate imperative code. Important tools have been developed on HTT,
most notably the Ynot project [25]. Apart from being axiomatic, their monadic
approach does not allow to mix effectful programs and dependency but is rather
made for proving inside Coq properties on simply typed imperative programs.
Internal translation of type theory. A non-axiomatic way to extend type theory
with new features is to use internal translation, that is translation of type theory
into itself as advocated by Boulier et al. [9]. The presentation of parametricity
for type theory given by Bernardy and Lasson [5] can be seen as one of the24first internal translation of type theory. However, this one does not add any new
power to type theory as it is a conservative extension. Barthe et al. [26] havedescribed a CPS translation for CCω featuring call-cc, but without dealing
with inductive types and relying on a form of type stratification. A variant of
this translation has been extended recently by Bowman et al. [27] to dependentsums using answer-type polymorphism Πα : . (A → α) → α. A generic class ofinternal translations has been defined by Jaber et al. [28] using forcing, which canbe seen as a type theoretic version of the presheaf construction used in categorical
logic. This class of translation works on all CIC but for a restricted version of
dependent elimination, identical to the Baclofen type theory [2]. Therefore, to thebest of our knowledge, the exceptional translation is the first complete internaltranslation of CIC adding a particular notion of effect.

8

Conclusion and Future WorkIn this paper, we have defined the exceptional translation, the first syntactictranslation of the Calculus of Inductive Constructions into itself, adding effects
and that covers full dependent elimination. This results in a new type theory, which features call-by-name exceptions with decidable type-checking anda weaker form of canonicity. We have shown that although the resulting theory
is inconsistent, it is possible to reason on exceptional programs and show thatsome of them actually never raise an exception by relying on the target theory.
This provides a sound logical framework allowing to transparently prove safety
properties about impure dependently-typed programs. Then, using parametricity, we have given an additional layer at the top of the exceptional translationin order to tame exceptions and preserve consistency. This way, we have consistently extended the logical expressivity of CIC with independence of premises,
Markov’s rule, and the negation of function extensionality while retaining ηexpansion. Both translations have been implemented in a Coq plugin, which we
use to formalize the examples.
One of the main directions of future work is to investigate whether other kindof effects can give rise to an internal translation of CIC. To that end, it seems
promising to look at algebraic presentation of effects. Indeed, the recent work on
the non-necessity of the value restriction policy for algebraic effects and handlersof Kammar and Pretnar [29] suggests that we should be able to perform similartranslations on CIC with full dependent elimination for other algebraic effects
and handlers than exceptions.

Acknowledgements
This research was supported in part by an ERC Consolidator Grant for the
project “RustBelt”, funded under Horizon 2020 grant agreement № 683289 and
an ERC Starting Grant for the project “CoqHoTT”, funded under Horizon 2020
grant agreement № 637339.
25

References
1. Moggi, E.: Notions of computation and monads. Information and Computation
93(1) (July 1991) 55–92
2. Pédrot, P., Tabareau, N.: An effectful way to eliminate addiction to dependence.
In: 32nd Annual Symposium on Logic in Computer Science, LICS 2017, Reykjavik,
Iceland, June 20-23, 2017. (2017) 1–12
3. Munch-Maccagnoni, G.: Models of a Non-Associative Composition. In Muscholl,A., ed.: 17th International Conference on Foundations of Software Science andComputation Structures. Volume 8412., Grenoble, France, Springer (April 2014)
396–410
4. Kreisel, G.: Interpretation of analysis by means of constructive functionals of finite
types. In Heyting, A., ed.: Constructivity in Mathematics. Amsterdam: NorthHolland Pub. Co. (1959) 101–128
5. Bernardy, J.P., Lasson, M.: Realizability and parametricity in pure type systems.
In: Foundations of Software Science and Computational Structures. Volume 6604.,
Saarbrücken, Germany (March 2011) 108–122
6. Avigad, J., Feferman, S.: Gödel’s functional ("Dialectica") interpretation. In: The
Handbook of Proof Theory. North-Holland (1999) 337–405
7. Coquand, T., Mannaa, B.: The independence of Markov’s principle in type theory. In: 1st International Conference on Formal Structures for Computation and
Deduction, FSCD 2016, June 22-26, 2016, Porto, Portugal. (2016) 17:1–17:18
8. Coquand, T., Huet, G.P.: The Calculus of Constructions. Inf. Comput. 76(2/3)(1988) 95–120
9. Boulier, S., Pédrot, P., Tabareau, N.: The next 700 syntactical models of type theory. In: Proceedings of the 6th ACM SIGPLAN Conference on Certified Programs
and Proofs, CPP 2017, Paris, France, January 16-17, 2017. (2017) 182–194
10. The Coq Development Team: The Coq proof assistant reference manual (2017)
11. Werner, B.: Une Théorie des Constructions Inductives. PhD thesis, Université
Paris-Diderot - Paris VII (May 1994)
12. Tanter, É., Tabareau, N.: Gradual certified programming in Coq. In: Proceedingsof the 11th ACM Dynamic Languages Symposium (DLS 2015), Pittsburgh, PA,
USA, ACM Press (October 2015) 26–40
13. Reynolds, J.C.: Types, abstraction and parametric polymorphism. In: IFIP
Congress. (1983) 513–523
14. Friedman, H. In: Classically and intuitionistically provably recursive functions.
Springer Berlin Heidelberg, Berlin, Heidelberg (1978) 21–27
15. Troelstra, A., ed.: Metamathematical Investigation of Intuitionistic Arithmetic andAnalysis. Lecture Notes in Mathematics. Springer (1973)
16. Herbelin, H.: An intuitionistic logic that proves Markov’s principle. In: Proceedingsof the 25th Annual Symposium on Logic in Computer Science, LICS 2010, 11-14
July 2010, Edinburgh, United Kingdom. (2010) 50–56
17. Keller, C., Lasson, M.: Parametricity in an impredicative sort. In: Computer
Science Logic (CSL’12) - 26th International Workshop/21st Annual Conference of
the EACSL, CSL 2012, September 3-6, 2012, Fontainebleau, France. (2012) 381–
395
18. Xi, H., Pfenning, F.: Dependent types in practical programming. In: Proceedingsof the 26th ACM SIGPLAN-SIGACT Symposium on Principles of Programming
Languages. POPL ’99, New York, NY, USA, ACM (1999) 214–227

26

19. Casinghino, C., Sjöberg, V., Weirich, S.: Combining proofs and programs in adependently typed language. In: Proceedings of the 41st ACM SIGPLAN-SIGACT
Symposium on Principles of Programming Languages. POPL ’14, New York, NY,
USA, ACM (2014) 33–45
20. Swamy, N., Hriţcu, C., Keller, C., Rastogi, A., Delignat-Lavaud, A., Forest, S.,
Bhargavan, K., Fournet, C., Strub, P.Y., Kohlweiss, M., Zinzindohoue, J.K.,
Zanella-Béguelin, S.: Dependent types and multi-monadic effects in F*. In: 43nd
ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages(POPL), ACM (January 2016) 256–270
21. Ahman, D., Ghani, N., Plotkin, G.D.: Dependent types and fibred computational
effects. In: 19th International Conference on Foundations of Software Science andComputation Structures, Eindhoven, The Netherlands, Springer Berlin Heidelberg(2016) 36–54
22. Vákár, M.: A framework for dependent types and effects (2015) draft.
23. Brady, E.: Idris, a general-purpose dependently typed programming language:
Design and implementation. Journal of Functional Programming 23(05) (2013)
552–593
24. Nanevski, A., Morrisett, G., Birkedal, L.: Hoare type theory, polymorphism andseparation. Journal of Functional Programming 18(5-6) (2008) 865–911
25. Chlipala, A., Malecha, G., Morrisett, G., Shinnar, A., Wisnesky, R.: Effective
interactive proofs for higher-order imperative programs. In: Proceedings of the
14th ACM SIGPLAN International Conference on Functional Programming. ICFP
’09, New York, NY, USA, ACM (2009) 79–90
26. Barthe, G., Hatcliff, J., Sørensen, M.H.B.: CPS translations and applications: The
cube and beyond. Higher Order Symbol. Comput. 12(2) (September 1999) 125–170
27. Bowman, W., Cong, Y., Rioux, N., Ahmed, A.: Type-preserving CPS translationof σ and π types is not not possible. In: Proceedings of the 45st ACM SIGPLANSIGACT Symposium on Principles of Programming Languages. POPL ’18, New
York, NY, USA, ACM (2018)28. Jaber, G., Lewertowski, G., Pédrot, P., Sozeau, M., Tabareau, N.: The definitional
side of the forcing. In: Proceedings of the 31st Annual ACM/IEEE Symposium on
Logic in Computer Science, LICS ’16, New York, NY, USA, July 5-8, 2016. (2016)
367–376
29. Kammar, O., Pretnar, M.: No value restriction is needed for algebraic effects andhandlers. Journal of Functional Programming 27 (2017)

27

A

Appendix

A.1

Negative Records

In this section we describe the generic translation of negative records, that is,
structures defined by their projections and satisfying definitional η-laws.
[R]:= λ(p1 : [[P1 ]]) . . . (pn : [[Pn ]]).
TypeVal (R• p1 . . . pn )~ e} ei)(λe : E. h[A1 ]∅ e, . . . , [Ak ]∅ {f~ := [A]∅•:= [M ].f1

[M.f1 ]...
[M.fk ]:= [M ].fk•[hM1 , . . . , Mk iR ] := h[M1 ], . . . , [Mk ]iR•

Fig. 9. Record Type Translation

Definition 47. Let R be the record type defined by– parameters p1 : P1 , . . . , pn : Pn ;– fields f1 : A1 , . . . , fn : An ;– constructor h·iR : Π(f1 : A1 ) . . . (fn : An ). R p1 . . . pn .
We define the exceptional translation of R and its constructors in Figure 9, whereR• is the record type defined by– parameters p1 : [[P1 ]], . . . , pn : [[Pn ]];– fields f1• : [[A1 ]], . . . , fk• : [[Ak ]]{f~ := f~• };– constructor h·iR• : Π(f1 : [[A1 ]]) . . . (fk : [[Ak ]]). R• p1 . . . pn
where in the recursive calls in the various A, we locally set[[R M1 . . . Mn ]] := R• [M1 ] . . . [Mn ].
Example 48. We give examples of this translation in Figure 10 in a Coq-like
syntax. For completeness, we show that we can also handle co-inductive records.
Remark that in particular, the default value is defined pointwise, e.g.
[sig A B]∅ e ≡ ex• [A] [B] ([A]∅ e) ([B x]∅ {x := [A]∅ e} e)which is completely different from the default value one would have obtained in
the one-constructor inductive type case.
It is straightforward to check that this translation preserves both typing andreduction rules, as well as positivity conditions in the absence of nested record
types 6 .
6

CIC becomes fairly blurry at this point about the specification of positivity forrecords, as the only reference is the Coq implementation. There is no formal proof
that it is a sound system whatsoever, and in practice a few surprises arose like theinconsistency of definitional η-laws for co-inductive types.

28

Record sig (A : ) (B : A → ) := ex {fst : A;snd : B fst;}
Record sig• (A : [[]]) (B : [[A]] → [[]]) := ex• {fst• : El A;
snd• : El (B fst• );}CoInd stream (A : ) := Stream {hd : A;tl : stream A;}
CoInd stream• (A : [[]]) := Stream• {hd• : El A;
tl• : stream• A;}

Fig. 10. Examples of Record Type Translations

Theorem 49. If T has negative records, then so has TE .
This translation can be extended to the parametric case in a generic way.
Once again, there is fundamental difference with the inductive case. Instead ofenforcing parametricity through an index, the parametric exceptional translationof records has an additional parameter, and parametricity is ensured pointwisefor every projection. We define this formally below.
Definition 50. Let the record type R be defined as above. We define the exceptional parametricity record translation Rε as the record– parameters [[~p : P~ ]]ε , r : R p1 . . . pn ;– fields f1ε : [[A1 ]]ε r.f1• , . . . , fkε : [[Ak ]]ε {f~ := r.f~• } r.fk• ;– constructor h·iRε : Π(f1 : [[A1 ]]ε r.f1• ) . . . (fk : [[Ak ]]ε r.fk• ). Rε p1 . . . pn r
Example 51. We give examples of the parametric translation in Figure 11.
The parametricity translation can be shown to preserve typing and reduction.
Theorem 52. If T has negative records, then so has TEp .
A.2

Impredicative Universe

Although we only considered predicative universes up to now, it is actually possible to interpret an impredicative universe through the exceptional translation.
Rules for manipulation of an impredicative universe are given in Figure 12. We
assume in this section that T features an impredicative universe.
29

Record sigε A Aε B Bε (r : sigma• A B) := exε {fstε : Aε r.fst• ;sndε : Bε r.fst• fstε r.snd• ;}CoInd streamε (A : [[]]) (Aε : [[A]] → ) (r : stream• A) := Streamε {hdε : Aε r.hd• ;tlε : streamε A Aε r.tl• ;}Fig. 11. Examples of Record Type Translations

A, B, M, N ::= . . . | ∗Γ ` A : iΓ, x : A ` B : ∗Γ ` Πx : A. B : ∗`ΓΓ ` ∗ : iFig. 12. Impredicative universe

It is not very difficult to adapt the translation to account for such a sort. We
simply require that T features the following additional structure, which is theequivalent of type for an impredicative universe.
Definition 53 (Exceptional Prop). We assume that T features the data below, where the i index stands for universe polymorphism.
– prop : i
– PropVal : ΠA : ∗. (E → A) → propi– PropErr : E → prop
– prop_elimi : ΠP : prop → i .
(Π(A : ∗) (A∅ : E → A). P (PropVal A A∅ )) →(Πe : E. P (PropErr e)) → ΠT : prop. P T– Ω∗ : E → ∗– ω∗ : Πe : E. Ω∗ esubject to the following definitional equations:
prop_elimi P pv p∅ (PropVal A A∅ ) ≡ pv A A∅
prop_elimi P pv p∅ (PropErr e) ≡ p∅ e.
The prop type is just an inductive type (in !), but is once again positedinstead of declared as such for the sake of simplicity. Also, Ω∗ serves the verysame purpose for ∗ as Ωi does for i and can be taken to be degenerate in thesame way.
Just as for exceptional types, it is possible to derive the following functions.
30Definition 54. We derive the following definitions.
El∗ : prop → ∗:= λA : prop. prop_elim (λT : prop. ∗)(λ(A0 : ∗) (A∅ : E → A0 ). A0 ) Ω∗ A
Err∗ : ΠA : prop. E → El∗ A:= λ(A : prop) (e : E). type_elim El∗(λ(A0 : ∗) (A∅ : E → A0 ). A∅ e) ω∗ A
In order to properly extend the translation to interpret CCω + ∗, one would
technically need to annotate λ, Π and context variables with the sort they aresupposed to live in so that the translation operates at the level of pure terms
instead of derivations. While doable, this would be cumbersome, and as we only
want to show a proof of concept, we stick to an intuitive but slightly wrong
presentation here.
Definition 55 (Exceptional Translation of CCω + ∗). The translation isdefined as in Figure 2, except that we override the following macros.
[∗]:= TypeVal prop PropErr(
PropVal (Πx : [[A]]. [[B]]) (λ(e : E) (x : [[A]]). [B]∅ e) if B : ∗[Πx : A. B] :=TypeVal (Πx : [[A]]. [[B]]) (λ(e : E) (x : [[A]]). [B]∅ e) otherwise(
Err∗ [A] if A : ∗[A]∅:=
Err [A] otherwise(El∗ [A] if A : ∗[[A]]:=
El [A] otherwise
Note that this translation would not be able to interpret a cumulativity
relation of the form ∗ ⊆ i , as this would require prop ≤ typei which is not
the case if we implement them as distinct inductive types. Here, this does notmatter as we did not endow CCω with such a cumulativity relation.
Lemma 56. The following definitional equations hold:– [[∗]] ≡ prop
– [∗]∅ e ≡ PropErr e
Theorem 57. The exceptional translation is a syntactic model of CCω + ∗.
Proof. We only have to specifically check the following properties, the rest is avariation over Theorem 4.
– We have ` [∗] : [[]] by virtue of the typing rules for prop and PropErr.
– Furthermore, if Γ, x : A ` B : ∗, we have to show that [[Γ]] ` [Πx : A. B] : [[∗]].
This holds by impredicativivity, as [[Γ]], x : [[A]] ` [[B]] : ∗ by using theinduction hypothesis on B and the fact that El∗ : prop → ∗.
31

One could also hope that the exceptional translation is a model of CIC + ∗too. Although we will not give the full details, we argue here that is almost thecase... but not quite. The only thing we have to do is to provide a translation
for inductive types living in ∗ together with their pattern-matching.
Definition 58. The translations from Definitions 6 and 12 can be carried out inpresence of an impredicative universe in a similar way, by applying the changes
from Definition 55 and changing TypeVal into PropVal for inductive types livingin ∗.
For the same reason as their predicative counterpart, this translation preserves typing, but for a critical detail.
Theorem 59. The exceptional translation is a syntactic model of CIC + ∗ without the singleton elimination rule.
Proof. It is not hard to adapt the arguments to prove that typing is preserved
similarly. Failure of preservation of singleton elimination is discussed below.
Singleton elimination is a key feature of CIC + ∗ that acts as an escape hatch
to make information flow from ∗ to . This rule can be formalized as follows:
pattern-matching over a term in an inductive type in ∗ whose return clause is in is forbidden, except if the two following conditions are met:
1. The inductive type being matched over has at most one constructor.
2. If it has one, all its arguments live in ∗.
It is very simple to check why the exceptional translation breaks singleton
elimination. First, it adds a new default constructor, so that any type with one
constructor has now two. The only types that may preserve singleton elimination
have thus no constructors, i.e. they are empty. But the default constructor of
any inductive type takes as an argument a term of type E, which lives in .
Therefore, no translated inductive satisfy the singleton elimination criterion.
This means that, although the translation can interpret an impredicative
universe, there is no way to make any information flow from the latter to any
predicative universe. Somehow, the two worlds are completely disconnected.

32