Lambda calculus (cont)
Deian Stefan (adopted from my & Edward Yang’s CSE242 slides)
Lambda calculus (cont) Deian Stefan (adopted from my & Edward - - PowerPoint PPT Presentation
Lambda calculus (cont) Deian Stefan (adopted from my & Edward Yangs CSE242 slides) Logistics Assignments: HW 1 is out and due this week (Sunday) There will be one more homework on functions After this: 1 homework / general
Deian Stefan (adopted from my & Edward Yang’s CSE242 slides)
➤ HW 1 is out and due this week (Sunday) ➤ There will be one more homework on functions ➤ After this: 1 homework / general topic area
➤ Sorry :(
➤ A: easy, B: okay, C: hard, D: wtf is PA1?
➤ A: easy, B: okay, C: hard
➤ A: too slow, B: it works for me, C:too fast
➤ Recall free and bound variables ➤ Substitution ➤ Evaluation order
➤ Is λ(x+y).3 a valid term? (A: yes, B: no) ➤ Is λx. (x x) a valid term? (A: yes, B: no) ➤ Is λx. (x) y a valid term? (A: yes, B: no)
➤ e1 e2 e3 ≝ (e1 e2) e3
find unmatched closing paren ‘)’
➤ λx.λy.λz.e ≝ λx.(λy.(λz.e))
➤ A: λx.(x x) ➤ B: (λx.x) x
➤ A: λy.(λx.x) x ➤ B: λy.(λx.(x x)) ➤ C: (λy.(λx.x)) x
➤ A: yes ➤ B: no
➤ When do we use substitution? ➤ What’s the challenge with substitution?
➤ λx.λy.λz.e ≝ λxyz.e
➤ Can implement multiple-argument function using
single-argument functions: called currying (bonus)
➤ let x = a+b in
let a = 7 in x + a
➤ Reduce (λx. (λa. x + a) 7) (a+b)
➤ A: rename all free variables ➤ B: rename all bound variables
➤ e.g., y is free in λx.(x+y) ➤ e.g., x is bound in λx.(x+y)
➤ FV(x) = {x} ➤ FV(λx.e) = FV(e) \ {x} ➤ FV(e1 e2) = FV(e1) ∪ FV(e2)
think: build out!
➤ e.g., y is free in λx.(x+y) ➤ e.g., x is bound in λx.(x+y)
➤ FV(x) = {x} ➤ FV(λx.e) = FV(e) \ {x} ➤ FV(e1 e2) = FV(e1) ∪ FV(e2)
think: build out!
➤ e.g., y is free in λx.(x+y) ➤ e.g., x is bound in λx.(x+y)
➤ FV(x) = {x} ➤ FV(λx.e) = FV(e) \ {x} ➤ FV(e1 e2) = FV(e1) ∪ FV(e2)
think: build out!
➤ x[x:=e] = e ➤ y[x:=e] = y if y ≠ x ➤ (e1 e2)[x := e] = (e1[x := e]) (e2[ x:= e]) ➤ (λx.e1)[x := e] = λx.e1 ➤ (λy.e1)[x := e2] = λy.e1[x := e2] if y ≠ x and y ∉ FV(e2) ➤ Why the if? If y is free in e2 this would capture it!
➤ x[x:=e] = e ➤ y[x:=e] = y if y ≠ x ➤ (e1 e2)[x := e] = (e1[x := e]) (e2[ x:= e]) ➤ (λx.e1)[x := e] = λx.e1 ➤ (λy.e1)[x := e2] = λy.e1[x := e2] if y ≠ x and y ∉ FV(e2) ➤ Why the if? If y is free in e2 this would capture it!
➤ x[x:=e] = e ➤ y[x:=e] = y if y ≠ x ➤ (e1 e2)[x := e] = (e1[x := e]) (e2[ x:= e]) ➤ (λx.e1)[x := e] = λx.e1 ➤ (λy.e1)[x := e2] = λy.e1[x := e2] if y ≠ x and y ∉ FV(e2) ➤ Why the if? If y is free in e2 this would capture it!
➤ λx.e = λy.e[x:=y] where y∉FV(e)
➤ (λx.e1) e2 = e1 [x:=e2]
➤ λx.(e x) = e where x∉FV(e)
➤ (λf.(λx. f (f x))) (λy.y+x)
➤ (λf.(λx. f (f x))) (λy.y+x) =α (λf.(λz. f (f z))) (λy.y+x)
➤ (λf.(λx. f (f x))) (λy.y+x) =α (λf.(λz. f (f z))) (λy.y+x) =β λz. (λy.y+x) ((λy.y+x) z)
➤ (λf.(λx. f (f x))) (λy.y+x) =α (λf.(λz. f (f z))) (λy.y+x) =β λz. (λy.y+x) ((λy.y+x) z) =β λz. (λy.y+x) (z+x)
➤ (λf.(λx. f (f x))) (λy.y+x) =α (λf.(λz. f (f z))) (λy.y+x) =β λz. (λy.y+x) ((λy.y+x) z) =β λz. (λy.y+x) (z+x) =β λz. z+x+x
➤ Recall free and bound variables ✓ ➤ Substitution ✓ ➤ Evaluation order
➤ A: The outer term: (λy.y) z ➤ B: The inner term: (λx.x) z
➤ No! They both reduce to z! ➤ Church-Rosser Theorem: “If you reduce to a normal
form, it doesn’t matter what order you do the reductions.” This is known as confluence.
➤ A: The outer term: (λy.y) z ➤ B: The inner term: (λx.x) z
➤ No! They both reduce to z! ➤ Church-Rosser Theorem: “If you reduce to a normal
form, it doesn’t matter what order you do the reductions.” This is known as confluence.
➤ A: The outer term: (λy.y) z ➤ B: The inner term: (λx.x) z
➤ No! They both reduce to z! ➤ Church-Rosser Theorem: “If you reduce to a normal
form, it doesn’t matter what order you do the reductions.” This is known as confluence.
➤ Ω ≝ (λx.x x) (λx.x x)
➤ Ω ≝ (λx.x x) (λx.x x) =β (x x)[ x:= (λx.x x)]
➤ Ω ≝ (λx.x x) (λx.x x) =β (x x)[ x:= (λx.x x)] =β (λx.x x) (λx.x x)
➤ Ω ≝ (λx.x x) (λx.x x) =β (x x)[ x:= (λx.x x)] =β (λx.x x) (λx.x x) = Ω Deja vu!
(Ω has no normal form)
(λx.y) Ω
(λx.y) Ω y
(λx.y) Ω y (λx.y) Ω
(λx.y) Ω y y (λx.y) Ω
(λx.y) Ω y y (λx.y) Ω (λx.y) Ω
(λx.y) Ω y y (λx.y) Ω (λx.y) Ω y (λx.y) Ω y
➤ e1 e2 →…→ (λx.e1’) e2 →…→ (λx.e1’) n → e1’[x:=n]
➤ What does this program do? ➤ (x => 33) ((x => x(x)) (x => x(x))) ➤ RangeError: Maximum call stack size exceeded
➤ e1 e2 →…→ (λx.e1’) e2 →…→ (λx.e1’) n → e1’[x:=n]
➤ What does this program do? ➤ (x => 33) ((x => x(x)) (x => x(x))) ➤ RangeError: Maximum call stack size exceeded
➤ e1 e2 →…→ (λx.e1’) e2 →…→ (λx.e1’) n → e1’[x:=n]
➤ What does this program do? ➤ (x => 33) ((x => x(x)) (x => x(x))) ➤ RangeError: Maximum call stack size exceeded
➤ e1 e2 →…→ (λx.e1’) e2 →…→ (λx.e1’) n → e1’[x:=n]
➤ What does this program do? ➤ (x => 33) ((x => x(x)) (x => x(x))) ➤ RangeError: Maximum call stack size exceeded
➤ e1 e2 →…→ (λx.e1’) e2 →…→ (λx.e1’) n → e1’[x:=n]
➤ What does this program do? ➤ (x => 33) ((x => x(x)) (x => x(x))) ➤ RangeError: Maximum call stack size exceeded
➤ e1 e2 →…→ (λx.e1’) e2 → e1’[x:=e2] →…
➤ It only does what is absolutely necessary!
➤ e1 e2 →…→ (λx.e1’) e2 → e1’[x:=e2] →…
➤ It only does what is absolutely necessary!
➤ e1 e2 →…→ (λx.e1’) e2 → e1’[x:=e2] →…
➤ It only does what is absolutely necessary!
➤ e1 e2 →…→ (λx.e1’) e2 → e1’[x:=e2] →…
➤ It only does what is absolutely necessary!
➤ Evaluation strategy says which redex to evaluate ➤ Evaluation not guaranteed to find normal form
➤ Recall free and bound variables ✓ ➤ Substitution ✓ ➤ Evaluation order ✓
➤ “Simplest reasonable programming language”-Ramsey ➤ Binders show up everywhere! ➤ Know your capture-avoiding substitution! ➤ Macros in HW1 ➤ JavaScript modules in PA1