Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto, Stephen A. Edwards) Benjamin Pierce Types and Programming Languages http://www.cs.cornell.edu/courses/cs3110/2008fa/recitations/rec26.html
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
Alonzo Church 1903 1995 Professor at Princeton (1929 1967) and UCLA (1967 1990) Invented the Lambda Calculus Notable students: Alan Turing Stephen Cole Kleene Martin Davis Barkley Roser Dana Scott Leon Hankin Michael Rabin John Kemeny
Simple Eample factorial int fact(int ) { if (==0) return 1; else return n * fact(n-1); } C No state mutations fact : int : int = if =0 then 1 else n * fact n-1 ML No state mutations Epression only
Another Eample Compose comp f: int int g: int int: int int = fun g (f ) comp fun + 1 fun + 1 comp fun + 1 fun + 1 2 comp fun 2 * fun y 4 * y comp fun 2 * fun y 4 * y 3
What is calculus A complete computational model An assembly language for functional programming Powerful Concise Counterintuitive Can eplain many interesting PL features
Basics Repetitive epressions can be compactly represented using functional abstraction Eample: (5 * 4* 3 * 2 * 1) + (7 * 6 * 5 * 4 * 3 * 2 *1) = factorial(5) + factorial(7) factorial(n) = if n = 0 then 1 else n * factorial(n-1) factorial= n. if n = 0 then 1 else n * factorial(n-1) factorial= n. if n = 0 then 1 else n * apply (factorial (n-1))
Untyped Lambda Calculus 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) ) apply apply e1 e2 e3
Untyped Lambda Calculus 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) ) y apply apply y
Untyped Lambda Calculus++ t ::=. t t t number op t 1 t 2 t k true false terms variable abstraction application number built-in operator op t 1, t 2,, t k true false
Lambda Calculus in Python (. ) y (lambda : ) (y)
Lambda Calculus in ML (. ) y (fun ) (y)
Functions on int. succ apply succ (. succ ) 0 apply 0 succ apply 1 Define a function which computes 2?
Simon Peyton Jones, The Implementation of Functional Programming Languages, Prentice-Hall, 1987. Lambda Epressions Function application are written in prefi form Add 4 to 5 (+ 4 5) Evaluation: Select a rede and apply function (+(* 5 6) (* 8 3)) (+ 30 (* 8 3)) Different evaluation orders (+( 30 24) 54 (+(* 5 6) (* 8 3)) (+(* 5 6 ) 24) (+ 30 24) 54
Function Application and Currying Function application written as jutaposition f Every function has eactly one argument Multiple arguments can be simulated by Curring (+ ) is a function which adds to its argument What is +4? What is + (+ 4 5) = (+4) 5 9
Lambda Abstraction The only other thing in the lambda calculus is lambda abstraction a notation for defining unnamed functions (. + 1) The function of that adds 1 to
Boolean Epressions (if true y) (if false y) y Boolean negation? Boolean conjunction Boolean disjunction
Beta Reduction Substitute formal by actual arguments (. + 1) 4 ( + 4 1) can occur multiple times Or not at all 5 (. + ) 4 ( + 4 4) 8 (. + 1 6) 4 ( + 1 6) 7
Beta-Reduction(Cont) Fuzzy but mechanical Etra parenthesis and tree-notations can help (. y. + y) 3 4 = ((.( y. ((+ ) y))) 3) 4 ( y. ((+ 3) y)) 4 ((+ 3) 4) 7
Functions can be arguments Arbitrary functions as arguments/return ( f. f 3) (. + 1) (. + 1) 3 (. + 1) 3 (+ 3 1) 4
Free and Bound Variables. (+ y) 2 is bound y is free Determined in the enclosed contet Like global variables Both and y are free in (+ y)
Ill-Formed Beta Reductions Sometimes beta-reductions cannot be applied Like runtime error/undefined semantics in imperative programs (1 5) ( 5 y. y) (+ true 2) (test 5 1 2) Later will refine lambda calculus to avoid such terms by the compiler
Beta Reduction Formally
Substitution Replace a term by a term + (( + 2) * y)[ 3, y 7] =? + (( + 2) * y)[ z + 2] =? + (( + 2) * y)[t z + 2] =? + (( + 2) * y)[ y] More tricky in programming languages Why?
Free vs. Bound Variables An occurrence of is bound in t if it occurs in. t otherwise it is free is a binder Eamples Id=. y. (y z) z.. y. (y z) (. ) FV: t 2 Var is the set free variables of t FV() = {} FV(. t) = FV(t) {} FV (t 1 t 2 ) = FV(t 1 ) FV(t 2 )
Beta-Reduction (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) [ s] = s [ s] y = y if y [ s] (. t 1 ) =. t 1 [ s] ( y. t 1 ) = y. [ s] t 1 if y and y FV(s) [ s] (t 1 t 2 ) = ([ s] t 1 ) ([ s] t 2 ) [ s] y y t 1 [ s] t 1
Beta-Reduction (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) [ s] = s [ s] y = y [ s] ( y. t 1 ) = y. [ s] t 1 [ s] (t 1 t 2 ) = ([ s] t 1 ) ([ s] t 2 ) [ s] apply if y if y and y FV(s) apply t 1 t 2 [ s] t1 [ s] t 2
Eample Beta-Reduction (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede (. ) y y apply y [ y] y
Eample Beta-Reduction (e 2) (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede apply (. (. ) ) (u r) apply apply apply u r apply apply apply u r apply u r u r
Eample Beta-Reduction (e 2) (cont) (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede (. (. ) ) (u r) u r (. ) apply apply apply u r apply apply u r u r
Alpha- Renaming Alpha conversion: Renaming of a bound variable and its bound occurrences. y.y. z.z Simplifies terms 32
Eample Beta-Reduction (e 2) with renaming (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede (. (. ) ) (u r)
Eample Beta-Reduction (e 3) (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) rede ( ( w. w)) (y z) w. y z w w apply apply y apply z y apply w apply z w apply y apply z w w w
Simple Eercise Adding scopes to epressions Proposed synta let = t 1 in t 2 Informal meaning: all the occurrences of in t2 are replaced by t1 Eample: let a =. ( w. w) in a a = How can we simulate let?
Divergence (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (.y) ((.( )) (.( ))) y
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())
Different Evaluation Orders (. t 1 ) t 2 [ t 2 ] t 1 ( -reduction) (. ) ((. ) ( z. (. ) z)) id (id ( z. id z)) z z
Order of Evaluation Full-beta-reduction All possible orders Applicative order call by value (Eager) Left to right Fully evaluate arguments before function Normal order The leftmost, outermost rede is always reduced first Call by name Evaluate arguments as needed Call by need Evaluate arguments as needed and store for subsequent usages Implemented in Haskel
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
Call-by-value Operational Semantics t ::=. t t t terms variable abstraction application v ::=. t other values values abstraction values (. 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)
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 Value OS (. y. ( z. z) y) (( u. u) ( w. w)) E-AppAbs w u w v 2 w t 1 u w
Call By Value OS (2) v 1 t 2 (. y. ( z. z) y) (( u. u) ( w. w)) u w u w w w w y u w E-Appl2 y w u w z y z y z z
Call By Value OS (3) (. y. ( z. z) y) (( u. u) ( w. w)) v 2 t 1 w EAppAbs y y w z y z z y z
Programming in Calculus Functions with multiple arguments Simulating values Tuples Booleans Numerics Recursion
Programming in the Multiple arguments f= (, y). s -> Currying f=. y. s f v w = ((. y.s) v) w ( y.[ v]s) w [ v] [y w] s (f v) w = ((. y. *+y*y) 3) 4 ( y.[ 3] *+y*y) 4 = ( y.3*3+y*y) 4 [y 4] 3*3+y*y = 3*3+4*4
Adding Values Can be eplicitly added t ::= terms variable. t abstraction t t application true false 1 2 Can be simulated
Simulating Booleans tru = t. f. t fls = t. f. f
Simulating Tests 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 l t then else value value m f n t t n f m then n n m else l m t
Simulating Tests(2) 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 then else n n else n n t then t f t m f t
Simulating Tests(3) 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 n else t then n t then else f f t t
Simulating Tests(4) 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 t f then else f then else t
Simulating Tests(5) 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 f then then else
Simulating Tests 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) test fls then else = ( l. m. n. l m n) ( t. f. f)
Programming in Booleans 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) test fls then else = ( l. m. n. l m n) ( t. f. f) and = b. b. test b b fls or =? not =?
Programming in 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)) succ = n. s. z. s (n s z) plus = m. n. s. z. m s (n s z) times = m. n. m (plus n) c
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
Recursion Where is recursion in the lambda calculus? Cannot define FAC = (test (= n 0) 1 (* n (FAC (- n 1)))) Even let does not help
Fiedpoints We say X is a fiedpoint of a function F if F(X)= X Eamples F =. F =.2 F =.(+ 1 ) FAC is a fiedpoint of the non-recursive equation H= f. n. (test (= n 0) 1 (* n (f (- n 1)))) Compute FAC from H using beta reductions
The Y-combinator FAC is a fiedpoint of the non-recursive equation H= f. n. (test (= n 0) 1 (* n (f (- n 1)))) A function Y which computes FAC from H FAC = Y H FAC = H FAC Y H = H (Y H) Y is the function which that takes a function f and applies f (f ( (f )))
The Y-combinator FAC is a fiedpoint of the non-recursive equation H= f. n. (test (= n 0) 1 (* n (f (- n 1)))) A function Y which computes FAC from H FAC = Y H FAC = H FAC Y H = H (Y H) FAC 1 = Y H 1 = H (Y H) 1 = f. n. (test (= n 0) 1 (* n (f (- n 1)))) (Y H) 1 n. (test (= n 0) 1 (* n (Y H)(- n 1)))) 1 * (test (= 1 0) 1 (* n (Y H)(- n 1)))) * 1 (Y H) 0 = * 1 H (Y H) 0 = * 1 f. n. (test (= n 0) 1 (* n (f (- n 1)))) (Y H) 0 * * 1 n. (test (= n 0) 1 (* n ((Y H) (- n 1)))) 0 * * 1 (test (= 0 0) 1 (* n ((Y H) (- n 1)))) * * 1 1 1
The Y-combinator omega= (. ) (. ) Recursion can be simulated Y = (. ( y. (y y)) ( y. (y y))) Y f * f (Y f)
Summary: Lambda Calculus Powerful The ultimate assembly language Useful to illustrate ideas But can be counterintuitive Usually etended with useful syntactic sugars Other calculi eist pi-calculus object calculus mobile ambients