Failure is Not an Option An Exceptional Type Theory

. We deﬁne the exceptional translation , a syntactic translation of the Calculus of Inductive Constructions (CIC) into itself, that covers full dependent elimination. The new resulting type theory features call-by-name exceptions with decidable type-checking and canonicity, but at the price of inconsistency. Then, noticing parametricity amounts to Kreisel’s realizability in this setting, we provide an additional layer on top of the exceptional translation in order to tame exceptions and ensure that all exceptions used locally are caught, leading to the parametric exceptional translation which fully preserves consistency. This way, we can consistently extend the logical expressivity of CIC with independence of premises, 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.


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 to dependent 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 type theory, 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 the universe.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 the particular case of the error monad, and show that its weaning translation can be simplified and tweaked so that full dependent elimination is valid.
This exceptional translation gives rise to a novel extension of CIC with new computational behaviours, namely call-by-name exceptions. 3That is, the type theory induced by the exceptional translation features new operations to raise and 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.Furthemore, 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 sound logical 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 [5] inside type theory.This parametric exceptional translation ensures that all exceptions used locally are caught, thus ensuring consistency of the resulting theory.We exploit this computational extension of CIC to show various logical results over CIC. Contributions.
-We describe the exceptional translation, the first monadic translation for the error 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 and more generally, that classical logic is conservative over the type-theoretic version of Π 0 2 formulae.-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 the independence of premises (which is known to be generally not valid in intuitionistic logic [6]) and use it to recover the recent result of Coquand and Mannaa [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 the resulting new computational principles arising from it.In Section 3, we present the parametric variant of the exceptional translation.Section 4 is devoted to the 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.

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 make various assumptions depending on the objects we want to translate.

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 Calculus of 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 of the dependent sum ΣA : i .(A + E) → A. In this paper, we take advantage of the 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 type i , representing types that can raise exceptions.This type comes with two constructors: TypeVal i which allows to construct a type i from a type and a default function on this type ; and another constructor TypeErr i that represents the default function at the level of type i .Furthermore, type i is equipped with an eliminator type_elim i and thus can be thought of as an inductive definition.For simplicity, we axiomatize it instead of requiring inductive types in the target of the translation.Definition 1.We assume that T features the data below, where i, j indices stand for universe polymorphism. - → ΠT : type i .P T subject to the following definitional equations: The Ω term describes what it means for a type to fail, i.e. it ascribes a meaning to sequents of the form Γ M : fail e.In practice, it is irrelevant and can 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 that recovers the underlying type from an inhabitant of type and Err that lifts the default function to this underlying type.
Definition 2. From the data of Definition 1, we derive the following terms.
The exceptional translation is defined in Figure 2. As usual for syntactic translations [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 Err i , 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 and on the dependent function space.
The soundness of the translation follows from the following properties, which are fundamental but straightforward to prove.
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 the typing derivation, the most important rule being i : j , which holds because [ i ] ≡ TypeVal type i TypeErr i has type type j which is convertible to [[ j ]] by Lemma 1.The last property is a direct application of typing soundness and unfolding of Lemma 1 for universes.
We call T E the theory arising from this interpretation, which is formally defined in a way similar to standard categorical constructions over dependent type theory.Terms and contexts of T E are simply terms and contexts of That is, the only way to extend T E is to add a new constant c of a given type A and to provide an inhabitant c E of the translated type [[A]].Then the translation is extended with [c] := c E .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 c E .Using T E , Theorem 1 can be rephrased in the following way.
Theorem 2. If T interprets CC ω then so does T E , that is, the exceptional translation is a syntactic model of CC ω .

Exceptional Inductive Types
The fact that the only effect we consider is raising exceptions does not really affect 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 at the same time providing a complete interpretation of CIC, that is, including full dependent elimination.
From now on, we assume that the target theory is a predicative restriction of CIC, i.e. that we can construct in it new inductive datatypes as we do in e.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 predicative fragment 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].Type and Constructor Translation As explained before, the intuitive interpretation of a type through the exceptional translation is a pair of a type and a default function from exceptions into that type.In particular, when translating some inductive type I, we must come up with a type 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 consists in translating constructors pointwise, while adding a new dedicated constructor standing for the exceptional case.We now turn to the formal construction.Definition 3. Let I be an inductive datatype with parameters p 1 : P 1 , . . ., p n : P n ; indices i 1 : I 1 , . . ., i m : I m ; constructors c 1 : Π(a 1,1 : A 1,1 ) . . .(a 1,l1 : A 1,l1 ).I p 1 . . .
We define the exceptional translation of I and its constructors in Figure 3, where I • is the inductive type defined by where in the recursive calls in the various A, we locally set  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.
Lemma 2. If I is given as in Definition 3, we have for any terms M , N This justifies a posteriori the simplified local definition we used in the recursive calls of the translation of the constructors.Theorem 3.For any inductive type I not using nested inductive types, the translation from Definition 3 is well-typed and satisfies the positivity criterion.
Proof.Preservation of typing is a consequence of Theorem 1.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 of every constructor c k is preserved, and furthermore by Lemma 1 the structure of every argument type is preserved by [[•]] as well.The only additional constructor I ∅ does not mention the recursive type and is thus automatically positive.
Corollary 1. Type soundness holds for the translation of inductive types and their constructors.

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 care has to be taken in order to accomodate for the fact that the return type of the pattern-matching depends on the scrutinee, in particular when it is the default constructor of the inductive type.
In what follows, we use the i 1 . . .i n notation for clarity, but compact it to i for space reason-when appropriate.Definition 4. Assume an inductive I as given in Definition 3. Let Q be the well-typed pattern-matching defined as then we pose [Q] to be the following pattern-matching.
. With notations and typing assumptions from Definition 4, we have Proof.Mostly a consequence of Theorem 1 applied to all of the premises of the pattern-matching rule.The only thing we have to check specifically is that the branch for the default constructor I ∅ is well-typed as which is also due to Theorem 1 applied to R. Lemma 4. 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 4. If T interprets CIC, then so does T E , and thus the exceptional translation is a syntactic model of CIC.

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.
Proof.The empty type is translated as 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 T E enjoys the following weaker canonicity lemma.
Lemma 5 (Exceptional Canonicity).Let I be an inductive type with constructors c 1 , . . ., c n and assume that T satisfies canonicity.The translation of any closed term T E M : I evaluates either to a constructor of the form c • i N 1 . . .N li or to the default constructor I ∅ e for some e : E.
Proof.Direct application of Theorem 1 and canonicity of T .
A direct consequence of Lemma 5 is that any proof of the empty type is an exception.As we will see in Section 4.1, for some types it is also possible to dynamically check whether a term of this type is a correct proof, in the sense that it does not raise an uncaught exception.This means that while T E is logically unsound, 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 5 a progress lemma.This is not the end of the story, though.Recall that T E only exists through its embedding [•] into T .In particular, if T is consistent, this means that one can reason about terms of T E directly in T .For instance, it is possible to prove in T that assuming some properties about its input, a function in T E 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 recover a consistent type theory as long as T is consistent itself.

Living in an Exceptional World
We describe here what T E feels like in direct style.The exceptional theory feature a new type E which reifies the underlying type E of exceptions in T E .It uses the fact that for E, the default function (here of type E → E) can simply be defined as the identity function.Its translation is given by Then, it is possible to define in T E a function raise : ΠA : .E → A that raises the provided exception at any type as 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 T E raise (Πx : A. B) e ≡ λx : A. raise B e which means that exceptions cannot be caught on Π-types.We can catch them on 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 T E a term catch bool : ΠP : bool → .P true → P false → (Πe : E. P (raise bool e)) → Πb : bool.P b defined by end satisfying the expected reduction rules on all three cases.In Section 6, we illustrate the use of the exceptional theory using the Coq plugin to define a simple cast framework as in [12].

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 T E .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 first present this translation on the negative fragment, then extend it to CIC and finally discuss its meta-theoretical properties.

Exceptional Parametricity in a Negative World
The exceptional parametricity translation for terms of CC ω is defined in Figure 5.
Intuitively, any type A in T E 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 of a term of the original theory used in the parametricity translation is replaced by its exceptional translation, using [•] or [[•]] depending on whether it is used as a term or as a type.For instance, the translation of an application Lemma 6 (Substitution lemma).The translation satisfies the following conversion: Theorem 6 (Soundness).The two following properties hold. - 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 to sequents, this model is slightly more subtle as sequents are translated to pairs of sequents instead.This is similar to the usual parametricity translation.
Once again, Theorem 6 can be rephrased in terms of preservation of theories and syntactic models.This construction preserves definitional η-expansion, as functions are mapped to (slightly more complicated) functions.
Lemma 7. If T satisfies definitional η-expansion, then so does T p E .
Proof.The first component of the translation preserves definitional η-expansion because functions are mapped to functions.It remains to show that 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 same kind of glueing construction.Nonetheless, this model is somewhat degenerated from 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, and the original sequent can be retrieved by the first projection.This is definitely not the case with the T p E theory, because the first projection is not the identity.In particular, because of Theorem 5, every sequent in the first projection is inhabited, although it is not the case in T itself if it is consistent.This means that parametricity can actually bring an additional expressivity power when it applies to a theory which is not pure, as it is the case here.

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 6.Let I be an inductive type as given in Definition 3. We define the exceptional parametricity translation I ε of I as the inductive type defined by: parameters [[p 1 : P 1 , . . ., p n : ). and we extend the translation as Example 2. We give the exceptional parametric inductive translation of our running 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 the additional realizability-like interpretation.
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 7. Let Q be the pattern-matching defined in Definition 4. We pose [Q] ε to be the pattern-matching (xε : where Q x is the following pattern-matching that is Q where the scrutinee has been turned into the index variable of the parametricity predicate.
Lemma 8.With notations and typing assumptions from Definition 4, we have 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 the same issue.In practice, standard recursors can be automatically translated, and fancy fixpoints may require hand-writing the parametricity proof.We do not describe 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 8.If T interprets CIC, then so does T p E , and thus the exceptional parametricity translation is a syntactic model of CIC.

E
Being built as a syntactic model, T p E inherits a lot of meta-theoretical properties of T .We list a few of interest below.Theorem 9.If T is consistent, then so is T p E .
Proof.Assume T p E M 0 : empty for some M 0 .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 10.If T enjoys canonicity, then so does T p E .
Proof.The exceptional parametricity translation for inductive types has the same structure as the original type, so any normal form in T p E can be mapped back to a normal form in T .

Effectively Extending CIC
The parametric exceptional translation allows to extend the logical expressivity of 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 already sketched this result in our previous paper [2], but we come back to it in more details.More generally, we show a form of conservativity of classical logic over the type-theoretic version of Π 0 2 formulae.In Section 4.2, we exhibit a syntactic model of CIC with 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.

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 to intuitionistic logic, which is precisely what T E does for CIC.Definition 8.A inductive type in CIC is said to be first-order if all the types of the arguments of its constructors, in its parameters and in its indices are recursively first-order.Example 3. 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 p 0 p 1 .Consequently, the CIC equivalent of Σ 0 1 formulae are in particular first-order.
First-order types enjoy uncommon properties, like the fact that they can be injected into effectful terms and purified away.This is then use to prove the generalized Markov's Rule.
Lemma 9.For every first-order type p : P Q : where all P are first-order, there are retractions ι P , ι Q and θ P , θ Q s.t.: 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 of equality of first-order type to fix the indices.
Theorem 11 (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 But empty • ∼ = E ≡ Q p, so we can derive from [M ] a term M s.t.
The proofterm we were looking for is thus no more than λp : P. M (ι P p) θ Q .

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 7, we know that the parametric exceptional translation preserves definitional η-expansion.It is thus sufficient to find two functions that are extensionality equal but intensionally distinct in the model.Let us consider to this end the unit → unit functions id ⊥ := λu : unit.u id := λu : unit.tt.
Theorem 12.The following sequents are derivable: 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 is derivable in T p E 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 to showing Π(p : , which we just showed was absurd.

Independence of Premise
Independence of premise (IP) is a semi-classical principle from first-order logic whose CIC equivalent can be stated as follows.
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, which contains 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 to receive 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.
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 T p E by relying internally on paraproofs, i.e. terms raising exceptions, while ensuring these exceptions never escape outside of the locally unsafe boundary.The resulting T p E 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 combinators that can be defined in T E .Definition 9. We define in T E the following terms.
fail : ΠA : .A It is worth insisting that these combinators are not necessarily parametric.While it can be shown that is Σ and is N actually are, fail is luckily not.The is Σ and is N functions are used in order to check that a value is actually pure and does not contain exceptions.Definition 10.We define ip in T E in direct style below, using the available combinators from Definition 9 and a bit of syntactic sugar.
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 the semantics 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 an inconsistent 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 a valid 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 that they need to be checked for exceptions as well.The only thing that remains to be proved is that ip also lives in 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.
Corollary 2. We have T p E IP.

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 a semantic 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 11.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 n P : N s.t.S Πm : N. P m = true → P n P = true.But then one can decide whether P holds for some n by just computing P n P , so that we effectively obtained an oracle deciding the halting problem (which is expressible in CIC).
Corollary 3. We have CIC p E MP and thus also CIC MP.
5 Possible extensions

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, introduced by this constructor and eliminated by pattern-matching.They do not (and in 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 the definitional ηlaw.
A, B, M, N :: In the remainder of this section, we wil focus on the specific case of pairs, but the same 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 as a 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-name language with effects, the two are sharply distincts.For instance, in presence of exceptions, assuming M : Σx : A. B, one does not have in general 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 matching on 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 actually possible to interpret negative pairs through the translation, but in a way that significantly differs from the translation of positive pairs.In this section, we assume that T contains negative pairs.Definition 11.The translation of negative pairs is given in Figure 8.
It is straightforward to check that the definitions of Figure 8 preserve the conversion and typing rules from Figure 7.The same translation can be extended to any record.We thus have the following theorem.Theorem 14.If T has negative records, then so has T E .
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 them clearly.Indeed, compare 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 two constructors.The trick is that the two types are externally distinguishible, but are not internally so, because T E is a model of CIC + & and thus proves that they 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 15.If T has negative records, then so has T p E .

Impredicative Universe
All the systems we have considered so far are predicative.It is nonetheless possible to implement an impredicative universe * in T E if T features one.We only allude to the construction here, a detailed account can be found in the Appendix.Intuitively, it is sufficient to ask for an inductive type prop living in i for all i, which is defined just as type, except that its constructor PropVal corresponding to TypeVal contains elements of * rather than .Then one can similarly define El * and Err * acting on prop rather than type.One then slightly tweaks the [[•]] macro from Figure 2 by defining it instead as and similarly for type constructors.With this modified translation, one obtains a soundness theorem for CC ω .
Theorem 16.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 17.The exceptional translation is a syntactic model of CIC + * without the singleton elimination rule.
Indeed, the addition of the default constructor disrupts the singleton elimination criterion for all inductive types.Actually, this criterion is very fragile, and even if T E satisfied it, Keller and Lasson showed that the parametricity translation could not interpret inductive types in * for similar reasons [17], and T p E would face the same issue.
6 The Exceptional Translation in Practice 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.

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 type class 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).
Using this cast mechanism, it is easy to definition a function list_to_pair from lists 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 a list of size two using a pure function.
In the exceptional layer, it is possible to prove the following property 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 : where underscores represent arguments inferred by Coq.

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 of terms) 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 effects 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 and overlapping 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 effect described in monadic style.On another line of work, Brady advocates for the use 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 which to 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 for by Boulier et al. [9].The presentation of parametricity for type theory given by Bernardy and Lasson [5] can be seen as one of the 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] have described 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 dependent sums using answer-type polymorphism Πα : .(A → α) → α.A generic class of internal translation has been defined by Jaber et al. [28] using forcing, which can be 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 the best of our knowledge, the exceptional translation is the first complete internal translation of CIC adding a particular notion of effect.

Conclusion and Future Work
In this paper, we have defined the exceptional translation, the first syntactic translation of the Calculus of Inductive Constructions into itself, adding effects and that covers full dependent elimination.This result in a new type theory, which features call-by-name exceptions with decidable type-checking and a weaker form of canonicity.We have shown that although the resulting theory is inconsistent, it is possible to reason on exceptional programs and show that some 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 translation in order to tame exceptions and preserves 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 direction of future work is to investigate whether other kind of 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 handlers of Kammar and Pretnar [29] suggests that we should be able to perform similar translations on CIC with full dependent elimination for other algebraic effects and handlers than exceptions.

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.We define the exceptional translation of R and its constructors in Figure 9, where R • is the record type defined by Remark that in particular, the default value is defined pointwise, e.g.
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 and reduction rules, as well as positivity conditions in the absence of nested record types This translation can be extended to the parametric case in a generic way.Once again, there is fundamental difference with the inductive case.Instead of enforcing parametricity through an index, the parametric exceptional translation of records has an additional parameter, and parametricity is ensured pointwise for every projection.We define this formally below.The parametricity translation can be shown to preserve typing and reduction.
Theorem 19.If T has negative records, then so has T p E .

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.
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 the case... 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 17.The translations from Definitions 3 and 4 can be carried out in presence of an impredicative universe in a similar way, by applying the changes from Definition 16 and changing TypeVal into PropVal for inductive types living in * .
For the same reason as their predicative counterpart, this translation preserves typing, but for a critical detail.
Theorem 21.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.

Definition 5 .
The theory T p E is defined by the following data.-Terms of T p E are pairs of terms of T .-Contexts of T p E are pairs of contexts of T .

Theorem 7 .
If T interprets CC ω then so does T p E .That is, the parametric exceptional translation is a syntactic model of CC ω .
6.1 Implementation as a Coq PluginThe (parametric) exceptional translation is a translation of CIC into itself, which means that we can directly implement it as a Coq plugin.This way, we can use the translation to extend safely Coq with new logical principles, so that typechecking 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 Figures4 and 6, are obtained by typing the following commands, which define each one new inductive type in Coq.