Programming Languages Fall 2014

Similar documents
CIS 500 Software Foundations Fall October 2

CIS 500 Software Foundations Midterm I

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Programming Languages Fall 2013

Costly software bugs that could have been averted with type checking

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

(Advanced) topics in Programming Languages

Lecture 9: Typed Lambda Calculus

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

CIS 500 Software Foundations Midterm II Answer key November 16, 2005

Subsumption. Principle of safe substitution

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

Fundamental Concepts. Chapter 1

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Programming Languages

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

Concepts of programming languages

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Induction and Semantics in Dafny

Chapter 22: Type Reconstruction (Type Inference)

CIS 500 Software Foundations Fall September 25

Formal Systems and their Applications

The Substitution Model

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

Typed Lambda Calculus. Chapter 9 Benjamin Pierce Types and Programming Languages

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Part III. Chapter 15: Subtyping

CSE 505 Graduate PL. Fall 2013

Type Systems Winter Semester 2006

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

1 Introduction. 3 Syntax

Type Systems Winter Semester 2006

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Simply-Typed Lambda Calculus

Part III Chapter 15: Subtyping

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

The Substitution Model. Nate Foster Spring 2018

Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009

Typed Lambda Calculus and Exception Handling

上海交通大学试卷 ( A 卷 ) ( 2015 至 2016 学年第 2 学期 )

Notes for Recitation 8

Lambda Calculus. Concepts in Programming Languages Recitation 6:

CSE505, Fall 2012, Midterm Examination October 30, 2012

CS4215 Programming Language Implementation. Martin Henz

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

COS 320. Compiling Techniques

15 212: Principles of Programming. Some Notes on Induction

Softwaretechnik. Lecture 03: Types and Type Soundness. Peter Thiemann. University of Freiburg, Germany SS 2008

CSE-321 Programming Languages 2012 Midterm

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Mutable References. Chapter 1

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Lecture 13: Subtyping

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

Type Systems. Parametric Polymorphism. 1. Recall Let-Polymorphism. 1. Recall Let-Polymorphism. Lecture 9 Dec. 15th, 2004 Sebastian Maneth

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

Programming Languages Lecture 14: Sum, Product, Recursive Types

Axiomatic Rules. Lecture 18: Axiomatic Semantics & Type Safety. Correctness using Axioms & Rules. Axiomatic Rules. Steps in Proof

Simple Unification-based Type Inference for GADTs

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

CMSC 330: Organization of Programming Languages

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CIS 500 Software Foundations Fall December 6

Administrivia. Existential Types. CIS 500 Software Foundations Fall December 6. Administrivia. Motivation. Motivation

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Inductive Definitions, continued

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Lambda Calculus and Type Inference

Computing Fundamentals 2 Introduction to CafeOBJ

Fall Recursion and induction. Stephen Brookes. Lecture 4

Expr_annotated.v. Expr_annotated.v. Printed by Zach Tatlock

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Assistant for Language Theory. SASyLF: An Educational Proof. Corporation. Microsoft. Key Shin. Workshop on Mechanizing Metatheory

Operational Semantics 1 / 13

Behavioral Equivalence

Behavioral Equivalence

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Meeting13:Denotations

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper)

CIS 500 Software Foundations. Final Exam. May 3, Answer key

Type Safety. Java and ML are type safe, or strongly typed, languages. C and C++ are often described as weakly typed languages.

Type families and data kinds

CSE-321 Programming Languages 2010 Midterm

Lambda Calculus and Type Inference

Lecture 3: Recursion; Structural Induction

CS558 Programming Languages

A Type System for Functional Traversal-Based Aspects

MoreIntro.v. MoreIntro.v. Printed by Zach Tatlock. Oct 07, 16 18:11 Page 1/10. Oct 07, 16 18:11 Page 2/10. Monday October 10, 2016 lec02/moreintro.

CS558 Programming Languages

Lambda Calculi With Polymorphism

Checks and Balances - Constraint Solving without Surprises in Object-Constraint Programming Languages: Full Formal Development

Types and Programming Languages. Lecture 5. Extensions of simple types

Transcription:

Programming Languages Fall 2014 Lecture 7: Simple Types and Simply-Typed Lambda Calculus Prof. Liang Huang huang@qc.cs.cuny.edu 1

Types stuck terms? how to fix it? 2

Plan First I For today, we ll go back to the simple language of arithmetic and boolean expressions and show how to equip it with a (very simple) type system I The key property of this type system will be soundness: Well-typed programs do not get stuck Next I Next time, we ll develop a simple type system for the lambda-calculus I We ll spend a good part of the rest of the semester adding features to this type system 3

Outline 1. begin with a set of terms, a set of values, and an evaluation relation 2. define a set of types classifying values according to their shapes 3. define a typing relation t : T that classifies terms according to the shape of the values that result from evaluating them 4. check that the typing relation is sound in the sense that, 4.1 if t : T and t! v, then v : T 4.2 if t : T, then evaluation of t will not get stuck 4

Review: Arithmetic Expressions Syntax t ::= terms true constant true false constant false if t then t else t conditional 0 constant zero succ t successor pred t predecessor iszero t zero test v ::= values true true value false false value nv numeric value nv ::= numeric values 0 zero value succ nv successor value 5

Evaluation Rules if true then t 2 else t 3! t 2 (E-IfTrue) if false then t 2 else t 3! t 3 (E-IfFalse) t 1! t 0 1 if t 1 then t 2 else t 3! if t 0 1 then t 2 else t 3 (E-If) 6

t 1! t 0 1 succ t 1! succ t 0 1 (E-Succ) pred 0! 0 (E-PredZero) pred (succ nv 1 )! nv 1 (E-PredSucc) t 1! t 0 1 pred t 1! pred t 0 1 (E-Pred) iszero 0! true (E-IszeroZero) iszero (succ nv 1 )! false (E-IszeroSucc) t 1! t 0 1 iszero t 1! iszero t 0 1 (E-IsZero) 7

Types In this language, values have two possible shapes : they are either booleans or numbers. T ::= types Bool type of booleans Nat type of numbers 8

Typing Rules true : Bool false : Bool t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T (T-True) (T-False) (T-If) 0 : Nat (T-Zero) t 1 : Nat succ t 1 : Nat t 1 : Nat pred t 1 : Nat t 1 : Nat iszero t 1 : Bool (T-Succ) (T-Pred) (T-IsZero) 9

Typing Derivations Every pair (t, T) in the typing relation can be justified by a derivation tree built from instances of the inference rules. 0 : Nat iszero 0 : Bool T-Zero T-IsZero 0 : Nat T-Zero 0 : Nat T-Zero T-Pred pred 0 : Nat T-If if iszero 0 then 0 else pred 0 : Nat Proofs of properties about the typing relation often proceed by induction on typing derivations. 10

Imprecision of Typing Like other static program analyses, type systems are generally imprecise: they do not predict exactly what kind of value will be returned by every program, but just a conservative (safe) approximation. t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T (T-If) Using this rule, we cannot assign a type to if true then 0 else false even though this term will certainly evaluate to a number. 11

Properties of the Typing Relation 12

Type Safety The safety (or soundness) of this type system can be expressed by two properties: 1. Progress: A well-typed term is not stuck If t : T, then either t is a value or else t some t 0.! t 0 for 2. Preservation: Types are preserved by one-step evaluation If t : T and t! t 0, then t 0 : T. 13

Inversion Lemma: 1. If true : R, then R = Bool. 2. If false : R, then R = Bool. 3. If if t 1 then t 2 else t 3 : R, then t 1 : Bool, t 2 : R, and t 3 : R. 4. If 0 : R, then R = Nat. 5. If succ t 1 : R, then R = Nat and t 1 : Nat. 6. If pred t 1 : R, then R = Nat and t 1 : Nat. 7. If iszero t 1 : R, then R = Bool and t 1 : Nat. 14

Inversion Lemma: 1. If true : R, then R = Bool. 2. If false : R, then R = Bool. 3. If if t 1 then t 2 else t 3 : R, then t 1 : Bool, t 2 : R, and t 3 : R. 4. If 0 : R, then R = Nat. 5. If succ t 1 : R, then R = Nat and t 1 : Nat. 6. If pred t 1 : R, then R = Nat and t 1 : Nat. 7. If iszero t 1 : R, then R = Bool and t 1 : Nat. Proof:... 15

Inversion Lemma: 1. If true : R, then R = Bool. 2. If false : R, then R = Bool. 3. If if t 1 then t 2 else t 3 : R, then t 1 : Bool, t 2 : R, and t 3 : R. 4. If 0 : R, then R = Nat. 5. If succ t 1 : R, then R = Nat and t 1 : Nat. 6. If pred t 1 : R, then R = Nat and t 1 : Nat. 7. If iszero t 1 : R, then R = Bool and t 1 : Nat. Proof:... This leads directly to a recursive algorithm for calculating the type of a term... 16

Typechecking Algorithm typeof(t) = if t = true then Bool else if t = false then Bool else if t = if t1 then t2 else t3 then let T1 = typeof(t1) in let T2 = typeof(t2) in let T3 = typeof(t3) in if T1 = Bool and T2=T3 then T2 else "not typable" else if t = 0 then Nat else if t = succ t1 then let T1 = typeof(t1) in if T1 = Nat then Nat else "not typable" else if t = pred t1 then let T1 = typeof(t1) in if T1 = Nat then Nat else "not typable" else if t = iszero t1 then let T1 = typeof(t1) in if T1 = Nat then Bool else "not typable" 17

Canonical Forms Lemma: 1. If v is a value of type Bool, then v is either true or false. 2. If v is a value of type Nat, then v is a numeric value. Proof: 18

Canonical Forms Lemma: 1. If v is a value of type Bool, then v is either true or false. 2. If v is a value of type Nat, then v is a numeric value. Proof: Recall the syntax of values: v ::= values true true value false false value nv numeric value nv ::= numeric values 0 zero value succ nv successor value For part 1, 19

Canonical Forms Lemma: 1. If v is a value of type Bool, then v is either true or false. 2. If v is a value of type Nat, then v is a numeric value. Proof: Recall the syntax of values: v ::= values true true value false false value nv numeric value nv ::= numeric values 0 zero value succ nv successor value For part 1, if v is true or false, the result is immediate. 20

Canonical Forms Lemma: 1. If v is a value of type Bool, then v is either true or false. 2. If v is a value of type Nat, then v is a numeric value. Proof: Recall the syntax of values: v ::= values true true value false false value nv numeric value nv ::= numeric values 0 zero value succ nv successor value For part 1, if v is true or false, the result is immediate. But v cannot be 0 or succ nv, since the inversion lemma tells us that v would then have type Nat, not Bool. 21

Canonical Forms Lemma: 1. If v is a value of type Bool, then v is either true or false. 2. If v is a value of type Nat, then v is a numeric value. Proof: Recall the syntax of values: v ::= values true true value false false value nv numeric value nv ::= numeric values 0 zero value succ nv successor value For part 1, if v is true or false, the result is immediate. But v cannot be 0 or succ nv, since the inversion lemma tells us that v would then have type Nat, not Bool. Part 2 is similar. 22

Progress Theorem: Suppose t is a well-typed term (that is, t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. 23

Progress Theorem: Suppose t is a well-typed term (that is, t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. Proof: 24

Progress Theorem: Suppose t is a well-typed term (that is, t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. Proof: By induction on a derivation of t : T. 25

Progress Theorem: Suppose t is a well-typed term (that is, t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. Proof: By induction on a derivation of t : T. The T-True, T-False, and T-Zero cases are immediate, since t in these cases is a value. 26

Progress Theorem: Suppose t is a well-typed term (that is, t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. Proof: By induction on a derivation of t : T. The T-True, T-False, and T-Zero cases are immediate, since t in these cases is a value. Case T-If: t = if t 1 then t 2 else t 3 t 1 : Bool t 2 : T t 3 : T 27

Progress Theorem: Suppose t is a well-typed term (that is, t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. Proof: By induction on a derivation of t : T. The T-True, T-False, and T-Zero cases are immediate, since t in these cases is a value. Case T-If: t = if t 1 then t 2 else t 3 t 1 : Bool t 2 : T t 3 : T By the induction hypothesis, either t 1 is a value or else there is some t 0 1 such that t 1! t 0 1. If t 1 is a value, then the canonical forms lemma tells us that it must be either true or false, in which case either E-IfTrue or E-IfFalse applies to t. On the other hand, if t 1! t 0 1, then, by E-If, t! if t 0 1 then t 2 else t 3. 28

Preservation Theorem: If t : T and t! t 0, then t 0 : T. 29

Preservation Theorem: If t : T and t! t 0, then t 0 : T. Proof: By induction on the given typing derivation. 30

Preservation Theorem: If t : T and t! t 0, then t 0 : T. Proof: By induction on the given typing derivation. Case T-True: t = true T = Bool Then t is a value, so it cannot be that t! t 0 for any t 0, and the theorem is vacuously true. 31

Preservation Theorem: If t : T and t! t 0, then t 0 : T. Proof: By induction on the given typing derivation. Case T-If: t = if t 1 then t 2 else t 3 t 1 : Bool t 2 : T t 3 : T There are three evaluation rules by which t! t 0 can be derived: E-IfTrue, E-IfFalse, and E-If. Consider each case separately. 32

Preservation Theorem: If t : T and t! t 0, then t 0 : T. Proof: By induction on the given typing derivation. Case T-If: t = if t 1 then t 2 else t 3 t 1 : Bool t 2 : T t 3 : T There are three evaluation rules by which t! t 0 can be derived: E-IfTrue, E-IfFalse, and E-If. Consider each case separately. Subcase E-IfTrue: t 1 = true t 0 = t 2 Immediate, by the assumption t 2 : T. (E-IfFalse subcase: Similar.) 33

Preservation Theorem: If t : T and t! t 0, then t 0 : T. Proof: By induction on the given typing derivation. Case T-If: t = if t 1 then t 2 else t 3 t 1 : Bool t 2 : T t 3 : T There are three evaluation rules by which t! t 0 can be derived: E-IfTrue, E-IfFalse, and E-If. Consider each case separately. Subcase E-If: t 1! t 0 1 t 0 = if t 0 1 then t 2 else t 3 Applying the IH to the subderivation of t 1 : Bool yields t 0 1 : Bool. Combining this with the assumptions that t 2 : T and t 3 : T, we can apply rule T-If to conclude that if t 0 1 then t 2 else t 3 : T, that is, t 0 : T. 34

Recap: Type Systems I Very successful example of a lightweight formal method I big topic in PL research I enabling technology for all sorts of other things, e.g. language-based security I the skeleton around which modern programming languages are designed 35

The Simply Typed Lambda-Calculus 36

The simply typed lambda-calculus The system we are about to define is commonly called the simply typed lambda-calculus, or! for short. Unlike the untyped lambda-calculus, the pure form of! (with no primitive values or operations) is not very interesting; to talk about!, we always begin with some set of base types. I So, strictly speaking, there are many variants of!, depending on the choice of base types. I For now, we ll work with a variant constructed over the booleans. 37

Untyped lambda-calculus with booleans t ::= terms x variable x.t abstraction t t application true constant true false constant false if t then t else t conditional v ::= values x.t abstraction value true true value false false value 38

Simple Types T ::= types Bool type of booleans T!T types of functions 39

Type Annotations We now have a choice to make. Do we... I annotate lambda-abstractions with the expected type of the argument x:t 1. t 2 (as in most mainstream programming languages), or I continue to write lambda-abstractions as before x. t 2 and ask the typing rules to guess an appropriate annotation (as in OCaml)? Both are reasonable choices, but the first makes the job of defining the typin rules simpler. Let s take this choice for now. 40

Typing rules true : Bool false : Bool t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T (T-True) (T-False) (T-If) 41

Typing rules true : Bool false : Bool t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T??? x:t 1.t 2 : T 1!T 2 (T-True) (T-False) (T-If) (T-Abs) 42

Typing rules true : Bool false : Bool t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T (T-True) (T-False) (T-If), x:t 1 `t 2 : T 2 ` x:t 1.t 2 : T 1!T 2 (T-Abs) x:t 2 `x : T (T-Var) 43

Typing rules `true : Bool `false : Bool `t 1 : Bool `t 2 : T `t 3 : T `if t 1 then t 2 else t 3 : T (T-True) (T-False) (T-If), x:t 1 `t 2 : T 2 ` x:t 1.t 2 : T 1!T 2 (T-Abs) x:t 2 `x : T (T-Var) `t 1 : T 11!T 12 `t 2 : T 11 `t 1 t 2 : T 12 (T-App) 44

Typing Derivations What derivations justify the following typing statements? I ` ( x:bool.x) true : Bool I f:bool!bool ` f (if false then true else false) : Bool I f:bool!bool ` x:bool. f (if x then false else x) : Bool!Bool 45

46

Properties of! The fundamental property of the type system we have just defined is soundness with respect to the operational semantics. 1. Progress: A closed, well-typed term is not stuck If ` t : T, then either t is a value or else t! t 0 for some t 0. 2. Preservation: Types are preserved by one-step evaluation If ` t : T and t! t 0, then ` t 0 : T. 47

Proving progress Same steps as before... 48

Proving progress Same steps as before... I inversion lemma for typing relation I canonical forms lemma I progress theorem 49

Inversion Lemma: 1. If ` true : R, then R = Bool. 2. If ` false : R, then R = Bool. 3. If ` if t 1 then t 2 else t 3 : R, then ` t 1 : Bool and ` t 2, t 3 : R. 50

Inversion Lemma: 1. If ` true : R, then R = Bool. 2. If ` false : R, then R = Bool. 3. If ` if t 1 then t 2 else t 3 : R, then ` t 1 : Bool and ` t 2, t 3 : R. 4. If ` x : R, then 51

Inversion Lemma: 1. If ` true : R, then R = Bool. 2. If ` false : R, then R = Bool. 3. If ` if t 1 then t 2 else t 3 : R, then ` t 1 : Bool and ` t 2, t 3 : R. 4. If ` x : R, then x:r 2. 52

Inversion Lemma: 1. If ` true : R, then R = Bool. 2. If ` false : R, then R = Bool. 3. If ` if t 1 then t 2 else t 3 : R, then ` t 1 : Bool and ` t 2, t 3 : R. 4. If ` x : R, then x:r 2. 5. If ` x:t 1.t 2 : R, then 53

Inversion Lemma: 1. If ` true : R, then R = Bool. 2. If ` false : R, then R = Bool. 3. If ` if t 1 then t 2 else t 3 : R, then ` t 1 : Bool and ` t 2, t 3 : R. 4. If ` x : R, then x:r 2. 5. If ` x:t 1.t 2 : R, then R = T 1!R 2 for some R 2 with, x:t 1 ` t 2 : R 2. 54

Inversion Lemma: 1. If ` true : R, then R = Bool. 2. If ` false : R, then R = Bool. 3. If ` if t 1 then t 2 else t 3 : R, then ` t 1 : Bool and ` t 2, t 3 : R. 4. If ` x : R, then x:r 2. 5. If ` x:t 1.t 2 : R, then R = T 1!R 2 for some R 2 with, x:t 1 ` t 2 : R 2. 6. If ` t 1 t 2 : R, then 55

Inversion Lemma: 1. If ` true : R, then R = Bool. 2. If ` false : R, then R = Bool. 3. If ` if t 1 then t 2 else t 3 : R, then ` t 1 : Bool and ` t 2, t 3 : R. 4. If ` x : R, then x:r 2. 5. If ` x:t 1.t 2 : R, then R = T 1!R 2 for some R 2 with, x:t 1 ` t 2 : R 2. 6. If ` t 1 t 2 : R, then there is some type T 11 such that ` t 1 : T 11!R and ` t 2 : T 11. 56

Canonical Forms Lemma: 57

Canonical Forms Lemma: 1. If v is a value of type Bool, then 58

Canonical Forms Lemma: 1. If v is a value of type Bool, then v is either true or false. 59

Canonical Forms Lemma: 1. If v is a value of type Bool, then v is either true or false. 2. If v is a value of type T 1!T 2, then 60

Canonical Forms Lemma: 1. If v is a value of type Bool, then v is either true or false. 2. If v is a value of type T 1!T 2, then v has the form x:t 1.t 2. 61

Progress Theorem: Suppose t is a closed, well-typed term (that is, ` t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. Proof: By induction 62

Progress Theorem: Suppose t is a closed, well-typed term (that is, ` t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. Proof: By induction on typing derivations. 63

Progress Theorem: Suppose t is a closed, well-typed term (that is, ` t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. Proof: By induction on typing derivations. The cases for boolean constants and conditions are the same as before. The variable case is trivial (because t is closed). The abstraction case is immediate, since abstractions are values. 64

Progress Theorem: Suppose t is a closed, well-typed term (that is, ` t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. Proof: By induction on typing derivations. The cases for boolean constants and conditions are the same as before. The variable case is trivial (because t is closed). The abstraction case is immediate, since abstractions are values. Consider the case for application, where t = t 1 t 2 with ` t 1 : T 11!T 12 and ` t 2 : T 11. 65

Progress Theorem: Suppose t is a closed, well-typed term (that is, ` t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. Proof: By induction on typing derivations. The cases for boolean constants and conditions are the same as before. The variable case is trivial (because t is closed). The abstraction case is immediate, since abstractions are values. Consider the case for application, where t = t 1 t 2 with ` t 1 : T 11!T 12 and ` t 2 : T 11. By the induction hypothesis, either t 1 is a value or else it can make a step of evaluation, and likewise t 2. 66

Progress Theorem: Suppose t is a closed, well-typed term (that is, ` t : T for some T). Then either t is a value or else there is some t 0 with t! t 0. Proof: By induction on typing derivations. The cases for boolean constants and conditions are the same as before. The variable case is trivial (because t is closed). The abstraction case is immediate, since abstractions are values. Consider the case for application, where t = t 1 t 2 with ` t 1 : T 11!T 12 and ` t 2 : T 11. By the induction hypothesis, either t 1 is a value or else it can make a step of evaluation, and likewise t 2. If t 1 can take a step, then rule E-App1 applies to t. If t 1 is a value and t 2 can take a step, then rule E-App2 applies. Finally, if both t 1 and t 2 are values, then the canonical forms lemma tells us that t 1 has the form x:t 11.t 12, and so rule E-AppAbs applies to t. 67

Preservation Theorem: If ` t : T and t! t 0, then ` t 0 : T. Proof: By induction 68

Preservation Theorem: If ` t : T and t! t 0, then ` t 0 : T. Proof: By induction on typing derivations. Which case is the hard one?? 69

Preservation Theorem: If ` t : T and t! t 0, then ` t 0 : T. Proof: By induction on typing derivations. Case T-App: Given t = t 1 t 2 `t 1 : T 11!T 12 `t 2 : T 11 T = T 12 Show ` t 0 : T 12 70

Preservation Theorem: If ` t : T and t! t 0, then ` t 0 : T. Proof: By induction on typing derivations. Case T-App: Given t = t 1 t 2 `t 1 : T 11!T 12 `t 2 : T 11 T = T 12 Show ` t 0 : T 12 By the inversion lemma for evaluation, there are three subcases... 71

Preservation Theorem: If ` t : T and t! t 0, then ` t 0 : T. Proof: By induction on typing derivations. Case T-App: Given t = t 1 t 2 `t 1 : T 11!T 12 `t 2 : T 11 T = T 12 Show ` t 0 : T 12 By the inversion lemma for evaluation, there are three subcases... Subcase: t 1 = x:t 11. t 12 t 2 a value v 2 t 0 =[x 7! v 2 ]t 12 72

Preservation Theorem: If ` t : T and t! t 0, then ` t 0 : T. Proof: By induction on typing derivations. Case T-App: Given t = t 1 t 2 `t 1 : T 11!T 12 `t 2 : T 11 T = T 12 Show ` t 0 : T 12 By the inversion lemma for evaluation, there are three subcases... Subcase: t 1 = x:t 11. t 12 t 2 a value v 2 t 0 =[x 7! v 2 ]t 12 Uh oh. 73

The Substitution Lemma Lemma: Types are preserved under substitition. That is, if, x:s ` t : T and ` s : S, then ` [x 7! s]t : T. 74

The Substitution Lemma Lemma: Types are preserved under substitition. That is, if, x:s ` t : T and ` s : S, then ` [x 7! s]t : T. Proof:... 75

Preservation Recommended: Complete the proof of preservation 76