Concepts in Programming Languages Recitation 6: Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto ) Reference: Types and Programming Languages by Benjamin C. Pierce, Chapter 5 1
Untyped Lambda Calculus - Synta t ::=. t t t terms variable abstraction application Terms can be represented as abstract synta trees Syntactic Conventions: Applications associates to left : e 1 e 2 e 3 (e 1 e 2 ) e 3 The body of abstraction etends as far as possible:. y. y. ( y. (( y) )) 2
Free vs. Bound Variables An occurrence of in t is bound in. t otherwise it is free is a binder FV: t P(Var) is the set free variables of t FV() = {} FV(. t) = FV(t) {} FV (t 1 t 2 ) = FV(t 1 ) FV(t 2 ) 3
Semantics: Substitution, β-reduction, α-conversion Substitution [ s] = s [ s] y = y [ s] ( y. t 1 ) = y. [ s] t 1 if y if y and y FV(s) [ s] (t 1 t 2 ) = ([ s] t 1 ) ([ s] t 2 ) -reduction (. t 1 ) t 2 [ t 2 ] t 1 α-conversion (. t) y. [ y] t if y FV(t) 4
Eamples of β-reduction, α-conversion (. ) y y (. (. ) ) (u r) + u r (. ) ( ( w. w)) (y z) w. y z w (. (. )) y (. ( y. )) y (. ( z. z)) y (. ( z. )) y z. z z. y 5
Non-Deterministic Operational Semantics (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 t t. t. t (E-Abs) t 1 t 1 t 1 t 2 t 1 t 2 t 2 t 2 (E-App 1 ) (E-App 2 ) t 1 t 2 t 1 t 2 Why is this semantics non-deterministic? 6
Different Evaluation Orders (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 t t. t. t (E-Abs) t 1 t 1 t 2 t 2 (E-App 1 ) (E-App 2 ) t 1 t 2 t 1 t 2 t 1 t 2 t 1 t 2 (. (add )) (add 2 3) (. (add )) (5) add 5 5 10 (. (add )) (add 2 3) (add (add 2 3) (add 2 3)) (add 5 (add 2 3)) (add 5 5) 10 This eample: same final result but lazy performs more computations 7
Different Evaluation Orders (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 t t. t. t (E-Abs) t 1 t 1 t 2 t 2 (E-App 1 ) (E-App 2 ) t 1 t 2 t 1 t 2 t 1 t 2 t 1 t 2 (. y. ) 3 (div 5 0) Eception: Division by zero (. y. ) 3 (div 5 0) ( y. 3) (div 5 0) 3 This eample: lazy suppresses erroneous division and reduces to final result Can also suppress non-terminating computation. Many times we want this, for eample: if i < len(a) and a[i]==0: print found zero 8
Strict Lazy Normal Order (E-App 1 ) t 1 t 1 t 1 t 2 t 1 t 2 precedence (E-App 2 ) t 2 t 2 t 1 t 2 t 1 t 2 precedence (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 (E-App 1 ) t 1 t 1 t 1 t 2 t 1 t 2 (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 precedence (E-App 1 ) t 1 t 1 t 1 t 2 t 1 t 2 precedence (E-App 2 ) t 2 t 2 t 1 t 2 t 1 t 2 (E-Abs) t t. t. t 9
Call-by-value Operations Semantics via Inductive Definition (no precedence) t ::= terms v ::=. t abstraction values variable. t abstraction t t application (. t 1 ) v 2 [ v 2 ] t 1 (E-AppAbs) t 1 t 1 t 1 t 2 t 1 t 2 (E-APPL1) t 2 t 2 v 1 t 2 v 1 t 2 (E-APPL2) 10
Different Evaluation Orders - Eample (. y. ( z. z) y) (( u. u) ( w. w)) y u w u w z y z 11
Call By Value (. y. ( z. z) y) (( u. u) ( w. w)) y y u w y w w z y u w z z y z y z z 12
Call By Name (Lazy) (. y. ( z. z) y) (( u. u) ( w. w)) y y u w z y u w z z y z 13
Normal Order (. y. ( z. z) y) (( u. u) ( w. w)) y y u w z y y y u w z z y z 14
Divergence (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.( )) (.( )) 15
Divergence (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.( )) (.( )) 16
Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y y 17
Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y y 18
Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) def f(): while True: pass y def g(): return 2 print g(f()) 19
Summary: Orders of Evaluation Full-beta-reduction, Non-deterministic semantics All possible orders Call by value, Eager, Strict, Applicative order Left to right Fully evaluate arguments before function Normal order The leftmost, outermost rede is always reduced first Call by name, Lazy Evaluate arguments as needed Call by need Evaluate arguments as needed and store for subsequent usages Implemented in Haskell 20
Church Rosser Theorem If: a * b, a * c then there eists d such that: b * d, and c * d b a c d 21
Normal Form & Halting Problem A term is in normal form if it is stuck in normal order semantics Under normal order every term either: Reduces to normal form, or Reduces infinitely For a given term, it is undecidable to decide which is the case 22
Combinators A combinator is a function in the Lambda Calculus having no free variables Eamples. is a combinator. y. ( y) is a combinator. y. ( z) is not a combinator Combinators can serve nicely as modular building blocks for more comple epressions The Church numerals and simulated Booleans are eamples of useful combinators 23
Iteration in Lambda Calculus omega = (. ) (. ) (. ) (. ) (. ) (. ) Y = f. (. f ( )) (. f ( )) Z = f. (. f ( y. y)) (. f ( y. y)) Recursion can be simulated Y only works with call-by-name semantics Z works with call-by-value semantics Defining factorial: g = f. n. if n==0 then 1 else (n * (f (n - 1))) fact = Y g (for call-by-name) fact = Z g (for call-by-value) 24
Y-Combinator in action (lazy) g = f. n. if n==0 then 1 else (n * (f (n - 1))) Y = f. (. f ( )) (. f ( )) Y g v = ( f. (. f ( )) (. f ( ))) g v ((. g ( )) (. g ( ))) v (g ((. g ( )) (. g ( ))) ) v ~ (g (Y g)) v What happens to Y in strict semantics? 25
Z-Combinator in action (strict) g = f. n. if n==0 then 1 else (n * (f (n - 1))) Z = f. (. f ( y. y)) (. f ( y. y)) Z g v = ( f. (. f ( y. y)) (. f ( y. y))) g v ((. g ( y. y)) (. g ( y. y))) v (g ( y. (. g ( y. y)) (. g ( y. y)) y)) v ~ (g ( y. (Z g) y) ) v ~ (g (Z g)) v def f1(y): return f2(y) 26
Simulating laziness like Z-Combinator def f(): if ask_user("wanna see it?"): print def g(, y, z): # very epensive computation without side effects def main(): # compute a, b, c with side effects f(g(a, b, c)) In strict semantics, the above code computes g anyway Lazy will avoid it How can achieve this in a strict programming language? 27
Simulating laziness like Z-Combinator def f(): if ask_user(?"): print def g(, y, z): # epensive def main(): # compute a, b, c f(g(a, b, c)) def f(): if ask_user("?"): print () def g(, y, z): # epensive def main(): # compute a, b, c f(lambda: g(a, b, c)) Z = f. (. f ( y. y)) (. f ( y. y)) (E-Abs) t t. t. t 28
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages
Simple Types T ::= Bool T T types type of Booleans type of functions T 1 T 2 T 3 = T 1 ( T 2 T 3 ) 30
Simple Typed Lambda Calculus t ::= : T. t t t terms variable abstraction application T::= Bool T T types atomic type for Booleans types of functions Tying Relation Type fact: t:t means term t has type T (e.g. 1+3:Int, f:int Int) Typing relation : relates sets of type facts and type facts t:t means under (contet, environment), term t has type T t:t means term t has type T under the empty environment (no assumptions) Can t have free variables? 31
Typing Relation Eamples Type fact: t:t means term t has type T (e.g. 1+3:Int, f:int Int) t:t means under (contet, environment), term t has type T t:t means closed term t has type T under the empty environment Eamples :Int, y:int +y :? :Int, y:bool +y :? :Int +y :? :Int, y:int, b:bool if b then else y :? :Int, y:bool, b:bool if b then else y :? :Int, b:bool if b then else y :? :Int, f:int Bool f :? :Int, f: Int Int Bool f :? :Int, f: Int Int Bool f :? f: Int Int Bool f :? 1+2 :? true :? :Int, y:int, f: Int Int Bool 1+2 :? 32
Formally Defining t ::= terms variable : T : T (T-VAR) : T. t t t abstraction application, : T 1 t 2 : T 2 : T 1. t 2 : T 1 T 2 (T-ABS) T::= types t 1 : T 11 T 12 t 2 : T 11 t 1 t 2 : T 12 (T-APP) T T types of functions ::= contet empty contet, : T term variable binding 33
Adding Booleans t ::= terms variable : T. t abstraction t t application true constant true false constant false if t then t else t conditional T::= types Bool type of Booleans T T types of functions ::= contet empty contet, : T term variable binding : T : T, : T 1 t 2 : T 2 : T 1. t 2 : T 1 T 2 t 1 : T 11 T 12 t 2 : T 11 t 1 t 2 : T 12 true : Bool false : Bool t 1 :Bool t 2 :T t 3 :T if t 1 then t 2 else t 3 : T (T-VAR) (T-ABS) (T-APP) (T-TRUE) (T-FALSE) (T-IF) 34
Eample Typing Tree T-VAR f:bool Bool, :Bool :Bool T-VAR f:bool Bool, :Bool f:bool Bool T-FALSE T-VAR T-APP f:bool Bool, :Bool false:bool f:bool Bool, :Bool :Bool f:bool Bool, :Bool f : Bool T-IF T-ABS f:bool Bool, :Bool if false then else f : Bool f:bool Bool :Bool if false then else f : Bool Bool : T : T (T-VAR) t 1 : T 11 T 12 t 2 : T 11 t 1 t 2 : T 12 (T-APP), : T 1 t 2 : T 2 : T 1. t 2 : T 1 T 2 (T-ABS) t 1 :Bool t 2 :T t 3 :T if t 1 then t 2 else t 3 : T (T-IF) false : Bool (T-FALSE) 35
Curry-Howard Isomorphism Beautiful connection between type systems (PL) and proof systems (logic) 36
Retrospect
Natural Operational Semantics is defined inductively using inference rules, with both syntactic conditions on S and semantic conditions on s 38
Structural Operational Semantics is defined inductively using inference rules, with both syntactic conditions on S and semantic conditions on s 39
-Calculus: Non-Deterministic Operational Semantics (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 t t. t. t (E-Abs) t 1 t 1 t 1 t 2 t 1 t 2 t 2 t 2 (E-App 1 ) (E-App 2 ) t 1 t 2 t 1 t 2 40
-Calculus: Lazy Evaluation Operational Semantics (E-AppAbs) (. t 1 ) t 2 [ t 2 ] t 1 (E-App 1 ) t 1 t 1 t 1 t 2 t 1 t 2 41
-Calculus: Call-by-Value Operational Semantics (E-AppAbs) (. t 1 ) v 2 [ v 2 ] t 1 t 1 t 1 t 1 t 2 t 1 t 2 t 2 t 2 (E-App 1 ) (E-App 2 ) v 1 t 2 v 1 t 2 42
Simply Typed -Calculus t ::= terms variable : T. t abstraction t t application true constant true false constant false if t then t else t conditional T::= types Bool type of Booleans T T types of functions ::= contet empty contet, : T term variable binding : T : T, : T 1 t 2 : T 2 : T 1. t 2 : T 1 T 2 t 1 : T 11 T 12 t 2 : T 11 t 1 t 2 : T 12 true : Bool false : Bool t 1 :Bool t 2 :T t 3 :T if t 1 then t 2 else t 3 : T (T-VAR) (T-ABS) (T-APP) (T-TRUE) (T-FALSE) (T-IF) 43
Retrospect Conclusion: For the past 7 weeks we have been formalizing programming languages Synta, Semantics, and Type Rules using Inductive Definitions Q: What s the difference between a mathematician and a computer scientist? A: The mathematician sometimes proves theorems without induction