# Programming Languages Fall 2014

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Programming Languages Fall 2014 Lecture 7: Simple Types and Simply-Typed Lambda Calculus Prof. Liang Huang 1

2 Types stuck terms? how to fix it? 2

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 Properties of the Typing Relation 12

13 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

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. 14

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:... 15

16 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

17 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

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: 18

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, 19

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. 20

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. 21

22 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

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. 23

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: 24

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. 25

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. 26

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 27

28 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

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

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

31 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

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. 32

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-IfTrue: t 1 = true t 0 = t 2 Immediate, by the assumption t 2 : T. (E-IfFalse subcase: Similar.) 33

34 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

35 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

36 The Simply Typed Lambda-Calculus 36

37 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

38 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

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

40 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

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 (T-True) (T-False) (T-If) 41

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??? x:t 1.t 2 : T 1!T 2 (T-True) (T-False) (T-If) (T-Abs) 42

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) 43

44 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

45 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 46

47 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 Preservation: Types are preserved by one-step evaluation If ` t : T and t! t 0, then ` t 0 : T. 47

48 Proving progress Same steps as before... 48

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

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. 50

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 51

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. 52

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 If ` x:t 1.t 2 : R, then 53

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 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

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 If ` x:t 1.t 2 : R, then R = T 1!R 2 for some R 2 with, x:t 1 ` t 2 : R If ` t 1 t 2 : R, then 55

56 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 If ` x:t 1.t 2 : R, then R = T 1!R 2 for some R 2 with, x:t 1 ` t 2 : R If ` t 1 t 2 : R, then there is some type T 11 such that ` t 1 : T 11!R and ` t 2 : T

57 Canonical Forms Lemma: 57

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

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

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 60

61 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

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 62

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. 63

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. 64

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

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. 66

67 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

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

69 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

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 70

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... 71

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 72

73 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

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. 74

75 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

76 Preservation Recommended: Complete the proof of preservation 76

### CIS 500 Software Foundations Midterm I

CIS 500 Software Foundations Midterm I October 11, 2006 Name: Student ID: Email: Status: Section: registered for the course not registered: sitting in to improve a previous grade not registered: just taking

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

CIS 500 Software Foundations Midterm I Answer key October 8, 2003 Inductive Definitions Review: Recall that the function size, which calculates the total number of nodes in the abstract syntax tree of

### Lecture 9: Typed Lambda Calculus

Advanced Topics in Programming Languages Spring Semester, 2012 Lecture 9: Typed Lambda Calculus May 8, 2012 Lecturer: Mooly Sagiv Scribe: Guy Golan Gueta and Shachar Itzhaky 1 Defining a Type System for

### Fundamental Concepts. Chapter 1

Chapter 1 Fundamental Concepts This book is about the mathematical foundations of programming, with a special attention on computing with infinite objects. How can mathematics help in programming? There

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

Lambda Calculus Type Systems, Lectures 3 Jevgeni Kabanov Tartu, 13.02.2006 PREVIOUSLY ON TYPE SYSTEMS Arithmetical expressions and Booleans Evaluation semantics Normal forms & Values Getting stuck Safety

### CIS 500 Software Foundations Fall September 25

CIS 500 Software Foundations Fall 2006 September 25 The Lambda Calculus The lambda-calculus If our previous language of arithmetic expressions was the simplest nontrivial programming language, then the

### More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Concepts in Programming Languages Recitation 6: More Untyped Lambda Calculus & Simply Typed Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky,

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

Review CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics e ::= λx. e x ee c v ::= λx. e c (λx. e) v e[v/x] e 1 e 2 e 1 e 2 τ ::= int τ τ Γ ::= Γ,x : τ e 2 e 2 ve 2 ve 2 e[e /x]:

### Computing Fundamentals 2 Introduction to CafeOBJ

Computing Fundamentals 2 Introduction to CafeOBJ Lecturer: Patrick Browne Lecture Room: K408 Lab Room: A308 Based on work by: Nakamura Masaki, João Pascoal Faria, Prof. Heinrich Hußmann. See notes on slides

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

Lecture Outline Global Optimization Global flow analysis Global constant propagation Liveness analysis Compiler Design I (2011) 2 Local Optimization Recall the simple basic-block optimizations Constant

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Lambda Calculus Encodings CMSC 330 Spring 2017 1 Review A lambda calculus expression is defined as e ::= x variable λx.e abstraction (fun def) e e application

### Week 5 Tutorial Structural Induction

Department of Computer Science, Australian National University COMP2600 / COMP6260 Formal Methods in Software Engineering Semester 2, 2016 Week 5 Tutorial Structural Induction You should hand in attempts

### 3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

3.4 Deduction and Evaluation: Tools 3.4.1 Conditional-Equational Logic The general definition of a formal specification from above was based on the existence of a precisely defined semantics for the syntax

### Programming with Math and Logic

.. Programming with Math and Logic an invitation to functional programming Ed Morehouse Wesleyan University The Plan why fp? terms types interfaces The What and Why of Functional Programming Computing

### Lecture 6: Sequential Sorting

15-150 Lecture 6: Sequential Sorting Lecture by Dan Licata February 2, 2012 Today s lecture is about sorting. Along the way, we ll learn about divide and conquer algorithms, the tree method, and complete

### More Lambda Calculus and Intro to Type Systems

More Lambda Calculus and Intro to Type Systems Plan Heavy Class Participation Thus, wake up! Lambda Calculus How is it related to real life? Encodings Fixed points Type Systems Overview Static, Dyamic

### λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Course 2D1453, 2006-07 Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Some material from B. Pierce: TAPL + some from G. Klein, NICTA Alonzo Church, 1903-1995 Church-Turing thesis First

### Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

### Polymorphism. Lecture 19 CS 565 4/17/08

Polymorphism Lecture 19 CS 565 4/17/08 The Limitations of F 1 (simply-typed λ- calculus) In F 1 each function works exactly for one type Example: the identity function id = λx:τ. x : τ τ We need to write

### Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

### Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Formal Semantics Aspects to formalize Why formalize? some language features are tricky, e.g. generalizable type variables, nested functions some features have subtle interactions, e.g. polymorphism and

### Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

### Induction for Data Types

Induction for Data Types COMP1600 / COMP6260 Dirk Pattinson Australian National University Semester 2, 2017 Catch Up / Drop in Lab When Fridays, 15.00-17.00 Where N335, CSIT Building (bldg 108) Until the

### the application rule M : x:a: B N : A M N : (x:a: B) N and the reduction rule (x: A: B) N! Bfx := Ng. Their algorithm is not fully satisfactory in the

The Semi-Full Closure of Pure Type Systems? Gilles Barthe Institutionen for Datavetenskap, Chalmers Tekniska Hogskola, Goteborg, Sweden Departamento de Informatica, Universidade do Minho, Braga, Portugal

### Modules and Representation Invariants

Modules and Representation Invariants COS 326 Andrew W. Appel Princeton University slides copyright 2013-2015 David Walker and Andrew W. Appel In previous classes: Reasoning about individual OCaml expressions.

### Combining Programming with Theorem Proving

Combining Programming with Theorem Proving Chiyan Chen and Hongwei Xi Boston University Programming with Theorem Proving p.1/27 Motivation for the Research To support advanced type systems for practical

### CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CS 3110 Lecture 6: Map and Fold Prof. Clarkson Spring 2015 Today s music: Selections from the soundtrack to 2001: A Space Odyssey Review Course so far: Syntax and semantics of (most of) OCaml Today: No

### More Lambda Calculus and Intro to Type Systems

#1 More Lambda Calculus and Intro to Type Systems #2 Plan Heavy Class Participation Thus, wake up! (not actually kidding) Lambda Calculus How is it related to real life? Encodings Fixed points Type Systems

### A formal derivation of an executable Krivine machine

A formal derivation of an executable Krivine machine Wouter Swierstra Universiteit Utrecht IFIP WG 2.1 Meeting 68; Rome, Italy 1 β reduction (λx. t0) t1 t0 {t1/x} 2 Motivation Implementing β-reduction

### Beluga: A Framework for Programming and Reasoning with Deductive Systems (System Description)

Beluga: A Framework for Programming and Reasoning with Deductive Systems (System Description) Brigitte Pientka and Joshua Dunfield McGill University, Montréal, Canada {bpientka,joshua}@cs.mcgill.ca Abstract.

### (How Not To Do) Global Optimizations

(How Not To Do) Global Optimizations #1 One-Slide Summary A global optimization changes an entire method (consisting of multiple basic blocks). We must be conservative and only apply global optimizations

### On Meaning Preservation of a Calculus of Records

On Meaning Preservation of a Calculus of Records Emily Christiansen and Elena Machkasova Computer Science Discipline University of Minnesota, Morris Morris, MN 56267 chri1101, elenam@morris.umn.edu Abstract

### Part II. Hoare Logic and Program Verification. Why specify programs? Specification and Verification. Code Verification. Why verify programs?

Part II. Hoare Logic and Program Verification Part II. Hoare Logic and Program Verification Dilian Gurov Props: Models: Specs: Method: Tool: safety of data manipulation source code logic assertions Hoare

### 0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

0.1 Welcome http://cs161.stanford.edu My contact info: Jessica Su, jtysu at stanford dot edu, office hours Monday 3-5 pm in Huang basement TA office hours: Monday, Tuesday, Wednesday 7-9 pm in Huang basement

### Introduction to Functional Programming in Haskell 1 / 56

Introduction to Functional Programming in Haskell 1 / 56 Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order

### Second-Order Type Systems

#1 Second-Order Type Systems Homework 5 Summary Student : 37.9704 Student : 44.4466 ORIGINAL : 50.2442 Student : 50.8275 Student : 50.8633 Student : 50.9181 Student : 52.1347 Student : 52.1633 Student

### The Polymorphic Blame Calculus and Parametricity

1 / 31 The Polymorphic Blame Calculus and Parametricity Jeremy G. Siek Indiana University, Bloomington University of Strathclyde August 2015 2 / 31 Integrating static and dynamic typing Static Dynamic

### Introduction to the λ-calculus

Announcements Prelim #2 issues: o Problem 5 grading guide out shortly o Problem 3 (hashing) issues Will be on final! Friday RDZ office hours are 11-12 not 1:30-2:30 1 Introduction to the λ-calculus Today

### CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

CS 3512, Spring 2011 Instructor: Doug Dunham Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010 Prerequisites: Calc I, CS2511 Rough course outline:

### Type Checking and Type Inference

Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled

### CMSC 22100/32100: Programming Languages Lecture Notes M. Blume December 5, 2008

CMSC 22100/32100: Programming Languages Lecture Notes M. Blume December 5, 2008 1 Introduction What are programming languages? Languages: formal languages, i.e., sets of strings Programming: languages

### Abstract Interpretation

Abstract Interpretation Ranjit Jhala, UC San Diego April 22, 2013 Fundamental Challenge of Program Analysis How to infer (loop) invariants? Fundamental Challenge of Program Analysis Key issue for any analysis

### From Math to Machine A formal derivation of an executable Krivine Machine Wouter Swierstra Brouwer Seminar

From Math to Machine A formal derivation of an executable Krivine Machine Wouter Swierstra Brouwer Seminar β reduction (λx. t0) t1 t0 {t1/x} Substitution Realizing β-reduction through substitution is a

### Boolean Algebra & Digital Logic

Boolean Algebra & Digital Logic Boolean algebra was developed by the Englishman George Boole, who published the basic principles in the 1854 treatise An Investigation of the Laws of Thought on Which to

### COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

COMP 1130 Lambda Calculus based on slides by Jeff Foster, U Maryland Motivation Commonly-used programming languages are large and complex ANSI C99 standard: 538 pages ANSI C++ standard: 714 pages Java

### CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CS 3110 Lecture 6: Map and Fold Prof. Clarkson Fall 2014 Today s music: Selections from the soundtrack to 2001: A Space Odyssey Review Features so far: variables, operators, let expressions, if expressions,

### Using Vampire in Soundness Proofs of Type Systems

Sylvia Grewe 1, Sebastian Erdweg 1, and Mira Mezini 1,2 1 TU Darmstadt, Germany 2 Lancaster University, UK Abstract Type systems for programming languages shall detect type errors in programs before runtime.

### Intro. Scheme Basics. scm> 5 5. scm>

Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

### Once Upon a Polymorphic Type

Once Upon a Polymorphic Type Keith Wansbrough Computer Laboratory University of Cambridge kw217@cl.cam.ac.uk http://www.cl.cam.ac.uk/users/kw217/ Simon Peyton Jones Microsoft Research Cambridge 20 January,

### The Untyped Lambda Calculus

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in... paste or by selection, annotation, or rewording. [Järvi] is in turn

### Propositional Logic Formal Syntax and Semantics. Computability and Logic

Propositional Logic Formal Syntax and Semantics Computability and Logic Syntax and Semantics Syntax: The study of how expressions are structured (think: grammar) Semantics: The study of the relationship

### Hoare Logic: Proving Programs Correct

Hoare Logic: Proving Programs Correct 17-654/17-765 Analysis of Software Artifacts Jonathan Aldrich Reading: C.A.R. Hoare, An Axiomatic Basis for Computer Programming Some presentation ideas from a lecture

### 15 212: Principles of Programming. Some Notes on Continuations

15 212: Principles of Programming Some Notes on Continuations Michael Erdmann Spring 2011 These notes provide a brief introduction to continuations as a programming technique. Continuations have a rich

### CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

Lambda Calculus CSE 505 Lecture #9 October 1, 2012 Expr ::= Var λ Var. Expr (Expr Expr) Key Concepts: Bound and Free Occurrences Substitution, Reduction Rules: α, β, η Confluence and Unique Normal Form

### Reasoning About Programs Panagiotis Manolios

Reasoning About Programs Panagiotis Manolios Northeastern University April 2, 2016 Version: 95 Copyright c 2016 by Panagiotis Manolios All rights reserved. We hereby grant permission for this publication

### COUNTING THE NUMBER OF WINNING BINARY STRINGS IN THE 1-DIMENSIONAL SAME GAME. Department of Mathematics Oberlin College Oberlin OH 44074

COUNTING THE NUMBER OF WINNING BINARY STRINGS IN THE 1-DIMENSIONAL SAME GAME CHRIS BURNS AND BENJAMIN PURCELL Department of Mathematics Oberlin College Oberlin OH 44074 Abstract. Ralf Stephan recently

### 4 Programming with Types

4 Programming with Types 4.1 Polymorphism We ve been working a lot with lists of numbers, but clearly programs also need to be able to manipulate lists whose elements are drawn from other types lists of

### Sound Type-Dependent Syntactic Language Extension

Sound Type-Dependent Syntactic Language Extension Florian Lorenzen TU Berlin, Germany Sebastian Erdweg TU Darmstadt, Germany Abstract Syntactic language extensions can introduce new facilities into a programming

### Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Types for References, Exceptions and Continuations Annoucements How s the midterm going? Meeting 21, CSCI 5535, Spring 2009 2 One-Slide Summary Review of Subtyping If τ is a subtype of σ then any expression

### SOFTWARE VERIFICATION AND COMPUTER PROOF (lesson 1) Enrico Tassi Inria Sophia-Antipolis

SOFTWARE VERIFICATION AND COMPUTER PROOF (lesson 1) Enrico Tassi Inria Sophia-Antipolis Who am I? 1. I'm a researcher at Inria 2. I work on proof assistants, the kind of tools that we will be using for

### Part III. Static and Dynamic Semantics

Part III Static and Dynamic Semantics Chapter 9 Static Semantics Most programming languages exhibit a phase distinction between the static and dynamic phases of processing. The static phase consists of

### Coq Summer School, Session 2 : Basic programming with numbers and lists. Pierre Letouzey

Coq Summer School, Session 2 : Basic programming with numbers and lists Pierre Letouzey Predened data structures Predened types are actually declared to Coq at load time 1 : Inductive bool := true false.

### Lecture Notes on Karger s Min-Cut Algorithm. Eric Vigoda Georgia Institute of Technology Last updated for Advanced Algorithms, Fall 2013.

Lecture Notes on Karger s Min-Cut Algorithm. Eric Vigoda Georgia Institute of Technology Last updated for 4540 - Advanced Algorithms, Fall 2013. Today s topic: Karger s min-cut algorithm [3]. Problem Definition

### CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

CS 4110 Programming Languages & Logics Lecture 27 Recursive Types 4 November 2016 Announcements 2 My office hours are at the normal time today but canceled on Monday Guest lecture by Seung Hee Han on Monday

### 1 Variations of the Traveling Salesman Problem

Stanford University CS26: Optimization Handout 3 Luca Trevisan January, 20 Lecture 3 In which we prove the equivalence of three versions of the Traveling Salesman Problem, we provide a 2-approximate algorithm,

### Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction Readings: HtDP, sections 19-24. Language level: Intermediate Student With Lambda different order used in lecture section 24 material introduced much earlier sections 22, 23 not covered

### Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

CSE 130 : Fall 2011 Recap from last time Programming Languages Lecture 3: Data Types Ranjit Jhala UC San Diego 1 2 A shorthand for function binding Put it together: a filter function # let neg = fun f

### 7. Introduction to Denotational Semantics. Oscar Nierstrasz

7. Introduction to Denotational Semantics Oscar Nierstrasz Roadmap > Syntax and Semantics > Semantics of Expressions > Semantics of Assignment > Other Issues References > D. A. Schmidt, Denotational Semantics,

### The Untyped Lambda Calculus

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in... paste or by selection, annotation, or rewording. [Järvi] is in turn

### Exercises on Semantics of Programming Languages

Technische Universität Wien SS 2014 Fakultät für Informatik Repetition sheet Assist. Prof. Florian Zuleger Tuesday, 8 April 2014 Assist. Prof. Georg Weissenbacher Univ. Prof. Agata Ciabattoni Moritz Sinn,

### COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

COMP80 Programming Languages Slides Courtesy of Prof. Sam Guyer Lambda Calculus Formal system with three parts Notation for functions Proof system for equations Calculation rules called reduction Idea:

### COSE212: Programming Languages. Lecture 0 Course Overview

COSE212: Programming Languages Lecture 0 Course Overview Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 0 September 4, 2017 1 / 9 Basic nformation nstructor: Hakjoo Oh TAs: Position: Assistant

### Type Inference: Constraint Generation

Type Inference: Constraint Generation sk and dbtucker 2002-11-11 1 Inferring Types We ve seen the value of having explicit polymorphism in our language it lets us write programs that work on may different

### 9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Dr A Sahu Dept of Computer Science & Engineering IIT Guwahati Why study? Lambda calculus Syntax Evaluation Relationship to programming languages Church Rosser theorem Completeness of Lambda Calculus: Turing

### A Correctness Proof for a Practical Byzantine-Fault-Tolerant Replication Algorithm

Appears as Technical Memo MIT/LCS/TM-590, MIT Laboratory for Computer Science, June 1999 A Correctness Proof for a Practical Byzantine-Fault-Tolerant Replication Algorithm Miguel Castro and Barbara Liskov

### Introduction to System F. Lecture 18 CS 565 4/20/09

Introduction to System F Lecture 18 CS 565 4/20/09 The Limitations of F 1 (simply-typed λ- calculus) In F 1 each function works exactly for one type Example: the identity function id = λx:τ. x : τ τ We

### Dependent Types and Program Equivalence

Dependent Types and Program Equivalence Limin Jia Jianzhou Zhao Vilhelm Sjöberg Stephanie Weirich Computer and Information Sciences Department, University of Pennsylvania {liminjia,jianzhou,vilhelm,sweirich}@cis.upenn.edu

### Chapter 5: The Untyped Lambda Calculus

Chapter 5: The Untyped Lambda Calculus What is lambda calculus for? Basics: syntax and operational semantics Programming in the Lambda Calculus Formalities (formal definitions) What is Lambda calculus

### Agenda. We ve done Growth of functions Asymptotic Notations (O, o,,!, ) Now Binary search as an example Recurrence relations, solving them

Agenda We ve done Growth of functions Asymptotic Notations (O, o,,!, ) Now Binary search as an example Recurrence relations, solving them c Hung Q. Ngo (SUNY at Buffalo) CSE 250 Data Structures in C++

### λ calculus is inconsistent

Content Rough timeline COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Gerwin Klein, June Andronick, Toby Murray λ Intro & motivation, getting started [1] Foundations & Principles

### Topic 3: Propositions as types

Topic 3: Propositions as types May 18, 2014 Propositions as types We have seen that the main mathematical objects in a type theory are types. But remember that in conventional foundations, as based on

### The Typed λ Calculus and Type Inferencing in ML

Notes on Types S. Arun-Kumar Department of Computer Science and Engineering Indian Institute of Technology New Delhi, 110016 email: sak@cse.iitd.ernet.in April 14, 2002 2 Chapter 1 The Typed λ Calculus

### Recursive Functions. Recursive functions are built up from basic functions by some operations.

Recursive Functions Recursive functions are built up from basic functions by some operations. The Successor Function Let s get very primitive. Suppose we have 0 defined, and want to build the nonnegative

### Scoping rules match identifier uses with identifier definitions. A type is a set of values coupled with a set of operations on those values.

#1 Type Checking Previously, in PL Scoping rules match identifier uses with identifier definitions. A type is a set of values coupled with a set of operations on those values. A type system specifies which

### Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Recap Epressions (Synta) Compile-time Static Eec-time Dynamic Types (Semantics) Recap Integers: +,-,* floats: +,-,* Booleans: =,

### Infinite Objects and Proofs 1

Infinite Objects and Proofs 1 Pierre Castéran Beijing, August 2009 1 This lecture corresponds to the chapter 13 : Infinite Objects and Proofs of the book. In this lecture, we shall see how to represent

### Dijkstra s algorithm for shortest paths when no edges have negative weight.

Lecture 14 Graph Algorithms II 14.1 Overview In this lecture we begin with one more algorithm for the shortest path problem, Dijkstra s algorithm. We then will see how the basic approach of this algorithm

### Lists. Michael P. Fourman. February 2, 2010

Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a built-in ML type constructor. However, to introduce

### Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type

### School of Computer & Communication Sciences École Polytechnique Fédérale de Lausanne

Principles of Dependable Systems Building Reliable Software School of Computer & Communication Sciences École Polytechnique Fédérale de Lausanne Winter 2006-2007 Outline Class Projects: mtgs next week

### Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

Graph fundamentals Bipartite graph characterization Lemma. If a graph contains an odd closed walk, then it contains an odd cycle. Proof strategy: Consider a shortest closed odd walk W. If W is not a cycle,

### Concepts of program design Exam January 31, 13:30 16:30

Concepts of program design 2016 2017 Exam January 31, 13:30 16:30 Name: Student number: Please read the following instructions carefully: Fill in your name and student number above. Be prepared to identify

### n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

Questions 1. Use mathematical induction to prove that, for any n 1 n 1 x i = xn 1 x 1. (Recall that I proved it in a different way back in lecture 2.) 2. Use mathematical induction to prove that, for all

### Program verification. Generalities about software Verification Model Checking. September 20, 2016

Program verification Generalities about software Verification Model Checking Laure Gonnord David Monniaux September 20, 2016 1 / 43 The teaching staff Laure Gonnord, associate professor, LIP laboratory,

### Types, Type Inference and Unification

Types, Type Inference and Unification Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Cornell CS 6110 Summary (Functional Programming) Lambda Calculus Basic ML Advanced ML: Modules, References,

### We assume uniform hashing (UH):

We assume uniform hashing (UH): the probe sequence of each key is equally likely to be any of the! permutations of 0,1,, 1 UH generalizes the notion of SUH that produces not just a single number, but a

### Supplementary Notes on Recursive Types

Supplementary Notes on Recursive Types 15-312: Foundations of Programming Languages Frank Pfenning Lecture 13 October 8, 2002 In the last two lectures we have seen two critical concepts of programming