The λ-calculus Lecturer: John Wickerson Phil Wadler
A tiny bit of Java expr ::= expr + expr expr < expr x n block ::= cmd { cmd... cmd } cmd ::= expr; if(cmd) block else block; if(cmd) block; try{cmd} finally{cmd... cmd}; while(cmd) block;... 2
A tiny bit of Haskell expr ::= expr + expr expr < expr x n let x = expr in expr if expr then expr else expr expr expr \x. expr expr : expr [] true false (expr, expr) 3
The whole λ-calculus M ::= λx. M M M x 4
The λ-calculus is... The simplest programming language in the world A training ground for studying other programming languages 5
Outline 1. Syntax (free variables, α-equivalence, substitution) 2. Semantics (-reduction, confluence, reduction strategies) 3. Usage (encoding arithmetic, recursion) 6
Examples M ::= λx.m M M λx. x x λx. λy. λz. M = λxyz. M λx. y (λx. x)(λy. y) M1 M2 M3 = (M1 M2) M3 λx. λy. λz. x (λx. x)(λy. λz. x (y z))(λx. y x x) 7
KEY CONCEPT λx. x y binder free variable bound variable 8
Free variables Let FV(M) denote the set of free variables in the λ-term M. For instance: FV(λx. y) = {y}. If FV(M) = then we say M is "closed". 9
α-equivalence For example: (λx. x) =α (λy. y) int i; for(i=0; i<5; i++) x+=i; (λx. x (λy. y) y) =α? (λy. y (λx. x) x) (λx. x (λy. y) y) =α? (λy. y (λx. x) y) (λx. x (λy. y) y) =α? (λw. w (λw. w) y) (λz. z (λy. y) y) =α? (λz. z (λw. w) y) (λz. z (λz. z) y) =α? (λz. z (λz. z) y) 10
α-equivalence M ::= λx.m M M x M =α M' N =α N' x =α x M N =α M' N' z FV(M) FV(N) M[z/x] =α N[z/y] (λx. M) =α (λy. N) 11
Substitution Replace e with 2.718 in try { f.writefloat(e ^ 2); } catch (IOException e) { e.printstacktrace(); } 12
Substitution let x= 4*y in let y=w+5 in x*y 13
Substitution let y=w+5 4*y in *y 14
Substitution let x= 4*y in let z=w+5 in x*z 15
Substitution let z=w+5 4*y in *z 16
y[m/x] = Substitution M if y=x {y if y x M ::= λx.m M M x (λy. N)[M/x] = { λy. N λz. N[z/y][M/x] if y=x if y x where z FV(M) (FV(N) - {y}) {x} (N1 N2)[M/y] = (N1[M/y])(N2[M/y]) 17
KEY CONCEPTS α-equivalence capture-avoiding substitution 18
BONUS DeBruijn indices let x=4*y in let z=w+x in x*z let =4*y in let =w+ in * 19
BONUS DeBruijn indices λx. x λ. 0 λx. y λ. y (λx. x)(λy. y) (λ. 0)(λ. 0) λx. λy. λz. x λ. λ. λ. 2 (λx. x)(λy. λz. x (y z))(λx. y x x) (λ. 0)(λ. λ. x (1 0))(λ. y 0 0) 20
Outline 1. Syntax (free variables, α-equivalence, substitution) 2. Semantics (-reduction, confluence, reduction strategies) 3. Usage (encoding arithmetic, recursion) 21
Java semantics (C, σ) (C', σ') (if(c)s, σ) (if(c')s, σ') (if(true)s, σ) (S, σ) (if(false)s, σ) (skip, σ) 22
λ-calculus semantics "redex" (λx. M) N M[N/x] M M' M M' N N' λx. M λx. M' M N M' N M N M N' M =α M' M' N' N' =α N M N 23
-reduction examples (λx. x x) ((λx. y) z) 24
-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) 25
-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) y ((λx. y) z) 26
-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) y ((λx. y) z) y y 27
-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) ((λx. y) z) y y ((λx. y) z) y y 28
-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) ((λx. y) z) y y ((λx. y) z) y y 29
-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) (λx. x x) y ((λx. y) z) y y ((λx. y) z) y y 30
-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) (λx. x x) y ((λx. y) z) y y ((λx. y) z) y y 31
KEY CONCEPTS -reduction: (λx. M) N M[N/x] 32
Many steps of -reduction Define * as follows: M =α M' M M'' M'' * M' M * M' M * M' 33
Confluence Theorem (Church Rosser). If M * M1 and M * M2 then there exists M' such that M1 * M' and M2 * M'. M * M1* M2 * * M' 34
-reduction examples (λx. x x) ((λx. y) z) ((λx. y) z) ((λx. y) z) (λx. x x) y ((λx. y) z) y y ((λx. y) z) y y 35
-normal form "in -normal form" = "contains no redexes" M has a -normal form if M * N for some N in -normal form. Theorem (Uniqueness of -normal forms). If M * N1, M * N2, and N1 and N2 are in -normal form, then N1 =α N2. 36
-normal form Theorem (Uniqueness of -normal forms). If M * N1, M * N2, and N1 and N2 are in -normal form, then N1 =α N2. Proof. By Church Rosser, obtain N such that N1 * N and N2 * N. But N1 and N2 are in -normal form, so N1 =α N =α N2. 37
-equivalence = is the smallest equivalence relation containing. M4 M1 M3 M5 M2 * * M' Simpler version. M 1 = M 2 iff there exists M' such that M 1 * M' and M 2 * M'. 38
KEY CONCEPTS -reduction confluence (Church Rosser) -normal form -equivalence 39
Non-termination Some terms do not have a -normal form. For instance: (λx. x x)(λx. x x) (λx. x x)(λx. x x)... 40
Possible non-termination Some terms might not terminate. For instance: (λx. y)((λx. x x)(λx. x x)) y (λx. y)((λx. x x)(λx. x x)) y... 41
Reduction strategies (λx. M) N Substitute N for x? "call by name" Reduce N? "call by value" Reduce M? not usually done 42
Full -reduction Call by name Call by value M M' λx. M λx. M' M M' M N M' N M N M' M N N M' N M V M' M N V M' N N N' M V / N V N' M N M N' M N V M N' (λx. M) N M[N/x] (λx. M) N N M[N/x] N V / (λx. M) N V M[N/x] 43
Reduction strategies (λx. x x) ((λx. y) z) N V ((λx. y) z) ((λx. y) z) N ((λx. y) z) y y ((λx. y) z) N V (λx. x x) y y y 44
Reduction strategies (λx. y)((λx. x x)(λx. x x)) N V y (λx. y)((λx. x x)(λx. x x)) N V y... 45
KEY CONCEPTS call-by-name reduction (wins if argument is unused) call-by-value reduction (wins if argument is used more than once) 46
BONUS Extensionality Is -equivalence the best notion of "equality" between λ- terms? We don't have (λx. sin x) = sin. But we do have (λx. sin x) M = sin M, for any M. Add η-equivalence: x FV(M) (λx. M x) =η M η-equivalence captures "equality" nicely. 47
BONUS Proving Church Rosser -reduction? * * * * 48
BONUS Proving Church Rosser -reduction?? * * * * 49
BONUS Proving Church Rosser -reduction? * * * * 50
BONUS Proving Church Rosser 51
BONUS Proving Church Rosser -reduction? * * * * 52
BONUS Proving Church Rosser -reduction * * * * 53
BONUS Proving Church Rosser -reduction?? * * * * * * 54
BONUS Proving Church Rosser -reduction? * * * * * * 55
BONUS Proving Church Rosser -reduction * * * * * * 56
BONUS Proving Church Rosser -reduction * * * * * * ("Property A") ("Property B") 57
58
59
BONUS Proving Church Rosser -reduction * * * * * * 60
BONUS Proving Church Rosser -reduction? * * * * 61
Outline 1. Syntax (free variables, α-equivalence, substitution) 2. Semantics (-reduction, confluence, reduction strategies) 3. Usage (encoding arithmetic, recursion) 62
Encoding numbers 0 λs. λz. z 1 λs. λz. s z 2 λs. λz. s (s z) n {n λs. λz. s (... s(z)...) 3 λs. λz. s (s (s z)) 63
Encoding arithmetic plus λm. λn. λs. λz. m s (n s z) mult λm. λn. λs. λz. m (n s) z n {n λs. λz. s (... s(z)...) 64
Encoding arithmetic plus λm. λn. λs. λz. m s (n s z) mult λm. λn. λs. λz. m (n s) z Exercise. Evaluate "plus 2 3" and "mult 2 3". ifz λn. λx1. λx2. n (λz. x2) x1 Exercise. Find pred such that "pred 0 = 0" and "pred n+1 = n". 65
λ-definability A partial function f : IN k IN is λ-definable if there exists a closed λ-term F such that f(x1,...,xk) = y iff F(x1,...,xk) = y, and f(x1,...,xk) iff F(x1,...,xk) has no normal form. Church-Turing thesis: f is λ-definable f is computable via register machine f is "computable" f is computable via Turing machine 66
Example: factorial fac n = if n=0 then 1 else n*fac(n-1) fac = λn. ifz n 1 (mult n (fac (pred n)))) 67
Encoding recursion Let fix (λx. λy. y (x x y))(λx. λy. y (x x y)). Observe: fix M * M (fix M). [Recall: x is a fixpoint of f whenever x=f(x).] This means: fix M is a fixpoint of M. 68
Example: factorial fac n = if n=0 then 1 else n*fac(n-1) fac = λn. ifz n 1 (mult n (fac (pred n)))... = (λf. λn. ifz n 1 (mult n (f (pred n)))) fac fac fix (λf. λn. ifz n 1 (mult n (f (pred n))))) Exercise. Evaluate "fac 2". 69
KEY CONCEPTS we can encode... numbers and arithmetic if-statements recursion 70
BONUS Combinators Lambda calculus: M ::= λx. M M M x Can we restrict even further? Yes, we can even get rid of variables! Let S λx. λy. λz. (x z) (y z) and K λx. λy. x. M ::= M M S K 71
BONUS Is λ-calculus broken? Define silly (λx. (x x))(λx. (x x)). Then silly = silly. 72
BONUS Adding types Untyped: M ::= λx. M M M x Typed: M ::= λx:τ. M M M x where τ ::= τ τ no more fix function... not Turing-complete add fix to language explicitly 73
Outline 1. Syntax (free variables, α-equivalence, substitution) 2. Semantics (-reduction, confluence, reduction strategies) 3. Usage (encoding arithmetic, recursion) 74