Concepts in Programming Languages Recitation 5: Untyped 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
Computation Models Turing Machines Wang Machines Counter Programs Lambda Calculus 2
Historical Contet Like Alan Turing, another mathematician, Alonzo Church, was very interested, during the 1930s, in the question What is a computable function? He developed a formal system known as the pure lambda calculus, in order to describe programs in a simple and precise way. Today the Lambda Calculus serves as a mathematical foundation for the study of functional programming languages, and especially for the study of denotational semantics. Reference: http://en.wikipedia.org/wiki/lambda_calculus 3
t ::=. t t t Untyped Lambda Calculus - Synta 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) ) Eamples (taken from 2015 eams): (λ. λ. (λ.) ) ((λ. ) λ.) (λt. λf. t) (λ.) ((λ.) (λs. λz. s z)) 4
Free vs. Bound Variables An occurrence of in t is bound in. t otherwise it is free is a binder Eamples. y. (y z) z.. y. (y z) (. ) 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 ) 5
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) 6
Beta-Reduction: Eamples (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede (. ) y y (. (. ) ) (u r) u r (. ) ( ( w. w)) (y z) w. y z w 7
[ s] = s Substitution Subtleties (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) [ s] y = y [ s] ( y. t 1 ) = y. [ s] t 1 [ s] (t 1 t 2 ) = ([ s] t 1 ) ([ s] t 2 ) if y if y and y FV(s) (. (. )) y [ y] (. ) =. y? (. ( y. )) y [ y] ( y. ) = y. y? (. (. )) y and (. ( y. )) y are stuck! They have no -reduction 8
Alpha Conversion Alpha conversion: Renaming of a bound variable and its bound occurrences (. t) y. [ y] t if y FV(t). (. (. )) y (. ( z. z)) y [ y] ( z. z) = z. z. y (. ( y. )) y (. ( z. )) y [ y] ( z. ) = z. y y. y 9
Currying Multiple arguments Say we want to define a function with two arguments: f = (, y). s We do this by Currying: f =. y. s f is now a function of that returns a function of y Currying and -reduction: f v w = (f v) w = ((. y. s) v) w ( y.[ v]s ) w [ v] [y w] s Conclusion: f = (, y). s f =. y. s f (v,w) f v w 10
Church Booleans Define: tru = t. f. t fls = t. f. f test = l. m. n. l m n test tru then else = ( l. m. n. l m n) ( t. f. t) then else ( m. n. ( t. f. t) m n) then else ( n. ( t. f. t) then n) else ( t. f. t) then else ( f. then) else then test fls then else = ( l. m. n. l m n) ( t. f. f) then else ( m. n. ( t. f. f) m n) then else ( n. ( t. f. f) then n) else ( t. f. f) then else ( f. f) else else and = b. c. b c fls or = not = 11
Church Numerals c 0 = s. z. z c 1 = s. z. s z c 2 = s. z. s (s z) c 3 = s. z. s (s (s z)) scc = n. s. z. s (n s z) plus = m. n. s. z. m s (n s z) times = m. n. m (plus n) c 0 iszero = 12
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? 13
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 14
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 15
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 16
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) 17
Different Evaluation Orders - Eample (. y. ( z. z) y) (( u. u) ( w. w)) y u w u w z y z 18
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 19
Call By Name (Lazy) (. y. ( z. z) y) (( u. u) ( w. w)) y y u w z y u w z z y z 20
Normal Order (. y. ( z. z) y) (( u. u) ( w. w)) y y u w z y y y u w z z y z 21
Divergence (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.( )) (.( )) 22
Divergence (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.( )) (.( )) 23
Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y y 24
Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y y 25
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()) 26
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 27
Church Rosser Theorem If: a * b, a * c then there eists d such that: b * d, and c * d b a c d 28
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 29
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 30
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) 31
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? 32
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) 33
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? 34
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 35