Beruflich Dokumente
Kultur Dokumente
tional
Programming
Simon Thompson
Mar h 1999
Not to be reprodu
ed
i
ii
To my parents
Prefa
e
Constru
tive Type theory has been a topi
of resear
h interest to
omputer
s
ientists, mathemati
ians, logi
ians and philosophers for a number of years.
For
omputer s
ientists it provides a framework whi
h brings together logi
and programming languages in a most elegant and fertile way: program
development and veri
ation
an pro
eed within a single system. Viewed
in a dierent way, type theory is a fun
tional programming language with
some novel features, su
h as the totality of all its fun
tions, its expressive
type system allowing fun
tions whose result type depends upon the value
of its input, and sophisti
ated modules and abstra
t types whose interfa
es
an
ontain logi
al assertions as well as signature information. A third
point of view emphasizes that programs (or fun
tions)
an be extra
ted
from proofs in the logi
.
Up until now most of the material on type theory has only appeared in
pro
eedings of
onferen
es and in resear
h papers, so it seems appropriate
to try to set down the
urrent state of development in a form a
essible to
interested nal-year undergraduates, graduate students, resear
h workers
and tea
hers in
omputer s
ien
e and related elds { hen
e this book.
The book
an be thought of as giving both a rst and a se
ond
ourse in
type theory. We begin with introdu
tory material on logi
and fun
tional
programming, and follow this by presenting the system of type theory itself,
together with many examples. As well as this we go further, looking at
the system from a mathemati
al perspe
tive, thus elu
idating a number
of its important properties. Then we take a
riti
al look at the profusion
of suggestions in the literature about why and how type theory
ould be
augmented. In doing this we are aiming at a moving target; it must be
the
ase that further developments will have been made before the book
rea
hes the press. Nonetheless, su
h an survey
an give the reader a mu
h
more developed sense of the potential of type theory, as well as giving the
ba
kground of what is to
ome.
iii
iv PREFACE
Outline
It seems in order to give an overview of the book. Ea
h
hapter begins with
a more detailed introdu
tion, so we shall be brief here. We follow this with
a guide on how the book might be approa
hed.
The rst three
hapters survey the three elds upon whi
h type theory
depends: logi
, the -
al
ulus and fun
tional programming and
onstru
-
tive mathemati
s. The surveys are short, establishing terminology, notation
and a general
ontext for the dis
ussion; pointers to the relevant literature
and in parti
ular to more detailed introdu
tions are provided. In the se
ond
hapter we dis
uss some issues in the -
al
ulus and fun
tional program-
ming whi
h suggest analogous questions in type theory.
The fourth
hapter forms the fo
us of the book. We give the formal
system for type theory, developing examples of both programs and proofs
as we go along. These tend to be short, illustrating the
onstru
t just
introdu
ed {
hapter 6
ontains many more examples.
The system of type theory is
omplex, and in
hapter whi
h follows we
explore a number of dierent aspe
ts of the theory. We prove
ertain results
about it (rather than using it) in
luding the important fa
ts that programs
are terminating and that evaluation is deterministi
. Other topi
s examined
in
lude the variety of equality relations in the system, the addition of types
(or `universes') of types and some more te
hni
al points.
Mu
h of our understanding of a
omplex formal system must derive
from out using it. Chapter six
overs a variety of examples and larger
ase studies. From the fun
tional programming point of view, we
hoose to
stress the dieren
es between the system and more traditional languages.
After a lengthy dis
ussion of re
ursion, we look at the impa
t of the quan-
tied types, espe
ially in the light of the universes added above. We also
take the opportunity to demonstrate how programs
an be extra
ted from
onstru
tive proofs, and one way that imperative programs
an be seen as
arising. We
on
lude with a survey of examples in the relevant literature.
As an aside it is worth saying that for any formal system, we
an really
only understand its pre
ise details after attempting to implement it. The
ombination of symboli
and natural language used by mathemati
ians is
surprisingly suggestive, yet ambiguous, and it is only the dis
ipline of having
to implement a system whi
h makes us look at some aspe
ts of it. In the
ase of T T , it was only through writing an implementation in the fun
tional
programming language Miranda1 that the author
ame to understand the
distin
tive role of assumptions in T T , for instan
e.
The system is expressive, as witnessed by the previous
hapter, but
are programs given in their most natural or eÆ
ient form? There is a
1 Miranda is a trade mark of Resear
h Software Limited
v
Chapter seven is perhaps best read after the examples of
hapter six,
and digesting the deliberations of
hapter ve.
In ea
h
hapter exer
ises are in
luded. These range from the routine
to the
hallenging. Not many programming proje
ts are in
luded as it
is expe
ted that readers will to be able to think of suitable proje
ts for
themselves { the world is full of potential appli
ations, after all.
A
knowledgements
The genesis of this book was a set of notes prepared for a le
ture series on
type theory given to the Theoreti
al Computer S
ien
e seminar at the Uni-
versity of Kent, and subsequently at the Federal University of Pernambu
o,
Re
ife, Brazil. Thanks are due to
olleagues from both institutions; I am
espe
ially grateful to David Turner and Allan Grimley for both en
ourage-
ment and stimulating dis
ussions on the topi
of type theory. I should also
thank
olleagues at UFPE, and the Brazilian National Resear
h Coun
il,
CNPq, for making my visit to Brazil possible.
In its various forms the text has re
eived detailed
ommment and
riti-
ism from a number of people, in
luding Martin Henson, John Hughes, Ni
M
Phee, Jerry Mead and various anonymous reviewers. Thanks to them
the manus
ript has been mu
h improved, though needless to say, I alone
will a
ept responsibility for any infeli
ities or errors whi
h remain.
The text itself was prepared using the LaTeX do
ument preparation
system; in this respe
t Tim Hopkins and Ian Utting have put up with nu-
merous queries of varying
omplexity with unfailing good humour { thanks
to both of them. Dun
an Langford and Ri
hard Jones have given me mu
h
appre
iated advi
e on using the Ma
intosh.
The editorial and produ
tion sta at Addison-Wesley have been most
helpful; in parti
ular Simon Plumtree has given me exa
tly the right mix-
ture of assistan
e and dire
tion.
The most important a
knowledgements are to Jane and Ali
e: Jane has
supported me through all stages of the book, giving me en
ouragement
when it was needed and
oping so well with having to share me with this
enterprise over the last year; without her I am sure the book would not
have been
ompleted. Ali
e is a joy, and makes me realise how mu
h more
there is to life than type theory.
Contents
Prefa
e iii
Introdu
tion 1
1 Introdu
tion to Logi
7
1.1 Propositional Logi
. . . . . . . . . . . . . . . . . . . . . . . 8
1.2 Predi
ate Logi
. . . . . . . . . . . . . . . . . . . . . . . . . 16
1.2.1 Variables and substitution . . . . . . . . . . . . . . . 18
1.2.2 Quantier rules . . . . . . . . . . . . . . . . . . . . . 21
1.2.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . 24
2 Fun
tional Programming and -Cal
uli 29
2.1 Fun
tional Programming . . . . . . . . . . . . . . . . . . . . 30
2.2 The untyped -
al
ulus . . . . . . . . . . . . . . . . . . . . 32
2.3 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.4 Convertibility . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.5 Expressiveness . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.6 Typed -
al
ulus . . . . . . . . . . . . . . . . . . . . . . . . 42
2.7 Strong normalisation . . . . . . . . . . . . . . . . . . . . . . 45
2.8 Further type
onstru
tors: the produ
t . . . . . . . . . . . . 50
2.9 Base Types: Natural Numbers . . . . . . . . . . . . . . . . 53
2.10 General Re
ursion . . . . . . . . . . . . . . . . . . . . . . . 55
2.11 Evaluation revisited . . . . . . . . . . . . . . . . . . . . . . 56
3 Constru
tive Mathemati
s 59
4 Introdu
tion to Type Theory 67
4.1 Propositional Logi
: an Informal View . . . . . . . . . . . . 69
4.2 Judgements, Proofs and Derivations . . . . . . . . . . . . . 71
4.3 The Rules for Propositional Cal
ulus . . . . . . . . . . . . . 73
vii
viii CONTENTS
Types are types and propositions are propositions; types
ome from pro-
gramming languages, and propositions from logi
, and they seem to have
no relation to ea
h other. We shall see that if we make
ertain assumptions
about both logi
and programming, then we
an dene a system whi
h is
simultaneously a logi
and a programming language, and in whi
h proposi-
tions and types are identi
al. This is the system of
onstru
tive type theory,
based primarily on the work of the Swedish logi
ian and philosopher, Per
Martin-Lof. In this introdu
tion we examine the ba
kground in both logi
and
omputing before going on to look at
onstru
tive type theory and its
appli
ations. We
on
lude with an overview of the book proper.
Corre
t Programming
The problem of
orre
tness is ever-present in
omputing: a program is
written with a parti
ular spe
i
ation in view and run on the assumption
that it meets that spe
i
ation. As is all too familiar, this assumption
is unjustied: in most
ases the program does not perform as it should.
How should the problem be ta
kled? Testing
annot ensure the absen
e
of errors; only a formal proof of
orre
tness
an guarantee that a program
meets its spe
i
ation. If we take a nave view of this pro
ess, we develop
the program and then, post ho
, give a proof that it meets a spe
i
ation. If
we do this the possibility exists that the program developed doesn't perform
as it ought; we should instead try to develop the program in su
h a way
that it must behave a
ording to spe
i
ation.
A useful analogy here is with the types in a programming language. If we
use a typed language, we are prevented by the rules of syntax from forming
an expression whi
h will lead to a type error when the program is exe
uted.
We
ould prove that a similar program in an untyped language shares this
property, but we would have to do this for ea
h program developed, whilst
in the typed language it is guaranteed in every
ase.
1
2 INTRODUCTION
This is a prin
iple of indire
t proof, whi
h has formed a
ornerstone of mod-
ern mathemati
s sin
e it was rst used by Hilbert in his proof of the Basis
Theorem about one hundred years ago. The problem with the prin
iple is
that it asserts the existen
e of an obje
t without giving any indi
ation of
what the obje
t is. It is a non-
onstru
tive method of proof, in other words.
We
an give a dierent,
onstru
tive, rendering of mathemati
s, based on
the work of Brouwer, Heyting, Bishop and many others, in whi
h every
statement has
omputational
ontent; in the light of the dis
ussion above
it is ne
essary to reje
t
lassi
al logi
and to look for modes of reasoning
whi
h permit only
onstru
tive derivations.
To explain exa
tly what
an be derived
onstru
tively, we take a dier-
ent foundational perspe
tive. Instead of giving a
lassi
al, truth-fun
tional,
explanation of what is valid, we will explain what it means for a parti
ular
obje
t p to be a proof of the proposition P . Our logi
is proof-fun
tional
rather than truth-fun
tional.
The
ru
ial explanation is for the existential quantier. An assertion
that 9z:P (z )
an only be dedu
ed if we
an produ
e an a with the property
P (a). A proof of 9z:P (z ) will therefore be a pair, (a; p),
onsisting of an
obje
t a and a proof that a does in fa
t have the property P . A universal
statement 8z:Q(z )
an be dedu
ed only if there is a fun
tion taking any
obje
t a to a proof that Q(a). If we put these two explanations together, a
onstru
tive proof of the statement
8x:9y:R(x; y)
an be seen to require that there is a fun
tion, f say, taking any a to a
value so that
R(a; f a)
Here we see that a
onstru
tive proof has
omputational
ontent, in the
shape of a fun
tion whi
h gives an expli
it witness value f a for ea
h a.
The other proof
onditions are as follows. A proof of the
onjun
tion
A ^ B
an be seen as a pair of proofs, (p; q), with p a proof of A and q of B .
A proof of the impli
ation A ) B
an be seen as a proof transformation:
given a proof of A, we
an produ
e a proof of B from it. A proof of the
disjun
tion A _ B is either a proof of A or a proof of B , together with
an indi
ation of whi
h (A or B ). The negation :A is dened to be the
impli
ation A ) ?, where ? is the absurd or false proposition, whi
h has
no proof but from whi
h we
an infer anything. A proof of :A is thus a
fun
tion taking a proof of A to a proof of absurdity.
Given these explanations, it is easy to see that the law of the ex
luded
middle will not be valid, as for a general A we
annot say that either A or
:A is provable. Similarly, the law of indire
t proof will not be valid.
4 INTRODUCTION
Having given the ba
kground from both
omputing and logi
, we turn
to examining the link between the two.
taking us from proofs to programs. This idea has been used with mu
h
su
ess in the Nuprl system developed at Cornell University, and indeed in
other proje
ts.
Ba
kground
Our exposition of type theory and its appli
ations will make
ontinual refer-
en
e to the elds of fun
tional programming and
onstru
tivism. Separate
introdu
tions to these topi
s are provided by the introdu
tion to
hapter 2
and by
hapter 3 respe
tively. The interested reader may
are to refer to
these now.
Se
tion 9.2
ontains some
on
luding remarks.
Chapter 1
This
hapter
onstitutes a short introdu
tion to formal logi
, whi
h will
establish notation and terminology used throughout the book. We assume
that the reader is already familiar with the basi
s of logi
, as dis
ussed in
the texts [Lem65, Hod77℄ for example.
Logi
is the s
ien
e of argument. The purposes of formalization of logi
al
systems are manifold.
The formalization gives a
lear
hara
terisation of the valid proofs
in the system, against whi
h we
an judge individual arguments, so
sharpening our understanding of informal reasoning.
If the arguments are themselves about formal systems, as is the
ase
when we verify the
orre
tness of
omputer programs, the argument
itself should be written in a form whi
h
an be
he
ked for
orre
tness.
This
an only be done if the argument is formalized, and
orre
tness
an be
he
ked me
hani
ally. Informal eviden
e for the latter require-
ment is provided by Prin
ipia Mathemati
a [RW10℄ whi
h
ontains
numerous formal proofs; unfortunately, many of the proofs are in-
orre
t, a fa
t whi
h all too easily es
apes the human proof-reader's
eye.
As well as looking at the
orre
tness or otherwise of individual proofs
in a formal theory, we
an study its properties as a whole. For ex-
ample, we
an investigate its expressive strength, relative to other
theories, or to some sort of meaning or semanti
s for it. This work,
whi
h is predominantly mathemati
al in nature, is
alled mathemat-
i
al logi
, more details of whi
h
an be found in [Men87a℄ amongst
others.
7
8 CHAPTER 1. INTRODUCTION TO LOGIC
The assumptions upon whi
h the proofs of A and B depend are those of
the proof of A ^ B .
We have another example whi
h
ombines the use of the rules of introdu
-
tion and elimination. From the assumption (A ^ B ) ^ C we have
(A ^ B ) ^ C
(^E1 )
A^B (^E1 )
A
and
(A ^ B ) ^ C
(^E1 ) (A ^ B ) ^ C
A^B (^E2 ) (^E2 )
B C (^I )
B^C
Putting these proofs together we have
(A ^ B ) ^ C (A ^ B ) ^ C
.. ..
. .
A B^C
(^I )
A ^ (B ^ C )
This proof exhibits the asso
iativity of the
onjun
tion operation, a fa
t
with whi
h we are familiar. Note that the single assumption upon whi
h
the proof depends is the formula (A^B )^C , with the assumption appearing
at three dierent points in the proof.
There is another way in whi
h we
an read the elimination rules. Note
that the introdu
tion rule states that we
an infer A ^ B from A and B .
The elimination rules state that this is (essentially) the only way we
an
infer it, sin
e it states that if we
an prove A ^ B then we
an prove ea
h
of the
omponent formulae.
In giving the rules for impli
ation, ), we rst take an informal look at
what the
onne
tive is meant to mean. We think of A ) B as expressing A
implies B or that we
an dedu
e B from A. In other words we would like to
on
lude A ) B when we have a dedu
tion of B assuming A. What are the
assumptions upon whi
h this new proof of A ) B depends? All those on
whi
h the proof of B depends, ex
ept the assumption A itself. The reason
that we no longer depend upon A is that A has be
ome the hypothesis in
the formula A ) B | this expresses through a formula of the logi
that
we
an dedu
e B from A.
This is reinfor
ed by the elimination rule for )whi
h states that given
proofs of A and A ) B , we
an infer B . Now we state the rules.
1.1. PROPOSITIONAL LOGIC 11
) Introdu
tion
From a proof of the formula B , whi
h may depend upon the assumption
A amongst others, we
an infer the formula A ) B from the same set of
assumptions with A removed. We write this thus
[A℄
..
.
B
() I )
A)B
where the square bra
kets around the A indi
ate that all o
urren
es of the
assumption A in the proof of B are to be dis
harged. It should be stressed
that the proof of the formula B need not
ontain the assumption A for this
rule to be applied. We shall see an example of this in the proof of
B ) (A ) B )
whi
h appears later in this se
tion.
In a substantial proof there will be many o
urren
es of rules whi
h
dis
harge assumptions. In order to make the link between the dis
harged
assumption and the instan
e of the rule dis
harging it, we use labels , as in
the s
hemati
[A℄1
..
.
B
() I )1
A)B
We shall see further examples of the use of labels after seeing the rule for
impli
ation elimination.
) Elimination
From proofs of the formulas A and A ) B we
an infer the formula B .
The assumptions upon whi
h the proof of B depends are those of the proofs
of A and A ) B
ombined. The rule is written
A A)B
() E )
B
Now we
an
onsider a more
ompli
ated example,
A B
(^I ) (A ^ B ) ) C
A^B () E )
C
12 CHAPTER 1. INTRODUCTION TO LOGIC
t1 = t2
where t1 and t2 are terms.
Propositional
ombinations of formulas under the propositional
on-
ne
tives _; ^; ); ,; :.
Quantied formulas
8x:A 9x:B
where as in the propositional
ase we use A; B; : : : for arbitrary for-
mulas, as well as using x for an arbitrary variable.
The quantiers 8 | for all | and 9 | there exists | are intended to
express the assertions that a parti
ular formula holds for all obje
ts and for
some obje
t, respe
tively. (Hen
e the name `quantier'; quantied formulas
express the quantity of obje
ts with a parti
ular property.)
To reinfor
e the intuitive interpretation of the quantiers, we now look
at their use in expressing various properties. In ea
h parti
ular
ase, there
will be an intended domain of appli
ation of the quantiers, so that \for
all" will mean \for all sh", \for all real numbers" and so on. We assume
for these examples that our domain of dis
ourse is the natural numbers, so
that the quantiers will range over 0; 1; 2; : : :. Moreover, we assume that
the (inx) predi
ate symbol < is
hosen so that x < y expresses
` y is greater than x '
Suppose rst that f is a fun
tion. We say that a value is in the range
of f if it is the value of f at some argument. How
an we state in logi
al
terms that m is the maximum value in the range? First we say that m is
in the range
9i:(f (i) = m)
and then that m is greater than or equal to every element in the range
8j:(f (j ) m)
18 CHAPTER 1. INTRODUCTION TO LOGIC
On the other hand, in the pro
ess of proving a theorem we may use
variables in a dierent way. If we make an assumption with a free x, x > 0
say, and then prove a result like
8z:(z x _ z 0)
then this result is not true for all x. (Try x = 1!). This is pre
isely
be
ause the x is not arbitrary | we have assumed something about it:
that it is greater than zero. In other words, we
an say that x is arbitrary
if and only if x does not appear free in any of the assumptions of the proof
of the result.
We
an now state formally our introdu
tion rule for the universal quan-
tier:
8 Introdu
tion
For any formula A, whi
h may or may not involve the free variable x, we
an from a proof of A infer 8x:A if x is arbitrary, that is if x does not o
ur
free in any of the assumptions of the proof of A(x). This is
alled the side
ondition of the rule.
A
8x:A (8I )
The assumptions of the proof derived are the same as those of the proof of
A. Note that the formula A may or may not involve the free variable x,
and may involve free variables other than x.
The elimination rule for 8 is easier to state. It says that a universally
quantied formula is true for an arbitrary obje
t, and so is true of any term.
We express this by substituting the term for the quantied variable.
8 Elimination
From a proof of 8x:A(x) we
an infer A(t) for any term t.
8x:A(x) (8E )
A(t)
With our notation for substitution as above, we would write
8x:A (8E )
A[t=x℄
Now we turn to the existential quantier. There is a
ertain duality
between the two quantiers and we nd it re
e
ted in the rules. The
simpler of the rules to state is the existential introdu
tion rule: it states
that if we
an prove a substitution instan
e of a formula, then we
an infer
the existentially quantied statement.
1.2. PREDICATE LOGIC 23
9 Introdu
tion
If for a parti
ular term t we
an prove A(t) then
learly we have demon-
strated that there is some obje
t for whi
h A is provable.
A(t)
9x:A(x) (9I )
Alternatively, we write
A[t=x℄
9x:A (9I )
In order to frame the existential elimination rule we have to de
ide what
we are able to dedu
e on the basis of 9x:A. Let us return to the informal
dis
ussion we started on page 21. We looked there at an argument whi
h
had an assumption of the form
x>0
What is the for
e of su
h an assumption? It is to assume the existen
e of
an obje
t greater than zero, and to name it x. Now, suppose that on the
basis of this we
an prove some B whi
h does not mention x; if we also
know that indeed the existential assumption is valid, that is we know that
9x:(x > 0)
then we
an infer B outright, dis
harging the assumption.
9Elimination
[A℄
..
.
9x:A B
(9E )
B
where x is not free in B or any of the assumptions of the proof of B , ex
ept
for A itself, in whi
h it may be free. (This stipulation is the side
ondition
for the appli
ation of the rule.) The assumption A is dis
harged by the
appli
ation of this rule, so that the assumptions of the resulting proof are
those of the proof of 9x:A together with all those from the proof of B apart
from A.
Thinking of the rule in programming terms, we
an think of it as intro-
du
ing a temporary (or `lo
al') name x for the obje
t asserted to exist by
the formula 9x:A.
Returning to our informal dis
ussion a se
ond time, we
an perhaps see
more
learly a duality between the two quantiers and their treatment of
formulas with free variables.
24 CHAPTER 1. INTRODUCTION TO LOGIC
1.2.3 Examples
Now we put these rules to some use in a series of examples.
For the rst example we assume
df 8x:(P (x) ) Q(x))
9x:P (x)
and try to prove that
9x:Q(x)
1.2. PREDICATE LOGIC 25
P (x)
8x:(P (x) ) Q(x)) (8E )
(P (x) ) Q(x))
() E )
Q(x)
From this we
an infer 9x:Q(x) (even though x is free in one of the assump-
tions | there are no restri
tions on the rule (9I )), and nally by existential
elimination we have:
[P (x)℄1
8x:(P (x) ) Q(x)) (8E )
(P (x) ) Q(x))
() E )
9x:P (x) Q(x) (9I )
9x:Q(x) (9E )1
9x:Q(x)
For our se
ond example, suppose we make the assumption that every
obje
t is either an apple or a banana:
8x:(A(x) _ B (x))
and that both apples and bananas are tasty:
8x:(A(x) ) T (x))
8x:(B (x) ) T (x))
We will show that everything is tasty, 8x:T (x).
Applying the universal elimination rule three times we have
(A(y) _ B (y))
(A(y) ) T (y))
(B (y) ) T (y))
26 CHAPTER 1. INTRODUCTION TO LOGIC
and then we
an infer T (y) on the basis of the se
ond of these and the
assumption A(y), using () E ).
A(y)
8x:(A(x) ) T (x)) (8E )
(A(y) ) T (y))
() E )
T (y)
We
an similarly infer T (y) from B (y) and the universal statement, and
are then in a position to apply (_E ). (In the diagram whi
h follows the
hypotheses of the
entral proof are listed verti
ally.)
[A(y)℄1 [B (y)℄1
8x:(A(x) ) T (x)) :::
.. ..
8x:(A(x) _ B (x)) (8E ) . .
T (y) T (y)
(A(y) _ B (y))
(_E )1
T (y)
8 introdu
tion is then applied, giving
..
.
8x:T (x) (8I )
Our nal example
on
erns the proof of
9y:8x:A(x; y) ) 8x:9y:A(x; y)
The reader might like to refer ba
k to the dis
ussion on page 18 above.
Remembering the rule of impli
ation introdu
tion, it is suÆ
ient to prove
8x:9y:A(x; y)
on the assumption of
9y:8x:A(x; y)
We have an existential assumption, so stripping o the quantier we
an
by the rule (9E ) use instead the assumption
8x:A(x; y)
Using (8E ) we have
8x:A(x; y) (8E )
A(x; y)
1.2. PREDICATE LOGIC 27
and by (9I )
..
.
9y:A(x; y) (9I )
Now, we
an dedu
e
..
.
8x:9y:A(x; y) (8I )
using (8I ), sin
e x is not free in the assumptions (and therefore arbitrary).
We
omplete the proof with appli
ations of (9E ) and () I ):
[8x:A(x; y)℄1
(8E )
A(x; y)
[9y:8x:A(x; y)℄2
9y:A(x; y) (9I ) (8I )
8x:9y:A(x; y) (9E )1
8x:9y:A(x; y) () I )2
9y:8x:A(x; y) ) 8x:9y:A(x; y)
Exer
ises
1.13. Explain how the side
onditions in the proof rules prevent the
on-
stru
tion of a proof of
8x:9y:A(x; y) ) 9y:8x:A(x; y)
analogous to the proof of
9y:8x:A(x; y) ) 8x:9y:A(x; y)
above.
1.14. Assuming that the variable x is not free in B , prove that the following
formulas are equivalent, i.e. ea
h
an be proved on the assumption of the
other
8x:(A(x) ) B )
((9x:A(x)) ) B )
1.15. Using the previous exer
ise, or otherwise, argue that the following
formulas are equivalent:
:(9x:A(x)) 8x::A(x)
and show that
9x::A(x) ) :8x:A(x)
Would you expe
t the
onverse of the nal result to be provable?
28 CHAPTER 1. INTRODUCTION TO LOGIC
Chapter 2
29
30 CHAPTER 2. FUNCTIONAL PROGRAMMING AND -CALCULI
fun
tion over every type, after all it simply returns its argument. Sim-
ilarly we
an re-use the
ode to reverse a linked list over stru
turally
similar lists (whi
h only dier in the type of entries at ea
h node)
as the
ode is independent of the
ontents. We
an a
ommodate
this kind of generi
ity and retain strong typing if we use the Hindley-
Milner type system, [Mil78℄, or other sorts of polymorphi
type. The
type of the identity fun
tion be
omes * -> *, where * is a type vari-
able, indi
ating that the type of the fun
tion is a fun
tional type, in
whi
h the domain and range type are the same. This means that it
an be used on booleans, returning a boolean, on numeri
fun
tions
returning a numeri
fun
tion, and so on.
Algebrai
types: Lists, trees, and other types
an be dened dire
tly
by re
ursive denitions, rather than through pointer types. The
me
hanism of algebrai
types generalises enumerated types, (variant)
re
ords,
ertain sorts of pointer type denitions, and also permits type
denitions (like those of lists) to be parametrised over types (like the
type of their
ontents). Pattern mat
hing is usually the means by
whi
h
ase analyses and sele
tions of
omponents are performed.
Modularity: The languages provide systems of modules of varying degrees
of
omplexity by means of whi
h large systems
an be developed more
easily.
One area in whi
h there are dieren
es is in the me
hanism of evaluation.
The SML system in
orporates stri
t evaluation, under whi
h s
heme ar-
guments of fun
tions are evaluated before the instantiated fun
tion body,
and
omponents of data types are fully evaluated on obje
t formation. On
the other hand, Miranda and Haskell adopt lazy evaluation, under whi
h
fun
tion arguments and data type
omponents are only evaluated when
this be
omes ne
essary, if at all. This permits a distin
tive style of pro-
gramming based on innite and partially-dened data stru
tures. There
are advantages of ea
h system, and indeed there are hybrids like Hope+
[Per89℄ whi
h
ombine the two.
This is not the pla
e to give a
omplete introdu
tion to fun
tional pro-
gramming. There is a growing number of good introdu
tory textbooks
on the subje
t [BW88, Rea89, Wik87℄, as well as books looking at the
foundations of the subje
t [Hue90a℄ and at
urrent resear
h dire
tions
[Pey87, Tur90℄. We shall look at the topi
s des
ribed above as we de-
velop our system of type theory; rst, though, we investigate the lambda
al
ulus, whi
h is both a pre
ursor of
urrent fun
tional programming lan-
guages, having been developed in the nineteen thirties, and an abstra
t
version of them.
32 CHAPTER 2. FUNCTIONAL PROGRAMMING AND -CALCULI
and in mathemati
al texts we might well talk about the fun
tion f given
by
f (x) = e
In the -
al
ulus we have an anonymous notation for the fun
tion whi
h
introdu
es the fun
tion without giving it a name (like f ). The parameter
x is a formal parameter and so we would expe
t that the fun
tion
x : y : xy
would be indistinguishable from
u : v : uv
for instan
e. Formally, as we saw in the
hapter on logi
, su
h variables x
are
alled bound, the being the binding
onstru
t.
How do we asso
iate a
tual parameters with the formals? We form
appli
ations
(x : e1 ) e2
To evaluate these appli
ations, we pass the parameter: we substitute the
a
tual parameter for the formal, whi
h we denote
e1[e2 =x℄
As for the binding
onstru
ts of logi
, the quantiers, we have to be
areful
about how we dene substitution, whi
h we do, after saying formally what
it means to be bound and free.
Denition 2.3 An o
urren
e of a variable x within a sub-expression x : e
is bound; all other o
urren
es are free. The o
urren
e of x in x: is the
binding o
urren
e whi
h introdu
es the variable { other o
urren
es are
alled applied. We say that a variable x o
urs free in an expression f if
some o
urren
e of x is free in f . A variable x is bound by the synta
ti
ally
innermost en
losing , if one exists, just as in any blo
k-stru
tured pro-
gramming language. An expression is
losed if it
ontains no free variables,
otherwise it is open .
34 CHAPTER 2. FUNCTIONAL PROGRAMMING AND -CALCULI
The same variable may o
ur both bound and free in an expression. For
example, the rst applied o
urren
e of x in
(x : y : yx)((z : zx)x)
is bound, but the se
ond and third applied o
urren
es are free.
Denition 2.4 The substitution of f for the free o
urren
es of x in e,
written e[f=x℄, is dened thus:
x[f=x℄ df f and for a variable y 6 x, y[f=x℄ df y
For appli
ations, we substitute into the two parts:
(e1 e2 )[t=x℄ df (e [t=x℄ e [t=x℄)
1 2
Denition 2.5 The rule of -redu
tion states that for all x, e and f , we
an redu
e the appli
ation
(x : e)f ! e[f=x℄
2.2. THE UNTYPED -CALCULUS 35
2.3 Evaluation
The redu
tion relation, ! !, of the previous se
tion embodies what it is for
one expression to redu
e to another by a number of elementary
omputation
steps. We
an ask a number of fundamental questions about this relation.
It is somewhat arti
ial to examine evaluation in the
ontext of the
untyped -
al
ulus, sin
e all we have in this
ontext are fun
tions, and
we do not usually
onsider fun
tions to be obje
ts whi
h
an be evaluated
themselves, rather we work in typed systems and print only the results of
evaluating expressions of ground type , like numbers, pairs of booleans and
so forth. Nonetheless we
an both establish some terminology and begin
dis
ussions here, even if we will have more to say later.
First, if the relation des
ribes evaluation, what is it that expressions
evaluate to? There are a number of answers to this.
Denition 2.8
ye1 : : : em
y : ((x : xx)(x : xx)) is a weak head normal form with no head normal
form. Both these examples use the term
df (x : xx)(x : xx)
whi
h has the property that
!
and only to
, thus proving that it has
no normal form; indeed it has no weak head normal form. Whatever the
notion of answer, an attempt to evaluate
results in an undened answer,
sin
e
omputation fails to terminate.
It is
lear why we
an think of a normal form as being the result of
a
omputation, but how do the other denitions arise? It might at rst
be thought that any expression without a normal form is in some sense
equivalent to
in being undened, but the position is not so simple. A
ru
ial example is the fun
tion
F df f : ((x : f (xx))(x : f (xx)))
whi
h has the following properties. It has no normal form, so
omputation
of it fails to terminate, yet when applied to an argument f it returns a xed
point of the fun
tion f , that is an obje
t with the property
(F f ) !! f (F f )
In many
ases the
omputation of the appli
ation will terminate {
onsider
the
ase that f is a
onstant fun
tion whose value is a normal form. We
an
hara
terise the property of F as being able to yield an answer (normal
form) in some
ontext, even though it has no normal form itself. Wadsworth
has shown that it is pre
isely those fun
tions whi
h have a head normal
form, so in evaluating fun
tions it seems more sensible only to
ompute to
normal form, if we wish
omputations on `meaningful' obje
ts to terminate.
More details on this analysis
an be found in the useful dis
ussion of se
tion
2 of [Bar84℄. Not every fun
tional expression has a head normal form, the
simplest
ase being x:
.
In evaluating fun
tional expressions we might
hoose to halt evaluation
as soon as a fun
tional form x:e is rea
hed { this gives rise to the notion of
weak head normal form whi
h has re
ently been dis
ussed in [Pey87, Abr90℄.
The
ontext for this dis
ussion about termination, the untyped -
al
ulus,
is somewhat arti
ial sin
e it
ontains no true printable values su
h as num-
bers or
hara
ters. We therefore defer dis
ussion of the form that results
(or
anoni
al values) take until se
tion 2.11.
Whi
hever notion we
hoose, we
an see that there are expressions whose
evaluation fails to terminate. No sequen
e of redu
tions starting from
2.4 Convertibility
Our introdu
tion to the -
al
ulus has fo
ussed so far on the
omputation
relations ! , ! ! and ! . We
an also ask the more general question
of whi
h expressions have the same
omputational behaviour. This se
tion
introdu
es a number of
onvertibility relations, that is equivalen
e rela-
tions whi
h are also substitutive: equivalent expressions substituted into
equivalent
ontexts are equivalent.
The relations ` ! ' and ` ! ' are asymmetri
al: the left hand side is (in
some sense) simplied in transition to the right hand side. The relations
generate two
onvertibility relations, $ $ and $ $ :
Denition 2.16 . ` $ $ ' is the smallest equivalen
e relation extending `! !'
Expli
itly, e $
$ f if and only if there is a sequen
e e0 ; ; en with n 0
e e0 , en f and for ea
h i, 0 i < n, ei ! ! ei+1 or ei+1 ! ! ei .
A similar relation based on and redu
tion together is
alled -
onvertibility.
As a
onsequen
e of the Chur
h-Rosser theorems, two expressions e and
f will be (-)
onvertible if and only if there exists a
ommon (-)redu
t
of e and f . If one of them has a normal form then the other has the same.
Two fun
tions with normal forms are
onvertible if and only if they have
the same normal form; in parti
ular we fail to identify
y : (x : (yx)) y : y
Applied to an argument z , the fun
tions give equivalent results, as they
-redu
e to
(x : (zx)) z
whi
h themselves have the same behaviour as fun
tions. It is for this reason
that -
onvertibility is dened. -
onvertibility is the smallest substitu-
tive equivalen
e relation R extending $ $ whi
h is extensional , meaning
that if (f y) R (g y) for y a variable, then f R g. This result is again found
in the en
y
lopaedi
[Bar84℄.
The
onvertibility relations are not ne
essary to explain the
omputa-
tional behaviour of -expressions; they are used when we reason about the
2.5. EXPRESSIVENESS 41
behaviour of expressions, in parti
ular they
an tell us whi
h fun
tions have
the same behaviour, and whi
h transformations (of one expression into an-
other) are permissible. We shall return to this topi
in se
tion 2.11, after
dis
ussing typed -
al
uli.
Exer
ises
2.5. Show that if e has the form y : e0 where x is not free in e0 then
(x : ex) ! e
2.5 Expressiveness
The untyped -
al
ulus is a simple theory of (pure) fun
tions, yet
ompu-
tationally it is as strong as other fundamental theories of
omputation. It
is Turing-
omplete, exhibiting an equivalen
e with Turing
omputability.
One half of this equivalen
e
onsists in showing that obje
ts su
h as the
natural numbers, booleans and so forth
an be represented as terms, and
that re
ursive fun
tions
an be dened over them. Yet again, Barendregt
provides a suitable referen
e for this material. One representation of the
natural numbers is as the iterators, n being represented by
f : x : f (f : : : f (f x) : : :)
| {z }
n
whi
h are in normal form. To derive re
ursive fun
tions, with denitions
like
f x df : : : f : : :
whi
h
an be written
f df x: : : : f : : :
we need to be able to solve equations of the form
f df R f
where R is a term. In fa
t we
an dene operations, F ,
alled xed
point
ombinators whi
h solve these equations thus:
FR!
! R (F R)
Two examples are the expressions:
where df a : b : (b(aab)))
42 CHAPTER 2. FUNCTIONAL PROGRAMMING AND -CALCULI
Denition 2.17 Given a set B of base types, we form the set S of simple
types
losing under the rule of fun
tion type formation. This states that
if and are types then so is ( ) ). We assume that `)' is right
asso
iative, and omit bra
kets a
ordingly.
In typed lambda
al
uli ea
h -expression (or term, we use these inter-
hangeably) has a type. We shall write
e:
for `e is a -expression of type '. We spe
ify the type of ea
h dened
-expression below.
Denition 2.18 The expressions of the typed -
al
ulus have three forms.
Individual variables or simply variables, v;0 ; v;1 ;;2 ; : : :, for ea
h type
.
v;i :
We shall write x ; y ; z ; : : :, with or without type subs
ripts for ar-
bitrary individual variables in what follows.
Appli
ations If e1 :( ) ) and e2 : then
(e1 e2 ):
We
an only form an appli
ation when the type of the argument is
the same as the type of the domain of the fun
tion.
Abstra
tions If x : and e : then
(x : e) : ( ) )
The type of an abstra
tion is a fun
tion type, whose domain is the
type of the formal parameter and whose range is the type of the
fun
tion body (or result).
Many notions dened in the untyped
ase
arry over to here. These
in
lude the substitution me
hanism, - and -redu
tion and
onvertibil-
ity and the notions of
anoni
al element: normal form, head normal form
et
etera. It is easy to see that
onvertible expressions must be of the same
type.
Many results
arry over too. The Chur
h-Rosser theorems for and
-redu
tion have the same proofs. Given a variable x , we are unable to
form the appli
ation x x and thus unable to dene
and the xed point
ombinators we saw above. This is no a
ident, as we
an prove
44 CHAPTER 2. FUNCTIONAL PROGRAMMING AND -CALCULI
and
` e : 2
then
` (e e ):
1 2
Abstra
tions If
; x: ` e:
then
` (x : e) : ( ) )
The type of an abstra
tion is a fun
tion type, whose domain is the
type of the formal parameter and whose range is the type of the
fun
tion body (or result).
The rule giving the type of an abstra
tion has one unusual aspe
t. The
assumption that x : is used in typing the body of the fun
tion, e, but is
not needed to give a type to the fun
tion itself. Why is this? In general
the variable x will appear free in the expression e | we
annot type an
expression
ontaining a free variable without knowing the type for the vari-
able, and this is given by the assumption. On the other hand, in (x : e)
x is bound and asso
iated with the type it is assumed to have in the ex-
pression e, whi
h is the s
ope of that parti
ular de
laration of the variable
x.
Exer
ises
2.6. Show that
x : y : z : (xz )(yz ) : ( ) ) ) ) ( ) ) ) ( ) )
2.7. Explain why x : xx and the xed point
ombinator F are not terms
of the typed -
al
ulus.
How should the proof pro
eed? One method we
annot use is a straight-
forward stru
tural indu
tion, as a proof using this alone would
arry over
to the untyped
ase, where we know that not even the terms with normal
form are strongly normalising. The method we use will involve an indu
tion
over the stru
ture of types:
Denition 2.22 The method of Indu
tion over Types states that to
prove the result P ( ) for all types it is suÆ
ient to
Prove P () for all base types 2 B . This is
alled the base
ase.
Prove P ( ) ) assuming that P () and P ( ) hold. This is
alled
the indu
tion step.
As is
ommon in proofs by indu
tion, in order to prove a property
R(e) of every expression e we in fa
t prove a strengthening R0 of R. This is
be
ause the obvious indu
tion hypothesis will not be strong enough to work
at the indu
tion step. This is the
ase with the property of e is strongly
normalising whi
h we abbreviate `e is SN': two terms e and e0 may be
strongly normalising without it being
lear that the appli
ation (e e0) is so.
(It will of
ourse be strongly normalising by the proof we
onstru
t, but
that begs the question of how we establish it.)
where at ea
h stage for exa
tly one index j , fj ! gj and for the others,
fi gi . This means that if there is an innite redu
tion sequen
e from
xe1 : : : ek then there must be one from one of the ei s, a
ontradi
tion to
their being SN.
(
) A redu
tion sequen
e from ex will either have the form
ex ! e1 x ! e2 x ! : : : ! en x ! : : :
or the form
ex ! e1 x ! : : : ! (y : f ) x ! f [x=y℄ ! f [x=y℄ ! f [x=y℄ ! : : :
1 2
where
y : f ! y : f1 ! y : f2 ! : : :
is a redu
tion sequen
e
ontinuing e ! e1 ! : : :. In either
ase an innite
sequen
e starting at ex gives rise one starting at e.
(d) A redu
tion sequen
e starting at x : e will have the form
y : e ! y : e1 ! y : e2 ! : : :
where
e ! e1 ! e2 ! : : :
and so an innite sequen
e starting at y : e gives rise to another starting
at e. 2
The proof of the theorem itself is based on two further lemmas. In
the rst we show that stable obje
ts are strongly normalising (at the same
time showing that variables are stable), and in the se
ond we show that all
obje
ts are stable.
Lemma 2.25 (a) If e 2 k k then e 2 SN
(b) If xe1 : : : en : and e1 ; : : : ; en 2 SN then xe1 : : : en 2 k k
(
) If x : then x 2 k k.
48 CHAPTER 2. FUNCTIONAL PROGRAMMING AND -CALCULI
Proof: We prove (a), (b) and (
) by a simultaneous indu
tion over the
type .
Base
ase: is a base type. The property (a) is true by denition of
stability for a base type.
For (b), if e1 ; : : : ; en 2 SN then by lemma 2.24, part (b), xe1 : : : ek will be
strongly normalising, and sin
e is a base type, the expression is stable.
Finally, to prove (
) observe that any variable is strongly normalising and
therefore stable if it is of base type.
Indu
tion step: We assume that is the type ( ) ) and that the
results (a), (b) and (
) hold for the types ; .
To prove (a) we assume that e 2 k k. We have to prove that e is SN. Take
x of type . By (
) for , x is stable, and so by the denition of stability
for e, ex will be stable. ex is of type and so by (a) for , ex is SN. Using
lemma 2.24 part (
), e is therefore SN.
Now we show (b). To prove that xe1 : : : en 2 k k we need to show that
xe1 : : : en f is in kk, if f 2 kk. By hypothesis e1 ; : : : ; en 2 SN and by (a)
for , f is also SN. The expression xe1 : : : en f is of type and so by (b) for
,
xe1 : : : en f 2 kk
as required.
Finally, we show (
). Suppose that f in kk. By (a) for , f is SN, and
sin
e the expression (xf ) has type , by (b) for , (xf ) is in kk, so x is
stable. 2
Our task now is to show that all expressions are stable. We aim to do this
by stru
tural indu
tion over the expressions. We know that variables are
stable by the previous result, and we prove easily that appli
ation preserves
stability: indeed this was a motivation of the denition. The
ase of -
abstra
tion is more tri
ky. We aim to prove that x : f is stable, assuming
f is, and so we have to prove that for all stable g of the appropriate type,
(x : f ) g
is stable. This expression redu
es to f [g=x℄. We need to dedu
e the sta-
bility of the former from that of the latter. In fa
t we need to prove a
generalisation of this to get the indu
tion to work, whi
h readers
an see
for themselves by trying a dire
t proof. The generalisation is
lause (b) of
the next lemma.
Before we state the lemma, we give another denition.
Denition 2.26 A s-instan
e e0 of an expression e is a substitution in-
stan
e e0 e[g1=x1 ; : : : ; gr =xr ℄ where the gi are stable expressions.
2.7. STRONG NORMALISATION 49
onstru
tion, the fun
tion spa
e. We
an extend the system in two dierent
ways, adding both new base types and new
onstru
tors. First we look at
type
onstru
tors.
Familiar type
onstru
tors in
lude the produ
t type and the disjoint
sum (or disjoint union). In Pas
al these together are embodied in the
variant re
ord type. The addition of these is standard; we review brie
y
the addition of the produ
t type to the simply typed -
al
ulus now.
To the denition of types we add the third
lause that is a type
if and are.
We add two
lauses to the denition of expressions:
Pairs If x : and y : then
(x; y) :
The pair (x; y) is a member of the produ
t type.
Proje
tions If p : then fst p : and snd p : . The operations
fst and snd proje
t a pair onto its
omponents.
To the rules of
omputation we add the rules
fst (p; q) ! p snd (p; q) ! q
whi
h show that fst and snd do indeed behave as proje
tion opera-
tions, and we ask also that redu
tion is preserved by pairing, so that
if
p ! p0
then
(p; q) ! (p0 ; q) (q; p) ! (q; p0 )
To the rules we
an also add
(fst p; snd p) ! p
for p of produ
t type. This implies the extensionality rule that an
element of a produ
t type is
hara
terised by its
omponents, sin
e if
fst p $
$ fst q snd p $
$ snd q
then
p $
$ (fst p; snd p) $
$ (fst q; snd q) $
$q
52 CHAPTER 2. FUNCTIONAL PROGRAMMING AND -CALCULI
for the left hand side to be a term of the typed
al
ulus, e must be of a
fun
tion type. Similarly, in
(fst p; snd p) ! p
for fst p and snd p to be well-formed, p must be of produ
t type. We will
take up this dis
ussion further below, after adding a base type.
Exer
ises
2.8. Show that
x : y : z : (x (y; z )) : (( ) ) ) ) ( ) ) )
and that
x : y : (x (fst y) (snd y)) : ( ) ) ) ) (( ) ) )
P re e0 f (n + 1) ! f n (P re e0 f n)
F (n + 1) df f n (F n)
where
f n m df h (n + 1)
h and the fun
tion just dened take the same values at every numeral
0, su
0, su
(su
0) and so on: we state that the two fun
tions are
themselves equivalent by adding the redu
tion rule:
P re
(h 0) (n : m : h (n + 1)) !h
for h of type N ) . Again, it is worth noting that the types of
the obje
ts related by this rule are not
ompletely arbitrary: they are
fun
tions over the domain N .
We
an extend the strong normalisation result of se
tion 2.7 to a system
ontaining produ
t types and a base type of natural numbers. We retain the
notion that expressions of type N are stable if and only if they are strongly
normalising, and add the
lause that pairs p are stable if and only if their
omponents fst p and snd p are. It is not hard to show that all stable
obje
ts are strongly normalising; we then have to show that all obje
ts are
stable.
To do this we need an auxiliary result analogous to lemma 2.27, part
(b), stating that if all the expressions a
essible by a single redu
tion from
the expression P re
e0 f t are stable, then so is the expression itself. We
prove this by a type indu
tion.
2.10. GENERAL RECURSION 55
Given this result, the proof of stability of all terms pro
eeds by a stru
-
tural indu
tion, with an auxiliary indu
tion over the natural numbers in
the proof that
P re
e0 f t
is stable for stable e0 , f and t.
Further details of this proof
an be found in [Tai67, GLT89, Tro73℄
Exer
ises
2.9. Give primitive re
ursive denitions of addition and multipli
ation.
initiated by the work of S
ott and Stra
hey in the late sixties. The values
omputed by general re
ursive fun
tions are members of domains, whi
h
reify the ideas of approximation and limit by whi
h we
an give an informal
explanation of re
ursion. More details
an be found in the ex
ellent [S
h86℄.
In type theory we adopt a dierent approa
h, keeping to systems in
whi
h normalisation, at least in some form, is assured.
We therefore feel justied in making the distin
tion between the two kinds
of rule whi
h we
alled
omputation and equivalen
e rules above. The
omputation rules suÆ
e for the evaluation of parti
ular terms, whilst the
equivalen
e rules are used when reasoning about the general behaviour of
fun
tions (applied to terms whi
h may
ontain variables).
Chapter 3
The aim of this brief
hapter is to introdu
e the major issues underlying the
on
i
t between `
onstru
tive' and `
lassi
al' mathemati
s, but it
annot
hope to be anything other than an hors d'oeuvre to the substantial and
lengthy dialogue between the two s
hools of thought whi
h
ontinues to
this day.
Lu
kily, there are other sour
es. Bishop gives a rousing
all to the
onstru
tive approa
h in the prologue to [BB85℄, whi
h is followed by a
losely argued `Constru
tivist Manifesto' in the rst
hapter. Indeed the
whole book is proof of the viability of the
onstru
tivism, developing as it
does substantial portions of analysis from su
h a standpoint. It
ontains a
bibliography of further work in the eld.
An invaluable histori
al a
ount of the basis of the
on
i
t as well as
subsequent a
tivity in the eld
an be found in the histori
al appendix
of [Bee85℄. The rst part of [Bee85℄, entitled `Pra
ti
e and Philosophy of
Constru
tive Mathemati
s', also gives a most
apable summary of both the
s
ope and the foundations of
onstru
tive mathemati
s. [Dum77℄ is also a
good introdu
tion, looking in detail at the philosophy of intuitionism, and
the re
ent survey [TvD88℄ also serves its purpose well.
Bishop identies
onstru
tivism with realism,
ontrasting it with the
idealism of
lassi
al mathemati
s. He also says that it gives mathemati
al
statements `empiri
al
ontent', as opposed to the purely `pragmati
' nature
of parts of
lassi
al mathemati
s, and sums up the programme of [BB85℄
thus `to give numeri
al [i.e.
omputational ℄ meaning to as mu
h as possible
of
lassi
al abstra
t analysis'.
A
onstru
tive treatment of mathemati
s has a number of interlinked
aspe
ts. We look at these in turn now.
59
60 CHAPTER 3. CONSTRUCTIVE MATHEMATICS
Mathemati
al obje
ts
The nature of obje
ts in
lassi
al mathemati
s is simple: everything is a
set. The pair of obje
ts a and b `is' the set ffag; fa; bgg and the number 4
`is' the set
onsisting of
; ; f;g ; f ;; f;g g ; f ;; f;g; f ;; f;g g g
Fun
tions are represented by sets of pairs
onstituting their graph, so that
the su
essor fun
tion on the natural numbers is
f (0; 1); (1; 2); (2; 3); : : : g
whi
h is itself shorthand for
f ff;g; f;; f;ggg ; fff;gg; ff;g; f;; f;g ggg : : :
Obje
ts like this are innite, and an arbitrary fun
tion graph will be in-
nitary, that is it will have no nite des
ription. Su
h obje
ts fail to have
omputational
ontent: given the nitary nature of
omputation, it is im-
possible
ompletely to spe
ify su
h an obje
t to an algorithm. This is an
example of a fundamental tenet of
onstru
tive mathemati
s:
Every obje
t in
onstru
tive mathemati
s is either nite, like
natural or rational numbers, or has a nitary des
ription, su
h
as the rule
x : x + 1
whi
h des
ribes the su
essor fun
tion over the natural numbers
The real numbers provide an interesting example: we
an supply a des
rip-
tion of su
h a number by a sequen
e of approximations, (an )n say. This
sequen
e is nitary if we
an write down an algorithm or rule whi
h allows
us to
ompute the transformation
n 7! an
for all n. (We shall return to the example of the reals below.)
63
This is a prin
iple to whi
h Bishop adheres in prin
iple, but to smooth the
presentation of the results, he adopts a poli
y of systemati
suppression
of the eviden
e, invoking it only when it is ne
essary. This s
hizophreni
attitude will also pervade the systems we shall introdu
e.
There is a relationship between this view and the
lassi
al. The
on-
stru
tive model of obje
ts like the reals
an be seen as related to the
lassi-
al; witnessing eviden
e whi
h
an always be provided by a non-
onstru
tive
existen
e proof in the
lassi
al setting is in
orporated into the obje
t itself
by the
onstru
tivist.
A nal area of note is equality over innite obje
ts like the reals. For
the natural numbers, we
an judge whether two obje
ts are equal or not,
simply by examining their form. For the reals, we are not interested so
mu
h in the synta
ti
form of two numbers as their respe
tive limits. Two
reals (an )n , (bn )n (we suppress the eviden
e!) have the same limit, and so
are deemed equal, if
8n:9m:8i m:8j m : jai bj j < 1=n
We
annot expe
t that for an arbitrary pair of reals that we
an de
ide
whether a = b or a 6= b,as one
onsequen
e of this is the limited prin
iple
of omnis
ien
e. A nal pre
ept whi
h is useful here is
Negative assertions should be repla
ed by positive assertions
whenever possible.
In this
ontext we repla
e `6=' by a notion of `apartness'. Two real numbers
(an )n , (bn )n are separated, a#b, if
9n:9m:8i m:8j m : jai bj j > 1=n
This is a strong enough notion to repla
e the
lassi
ally equivalent inequal-
ity. We shall return to the topi
of the real numbers in se
tion 7.6.
One area whi
h is overlooked in this study is the link between
ategory
theory and logi
, the topi
of [LS86℄. This link has a number of threads,
in
luding the relationship between the -
al
ulus and
artesian
losed
at-
egories, and the
ategory-theoreti
models of intuitionisti
type theory pro-
vided by toposes. The interested reader will want to follow up the primary
referen
es in [LS86℄.
Con
lusion
We have seen that
onstru
tive mathemati
s is based on prin
iples quite
dierent from
lassi
al mathemati
s, with the idealisti
aspe
ts of the latter
repla
ed by a nitary system with
omputational
ontent. Obje
ts like
fun
tions are given by rules, and the validity of an assertion is guaranteed
by a proof from whi
h we
an extra
t relevant
omputational information,
rather than on idealist semanti
prin
iples. We lose some theorems, su
h
as
Theorem 3.1 (Intermediate Value Theorem - Classi
al)
Suppose that f is
ontinuous on [0; 1℄ with f (0) < 0 and f (1) > 0, then
there is an r 2 [0; 1℄ with f (r) = 0.
All is not lost, and we
an prove the weaker
Theorem 3.2 (Intermediate Value Theorem - Constru
tive)
Suppose that f is
ontinuous on [0; 1℄ with f (0) < 0 and f (1) > 0, then
for all " > 0 there is an r 2 [0; 1℄ with jf (r)j < ".
The
onstru
tive version states that we
an get arbitrarily
lose to the
root, and of
ourse, that is all we
ould expe
t to do, from a
omputational
point of view. In this respe
t, we have in the latter theorem a truer pi
ture
of our `empiri
al'
apabilities.
For other examples, and more
ogent pleading of the
onstru
tivist
ase,
we would heartily re
ommend the opening passages of [BB85℄. Indeed, the
whole book will repay detailed study. We now pass on to looking at our
formal system for type theory.
66 CHAPTER 3. CONSTRUCTIVE MATHEMATICS
Chapter 4
This
hapter forms the fo
us of the book, drawing together the three themes
of logi
, fun
tional programming and
onstru
tive mathemati
s into a single
system, whi
h we investigate, develop and
riti
ise in the
hapters to
ome.
The short dis
ussion of
onstru
tive mathemati
s introdu
ed the idea that
proofs should have
omputational
ontent; we saw that to a
hieve this goal,
the underlying logi
of the system needed to be
hanged to one in whi
h
we only assert the validity of a proposition when we have a proof of the
proposition. Be
ause of this, the system we dene is dierent from those
of the rst
hapter, deriving as it does statements of the form
`p is a proof of the proposition P ',
whi
h we write thus:
p:P
Central to type theory is the duality between propositions and types, proofs
and elements: a proof of a proposition T
an be seen as a member of the
type T , and
onversely. Ideas whi
h
ome from one side of the divide
an
be re-interpreted on the other, enri
hing both elds. We rst present type
theory as a logi
al system, and then re-interpret the derivation rules as
rules for program
onstru
tion in a typed fun
tional language, with p : P
read as
`p is a member of the type P '
67
68 CHAPTER 4. INTRODUCTION TO TYPE THEORY
The
hapter begins with an informal examination of what it means for some-
thing to be a proof of a formula of propositional logi
. Before introdu
ing
the formal system itself, we look at the general form that the rules will take,
and establish some important terminology whi
h will distinguish between
the formal proof obje
ts, like p above, and derivations of the statements,
or judgements , like p : P .
With ea
h
onne
tive or type forming operation we asso
iate four kinds
of rule. The familiar rules of introdu
tion and elimination des
ribe how
ertain propositions
an be proved. From the programming point of view
they assert the existen
e of
ertain obje
ts of
ertain types; in other words,
they spe
ify the syntax of a programming language. To des
ribe a language
we need to supply not only the syntax but also also to explain how to
evaluate or run programs written in the language. The
omputation rules
explain this, with
e1 ! e2
denoting `e1 redu
es to e2 in one step of
omputation'; their logi
al inter-
pretation is a des
ription of how proof obje
ts may be simplied. We also
give a formation rule whi
h embodies the syntax of the
onne
tive: this
presentation of the syntax together with the rules of the system is one of
the distin
tive features of type theory, and it is ne
essary be
ause of the
interdependen
e in the denitions of types and values.
The formal presentation of the logi
al rules for propositional
al
ulus is
followed by a number of examples, in whi
h we see standard fun
tions like
omposition serving as proofs of familiar statements like
(A ) B ) ) (B ) C ) ) (A ) C )
After this we introdu
e the logi
al notions of universal and existential quan-
tiers, whi
h when given a programming interpretation, dene dependent
fun
tion spa
es and modules, amongst other things.
Programming is dependent on the presen
e of data types su
h as the
booleans, the nite types and the natural numbers, whi
h are introdu
ed
next. The one and zero element types are also representatives of the logi
al
statements T ruth and F alsity, truth having the trivial proof, and falsity
having none. Innite data types are
hara
terised by prin
iples of denition
by re
ursion and proof by indu
tion; as we
annot dene every
ase of a
fun
tion or examine every
ase in a proof, we need these indire
t methods.
The two methods go hand-in-hand, as we dene an obje
t by re
ursion
and then prove any property of the obje
t by indu
tion. One of the most
elegant aspe
ts of the type theoreti
approa
h is that the two are identi
al
| a proof by indu
tion is nothing other than a proof obje
t dened using
re
ursion.
4.1. PROPOSITIONAL LOGIC: AN INFORMAL VIEW 69
This re
ursive
hara
terisation
arries over to other types, and we look
at binary trees as an example of a general well-founded type
onstru
tion.
The primitive propositions introdu
ed thus far are the types bool, N
and so on. The assertion of the identity
a =A b
of two values a and b of type A forms a fundamental atomi
proposition, or
type. Here we
an see the interdependen
e between the denitions of types
and values in the language: a =A b is a type of the language if a and b are
values of type A.
There is another relation linking items, generated by ` ! ': two ex-
pressions are
onvertible if a sequen
e of forwards or ba
kwards redu
tions
using ` ! ' leads from the rst to the se
ond: as ea
h step leads from equals
to equals,
onvertible expressions are deemed to be equal internally, so that
we
an use the relation `=' to reason about the
omputational behaviour
of programs. This means that our system gives an integrated treatment of
programming and veri
ation: we
an prove a program
orre
t, or develop
it from a spe
i
ation, in the same system in whi
h the program is written.
means that we are not interested in validity alone, but in expli
it demon-
strations or proofs of propositions. The judgements we introdu
e therefore
have the form `p : P ' whi
h should be read `the obje
t p is a proof of the
proposition P '.
The rule to introdu
e
onjun
tions, a
ording to the explanation in the
previous se
tion, will be
p:A q :B
(^I )
(p; q) : (A ^ B )
The rules are to be used just as were the logi
al rules in
hapter 1, to
produ
e derivations of judgements: a rule is applied to derivations of the
judgements above the line to produ
e a derivation of the judgement below
the line. Note that this is a
hange of terminology from the earlier
hapter;
there we used rules to give proofs of judgements | here we use rules to
derive judgements whi
h themselves
ontain proof obje
ts or
onstru
tions.
To re-iterate, proofs and propositions form the obje
t language; derivations
are the means by whi
h we infer judgements
on
erning the obje
t language.
Derivations are built indu
tively by applying the dedu
tion rules. If we now
introdu
e the rules for
onjun
tion elimination, we will be able to look at
an example whi
h we examined informally a little earlier.
r : (A ^ B ) r : (A ^ B )
(^E1 ) (^E2 )
fst r : A snd r : B
Here we see a derivation of the judgement (snd r; fst r) : (B ^ A)
r : (A ^ B ) r : (A ^ B )
(^E2 ) (^E1 )
snd r : B fst r : A (^I )
(snd r; fst r) : (B ^ A)
The proof obje
t derived is the pair (snd r; fst r) whi
h is shown to prove
(B ^ A) assuming that r is a proof of (A ^ B ).
As in our earlier treatment of logi
, derivations
an be based on assump-
tions, and these assumptions
an be dis
harged by
ertain of the rules. We
will use the same notation for dis
harge as we did earlier.
Note that as there is no possibility of ambiguity, we have used the same
names for the proof rules, (^I), (^E) and so on, as we did in the
hapter
overing logi
.
Some of our rules will involve another judgement, whi
h asserts that a
parti
ular sequen
e of symbols is a formula:
A is a formula
4.3. THE RULES FOR PROPOSITIONAL CALCULUS 73
? is a formula (?F )
and we have no introdu
tion rule asso
iated with ?, as we know of no
way of forming proofs of the absurd proposition. We
an eliminate it freely,
thus,
Elimination Rule for ?
p:?
(?E )
abortA p : A
This rule says that if we
an prove absurdity (with p), then the proof
abortA p proves A. This is the se
ond half of our
hara
terisation of ? as
absurdity. Not only do we give no method by whi
h ?
an be introdu
ed,
but in order to show that we did not simply forget to give these rules we
say that given any su
h proof our system must
rash, and prove everything,
4.3. THE RULES FOR PROPOSITIONAL CALCULUS 77
the rule of ex falso quodlibet. There are no
omputation rules asso
iated
with su
h an obje
t | it simply registers the fa
t that the obje
t is proved
dire
tly from absurdity.
The rule for impli
ation introdu
tion dis
harges an assumption of the
form x : A where x is a variable. How do these assumptions be
ome parts
of derivations? In order for an assumption x : A to be sensible, we need A
to be a formula. This is a suÆ
ient
ondition too, so we have
Rule of Assumption
A is a formula
(AS )
x:A
We make the impli
it (informal!) assumption that our sets of assump-
tions will always be
onsistent: we will not assume that any variable is a
proof of more than one formula. We elaborate on the details of this in the
hapter to
ome.
This rule is unusual, in that it shows that our assumptions do not appear
at the leaves of derivations, but rather only after a derivation that the
expression whi
h follows the
olon is indeed a formula.
In many of our examples we will relax this
onstraint, omitting the
derivation of `A is a formula'; this will only be done when the derivation
is trivial, or we simply assume, informally, that the derivation has been
performed prior to the derivation at issue.
This
ompletes our exposition of the propositional part of type theory
| in the next se
tion we get to the heart of our exposition, and tie the link
between fun
tional programming (as represented by the lambda
al
ulus)
and logi
.
Exer
ises
4.1. Show that
onjun
tion is asso
iative by deriving a proof of the formula
(A ^ B ) ^ C ) A ^ (B ^ C )
4.2. Show that the formula (:A _ B ) ) (A ) B ) is valid by exhibiting a
proof obje
t for it. Do you expe
t the
onverse, (A ) B ) ) (:A _ B ), to
be provable?
4.3. Show that from the assumption x : (A _ :A) that you
an derive a
proof obje
t for the formula (::A ) A). Show that you
an nd a proof
obje
t for the
onverse, (A ) ::A) without this assumption.
4.4. Show that from the assumptions x : ((A ^ B ) ) C ) and y : A you
an derive a proof of B ) C . What is the formula whi
h results from the
dis
harge of the two assumptions, and what proof obje
t of this formula is
given by your
onstru
tion?
78 CHAPTER 4. INTRODUCTION TO TYPE THEORY
Another way of looking at the rules is to say that the formation rules
explain the types of the language and that the introdu
tion and elimination
rules explain the typing rules for expressions (and so explain how type
he
king for the system should pro
eed) | together these des
ribe the stati
part of a traditional language, with the
omputation rules explaining the
dynami
s of its behaviour. We shall see in se
tion 4.10 that the distin
tion
between the stati
and the dynami
be
omes blurred in the full system, as
type
he
king and
omputation be
ome inextri
ably linked.
We now run through the rules
onne
tive by
onne
tive,
hanging the
judgement ` is a formula' to ` is a type' to re
e
t our dierent orientation.
Formation Rule for ^
A is a type B is a type
(^F )
(A ^ B ) is a type
4.4. THE CURRY HOWARD ISOMORPHISM 79
ases (inr r) f g ! gr
The last rules we gave were for the type ?, the absurd proposition. We
hara
terised this as a formula without proofs, so under our other view, it
is a type without members, the empty type.
Formation Rule for ?
? is a type (?F )
Elimination Rule for ?
p:?
(?E )
abortA p : A
This rule says that if we
an nd a member p of the empty type then
our program should abort | we express this by saying that for any A at
all, the obje
t abortA p is a member of A.
The nal rule we saw was the rule of assumption; in order for the as-
sumption that x is a member of A to make sense, A must be a type.
Rule of Assumption
A is a type
(AS )
x:A
As we said earlier, in many of our examples we shall omit the derivation
of `A is a type', assuming it has already been performed. Nonetheless, the
system does not make sense without this
ondition.
That
ompletes our se
ond reading of the rules, and shows that they
an equally well be read as
rules for the types and obje
ts of a typed fun
tional programming
system and as
rules for the propositions and proofs of a logi
.
We shall explore the
orresponden
e further in the
oming se
tions, seeing
some of the rami
ations of the
orresponden
e in propositional logi
, and
also how extensions to both the logi
and the fun
tional language have their
analogues. Finally we will explore the
onsequen
es of this
orresponden
e
for programming methodology. Note also that we have not yet introdu
ed
any dependent types | we do this with the identity or I types of se
tion
4.10.
4.5. SOME EXAMPLES 83
Exer
ises
4.5. Given a fun
tion of type A ) (B ) C ) how would you dene a
fun
tion of type (A ^ B ) ) C from it? How would you do the reverse?
4.6. Show that from obje
ts x : A and y :(B _ C ) you
an derive an obje
t
of type (A ^ B ) _ (A ^ C ).
4.7. Show how to dene a fun
tion of type
(A ^ B ) ) (C ^ D)
from fun
tions f : A ) C and g : B ) D.
and we make a third assumption, x : A whi
h will be dis
harged during the
proof.
x : A a :(A ) B )
() E ) b :(B ) C )
(a x): B
() E )
(b (a x)): C
This gives an element of C , depending upon the element x of A. We now
abstra
t over this to give
[x : A℄1 a :(A ) B )
() E ) b :(B ) C )
(a x): B
() E )
(b (a x)): C () I )1
xA : (b (a x)) : (A ) C )
We have on the last line a derivation of a fun
tion of type A ) C , as was
required.
The fun
tion derived here is the
omposition of the fun
tions b and a.
If we abstra
t over these, we form the
omposition fun
tion
a(A)B) : b(B)C ) : xA : (b (a x))
whi
h has type
(A ) B ) ) (B ) C ) ) (A ) C )
Note that we have assumed that the `)' is right-asso
iative in writing the
type above.
A standard result in logi
is
(A ) B ) ) (:B ) :A)
Re
all that we dened :A to be A ) ?, so expanding the denitions we
have
(A ) B ) ) ((B ) ?) ) (A ) ?))
It is pre
isely the
omposition operator whi
h gives this proof.
As for the propositional
onne
tives, our rules are of four kinds. We
start by giving the rules for the universal quantier.
Formation Rule for 8
[x : A℄
..
.
A is a formula P is a formula
(8F )
(8x : A) : P is a formula
This shows a rather more subtle formation rule than we have seen so
far. There are two hypotheses
The rst is that A is a formula, or type.
The se
ond, that P is a formula, on the assumption that x is a variable
of type A.
This is an analogue, at the level of types, of the rule of -abstra
tion, in
whi
h the typing of the expression forming the body of the fun
tion depends
upon the hypothesis whi
h types the bound variable. We have not yet seen
how to build type expressions (or propositions) whi
h depend upon free
variables; these result from our introdu
ing the atomi
propositions of the
system, in
luding equality in se
tion 4.10, and through re
e
tion prin
iples
whi
h are
onsequen
es of the introdu
tion of universes (see se
tion 5.9).
If we
an prove P by a proof p whi
h may depend upon the assumption
of the existen
e of a variable, x say, of type A, then we are entitled to
assert the universal generalisation, abstra
ting over the variable x, as long
as x is not free in any other of the assumptions upon whi
h the proof
depends. This is just the
ondition of being arbitrary whi
h we dis
ussed
in our introdu
tion to logi
earlier. Note also that formally any remaining
o
urren
es of x in the assumptions would be a `dangling referen
e' to an
obje
t outside its s
ope, as it were.
Introdu
tion Rule for 8
If in a proof p of P no assumptions other than x : A
ontain x free, then
we
an infer the universal generalisation of P by means of the lambda
abstra
tion of p over x. This abstra
tion dis
harges the assumption x.
[x : A℄
..
.
p:P
(8I )
(x : A) : p : (8x : A) : P
90 CHAPTER 4. INTRODUCTION TO TYPE THEORY
In the proof above the assumptions of the two hypotheti
al proofs are listed
one above the other, rather than next to ea
h other; this makes the repre-
sentation of the derivation easier to read. If we now abstra
t over both the
hypotheses (and omit the typings on the variables) we have
r:p:x:(r x)(p x)
whi
h is of type
(8x : A) : (B ) C ) ) (8x : A) : B ) (8x : A) : C
if we
hoose to omit the type annotations on the variables. This fun
tion is
familiar to fun
tional programmers as the S
ombinator, as well as proving
the formula
(A ) (B ) C )) ) (A ) B ) ) (A ) C )
in the
ase that B and C do not involve the variable x.
Next we prove the equivalen
e between the following pair of formulae
((9x : X ) : P ) ) Q (8x : X ) : (P ) Q)
in the
ase that x is not free in Q. (It is not valid in general | think of the
ase when P and Q are the same.) Reading the rule (8I ) ba
kwards, we
see that to nd an obje
t of type (8x : X ) : (P ) Q) it is suÆ
ient to nd
an obje
t in (P ) Q) assuming we have an x in X . To nd an obje
t of
type (P ) Q) it is enough to nd an obje
t of type Q assuming an obje
t
of type P (as well as the obje
t of type X we assumed earlier). Building
the proof tree ba
kwards we have
?? : Q
() I )
pP : ?? : (P ) Q)
(8I )
xX : pP : ?? : (8x : X ) : (P ) Q)
There are
onstraints on the form of proof of ?? here. We
an only introdu
e
a universal quantier or an impli
ation abstra
ting over the variable y, say,
in the
ase that y is free only in the assumption dis
harged. How are we to
infer Q? Pro
eeding from the assumptions we have
x:X p:P
(9I )
(x; p) : (9x : X ) : P
and then by modus ponens, we have
x:X p:P
(9I ) e : ((9x : X ) : P ) ) Q
(x; p) : (9x : X ) : P
() E )
e (x; p) : Q
94 CHAPTER 4. INTRODUCTION TO TYPE THEORY
Putting the parts together, and repla
ing the unknown terms ?? with a
tual
values, we have
[x : X ℄2 [p : P ℄1
(9I ) e : ((9x : X ) : P ) ) Q
(x; p) : (9x : X ) : P
() E )
e (x; p) : Q
() I )1
pP : (e (x; p)) : (P ) Q)
(8I )2
xX : pP : (e (x; p)) : (8x : X ) : (P ) Q)
The rst abstra
tion, over p, is legitimate as p is free in none of the other
assumptions, and the se
ond is OK as there is only one a
tive assumption
at this stage. Note, however, that we
annot dis
harge the assumptions
in the opposite order, sin
e x will in general be free in P and thus in the
assumption p : P .
How does the
onverse proof pro
eed?
To nd a proof of an impli
ation, we pro
eed with an assumption of the
ante
edent formula, in this
ase p : (9x : X ) : P , and try to nd ?? : Q.
Pro
eeding forward from the assumption p, we have
p : (9x : X ) : P
(9E 0 )
Snd p : P [Fst p=x℄ 2
Using the other assumption, whi
h is e : (8x : X ) : (P ) Q), we
an mat
h
the hypothesis of this impli
ation with P [Fst p=x℄ by universal elimination
p : (9x : X ) : P
(9E10 ) e : (8x : X ) : (P ) Q)
Fst p : X (8E )
(e(Fst p)) : P [Fst p=x℄ ) Q
Note that in the
on
lusion we have Q and not Q[Fst p=x℄, sin
e we have
assumed that x is not free in Q, and we noted that Q[t=x℄ Q (for any
t) in su
h a
ase. We now apply impli
ation elimination, and
omplete as
above.
[p : (9x : X ) : P ℄1
[p : (9x : X ) : P ℄ 1
e : (8x : X ) : (P ) Q)
.. ..
. .
Snd p : P [Fst p=x℄ (e(Fst p)) : P [Fst p=x℄ ) Q
() E )
(e(Fst p))(Snd p) : Q
p : ((e(Fst p))(Snd p)) : ((9x : X ) : P ) ) Q () I )1
Is there a fun
tional interpretation of the equivalen
e we have seen above?
If we
onsider the
ase in whi
h P does not
ontain x free, we have the
types
(X ^ P ) ) Q X ) (P ) Q)
4.6. QUANTIFIERS 95
These two fun
tion spa
es give two dierent representations of binary fun
-
tions. In the rst, the fun
tion takes a pair of arguments, of type (X ^ P ),
to a result of type Q. The other representation, whi
h is often
alled the
urried form in honour of Haskell B. Curry, the -
al
ulus pioneer, makes
the fun
tion higher order. By this we mean that on being passed an argu-
ment of type X , the fun
tion returns a fun
tion of type (P ) Q) whi
h
expe
ts an argument of type P , the se
ond half of the pair. (We often
all
the rst representation the un
urried form, in
ontrast to the latter.)
The pair of fun
tions we derived above
an be seen to map from one
fun
tion representation to another
xX : pP : (e (x; p))
takes separately the two arguments x and p, forms a pair from them and,
applies the un
urried fun
tion e to the result. Conversely,
p : ((e(Fst p))(Snd p))
takes a pair p as argument, and splits the pair into its
omponents Fst p
and Snd p, and applies the
urried fun
tion e to the two halves one at a
time.
The fun
tions perform a similar fun
tion in the general
ase that P
depends upon x, and we deal with dependent sum and produ
t types.
Exer
ises
4.12. Give a derivation of a proof obje
t of the formula
(9x : X ) : :P ) :(8x : X ) : P
Would you expe
t the reverse impli
ation to be derivable?
4.13. Show that the formulas (8x : X ) : :P and :(9x : X ) : P are equivalent
by deriving two fun
tions mapping ea
h into the other.
4.14. Derive an obje
t of type
(8x : X ) : (A ) B ) ) ((9x : X ) : A ) (9x : X ) : B )
What is this formula in the
ase that A and B are independent of the
variable x?
4.15. Derive an obje
t of type
(9y : Y ) : (8x : X ) : P ) (8x : X ) : (9y : Y ) : P
where in general P will
ontain x and y free. Under what
ir
umstan
es
an you derive an obje
t of the
onverse type?
(8x : X ) : (9y : Y ) : P ) (9y : Y ) : (8x : X ) : P
Can you give a simpler reading of the formula in
ases when P does not
ontain both x and y free?
96 CHAPTER 4. INTRODUCTION TO TYPE THEORY
4.7.1 Booleans
The system we are building here is one in whi
h propositions, or formulas,
are identied with types, and vi
e versa. Amongst these propositions are
the propositions ?, or \false", >, or \true" (whi
h we introdu
e below),
and
ombinations of the propositions using logi
al
onne
tives su
h as ^
and _. Ea
h of these propositions is a type: the
olle
tion of proofs of that
proposition.
Consider the
ase in whi
h we want to return one of two results
ondi-
tional on some property. We need, informally, to be able to ask the question
and re
eive the answer Yes or the answer No, so we need a parti
ular type
whi
h
ontains these two values. We
all this the type bool of boolean val-
ues and for the sake of tradition
all the two values T rue and F alse. The
type bool is simply a nite type
ontaining two values. The r^ole of the type
is
omputational: we
an build the expression if : : : then : : : else : : :
(where the
ondition is a boolean expression) in our language, allowing
ase
swit
hes in
omputations. The value of if b then e else f is e if b evaluates
to T rue, otherwise it is f .
To re
ap, T rue and F alse are values of the type bool, whilst ? and >
are the false and true propositions. Readers may be familiar with similar
distin
tions between boolean values and propositions from program veri-
ation. Languages su
h as Pas
al
ontain a boolean type | if we reason
about these programs we use an external logi
al language to
onstru
t state-
ments about the language, we are not
onstru
ting expressions of type bool
whi
h are expressions of the language.
We should also note that these denitions form a template for the de-
nition of any type with a nite set of members.
4.7. BASE TYPES 97
and the
omputation rules
hoose the appropriate
ase from the
hoi
e.
Computation Rules for Nn
asesn 1n
1 : : :
n !
1
asesn 2n
1 : : :
n !
2
:::
asesn nn
1 : : :
n !
n
Exer
ises
4.18. Explain how to dene the equality fun
tion
equaln : Nn ) Nn ) bool
4.19. The su
essor and prede
essor of mn are (m + 1)n and (m 1)n ,
ex
ept for the prede
essor of 0n , whi
h is nn and the su
essor of nn whi
h
is 0n . Give formal denitions of the fun
tions su
n and predn of type
Nn ) Nn and prove that for all 0n ; : : : ; nn
su
(pred mn ) !
! mn
4.7.3 > and ?
If we have a one element type, the rules may be spe
ialised. We express
the results in a slightly dierent syntax, repla
ing N1 by >, 11 by T riv and
Cases1 by
ase. We obtain.
Formation Rule for >
The one element type, >, has a logi
al interpretation. Just as the false
proposition `falsity' (whi
h should not be
onfused with the obje
t F alse
of type bool) was represented by the empty type, so the true proposition is
represented by the one element type. Why one element? The intuition is
that the proposition is valid for obvious reasons, so there is only one trivial
proof T riv of it.
The rules
an also be interpreted in the
ase that n = 0, yielding the
rules for ?.
Exer
ise
4.20. Show that in the
ase n = 0 the rules for Nn redu
e to those for ?.
all the fa
torials of natural numbers are stri
tly positive. This assertion
takes the form
(8n : N )(fa
n > 0) df (8n : N )C (n)
What do we have to do to prove this? First of all we show that C (0) is
valid, that is we supply some
with
: C (0)
and then we show that C (n + 1) is valid, assuming that C (n) is. In this
ase we supply some
f 0 : \C (n) ) C (n + 1)"
In fa
t, the f 0
an be slightly more general,
f : \N ) C (n) ) C (n + 1)"
Note that we have en
losed the types in inverted
ommas | they are not
part of our system. We
an make them so, using the dependent type
on-
stru
tor:
f : (8n : N )(C (n) ) C (su
n))
Given these we produ
e the proof:
Elimination Rule for N (General Case)
n : N
: C [0=x℄ f : (8n : N ) : (C [n=x℄ ) C [su
n=x℄)
(NE )
prim n
f : C [n=x℄
Again, if we dis
harge the assumption n : N , we have
: C [0=x℄ f : (8n : N ) : (C [n=x℄ ) C [su
n=x℄)
nN : (prim n
f ) : (8n : N ) : C [n=x℄
whi
h is the familiar proof of the universal statement.
The
omputation rule is exa
tly the same in the two
ases. Thinking
of a
omputation of a re
ursive fun
tion we inspe
t the argument and then
unfold the denition a
ording to whether we are at the base
ase or not.
Computation Rules for N
prim 0
f !
prim (su
n)
f ! f n (prim n
f )
What do the rules mean in the logi
al
ase? They tell us how to build a
proof for any parti
ular natural number that we might supply. This is, of
102 CHAPTER 4. INTRODUCTION TO TYPE THEORY
ourse, how we justify the rule in the rst pla
e. Using C (k) for C [k=x℄, we
argue that C (2) is valid thus: \C (0) is valid outright, and by the indu
tive
ase for n = 0, C (0) ) C (1) and applying modus ponens, we have C (1). In
a similar way, we have C (1) ) C (2), and so we
an get C (2)."
This rule is one of the high points of type theory. Intuitively, we
an
appre
iate that there is an aÆnity between the rules for primitive re
ursion
and mathemati
al indu
tion. Fun
tions introdu
ed by primitive re
ursion
have their properties proved by mathemati
al indu
tion. What is so elegant
here, with our identi
ation of propositions and types, is that they are
exa
tly the same rule.
Let us
onsider some examples. The su
essor fun
tion is dened to be
(x : N )(su
x)
For the purposes of illustration, without re
ommeding this as an eÆ
ient
algorithm, we now examine the behaviour of a su
essor fun
tion dened
by primitive re
ursion:
addone 0 = 1
addone (n + 1) = (addone n) + 1
whi
h is formalised thus:
addone df xN : (prim x (su
0) f )
where
f df nN : (yN : (su
y))
What happens when we apply addone to the formal representative of 2,
that is su
(su
0)?
((x : N )(prim x(su
0)f )) (su
(su
0))
! (prim (su
(su
0)) (su
0) f )
! f (su
0)(prim (su
0) (su
0) f )
((n : N )(y : y)(su
y)) (su
0) (prim (su
0) (su
0) f )
! su
(prim (su
0) (su
0) f )
By a similar pro
ess we see that
prim (su
0) (su
0) f !
! (su
(su
0))
and so nally we see that
addone(su
(su
0)) !
! (su
(su
(su
0)))
4.8. THE NATURAL NUMBERS 103
be higher order, and here we give a version of the A
kermann fun
tion. The
two argument version of the fun
tion is given by the re
ursion equations
a
k 0 n = n + 1
a
k (m + 1) 0 = 1
a
k (m + 1) (n + 1) = a
k m (a
k (m + 1) n)
We
an take a higher-order view of this, dening the fun
tions thus:
a
k 0 = su
a
k (m + 1) = iter (a
k m)
Where the fun
tion iter, of type
(N ) N ) ) (N ) N )
iterates its argument, having the denition
iter f 0 = 1
iter f (n + 1) = f (iter f n)
This fun
tion is given by the term
f(N )N ) : nN : prim n 1 (p : q : (f q))
whi
h we shall
all iter, and the A
kermann fun
tion itself will be given by
nN : (prim n su
p : g : (iter g))
There is a limit to the expressibility of primitive re
ursion, even at higher or-
ders. All fun
tions dened by primitive re
ursion are total, and so there are
intuitively
omputable fun
tions whi
h are not primitive re
ursive. Among
these are the fun
tions whi
h
ode an interpreter for the pro
ess of
omputa-
tion of primitive re
ursive fun
tions. We return to the issue of expressibility
below.
We are not in a position to give any non-trivial examples of proof by
indu
tion as we still have not dened any predi
ates whi
h
ontain free
variables, a situation whi
h we remedy in due
ourse (in se
tion 4.10, in
fa
t).
Exer
ises
4.21. Dene the equality fun
tion
equalN : N ) N ) bool
4.9. WELL-FOUNDED TYPES | TREES 105
Miranda type denitions for booleans and natural numbers might be given
by
bool ::= True | False
nat ::= Zero | Su
nat
The example of a general algebrai
type whi
h we shall take here is a type
of numeri
al trees, dened by
tree ::= Null |
Bnode nat tree tree
As we mentioned above, a
ompanying a denition like this we have the
two prin
iples of proof by stru
tural indu
tion and denition by primitive
re
ursion.
Denition 4.2 Stru
tural indu
tion states that in order to prove P(t)
for every tree, t, it is suÆ
ient to prove it outright for Null,
P(Null)
and to prove
P(Bnode n u v)
assuming P(u) and P(v), that is assuming the validity of the result for the
immediate prede
essors u and v of the node (Bnode n u v).
Compare this with the prin
iple of indu
tion over the natural numbers;
we prove the result outright at 0, and prove it at (n + 1) assuming it is
valid at the (immediate) prede
essor n.
Denition 4.3 Primitive re
ursion is a prin
iple of denition for fun
-
tions. In order to dene a (total) fun
tion
f : tree -> P
we need only supply a starting value,
a : P
whi
h will be the value of f Null and a means of dening
f (Bnode n u v)
in terms of the previous values (f u) and (f v), the subtrees u and v and
the entry at the node, n. We will represent this as a fun
tion
4.9. WELL-FOUNDED TYPES | TREES 107
so that
f (Bnode n u v) = F n u v (f u) (f v)
and F of type
(n:nat)->(u:tree)->(v:tree)->P(u)-P(v)->P(Bnode n u v)
equal to n and the trees u and v are themselves ordered. The tree Null is
ordered. Dene a fun
tion
ordered : tree ) bool
whi
h returns T rue if and only if its argument is ordered. You
an assume
that the fun
tion
leq : N ) N ) bool
represents the ordering relation over N . How might you dene this by
primitive re
ursion?
4.28. Dene fun
tions insert and delete of type
N ) tree ) tree
whi
h, respe
tively, insert an obje
t into an ordered tree, preserving the
order of the tree and delete an obje
t, if present, from the tree, preserving
the ordered property. (If in either
ase the tree argument is not ordered, it
is not spe
ied how the fun
tion should behave.)
4.10 Equality
We have introdu
ed a number of type
onstru
tors or logi
al operations
whi
h
an bind variables whi
h are free in formulas, but as yet we have
no formulas
ontaining free variables; we have no primitive predi
ates, in
other words. We remedy that de
ien
y here, introdu
ing the equality
proposition. To assert that
`a and b are equal elements of the type A'
we write either
a =A b
or to remind us for
ibly that this is a proposition or type of the system we
sometimes use Martin-Lof's notation of
I (A; a; b)
instead of using the (often overused) equality symbol `='.
The I types have a drasti
ee
t on the behaviour of the system, both
formally and intuitively. We shall see why as we introdu
e the type and
look at examples whi
h use it.
110 CHAPTER 4. INTRODUCTION TO TYPE THEORY
We now introdu
e the rules governing the I-proposition. First the for-
mation rule. If a and b are both elements of the type A, then I (A; a; b) is
a type.
Formation Rule for I
A is a type a : A b : A
(IF )
I (A; a; b) is a type
This is dierent from the type (or formula) formation rules we have seen
so far. These take the form
: : : is a type : : : is a type
(: : : F )
: : : is a type
whi
h means that with these rules alone, we
an say what are the formu-
las or types of the system independently of whi
h elements o
upy those
types. The rule of I formation breaks this rule, sin
e a ne
essary
ondition
for I (A; a; b) to be a type is that a : A. This means that the rules generat-
ing the formulas are inextri
ably mixed up with the rules for derivations,
whi
h explains our de
ision not to express the syntax of formulas (or types)
separately.
Now, the presen
e of an element in the type I (A; a; b) will indi
ate that
the obje
ts a and b are taken to be equal. When
an we
on
lude that?
The obje
t a is equivalent to itself, so
Introdu
tion Rule for I
a:A
(II )
r(a) : I (A; a; a)
We
an derive rules whi
h look stronger than this, a subje
t we defer
until the next se
tion.
What is the
ontent of this rule, and more to the point, what is the
stru
ture of r(a)? This obje
t has no internal stru
ture, and at rst sight
this seems to render it useless. However, its mere presen
e
an allow us
to do things whi
h would not be possible without it | we shall amplify
this after we have
onsidered the elimination and
omputation rules, and
espe
ially in our
ase studies.
The essen
e of equality is that
equals
an be substituted for equals
and this is known as Leibnitz's law, after the logi
ian who
oined it. Sup-
pose that we have some proof p of a proposition P involving a, and also
that we know that
: I (A; a; b). We should be able to infer the proposition
4.10. EQUALITY 111
4.10.2 Inequalities
Nothing we have spe
ied in the system so far prevents there being a single
element at ea
h type. In order to make the system non-trivial we
an add
an axiom to the ee
t that
ax : :(T rue =bool F alse)
so that T rue and F alse are distin
t. This is suÆ
ient to imply the non-
triviality of other types; we
an show that 0 is not the su
essor of any
natural number using a denition by primitive re
ursion of the fun
tion
f0 df T rue
f (n + 1) df F alse
This proof follows a further dis
ussion of
onvertibility and equality in the
next se
tion.
In se
tion 5.9 below we shall see a more dire
t means of dening dependent
types.
From programming, an interesting example is the prede
essor fun
tion
over the natural numbers. Only the positive natural numbers, (n +1), have
a prede
essor, n. In traditional programming languages, we usually give
the prede
essor an arbitrary value, like 0, at 0. In our language we
an
represent its type thus:
(8x : N ) : ((x 6=N 0) ) N )
where we use x 6=N 0 as an abbreviation for :(x =N 0).
To dene the fun
tion we need to nd an element of
(x 6=N 0) ) N
for ea
h x in N . We do this by primitive re
ursion, and so rst we have to
nd an element of
(0 6=N 0) ) N
Now, we have r(0) : (0 =N 0). Suppose we have z : (0 6=N 0). The appli
a-
tion of z to r(0) is in ?, sin
e 0 6=N 0 is an abbreviation of
(0 =N 0) ) ?
From this we
an
onstru
t abortN (z r(0)) : N , giving the element in the
ase of 0. This trivial element simply re
e
ts the fa
t that at zero we have
no true prede
essor.
In the indu
tion step, we have to dene the prede
essor of the element
(n + 1) from the prede
essor of n and n itself. We simply
hoose the
latter. Putting these together into a formal dedu
tion and writing C for
(x 6=N 0) ) N , we have rst for the two
ases
r(0):(0 =N 0) [z :(0 6=N 0)℄1
() E )
(z r(0)): ?
(?E )
abortN (z r(0)): N
() I )1
z : abortN (z r(0)) : C [0=x℄
and
[n : N ℄1
() I )
q : n : C [su
n=x℄ () I )
p : q : n : (C [n=x℄ ) C [su
n=x℄) () I )1
n : p : q : n : (8n : N ) : (C [n=x℄ ) C [su
n=x℄)
116 CHAPTER 4. INTRODUCTION TO TYPE THEORY
4.11 Convertibility
In this se
tion we examine the rules of
omputation together with the re-
lation of equivalen
e or
onvertibility generated by them.
The rules of our system have two quite distin
t forms and purposes. The
formation, introdu
tion and elimination rules des
ribe how derivations of
judgements are
onstru
ted. If we are simply interested in nding out from
the logi
al point of view whi
h propositions have proofs, or are inhabited ,
then this
ategory of rules would seem to suÆ
e. This would be the
ase if
we were to omit the equality rules, but in the full system the derivability of
equality propositions is
losely linked with the
omputability rules through
the
onvertibility relation and the rules of substitution.
On the other hand, if we read the rules as those of a programming
language, then the rst three kinds of rule express only the syntax of the
language, spe
ifying as they do whi
h expressions have whi
h type. In
programming the pro
ess of exe
ution or evaluation is
entral, and it is this
that the rules express. We might ask what it is in logi
that
orresponds to
evaluation? It is the idea of simpli
ation of proof obje
ts. For example,
suppose we
hoose the following (partial) proof for A. `Given proofs a : A
and b : B , build the proof (a; b) of A ^ B , then
onstru
t a proof of A by
taking the rst
omponent'. This is the proof fst (a; b) and the proof obje
t
we have
onstru
ted
an be redu
ed simply to the obje
t a.
Note that this is more restri
tive than the denition of redu
tion we
had earlier, sin
e the redu
tion
an take pla
e within a variable-binding
operator like x : : : only when the expression redu
ed has nothing bound
by the lambda { in other words it does not
ontain x free. This restri
tion
is reasonable for the sorts of reasons we dis
ussed in
hapter 2, and makes
the redu
tion relation more amenable to analysis.
e e ! ! en f
0
A $
$ B A is a type (S ) A $
$ B p : A (S )
B is a type 3
p:B 4
(In writing these rules we have used our informal notation for substitution.
The reader may be happier to repla
e B (a) by B [a=x℄ and so on.)
4.11. CONVERTIBILITY 119
There are two
orresponding rules whi
h permit substitution for a free
variable. They
an be derived from the rules above.
[x : A℄ [x : A℄
.. ..
. .
a : A B is a type a:A b:B
B [a=x℄ is a type (S5 ) b[a=x℄: B [a=x℄ (S6 )
There is one other point we should emphasise about substitution and as-
sumptions. If we make a substitution of a, say, for a free variable x in
a derivation, then we should substitute a for any o
urren
e of x in the
assumptions other than in the assumption on x itself.
On the basis of the substitution rules we
an give a strengthened version
of the rule of equality introdu
tion. From a : A we
an derive I (A; a; a). If
a$ $ b then we
an substitute b for the se
ond o
urren
e of a in I (A; a; a),
giving I (A; a; b). We write this
Introdu
tion Rule for I
a $
$ b a : A b : A (II 0 )
r(a): I (A; a; b)
This makes plain that inter
onvertible expressions are not only equal a
-
ording to a relation of
onvertibility external to the system, but also
an
be proved equal (by the trivial proof obje
t r(a)).
With this strengthening of the equality proposition we
an reason about
the
omputational behaviour of expressions inside the system. We give an
example in the next subse
tion.
By the above, we have for all n, the type ((su
n) 6=N 0) is inhabited, by
tsn , say. Then,
pred (su
n) tsn : N
How does this behave under redu
tion?
pred (su
n) tsn : N (n : prim n f g) (su
n) tsn
! (prim (su
n) f g) tsn
! g n (prim n f g) tsn
(n : p : q : n) n (prim n f g) tsn
! (p : q : n) (prim n f g) tsn
! (q : n) tsn
! n
If we know that n and m have equal su
essors, then by the above
al
u-
lation and the substitutivity of equality, we have the equality of m and n
themselves.
To re
ap, we have shown that the su
essor fun
tion is 1-1 and that zero
is not a su
essor. These are standard properties of the natural numbers
whi
h we have proved using primitive re
ursion over the natural numbers,
together with the fa
t that the two booleans are distin
t.
This short dis
ussion of
onversion
ompletes our exposition of the
ore
system of type theory, together with some small examples of the system in
use. In the
hapter whi
h follows we will step ba
k from the system and
survey some alternative formulations of rules; look at some of the properties
of the system; examine the various identity relations in the theory, and so
on.
Exer
ises
4.31. Show that
x : ((y : y)x) ! x : x
but argue that we
annot generate x : ((y : y)x) by substituting (y : y)x
for z in x : z .
4.32. Show that the substitution rules whi
h follow are derivable from the
other rules of the system.
[x : A℄ [x : A℄
.. ..
. .
a : A B is a type a:A b:B
B [a=x℄ is a type b[a=x℄: B [a=x℄
124 CHAPTER 4. INTRODUCTION TO TYPE THEORY
The last
hapter was taken up with the introdu
tion of the system of type
theory T T0. It is a
ompli
ated system, with many of its aspe
ts deserving
of further study | this we do here.
As type theory is a formal system, it is amenable to study as an obje
t
in its own right. In se
tion 5.4 we show that from some derivations, like
a : A we
an dedu
e others, like A is a type. Following that, we show that
the derivable types of obje
ts are unique, and that the substitution rules
an be derived.
An important aspe
t of type theory is its
omputational behaviour, and
we study this for two systems related to T T0. We introdu
ed the basi
questions in our earlier introdu
tion to the -
al
ulus | here we ask them
of the system of type theory. First we give (in se
tion 5.5) a strong nor-
malisation result for the system T T0, whi
h unfortunately fails to have the
Chur
h-Rosser property. We then present T T0
whi
h was rst introdu
ed
by Martin-Lof in [ML75b℄. This diers from T T0 in the way in whi
h ab-
stra
tion is performed. After explaining the abstra
tion me
hanism and
showing that T T0
is an extension of T T0, we prove a normalisation theo-
rem for it. From the proof we obtain a number of important
orollaries,
in
luding the Chur
h-Rosser property for T T0
and the de
idability of ` $ $'
and of judgements in general. It is interesting to note that the pre
ise form
of -abstra
tion in T T0
is very
lose to the way in whi
h it is performed in
modern `
ompiled' implementations of fun
tional programming languages,
[Pey87℄.
We begin the
hapter by looking at some more te
hni
al aspe
ts of the
system whi
h merit more attention than they re
eived in the introdu
tion.
These in
lude the pre
ise role played by assumptions and ways in whi
h
terms
an be made more readable by naming and abbreviation. Naming
125
126 CHAPTER 5. EXPLORING TYPE THEORY
5.1 Assumptions
The derivations we
onstru
t using the rules of type theory depend in gen-
eral upon
olle
tions of assumptions. In this se
tion we look at the pre
ise
form that these
olle
tions take, together with
onsisten
y
riteria that
they should obey, and re-examine the rules and tighten up their statement
in some
ases.
A useful exer
ise for anyone interested in a formal system is to make an
implementation of it. Many of the issues dis
ussed here be
ame apparent
to the author while writing an implementation of type theory in the fun
-
5.1. ASSUMPTIONS 127
The rules above may best be understood by realizing that the linear
ordering of the assumptions in a valid
ontext is simply an (arbitrary) ex-
tension of the partial ordering on the assumptions indu
ed by their position
in a derivation tree
onstru
ted a
ording to the restri
tions we outlined
above, and whi
h we summarise now.
We may only dis
harge an assumption x : A if the variable x appears
free in the (types of) no other assumptions.
5.2.1 Naming
The pure system, just like the pure -
al
ulus, is a
al
ulus of expressions
without names. In using a system like this, we need some primitive no-
tion of naming expressions, both to make them more readable and more
abbreviated. We say, simply, that
name df expression
when we want to use the name name as a shorthand for the expression
expression, the two being treated as identi
al. We
all df the deni-
tional equality symbol. We do not permit re
ursive namings, or the use
of a name before its denition, thereby avoiding indire
t mutual re
ursions;
we just require a shorthand.
To make sum types more readable we allow the renaming of the inje
tion
fun
tions inl and inr, so we might say
numOrBool df num N + boo bool
with the intention that obje
ts of this type look like num n and boo b where
n : N and b : bool. We
an extend this notation to n-ary sums, if we represent
them in some standard form { a left asso
iated form, say. We will also use
the n-tuple notation for iterated produ
ts when this is appropriate.
Again, disallowing re
ursive denitions, we shall sometimes write
f x df e
instead of a denition
f df xA : e
An elegant form of denition in Miranda uses pattern mat
hing. For a
simple
ase analysis over the type numOrBool we use the operator
ases,
with
df p :
ases p g h : (A _ B ) ) C
if g : A ) C and h : B ) C . Suppose that
g n df e
h b df f
then we
an write the denition of
dire
tly thus:
(num n) df e
(boo b) df f
132 CHAPTER 5. EXPLORING TYPE THEORY
An example is provided by
toNum (num n) df n
toNum (boo b) df if b then 1 else 0
whi
h is shorthand for the denition
toNum df p : (
ases p (n : n) (b : if b then 1 else 0 ))
We
an allow
onstrained forms of re
ursion, too, as long as they
onform
to the re
ursion operation over the type in question. Over the natural
numbers, we allow denitions like
fa
0 df 1
fa
(su
n) df mult (su
n) (fa
n)
In the rst
lause we permit no re
ursive
all. In the se
ond we
an
all
only fa
n and n itself. This
orresponds to the formal denition
fa
df n : (prim n 1 (p; q : (mult (su
p) q)))
Of
ourse, in all these abbreviations, we assume that the derivations of
the hypotheses of the appropriate rule, like (NE ) here, have been de-
rived. In this
ase, we assume that we
an already derive 1 : N and that
(mult (su
p) q): N assuming that p; q : N .
We will look at a system based on naming of abstra
tions in se
tion
5.5.3 below.
5.2.2 Abbreviations
There are various pla
es in the system where we
an abbreviate derivations
without problems. In any situation where the same judgement forms more
than one hypothesis of a rule we may supply a single derivation of that
judgement. Examples are
A is a type A is a type a:A a:A
(^F ) (^I )
A ^ A is a type (a; a): A ^ A
and the
ommon
ase
A is a type a : A a : A
(IF )
I (A; a; a) is a type
5.3. REVISING THE RULES 133
5.3.2 Generalising _
One reason for presenting the rule for _ as we did in the previous se
tion
is that in this form it naturally suggests a generalisation. The type C
an
be a type family, dependent upon a variable z of type A _ B . Stated in this
form, we have
[x : A℄ [y : B ℄
.. ..
. .
p :(A _ B ) u : C [inl x=z ℄ v : C [inr y=z ℄
(_E 00 )
v
ases00x;y p u v : C [p=z ℄
In the se
ond hypothesis we have an obje
t x of type A, from whi
h we
form the obje
t inl x of type A _ B ; this is substituted for the variable
z in the formula C as this
ase
overs those elements from the left hand
summand. In the third hypothesis we have y : B , giving inr y : A _ B .
In the result, the obje
t P of type A _ B is substituted for the variable z .
The rule of
omputation is exa
tly the same as the rule for v
ases0 . The
operator v
ases00x;y binds o
urren
es of the variables x and y.
We
an, in fa
t, give a version of this rule in whi
h the operator is
not binding, but it involves our using the quantiers; this is the reason we
deferred its introdu
tion originally. It is
p :(A _ B ) q :(8x : A) : C [inl x=z ℄ r :(8y : B ) : C [inr y=z ℄
(_E y )
asesy p q r : C [p=z ℄
136 CHAPTER 5. EXPLORING TYPE THEORY
The
omputation rule for
asesy is the same as for
ases | a generalisation
of type need not alter the dynami
behaviour of an operator.
How is this generalisation useful? The operator
ases
an be seen as
a way of
ombining fun
tions f and g with domains A and B and
om-
mon
odomain C into a single fun
tion on the sum domain A _ B . The
generalised operator will do the same for dependent fun
tions for whi
h
the type of the result depends upon the value of the input. The families
whi
h are the result type of the dependent fun
tions must t together in
the appropriate way: we ensure this by asking that ea
h is a spe
ialisation
to a family over A, i.e. C [inl x=z ℄, or over B (C [inr y=z ℄) of a family C
over A _ B .
From the logi
al point of view, we have a way of lifting proofs of universal
results over A and B separately into universal results over A _ B . We might,
for example,
hoose to represent the integers by the sum N _ N , or using
a more suggestive notation
integers df poszro N + neg N
(We think of neg n as representing (n + 1).) We would then be able to
prove results for the integers by means of twin indu
tions over the non-
negative and the negative integers. If we dene the fa
torial of an integer
by
fa
0 df 1
fa
(su
n) df mult (su
n) (fa
n)
fa
1 df 1
fa
( (su
n)) df mult (su
n) (fa
( n))
a proof that for all integers p, fa
p > 0 would take the form suggested
above.
Exer
ises
5.8. Expand out the denition of fa
torial given above, and using the
expanded denition give a proof that fa
p is positive for all integers p.
5.9. Give a denition of subtra
tion over the integers, and prove for all a
and b that
(a + b) b = a
as required. We therefore dene
Fst df p : Casesx;y p x
The term Snd is more problemati
, as the type of its result depends upon
the value of the rst
omponent of its argument. It
an be shown that Snd
is not derivable from (9E 0 ), as a
onsequen
e of the
hara
terisation of the
various existential elimination rules by Swaen, examined in se
tion 8.1.3.
Obviously, if Fst is derivable from (9E 0 ) then it is derivable from the
stronger (9E ). We now show that Snd is also derivable from (9E ). To do
this we need to be able to
ast the judgement
y :B
in the form
: C [(x; y)=z ℄.
an be y, but we need to
ast B as a formula
dependent on the pair (x; y) and not simply on the variable x. The way
out is provided by Fst , and we write B in the form
B [(Fst (x; y))=x℄
so that to meet the rule, we have C df B [(Fst z )=x℄, giving
C [(x; y)=z ℄ B [(Fst z )=x℄[(x; y)=z ℄ B [(Fst (x; y))=x℄
We then have,
Casesx;y p y : C [p=z ℄ B [(Fst p)=z ℄
and
Casesx;y (a; b) y ! y[a=x; b=y℄ b
whi
h justies the denition
Snd df p : Casesx;y p y
In the opposite dire
tion, we now show that every instan
e of the rule
(9E )
an be derived from (9E10 ) , introdu
ing Fst and (9E20 ) , introdu
ing
Snd . Suppose that we have a derivation
[x : A; y : B ℄
..
.
: C [(x; y)=z ℄
and p :(9x : A) : B . By the rules (9E10 ) and (9E20 ) we have
Fst p : A Snd p : B [Fst p=x℄
5.4. DERIVABILITY 139
Exer
ise
5.10. Using the rule (9E ) amongst others, give a proof of the axiom of
hoi
e:
(8x : A) : (9y : B ) : C (x; y) ) (9f :(A ) B )) : (8x : A) : C (x; (f x))
Can you use (9E 0 ) instead of (9E )?
5.4 Derivability
In this se
tion we take a general look at derivability in variants of the
system T T0. These results will be proved by indu
tion over derivations.
Before looking at parti
ular results, it is worth noting a parti
ular property
of the system of type theory we have adopted.
In the system T T0 any parti
ular judgement, like
fst (a; b): A
an be derived in two dierent ways. First we might use the rule whi
h
introdu
es that parti
ular pie
e of syntax, in this
ase (^E1 ). Alternatively,
we might use one of the substitution rules, su
h as (S2 ),to derive the same
result:
a $
$ b fst (a; a): A (S )
fst (a; b): A 2
The nal
ase we should
onsider is one in whi
h the last rule applied
is a substitution. Consider rst the
ase of (S2 ):
$
$ a p(
): B (
) (S )
p(a): B (a) 2
Theorem 5.5 Using the modied system of rules outlined above, given a
derivation of a : A we
an
onstru
t a derivation of A is a type.
Proof: The proof pro
eeds by indu
tion over the derivation of a : A and
follows the outline sket
hed above. 2
We managed to prove the property above by adding suÆ
ient type hy-
potheses to the rules so that ea
h element derivation
ontains embedded
derivations of the typehood of its various type expressions. In a pra
ti
al
system based on type theory, we would expe
t to separate these
on
erns
as mu
h as possible; on
e a type had been derived, we
ould
onstru
t
elements of that type without an expli
it re-derivation of the type.
Exer
ise
5.11. Complete the proof of theorem 5.5 above.
The inje
tion operators inl and inr do not have unique ranges. In-
deed,
inl 0
is a member of (N _ A) for any type A. We remedy this by labelling
the inje
tion operators with their range type, so that
inl(N _A) 0 : (N _ A)
We
an now see the unlabelled operators as shorthand for these op-
erators.
Theorem 5.6 In the theory T T0, if from a
onsistent
olle
tion of assump-
tions we
an derive the judgements a : A and a : B then A $
$ B.
Proof: The proof is performed by indu
tion over the derivation of a : A.
Consider rst the
ase of a variable x : A. Judgements of this sort will
be derived in one of two ways. First we may use the rule of assumptions,
in whi
h
ase there is only one
hoi
e of type A by the
onsisten
y of the
assumption set. Alternatively, we
an use the substitution rule (S4 ) or (S2 ).
In the former
ase, we have a derivation ending in
C $ $ A x : C (S )
x:A 4
5.5 Computation
Up to now we have devoted the major part of our exposition to the stati
properties of the system, in looking at how the various judgements are
derived. Whilst doing this we have introdu
ed
omputation rules whi
h are
of fundamental importan
e from the point of view of programming, sin
e
it is these rules whi
h dene the dynami
behaviour of the system; how the
programs are exe
uted, in other words. This se
tion looks at the rules from
a general perspe
tive.
The redu
tion rules are also used to generate a relation of
onversion,
whi
h is an equivalen
e relation;
onvertible obje
ts are seen as being the
same, and this allows the substitution of one for the other in any
ontext.
The issues of interest for redu
tion will be those we dis
ussed rst in
se
tions 2.3, 2.4 and 2.11. Does evaluation terminate (along all paths)? |
is the system (strongly) normalising? Can any two evaluation sequen
es be
extended to yield a
ommon expression | the Chur
h-Rosser Property?
This will be the
ase for the system T T0; in this se
tion we present
two variants for whi
h there are the results we require in the literature.
The rst, T T0, is one in whi
h redu
tion, ` ! ' is limited. This system
possesses normalisation properties, but has the drawba
k that it is not
Chur
h-Rosser.
The se
ond system,
alled T T0
, is based on a quite dierent way of
introdu
ing fun
tions, and types, though it is equivalent to T T0. Instead
of introdu
ing binding operators like , or type forming operators like 8
and so on, fun
tions are introdu
ed as
onstants, ea
h
onstant having a
orresponding redu
tion rule. Types are introdu
ed in a similar way. This
bears a striking resemblan
e not only to the top-level form of languages
like Miranda, but also in its details to the methods of -lifting [Joh85℄ and
super
ombinators [Hug83℄ used in fun
tional language implementations.
Exer
ise
5.14. In the untyped -
al
ulus, the natural number n is represented by
the fun
tion \apply n times", that is
f : x : f (f : : : f (f x) : : :)
| {z }
n
and addition is represented by the fun
tion
add df f : g : h : x : f h(g h x)
Show that add 2 3 redu
es to 5, but observe that the unrestri
ted -
redu
tion rule must be used to a
hieve the result.
Instan
es of this might be the term x : (x + y), and the term x : (II ), in
whi
h the redex (II ) is still visible.
An alternative is to make a fun
tion or
ombinator denition for the
term, saying
fx !e
but there is a problem with doing simply this, exemplied by the addition
example we saw above. Our denition would state
fx ! (x + y)
whi
h
ontains an unbound variable on the right-hand side. To make a
proper denition of a fun
tion we need to in
lude the variable y amongst
the arguments, and say
fyx ! (x + y)
The term x : (x + y) will now be repla
ed by the term (f y), as it is this
appli
ation whi
h represents the abstra
tion over x. In general, a fun
-
tion
onstant formed in this way needs to have as arguments not only the
abstra
ted variable, but also all the other variables free in the expression.
These variables
an be
alled the parameters of the denition. In order to
form the abstra
tion of interest, these parameters must then be passed to
the
onstant to form the required abstra
tion. In the
ase of the addition
fun
tion above, the abstra
tion over x is given by the term (f y) and not
the `bare'
onstant f .
For our se
ond example above, x : (II ), the denition is simpler, we
just have to write
x ! (II )
and form the term
. Note that in the
onstant there is no redex. This
method of
onstant denitions hides from view the redexes within the bod-
ies of fun
tions, only making them visible when the fun
tion is applied to
suÆ
iently many arguments.
Denition 5.9 Combinator abstra
tion. Suppose that the derivation
of the judgement
e:B
depends upon the assumptions
x1 : A1 ; : : : ; xk : Ak ; x : A
150 CHAPTER 5. EXPLORING TYPE THEORY
then we
an form a new fun
tion
onstant f whi
h will take arguments of
the types A1 ; : : : ; Ak (as `parameters') and A, giving a result of type B . In
other words, we
an introdu
e the term
fx1 : : : xk
of type (8x : A) : B . The
omputation rule for the term f is given by
fx1 : : : xk x !e
We
an give the type of the new
onstant f dire
tly:
(8x1 : A1 ) : : : : (8xk : Ak ) : (8x : A) : B
whi
h, when none of the dependen
ies are exhibited by B , redu
es to
A1 ) : : : ) Ak ) A ) B
The introdu
tion rule
an be written in a familiar form, thus
[x : A℄
..
.
e:B
(8I
)
f x1 : : : xk : (8x : A) : B
where the undis
harged assumptions of the derivation of the hypothesis are
x1 : A1 ; : : : ; xk : Ak , and the new
onstant to be introdu
ed is
alled f . The
omputation rule is stated above and the formation and elimination rules
for the type (8x : A) : B (and therefore for the impli
ation A ) B ) are as
before.
Let us
onsider a simple example. Assuming we have derived that A
and B are types then on the basis of the assumptions
x:A ; y :B
we
an derive
x:A
Suppose we wish to form the (informal) expression x : y : x, we rst have
to abstra
t over y. In doing this we produ
e a binary fun
tion
onstant, f ,
whi
h will take the free variable x as an argument as well as the variable y
whi
h is the variable bound by the denition
f xy !x
5.5. COMPUTATION 151
Martin-Lof's proof
arries | this was done originally to make the proof
lo
ally formalizable in the [type℄ theory itself ([ML75a℄, se
tion 2.1.4) |
the approa
h here is not so formal. The result is proved in su
h a way
that it has a number of important
orollaries for the system, in
luding
the fa
ts that normal forms are unique, and the Chur
h-Rosser property
for redu
tion holds. Martin-Lof observes that the Chur
h-Rosser property
does not have a dire
t
ombinatorial proof to his knowledge, this route
providing the only means of proof.
As we have seen a number of times up to now, the fa
t that in type
theory the types and the obje
ts are dened by a simultaneous indu
tion
means that we have to prove things in a dierent manner than in the typed
-
al
ulus. In se
tion 2.7 we were able rst to dene stability by indu
tion
over the types, and then to show all elements are stable. Here we work by
indu
tion over the derivations of
losed judgements
a:A A is a type
(a
losed judgement is one whi
h depends upon no assumptions, and so in
parti
ular one for whi
h a and A are
losed). The indu
tion denes
A0 , a
losed normal form of A.
a0 , a
losed normal form of a so that a !
! a0 . a0 is also a member of
the set kAk.
kAk, the set of stable terms of type A, whi
h are members of the
type A0 . Our proof will show that ea
h
losed term is redu
ible to a
stable term, and these terms are
learly seen to be
losed and normal.
Be
ause assumptions in open derivations
an be dis
harged, we need also
to look at open judgements and derivations. For the open judgement a : A,
made in the
ontext
x1 : A1 ; : : : ; xn : An
we also dene fun
tions a0 and A0 , depending upon (meta-)variables
x0 ; : : : ; x0
1 n
whi
h range (respe
tively) over
losed normal terms of type
A0 ; : : : ; A0 (x0 ; : : : x0 )
1 n 1 n 1
It is important to note that these fun
tions are not dened by terms of
the system, in general: they are the operations whi
h assign
losed normal
forms depending upon the
losed normal forms whi
h are their parameters.
5.6. T T0C : NORMALISATION AND ITS COROLLARIES 155
with a similar denition for A0 . (Re
all that we use to mean `iden-
ti
al up to
hange of bound variable'). In the proof we say that a0 is
parametri
if it has this pair of properties.
Theorem 5.14 (Normalisation for T T0
) Every
losed term b of type
theory has a normal form b0 , and moreover if b $
$
then b0
0 .
Proof: As outlined above we use indu
tion over the length of derivations
of judgements. We must go through the
onstru
ts of the theory in turn.
We must also verify at ea
h step that the normal forms assigned to the two
sides of a
omputability rule like
fst (a; b) !a
are equal | this is used in proving that the normal forms given to
onvert-
ible terms are identi
al. We shall not
over the
ases of fun
tion spa
es or
onjun
tions as they are simply spe
ial
ases of universally and existentially
quantied types.
156 CHAPTER 5. EXPLORING TYPE THEORY
and the normal form for the expression (f f1 : : : fl ) is given by the
lause
for 8 elimination below.
If we restri
t ourselves to the simple
ase of no parameters ki , then f 0
is stable sin
e for any
nf a0 ,
f 0 a0 ! e[a0 =x℄ !
! e0 [a0 =x℄ (e[a=x℄)0
the equivalen
e holding by the parametri
ity property for e, whi
h we as-
sume by indu
tion. For the general
ase we argue in a similar way, also
invoking the
ase for 8 elimination below.
8 elimination gives an appli
ation. We dene (f a)0 to be the
losed
normal form to whi
h (f 0 a0 ) redu
es, whi
h exists by indu
tion and the
denition of
onvertibility for the universal type. The
omputation rule for
8 states that
fa ! e[a=x℄
now observe that
(f a)0 e0 [a0 =x℄ (e[a=x℄)0
The rst equivalen
e holds by the denition of
onvertibility for the -
abstra
tion and the se
ond by parametri
ity for e0 .
It is not hard to see that the denitions for 8-introdu
tion and elimina-
tion are parametri
.
Case 9
As for the universal type, we dene
((9x : A) : B )0 df (9x : A) : B
The stable terms of this type are pairs of stable terms, (a0 ; b0 ), with a : A0
and b0 : B 0 [a0 =x℄. The introdu
tion rules introdu
e a pair, we say that
(a; b)0 df (a0 ; b0)
The elimination rules (9E10 ) and (9E20 ) introdu
e the proje
tion operators.
If we have terms
Fst p Snd p
then p0 , the
nf of p, will be a pair (q0 ; r0 ). We therefore set
(Fst p)0 df q0 (Snd p)0 df r0
A pair of parametri
terms will be parametri
, as will be the
omponents of
a parametri
pair, so the two
onstru
ts preserve parametri
ity. This will
apply similarly in all other
ases of non-variable-binding
onstru
ts.
158 CHAPTER 5. EXPLORING TYPE THEORY
Finally we should
he
k that the normal forms of the two sides of the
omputation rules are identi
al. The rules state that
Fst (q; r) !q Snd (q; r) !r
By the denitions above
(Fst (q; r))0 (Fst (q0 ; r0 )) q0
as we require. A similar proof shows the result for Snd .
Case _
We dene
(A _ B )0 df (A0 _ B 0 )
and take the set of stable elements, k(A _ B )k to be (kAk _ kB k).
In introdu
ing an element of a disjun
tion we inje
t the obje
t into
either the left-hand or right-hand side. We dene
(inl a)0 df inl a0 (inr b)0 df inr b0
in eliminating an element by means of (_E 0 ) we use the v
ases0
onstru
t,
v
ases0x;y p u v. p0 will take the form of (inl q0 ) or (inr r0 ). In the rst
ase,
let
(
ases p u v)0 df u0[q0 =x℄
and in the se
ond
(
ases p u v)0 df v0 [r0 =x℄
The forms dened are stable by the parametri
ity property, whi
h is also
preserved by the denition. It is also easy to
he
k that the denitions
respe
t the
omputation rules for v
ases.
Case ?
The
nf of ?, ?0 is ? itself, and the set of stable terms is the empty
set. There are no introdu
tion rules for terms of type ?, but there is the
elimination rule. We write
(abortA p)0 df abortA0 p0
This
learly satises parametri
ity. As there is no introdu
tion rule, there
is no
omputation rule, and therefore no
ondition to
he
k. In fa
t this
ase is a spe
ial
ase of the nite types, whi
h follow.
5.6. T T0C : NORMALISATION AND ITS COROLLARIES 159
Case Nn
The
nf of Nn is Nn , and the stable terms are
1n; : : : ; nn
so that it is easy to see that for the introdu
tion rules we have
1n0 df 1n ; : : : ; nn 0 df nn
For the elimination rule we have the n-way
ase statement
asesn e
1 : : :
n
The
nf of e will be mn for some 1 m n. We dene
(
asesn mn
1 : : :
n )0 df
m 0
Again, parametri
ity is plain. The
omputation rule sele
ts the appropriate
ase a
ording to the value of the obje
t of type Nn | this is respe
ted by
the denition.
Note that this also
overs the spe
ial
ases of >, bool, and indeed ?.
Case N
N 0 is dened to be N , and the
olle
tion of stable terms is dened by
(meta-theoreti
al) indu
tion thus:
0 is a stable term.
(su
n) is a stable term if n is.
It should be
lear how we dene the
nfs for the introdu
tion rules:
00 df 0 (su
n)0 df (su
n0 )
In the
ase of elimination, we have terms of the form
prim m
f
The
nf m0 will be either 0 or (su
n) for some n. By an indu
tion over
the
nf we say that
(prim 0
f )0 df
0
(prim (su
n)
f )0 df y
where y is the
nf of the term given by the appli
ation
f 0 n (prim n
f )0
160 CHAPTER 5. EXPLORING TYPE THEORY
whi
h exists by the denition of the
nfs f 0 of fun
tional type together with
the fa
t that n and (prim n
f )0 are themselves
nfs. Parametri
ity and
respe
ting the
omputation rule follow.
We treat the type tree in an analogous way.
Case I
We say that
I (A; a; b)0 df I (A0 ; a0 ; b0 )
and that the set of stable terms
onsists of r(a0 ). The introdu
tion rule
introdu
es r, and we say
(r(a))0 df r(a0 )
In an elimination we form J (
; d) : C (a; b;
). The only normal form
an
have is r(a0 ), and we say
(J (r(a0 ); d))0 df d0
Sin
e in the
omputation rule, we redu
e J (r(a); d) to d, we see that this rule
is respe
ted by the denition of the
nf. Again, parametri
ity is preserved.
This exhausts all the
ases, and so we have shown all
losed normal terms
have a
losed normal form. We have also veried that for ea
h redu
tion
rule b !
, the
nfs are equal: b0
0 An indu
tion over the relation ` $ $'
is enough to show that if b $$
then b0
0 .
This
ompletes the proof of the normalisation theorem. 2
The normalisation result is important in itself, showing that all expres-
sions have a value, and in parti
ular that all expressions of ground type
have a printable value, but also the proof itself
an yield other results.
Corollary 5.15 There is a model of the system T T0
.
Proof: Using the proof of the theorem, types may be modelled by the
sets kAk, and
losed terms a by their (
losed) normal forms a0 , whi
h are
members of the sets kAk. 2
Corollary 5.16 If a and b are
losed normal terms whi
h are inter
onvert-
ible then they are identi
al.
Proof: Martin-Lof attributes this result to Peter Han
o
k. a and b redu
e
to a0 and b0 respe
tively, but as a and b are normal, a a0 and b b0 . Also,
we know from the proof that if a $ $ b then a0 b0 , whi
h gives the result
by the transitivity of `'. 2
5.6. T T0C : NORMALISATION AND ITS COROLLARIES 161
if they are identi
al up to
hange of bound variable after all the dened
terms, introdu
ed by means of the denitional equality ` df ', have been
expanded out. We simply treat identi
al expressions as identi
al | there
are no
ontexts in whi
h we wish to distinguish between two identi
al ex-
pressions.
As an aside, it is worth noting that although it is obvious what this
relation is, we have to do some work in a
omputer implementation to
ensure that we
an de
ide exa
tly when two expressions are identi
al.
5.7.2 Convertibility
Two expressions are
onvertible if the
omputation steps embodied in the
omputation rules for the system are suÆ
ient to bring them together.
Formally we build the relation ` $ $ ' by taking the re
exive, symmetri
transitive and substitutive
losure of the relation ` ! '. In other words, we
ask that, for all expressions a; b;
; : : : and variables x,
Computation If a ! b then a $
$ b.
Re
exivity a $
$ a.
Symmetry If a $ $ b then b $
$ a.
Transitivity If a $
$ b and b $
$
then a $
$
.
Substitutivity If a $
$ b and
$
$ d then a[
=x℄ $
$ b[d=x℄.
We saw in the last se
tion that two terms were
onvertible if and only if they
have the same normal form, this means that the relation of
onvertibility
is de
idable.
The denition of
onvertibility is external to the system | a $ $ b is
intended to embody the fa
t that the two expressions a and b denote the
same obje
t. In the light of the
hara
terisation above, we
an identify this
obje
t as the normal form of the expression, if we wish.
In se
tion 4.11 we introdu
ed the rules of substitution whi
h allow in-
ter
onvertible expressions to be substituted for ea
h other in derivations of
judgements. This emphasizes the fa
t that judgements are intended to be
about the obje
ts denoted by the expressions, rather than the expressions
themselves. We shall
ome ba
k to this important distin
tion below.
Be
ause `a $ $ b' is not a proposition of the system, we are unable to
build more
omplex assertions on the basis of it. To do this we turn to our
third relation, the identity predi
ate.
5.7. EQUALITIES AND IDENTITIES 165
Note that one
onsequen
e of the denition is that for
losed a,b if a $
$b
then
equalA a b $
$ T rue
but on the other hand the non-derivability of a $
$ b does not imply that
equalA a b $
$ F alse
Over whi
h types do we have an equality operation? We start our dis
ussion
with two denitions.
Denition 5.24 A predi
ate P (x1 ; : : : ; xk ) is formally de
idable if and
only if the following proposition is derivable
(8x1 : A1 ) : : : : (8xk : Ak ) : (P (x1 ; : : : ; xk ) _ :P (x1 ; : : : ; xk )) (5.1)
Denition 5.25 A predi
ate P (x1 ; : : : ; xk ) is representable if and only
if for some term r the following propositions are derivable
(8x1 : A1 ) : : : : (8xk : Ak ) : (5.2)
(r x1 : : : xk =bool T rue ) P (x1 ; : : : ; xk ))
(8x1 : A1 ) : : : : (8xk : Ak ) : (5.3)
(r x1 : : : xk =bool F alse ) :P (x1 ; : : : ; xk ))
Theorem 5.26 A predi
ate is representable if and only if it is formally
de
idable.
Proof: To prove that a representable predi
ate is de
idable, note rst that
using the axiom of bool elimination we
an derive
(8b : bool) : (b =bool T rue _ b =bool F alse)
(a proof of this appears in se
tion 4.10.1). By means of the propositions
5.2, 5.3, we
an derive the formula 5.1, as required.
To prove the
onverse, we need to take the derivation given by 5.1,
d : (8x1 : A1 ) : (8xk : Ak ) : (P (x1 ; : : : ; xk ) _ :P (x1 ; : : : ; xk ))
The term d is a fun
tion, whi
h we
ompose with the fun
tion dened over
a disjun
tion whi
h returns T rue over the rst disjun
t and F alse over the
se
ond. This fun
tion is given by the term
x : (
ases x (x : T rue) (x : F alse))
The resulting fun
tion will form a representation of the predi
ate. 2
5.7. EQUALITIES AND IDENTITIES 167
Corollary 5.27 A type A
arries an equality fun
tion if and only if the
equality over that type is formally de
idable.
Proof: The equality fun
tion is a representation of equality over the type.
The theorem therefore applies the result immediately. 2
Theorem 5.28 A ground type A
arries an equality fun
tion.
Proof: By the previous
orollary it is suÆ
ient to show that equality
over the type is formally de
idable. We
an prove by indu
tion over the
onstru
tion of ground types that equality is de
idable for them. Indeed,
we have given dire
t denitions of equality fun
tions in the exer
ises in the
previous
hapter. 2
Will equality over any other types be de
idable? It seems highly unlikely
that this is so. Two
losed terms of type N ) N
an be proved equal if
and only if they have the same normal form, but there is no way, internally
to type theory to
ompare normal forms. An extensional equality, to whi
h
we turn in the following se
tion, has other drawba
ks. From an extensional
de
idability predi
ate over a fun
tional type we are able to prove a result
like
((8x : N ) : f x =N 0) _ :((8x : N ) : f x =N 0)
whi
h is not in general a
eptable to the
onstru
tivist, breaking as it does
the
onstraint that properties be nitary.
For the fun
tional programmer (or the primary s
hool
hild) the interest
of su
h a proof is that two expressions whi
h are not prima fa
ie equivalent
in fa
t have the same meaning.
These proofs have in
ommon the fa
t that they would be
onsidered
trivial in the
ontext of type theory; they simply involve showing that two
expressions are
onvertible, and this is formalised outside the theory in the
onvertibility relation.
In order to extend the theory to embra
e this kind of equality reasoning,
we have radi
ally to modify the theory. The proof obje
ts of the equality
(I ) types will no longer have the trivial form r(a), but will need to re
e
t
the
hains of equalities as above. Also the proposition
(2 + 3) + (4 + 5) = 14
must be distinguished from the proposition
14 = 14
sin
e the latter is trivial, whilst the former re
e
ts three non-trivial
om-
putation steps; proof obje
ts of the two types will be
ompletely dierent.
The departure here is to
onsider proofs to be about linguisti
expres-
sions (su
h as (2 + 2)) rather than about mathemati
al obje
ts (like the
number 4). It would be interesting to see a
omplete development of a
theory analogous to type theory along the lines proposed here.
Denition 5.29 By indu
tion over the
onstru
tion of the type A we
an
dene the operator 'A embodying extensional equality over A. For-
mally, we have the derived rule
A is a type a : A b : A
(EEF )
(a 'A b) is a type
For base types N , Nn , bool, I (T; n; m) and so on we dene
a 'A b df I (A; a; b)
Omitting the type subs
ripts from now on, for fun
tion types we say
f 'g df (8x; y : A) : ((x ' y) ) (f x ' g y))
For produ
t types,
u'v df (fst u ' fst v) ^ (snd u ' snd v)
and similarly for disjun
tions.
For f and g of the universally quantied type
(8x : A) : B
the proposition
(8x; y : A) : ((x ' y) ) (f x ' g y))
is only well formed if the type family B is itself extensional, that is if x ' x0
then B $ $ B [x0 =x℄.
The relation `'' is a partial equivalen
e relation, in that
Lemma 5.30 The relation `'A ' has the following properties
Symmetry If f ' g is derivable, then so is g ' f .
' g and g ' h are derivable, then so is f ' h.
Transitivity If f
Semi-Re
exivity If f ' g is derivable, then so is f ' f .
Proof: The proof is by indu
tion over
onstru
tion of the type A. The
interesting
ase is that of the fun
tion type B ) C . We look at the various
properties in turn. First suppose that we have f ' g, that is
(8x; y : B ) : ((x ' y) ) (f x ' g y))
by the symmetry of `'' for the type C we have
(8x; y : B ) : ((x ' y) ) (g y ' f x))
5.8. DIFFERENT EQUALITIES 173
as required. Now suppose that we have f ' g and g ' h, and suppose that
x 'B y. By the rst proposition, we have
fx'gy
Sin
e x ' y, we have by symmetry and semi-re
exivity for B that y ' y,
so by g ' h we
an derive
gy 'hy
and nally by transitivity for ' over C we have
f x'hy
whi
h establishes transitivity. Semi-re
exivity is a
onsequen
e of symme-
try and transitivity. 2
Denition 5.31 If a is an open term with free variables x1 ; : : : ; xk then
its
losure is the term x1 ; : : : ; xk : a.
Denition 5.32 We
all a
losed term a of type A extensional if a 'A a.
If a is open, we
all it extensional if its
losure is.
Not all terms are extensional. Take the fun
tion
h df x : (x; r(x)) : (A ) (9x : A) : I (A; x; x))
Suppose that we take the type A to be N ) N , and
hoose two fun
tions
f and g of this type so that f ' g yet f $6$ g. The two addition fun
tions
of se
tion 4.11.2 will do for f and g. Now,
hf !
! (f; r(f ))
hg !
! (g; r(g))
These two values are not extensionally equal, as the obje
ts r(f ) and r(g)
are of dierent types, sin
e the family I (A; x; x) is not extensional.
We
ould leave the development here, with a denition of what it is for
two terms to be extensionally equal. Unfortunately, what we have devel-
oped thus far is not very useful | we have no way of using the dened
relation dire
tly, and instead we must expand out its denition to use it.
The equality relation I (A; a; b) is
hara
terised by its elimination rule, stat-
ing that a and b may be substituted for ea
h other in any
ontext. `'' is a
weaker relation than identity, and so we
annot expe
t to substitute exten-
sionally equal terms for ea
h other in all
ontexts. Instead, we prove that
substitution
an be performed safely in a large
lass of
ontexts.
174 CHAPTER 5. EXPLORING TYPE THEORY
5.9 Universes
The system T T0 makes a rigid distin
tion between types, su
h as N , A ^ B
and I (A; a; b), and the obje
ts whi
h inhabit them, 0, (a; b), r(a) and so
on. There are situations in whi
h this distin
tion
an usefully be blurred.
We may wish to make an obje
t depend upon a type parameter |
this is often
alled type polymorphism.
5.9. UNIVERSES 175
In other rules whi
h have premisses of the form A is a type, those premisses
are repla
ed by A : Un . We also add the following formation rule
Formation Rule for U
(UF )
Un : Un+1
The system of universes is not
umulative; ea
h type is a member of
exa
tly one universe, Uk say, rather than being a member of all the universes
Uk ; Uk+1 ; : : :.
We end this introdu
tion by remarking that the results of se
tion 5.6
arry over to T T with no modi
ation, so that
Theorem 5.36 T T is strongly normalising, has the Chur
h-Rosser prop-
erty, and both
onvertibility and the derivability of judgements of the form
a : A are de
idable.
Proof: Exa
tly as se
tion 5.6. 2
5.9.1 Type families
Be
ause the universes are types just like any other, we
an form new obje
ts
of these types. For example, we have
x : bool ? : U0 > : U0
(bool E )
if x then ? else > : U0
The term B df ( if tr then ? else > ) is a type family over the variable
x : bool, with the property that
B (T rue) ! ?
B (F alse) ! >
This gives a more dire
t denition of type family than that des
ribed in
se
tion 4.10.3 above.
Now we prove a theorem using the universe U0 to give a result we
annot
prove in T T0.
Theorem 5.37 In T T we
an derive :(T rue =bool F alse).
Proof: Suppose that we have p : T rue =bool F alse. Applying the fun
tion
x : ( if x then ? else > )
to the two sides, and redu
ing, we nd
p0 : ? =U0 >
5.9. UNIVERSES 177
A is a (small) type, and p is a proof that it has the property P (A). Suppose
we have dened P (A) to be
(A ) A) ^ (A ) A)
then an obje
t of the existential type will be a type A together with
p : (A ) A) ^ (A ) A)
that is a pair of fun
tions from A to itself. An obje
t of this type is equiv-
alent to an implementation of an abstra
t type, with signature (written in
Miranda notation)
abstype A
with f1 :: A -> A
f2 :: A -> A
where f1 and f2 are the rst and se
ond proje
tions of p, of
ourse.
We shall have more to say about quantied types in the following
hap-
ter, where we look at a series of examples.
5.9.4 Extensions
Why do we stop with a
hain of universes U0 ; U1; : : :, when there are natural
fun
tions whi
h
annot be dened in the system? The obvious one whi
h
springs to mind is
(n : N ) : Un
whi
h
learly inhabits none of the Un . To give this a type we need to
add the rst transnite universe U! , whi
h is itself a member of U!+1 ,
and so we
an iterate through the
onstru
tive ordinals. Whether this
extension is interesting, either proof theoreti
ally or from the point of view
of programming, is open to question, but the interested reader may wish
to
onsult [And65℄ for a similar transnite theory.
Another possible dire
tion is to distinguish between the types whi
h are
sets, su
h as N and tree, and the propositions, and to try to extend the
type theory with a type of propositions. This has itself been shown to be
in
onsistent in [Ja
89℄.
If one is prepared to limit the type forming operations, then systems
with a type of types
an be built
onsistently. The work of Girard on system
F [Gir80℄ and of Huet and Coquand [CH85℄ testies to this. More details
of these systems are given in se
tion 9.1.5 below.
5.10.1 Lists
A list is either empty, [ ℄, or
an be thought of as having a rst element,
or head, a and a remainder, or tail, x. The list with head a and tail x is
written
(a :: x)
180 CHAPTER 5. EXPLORING TYPE THEORY
y
y ?y?y su
y
g g R yR y
ons y?y
ons 1
?y
0 su
1
ons
ons 0
0 0 nil nil
Exer
ises
5.26. Using lre
dene the fun
tion map whi
h takes as arguments a fun
-
tion f and a list [a1 ; : : : an ℄, and returns the list [f a1 ; : : : f an ℄.
5.27. Dene the fun
tion segs of type [A℄ ) [[A℄℄ with the property that
segs [n1 ; : : : ; nm ℄ df [ [ ℄ ; [n1 ℄ ; [n1 ; n2 ℄ ; : : : ; [n1 ; : : : ; nm℄℄
5.28. Using segs or otherwise, dene the fun
tion sums of type [N ℄ ) [N ℄
with the property that
sums [n1 ; : : : ; nm ℄ df [0 ; n1 ; n1 + n2 ; : : : ; n1 + + nm ℄
What is a suitable value for the empty list [ ℄?
5.29. Formulate what it means for one list to be a sublist of another, and
dene the fun
tion
sublists : [A℄ ) [[A℄℄
whi
h returns the list of all sublists of a list. How would you remove
dupli
ate entries from the list if ne
essary?
5.30. How is the equality operation on the list type [A℄ dened from the
equality operation on A?
3
~ R
0
~ R ~
Figure 5.2: A binary tree of type tree.
Null node, illustrated by a bla
k dis
, has no prede
essors, whereas a node
of sort Bnode 0; Bnode 1; : : :, shown in white, has two prede
essors. (The
terminology immediate prede
essor is sometimes used for our `prede
essor'.)
For a general algebrai
type we will have a type A of sorts of node. In
the
ase of trees this type is best thought of as a sum type,
A df (> _ N )
where > is the one element type, for the Null node, and the other summand,
N , is for the Bnode nodes, whi
h
arry numbers. To make the subsequent
a
ount more readable, we rename the inje
tion fun
tions nu and bnode,
and use null for the appli
ation(nu T riv). This means that we
an think
of the elements of A as null together with (bnode n) for natural numbers n.
Dierent kinds of nodes have dierent numbers of prede
essors. For a
parti
ular kind of node a : A we spe
ify what form the prede
essors of the
node take by supplying a type B (a), whi
h we
an think of as the type of
names of prede
essor pla
es.
For a parti
ular node of that sort we spe
ify the
olle
tion of prede
es-
sors of the node by a fun
tion from B (a) to the type in question.
Considering the parti
ular
ase of the type tree, sin
e the Null node
has no prede
essors, we say
B (null) df ?
and for the binary nodes (Bnode n), we have two prede
essors, so we dene
B (bnode n) df N2
We
ould make N2 more readable by repla
ing 12; 22 by Left and Right, as
we would expe
t for the names of the two prede
essor pla
es of the Bnode.
5.10. WELL-FOUNDED TYPES 183
To dene the family B (x) in this way requires the use of the universe U0 ;
without using a universe we
an say
B (x) df ((isnull x = T rue) ^ ?) _ ((isnull x = F alse) ^ N2 )
where the fun
tion isnull of type A ) bool is dened thus:
isnull x df (
ases x y : T rue z : F alse)
The type we build is determined by the
lass of sorts of node, A, and
the family determining the nature of the set of prede
essors of ea
h sort of
node, B (x). The type thus
onstru
ted is
alled (W x : A) : B (x), the W
being used as a reminder that the type is well-founded.
Formation Rule for W
[x : A℄
..
.
A is a type B (x) is a type
(W F )
(W x : A) : B (x) is a type
As far as our type tree is
on
erned, it is
lear that we have satised
the hypotheses of the rule with our denitions of A and B (x).
A general node of type (W x : A) : B (x)
an be built from a node sort,
a : A, and a
olle
tion of prede
essors
f : B (a) ) (W x : A) : B (x)
The node given by f and a is
alled
node a f : (W x : A) : B (x)
This is formalised in the introdu
tion rule
Introdu
tion Rule for W
a : A f : (B (a) ) (W x : A) : B (x))
(W I )
node a f : (W x : A) : B (x)
Going ba
k to our example tree type, how do we form nodes? Choose
rst the element null of A. The set of prede
essor names is B (null), whi
h
is the empty type, ?. For any type T there is a fun
tion from the type ?
to T , given by the abort
onstru
t,
efun df x : abortT x
taking T to be (W x : A) : B (x) itself, we produ
e one element of the W
-type:
node null efun
184 CHAPTER 5. EXPLORING TYPE THEORY
This is the representative of the node Null, whi
h has no prede
essors, and
the argument above
onstitutes an informal derivation of the rule (treeI1 ).
How
an we derive the other tree introdu
tion rule from (W I )? The hy-
potheses of (treeI2 ) are that n : N , u : tree and v : tree. The term
fu;v df x : (
ases2 x u v)
is of type (N2 ) tree), and N2 B (bnode n), whi
h means that this is
a prede
essor fun
tion for a (bnode n) node. Formally, (W I ) allows the
formation of
node (bnode n) fu;v
whi
h represents the node (Bnode n u v). Using the notation Null for the
term (node null efun), the node
node (bnode 3) fNull;g
where g (node (bnode 0) fNull;Null ), represents the tree
Bnode 3 Null (Bnode 0 Null Null)
as illustrated in gure 5.2.
We eliminate a node by the operator whi
h performs re
ursion or in-
du
tion, whi
h we shall
all Re
. The idea of an indu
tion is to prove
C (node a f )
on the basis of proofs of
C (p)
for all the prede
essors p of the node. Remember that the prede
essors
are given by the values of the fun
tion f over the type B (a), so that this
olle
tion of proofs will have the form
pr : (8y : B (a)):C (f y)
The obje
t whi
h performs the proof transformation, i.e. the indu
tion step
of the proof, for a parti
ular node (node a f ) is therefore an obje
t tra;f of
type
tra;f : (8y : B (a)):(C (f y) ) C (node a f ))
Finally, this should be parametrised over f and a, giving our general proof
transformer or indu
tion step as an obje
t of type
(8a : A)(8f : (B (a) ) (W x : A) : B (x)))
((8y : B (a))C (f y) ) C (node a f ))
5.10. WELL-FOUNDED TYPES 185
How
an we dene an operator R
R : (8a : A)(8f : (B (a) ) (W x : A) : B (x)))
((8y : B (a))C (f y) ) C (node a f ))
for these types? We need to dene R(a) for ea
h a in A. Starting with
null, we have to dene an obje
t of type
(8f : (? ) (W x : A) : B (x)))((8y : ?)C (f y) ) C (node a f ))
What are the fun
tions f of type (? ) (W x : A) : B (x))? If we adopt an
extensional approa
h there is but one, the fun
tion efun, sin
e the domain
186 CHAPTER 5. EXPLORING TYPE THEORY
of the fun
tion is the empty type. In that
ase we have to dene an obje
t
of type
((8y : ?)C (f y) ) C (Null))
(where we repla
e (node null efun) with Null). What is the domain type
here? Again it is easy to argue that there is a single fun
tion in the de-
pendent type (8y : ?)C (f y), so that the fun
tion type above
ollapses
to
C (Null)
In other words, our starting value for the indu
tion is a single value
of
type C (Null), just as it is in the rule (treeE ). A similar argument, with
fewer simpli
ations, allows us to see that the
ase of the nodes
(Bnode n u v)
is also
overed by R applied to the elements (bnode n) | we leave this as
an exer
ise for the reader.
Finally, we look at the
omputation rule in the
ase of the Null node.
We assume that R is determined for tree as outlined above, and note that
the general rule is
Re
(node a f ) R ! R a f (x : Re
(f x) R)
In the
ase of (node null efun), it be
omes
Re
Null R ! R null efun (x : Re
(efun x) R)
but re
alling the denition above, the right hand side is simply the value
: C (null). A similar argument applies in the Bnode
ase.
It is worth observing that we needed to move to an extensional equality
between fun
tions to prove the uniqueness of the fun
tion from an empty
domain. If it is wished to remain in an intensional system, the alterna-
tive seems to be to introdu
e the rules expli
itly, type-by-type. Another
alternative is to introdu
e a type of nite fun
tions, whi
h
an be treated
extensionally in an intensional system; this would only work for a limited
lass of W types, in whi
h A is nite and B (a) is nite for ea
h a : A.
Making the argument above more formal, we
an say that we have
established that
ertain types are extensionally isomorphi
, where this is
dened thus.
Denition 5.38 Two types A and B are extensionally isomorphi
if
there are two fun
tions
f :A ) B g :B ) A
5.10. WELL-FOUNDED TYPES 187
5.11 Expressibility
This se
tion gives a
hara
terisation of the fun
tions whi
h
an be written
in the system T T0.
Denition 5.41 A term e of T T0 (or T T , T T +) represents the fun
tion
f over the natural numbers if and only if for all natural numbers n1 ; : : : ; nk ,
e n1 : : : nk !
! f n1 : : : nk
where n is the representation of the natural number n, given by
su
(su
: : : (su
0))
| {z }
n
How
an we
hara
terise the fun
tions f whi
h are representable? First we
know by the normalisation theorem that they are re
ursive, sin
e for ea
h
term e, to nd the value of
e n1 : : : nk
we simply have to redu
e the expression to normal form, and the appli
ation
of the rules is
ertainly a me
hani
al pro
ess. It is equally
lear that we
annot represent all re
ursive fun
tions in this way, sin
e if we
ould a
diagonalisation argument would lead to a
ontradi
tion. (For an exposition
of the elementary details of
omputability theory see, for example, [Cut81,
Rog67℄.)
We thus have that the
lass of fun
tions is properly
ontained between
the
lasses of primitive re
ursive fun
tions and total re
ursive fun
tions. A
lue to the pre
ise
hara
terisation lies in the normalisation result, and the
formalisation of its proof term by term.
Theorem 5.42 For ea
h term e of T T0, the proof of normalisation of e
an be formalised in the theory of rst-order intuitionisti
arithmeti
, HA,
or its
lassi
al
ounterpart P A.
Proof: The proof uses a
oding (or Godel numbering) of the system T T0
within the theory of arithmeti
. It involves
he
king that the steps of the
proof outlined in se
tion 5.6
an be en
oded thus. 2
Note that the result does not
laim that the
omplete normalisation
proof
an be
oded as a whole | the
oding is uniform, but the individual
results
annot be
ombined into a single proof, as the logi
al
omplexity
of the individual proofs grows unboundedly with the
omplexity of the
expression e.
Just as we explained what it was for a fun
tion f to be representable in
one of our type theories, we
an dene how a fun
tion is representable in
P A.
190 CHAPTER 5. EXPLORING TYPE THEORY
The author is unaware of pre
ise
hara
terisations of the fun
tions repre-
sentable in the stronger theories T T and T T +, although [Bee85℄ gives some
partial results, in
luding one for a system with a single universe. Whatever
the
ase, the
lass of fun
tions representable in the type theories is very
large, and indeed it
an be argued that this more than en
ompasses all the
fun
tions we might ever wish to program. In terms of sheer
omputation
time all the fun
tions we program are primitive re
ursive, in the sense that
by suitable transformation any more
omplex
al
ulations
an be bounded
by primitive re
ursive bounds. This is not the most natural way to pro-
eed; in the next
hapter we look at the ways in whi
h fun
tions are most
naturally implemented in the language.
Exer
ise
5.34. One fun
tion whi
h
annot be written in T T is an interpreter for the
expressions of T T itself. Dis
uss how a bounded interpreter for the language
an be written.
5.12. THE CURRY HOWARD ISOMORPHISM? 191
5.12.1 Assumptions
Suppose we have a proof p of the proposition B depending upon the as-
sumption A. The rule of )introdu
tion allows us to derive A ) B without
the assumption of A. There may be a number of o
urren
es of A in p;
without loss of generality all these are dis
harged by the impli
ation in-
trodu
tion. This intuitive a
ount is not an a
urate a
ount of the rule
() I ); only the assumptions of A named x are dis
harged in the appli
ation
[x : A℄
..
.
e:B
() I )
x : e : A ) B
and if e also
ontains y : A, the proof of A ) B still depends upon A. The
alternative rule, whi
h we
all () I )alt would dis
harge all assumptions of
A. It might be argued that the rule () I ) allows the user of the system more
freedom in proof
onstru
tion. This is the
ase, but nonetheless it allows
no more theorems to be proved, for we
an simply repla
e all o
urren
es
of () I ) by () I )alt , some of the appli
ations of the latter resulting in
va
uous dis
harges of the hypothesis of the impli
ation.
On the other hand, named variables are
ru
ial, as
an be seen by the
derivation
[x : N ℄2 [y : N ℄1
(x + y) : N
() I )1
y : (x + y) : N ) N () I )2
x : y : (x + y) : N ) N ) N
For the obje
t x : y : (x + y) to have the proper
omputational behaviour,
it is
ru
ial that the two assumptions x : N and y : N are distin
t, and
that x : N is not identied with y : N . As far as the inhabitation of the
proposition N ) N ) N is
on
erned, it is irrelevant, naturally.
The mismat
h here
an be tra
ed to the divergen
e of interests between
the users of a logi
al system, who are primarily interested in proving theo-
rems, that is in showing that parti
ular types are inhabited, and the users
of a programming language who are interested in the behaviour of many
192 CHAPTER 5. EXPLORING TYPE THEORY
dierent obje
ts of a given type. On the other hand, the proof theorist
who studies the general behaviour of logi
al systems is interested in su
h
behaviour. We look at this next.
The
omputation rules are not the only simpli
ations possible. For the
reasons above, the arguments of 2.11 do not apply, and so we have another
mismat
h. The extra rules
ome in two dierent forms. Instead of repla
ing
`introdu
tion then elimination' we
an also repla
e `elimination then intro-
du
tion'. These are examples of the equivalen
e rules we dis
ussed earlier.
For example, we might en
ounter the following steps in a proof.
[A℄ A ) B A^B A^B
() E ) (^E1 ) (^E2 )
B () I ) A B (^I )
A)B A^B
both of whi
h are
ompletely irrelevant to the result of the proof. The
orresponding redu
tion rules in type theory are
x : (f x) ! f if x not free in f
(fst p; snd p) ! p
and for ea
h type we
an devise a similar rule. The reading we have given to
the rules above shows that as far as proofs are
on
erned, they do perform
a simpli
ation.
The other
lass of
ommutation rules are in
luded in the system stud-
ied by Prawitz for more te
hni
al reasons, whi
h are dis
ussed by him and
also in [GLT89, Se
tion 10℄. The simplest is the equivalen
e between
P 1 P 2
P 2
P
9x:B F P F 3
(R)
(E 9) P 1
F 3
(R)
9x:B D
(E 9)
D D
in whi
h we
an see that the proof of D from F and the proof P3
an be
performed before or after the existential elimination. Any orientation of
this equivalen
e into a redu
tion rule will be arbitrary. Prawitz
hooses to
redu
e the left-hand to the right-hand side.
These
onsiderations seem to be motivated by proof-theoreti
onsider-
ations, but a nal twist is added by their link with the dis
ussion of the
omputational eÆ
ien
y (or otherwise) of
ertain rules, and in parti
ular
the
onsiderations whi
h lead us to the strong elimination rules of se
tion
7.7.
194 CHAPTER 5. EXPLORING TYPE THEORY
Chapter 6
This
hapter investigates the dierent ways in whi
h the system of type
theory
an be used.
We are already familiar with type theory as a
onstru
tive logi
, and
have seen a number of examples of proofs being built in, for example,
se
tions 4.5 and 4.6.1.
We have also seen that T T
an be seen as a fun
tional programming
language, with a number of novel features, su
h as:
{ Every expression has a dened value; every program terminates.
{ The system of types is more expressive than those in
ommon
use, allowing as it does dependent produ
t and fun
tion spa
es.
{ The fun
tional language is integrated with a logi
in whi
h to
reason about the programs.
Another view of program development is provided by the insight that
in T T we
an think of programs as being extra
ted from
onstru
-
tive proofs. This
ombines the two interpretations in an elegant and
powerful way.
Not only
an we use the logi
al system to reason about the prop-
erties of programs, we
an also use to system to support program
transformation.
Finally, we show how we
an develop imperative programs within a
type-theoreti
framework.
195
196 CHAPTER 6. APPLYING TYPE THEORY
6.1 Re
ursion
One of the properties of the systems of type theory T T0, T T and T T + is
strong normalisation: every
omputation sequen
e terminates. This means
6.1. RECURSION 197
that the system does not permit full general re
ursion to be used, as in an
unrestri
ted form this
an lead to non-termination. A simple example is
given by
f0 df 0
f (n + 1) df f (n + 2) + 1
and other, less obvious, examples
an be
onstru
ted.
As a
ounterbalan
e to the weaker re
ursion operation, we have a more
powerful type system than is
ommon in programming languages. We are
able thus to express more pre
isely the true types of fun
tions, using for
instan
e the existential quantier to build a subset type, over whi
h the
fun
tion is total.
In some
ases, the denition of the fun
tion itself depends upon an
indu
tive proof that it terminates; the ee
t of this is to give fun
tions
whose denitions manipulate information witnessing
ertain fa
ts, as well as
the
omputational data. This intermingling of veri
ation and
omputation
is
hara
teristi
of type theory.
We
on
entrate on examples over the natural numbers and lists in this
se
tion, with some introdu
tion of quantied types as we go along. We look
in more depth at these types in the next se
tion.
Before examining parti
ular examples of denitions, it is worth men-
tioning that two general methods present themselves.
Theorem 5.45 shows that anything provably total in P A
an be pro-
grammed in T T0, and indeed the proof will provide a term. This
does beg the question of how the fun
tion is proved total; we would
argue that the system T T0 provides exa
tly the right environment in
whi
h to give su
h proofs, as it allows a
onstru
tive derivation of the
fun
tion whi
h assures its totality.
Proof theorists have
hara
terised
lasses of provably total fun
tions
by means of the well-orderings whi
h
an be used in dening these
fun
tions by re
ursion [S
h77℄. We
ould use this
hara
terisation to
give fun
tions in T T0, but again would argue for the natural nature
of the system itself. There have been a number of proposals for in-
orporating prin
iples of well-founded or general re
ursion; we look
at these in se
tion 7.9.
rithm,
add a 0 df a
add a (n + 1) df add (a + 1) n
This appears to be primitive re
ursive, ex
ept for the fa
t that the argument
a is in
reased on the re
ursive
all. To make a properly primitive re
ursive
denition, we observe that we
an dene the values add a n simultaneously
for all a, by indu
tion on n. In other words, we dene the fun
tions
a : (add a n)
by indu
tion over n. That this is possible is due to the fa
t that in the
denition of adda (n +1) we appeal to a value of add with se
ond argument
n. Formally, if we let C df (N ) N ), then at the base
ase we dene
df a : a : C
To give the re
ursion step, we say
f n h a df h (a + 1)
where n; a : N and h : C . This gives
f : (N ) C ) C)
and so by a simplied form of (NE ) in whi
h C is
onstant,
n : N
: C f : (N ) C ) C )
(NE )
prim n
f : C
with the redu
tion properties
prim 0
f a !
a
(a : a) a
! a
prim (n + 1)
f a ! f n (prim n
f ) a
! (prim n
f ) (a + 1)
We therefore have the denition
add df a : n : (prim n
f a)
There is an analogy between the generalisation we had to make here, from
dening one fun
tion to dening a
lass of fun
tions simultaneously, and
the generalisations of indu
tion hypotheses we often have to make when
6.1. RECURSION 199
In the
ases above we have simply used the
ase analysis aspe
t of
primitive re
ursion in making the denitions. Now we look at an example
whi
h uses the full power of re
ursion in dening the `less than' relation
over the natural numbers.
Informally, nothing is smaller than zero, zero is smaller than n + 1,
and m + 1 is smaller than n + 1 if and only if m is smaller than n. Two
possibilities suggest themselves for the representation of the relation. We
an dene the boolean fun
tion
lt1 : N ) N ) bool
lt1 m 0 df F alse
lt1 0 (n + 1) df T rue
lt1 (m + 1) (n + 1) df lt m n
1
lt2 m 0 df ?
lt2 0 (n + 1) df >
lt2 (m + 1) (n + 1) df lt m n
2
6.5. How would you formulate the relation `less than or equal to' by analogy
with the formulations above?
6.6. Give a re
ursive denition of the iterated
artesian produ
t operator,
whi
h maps a type A and a natural number n to the produ
t
A ^ (A ^ : : : (A ^ A) : : :)
| {z }
n
where the produ
t of zero
opies of A is dened to be >. How would you
dene the proje
tion operations on these types?
[ ℄ ++ y df y
(a :: x) ++ y df a :: (x ++ y)
the reader should have no problem putting them in lre
form.
With the example of
ourse-of-values re
ursion in mind, we need to
dene a fun
tion whi
h will extra
t one of the values from a list. Informally,
[a0 ; : : : ; an 1 ℄ ! m df am
What do we do when m is out of the range 0; : : : ; n 1? One option is to
return a default value, or the last value in the
ase of a non-empty list, but
we
an dene the fun
tion so that its type is suÆ
iently restri
ted not to
allow indexing whi
h is `out of range'. Given a list l the permissible indi
es
are those less than the length # l. The next subse
tion explores various
options for this.
Exer
ises
6.7. Give a denition of the fun
tion last mentioned in the se
tion above.
6.8. Give an expli
it denition of the fun
tion
head4 : (8l :[A℄) : (ne l ) A)
6.1. RECURSION 205
foldl : (A ) B ) A) ) A ) [B ℄ ) A
foldl f a [ ℄ df a
foldl f a (b :: y) df foldl f (f a b) y
A list is sorted if and only if for ea
h pair of elements
hosen from the
list, the element whi
h lies to the left is smaller than or equal to the other.
Formally,
(8m : N ) : (8n : N ) :
(8p :(m < n)) : (8q :(n < #l)) :
(index l m p0 index l n q)
where p0 is the proof of (m < #l) derived from p and q by transitivity of the
ordering relation. List indexing was dened above, the reader might re
all
that in order for an appli
ation of the fun
tion to be legitimate, there needs
to be eviden
e that the index is less than the length of the list argument.
The proof
an be developed for this
hara
terisation, but we
hoose
instead to dene sorting in an indu
tive way, over the stru
ture of the list.
sorted : [N ℄ ) U0
sorted [ ℄ df >
sorted [a℄ df >
sorted (a :: b :: x) df (a b) ^ (sorted (b :: x))
We say that one list is a permutation of another if the number of o
urren
es
of any possible element is the same in both the lists.
perm l l0 df (8a : N ) : (o
s a l =N o
s a l0)
The fun
tion
ounting the o
urren
es is given by
o
s a [ ℄ df 0
o
s a (b :: x) df 1 + o
s a x if eqN a b
o
s a (b :: x) df o
s a x if not
In stating a number of auxiliary results we will need one further denition:
mem :: A ) [A℄ ) U0
mem a [ ℄ df ?
mem a (b :: x) df (a = b) _ (mem a x)
The lemma whi
h follows enumerates a number of basi
properties of
the oredering relation, the fun
tions o
s, mem and the relation perm.
Lemma 6.2 The following types are inhabited.
1. (8x; y : N ) : (lesseq x y = T rue ) x y)
2. (8x; y : N ) : (lesseq x y = T rue _ greater x y = T rue)
6.2. A CASE STUDY { QUICKSORT 211
the former, the result is [ ℄ and this is both sorted and a permutation of
itself, giving the result. Suppose we have a non-empty list; just as when we
were dening the fun
tion, from the proof p we
an extra
t a proof of ?
and then
e a proof of anything, in
luding the
orre
tness
onditions. This
ompletes the proof in the base
ase.
Case m (n + 1)
Again there are two sub-
ases. In the
ase of an empty list we pro
eed
exa
tly as above, so suppose that we have a non-empty list l (a :: x).
Now
qsort0 (n + 1) (a :: x) p l1 ++[a℄ ++l2
where
l1 df qsort0 n (filter (lesseq a) x) p1
l2 df qsort0 n (filter (greater a) x) p2
By indu
tion we know that
sorted l1 ^ sorted l2 (6.2)
perm (filter (lesseq a) x) l1 ^ perm (filter (greater a) x) l 2 (6.3)
We aim to use lemma 6.3 to show that l1 ++[a℄++l2 is sorted. The sortedness
hypotheses are given by (6.2), so we need to show that every element of l1
is less than or equal to a, and to show that every element of l2 is greater
than or equal to a. By (6.3) and lemma 6.2 (parts 5 and 7) we
an dedu
e
(8x : N ) : (mem x l1 ) x a)
and a similar proof establishes
(8x : N ) : (mem x l2 ) a x)
Now, by lemma 6.3 we
an dedu
e that the result is sorted.
To prove that
perm (a :: x) ( l1 ++[a℄ ++l2 )
we use a series of lemmas. By lemma 6.2(11) the list x is a permutation
of the two halves of the partition, whi
h have permutations l1 and l2 by
(6.3). Using lemma 6.2, parts 9, 10 and 12, we have the desired result. This
ompletes the indu
tion step and the proof itself. 2
Corollary 6.5 For all lists l,
sorted (qsort l) ^ perm l (qsort l)
6.2. A CASE STUDY { QUICKSORT 213
related areas this fun
tion is often
alled a norm. This method justies
many important denitions, some of whi
h are in
luded in the exer
ises
whi
h follow.
Exer
ises
6.16. There are many other sorting algorithms over lists, amongst whi
h are
insertion sort and tree sort. Show rst how these expressed in type theory
and then show how they meet the spe
i
ation of a sorting algorithm.
Alternatively prove the result (6.4) in su
h a way as to make the fun
tion
extra
ted from the proof the algorithm you wish to express.
6.17. How would you show that the greatest
ommon divisor fun
tion
dened by
g
d n m df n if n = m
g
d n m df g
d m n if n < m
g
d n m df g
d (n m) m if n > m > 0
g
d n 0 df 0 if not
an be dened in type theory?
6.18. Show how the algorithm for the permutations of a list, whi
h uses
the Miranda list
omprehension notation,
an be
oded in type theory.
perms [ ℄ df [[℄℄
perms x df [ (a :: p) j a x; p perms (x a) ℄
The rule of I formation introdu
es values into the types. Re
all the rule
A is a type a : A b : A
(IF )
I (A; a; b) is a type
whi
h forms the atomi
equality proposition whi
h is also written a =A b.
Clearly the expressions a and b
an
ontain free variables, and thus are
variables introdu
ed into the types; depending on the
omplexity of a and
b we build more or less
omplex propositions. Other dependent types are
then formed using the propositional
onne
tives and quantiers, but these
introdu
e no additional dependen
y. We have already seen examples of
this, in se
tions 4.10 and 6.1.2; another atomi
example is the type
(#l =N n)
where l and n are variables of type [A℄ and N , and from this we
an form
the dependent type
(9l :[A℄) : (#l =N n)
whi
h
ontains the variable n free. For a xed value of n, this is the type
of lists of that length, or more stri
tly, pairs
(l; r)
where r is a witness to (or proof of) the proposition (#l =N n).
One general
lass of propositions we
an dene in this way are those
whi
h are representable by a boolean-valued fun
tion, su
h as the `less
than' relation by
lt1 : (N ) N ) bool)
This is turned into a proposition by forming
I (bool ; lt1 m n ; T rue)
with n,m free. Propositions representable by boolean-valued fun
tions are
de
idable, and so this
lass of propositions is not
losed under quanti
ation
over innite domains. On
e we have turned su
h a representation into a
proposition as above its universal and existential
losure as a proposition
an be formed. This method of forming propositions is indire
t: we dene
a boolean-valued fun
tion, by re
ursion say, and then make a proposition
by equating its appli
ation to T rue or F alse.
Using the universes U0 ; : : : we
an dene dependent propositions di-
re
tly. The `small' types are members of the type U0 , and so we
an use
the expression forming operators, su
h as
ases and re
ursion, to form type
216 CHAPTER 6. APPLYING TYPE THEORY
so that
mapn f (l; r) df (map f l ; r)
whi
h is legitimate sin
e #(map f l) = #l is provable for all l, by indu
tion.
These mapn fun
tions behave like map, ex
ept that they
arry along the
proof-theoreti
information about the length of the list, whi
h
an be used
by fun
tions that use the result.
We usually read a judgement a : A as either asserting that the obje
t a
has type A, or that a is a proof of A. There is a parti
ular
ase in whi
h
both interpretations are used. This is the assertion that
(a; p) : (9x : A) : P
whi
h we
an read as saying that obje
t a, of type A, meets the spe
i
ation
P (x), witnessed by the proof p : P [a=x℄ | we shall
ome ba
k to this topi
later.
for variables and the two
onstant propositions, and at Andf1 f2, Impf1 f2
will make re
ursive
alls to the values at f1 and f2.
There are two approa
hes to dening the proofs of the logi
. The rst
is to make an embedding into the logi
of T T itself, by means of a fun
tion
from formulas to types.
proof : fmla ) U0
proof (V bl v) df fAssum vg
proof T df >
proof F df ?
proof (And f1 f2 ) df (proof f ) ^ (proof f )
1 2
fAssum vg is intended to denote the type with the single element Assum v.
Proofs are to be
onstru
ted in this system using the me
hanisms of T T
itself; unfortunately, the method is
awed. Consider the proof (Assum v)
of the propositional variable (V ar v). In order to
onstru
t a proof of the
tautology
Imp (V ar v) (V ar v)
we need to be able to build a fun
tion from the one element type to itself:
this is trivial, but so is nding a fun
tion of type
Imp (V ar v) (V ar v0 )
for any variable v0 in the system! This is not a sound representation. The
diÆ
ulty is that the proofs of assumptions are not really variables of the
system T T , whi
h they would have to be for the embedding to be sound.
Nonetheless, the embedding is sound if we leave out the variables, giving a
system with
onstants and
onne
tives.
In order to a
hieve a sound implementation in general, we look at the
traditional `LCF' approa
h to the problem [Pau87℄. Under this approa
h we
build an abstra
t type of proofs, with ea
h dedu
tion rule represented by a
fun
tion over the type. The T T me
hanism for abstra
t types is dis
ussed
in the se
tion to
ome, here we look at the implementation of the proof
type. We dene
proof df T r + ConjI proof proof +
ConjE1 proof + ConjE2 proof +
ImpI proof fmla + ImpE proof proof
The type
an be interpreted
ase-by-
ase
6.3. DEPENDENT TYPES AND QUANTIFIERS 219
Exer
ises
6.19. Give denitions of the fun
tions proves,
onjE1 and impE introdu
ed
above.
6.20. Give types for and then dene the fun
tions tr,
onjI and impI .
6.21. Prove that the type se
proof
ontains only legitimate proofs, and
that all su
h proofs are
ontained therein.
q : P rod A B ) B
qF df F B (xA : yB : y)
Observe that
p Fa;b df Fa;b A (xA : yB : x)
df (C : U ) : (f : A ) B ) C ) : (f a b) A (xA : yB : x)
0
Instead of the predi
ate part P of the type (9A : U0 ) : P being simply a
type, it
an
ontain proof information as well. For instan
e we might want
to stipulate that any implementation of the sta
k signature satises
(8n : N ) : (8a : A) : (pop (push n a) = a)
(8n : N ) : (8a : A) : (top (push n a) = n)
We do this by forming a subtype of
A ^ (N ) A ) A) ^ (A ) A) ^ (A ) N )
thus:
(9 (empty; push; pop; top): A ^ (N ) A ) A) ^ (A ) A) ^ (A ) N )) :
((8n : N ) : (8a : A) : (pop (push n a) = a)^
(8n : N ) : (8a : A) : (top (push n a) = n))
In exa
tly the same way we
an form logi
al type
lasses for whi
h we
spe
ify not only the existen
e of obje
ts of a
ertain type, but also demand
that they have
ertain properties. In the
ase of Eq, we might ask for
the relation to be an equivalen
e relation, rather than an arbitrary binary
boolean valued fun
tion. The extra information supplied in one of these
type
lasses would allow us to infer properties of the fun
tions dened over
the
lass.
It is interesting to observe that abstra
t data types and type
lasses are
modelled by exa
tly the same
onstru
tion in type theory. In Haskell the
dieren
e between the two lies in their use. In a Miranda abstype de
la-
ration, we see a signature dened and bound immediately to a parti
ular
implementation. Any
ode whi
h uses the abstype must be
apable of us-
ing any implementation of the signature, and so
an be thought of as a
fun
tion over the appropriate type
lass. The novel feature of type
lasses
is that these fun
tions over
lasses
an be de
lared expli
itly and
an be
applied to more than one implementation of the spe
i
ation within a given
s
ope.
Type
lasses
an be thought of as adding one aspe
t of obje
t-oriented
programming to a fun
tional language. A member of a type
lass might be
thought of as an obje
t. From this perspe
tive, it is interesting to see that
there is support for (multiple) inheritan
e. A sub
lass of the
lass
C1 df (9t : U0 ) : S1
is dened by extending the signature part by adding more operations (or
in the
ase of a logi
al type
lass more operations or properties) to give the
226 CHAPTER 6. APPLYING TYPE THEORY
Theorem 6.6 The relation `<' is a total ordering over the natural num-
bers, as ea
h of the following propositions is inhabited.
1. Re
exivity. x 6< x
2. Symmetry. :(x < y ^ y < x)
3. Transitivity. (x < y) ) (y < z ) ) (x < z )
4. Totality. (x < y) _ (x = y) _ (x > y)
5. Su
essor. x < (x + 1)
Proof: Ea
h of the parts is proved by indu
tion. We look at the rst and
the penultimate. For re
exivity, we work by indu
tion over x. The base
ase 0 6< 0 is given by the
lause m < 0 df F alse of the denition. Now
suppose that (n + 1) < (n + 1). By the third
lause of the denition we
have n < n, whi
h gives ? by indu
tion, so (n + 1) 6< (n + 1).
To prove totality, we work by indu
tion on x with a subsidiary indu
tion
over y. Take x to be 0 | if y is zero then x = y, otherwise by the se
ond
lause of the denition x < y. The indu
tion step is analogous. 2
Denition 6.7 The nite types Cn are dened thus
Cn df (9m : N ) : (m < n)
This denition is uniform in the variable n.
We
an view the transitivity of `<' as asserting the existen
e of
anoni
al
embeddings
fp;q : Cp ) Cq
when p q. If p = q the fun
tion is simply the identity, whilst if p < q,
for any (m; r) : Cp , m < p and p < q gives m < q and thus (m; s) : Cq for
some proof s of the inequality.
We
an also establish a
hara
terisation like that of the types Nn .
Theorem 6.8 For ea
h Cn we
an show that the following proposition is
provable.
(8x : Cn ) : ( x = 0 _ x = 1 _ : : : _ x = n 1 )
where by n we mean the pair
( su
(su
: : : (su
0) : : :) ; T riv )
| {z }
n
228 CHAPTER 6. APPLYING TYPE THEORY
6.4.2 Ve
tors
Using the nite types of the previous se
tion we are in a position to dene
the ve
tor types.
6.27. How would you dene a general sorting fun
tion for ve
tors?
6.28. Explain how the model above
an be extended to treat re
tangular
arrays of any dimension.
6.29. Using the types dened in the previous question, dene the array
produ
t and inversion operations. In the latter
ase you
an use a subtype
to restri
t the domain to those arrays whi
h possess inverses.
baldly stated, is this: given a sequen
e of items whi
h are either red or
white, return a permuted sequen
e in whi
h all the red items pre
ede the
white.
Our approa
h diers from that in [NPS90℄ in two ways. First, we ex-
press the spe
i
ation in a dierent way, so as to separate the
omputational
from the proof-theoreti
; se
ondly, we es
hew the use of the subset type.
We shall dis
uss this type in some detail in
hapter 7. We also use an `equa-
tional' notation whi
h is
loser to that in use in the majority of fun
tional
programming languages.
Our development depends upon some of the fun
tions and predi
ates
whi
h were introdu
ed earlier; in parti
ular we shall use ++, the list
on-
atenation operator, the predi
ate perm l m, whi
h expresses the fa
t that
the list l is a permutation of the list m, together with the auxiliary o
s a l,
whi
h
ounts the number of o
urren
es of a in the list l. We assume that
the
olours are represented by the boolean type, bool (whi
h we shall ab-
breviate C , for
olour), with the value T rue representing the
olour red.
We therefore say isRed a df a = T rue and similarly for isW hite. Also, we
dene
allRed : [bool℄ ) U 0
This pair
onsists pre
isely of the fun
tion required together with a proof
that is has the property required of it. This is the general form that a
spe
i
ation in type theory should take { we return to this topi
in
hapter
7. Note that the transformation of the rst spe
i
ation into the se
ond
is by no means ad ho
{ we have applied the axiom of
hoi
e to the rst
to obtain the se
ond; indeed given this axiom the two spe
i
ations are
logi
ally equivalent. The axiom of
hoi
e is the statement
(8x : A) : (9y : B ) : P (x; y) ) (9f : A ) B ) : (8x : A) : P (x; f x)
It is not hard to show that this type is inhabited; it is left as an exer
ise
for the reader, who will be able to nd a proof in [ML85℄.
There are two distin
t ways of proving a statement like 6.6.
We
an prove the statement by giving a term f and proving the
formula 6.7. This method
orresponds to traditional program devel-
opment in a fun
tional programming language: we rst dene the
fun
tion we think has the desired property and separately we prove
that it does have the property, an exer
ise in program veri
ation.
This was the method we used in developing qui
ksort in se
tion 6.2.
Alternatively, we
an develop the proof from the top down, redu
ing
the existential statement 6.6 to 6.5. We then try to prove this formula
dire
tly and from the proof we extra
t a fun
tion by applying the
axiom of
hoi
e. This program extra
tion te
hnique is the one whi
h
we follow here: note that both methods
an lead to the same denition
of the required fun
tion.
Using the rst method we would dene the fun
tion
split : [C ℄ ) [C ℄ ^ [C ℄
split [ ℄ df ([ ℄; [ ℄)
split (a :: m) df (a :: l0 ; l00 ) if a
df (l0 ; a :: l00 ) if not
where
(l0 ; l00 ) split m
and then attempt to verify, by indu
tion, that split has the property (6.7).
Alternatively we try to prove the result (6.5) dire
tly; this we do now.
Theorem 6.10 For all lists l : [C ℄,
(9(l0 ; l00 ):[C ℄ ^ [C ℄) : (6.8)
(allRed l0 ^ allW hite l00 ^ perm l (l0 ++l00))
6.6. PROGRAM DEVELOPMENT { POLISH NATIONAL FLAG 237
Proof: We prove the result by indu
tion over the list l. We shall
all the
statement (6.8) P (l).
Case: l [ ℄
It is immediate that the types allRed [ ℄ and allW hite [ ℄ are inhabited.
Also [ ℄ ++ [ ℄ [ ℄ and as perm is re
exive (see lemma 6.2, part 12) we
have
perm l (l0 ++l00)
is inhabited. Putting the proof obje
ts together we have some p0 ,
p0 : P ([ ℄)
as required in an indu
tive proof.
Case: l (a :: m)
Suppose that pm : P (m). Now,
pm ((l0 ; l00 ); q)
where q (q1 ; q2 ; q3 ) and
(q1 ; q2 ; q3 ) : (allRed l0 ^ allW hite l00 ^ perm m (l0 ++l00))
Now, sin
e there are only two booleans, a will be either red or white. The
proof pro
eeds now by a
ase analysis. Suppose that
sw : isW hite a
It is not hard to see that
q20 df (
sr ; q2 ) : allW hite (a :: l00 )
and
ertainly
q1 : allred l0
Now, by an earlier result on the simple properties of perm, lemma 6.2, parts
9 and 11, we
an nd
q30 : perm (a :: m) (l0 ++(a :: l00))
where q30 depends upon the proof obje
t q3 . Pulling this together, we have
qw df (q1 ; q20 ; q30 ) :
(allRed l0 ^ allW hite (a :: l00 ) ^ perm m (l0 ++(a :: l00 )))
and so
pw df ( (l0 ; (a :: l00 )) ; qw ) : P (a :: m)
238 CHAPTER 6. APPLYING TYPE THEORY
Here we have an intera
tion between fold and map0 , and as the
onditions
of theorem 6.17 apply, we have
a + ( ((fold bimax) Æ (map0 sum) Æ frontlists0) x)
If we now write
maxfront df (fold bimax) Æ (map0 sum) Æ frontlists0
we have the original expression 6.14 equal to
bimax (a + maxfront x) (maxsub x)
A similar transformation of maxfront yields
maxfront [ ℄ = 0
maxfront (a :: x) = bimax 0 (a + maxfront x)
and for the original fun
tion we have the nal form
maxsub [ ℄ = 0
maxsub (a :: x) = bimax (maxsub x) (a + maxfront x)
We
an make these equations a denition of the maxsub fun
tion, and it
an be seen that its
omplexity dependent on the length of the list. Also,
the fun
tions are free of any of the proof information whi
h appeared in
the original algorithm, be
ause it used non-empty lists.
Exer
ises
6.34. What is the prin
iple of indu
tion for the type of integers int intro-
du
ed above? [Hint: it
an be derived from the prin
iples of indu
tion on
the
omponent parts of the type int.℄
6.35. In the proof of theorem 6.11 why is the indu
tion over the list l and
not the list m?
6.36. Complete the proofs whi
h were only sket
hed in the text.
6.37. State and prove the theorem
orresponding to theorem 6.16 for the
operator foldr rather than fold.
6.38. Give a formal derivation of the fun
tion
sublists0 : [A℄ ) (nel A)
6.39. Explain why the original denition of maxsub has quadrati
om-
plexity and why the nal one is linear.
6.8. IMPERATIVE PROGRAMMING 247
tprim n f n = prim n f
6.9.1 Martin-Lof
In Martin-Lof's work there are few examples. The extensional version of
the theory, [ML85℄,
ontains a proof of the axiom of
hoi
e,
(8x : A) : (9y : B ) : C ) (9f :(8x : A) : B ) : (8x : A) : C [(f x)=y℄
whi
h involves the strong existential elimination rule, (9E ), in an essential
way. As well as a proof of the axiom of
hoi
e, the Padova notes [ML84℄,
ontain small examples but nothing large-s
ale. Martin-Lof's notes on
on-
stru
tive mathemati
s, [ML70℄,
ontain examples of mathemati
al proofs
developed from a
onstru
tive standpoint, but it is fair to say that they
bear little formal relation to proofs in T T .
6.9.2 Goteborg
A sour
e of many examples is the Programming Methodology Group at the
University of Goteborg and Chalmers University of Te
hnology, in Sweden.
In [Dyb87℄ type theory is examined as one of a number of systems applied
to the example of the derivation of an algorithm to normalise the synta
ti
form of propositional expressions.
Generalising the idea of a multilevel array, as used in VDL and VDM,
is the type of multilevel fun
tions, whi
h also en
ompasses the types of
ve
tors, natural numbers, lists and others. A formal presentation of this
type as an addition to T T is given in [Nor85℄. The types
ould also be
developed using the W -types of T T + but the authors argue that it is more
natural to add the new rules to embody the type dire
tly.
In [NP85℄ there is a dis
ussion of how the quantiers and dependent
types of T T
an be used to provide spe
i
ations of modules.
6.9.4 Nuprl
Constable's group at Cornell University have
on
entrated in their Nuprl
system, whi
h we shall dis
uss in more detail in se
tion 9.1.1 on `Imple-
menting Mathemati
s', whi
h is indeed the title of their exposition of Nuprl,
[C+ 86a℄. The system is suÆ
iently
lose to type theory for the majority
of their examples to be
omprehensible if not translatable. [C+ 86a℄
on-
tains short expository examples, of
ourse, but also develops a number of
libraries of mathemati
al obje
ts in Chapter 11.
Howe's thesis, [How88℄, gives a detailed analysis of Girard's paradox in
Chapter 4. Se
tions 3.10 and 3.11
ontain a number-theoreti
example, and
a saddleba
k sear
h, and
hapter 5 addresses the issue of re
e
tion, whi
h
is taken up by [KC86℄. The addition of partial obje
ts to type theory is
dis
ussed in [CS87℄
A more
on
rete appli
ation to hardware spe
i
ation and veri
ation
is presented in [BdV89℄. The spe
i
omponent investigated is the front
end of a
oating point adder/subtra
tor.
253
254 CHAPTER 7. AUGMENTING TYPE THEORY
for a more
ompli
ated type system in view of
omputational eÆ
ien
y are
irrelevant in
ase the system is implemented using lazy evaluation.
After this ba
kground dis
ussion, in se
tion 7.2 we introdu
e the nave
denition of the subset type, and examine some of its theoreti
al draw-
ba
ks. As we said above, mu
h of the motivation for subsets is to a
hieve
a separation between logi
al and
omputational aspe
ts of type theory; in
se
tion 7.3 we look at how subsets
an properly be expressed only in a
system in whi
h propositions and types are no longer identied.
We give a review of the proposals in se
tion 7.4 where we argue that
all the examples of subsets in the literature
an be handled quite smoothly
within T T , without re
ourse to the new type. This is a
hieved by a lazy
implementation together with a judi
ious
hoi
e of types in spe
i
ations:
we
hoose to spe
ify a fun
tion by asserting its existen
e rather than by
a 89-formula, for instan
e. We provide a number of general and
on
rete
examples by way of illustration.
The primitive type forming operation, W ,
onstru
ts free data types,
whilst there are examples in whi
h an abstra
t obje
t
an best be repre-
sented by a
olle
tion of
on
rete items: a set represented as a list is one
ase. To reason and program most simply with su
h a type it is argued that
a quotient type,
onsisting of equivalen
e
lasses, be formed. We examine
two variants of this idea in se
tion 7.5.
An important appli
ation area for
onstru
tive mathemati
s is real anal-
ysis. Se
tion 7.6 gives the beginnings of a treatment of the topi
, and also
serves as a
ase study in whi
h to examine the utility of the subset and
quotient type.
Various infeli
ities have been shown in the rules for elimination | the
following se
tion examines proposals to over
ome these by means of the
strong elimination rules. A generalisation of these, the polymorphi
types
of Ba
khouse et. al. are also introdu
ed. It is interesting to observe that
an addition of this sort
an lead to a term whi
h has no normal form
and indeed no weak head normal form: a genuine non-termination is thus
present in the augmented system. This will maybe underline the fa
t the
`reasonable' additions to a system made in an ad ho
way
an lead to
unforeseen
ir
umstan
es, and that we should make quite
ertain that any
addition we make does not destroy something more important than it adds.
As we saw in the last
hapter, re
ursions
an be
oded in type theory
only in terms of the primitives provided. The most general form of termi-
nating re
ursion is
alled well-founded re
ursion. We rst look at what this
is in a set-theoreti
ontext in se
tion 7.8, and then in the following se
tion
we look at two dierent ways that well-founded re
ursion
an be added
to type theory. The general me
hanism provided derives on
e-and-for-all
the prin
iples of whi
h parti
ular
ases, like qui
ksort, were derived in the
7.1. BACKGROUND 255
7.1 Ba
kground
This se
tion explores some of the ba
kground to the introdu
tion of the
subset type into T T , before we go on to dis
uss exa
tly how this might be
done, and indeed whether it is ne
essary.
256 CHAPTER 7. AUGMENTING TYPE THEORY
onstru
tivist, who would argue that we
an only assert (y) if we have the
appropriate eviden
e, namely the proof obje
t.
In developing a proof of the formula (9o : T ) : P we
onstru
t a pair
on-
sisting of an obje
t of type T and a proof that the obje
t has the property
P . Su
h a pair keeps separate the
omputational and logi
al aspe
ts of the
development, so that we
an extra
t dire
tly the
omputational part simply
by
hoosing the rst element of the pair.
There is a variation on this theme, mentioned in [NPS90℄ and examined
in se
tion 6.6, whi
h suggests that a spe
i
ation of a fun
tion should be
of the form
(8x : A) : (9y : B ) : P (x; y) (7.1)
Elements of this type are fun
tions F so that for all x : A,
F x : (9y : B ) : P (x; y)
and ea
h of these values will be a pair (yx ; px) with
yx : B and px : P (x; y)
The pair
onsists of value and proof information, showing that under this
approa
h the program and its veri
ation are inextri
ably mixed. It has
been argued that the only way to a
hieve this separation is to repla
e
the inner existential type with a subset type, whi
h removes the proof
information px . This
an be done, but the intermingling
an be avoided
without augmenting the system. We simply have to give the intended
fun
tion a name . That su
h a naming
an be a
hieved in general is a
simple
onsequen
e of the axiom of
hoi
e, whi
h states that
(8x : A) : (9y : B ) : P (x; y) ) (9f : A ) B ) : (8x : A) : P (x; f x)
and applying modus ponens to this and (7.1) we dedu
e an `existential'
spe
i
ation as above. Note that the
onverse impli
ation to that of the
axiom of
hoi
e is easily derivable, making the two forms of the spe
i
ation
logi
ally equivalent.
The equivalent spe
i
ations
an be thought of as suggesting dierent
program development methods: using the 98 form, we develop the fun
tion
and its proof as separate entities, either separately or together, whilst in
the 89 form we extra
t a fun
tion from a proof, post ho
.
This analysis of spe
i
ations makes it
lear that when we seek a pro-
gram to meet a spe
i
ation, we look for the rst
omponent of a mem-
ber of an existential type; the se
ond proves that the program meets the
onstraint part of the spe
i
ation. As long as we realise this, it seems
irrelevant whether or not our system in
ludes a type of rst
omponents,
258 CHAPTER 7. AUGMENTING TYPE THEORY
whi
h is what the subset type
onsists of. There are other arguments for
the introdu
tion of a subset type, whi
h we review now.
Exer
ises
7.1. How would you spe
ify the operations + and over the natural num-
bers N ? In general, how would you spe
ify fun
tions whose natural spe
i-
ations are their primitive re
ursive denitions?
7.2. How would you spe
ify the head and tail fun
tions, ++, and the length
and sorting fun
tions over lists?
7.3. When we dis
ussed qui
ksort, we spe
ied permutation by means of
the predi
ate perm, whi
h used the equality fun
tion eqN over the type N .
Give a denition of permutation whi
h does not depend upon the existen
e
of an equality fun
tion and whi
h
an therefore be used over any type of
lists.
7.4. Dis
uss ways in whi
h spe
i
ations
an be made modular, using the
universal and existential quantiers.
annot depend upon this y. We use the fa
t that B [a=x℄ is provable, but
we
annot depend on the proof y itself:
Elimination Rule for Set
[x : A; y : B ℄
..
.
a : f x : A j B g
(x): C (x)
(SetE )
(a) : C (a)
Sin
e no new operator is added by the elimination rule, there is no
ompu-
tation rule for the subset type. We should note that this makes these rules
dierent from the others in type theory. This is also evident from the fa
t
that they fail to satisfy the inversion prin
iple of se
tion 8.4.
We shall write T T0S for the system T T0 with the above rules for subsets
added. How are T T0 and T T0S related? The following theorems are proved
in [Chi88a℄.
Theorem 7.2 From a derivation d in T T0S of the judgement p : (9x : A) : B
a derivation of
fst p : f x : A j B g
from the same set of assumptions
an be
onstru
ted.
Proof: Use the rule (SetI ). 2
Theorem 7.3 If in T T0S we
an derive p : f x : A j B g from the assumptions
, then from this derivation we
an, for some q : B ,
onstru
t a derivation
in T T0 of
(p; q) : (9x : A) : B
from the assumptions 0 . The assumptions 0 result from by repla
ing
assumptions of the form y : f y : C j D g by y : C ; y0 : D where y0 is
hosen
to be a new variable.
Proof: The proof is by indu
tion over the size of the derivation of the
judgement p : f x : A j B g; details
an be found in [Chi88a℄. 2
Neither of these results should be a surprise. The rst simply uses
subset introdu
tion, whilst the se
ond shows that if we are able to derive
membership of a subset type then impli
it in that derivation is a proof that
the element has the property required of it. Examining the derivation allows
us to extra
t that proof obje
t. On the other hand, Smith and Salvesen
show by an elaboration of Martin-Lof's normalisation proof (in se
tion 5.6
and [ML75b℄) that
7.2. THE SUBSET TYPE 263
7.10. Can the fun
tions head0 and tail0 be dened in the extensional version
of type theory dis
ussed above?
7.11. Show that the propositions N , Nn, are stable, and that if equality
over A is formally de
idable then x =A y is stable. Show that if A and B
are stable, then so are A ^ B , A ) B and (8x : A) : B .
7.12. Give examples of formulas whi
h are not stable using disjun
tion and
existential quanti
ation.
and
y :> ; q :B
From these we
an infer (p; q) : (A ^ B ), and so by the law (SetI ),
T riv : f t : > j A ^ B g
a judgement in whi
h neither p nor q is free. This means that by subset
elimination twi
e, we have the same result on the assumption
T riv : f t : > j A g ; T riv : f t : > j B g
whi
h is pre
isely (7.4).
This programme
omes to grief when we try to prove that
[x : A℄
..
. (7.5)
B (x) is true
(8x : A) : B (x) is true
as it is not diÆ
ult to show that a derivation of this will
ontradi
t rule 7.4;
see [Sal89a, Se
tion 3℄. Intuitively we might argue that knowing that B is
true at ea
h instan
e is not suÆ
ient to establish in a uniform way that it
is true universally. This is the only rule for whi
h the
riterion fails, but it
shows that a dierent approa
h must be adopted if we are to nd a proper
representation of the judgement A is true in type theory.
Types are pairs A, A0 in the basi
theory, where A is a type in the theory,
and A0 a predi
ate over A.
Propositions in the new theory are propositions, i.e. types in the old the-
ory, whi
h may of
ourse
ontain quanti
ations over the new types,
and so not all propositions of the old theory are propositions of the
new. The interpretation is dened
onstru
t-by-
onstru
t.
Under this interpretation, all the rules for the propositions are derivable in
the basi
system, so that the
onsisten
y of the basi
system lifts to the
subset theory.
of type theory is its identi
ation of propositions and types, proofs and
programs, but it was argued that it was ne
essary for two major reasons.
Spe
i
ations and fun
tions are made more
ompli
ated by the pres-
en
e of proof theoreti
information. It is argued in [NPS90℄, page 125,
that the inhabitants of a type like
(8x : A) : f y : B j P (x; y) g
we obtain a fun
tion giving only the b without the witnessing infor-
mation.
In general, the development of many well-known fun
tions, like qui
k-
sort in se
tion 6.2, involves the introdu
tion of proof information into
the fun
tions, and this will have a deleterious ee
t on the evalua-
tion eÆ
ien
y of the fun
tion,
ompared to a `purely
omputational'
version.
Any dis
ussion of eÆ
en
y, like the latter point above, rests on the imple-
mentation envisaged, and we would argue, as we did in se
tion 7.1.2, that a
lazy implementation of type theory will result in only the
omputationally
relevant information being evaluated. Now we examine the rst argument
in more detail.
The idea is that we should be able to separate from a
omplex derivation
exa
tly the part whi
h is
omputationally relevant, and that this is to be
done by repla
ing some o
urren
es of existential types by subset types,
from whi
h the witnessing information is absent. We would propose an
alternative whi
h we believe is superior for two reasons:
it is a solution whi
h requires no addition to the system of type theory,
and
it allows for more deli
ate distin
tions between proof and
omputa-
tion.
270 CHAPTER 7. AUGMENTING TYPE THEORY
If we use the Curry equivalen
e, rst proved in se
tion 4.6.1, page 93, whi
h
repla
es existential quantiers in the domain position of a fun
tion type,
we have
(9f :(8z : A) : (D(z ) ) B )) : (8x0 : A) : (8p : D(x0 )) : P ((x0 ; p); (f x0 p))
whi
h makes manifest the fun
tional dependen
e required. Observe that
we
ould indeed have written this formal spe
i
ation dire
tly on the basis
of the informal version from whi
h we started.
If we do not wish the obje
t sought to depend upon the proof of the
property D, we
an write the following spe
i
ation:
(9f : A ) B ) : (8x0 : A) : (8p : D(x0 )) : P ((x0 ; p); (f x0 )) (7.7)
in whi
h it is plain that the obje
t (f x0 ) in B is not dependent on the proof
obje
t p : D(x0 ). Observe that there is still dependen
e of the property P
on the proof p; if we were to use a subset type to express the spe
i
ation,
thus, we would have something of the form
(8x0 : f x0 : A j D(x0 ) g) : (9y : B ) : P 0 (x0 ; y)
where the property P 0 (x; y) relates x0 : A and y : B . This is equivalent to
the spe
i
ation
(9f : A ) B ) : (8x0 : A) : (8p : D(x0 )) : P 0 (x0 ; (f x0 ))
in whi
h the property P 0 must not mention the proof obje
t p, so that with
our more expli
it approa
h we have been able to express the spe
i
ation
7.7 whi
h
annot be expressed under the nave subset dis
ipline.
It is instru
tive to examine examples from the literature in whi
h the
subset type has been used. We have already dis
ussed the Polish
ag prob-
lem, a se
ond example is the parsing algorithm of [Chi87℄. In fa
t we nd
that in Chisholm's derivation of the parsing algorithm, the subset type is
not used in an essential way: the solution is presented as a member of the
type
(8w : W ord) : (P arse w) _ :(P arse w)
where
P arse w df f pt : P T j w = spell pt g
the fun
tion spell giving the word spelt out by the parse tree pt whi
h is a
member of the algebrai
type P T of parse trees. The subset type is used
nowhere else in the derivation, and it is used here only be
ause the proof
272 CHAPTER 7. AUGMENTING TYPE THEORY
The introdu
tion rule breaks uni
ity of typing. An alternative would be
to `tag' variables in some way to indi
ate to whi
h type they belong.
Introdu
tion Rule for A==Ex;y
a:A
(QI )
a : A==Ex;y
If we dene a fun
tion over a quotient type, then the value on equivalent
elements has to be equal, otherwise the fun
tion is not well-dened, giving
dierent values when dierent representatives of the same equivalen
e
lass
are
hosen. The elimination rule for the quotient type, whi
h is the rule
introdu
ing fun
tions over the type must re
e
t this.
In eliminating an element of a quotient type, we behave in the same
way as for the an element of type A, produ
ing some obje
t
(x) : C (x),
ex
ept that for the elimination to be well-dened,
should give the same
values for equivalent elements. This gives an extra hypothesis in the rule.
Elimination Rule for A==Ex;y
[x : A℄ [x : A ; y : A ; p : E ℄
.. ..
. .
a : A==Ex;y
(x): C (x) t : I (C (x);
(x);
(y))
(QE )
(a) : C (a)
There is no separate
omputation rule for the quotient type, but there is a
rule indi
ating that equivalent elements are deemed to be equal, so making
the equivalen
e relation the equality over the type.
Equality rule for A==Ex;y
a : A b : A p : E [a=x; b=y℄
(Q =)
r(a) : I (A==Ex;y ; a; b)
This has the ee
t of allowing equivalent elements to be substituted for
equivalents in any
ontext involving elements of the type A==Ex;y , and so
it is by this rule that the new equality is dened on the type. Given the
judgement r(a) : I (A==Ex;y ; a; b) we
an substitute b for a in any
ontext
where a is
onsidered to be of type A==Ex;y | su
h
ontexts are of
ourse
restri
ted to those where these substitutions
an be performed safely. Note
that it is not asserted that the type I (A; a; b) is inhabited, so that we
annot
substitute b for a in every
ontext.
If it is thought
onfusing that a has both the type A and the type
A==Ex;y we
ould introdu
e a label to suggest when a is being
onsidered
as an element of the quotient type. There is no inverse to this operation,
7.5. QUOTIENT OR CONGRUENCE TYPES 275
StarE A is a type
and the introdu
tion rule
a:A
? a : StarE A
In eliminating the type, we have
[a : A℄ [x : A ; y : A ; p : E ℄
.. ..
. .
s : StarE A
(a) : C (? a) t : I (C (? x);
(x);
(y))
? elimx(
; s) : C (s)
7.5. QUOTIENT OR CONGRUENCE TYPES 277
whi
h would swap any pairs whi
h were out of order. This is more limited
than the general type theoreti
onstru
t both be
ause the language in
whi
h the equivalen
es
ould be written was mu
h simpler, and also be
ause
ea
h of the rules had to be oriented, so that rewriting would only take pla
e
in one dire
tion. Further details of the types and the te
hniques available
to reason about them
an be found in [Tho86, Tho90℄.
As a
oda to this dis
ussion, we should mention [Chi88b℄, in whi
h it is
shown that the subset type
an be used to advantage with the
ongruen
e
type, redu
ing the proof burden whi
h arises in
he
king the
onditions for
the elimination rule. The author shows the dieren
e between the deriva-
tions of the
ardinality fun
tion over types of nite sets implemented with
and without using the subset
onstru
t. The example
hosen is one in
whi
h the equivalen
e
lasses under the equivalen
e relation fail to have
anoni
al members, and it is in this sort of example that the advantage is
most marked.
Exer
ises
7.16. Complete the arguments that + and are well-dened over the
rationals.
7.17. How would you dene the division fun
tion over the rationals?
7.18. Give an expli
it denition of the equivalen
e relation generated by
the equation (7.8).
278 CHAPTER 7. AUGMENTING TYPE THEORY
(9s : N ) Q) : (9m : Q ) N ) :
(8q : Q) : (8n : N ) : (q 0 ^ n > (m q) ) jsn s(m q) j q)
where we have used the subs
ript sn instead of a fun
tion appli
ation for
readability. What are elements of this type? They have the form
(s; (m; p))
where s is a sequen
e, m is a modulus of
ontinuity for s and p is a proof
of this fa
t, whi
h is an element of
(8q : Q) : (8n : N ) : (q 0 ^ n > (m q) ) jsn s(m q) j q)
In
omputing with reals, this latter proof information will be
omputation-
ally irrelevant, but nonetheless has to be dealt with { as we dene new
reals, that is a new sequen
es and its modulus of
ontinuity, we are for
ed
to show that the latter is indeed a modulus fun
tion.
There is a slightly more streamlined approa
h whi
h has been adopted
in [BB85℄ amongst other pla
es. We
an take the sequen
es whi
h have a
xed modulus of
ontinuity, the regular sequen
es, and write
Real df (9s : Seq) : Reg(s)
where Seq df (N ) Q) and
1 1
Reg(s) df (8m; n : N ) : (jsn sm j + )
m+1 n+1
Elements of this type will be pairs
(s; p)
7.6. CASE STUDY { THE REAL NUMBERS 279
m+1 + n+1
1 1
for ea
h natural number k. We say that two reals (s; p) and (t; q) are equal
if the following type is inhabited
1
Eq(s; t) df (8n : N ) : (jsn tn j )
2n+1
Note that this denition depends only upon s and t and not on the proof
information p and q.
We leave it as an exer
ise for the reader to prove that this equality is an
equivalen
e relation over the type Real. It is not diÆ
ult to see that ea
h
of the representatives of zero above is equal, and to see that the denition
of addition above respe
ts equality, so that the following type is inhabited.
Eq(s; s0 ) ^ Eq(t; t0 ) ) Eq(addS s t ; addS s0 t0 )
As Eq is an equivalen
e relation over Real, it seems sensible to investigate
the quotient Realq df Real==Eqs;t. We have already seen that addition
respe
ts the relation Eq, so we
an dene a version of addition
addq : Realq ) Realq ) Realq
In many appli
ations we need to sele
t a parti
ular representative sequen
e
for a real. A
lassi
example is to sele
t, given a real r and a positive ratio-
nal x, a rational within a distan
e x of r. This is trivial given a parti
ular
representative sequen
e, but the rational
hosen will depend upon the par-
ti
ular sequen
e, dierent sequen
es giving dierent approximations. This
means that for a general treatment of the reals we need to use the type Real;
as is remarked in [C+ 86b℄, the type Realq
an provide a useful framework
for substitution if nothing else.
It is no surprise that our
onstru
tive approa
h to the reals is quite
dierent from a
lassi
al one, fo
using as it does on
onvergent sequen
es
of rationals. When we
ompute with `real numbers' we do pre
isely this { it
is only in the idealised framework of
lassi
al mathemati
s that we are able
to deal with innitary obje
ts like equivalen
e
lasses of innite sequen
es.
For further material we would refer the reader to [C+ 86b℄, on whi
h we
have relied here, whi
h develops the theory a little further, and of
ourse
to [BB85℄ whi
h gives a re-development of mu
h of
lassi
al analysis, in a
rigorous but informal way.
Exer
ises
7.19. Show that equality as dened above is an equivalen
e relation, and
give denitions of subtra
tion, absolute value and multipli
ation whi
h re-
spe
t this relation.
7.7. STRENGTHENED RULES; POLYMORPHISM 281
7.20. How would you dene equality and the arithmeti
operations over the
type of Cau
hy reals, RealC ? How would you separate the
omputational
from the proof theoreti
in making these denitions?
7.21. Give denitions of
onvergen
e of sequen
es of reals, and so of
ontinu-
ity of fun
tions, and using this give a proof of theorem 3.2, the
onstru
tive
intermediate value theorem.
7.7.1 An Example
In [Dy
87℄ the example of the proof of
(9z : A _ B ) : P (z ) ) ( (9x : A) : P (inl x) _ (9y : B ) : P (inr y) ) (7.9)
is
ited as the motivation for a re-examination of the rules of type theory.
How does the proof pro
eed? Working top-down we assume
p :(9z : A _ B ) : P (z )
and aim to show that the
onsequent of the impli
ation above is inhabited.
We have an existential assumption, whi
h is used by means of the rule (9E ).
We should therefore try to prove
( (9x : A) : P (inl x) _ (9y : B ) : P (inr y) )
on the basis of the assumptions
z : A _ B ; r : P (z ) (7.10)
To use the disjun
tive assumption z : A _ B , we would like to reason in the
two
ases that z is in the left and right hand sides of the sum; we
annot
do this as the variable z is free in the se
ond assumption. In order to be
able to perform the
ase analysis, we have to make this extra assumption
282 CHAPTER 7. AUGMENTING TYPE THEORY
of whi
h
ertain assumptions may be dis
harged on the appli
ation of the
rule.
A hypotheti
al hypothesis should be thought of as the hypothesis that
a parti
ular judgement
an be derived on the basis of
ertain assumptions,
rather than being derivable outright. In an adaptation of Ba
khouse's no-
tation, as presented in [Ba
87b℄, where we write the hypotheses in a verti
al
list and we use the notation
f J1 ; : : : ; Jk J g
for the hypotheti
al hypothesis that J is derivable from the assumptions
J1 to Jk , the stronger rule for disjun
tion elimination is
p :(A _ B )
f v :(A _ B ); w : C E is a type g
f x : A; w : C [inl x=w℄ b : E [inl x=w℄ g
f y : B; w : C [inr y=w℄
: E [inr y=w℄ g (_EH )
f w : C [a=w℄ whenx;y a b
: E [a=w℄ g
The rules of
omputation for when are exa
tly the same as those for v
ases
and de
ide. More details on this material
an be found in [MC88℄, and we
shall have more to say in general about derivations
ontaining hypotheti
al
hypotheses in se
tion 8.4 below.
The elimination rules for all
onstru
ts
an be given a hypotheti
al form;
some details of this are to be found in [SM87℄, where it is argued that their
use leads to more
ompa
t proof obje
ts, for reasons similar to those whi
h
improved the proof above. One slightly unsatisfa
tory point about the rules
is the
ase of indu
tively dened types, like lists, where the rules are less
general than might be hoped (if
onsisten
y is to be preserved!) In the next
se
tion we look at an alternative approa
h.
7.7.4 Non-termination
There are
onsequen
es of introdu
ing the polymorphi
type A 7! B in an
extensional system. It is well-known that not every term of [ML85℄ has a
normal form, as we
an derive
(p : ?) : ((x : xx)(x : xx)) : ? ) A (7.13)
for any type A. This is be
ause assuming that p : ?, we
an dedu
e that
r : I (Un ; A; A ) A)
and then
A$ $ (A ) A)
Using substitution of types, from x : A we
an dedu
e that x :(A ) A) and
so that
(xx) : A
286 CHAPTER 7. AUGMENTING TYPE THEORY
Further dedu
tions give (7.13). On the other hand, we
an show that every
losed expression has a
anoni
al value, or weak head normal form (
.f.
se
tion 2.3), so that with this weaker notion, the system is terminating. If
we in
lude the polymorphi
type, then there are terms without
anoni
al
form, so the system is not terminating. Take the derivation that
((x : xx)(x : xx)) : A
depending upon the assumption p : ?. By (7! I ), we have
((x : xx)(x : xx)) : ? 7! A
depending upon no assumptions. This term has no
anoni
al form, sin
e
((x : xx)(x : xx)) ! e
if and only if e ((x : xx)(x : xx)).
In an intensional theory like T T , there is no diÆ
ulty of this kind in
adding the polymorphi
type, but note that the polymorphi
types will
ontain fewer obje
ts sin
e as was argued in [SS89℄ none of the rules of T T
lose mention of obje
ts mentioned in their hypotheses.
Exer
ises
7.22. Give a strengthened elimination rule for the existential quantier,
and an example in whi
h its use simplies a proof obje
t.
7.23. Show how the strong elimination rule for _ is a
onsequen
e of the
hypotheti
al rule (_EH ).
7.24. Give `hypotheti
al' versions of the strengthened rules for 9and N
elimination.
7.25. Give a derivation of the judgement (7.12) and use this to show that
head
an be given the type (7.11).
7.26. Complete the derivation of the judgement (7.13).
are simpler than the tree itself. The elimination rule for these types legit-
imises denition by re
ursion and proof by indu
tion over the types. Su
h
re
ursions are limited, in that we limit our notion of what is simpler to a
stru
tural one:
omponent parts are simpler than the whole. The ques-
tion we address in this se
tion is whether there is a more general notion of
`simpler than' over whi
h we
an make re
ursive denitions and indu
tive
proofs. We shall see that indeed there is, and that some of the examples of
re
ursion we have seen in
hapter 6
an be
ast in a more natural form in
this way.
We shall go on to look at other ways in whi
h the re
ursive
apabilities
of the system may be in
reased. In parti
ular we shall also examine how
a wider
lass of `indu
tive' type denitions
an be added to the system,
and also how a treatment of partial fun
tions
an be in
luded, without
breaking the important property of strong normalisation. First, however,
we examine well-founded types and re
ursion.
As this se
tion is intended to be an introdu
tion to the idea of well-
founded re
ursion, we shall use ideas and notation of nave set theory, as
dis
ussed in [End77℄ for instan
e, going on in the next se
tion to examine
how these ideas
an best be in
orporated into type theory.
Denition 7.6 A binary relation is a partial order if for all x, y, z ,
x 6 x
xy^y z )xz
We
an think of x y as expressing `x is simpler than y', as we are
ertain by the rst
lause that we have no loops x x, and in
ombination
with the se
ond that we have nothing of the form
xo x1 : : : xn x0
However, being a partial order is insuÆ
ient to guarantee that we
an
perform re
ursion over the ordering, and the
lassi
example is the relation
n m df m < n
so that
:::n + 1 n ::: 1 0
How would a re
ursion over this type work? We would have to dene
the value at 0 in terms of the value at 1; 2; : : :. In turn, the value at 1 is
determined by values at 2; 3; : : :: never at any point do we make a start with
288 CHAPTER 7. AUGMENTING TYPE THEORY
Theorem 7.13 For every fun
tion F as above, there is a unique solution
to the equation (7.17).
Proof: The proof is by indu
tion over the relation . 2
Just to show how the formal treatment works, we look again at the pow
fun
tion. The fun
tion F dening the re
ursion is given by
F h 0 df 1
F h n df (h (n div 2))2 2(n mod 2)
where the fun
tion h gives the values of the re
ursion on arguments smaller
than the se
ond argument. h is only applied to smaller arguments here:
in the rst
ase it is not used, and in the se
ond it is only applied to
(n div 2) < n when n > 0. The F notation is slightly
umbersome, so we
shall tend to present fun
tion denitions in the form of (7.16) above.
What examples of well-founded orderings are there? For ea
h of the
types A in T T with a re
ursion operator we
an dene a well-founded
ordering A whi
h embodies that re
ursion. We
an read o the relation
from the introdu
tion rules, the elements of the type whi
h appear above
the line being the immediate prede
essors of those appearing below it.
For instan
e, for the type of trees there are two
ases as there are two
introdu
tion rules. The node Null has no prede
essors, and for a Bnode,
we have
u 1 (Bnode n u v) v 1 (Bnode n u v)
and for n + 1 > 1,
t n+1 t0 df 9u(t 1 u ^ u n t0 )
Finally, we say that
t t0 df 9n(t n t0 )
The denition of spe
ies the formation of the transitive
losure of
the immediate prede
essor relation 1 .
Using the original denition of well-foundedness, it is easy to see that
there is a wealth of well-founded orderings. We summarise them now,
leaving proofs to the reader.
The orderings indu
ed by the introdu
tion rules for the types N , [A℄,
tree and the W types in general are well-founded.
If 0 is a well-founded ordering on B and f : (A ) B ) then the
ordering on A dened by
a a0 df (fa) 0 (fa0 )
7.8. WELL-FOUNDED RECURSION 291
Exer
ises
7.31. Che
k that orderings on the types N , lists, trees and so forth are
well-founded a
ording to the denition above { this will involve using the
re
ursion operators in an essential way.
7.32. Complete the proof of theorem 7.15 by giving an expli
it denition
for the expression , and
he
king that it has the property (7.20).
7.33. Show that the well-founded orderings dened in the previous se
tion,
in
luding the produ
t and lexi
ographi
produ
t, satisfy denition 7.14.
7.34. Can you dene an ordering on A whi
h is well-founded a
ording to
predi
ates A ) U0 but not with respe
t to predi
ates in A ) U1 ?
296 CHAPTER 7. AUGMENTING TYPE THEORY
Exer
ises
7.35. Show that the
lass of elements a
essible under the ordering `<' on
N is N itself.
7.36. Given orderings and 0 on A and A0 , how would you
hara
-
terise the elments a
essible in the produ
t, lexi
ographi
produ
t and sum
orderings on A ^ B and A _ B ?
How would you argue that it does not meet the indu
tive
hara
terisation
of the least xed point?
7.38. Argue that the operator whi
h sends T to f1g if 0 2 T , and to f0g if
not has no xed point.
7.39. Show that the interse
tion of a non-empty set of xed points of a
monotoni
operator is a xed point of , and therefore that a least xed
point exists.
7.40. Give xed point denitions of the types of natural numbers and trees.
7.41. How would you dene a type of lists whose elements are either natural
numbers or lists themselves?
7.42. Give a xed point denition of a type of nite and
ountable ordinal
numbers.
There is no introdu
tion rule for the type, rather we have a rule of type
equality.
Type equality rule for Ind:
F ix ! (F ix ) (7.26)
This rule is suÆ
ient to give the usual introdu
tion rule in the
ase of the
operator L. We have
[℄ : L (F ix L)
so that by (7.26), [ ℄ : F ix L. Similarly, if n : N and x : F ix L then
(n :: x) : L (F ix L)
and thus (n :: x) is in F ix L itself.
The elimination rule
an be written in a number of forms. Giving it the
full parametri
ity, we have
Elimination Rule for Ind
[ T F ix ℄
..
.
g : (8x : T ) : C ) (8y : T ) : C [y=x℄
(IndE )
fix g : (8z : F ix ) : C [z=x℄
and the
omputation rule is
Computation Rule for Ind
fix g ! g (fix g)
Examples of types thus denable are lists, trees, and the general W -
types. Mendler augments the me
hanism by allowing the denitions to be
parametri
. This means that types
an be dened by simultaneous re
ur-
sion, and in this
ontext the types
an readily be thought of as predi
ates.
For instan
e, the denition
Root f n df (f n = 0) _ (Root f (n + 1))
denes a family of types by a simultaneous re
ursion over n : N . The
predi
ate dened expresses the property that f has a root greater than or
equal to n. Note that here we have a predi
ate whi
h depends upon a value
n but whi
h is dened without using the universes Ui . Using the re
ursion
operator over this type and the subset type to hide information, Mendler
is able to give a fun
tion implementing unbounded sear
h.
7.11. CO-INDUCTIONS 303
[Dyb88℄ argues than in many
ases, the traditional introdu
tion and
elimination rules of the types
an be read o from the operator , as indeed
we saw for the operator L above. Certainly if it is a sum of produ
ts of
expressions, it is easy to see that this bears a resemblan
e to, say, the
Miranda algebrai
types
ty ::=
on1 t11 ... t1k |
on2 t21 ... t2l |
...
with one
onstru
tor per summand, and one sele
tor per
onstru
tor argu-
ment, or produ
t
omponent. This paper also explores a representation of
these indu
tively dened sets in T T , by means of the W -types. This rep-
resentation is proved to be an isomorphism in
ase the type theory
arries
an extensional equality.
One drawba
k to the addition of these types is that equality between
types is made unde
idable: we
an adopt a tighter notion of equality, like
name equality (see, for example, [Ten79℄) but this seems to be in
on
i
t
with the remainder of the system, for whi
h a stru
tural equality of types
an be maintained.
Exer
ises
7.43. What is the rule of indu
tion for lists given by (IndE )? Compare it
with the rule given earlier in se
tion 5.10.
7.44. Using the rules for sets given in se
tion 7.2, derive a root-nding
program of type
f f : N ) N j (9n : N ) : (Root f n) g ) (9n : N ) : I (N; (f n); 0)
7.45. Give indu
tive denitions of the transitive
losure of R and of the
smallest equivalen
e relation extending R when R is a binary relation over
a type A, say.
7.46. (For logi
ians) Given a formal system F for rst-order arithmeti
,
give an indu
tive denition of the set of theorems provable in the system.
Explain how to give an interpretation of the system over the type N of type
theory, and write down an indu
tive denition of the formulas of F whi
h
are valid under that interpretation.
the (inx)
ons, `::' will see the type of lists as sadly de
ient,
ontaining
as it does only the nite lists. The Miranda programmer is a
ustomed to
being able to dene innite lists su
h as
2::3::5::7::11::...
where undef=undef is the denition of the tail of the list, this list has a
head of 3; only if we examine the tail do we nd that it is undened. We
an
see that the
ombination of unrestri
ted re
ursion and lazy evaluation leads
naturally to these partial lists, whi
h we obviously
annot a
ommodate in
a hereditarily total type theory. Is there any way that we
an retain some
of the power of programming with lazy lists, whi
h is des
ribed so
learly in
[Hug90℄, se
tion 4? We shall see that by looking at the
lass of
o-indu
tive
denitions, i.e. denitions of greatest xed points of operators, we
an build
types with innite obje
ts without adding partially-dened data items. We
pursue the example of the type of innite lists of numbers as a running
illustration.
Innite lists are des
ribed in quite a dierent way to their nite
oun-
terparts. Instead of saying what are the
omponent parts, [ ℄; 0; 1; : : :, and
the `glue', ::, from whi
h they are built, by introdu
tion rules, all we
an
say is that given an innite list, we
an split it up into a head and a tail,
whi
h is again innite. The equation
I df N ^ I (7.27)
des
ribes this in another way, for it says if l : I then
l:N ^ I
so that l (n :: l0 ), with n : N and l0 : I . The equation (7.27) has many
solutions, the smallest of whi
h is ;! Surely anything whi
h de
omposes
l ! (n :: l0 ) has a right to be
alled an innite list, so we should
hoose
the largest and not the smallest of the solutions of (7.27), that is the largest
xed point of the operator I ,
I I df N ^ I
7.11. CO-INDUCTIONS 305
Other types whi
h
an be dened in this way are the type of nite and
innite lists, the largest xed point of the operator L, innite trees and the
like.
We
an dene sophisti
ated fun
tions over the type of innite lists if
we are prepared to in
orporate some proof-theoreti
information into the
domain. One example might be a fun
tion whi
h splits a stream of
har-
a
ters into a stream of words, splitting at ea
h white spa
e
hara
ter. We
annot dene this fun
tion over all the innite lists, but only those with
white spa
e o
urring innitely often. We
an des
ribe exa
tly this
lass
as an existential or subset type, and therefore dene the fun
tion.
Equality over these types is interesting also. If it is intensional, then we
will only identify (for example) two innite lists if they are dened in the
same way. An alternative is to adopt an extensional approa
h, saying that
l ' l0 , (8n : N ) : ( index n l = index n l0 )
This approa
h is adopted in a logi
for Miranda, [Tho89b℄, where it ax-
iomatises equality of innite lists. Using the denotational semanti
s for
Miranda, we
an of
ourse prove that this is the
ase.
In
on
lusion, we would suggest that many of the advantages advan
ed
for lazy lists a
rue here also. In parti
ular, the examples of [Hug90, Se
tion
4℄ seem to
arry over with no diÆ
ulty.
Exer
ises
7.47. Give formal denitions of the fun
tions mapi, sumi and index dened
above.
7.48. Dene the fun
tions
iterate f st df [ st ; f st ; f (f st) ; : : :℄
infold f st [a0 ; a1 ; : : : ; an ; : : :℄ df [ st ; f st a0 ; f (f st a0 ) a1 ; : : :℄
7.49. A natural number greater than one is
alled a Hamming number if
its only prime fa
tors are 2, 3 and 5. Show how to dene a fun
tion whi
h
will merge two innite lists, removing dupli
ate o
urren
es whi
h appear
in both lists and preserving order in the
ase that the lists are ordered.
Using this fun
tion and the iterate fun
tion above give a denition of the
list of Hamming numbers, enumerated in as
ending order. (This problem
is des
ribed in more detail in [Dij76℄.)
7.50. Give a denition of the list of prime numbers.
7.51. Write a denition of a general map fun
tion over the nite and innite
lists.
308 CHAPTER 7. AUGMENTING TYPE THEORY
i
i i i
2
0
R 1 i i
l2 0 l0
2
l1
R 1
7.52. Why
annot an analogue of the filter fun
tion over nite lists be
dened over the innite lists? Can you dene one over the type of nite
and innite lists?
7.53. Give a type and denition to the splitting fun
tion dis
ussed above.
7.11.1 Streams
One of the most prominent appli
ations of the innite lists of Miranda is
to streams between intera
ting pro
esses. We model a system su
h as that
in gure 7.1 by giving expli
it denitions of the
ommuni
ations along the
three
hannels. These
ommuni
ations form lists l0 ; l1 ; l2 , and we
an in the
liberal environment of Miranda write denitions of networks of pro
esses
whi
h will result in deadlo
k | just dene ea
h of the pro
esses to
opy
its input to its output. What happens if we look at an example like this
in the
ontext of type theory, using the innite lists of the last se
tion?
We will have in the
ase above to dene the three lists l0 ; l1 ; l2 by a mutual
re
ursion, and moreover by one whi
h ensures that ea
h of the lists li has at
least a head. In other words, the
ondition on innite lists ensures that we
never have deadlo
k in networks of pro
esses that we dene | the pro
ess
of denition itself prevents that.
In a similar way, we
an model streams whi
h
an
lose down by taking
the streams to be in the greatest xed point of L whi
h gives the type of
nite and innite lists | the absen
e of partial lists again shows that if
a system
an be dened, then it will not deadlo
k: it will either
ontinue
forever, or will
lose down.
Exer
ise
7.54. Give a solution to the dining philosophers problem using the innite
lists of type theory.
7.12. PARTIAL OBJECTS AND TYPES 309
7.13 Modelling
Thus far we have looked at generi
extensions of the system; for the indi-
vidual, the problem at hand will normally be to model a small number of
spe
i
kinds of obje
t, su
h as groups, sta
ks, re
ords and so forth. Two
approa
hes suggest themselves:
A model of the obje
ts is built within type theory, as a series of
abstra
t data types, for instan
e.
The system is itself augmented with new rules des
ribing the obje
ts.
The
ontrast between these approa
hes is investigated in [Dy
85℄, whi
h
examines how to model elementary
ategory theory up to the level of natural
transformations. We look at a simpler
ase here, that of semigroups.
Denition 7.17 A semigroup is a set A together with an asso
iative
operation ? over A. An identity element is an element so that for all
a 2 A,
a?=a=?a
7.13. MODELLING 311
Theorem 7.19 Using the same
onvention as above, if for a parti
ular
element a of A it is the
ase that
a ? a0 = ^ = a0 ? a
a ? a00 = ^ = a00 ? a
are both inhabited, then a0 = a00 is inhabited, proving that inverses are
unique if they exist.
Proof: Exer
ise: the proof uses the fa
t that the operation `?' is asso
ia-
tive. 2
The results depend upon us repeatedly unpa
king the triples (A; (?; f ))
and upon the system having the
apability of giving temporary names to
obje
ts. Dy
kho found that as this was diÆ
ult in the implementation
available to him, it would be more appropriate to axiomatise the theory
dire
tly. For semigroups, axioms would take the form.
Formation Rule for Semigroup
(SemiF )
Semigroup is a type
Writing the three hypotheses in a verti
al list, we have the introdu
tion
rule
Introdu
tion Rule for Semigroup
A istype
? : A)A)A
r : (8a; b;
: A) : ( (a ? b) ?
= a ? (b ?
) )
(SemiI )
SG A ? r : Semigroup
Elimination Rules for Semigroup
S : Semigroup (SemiE )
set S is a type 1
S : Semigroup (SemiE2 )
op S : setS ) set S ) set S
S : Semigroup (SemiE )
asso
S : (8a; b;
: set S ) : ( (a (op S ) b)(op S )
3
Foundations
315
316 CHAPTER 8. FOUNDATIONS
eralisation to the simple types, HA! . The `H ' in these titles is in honour of
the intuitionist Heyting, who was one of the rst
onstru
tivists. We follow
this with a dis
ussion of the te
hnique of realizability, whi
h is used to give
interpretations of intuitionisti
systems, and whi
h
an form the basis of a
more
exible algorithm extra
tion dis
ipline than that of type theory. We
on
lude with a dis
ussion of the various rules for existential elimination,
and what
onsequen
es the
hoi
e of rules has for our implementation of
modules.
Theorem 8.4 For any formula , if HA! ` then for some term t, we
an derive in T T0 the judgement t : .
Proof: The term t is a
oding of the proof of in HA! . More details are
given in [Bee85℄, theorem XI.17.1. 2
Moreover, if we add to HA! the axiom of
hoi
e over nite types, ACF T ,
8x:9y:A(x; y) ) 9f:8x:A(x; f x)
this is also validated by T T0. Showing that the Axiom of Choi
e is derivable
in type theory is one of Martin-Lof's few
on
essions to examples in his
papers. See [ML85, ML84℄, and observe that the derivation does not use
extensionality. If we adopt the extensional theory of [ML85℄, then our type
theory extends the extensional version HA! + Ext + ACF T .
What results
an we derive on the basis of this relationship?
Our rst result, due to Troelstra, is typi
al of the gain we
an get by
looking at these inter-relationships: we are able to transfer a negative result
from HA! to type theory. First we give another denition.
8.1.2 Realizability
We remarked that the proof of theorem 8.8 was by the realizability method.
We take the opportunity of explaining that method here, as it forms the
foundation of an important pie
e of resear
h whi
h we also des
ribe.
Realizability was introdu
ed by Kleene in 1945 as a way of forming re-
ursive models of intuitionisti
theories. Given the informal explanations
of the
onne
tives whi
h we rst en
ountered in
hapter 3, we
an see that
entral to any explanation of a
onstru
tive system is a notion of transfor-
mation, as it is thus that impli
ation and universal quanti
ation are in-
terpreted. Kleene's idea was to use re
ursive fun
tions as the transforming
fun
tions. Of
ourse, re
ursive fun
tions
an be
oded by natural numbers,
and so we shall dene a relation
ek
with e a natural number and a formula. We also write feg(q) # for `the
re
ursive fun
tion e terminates on argument q'. Now we dene realizability
as originally given in [Kle45℄:
Denition 8.9 Realizability for arithmeti
(r-realizability, in fa
t) is de-
ned by the following
lauses.
e k (A ) B ) i 8q:(q k A ) feg(q) # ^ feg(q) k B )
e k 8x:A i 8x:(feg(x) # ^ feg(x) k B )
e k 9x:A i first e k A(se
ond e)
ek A^B i first e k A ^ se
ond e k B
ek A_B i (first e = 0 ) se
ond e k A) ^
(first e 6= 0 ) se
ond e k B )
Finally, any number realizes a true atomi
formula.
We
an think of the interpretation as giving a model of the logi
al sys-
tem, with the valid formulas those for whi
h some e k . The important
point about realizability is the theorem
Theorem 8.10 (Soundness) If HA proves then there is some natural
number e whi
h realizes , that is e k .
Proof: Is by indu
tion over the size of the proof of . A detailed proof is
given in [Bee85℄, se
tion VII.1. 2
This
an be read as saying that for any theorem , we have a term whi
h
gives the formula a
omputational interpretation, as is seen by examining
a formula like
8x:9y:P (x; y)
320 CHAPTER 8. FOUNDATIONS
where P is atomi
.
e k 8x:9y:P (x; y)
i 8x:(feg(x) # ^feg(x) k 9y:P (x; y))
i 8x:(feg(x) # ^first feg(x) k P (x ; se
ond feg(x)))
whi
h means in parti
ular that there is a re
ursive fun
tion g su
h that
8x:P (x; g x)
There are a number of notable aspe
ts of realizability. First observe
that the right-hand sides of the denitions 8.9 are themselves expressions
of arithmeti
. This is
ru
ial to the earlier result that type theory gives a
onservative extension of HA. Be
ause of this identi
ation, we
an study
the relation between a formula and the formal expression of its realizability:
it transpires that for r-realizability the formulas whi
h are equivalent to
the statement of their own realizability 9e:(e k ) are those whi
h have
no existential import: those with no
omputational signi
an
e, in other
words.
Se
ondly we should note how general the denition is: all we need to
give a dierent notion of realizability is a dierent
olle
tion of realizing
fun
tions, or a slightly dierent right-hand
lause. Given a soundness the-
orem, we
an extra
t some
omputational information from a proof of a
formula. This applies equally well to target theories: the denition above
is for rst-order arithmeti
, but in general we might look at other theories,
su
h as higher-order versions of arithmeti
.
Given this, our theory T T0 begins to look like a parti
ular version of re-
alizability for a higher-order type system. Might some disadvantages a
rue
from too intimate a link between the logi
and the notion of realizability
whi
h
an be used to give
omputational
ontent to the theorems of the sys-
tem? One area whi
h we looked at in se
tion 7.1.2 is that of `
omputational
relevan
e', where we saw that in
ertain
ir
umstan
es our proof obje
ts
ontained information whi
h was unne
essary from a
omputational point
of view. A de
oupling of the logi
al rules from the fun
tion extra
tion
me
hanism
ould well result in more eÆ
ient extra
ted obje
ts, without
modi
ation of the logi
itself.
The major advantage of su
h an approa
h is that the logi
in whi
h
proofs are written
an remain xed whilst dierent proof extra
tion te
h-
niques (i.e. notions of realizability) are applied to the proofs. This
ontrasts
with the
ompli
ations introdu
ed by the augmented versions of T T in the
previous
hapter.
This de
oupling lies at the heart of investigations into the system T K
by Henson and Turner, to whi
h we turn in the following
hapter.
8.2. MODEL THEORY 321
denition, and a suÆ
ient
ondition for monotoni
ity is for the dening
formula (8.1) to be positive in the relation
::: = ::: 2 :::
This is not the
ase, as the relation appears in the hypothesis of an impli-
ation. This failure means that the denition
annot be given simply as an
indu
tive denition in this form.
Instead, Allen denes his system in a more
ompli
ated way, spe
ifying
a denition of an operator M, whi
h
an be thought of as a monotone
operator on type theories. In turn, a type theory is seen as a two pla
e
relation T where
T A A
holds if and only if A is a type,
arrying the equality relation A in the type
theory T . Sin
e the operator M is monotone, a semanti
s is then given by
the least xed point of the operator.
In both [All87a℄ and [All87b℄ it is argued that this approa
h is
lose to
that of Beeson, with Allen's more faithful to the lazy evaluation in Martin-
Lof's informal semanti
s of the system.
An advantage of an indu
tive denition is that it
an readily be extended
to augmented systems. In [CS87℄ it is shown how to extend the semanti
s to
the partial types of that paper. Allen himself argues that it
an be used in
justi
ation of some of the `dire
t
omputation rules' of Nuprl, whi
h allow
the redu
tion of terms under fewer hypotheses than would be permitted in
TT.
where `' is the dependent fun
tion spa
e
onstru
tor in the meta-language,
and El is a
onstant of meta-type
(t : T ype):Elem
whi
h asso
iates with a type expression the
olle
tion of elements it is
intended to denote. These two examples suggest that as a meta-theory we
an use a typed -
al
ulus with dependent produ
t types, with rules
of - and -
onversion.
All the operations of the system T T , in
luding those su
h as whi
h
bind variables,
an be presented as
onstants in the meta-theory, taking
advantage of the binding in the meta-language. The operator over the
non-dependent types
an be des
ribed by
:: (t : T ype):(s : T ype):( (El(t) ! El(s)) ! El(t ) s) )
where `e :: ' means `e has meta-type '. The appli
ation operator is a
onstant of type
app :: (t : T ype):(s : T ype):( El(t ) s) ! El(t) ! El(s) )
The
omputation rule for the fun
tion spa
e is, of
ourse, -redu
tion.
Using the subs
ripted form appt;s for the appli
ation of the operator app
to the types t; s, this is des
ribed by the equation between terms
appt;s (t;s f ) a = f a
Sin
e f is a meta-fun
tion, i.e. an element of type
El(t) ! El(s)
substitution in the obje
t language is thus des
ribed by -redu
tion in
the meta-language. A similar approa
h is equally ee
tive with the other
variable-binding operations.
The origins of this approa
h seem to lie in Martin-Lof's theory of ar-
ities , [ML85℄ and his work on
ategories of the philosophi
al and not the
mathemati
al sort, reported in [ML83℄. Expli
it dis
ussions of the presen-
tation of type theory in this form are to be found in [Dyb88℄ and in Part
III of [NPS90℄.
This approa
h has been used in the Edinburgh Logi
al Framework,
whi
h provides a ma
hine environment for general logi
al reasoning, [HHP87℄,
and has shown itself
apable of expressing a variety of dierent logi
s
[AHM87℄. A
onsequen
e of des
riptions of this sort is that the
omplexity
of logi
s like Hoare's logi
for imperative languages like Pas
al is greater
than might at rst be thought; the ee
t is similar to the
omplexity of
languages revealed by their
omplete denotational semanti
denition.
326 CHAPTER 8. FOUNDATIONS
Given proof obje
ts, p; u; v, of the appropriate type we form the new proof
obje
t
v
ases0x;y p u v
binding the variables x and y in u and v respe
tively, sin
e the logi
al rule
dis
harges these assumptions. This is a new expression form, but we
an
see how it may be simplied. A proof of A _ B is either inl a or inr b. In
the former
ase, we
an get a proof of C by substituting a for x in u; in the
latter we substitute b for y in v, giving the familiar
omputation rules
v
ases0x;y (inl a) u v ! u[a=x℄
v
ases0x;y (inr b) u v ! v[b=y℄
We
an generalise this inversion thus: If the n introdu
tion rules for the
onne
tive take the form
Hi;1 : : : Hi;mi
(Ii )
A1 : : : Ak
for i = 1; : : : ; n then there are n and only n dierent ways of introdu
ing
the formula A1 : : : Ak (whi
h we shall write as ). If we
an dedu
e a
formula C from ea
h of the sets of hypotheses
Hi;1 : : : Hi;mi
then this exhausts all the ways in whi
h we
ould have introdu
ed and
so we
an dedu
e C from itself. This is written as a rule
[H1;1 : : : H1;m1 ℄ [Hn;1 : : : Hn;mn ℄
.. ..
. .
C ::: C
(E )
C
If we now look at the situation in type-theory, ea
h of the introdu
tion rules
introdu
es a
onstru
tor Ki for elements of the type , depending upon the
appropriate elements of the hypothesis types,
yi;1 : Hi;1 : : : yi;mi : Hi;mi
(Ii )
Ki yi;1 : : : yi;mi :
We dene a new elimination obje
t elim, whi
h will bind the variables
yi;j in the hypotheti
al proofs pi :
[yi;1 : Hi;1 : : : yi;mi : Hi;mi ℄
..
.
p: : : : pi : C :::
(E )
elim p p1 : : : pn : C
328 CHAPTER 8. FOUNDATIONS
Con lusions
This
hapter gives a survey of a number of approa
hes related to some de-
gree to
onstru
tive type theory; we have tried to stress the major points of
dieren
e between these systems and type theory itself, but to do
omplete
justi
e to them would require another book of this size.
331
332 CHAPTER 9. CONCLUSIONS
whi
h
ontains pre
isely those x whi
h have the property . Indu
tive
onstru
tions are performed along lines similar to se
tion 7.10 above. To
in
rease expressibility, a hierar
hy of universes or kinds are also added to
the set theory { hen
e the name T K .
Another dieren
e betweem T K and T T is that terms in the former
theory
an be partial or undened, whereas in T T they are, of
ourse,
total. The merits of the two approa
hes
an be argued; note that sin
e the
logi
and the types are no longer identied the presen
e of partial obje
ts
does not make the logi
in
onsistent.
Reasoning about the sets is performed in a ri
h logi
al language, and
sin
e the logi
is
onstru
tive, there are realisability models for it. Using
the te
hniques outlined in se
tion 8.1.2 programs
an be extra
ted from
onstru
tive proofs, and it is this te
hnique for program development whi
h
the authors stress, espe
ially in [Hen89℄. The realizability approa
h allows
a lot of
exibility in the
hoi
e of realizing operations: for example, a
onditional assertion
an be added to the language and its realizers
an be
made quite distin
t from those of its usual logi
al denition, for example.
Another advantage of the realizability approa
h is that
omputationally
irrelevant parts of fun
tions need not appear, a topi
we dis
ussed earlier
and whi
h is examined in [Hen91℄.
On the other hand, we have seen that in T T both the options of program
extra
tion and expli
it fun
tional programming are available and
an be
ombined within a single development { whether they
an be
ombined so
well in T K is not
lear.
Work on the theoreti
al foundations of T K and on an implementation
both pro
eed, and it seems
ertain that our understanding of T T will be
enri
hed by these. It remains to be seen whi
h, if either, of the two systems
is the superior for pra
ti
al program development purposes.
sorts of variable in the system, one kind ranging over terminating obje
ts,
the other over all obje
ts, together with a denedness predi
ate, E , mu
h
as in [S
o79℄.
Cru
ial to the logi
is the prin
iple of CIG (for Conditional Indu
tive
Generation). This is the means by whi
h sub-
lasses of the domain are
dened, by whi
h re
ursions pro
eed and by whi
h indu
tive proofs, in-
luding proofs of termination over a domain, are given. In this respe
t it
is analogous to the re
ursive denitions given by well-founded re
ursion in
se
tion 7.9, as well as the indu
tive types of se
tion 7.10.
Hayashi denes a subset of the language
onsisting of the formulas whi
h
ontain no _ or 9 and whi
h are said to be of Rank 0 . No realising terms
are needed for rank zero formulas, as they have no
omputational
ontent.
An example of a formula of rank zero is the in
lusion A B ; an instan
e of
su
h a formula might be an assertion of termination: the set A is
ontained
in the domain of denition, B , of the fun
tion f . It is shown that su
h
formulas
an be proved using
lassi
al logi
without risking the
onsisten
y
or
omputational interpretation of the system, manifesting the
exibility
we dis
ussed in the previous se
tion.
9.1.4 AUTOMATH
The AUTOMATH proje
t was begun at Eindhoven University under the
dire
tion of Ni
olaas de Bruijn in 1966, with the aim
to develop a system of writing entire mathemati
al theories in
su
h a pre
ise fashion that veri
ation of the
orre
tness
an be
arried out by formal operations on the text.
The quotation
omes from [dB80℄ whi
h gives a survey of the proje
t; exam-
ples of the style of presentation of mathemati
s in the system
an be found
in [dB73℄. A pinna
le in the a
hievements of the group is the
omplete
formalization of Landau's Grundlagen, a text on analysis.
The system itself uses a form of type theory, based on the notion of
propositions as types, to represent logi
s of either
lassi
al or
onstru
tive
mathemati
s. Amongst its te
hni
al innovations are a dis
ussion of the
irrelevan
e of proofs when working in a
lassi
al
ontext, whi
h is one
of the reasons advan
ed by de Bruijn for the separation between the no-
tions of type and prop in the system, an idea anti
ipating by some years
dis
ussions in the
omputing s
ien
e
ommunity, reported in se
tion 7.1.2.
In the
ourse of implementing the system, the problem of identifying
-expressions whi
h agree up to a
hange of bound variable names (-
onversion) was found to be a
onsiderable overhead. [dB72℄ introdu
es
a most useful method of
ir
umventing the problem. Variable names are
9.1. RELATED WORK 335
339
340 BIBLIOGRAPHY
[Ba
87b℄ Roland Ba
khouse. Over
oming the mismat
h between pro-
grams and proofs. In Peter Dybjer et al., editors, Pro
eedings
of the Workshop on Programming Logi
. Programming Method-
ology Group, University of Goteborg and Chalmers University
of Te
hnology, 1987. Te
hni
al Report, number 37.
[Bar84℄ Henk P. Barendregt. The lambda
al
ulus | its syntax and
semanti
s, volume 103 of Studies in Logi
and Foundations of
Mathemati
s. North-Holland, 1984.
[BB85℄ Errett Bishop and Douglas Bridges. Constru
tive Mathe-
mati
s, volume 279 of Grundlehren der Mathematis
hen Wis-
sens
haften. Springer-Verlag, 1985.
[BC85℄ Joseph L. Bates and Robert L. Constable. Proofs as programs.
ACM Transa
tions on Programming Languages and Systems, 7,
1985.
[BCMS89℄ Roland Ba
khouse, Paul Chisholm, Grant Mal
olm, and Erik
Saaman. Do-it-yourself type theory. Formal Aspe
ts of Com-
puting, 1, 1989.
[BdV89℄ David A. Basin and Peter del Ve
hio. Veri
ation of
omputa-
tional logi
in Nuprl. Te
hni
al Report TR 89-1018, Department
of Computer S
ien
e, Cornell University, 1989.
[Bee85℄ Mi
hael J. Beeson. Foundations of Constru
tive Mathemati
s.
Springer-Verlag, 1985.
[Ben86℄ Jon Bentley. Programming Pearls. Addison Wesley, 1986.
[BMS80℄ Rod M. Burstall, David B. Ma
Queen, and Donald T. Sanella.
HOPE: An experimental appli
ative language. Te
hni
al re-
port, Department of Computer S
ien
e, University of Edin-
burgh, 1980.
[BW88℄ Ri
hard Bird and Philip Wadler. An Introdu
tion to Fun
tional
Programming. Prenti
e-Hall, 1988.
[C+ 86a℄ Robert L. Constable et al. Implementing Mathemati
s with the
Nuprl Proof Development System. Prenti
e-Hall In
., 1986.
[C+ 86b℄ Robert L. Constable et al. Implementing Mathemati
s with the
Nuprl proof development system. Prenti
e-Hall, 1986.
BIBLIOGRAPHY 341
[Dy
87℄ Roy Dy
kho. Strong elimination rules in type theory. In Peter
Dybjer et al., editors, Pro
eedings of the Workshop on Program-
ming Logi
. Programming Methodology Group, University of
Goteborg and Chalmers University of Te
hnology, 1987. Te
h-
ni
al Report, number 37.
[End77℄ Herbert B. Enderton. Elements of Set Theory. A
ademi
Press,
1977.
[Fef79℄ Solomon Feferman. Constru
tive theories of fun
tions and
lasses. In M. Boa, D. van Dalen, and K. Ma
Aloon, editors,
Logi
Colloquium '78. North Holland, 1979.
[FLO83℄ Steven Fortune, Daniel Leviant, and Mi
hael O'Donnell. The
expressiveness of simple and se
ond-order type stru
tures. Jour-
nal of the ACM, 30(1):151{185, 1983.
[G58℄
Kurt Godel. Uber eine bisher no
h ni
ht benutze Erweiterung
des niten Standpunktes. Diale
ti
a, 12, 1958.
[Gir72℄ Jean-Yves Girard. Interpretation fon
tionelle et elimination des
oupures dans l'arithmetique d'ordre superieure. These d'Etat,
Universite Paris VII, 1972.
[Gir80℄ Jean-Yves Girard. The system F of variable types, fteen years
later. Theoreti
al Computer S
ien
e, 45, 1980.
[GLT89℄ Jean-Yves Girard, Yves Lafont, and Paul Taylor. Proofs and
Types, volume 7 of Cambridge Tra
ts in Theoreti
al Computer
S
ien
e. Cambridge University Press, 1989.
[Har60℄ Ronald Harrop. Con
erning formulas of the types A ! B _
C , A ! (9x)B (x) in intuitionisti
formal systems. Journal of
Symboli
Logi
, 25, 1960.
[Har86℄ Robert Harper. Introdu
tion to Standard ML. Te
hni
al Re-
port ECS-LFCS-86-14, Laboratory for Foundations of Com-
puter S
ien
e, Department of Computer S
ien
e, University of
Edinburgh, November 1986.
[Hay90℄ Susumu Hayashi. An introdu
tion to PX. In Gerard Huet, ed-
itor, Logi
al Foundations of Fun
tional Programming. Addison
Wesley, 1990.
[Hen89℄ Martin C. Henson. Program development in the
onstru
tive
set theory TK. Formal Aspe
ts of Computing, 1, 1989.
344 BIBLIOGRAPHY
[NP83℄ Bengt Nordstrom and Kent Petersson. Types and spe
i
ations.
In IFIP'83. Elsevier, 1983.
[NP85℄ Bengt Nordstrom and Kent Petersson. The semanti
s of module
spe
i
ations in Martin{Lof's type theory. Te
hni
al Report 36,
Programming Methodology Group, University of Goteborg and
Chalmers University of Te
hnology, 1985.
[NPS90℄ Bengt Nordstrom, Kent Petersson, and Jan M. Smith. Program-
ming in Martin-Lof's Type Theory | An Introdu
tion. Oxford
University Press, 1990.
[P67℄ Rosa Peter. Re
ursive Fun
tions. A
ademi
Press, 1967.
[Pau86℄ Lawren
e C. Paulson. Constru
ting re
ursion operators in in-
tuitionisti
type theory. Journal of Symboli
Computation, 2,
1986.
[Pau87℄ Lauren
e C. Paulson. Logi
and Computation | Intera
tive
proof with Cambridge LCF. Cambridge University Press, 1987.
[Per89℄ Nigel Perry. Hope+. Te
hni
al report, Department of Comput-
ing, Imperial College, London, 1989. Version 6.
[Pey87℄ Simon Peyton Jones. The Implementation of Fun
tional Pro-
gramming Languages. Prenti
e Hall, 1987.
[PM87℄ Christine Paulin-Mohring. An example of algorithm develop-
ment in the
al
ulus of
onstru
tions: Binary sear
h for the
al
ulation of the lambo fun
tion. In Peter Dybjer et al.,
editors, Pro
eedings of the Workshop on Programming Logi
.
Programming Methodology Group, University of Goteborg and
Chalmers University of Te
hnology, 1987. Te
hni
al Report,
number 37.
[PM89℄ Christine Paulin-Mohring. Extra
ting F! 's programs from
proofs in the
al
ulus of
onstru
tions. In Pro
eedings of the 16th
ACM Symposium on Prin
iples of Programming Languages.
ACM Press, 1989.
[Pra65℄ Dag Prawitz. Natural Dedu
tion | A Proof-Theoreti
al Study.
Almqvist & Wiksell, 1965.
[PS85℄ Kent Petersson and Jan Smith. Program derivation in type the-
ory: The Polish
ag problem. In Peter Dybjer et al., editors,
Pro
eedings of the Workshop on Spe
i
ation and Derivation
348 BIBLIOGRAPHY
352
INDEX 353
[x : A℄
..
.
A is a type B is a type e:B
(A ) B ) is a type () F ) (x : A) : e : (A ) B ) () I )
q : (A ) B ) a : A
(q a) : B () E )
A is a type B is a type
(A _ B ) is a type (_F )
q :A r :B
inl q : (A _ B ) (_I1 ) inr r : (A _ B ) (_I2 )
p : (A _ B ) f : (A ) C ) g : (B ) C )
(_E )
ases p f g : C
[x : A℄ [y : B ℄
.. ..
. .
p :(A _ B ) u : C v :C
(_E 0 )
v
ases0x;y p u v : C
[x : A℄ [y : B ℄
.. ..
. .
p :(A _ B ) u : C [inl x=z ℄ v : C [inr y=z ℄
(_E 00 )
v
ases00x;y p u v : C [p=z ℄
a : A f : (8x : A) : P
f a : P [a=x℄ (8E )
[x : A℄
..
.
A is a type P is a type a : A p : P [a=x℄
(9x : A) : P is a type (9F ) (a; p) : (9x : A) : P (9I )
p : (9x : A) : P (9E 0 ) p : (9x : A) : P 0
Fst p : A 1
Snd p : P [Fst p=x℄ (9E2 )
[x : A; y : B ℄
..
.
p : (9x : A) : B
:C
Casesx;y p
: C (9E 0 )
[x : A; y : B ℄
..
.
p :(9x : A) : B
: C [(x; y)=z ℄
Casesx;y p
: C [p=z ℄ (9E )
a $
$ b B (a) is a type (S ) a $
$ b p(a): B (a) (S )
B (b) is a type 1
p(b): B (b) 2
A $
$ B A is a type (S ) A $
$ B p : A (S )
B is a type 3
p:B 4
[x : A℄ [x : A℄
.. ..
. .
a : A B is a type a:A b:B
B [a=x℄ is a type (S5 ) b[a=x℄: B [a=x℄ (S6 )
362 RULE TABLES
A is a type a : A b : A a:A
I (A; a; b) is a type (IF ) r(a) : I (A; a; a) (II )
a $
$ b a:A b:A 0 l : I (A; a; b) d : C (a; a; r(a))
r(a): I (A; a; b) (II ) J (
; d) : C (a; b;
) (IE )
363
A is a type
[A℄ is a type (list F )
a : A l :[A℄
[ ℄ : [A℄ (list I1 ) (a :: l) : [A℄ (list I2 )
l :[A℄
s : C [ [ ℄ =x℄
f :(8a : A) : (8l :[A℄) : (C [l=x℄ ) C [(a :: l)=x℄)
lre
l s f : C [l=x℄ (list E )
[x : A℄
..
.
A is a type B (x) is a type
(W x : A) : B (x) is a type (W F )
a : A f : (B (a) ) (W x : A) : B (x))
node a f : (W x : A) : B (x) (W I )
[x : A℄
..
.
A is a type B is a type a : A p : B [a=x℄
f x : A j B g is a type (SetF ) a : f x : A j B g (SetI )
[x : A; y : B ℄
..
.
a : f x : A j B g
(x): C (x)
(SetE )
(a) : C (a)
A is a type
x : A ; y : A ` E is a type
x : A ` r : E [x=x; x=y℄
x : A ; y : A ; r : E ` s : E [y=x; x=y℄
x:A ; y :A ; z :A ;
r : E ; s : E [y=x; z=y℄ ` t : E [x=x; z=y℄
(QF )
A==Ex;y is a type
a:A
(QI )
a : A==Ex;y
364 RULE TABLES
[x : A℄ [x : A ; y : A ; p : E ℄
.. ..
. .
a : A==Ex;y
(x): C (x) t : I (C (x);
(x);
(y))
(QE )
(a) : C (a)
a : A b : A p : E [a=x; b=y℄
(Q =)
r(a) : I (A==Ex;y ; a; b)
[x : A; y : A℄
..
.
A is a type (x y) is a type
(A
F )
A
(A; ) is a type
[y : A; y a℄
..
.
a : A y : A
(A; )
(A
I )
a : A
(A; )
x : A
(A; )
z : A; z x (f z ): C (z )
..
.
p : A
(A; ) (e x f ): C (x)
(A
E )
re
e p : C (p)
monotoni
(IndF )
F ix is a type
[T F ix ℄
..
.
g : (8x : T ) : C ) (8y : T ) : C [y=x℄
(IndE )
fix g : (8z : F ix ) : C [z=x℄
monotoni
(CoinF )
(Xif ) is a type
[y : D ; z : D ) T ℄
..
.
d:D b : T
(CoinI )
xify;z b d : Xif
365
Computation Rules
fst (p; q) ! p
snd (p; q) ! q
((x : A) : p) a ! p[a=x℄
ases (inl q) f g ! fq
ases (inr r) f g ! gr
v
asesx;y (inl a) u v ! u[a=x℄
v
asesx;y (inr b) u v ! v[b=y℄
Fst (p; q) ! p
Snd (p; q) ! q
Casesx;y (a; b)
!
[a=x; b=y℄
if T rue then
else d !
if F alse then
else d ! d
asesn 1n
1 : : :
n !
1
asesn 2n
1 : : :
n !
2
:::
asesn nn
1 : : :
n !
n
ase x
!
prim 0
f !
prim (su
n)
f ! f n (prim n
f )
tre
Null
f !
tre
(Bnode n u v)
f ! f n u v (tre
u
f ) (tre
v
f )
J (r(a); d) ! d
lre
[ ℄ s f ! s
lre
(a :: l) s f ! f a l (lre
l s f )
Re
(node a f ) R ! R a f (x : Re
(f x) R)
re
e p ! e p (re
e)
F ix ! (F ix )
fix g ! g (fix g)
xify;z b d ! b[d=y ; w : (xify;z b w)=z ℄