Foundations For Programming Languages by John C. Mitchell
Foundations For Programming Languages by John C. Mitchell
Foundations For Programming Languages by John C. Mitchell
Programming Languages
John C. Mitchell
Department of Computer Science
Stanford University
[email protected]
1
Foundations for Programming Languages 2
Bibliography 661
Index 676
List of Figures
1.1 Binary trees. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 40
3.1 A locally con
uent but non-con
uent reduction. : : : : : : : : : : : : : : : : : : : : : 193
3.2 Disjoint reductions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 194
3.3 Trivial overlap : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 195
3.4 Critical pair : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 196
5.1 Ordering of continuous functions B? ! B? : : : : : : : : : : : : : : : : : : : : : : : 270
7.1 Morphism of cones. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 423
7.2 F -algebras and unique morphisms of cocones. : : : : : : : : : : : : : : : : : : : : : : 428
7.3 Unique morphism from ( ) into limit cone over
i prj
. : : : : : : : : : : : : : : : : 434
11.1 Unication on expression graphs : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 627
8
List of Tables
0.1 Introductory course outline : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 13
0.2 Mathematical course on typed lambda calculus : : : : : : : : : : : : : : : : : : : : : 14
0.3 Course on type theory : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 15
1.1 Well-founded relations for common forms of induction. : : : : : : : : : : : : : : : : : 47
2.1 Equational proof system for PCF : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 71
2.2 Reduction axioms for PCF : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 75
2.3 Left-most reduction for PCF. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 85
2.4 Lazy reduction for PCF. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 87
2.5 Eager PCF reduction. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 90
2.6 Evaluation contexts for lazy PCF reduction. : : : : : : : : : : : : : : : : : : : : : : : 108
3.1 Algebraic specication of stacks. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 152
3.2 Algebraic specication of multi-sets, nat and bool . : : : : : : : : : : : : : : : : : : : 153
3.3 Algebraic specication of trees. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 154
3.4 A specication for set, nat and bool . : : : : : : : : : : : : : : : : : : : : : : : : : : : 170
3.5 A specication for list , atom and bool . : : : : : : : : : : : : : : : : : : : : : : : : : 177
3.6 Naive treatment of error values for list , atom and bool . : : : : : : : : : : : : : : : : 178
3.7 A specication for list , atom and bool with error values. : : : : : : : : : : : : : : : 180
4.1 Type-checking algorithm. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 223
7.1 Smyth-Plotkin method for nding xed-points of functors. : : : : : : : : : : : : : : : 438
11.1 Recursive algorithm Unify. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 628
11.2 Algorithm P T for principal ! (Curry) typing. : : : : : : : : : : : : : : : : : : : : 631
Algorithm reducing ! (Curry) typing to unication.
t
11.3 t : : : : : : : : : : : : : : : : : 637
11.4 Algorithm PTL for principal typing with let. : : : : : : : : : : : : : : : : : : : : : : 648
9
Preface
This book presents a framework for the analysis of syntactic, operational and semantic properties
of programming languages. The framework is based on a mathematical system called typed lambda
calculus. The main features of lambda calculus are a notation for functions and other computable
values, together with an equational logic and rules for evaluating expressions. The book is organized
around a sequence of lambda calculi with progressively more complicated type systems. These are
used to analyze and discuss relevant programming language concepts. The emphasis is on sequential
languages, although many of the techniques and concepts also apply to concurrent programming
languages.
The simplest system in the book is an equational system sometimes called universal algebra.
This logic without function variables may be used to axiomatize and analyze many of the data
types commonly used in programming. The next system is a lambda calculus with function types
and, optionally, cartesian products and disjoint unions. When enriched with recursive denitions,
this language provides a useful framework for studying operational and semantic properties of
functional programs. When combined with algebraic data types, this system is adequate to dene
many Algol-like languages. In particular, with types for memory locations and stores, we may
study traditional axiomatic, operational and denotational semantics of imperative programs. More
advanced technical machinery, such as the method of logical relations, category theory, and the
semantics of recursively dened types are covered in the middle chapters. The last three chapters
of the book study polymorphic types, along with declaration forms for abstract data types and
program modules, systems of subtyping, and type inference.
Prerequisites and relation to other topics
The book is written for upper-level undergraduates or beginning graduate students specializing in
theoretical computer science, software systems, or mathematics. It is also suitable for advanced
study or technical reference. While the only true prerequisite is the proverbial \appropriate level
of mathematical maturity," most students will nd some prior experience with formal logic, com-
putability or complexity theory, and programming languages helpful. In general, students familiar
with these topics at the level of a general introductory course such as [AU92] or above should pro-
ceed with condence and with their sleeves rolled up. To give the prospective reader or instructor
more information, the primary connections with related topics are summarized below.
Mathematical logic. The systems of lambda calculus used in this book share many features
with traditional mathematical logic. Each has a syntax, a proof system, and a model theory. For
this reason, general ideas from logic such as the denition of well-formed formulas, soundness and
completeness of proof systems, and interpretation of expressions in mathematical structures are
used. These are introduced brie
y as needed. First-order logic itself is used only in the sections
on proving properties of programs; here an intuitive understanding of the meaning of formulas is
assumed.
10
Foundations for Programming Languages 11
Computability and complexity theory. The basic distinction between computable and non-
computable functions is used in the study of PCF (Chapter 2). The text denes and uses the
class of partial recursive functions and refers to Turing machines in the exercises of two sections. A
few additional concepts from recursion theory are assumed in constructing semantic models using
Godel numbering of recursive functions (Chapter 4). All of these would be familiar from any course
that covers universal Turing machines or undecidable properties of computable functions. A certain
amount of basic recursion theory is developed in the text using PCF, including a simple exercise
showing that the halting problem for PCF programs is not programmable in PCF.
Programming. Although no specic programming experience is required, students with some
exposure to a programming language with higher-order functions, such as Lisp, Scheme or ML,
will nd it easier to relate this theory to practice. To give a general feel for the expressiveness of
typed lambda calculus, Chapter 2 contains a series of programming examples and techniques. This
provides a self-contained overview of some relevant programming issues.
Category theory. Category theory appears only in more advanced sections of the book. While all
the necessary denitions are presented and illustrated by example, a non-mathematical reader with
no prior exposure to category theory may wish to consult additional sources. If a more leisurely or
comprehensive introduction is needed, the reader is referred to an elementary introduction tailored
to computer scientists, e.g., [BW90, Pie91].
Sample Course Outlines
Three sample course outlines are given in Tables 0.1 through 0.3. The rst is an introductory
course that has been taught several times as Stanford CS 258. The listed prerequisites for this
course, which covers the core topics in Chapters 2{6, are a one-quarter course in automata and
computability theory and a one-quarter course that includes mathematical logic but does not cover
soundness, completeness or model-theoretic constructions in depth. CS 258 has been completed
successfully by undergraduates, M.S. students specializing in systems or theory, and beginning
Ph.D. students. While the Stanford course is taught in 10 weeks, it is easy to expand the course to
a 15-week semester. Some options for expansion are: (i) cover the topics listed at a more leisurely
pace, (ii) include the section on algebraic rewrite systems, (iii) prove soundness, completeness
and other properties of typed lambda calculus, or (iv) survey selected topics from Chapters 9{
11. It is also possible to drop imperative programs (Chapter 6) in favor of one or more of these
options. While the chapter on algebra (Chapter 3) is not strictly required for later topics, universal
algebra provides a useful opportunity to introduce or review logical concepts in a relatively simple
mathematical setting. This aspect of the chapter may be redundant if students have taken a more
rigorous undergraduate course on mathematical logic.
The second course, in Table 0.2, is a more mathematical course on typed lambda calculus
and semantic techniques, with more technical detail and less programming motivation. The third
course, in Table 0.3, covers type systems, beginning with typed lambda calculus and proceeding
with polymorphism, subtyping and type inference. These three overlapping courses cover most of
the book.
Acknowledgements and Disclaimers
Many people have read drafts and provided useful comments and suggestions. I would like to
thank M. Abadi, S. Abramsky, V. Breazu-Tannen, K. Bruce, L. Cardelli, R. Casley, P.-L. Curien,
P. Gardner, D. Giord, D. Gries, C. Gunter, R. Harper, S. Hayashi, F. Henglein, B. Howard, P.
Kanellakis, A. Kfoury, P. Lescanne, H. Mairson, I. Mason, A. Meyer, E. Moggi, N. Marti-Oliet, A.
Foundations for Programming Languages 12
Pitts, J. Riecke, K. Ross, D. Sanella, P. Scott, D. Tranah, T. Uribe and the students of Stanford
CS 258 and CS 358. Special thanks to teaching assistants My Hoang, Brian Howard and Ramesh
Viswanathan for their help with homework exercises and sample solutions, a few of which made
their way into examples in the text.
Almost all of this book is based on previously published research, some by the author. When
specic results are taken from the literature, an eort has been made to cite original sources as
well as relevant survey articles and books. However, as with any project of this size, there are
likely to be some errors and omissions. In addition, while an eort has been made to circulate and
teach any original material or alternate proofs developed for this book, there are undoubtably some
remaining errors.
John C. Mitchell
Stanford, CA
Foundations for Programming Languages 13