Coq - Modeling and Proving in Computational Type Theory
Coq - Modeling and Proving in Computational Type Theory
Gert Smolka
Saarland University
Preface xi
I Basics 1
1 Getting Started 3
1.1 Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Notational Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 Structural Induction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5 Quantified Inductive Hypotheses . . . . . . . . . . . . . . . . . . . . . . . . 10
1.6 Preview of Proof Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.7 Procedural Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.8 Pairs and Polymorphic Functions . . . . . . . . . . . . . . . . . . . . . . . . 15
1.9 Argument Inference and Implicit Arguments . . . . . . . . . . . . . . . . . 17
1.10 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.11 Ackermann Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.12 Unfolding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.13 Concluding Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
iii
Contents
3 Propositions as Types 43
3.1 Implication and Universal Quantification . . . . . . . . . . . . . . . . . . . 44
3.2 Falsity and Negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.3 Conjunction and Disjunction . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.4 Propositional Equivalence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.5 Notational Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.6 Impredicative Characterizations . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.7 Proof Term Construction using Proof Tables . . . . . . . . . . . . . . . . . 52
3.8 Law of Excluded Middle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.9 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5 Inductive Eliminators 75
5.1 Generalized Subtyping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
5.2 Propositional Discrimination Restriction . . . . . . . . . . . . . . . . . . . 76
5.3 Universe Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
5.4 Boolean Eliminator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.5 Example: Boolean Case Analysis . . . . . . . . . . . . . . . . . . . . . . . . . 80
5.6 Kaminski’s Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
5.7 Eliminator for Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
5.8 A Formal Inductive Proof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
5.9 Equality of Numbers is Logically Decidable . . . . . . . . . . . . . . . . . . 85
5.10 Eliminator for Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.11 Disequality of Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
5.12 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6 Existential Quantification 91
6.1 Inductive Definition and Basic Facts . . . . . . . . . . . . . . . . . . . . . . 91
6.2 Barber Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
6.3 Lawvere’s Fixed Point Theorem . . . . . . . . . . . . . . . . . . . . . . . . . 95
iv
Contents
7 Arithmetic Pairing 97
7.1 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
7.2 Proofs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
7.3 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
v
Contents
14 EWOs 157
14.1 Linear Search Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
14.2 EWO for Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
14.3 General EWOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
14.4 EWO Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
14.5 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
16 Extensionality 177
16.1 Extensionality Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
16.2 Set Extensionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
16.3 Proof Irrelevance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
16.4 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
vi
Contents
18 Provability 191
18.1 Provability Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
18.2 Consistency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
21 Lists 217
21.1 Inductive Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
21.2 Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
21.3 Membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
21.4 Inclusion and Equivalence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
21.5 Nonrepeating Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
21.6 Lists of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
21.7 Position-Element Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
21.8 Constructive Discrimination Lemma . . . . . . . . . . . . . . . . . . . . . . 229
21.9 Element Removal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
21.10 Cardinality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
21.11 Setoid Rewriting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
vii
Contents
viii
Contents
ix
Contents
V Appendices 379
Bibliography 418
x
Preface
This text teaches topics in computational logic computer scientists should know
when discussing correctness of software and hardware. We acquaint the reader with
a foundational theory and a programming language for interactively constructing
computational models with machine-checked proofs. As common with program-
ming languages, we teach foundations, case studies, and practical programming in
an interleaved fashion.
The foundational theory we are using is a computational type theory extending
Martin-Löf type theory with inductive definitions and impredicative propositions.
All functions definable in the theory are computable. The proof rules of the theory
are intuitionistic while assuming the law of excluded middle is possible. As it will
become apparent through case studies in this text, computational type theory is
a congenial foundation for computational models and correctness arguments, im-
proving much on the set-theoretic language coming with mainstream mathematics.
We will use the Coq proof assistant, an implementation of the computational
type theory we are using. The interactive proof assistant assists the user with
the construction of theories and checks all definitions and proofs for correctness.
Learning computational logic with an interactive proof assistant makes a dramatic
difference to learning logic offline. The immediate feedback from the proof as-
sistant provides for rapid experimentation and effectively teaches the rules of the
underlying type theory. While the proof assistant enforces the rules of type theory,
it provides much automation as it comes to routine verifications.
We will use mathematical notation throughout this text and confine all Coq code
to Coq files accompanying the chapters. We assume a reader unfamiliar with type
theory and the case studies we consider. So there is a lot of material to be ex-
plained and understood at mathematical levels abstracting from the Coq program-
ming language. In any case, theories and proofs need informal explanations to be
appreciated by humans, and informal explanations are needed to understand for-
malisations in Coq.
The abstraction level coming with mathematical notation gives us freedom in ex-
plaining the type theory and helps with separating type-theoretic design principles
from engineering aspects coming with the Coq language. For instance, we will have
equational inductive function definitions at the mathematical level and realize them
with Coq’s primitives at the coding level. This way we get mathematically satisfying
function definitions and a fine explanation of Coq’s pattern matching construct.
xi
Preface
Acknowledgements
This text has been written for the course Introduction to Computational Logic I teach
every summer semester at Saarland University (since 2003). In 2010, we switched to
computational type theory and the proof assistant Coq. From 2010–2014 I taught
the course together with Chad E. Brown and we produced lecture notes discussing
Coq developments (in the style of Benjamin Pierce’s Software Foundations at the
time). It was great fun to explore with Chad intuitionistic reasoning and the propo-
sitions as types paradigm. It was then I learned about impredicative characteriza-
tions, Leibniz equality, and natural deduction. Expert advice on Coq often came
from Christian Doczkal.
By Summer 2017 I was dissatisfied with the programming-centered approach we
had followed so far and started writing lectures notes using mathematical language.
There were also plenty of exciting things about type theory I still had to learn. My
chief teaching assistants during this time were Yannick Forster (2017), Dominik
Kirst (2018-2020), and Andrej Dudenhefner (2021), all of them contributing exer-
cises and ideas to the text.
My thanks goes to the undergraduate and graduate students who took the course
and who worked on related topics with me, and to the persons who helped me teach
the course. You provided the challenge and motivation needed for the project. And
the human touch making it fun and worthwhile.
xii
Part I
Basics
1
1 Getting Started
We start with basic ideas from computational type theory and Coq. The main issues
we discuss are inductive types, structural recursion, and equational reasoning with
structural induction. We will see inductive types for booleans, natural numbers, and
pairs. Based on inductive types, we will define inductive functions using equations
and structural case analysis. This will involve functions that are cascaded, recursive,
higher-order (i.e., take functions as arguments), and polymorphic (i.e., take types
as leading arguments). Recursion will be limited to structural recursion so that
functional computation always terminates.
Our main interest is in proving equations involving recursive functions (e.g., com-
mutativity of addition, x + y = y + x). This will involve proof steps known as
simplification, rewriting, structural case analysis, and structural induction. Equal-
ity will appear in a general form called propositional equality, and in a specialized
form called computational equality. Computational equality is a prominent design
aspect of type theory that is important for mechanized proofs.
We will follow the equational paradigm and define functions with equations. We
will mostly define cascaded functions and use the accompanying notation known
from functional programming.
Type theory is a foundational theory starting from computational intuitions. Its
approach to mathematical foundations is very different from set theory. We may say
that type theory explains things computationally while set theory explains things
at a level of abstraction where computation is not an issue. When working with
computational type theory, set-theoretic explanations (e.g., of functions) are often
not helpful, so free your mind for a foundational restart.
1.1 Booleans
In Coq, even basic types like the type of booleans are defined as inductive types.
The type definition for the booleans
3
1 Getting Started
B: T
true : B
false : B
The constructors represent the type B and its two values true and false. Note that
the constructor B also has a type, which is the universe T (a special type whose
elements are types).
Inductive types provide for the definition of inductive functions, where a
defining equation is given for each value constructor. Our first example for an
inductive function is a boolean negation function:
!: B→B
! true := false
! false := true
There is a defining equation for each of the two value constructors of B. We say
that an inductive function is defined by discrimination on an inductive argument
(an argument that has an inductive type). There must be exactly one defining equa-
tion for every value constructor of the type of the inductive argument the func-
tion discriminates on. In the literature, discrimination is known as structural case
analysis.
The defining equations of an inductive function serve as computation rules. For
computation, the equations are applied as left-to-right rewrite rules. For instance,
we have
by rewriting with the first, the second, and again with the first defining equation of !.
Note that ! ! ! true is to be read as !(!(! true)), and that the first rewrite step replaces
the subterm ! true with false. Computation in Coq is logical and is used in proofs.
For instance, the equation
! ! ! true = ! true
follows by computation:
! ! ! true ! true
= ! ! false = false
= ! true
= false
4
1.2 Numbers
!!x = x
involving a boolean variable x takes more than computation since none of the defin-
ing equations applies. What is needed is discrimination (i.e., case analysis) on the
boolean variable x, which reduces the claim ! ! x = x to the equations ! ! true = true
and ! ! false = false, which both hold by computational equality.
Next we define inductive functions for boolean conjunction and boolean disjunc-
tion:
Both functions discriminate on their first argument. Alternatively, one could de-
fine the functions by discrimination on the second argument, resulting in different
computation rules. There is the general principle that computation rules must be
disjoint (at most one computation rule applies to a given term).
The left hand sides of defining equations are called patterns. Often, patterns
bind variables that can be used in the right hand side of the equation. The patterns
of the defining equations for & and | each bind the variable y.
Given the definitions of the basic boolean connectives, we can prove the usual
boolean indenties with discrimination and computational equality. For instance, the
distributivity law
x &y =y &x
1.2 Numbers
N ::= 0 | S(N)
5
1 Getting Started
N: T
0: N
S: N→N
The value constructors provide 0 and the successor function S. A number n can
be represented by the term that applies the constructor S n-times to the construc-
tor 0. For instance, the term S(S(S0)) represents the number 3. The constructor
representation of numbers dates back to the Dedekind-Peano axioms.
We will use the familiar notations 0, 1, 2, . . . for the terms 0, S0, S(S0), . . . .
Moreover, we will take the freedom to write terms like S(S(Sx)) without parentheses
as SSSx.
We define an inductive addition function discriminating on the first argument:
+:N→N→N
0 + y := y
Sx + y := S(x + y)
The second equation is recursive since it uses the function ’+’ being defined at the
right hand side.
Computational type theory does not admit partial functions. To fulfill this de-
sign principle, recursion must always terminate. To ensure termination, recursion is
restricted to inductive functions and must act on a single discriminating argument.
One speaks of structural recursion. Recursive applications must be on variables
introduced by the constructor of the pattern of the discriminating argument. In the
above definitions of ’+’, only the variable x in the second defining equation quali-
fies for recursion. Intuitively, structural recursion terminates since every recursion
step skips a constructor of the recursive argument. The condition for structural
recursion can be checked automatically by a proof assistant.
We define truncating subtraction for numbers:
− : N→N→N
0 − y := 0
Sx − 0 := Sx
Sx − Sy := x − y
6
1.3 Notational Conventions
· : N→N→N ˆ: N→N→N
0 · y := 0 x 0 := 1
Sx · y := y + x · y x Sn := x · x n
& :B→B→B
true & true := true
true & false := false
false & true := false
false & false := false
a) Prove that the symmetric function satisfies the defining equations for the stan-
dard boolean conjunction function (true & y = y and false & y = false).
b) Prove that the symmetric function agrees with the standard boolean conjunction
function.
c) Define a symmetric boolean disjunction function and show that it agrees with
the standard boolean disjunction function.
We are using notational conventions common in type theory and functional pro-
gramming. In particular, we omit parentheses in types and applications relying on
the following rules:
s→t→u s → (t → u)
stu (st)u
7
1 Getting Started
x+0=x induction x
1 0+0=0 computational equality
2 IH : x + 0 = x Sx + 0 = Sx simplification
S(x + 0) = Sx rewrite IH
Sx = Sx computational equality
For the arithmetic operations we assume the usual precedences, so multiplication ’·’
binds before addition ’+’ and subtraction ’−’, and all three of them are left associa-
tive. For instance:
x+0=x (1.1)
x + Sy = S(x + y) (1.2)
x+y =y +x (1.3)
(x + y) − y = x (1.4)
None of the equations can be shown with structural case analysis and computation
alone. In each case structural induction on numbers is needed. Structural induction
strengthens structural case analysis by providing an inductive hypothesis in the
successor case. Figure 1.1 shows a proof table for Equation 1.1. The induction rule
reduces the initial proof goal to two subgoals appearing in the lines numbered 1
and 2. The two subgoals are obtained by discrimination on x and by adding the
inductive hypothesis (IH) in the successor case. The inductive hypothesis makes it
possible to close the proof of the successor case by simplification and by rewriting
with the inductive hypothesis. A simplification step simplifies a claim by applying
defining equations from left to right. A rewriting step rewrites with an equation
that is either assumed or has been established as a lemma. In the example above,
rewriting takes place with the inductive hypothesis, an assumption introduced by
the induction rule.
We will explain later why structural induction is a valid proof principle. For now
we can say that inductive proofs are recursive proofs.
We remark that rewriting can apply an equation in either direction. The above
proof of Equation 1.1 can in fact be shortened by one line if the inductive hypothesis
is applied from right to left as first step in the second proof goal.
8
1.4 Structural Induction
x+y −y =x induction y
1 x+0−0=x rewrite Equation 1.1
x−0=x case analysis x
1.1 0−0=0 comp. eq.
1.2 Sx − 0 = Sx comp. eq.
2 IH : x + y − y = x x + Sy − Sy = x rewrite Equation 1.2
S(x + y) − Sy = x simplification
x+y −y =x IH
Note that Equations 1.1 and 1.2 are symmetric variants of the defining equations
of the addition function ’+’. Once these equations have been shown, they can be
used for rewriting in proofs.
Figure 1.2 shows a proof table giving an inductive proof of Equation 1.4. Note
that the proof of the base case involves a structural case analysis on x so that
the defining equations for subtraction apply. Also note that the proof rewrites
with Equation 1.1 and Equation 1.2, assuming that the equations have been proved
before. The successor case closes with an application of the inductive hypothesis
(i.e., the remaining claim agrees with the inductive hypothesis).
We remark that a structural case analysis in a proof (as in Figure 1.2) may also
be called a discrimination or a destructuring.
The proof of Equation 1.3 is similar to the proof of Equation 1.4 (induction on x
and rewriting with 1.1 and 1.2). We leave the proof as exercise.
One reason for showing inductive proofs as proof tables is that proof tables
explain how one construct proofs in interaction with Coq. With Coq one states the
initial proof goal and then enters commands called tactics performing the proof
actions given in the rightmost column of the proof tables. The induction tactic
displays the subgoals and automatically provides the inductive hypothesis. Except
for the initial claim, all the equations appearing in the proof tables are displayed
automatically by Coq, saving a lot of tedious writing. Replay all proof tables shown
in this chapter with Coq to understand what is going on.
A proof goal consists of a claim and a list of assumptions called context. The
proof rules for structural case analysis and structural induction reduce a proof goal
to several subgoals. A proof is complete once all subgoals have been closed.
A proof table comes with three columns listing assumptions, claims, and proof
actions.1 Subgoals are marked by hierarchical numbers and horizontal lines. Our
1
In this section, only inductive hypotheses appear as assumption. We will see more assumptions
once we prove claims with implication in Chapter 3.
9
1 Getting Started
proof tables may be called have-want digrams since they come with separate
columns for assumptions we have, claims we want to prove, and actions we per-
form to advance the proof.
Exercise 1.4.1 Give a proof table for Equation 1.2. Follow the layout of Figure 1.2.
Exercise 1.4.2 Prove that addition is commutative (1.3). Use equations (1.1)
and (1.2) as lemmas.
Exercise 1.4.3 Shorten the given proofs for Equations 1.1 and 1.4 by applying the
inductive hypothesis from right to left thus avoiding the simplification step.
Exercise 1.4.6 Prove that multiplication is commutative. You will need lemmas.
D: N→N→N
D 0 y := y
D (Sx) 0 := Sx
D (Sx)(Sy) := Dxy
10
1.5 Quantified Inductive Hypotheses
Figure 1.3: Proof table for a proof with a quantified inductive hypothesis
The defining equations discriminate on the first argument and in the successor
case also on the second argument. The recursion occurs in the third equation and
is structural in the first argument.
We now want to prove
Dxy = (x − y) + (y − x)
Dxy = (x − y) + (y − x)
If this was the inductive hypothesis, closing the proof is trivial. However, the actual
inductive hypothesis is
∀y. Dxy = (x − y) + (y − x)
11
1 Getting Started
+:N→N→N
0 + 0 := 0
0 + Sy := Sy
Sx + 0 := Sx
Sx + Sy := S(S(x + y))
We have seen a number of proofs using structural case analysis and structural in-
duction on numbers. Our presentation is informal and leaves important issues
unexplained, in particular as it comes to structural induction on numbers. Latter in
this text, we will derive formal proof rules for structural case analysis and induction
from first principle in type theory. We now give a preview of the formal proof rules
explaining their meaning informally.
The rule for structural case analysis on booleans takes the form
The rule says that we can prove a proposition px for all booleans x by proving it
for true and false. Type-theoretically, we see EB as a function that given proofs of
the propositions p true and p false yields a proof of the proposition px for every
boolean x.
12
1.7 Procedural Specifications
The rule for structural case analysis on numbers takes the form
The rule says that we can prove a proposition pn for all numbers n by giving proofs
for 0 and all successors Sn. Type-theoretically, we see MN as a function that given
proofs of the propositions p0 and ∀n. p(Sn) yields a proof of the proposition
∀[Link].
The rule for structural induction on numbers takes the form
It says that a proposition pn can be proved for all numbers n by giving proofs for
the zero case p0 and the successor case ∀n. pn → p(Sn). A proof of the successor
case ∀n. pn → p(Sn) is a function that given a number n and a proof of pn yields
a proof of the proposition p(Sn). In the context of the rule EN the proposition pn
acts as an assumption which is usually referred to as inductive hypothesis.
We may see the proof of the successor case of the induction rule as a method
that for every n upgrades a proof of pn to a proof of p(Sn). By iterating this
method n-times on a proof of p0
The rules we have given for defining inductive functions are very restrictive as it
comes to termination. There are many cases where a function can be specified
with a system of equations that are exhaustive, disjoint, and terminating. We then
speak of a procedural specification and its specifying equations. It turns out that
in practice using strict structural recursion one can construct inductive functions
satisfying procedural specifications relying on more permissive termination argu-
ments.
Our first example for a procedural specification specifies a function E : N → B
that checks whether a number is even:
E(0) = true
E(S0) = false
E(S(Sn)) = E(n)
13
1 Getting Started
The equations are exhaustive, disjoint, and terminating (two constructors are
skipped). However, the equations cannot serve as defining equations for an in-
ductive function since the recursion skips two constructors (rather that just one).
We can define an inductive function satisfying the specifying equations using the
defining equations
E(0) := true
E(Sn) := ! E(n)
(recall that ’!’ is boolean negation). The first and the second equation specifying E
hold by computational equality. The third specifying equation holds by simplifica-
tion and by rewriting with the lemma ! ! b = b.
Our second example specifies the Fibonacci function F : N → N with the equa-
tions
F0 = 0
F1 = 1
F (S(Sn)) = F n + F (Sn)
The equations do not qualify as defining equations for the same reasons we ex-
plained for E. It is however possible to define a Fibonacci function using strict
structural recursion. One possibility is to obtain F with a helper function F ′ taking
an extra boolean argument such that, informally, F ′ nb yields F (n + b) :
F′ : N → B → N
F ′ 0 false := 0
F ′ 0 true := 1
F ′ (Sn) false := F ′ n true
F ′ (Sn) true := F ′ n false + F ′ n true
F : N→N
F n := F ′ n false
That F satisfies the specifying equations for the Fibonacci function follows by com-
putational equality.
Note that F is defined with a single defining equation without a discrimination.
We speak of a plain function and a plain function definition. Since there is no
discrimination, the defining equation of a plain function can be applied as soon
14
1.8 Pairs and Polymorphic Functions
Exercise 1.7.2 Verify that F n := F ′ n false satisfies the specifying equations for the
Fibonacci function.
H0=0
H1=0
H(S(Sn)) = S(Hn)
using strict structural recursion. Hint: Use a helper function with an extra boolean
argument.
We have seen that booleans and numbers can be accommodated as inductive types.
We will now see that pairs (x, y) can also be accommodated with an inductive type
definition.
A pair (x, y) combines two values x and y into a single value such that the
components x and y can be recovered from the pair. Moreover, two pairs are equal
if and only if they have the same components. For instance, we have (3, 2 + 3) =
(1 + 2, 5) and (1, 2) ≠ (2, 1).
Pairs whose components are numbers can be accommodated with the inductive
definition
15
1 Getting Started
Pair : T
pair : N → N → Pair
A function swapping the components of a pair can be defined with a single equation:
swap (swap p) = p
for all pairs p (that is, for a variable p of type Pair). Note that discrimination for
pairs involves only a single case for the single value constructor for pairs.
Above we have defined pairs where both components are numbers. Given two
types X and Y , we can repeat the definition to obtain pairs whose first component
has type X and whose second component has type Y . We can do much better,
however, by defining pair types for all component types in one go:
Pair : T → T → T
pair : ∀X Y . X → Y → Pair X Y
The polymorphic value constructor pair comes with a polymorphic function type
saying that pair takes four arguments, where the first argument X and the second
argument Y fix the types of the third and the fourth argument. Put differently, the
types X and Y taken as first and second argument are the types for the components
of the pair constructed. We say that the first and second argument of the value
constructor pair are parametric and the third and fourth are proper.
We shall use the familiar notation X × Y for product types Pair X Y .
We can write partial applications of the value constructor pair :
pair N : ∀Y . N → Y → N × Y
pair N B : N → B → N × B
pair N B 0 : B → N × B
pair N B 0 true : N × B
16
1.9 Argument Inference and Implicit Arguments
We can also define a polymorphic swap function working for all pair types:
swap : ∀X Y . X × Y → Y × X
swap X Y (pair x y) := pair Y X y x
Note that the parametric arguments of pair are omitted in the pattern of the defin-
ing equation (i.e, the left hand side of the defining equation). The reason for the
omission is that the parametric arguments of pair don’t contribute relevant infor-
mation in the pattern of a defining equation.
pair : ∀X Y . X → Y → X × Y
we see that the first and second argument of pair provide the types of the third and
fourth argument. This means that the first and second argument can be derived
from the third and fourth argument. In Coq, we can write the underline symbol “_”
for arguments the proof assistant should derive from the context. For instance, we
may write pair _ _ 5 true for the term pair N B 5 true. More generally, we may write
pair _ _ s t
for the term pair u v s t if the types of the terms s and t can be determined as u
and v. We can now define the usual pair notation for applications of the constructor
pair:
(s, t) := pair _ _ s t
We remark that Coq will accept an input only if it can derive all terms specified with
the underline symbol.
Coq offers the possibility to declare arguments of functional constants as im-
plicit arguments. This has the effect, that the implicit arguments will always be
inserted as underlines. For instance, if we declare the type arguments of
swap : ∀X Y . X × Y → Y × X
17
1 Getting Started
We will routinely use implicit arguments for polymorphic constructors and func-
tions.
With implicit arguments, argument inference, and the notation for pairs we can
write the definition of swap as follows:
swap : ∀X Y . X × Y → Y × X
swap (x, y) := (y, x)
Note that it takes considerable effort to recover the usual mathematical nota-
tion for pairs in the typed setting of computational type theory. There were four
successive steps:
1. Polymorphic function types for functions taking types as arguments.
2. Argument inference requested with the underline symbol.
3. Notation for pairs and pair types.
4. Implicit arguments for polymorphic functions.
Finally, we define two functions providing the first and the second projection
for pairs:
π1 : ∀X Y . X × Y → X π2 : ∀X Y . X × Y → Y
π1 (x, y) := x π2 (x, y) := y
(π1 a, π2 a) = a
To prove the η-law, one discriminates on the variable a, which replaces it with a
general pair (x, y). This leaves us with the claim
Exercise 1.9.1 Write the definitions of the projections and the η-law and in full
detail not using the notations (x, y) and X × Y , underlines, or implicit arguments.
Give the types of all variables.
Exercise 1.9.2 Let a be a variable of type X ×Y . Write proof tables for the equations
swap (swap a) = a and (π1 a, π2 a) = a.
18
1.10 Iteration
1.10 Iteration
3 + x = S(S(Sx))
3 · x = x + (x + (x + 0))
x 3 = x · (x · (x · 1))
we see a common scheme we call iteration. In general, iteration takes the form f n x
where a step function f is applied n-times to an initial value x. With the notation
f n x the equations from above generalize as follows:
n + x = Sn x
n · x = (+x)n 0
x n = (· x)n 1
The partial applications (+x) and (· x) supply only the first argument to the func-
tions for addition and multiplication. They yield functions N → N, as suggested by
the cascaded function type N → N → N of addition and multiplication.
We formalize the notation f n x with a polymorphic function:
iter : ∀X. (X → X) → N → X → X
iter X f 0 x := x
iter X f (Sn) x := f (iter X f n x)
3 + x = iter S 3 x
3 · x = iter (+x) 3 0
x 3 = iter (· x) 3 1
now hold by computational equality. More generally, we can prove the following
equations by induction on n:
n + x = iter S n x
n · x = iter (+x) n 0
x n = iter (· x) n 1
Figure 1.4 gives a proof table for the equation for multiplication.
Exercise 1.10.1 Check that iter S 2 = λx. S(Sx) holds by computational equality.
19
1 Getting Started
add : N → N → N
add x y := iter S x y
add 0 y = y
add (Sx) y = S(add x y)
by computational equality.
Exercise 1.10.5 (Tail recursive iteration) Define a tail recursive version of iter and
verify that it agrees with iter.
Exercise 1.10.6 (Even) The term !n true tests whether a number n is even (’!’ is
boolean negation). Prove iter ! (n · 2) b = b and iter ! (S(n · 2)) b = ! b.
Exercise 1.10.8 (Fibonacci with iteration) Fibonacci numbers (§1.7) can be com-
puted by iteration on pairs. Find a function f such that F n := π1 (f n (0, 1)) satisfies
the specifying equations for the Fibonacci function:
F0 = 0
F1 = 1
F (S(Sn)) = F n + F (Sn)
Hint: If you formulate the step function with π1 and π2 , the third specifying equa-
tion should follow by computational equality, otherwise discrimination on a sub-
term obtained with iter may be needed.
20
1.11 Ackermann Function
A0y = Sy
A(Sx)0 = Ax1
A(Sx)(Sy) = Ax(A(Sx)y)
The equations cannot serve as a defining equations since the recursion is not struc-
tural. The problem is with the nested recursive application A(Sx)y in the third
equation.
However, we can define a structurally recursive function satisfying the given
equations. The trick is to use a higher-order helper function: 2
A:N→N→N A′ : (N → N) → N → N
A0 := S A′ h0 := h1
A(Sx) := A′ (Ax) A′ h(Sy) := h(A′ hy)
A(Sx)(Sy) Ax(A(Sx)y)
′
= A (Ax)(Sy) = Ax(A′ (Ax)y)
= Ax(A′ (Ax)y)
Note that the three specifying equations hold by computational equality (i.e., both
sides of the equations reduce to the same term). Thus verifying the equations with
a proof assistant is trivial.
We remark that the three equations specifying A are exhaustive and disjoint.
They are also terminating, which can be seen with a lexical argument: Either the
first argument is decreased, or the first argument stays unchanged and the second
argument is decreased.
21
1 Getting Started
Fib : (N → N) → N → N Ack : (N → N → N) → N → N → N
Fib f 0 := 0 Ack f 0 y := Sy
Fib f 1 := 1 Ack f (Sx) 0 := f x1
Fib f (SSn) := f n + f (Sn) Ack f (Sx)(Sy) := f x(f (Sx) y)
Figure 1.5: Unfolding functions for the Fibonacci and Ackermann functions
An := B n S
using a higher-order helper function B defined with iteration. Define B and verify
that A satisfies the specifying equations for the Ackermann function by computa-
tional equality. Consult Wikipedia to learn more about the Ackermann function.
f n = Fib f n
for the unfolding function Fib. Formally, this follows from the fact that the specify-
ing equations for the Fibonacci function are computationally equal to the respective
instances of the unfolding equation:
f 0 = Fib f 0
f 1 = Fib f 1
f (SSn) = Fib f (SSn)
22
1.13 Concluding Remarks
Exercise 1.12.1 Verify with the proof assistant that the realizations of the Fibonacci
function defined in §1.7 and Exercise 1.10.8 satisfy the unfolding equation for the
specifying unfolding function.
Exercise 1.12.2 Verify with the proof assistant that the realizations of the Acker-
mann function defined in §1.11 satisfies the unfolding equation for the specifying
unfolding function.
Exercise 1.12.3 Give unfolding functions for addition and truncating subtraction
and show that the unfolding equations are satisfied by the inductive functions we
defined for addition and subtraction.
Exercise 1.12.4 The unfolding function Fib is defined with a nested pattern SSn
in the third defining equation. Show how the nested pattern can be removed by
formulating the third equation with a helper function.
g: N→N→N→N
gab0 := a
gab(Sn) := gb(a + b)n
The underlying idea is to start with the first two Fibonacci numbers and then iterate
n-times to obtain the n-th Fibonacci number. For instance,
The equational language we have seen in this chapter is a sweet spot in the type-
theoretic landscape. With a minimum of luggage we can define interesting func-
tions, explore equational computation, and prove equational properties using struc-
tural induction. Higher-order functions and polymorphic functions are natural fea-
tures of this equational language. The power of the language comes from the fact
that functions and types can be passed as arguments and results of functions.
23
1 Getting Started
24
2 Basic Computational Type Theory
This chapter presents the basic fragment of computational type theory (CTT) in
a nutshell. The basic fragment covers the type and function definitions we have
seen in Chapter 1. The next two chapters will extend the basic fragment such that
propositions and proofs are covered.
We start with the grammar and the typing rules for terms obtained with ap-
plications and dependent function types. The typing rules yield a type checking
algorithm assigning unique types to well-typed terms.
We continue with inductive type definitions and inductive function definitions.
The defining equations of inductive functions provide for rewriting of terms, a form
of computation we call reduction. The essential properties of reduction are type
preservation, termination, unique normal forms, and canonicity. The properties
will be maintained by all further extensions of CTT. The properties crucially de-
pend on the assumption that only well-typed terms are admitted, a discipline proof
assistants will enforce.
The basic fragment also comes with plain definitions, lambda abstractions, let
expressions, and matches. Matches and plain definitions can be expressed with
inductive function definitions, and let expressions can be expressed with lambda
abstractions.
The proof assistant Coq we are targeting deviates from the architecture of CTT
in that it obtains inductive functions with recursive abstractions and native matches
rather than with defining equations. Thus we also consider recursive abstractions.
We start with a small fragment of CTT featuring simple function types and applica-
tions. The terms of the simply typed fragment are given by the grammar
s, t, u, v ::= c | T | s → t | st
The letter c ranges over constants. The term T is called universe and serves as the
type of all types. A function type s → t applies to functions taking arguments of
type s and returning results of type t. An application st describes the application
of the function described by the term s to the argument described by the term t.
25
2 Basic Computational Type Theory
The rule for T says that T has type T (an arrangement to be refined in § 5.3). The
rule for function types says that s → t has type T if the constituents s and t have
type T. The rule for applications st says that s can be applied to t if s has a function
type u → v and t has type u. In this case the application has type v.
A term s is well-typed and has type t if a typing ⊢ s : t can be derived with the
rules. Given unique types for the involved constants, well-typed terms have unique
types. For instance, we can derive the typings
⊢T→T : T
⊢ T → (T → T ) : T
An example for an ill-typed term (i.e., not well-typed term) is the application T T.
Given the typings of the constructors for numbers
⊢N:T
⊢0:N
⊢S:N→N
⊢N→N : T
⊢ N → (N → N) : T
⊢ S0 : N
⊢ S(S0) : N
26
2.2 Dependently Typed Terms
CTT treats types and functions like all other values. We say that types and
functions are first-class citizens in CTT.
Parentheses in right-nested function types may be omitted:
T→T→T T → (T → T )
N→N→N N → (N → N)
s→t→u s → (t → u)
stu (s t) u
s, t, u, v ::= c | x | T | ∀x : s. t | st
where c ranges over constants and x ranges over variables. The typing rules for
dependently typed terms are as follows:
Using the typing rules for dependent types, we can derive the typing
⊢ T→T→T : T
⊢ Pair : T → T → T
of the type constructor for pairs. We can now derive the typing
⊢ ∀X : T.∀Y : T. X → Y → Pair X Y : T
27
2 Basic Computational Type Theory
Given the typings of the constructors for numbers and pairs, we can derive the
following typings:
⊢ Pair N : T → T
⊢ pair N : ∀Y : T. N → Y → Pair N Y
⊢ pair N N : N → N → Pair N N
⊢ pair N N 0 (S0) : Pair N N
are well-typed given the typings for the constructors for numbers and pairs. In each
case determine the type of the term.
N ::= 0 | S(N)
Pair(X : T, Y : T) ::= pair(X, Y )
N: T
0: N
S: N→N
Pair : T → T → T
pair : ∀X T .∀Y T . X → Y → Pair X Y
28
2.4 Inductive Function Definitions
Note that the constructors S, Pair, and pair have types classifying them as functions.
From the types of 0 and S and the information that there are no other value con-
structors for N it is clear that the values of N are obtained as the terms 0, S0, S(S0),
S(S(S0)) and so forth. Analogously, given two types s and t, the values of the type
Pair st are described by terms pair stuv where u has type s and v has type t.
We say that the value constructor pair for pairs has two parametric and two
proper arguments. The parametric arguments are the leading arguments shared
with the type constructor Pair.
We take the opportunity to define two particular inductive types called unit and
void by listing their constructors:
1 : T
U : 1
0 : T
Note that 1 (unit) has exactly one value U, and that 0 (void) has no value at all.
29
2 Basic Computational Type Theory
add : N → N → N
add 0 y := y
add (Sx) y := S(add xy)
sub : N → N → N
sub 0 y := 0
sub (Sx) 0 := Sx
sub (Sx) (Sy) := sub xy
Fib : (N → N) → N → N
Fib f 0 := 0
Fib f (S0) := 1
Fib f (S(Sn)) := f n + f (Sn)
The defining equations of an inductive function must all take the same number
n ≥ 1 of arguments. We speak of the arity of an inductive function. In Figure 2.4,
add, sub, and Fib have arity 2 and swap has arity 4.
Every defining equation must be well-typed. Using the type declared for the func-
tion, every variable bound by the pattern of a defining equation receives a unique
type. Give the types for the bound variables, type checking of a defining equation
works as usual.
If an inductive function recurses, the recursion must be on the first discriminat-
ing argument and the variables introduced by the pattern for this argument. In the
examples in Figure 2.4, only the variable x in the defining equations for add and
sub qualifies for recursion. We refer to this severely restricted form of recursion as
structural recursion.
2.5 Reduction
30
2.5 Reduction
Things are arranged such that at most one defining equation applies to an applica-
tion (disjointness), and such that every application of an inductive function can be
reduced (exhaustiveness) provided all arguments required by the defining equations
are given such that the discriminating arguments start with value constructors. We
refer to the process of applying reductions rules as reduction. We see reduction as
computation and refer to reduction rules also as computation rules.
We consider reduction only for well-typed terms. Reduction has the important
property that it preserves well-typedness and the type of a term.
Things are arranged such that reduction always terminates. Without a restric-
tion on recursion, non-terminating inductive functions are possible. The structural
recursion requirement is a sufficient condition for termination that can be checked
algorithmically.
Since reduction always terminates, we can compute a normal form for every
term. There is no restriction on the application of reduction rules: Reduction rules
can be applied to any subterm of a term and in any order. Since the reduction
rules obtained from the defining equations do not overlap, terms nevertheless have
unique normal forms. We say that a term evaluates to its normal form and refer to
irreducible terms as normal terms.
Terms that are normal and closed (i.e., no unbound variables) are called canoni-
cal terms. Reduction preserves closedness of terms.
We now formulate the key properties of reduction:
• Termination Reduction always terminates.
• Unique normal forms Terms reduce to unique normal forms.
• Type preservation Reduction preserves types: If a term of type t is reduced,
the obtained term is again of type t.
• Canonicity Canonical terms of an inductive type start with a value constructor
of the type.
Canonicity gives an important integrity guarantee for inductive types saying that the
elements of an inductive type do not change when inductive functions returning
values of the type are added. Canonicity ensures that the canonical terms of an
inductive type are exactly the terms that can be obtained with the value constructors
of the type.
The definition format for inductive functions is carefully designed such that the
key properties are preserved when a definition is added. Exhaustiveness of the
defining equations is needed for canonicity, disjointness of the defining equations
is needed for uniqueness, and the structural recursion requirement ensures termi-
nation. Moreover, the type checking conditions for defining equations are needed
for type preservation.
Given an inductive type, we refer to the canonical terms of this type as the values
31
2 Basic Computational Type Theory
Exercise 2.5.1 Give all reduction chains that reduce the term
to its normal form. Note that there are chains of different length. Here is an example
for a reduction chain to normal form using s ≻ t as notation for a reduction step.
c:t
cx1 . . . xn := s
Chapter 1 contains several examples for plain definitions. Plain definitions can
be expressed as inductive function definitions discriminating on an extra argument
of type 1:
c:1→t
c U x1 . . . xn := s
Expressing plain definitions as inductive function definitions has the benefit that
type checking of plain definitions is explained as type checking of inductive func-
tions. Moreover, reduction of plain function applications is explained with reduc-
tion of inductive function applications. Most importantly, expressing plain defini-
tions as inductive function definitions has the advantage that the properties of the
type theory remain unchanged.
Exercise 2.6.2 Recall the definition of iter (§ 1.10). Explain the difference between
the following plain definitions:
A := iter S
Bxy := iter S x y
Note that the terms Axy and Bxy both reduce to the normal term iter S x y. More-
over, note that the terms A and Ax are reducible, while the terms B and Bx are not
reducible.
32
2.7 Lambda Abstractions
Exercise 2.6.3 Type checking is crucial for termination of reduction. Consider the
ill-typed plain function definition cx := xx and the ill-typed term cc, which reduces
to itself: cc ≻ cc. Convince yourself that there cannot be a type for c such that the
self application cc type checks.
λx : t. s
⊢u:T x:u ⊢ s : v
⊢ λx : u. s : ∀x u . v
⊢ λX T .λx X .x : ∀X T . X → X
(λx t .s) u ≻β s x
u
is called β-reduction and replaces an application (λx t .s) u with the term s x u ob-
tained from the term s by replacing every free occurence of the argument variable x
with the term u. Applications of the form (λx t .s) u are called β-redexes. Here is an
example featuring two consecutive β-reductions of an application of the polymor-
phic identity function:
33
2 Basic Computational Type Theory
λ[Link] λx.(st)
λxy.s λx.λy.s
To specify the type of an argument variable, we use either the notation x t or the
notation x : t, depending on what we think is more readable.
Adding lambda abstractions and β-reduction to a computational type theory pre-
serves all key properties: unique types, unique normal forms, termination, type
preservation, and canonicity.
let x : t = s in u
providing for local definitions. The reduction rule for let expressions is
let x : t = s in u ≻ ux
s
It turns out that in the system we are considering let expressions can be expressed
with β-redexes:
34
2.9 Matches
This syntactic transformation explains the typing rule and the reduction rule for let
expressions.
There will be a feature of full computational type theory (the conversion rule
in § 4.1) that distinguishes let expressions from β-redexes in that let expressions
introduce local reduction rules.
2.9 Matches
Matches are a defined notation for applicative expressions providing structural case
analysis for inductive types. A match has a clause for every constructor of the
underlying inductive type.
Matches for numbers take the form
match s [ 0 ⇒ u | Sx ⇒ v ]
match 0 [ 0 ⇒ u | Sx ⇒ v ] ≻ u
match S s [ 0 ⇒ u | Sx ⇒ v ] ≻ (λx.v) s
match s [ (x, y) ⇒ u ]
match s [ 0 ⇒ u | Sx ⇒ v ] MN _ s u (λx.v)
match s [ true ⇒ u | false ⇒ v ] MB _ s u v)
match s [ (x, y) ⇒ u ] M× _ _ _ s (λxy.u)
35
2 Basic Computational Type Theory
Match functions are inductive functions defined as one would expect form the de-
rived reduction rules:
MN : ∀Z T . N → Z → (N → Z) → Z MB : ∀Z T . B → Z → Z → Z
MN Z 0 e1 e2 := e1 MB Z true e1 e2 := e1
MN Z (Sx) e1 e2 := e2 x MB Z false e1 e2 := e2
M× : ∀XY Z T . X × Y → (X → Y → Z) → Z
M× XY Z (x, y) e := exy
Note that the definition of the match notation as an application of a match func-
tion provides both for type checking and reduction. Informally, we may summarize
the typing of matches as follows: A term match s [· · · ] has type u if s is has an
inductive type v, the match has a clause for every constructor of v, and every clause
of the match yields a result of type u.
We may write boolean matches with the familiar if-then-else notation:
More generally, we may use the if-then-else notation for all inductive types with ex-
actly two value constructors, exploiting the order of the constructors. For numbers
we have
Another notational device we take from Coq writes matches with exactly one
clause as let expressions. For instance:
36
2.11 Function Definitions in Coq
The same is true for formalizations on paper, where we formalize syntactic descrip-
tions, not values. We may see values as objects of our mathematical imagination.
The values of a type are also referred to as elements, members, or inhabitants
of the type. We call a type inhabited if it has at least one inhabitant, and uninhab-
ited or empty or void if it has no inhabitant. Values of functional types are referred
to as functions, and values of T are referred to as types.
As syntactic objects, terms may not be well-typed. Ill-typed terms are semanti-
cally meaningless and must not be used for computation and reasoning. Ill-typed
terms are always rejected by a proof assistant. Working with a proof assistant is the
best way to develop a reliable intuition for what goes through as well-typed. When
we say term in this text, we always mean well-typed term.
Recall that a term is closed if it has no free variables, and canonical if it is closed
and irreducible. CTT is designed such that every canonical term is either a constant,
or a constant applied to canonical terms, or an abstraction (obtained with λ), or a
function type (obtained with ∀), or a universe (so far we have T). A constant is
either a constructor or the name of an inductive function.
CTT is designed such that every closed term reduces to a canonical term of the
same type. More generally, every term reduces to an irreducible term of the same
type.
Different canonical terms may describe the same value, in particular when it
comes to functions.
For simple inductive types such as N, the canonical terms of the type are in
one-to-one correspondence with the values of the type. In this case we may see
the canonical terms of the type as the values of the type. For function types the
situation is more involved since semantically we may want to consider two functions
as equal if they agree on all arguments.
The proof assistant Coq realizes function definitions based on plain definitions of
the form
c : t := s
using lambda abstractions, native matches, and recursive abstractions. In fact, all
plain definitions in Coq have arity 0. Not having function definitions with arity
n ≥ 1 results in more fine-grained reduction and introduces intermediate normal
forms one doesn’t want to see from the application perspective. To mitigate the
problem, Coq refines the basic reduction rules with simplification rules simulating
the reductions one would have with inductive function definitions. Sometimes the
37
2 Basic Computational Type Theory
simulation is not perfect and the user is confronted with unpleasant intermediate
terms.
Coq comes with syntactic sugar facilitating the translation of inductive and plain
function definitions into Coq’s kernel language.
In this text we work with inductive function definitions and do not use recursive
abstractions at all. The accompanying demo files show how our high-level style can
be simulated with Coq’s primitives.
Having recursive abstractions and native matches is a design decision from Coq’s
early days (around 1990) when inductive types where added to a language designed
without having inductive types in mind (around 1985). Agda is a modern implemen-
tation of computational type theory that comes with inductive function definitions
and does not offer matches and recursive abstractions.
To express recursive inductive functions, Coq has recursive abstractions. Recur-
sive abstractions take the form
fix f s→t x s . u
and represent recursive functions as unfolding functions. There are two local vari-
ables f and x, where f acts as continuation function and x as argument. The type
of u must be t, and the type of the recursive abstraction itself is s → t.
We restrict our discussion to simply typed recursive abstractions. To gain full
expressivity, recursive abstractions in Coq are dependently typed.
Using an inductive function definition, a function D doubling its argument can
be defined as follows:
D: N→N
D 0 := 0
D (Sx) := S(S(Dx))
To express this definition in Coq, we use a plain definition with a recursive abstrac-
tion and a match:
Without limitations on recursive abstractions, one can easily write recursive abstrac-
tions whose reduction does not terminate. Coq imposes two limitations:
• An application of a recursive abstraction can only be reduced if the argument
term t starts with a constructor.
38
2.11 Function Definitions in Coq
Figure 2.2: Reduction chain for D(S0) defined with a recursive abstraction
of a boolean negation function. Give a complete reduction chain for !(! true).
Exercise 2.11.3 Verify every single reduction step in Figure 2.2 and convince your-
self that there is no other reduction chain.
39
2 Basic Computational Type Theory
2.12 Summary
We have outlined a typed and terminating functional language where functions and
types are first-class values that may appear as arguments and results of functions.
Termination is ensured by restricting recursion to structural recursion on inductive
types. Termination buys two important properties: decidability of computational
equality and integrity of inductive types (canonicity).
A key feature of modern type theories are dependent function types generalizing
simple function types. One speaks of dependent type theories to acknowledge the
presence of dependent function types.
In the system presented so far type checking and reduction are separated: Type
checking does not involve reduction, and reduction does not involve type checking.
Soon we will boost the expressivity of the system by extending it such that type
checking operates modulo computational equality of types. Type checking modulo
computational equality is needed so that the proof rules for equational rewriting
and induction on numbers can be obtained within computational type theory.
The computational type theory we are considering in this text is based on depen-
dent function types and inductive function definitions. In addition there are lambda
abstractions and let expressions. The language of the proof assistant Coq we are
using differs in that it replaces native function definitions with native matches and
recursive abstractions
Last but not least we mention that every function expressible with a closed term
in computational type theory is algorithmically computable. This claim rests on
the fact that there is an algorithm that evaluates every closed term to a canonical
term. The evaluation algorithm performs reduction steps as long as reduction steps
are possible. The order in which reduction steps are chosen matters neither for
termination nor for the canonical term finally obtained.
2.13 Notes
40
2.13 Notes
system.
A comprehensive discussion of the historical development of computational type
theories can be found in Constable’s survey paper [8]. We recommend the book
on homotopy type theory [30] for a complementary presentation of computational
type theory. The reader may also be interested in learning more about lambda
calculus [4, 17], a minimal computational system arranged around lambda abstrac-
tions and beta reduction.
41
3 Propositions as Types
A great idea coming with computational type theory is the propositions as types
principle. The principle says that propositions (i.e., logical statements) can be rep-
resented as types, and that the elements of the representing types can serve as
proofs of the propositions. This simple approach to logic works incredibly well
in practice and theory: It reduces proof checking to type checking, accommodates
proofs as first-call values, and provides a basic form of logical reasoning known as
intuitionistic reasoning.
The propositions as types principle is just perfect for implications s → t and
universal quantifications ∀x s . t. Both kind of propositions are accommodated as
function types (note the notational coincidence) and thus receive proofs as follows:
• A proof of an implication s → t is a function mapping every proof of the
premise s to a proof of the conclusion t.
• A proof of an universal quantification ∀x s .t is a function mapping every element
of the type of s to a proof of the proposition t.
The types for conjunctions s∧t and disjunctions s∨t will be obtained with inductive
type constructors such that a proof of s ∧ t consists of a proof of s and a proof of t,
and a proof of s ∨ t consists of either a proof of s or a proof of t. The proposition
falsity having no proof will be expressed as an empty inductive type ⊥. With falsity
we will express negations ¬s as implications s → ⊥. The types for equations s = t
and existential quantifications ∃x s .t will be discussed in later chapters once we
have extended the type theory with the conversion rule.
In this chapter you will see many terms describing proofs with lambda abstrac-
tions and matches. The construction of proof terms is an incremental process that
can be carried out in interaction with a proof assistant. On paper we will facilitate
the construction of proof terms with proof tables.
43
3 Propositions as Types
⊢ ∀X P . X → X : P
⊢ λX P .λx X . x : ∀X P . X → X
⊥ : P ::= []
⊤ : P ::= I
X→X λx.x
X→Y →X λxy.x
X→Y →Y λxy.y
(X → Y → Z) → Y → X → Z λf yx.f xy
We have omitted the types of the argument variables appearing in the lambda ab-
stractions on the right since they can be derived from the propositions appearing
on the left.
Our final examples express mobility laws for universal quantifiers:
44
3.2 Falsity and Negation
Functions that yield propositions once all arguments are supplied are called
predicates. In the above examples p is a unary predicate on the type X. In gen-
eral, a predicate has a type ending with P.
⊥ : P ::= []
E⊥ : ∀Z P . ⊥ → Z
¬s s→⊥
With this definition we have a proof of ⊥ if we have a proof of s and ¬s. Thus, given
a proof of ¬s, we can be sure that there is no proof of s. We say that we can disprove
a proposition s if we can give a proof of ¬s. The situation that we have some
proposition s and hypothetical proofs of both s and ¬s is called a contradiction
1
Suppose there is a closed term of type ⊥. Because of termination and type preservation there is a
closed and normal term of type ⊥. By canonicity this term must be obtained with a constructor
for ⊥. Contradiction.
45
3 Propositions as Types
X → ¬X → ⊥ λxf . f x
X → ¬X → Y λxf . E⊥ Y (f x)
(X → Y ) → ¬Y → ¬X λf gx. g(f x)
X → ¬¬X λxf .f x
¬X → ¬¬¬X λf g. gf
¬¬¬X → ¬X λf x. f (λ[Link])
¬¬X → (X → ¬X) → ⊥ λf g. f (λ[Link])
(X → ¬X) → (¬X → X) → ⊥ λf g. let x = g(λx.f xx) in f xx
⊤ : P ::= I
We have now seen two inductive type definitions whose type constructors ⊥
and ⊤ are declared as propositional types. We will call the value constructors for
inductive propositions (e.g., ⊤) proof constructors.
We speak of consistency if a type theory can express empty types. Consistency
is needed for a type theory so that it can express negative propositions.
Exercise 3.2.1 Show that ∀X P . X has no proof. That is, disprove ∀X P . X. That is,
prove ¬∀X P . X.
46
3.3 Conjunction and Disjunction
Most people are familiar with the boolean interpretation of conjunctions s ∧ t and
disjunctions s ∨ t. In the type-theoretic interpretation, a conjunction s ∧ t is a
proposition whose proofs consist of a proof of s and a proof of t, and a disjunction
s ∨ t is a proposition whose proofs consist of either a proof of s or a proof of t. We
make this design explicit with two inductive type definitions:
∧: P→P→P ∨: P→P→P
P P
C : ∀X Y . X → Y → X ∧ Y L : ∀X P Y P . X → X ∨ Y
R : ∀X P Y P . Y → X ∨ Y
With the type constructors ’∧’ and ’∨’ we can form conjunctions s ∧ t and disjunc-
tions s ∨ t from given propositions s and t. With the proof constructors C, L, and R
we can construct proofs of conjunctions and disjunctions:
• If u is a proof of s and v is a proof of t, then the term Cuv is a proof of the
conjunction s ∧ t.
• If u is a proof of s, then the term Lu is a proof of the disjunction s ∨ t.
• If v is a proof of t, then the term Rv is a proof of the disjunction s ∨ t.
Note that we treat the propositional arguments of the proof constructors as implicit
arguments, something we have seen before with the value constructor for pairs.
Since the explicit arguments of the proof constructors for disjunctions determine
only one of the two implicit arguments, the other implicit argument must be derived
from the surrounding context. This works well in practice.
The type constructors ’∧’ and ’∨’ have the type P → P → P, which qualifies
them as predicates. We will call type constructors inductive predicates if their type
qualifies them as predicates. We may say that disjunctions are accommodated with
an inductive predicate coming with two proof constructors.
Proofs involving conjunctions and disjunctions will often make use of matches.
Recall that matches are notation for applications of match functions obtained with
inductive function definitions. For conjunctions and disjunctions, we will use the
definitions appearing in Figure 3.2.
We note that E⊥ (§ 3.2) is the match function for the inductive type ⊥. We define
the notation
match s [] E⊥ _ s
47
3 Propositions as Types
M∧ : ∀XY Z P . X ∧ Y → (X → Y → Z) → Z
M∧ XY Z (C xy) e := exy
M∨ : ∀XY Z P . X ∨ Y → (X → Z) → (Y → Z) → Z
M∨ XY Z (L x) e1 e2 := e1 x
M∨ XY Z (R y) e1 e2 := e2 y
match s [ C xy ⇒ t ] M∧ _ _ _ s (λxy.t)
match s [ L x ⇒ t1 | R y ⇒ t2 ] M∨ _ _ _ s (λx.t1 ) (λy.t2 )
X →Y →X ∧Y CXY
X →X ∨Y LXY
Y →X ∨Y RXY
X ∧Y →X λa. match a [ Cxy ⇒ x ]
X ∧Y →Y λa. match a [ Cxy ⇒ y ]
X ∧Y →Y ∧X λa. match a [ Cxy ⇒ Cyx ]
X ∨Y →Y ∨X λa. match a [ Lx ⇒ RY X x | Ry ⇒ LY X y ]
with the nested pattern C(Cxy)z translates into the plain match
48
3.4 Propositional Equivalence
(X ∧ Y ) ∧ Z → X ∧ (Y ∧ Z)
λa. match a [ C(Cxy)z ⇒ Cx(Cyz) ]
(X ∨ Y ) ∨ Z → X ∨ (Y ∨ Z)
λa. match a [ L(Lx) ⇒ Lx | L(Ry) ⇒ R(Ly) | R z ⇒ R(R z) ]
X ∧ (Y ∨ Z) → (X ∧ Y ) ∨ (X ∧ Z)
λa. match a [ Cx(Ly) ⇒ L(Cxy) | Cx(Rz) ⇒ R(Cxz) ]
Exercise 3.3.1 Elaborate the proofs in Figure 3.4 such that they use nested plain
matches. Moreover, annote the implicite arguments of the constructors C, L and R
provided the application does not appear as part of a pattern.
s ←→ t (s → t) ∧ (t → s)
Exercise 3.4.1 Give proofs for the equivalences shown in Figure 3.5. The equiva-
lences formulate well-known properties of conjunction and disjunction.
49
3 Propositions as Types
X ∧ Y ←→ Y ∧ X commutativity
X ∨ Y ←→ Y ∨ X
X ∧ (Y ∧ Z) ←→ (X ∧ Y ) ∧ Z associativity
X ∨ (Y ∨ Z) ←→ (X ∨ Y ) ∨ Z
X ∧ (Y ∨ Z) ←→ X ∧ Y ∨ X ∧ Z distributivity
X ∨ (Y ∧ Z) ←→ (X ∨ Y ) ∧ (X ∨ Z)
X ∧ (X ∨ Y ) ←→ X absorption
X ∨ (X ∧ Y ) ←→ X
X ←→ X reflexivity
(X ←→ Y ) → (Y ←→ X) symmetry
(X ←→ Y ) → (Y ←→ Z) → (X ←→ Z) transitivity
′ ′ ′ ′
(X ←→ X ) → (Y ←→ Y ) → (X ∧ Y ←→ X ∧ Y ) compatibility with ∧
(X ←→ X ′ ) → (Y ←→ Y )′ → (X ∨ Y ←→ X ′ ∨ Y ′ ) compatibility with ∨
′ ′ ′ ′
(X ←→ X ) → (Y ←→ Y ) → ((X → Y ) ←→ (X → Y )) compatibility with →
50
3.5 Notational Issues
¬ ∧ ∨ ←→ →
for the logical connectives. Thus we may omit parentheses as in the following ex-
ample:
¬¬X ∧ Y ∨ Z ←→ Z → Y (((¬(¬X) ∧ Y ) ∨ Z) ←→ Z) → Y
¬¬X ¬(¬X)
X ∧Y ∧Z X ∧ (Y ∧ Z)
X ∨Y ∨Z X ∨ (Y ∨ Z)
⊥ ←→ ∀Z P . Z
X ∧ Y ←→ ∀Z P . (X → Y → Z) → Z
X ∨ Y ←→ ∀Z P . (X → Z) → (Y → Z) → Z
which say that ⊥, X ∧Y , and X ∨Y can be characterized with just function types. The
equivalences are known as impredicative characterizations of falsity, conjunction,
and disjunction. Figure 3.6 gives proof terms for the equivalences. One speaks of
an impredicative proposition if the proposition contains a quantification over all
propositions.
Note that the impredicative characterizations are related to the types of the
match functions for ⊥, X ∧ Y , and X ∨ Y .
51
3 Propositions as Types
⊥ ←→ ∀Z P . Z
C (E⊥ (∀Z P . Z)) (λf . f ⊥)
X ∧ Y ←→ ∀Z P . (X → Y → Z) → Z
C (λaZf . match a [ Cxy ⇒ f xy ]) (λf . f (X ∧ Y )CXY )
X ∨ Y ←→ ∀Z P . (X → Z) → (Y → Z) → Z
C (λaZf g. match a [ Lx ⇒ f x | Ry ⇒ gy ]) (λf . f (X ∨ Y ) LXY RXY )
The natural direction for proof term construction is top down, in particular as it
comes to lambda abstractions and matches. When we construct a proof term top
down, we need an information structure keeping track of the types we still have
to construct proof terms for and recording the typed variables introduced by sur-
rounding lambda abstractions and patterns of matches. It turns out that the proof
tables we have introduced in Chapter 1 provide a convenient information structure
for constructing proof terms.
Here is a proof table showing the construction of a proof term for a proposition
we call Russell’s law:
The table is written top-down beginning with the initial claim. It records the con-
struction of the proof term
52
3.7 Proof Term Construction using Proof Tables
consists of the first line. As the proof term construction proceeds, we add further
lines and further proof goals until we arrive at a complete proof table.
The rightmost column of a proof table records the actions developing the table
and the corresponding proof term.
• The action intro introduces λ-abstractions and matches.
• The action assert creates subgoals for an intermediate claim and the current
claim with the intermediate claim assumed. An assert action is realised with a
let expression in the proof term.
• The action apply applies a function and creates subgoals for the arguments.
• The action exact proves the claim with a complete proof term. We will not write
the word “exact” in future proof tables since that an exact action is performed
will be clear from the context.
With Coq we can construct proof terms interactively following the structure of
proof tables. We start with the initial claim and have Coq perform the proof actions
with commands called tactics. Coq then maintains the proof goals and displays the
assumptions and claims. Once all proof goals are closed, a proof term for the initial
claim has been constructed.
Technically, a proof goal consists of a list of assumptions called context and a
claim. The claim is a type, and the assumptions are typed variables. There may
be more than one proof goal open at a point in time and one may navigate freely
between open goals.
Interactive proof term construction with Coq is fun since writing, bookkeeping,
and verification are done by Coq. Here is a further example of a proof table:
The proof term constructed is λf g.f (λ[Link]). As announced before, we write the
proof action “exact gxx” without the word “exact”.
Figure 3.7 shows a proof table for a double negation law for universal quantifi-
cation. Since universal quantifications are function types like implications, no new
proof actions are needed.
Figure 3.8 shows a proof table using a destructuring action contributing a match
in the proof term. The reason we did not see a destructuring action before is that
so far the necessary matches could be inserted by the intro action.
Figure 3.9 gives a proof table for a distributivity law involving 6 subgoals. Note
the symmetry in the proof digram and the proof term constructed.
53
3 Propositions as Types
Figure 3.7: Proof table for a double negation law for universal quantification
∀X T ∀p X→P ∀qX→P .
(∀x. px ←→ qx) → (∀x. qx) → ∀x. px intro
X : T, p : X → P, q : X → P
f : ∀x. px ←→ qx
g : ∀x. qx
x:X px destruct f x
h : qx → px h(gx)
X ∧ (Y ∨ Z) ←→ (X ∧ Y ) ∨ (X ∧ Z) apply C
1 X ∧ (Y ∨ Z) → (X ∧ Y ) ∨ (X ∧ Z) intro
x:X
1.1 y :Y (X ∧ Y ) ∨ (X ∧ Z) L(Cxy)
1.2 z:Z (X ∧ Y ) ∨ (X ∧ Z) R(Cxz)
2 (X ∧ Y ) ∨ (X ∧ Z) → X ∧ (Y ∨ Z) intro
2.1 x : X, y : Y X ∧ (Y ∨ Z) Cx(Ly)
2.2 x : X, z : Z X ∧ (Y ∨ Z) Cx(Rz)
54
3.7 Proof Term Construction using Proof Tables
Figure 3.10: Proof table for a double negation law for implication
Figure 3.10 gives a proof table for a double negation law for implication. Note
the use of the exfalso action applying the explosion rule as realized by E⊥ .
Exercise 3.7.1 Give proof tables and proof terms for the following propositions:
a) ¬¬(X ∨ ¬X)
b) ¬¬(¬¬X → X)
c) ¬¬(((X → Y ) → X) → X)
d) ¬¬((¬Y → ¬X) → X → Y )
e) ¬¬(X ∨ ¬X)
f) ¬(X ∨ Y ) ←→ ¬X ∧ ¬Y
g) ¬¬¬X ←→ ¬X
h) ¬¬(X ∧ Y ) ←→ ¬¬X ∧ ¬¬Y
i) ¬¬(X → Y ) ←→ (¬¬X → ¬¬Y )
j) ¬¬(X → Y ) ←→ ¬(X ∧ ¬Y )
Exercise 3.7.2 Give a proof table and a proof term for the distribution law
∀X T ∀p X→P ∀qX→P . (∀x. px ∧ qx) ←→ (∀x. px) ∧ (∀x. qx).
55
3 Propositions as Types
The propositions as types approach presented here yields a rich form of logical rea-
soning known as intuitionistic reasoning. Intuitionistic reasoning refines reasoning
in mathematics in that it does not build in the law of excluded middle. This way in-
tuitionistic reasoning makes finer differences than the so-called classical reasoning
used in mathematics. Since type-theoretic logic can quantify over propositions, the
law of excluded middle can be expressed as the proposition ∀P P . P ∨ ¬P . Once we
assume excluded middle, we can prove all the propositions we can prove in boolean
logic.
3.9 Discussion
In this chapter we have seen that computational type theory can elegantly express
propositions and their proofs. Implications are expressed as function types s → t,
and functions of type s → t are taken as proofs of the implication s → t. More gen-
erally, universal quantifications ∀x u . t are expressed as dependent function types
∀x u . t. One speaks of the propositions as types approach. In contrast to conven-
tional mathematical reasoning, the propositions as types approach accommodates
propositions and proofs as first-class values that can be passed around with func-
tions.
We have expressed falsity ⊥, conjunction s ∧ t, and disjunction s ∨ t as inductive
propositions. We have seen that these propositions can be characterized without
inductive types just using function types. Negation ¬s is obtained as implication
s → ⊥.
56
3.9 Discussion
The logical reasoning coming with the propositions as types approach is known
as intuitionistic reasoning. Intuitionistic reasoning is more basic than conventional
mathematical reasoning in that it does not provide the law of excluded middle. Con-
ventional mathematical reasoning is then recovered by having the law of excluded
middle as an explicit assumption when needed. The law of excluded middle may be
expressed with the proposition ∀X P . X ∨ ¬X quantifying over propositions. Having
a dedicated universe P : T for propositional types is essential so that assumptions
such as excluded middle can be limited to propositional types and do not concern
computational types such as B and N.
In later chapters, we will see that the propositions as types approach extends to
equations and existential quantifications.
The propositions as types approach uses the typing rules of the underlying type
theory as proofs rules. This reduces proof checking to type checking and much
simplifies the implementation of proof assistants.
In the propositions as types approach, formal proofs are obtained as terms built
with constants, variables, function application, and lambda abstraction. Constants
appear as proof constructors for s ∧ t, and s ∨ t and with the inductive match
functions for ⊥, s ∧ t, and s ∨ t. The construction of (proof) terms is best done in
a type-driven top-down fashion recorded with a proof table. The main operation is
function application, where the function applied yields the required propositional
type and subgoals are issued for the arguments of the function. The resulting proof
language is amazingly elegant and easily yields familiar proof patterns: making
assumptions (lambda abstraction), destructuring of assumptions (application of a
match function), applying implicational assumptions (function application).
For the beginner, this chapter is the place where you will get fluent with lambda
abstractions, matches (i.e. applications of match functions), and dependent func-
tion types. We offer dozens of examples for exploration on paper and in interaction
with a proof assistant. For proving on paper, we use proof tables recording incre-
mental top-down constructions of proof terms. When we construct proof terms in
interaction with a proof assistant, we issue proof actions that incrementally build
the proof term and display the information recorded in the proof table.
In the system presented so far, proofs are verified with the typing rules and
no use is made of the reduction rules. This will change in the next chapter where
we extend the typing discipline with a conversion rule identifying computationally
equal types.
The details of the typing rules matter. What prevents a proof of falsity are the
typing rules and the rules restricting the form of inductive definitions. In this text,
we explain the details of the typing rules mostly informally, exploiting that compli-
ance with the typing rules is verified automatically by the proof assistant. To be sure
that something is well-typed or has a certain type, it is always a good idea to have it
57
3 Propositions as Types
checked by the proof assistant. We expect that you deepen your understanding of
the typing rules using the proof assistant.
We have already pointed out that we have a separate universe P of propositional
types so that assumptions like the law of excluded middle do not affect computa-
tional types. If we were not interested in computational types, we could work with
a single universe T and consider all types as propositions.
58
4 Conversion and Leibniz Equality
This chapter introduces a typing rule called conversion. The conversion rule
strengthens basic computational type theory so that propositional equality, exis-
tential quantification, and induction lemmas can be defined. The conversion rule
relaxes the type discipline so that the typing rules apply modulo computational
equality of types.
We will define propositional equality s = t following a scheme known as Leibniz
equality. Three typed constants will suffice: One constant accommodating equa-
tions s = t as propositions, one constant providing proofs of trivial equations s = s,
and one constant providing for equational rewriting. As it turns out, the constants
provide for equational reasoning by means of their types, the actual definitions of
the constants are not needed.
The conversion rule of the type theory gives the constants for trivial equations
and for rewriting the proof power required. Due to the conversion rule, proposi-
tional equality will subsume computational equality, and a single rewriting constant
will capture all equational rewriting situations.
We extend the type theory with abstract constant definitions hiding the defini-
tion of constants except for the types of the constants. Abstract constants suffice
for propositional equality and many other purposes, including propositional con-
nectives and theorems.
While abstract constants don’t strengthen the type theory as it comes to what
can be defined and to what can be proven, they provide a means for enforcing
abstraction layers. A system of abstract constants constitutes an interface between
the uses and the definitions of the constants.
In this chapter we start proving theorems in type theory. Theorems will be rep-
resented as abstract constants in type theory. On paper, we will transition from
formal proofs (proof terms) to informal proofs similar to informal proofs in math-
ematical presentations. Our informal proofs will be formulated such that they can
be elaborated into formal proofs, and the scripts constructing the formal proofs in
the proof assistant will appear in the accompanying Coq files.
There is much elegance and surprise in this chapter. There are plenty of new
subjects, including typing modulo computational equality, propositional equality,
abstract constants, and the representation of theorems. Take your time to fully
understand the beautiful constructions.
59
4 Conversion and Leibniz Equality
Recall the typing rules from Chapter 2. The conversion rule is an additional typing
rule relaxing typing by making it operate modulo computational equality of types:
⊢ s : u′ u ≈ u′ ⊢u:T
⊢s:u
The rule says that a term s has type u if s has type u′ and u and u′ are compu-
tationally equal. We use the notation u ≈ u′ to say that two terms u and u′ are
computationally equal. For now two terms are computationally equal if they reduce
to the same term (up to renaming of bound variables). Later, we will strengthen
computational equality with a law for lambda abstractions called eta equivalence.
Note that the conversion rule has a premise ⊢ u : T, which ensures that the
term u describes a type. Importantly, the conversion rule also applies to proposi-
tional types (since there is a typing rule typing types in P also as types in T).
Adding the conversion rule to the basic typing rules preserves the key properties
of computational type theory (§ 2.5). As before, there is a type checking algorithm
deriving types for well-typed terms, where the type is now unique up to computa-
tional equality and minimality with respect to universes.
For a first example of the use of the conversion rule consider the proposition
Without the conversion rule, the proposition has no proof. To see this, consider the
term
λp N→P . λap(4) . a
which has the propositional type ∀p N→P . p(4) → p(4), which is equal to the given
proposition up to computational equality. Using the conversion rule the term is a
proof of the proposition. There is also the possibility to use the conversion rule
early as shown in the proof table
The proof table gives us exactly the proof term shown above. So we learn that a
term leaves open where type checking uses the conversion rule. On the other hand,
we can use proof tables to say where the conversion rule is used with type checking.
60
4.2 Abstract Propositional Equality
With the conversion rule at our disposal, we can now show how the propositions
as types approach can accommodate propositional equality. It turns out that all we
need are three typed constants:
eq : ∀X T . X → X → P
Q : ∀X T ∀x X . eq X x x
R : ∀X T ∀xy X ∀p X→P . eq Xxy → px → py
61
4 Conversion and Leibniz Equality
We will keep the constants abstract. It turns out that we can do equational reasoning
without knowing the definitions of the constants. All we need are the types of the
constants.
The constant eq : ∀X T . X → X → P allows us to write equations as proposi-
tional types:
s=t eq _ st
s≠t ¬eq _ st
The constants Q and R provide the basic proof rules for equations. For applications
of Q and R the conversion rule of the type theory is essential.
(λy.y = z → x = z)y
62
4.2 Abstract Propositional Equality
While the uses of the conversion rule appear prominently in the proof table, they
don’t show up explicitly in the proof term. While the first conversion is forced
by the rewriting predicate in the application of R, the second conversion will only
appear if the context imposes the type x = y → y = z → x = z for the proof term.
Here the predicate p taken as argument determines the target type py of the rewrit-
ing constant R. Target type functions work because of the conversion rule and
lambda abstractions. We will see nonpropositional target type functions in Chap-
ter 5.
Exercise 4.2.1 Give a proof term for the equation ! true = false. Explain why the
term is also a proof term for the equation false = ! ! false.
Exercise 4.2.2 Give a proof term for the converse rewriting law
∀X T ∀xy∀p X→P . eq Xxy → py → px.
63
4 Conversion and Leibniz Equality
The constants Q and R give us straightforward proofs for many equational facts.
To say it once more, Q together with the conversion rule provides proofs by com-
putational equality, and R together with the conversion rule provides equational
rewriting. Figure 4.1 shows a collection of basic equational facts, and Figure 4.2
gives proof tables and the resulting proof terms for most of them. The remaining
proofs are left as exercise. It is important that you understand each of the proofs
in detail.
Note that the proof tables in Figure 4.2 all follow the same scheme: First comes
a step introducing assumptions, then a conversion step making the rewriting pred-
icate explicit, then the rewriting step as application of R, then a conversion step
simplifying the claim, and then the final step proving the simplified claim.
We now understand how the basic proof steps “rewriting” and “proof by compu-
tational equality” used in the tables in Chapter 1 are realized in the propositions as
types approach.
Interestingly, the proof of the transitivity law
∀X T x X y X zX . x = y → y = z → x = z
can be simplified so that the conversion rule is not used. The simplified proof term
λXxyze1 e2 . R (eq x) e2 e1
exploits the fact that the equation x = z is the application (eqx)z up to notation.
Constructor laws
If we look at the facts in Figure 4.2, we see that three of them
64
4.3 Basic Equational Facts
Fact: ⊤ ≠ ⊥
Proof term: λe. R (λX P .X) e I
⊤≠⊥ intro
e:⊤=⊥ ⊥ conversion
(λX P .X)⊥ apply R _ e
(λX P .X)⊤ conversion
⊤ I
Fact: ∀xy N . Sx = Sy → x = y
Proof term: λxye. R (λz. x=match z [ 0⇒0|Sz′ ⇒z′ ]) e (Qx)
x : N, y : N Sx = Sy → x = y intro
e : Sx = Sy x=y conversion
′ ′
(λz. x = match z [ 0 ⇒ 0 | Sz ⇒ z ]) (Sy) apply R _ e
(λz. x = match z [ 0 ⇒ 0 | Sz′ ⇒ z′ ]) (Sx) conversion
x=x Qx
Fact: ∀X T ∀xyzX . x = y → y = z → x = z
Proof term: λXxyze. R (λy. y=z→x=z) e (λe.e)
65
4 Conversion and Leibniz Equality
concern inductive types while the others are not specifically concerned with induc-
tive types. We speak of constructor laws for inductive types. Note that the proofs
of the constructor laws all involve a match on the underlying inductive type, and
recall that matches are obtained as inductive functions. So to prove a constructor
law, one needs to discriminate on the underlying inductive type at some point. A
proof assistant will automatically provide constructor laws for every inductive type
definition it accepts.
Exercise 4.3.1 Study the two proof terms given for transitivity in detail using Coq.
Give the proof table for the simplified proof term. Convince yourself that there is
no proof term for symmetry that can be type-checked without the conversion rule.
Exercise 4.3.2 Give proof tables and proof terms for the following propositions:
a) ∀x N . 0 ≠ Sx
b) ∀X T x X y X . x = y → y = x
c) ∀X T Y T f X→y x y. x = y → f x = f y
d) ∀X T Y T f X→Y g X→Y x. f = g → f x = gx
e) (∀X P . X → X) ≠ (∀X P . X)
x = y ←→ ∀p X→P . px → py
x = y ←→ ∀p X→P . px ←→ py
which may be phrased as saying that two objects are equal if and only if they satisfy
the same properties.
Note that each of the two equivalences suggests a possible definition of the con-
stant eq. We will choose the first equivalence.
66
4.4 Definition of Leibniz Equality
Here are plain function definitions defining the constants for abstract propositional
equality:
eq : ∀X T . X → X → P
eq Xxy := ∀p X→P . px → py
Q : ∀X T ∀x. eq X x x
Q Xx := λpa.a
The definitions are amazingly simple. Note that the conversion rule is needed to
make use of the defining equation of eq. The definition of eq follows the Leibniz
characterization of equality established in Exercise 4.3.4.
The above definition of propositional equality is known as Leibniz equality and
appears in Whitehead and Russell’s Principia Mathematica (1910-1913). Computa-
tional type theory also provides for the definition of a richer form of propositional
equality using an indexed inductive type family. We will see the definition of induc-
tive equality in § 15.1 and study it carefully in Chapter 29. Except for Chapter 29
the concrete definition of propositional equality does not matter since everything
we need can be obtained from the abstract equality constants eq, Q, R introduced
in § 4.2.
≤: N→N→P
x ≤ y := (x − y = 0)
We now extend the definitional facilities of our type theory with abstract constant
definitions. An abstract constant definition is like a plain constant definition but
comes with the proviso that the defining equation is not available for reduction.
67
4 Conversion and Leibniz Equality
Thus when we use an abstract constant we cannot make use of its definition. We
may say that the definition of an abstract constant is hidden.
We may use abstract constants to represent the constants for equality, conjunc-
tion, disjunction, and falsity. For using these constructs the definitions of the con-
stants are not needed, all we need are the types of the constants.
In the examples considered so far always a group of constants is defined and the
formation constants can only be made abstract after the accompanying introduction
and elimination constants have been defined.
A mathematical development is a carefully arranged collection of definitions and
theorems that build on each other. Theorems are observations that have been ver-
ified. In type theory, we model theorems as abstract constants with propositional
types. The use of an abstract constant for a theorem models the fact that theorems
can be used without knowing their proofs. It suffices to know that there is a proof.
Sometimes theorems have involved proofs appearing in the literature the user has
never worked through.
Theorems come with several different names including facts, lemmas, and corol-
laries. The different names are a matter of presentation and do not have technical
significance. In type theory all we have are abstract constants with propositional
types. We often use the term lemma to refer to abstract constants with proposi-
tional types.
All theorems stated as such in this text are theorems in type theory. They are given
informal proofs in the text and formal proofs in the accompanying Coq files, where
they appear as abstract constants with propositional types. The informal proofs we
give in the text are designed such that they can be elaborated into the formal proofs
appearing in the accompanying Coq files.
In this text, we mostly use the heading “Fact” to state theorems obtained in type
theory. Here is a first example.
Proof Rewriting. ■
68
4.7 Abstract Presentation of Propositional Connectives
formal proofs (i.e., proof terms) using a proof assistant. Typically, we will delegate
the elaboration to the accompanying Coq files. For the purpose of illustration, we
give a complete proof table for the proof of Fact 4.6.1:
To demonstrate the use of Fact 4.6.1, we will prove the constructor laws for
numbers using inductive functions for predecessors and not-zero tests:
pred : N → N notzero : N → P
pred 0 := 0 notzero 0 := ⊥
pred (Sx) := x notzero (S_) := ⊤
Exercise 4.6.4 Give proof terms for Facts 4.6.1, 4.6.2, and 4.6.3.
69
4 Conversion and Leibniz Equality
⊥ : P
E⊥ : ∀Z P . ⊥ → Z
∧ : P→P→P
C : ∀X P Y P . X → Y → X ∧ Y
E∧ : ∀X P Y P Z P . X ∧ Y → (X → Y → Z) → Z
∨ : P→P→P
L : ∀X P Y P . X → X ∨ Y
R : ∀X P Y P . Y → X ∨ Y
E∨ : ∀X P Y P Z P . X ∨ Y → (X → Z) → (Y → Z) → Z
eq : ∀X T . X → X → P
Q : ∀X T ∀x X . eq X x x
E= : ∀X T ∀xy X ∀p X→P . eq Xxy → px → py
70
4.8 Computational Equality and Eta Equivalence
ing the abstract constants, and in each case the impredicative characterization can
be read of the type of the elimination constants. Thus any definition of a forma-
tion constant satisfying the impredicative characterization will be equivalent to the
abstract formation constant. Moreover, in each case, the impredicative characteri-
zation of the formation constant suggests a definition of the abstract constants.
One more observation. If we have the type of the formation constant, the type
of the elimination constant follows from the types of the introduction constants
by thinking of the given constants an as inductive definition. Conversely, given
the types of the formation and elimination constants, the types of the introduction
constants follow from the type of the elimination constant by reconstructing the
connecting inductive definition.
We will see in § 6.1 that existential quantification can also be captured with a sys-
tem of abstract constants following the formation-introduction-elimination scheme,
and that the constants can be defined either inductively or impredicatively.
Exercise 4.7.2 (Impredicative definitions) Define the abstract constants for falsity,
conjunction, and disjunction using the impredicative characterization of the forma-
tion constant. Do not use the inductive definitions.
Exercise 4.7.3 Prove commutativity of conjunction and disjunction using the ab-
stract constants for conjunction and disjunction.
71
4 Conversion and Leibniz Equality
which equates a well-typed lambda abstraction λ[Link] with the term s, provided x
does not occur free in t. Eta equivalence realizes the commitment to not distinguish
between the function described by a term s and the lambda abstraction λ[Link]. A
concrete example is the η-equivalence between the constructor S and the lambda
abstraction λnN .Sn.
Computational equality is compatible with the term structure. That is, if we
replace a subterm of a term s with a term that has the same type and is computa-
tionally equal, we obtain a term that is computationally equal to s.
We say that two terms are convertible if they are computationally equal, and
call conversion the process of replacing a term with a convertible term. A simpli-
fication is a conversion where the final term is obtained from the initial term by
reduction. Examples for conversions that are not simplifications are applications of
the η-equivalence law, or expansions, which are reductions in reverse order (e.g.,
proceeding from x to 0+x). Figure 4.2 contains several proof tables with expansion
steps.
C : (X × Y → Z) → (X → Y → Z)
U : (X → Y → Z) → (X × Y → Z)
such that the equations C(U f ) = f and U(Cg)(x, y) = g(x, y) hold by computa-
tional equality. Find out where η-equivalence is used.
Exercise 4.8.2 Verify that the following equations hold by computational equality
using the definitions of +, −, and iter from Chapter 1.
a) (+)1 = S
b) (+)2 = λx. S(Sx)
c) (+)(3 − 2) = S
d) (λx. 1 + x) = S
e) (λx. 3 + x − 2) = S
f) iter S 2 = λx. S(Sx)
Note that all right hand sides are canonical terms. Thus it suffices to compute the
normal forms of the left hand sides and then check whether the two normal forms
are equal up to α- and η-equivalence.
72
4.9 Notes
4.9 Notes
There are plenty of new ideas in this chapter playing a major role from now on:
• Type checking modulo computational equality of types
• Target type functions
• Abstract constants
• Representation of equality with abstract constants
• Representation of theorems with abstract constants
• Representation of propositional connectives with abstract constants
• Informal proofs rather than formal proofs in the text
• Elaboration of informal proofs in the text into formal proofs in the Coq files
Exercise 4.9.1 Figure 4.4 summarizes the equational laws we have discussed in this
chapter. Make sure you can give for each law the complete quantifier prefix and a
proof term using the abstract constants eq, Q, and R.
73
4 Conversion and Leibniz Equality
Equivalence
x=x
x=y →y =x
x=y →y =z→x=z
Rewriting
x = y → px → py
x = y → py → px
Applicative closure
x = y → fx = fy
f = g → f x = gx
Impredicative characterization
x = y ←→ ∀p. px → py
Disequality
px → ¬py → x ≠ y
Constructor laws
true ≠ false
0 ≠ Sx
Sx = Sy → x = y
(x, y) = (x ′ , y ′ ) → x = x ′ ∧ y = y ′
74
5 Inductive Eliminators
For the inductive types we have seen we have defined match functions. Match func-
tions are general inductive functions applying continuations given as arguments.
The type of a match function may be formulated with a target type function, some-
thing we have seen before with the rewriting law. Target type functions raise the
proof power of match functions substantially, as we will see with booleans, num-
bers, and pairs.
Following comon language, we call general inductive functions like match func-
tions (inductive) eliminators. For every inductive type there is a canonical elimina-
tor, which, in some sense, is most general. If the inductive type is recursive (like
the type of numbers), the canonical eliminator will also take care of the recursion.
Speaking from the perspective of proving, the types of eliminators describe proof
rules for structural case analysis and structural induction. With the canonical elimi-
nators for booleans, numbers, and pairs we can finally formalize all informal proofs
we have seen in Chapter 1.
We will study the eliminators for booleans, numbers, and pairs using examples.
This will include a formal proof of the disequation N ≠ B. We will also look at the
eliminators for void and unit.
It is time to spell out two important restrictions of the typing discipline. The
restrictions are needed so that we obtain a consistent type theory that cannot
prove falsity. The restrictions concern propositional discrimination and the self-
containment T : T.
With this chapter we arrive at a computational type theory that can formalize a
large variety of theorems, including all theorems in Chapter 1. The only extension
of computational type theory still to come concerns generalized inductive types
(Chapter 15).
⊢s:P
⊢s:T
75
5 Inductive Eliminators
makes it possible to see propositional types as types of the full universe T. We now
generalize the subtyping rule such that predicates can be seen as type functions:
⊢ s : ∀x1 : t1 . · · · ∀xn : tn . P
n≥0
⊢ s : ∀x1 : t1 . · · · ∀xn : tn . T
To have an example for the use of the subtyping rule, suppose we have a function
Then both λp B→P . E p and λp B→T . E p are well-typed terms. A function E with the
given type will appear as the boolean eliminator in § 5.4, and the generalized sub-
typing rule frees us from defining E separately for predicates and type functions.
E⊥ : ∀Z T . ⊥ → Z
⊤ : P ::= I
E⊤ : ∀p ⊤→T . p I → ∀a. pa
E⊤ pa I := a
76
5.2 Propositional Discrimination Restriction
We will refer to E⊥ and E⊤ as the eliminators for the inductive propositions ⊥ and ⊤.
The eliminator E⊥ makes it possible to obtain a value for every type in a proposi-
tionally contradictory situation. The eliminator E⊤ makes it possible to prove that I
is the only value of ⊤.
The inductive types 0 (void) and 1 (unit) defined in § 2.3 correspond to the induc-
tive propositions ⊥ (falsity) and ⊤ (truth). The propositional variants ⊥ and ⊤ are
more flexible than 0 and 1 since they can also be used as propositions. As a matter
of style, we will prefer 1 over ⊤ in computational situations.
We call inductive predicates that are exempted from the discrimination restric-
tion computational predicates. It will take some time until we see computational
predicates that matter. You may check § 14.1 (linear search) and Chapter 30 (well-
founded recursion). The inductive predicate ∧P→P→P providing conjunction also
comes out as computational, but there are no worthwhile applications of this fea-
ture.
We call a discrimination on a value of a propositional inductive type a proposi-
tional transfer discrimination if the traget type of the discrimination is not propo-
sitional.1 A propositional transfer discrimination is only admissible if its proposi-
tional inductive type is computational. The defining equations of the eliminators E⊥
and E1 are examples for transfer discriminations.
A helpful intuition for the propositional discrimination restriction is that it en-
sures that no information is leaked from the propositional level to the computa-
tional level. For instance, given a proof of a disjunction, it cannot be leaked to the
computational level which side of the disjunction is served by the proof.
F : P ::= C(F)
The propositional discrimination restriction does not apply to F since the argument
of the single proof constructor C has a propositional type.
a) Define an inductive function E : ∀Z T . F → Z.
b) Prove F ←→ ⊥.
1
In Coq slang transfer discriminations are called large eliminations.
77
5 Inductive Eliminators
The typing rules involving the universe T need to be refined with universe levels
in order that a consistent type theory is obtained. The problem is with the self-
containment T : T, which permits vicious cycles if not restricted.2 To fix the problem,
every occurrence of the universe T is assigned a positive integer called a universe
level during type checking. The typing rules containing more than one occurrence
of T are augmented so that they exclude cycles Ti : Ti .
⊢ u : Ti x : u ⊢ v : Ti
⊢ Ti : Ti+1 ⊢ ∀x : u. v : Ti
u := ∀X : Ti . t
where t : Tj but t is not propositional. Then u can only be typed with some Tk
where k > i and k ≥ j. The situation completely changes if t is propositional.
Then u can be typed with P no matter how large the level i is.
One may think of graded universes Ti as an infinite cumulative hierarchy of
universes:
P ⊆ T1 ⊆ T2 ⊆ T3 ⊆ · · ·
P : T1 : T2 : T3 : · · ·
The lowest universe P is distinguished from the higher universes in that it is closed
under function types taking types as arguments. Speaking propositionally, propo-
sitions are closed under all quantifications, including big quantifications over uni-
verses. This feature of P is known as impredicativity. The impredicative character-
izations we have seen for falsity, conjunction, disjunction, and equality exploit the
impredicativity of P.
In practice, there is no need to worry about universe levels since the proof as-
sistant will verify that they can be assigned consistently. It requires agressive con-
structions to force a universe level inconsistency. In Chapter 31 we will present a
construction where universe levels become crucial and ignoring them would lead to
a proof of falsity.
Finally, there is a generalized subtyping rule for graded universes:
⊢ s : ∀x1 : t1 . · · · ∀xn : tn . Ti
n≥0
⊢ s : ∀x1 : t1 . · · · ∀xn : tn . Ti+1
2
A related problem appears in set theory, where the set of all sets must be excluded.
78
5.4 Boolean Eliminator
λaT1 . 0 : T1 → N : T2
λaT1 →N . 0 : (T1 → N) → N : T3
λa(T1 →N)→N . 0 : ((T1 → N) → N) → N : T4
f true := s1
f true := s2
f e1 e2 true := e1
f e1 e2 true := e2
f : ∀Z T . Z → Z → B → Z
A more general type for this format uses a target type function:3
It turns out that the general format with the target type function is necessary when
we use f to do a boolean case analysis. We fix the definition
79
5 Inductive Eliminators
and call the function EB the boolean eliminator. We refer to the arguments e1 and e2
of EB as continuations.
The type of EB says that we obtain a proof of ∀x B . px if we provide proofs
of p true and p false. This provides us with a general means to do boolean case
analysis. To do a boolean case analysis with EB , the defining equations of EB are not
needed.
Consider the defining equations of EB . They are well-typed since the patterns
EB pab true and EB pab false on the left instantiate the target type to p true and
p false, which are the types of the variables e1 and e2 .
∀p B→T . p true→p false→∀[Link]
Note that the eliminator EB generalizes the match func-
T
tion MB ∀Z . B→Z→Z→Z from § 2.9. The generalization comes with the target type
function p of EB , which provides more flexibility than the target type Z of MB .
Recall that in type theory a boolean conditional is an abbreviation for an appli-
cation of an inductive function discriminating on B. From now on we shall use the
boolean eliminator for this purpose:
if s1 then s2 else s3 EB t s2 s3 s1
Note that the term t describing the target type function must be derived from the
context of the boolean conditional.
Exercise 5.4.1 Define boolean negation and boolean conjunction with the boolean
eliminator.
∀x B . x = true ∨ x = false
The types enclosed in the upper corners are placeholders for terms having the given
types. We refer to the placeholders as subgoals. Note that the types of the subgoals
are obtained with the conversion rule. We may now use the proof terms L(Q true)
and R(Q false) for the subgoals and obtain the complete proof term
80
5.6 Kaminski’s Equation
Proof term constructed: EB (λx. x = true ∨ x = false) (L(Q true)) (R(Q false))
Figure 5.1 shows a proof table constructing the proof term. The table makes
explicit the conversions handling the applications of the target type functions.
That all boolean case analysis can be obtained with a single eliminator crucially
depends on the use of a target type function in the type of the eliminator. A simply
typed boolean eliminator ∀Z T . Z → Z → B → Z can for instance not express the
boolean case analysis needed for ∀x B . x = true ∨ x = false. And recall that target
type functions only work with lambda abstractions and a conversion rule taking
care of β-reductions.
Exercise 5.5.1 For each of the following propositions give a proof term applying
the boolean eliminator.
a) ∀p B→P ∀x. (x = true → ptrue) → (x = false → pfalse) → px.
b) x & y = true ←→ x = true ∧ y = true.
c) x | y = false ←→ x = false ∧ y = false.
d) ∀p B→P . (∀xy. y = x → px) → ∀[Link].
Obviously, a boolean case analysis on just x does not suffice for a proof. What we
need in addition is boolean case analysis on the terms f true and f false. To make
this possible, we prove the equivalent claim
81
5 Inductive Eliminators
by boolean case analysis on x, y, and z. This yields 8 subgoals, all of which have
straightforward equational proofs. Here is the subgoal for x = false, y = false, and
z = true :
N : T ::= 0 | S(N)
f 0 := s1
f (Sn) := s2
f e1 e2 0 := e1
f e1 e2 (Sn) := e2 n (f n)
Note that the argument e2 is a function taking the predecessor n and the result of
the recursive application f n as arguments.6
5
Coq supports the pattern with the eqn modifier of the destruct tactic.
6
Since computation in type theory is always terminating, eager recursion cannot cause problems.
82
5.7 Eliminator for Numbers
We now come to the typing of the eliminator function. To obtain enough flexi-
bility, we shall employ a target type function p N→T . Given the equations for f , this
design decision forces the type
It says that we can obtain a proof of ∀n. pn by supplying proofs for p0 and
∀n. pn → p(Sn). For the second proof obligation we have to supply a function
that for every n yields a proof of p(Sn) given a proof of pn. Thus EN gives us a
proof rule for structural induction on numbers. Note that the so-called inductive
hypothesis appears as pn in the type ∀n. pn → p(Sn) of the continuation function
for the successor case.
We have just seen one of the most elegant and novel aspects of computational
type theory: Induction on numbers is obtained through (the type of) the arithmetic
eliminator, which is obtained as a recursive inductive function on numbers.
The type of EN clarifies many aspects of informal inductive proofs. For instance,
the type of EN makes clear that the variable n in the final claim ∀n. pn is different
from the variable n in the successor case ∀n. pn → p(Sn). Nevertheless, it makes
sense to use the same name for both variables since this makes the inductive hy-
pothesis pn agree with the final claim.
We shall use the notations
for arithmetic case analysis. The terms t describing the target type function must
be inferred from the context.
83
5 Inductive Eliminators
x+0=x conversion
(λx. x + 0 = x) x apply EN
1 (λx. x + 0 = x) 0 conversion
0=0 comp. eq.
2 ∀x. (λx. x + 0 = x) x → (λx. x + 0 = x)(Sx) conversion
∀x. x + 0 = x → Sx + 0 = Sx intro
IH : x + 0 = x Sx + 0 = Sx conversion
S(x + 0) = Sx rewrite IH
Sx = Sx comp. eq.
a) Explain how a match function for numbers provides for case analysis.
b) Define a match function for numbers as an inductive function.
c) Define a match function for numbers using the arithmetic eliminator EN .
We can now do inductive proofs completely formally. As our first example we con-
sider a proof of the fact
∀x. x + 0 = x
The application yields two subgoals known as base case and successor case. Both
subgoals have straightforward proofs. Note how the inductive hypothesis appears
as an implicational premise in the successor case. Figure 5.2 shows a proof table
for a proof term completing the partial proof term.
We will see many inductive proofs in this text. We shall write inductive proofs in
an informal style making sure that the informal proof can be easily elaborated into
a formal proof with the proof assistant. A (detailed) informal proof for our example
may look as follows.
84
5.9 Equality of Numbers is Logically Decidable
Fact 5.8.1 x + 0 = x.
Exercise 5.8.3 Prove the following equations stating the correctness of addition
functions obtained with the arithmetic eliminator. Both equations require inductive
proofs.
a) x + y = EN (λ_.N) y (λ_.S) x
b) x + y = EN (λ_.N → N) (λy.y) (λ_ay.S(ay)) x y
Fact 5.9.1 ∀x N y N . x = y ∨ x ≠ y.
The informal proof is burdened with many cases and much detail. Constructing
a formal proof with a proof assistant will organize the cases and the details in a
pleasant and more manageable way.
85
5 Inductive Eliminators
∀x N y N . x = y ∨ x ≠ y apply EN , intro y
1 0=y ∨0≠y destruct y
1.1 0=0∨0≠0 trivial
1.2 0 = Sy ∨ 0 ≠ Sy constructor law
2 IH: ∀y N . x = y ∨ x ≠ y Sx = y ∨ Sx = y destruct y
2.1 Sx = 0 ∨ Sx ≠ 0 constructor law
2.2 Sx = Sy ∨ Sx ≠ Sy destruct IH y
2.2.1 H: x = y Sx = Sy rewrite H, trivial
2.2.2 H: x ≠ y Sx ≠ Sy intro, apply H
H1 : Sx = Sy x=y injectivity S
If you are not experienced with inductive proofs, the remark that y be quantified
in the inductive hypothesis may be confusing. The confusion will go away with the
formal proof.
A proof table constructing a proof term following the informal proof appears in
Figure 5.3.
Following the table, we begin the construction of the formal proof with the par-
tial proof term
EN (λx. ∀y. x = y ∨ x ≠ y)
[∀y. 0 = y ∨ 0 ≠ y\
[∀x. (∀y. x = y ∨ x ≠ y) → ∀y. Sx = y ∨ Sx ≠ y\
This first step makes explicit the quantified inductive hypothesis. Both subgoals are
shown by case analyis on the quantfied target. This can be done with the elimina-
tor EN . To keep things manageable we will work with a match function
MN (λy. 0 = y ∨ 0 ≠ y)
[0 = 0 ∨ 0 ≠ 0\
[∀y. 0 = Sy ∨ 0 ≠ Sy\
The first subgoal is trivial, and the second subgoal follows with constructor dis-
jointness.
86
5.10 Eliminator for Pairs
x=y∨x≠y
λxh∀y. . MN (λy. Sx = y ∨ Sx ≠ y)
[Sx = 0 ∨ Sx ≠ 0\
[∀y. Sx = Sy ∨ Sx ≠ Sy\
The first subgoal follows with constructor disjointness. The second subgoal follows
with the instantiated inductive hypothesis hy and injectivity of S.
This completes our explanation of a (formal) proof of Fact 5.9.1. If we do the
proof with a proof assistant, a fully formal proof is constructed but most of the
details are taken care of by the assistant. To document the proof informally for a
human reader, it’s probably best to write something like the following:
s×t Pair st
(s, t) pair _ _ st
Following the scheme we have seen for booleans and numbers, we define an elimi-
nator for pairs as follows:
87
5 Inductive Eliminators
for cartesian destructuring. The terms t1 , t2 , and t1 describing the component types
and the target type function must be inferred from the context.
Exercise 5.10.1 Prove the following facts for pairs a : X × Y using the eliminator E× :
a) (π1 a, π2 a) = a η-law
b) swap(swap a) involution law
See § 1.8 for the definitions of the projections π1 and π2 and the function swap.
Exercise 5.10.2 Use E× to write functions that agree with π1 , π2 , and swap.
Exercise 5.10.3 By now you know enough to formalize all proofs of Chapter 1 in
computational type theory. Do some of the proofs in Coq without using the tactics
for destructuring and induction. Apply the eliminators you have seen in this chapter
instead.
The types N and B of booleans and numbers are different since they have different
cardinality: While there are infinitely many numbers, there are only two booleans.
But how can we show this fact in the logical system we have arrived at?
Since B and N both have type T, we can write the propositions N = B and N ≠ B.
So the question is whether we can prove N ≠ B. We can do this with a property
distinguishing the two types (Exercise 4.3.5). We choose the predicate
p(X T ) := ∀xyzX . x = y ∨ x = z ∨ y = z
saying that a type has at most two elements. It now suffices to prove pB and ¬pN.
With boolean case analysis on the variables x, y, z we can show that p holds for B.
Moreover, we can disprove pN by choosing x = 0, y = 1, and z = 2 and proving
(0 = 1 ∨ 0 = 2 ∨ 1 = 2) → ⊥
Fact 5.11.1 N ≠ B.
On paper, it doesn’t make sense to work out the proof in more detail since this
involves a lot of writing and routine verification. With Coq, however, doing the
complete proof is quite rewarding since the writing and the tedious details are taken
care of by the proof assistant. When we do the proof with Coq, we can see that the
techniques introduced so far smoothly scale to more involved proofs.
88
5.12 Notes
a) B ≠ B × B d) B ≠ ⊤
b) ⊥ ≠ ⊤ e) P ≠ ⊤
c) ⊥ ≠ B f) B ≠ T
Exercise 5.11.3 Note that one cannot prove B ≠ B × ⊤ since one cannot give a pred-
icate that distinguishes the two types. Neither can one prove B = B × ⊤.
5.12 Notes
M∧ : ∀XY Z P . X ∧ Y → (X → Y → Z) → Z
M∨ : ∀XY Z P . X ∨ Y → (X → Z) → (Y → Z) → Z
used in Chapter 3 (Figure 3.2) don’t use target type functions. In fact, there is no
need for target type functions since in ordinary mathematical reasoning proposi-
tions don’t talk about their proofs.
Functions typed with target type functions are polymorphic in the number of
their arguments. For instance:
E⊥ N : ⊥ → N
E⊥ (N → N) : ⊥ → N → N
E⊥ (N → N → N) : ⊥ → N → N → N
We remark that the proof assistant Coq automatically derives eliminators for
every inductive type definition it processes. For the inductive types discussed in
this chapter Coq derives the eliminators we have presented (except for ⊤).
89
6 Existential Quantification
An existential quantification ∃x t . s says that the predicate λx t .s is satisfiable, that
is, that there is some u such that the proposition (λx t .s)u is provable. Follow-
ing this idea, a basic proof of ∃x t . s is a pair (u, v) consisting of a witness u : t
and a certificate v : (λx t .s)u. This design may be realized with an inductive type
definition.
We will prove two prominent logical facts involving existential quantification:
Russell’s Barber theorem (a non-existence theorem) and Lawvere’s fixed point theo-
rem (an existence theorem). From Lawvere’s theorem we will obtain a type-theoretic
variant of Cantor’s power set theorem (there is no surjection from a set to its power
set).
ex : ∀X T . (X → P) → P
∃x t . s ex (λx t . s)
E : ∀X T ∀p X→P ∀x X . px → ex X p
M∃ : ∀X T ∀p X→P ∀Z P . ex p → (∀x. px → Z) → Z
91
6 Existential Quantification
ex (X : T, p : X → P) : P ::= E (x : X, px)
The inductive type definition for ex and E has two parameters where the type of
the second parameter p depends on the first parameter X. This is the first time
we see such a parameter dependence. The inductive definitions for pair types and
conjunctions also have two parameters, but there is no dependency. Also, the def-
inition for existential quantification is the first time we see a parameter (p) that is
not a type. Moreover, the proof constructor E comes with an additional dependency
between its first proper argument x and the type px of its second proper argument.
Again, this is the first time we see such a dependency. Inductive type definitions
with dependencies between parameters and proper arguments of constructors are
standard in computational type theory.
The elimination constant M∃ can now be defined as an inductive function:
M∃ : ∀X T ∀p X→P ∀Z P . ex p → (∀x. px → Z) → Z
M∃ XpZ (E _ _ xa) f := f xa
We now recognize M∃ as the simply typed match function for existential types.
When convenient, we will use the match notation
match s [ E xa ⇒ t ] M∃ _ _ _ s (λxa.t)
where the first equation is just a notational change and the second equation is by
application of the η-law.
In practice, it is not a good idea to make explicit inessential conversions like the
ones in Figure 6.1. Instead, it is preferable to think modulo conversion. Figure 6.2
shows a proof table with implicit conversions constructing the same proof term.
This is certainly a better presentation of the proof. The second table gives a fair
representation of the interaction you will have with Coq. In fact, Coq will immedi-
ately reduce the first two β-redexes you see in Figure 6.1 as part of the proof actions
introducing them. This way there will be no need for explicit conversion steps.
92
6.1 Inductive Definition and Basic Facts
Exercise 6.1.1 Prove the following propositions with proof tables and give the re-
sulting proof terms. Mark the proof actions involving implicit conversions.
x ≠ y ←→ ∃p. px ∧ ¬py
93
6 Existential Quantification
(∃[Link]) ←→ ∀Z P . (∀x. px → Z) → Z
Exercise 6.1.5 Universal and existential quantification are compatible with propo-
sitional equivalence. Prove the following compatibility laws:
Ex : ∀X T . (X → P) → P
E : ∀X T ∀p X→P ∀x X . px → Ex X p
M : ∀X T ∀p X→P ∀Z P . ex p → (∀x. px → Z) → Z
Exercise 6.1.7 (Intuitionistic drinker) Using excluded middle, one can argue that
in a bar populated with at least one person one can always find a person such that
if this person drinks milk everyone in the bar drinks milk:
The fact follows intuitionistically once two double negations are inserted:
Nonexistence results often get a lot of attention. Here are two famous examples:
1. Russell: There is no set containing exactly those sets that do not contain them-
selves: ¬∃x ∀y. y ∈ x ←→ y ∉ y.
94
6.3 Lawvere’s Fixed Point Theorem
2. Turing: There is no Turing machine that halts exactly on the codes of those
Turing machines that don’t halt on their own code: ¬∃x ∀y. Hxy ←→ ¬Hyy.
Here H is a predicate that applies to codes of Turing machines such that Hxy
says that Turing machine x halts on Turing machine y.
It turns out that both results are trivial consequences of a straightforward logical
fact known as barber theorem.
Proof Suppose there is an x such that ∀y. pxy ←→ ¬pyy. Then pxx ←→ ¬pxx.
Contradiction by Russell’s law ¬(X ←→ ¬X) as shown in §3.7. ■
Exercise 6.2.2 Give a proof table and a proof term for the barber theorem. Con-
struct a detailed proof with Coq.
Proof Consider ! x = x and derive a contradiction with boolean case analysis on x.■
95
6 Existential Quantification
Proof Let f X→(X→Y ) be surjective and g Y →Y . Then f a = λx.g(f xx) for some a.
We have f aa = g(f aa) by rewriting and conversion. ■
We remark that Corollaries 6.3.4 and 6.3.5 may be seen as variants of Cantor’s
theorem.
Exercise 6.3.6 Construct with Coq detailed proofs of the results in this section.
Exercise 6.3.7
a) Prove that all functions ⊤ → ⊤ have fixed points.
b) Prove that the successor function S : N → N has no fixed point.
c) For each type Y = ⊥, B, B × B, N, P, T give a function Y → Y that has no fixed
point.
Exercise 6.3.8 With Lawvere’s theorem we can give another proof of Fact 6.3.2
(propositional negation has no fixed point). In contrast to the proof given with
Fact 6.3.2, the proof with Lawvere’s theorem uses mostly equational reasoning.
The argument goes as follows. Suppose (¬X) = X. Since the identity is a surjec-
tion X → X, the assumption gives us a surjection X → (X → ⊥). Lawvere’s theorem
now gives us a fixed point of the identity on ⊥ → ⊥. Contradiction since the type of
the fixed point is falsity.
Do the proof with Coq.
96
7 Arithmetic Pairing
Cantor discovered that numbers are in bijection with pairs of numbers. Cantor’s
proof rests on a counting scheme where pairs appear as boxes in a plane. Based
on Cantors scheme, we realize the bijection between numbers and pairs with two
functions inverting each other. We obtain an elegant formal development using only
a few basic facts about numbers.
7.1 Definitions
E : N×N→N encode
D : N→N×N decode
that invert each other: D(E(x, y)) = (x, y) and E(Dn)) = n. The functions are
based on the counting scheme for pairs shown in Figure 7.1. The pairs appear as
points in the plane following the usual coordinate representation. Counting starts
at the origin (0, 0) and follows the diagonals from right to left:
Assuming a function
η: N×N→N×N
that for every pair yields its successor on the diagonal walk described by the count-
ing scheme, we define the decoding function D as follows:
D(n) := ηn (0, 0)
η(0, y) := (Sy, 0)
η(Sx, y) := (x, Sy)
97
7 Arithmetic Pairing
..
y .
5 20
4 14 19
3 9 13 18
2 5 8 12 17
1 2 4 7 11 16
0 0 1 3 6 10 15 ···
0 1 2 3 4 5 x
We now come to the definition of the encoding function E. We first observe that
all pairs (x, y) on a diagonal have the same sum x + y, and that the length of the
nth diagonal is n. We start with the equation
E(x, y) := σ (x + y) + y
σn = 0 + 1 + 2 + ··· + n
σ (0) := 0
σ (Sn) := Sn + σ n
7.2 Proofs
We will prove E(Dn)) = n and D(Ea) = a. The first equation is easier than the sec-
ond equation. Both directions will profit from an equation saying that the encoding
of the successor of a pair is the successor of the encoding of the pair.
98
7.2 Proofs
We now come to the proof of the second equation D(Ea) = a. The difficulty
here is that there is no n we can do induction on. We solve the problem by doing
induction on the number Ea using an auxiliary variable n = Ea.1 We prepare the
necessary case analysis for the induction with three conditional equations for the
encoding function. Two of the equations tell us how we can obtain for Ea = Sn a
pair a′ such that η(Ea′ ) = a. Note that a′ is clear from the geometric presentation
of the counting scheme.
Proof (1) Follows with a = (x, y) and case analysis on x and y using S _ ≠ 0.
(2) follows with (Sx, 0) = η (0, x) and Fact 7.2.1. (3) follows with (x, Sy) = η (Sx, y)
and Fact 7.2.1. ■
∀n ∀a. Ea = n → Dn = a
by induction on n.
For n = 0, Fact 7.2.3 gives us a = (0, 0) making the conclusion trivial.
For the successor case we prove
Ea = Sn → D(Sn) = a
We consider three cases: a = (0, 0), (Sx, 0), (x, Sy). The case a = (0, 0) is trivial
since the premise is contradictory. The other two cases follow with (2) and (3) of
Fact 7.2.3 and the inductive hypothesis. ■
Exercise 7.2.5 (Walking diagonals the other way) The encoding function for
pairs walks diagonals from right to left. One may also walk the diagonals from
left to right. Rework the Coq development accordingly.
The exercise will help you with your understanding of arithmetic pairing.
The exercise will boost your understanding of the role a proof assistant can play
in a mathematical development. Modifying a development with a proof assistant is
more efficient and more reliable than doing it on paper. Type checking and proof
checking turn out to be very helpful for this task.
1
We have seen a similar unfolding step with Kaminski’s equation in § 5.6.
99
7 Arithmetic Pairing
Exercise 7.2.6 (Bijections) A bijection between two types X and Y consists of two
functions f X→Y and g Y →X such that ∀x. g(f x) = x and ∀y. f (gy) = y.
a) Give and verify a bijection between N and (N × N) × N.
b) Prove that there is no bijection between B and 1.
c) Prove that there is no bijection between N and N → B.
Exercise 7.2.7 (Internal Arithmetic pairing)
Arithmetic pairing establishes N as a universal type that can represent pairs inter-
nally. To make this insight explicit, define functions
π :N→N→N π1 : N → N π2 : N → N
7.3 Discussion
Arithmetic pairing is a great case study at several levels. The magic starts with
the geometrical presentation of the counting scheme, which informally establishes
a computable bijection. All the rest is about formal verification of what we see
geometrically. The first step is the recursive definition of the encoding and decoding
function, which is a nice programming exercise in computational type theory. As it
comes to the verification of the two roundtrip equations, it is clear that both require
induction on numbers. For one of the equations, the number we can do induction
on needs to be explicated. Both inductive proofs hinge on the fact that a successor
function η for pairs is made explicit that commutes with the successor function on
numbers.
The elaboration of the proof in the accompanying Coq file is enlightening. We
use it to demo several advanced features of the tactic language. As with most proofs
in the text, the informal proofs in this chapter were written only after formal proofs
had been constructed and polished with the proof assistant.
It is interesting to look up Cantor’s pairing function in the mathematical liter-
ature and in Wikipedia, where the computational aspects of the construction are
ignored as much as possible. One typically starts with the encoding function using
the Gaussian sum formula to avoid the recursion. Then injectivity and surjectivity
of the encoding function are shown, which non-constructively yields the existence
of the decoding function. The simple recursive definition of the decoding function
does not appear. The construction presented here is due to Andrej Dudenhefner
(March 2020) who contributed it to Coq’s standard library.
100
7.3 Discussion
What I like about the development of the pairing function is the interplay be-
tween the geometric presentation of the counting scheme and the formalization
with function definitions and proofs. Their is much elegance at all levels. Cantor’s
pairing function could make a great example for an educated Programming 1 course
addressing functional programming and program verification.
101
8 Abstract Syntax
8.1 Lists
[] , nil
[x] , cons x nil
[x , y] , cons x (cons y nil)
[x , y , z] , cons x (cons y (cons z nil))
103
8 Abstract Syntax
The constructor nil provides the empty list. The constructor cons yields for a
value x and a list representing the sequence [x1 , . . . , xn ] a list representing the
sequence [x , x1 , . . . , xn ]. Given a list cons x A, we call x the head and A the tail
of the list. We say that lists provide a nested pair representation of sequences.
Following the above design in type theory, we obtain lists with an inductive type
definition
The type constructor L : T → T gives us a list type L(X) for every base type X. The
value constructor nil : ∀X T . L(X) gives us an empty list for every base type. Finally,
the value constructor cons : ∀X T . X → L(X) → L(X) provides for the construction
of nonempty lists by adding an element in front of a given list. The type of cons
ensures that all elements of a list of type L(X) are of type X.
For nil and cons, we don’t write the first argument X. We use the notations
[] := nil
x :: A := cons x A
x :: y :: A x :: (y :: A)
When convenient, we shall use the sequence notation [x1 , . . . , xn ] for lists.
The inductive definition of lists provides for case analysis, recursion, and induc-
tion on lists, in a way that is similar to what we have seen for numbers. We may see
the constructors nil and cons as refinements of the constructors 0 and S.
Concatenation of sequences
[x1 , . . . , xm ] +
+ [y1 , . . . , yn ] = [x1 , . . . , xm , y1 , . . . , yn ]
appends two sequences. You may be familiar with concatenation of strings, which
are sequences of characters.
We provide concatenation of lists with an inductive function
Concatenation of lists is similar to addition of numbers. Two basic laws for addition
are x + 0 and (x + y) + z = x + (y + z). We prove the list versions A + +[] = A and
A++(B ++ C) = (A +
+ B) ++ C of the laws. The proofs are very similar to the arithmetic
proofs with induction on lists replacing induction on numbers.
104
8.2 Expressions and Evaluation
[] +
+[] = []
(x :: A) +
+[] = x :: A
The first obligations holds by computational equality. The second obligation sim-
plifies to x :: (A +
+[]) = x :: A and follows with the induction hypothesis. ■
which is obtained with a scheme generalizing the scheme we have seen for numbers.
Proof assistants will generate a suitable eliminator function when they accept an
inductive type definition.
Exercise 8.1.3 (Eliminator for lists) Define the eliminator function for lists with the
type stated above. Verify that the inductive proofs of Facts 8.1.2 and 8.1.1 can be
formalized with the inductive eliminator function.
Exercise 8.1.4 (Length) Define a length function len : ∀X. L(X) → N for lists and
prove len (A +
+ B) = len A + len B. Note that the length function prunes a list into the
number that remains if one cuts away the elements coming with cons.
Exercise 8.1.5 (Reversal) Define a function rev : ∀X. L(X) → L(X) reversing lists
and prove rev (A + + B) = rev B +
+ rev A and rev (rev A) = A. For instance, we have
rev [1, 2, 3, 4] = [4, 3, 2, 1].
We will consider arithmetic expressions obtained with constants, addition, and sub-
traction. Informally, we describe the abstract syntax of expressions with a scheme
known as a BNF grammar:
e : exp ::= x | e1 + e2 | e1 − e2 (x : N)
105
8 Abstract Syntax
Note that the inductive type exp has one value constructor for every form of ex-
pression identified by the grammar.
To ease our presentation, we will write the formal expressions provided by the
inductive type exp using the notation suggested by the BNF. For instance:
e1 + e2 − e3 sub(add e1 e2 )e3
E : exp → N
E x := x
E (e1 + e2 ) := E e1 + E e2
E (e1 − e2 ) := E e1 − E e2
Exercise 8.2.1 Do the reduction E(3 + 5 − 2) ≻∗ 6 step by step (at the equational
level).
Exercise 8.2.2 (Constructor Laws for expressions) Prove some of the constructor
laws for expressions. For instance, show that con is injective and that add and sub
are disjoint.
∀p exp→T .
(∀x. p(con x) →
(∀e1 e2 . pe1 → pe2 → p(add e1 e2 ) →
(∀e1 e2 . pe1 → pe2 → p(sub e1 e2 ) →
∀[Link]
106
8.3 Code and Execution
We will compile expressions into lists of numbers. We refer to the list obtained for
an expression as the code of the expression. The compilation will be such that an
expression can be reconstructed from its code, and that execution of the code yields
the same value as evaluation of the expression.
Code is executed on a stack and yields a stack, where stacks are list of numbers.
We define an execution function RCA executing a code C on a stack A as follows:
Note that the function R is defined by recursion on the first argument (the code) and
by case analysis on the second argument (the stack). From the equations defining R
you can see that the first number of the code determines what is done:
• 0 : take two numbers from the stack and put their sum on the stack.
• 1 : take two numbers from the stack and put their difference on the stack.
• SSx : put x on the stack.
The first equation defining R returns the stack obtained so far if the code is ex-
hausted. The last equation defining R is a so-called catch-all equation: It applies
whenever none of the preceding equations applies. Catch-all equations are a nota-
tional convenience that can be replaced by several equations providing the full case
analysis.
Note that the execution function is defined with tail recursion, which can be real-
ized with a loop at the machine level. This is in contrast to the evaluation function,
which is defined with binary recursion. Binary recursion needs a procedure stack
when implemented with loops at the machine level.
Exercise 8.3.1 Do the reduction R[5, 7, 1][] ≻∗ [2] step by step (doing operations
on numbers in one step).
8.4 Compilation
We will define a compilation function γ : exp → L(N) such that ∀e. R(γe)[] = [Ee].
That is, expressions are compiled to code that will yield the same value as evaluation
when executed on the empty stack.
107
8 Abstract Syntax
γ : exp → L(N)
γx := [SSx]
γ(e1 + e2 ) := γe2 +
+ γe1 +
+[0]
γ(e1 − e2 ) := γe2 +
+ γe1 +
+[1]
R (γe) [] = [Ee]
The first idea is to show the equation by induction on e. This, however, will fail
since the recursive calls of R leave us with nonempty stacks and partial codes not
obtainable by compilation. So we have to generalize both the possible stacks and the
possible codes. The generalization of codes can be expressed with concatenation.
Altogether we obtain an elegant correctness theorem telling us more about code
execution than the correctness equation we started with. Formulated in words, the
correctness theorem says that executing the code γe + + C on a stack A gives the
same result as executing the code C on the stack Ee :: A.
R (γ(e1 + e2 ) +
+ C) A
= R (γe2 +
+ γe1 + + C) A
+[0] + definition γ
= R (γe1 + + C) (Ee2 :: A)
+[0] + inductive hypothesis
= R ([0] +
+ C) (Ee1 :: Ee2 :: A) inductive hypothesis
= R C ((Ee1 + Ee2 ) :: A) definition R
= R C (E(e1 + e2 ) :: A) definition E
Exercise 8.4.3 Do the reduction γ(5 − 2) ≻∗ [4, 7, 1] step by step (at the equational
level).
Exercise 8.4.4 Explore the proof of the correctness theorem starting with the proof
script in the accompanying Coq development.
108
8.5 Decompilation
8.5 Decompilation
We now define a decompilation function that for all expressions recovers the expres-
sion from its code. This is possible since the compiler uses a reversible compilation
scheme, or saying it abstractly, the compilation function is injective. The decompi-
lation function closely follows the scheme used for code execution, where this time
a stack of expressions is maintained:
The correctness theorem for decompilation closely follows the correctness the-
orem for compilation.
δ (γ(e1 + e2 ) +
+ C) B
= δ (γe2 +
+ γe1 + + C) B
+[0] + definition γ
= δ (γe1 + + C) (e2 :: B)
+[0] + inductive hypothesis
= δ ([0] +
+ C) (e1 :: e2 :: B) inductive hypothesis
= δ C ((e1 + e2 ) :: B) definition δ
The equational reasoning tacitly employs conversion and associativity for concate-
nation +
+. ■
8.6 Notes
The semantics of the expressions and codes considered here is particularly simple
since evaluation of expressions and execution of codes can be accounted for by
structural recursion.
Expressions are represented as abstract syntactic objects using an inductive
type. Inductive types are the canonical representation of abstract syntactic objects.
109
8 Abstract Syntax
110
9 Certifying Functions and Sum Types
In computational type theory one can write function types that specify the input-
output relation of their functions. We speak of certifying function types and of
certifying functions. When proving properties of a certifying function, one ignores
its definition and relies on its type. For this reason, we will accommodate certifying
functions with abstract constants.
Certifying functions are at the heart of type theory and will play a main role
from now on. As it turns out, language used for mathematical proofs generalizes
to language for constructing certifying functions.
Certifying functions are the computational analogue of propositional lemmas.
Like lemmas, they come as abstract constants whose definition does not matter for
their use. Like lemmas, certifying functions are constructed with informal argu-
ments providing for their formal construction in tactic mode. As with lemmas, the
details of the formal definition of certifying functions do not matter.
The types of certifying functions are often obtained with sum types and sigma
types, which are computational variants of propositional disjunctions and existen-
tial quantifications. To keep the amount of new ideas digestible, we postpone the
discussion of sigma types to the next chapter.
One important application of sum type are certifying equality deciders. We refer
to types having a certifying equality decider as discrete types. We will see that the
class of discrete types is closed under taking product types and under taking sum
types. Moreover, discrete types are closed under taking injective preimages.
Sum types are a basic type construction and it is about time we introduce them.
Like product types X × Y sum types X + Y combine two types X and Y . However,
sum types are dual to product types in that their elements carry a value of one of
the types rather than values of both types. Sum types may be seen as disjoint type
unions. The propositional versions of sum types and product types are conjunc-
tions and disjunctions.
111
9 Certifying Functions and Sum Types
+: T→T→T
L : ∀XY T . X → X + Y
R : ∀XY T . Y → X + Y
∀XY Z T . X + Y → (X → Z) → (Y → Z) → Z
E+ XY pe1 e2 (L x) := e1 x
E+ XY pe1 e2 (R y) := e2 y
We can use sum types to construct finite types of any cardinality:
⊥ no element
⊥+1 1 element
(⊥ + 1) + 1 2 elements
((⊥ + 1) + 1) + 1 3 elements
Exercise 9.1.1 Give all elements of the type ((⊥ + 1) + 1) + 1 and prove that your
enumeration is complete: ∀a((⊥+1)+1)+1 . a = R I ∨ · · · .
Exercise 9.1.2 (Constructor laws for sum types)
Prove the constructor laws for sum types:
a) L x ≠ R y.
b) L x = L x ′ → x = x ′ .
c) R y = R y ′ → y = y ′ .
Hint: The techniques used for numbers (Figure 4.2) also work for sums.
1
Relying on the context for disambiguation, we reuse the names L and R also used for the proof
constructors of disjunctions.
112
9.2 Proving at Type Level
X a Y := (X → Y ) × (Y → X)
(X × Y → Z) a (X → Y → Z)
(X + Y → Z) a (X → Z) × (Y → Z)
(X → Y × Z) a (X → Y ) × (X → Z)
∀XY Z T . (X × Y → Z) a (X → Y → Z)
∀XY Z P . (X ∧ Y → Z) ←→ (X → Y → Z)
The advantage of taking the proof view for types is that the construction of a value
can be done at the proof level rather than at the term level. As we know from
propositions, writing an informal proof that can be elaborated into a formal proof
is much easier than writing a formal proof. Moreover, the elaboration of informal
proofs into formal proofs is greatly assisted by the tactic interpreter of a proof
assistant. It is now time to acknowledge that the tactic interpreter of Coq works for
types in general, not just propositions. The only significant difference between the
propositional and the general level is the propositional discrimination restriction,
which imposes extra conditions on propositional destructuring.
113
9 Certifying Functions and Sum Types
b) ∀XY Z T . (X + Y → Z) a (X → Z) × (Y → Z)
c) ∀XY Z T . (X → Y + Z) a (X → Z) × (X → Z)
Exercise 9.2.3 Prove that double negated disjunction agrees with double negated
sum: ¬¬(P ∨ Q) ←→ ¬(P + Q → ⊥).
D(X T ) : T := X + (X → ⊥)
114
9.4 Certifying Functions
Our lead example for certifying functions are certifying equality deciders for num-
bers:
∀xy N . (x = y) + (x ≠ y)
Functions of this type take two numbers and decide whether they are equal. The
decision is returned with a proof asserting the correctness of the decision. When
convenient, we will refer to such proofs as certificates.
Constructing a certifying equality decider for numbers in tactic mode is routine.
In fact, we have carried out the construction before (Fact 5.9.1) for the proposition
∀xy N . (x = y) ∨ (x ≠ y).
115
9 Certifying Functions and Sum Types
Exercise 9.4.2
a) Construct a certifying equality decider F : ∀xy B . (x = y) + (x ≠ y).
b) Prove ∀xy. if F xy then x = y else x ≠ y.
c) Define a boolean equality decider f B→B→B and prove
∀xy. if f xy then x = y else x ≠ y.
d) Prove ∀xy. f xy = if F xy then true else false.
∀xy X . D(x = y)
116
9.6 Computational Decidability
Given two values of type X, a certifying equality deciders decides whether the val-
ues are equal and returns its decision together with a certificate (a proof that the
decision is correct).
We say that a type is discrete if it has a certifying equality decider. It will be con-
venient to have a notation for the type of certifying equality deciders for a type X:
Proof E(N) is Fact 9.4.1. The remaining claims are left as exercises. ■
Computational type theory is designed such that every definable function is algo-
rithmically computable. Thus we can prove that predicates are computationally de-
cidable within computational type theory by constructing (certifying) deciders for
117
9 Certifying Functions and Sum Types
We can also define dec0 (X) := D(X). We remark that type theory cannot express a
uniform and readable definition of a type family decn for n ≥ 0.
So far we have focussed on deciders for the equality predicate. We have seen
that the data types ⊥, 1, B, and N have equality deciders, and that this extends to
their closure under product and sum types and injective preimages.
We remark that type theory can easily express undecidable predicates. We will
formalize Post correspondence problem with an inductive predicate in § 15.6.
118
10 Certifying Functions and Sigma Types
Sigma types are dependent pair types. Sigma types are obtained with a type function
p X→T and take as values pairs (x, y) with x : X and y : px. Sigma types are the
computational version of the propositional types for existential quantification.
Sigma types are essential for certifying function types. Given a relation p X→Y →P ,
we can write the function type ∀x. Σy. pxy whose functions take an argument x X
and yield a result y Y such that pxy. More precisely, functions of type ∀x. Σy. pxy
yield for every x a pair (y, a) where a : pxy. A concrete example we will consider
is a certifying function type
∀x N . Σn. (x = 2 · n) + (x = 2 · n + 1)
for division by 2. Functions of this type take a number x and yield the Euclidean
quotient of x/2. Moreover, functions of this type decide whether there is a remain-
der and yield a proof for the correctness of the result (a certificate).
We can translate a certifying function into a simply typed function and a cor-
rectness proof. The translation from simply typed to certified is also possible. For
certifying deciders, we can state the presence of the translations with the computa-
tional equivalence
(P ∨ Q) ←→ □(P + Q)
(∃[Link]) ←→ □(Σ[Link])
(x, y) : X × Y
119
10 Certifying Functions and Sigma Types
fixing the types X and Y of their components a priori. Dependent pair types
(x, y) : sig p
employ a type function p X→T and admit all pairs (x, y) such that x : X and y : px.
Thus a dependent pair type doesn’t fix the type of the second component a priory
but instead determines it for each pair depending on the first component.
Dependent pair types sig p are usually written as
Σx X .px
sig : ∀X T . (X → T) → T
E : ∀X T ∀p X→T ∀x X . px → sigX p
We will write (x, a) or (x, a)p for a dependent pair E Xpxa and call x and a the
first and second component of the pair. When p is a predicate, we may refer to x
as the witness and to a as the certificate, as we did for existential quantification.
We often write Σx X .px for sigX p. Following common speak, we will often refer to
dependent pair types as sigma types.
We define the universal eliminator for sigma types:
There is considerable typing bureaucracy but the basic idea is familiar from product
types X × Y and existential quantifications ∃x X . px: If we have a pair s : sigX p, we
can assume x and y and replace s with (x, y). This idea becomes apparent with
the simple type
(∀x X . px → Z) → (sigX p → Z)
Using the eliminator with the simple type suffices whenever s : sigX p occurs only
once in the context we are working in.
We extend the correspondence table between propositional type constructors
and computational type constructors with ∀, ∃, and Σ:
120
10.2 Certifying Division
∀ → + × Σ a computational types in T
∀ → ∨ ∧ ∃ ←→ propositional types in P
We discuss certifying division as our lead example for certifying functions whose
target type is a sigma type:
∀x N . Σn. (x = 2 · n) + (x = 2 · n + 1)
A function of this type takes a number x and returns the Euclidean quotient n of x
and 2 together with a decision of whether x is even or odd. In each case a certificate
(i.e., correctness proof) is provided.
Proving that there is a certifying division function of the given type is straight-
forward. As it comes to the informal proof, there is no difference to proving the
propositional version obtained by replacing Σ with ∃ and + with ∨.
Fact 10.2.1 ∀x N . Σn. (x = 2 · n) + (x = 2 · n + 1).
We remark that the proof uses simply typed applications of the eliminators for
sigma and sum types for destructuring the inductive hypothesis. As usual we do
not mention these type-theoretic details.
Fact 10.2.2 Let F : ∀x N . Σn. (x = 2 · n) + (x = 2 · n + 1) and D and M be functions
N → N defined as follows:
Dx := let (n, _) = F x in n
Mx := let (_, a) = F x in if a then 0 else 1
Proof The let expressions used in the definitions of D and M are notation for sim-
ply typed applications of the eliminator for sigma types. The conditional in the
definition of M is notation for a simply typed application of the eliminator for sum
types.
We fix x and prove x = 2 · Dx + Mx and Dx ≤ 1. Let F x = (n, a).
If a = L_, we have x = 2 · n, Dx = n, and Mx = 0. Thus x = 2 · Dx + Mx and
Mx ≤ 1.
If a = R_, we have x = 2 · n + 1, Dx = n, and Mx = 1. Thus x = 2 · Dx + Mx
and Mx ≤ 1. ■
121
10 Certifying Functions and Sigma Types
The type-theoretic details of the proof can be elaborated and verified with a proof
assistant. On paper, one follows mathematical intuition and ignores type-theoretic
details as done above. In case of doubt, one elaborates the informal proof with a
proof assistant.
We provide a few remarks on the type-theoretic details of the above proof. The
equation x = 2 · Dx + Mx is shown after unfolding of D and M (conversion rule)
by destructuring F x into (n, a) (dependently typed application of eliminator for
sigma types) and destructuring a (simply typed application of the eliminator for
sum types). If interested, elaborate the proof with the proof assistant so that all de-
structuring is done by explicitly applying the eliminators for sigma and sum types.
Note that Fact 10.2.2 makes explicit that a definition of the assumed certifying
division function F is not available for the proof.
We remark that in tactic mode eliminators are typically used as certifying
functions whose definition is not needed (here the eliminators for numbers in
Fact 10.2.1, and the eliminators for sum types and sigma types in Fact 10.2.2). The
exception are the applications of the eliminators in the local definitions of D and M
in Fact 10.2.2, where the defining equations of the eliminators are needed for the
proof.
122
10.3 Translation Theorems
Exercise 10.2.9 Prove that double negated existential quantification agrees with
double negated sigma quantification: ¬¬ex p ←→ ¬¬sig p.
We can translate between certifying and boolean deciders for types and equality.
Using sigma types, we can state this fact formally.
Recall the notation for certifying deciders for type functions:
A function of type dec (p X→T ) decides for every x whether the type px is inhabited.
In case px is inhabited, a witness is returned, and otherwise a proof px → ⊥.
123
10 Certifying Functions and Sigma Types
f x := let (y, _) = F x in y
satisfies px(f x) for all x. The other direction is obvious since px(f x). ■
10.4 Projections
We assume a type function p : X → T and define projections yielding the first and
the second component of a dependent pair asig p :
Note that the type of π2 is given using the projection π1 . The use of π1 is necessary
since the type of the second component of a depends on the first component of a.
Type checking the defining equation of π2 requires a conversion step applying the
defining equation of π1 .
Using the projections we can describe the Skolem translations from Fact 10.3.2
concisely as terms.
λF . (λx.π1 (F x), λx.π2 (F x)) : (∀x Σy. pxy) → (Σf . ∀x. px(f x))
λax. (π1 ax, π2 ax) : (Σf . ∀x. px(f x)) → (∀x Σy. pxy)
We emphasize that in practice one would establish the Skolem translations in tactic
mode (see Fact 10.3.2). Still writing the term descriptions using a proof assistant is
fun and demonstrates the smooth working of implicit argument inference and type
conversion.
124
10.4 Projections
Exercise 10.4.3 Construct the eliminator for sigma types as a certifying function
using the projections and the eta law (Fact 10.4.1).
Exercise 10.4.4 Express the projections π1 and π2 for sigma types with terms t1
and t2 using the eliminator EΣ such that π1 ≈ t1 and π2 ≈ t2 .
125
10 Certifying Functions and Sigma Types
E xy = E x ′ y ′ → x = x ′
E xy = E xy ′ → y = y ′
are both provable. While the first law is easy to prove, the second law cannot be
shown in general in computational type theory. This certainly conflicts with in-
tuitions that worked well so far. The problem is with subtleties of dependent type
checking and conversion. In Chapter 29, we will show that the second injectivity law
holds if we assume proof irrelevance, or if the type of the first component discrete.
a) Prove the first injectivity law.
b) Try to prove the second injectivity law. If you think you have found a proof on
paper, check it with Coq to find out where it breaks. The obvious proof idea that
rewrites π2 (Exy) to π2 (Exy ′ ) does not work since there is no well-typed rewrite
predicate validating the rewrite.
10.5 Truncation
126
10.5 Truncation
Exercise 10.5.3 Define a simply typed match function for inhabitation types and
prove Facts 10.5.1 and 10.5.2. Moreover, prove ∀P P . □(P ) ←→ P .
Exercise 10.5.4 Prove the following truncation laws for all types X:
a) X → □X
b) □X → (X → □Y ) → □Y
c) ¬□X ←→ ¬X
d) □X → ¬¬X
e) XM → □X ∨ ¬X
You will show that there are translations between ∀XY T . choice X Y and
□(∀X T . witness X). The translation from choice to witness needs to navigate
cleverly around the propositional discrimination restriction. The presence of the
inhabitation operator is essential for this direction.
a) Prove □(∀X T . witness X) → (∀XY T . choice X Y ).
b) Prove (∀XY T . choice X Y ) → □(∀X T . witness X).
c) Convince yourself that the equivalence
is not provable since the two directions require different universe levels for X
and Y .
Hints. For (a) use f := λx. π1 (W Y (px)(F x)) where W is the witness operator and F
is the assumption from the choice operator. For (b) use the choice operator with
the predicate λa Σ(X,p). ex p . λb Σ(X,p). sig p . π1 a = π1 b where p X→P . Keeping the argu-
ments of the predicate abstract makes it possible to obtain the choice function f
before the inhabitation operator is removed. The proof idea is taken from the Coq
library ChoiceFacts.
127
10 Certifying Functions and Sigma Types
10.6 Notes
Sum and sigma types may be seen as computational variants of disjunctions and
existential quantifications. While sum types provide disjoint unions of types, sigma
types are dependent pair types where the type of the second component depends
on the first component (similar to a dependent function type where the type of the
result depends on the argument). With sum and sigma types we can write function
types specifying an input-output relation. Using such informative function types,
we can construct functions together with their correctness proofs, which often is
superior to a separate construction of the function and the correctness proof. We
speak of certifying functions if the type of the function includes the relational speci-
fication of the function. It turns out that the abstract techniques for proof construc-
tion (e.g., induction) apply to the construction of certifying functions starting from
their types, thus eliminating the need to start with defining equations. Certifying
functions are an essential feature of constructive type theories having no equivalent
in set-theoretic mathematics. With informative types we can describe computational
situations often lacking adequate descriptions in set-theoretic language.
Mathematics comes with a rich language for describing proofs. Using this lan-
guage, we can write informal proofs for human readers that can be elaborated into
formal proofs when needed. The tactic level of the Coq proof assistant provides an
abstraction layer for the elaboration of informal proofs making it possible to dele-
gate to the proof assistant the type-theoretic details coming with formal proofs.
It turns out that the idea of informal proof extends to the construction of
certifying functions, which are functions whose type encompasses an input-output
relation. The proof-style construction of certifying functions turns out to be benefi-
cial in practice. It comes for free in a proof assistant since the tactic level addresses
types in general, not just propositional types. The proof-style construction of certi-
fying functions is guided by the specifying type and uses high-level building blocks
like induction. Often, one shows a for-all-exists lemma ∀x X Σy Y .pxy and then
extracts a function f X→Y and a correctness lemma ∀x. px(f x).
Most propositions have functional readings. Once we describe propositions as
computational types using sum and sigma types, their proofs become certifying
functions that may be used in computational contexts. Certifying functions carry
their specifications in their types and may be seen as computational lemmas. Like
propositional lemmas, certifying functions are best described with high-level proof
outlines, which may be translated into formal proof terms using the tactic inter-
preter of a proof assistant.
Product, sum, and sigma types are obtained as inductive types. In contrast to
the propositional variants, where simply typed eliminators suffice, constructions
involving product, sum, and sigma types often require dependently typed elimina-
128
10.6 Notes
tors. Existential quantifications and sigma types are distinguished from the other
inductive types we have encountered so far in that their value constructors model
a dependency between witness and certificate using a type function.
Computational type theory originated with Martin-Löf type theory [23]. In con-
trast to the type theory we are working in, Martin-Löf type theory does not have a
special universe for propositions but accommodates propositions as ordinary types.
So there is no propositional discrimination restriction and there are no proposi-
tional variants of product, sum and sigma types. This simplicity comes at the price
that assumptions like excluded middle can only be formulated for all types, which
is not meaningful (but consistant). Having an impredicative universe of proposi-
tions is a key feature of the computational type theory underlying the Coq proof
assistant [9].
129
11 More Computational Types
The main tool for relating types are injections and bijections. We consider injections
and bijections that come with inverse functions. For the types of injections and
bijections dependent tuple types are needed. We shall use customised inductive
types for injection and bijection types. We show Cantor’s theorem for injections.
We also show that sigma types can represent sum types and product types up to
bijection.
We discuss option types O(X), which extend a type X with an extra element.
Option types O(X) may be defined as sum types X + 1, but we shall use a dedicated
inductive type constructor. For option types, we construct a function that from a
bijection between O(X) and O(Y ) obtains a bijection between X and Y . For the
construction a clever use of a certifying function is essential.
We then discuss two prominent type families Nn and Vn X known as numeral
types and vector types. We obtain both families by structural recursion on the in-
dex n using option types and product types. Numeral types are finite types On ⊥
obtained by applying the option type constructor to void. Vector types are sequence
types (λY .X×Y )n 1 obtained by nesting pair types starting with unit. Both construc-
tions make full use of type conversion.
We remark that the basic notions discussed in this chapter (injections, bijections,
numerals, vectors) are computational refinements of notions that have been studied
in set theory for a long time.
inv : ∀XY T . (X → Y ) → (Y → X) → P
invXY gf := ∀x. g(f x) = x
131
11 More Computational Types
Proof Exercise. ■
Fact 11.1.4 (Cantor) I(X → B)X → ⊥. That is, X → B does not embed into X.
Proof Follows from Fact 6.3.4 since the inverse function of the embedding function
is surjective. ■
A bijection is a symmetric injection where the two functions invert each other in
either direction. We define bijection types
132
11.1 Injections and Bijections
Speaking informally, a bijection between X and Y says that X and Y are renamed
versions of each other. This is not necessarily the case for an injection IXY where
the target type Y may have elements not appearing as images of elements of the
source type X.
Fact 11.1.9 (Sigma types can express product and sum types)
Sigma types can express product types X × Y and sum types X + Y up to bijection.
1. X × Y and sig (λx X .Y ) are in bijection.
2. X + Y and sig (λbB . if b then X else Y ) are in bijection.
Proof The functions for the bijections can be defined with the simply typed match
function for sigma types. The proofs of the roundtrip equations, however, require
the dependently typed eliminator (with one exception). ■
Exercise 11.1.10 Prove the claims of all facts stated without proofs.
Exercise 11.1.11 Give a function f N→N that has two non-agreeing inverse functions.
133
11 More Computational Types
Exercise 11.1.14 Show that the following types are in bijection using bijection
types.
a) B and ⊤ + ⊤.
b) X × Y and Y × X.
c) X + Y and Y + X.
d) X and X × ⊤.
Exercise 11.1.15
Show that BXY and Σ f X→Y Σ g Y →X . inv gf ∧ inv f g are in bijection:
a) BXY a Σ f X→Y Σ g Y →X . inv gf ∧ inv f g.
b) B (BXY ) (Σ g Y →X . inv gf ∧ inv f g).
Exercise 11.1.16 Prove B N B → ⊥.
Exercise 11.1.17
Prove that bijections transport equality deciders: BXY → E(X) → E(Y ).
Given a type X, we may see the sum type X + 1 as a type extending X with an
additional element. Such one-element extensions are often useful and can be ac-
commodated with dedicated inductive types called option types:
O(X : T) : T ::= ◦X |
O: T→T
◦
: ∀X T . X → O(X)
: ∀X T . O(X)
134
11.2 Option Types
135
11 More Computational Types
Exercise 11.2.11 Prove the bijection theorem with the proof assistant not looking
at the code we provide. Formulate a lemma providing for the two symmetric cases
in the proof of Theorem 11.2.4.
N0 := ⊥
NSn := O(Nn )
We can now give a formal definition of what it means that a type has n elements.
We would like to prove that a type X has at most one cardinality. Suppose X is
in bijection with Nm and Nn . Then Nm and Nn are in bijection.
136
11.4 Vector Types
Vectors are sequences ⟨x1 , . . . , xn ⟩ of values from a common base type. We define
vector types Vn X recursively:
V0 X := 1
VSn X := X × Vn X
1
The nested pair representation is well-known from lists. It appeared in set theory where it us used
to represent tuples of arbitrary length.
137
11 More Computational Types
Vector types can be seen as refinement of list types where in addition to the
element type the length is prescribed. We define a dedicated constant nil for the
empty vector and list-style operations cons, hd, and tl:
Note that we have defined hd and tl as inductive functions and that conversion is
needed for type checking the patterns of the defining equations. We remark that
for list types hd cannot be defined as a total function since we don’t have a default
value for the empty list. We will treat X and n as implicit arguments.
enum : N → ∀n. Vn N
enum k 0 := nil
enum k (Sn) := cons k (enum (Sk) n)
Exercise 11.4.3 (Tuple types) Tuple types generalize vector types in that they de-
termine their component types with a type function N → T:
tup : (N → T) → N → T
tup p 0 := I
tup p (Sn) := pn × tup p n
138
11.4 Vector Types
We may number the positions of a vector from left to right starting with 0. We define
a function that given a vector and a position returns the element at the position:
Note that the primary discrimination is on n. Also note that the type of sub ensures
that in case the given position k is too large the last element of the vector can be
returned.
Note that the numeral type Nn has exactly as many elements as the vectors of
type Vn have positions. Using numerals, we can define a safe position element map
always returning the element at the given position:
sub′ : ∀Xn. Vn X → Nn → X
sub′ X 0 va := E⊥ Xa
sub′ X (Sn) v ◦a := sub′ Xn (tl v) a
sub′ X (Sn) v := hd v
Note the use of the eliminator for void in the contradictory case handled by the first
defining equation.
Exercise 11.4.4 We treat X and n as implicit arguments of sub and sub′ . Prove the
following equations. For each equation, first determine the most general type for v.
sub v 0 = hd v sub′ v = hd v
sub v 1 = hd(tl v) sub′ v ◦ = hd(tl v)
Exercise 11.4.5
a) Define a function last : ∀Xn. VSn X → X returning the element at the last
position of a vector.
b) Prove ∀v VSn X . sub vn = last v.
c) Define a function snoc : ∀Xn. Vn → X → VSn appending an element at the end
of a vector.
d) Prove last(snoc vx) = x.
e) Define a function rev : ∀Xn. Vn X → Vn X reversing a vector.
f) Prove rev (rev v) = v.
139
11 More Computational Types
11.4.2 Concatenation
(u +
+ v) +
+ w : V(n+m)+k X
u+ + w) : Vn+(m+k) X
+(v +
have incompatible types (that is, nonconvertible types) although they yield the same
value. Thus the associativity law for vector concatenation cannot be expressed di-
rectly. The problem goes away if m, n, and k are concrete numbers rather than
variables.
The type checking problem can be bypassed with a dedicated cast function:
The formulation and proof of the associativity law for vector concatenation come
with massive type conversion and with massive elaboration of implicit arguments.
This is quite feasible with a proof assistant but too tedious to be done with pen and
paper.
(enum 0 3 +
+ enum 3 3) +
+ enum 6 3 = enum 0 3 +
+(enum 3 3 +
+ enum 6 3)
140
12 Linear Arithmetic
Numbers 0, 1, 2, . . . are the basic recursive data structure. Starting from the induc-
tive definition of numbers, we study the algebraic properties of addition and trun-
cating subtraction. Comparisons x ≤ y will be obtained as equations x − y = 0.
The system obtained with addition and subtraction of numbers is called linear
arithmetic. Proof assistants come with automatic provers for linear arithmetic free-
ing users from knowing the basic lemmas for numbers. Linear arithmetic provers
realize an abstraction level that is commonly used in informal proofs.
We also define multiplication and prove its basic algebraic properties.
Studying linear arithmetic in computational type theory starting from first prin-
ciples is fun. As always, the thrill is in finding the right definitions and the right
theorems in the right order. There is beauty and elegance in the development pre-
sented here.
N ::= 0 | S(N)
N : T, 0 : N, S:N→N
Based on the inductive type definition, we can define inductive functions. A basic
inductive function is an eliminator providing for inductive proofs:
A discussion of the eliminator appears in §5.7. Matches for numbers can be ob-
tained as applications of the eliminator where no use of the inductive hypothesis
is made. Or more directly, a specialized elimination function for matches omitting
the inductive hypothesis can be defined.
We shall often use the if-then-else notation for zero testing:
141
12 Linear Arithmetic
Proof The results follow with matches, rewriting, and conversion. The proofs are
discussed in § 4.3. ■
12.2 Addition
Proof By induction on x. ■
Proof By induction on x. ■
142
12.3 Subtraction
12.3 Subtraction
− : N→N→N
0 − y := 0
Sx − 0 := Sx
Sx − Sy := x − y
The recursion is on the first argument. The successor case is realized with a sec-
ondary discrimination on the second argument.
Fact 12.3.1 x − 0 = x.
Fact 12.3.2
1. x + y − x = y
2. x − (y + z) = x − y − z
Corollary 12.3.3
1. x − x = 0
2. x − (x + y) = 0
12.4 Comparisons
x ≤ y := (x − y = 0)
x < y := Sx ≤ y
x ≥ y := y ≤ x
x > y := y < x
143
12 Linear Arithmetic
Fact 12.4.2
1. x ≤ x + y
2. x ≤ x
3. x ≤ Sx
4. x − y ≤ x
Proof (1) follows with Fact 12.3.3(2). (2) and (3) follow from (1). (4) follows with
Fact 12.3.2(2) and (1). ■
Proof For (1), replace z and then y in the claim with the additive characterization
for the assumptions. The reduced claim follows with Fact 12.4.2(1).
(2) is an instance of (1), and (3) is an instance of (1) modulo conversion. ■
Proof (1) follows by the disjointness constructor law. (2) follows by Fact 12.3.2(1).
(3) follows from (2). ■
144
12.4 Comparisons
145
12 Linear Arithmetic
Fact 12.5.1 Predicates that have an arithmetic tester have a certifying decider.
Proof Trivial. ■
Proof All three propositions follow by discrimination on the guard of the condi-
tional. For the third proposition we need memorizing discrimination. In the zero
case, zero propagation (Fact 12.2.3) gives us x ≤ y and y ≤ x, so the claim follows
with antisymmetry. In the successor case, we assume x = y and obtain a contradic-
tion with constructor disjointness since (x − x) + (x − x) = 0 with Fact 12.3.3(1). ■
Exercise 12.5.5 Define a boolean decider for x ≤ y and prove its correctness.
146
12.6 Linear Arithmetic Prover
Proof assistants come with terminating provers that for linear arithmetic propo-
sitions construct a proof whenever the proposition is provable. Linear arithmetic
propositions are obtained with =, ≤, ⊥, →, ∧, and ∨ from linear arithmetic ex-
pressions obtained with +, −, and S from numbers and variables. In Coq a linear
arithmetic prover is available through the automation tactic lia (linear integer arith-
metic). A linear arithmetic prover frees the user from knowing the lemmas for linear
arithmetic, a service making a dramatic difference with more involved proofs where
the details of linear arithmetic would be overwhelming. A linear arithmetic prover
will for instance find a proof for ¬(x > y) ←→ ¬(x ≥ y) ∨ ¬(x ≠ y).
From now on we will write proofs involving numbers assuming the abstraction
level provided by linear arithmetic.
Coq defines comparisons x ≤ y with an inductive type constructor, a definition
that is quite different from our definition. The difference doesn’t matter if compar-
isons are handled with lia, and we will do this from now on.
Coq’s automation tactic lia cannot do type sums. Still, a certifying decider like
∀xy. (x ≤ y) + (y < x) can be constructed with a single memorizing arithmetic
discrimination and linear arithmetic. The trick is matching on the number x − y,
which determines the result decision. The certificates are then obtained with the
linear arithmetic prover. Similar ideas work for the other deciders in § 12.5.
Exercise 12.6.1 Define the certifying deciders in § 12.5 in Coq using in each case a
single memorizing arithmetic discrimination and the linear arithmetic prover lia.
12.7 Multiplication
· :N→N→N
0 · y := 0
Sx · y := y + x · y
0·y =0 1·y =y +0 2 · y = y + (y + 0)
147
12 Linear Arithmetic
Proof Both equations follow by induction on x and linear arithmetic. For the equa-
tion with subtraction discrimination on y is needed in the successor case. Hence y
must be quantified in the inductive hypothesis. ■
Proof By induction on x. The successor case follows with distributivity over “+”. ■
12.8 Notes
The inclined reader may compare the computational development of linear arith-
metic given here with Landau’s [22] classical set-theoretic development from 1929.
148
13 Least Witness Operators
A least witness operator (LWO) decides for a decidable predicate p N→P and a
bound n whether p is satisfied by some k ≤ n. In this is the case, the operator
returns the least such k called the least witness of p.
The most principled way to obtain an LWO is, as usual, to construct it as a certi-
fying function in tactic mode. Based on the certifying LWO, we construct certifying
deciders for finite quantifications ∀k<n. pk and ∃k<n. pk.
We also present and verify a number of simply typed reducible least witness
functions. The correctness proofs for these functions provide us the with opportu-
nity to demonstrate basic program verification techniques in the context of compu-
tational type theory.
We define divisibility and primality of numbers with finite quantification and
obtain deciders for the corresponding predicates with the general deciders for finite
quantification.
On the foundational side, and related to LWOs, we prove that satisfiable pred-
icates on numbers have least witnesses if and only if the law of excluded middle
holds.
In this chapter, p will denote a predicate N → P and n and k will denote numbers.
We will use the notations (with ≤)
149
13 Least Witness Operators
Proof (1) follows with linear arithmetic. For (2), we have the assumption k < Sn.
Thus k < n or k = n. Both cases are straightforward. ■
A least witness operator (LWO) decides for a decidable predicate p N→P and a
bound n whether p is satisfied by some k < n. In the positive case the opera-
tor yields the least such k (that is, the least witness of p). In the negative case the
operator yields a certificate that there is no witness below n.
Proof By induction on n using the propagation rules for safety (Fact 13.1.2).
The zero case is trivial since we have safe p 0. In the successor case we have
(Σk<n. least pk) + safe pn and need
The case Σk<n. least pk is straightforward. Otherwise, we have safe pn. If pn, we
have least pk and the claim follows. Otherwise, we have ¬pn and hence safe p(Sn)
by the safety upgrade rule (Fact 13.1.2(2)). ■
150
13.3 Decidability Results
The existential least witness operator is obviously extensional; that is, the wit-
ness computed does not depend on the decider and does not change if we switch
to an equivalent predicate.
Proof The construction of the decider (1) is straightforward with the LWO from
Fact 13.2.1. The construction of (2) is straightforward with the decider (1) ■
151
13 Least Witness Operators
Proof (1) is straightforward and holds for all quantifications. This is also true for
the direction “←” of (2). In contrast, the direction “→” of (2) depends on the decid-
ability of p. If follows with the certifying LWO from Fact 13.2.1 instantiated with
λk.¬pk and exploits the double negation law for p. ■
Proof (1) follows with the decider for the safeness predicate (Fact 13.3.1) instanti-
ated with λk.¬pk exploiting the double negation law for p.
(2) follows with the certifying LWO from Fact 13.2.1 and the de Morgan law for
negated finite existential quantification (Fact 13.3.2(1)). ■
Exercise 13.3.4 Obtain deciders for finite quantification with “≤” using the deciders
for “<” instantiated with Sn.
Exercise 13.3.5 (Decidability of Primality)
We define divisibility and primality using finite quantification:
n | x := x ≠ 0 → n ≠ 1 → n ≠ x → ∃k<x. x = k · n
prime x := x ≥ 2 ∧ ∀k<x. k | x → k = 1
The deciders and LWOs obtained so far are not reducible since they are derived
from the abstract certifying LWO provided by Fact 13.2.1. We can obtain a reducible
152
13.4 Reducible LWOs
LWO by defining a simply typed recursive function following the construction of the
certifying LWO.
We assume a predicate p N→P with a boolean decider p̂ N→B such that
G : N → O(N)
G 0 :=
G (Sn) := match Gn [ ◦k ⇒ ◦k | ⇒ if p̂n then ◦n else ]
ϕ : N → O(N) → P
ϕn := safe pn
ϕn ◦k := least pk ∧ k < n
Proof By induction on n. In the zero case, the claim reduces to safe p0, which holds
by Fact 13.1.2(1). In the successor case, we have the inductive hypothesis ϕn(Gn)
and the claim ϕ(Sn)(G(Sn)). We discriminate on Gn. If Gn = ◦k, the inductive
hypothesis simplifies to k < n ∧ least pk and the claim to k < Sn ∧ least pk. The
claim follows.
If Gn = , the inductive hypothesis is safe pn. We discriminate on p̂n in the claim.
If pn, the claim reduces to n < Sn ∧ least pn and follows. If ¬pn, the claim reduces
to safe p(Sn) and follows with Fact 13.1.2(2). ■
L : N → N → O(N)
L 0 m :=
L (Sn) m := if p̂m then ◦m else L n (Sm)
153
13 Least Witness Operators
Proof By induction on n with m quantified. The zero case is trivial. In the successor
case we discriminate on p̂m. If pm, we have the trivial proof obligation
which reduces to an instance of Fact 13.1.2(2) using the inductive hypothesis for Sm
and rewriting with Sn + m = n + Sm. ■
Proof Given the correctness theorems for G and L (Facts 13.4.1 and 13.4.3), the
claim follows with the uniqueness of ϕn which follows with the uniqueness of
least p after discrimination on the option arguments. ■
The proofs we have just seen for L are typical for program verifications. While
the underlying ideas are clear, the detailed execution of the proofs is tedious and
calls for the help of a proof assistant.
Exercise 13.4.5 (Invariant Puzzle) We have one more reducible LWO in the offer.
Consider the function
W :N→N
W 0 := 0
W (Sn) := let k = W n in if p̂k then k else Sn
154
13.5 Least Witness Existence and Excluded Middle
It turns out that the existence of least witnesses for satisfiable predicates on num-
bers is equivalent to the law of excluded middle:
Proof Assume XM. Then every predicate is logically decidable: ∀n. pn ∨ ¬pn.
Hence we can carry out the constructions of Facts 13.2.1 and 13.2.4 at the proposi-
tional level:
Proof We pick a proposition P and prove P ∨¬P . Using the assumption, we discrim-
inate on the least witness n of the satisfiable predicate pn := if n then P else ⊤.
We have p0 ←→ P . Thus if the least witness is 0, we have P , and otherwise ¬P . ■
155
14 EWOs
The first point to notice about the definition of T is that there is recursion through
the right-hand side of the function type serving as argument type of the single proof
constructor C. Such higher-order structural recursions are legal in computational
type theory. The second point to notice about the definition of T is that T is a com-
putational predicate (§ 5.2) since the type ¬pn → T p(Sn) of the proper argument
1
The letter T derives from the intuition that propositions obtained with the computational T provide
for a transfer from the propositional level to the computational level.
157
14 EWOs
We will construct an EWO for N based on an abstract interface that we realize with
the inductive predicate T . The interface provides an abstract computational predi-
cate T ′ with two constructors.
158
14.2 EWO for Numbers
T′ : N → P
I : ∀n. pn → T ′ n
D : ∀n. T ′ (Sn) → T ′ n
E ′ : ∀qN→T . dec p → (∀n. pn → qn) → (∀n. q(Sn) → qn) → (∀n. T ′ n → qn)
Note that the abstract constructors I and D and the abstract eliminator E ′ make
the predicate T ′ appear as a computational inductive predicate obtained with two
first-order constructors. A proof of T ′ pn verifies that there is k ≥ n such that pk.
Fact 14.2.2 (EWO for numbers)
There is a function ∀p N→P . dec p → ex p → sig p.
Proof We assume d : dec p, H : ex p, and the functions from Fact 14.2.1. We obtain
sig p with E ′ instantiated with qn := sig p, d, and n := 0. This results in three
proof obligations:
1. ∀n. pn → sigp
2. ∀n. sigp → sigp
3. T ′ 0.
Obligations (1) and (2) are trivial. For (3) we note that T ′ 0 is a proposition. Thus we
can unpack H and obtain pn and T ′ n using I. We now close the proof by proving
∀n. T ′ n → T ′ 0 by induction on n using D. ■
Exercise 14.2.4 (Empty search type) With linear search types we can express an
empty propositional type allowing for computational elimination:
V : P := T (λn.⊥)0
Define a function V → ∀X T . X.
159
14 EWOs
V : P ::= C(⊤ → V )
B : T
C : (B → ⊥) → B
E : ∀Z. B → ((B → ⊥) → Z) → B → Z
Proof The EWO for N was obtained with Fact 14.2.2. For the other three types
computational falsity elimination is essential. For ⊥ an EWO is trivial since it is
given an element of ⊥. For ⊤ and B we can check p for all elements and obtain a
contradiction if p holds for no element. ■
Proof Use the EWO for X with the predicate λ[Link] ∨ qx. ■
160
14.3 General EWOs
Next we show that all numeral types On ⊥ have EWOs. The key insight for this
result is that EWOs transport from X to O(X). It turns out that EWOs also transport
backwards from O(X) to X.
λa. match a [ ◦x ⇒ px | ⇒ ⊥ ]
is a decidable and satisfiable predicate on O(X). The EWO for O(X) gives us ◦x such
that px.
For the other direction, suppose p is a decidable and satisfiable predicate
on O(X). Then λx.p(◦x) is a decidable and satisfiable predicate on X. The EWO
for X gives x with p(◦x). ■
Proof Let invXY f g. To show that there is an EWO for X, we assume a decidable and
satisfiable predicate p X→P . Then λy.p(gy) is a decidable and satisfiable predicate
on Y . The EWO for Y now gives us a y such that p(gy). ■
Fact 14.3.6 Every type that embeds into N has an EWO: IXN → EWO X.
Proof Follows with Fact 14.3.6 since N × N and N are in bijection (Fact 11.1.8). ■
Proof Follows with the EWO for N × N and the predicate λa. p(π1 a)(π2 a). ■
161
14 EWOs
a) Define an EWO for boolean deciders (i) using the EWO for numbers, and (ii) from
scratch using customized linear search types.
b) Using an EWO for boolean deciders, define an EWO for decidable predicates on
numbers.
c) Using an EWO for boolean deciders, define an EWO
Proof It suffices to construct a function ∀y. Σx. f x = y. We fix y and use the EWO
for X to obtain x with f x = y. This works since f is surjective and equality on Y
is decidable. ■
Proof Fact 14.4.1 gives us g with inv f g. Now inv gf follows since f is injective
(Fact 11.1.1). ■
Proof We prove D(x = y) for fixed x, y : X. Using the EWO for numbers we
obtain n such that f xxn = true. If f xyn = true, we have x = y. If f xyn = false,
we have x ≠ y. ■
162
14.5 Notes
14.5 Notes
With linear search types we have seen an inductive predicate going beyond of the
inductive type definitions we have seen so far. The proof constructor of linear
search types employs higher-order structural recursion through the right-hand side
of a function type. Higher-order structural recursion greatly extends the power
of structural recursion. Higher-order structural recursion means that an argument
of a recursive constructor is a function that yields a structurally smaller value for
every argument. That higher-order structural recursion terminates is a basic design
feature of computational type theories.
163
15 Indexed Inductives
Indexed inductive type definitions are generalized inductive type definitions where
the target types of value constructors may instantiate nonparametric arguments
(called indices) of their type constructors. Indexed inductive type definitions pro-
vide for the formalization of derivation systems as they appear in the study of proof
systems, computational systems, and programming languages.
We discuss indexed inductives at the example of two derivation systems, one for
the reflexive transitive closure of relations, and one for arithmetic comparisons. We
also model numeral types and vector types as indexed inductive types and show
that the inductive formalizations are in bijection with non-inductive formalizations
employing arithmetic recursion.
It turns out that the constants for equality can be defined as indexed inductives,
and that the inductive definition adds properties to the Leibniz characterization of
equality that are of foundational interest.
We start with inductive equality since its indexed inductive definition is particularly
simple. Recall that we have accommodated equality with three constants (§ 4.2):
eq : ∀X T . X → X → P
Q : ∀X T ∀x X . eq X x x
R : ∀X T ∀xy X ∀p X→P . eq Xxy → px → py
eq (X : T, x : X) : X → P ::=
| Q : eq Xxx
Note that the target type of the constructor Q is eq X x x and not eq X x y. This
means that only X and x are parametric arguments of eq X x y, and that y is a
nonparametric argument. Following common speak we will refer to nonparametric
arguments of type constructors as indices. The extra freedom coming with index-
ical arguments of type constructors is that they can be freely instantiated in the
target types of value constructors.
165
15 Indexed Inductives
We can now obtain the rewriting constant R with an inductive function definition:
The definition discriminates on the inductive argument of type eq Xxy. Note that
the value constructor Q equates the variables x and y in the defining equation. Also
note that the pattern of the defining equation of R gives the argument y with an
underline. The accounts for the fact that the argument y of R is determined by the
discrimination with Q.
We have defined R with a general type function p X→T rather than just a predicate
p X→P . This is possible since the inductive predicate eq is computational since the
value Q has no proper argument.
There is a single but important restriction on the types of inductive functions
discriminating on arguments of indexed inductive types: The indexed argument
type must be given with variables in the index positions that don’t occur otherwise
in the argument type. We speak of the index condition. The index condition dis-
allows an inductive function discriminating on an argument of type eq Xxx, for
instance.
Inductive equality has a number of properties Leibniz equality doesn’t have, an
issue we will study in Chapter 29.
Rxy R ∗ yz
R ∗ xx R ∗ xz
The first rule says that for every x : X there is a path R ∗ xx. The second rule says
that there is a path R ∗ xz if there is an edge Rxy and a path R ∗ yz.
We formalize the notation R ∗ xy and the two derivation rules with an indexed
inductive predicate star defined as follows:
star (X : T, R : X → X → P, x : X) : X → P ::=
| Nil : star XRxx
| Cons : ∀yz. Rxy → star XRyz → star XRxz
We now write R ∗ for the relation star XR. Note that a proof of R ∗ xy with the
constructors Nil and Cons describes a path from x to y. We may see an inductive
166
15.2 Reflexive Transitive Closure
proposition R ∗ xy as the type of all paths from x to y. We remark that the inductive
formalization of R ∗ is minimal in that it doesn’t presuppose numbers or lists.
Considering the inductive predicate star XRxy from a formal point of view, the
arguments X and R are uniform parameters, x is a nonuniform parameter, and y
is an index. That y is an index is forced by the type of the constructor Nil.
Let p and p ′ be predicates X → X → P. We define inclusion of predicates as one
would expect:
p ⊆ p ′ := ∀xy. pxy → p ′ xy
Proof Let Rxy. We show R ∗ xy. We have R ∗ yy with Nil. Thus R ∗ xy with Cons. ■
We would like to prove that the relation R ∗ is transitive; that is, if there are paths
R ∗ xy and R ∗ yz, then there is a path R ∗ xz. We prove this fact by induction on the
first path R ∗ xy.
E : ∀X ∀R ∀p X→X→P .
(∀x. pxx) →
(∀xyz. Rxy → pyz → pxz) →
∀xy. R ∗ xy → pxy
will suffice. For the transitivity proof E can be used with the target predicate
pxy := R ∗ yz → R ∗ xz
167
15 Indexed Inductives
E : ∀X ∀R ∀p X→X→P .
(∀x. pxx) →
(∀xyz. Rxy → pyz → pxz) →
∀xy. R ∗ xy → pxy
E XRp e1 e2 x _ Nil := e1
E XRp e1 e2 x _ (Cons x ′ z r a) := e2 xx ′ z r (E XRp e1 e2 x ′ za)
The equation for Cons may look complicated but it is just a routine construction
following the types of E and Cons.
path : ∀X. (X → X → P) → N → X → X → P
path XR0xy := (x = y)
path XR(Sn)xy := (∃x ′ . Rxx ′ ∧ path XRnxy)
star′ (X : T, R : X → X → P, x : X) : X → P ::=
| Incl : ∀xy. Rxy → star′ XRxy
| Refl : star′ XRxx
| Trans : ∀xyz. → star′ XRxy → star′ XRyz → star′ XRxz
168
15.3 Inductive Comparisons
x≤y
x≤x x ≤ Sy
The rules are obviously correct. We will show that they are also complete in the
sense that they can derive every valid comparison.
We formalize the derivation system comprised by the two rules with an inductive
type definition:
le (x : N) : N → P ::=
| leE : le xx
| leS : ∀y. le xy → le x (Sy)
Proof By induction on y.
Let y = 0. The x = 0 and hence le xy with leE.
In the successor case we assume x ≤ Sy and show le x(Sy). If x = Sy, the claim
follows with leE. If x ≤ y, the inductive hypothesis gives us le xy. The claim
follows with leS. ■
The other direction is intuitively obvious since both derivation rules represent
valid facts about arithmetic comparisons. To do the proof formally, we need induc-
tion on derivations of comparisons le xy, which can be provided with an inductive
function
E : ∀x ∀p N→P .
pxx →
(∀y. py → p(Sy)) →
∀y. x ≤ y → py
E xpe1 e2 _ leE := e1
E xpe1 e2 _ (leS ya) := e2 y(E xpe1 e2 ya)
169
15 Indexed Inductives
Exercise 15.3.3 Give two derivation rules for comparisons x < y and show their
soundness and completeness.
Numeral types are a family of finite types providing a canonical finite type for every
cardinality. In § 11.3, we obtained numeral types recursively as On ⊥. Numeral types
may also be obtained as indexed inductive types:
fin : N → T ::=
| Old : ∀n. fin n → fin (Sn)
| New : ∀n. fin (Sn)
the constructors Old and New are in correspondence with the option type construc-
tor some and none. The definition suggests that fin0 is empty and that finSn has one
more element than finn . We prove this fact by establishing a bijection between finn
and Nn :
The definitions of the inductive functions f and g clarify how the numeral types
finn and Nn relate to each other. While the definition of f is straightforward, the
definition of g is quite involved. In the zero case void elimination E⊥ is needed. In
the successor case a secondary discrimination on options is needed. Note that in the
patterns of the equations defining f the first argument n is given as an underline,
as is required by the fact that n provides the index argument of finn in the type
of f .
170
15.4 Inductive Numeral Types
Proof By induction on a : finn . For the constructor Old we have the proof obligation
g(Sn)(f (Sn)(Old na)) = Old na, which simplifies to Old n(gn(f na)) = Old na,
which holds by the inductive hypothesis gn(f na) = a. For the constructor New
we have the proof obligation g(Sn)(f (Sn)(New n)) = New n, which simplifies to
New n = New n. ■
The proof does an induction on values of the inductive type finn . The induction
can be formalized with an eliminator function defined as follows:
finn →T
E : ∀p ∀n.
(∀na. pna → p(Sn)(Old na)) →
(∀n. p(Sn)(New n)) →
∀na. pna
Exercise 15.4.4 Use the bijection between inductive and recursive numeral types to
show that the inductive numeral types finn have equality deciders and EWOs.
Exercise 15.4.5 We want to prove that the inductive numeral type fin0 is empty.
Formally, we represent this statement with the proposition fin0 → ⊥.
a) Prove fin0 → ⊥ using the eliminator for inductive numeral types.
b) Prove fin0 → ⊥ using the bijection between inductive and recursive numeral
types.
Hint: For (a) prove the equivalent proposition ∀n. finn → n ≠ 0.
171
15 Indexed Inductives
vec (X : T) : N → T ::=
| Nil : vec X 0
| Cons : ∀n. X → vec Xn → vec X(Sn)
The constructors Nil and Cons give us the functions nil and cons we defined for the
recursive representation with products and unit.
We define a bijection between inductive and recursive vector types. For simplic-
ity we don’t write the base type X.
The proof does an induction on values of inductive vector types vecn . The in-
duction can be formalized with an eliminator function defined as follows:
vecn →T
E : ∀p ∀n.
p 0 Nil →
(∀nxa. pna → p(Sn)(Cons nxa)) →
∀na. pna
E pe1 e2 _ Nil := e1
E pe1 e2 _ (Cons nxa) := e2 nxa(E pe1 e2 na)
172
15.6 Post Correspondence Problem
Exercise 15.5.4 Use the bijection between inductive and recursive vector types to
show that inductive vector types have equality deciders and EWOs.
Many problems in computer science have elegant specifications using inductive rela-
tions. As an example we consider the Post correspondence problem (PCP), a promi-
nent undecidable problem providing a base for undecidability proofs. The problem
involves cards with an upper and a lower string. Given a list C of cards, one has to
decide whether there is a nonempty list D of cards in C (possibly containing dupli-
cates) such that the concatenation of all upper strings equals the concatenation of
all lower strings. For instance, assuming the binary alphabet {a, b}, the list
has no solution.
We formalize PCP over the binary alphabet B with an inductive predicate
Note that post CAB is derivable if there is a nonempty list D ⊆ C of cards such that
the concatenation of the upper strings of D is A and the concatenation of the lower
strings of D is B. Undecidability of PCP over a binary alphabet now means that there
is no computable function
Since Coq’s type theory can only define computable functions, we can conclude that
no function of type (15.1) is definable.
As it comes to the arguments of the inductive predicate post CAB, the defining
rules establish C as a uniform parameter and A and B as indices.
173
15 Indexed Inductives
Inductive type definitions with indices can be translated into inductive type defini-
tions without indices using equations in the premises. For instance, the initial rule
for reflexive transitive closure may be written as
x=y Rxx ′ R∗ x ′ y
R ∗ xy R ∗ xy
star (X : T, R : X → X → P, x : X, y : X) : P ::=
| Nil : x = y → star XRxy
| Cons : ∀x ′ . Rxx ′ → star XRx ′ y → star XRxy
∀p X→P . px → py
eq Xxy
eq (X : T, x : X, y : X) : P ::=
| L : (∀p X→P . px → py) → eq Xxy
Q : ∀X T ∀x X . eq X x x
R : ∀X T ∀xy X ∀p X→P . eq Xxy → px → py
What we cannot do, however, is define R with a type function p X→T . The only way to
obtain rewriting of types is to work with the indexed inductive equality predicate.
We remark that rewriting of types is not needed for the usual things we do in
computational type theory. We may thus say that indexed inductive types are a
convenience not essential for our purposes. The benefits of inductive equality only
show at an internal technical level we will explore in Chapter 29.
15.8 Notes
174
15.8 Notes
discriminating argument type must be given with variables not appearing otherwise
in the argument type.
Recall that the proof assistant Coq simulates inductive functions as plain func-
tions discriminating with native matches. There are situations called inversions in
the literature where a match for an indexed inductive type does not type check al-
though intuition says it should. An example appears as Exercise 15.4.5. The reason
for the mismatch with intuition is that the type of the inductive function explaining
the match would violate the index condition.
As it comes to expressivity added by indexed inductives, inductive equality gives
us something we didn’t have before (see Chapter 29). Numeral types and vector
types can be obtained elegantly with type functions recursing on numbers not in-
volving indexed inductives. There is a noninductive definition of a reflexive tran-
sitive closure predicate using recursion on the path length (Exercise 15.2.5), but
the representation with indexed inductives is simpler and doesn’t involve numbers,
which need to be erased anyway (with existential quantification) to state the transi-
tivity law (see the discussion for vectors in § 11.4.2).
Indexed inductives really pay when it comes to derivation systems as they
are common in proof theory and programming languages. Examples appear in
Chapters 24 (propositional deduction systems) and Chapter 26 (regular expression
matching).
Section § 20.6 gives an indexed inductive predicate characterizing greatest
comon divisors with three computation rules.
175
16 Extensionality
Computational type theory does not fully determine equality of functions, propo-
sitions, and proofs. The missing commitment can be added through extensionality
assumptions.
177
16 Extensionality
We can represent boolean functions f B→B as boolean pairs (f true, f false). Un-
der FE, the boolean function can be fully recovered from the pair.
Given FE and PE, predicates over a type X correspond exactly to sets whose elements
are taken from X. We may define membership as x ∈ p := px. In particular, we
obtain that two sets (represented as predicates) are equal if they have the same
elements (set extensionality). Moreover, we can define the usual set operations:
:= λx X .⊥ empty set
X
p ∩ q := λx .px ∧ qx intersection
X
p ∪ q := λx .px ∨ qx union
p − q := λx X .px ∧ ¬qx difference
178
16.3 Proof Irrelevance
unique (X T ) := ∀xy X . x = y
Exercise 16.3.7 Assume PI and p X→P . Prove ∀xy ∀ab. x = y → (x, a)p = (y, b)p .
179
16 Extensionality
16.4 Notes
180
17 Excluded Middle and Double Negation
One of the first laws of logic one learns in an introductory course on mathematics
is excluded middle saying that a proposition is either true or false. On the other
hand, computational type theory does not prove P ∨ ¬P for every proposition P .
It turns out that most results in computational mathematics can be formulated
such that they can be proved without assuming a law of excluded middle, and that
such a constructive account gives more insight than a naive account using excluded
middle. On the other hand, the law of excluded middle can be formulated with the
proposition
∀P P . P ∨ ¬P
and assuming it in computational type theory is consistent and meaningful.
In this chapter, we study several characterizations of excluded middle and the
special reasoning patterns provided by excluded middle. We show that these rea-
soning patterns are locally available for double negated claims without assuming
excluded middle.
XM := ∀P P . P ∨ ¬P
Computational type theory neither proves nor disproves XM. Thus it is interesting
to assume XM and study its consequences. This study becomes most revealing if
we assume XM only locally using implication.
There are several propositionally equivalent characterizations of excluded mid-
dle. Most amazing is may be Peirce’s law that formulates excluded middle with just
implication.
Fact 17.1.1 The following propositions are equivalent. That is, if we can prove one
of them, we can prove all of them.
1. ∀P P . P ∨ ¬P excluded middle
2. ∀P P . ¬¬P → P double negation
3. ∀P P QP . (¬P → ¬Q) → Q → P contraposition
4. ∀P P QP . ((P → Q) → P ) → P Peirce’s law
181
17 Excluded Middle and Double Negation
XM → (¬P → ⊥) → P
Proof Assume XM and p X→P . By XM we assume ¬∃x.¬px and prove ∀[Link]. By the
de Morgan law for existential quantification we have ∀x.¬¬px. The claim follows
since XM implies the double negation law.
Now assume the right hand side and let P be a proposition. We prove P ∨ ¬P . We
choose p := λa⊤ .P . By the right hand side and conversion we have either ∀a⊤ .P or
∃a⊤ .¬P . In each case the claim follows. Note that choosing an inhabited type for X
is essential. ■
Figure 17.1 shows prominent equivalences whose left-to-right directions are only
provable with XM. Note the de Morgan laws for conjunction and universal quantifi-
cation. Recall that the de Morgan laws for disjunction and existential quantification
¬(P ∨ Q) ←→ ¬P ∧ ¬Q de Morgan
¬(∃[Link]) ←→ ∀x. ¬px de Morgan
Exercise 17.1.3
a) Prove the right-to-left directions of the equivalences in Figure 17.1.
b) Prove the left-to-right directions of the equivalences in Figure 17.1 using XM.
182
17.1 Characterizations of Excluded Middle
¬(P ∧ Q) ←→ ¬P ∨ ¬Q de Morgan
¬(∀[Link]) ←→ ∃x. ¬px de Morgan
(¬P → ¬Q) ←→ (Q → P ) contraposition
(P → Q) ←→ ¬P ∨ Q classical implication
Exercise 17.1.4 Prove the following equivalences possibly using XM. In each case
find out which direction needs XM.
¬(∃x.¬px) ←→ ∀[Link]
¬(∃x.¬px) ←→ ¬¬∀[Link]
¬(∃x.¬px) ←→ ¬¬∀x.¬¬px
¬¬(∃[Link]) ←→ ¬∀x.¬px
Exercise 17.1.5 Make sure you can prove the de Morgan laws for disjunction and
existential quantification (not using XM).
Exercise 17.1.7 Explain why Peirce’s law and the double negation law are indepen-
dent in Coq’s type theory.
Exercise 17.1.8 Prove the equivalence of Peirce’s law and the double negation law
directly without going through other laws or disjunction. In each case only a single
instance of the other law is needed, which can be taken right at the beginning. Use
Coq’s tactic tauto to convince yourself that you have chosen the right instance.
Exercise 17.1.10 (Drinker Paradox) Consider a bar populated by at least one per-
son. Using excluded middle, one can argue that one can pick some person in the
bar such that everyone in the bar drinks Wodka if this person drinks Wodka.
We assume an inhabited type X representing the persons in the bar and a pred-
icate p X→P identifying the persons who drink Wodka. The job is now to prove the
proposition ∃x. px → ∀[Link]. Do the proof in detail and point out where XM and
183
17 Excluded Middle and Double Negation
inhabitation of X are needed. A nice proof can be done with the counterexample
law Fact 17.1.2.
An informal proof may proceed as follows. Either everyone in the bar is drinking
Whisky. Then we can pick any person for x. Otherwise, we pick a person for x not
drinking Whisky, making the implication vacuously true.
the drinker proposition implies excluded middle. The proof was found by Dominik
Kirst in March 2023 and goes as follows. We show P ∨ ¬P for some proposition P .
To do so, we instantiate the drinker proposition with the type X := O(P ∨ ¬P ) and
the predicate defined by p(◦_) := ¬P and p() := ⊤. We now obtain some a such
that pa → ∀[Link]. If a is obtained with some, a gives us a proof of the claim P ∨¬P .
Otherwise, we have ∀[Link] and prove ¬P . We assume P and obtain a contradiction
with the some case of p.
Kirst’s proof exploits that we are in a logical framework where proofs are values.
There is a paper [31] discussing the drinker paradox and suggesting it does not
imply excluded middle in a more conventional logical framework.
Given a proposition P , we call ¬¬P the double negation of P . It turns out that the
double negation of a quantifier-free proposition is provable even if the proposition
by itself is only provable with XM. For instance,
∀P P . ¬¬(P ∨ ¬P )
is provable. This metaproperty cannot be proved in Coq. However, for every in-
stance a proof can be given in Coq. Moreover, for concrete propositional proof
systems the translation of classical proofs into constructive proofs of the double
negated claim can be formalized and verified (Glivenko’s theorem 24.7.2).
There is a useful proof technique for working with double negation: If we have a
double negated assumption and need to derive a proof of falsity, we can drop the
184
17.2 Double Negation
double negation. The lemma behind this is an instance of the polymorphic identity
function:
¬¬P → (P → ⊥) → ⊥
With excluded middle, double negation distributes over all connectives and
quantifiers. Without excluded middle, we can still prove that double negation dis-
tributes over implication and conjunction.
Fact 17.2.1 The following distribution laws for double negation are provable:
Exercise 17.2.6 Make sure you can prove the double negations of the following
propositions:
P ∨ ¬P
¬¬P → P
¬(P ∧ Q) → ¬P ∨ ¬Q
(¬P → ¬Q) → Q → P
((P → Q) → P ) → P
(P → Q) → ¬P ∨ Q
(P → Q) ∨ (Q → P )
185
17 Excluded Middle and Double Negation
DNS is provable for finite types but unprovable in general. Prove the following:
a) ∀p B→P . (∀x. ¬¬px) → ¬¬∀x. px
b) ¬¬XM ←→ DNS
c) ∀X T ∀p X→P . ¬¬(∀x. px) → (∀x. ¬¬px)
Hint: Direction ← of (b) follows with λP .P ∨ ¬P .
definite P P := P ∨ ¬P
186
17.4 Stable Propositions
stable P P := ¬¬P → P
Stable propositions matter since there are proof rules providing classical reason-
ing for stable claims.
The rules say that when we prove a stable claim, we can assume for every propo-
sition P that it is definite or stable. Note that the second rule follows from the first
rule since definiteness implies stability.
187
17 Excluded Middle and Double Negation
Exercise 17.4.10 Prove the following classical reasoning rules for stable claims:
a) stable Q → (P → Q) → (¬P → Q) → Q.
b) stable Q → ¬(P1 ∧ P2 ) → (¬P1 ∨ ¬P2 → Q) → Q.
c) stable Q → (¬P1 → ¬P2 ) → ((P2 → P1 ) → Q) → Q.
P ∧c Q := (P → Q → ⊥) → ⊥ ¬(P → ¬Q)
P ∨c Q := (P → ⊥) → (Q → ⊥) → ⊥ ¬P → ¬¬Q
∃c [Link] := (∀x. px → ⊥) → ⊥ ¬(∀x. ¬px)
TVS := ∀P P . P = ⊤ ∨ P = ⊥
188
17.6 Notes
Proof We show TVS → PE. Let P ←→ Q. We apply TVS to P and Q. If they are both
assigned ⊥ or ⊤, we have P = Q. Otherwise we have ⊤ ←→ ⊥, which is contradictory.
The remaining implications TVS → XM and XM ∧ PE → TVS are also straightforward.■
Exercise 17.5.3 Prove TVS ←→ ∀p P→P . p⊤ → p⊥ → ∀[Link]. Note that the equiva-
lence characterizes TVS without using propositional equality.
17.6 Notes
Proof systems not building in excluded middle are called intuitionistic proof systems,
and proof systems building in excluded middle are called classical proof systems.
The proof system coming with computational type theory is clearly an intuitionistic
189
17 Excluded Middle and Double Negation
system. What we have seen in this chapter is that an intuitionistic proof system pro-
vides for a fine grained analysis of excluded middle. This is in contrast to a classical
proof system that by construction does not support the study of excluded middle.
It should be very clear from this chapter that an intuitionistic system provides for
classical reasoning (i.e., reasoning with excluded middle) while a classical system
does not provide for intuitionistic reasoning (i.e., reasoning without excluded mid-
dle).
Classical and intuitionistic proof systems have been studied for more than a
century. That intuitionistic reasoning is not made explicit in current introductory
teaching of mathematics may have social reasons tracing back to early advocates of
intuitionistic reasoning who argued against the use of excluded middle.
190
18 Provability
provable : P → P
With this trick provable (P ) and ¬provable (P ) are both propositions in compu-
tational type theory we can reason about. We define three standard notions for
propositions and the assumed provability predicate:
191
18 Provability
independent (P ) → consistent (P )
consistent (P ) → ¬disprovable (P )
provable (P ) → ¬independent (P )
To show more, we make the following assumptions about the assumed provability
predicate:
Since the provability predicate coming with computational type theory satisfies
these properties, we can expect that properties we can show for the assumed prov-
ability predicate also hold for the provability predicate coming with computational
type theory.
Proof Claim 1 follows with PMP. Claim 2 follows with PC and (1). ■
Exercise 18.1.3 Show that the functions λP P .P and λP P .⊤ are provability predi-
cates satisfying PMP, PI, PK, and PC.
Exercise 18.1.4 Let P → Q be provable. Show that P and Q are both independent
if P is consistent and Q is unprovable.
in addition to PMP, PI, PK, and PC. Prove ¬provable (⊥) ←→ ¬∀P P . provable (P ).
192
18.2 Consistency
18.2 Consistency
Proof 1 → 2. We assume provable (¬¬⊥) and show provable (⊥). By PMP it suffices
to show provable(¬⊥), which holds by PI.
2 → 3. Trivial.
3 → 1. Suppose P is consistent. We assume provable ⊥ and show provable (¬P ).
Follows by PK.
1 → 4. We assume that ⊥ is unprovable, P is provable, and ¬P is provable. By
PMP we have provable ⊥. Contradiction.
4 → 1. We assume that ⊥ is provable and derive a contradiction. By the primary
assumption it follows that ¬⊥ is unprovable. Contradiction since ¬⊥ is provable
by PI.
1 → 5. Follows with PMP.
5 → 1. Assume disprovable (⊥) → ¬provable (⊥). It suffices to show
disprovable(¬⊥), which follows with PI. ■
provable : prop → P
Show that all results of this chapter hold for such abstract proof systems.
Exercise 18.2.3 (Hilbert style assumptions) The assumptions PI, PK, and PC can be
obtained from the simpler assumptions
PK′ : ∀P Q. provable (P → Q → P )
PS : ∀P QZ. provable ((P → Q → Z) → (P → Q) → P → Z)
that will look familiar to people acquainted with propositional Hilbert systems.
Prove PK, PI, and PC from the two assumptions above. PK and PI are easy. PC
is difficult if you don’t know the technique. You may follow the proof tree
S(S(KS)(S(KK)I))(KH). Hint: PI follows with the proof tree SKK.
The exercise was prompted by ideas of Jianlin Li in July 2020.
193
Part II
More Basics
195
19 Arithmetic Recursion
We will establish complete induction as a certifying function with the above type.
Complete induction can be used to construct functions since p may be a type
function.
When we use complete induction to construct a proof, we will refer to the hy-
pothesis
∀y. y < x → py
as the step function. Moreover, we will refer to the certifying function for complete
induction as the complete induction operator.
Compared to structural induction
197
19 Arithmetic Recursion
complete induction introduces only one proof obligation. Moreover, the inductive
hypothesis of complete induction is stronger than the inductive hypothesis of struc-
tural induction in that it provide py for all y < x, not just the predecessor.
Computationally, complete induction says that when we compute a function f
for a number x, we can obtain f y for all y < x by recursion.
Constructing a complete induction operator with a structural induction operator
is straightforward. The trick is to replace the claim ∀[Link] with the equivalent claim
∀nx. x < n → px and do structural induction on the introduced upper bound n.
Definition 19.1.1 (Complete induction operator)
∀p N→T . (∀x. (∀y. y < x → py) → px) → ∀[Link].
∀nx. x < n → px
by structural induction on the upper bound n. For n = 0, the claim follows withe
computational falsity elimination since we have the hypothesis x < 0. In the suc-
cessor case, we assume x < Sn and prove px. We apply the step function F , which
gives us the assumption y < x and the claim py. By the inductive hypothesis it
suffices to show y < n, which follows by linear arithmetic ■
Fib : (N → N → N) → N → N
Fib f n := if n ≤ 1 then n else f (n − 2) + f (n − 1)
Ack : (N → N → N) → N → N → N
Ack f 0 y := Sy
Ack f (Sx) 0 := f x1
Ack f (Sx)(Sy) := f x(f (Sx) y)
is unique.
198
19.2 Size Induction
Exercise 19.1.4 (Double induction) Prove the following double induction principle
for numbers (from Smullyan and Fitting [27]):
∀p N→N→T .
(∀x. px0) →
(∀xy. pxy → pyx → px(Sy)) →
∀xy. pxy
There is a nice geometric intuition for the truth of the principle: See a pair (x, y) as
a point in the discrete plane spanned by N and convince yourself that the two rules
are enough to reach every point of the plane.
Hint: First do induction on y with x quantified. In the successor case, first apply
the second rule and then prove pxy by induction on x.
Size induction is a generalization of complete induction working for all types that
have a numeric size function:
∀X T ∀σ X→N ∀p X→T .
(∀x. (∀y. σ y < σ x → py) → px) →
∀x. px
With size induction, when we prove px, we can assume py for all y whose size
is smaller than the size of x. The construction of the size induction operator is a
straightforward adaption of the construction of the complete induction operator.
∀X T ∀σ X→N ∀p X→T .
(∀x. (∀y. σ y < σ x → py) → px) →
∀x. px
Proof We assume X, σ , p and the step function F : ∀x. (∀y. y < x → py) → px
and show ∀[Link]. We prove the equivalent claim
∀nx. σ x < n → px
by structural induction on the upper bound n. For n = 0, the claim follows withe
computational falsity elimination since we have the hypothesis x < 0. In the succes-
sor case, we assume σ x < Sn and prove px. We apply the step function F , which
gives us the assumption σ y < σ x and the claim py. By the inductive hypothesis it
suffices to show σ y < σ n, which follows by linear arithmetic ■
199
19 Arithmetic Recursion
There will be applications of size induction where the size function has two
arguments. We establish a binary size induction operator adapted to this situation.
Definition 19.2.2 (Binary size induction operator)
Proof Straightforward adaption of the construction of the unary size induction op-
erator (Fact 19.2.1). ■
Exercise 19.2.3 Assume a size induction operator and construct a binary size
induction operator and a structural induction operator not using structural induc-
tion.
The Euclidean quotient of two numbers x and Sy is the number of times Sy can be
subtracted from x without truncation. This computational characterization of the
Euclidean quotient can be formalized with a procedural specification
The computation captured by the equation terminates since the first argument is
decreased upon recursion. Thus if D is a function satisfying the equation, Dxy is
the Euclidean quotient of x and Sy for all numbers x and y. There are obvious
questions about the procedural specification of Euclidean division:
• Existence: Is there a function satisfying the specification?
• Uniqueness: Do all functions satisfying the specification agree?
• Formalization: How can the specification be expressed in type theory?
We will answer the first two questions positively. Existence will be shown with a
general technique called step indexing that applies to all procedural specifications
whose recursion is guarded by an arithmetic size function. Uniqueness will follow
with complete induction on the first argument x.
First we take care of the formalization of the procedural specification. We ex-
press the procedural specification with an unfolding function (§ 1.12)
∆ : (N → N) → N → N → N
∆ f xy := if x ≤ y then 0 else S(f (x − Sy)y)
f ≡ f′ ∀xy. f xy = f ′ xy
200
19.3 Euclidean Quotient
Exercise 19.3.2 Let D be a function satisfying the procedural specification for the
Euclidean quotients: D ≡ ∆D. Prove Dxy · Sy ≤ x < Dxy · Sy + Sy.
Exercise 19.3.4 We have shown some results about the procedural specification ∆
using complete induction. It turns out that the results can also be shown using
structural induction on an upper bound for the variable used for complete induc-
tion. For instance, if D satisfies Γ , one may prove
∀y N ∀p N→T .
(∀x ≤ y. px) →
(∀x > y. p(x − Sy) → px) →
∀x. px
201
19 Arithmetic Recursion
We now introduce a technique called step indexing providing for the direct con-
struction of functions satisfying procedural specifications. Step indexing works
whenever the termination of the procedural specification can be argued with an
arithmetic size function.
Suppose we have a procedural specification whose termination can be argued
with an arithmetic size function. Then we can define an helper function taking the
size (a number) as an additional argument called step index and arrange things such
that the recursion is structural recursion on the step index. We obtain the specified
function by using the helper function with a sufficiently large step index.
We demonstrate the technique with the procedural specification δ of Euclidean
quotients. The step-indexed helper function comes out as follows:
Div 0 x y := 0
Div (Sn) x y := ∆ (Div n) x y
The essential result about Div is index independence : Div nxy = Div n′ xy whenever
the step indices are large enough.
Proof By induction on n with n′ and x quantified. The base case has a contradictory
assumption. In the successor case, we destructure n′ . The case n′ = 0 has a
contradictory assumption. If n = Sn1 and n′ = Sn′1 , the claim reduces to
202
19.5 Greatest Common Divisor
The techniques we have seen for Euclidean quotients also work for GCDs (greatest
common divisors). Recall that the GCD of two numbers can be computed by re-
peated non-truncating subtraction. We formalize the algorithm with the procedural
specification
Γ : (N → N) → N → N → N
Γ f xy := if x then y else if x ≤ y then f x(y − x) else f yx
The recursion of the specification terminates since it decreases the binary size func-
tion σ xy := 2 · x + y.
Gcd 0 x y := 0
Gcd (Sn) x y := Γ (Gcd n) x y
Proof By induction on n with n′ , x, and y quantified. The base case has a contra-
dictory assumption. In the successor case, we destructure n′ . The case n′ = 0 has
a contradictory assumption. Let n = Sn1 and n′ = Sn′1 . Following Γ , we consider
three cases: (1) x = 0, (2) 0 < x ≤ y, and (3) y > x > 0. The base case follows by
computational equality, and the recursive cases follow with the inductive hypothe-
sis. ■
Proof Following Γ , we consider three cases: (1) x = 0, (2) 0 < x ≤ y, and (3)
y > x > 0. The base case follows by computational equality, and the recursive cases
follow with index independence (Lemma 19.5.2). ■
203
19 Arithmetic Recursion
Recall the definition of vector types in § 11.4 with a type function recursing on
numbers:
V:T→N→T
V X 0 := 1
V X (Sn) := X × V X n
Also recall that we are using the notation Vn X for V X n. The values of a vector
type Vn X represent sequences ⟨x1 , . . . , xn ⟩ whose elements are of type X. For in-
stance, we have
(1, (2, (3, I))) : V3 N ≈ N × (N × (N × 1))
Given a step function f : ∀n. Vn X → X, we can start from the empty vector and
recursively compute vectors (f nv, v) : VSn X. We speak of vector recursion and
formalize the computation scheme with a function
vecrec : ∀X T . (∀n. Vn X → X) → ∀n. Vn X
vecrec X f 0 := I
vecrec X f (Sn) := let v = vecrec X f n in (f nv, v)
Building on vector recursion, we define course-of-values recursion as follows:
covrec : ∀X T . (∀n. Vn X → X) → N → X
covrec X f n := π1 (vecrec X f (Sn))
An untyped form of course by value recursion appeared with the study of primitive
recursive functions in the 1920s.
Course-of-values recursion can be used to construct the functions representing
sequences like those of the square numbers, factorials, and Fibonacci numbers:
1, 4, 9, 16, 25, 36, . . .
1, 1, 2, 6, 24, 120, . . .
0, 1, 1, 2, 3, 5, . . .
204
19.6 Course-of-values Recursion
covrec N (λn_. n2 )
The function for the factorial sequence is obtained with the step function
facstep : ∀n. Vn N → N
facstep 0 _ := 1
facstep (Sn) v := Sn · π1 v
Note that type checking of the second defining equation requires conversion to
validate the application of the projection π1 . Finally the function for the Fibonacci
sequence is obtained with the step function.
fibstep : ∀n. Vn N → N
fibstep 0 _ := 0
fibstep 1 _ := 1
fibstep (SSn) v := π1 (π2 v) + π1 v
Informally, the step function says that the first two Fibonacci numbers are 0 and 1,
and that higher Fibonacci numbers are obtained as the sum of the two preceding
Fibonacci numbers.
We prove that the Fibonacci function obtained with course-of-values recursion
satisfies the standard specification of the Fibonacci sequence.
205
20 Euclidean Division
Suppose you have a chocolate bar of length x and you want to cut it in pieces of
length Sy. Then the number of pieces you can obtain is the Euclidean quotient of
x and Sy and the short piece possibly remaining is the Euclidean remainder of x
and Sy.
Mathematically speaking, the Euclidean quotient of x and Sy is the maximal
number a such that a · Sy ≤ x. Following this characterization, we define a
relational specification
δxyab := x = a · Sy + b ∧ b ≤ y
Given δxyab, we say that a is the quotient and b is the remainder of x and Sy.
For instance, we have 8 = 2 · 3 + 2 saying that 2 is the quotient and the remainder
of 8 and 3.
Note that we consider Euclidean division for x and Sy to avoid the division-by-
zero problem.
We will construct functions that for x and y compute a and b such that δxyab.
This tells us that Euclidean quotients and remainders always exist. We will also
show that quotients and remainders uniquely exist.
Given x and y, we compute a and b such that δxyab by structural recursion
on x. The zero case is trivial:
0 = 0 · Sy + 0
207
20 Euclidean Division
x = a · Sy + b and b ≤ y
Sx = a · Sy + Sb and Sb ≤ y
Sx = Sa · Sy + 0
Proof Let F be a function ∀xy. Σab. δxyab as provided by Fact 20.1.1. We define
the functions D and M as Dxy := π1 (F xy) and Mxy := π1 (π2 (F xy)). Now
π2 (π2 (F xy)) is a proof of δxy(Dxy)(Mxy) (up to conversion). ■
Note that Definition 20.1.2 provides D and M as abstract constants. The only
thing we know about the functions D and M is that they yield quotients and re-
mainders as asserted by the defining property ∀xy. δxy(Dxy)(Mxy). We know
have examples for simply typed abstract functions that are specified with a defining
property.
Exercise 20.1.5
Convince yourself that the following statements follow by linear arithmetic:
a) δxyab → b = x − a · Sy
b) Mxy = x − Dxy · Sy
208
20.2 Uniqueness of Quotient and Remainder
(b ≤ y) → (b′ ≤ y) → (a · Sy + b = a′ · Sy + b′ ) → a = a′
To obtain a solid proof, the above outline needs elaboration and verification.
This is best done with a proof assistant, where the reasoner for linear arithmetic
takes care of all arithmetic details.
The uniqueness of δ has important applications.
Proof Follows with linear arithmetic from uniqueness of δ (20.2.1) and the defining
property of D and M 20.1.2. ■
209
20 Euclidean Division
The rules justify an algorithm taking x and y as input and computing a and b
such that δxyab by repeated subtraction. The relational algorithm can be factor-
ized into two functional algorithms computing quotient and remainder separately.
The functional algorithms can be formalized with unfolding functions as follows:
DIV : (N → N) → N → N → N
DIV f xy := if x ≤ y then 0 else S(f (x − Sy)y)
MOD : (N → N) → N → N → N
MOD f xy := if x ≤ y then 0 else S(f (x − Sy)y)
210
20.4 Divisibility
Proof By the uniqueness of δ (Fact 20.2.1) and the definition of D and M (Defini-
tion 20.1.2) it suffices to show
Fact 20.3.2 is remarkable, both as it comes to the result and to the proof. It states
that the abstract functions D and M satisfy procedural specifications employing re-
peated subtraction. The proof of the result hinges on the uniqueness of the rela-
tional specifications δ, the correctness of repeated subtraction for δ (Fact 20.3.1),
and the definition of D and M.
Recall that we studied the procedural specifications DIV and Mod in § 19.3. In
particular we showed that both specifications are unique (Fact 19.3.1 and Exer-
cise 19.3.3). Thus we now know that all functions satisfying the specifications agree
with D and M.
Corollary 20.3.3 All functions satisfying DIV agree with D, and all functions satis-
fying MOD agree with M.
Exercise 20.3.4 Let F : ∀xy. Σab. δxyab and f xy := (π1 (F xy), π1 (π2 (F xy)).
a) Prove that f satisfies δxy(π1 (f xy))(π2 (f xy)).
b) Prove that f satisfies the equation
f xy = if x ≤ y then (0, x) else let (a, b) = f (x − Sy) y in (Sa, b).
Remark: Both proof are straightforward when done with a proof assistant. Checking
the details rigorously is annoyingly tedious if done by hand. The second proof best
follows the proof of Fact 20.3.2 using uniqueness (Fact 20.2.1) and the rules for
repeated subtraction (Fact 20.3.1). No induction is needed.
20.4 Divisibility
n | x := ∃k. x = k · n
211
20 Euclidean Division
Proof Claims 1–4 have straightforward proofs unfolding the definition of divisibil-
ity. Claim 6 follows from claim 5.
For claim 5, we consider y = 0 and y > 0. For y = 0, we obtain x = 0 by (4) with
n := Sx and (1). For y > 0, we obtain x ≤ y by (3) and (1). ■
The greatest common divisor of two numbers x and y is a number z such that the
divisors of z are exactly the common divisors of x and y. Following this character-
ization, we define a GCD predicate:
γxyz := ∀n. n | z ←→ n | x ∧ n | y
212
20.5 Greatest Common Divisors
Recall that Mxy subtracts Sy from x as long as the subtraction doesn’t truncate.
Thus we may use the remainder function to compute GCDs.
Fact 20.5.4 (Remainder rule) γ(Sx)(Myx)z → γ(Sx)yz.
To compute GCDs, we need two further rules for γ. The correctness of both
rules is obvious from the definition of δ.
Fact 20.5.5 (Symmetry rule) γyxz → γxyz.
Γ : (N → N → N) → N → N → N
Γ f 0 y := y
Γ f (Sx) y := f (Myx)(Sx)
The recursion terminates since the first argument is decreased. We use the algo-
rithm to show that GCDs exist.
Fact 20.5.7 (Existence) ∀xy. Σz. γxyz.
Exercise 20.5.10
Show that every function satisfying the procedural specification Γ agrees with gcd.
Exercise 20.5.11
Show gcd xy = if x then y else if x ≤ y then gcd x(y − x) else gcd yx.
213
20 Euclidean Division
We may phrase soundness as saying that G is the least gcd relation. The other
direction of the equivalence is more demanding.
Fact 20.6.3 (Completeness) For every functional gcd relation: pxyz → Gxyz.
Proof Let pxyz. By totality and soundness we have Gxyz′ and pxyz′ . Now z = z′
by functionality of p. Thus Gxyz′ . ■
214
20.7 Reducible Quotient and Remainder Functions
Assuming extensionality (PE and FE), our results say that G = γ and that γ is the
only functional gcd relation.
Exercise 20.6.6 Give and verify an inductive characterization of the Euclidian divi-
sion predicate λxyz. z · Sy ≤ x < Sz · Sy.
Exercise 20.6.7 Give and verify an inductive characterization of the Euclidian re-
mainder predicate.
We now define reducible functions Div xcy and Mod xcy for quotient and remain-
der by structural recursion on x. The auxiliary argument c will be decremented
upon recursion. We will have Dxy = Div xyy and Mxy = Mod xyy.
Definition 20.7.1
Div : N → N → N → N Mod : N → N → N → N
Div 0 c y := 0 Mod 0 c y := y − c
Div Sx 0 y := S(Div xyy) Mod Sx 0 y := Mod xyy
Div Sx Sc y := Div xcy Mod Sx Sc y := Mod xcy
The design of Div and Mod is explained with a correctness lemma relating the
functions with the specification δ.
215
20 Euclidean Division
Proof Follows with the uniqueness of δ, the defining property of D and M, and
Lemma 20.7.2 instantiated with c = y. ■
Exercise 20.7.4 (Remainder without subtraction) The first equation of Mod uses
subtraction. The use of subtraction can be eliminated with an additional argument d
acting as a counter initialized with 0 and being incremented such that c + d = y.
Verify the correctness of such a remainder function.
20.8 Notes
Informally, one may start a development of Euclidean division with separate recur-
sive functions for quotient and remainder applying subtraction. However, this is
not an option in a computational type theory where recursion is restricted to struc-
tural recursion. Thus our development started with a certifying division function
obtaining quotient and remainder with structural recursion. The certifying func-
tion gives us abstract functions for quotient and remainder. Using the uniqueness
of the specification of Euclidean division, we showed that the abstract functions
satisfy the equations for repeated subtraction and agree with reducible functions
employing an auxiliary argument.
The proofs in this section often involve considerable formal detail, which is typ-
ical for program verification. They are examples of proofs whose construction and
analysis profits much from working with a proof assistant, where a prover for linear
arithmetic takes care of tedious arithmetic details. When done by hand, the details
of the proofs can be overwhelming and their verification is error-prone.
216
21 Lists
Finite sequences [x1 , . . . , xn ] are omnipresent in mathematics and computer
science, appearing with different interpretations and notations, for instance, as
vectors, strings, or states of stacks and queues. In this chapter, we study inductive
list types providing a recursive representation for finite sequences whose elements
are taken from a base type. Besides numbers, lists are the most important recursive
data type in computational type theory. Lists have much in common with numbers,
given that recursion and induction are linear for both data structures. Lists also
have much in common with finite sets, given that both have a notion of member-
ship. In fact, our focus will be on the membership relation for lists.
We will see recursive predicates for membership and disjointness of lists, and
also for repeating and nonrepeating lists. We will study nonrepeating lists and
relate non-repetition to cardinality of lists.
Lists represent finite sequences [x1 , . . . , xn ] with two constructors nil and cons:
[] , nil
[x] , cons x nil
[x , y] , cons x (cons y nil)
[x , y , z] , cons x (cons y (cons z nil))
The constructor nil provides the empty list. The constructor cons yields for a
value x and a list representing the sequence [x1 , . . . , xn ] a list representing the
sequence [x , x1 , . . . , xn ]. Given a list cons x A, we call x the head and A the tail
of the list. We say that lists provide a nested pair representation of sequences.
Formally, we obtain lists with an inductive type definition
The type constructor L : T → T gives us a list type L(X) for every base type X. The
value constructor nil : ∀X T . L(X) gives us an empty list for every base type. Finally,
the value constructor cons : ∀X T . X → L(X) → L(X) provides for the construction
of nonempty lists by adding an element in front of a given list. Lists of type L(X)
are called lists over X. Note that all elements of a list over X must have type X.
217
21 Lists
For nil and cons, we don’t write the first argument X. We use the notations
[] := nil
x :: A := cons x A
x :: y :: A x :: (y :: A)
When convenient, we shall use the sequence notation [x1 , . . . , xn ] for lists.
Given a list [x1 , . . . , xn ], we call n the length of the list, x1 , . . . , xn the elements
of the list, and the numbers 0, . . . , n − 1 the positions of the list. An element may
appear at more than one position in a list. For instance, [2 , 2 , 3] is a list of length 3
that has 2 elements, where the element 2 appears at positions 0 and 1.
The inductive definition of lists provides for case analysis, recursion, and induc-
tion on lists, in a way that is similar to what we have seen for numbers. We define
the eliminator for lists as follows:
The eliminator provides for inductive proofs, recursive function definitions, and
structural case analysis.
Proof The proofs are similar to the proofs for numbers in § 4.3. Claim (4) corre-
sponds to Sn ≠ n and follows by induction on A with x quantified. ■
Fact 21.1.2 (Decidable Equality) If X is a discrete type, then L(X) is a discrete type:
E(X) → E(L(X)).
218
21.2 Basic Operations
We introduce three basic operations on lists, which yield the length of a list, con-
catenate two lists, and apply a function to every position of a list:
len : ∀X T . L(X) → N
len [] := 0
len (x :: A) := S (len A)
Fact 21.2.1
1. A + + C) = (A +
+(B + + B) +
+C (associativity)
2. A +
+[] = A
3. len (A +
+ B) = len A + len B
4. len (f @A) = len A
5. len A = 0 ←→ A = []
21.3 Membership
x ∈ [x1 , . . . , xn ] ←→ x = x1 ∨ · · · ∨ x = xn ∨ ⊥
219
21 Lists
(∈) : ∀X T . X → L(X) → P
(x ∈ []) := ⊥
(x ∈ y :: A) := (x = y ∨ x ∈ A)
Proof Straightforward. ■
Proof By induction on A. The nil case is contradictory. In the cons case a case
analysis using Fact 21.3.2 closes the proof. ■
Proof By induction on A. ■
Proof By induction on A. ■
220
21.4 Inclusion and Equivalence
Recall that finite quantification over numbers preserves decidability (Fact 13.3.3).
Similarly, quantification over the elements of a list preserves decidability. In fact,
quantification over the elements of a list is a form of finite quantification.
We will use the notations
∀x∈A. px := ∀x. x ∈ A → px
∃x∈A. px := ∃x. x ∈ A ∧ px
Σx∈A. px := Σx. x ∈ A × px
Exercise 21.3.8
Define a function δ : L(O(X)) → L(X) such that x ∈ δA ←→ ◦x ∈ A.
We may see a list as a representation of a finite set. List membership then corre-
sponds to set membership. The list representation of sets is not unique since the
same set may have different list representations. For instance, [1 , 2], [2 , 1], and
[1 , 1 , 2] are different lists all representing the set {1, 2}. In contrast to sets, lists
are ordered structures providing for multiple occurrences of elements.
From the type-theoretic perspective, sets are informal objects that may or may
not have representations in type theory. This is in sharp contrast to set-based math-
ematics where sets are taken as basic formal objects. The reason sets don’t appear
natively in computational type theory is that sets in general are noncomputational
objects.
We will take lists over X as type-theoretic representations of finite sets over X.
With this interpretation of lists in mind, we define list inclusion and list equiva-
lence as follows:
A ⊆ B := ∀x. x ∈ A → x ∈ B
A ≡ B := A ⊆ B ∧ B ⊆ A
221
21 Lists
Note that two lists are equivalent if and only if they represent the same set.
Fact 21.4.2 We have the following properties for membership, inclusion, and equiv-
alence of lists.
x ∉ [] x ∈ [y] ←→ x = y
[] ⊆ A A ⊆ [] → A = []
x ∈ y :: A → x ≠ y → x ∈ A x ∉ y :: A → x ≠ y ∧ x ∉ A
A⊆B→x∈A→x∈B A ≡ B → x ∈ A ←→ x ∈ B
A ⊆ B → x :: A ⊆ x :: B A ≡ B → x :: A ≡ x :: B
A ⊆ B → A ⊆ x :: B x :: A ⊆ B ←→ x ∈ B ∧ A ⊆ B
x :: A ⊆ x :: B → x ∉ A → A ⊆ B x :: A ⊆ [y] ←→ x = y ∧ A ⊆ [y]
x :: A ≡ x :: x :: A x :: y :: A ≡ y :: x :: A
x ∈ A → A ≡ x :: A
x ∈ A+
+ B ←→ x ∈ A ∨ x ∈ B
A ⊆ A′ → B ⊆ B ′ → A +
+ B ⊆ A′ +
+ B′ A+
+ B ⊆ C ←→ A ⊆ C ∧ B ⊆ C
Proof Except for the membership fact for concatenation, which already appeared
as Fact 21.3.5, all claims have straightforward proofs not using induction. ■
Proof Follows with Fact 21.3.1. There is also a direct proof by induction on A. ■
Proof Follows with Fact 21.3.3. There is also a direct proof by induction on A using
Fact 21.3.2. ■
Fact 21.4.5 Let A and B be lists over a discrete type. Then D(A ⊆ B) and D(A ≡ B).
Proof Holds since membership is decidable (Fact 21.3.4) and bounded quantifica-
tion preserves decidability (Fact 21.3.7). ■
222
21.5 Nonrepeating Lists
A list is repeating if it contains some element more than once. For instance, [1, 2, 1]
is repeating and [1, 2, 3] is nonrepeating. Formally, we define repeating lists over a
base type X with a recursive predicate:
rep : L(X) → P
rep [] := ⊥
rep (x :: A) := x ∈ A ∨ rep A
We also define a recursive predicate for nonrepeating lists over a base type X:
nrep : L(X) → P
nrep [] := ⊤
nrep (x :: A) := x ∉ A ∧ nrep A
Proof Both claims follow by induction on A. Discreteness is only needed for the
second claim, which needs decidability of membership (Fact 21.3.4) for the cons
case. ■
Proof By induction on A. For x :: A, let B be the list obtained for A with the
inductive hypothesis. If x ∈ A, B has the required properties for x :: A. If x ∉ A,
x :: B has the required properties for x :: A. ■
223
21 Lists
The next fact formulates a key property concerning the cardinality of lists (num-
ber of different elements). It is carefully chosen so that it provides a building block
for further results (Corollary 21.5.6). Finding this fact took experimentation. To
get the taste of it, try to prove that equivalent nonrepeating lists have equal length
without looking at our development.
Proof Interestingly, all claims follow without induction from Facts 21.5.5, 21.5.1,
and 21.5.3.
For (1), assume len A > len B and derive a contradiction with Fact 21.5.5.
Claims (2) and (3) follow from Claim (1), where for (3) we assume nrep A and
derive a contradiction (justified by Corollary 21.5.3).
For (4), we assume rep B and derive a contradiction (justified by Corollary 21.5.3).
By Fact 21.5.1, we obtain a list B ′ such that A ⊆ B ′ and len B ′ < len A. Contradiction
with (1).
For (5), it suffices to show B ⊆ A. We assume x ∈ B and show x ∈ A. Exploiting
the decidability of membership we assume x ∉ A and derive a contradiction. Using
Fact 21.5.5 for x :: A and B, we obtain z ∈ x :: A and z ∉ B, which is contradictory.■
Exercise 21.5.7 Prove the following facts about map and nonrepeating lists:
a) injective f → nrep A → nrep (f @A).
b) nrep (f @A) → x ∈ A → x ′ ∈ A → f x = f x ′ → x = x ′ .
c) nrep (f @A) → nrep A.
224
21.5 Nonrepeating Lists
Exercise 21.5.13 (Even and Odd) Define recursive predicates even and odd on
numbers and show that they partition the numbers: even n → odd n → ⊥ and
even n + odd n.
225
21 Lists
We now come to some facts about lists of numbers whose truth is intuitively clear
but whose proofs are surprisingly tricky. The facts about nonrepeating lists turn
out to be essential.
A segment is a list containing all numbers smaller than its length:
A list of numbers is serial if for every element it contains all smaller numbers:
We will show that a list is a segment if and only if it is nonrepeating and serial.
Proof By induction on A. ■
Proof Let A be a segment. By Facts 21.6.4 and 21.6.2 we have an equivalent nonre-
peating segment B of the same length. Hence A is nonrepeating by Fact 21.5.6(4). ■
Proof Let A be a nonrepeating list of numbers of length Sn. By Fact 21.3.7(1) we can
assume ∀k∈A. k < n and derive a contradiction. Fact 21.6.4 gives us a nonrepeating
list B of length n such that ∀k. k ∈ B ←→ k < n. Now A ⊆ B and len B < len A.
Contradiction by Fact 21.5.6(1). ■
Proof Let A be a nonrepeating serial list. If A = [], the claim is trivial. Otherwise,
len A = Sn. Fact 21.6.6 gives us x ∈ A such that x ≥ n. Fact 21.6.4 gives us a
nonrepeating segment B of length Sn. We now see that A is a segment if A ≡ B. We
have B ⊆ A since A is serial and hence contains all k ≤ n ≤ x. Now A ⊆ B follows
with Fact 21.5.6(5). ■
226
21.7 Position-Element Mappings
Fact 21.6.8 (Next number) There is function that for every list of numbers yields a
number that is not in the list: ∀AL(N) . Σn. ∀k∈A. k < n.
Proof By induction on A. ■
Fact 21.6.8 says that there are infinitely many numbers. More generally, if we
have an injection INX, we can obtain a new element generator for X and thus know
that X is infinite.
Proof Let invNX f g and ALX . Then Fact 21.6.8 gives us a number n ∉ g@A. To
show f n ∉ A, assume f n ∈ A. Then n = g(f n) ∈ g@A contradicting an above
assumption. ■
Exercise 21.6.10 (Pigeonhole) Prove that a list of numbers whose sum is greater
than the length of the list must contain a number that is at least 2:
Exercise 21.6.11 (Andrej’s Puzzle) Assume an increasing function f N→N (that is,
∀x. x < f x) and a list A of numbers satisfying ∀x. x ∈ A ←→ x ∈ f @A. Show
that A is empty.
Hint: First verify that A contains for every element a smaller element. It then
follows by complete induction that A cannot contain an element.
Exercise 21.6.12 Define a function seq : N → N → L(N) for which you can prove the
following:
a) seq 2 5 = [2, 3, 4, 5, 6]
b) seq n (Sk) = n :: seq (Sn) k
c) len (seq nk) = k
d) x ∈ seq nk ←→ n ≤ x < n + k
e) nrep (seq nk)
227
21 Lists
bijective relation between the positions and the elements of the list. For instance,
the list [7, 8, 5] gives us the bijective relation
0 7, 1 8, 25
It turns out that for a discrete type X we can define two functions
pos : L(X) → X → N
sub : X → L(X) → N → X
The function pos uses 0 as escape value for positions, and the function sub uses
a given y X as escape value for elements of X. The name sub stands for subscript.
The functions pos and sub will be used in Chapter 22 for constructing injections
and bijections between finite types and in Chapter 23 for constructing injections
into N.
Here are the definitions of pos and sub we will use:
pos : L(X) → X → N
pos [] x := 0
pos (a :: A) x := if [a = x\ then 0 else S(pos Ax)
sub : X → L(X) → N → X
sub y [] n := y
sub y (a :: A) 0 := a
sub y (a :: A) (Sn) := sub yAn
Proof All claims follow by induction on A. For (3), the inductive hypothesis must
quantify n and the cons case needs case analysis on n. ■
228
21.8 Constructive Discrimination Lemma
Exercise 21.7.3 Let A and B be lists over a discrete type X. Prove the following:
a) x ∈ A → pos Ax = pos (A +
+ B)x
b) k < len A → sub Ak = sub (A +
+ B)k
c) x ∈ A → y ∈ A → pos Ax = pos Ay → x = y
Note that (a) relies on the fact that pos Ax yields the first position of x in A, which
matters if x occurs more than once in A.
Exercise 21.7.4 One can realize pos and sub with option types
and this way avoid the use of escape values. Define pos and sub with option types
for a discrete base type X and verify the following properties:
a) x ∈ A → Σn. pos Ax = ◦n
b) n < len A → Σx. sub An = ◦x
c) pos Ax = ◦n → sub An = ◦x
d) nrep A → sub An = ◦x → pos Ax = ◦n
e) sub An = ◦x → x ∈ A
f) pos Ax = ◦n → n < len A
Using XM, we can prove that every non-repeating list contains for every shorter list
an element that is not in the shorter list:
replacing XM with an equality decider for the base type X. In this section our main
interest is in proving the constructive discrimination lemma
which assumes neither XM nor an equality decider. Note that the classical discrim-
ination lemma is a trivial consequence of the constructive discrimination lemma.
We may say that the constructive discrimination lemma is obtained from the classi-
cal discrimination lemma by eliminating the use of XM by weakening the existential
229
21 Lists
claim with a double negation. Elimination techniques for XM have useful applica-
tions.
We first prove the classical discrimination lemma following the proof of
Fact 21.5.5.
Lemma 21.8.1 (Classical discrimination)
XM → ∀AB L(X) . nrep A → len B < len A → ∃x. x ∈ A ∧ x ∉ B.
We observe that there is only a single use of XM. When we prove the constructive
version with the double negated claim, we will exploit that XM is available for stable
claims (Fact 17.4.8 (1)). Moreover, we will use the rule formulated by Fact 17.4.8 (2)
to erase the double negation from the inductive hypothesis so that we can harvest
the witness.
Lemma 21.8.2 (Constructive discrimination)
∀AB L(X) . nrep A → len B < len A → ¬¬∃x. x ∈ A ∧ x ∉ B.
Exercise 21.8.3 Prove that the double negation of ∃ agrees with the double negation
of Σ : ¬¬ex p ←→ ((sig p → ⊥) → ⊥).
\ : L(X) → X → L(X)
[]\_ := []
(x :: A)\y := if [x = y\ then A\y else x :: (A\y)
230
21.10 Cardinality
Fact 21.9.1
1. x ∈ A\y ←→ x ∈ A ∧ x ≠ y
2. len (A\x) ≤ len A
3. x ∈ A → len (A\x) < len A.
4. x ∉ A → A\x = A
Proof By induction on A. ■
Exercise 21.9.3 Prove the following equations, which are useful in proofs:
1. (x :: A)\x = A\x
2. x ≠ y → (y :: A)\x = y :: (A\x)
21.10 Cardinality
The cardinality of a list is the number of different elements in the list. For instance,
[1, 1, 1] has cardinality 1 and [1, 2, 3, 2] has cardinality 3. Formally, we may say
that the cardinality of a list is the length of an equivalent nonrepeating list. This
characterization is justified since equivalent nonrepeating lists have equal length
(Corollary 21.5.6 (3)), and every list is equivalent to a non-repeating list (Fact 21.5.4).
We assume that lists are taken over a discrete type X and define a cardinality
function as follows:
card : L(X) → N
card [] := 0
card(x :: A) := if [x ∈ A\ then card A else S(card A)
Note that we write [x ∈ A\ for the application of the membership decider provided
by Fact 21.3.4. We prove that the cardinality function agrees with the cardinalities
provided by equivalent nonrepeating lists.
Proof Claim 1 follows by induction on A. Claim 2 follows with Claim 1 and Corol-
lary 21.5.6 (2). ■
Corollary 21.10.2
231
21 Lists
1. card A ≤ len A
2. A ⊆ B → card A ≤ card B
3. A ≡ B → card A = card B.
4. rep A ←→ card A < len A (pigeonhole)
5. nrep A ←→ card A = len A
6. x ∈ A ←→ card A = S(card(A \ x))
Proof All facts follow without induction from Fact 21.10.1, Corollary 21.5.6, and
Corollary 21.5.3. ■
Exercise 21.10.3 Given direct proofs of (1), (4) and (5) of Corollary 21.10.2 by in-
duction on A. Use (1) for (4) and (5).
Card : L(X) → X → P
Card [] 0 := ⊤
Card [] (Sn) := ⊥
Card (x :: A) 0 := ⊥
Card (x :: A) (Sn) := if [x ∈ A\ then Card A (Sn) else Card A n
Prove that the cardinality predicate agrees with the cardinality function:
∀An. Card An ←→ card A = n.
Define a recursive predicate Disjoint : L(X) → L(X) → P in the style of the cardinal-
ity predicate and verify that it agrees with the above predicate disjoint.
232
21.11 Setoid Rewriting
P ∧ Q ←→ P ′ ∧ Q′ P ∨ Q ←→ P ′ ∨ Q′ (P → Q) ←→ (P ′ → Q′ )
¬P ←→ ¬P ′ (P ←→ Q) ←→ (P ′ ←→ Q′ )
x ∈ A ←→ x ∈ A′ A ⊆ B ←→ A′ ⊆ B ′ A ≡ B ←→ A′ ≡ B ′
x :: A ≡ x :: A′ + B ≡ A′ +
A+ + B′ f @A ≡ f @A′
Coq’s setoid rewriting facility makes it possible to use the rewriting tactic for
rewriting with equivalences, provided the necessary compatibility laws and equiv-
alence relations have been registered with the facility. The compatibility laws for
propositional equivalence are preregistered.
Exercise 21.11.3 Which of the compatibility laws are needed to justify rewriting
+ B) with the equivalence A ≡ A′ ?
the claim ¬(x ∈ y :: (f @A) +
233
22 Finite Types
We may call a type finite if we can provide a list containing all its elements. With
this characterization we capture the informal notion of finitely many elements with
covering lists. If the list covering the elements of a type is nonrepeating, the length
of the list gives us the cardinality of the type (the number of its elements). This leads
us to defining a finite type as a tuple consisting of a type, a list covering the type, and
an equality decider for elements of the type. The equality decider ensures that the
finite type is sufficiently concrete so that we can compute a covering nonrepeating
list yielding the cardinality of the type.
With this definition the numeral types Nn are in fact finite types of cardinality n.
We will show that finite types are closed under retracts and that two finite types of
equal cardinality are always in bijection. It then follows that finite types have EWOs,
and that the class of finite types is generated from the empty type by taking option
types and including types that are in bijection with a member of the class. We also
show that a function between finite types of the same cardinality
• is injective if and only if it is surjective.
• has an inverse function if it is surjective or injective.
• yields a bijection with every inverse function.
We prepare the definition of finite types by looking at lists covering all elements of
their base type.
A covering of a type is a list that contains every member of the type:
coveringX A := ∀x X . x ∈ A
We need a couple of results for coverings and listings of discrete types. First note
that all coverings of a type are equivalent lists.
235
22 Finite Types
Fact 22.1.1 Given a covering of a discrete type, one can obtain a listing of the type:
EX → coveringX A → Σ B. listingX B ∧ len B ≤ len A.
Fact 22.1.2 All listings of a discrete type have the same length.
We define finite types as discrete types that come with a covering list:
We may see the values of fin X as handlers providing an equality decider for X and a
listing of X. A handler fin X turns a type X into a computational type where we can
iterate over all elements and decide equality of elements. Given a handler fin X, we
can compute a listing of X and thus determine the cardinality of X (i.e., the number
of elements). It will be convenient to have a second type of handlers
declaring the cardinality of the type and providing a listing rather than a covering
of the type.
236
22.2 Basics of Finite Types
Proof Exercise. ■
Proof Fact 11.2.1 gives us an equality decider for OX. Moreover, :: (◦)@A) is a
listing of OX if A is a listing of X. ■
Proof Discreteness follows with Fact 9.5.1. We leave the construction of the listing
as exercise. ■
Quantification over finite types preserves decidability. This fact can be obtained
from the fact that quantification over lists preserves decidability.
Proof Suppose N is finite. Then we have a list A containing all numbers. Contradic-
tion with Fact 21.6.8. ■
237
22 Finite Types
We can establish the finiteness of a nonempty type by embedding it into a finite type
with large enough cardinality. More precisely, a type is finite with cardinality m ≥ 1
if it can be embedded into a finite type with cardinality n ≥ m.
Fact 22.3.1 (Finiteness by embedding)
IXY → finn Y → Σm ≤ n. finm X.
Proof Let invXY gf and B be a listing of Y such that len B = n. Fact 9.5.2 gives us an
equality decider for X. Moreover, g@B is a covering of X because of the inversion
property. By Fact 22.1.1 we obtain a listing A of X such that len A ≤ len (g@B) =
len B = n. The claim follows. ■
Corollary 22.3.5 The type N of numbers does not embed into finite types:
I N X → fin X → ⊥.
238
22.4 Existence of Injections
Given two finite types, the smaller one can always be embedded into the larger one.
There is the caveat that the smaller type must not be empty so that the embedding
function can have an inverse.
Proof Let A and B be listings of X and Y . Then A has length m and B has length n.
Since 1 ≤ m ≤ n, we can map the elements of A to elements of B preserving the
position in the lists. We realize the resulting bijection between X and Y using the
list operations sub and pos with escape values a ∈ A and b ∈ B (§21.7):
f x := sub b B (pos A x)
gy := sub a A (pos B y)
Recall that pos yields the position of a value in a list, and that sub yields the value
at a position of a list. ■
An embedding of a finite type into a finite type of the same cardinality is in fact
a bijection since in this case the second roundtrip property does hold.
Next we show that all finite types of the same size are in bijection.
Proof For n = 0 the claim follows with Facts 22.2.2 and 11.1.7. For n > 0 the claim
follows with Facts 22.4.1 and 22.4.2. ■
Proof Direction → follows with Facts 22.2.4 and 22.4.3. Direction ← follows with
Facts 22.2.4 and 22.3.4. ■
239
22 Finite Types
Proof Follows with Fact 22.4.5 since numeral types have EWOs (Fact 14.3.4) and
injections transport EWOs (Fact 14.3.5). ■
Fact 22.4.8 (Existence) Every nonempty finite type can be embedded into N:
finSn X → I XN.
Proof Let A be a listing of X. We realize the injection of X into N using the list
operations pos and sub with an escape values a ∈ A (§21.7):
f x := pos A x
gn := sub a A n ■
Proof Let A and B be listings for X and Y , respectively, with len A = len B. We fix
f X→Y and have covering(f @A) ←→ nrep(f @A) by Fact 22.1.3.
Let f be injective. Then f @A is nonrepeating by Exercise 21.5.7 (a). Thus f @A
is covering. Hence f is surjective.
Let f be surjective. Then f @A is covering and thus nonrepeating. Thus f is
injective by Exercise 21.5.7 (b). ■
Fact 22.5.2 (Upgrade) Let f X→Y be a surjective or injective function between finite
types of the same cardinality. Then one can obtain a function g such that f and g
constitute a bijection between X and Y .
Proof Let finn X and finn Y . The both types have equality deciders and EWOs
(Fact 22.4.7). By Fact 22.5.1 we can assume a surjective function f X→Y . Fact 14.4.1
gives us a function g such that inv f g. Fact 22.4.2 gives us inv gf as claimed. ■
240
22.6 Listless Development
Exercise 22.5.3
Prove finm X → finn Y → m > 0 → (∀f X→Y . injective f ←→ surjective f ) → m = n.
Exercise 22.5.4 Show that all inverse functions of an injective function between
finite types of the same cardinality agree.
Fact 22.4.5 characterizes finite types with numeral types and bijections not using
lists. In fact, in set theory finite sets are usually obtained as sets that are in bijection
with canonical finite sets. Moreover, a number n is represented as a particular set
with exactly n elements. Following the development in set theory, one may study
finite types in type theory not using lists. Important results of such a development
would be the following theorems:
1. B Nm Nn → m = n
2. ∀f g Nn →Nn . inv gf → inv f g
3. I Nn N
4. I N Nn → ⊥
In the proofs of these results induction over numbers (i.e., the cardinality n) will
replace induction over lists.
We will give a few list-free proofs for numeral types since they are interesting
from a technical perspective. They require a different mindset and sometimes re-
quire tricky techniques for option types.
The main tool for proving properties of numeral types Nn is induction on the
cardinality n. An important insight we will use is that we can lower an embedding
of O2 (X) into O2 (Y ) into an embedding of O(X) into O(Y ). We realize the idea with
a lowering operator as follows:
The idea behind L is simple: Given x, Lf checks whether f maps ◦x to ◦b. If so, Lf
maps x to b. Otherwise, Lf checks whether f maps to ◦b. If so, Lf maps x to b.
If not, Lf maps x to .
Proof Let inv gf . We show (Lg)(Lf a) = a by brute force case analysis following the
matches of Lf and Lg. There are 24 cases that all follow with equational reasoning
as provided by the ongruence tactic. ■
241
22 Finite Types
Proof We prove the claim by induction on n. For n = 0 and n = 1 the proofs are
straightforward.
Let f , g : OSSn (⊥) → OSSn (⊥) and inv gf . By Lemma 22.6.1 and the inductive
hypothesis we have inv (Lf )(Lg). We consider 2 cases:
1. f (g) = . We show f (g ◦b) = ◦b. We have (Lf )(Lgb) = b. The claim now
follows by case analysis and linear equational reasoning following the definitions
of Lf and Lg (7 cases are needed).
2. f (g) = ◦b. We derive a contradiction.
a) f = ◦b′ We have (Lf )(Lgb′ ) = b′ . A contradiction follows by case anal-
ysis and linear equational reasoning following the definitions of Lf and Lg
(4 cases are needed).
b) f = . Contradictory since inv gf . ■
The above proof requires the verification of 12 cases by linear equational reason-
ing as realized by Coq’s congruence tactic. The cascaded case analysis of the proof
is cleverly chosen as to minimize the cases that need to be considered. The need
for cascaded case analysis of function applications so that linear equational rea-
soning can finish the current branch of the proof appeared before with Kaminski’s
equation (§5.4).
We remark that the lowering operator L is related to the certifying lowering op-
erator established by Lemma 11.2.2. However, there are essential differences. The
lowering operator L uses a default value while Lemma 11.2.2 exploits an assump-
tion and computational falsity elimination to avoid the need for a default value.
In fact, the default value is not available in the setting of Lemma 11.2.2, and the
assumption is not available in the setting of the lowering operator.
Theorem 22.6.2 stands out in that its proof requires the verification of more
cases than one feels comfortable with on paper. Here the accompanying verification
with a proof assistant gives confidence beyond intuition and common belief.
We now generalize self-injection to general finite types.
Proof Let f1 , g1 form a bijection BXNn and f2 , g2 form a bijection BXNn . We have
242
22.6 Listless Development
Using the lowering lemma, we can also prove a cardinality result for numeral
types.
Exercise 22.6.6 Try to do the proof of Theorem 22.6.2 without looking at the details
of the given proof. This will make you appreciate the cleverness of the case analysis
of the given proof. It took a few iterations to arrive at this proof. Acknowledgements
go to Andrej Dudenhefner.
243
22 Finite Types
The property says that every total relation from X to Y contains a function from X
to Y . Prove choice XY for all finite types X:
a) choice ⊥ Y
b) choice X Y → choice (OX) Y
c) choice Nn Y
d) B X Nn → choice XY
The proposition choice N Y is known as countable choice for Y . The computational
type theory we are considering cannot prove countable choice for B.
22.7 Notes
We have chosen to define finite types using lists. This is a natural and convenient
definition given that lists are a basic computational data structure. On the other
hand, we could define finite types more abstractly as types that are in bijection with
numeral types obtained with option types and recursion. This definition is backed
up by two bijection theorems (22.6.4 and 22.6.2).
244
23 Countable Types
Countable types include finite types and N, and are closed under retracts, sums,
cartesian products, and list types. All countable types have equality deciders, enu-
merators, and EWOs. Infinite countable types are in bijection with N. Typical ex-
amples for infinite countable types are inductive types for syntactic objects (e.g.,
expressions and formulas). As it comes to characterizations of countable types, a
type X is countable iff X has an enumerator and an equality decider, or iff OX is a
retract of N, or iff X is a retract of N and also inhabited.
Proof Straightforward. Closure under + and × follows with I(N × N)N (Chapter 7).
(3) follows since fin X → Σn. IX(On ⊥) (Fact 22.4.6). ■
Given a function f N→OX , we call a function g X→N such that ∀x. f (gx) = ◦x
a co-enumerator for f .
245
23 Countable Types
Proof It suffices to show ∀x. Σn. f n = ◦x. Follows with an EWO of N since OX is
discrete. ■
Proof Follows with Fact 23.1.1 and the concomitant closure facts for discrete
types. ■
246
23.2 Countable Types
Proof Suppose cty X. Fact 23.2.2 gives us f and g such that ∀x. f (gx) = ◦x. We
obtain an injection I(OX)N as follows:
g ′ a := match a [ ◦x ⇒ S(gx) | ⇒ 0]
f ′ n := match n [ 0 ⇒ | Sn ⇒ f n]
The other direction follows with the transport lemmas for equality deciders 11.1.3
and 11.2.1 and the observation that the inverse function of the injection is an enu-
merator of X. ■
Proof Follows with an EWO for N (Fact 14.2.2) and Facts 23.2.3, 14.3.5, and 14.3.3.■
f ′ a := match a [ ◦x ⇒ S(f x) | ⇒ 0]
g ′ n := match n [ 0 ⇒ | Sn ⇒ ◦gn]
Fact 23.2.6
An injection I(OX)Y with X inhabited can be lowered into an injection IXY :
I(OX)Y → X → IXY .
f ′ x := f (◦x)
g ′ y := match gy [ ◦x ⇒ x | ⇒ x0 ]
Proof Suppose N → B is countable. Then Fact 23.2.7 give us an injection I(N → B)N
and thus a surjective function N → (N → B). Contradiction with Cantor’s theorem
(Fact 6.3.4). ■
247
23 Countable Types
Proof Let X be a discrete type and L and β be functions as specified. Since L1 is not
empty by (1), we have a default value x0 : X. We define functions f X→N and g N→X
f x := pos Lβx x
gn := sub LSn n
and show inv gf . We prepare the proof with the following lemmas:
3. m ≤ n → prefix Lm Ln
4. prefix Lm Ln ∨ prefix Ln Lm
5. k < len Lm → k < len Ln → sub Lm k = sub Ln k
6. n ≤ len Ln
(3) follows by induction on n. (4) is a straightforward consequence of (3). (5) follows
with Fact 23.3.1 and (4). (6) follows by induction on n and (1).
To show inv gf , we fix x and set k := pos Lβx x and show sub LSk k = x. By
Fact 21.7.1(1) it suffices to show sub LSk k = sub Lβx k, which follows by (5) since
k < len LSk by (6) and k < len Lβx by (2) and Fact 21.7.1(2). ■
248
23.4 More Countable Types
Using a list enumeration, we now show that the recursive inductive type
To define a list enumeration for tree, we need a function that given a list A of
trees returns a list containing all trees Tst with s, t ∈ A.
Lemma 23.4.2 (List product)
∀AB L(tree) . ΣC. ∀u. u ∈ C ←→ ∃ s∈A ∃ t∈B. u = Tst.
Proof We fix B and prove the claim by induction on A following the scheme
[] · B = []
(s :: A) · B = (Ts)@B +
+ A·B ■
Proof By Theorem 23.3.2 and Fact 23.4.1 it suffices to construct a list enumeration
for tree. We define:
L(0) := []
L(Sn) := L(n) +
+An+
+{ T(s, t) | s, t ∈ L(n) }
β(A n) := Sn
β(T st) := S(βs + βt)
where { T(s, t) | s, t ∈ L(n) } is notation for the application of the list product
function from Lemma 23.4.2 to L(n) and L(n). We show ∀t. t ∈ L(βt) by induction
on t, the rest is obvious.
For A, we show A n ∈ L(Sn), which is straightforward.
For T, we show T st ∈ L(S(βs + βt)). By induction we have s ∈ L(βs) and
t ∈ L(βt). It suffices to show s, t ∈ L(βs + βt). Follows with the monotonicity
property
∀mn. m ≤ n → Lm ⊆ Ln
which in turn follows by induction on n. ■
249
23 Countable Types
Proof By Facts 23.4.4 and 23.2.1(2a) it suffices to embed N × N into tree, which can
be done as follows:
Note that Fact 23.4.5 can also be obtained with Cantor pairing (Fact 11.1.8). The
point is that Fact 23.4.5 obtains countability of N × N with a routine method rather
than the ingenuous Cantor pairing.
Fact 23.4.6 cty (L N).
Proof By Facts 23.4.4 and 23.2.1(2a) it suffices to embed L N into tree, which can be
done as follows:
f [] := A 0
f (x :: A) := T(A x)(f A)
g(T(A x) t) := x :: gt
g _ := []
Fact 23.4.7 Countable types are closed under taking list types: cty X → cty(L X).
Proof Let cty X. The retract characterization (Fact 23.2.3) gives us functions F OX→N
and GN→OX such that inv GF . By Facts 23.4.6 and 23.2.1(2a) it suffices to embed L X
into L N, which can be done as follows:
f A := (λx. F (◦x)) @ A
g [] := []
g (n :: A) := match Gn [ ◦x ⇒ x :: gA | ⇒ [] ]
250
23.5 Alignments
Exercise 23.4.9 Prove that the type of expressions (§ 8.2) and the type of formu-
las (§ 24.1) are countable by embedding them into tree. No recursion is needed for
the embedding.
23.5 Alignments
Fact 23.5.1 Cutoffs are unique. That is, all cutoffs of a function X → N agree.
Fact 23.5.2 The surjective alignments are exactly the bijective functions X → N.
Proof Follows with Fact 14.4.2 since countable types have EWOs (Fact 23.2.4). ■
Proof Immediate since countable types have EWOs and equality on N is decidable.■
251
23 Countable Types
We will now show that countable types have alignments. The construction proceeds
in two steps and starts from the enumerator of a countable type. The first step
obtains a nonrepeating enumerator by removing repetitions.
An enumerator f N→OX is nonrepeating if ∀mn. f m = f n ≠ → m = n.
Fact 23.6.1 (Nonrepeating enumerator)
Every countable type has a nonrepeating enumerator:
cty X → Σ f N→OX . enum′ f ∧ nonrepeating f .
There is a second characterization of seriality that is useful for the second step
of the alignment construction.
Fact 23.6.2 A function f X→N is serial if and only if ∀n. hit f (Sn) → hit f n.
Proof One direction is trivial. For the other direction we assume ∀n. hit f (Sn) →
hit f n and prove ∀nk. hit f n → k ≤ n → hit f k by induction on n. ■
252
23.7 Bijection Theorem
Proof Let X be a countable type. By Facts 23.6.1 and 23.1.3 X has a nonrepeating
enumerator f N→OX with a co-enumerator g X→N . We define a function hN→N such
that hn is the number of hits f has for k < n:
h(0) := 0
h(Sn) := if f n ≠ then S(hn) else hn
We now show that two countable types are in bijection if they are retracts of each
other: cty X → cty Y → IXY → IY X → BXY .
Proof Let F X→Y and GY →X be such that ∀x. G(F x) = x. We fix x and show
Σy. gy = f x. By Fact 23.5.4 it suffices to show that g hits f x. With the Seg-
ment Lemma 23.5.5 we obtain a nonrepeating list A : L(X) of length S(f x). Now
g@(F @A) has length S(f x) and is nonrepeating since g and F are injective. The
Large Element Lemma 21.6.6 gives k ∈ g@(F @A) such that k ≥ f x. Thus g hits k.
Since g is serial and f x ≤ k, g hits f x. ■
253
23 Countable Types
Proof X and Y have alignments f and g by Fact 23.6.3. With the Transport
Lemma 23.7.1 we obtain functions F : ∀x. Σy. gy = f x and G : ∀y. Σx. f x = gy.
Using the injectivity of f and g one verifies that λx.π1 (F x) and λy.π1 (Gy) form
a bijection. ■
Proof Let INX and cty X. Then X is inhabited and thus IXN by Fact 23.2.7. Now
the bijection theorem 23.7.2 gives us a bijection BXN since cty N. ■
We will show that a countable type X is finite with cardinality n if and only if n is
the cutoff of some alignment of X.
Fact 23.8.1 Let n be the cutoff of an alignment of a countable type X. Then finn X.
Proof Let finn X and f be a alignment of X. It suffices to show that f doesn’t hit n.
By Fact 23.8.2 we have a cutoff n of f . Thus n < n if f hits n. ■
254
23.9 Finite or Infinite
Assuming the law of excluded middle, we show that a serial function either has a
cutoff or is surjective. Since there is no algorithm deciding this property, assuming
excluded middle seems necessary.
Proof Assume XM and let f be serial. Using XM, we assume that f is not surjective
and show that f has a cutoff. Using XM, we obtain a k not hit by f . Using XM and
Fact 13.5.2, we obtain the least n not hit by f . Since f is serial, n is the cutoff of f .■
We now have that under XM a countable type is either finite or in bijection with N.
23.10 Discussion
We have shown that a countable type is either a finite type or an infinite type that
is in bijection with N. In fact, most results in this chapter generalize results we
have already seen for finite types. The generalized results include the construction
of alignments for countable types and the construction of bijections for countable
types of the same cardinality.
Cantor pairing is an ingenuous construction establishing N × N as an infinite
countable type. We gave a more general construction (list enumeration) that works
for N × N and for syntactic types in general.
We did not give a formal definition of infinite types since there are several possi-
bilities: We may call a type X infinite if X is not a finite type, if there is an injection
INX, or if there is a new element generator, among other possibilities.
As it comes to cardinalities of countable types, we may say that the cardinality
of a countable type is a value of O(N), where ◦n represents the finite cardinality n
and represents the single infinite cardinality.
255
Part III
Case Studies
257
24 Propositional Deduction
24.1 ND Systems
259
24 Propositional Deduction
s, t, u, v : For := x | ⊥ | s → t | s ∧ t | s ∨ t (x : N)
Formulas of the kind x are called atomic formulas. Atomic formulas represent
atomic propositions whose meaning is left open. For the other kinds of formulas the
symbols used give away the intended meaning. Formally, the type For of formulas
is accommodated as an inductive type that has a value constructor for each kind of
formula (5 altogether).1 We will use the familiar notation
¬s := s → ⊥
The deduction rules of the intuitionistic ND system we will consider are given in
Figure 24.1 using several notational gadgets:
• Comma notation A, s for lists s :: A.
• Ruler notation for deduction rules. For instance,
A⊢s→t A⊢s
A⊢t
1
The use of abstract syntax is discussed more carefully in Chapter 8.
260
24.2 Intuitionistic ND System
All rules in Figure 24.1 express proof rules you are familiar with from mathematical
reasoning and the logical reasoning you have seen in this text. In fact, the system
of rules in Figure 24.1 can derive exactly those judgments A ⊢ s that are known to
be intuitionistically deducible (given the formulas we consider). Since reasoning
in type theory is intuitionistic, Coq can prove a goal (A, s) if and only if the rules
in Figure 24.1 can derive the judgment A ⊢ s (where atomic formulas are accom-
modated as propositional variables in type theory). We will exploit this coincidence
when we construct derivations using the rules in Figure 24.1.
The rules in Figure 24.1 with a logical constant (i.e., ⊥, →, ∧, ∨) in the conclusion
are called introduction rules, and the rules with a logical constant in the leftmost
premise are called elimination rules. The first rule in Figure 24.1 is known as
assumption rule. Note that every rule but the assumption rule is an introduction or
an elimination rule for some logical constant. Also note that there is no introduction
rule for ⊥, and that there are two introduction rules for ∨. The elimination rule for ⊥
is also known as explosion rule.
Note that no deduction rule contains more than one logical constant. This re-
sults in an important modularity property. If we want to omit a logical constant, for
instance ∧, we just omit all rules containing this constant. Note that every system
with ⊥ and → can express negation. When trying to understand the structural prop-
erties of the system, it is usually a good idea to just consider ⊥ and →. Note that the
assumption rule cannot be omitted since it is the only rule not taking a derivation
as premise.
Here are common conveniences for the turnstile notation we will make use of in
the following:
s⊢u [s] ⊢ u
s, t ⊢ u [s, t] ⊢ u
⊢u [] ⊢ u
261
24 Propositional Deduction
A A
s, ¬s ⊢ ¬s s, ¬s ⊢ s
E→
s, ¬s ⊢ ⊥
I→
s ⊢ ¬¬s
The labels A, E→ , and I→ at the right of the lines are the names for the rules used
(assumption, elimination, and introduction).
Constructing ND derivations
Generations of students have been trained to construct ND derivations. In fact,
constructing derivations in the intuitionistic ND system is pleasant if one follows
the following recipe:
1. Construct a proof table as if the formulas were propositions.
2. Translate the proof table into a derivation using the proof assistant.
Step 1 is the more difficult one, but you already well-trained as it comes to con-
structing intuitionistic proof tables. Once the proof assistant is used, constructing
derivations becomes fun. Using the proof assistant becomes possible once the rele-
vant ND system is realized as an inductive type.
The proof assistant comes with a decision procedure for intuitionistically prov-
able quantifier-free propositions. If in doubt whether a certain derivation can be
constructed in the intuitionistic ND system, the decision procedure of the proof
assistant can readily decide the question.
Exercise 24.2.3 If you are eager to construct more derivations, Exercise 24.3.3 will
provide you with interesting examples.
It turns out that propositional deduction systems like the one in Figure 24.2 can be
formalized elegantly and directly with inductive type definitions accommodating
deduction rules as value constructors of derivation types A ⊢ s.
Let us explain this fundamental idea. We may see the deduction rules in Fig-
ure 24.1 as functions that given derivations for the judgments in the premises yield
a derivation for the judgment appearing as conclusion. The introduction rule for
conjunctions, for instance, may be seen as a function that given derivations for
A ⊢ s and A ⊢ t yields a derivation for A ⊢ s ∧ t. We now go one step further
262
24.3 Formalization with Indexed Inductive Type Family
s∈A → A⊢s A
A⊢⊥ → A⊢s E⊥
A, s ⊢ t → A ⊢ (s → t) I→
A ⊢ (s → t) → A ⊢ s → A ⊢ t E→
A ⊢ s → A ⊢ t → A ⊢ (s ∧ t) I∧
A ⊢ (s ∧ t) → A, s, t ⊢ u → A ⊢ u E∧
A ⊢ s → A ⊢ (s ∨ t) I1∨
A ⊢ t → A ⊢ (s ∨ t) I2∨
A ⊢ (s ∨ t) → A, s ⊢ u → A, t ⊢ u → A ⊢ u E∨
and formalize the deduction rules as the value constructors of an inductive type
constructor
⊢ : L(For) → For → T
This way the values of an inductive type A ⊢ s represent the derivations of the
judgment A ⊢ s we can obtain with the deduction rules. To emphasize this point,
we call the types A ⊢ s derivation types.
The value constructors for the derivation types A ⊢ s of the intuitionistic ND
system appear in Figure 24.2. Note that the types of the constructors follow exactly
the patterns of the deduction rules in Figure 24.1.
When we look at the target types of the constructors in Figure 24.2, it becomes
clear that the argument s of the type constructor A ⊢ s is not a parameter since
it is instantiated by the constructors for the introduction rules (I→ , I∧ , I1∨ , I2∨ ). Such
nonparametric arguments of type constructors are called indices. In contrast, the
argument A of the type constructor A ⊢ s is a parameter since it is not instanti-
ated in the target types of the constructors. More precisely, the argument A is a
nonuniform parameter of the type constructor A ⊢ s since it is instantiated in some
argument types of some of the constructors (I→ , E∧ , and E∨ ).
We call inductive type definitions where the type constructor has indices indexed
inductive definitions. Indexed inductive definitions can also introduce indexed
inductive predicates. In fact, we alternatively could introduce ⊢ as an indexed
inductive predicate and this way demote derivations from computational objects to
proofs.
The suggestive BNF-style notation we have used so far to write inductive type
263
24 Propositional Deduction
In §24.9 we will show that ¬¬s ⊢ s is not derivable for some formulas s. In
particular, ¬¬s ⊢ s is not derivable if s is a variable. However, as the above proof
shows, ¬¬s ⊢ s is derivable for s = ⊥. This fact will play an important role.
The cut lemma gives us a function that given a derivation A ⊢ s and a derivation
A, s ⊢ t yields a derivation A ⊢ t. Informally, the cut lemma says that once we have
derived s from A, we can use s like an assumption.
Exercise 24.3.5 Prove the implicative facts (1)–(6) appearing in Exercise 24.11.6.
264
24.4 The Eliminator
265
24 Propositional Deduction
E⊢ : ∀p L(For)→For→T .
(∀As. s ∈ A → pAs) →
(∀As. pA⊥ → pAs) →
(∀Ast. p(s :: A)t → pA(s → t)) →
(∀Ast. pA(s → t) → pAs → pAt) →
(∀Ast. pAs → pAt → pA(s ∧ t)) →
(∀Astu. pA(s ∧ t) → p(s :: t :: A)u → pAu) →
(∀Ast. pAs → pA(s ∨ t)) →
(∀Ast. pAt → pA(s ∨ t)) →
(∀Astu. pA(s ∨ t) → p(s :: A)u → p(t :: A)u → pAu) →
∀As. A ⊢ s → pAs
266
24.5 Induction on Derivations
Next we show that premises of top level implications are interchangeable with
assumptions.
Design principles
The intuitionistic ND system satisfies the following design principles:
1. Modularity: Every logical constant is accommodated with introduction and elim-
ination rules where the constant appears once and no other constant appears.
2. Weakening: If s can be derived for A, s can be derived for every B containing all
formulas in A.
3. Agreement: Implication → agrees with the external turnstile ⊢.
Exercise 24.5.7 Prove the deduction laws for conjunctions and disjunctions:
a) A ⊢ (s ∧ t) a A ⊢ s × A ⊢ t
b) A ⊢ (s ∨ t) a ∀u. A, s ⊢ u → A, t ⊢ u → A ⊢ u
267
24 Propositional Deduction
[] · s := s
(t :: A) · s := t → (A · s)
Prove A ⊢ s a ⊢ A · s.
Hint: Prove the generalization ∀B. B +
+ A ⊢ s a B ⊢ A · s by induction on A.
A⊢⊥
A⊢s
A, ¬s ⊢ ⊥
A⊢s
with separate value constructors. Classical ND can prove the double negation law.
˙ (¬¬s → s).
Fact 24.6.1 (Double Negation) A ⊢
˙ s → A, s ⊢
Fact 24.6.2 (Cut) A ⊢ ˙ t → A⊢
˙ t.
268
24.6 Classical ND System
˙s →A⊆B →B⊢
Fact 24.6.3 (Weakening) A ⊢ ˙ s.
The classical system can prove the explosion rule. Thus every intuitionistic
˙ s.
derivation A ⊢ s can be translated into a classical derivation A ⊢
˙ ⊥ → A⊢
Fact 24.6.4 (Explosion) A ⊢ ˙ s.
˙ s.
Fact 24.6.5 (Translation) A ⊢ s → A ⊢
Proof By induction on A ⊢ s using the explosion lemma for the explosion rule. ■
˙ t a A⊢
Fact 24.6.6 (Implication) A, s ⊢ ˙ (s → t).
˙ s a ⊢ A · s.
Fact 24.6.7 (Reversion) A ⊢
Because of the contradiction rule the classical system has the distinguished prop-
erty that every proof problem can be turned into a refutation problem.
˙ s a A, ¬s ⊢
Fact 24.6.8 (Refutation) A ⊢ ˙ ⊥.
Proof Direction ⇒ follows with weakening. Direction ⇐ follows with the contradic-
tion rule. ■
While the refutation lemma tells us that classical ND can represent all infor-
mation in the context, the implication lemmas tell us that both intuitionistic and
classical ND can represent all information in the claim.
˙ s ∨ ¬s and ⊢
Exercise 24.6.10 Show ⊢ ˙ ((s → t) → s) → s.
Exercise 24.6.11 Prove the deduction laws for conjunctions and disjunctions:
˙ (s ∧ t) a A ⊢
a) A ⊢ ˙ s × A⊢
˙t
˙ (s ∨ t) a ∀u. A, s ⊢
b) A ⊢ ˙ u → A, t ⊢
˙ u → A⊢
˙u
269
24 Propositional Deduction
Exercise 24.6.12 Show that classical ND can express conjunction and disjunction
with implication and falsity. To do so, define a translation function f st not using
conjunction and prove ⊢˙ (s ∧ t → f st) and ⊢
˙ (f st → s ∧ t). Do the same for disjunc-
tion.
˙ ¬¬s → s
A⊢
˙⊥
A⊢ ˙s
A, s → t ⊢
E⊥ Peirce
˙s
A⊢ ˙s
A⊢
It turns out that a formula is classically provable if and only if its double negation
is intuitionistically provable. Thus a classical provability problem can be reduced
to an intuitionistic provability problem.
270
24.7 Glivenko’s Theorem
˙ s → A ⊢ ¬¬s.
Lemma 24.7.1 A ⊢
Proof By induction on A ⊢ ˙ s. This yields the following proof obligations (the obli-
gations for conjunctions and disjunctions are omitted).
• s ∈ A → A ⊢ ¬¬s
• A, ¬s ⊢ ¬¬⊥ → A ⊢ ¬¬s.
• A, s ⊢ ¬¬t → A ⊢ ¬¬(s → t)
• A ⊢ ¬¬(s → t) → A ⊢ ¬¬s → A ⊢ ¬¬t
Using rule E→ of the intuitionistic system, the obligations can be strengthened to:
• ⊢ s → ¬¬s
• ⊢ (¬s → ¬¬⊥) → ¬¬s
• ⊢ (s → ¬¬t) → ¬¬(s → t)
• ⊢ ¬¬(s → t) → ¬¬s → ¬¬t.
The proofs of the strengthened obligations are routine (Exercise 24.3.3). ■
˙ s a A ⊢ ¬¬s.
Theorem 24.7.2 (Glivenko) A ⊢
Proof Direction ⇒ follows with Lemma 24.7.1. Direction ⇐ follows with translation
(24.6.5) and double negation (24.6.1). ■
˙ ¬s a A ⊢ ¬s.
Corollary 24.7.3 (Agreement on negated formulas) A ⊢
271
24 Propositional Deduction
Hilbert systems are deduction systems predating ND systems.2 They are simpler
than ND systems in that they come without assumption management. While it is
virtually impossible for humans to write proofs in Hilbert systems, one can con-
struct compilers translating derivations in ND systems into derivations in Hilbert
systems.
To ease our presentation, we restrict ourselves in this section to formulas not
containing conjunctions and disjunctions. Since implications are the primary con-
nective in Hilbert systems and conjunctions and disjunctions appear as extensions,
adding conjunctions and disjunctions will be an easy exercise.
We consider an intuitionistic Hilbert system formalized with an inductive type
constructor H : For → T and the derivation rules
H (s → t) H (s)
HMP HK
H (t) H (s → t → s)
HS H⊥
H ((s → t → u) → (s → t) → s → u) H (⊥ → s)
There are a single two-premise rule called modus ponens and three premise-free
rules called axiomatic rules. So all the action comes with modus ponens, which
puts implication into the primary position. Note that the single argument of the
type constructor H comes out as an index.
A Hilbert system internalizes the assumption list of the ND system using impli-
cation. It keeps the elimination rule for implications (now called modus ponens)
but reformulates all other rules as axiomatic rules using implication. Surprisingly,
only two rules (HK and HS ) suffice to simulate the assumption management and the
introduction rule for implication. The axiomatic rules for conjunction and disjunc-
tion follow the ND rules and the translation scheme we see in the falsity elimination
rule H⊥ and come with the following conclusions:
s →t →s∧t
s ∧ t → (s → t → u) → u
s →s∨t
t →s∨t
s ∨ t → (s → u) → (t → u) → u
2
Hilbert systems are also known as axiomatic systems. They originated with Gottlob Frege before
they were popularized by David Hilbert and coworkers.
272
24.8 Intuitionistic Hilbert System
Hð
S Hð
⊥
A ð (s → t → u) → (s → t) → s → u Að⊥→s
Proof By induction on the derivation of H (s). The modus ponens rule can be
simulated with E→ , and the conclusions of the axiomatic rules are all easily derivable
in the intuitionistic system. ■
The other direction of the equivalence proof (completeness for ND) is challenging
since it has to internalize the assumption management of the ND system. We will
see that this can be done with the axiomatic rules HK and HS . We remark that
the conclusions of HK and HS may be seen as types for the functions λxy.x and
λf gx.(f x)(gx).
The completeness proof uses the generalized Hilbert system ð shown in Fig-
ure 24.4 as an intermediate system. Similar to the ND system, the generalized
Hilbert system maintains a context, but this time no rule modifies the context. The
assumption rule Hð A is the only rule reading the context. The context can thus be
accommodated as a uniform parameter of the type constructor ð.
273
24 Propositional Deduction
The next fact is the heart of the translation of ND derivations into Hilbert deriva-
tions. It is well-known in the literature under the name deduction theorem.
Fact 24.8.6 (Simulation function for I→ ) ∀Ast. (A, s ð t) → (A ð s → t).
Exercise 24.8.10 Extend the development of this section to formulas with conjunc-
tions and disjunctions. Add the axiomatic rules shown at the beginning of § 24.8.
Exercise 24.8.11 Define a classical Hilbert system and show its equivalence with
the classical ND system. Do this by replacing the axiomatic rule for ⊥ with an
axiomatic rule providing the double negation law ¬¬s → s.
274
24.9 Heyting Evaluation
The proof techniques we have seen so far do not suffice to show negative results
about the intuitionistic ND system. By a negative result we mean a proof saying that
a certain derivation type is empty, for instance,
̸⊢ ⊥ ̸⊢ x ̸⊢ (¬¬x → x)
(we write ̸⊢ s for the proposition ([] ⊢ s) → ⊥). Speaking informally, the above
propositions say that falsity, atomic formulas, and the double negation law for
atomic formulas are not intuitionistically derivable.
A powerful technique for showing negative results is evaluation of formulas into
a finite and ordered domain of so-called truth values. Things are arranged such that
all derivable formulas evaluate under all assignments to the largest truth value.3 A
formula can then be established as underivable by presenting an assignment under
which the formula evaluates to a different truth value.
Evaluation into the boolean domain 0 < 1 is well-known and suffices to disprove
⊢ ⊥ and ⊢ x. To disprove ⊢ (¬¬x → x), we need to switch to a three-valued
domain 0 < 1 < 2. Using the order of the truth values, we interpret conjunction
as minimum and disjunction as maximum. Falsity is interpreted as the least truth
value (i.e., 0). Implication of truth values is interpreted as a comparison that in the
positive case yields the greatest truth value 2 and in the negative case yields the
second argument:
Note that the given order-theoretic interpretations of the logical constants agree
with the familiar boolean interpretations for the two-valued domain 0 < 1. The
order-theoretic evaluation of formulas originated around 1930 with the work of
Arend Heyting.
We represent our domain of truth values 0 < 1 < 2 with an inductive type V and
the order of truth values with a boolean function a ≤ b. As a matter of convenience,
we write the numbers 0, 1, 2 for the value constructors of V. An assignment is a
function α : N → V. We define evaluation of formulas Eαs as follows:
E : (N → V) → For → V
Eαx := αx
Eα⊥ := 0
Eα(s → t) := if Eαs ≤ Eαt then 2 else Eαt
Eα(s ∧ t) := if Eαs ≤ Eαt then Eαs else Eαt
Eα(s ∨ t) := if Eαs ≤ Eαt then Eαt else Eαs
3
An assignment assigns a truth value to every atomic formula.
275
24 Propositional Deduction
For the proof we fix an assignment α and say that a formula s is true if Eαs = 2.
Next we verify that the conclusions of all axiomatic rules (see §24.8) are true, which
follows by case analysis on the truth values Eαs, Eαt, and Eαu. It remains to show
that modus ponens derives true formulas from true formulas, which again follows
by case analysis on the truth values Eαs and Eαt.
Proof By induction on the derivation H (s). The cases for the axiomatic rules follow
by case analysis on the truth values Eαs, Eαt, and Eαu. The case for modus
ponens follows by the inductive hypotheses and case analysis on the truth values
Eαs and Eαt. ■
E(λ_.1)⊥ = 0
E(λ_.1)x = 1
E(λ_.1)(¬x) = 0
E(λ_.1)(¬¬x) = 2
E(λ_.1)(¬¬x → x) = 1
̸ ˙ ⊥.
Corollary 24.9.4 (Consistency) ̸⊢ ⊥ and ⊢
Proof Intuitionistic consistency follows with Corollary 24.9.2 and the assignment
λ_.1. Classic consistency follows with equiconsistency (Corollary 24.7.5). ■
276
24.9 Heyting Evaluation
Exercise 24.9.7 Show that classical ND is not sound for the Heyting interpretation:
¬(∀αs. ⊢˙ s → Eαs = 2).
˙ x and ⊢
Exercise 24.9.8 Disprove ⊢ ˙ ¬x.
˙ (s ∨ t) a ⊢
Exercise 24.9.9 Disprove ⊢ ˙s ∨ A ⊢
˙ t.
Exercise 24.9.10 (Heyting interpretation for ND system) One can define evalua-
tion of contexts such that (A ⊢ s) → EαA ≤ Eαs and Eα[] = 2.
a) Define evaluation of contexts as specified.
b) Show EαA ≤ Eαs → A = [] → Eαs = 2.
c) Prove (A ⊢ s) → EαA ≤ Eαs by induction on A ⊢ s.
Hint: Define evaluation of contexts such that contexts may be seen as conjunctions
of formulas.
¬x ∨ ¬¬x
(x → y) ∨ (y → x)
⊥ < a, b < ⊤
277
24 Propositional Deduction
E : (N → B) → For → B
Eαx := αx
Eα⊥ := false
Eα(s → t) := if Eαs then Eαt else true
Eα(s ∧ t) := if Eαs then Eαt else false
Eα(s ∨ t) := if Eαs then true else Eαt
It will be convenient to use the word clause for lists of formulas. It is well
known that boolean satisfiability of clauses is decidable. There exist various prac-
tical tools for deciding boolean satisfiability. We will develop a certifying decider
˙ s)
∀A. D(sat A) for satisfiability and refine it into a certifying decider ∀As. D(A ⊢
for classical ND.
Our decider ∀A. D(sat A) for boolean satisfiability will be based on boolean formula
decomposition. We describe boolean formula decomposition with the decompo-
sition table in Figure 24.5. One way to read the table is saying that a boolean
assignment satisfies the formula on the left if and only if it satisfies both or one of
278
24.11 Boolean Formula Decomposition
¬⊥ nothing
s∧t s and t
¬(s ∧ t) ¬s or ¬t
s∨t s or t
¬(s ∨ t) ¬s and ¬t
s→t ¬s or t
¬(s → t) s and ¬t
the possibly negated subformulas on the right. Formally we have the equivalences
sat α (¬⊥) ←→ ⊤
sat α (s ∧ t) ←→ sat α s ∧ sat α t
sat α (¬(s ∧ t)) ←→ sat α (¬s) ∨ sat α (¬t)
sat α (s ∨ t) ←→ sat α s ∨ sat α t
sat α (¬(s ∨ t)) ←→ sat α (¬s) ∧ sat α (¬t)
sat α (s → t) ←→ sat α (¬s) ∨ sat α t
sat α (¬(s → t)) ←→ sat α s ∧ sat α (¬t)
for all boolean assignment α. The equivalences follow with the de Morgan laws
Eα(s → t) = Eα(¬s ∨ t)
Eα(¬¬s) = Eα(s)
The decomposition table suggests an algorithm that given a list of formulas re-
places decomposable formulas with smaller formulas. This way we obtain from an
initial list A one or several decomposed lists A1 , . . . , An containing only formulas of
the forms
x, ¬x, ⊥
such that an assignment satisfies the initial list A if and only if it satisfies one of the
decomposed lists A1 , . . . , An . We may get more than one decomposed list since the
279
24 Propositional Deduction
decomposition rules for ¬(s ∧ t), s ∨ t and s → t are branching (see Figure 24.5). For
a decomposed list, we can either construct an assignment satisfying all its formulas,
or prove that no such satisfying assignment exists. Put together, this will give us a
certifying decider ∀A. D(sat A).
We are now facing the challenge to give a formal account of boolean formula
decomposition. We do this with two derivation systems σ (A) and ρ(A) for clauses
shown in Figures 24.6 and 24.7. Like the derivation systems we have seen before,
σ (A) and ρ(A) can be formalized as inductive type families L(For) → T. We will
see that σ derives all satisfiable clauses and ρ derives all unsatisfiable clauses. We
define the application conditions for the terminal rules as follows:
• solved A := ∀s ∈ A. Σx. (s = x ∧ ¬x ∉ A) + (s = (¬x) ∧ x ∉ A)
Every formula in A is either a variable or a negated variable and there is no
clash x ∈ A ∧ ¬x ∈ A.
• clashed A := ⊥ ∈ A + Σs. s ∈ A ∧ (¬s) ∈ A
A contains either ⊥ or a clash s ∈ A ∧ ¬s ∈ A.
Except for the terminal rule and the second so-called rotation rule, the derivation
rules of both systems correspond to the decomposition schemes in Figure 24.5. The
relationship with the decomposition rules becomes clear if one reads the derivation
rules backwards from the conclusion to the premises. If a scheme decomposes
with an “or”, this translates for σ to two rules and for ρ to one rule with two
premises. The rotation rule (second rule in both systems) makes it possible to move
a decomposable formula into head position, as required by the decomposition rules.
The informal design rational for the rules of ρ is as follows: An assignment
satisfies the conclusion of the rule if and only if it satisfies one premise of the rule.
Proof By induction on the derivation of σ A exploiting that solved clauses are sat-
isfiable, and that for every recursive rule assignments satisfying the premise satisfy
the conclusion. ■
280
24.11 Boolean Formula Decomposition
solved A σ (A +
+[s]) σ (A)
σ (A) σ (s :: A) σ (¬⊥ :: A)
σ (s :: t :: A) σ (¬s :: A) σ (¬t :: A)
σ (s ∧ t :: A) σ (¬(s ∧ t) :: A) σ (¬(s ∧ t) :: A)
σ (s :: A) σ (t :: A) σ (¬s :: ¬t :: A)
σ (s ∨ t :: A) σ (s ∨ t :: A) σ (¬(s ∨ t) :: A)
σ (¬s :: A) σ (t :: A) σ (s :: ¬t :: A)
σ (s → t :: A) σ (s → t :: A) σ (¬(s → t) :: A)
clashed A ρ(A +
+[s]) ρ(A)
ρ(A) ρ(s :: A) ρ(¬⊥ :: A)
281
24 Propositional Deduction
˙ ⊥) → (t :: A ⊢
(¬s :: A ⊢ ˙ ⊥) → (s → t :: A ⊢
˙ ⊥)
Exercise 24.11.5 Define the derivation systems σ and ρ as inductive type families
and say whether the arguments are parameters or an indices.
Exercise 24.11.6 Verify the proof of ND soundness (Lemma 24.11.4) in detail. The
proof is modular in that there is a separate proof obligation for every rule of the
refutation systems (Figure 24.7). The obligation for the rotation rule
(A +
+[s] ⊢ ⊥) → (s :: A ⊢ ⊥)
follows with weakening, and the obligations for the terminal rules are obvious.
The obligations for the decomposition rules follow with the implication lemma
(Fact 24.5.2) and the derivability of the ND judgments from Exercise 24.5.8.
We now construct a certifying solver ∀A. σ (A) + ρ(A). Given the soundness
theorems for σ and ρ, this solver yields certifying solvers ∀A. D(sat A) and
∀A. sat A+(A ⊢ ˙ ⊥). The main issue in constructing the basic solver ∀A. σ (A)+ρ(A)
is finding a terminating strategy for formula decomposition.
282
24.12 Certifying Boolean Solvers
decomposable(A) := ΣBsC. A = B +
+ s :: C ∧ decomposable(s)
counting the constructors in the formulas in the list but omitting top-level nega-
tions. For instance,
Note that ¬x counts 1 if appearing at the top level, but 3 if not appearing at the top
level (since ¬x abbreviates x → ⊥). We observe that every scheme in the decompo-
sition table (Figure 24.5) reduces the size of a clause as obtained with γ.
Next we obtain a certifying function rotating a given formula in a clause to the
front of the clause such that derivability with σ and τ is propagated and the size
of the clause is preserved.
We now come to the theorem we were aiming at in this and the previous section.
283
24 Propositional Deduction
Exercise 24.12.6 From our development it is clear that a solver ∀A. sat A + (A ⊢ ˙ ⊥)
can be constructed without making the derivation systems σ and ρ and the accom-
panying soundness lemmas explicit. Try to rewrite the existing Coq development
accordingly. This will lead to a shorter (as it comes to lines of code) but less trans-
parent proof.
˙ s.
Proof By induction on the derivation A ⊢ ■
˙ s agree.
Corollary 24.13.3 Boolean entailment A î̇ s and classical ND A ⊢
284
24.14 Cumulative Refutation System
Proof Direction → is easy. For the other direction we assume sat(¬s :: A) → ⊥ and
sat αA and show Eαs = true. We now assume Eαs = false and obtain a contradic-
tion from our assumptions since Eα(¬s) = true. ■
˙ s).
Fact 24.13.5 (ND decidability) ∀As. D(A ⊢
Proof With the ND solver (Theorem 24.12.4) we obtain sat(¬s :: A) + (¬s :: A ⊢˙ ⊥).
˙ ⊥, we have A ⊢
If ¬s :: A ⊢ ˙ s by the contradiction rule. Otherwise, we assume
˙ s and obtain a contradiction with soundness (Fact 24.13.1) and
sat(¬s :: A) and A ⊢
Fact 24.13.4. ■
Exercise 24.13.6 Note that the results in this section did not use results from the
previous two sections except for the ND solver (Theorem 24.12.4). Prove the follow-
ing facts using the results from this section and possibly the ND solver.
a) ∀As. D(A î̇ s)
b) ∀A. D(sat A)
˙ ⊥) → ⊥)
c) ∀A. sat A a ((A ⊢
Exercise 24.13.7 Give a consistency proof for classical ND that does not make use
of intuitionistic ND.
˙.
Exercise 24.13.8 Show that x and ¬x are independent in ⊢
˙.
Exercise 24.13.9 Show that ¬¬¬x is independent in ⊢
˙ (s ∨ t) → ( ⊢
Exercise 24.13.10 Show (∀st. ⊢ ˙ s) ∨ ( ⊢
˙ t)) → ⊥.
285
24 Propositional Deduction
⊥∈A s∈A ¬s ∈ A
γ(A) γ(A)
Proof By induction on the derivation γ(A). Similar to the proof of Fact 24.11.3. ■
The rules of the cumulative refutation system yield a method for refuting formu-
las working well with pen and paper. We demonstrate the method at the example
of the unsatisfiable formula ¬(((s → t) → s) → s).
286
24.15 Substitution
Exercise 24.14.5 Refute the negations of the following formulas with the cumula-
tive refutation system writing a table as in the example above.
a) s ∨ ¬s e) ⊢ (t → ¬s) → ¬(s ∧ t)
b) s → ¬¬s f) ⊢ ¬¬s → ¬¬t → ¬¬(s ∧ t)
c) ⊢ ¬¬s → ¬t → ¬(s → t) g) ⊢ ¬s → ¬t → ¬(s ∨ t)
d) ⊢ (¬t → ¬s) → ¬¬(s → t) h) ⊢ (¬t → ¬¬s) → ¬¬(s ∨ t)
Exercise 24.14.6 (Saturated lists) A list A is saturated if the decomposition rules
of the cumulative refutation system do not add new formulas:
1. If (s ∧ t) ∈ A, then s ∈ A and t ∈ A.
2. If ¬(s ∧ t) ∈ A, then ¬s ∈ A or ¬t ∈ A.
3. If (s ∨ t) ∈ A, then s ∈ A or t ∈ A.
4. If ¬(s ∨ t) ∈ A, then ¬s ∈ A and ¬t ∈ A.
5. If (s → t) ∈ A, then ¬s ∈ A or t ∈ A.
6. If ¬(s → t) ∈ A, then s ∈ A and ¬t ∈ A.
Prove that an assignment α satisfies a saturated list A not containing ⊥ if it satisfies
all atomic formulas (x and ¬x) in A.
Hint: Prove
by induction on s.
24.15 Substitution
In the deduction systems we consider in this chapter, atomic formulas act as vari-
ables for formulas. We will now show that derivability of formulas is preserved if
one instantiates atomic formulas. To ease our language, we call atomic formulas
propositional variables in this section.
A substitution is a function θ : N → For mapping every number to a formula.
Recall that propositional variables are represented as numbers. We define applica-
tion of substitutions to formulas and lists of formulas such that every variable is
287
24 Propositional Deduction
θ·x := θx
θ·⊥ := ⊥
θ·(s → t) := θ·s → θ·t
θ·(s ∧ t) := θ·s ∧ θ·t
θ·(s ∨ t) := θ·s ∨ θ·t
θ·[] := []
θ·(s :: A) := θ·s :: θ·A
Proof By induction on A. ■
˙ s → θA ⊢
Fact 24.15.2 (Substitutivity) A ⊢ s → θA ⊢ θs and A ⊢ ˙ θs.
ð: L(For) → For → P
satisfying the properties listed in Figure 24.9. Note that the first five requirements
don’t make any assumptions on formulas; they are called structural requirements.
Each of the remaining requirements concerns a particular form of formulas: Vari-
ables, falsity, implication, conjunction, and disjunction.
4
We are reusing the turnstile ð previously used for Hilbert systems.
288
24.16 Entailment Relations
1. Assumption: s ∈ A → A ð s.
2. Cut: A ð s → A, s ð t → A ð t.
3. Weakening: A ð s → A ⊆ B → B ð s.
4. Consistency: ∃s. ̸ð s.
5. Substitutivity: A ð s → θA ð θs.
6. Explosion: A ð ⊥ → A ð s.
7. Implication: A ð (s → t) ←→ A, s ð t.
8. Conjunction: A ð (s ∧ t) ←→ A ð s ∧ A ð t.
9. Disjunction: A ð (s ∨ t) ←→ ∀u. A, s ð u → A, t ð u → A ð u.
˙ s) are
Fact 24.16.1 Intuitionistic provability (A ⊢ s) and classical provability (A ⊢
entailment relations.
Proof Follows with Fact 24.16.1 since boolean entailment agrees with classical ND
(Fact 24.13.3). ■
Fact 24.16.5 ð s → ð ¬s → ⊥.
Proof Let ð s and ð ¬s. By Fact 24.16.3 we have ð ⊥. By consistency and explosion
we obtain a contradiction. ■
289
24 Propositional Deduction
We now come to the key lemma for showing that abstract entailment implies
boolean entailment. The lemma was conceived by Tobias Tebbi in 2015. We define a
conversion function that given a boolean assignment α : N → B yields a substitution
as follows: α̂n := if αn then ¬⊥ else ⊥.
Proof Induction on s using Fact 24.16.3 and assumption, weakening, explosion, and
implication. ■
Note that we have formulated the lemma with a conditional. While this style of
formulation is uncommon in mathematics, it is compact and convenient in a type
theory with computational equality.
Lemma 24.16.8 ð s → î̇ s.
Proof Follows with reversion (Facts 24.16.6 and 24.16.2) and Lemma 24.16.8. ■
˙.
Theorem 24.16.10 (Sandwich) Every entailment relation ð satisfies ⊢ ⊆ ð ⊆ ⊢
24.17 Notes
The study of natural deduction originated in the 1930’s with the work of Gerhard
Gentzen [14, 15] and Stanisław Jaśkowski [19]. The standard text on natural deduc-
tion and proof theory is Troelstra and Schwichtenberg [28].
290
24.17 Notes
Certifying Functions The construction of the certifying solvers and their auxil-
iary functions in this chapter are convincing examples for the efficiency and power
of certifying functions. Imagine you would have to carry out these constructions
in a functional programming language with simply typed functions defined with
equations based on informal specifications.
291
25 Boolean Satisfiability
We study satisfiability of boolean formulas by constructing and verifying a DNF
solver and a tableau system. The solver translates boolean formulas to equivalent
clausal DNFs and thereby decides satisfiability. The tableau system provides a proof
system for unsatisfiability and bridges the gap between natural deduction and satis-
fiability. Based on the tableau system one can prove completeness and decidability
of propositional natural deduction.
The development presented here works for any choice of boolean connectives.
The independence from particular connectives is obtained by representing conjunc-
tions and disjunctions with lists and negations with signs.
The (formal) proofs of the development are instructive in that they showcast
the interplay between evaluation of boolean expressions, nontrivial functions, and
indexed inductive type families (the tableau system).
We will work with the boolean operations conjunction, disjunction, and negation,
which we obtain as inductive functions B → B → B and B → B:
and conjunctions can be pushed below disjunctions with the distribution law
293
25 Boolean Satisfiability
Besides the defining equations, we will also make use of the negation law
b ∧ ! b = false
to eliminate conjunctions.
There are the reflection laws
which offer the possibility to replace boolean operations with logical connectives.
As it comes to proofs, this is usually not a good idea since the computation rules
coming with the boolean operations are lost. The exception is the reflection rule
for conjunctions, which offers the possibility to replace the argument terms of a
conjunction with true.
Our main interest will be in boolean formulas, which are syntactic representations
of boolean terms. We will consider the boolean formulas
s, t, u : For ::= x | ⊥ | s → t | s ∧ t | s ∨ t (x : N)
realized with an inductive data type For representing each syntactic form with a
value constructor. Variables x are represented as numbers. We will refer to formu-
las also as boolean expressions.
Our development would work with any choice of boolean connectives for formu-
las. We have made the unusual design decision to have boolean implication as an
explicit connective. On the other hand, we have omitted truth ⊤ and negation ¬,
which we accommodate at the meta level with the notations
⊤ := ⊥ → ⊥ ¬s := s → ⊥
294
25.2 Boolean Formulas
Eαx := αx
Eα⊥ := false
Eα(s → t) := ! Eαs | Eαt
Eα(s ∧ t) := Eαs & Eαt
Eα(s ∨ t) := Eαs | Eαt
295
25 Boolean Satisfiability
We are working towards a decider for satisfiability of boolean formulas. The decider
will compute a DNF (disjunctive normal form) for the given formula and exploit
that from the DNF it is clear whether the formula is decidable. Informally, a DNF
is either the formula ⊥ or a disjunction s1 ∨ · · · ∨ sn of solved formulas si , where
a solved formula is a conjunction of variables and negated variables such that no
variable appears both negated and unnegated. One can show that every formula is
equivalent to a DNF. Since every solved formula is satisfiable, a DNF is satisfiable if
and only if it is different from ⊥.
There may be many different DNFs for satisfiable formulas. For instance, the
DNFs x ∨¬x and y ∨¬y are equivalent since they are satisfied by every assignment.
Formulas by themselves are not a good data structure for computing DNFs of
formulas. We will work with lists of signed formulas we call clauses:
Note that the empty clause represents the boolean true. We also consider lists of
clauses
∆ : L(Cla)
Eα [] := false
Eα (C :: ∆) := EαC | Eα∆
296
25.4 DNF Solver
satisfying assignments, we can speak about equivalence between these objects al-
though they belong to different types. For instance, s, s + , [s + ], and [[s + ]], are all
equivalent since they are satisfied by the same assignments.
A solved clause is a clause consisting of signed variables (i.e., x + and x − ) such
that no variable appears positively and negatively. Note that a solved clause C is
satisfied by every assignment that maps the positive variables in C to true and the
negative variables in C to false.
Fact 25.3.1 Solved clauses are satisfiable. More specifically, a solved clause C is
satisfied by the assignment λx. [x + ∈ C\.
+ ∆′ ) = Eα∆ | Eα∆′ .
+ D) = EαC & EαD and Eα(∆ +
Exercise 25.3.3 Prove Eα(C +
Exercise 25.3.4 Write a function that maps lists of clauses to equivalent formulas.
Exercise 25.3.5 Our formal proof of Fact 25.3.1 is unexpectedly tedious in that it
requires two inductive lemmas:
1. αsatC ←→ ∀S ∈ C. αsatS.
2. solved C → S ∈ C → ∃x. (S = x + ∧ x − ∉ C) ∨ (S = x − ∧ x + ∉ C).
The formal development captures solved clauses with an inductive predicate. This
is convenient for most purposes but doesn’t provide for a convenient proof of
Fact 25.3.1. Can you do better?
We would like to construct a function computing clausal DNFs for formulas. For-
mally, we specify the function with the informative type
∀s Σ ∆. DNF ∆ ∧ s ≡ ∆
where
297
25 Boolean Satisfiability
dnf C [] = [C]
dnf C (x + :: D) = if [x − ∈ C\ then [] else dnf (x + :: C) D
dnf C (x − :: D) = if [x + ∈ C\ then [] else dnf (x − :: C) D
dnf C (⊥+ :: D) = []
dnf C (⊥− :: D) = dnf C D
dnf C ((s → t)+ :: D) = dnf C (s − :: D) +
+ dnf C (t + :: D)
dnf C ((s → t)− :: D) = dnf C (s + :: t − :: D)
dnf C ((s ∧ t)+ :: D) = dnf C (s + :: t + :: D)
dnf C ((s ∧ t)− :: D) = dnf C (s − :: D) +
+ dnf C (t − :: D)
dnf C ((s ∨ t)+ :: D) = dnf C (s + :: D) +
+ dnf C (t + :: D)
dnf C ((s ∨ t)− :: D) = dnf C (s − :: t − :: D)
specified with equations in Figure 25.2. We refer to the first argument C of the
procedure as accumulator, and to the second argument as agenda. The agenda
holds the signed formulas still to be processed, and the accumulator collects signed
variables taken from the agenda. The procedure processes the formulas on the
agenda one by one decreasing the size of the agenda with every recursion step. We
define the size of clauses and formulas as follows:
σ [] := 0 σ x := 1
+
σ (s :: C) := σ s + σ C σ ⊥ := 1
σ (s − :: C) := σ s + σ C σ (s ◦ t) := 1 + σ s + σ t
Note that the equations specifying the procedure in Figure 25.2 are clear from the
correctness properties stated for the procedure, the design that the first formula on
the agenda controls the recursion, and the boolean identities given in §25.1.
Lemma 25.4.1 ∀CD. solved C → Σ ∆. DNF ∆ ∧ C +
+ D ≡ ∆.
298
25.5 DNF Recursion
Exercise 25.4.8 Rewrite the equations specifying the DNF procedure so that you
obtain a boolean decider D : Cla → Cla → B for satisfiability of clauses. Give an in-
formative type subsuming the procedure and specifying the correctness properties
for a boolean decider for satisfiability of clauses.
Exercise 25.4.9 Recall the definition of valid formulas from Exercise 25.2.6. Prove
the following:
a) Validity of formulas is decidable.
b) A formula is satisfiable if and only if its negation is not valid.
c) ∀s. val s + (Σα. Eαs = false).
Exercise 25.4.10 If you are already familiar with well-founded recursion in compu-
tational type theory (Chapter 30), define a function Cla → Cla → L(Cla) satisfying
the equations specifying the procedure dnf in Figure 25.2.
From the equations for the DNF procedure (Figure 25.2) and the construction of the
basic DNF solver (Lemma 25.4.1) one can abstract out the recursion scheme shown
in Figure 25.3. We refer to this recursion scheme as DNF recursion. DNF recursion
has one clause for every equation of the DNF procedure in Figure 25.2 where the
recursive calls appear as inductive hypotheses. DNF recursion simplifies the proof
of Lemma 25.4.1. However, DNF recursion can also be used for other constructions
(our main example is a completeness lemma (25.6.5) for a tableau system) given
that it is formulated with an abstract type function p. Note that DNF recursion
encapsulates the use of size induction on the agenda, the set-up and justification
of the case analysis, and the propagation of the precondition solved C. We remark
that all clauses can be equipped with the precondition, but for our applications the
precondition is only needed in the clause for the empty agenda.
299
25 Boolean Satisfiability
∀p Cla→Cla→T
(∀C. solved C → pC[]) →
(∀CD. x − ∈ C → pC(x + :: D)) →
(∀CD. x − ∉ C → p(x + :: C)D → pC(x + :: D)) →
(∀CD. x + ∈ C → pC(x − :: D)) →
(∀CD. x + ∉ C → p(x − :: C)D → pC(x − :: D)) →
(∀CD. pC(⊥+ :: D)) →
(∀CD. pCD → pC(⊥− :: D)) →
(∀CD. pC(s − :: D) → pC(t + :: D) → pC((s → t)+ :: D)) →
(∀CD. pC(s + :: t − :: D) → pC((s → t)− :: D)) →
(∀CD. pC(s + :: t + :: D) → pC((s ∧ t)+ :: D)) →
(∀CD. pC(s − :: D) → pC(t − :: D) → pC((s ∧ t)− :: D)) →
(∀CD. pC(s + :: D) → pC(t + :: D) → pC((s ∨ t)+ :: D)) →
(∀CD. pC(s − :: t − :: D) → pC((s ∨ t)− :: D)) →
∀CD. solved C → pCD
Proof By size induction on the σ D with C quantified using the decidability of mem-
bership in clauses. Straightforward. ■
DNF recursion provides the abstraction level one would use in an informal cor-
rectness proof of the DNF procedure. In particular, DNF recursion separates the
termination argument from the partial correctness argument. We remark that DNF
recursion generalizes the functional induction scheme one would derive for a DNF
procedure.
Exercise 25.5.2 Use DNF recursion to construct a certifying boolean solver for
clauses: ∀C. (Σα. αsatC) + (¬sat(C)).
300
25.6 Tableau Refutations
tab(S :: C +
+ D)
+ S :: D)
tab(C + tab(x + :: x − :: C) tab(⊥+ :: C)
Figure 25.4 defines an indexed inductive type family tab : Cla → T for which we will
prove
tab(C) a ¬sat(C)
We call the inhabitants of a type tab(C) tableau refutations for C. The above equiv-
alence says that for every clause unsatisfiability proofs are inter-translatable with
tableau refutations. Tableau refutations may be seen as explicit syntactic unsatis-
fiability proofs for clauses. Since we have ¬sat s a ¬sat [s + ], tableau refutations
may also serve as refutations for formulas.
We speak of tableau refutations since the type family tab formalizes a proof
system that belongs to the family of tableau systems. We call the value construc-
tors for the type constructor tab tableau rules and refer to type constructor tab as
tableau system.
We may see the tableau rules in Figure 25.4 as a simplification of the equations
specifying the DNF procedure in Figure 25.2. Because termination is no longer an
issue, the accumulator argument is not needed anymore. Instead we have a tableau
rule (the first rule) that rearranges the agenda.
We refer to the first rule of the tableau system as move rule and to the second
rule as clash rule. Note the use of list concatenation in the move rule.
The tableau rules are best understood in backwards fashion (from the conclusion
to the premises). All but the first rule are decomposition rules simplifying the
clause to be derived. The second and third rule derive clauses that are obviously
unsatisfiable. The move rule is needed so that non-variable formulas can be moved
301
25 Boolean Satisfiability
For the completeness lemma we need a few lemmas providing derived rules for
the tableau system.
Fact 25.6.4 (Move Rules) The following rules hold for tab:
tab(rev D +
+C +
+ E) +C +
tab(D + + E) + S :: D)
tab(C +
+D+
tab(C + + E) +D+
tab(C + + E) tab(S :: C +
+ D)
Proof The first rule follows by induction on D. The second rule follows from the
first rule with C = [] and rev (rev D) = D. The third rule follows from the second
rule with C = [S]. ■
Proof By DNF recursion. The case for the empty agenda is contradictory since
solved clauses are satisfiable. The cases with conflicting signed variables follow
with the clash lemma. The cases with nonconflicting signed variables follow with
the inverse move rule. The case for ⊥− follows with the weakening lemma. ■
302
25.7 Abstract Refutation Systems
Theorem 25.6.6
A clause is tableau refutable if and only if it is unsatisfiable:
tab(C) a ¬sat(C).
We remark that the DNF solver and the tableau system adapt to any choice of
boolean connectives. We just add or delete cases as needed. An extreme case would
be to not have variables. That one can choose the boolean connectives freely is due
to the use of clauses with signed formulas.
The tableau rules have the subformula property, that is, a derivation of a
clause C does only employ subformulas of formulas in C. That the tableau rules
satisfies the subformula property can be verified rule by rule.
+ S :: D +
Exercise 25.6.8 Prove tab(C + + T :: E) ←→ tab(C +
+ T :: D +
+ S :: E).
Exercise 25.6.9 Give an inductive type family deriving exactly the satisfiable
clauses. Start with an inductive family deriving exactly the solved clauses.
An unsigned clause is a list of formulas. We will now consider a tableau system for
unsigned clauses that comes close to the refutation system associated with natural
deduction. For the tableau system we will show decidability and agreement with un-
satisfiability. Based on the results for the tableau system one can prove decidability
and completeness of classical natural deduction (Chapter 24).
The switch to unsigned clauses requires negation and falsity, but as it comes to
the other connectives we are still free to choose what we want. Negation could be
accommodated as an additional connective, but formally we continue to represent
negation with implication and falsity.
We can turn a signed clause C into an unsigned clause by replacing positive
formulas s + with s and negative formulas s − with negations ¬s. We can also turn
an unsigned clause into a signed clause by labeling every formula with the positive
sign. The two conversions do not change the boolean value of a clause for a given
assignment. Moreover, going from an unsigned clause to a signed clause and back
yields the initial clause. From the above it is clear that satisfiability of unsigned
clauses reduces to satisfiability of signed clauses and thus is decidable.
Formalizing the above ideas is straightforward. The letters A and B will range
over unsigned clauses. We define αsatA and satisfiability of unsigned clauses anal-
ogous to signed clauses. We use Ĉ to denote the unsigned version of a signed clause
and A+ to denote the signed version of an unsigned clause.
303
25 Boolean Satisfiability
ρ (s :: A +
+ B)
ρ (A +
+ s :: B) ρ (x :: ¬x :: A) ρ (⊥ :: A)
ρ (¬s :: A) ρ (t :: A) ρ (s :: ¬t :: A)
ρ ((s → t) :: A) ρ (¬(s → t) :: A)
ρ (s :: t :: A) ρ (¬s :: A) ρ (¬t :: A)
ρ ((s ∧ t) :: A) ρ (¬(s ∧ t) :: A)
ρ (s :: A) ρ (t :: A) ρ (¬s :: ¬t :: A)
ρ ((s ∨ t) :: A) ρ (¬(s ∨ t) :: A)
Fact 25.7.5 A sound refutation system is decidable and derives exactly the unsatis-
fiable unsigned clauses.
304
25.7 Abstract Refutation Systems
Theorem 25.7.6 The minimal refutation system inductively defined with the rules
for abstract refutation systems derives exactly the unsatisfiable unsigned clauses.
Proof Follows with Fact 25.7.4 and a soundness lemma similar to Fact 25.6.1. ■
Exercise 25.7.7 (Certifying Solver) Construct a function ∀A. (Σα. αsatA) + tab A.
is decidable.
Exercise 25.7.9 Let A ⊢ ˙ s be the inductive type family for classical natural deduc-
˙ s is decidable and agrees with boolean entailment. Hint: Exploit
tion. Prove that A ⊢
refutation completeness and show that A ⊢ ˙ ⊥ is a refutation system.
305
26 Regular Expression Matching
We consider regular expressions describing list of numbers. The expressions are
formed with constructors for singleton lists, concatenation, star concatenation, and
union, among others. Using derivatives, we show that regular expression matching
is decidable.
26.1 Basics
Regular expressions are patterns for strings used in text search. There is a relation
A ⊢ s saying that a string A satisfies a regular expression s. One also speaks of a
regular expression matching a string. We are considering regular expressions here
since the satisfaction relation A ⊢ s has an elegant definition with derivation rules.
We represent strings as lists of numbers, and regular expressions with an induc-
tive type realizing the BNF
s, t : exp ::= x | 0 | 1 | s + t | s · t | s ∗ (x : N)
⊢ : L(N) → exp → T
Note that both arguments of ⊢ are indices. Concrete instances of the satisfaction
relation, for instance,
[1, 2, 2] ⊢ 1 · 2∗
s ⊆ t := ∀A. A ⊢ s → A ⊢ t
s ≡ t := ∀A. A ⊢ s a A ⊢ t
307
26 Regular Expression Matching
A ⊢ s · t → ΣA1 A2 . (A = A1 +
+ A2 ) × (A1 ⊢ s) × (A2 ⊢ t) (26.3)
and a lemma
A ⊢ s∗ → B ⊢ s∗ → A +
+ B ⊢ s∗ (26.4)
∀As. A ⊢ s → match s
[ x ⇒ A = [x]
|0⇒⊥
| 1 ⇒ A = []
| u + v ⇒ (A ⊢ u) + (A ⊢ v)
| u · v ⇒ Σ A1 A2 . (A = A1 +
+ A2 ) × (A1 ⊢ u) × (A2 ⊢ v)
| u∗ ⇒ (A = []) + ΣA1 A2 . (A = A1 +
+ A2 ) × (A1 ⊢ u) × (A2 ⊢ u∗ )
]
∀As. A ⊢ s → match s [ s ∗ ⇒ B ⊢ s ∗ → A +
+ B ⊢ s∗ | _ ⇒ ⊤ ]
308
26.1 Basics
s∗ · s∗ ≡ s∗
(s ∗ )∗ ≡ s ∗ (26.5)
saying that the star operation is idempotent. Given (26.1), it suffices to show
A ⊢ (s ∗ )∗ → A ⊢ s ∗ (26.6)
∀As. A ⊢ s → match s [ (s ∗ )∗ ⇒ A ⊢ s ∗ | _ ⇒ ⊤ ]
Exercise 26.1.2 (Restrictive star rule) The second derivation rule for star expres-
sions can be replaced with the more restrictive rule
x :: A ⊢ s B ⊢ s∗
+ B ⊢ s∗
x :: A +
Define an inductive family A ⊢ ˙ s adopting the more restrictive rule and show that
˙ s a A ⊢ s.
it is intertranslatable with A ⊢ s : ∀As. A ⊢
Exercise 26.1.3 After reading this section, do the following with a proof assistant.
a) Define a universal eliminator for A ⊢ s.
b) Define an inversion operator for A ⊢ s.
c) Prove s ∗ · s ∗ ≡ s ∗ .
d) Prove (s ∗ )∗ ≡ s ∗ .
Exercise 26.1.4 (Denotational semantics) The informal semantics for regular ex-
pressions described in textbooks can be formalized as a recursive function on reg-
ular expressions that assigns languages to regular expressions. We represent lan-
guages as type functions L(N) → T and capture the semantics with a function
R : exp → L(N) → T
309
26 Regular Expression Matching
defined as follows:
R x A := (A = [x])
R 0 A := ⊥
R 1 A := (A = [])
R (s + t) A := RsA + RtA
R (s · t) A := ΣA1 A2 . (A = A1 +
+ A2 ) × RsA1 × RtA2
R (s ∗ ) A := Σn. P (Rs)nA
P ϕ 0 A := (A = [])
P ϕ (Sn) A := ΣA1 A2 . (A = A1 +
+ A2 ) × ϕA1 × P ϕ n A
a) Prove R sA a A ⊢ s.
b) We have represented languages as type functions L(N) → T. A representation
as predicates L(N) → P would be more faithful to the literature. Rewrite the
definitions of ⊢ and R accordingly and show their equivalence.
∀xAs. x :: A ⊢ s a A ⊢ Dxs
In words we may say that a string x :: A satisfies a regular expression s if and only
if A satisfies the derivative Dxs. If we have a decider ∀s. D([] ⊢ s) and in addition
a derivation function, we have a decider for A ⊢ s.
Proof By induction on s. For 1 and s ∗ we have a positive answer, and for x and 0
we have a negative answer using the inversion function. For s + t and s · t we rely
on the inductive hypotheses for the constituents. ■
Proof By recursion on A using Fact 26.2.1 in the base case and the derivation func-
tion in the cons case. ■
310
26.2 Decidability of Regular Expression Matching
D : N → exp → exp
Dxy := if [x = y\ then 1 else 0
Dx 0 := 0
Dx 1 := 0
Dx (s + t) := Dxs + Dxt
Dx (s · t) := if [[] ⊢ s\ then Dxs · t + Dxt else Dxs · t
Dx (s ∗ ) := Dxs · s ∗
It remains to show that D is a derivation function. For this proof we need a strength-
ened inversion lemma for star expressions.
Proof By induction on the derivation of x :: A ⊢ s ∗ . Only the second rule for star
expressions applies. Hence we have x :: A = A1 + + A2 and subderivations A1 ⊢ s
and A2 ⊢ s ∗ . If A1 = [], we have A2 = x :: A and the claim follows by the inductive
hypothesis. Otherwise, we have A1 := x :: A′1 , which gives us the claim.
The formal proof follows this outline but works on a reformulation of the claim
providing an unconstrained inductive premise. ■
Proof By induction on s. All cases but the direction ⇒ for s ∗ follow with the inver-
sion operator and case analysis. The direction ⇒ for s ∗ follows with the eager star
inversion lemma 26.2.3. ■
311
27 Abstract Reduction Systems
R R R
x0 → x1 → · · · → xn
through the graph described by R following the edges. We capture this design
formally with an indexed inductive type
path (x : X) : X → T ::=
| P1 : path xx
| P2 : ∀x ′ y. Rxx ′ → path x ′ y → path xy
The constructors are chosen such that that the elements of a path type path xy
formalize the paths from x to y. The first argument of the type constructor path is
a nonuniform parameter and the second argument of path is an index. The second
argument cannot be made a parameter because it is instantiated to x by the value
constructor P1 . Here are the full types of the constructors:
path : ∀X T . (X → X → T) → X → X → T
P1 : ∀X T ∀R X→X→P ∀x X . pathXR xx
P2 : ∀X T ∀R X→X→P ∀xx ′ y X . Rxx ′ → pathXR x ′ y → pathXR xy
Note that the type constructor path takes three parameters followed by a single in-
dex as arguments. There is the general rule that parameters must go before indices.
We shall use notation with implicit arguments in the following. It is helpful to
see the value constructors in simplified form as inference rules:
Rxx ′ pathR x ′ y
P1 P2
pathR xx pathR xy
313
27 Abstract Reduction Systems
The second constructor is reminiscent of a cons for lists. The premise Rxx ′ ensures
that adjunctions are licensed by R. And, in contrast to plain lists, the endpoints of
a path are recorded in the type of the path.
Proof The function claimed can be obtained with the value constructors P1 and P2 :
P1
Rxy pathR yy
P2
pathR xy
We now define an inductive function len that yields the length of a path (i.e., the
number of edges the path runs trough).
Note the underlines in the patterns. The underlines after P1 and P2 are needed since
the first arguments of the constructors are parameters (instantiated to x by the pat-
tern). The underlines before the applications of P1 and P2 are needed since the re-
spective argument is an index argument. The index argument appears as variable y
in the type declared for len. We refer to y (in the type of len) as index variable.
What identifies y as index variable is the fact that it appears as index argument
in the type of the discriminating argument. The index argument must be written
as underline in the patterns since the succeeding pattern for the discriminating ar-
gument determines the index argument. There is the general constraint that the
index arguments in the type of the discriminating argument must be variables not
occurring otherwise in the type of the discriminating argument (the so-called index
condition). Moreover, the declared type must be such that all index arguments are
taken immediately before the discriminating argument.
Type checking elaborates the defining equations into quantified propositional
equations where the pattern variables are typed and the underlines are filled in. For
the defining equations of len, elaboration yields the following equations:
∀x N . len x x (P1 x) = 0
′ ′
∀xx ′ y N ∀r Rxx ∀apath x y . len x y (P2 x x ′ y r a) = S(len x ′ y a)
We remark that the underlines for the parameters are determined by the declared
type of the discriminating argument, and that the underlines for the index argu-
ments are determined by the elaborated type for the discriminating argument.
314
27.1 Paths Types
discriminating on the first path. The declared type and the choice of the discrimi-
nating argument (not explicit yet) identify y as an index variable and fix an index
argument for app. Note that the index condition is satisfied. The argument z is
taken first so that the index argument y can be taken immediately before the dis-
criminating argument. We can now write the defining equations:
As always, the patterns are determined by the declared type and the choice of the
discriminating argument. We have the types r : Rxx ′ and a : path x ′ y for the
respective pattern variables of the second equation. Note that the index argument
is instantiated to x in the first equation and to y in the second equation.
We would now like to verify the equation
which is familiar from lists. As for lists, the proof is by induction on a. Doing the
proof by hand, ignoring the type checking, is straightforward. After conversion, the
case for P2 gives us the proof obligation
which follows by the inductive hypothesis, Formally, the induction can be validated
with the universal eliminator for path:
path xy→T
E : ∀p ∀xy. .
(∀x. pxx(P1 x)) →
(∀xyz ∀r Rxy ∀apath yz . pxz(P2 xyz r a)) →
∀xya. pxya
E pe1 e2 x _ , (P1 _) := e1 x
E pe1 e2 x _ (P2 _ x ′ y r a) := e2 xx ′ y r (E pe1 e2 x ′ y a)
Not that the type function p takes the nonuniform parameter, the index, and the dis-
criminating argument as arguments. The general rule to remember here is that all
nonuniform parameters and all indices appear as arguments of the target type func-
tion of the universal eliminator. As always with universal eliminators, the defining
315
27 Abstract Reduction Systems
equations follow from the type of the eliminator, and the types of the continuation
functions e1 and e2 follow from the types of the value constructors and the type of
the target type function.
No doubt, type checking the above examples by hand is a tedious exercise, also
for the author. In practice, one leaves the type checking to the proof assistant
and designs the proofs assuming that the type checking works out. With trained
intuitions, this works out well.
Exercise 27.1.2 Give the propositional equations obtained by elaborating the defin-
ing equations for len, app, and E. Hint: The propositional equations for len are
explained above. Use the proof assistant to construct and verify the equations.
Exercise 27.1.3 Define the step function asserted by Fact 27.1.1 with a term.
∀p X→X→T .
(∀x. pxx) →
(∀xx ′ y. Rxx ′ → px ′ y → pxy) →
(∀xy. path xy → pxy)
Note that the type of the index eliminator is obtained from the type of the universal
eliminator by deleting the dependencies on the paths.
Exercise 27.1.5 Use the index eliminator to prove that the relation path is transi-
tive: ∀xyz. path xy → path yz → path xz.
Exercise 27.1.6 (Arithmetic graph) Let Rxy := (Sx = y). We can see R as the
graph on numbers having the edges (x, Sx). Prove pathR xy a x ≤ y.
Hints. Direction ⇒ follows with index induction (i.e., using the index eliminator
from Exercise 27.1.4). Direction ⇐ follows with ∀k. pathR x(k + x), which follows
by induction on k with x quantified.
We can see the type constructor path as a function that maps relations X → X → T
to relations X → X → T. We will write R ∗ for pathR in the following and speak
of the reflexive transitive closure of R. We will explain later why this speak is
meaningful.
We first note that R ∗ is reflexive. This fact is stated by the type of the value
constructor P1 .
316
27.2 Reflexive Transitive Closure
We also note that R ∗ is transitive. This fact is stated by the type of the inductive
function app.
Moreover, we note that R ∗ contains R (i.e., ∀xy. Rxy → R ∗ xy). This fact is
stated by Fact 27.1.1.
Fact 27.2.1 (Star recursion)
Every reflexive and transitive relation containing R contains R ∗ :
∀p X→X→T . refl p → trans p → R ⊆ p → R ∗ ⊆ p.
Proof Let p be a relation as required. We show ∀xy. R ∗ xy → pxy using the index
eliminator for path (Exercise 27.1.4). Thus we have to show that p is reflexive, which
holds by assumption, and that ∀xx ′ y. Rxx ′ → px ′ y → pxy. So we assume Rxx ′
and px ′ y and show pxy. Since p contains R we have pxx ′ and thus we have the
claim since p is transitive. ■
Star recursion as stated by Fact 27.2.1 is a powerful tool. The function realized
by star recursion is yet another eliminator for path. We can use star recursion to
show that R ∗ and (R ∗ )∗ agree.
Fact 27.2.2 R ∗ and (R ∗ )∗ agree.
Proof This fact is a reformulation of what we have just shown. On the one hand, it
says that R ∗ is a reflexive and transitive relation containing R. On the other hand,
it says that every such relation contains R ∗ . This is asserted by star recursion. ■
317
Part IV
Foundational Studies
319
28 Axiom CT and Semidecidability
321
28 Axiom CT and Semidecidability
Basic predicates are an abstract type-theoretic version of r.e. sets from set-theoretic
computability theory. R.e. sets are sets of numbers that can be algorithmically
enumerated. R.e. sets are also known recursively enumerable sets.
We define complement and equivalence of predicates p X→P and qX→P :
p := λx.¬px complement
p ≡ q := ∀x. px ←→ qx equivalence
T := N → B unary tests
T2 := N → N → B binary tests
T
Kf := ∃k. f k = true satisfiability
D f T2 := λn. K(f n) domain
N→P T2
basic p := ∃f . p ≡ Df basic predicates
We say that basic predicates are the domains of binary tests, and that basic predi-
cates are generated by binary tests.
Fact 28.1.1 Decidable predicates on numbers are basic: ∀p N→P . dec p → basic p.
Proof p is equivalent to the domain of the test λnk. if pn then true else false. ■
Proof Follows with an EWO for N (Fact 14.2.2) since boolean equality is decidable.■
322
28.2 UT and Undecidability
Proof We show that the complement of the domain of the diagonal binary test
λ[Link] is not basic. Suppose D f ≡ (λn.¬K(Unn)) for some binary test f . By UT
we have D f ≡ D (Uc) for some c. Hence ¬K(Ucc) ←→ D (Uc)c = K(Ucc), which is
contradictory. ■
Proof (1) follows from Fact 28.2.1 with p := D f . (2) follows from (1) since comple-
ments of decidable predicates are decidable and hence basic. ■
323
28 Axiom CT and Semidecidability
Proof (2) follows from (1) since complements of decidable predicates are decidable.
For (1), we assume a decider d : ∀f . ¬K f and derive a contradiction. We consider
the binary test f n := if d(Unn) then λ[Link] else λ[Link] with D f ≡ D (Uc).
We have K (f c) ←→ K (Ucc). We do a case analysis on d(Ucc). If ¬K (Ucc), f c =
λ[Link] and we have a contradiction. If ¬¬K (f c), f c = λ[Link] and we also have a
contradiction. ■
Note that in the above proof the diagonal test plays a key role again. Later
(Fact 28.8.5) we will see a different proof building on Fact 28.2.1 rather than UT.
Exercise 28.2.5 Assuming UT, prove that there is a basic predicate that cannot be
expressed as a unary test: UT → Σ p. basic p ∧ (¬∃f T ∀n. npn ←→ f n = true).
Exercise 28.2.6 Assuming UT, prove that there is a binary test f such that for every
binary test g whose domain is disjoint from the domain of f there exists a number n
such that neither f n nor gn is satisfiable.
Axiom CT will say that every basic predicate can be described with a diophantine
expression. CT implies that all basic predicates are generated by countably many
arithmetic expressions.
In what follows arithmetic pairing will be essential.
324
28.3 Diophantine Expressions
π :N→N→N π1 : N → N π2 : N → N
π1 (π n1 n2 ) = n1 π2 (π n1 n2 ) = n2 π (π1 n)(π2 n) = n
e : exp ::= x | 0 | 1 | e + e | e − e | e · e (x : N)
To evaluate an expression, we need values for the variables occurring in the expres-
sion. We assemble the values of variables into a single number using arithmetic
pairing:
cons n1 n2 := S(π n1 n2 )
get 0 _ := 0
get Sn 0 := π1 n
get Sn Sk := get (π2 n) k
Note that get nk gets the value for variable k from n. If n doesn’t provide an explicit
value for a variable, get returns 0 as default value.
We evaluate expressions with a function evaN→exp→N defined follows:
eva n x := get n x
eva n 0 := 0
eva n 1 := 1
eva n (e1 + e2 ) := eva n e1 + eva n e2
eva n (e1 − e2 ) := eva n e1 − eva n e2
eva n (e1 · e2 ) := eva n e1 · eva n e2
325
28 Axiom CT and Semidecidability
Proof Straightforward. ■
Exercise 28.3.4 (Binary universal test) UT models a universal test as a ternary test.
With arithmetic pairing, a universal test can be modeled equivalently with a binary
test. Prove UT a Σ g ∀f ∃c. D f ≡ D (λn.g(π cn)).
28.4 Axiom CT
CT := ∀f T2 ∃e. D f ≡ P e
326
28.4 Axiom CT
Proof Let δN→exp the surjective function provided by Fact 28.3.3. We verify that
U := λc. τ(δc) is a universal test. We assume f T2 and prove ∃c. D f ≡ D (Uc). CT
gives us an expression e such that D f ≡ P e. Since δ is surjective, we have δc = e
for some c. It remains to show D f ≡ D (Uc), which holds by the assumptions and
P e ≡ D (τe) (Fact 28.3.2 (1)). ■
Proof (1) follows with Facts 28.2.1 and 28.4.1. (2) is a consequence of (1), and (3) is
a consequence of (2). ■
Exercise 28.4.3
Prove CT → (basic p ←→ diophantine p) where diophantine p := ∃e. p ≡ Pe.
327
28 Axiom CT and Semidecidability
We may ask whether there are functional and total relations that cannot be repre-
sented with functions. Assuming UT, we will show that such relations exist. This
confirms our expectation that relations are more expressive than functions since
there is no computability constraint for relations.
We define the characteristic relation ρp of predicates p X→P :
ρ : ∀X. (X → P) → (X → B → P)
ρpxb := if b then px else ¬px
Fact 28.5.1 Characteristic relations are functional. Moreover, assuming XM, charac-
teristic relations are total.
Proof Let d be a decider for p. Then f x := if dx then true else false repre-
sents ρp. Conversely, let f represent ρp. Then f is a boolean decider for p. ■
Assuming UT and XM, we can now construct a functional and total relation
N→B→P
R that cannot be represented with a function. We speak of a recusant re-
lation.
Proof Fact 28.2.2 gives us an undecidable predicate p N→P . By Fact 28.5.2 we know
that ρp cannot be represented with a function. Moreover, Fact 28.5.1 tells us that
ρp is functional and total. ■
One must be extremely careful with axioms one adds to a foundational sytem. A
good example is
328
28.7 Post Hierarchy
TE := ∀f g T . (∀n. f n = gn) → f = g
Proof UTΣ gives us functions U N→T2 and γ T→N such that ∀f . K f ←→ K (U(γf ) 0).
We now assume f and construct a decision D(¬K f ). We consider the codes γf and
γ(λ[Link]).
If γf = γ(λ[Link]), we assume K f and derive a contradiction. We have K f ←→
K (λ[Link]), a contradiction.
If γf ≠ γ(λ[Link]), we assume ¬K f and prove γf = γ(λ[Link]). By the extension-
ality assumption it suffice to show ∀k. f k = false, which follows from ¬K f . ■
red : ∀XY . (X → P) → (Y → P) → (X → Y ) → P
redXY pqf := ∀x. p(x) ←→ q(f x)
1
The result appears in Troelstra and Van Dalen [29] and Forster [10].
329
28 Axiom CT and Semidecidability
p ç q := ¬(p ⪯ q)
p ≺ q := (p ⪯ q) × (p ç q)
p ≈ q := (p ⪯ q) × (q ⪯ p)
330
28.7 Post Hierarchy
Exercise 28.7.7 (General Tests) Besides unary and binary tests we may also con-
sider tests with three and more arguments. With arithmetic pairing we can show
that satisfiability of tests with n > 1 arguments is interreducible with satisfiabil-
ity of unary tests. What makes the problem interesting is its dependently typed
formalization. We define a family T N→T of test types
T0 := B
TSn := N → Tn
Tn → P
and a family sat∀n. of satisfaction predicates :
sat0 f := (f = true)
satSn f := ∃k. satn (f k)
331
28 Axiom CT and Semidecidability
D := λbB . b = true
sdecX p X→P := (p ⪯ K)
Fact 28.8.3
1. ∀f T2 . D f ⪯ K
2. D ⪯ K
3. D ⪯ K
4. K ⪯ D → K ⪯ K
332
28.8 Semidecidable Predicates
Fact 28.8.4
1. sdec K
2. p ⪯ q → sdec q → sdec p
3. dec p → sdec p
4. dec p → sdec p
5. ∀f T2 . sdec (D f )
We already know that K and K are undecidable (Fact 28.2.3). We now show
the stronger result that K is not semidecidable building on Fact 28.2.1. The proof
demonstrates the elegance of arguing within the Post hierarchy.
Proof (1) We have ¬ sdec D f for some binary test f by Fact 28.2.1. By contraposi-
tion we assume sdec K and prove sdec D f . It now suffices to prove D f ⪯ K, which
follows from D f ⪯ K.
(2) We have ¬ sdec K by (1). By contraposition we assume dec K and prove sdec K.
Easy since the assumption yields dec K and thus K ⪯ D ⪯ K. ■
Exercise 28.8.9 Prove that domain emptiness of binary tests is not semidecidable:
¬ sdec (λf T2 . ¬ex(D f )).
333
28 Axiom CT and Semidecidability
Proof Apply Fact 28.9.1 twice. Note that X ×N×N = (X ×N)×N, and that (x, n, k) =
((x, n), k). ■
It took several iterations until the author arrived at the formalization of arith-
metic projection appearing above. There are two essential design decisions: Assume
that the underlying predicate is semidecidable (rather than decidable) and use the
cartesian representation for predicates with more than one argument.
Using arithmetic pairing, we can obtain an EWO for basic predicates, a fact no-
ticed by Andrej Dudenhefner in 2020.
Fact 28.9.3 (EWO for basic predicates)
∀p N→P . sdec p → ex p → sig p.
334
28.10 Markov’s Principle
Exercise 28.9.5 (Arithmetic projection) Show the following using Fact 28.9.1:
a) ∀p X→N→P . (∀xn. D(pxn)) → sdec (λx. ex(px))
b) ∀p X×N×N×N→P . sdec p → sdec (λx. ∃k1 k2 k3 . p(x, k1 , k2 , k3 ))
MP := ∀f N→B . ¬¬K f → K f
335
28 Axiom CT and Semidecidability
Proof 1 → 2. Let ¬K f ←→ K g. Using Fact 28.10.3 and (1) we assume ¬satf |g and
derive a contradiction. Using the assumptions, we have ¬K f and ¬¬K f .
2 → 1. We assume ¬¬K f and prove K f . It suffices to prove K f + satλ[Link].
By (2) it suffices to prove ¬K f ←→ ⊥, which follows from the assumption ¬¬K f .
2 → 3. We assume P ←→ K f and ¬P ←→ K g and prove DP . It suffice to show
K f + K g, which follows with (2).
3 → 2. We assume ¬K f ←→ K g and prove K f + K g. We instantiate (3) with
P := K f and obtain DK f , which yields the claim. ■
Under MP, a predicate is decidable if and only if both the predicate and its com-
plement are semidecidable.
Proof Suppose f and g are semideciders for p and p. We fix x and construct
a decision D(px). The assumptions give us tests f x and gx characterizing px
and ¬px. Now Corollary 28.10.5 yields a decision D(px). ■
336
28.11 Promises
28.11 Promises
We call functions f N→OX promises. We see promises as tests with output. Given
a promise f , we can perform a linear search f 0, f 1, f 2, . . . until we find the first n
and x such that f n = ◦x. If the search terminates, we refer to n as the span and
to x as the value of f . Formally, we define the predicates
del f N→OX n := (f n ≠ )
span f N→OX n := least (del f ) n
δ f N→OX x n := span f n ∧ f n = ◦x
f ⇓ := ex (del f )
f ↓ x := ex (δf x)
Fact 28.11.1 f ↓ x → f ⇓.
337
28 Axiom CT and Semidecidability
1. f ⇓ → Σn. span f n
2. f ⇓ → Σx. f ↓ x
Proof (1) follows with an existential least witness operator (Fact 13.2.5) and the
decidability of del (Fact 28.11.3). (2) is a straightforward consequence of (1). ■
Fact 28.11.8 (Pruning) For every promise one can construct a value-equivalent
promise that delivers at most once:
∀f Σ f ′ . (∀x. f ↓ x ←→ f ′ ↓ x) ∧ (∀xn. f ′ n = ◦x → δf xn).
338
28.12 Promising Functions
PF XY := X → N → OY
Proof (1) follows with constant promises. (2) follows with the Skolem translation
and Fact 28.12.1 (2). ■
Proof We combine the spans for f x ↓ y and gy ↓ z with arithmetic pairing and
make use of the decidability of span:
339
28 Axiom CT and Semidecidability
Promising functions are semideciders with output. They can be seen as type-
theoretic representation of computable functions as they appear in set-theoretic
computability theory. We can translate between semideciders and promising func-
tions such that the domain is preserved.
Proof (1) follows with f xn := if gxn then ◦y else assuming y Y . (2) follows
with gxn := if f xn then true else false. ■
It follows that a predicate is semidecidable if and only if it agrees with the do-
main of a promising function.
∀x. Σ g. f x ↓ y ←→ K g
The test gn := if d(f x)yn then true else false where d decides δ (Fact 28.11.3)
does it. ■
Note that the definition of recusant states the nontotality of the extending func-
tion g existentially as ∃x. ¬gx⇓ rather than negatively as ¬(∀x. gx⇓). While the
340
28.13 Recusant Partial Deciders
existential version implies the negative version, it takes excluded middle for the
negative version to imply the existential version.
We call promising functions f PF X B partial deciders. Assuming UT, we will con-
struct a recusant partial decider. Recusant partial deciders capture the notion of
recursively inseparable sets from set-theoretic computability theory. If f PF N B is a
recusant partial decider, the predicates λx. f x ↓ true and λx. f x ↓ false represent re-
cursively inseparable sets of numbers. The idea to capture recursive inseparability
with partial deciders appears in Kirst and Peters [20].
Fact 28.13.1 (Decidable domain)
Promising functions with decidable domains have total extensions:
Y → ∀f PF XY . dec (λx. f x⇓) → Σg. (∀x. gx⇓) ∧ (∀xy. f x ↓ y → g ↓ y).
Proof Suppose y Y and ddec (λx. f x⇓) . Then gx := if dx then f x else λk.y is a
total extension of f . ■
Given a universal partial decider U, we show that the diagonal partial decider
λ[Link] is recusant.
Fact 28.13.4 (Recusant partial decider)
UPD → Σ f PF N B . recusant f .
Proof Let U be a universal partial decider. We show that the diagonal partial de-
cider λ[Link] is recusant. Suppose f PF N B is an extension of λ[Link]. Fact 28.13.3
gives us g PF N B such that ∀nb. f n ↓ b → gn ↓ ! b. Since U is universal, we have
∀nb. gn ↓ b ←→ Ucn ↓ b for some c. We now assume f c⇓ and derive a contradic-
tion. By Fact 28.11.4 (2) we have f c ↓ b. We instantiate the assumptions as follows:
Ucc ↓ ! b → f c ↓ ! b
gc ↓ ! b ←→ Ucc ↓ ! b
f c ↓ b → gc ↓ ! b
341
28 Axiom CT and Semidecidability
Exercise 28.13.5 We say that a partial decider f PF X B is sound for a predicate p X→P
if ∀x. if f x then px else ¬px. Show that a predicate is decidable if and only if it
has a sound and total partial decider.
We now construct a universal partial decider from a universal test. The idea is as
follows: Given a partial decider f PF N B , we slice it into two binary tests g1 and g2
such that the domains of g1 and g2 agree with λn.f n ↓ true and λn.f n ↓ false.
From the slices g1 and g2 we can rebuild the partial decider f . The universal partial
decider will now use the code π c1 c2 for a partial decider whose slices have the
codes c1 and c2 for the given universal test.
We define a function γf g combining two unary tests into a boolean promise:
γ : (N → B) → (N → B) → (N → OB)
γf g := λn. if f n then ◦true else if gn then ◦false else
Proof Direction → follows with Fact 28.14.1. We prove direction ← for b = true, the
other case is analogous.
We assume f ∥ g and K f and prove γf g ↓ true. The assumption gives us f n = true
for some n and thus γf g⇓. Now γf g ↓ b for some b with Fact 28.11.4 (2). This
closes the proof since γf g ↓ false is contradictory by Fact 28.14.1 and f ∥ g. ■
and show
∀f PF N B ∃c ∀nb. f n ↓ b ←→ V cn ↓ b
342
28.15 Notes
We assume f PF N B and slice it into binary tests g1 and g2 using Fact 28.12.7:
We now exploit the universality of U and obtain the codes for g1 and g2 :
We now show
f n ↓ b ←→ V (π c1 c2 )n ↓ b
f n ↓ b ←→ γ(Uc1 n)(Uc2 n) ↓ b
We now verify Uc1 n ∥ Uc2 n using uniqueness of delivery for f n (Fact 28.11.2) and
reduce the claim to
using Fact 28.14.2. This closes the proof since the equivalence is a straightforward
consequence of the assumption for c1 and c2 . ■
28.15 Notes
This chapter was written January to April 2024. I’m thankful to Dominik Kirst,
whose slides for an Australian summer school got me started, and with whom I
had weekly lunches and discussions during the writing. Dominik supplied me with
results from the literature that evolved into Facts 28.5.3, 28.6.2, and 28.13.4.
We may say that this chapter investigates results from set-theoretic computabil-
ity in computational type theory. The type-theoretic development profits from the
fact that an explicit model of computation (e.g. Turing machines) can be replaced
with the synthetic notion of computability that comes with CTT.
Different variants of Axiom CT (Church’s thesis) appear in the literature. Troel-
stra and Van Dalen [29] discuss CT informally in the context of constructive Math-
ematics. Forster [10, 11] is the first to study CT in computational type theory.
Forster [11] shows for a call-by-value lambda calculus L [13] formalized in CTT the
following formulations of CT are equivalent:
343
28 Axiom CT and Semidecidability
344
29 Inductive Equality
Inductive equality extends Leibniz equality with eliminators discriminating on iden-
tity proofs. The definitions are such that inductive identities appear as computa-
tional propositions enabling reducible casts between computational types.
There is an important equivalence between uniqueness of identity proofs (UIP)
and injectivity of dependent pairs (DPI) (i.e., injectivity of the second projection). As
it turns out, UIP holds for discrete types (Hedberg’s theorem) but is unprovable in
computational type theory in general
Hedberg’s theorem is of practical importance since it yields injectivity of depen-
dent pairs and reducibility of identity casts for discrete types, two features that are
essential for inversion lemmas for indexed inductive types.
The proofs in this chapter are of surprising beauty. They are obtained with de-
pendently typed algebraic reasoning about identity proofs and often require tricky
generalizations.
We define inductive equality as an inductive predicate with two parameters and one
index:
eq (X : T, x : X) : X → P ::=
| Q : eq X x x
C : ∀X T ∀x X ∀p X→T ∀y. x = y → px → py
C Xxp _ (Q_) a := a : px
J : ∀X T ∀x X ∀p ∀y. x=y→T
. px(Qx) → ∀ye. pye
J Xxpa _ (Q_) := a : px(Qx)
345
29 Inductive Equality
called cast operator and full eliminator. For C we treat the first four arguments as
implicit arguments, and for J the first two arguments.
We call applications of the cast operator casts. A cast Cp ea with ex=y changes
the type of a from px to py for every admissible type function p. We have
C(Qx)a ≈ a
and say that trivial casts C(Qx)a can be discharged. We also have
which says that the cast eliminator can be expressed with the full eliminator.
Inductive quality as defined here is stronger than the Leibniz equality considered
in Chapter 4. The constructors of the inductive definition give us the constants eq
and Q, and with the cast operator we can easily define the constant for the rewrit-
ing law. Inductive equality comes with two essential generalizations over Leibniz
equality: Rewriting can now take place at the universe T using the cast operator,
and both the cast operator and the full eliminator come with computation rules.
We will make essential use of both features in this chapter.
We remark that equality in Coq is defined as inductive equality and that the full
eliminator J corresponds exactly to Coq’s matches for identities.
The laws for propositional equality can be seen as operators on paths. It turns
out that that these operators have elegant algebraic definitions using casts:
σ : x=y →y =x
σ e := C(λy.y=x) e (Qx)
τ: x=y →y =z→x=z
τe := C(λy.y=z→x=z) e (λe.e)
ϕ : x = y → fx = fy
ϕe := C(λy.f x=f y) e (Q(f x))
It also turns out that these operators satisfy familiar looking algebraic laws.
Exercise 29.1.1 Prove the following algebraic laws for casts and identities ex=y .
a) Ce(Qx) = e
b) Cee = Qy
In each case, determine a suitable type function for the cast.
346
29.2 Uniqueness of Identity Proofs
We will now show that the following properties of types are equivalent:
The flagship property is UIP (uniqueness of identity proofs), saying that identities
have at most one proof. What is fascinating is that UIP is equivalent to DPI (de-
pendent pair injectivity), saying that the second projection for dependent pairs is
injective. While UIP is all about identity proofs, DPI doesn’t even mention identity
proofs. There is a famous result by Hofmann and Streicher [18] saying that compu-
tational type theory does not prove UIP. Given the equivalence with DPI, this result
is quite surprising. On the other hand, there is Hedberg’s theorem [16] (§29.3)
saying that UIP holds for all discrete types. We remark that UIP is an immediate
consequence of proof irrelevance.
We now show the above equivalence by proving enough implications. The proofs
are interesting in that they need clever generalization steps to harvest the power of
the identity eliminators J and C. Finding the right generalizations requires insight
and practice.1
1
We acknowledge the help of Gaëtan Gilbert, (Coq Club, November 13, 2020).
347
29 Inductive Equality
Proof Assume CD(X) and p X→T . We obtain the claim with backward reasoning:
∀ex=x . e = Qx by DPI
x=x
∀e . (x, e)eq x = (x, Qx)eq x by instantiation
∀ex=y . (y, e)eq x = (x, Qx)eq x by J ■
Proof Assume UIP′ (X). We obtain the claim with backward reasoning:
∀e′ ex=y . e = e′ by J on e′
∀ex=x . e = Qx by UIP′ ■
Theorem 29.2.7 UIP(X), UIP′ (X), K(X), CD(X), and DPI(X) are equivalent.
Exercise 29.2.8 Verify the above proofs with a proof assistant to appreciate the
subtleties.
Exercise 29.2.9 Give direct proofs for the following implications: UIP(X) → K(X),
K(X) → UIP′ (X), and CD(X) → UIP′ (X).
Exercise 29.2.10 Prove that dependent pair types are discrete if their component
types are discrete: ∀X ∀p X→T . E(X) → (∀x. E(pX)) → E(sig p).
348
29.3 Hedberg’s Theorem
We will now prove Hedberg’s theorem [16]. Hedberg’s theorem says that all discrete
types satisfy UIP. Hedberg’s theorem is important in practice since it says that the
second projection for dependent pair types is injective if the first components are
numbers.
The proof of Hedberg’s theorem consists of two lemmas, which are connected
with a clever abstraction we call Hedberg functions. In algebraic speak one may see
a Hedberg function a polymorphic constant endo-function on paths.
Lemma 29.3.2 (Hedberg) Every type that has a Hedberg function satisfies UIP.
We need to show f xye = f xye′ . If dxy = L ê, both sides are ê. Otherwise, we
have e : x = y and x ≠ y, which is contradictory. ■
349
29 Inductive Equality
Exercise 29.3.6 Prove Hedberg’s theorem with the weaker assumption that equality
on X is propositionally decidable: ∀xy X . x = y ∨ x ≠ y.
Sometimes a full inversion operator for an indexed inductive type family can only
be expressed with a cast. As example we consider derivation types for comparisons
x < y defined as follows:
L (x : N) : N → T ::=
| L1 : L x(Sx)
| L2 : ∀y. L xy → L x(Sy)
The formulation of the type follows the pattern we have seen before, except that
there is a cast in the branch for L1 :
′ =x
Σey . Cea = L1 x
The cast is necessary since a has the type L x(Sy ′ ) while L1 x has the type L x(Sx).
A formulation without a cast seems impossible. The defining equations for the
inversion operator discriminate on a, as usual, which yields the obligations
Σex=x . Ce(L1 x) = L1 x
Σa′ . L2 xy ′ a = L2 xy ′ a′
350
29.5 Constructor Injectivity with DPI
The first obligation follows with cast discharge and UIP for numbers. The second
obligation is trivial.
We need the inversion operator to show derivation uniqueness of L. As it turns
our, we need an additional fact about L:
L xx → ⊥ (29.1)
L xy → x < y (29.2)
∀xy ∀abL xy . a = b
follow with UIP for numbers and the inductive hypothesis, respectively.
We can also define an index inversion operator for L
by discriminating on a.
Exercise 29.4.1 The proof sketches described above involve sophisticated type
checking and considerable technical detail, more than can be certified reliably on
paper. Use the proof assistant to verify the above proof sketches.
We present another inversion fact that can only be verified with UIP for numbers.
This time we need DPI for numbers. We consider the indexed type family
K (x : N) : N → T ::=
| K1 : K x(Sx)
| K2 : ∀zy. K xz → K zy → K xy
351
29 Inductive Equality
which provides a derivation system for arithmetic comparisons x < y taking tran-
sitivity as a rule. Obviously, K is not derivation unique. We would like to show that
the value constructor K2 is injective:
satisfying
(R is one of the two value constructors for sums). Defining the inversion operator
Kinv is routine. We now prove (29.3) by applying Kinv using Fact 4.6.1 to both sides
of the assumed equation of (29.3), which yields
Exercise 29.5.1 Prove injectivity of the constructors for sum using the applicative
closure law (Fact 4.6.1).
352
29.6 Inductive Equality at Type
id (X : T, x : X) : X → T ::=
| I : id X x x
and ask how propositional inductive equality and computational inductive equal-
ity are related. In turns out that we can go back and forth between proofs of propo-
sitional identities x = y and derivations of general identities id xy, and that UIP
at one level implies UIP at the other level. We learn from this example that as-
sumptions concerning only the propositional level (i.e., UIP) may leak out to the
computational level and render nonpropositional types inhabited that seem to be
unconnected to the propositional level.
First, we observe that we can define transfer functions
↑ : ∀X ∀xy X ∀ex=y . id xy
↓ : ∀X ∀xy X ∀aid xy . x = y
such that ↑(Qx) ≈ Ix and ↓(Ix) ≈ Qx for all x, and ↓(↑ e) = e and ↑(↓ a) = a for
all e and a. We can also define a function
Exercise 29.6.3 Prove Hedberg’s theorem for general inductive equality. Do not
make use of propositional types.
Exercise 29.6.4 Formulate the various UIP characterizations for general inductive
equality and prove their equivalence. Make sure that you don’t use propositional
types. Note that the proofs from the propositional level carry over to the general
level.
353
29 Inductive Equality
29.7 Notes
354
30 Well-Founded Recursion
Well-founded recursion is provided with an operator
generalizing arithmetic size induction such that recursion can descend along any
well-founded relation. In addition, the well-founded recursion operator comes with
an unfolding equation making it possible to prove for the target function the equa-
tions used for the definition of the step function. Well-foundedness of relations is
defined constructively with recursion types
obtaining well-founded recursion from the higher-order recursion coming with in-
ductive types. Being defined as computational propositions, recursion types medi-
ate between proofs and computational recursion.
The way computational type theory accommodates definitions and proofs by
general well-founded recursion is one of the highlights of computational type
theory.
and call the elements of recursion types recursion certificates. Note that recur-
sion types are computational propositions. A recursion certificate of type AR (x)
justifies all recursions starting from x and descending on the relation R. That a
recursion on a certificate of type AR (x) terminates is ensured by the built-in ter-
mination property of computational type theory. Note that recursion types realize
higher-order recursion.
We will harvest the recursion provided by recursion certificates with a recursion
operator
355
30 Well-Founded Recursion
∀x. AR x → px
The step function describes a function ∀[Link] obtained with a continuation func-
tion
∀y. Ryx → py
Note that a proof of a proposition wf (R) is a function that yields a recursion cer-
tificate AR (x) for every x of the base type of R. For well-founded relations, we can
specialize the recursion operator W ′ as follows:
Note that the equivalence is much weaker than the inductive definition in that it
doesn’t provide recursion and in that it doesn’t force an inductive interpretation of
the predicate AR (e.g., the full predicate would satisfy the equivalence).
We speak of recursion types AR (x) rather than accessibility propositions AR (x)
to emphasize that the propositional types AR (x) support computational recursion.
356
30.2 Well-founded Relations
Exercise 30.1.2 Prove AR (x) ←→ (∀y. Ryx → AR (y)) from first principles. Make
sure you understand both directions of the proof.
Define a function V → ∀X T . X.
Proof We prove the more general claim ∀nx. x < n → A< (x) by induction on the
upper bound n. For n = 0 the premise x < n is contradictory. For the successor
case we assume x < Sn and prove A< (x). By the single constructor for A we
assume y < x and prove A< (x). Follows by the inductive hypothesis since y < n.■
357
30 Well-Founded Recursion
The above proof is completely straightforward when carried out formally with
the well-founded recursion operator W .
Another important construction for binary relations are retracts. Here one has
a relation R Y →Y →P and uses a function σ X→Y to obtain a relation Rσ on X:
Rσ := λx ′ x. R(σ x ′ )(σ x)
We will show that retracts of well-founded relations are well-founded. It will also
turn out that well-founded recursion on a retract Rσ is exactly well-founded size
induction on R with the size function σ .
Fact 30.2.3 (Retracts) wf (R) → wf (Rσ ).
We now obtain the arithmetic size induction operator from § 19.2 as a special
case of the well-founded size induction operator.
Corollary 30.2.5 (Arithmetic size induction)
∀σ X→N ∀p X→T . (∀x. (∀x ′ .σ x ′ < σ x → px ′ ) → px) → ∀x. px.
358
30.3 Unfolding Equation
Exercise 30.2.7 Give two proofs for wf (λxy. Sx = y) : A direct proof by structural
induction on numbers, and a proof exploiting that λxy. Sx = y is a sub-relation of
the order relation on numbers.
W F x = F x(λyr . W F y)
for the well-founded recursion operator W . We will refer to this equation as un-
folding equation. The equation makes it possible to prove that the function W F
satisfies the equations underlying the definition of the guarded step function F .
This is a major improvement over arithmetic size induction where no such tool is
available. For instance, the unfolding equation gives us the equation
0 if x ≤ y
Dxy =
S(D(x − Sy)y) if x > y
Dxy := W (F y)x
Note that the second argument y is treated as a parameter. Also note that the
equation for D is obtained from the unfolding equation for W by computational
equality.
We now prove the unfolding equation using FE. We first show the remarkable
fact that under FE all recursion certificates are equal.
Proof We prove
∀x ∀aAR (x) ∀bc AR (x) . b = c
359
30 Well-Founded Recursion
using W ′ . This gives us the claim ∀bc AR (x) . b = c and the inductive hypothesis
′
∀x ′ . Rx ′ x → ∀bc AR (x ) . b = c
Cϕ = Cϕ′
ϕx ′ r = ϕ′ x ′ r
′
for r Rx x . Holds by the inductive hypothesis. ■
for some a and ϕ. Using FE, it now suffices to prove the equation
W ′ F x ′ (ϕx ′ r ) = W ′ F x ′ b
f î F := ∀x. f x = F x(λyr .f y)
FE → wf (R) → W F î F
for Fact 30.3.2. We now prove that all functions satisfying a step function agree if
FE is assumed and R is well-founded.
Proof We prove ∀x. f x = f ′ x using W with R. Using the assumptions for f and f ′ ,
we reduce the claim to F x(λx ′ r .f x ′ ) = F x(λx ′ r .f ′ x ′ ). Using FE, we reduce that
claim to Rx ′ x → f x ′ = f ′ x ′ , an instance of the inductive hypothesis. ■
Exercise 30.3.4 Note that the proof of Lemma 30.3.1 doubles the quantification
of a. Verify that this is justified by the general law (∀a.∀[Link]) → ∀[Link].
360
30.4 Example: GCDs
g:N→N→N
g0y = y
g (Sx) 0 = Sx
g (Sx) (y − x) if x ≤ y
g (Sx) (Sy) =
g (x − y) (Sy) if x > y
guard conditions
x ≤ y → Sx + (y − x) < Sx + Sy
x > y → (x − y) + Sy < Sx + Sy
Our second example for the use of well-founded recursion and the unfolding equa-
tion is the construction of a function computing GCDs (§ 20.5). We start with the
procedural specification in Figure 30.1. We will construct a function g N→N→N satis-
fying the specification using W on the retract of <N for the size function
σ : N×N→N
σ (x, y) := x + y
The figure gives the guard conditions for the recursive calls adding the precondi-
tions established by the conditional in the third specifying equation.
Given the specification in Figure 30.1, the formal definition of the guarded step
function is straightforward:
g:N→N→N
gxy := W HF (x, y)
361
30 Well-Founded Recursion
obtained with the functions for recursion certificates for numbers (Fact 30.2.1) and
retracts (Fact 30.2.3). Each of the three specifying equations in Figure 30.1 can now
be obtained as an instance of the unfolding equation (Fact 30.3.2).
In summary, we note that the construction of a function computing GCDs with a
well-founded recursion operator is routine given the standard constructions for re-
tracts and the order on numbers. Proving that the specifying equations are satisfied
is straightforward using the unfolding equation and FE.
That the example can be done so nicely with the general retract construction is
due to the fact that type checking is modulo computational equality. For instance,
the given type of the step function F is computationally equal to
We have seen a proof of the unfolding equation assuming FE. Alternatively, one
can prove the unfolding equation assuming that the step function has a particular
extensionality property. For concrete step function one can usually prove that they
have this extensionality without using assumptions.
We assume a relation R X→X→P , a type function p X→T , and a step function
F : ∀x. (∀x ′ . Rx ′ x → px ′ ) → px
The property says that F xh remains the same if h is replaced with a function agree-
ing with h. We have FE → ext(F ). Thus all proofs assuming ext(F ) yields proofs for
the stronger assumption FE.
362
30.6 Witness Operator
Proof We assume ext(F ) and show ∀x∀aAR (x) . ∀aa′ . W ′ F xa = W ′ F xa′ using W ′ .
This give us the inductive hypothesis
W ′ F y(ϕyr ) = W ′ F y(ϕ′ yr )
Exercise 30.5.3 From the definition of extensionality for step function it seams
clear that ordinary step functions are extensional. To prove that an ordinary step
function is extensional, no induction is needed. It suffices to walk through the
matches and confront the recursive calls.
a) Prove that the step function for Euclidean division is extensional (§30.3).
b) Prove that the step function for GCDs is extensional (§30.4).
c) Prove that the step function for the Ackermann equations is extensional (Exer-
cise 30.4.1).
Exercise 30.5.4 Show that all functions satisfying an extensional step function for
a well-founded relation agree.
Rxy := x = Sy ∧ ¬py
363
30 Well-Founded Recursion
Proof Induction on x with y quantified. The base case follows by falsity elimina-
tion. For the successor case, we assume H : p(Sx + y) and prove AR (y). Using the
constructor for AR , we assume ¬py and prove AR (Sy). By the inductive hypothe-
sis it suffices to show p(x + Sy). Holds by H. ■
Proof By well-founded induction with W ′ . Using the decider for p, we have two
cases. If px, we have sig(p). If ¬px, we have R(Sx)x and thus the claim holds by
the inductive hypothesis. ■
Proof We assume a decidable and satisfiable predicate p N→P and define R as above.
By Lemma 30.6.2 it suffices to show AR (0). We can now obtain a witness x for p.
The claim follows with Lemma 30.6.2. ■
We may see the construction of an existential witness operator for numbers with
linear search types (Fact 14.2.2) as a specialization of the construction shown here
where the general recursion types used here are replaced with special purpose linear
search types.
Exercise 30.6.5 Prove that AR yields the elimination lemma for linear search types:
The results presented so far are such that, given a recursive specification of a func-
tion, we can obtain a function satisfying the specification, provided we can supply a
well-founded relation and proofs for the resulting guard conditions (see Figure 30.1
for an example). Moreover, if we don’t accept FE as an assumption, we need to prove
that the specified step function is extensional as defined in §30.5.
The proof assistant Coq comes with a tool named Equations package making it
possible to write recursive specifications and associate them with well-founded re-
lations. The tool then automatically generates the resulting proof obligations. Once
364
30.8 Padding and Simplification
the user has provided the requested proofs for the specification, a function is de-
fined and proofs are generated that the function satisfies the specifying equations.
This uses the well-founded recursion operator and the generic proofs of the unfold-
ing equation we have seen. One useful feature of Equations is the fact that one can
specify functions with several arguments and with size induction. Equations then
does the necessary pairing and the retract construction, relieving the user from
tedious coding.
Taken together, we can now define recursive functions where the termination
conditions are much relaxed compared to strict structural recursion. In contrast
to functions specified with strict structural recursion, the specifying equations are
satisfied as propositional equations rather than as computational equations. Nev-
ertheless, if we apply functions defined with well-founded recursion to concrete
and fully specified arguments, reduction is possible and we get the accompanying
computational equalities (e.g., gcd 21 56 ≈ 7).
This is a good place to mention Coq’s extraction tool. Given a function speci-
fied in computational type theory, one would expect that one can extract related
programs for functional programming languages. In Coq, such an extraction tool
is available for all function definitions, and works particularly well for functions
defined with Equations. The vision here is that one specifies and verifies functions
in computational type theory and then extracts programs that are correct by con-
struction. A flagship project using extraction is CompCert ([Link]) where a
verified compiler for a subset of the C programming language has been developed.
a ≈ Cx(λyr . a′ )
a ≈ Cx(λyr .Cy(λy ′ r ′ . a′′ ))
365
30 Well-Founded Recursion
The construction appears tricky and fragile on paper. When carried out with a proof
assistant, the construction is fairly straightforward: Type checking helps with the
definitions of D and P , and simplification automatically obtains the right hand sides
of the two examples from the left hand sides.
When we simplify a term P (k + n)xa where k is a concrete number and n, x,
and a are variables, we obtain a term that needs at least 2k additional variables to be
written down. Thus the example tells us that simplification may have to introduce
an unbounded number of fresh variables.
The possibility for padding functions seems to be a unique feature of higher-
order recursion.
Exercise 30.8.1 Write a padding function for linear search types (§ 14.1).
366
30.9 Classical Well-foundedness
Intuitively, progressive elements for a relation R are elements that have an infinite
descent in R. Progressive predicates are defined such that every witness has an
infinite descent in R. Progressive predicates generalize the frequently used notion
of infinite descending chains.
Proof Using XM, we assume ¬AR (x) and show proR (x). It suffices to show
proR (λz.¬AR (z)). We assume ¬AR (z) and prove ∃y. ¬AR (y) ∧ Ryz. Using XM,
we assume H : ¬∃y. ¬AR (y) ∧ Ryz and derive a contradiction. It suffices to prove
AR (z). We assume Rz′ z and prove AR (z′ ). Follows with H and XM. ■
Proof For direction → we assume wf (R) and proR (x) and derive a contradiction.
We have AR (x). Contradiction by Fact 30.9.2.
For direction ← we assume ¬∃x. proR (x) and prove AR (x). By Fact 30.9.3 we
assume proR (x) and have a contradiction with the assumption. ■
Using XM, we show that a predicate is progressive if and only if it has no minimal
element.
367
30 Well-Founded Recursion
Proof For direction →, we derive a contradiction from the assumptions proR (p),
px, and ∀y. py → ¬Ryx. Straightforward.
For direction ←, using XM, we derive a contradiction from the assumptions
¬∃x. minR,p (x), px, and H : ¬∃y. py ∧ Ryx. We show minR,p (x). We assume
py and Ryx and derive a contradiction. Straightforward with H. ■
Next we show that R has no progressive element if and only if every satisfiable
predicate has a minimal witness.
Fact 30.9.6 XM → (¬(∃x. proR (x)) ←→ ∀p. (∃x. px) → ∃x. minR,p (x)).
Proof For direction →, we use XM and derive a contradiction from the assumptions
¬∃x. proR (x), px, and ¬∃x. minR,p (x). With Fact 30.9.5 we have proR (p). Contra-
diction with ¬∃x. proR (x).
For direction ←, we assume px and proR (p) and derive a contradiction.
Fact 30.9.5 gives us ¬∃x. minR,p (x). Contradiction with the primary assumption. ■
The above proofs gives us ample opportunity to contemplate about the role of
XM in proofs. An interesting example is Fact 30.9.3, where XM is used to show that
an element is either well-founded or progressive.
Rxy R + xy ′ Ry ′ y
R + xy R + xy
We work with this format since it facilitates proving that taking the transitive clo-
sure of a well-founded relation yields a well-founded relation. Note that the induc-
tive predicate behind R + has four parameters X, R, x, y, where X, R, x are uniform
and y is non-uniform.
368
30.11 Notes
Proof We assume wf (R) and prove ∀y. AR+ (y) by well-founded induction on y
and R. This gives us the induction hypothesis and the claim AR+ (y). Using the
constructor for recursion types we assume R + xy and show AR+ (x). If R + xy is
obtained from Rxy, the claim follows with the inductive hypothesis. Otherwise we
have R + xy ′ and Ry ′ y. The inductive hypothesis gives us AR+ (y ′ ). Thus AR+ (x)
since R + xy ′ . ■
30.11 Notes
The inductive definition of the well-founded points of a relation appears in Aczel [1]
in a set-theoretic setting. Nordström [26] adapts Aczel’s definition to a constructive
type theory without propositions and advocates functions recursing on recursion
types. Balaa and Bertot [3] define a well-founded recursion operator in Coq and
prove that it satisfies the unfolding equation. They suggest that Coq should support
the construction of functions with a tool taking care of the tedious routine proofs
coming with well-founded recursion, anticipating Coq’s current Equations package.
369
31 Aczel Trees and Hierarchy Theorems
Aczel trees are wellfounded trees where each node comes with a type and a function
fixing the subtree branching. Aczel trees were conceived by Peter Aczel [2] as a
representation of set-like structures in type theory. Aczel trees are accommodated
with inductive type definitions featuring a single value constructor and higher-order
recursion.
We discuss the dominance condition, a restriction on inductive type definitions
ensuring predicativity of nonpropositional universes. Using Aczel trees, we will
show an important foundational result: No universe embeds into one of its types.
From this hierarchy result we obtain that proof irrelevance is a consequence of
excluded middle, and that omitting the propositional discrimination restriction in
the presence of the impredicative universe of propositions results in inconsistency.
T : T ::= T (X : T, X → T )
describes an atomic tree not having subtrees. Given two trees t1 and t2 , the term
describes a tree having exactly t1 and t2 as subtrees where the boolean values are
used as labels. The term
describes an infinitely branching tree that has a subtree for every number. All
subtrees of the infinitely branching tree are equal (to the atomic tree).
371
31 Aczel Trees and Hierarchy Theorems
T T (λs.s)
which seems to describe a universal tree having every tree as subtree. It turns out
that the term for the universal tree does not type check since there is a universe
level conflict. First we note that Coq’s type theory admits the definition
T : Ti ::= T (X : Tj , X → T )
only if i > j. This reflects a restriction on inductive definitions we have not dis-
cussed before. We speak of the dominance condition. In its general form, the
dominance condition says that the type of every value constructor (without the pa-
rameter prefix) must be a member of the universe specified for the type constructor.
The dominance condition admits the above definition for i > j since then Tj : Ti ,
X : Ti , and T : Ti and hence
(∀X Tj . (X → T ) → T ) : Ti
using the universe rules from §5.3. For the reader’s convenience we repeat the rules
for universes
T1 : T2 : T3 : · · ·
P ⊆ T1 ⊆ T2 ⊆ T3 ⊆ · · ·
P : T2
here. The variable U ranges over the computational universes Ti . The first rule
says that every computational universe is closed under taking function types. The
second rule says that the universe P enjoys a stronger closure property known as
impredicativity.
Note that the term for the universal tree T T (λs.s) does not type check since we
do not have T : Tj for i > j.
Exercise 31.1.1 The dominance condition for inductive type definitions requires
that the types of the value constructors are in the target universe of the type con-
structor, where the types of the value constructor are considered without the pa-
rameter prefix. That the parameter prefix is not taken into account ensures that
372
31.2 Propositional Aczel Trees
the universes Ti are closed under the type constructors for pairs, options, and lists.
Verify the following typings for lists:
Tp : P ::= Tp (X : P, X → Tp )
of the type of propositional Aczel trees satisfies the dominance condition since the
type of the constructor Tp is in P by the impredicativity of the universe P:
(∀X U . (X → Tp ) → Tp ) : P
up := Tp Tp (λs.s)
does type check for propositional Aczel trees. So there is a universal propositional
Aczel tree.
The universal propositional Aczel tree up is paradoxical in that it conflicts with
our intuition that all values of an inductive type are wellfounded. A value of an
inductive type is wellfounded if descending to a subvalue through a recursion in the
type definition always terminates. Given that reduction of recursive functions is as-
sumed to be terminating, one would expect that values of inductive types are well-
founded. However, the universal propositional Aczel tree Tp Tp (λs.s) is certainly
not wellfounded. So we have to adopt the view that because of the impredicativity
of the universe P certain recursive propositional types do admit non-wellfounded
values. This does not cause harm since the propositional discrimination restriction
reliably prevents recursion on non-wellfounded values.
We remark that there are recursive propositional types providing for functional
recursion. A good example are the linear search types for the existential witness op-
erator for numbers (§ 14.1)). It seems that the values of computational propositions
are always wellfounded.
373
31 Aczel Trees and Hierarchy Theorems
T : T2 ::= T (X : T1 , X → T )
Tp : P ::= Tp (X : P, X → Tp )
∈: T →T →P
s ∈ T Xf := ∃x. f x = s
Proof By induction on s (using ET ) it suffice to show TXf ∉ TXf given the induc-
tive hypothesis ∀x. f x ∉ f x. It suffices to show for every x X that f x = TXf is
contradictory. Since f x = TXf implies f x ∈ f x, we have a contradiction with the
inductive hypothesis. ■
For propositional Aczel trees we can prove that a subtree predicate R Tp →Tp →P
such that
R s (Tp Xf ) ←→ ∃x. f x = s
does not exist. This explains why the existence of the universal propositional Aczel
tree does not lead to a proof of falsity.
374
31.4 Propositional Hierarchy Theorem
T : U ::= T(X : V , X → T )
s ∈ TXf ←→ ∃x. f x = s
we cannot define a universal tree u ∈ u. This works out such that for propositional
Aczel trees we cannot define a subtree predicate (because of the propositional dis-
crimination restriction) and for computational Aczel trees we cannot define a uni-
versal tree (because of the dominance restriction).
Exercise 31.3.5 Suppose you are allowed exactly one violation of the propositional
discrimination restriction. Give a proof of falsity.
A fundamental result about Coq’s type theory says that the universe P of proposi-
tions cannot be embedded into a proposition, even if equivalent propositions may
be identified. This important result was first shown by Thierry Coquand [9] in 1989
for a subsystem of Coq’s type theory. We will prove the result for Coq’s type theory
by showing that an embedding as specified provides for the definition of a subtree
predicate for propositional Aczel trees.
Theorem 31.4.1 (Coquand) There is no proposition AP such that there exist func-
tions E P→A and D A→P such that ∀P P . D(E(P )) ←→ P .
Proof Let AP , E P→A , D A→P be given such that ∀P P . D(E(P )) ←→ P . By Fact 31.3.4
is suffices to show that
375
31 Aczel Trees and Hierarchy Theorems
With Coquand’s theorem we can show that the law of excluded middle implies proof
irrelevance (see §5.2 for definitions). The key idea is that given a proposition with
two different proofs we can define an embedding as excluded by Coquand’s theo-
rem. For the proof to go through we need the full elimination lemma for disjunc-
tions (see Exercise 31.5.2).
Proof Let d∀X : P. X∨¬X and let a and b be proofs of a proposition A. We show a = b.
Using excluded middle, we assume a ≠ b and derive a contradiction with Coquand’s
theorem. To do so, we define an encoding E P→A and a decoding D A→P as follows:
(a = if dX then a else b) ←→ X
X → (a = a ←→ X)
¬X → (a = b ←→ X)
We will now show that no computational universe embeds into one of its types. Note
that by Coquand’s theorem we already know that the universe P does not embed
into one of its types.
We define a general embedding predicate E T→T→P for types:
376
31.6 Hierarchy Theorem for Computational Universes
T : U ::= T (a : A, Da → T )
∈: T →T →P
s ∈ T af := ∃x. f x = s
Exercise 31.6.8 Assume the inductive type definition A : T1 ::= C(T1 ) is admitted
although it violates the dominance condition. Give a proof of falsity.
377
31 Aczel Trees and Hierarchy Theorems
Acknowledgements
Thorsten Altenkirch suggested Aczel trees as a means for obtaining negative results
in January 2016 at the POPL conference in St. Petersburg, Florida. Steven Schäfer
came up with an elegant proof of Coquand’s theorem using Aczel trees in June 2018
at the Types conference in Braga, Portugal.
378
Part V
Appendices
379
Appendix: Typing Rules
⊢ u : Ti x : u ⊢ v : Ti ⊢ u : Ti x : u ⊢ v : T0
u
⊢ Ti : Ti+1 ⊢ ∀x : u. v : Ti ⊢ ∀x . v : T0
⊢ s : ∀x : u. v ⊢t:u ⊢ u : Ti x:u ⊢ s : v
⊢st : vx
t ⊢ λx . s : ∀x u . v
u
⊢ s : u′ u′ ≈ u ⊢ u : Ti
⊢s:u
⊢ s : ∀x1 : t1 . · · · ∀xn : tn . Ti
n≥0
⊢ s : ∀x1 : t1 . · · · ∀xn : tn . Ti+1
• In practice, one writes just P and T and checks that all occurrences of T can be
assigned universe levels i ≥ 1. The particular universe level assignment does not
matter, provided it type checks.
• The subtyping rule (appearing last) realizes P ⊆ T1 ⊆ T2 ⊆ · · · with n = 0.
• We have ø Ti : Ti .
• vx
t is capture-free substitution.
• Computational equality s ≈ t is defined with reduction and α- and η-equivalence.
• Variable typings (x : u before ⊢) are introduced by the rules for ∀ and λ and the
patterns of defining equations.
• Simple function types u → v are notation for dependent function types ∀x : u. v
where x does not occur in v.
• Functions whose type ends with the universe P are called predicates.
• Functions whose type ends with a universe Ti with i > 0 are called type functions.
381
Appendix: Inductive Definitions
383
Appendix: Inductive Definitions
Remarks
1. The format for inductive functions is such that universal eliminators can be
defined that can express all other inductive functions. Inductive functions may
also be called eliminators.
2. The special case of zero value constructors is redundant. A proposition ⊥ with
an eliminator ⊥ → ∀X T . X can be defined with a single proof constructor ⊥ → ⊥.
3. Assuming type definitions at the computational level, accommodating type def-
initions also at the propositional level is responsible for the propositional dis-
384
crimination restriction.
4. The dominance condition is vacuously satisfied for propositional type defini-
tions.
5. Defining equations with a secondary case analysis (e.g., subtraction) come as
syntactic convenience. They can be expressed with auxiliary functions defined
as inductive functions.
6. Our presentation of inductive definitions is compatible with Coq but takes away
some of the flexibility provided by Coq. Our format requires that in Coq a re-
cursive abstraction (i.e., fix) is directly followed by a match on the discriminating
argument. This excludes a direct definition of Euclidean division. It also excludes
the (redundant) eager recursion pattern sometimes used for well-founded recur-
sion in the Coq literature.
Examples
Lists
L : T→T
cons : ∀X. X → L(X) → L(X)
E : ∀X.∀p L(X)→T . . . . → ∀A. pA
E Xp · · · (cons xA) := e xA(E · · · A)
T : (N → P) → N → T
C : ∀qn. (¬qn → T q(Sn)) → T qn
E : ∀q.∀p N→T . . . . → ∀n. T qn → pn
E qp · · · n (Cϕ) := e n(λa.E · · · (Sn)(ϕa))
385
Appendix: Inductive Definitions
H : For → T
K : ∀st. H (s → t → s)
E : ∀p For→T . . . . → ∀s. H (s) → ps
E p · · · _ (Kst) := e st
ND derivation types
⊢ : L(For) → For → T
I→ : ∀Ast. (s :: A ⊢ t) → (A ⊢ (s → t))
E : ∀p L(For)→For→T . . . . → ∀As. (A ⊢ s) → pAs
E p · · · A _ (I→ std) := e Ast(E · · · (s :: A)td)
386
Appendix: Basic Definitions
P ⊆ Q := ∀x. P x → Qx respect
P ≡ Q := ∀x. P x ←→ Qx agreement
387
Appendix: Basic Definitions
388
Appendix: Exercise Sheets
Below you will find the weekly exercise sheets for the course Introduction to
Computational Logic as given at Saarland University in the summer semester 2022
(13 weeks of full teaching). The sheets tell you which topics of MPCTT we covered
and how much time we spent on them.
389
Appendix: Exercise Sheets
Assignment 1
Do the following exercises on paper using mathematical notation and also with the
proof assistant Coq. Follow the style of Chapter 1 and the accompanying Coq file
gs.v. For each function state the type and the defining equations. Make sure you
understand the definitions and proofs you give.
Exercise 1.1 Define an addition function add for numbers and prove that it is com-
mutative.
Exercise 1.2 Define a distance function dist for numbers and prove that it is com-
mutative. Do not use helper functions.
Exercise 1.3 Define a minimum function min for numbers and prove that it is com-
mutative. Do not use helper functions. Prove min x (x + y) = x.
Exercise 1.4 Define a function fib satisfying the procedural Fibonacci equations.
Define the unfolding function for the equations and prove your function satisfies
the unfolding equation.
Exercise 1.5 Define an iteration function computing f n (x) and prove the shift laws
f Sn (x) = f n (f x) = f (f n (x)).
Exercise 1.6 Give the types of the constructors pair and Pair for pairs and pair
types. Give the inductive type definition. Define the projections fst and snd and
prove the η-law. Define a swap function and prove that it is self-inverting. Do not
use implicit arguments.
Want More?
You will find further exercises in Chapter 1 of MPCT. You may for instance define
Ackermann functions using either a higher-order helper function or iteration and
verify that your functions satisfy the procedural specification given as unfolding
function.
390
Assignment 2
Do the exercises on paper using mathematical notation and also with the proof
assistant Coq.
Exercise 2.1 Define a truncating subtraction function using a plain constant defini-
tion and a recursive abstraction.
Exercise 2.3 Prove the following propositions (tables, terms, and Coq). Assume
that X, Y , Z are propositions.
a) X → Y → X
b) (X → Y → Z) → (X → Y ) → X → Z
c) (X → Y ) → ¬Y → ¬X
d) (X → ⊥) → (¬X → ⊥) → ⊥
e) ¬(X ↔ ¬X)
f) ¬¬(¬¬X → X)
g) ¬¬(((X → Y ) → X) → X)
h) ¬¬((¬Y → ¬X) → X → Y )
i) (X ∧ Y → Z) → (X → Y → Z)
j) (X → Y → Z) → (X ∧ Y → Z)
k) ¬¬(X ∨ ¬X)
l) ¬(X ∨ Y ) → ¬X ∧ ¬Y
m) ¬X ∧ ¬Y → ¬(X ∨ Y )
391
Appendix: Exercise Sheets
Assignment 3
Do the exercises on paper using mathematical notation and also with the proof
assistant Coq.
to show that the law of excluded middle is intuitionistically equivalent to the double
negation law. Do the proof first with a table and then verify your reasoning with
Coq.
with a table and with Coq. Assume X : T and determine the types of the variables x
and y.
392
Exercise 3.5 (Propositional equality) Assume the constants
eq : ∀X T . X → X → P
Q : ∀X T ∀x X . eq X x x
R : ∀X T ∀xy X ∀p X→P . eq Xxy → px → py
for propositional equality and prove the following proposition assuming the vari-
able types x : X, y : X, z : X, f : X → Y , X : T, and Y : T:
a) eq xy → eq yx
b) eq xy → eq yz → eq xz
c) eq xy → eq (f x) (f y)
d) ¬eq ⊤ ⊥
e) ¬eq true false
For each occurrence of eq determine the implicit argument.
393
Appendix: Exercise Sheets
Assignment 4
Do the exercises on paper using mathematical notation and verify your findings
with the proof assistant Coq.
Exercise 4.2 MPCTT gives two proofs of transitivity, one using the conversion rule
and one not using the conversion rule. Give each proof as a table and as a term and
verify your findings with the proof assistant Coq.
Exercise 4.3 Define the eliminators for booleans, numbers, and pairs.
394
Assignment 5
Exercise 5.1 Define the constants ex, E, and M∃ for existential quantification both
inductively and impredicatively.
Exercise 5.2 Give and verify the impredicative characterization of existential quan-
tification.
Exercise 5.3 Give a proof term for (∃[Link]) → ¬∀x. ¬px using the constants for
existential quantification. Do not use matches.
Exercise 5.6 (Intuitionistic drinker) Using excluded middle, one can argue that in
a bar populated with at least one person one can always find a person such that if
this person drinks milk everyone in the bar drinks milk:
The fact follows intuitionistically once two double negations are inserted:
395
Appendix: Exercise Sheets
Exercise 5.7 Give the procedural specification for the Fibonacci function as an un-
folding function and prove that all functions satisfying the unfolding equation
agree.
Exercise 5.8 (Puzzle) Give two types that satisfy and dissatisfy the predicate
λX T . ∀f g X→X ∀xy X . f x = y ∨ gy = x.
396
Assignment 6
397
Appendix: Exercise Sheets
398
Assignment 7
Do the proofs with the proof assistant and explain the proof ideas on paper.
399
Appendix: Exercise Sheets
a) FE → PE → SE. c) SE → p − (q ∪ r ) = (p − q) ∩ (p − r ).
b) SE → PE.
400
Assignment 8
Do the proofs with the proof assistant and explain the proof ideas on paper.
a) x + y = x → y = 0 n) x ≤ x
b) x − 0 = x o) x ≤ y → y ≤ z → x ≤ z
c) x − x = 0 p) x ≤ y → y ≤ x → x = y
d) (x + y) − x = y q) x ≤ y < z → x < z
e) x − (x + y) = 0 r) ¬(x < 0)
f) x ≤ y → x + (y − x) = y s) ¬(x + y < x)
g) (x ≤ y) + (y < x) t) ¬(x < x)
h) ¬(y ≤ x) → x < y u) x ≤ y → x ≤ y + z
i) x ≤ y ←→ ∃z. x + z = y v) x ≤ y → x ≤ Sy
j) x ≤ x + y w) x < y → x ≤ y
k) x ≤ Sx x) ¬(x < y) → ¬(y < x) → x = y
l) x + y ≤ x → y = 0 y) x ≤ y ≤ Sx → x = y ∨ y = Sx
m) x ≤ 0 → x = 0 z) x + y ≤ x + z → y ≤ z
401
Appendix: Exercise Sheets
f :N→N→N
f x y := if [x ≤ y\ then x else f (x − Sy y) y
402
Assignment 9
f :N→N→N
f 0yb := 0
f (Sx)yb := if [b = y\ then S(f xy0) else f xy(Sb)
Show γ xy(f xy0); that is, f xy0 is the Euclidean quotient of x and Sy. This
requires a lemma. Hint: Prove b ≤ y → γ (x + b) y (f xyb).
403
Appendix: Exercise Sheets
404
Assignment 10
a) x :: A ≠ A d) x ∈ A +
+ B ←→ x ∈ A ∨ x ∈ B.
b) (A +
+ B) +
+C = A+ + C)
+(B + e) x ∈ f @A ←→ ∃a. a ∈ A ∧ x = f a.
c) len (A +
+ B) = len A + len B
405
Appendix: Exercise Sheets
406
Assignment 11
a) pn → T n d) T n → T 0
b) T (Sn) → T n e) pn → T 0.
c) T (k + n) → T n f) pn → m ≤ n → T m
g) ∀Z T . ((¬pn → T (Sn)) → Z) → T n → Z
h) ∀qN→T . (∀n. (¬pn → q(Sn)) → qn) → ∀n. T n → qn
i) T n ←→ ∃k. k ≥ n ∧ pk
Note that (h) provides an induction lemma for T useful for direction → of (i).
407
Appendix: Exercise Sheets
408
Assignment 12
409
Appendix: Exercise Sheets
[] · s := s
(t :: A) · s := t → (A · s)
Prove (A ⊢ s) a ( ⊢ A · s).
410
Assignment 13
411
Appendix: Exercise Sheets
⊥∈A s∈A ¬s ∈ A
ρ(A) ρ(A)
412
Appendix: Glossary
Here is a list of technical terms used in the text but not used (much) in the literature.
The technical terms are given in the order they appear first in the text.
• Discrimination
• Inductive function
• Target type function
• Propositional discrimination restriction
• Computational falsity elimination
• Index condition and index variables
• Reloading match
413
Appendix: Author’s Notes
Coq Wishlist
• Have equational function definitions with fixed arity and iterated and nested
discrimination. This is realized in Agda already.
• Change type inference so that the universe Set is not derived as default. Set is
not needed and smuggling it in as default is confusing.
• Have standard notations for sum and sigma types.
• An inductive predicate definition should say explicitly if it wants to lift the
propositional discrimination restriction, and have the assistant check whether
this can be granted. Conjunction can then be defined without granting compu-
tational discrimination.
Clarify
415
Appendix: Author’s Notes
Editorial decisions
Maybe
Work to do
Changelog
2024
2023
416
2021
• MPCTT started
• switch to inductive and plain functions in Chapter 2
• unfolding functions and procedural specifications
• computational falsity elimination
• Andrej Dudenhefner was lead TA
2020
• x ≤ y as x − y = 0, game changer
• chapters on finite types and data types appeared
2019
• semi-decidability
Done
417
Bibliography
[1] Peter Aczel. An introduction to inductive definitions. In Jon Barwise, editor,
Handbook of Mathematical Logic, pages 739–782. North-Holland, 1977.
[2] Peter Aczel. The Type Theoretic Interpretation of Constructive Set Theory.
Studies in Logic and the Foundations of Mathematics, 96:55–66, January 1978.
[3] Antonia Balaa and Yves Bertot. Fix-point equations for well-founded recursion
in type theory. In Mark Aagaard and John Harrison, editors, Theorem Proving
in Higher Order Logics, pages 1–16. Springer Berlin Heidelberg, 2000.
[4] Henk P. Barendregt. The Lambda Calculus: Its Syntax and Semantics. North-
Holland, 2nd revised edition, 1984.
[10] Yannick Forster. Church’s thesis and related axioms in Coq’s type theory. In
Christel Baier and Jean Goubault-Larrecq, editors, 29th EACSL Annual Confer-
ence on Computer Science Logic, CSL 2021, January 25-28, 2021, Ljubljana,
Slovenia (Virtual Conference), volume 183 of LIPIcs, pages 21:1–21:19, 2021.
[11] Yannick Forster. Computability in Constructive Type Theory. PhD thesis, Saar-
land University, 2021.
[12] Yannick Forster, Dominik Kirst, and Gert Smolka. On synthetic undecidability
in Coq, with an application to the Entscheidungsproblem. In 8th ACM SIGPLAN
International Conference on Certified Programs and Proofs, CPP 2019, Cascais,
Portugal, January 14-15, 2019, New York, NY, USA, Jan 2019. ACM.
419
Bibliography
[13] Yannick Forster and Gert Smolka. Weak call-by-value lambda calculus as a
model of computation in Coq. In ITP 2017, Brasília, Brazil, volume 10499 of
LNCS, pages 189–206. Springer, 2017.
[15] Gerhard Gentzen. Untersuchungen über das logische Schließen II. Mathematis-
che Zeitschrift, 39(1):405–431, 1935. Translation in: Collected papers of Ger-
hard Gentzen, ed. M. E. Szabo, North-Holland, 1969.
[16] Michael Hedberg. A coherence theorem for Martin-Löf’s type theory. Journal
of Functional Programming, 8(4):413–436, 1998.
[18] Martin Hofmann and Thomas Streicher. The groupoid model refutes unique-
ness of identity proofs. In LICS 1994, pages 208–212, 1994.
[19] Stanisław Jaśkowski. On the rules of supposition in formal logic, Studia Logica
1: 5—32, 1934. Reprinted in Polish Logic 1920-1939, edited by Storrs McCall,
1967.
[20] Dominik Kirst and Benjamin Peters. Gödel’s theorem without tears - essential
incompleteness in synthetic computability. In Bartek Klin and Elaine Pimentel,
editors, 31st EACSL Annual Conference on Computer Science Logic, CSL 2023,
February 13-16, 2023, Warsaw, Poland, volume 252 of LIPIcs, pages 30:1–30:18,
2023.
[21] Nicolai Kraus, Martín Hötzel Escardó, Thierry Coquand, and Thorsten Al-
tenkirch. Generalizations of Hedberg’s theorem. In Proceedings of TLCA 2013,
volume 7941 of LNCS, pages 173–188. Springer, 2013.
[22] Edmund Landau. Grundlagen der Analysis: With Complete German-English Vo-
cabulary, volume 141. American Mathematical Soc., 1965.
[23] Per Martin-Löf and Giovanni Sambin. Intuitionistic type theory, volume 9. Bib-
liopolis Naples, 1984.
[24] Yuri V. Matiyasevich. Martin Davis and Hilbert’s Tenth Problem. In Eugenio G.
Omodeo and Alberto Policriti, editors, Martin Davis on Computability, Compu-
tational Logic, and Mathematical Foundations, volume 10 of Outstanding Con-
tributions to Logic, pages 35–54. Springer, 2016.
420
Bibliography
[25] John McCarthy and James Painter. Correctness of a compiler for arithmetic
expressions. Mathematical aspects of computer science, 1, 1967.
[27] Raymond M. Smullyan and Melvin Fitting. Set Theory and the Continuum Hy-
pothesis. Dover, 2010.
[29] A.S. Troelstra and D. Van Dalen. Constructivism in Mathematics. Vol. 121 of
Studies in Logic and the Foundations of Mathematics. North-Holland, Amster-
dam, 1988.
[30] The Univalent Foundations Program. Homotopy Type Theory: Univalent Foun-
dations of Mathematics. [Link] Institute
for Advanced Study, 2013.
[31] Louis Warren, Hannes Diener, and Maarten McKubre-Jordens. The drinker para-
dox and its dual. CoRR, abs/1805.06216, 2018.
421