Programming Languages and Lambda Calculi (Utah CS7520 Version)
Total Page:16
File Type:pdf, Size:1020Kb
Programming Languages and Lambda Calculi (Utah CS7520 Version) Matthias Felleisen Matthew Flatt Draft: March 8, 2006 Copyright c 1989, 2003 Felleisen, Flatt 2 Contents I Models of Languages 7 Chapter 1: Computing with Text 9 1.1 Defining Sets ...................................... 9 1.2 Relations ........................................ 10 1.3 Relations as Evaluation ................................ 11 1.4 Directed Evaluation .................................. 11 1.5 Evaluation in Context ................................. 12 1.6 Evaluation Function .................................. 13 1.7 Notation Summary ................................... 13 Chapter 2: Structural Induction 15 2.1 Detecting the Need for Structural Induction ..................... 15 2.2 Definitions with Ellipses ................................ 17 2.3 Induction on Proof Trees ................................ 17 2.4 Multiple Structures ................................... 18 2.5 More Definitions and More Proofs .......................... 19 Chapter 3: Consistency of Evaluation 21 Chapter 4: The λ-Calculus 25 4.1 Functions in the λ-Calculus .............................. 25 4.2 λ-Calculus Grammar and Reductions ......................... 26 4.3 Encoding Booleans ................................... 28 4.4 Encoding Pairs ..................................... 29 4.5 Encoding Numbers ................................... 30 4.6 Recursion ........................................ 31 4.6.1 Recursion via Self-Application ........................ 32 4.6.2 Lifting Out Self-Application .......................... 33 4.6.3 Fixed Points and the Y Combinator ..................... 34 4.7 Facts About the λ-Calculus .............................. 35 4.8 History ......................................... 37 II Models of Realistic Languages 39 Chapter 5: ISWIM 41 5.1 ISWIM Expressions .................................. 41 5.2 ISWIM Reductions ................................... 42 5.3 The Yv Combinator .................................. 43 5.4 Evaluation ........................................ 45 5.5 Consistency ....................................... 45 3 4 5.6 Observational Equivalence ............................... 49 5.7 History ......................................... 51 Chapter 6: Standard Reduction 53 6.1 Standard Reductions .................................. 53 6.2 Proving the Standard Reduction Theorem ...................... 56 6.3 Observational Equivalence ............................... 63 6.4 Uniform Evaluation .................................. 66 Chapter 7: Machines 69 7.1 CC Machine ....................................... 69 7.2 SCC Machine ...................................... 72 7.3 CK Machine ....................................... 75 7.4 CEK Machine ...................................... 77 7.5 Machine Summary ................................... 81 Chapter 8: SECD, Tail Calls, and Safe for Space 83 8.1 SECD machine ..................................... 83 8.2 Context Space ..................................... 84 8.3 Environment Space ................................... 86 8.4 History ......................................... 87 Chapter 9: Continuations 89 9.1 Saving Contexts .................................... 89 9.2 Revised Texual Machine ................................ 90 9.3 Revised CEK Machine ................................. 91 Chapter 10: Errors and Exceptions 93 10.1 Errors .......................................... 93 10.1.1 Calculating with Error ISWIM ........................ 93 10.1.2 Consistency for Error ISWIM ......................... 95 10.1.3 Standard Reduction for Error ISWIM .................... 98 10.1.4 Relating ISWIM and Error ISWIM ...................... 99 10.2 Exceptions and Handlers ................................ 102 10.2.1 Calculating with Handler ISWIM ....................... 103 10.2.2 Consistency for Handler ISWIM ....................... 104 10.2.3 Standard Reduction for Handler ISWIM ................... 105 10.2.4 Observational Equivalence of Handler ISWIM ................ 106 10.3 Machines for Exceptions ................................ 107 10.3.1 The Handler-Extended CC Machine ..................... 107 10.3.2 The CCH Machine ............................... 108 Chapter 11: Imperative Assignment 111 11.1 Evaluation with State ................................. 111 11.2 Garbage Collection ................................... 114 11.3 CEKS Machine ..................................... 116 11.4 Implementing Garbage Collection ........................... 117 11.5 History ......................................... 118 5 III Models of Typed Languages 119 Chapter 12: Types 121 12.1 Numbers and Booleans ................................. 122 12.2 Soundness ........................................ 124 Chapter 13: Simply Typed ISWIM 127 13.1 Function Types ..................................... 127 13.2 Type Rules for Simply Typed ISWIM ........................ 128 13.3 Soundness ........................................ 130 13.4 Normalization ...................................... 131 Chapter 14: Variations on Simply Typed ISWIM 135 14.1 Conditionals ....................................... 135 14.2 Pairs ........................................... 136 14.3 Variants ......................................... 137 14.4 Recursion ........................................ 138 Chapter 15: Polymorphism 141 15.1 Polymorphic ISWIM .................................. 141 Chapter 16: Type Inference 145 16.1 Type-Inferred ISWIM ................................. 145 16.1.1 Constraint Generation ............................. 146 16.1.2 Unification ................................... 147 16.2 Inferring Polymorphism ................................ 149 Chapter 17: Recursive Types 151 17.1 Fixed-points of Type Abstractions .......................... 151 17.2 Equality Between Recursive Types .......................... 152 17.3 Isomorphisms Between Recursive Types ....................... 153 17.4 Using Iso-Recursive Types ............................... 154 Chapter 18: Data Abstraction and Existential Types 157 18.1 Data Abstraction in Clients .............................. 157 18.2 Libraries that Enforce Abstraction .......................... 158 18.3 Existential ISWIM ................................... 159 18.4 Modules and Existential Types ............................ 161 Chapter 19: Subtyping 163 19.1 Records and Subtypes ................................. 163 19.2 Subtypes and Functions ................................ 165 19.3 Subtypes and Fields .................................. 166 19.4 From Records to Objects ............................... 166 Chapter 20: Objects and Classes 169 20.1 MiniJava Syntax .................................... 169 20.2 MiniJava Evaluation .................................. 172 20.3 MiniJava Type Checking ................................ 173 20.4 MiniJava Soundness .................................. 175 20.5 MiniJava Summary ................................... 181 6 Part I Models of Languages 7 9 Chapter 1: Computing with Text In this book, we study how a programming language can be defined in a way that is easily understood by people, and also amenable to formal analysis—where the formal analysis should be easily understood by people, too. One way of defining a language is to write prose paragraphs that explain the kinds of expressions that are allowed in the language and how they are evaluated. This technique has an advantage that a reader can quickly absorb general ideas about the language, but details about the language are typically difficult to extract from prose. Worse, prose does not lend itself to formal analysis. Another way of defining a language is to implement an interpreter for it in some meta- language. Assuming that a reader is familiar with the meta-language, this technique has the advantage of clearly and completely specifying the details of the language. Assuming that the meta-language has a formal specification, then the interpreter has a formal meaning in that language, and it can be analyzed. The meta-language used to define another language need not execute efficiently, since its primary purpose is to explain the other language to humans. The meta-language’s primitive data constructs need not be defined in terms of bits and bytes. Indeed, for the meta-language we can directly use logic and set theory over a universe of program text. In the same way that computation on physical machines can be described, ultimately, in terms of shuffling bits among memory locations, computation in the abstract can be described in terms of relations on text. 1.1 Defining Sets When we write a BNF grammar, such as B = t | f | (B • B) then we actually define a set B of textual strings. The above definition can be expanded to the following constraints on B: t ∈ B f ∈ B a ∈ B and b ∈ B ⇒ (a • b) ∈ B Technically, the set B that we mean is the smallest set that obeys the above constraints. Notation: we’ll sometimes use “B” to mean “the set B”, but sometimes “B” will mean “an arbitrary element of B”. The meaning is always clear from the context. Sometimes, we use a subscript or a prime on the name of the set to indicate an arbitrary element of the set, such as 0 “B1” or “B ”. Thus, the above constraints might be written as t ∈ B [a] f ∈ B [b] (B1 • B2) ∈ B [c] Whether expressed in BNF shorthand or expanded as a set of constraints, set B is defined recursively. Enumerating all of the elements of B in finite space is clearly impossible in this case: B = {t, f, (t • t), (t • f),...} 10 Chapter 1: Computing with Text Given a particular bit of text