Advanced Topics in Programming Languages 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
Computation Models Turing Machines Wang Machines Counter Programs Lambda Calculus
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: (λ. λ. (λ.) ) ((λ. ) λ.) (λt. λf. t) (λ.) ((λ.) (λs. λz. s z))
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 ) Eamples: FV( (y z) ) = FV(. y. (y z) ) = FV( (. ) ) = FV( (. ) ) = 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)
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
[ 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
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
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 10
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? 11
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 12
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 13
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 14
Divergence (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.( )) (.( ))
Divergence (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.( )) (.( ))
Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y y
Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y y
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())
Summary Order of Evaluation Full-beta-reduction All possible orders Applicative order call by value (strict) 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
Different Evaluation Orders (. y. ( z. z) y) (( u. u) ( w. w)) y u w u w z y z
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
Call By Name (Lazy) (. y. ( z. z) y) (( u. u) ( w. w)) y y u w z y u w z z y z
Normal Order (. y. ( z. z) y) (( u. u) ( w. w)) y y u w z y y y u w z z y z
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 25
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 = 26
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 = 27
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 28
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) 29
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? 30
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) 31
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? 32
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 33
Church Rosser Theorem If: a * b, a * c then there eists d such that: b * d, and c * d b a c d 34
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 35