lambda calculus cont
play

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


  1. Lambda calculus (cont) Deian Stefan (adopted from my & Edward Yang’s CSE242 slides)

  2. Logistics • Assignments: ➤ HW 1 is out and due this week (Sunday) ➤ There will be one more homework on functions ➤ After this: 1 homework / general topic area • Podcasting: no video while projector is broken ➤ Sorry :( • Come to section and office hours!

  3. Questions • How are you finding PA1? ➤ A: easy, B: okay, C: hard, D: wtf is PA1?

  4. Questions • How are you finding HW1? ➤ A: easy, B: okay, C: hard

  5. Questions • How are you finding the pace of the lectures? ➤ A: too slow, B: it works for me, C:too fast

  6. Today • Recall syntax of λ calculus • Semantics of λ calculus ➤ Recall free and bound variables ➤ Substitution ➤ Evaluation order

  7. Review • λ -calculus syntax: e ::= x | λ x.e | e 1 e 2 ➤ 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)

  8. More compact syntax (HW) • Function application is left associative ➤ e 1 e 2 e 3 ≝ (e 1 e 2 ) e 3 • Lambdas binds all the way to right: only stop when you find unmatched closing paren ‘)’ ➤ λ x. λ y. λ z.e ≝ λ x.( λ y.( λ z.e))

  9. More on syntax • Write the parens: λ x.x x ➤ A: λ x.(x x) ➤ B: ( λ x.x) x

  10. More on syntax • Write the parens: λ y. λ x.x x = ➤ A: λ y.( λ x.x) x ➤ B: λ y.( λ x.(x x)) ➤ C: ( λ y.( λ x.x)) x

  11. More on syntax • Is ( λ y. λ x.x) x = λ y. λ x.x x ? ➤ A: yes ➤ B: no

  12. How do we compute in λ calculus?

  13. How do we compute in λ calculus? • Substitution! ➤ When do we use substitution? ➤ What’s the challenge with substitution?

  14. Example terms • Reduce ( λ x.(2 + x)) 5 • Reduce ( λ x.( λ y.2) 3) 5 → ( λ x. 2) 5 → 2 • Reduce (board): (( λ x.( λ y.2)) 3) 5 → (( λ y.2) 5) → 2 • Reduce: ( λ x. λ y. λ z.y+3) 4 5 6 


  15. Even more compact syntax • Can always variables left of the . ➤ λ x. λ y. λ z.e ≝ λ xyz.e • This makes the term look like a 3 argument function ➤ Can implement multiple-argument function using single-argument functions: called currying (bonus) • We won’t use this syntax, but you may see in the wild

  16. Why is substitution hard? • What does this reduce to if we do it blindly? ➤ let x = a+b in 
 let a = 7 in 
 x + a • Recall: let x = e 1 in e 2 ≝ ( λ x.e 2 ) e 1 ➤ Reduce ( λ x. ( λ a. x + a) 7) (a+b)

  17. How do we fix this? • Renaming! ➤ A: rename all free variables ➤ B: rename all bound variables

  18. Def: free variables (recall) • If a variable is not bound by a λ , we say that it is free ➤ e.g., y is free in λ x.(x+y) ➤ e.g., x is bound in λ x.(x+y) • We can compute the free variables of any term: ➤ FV(x) = {x} think: build out! ➤ FV( λ x.e) = FV(e) \ {x} ➤ FV(e 1 e 2 ) = FV(e 1 ) ∪ FV(e 2 )

  19. Def: free variables (recall) • If a variable is not bound by a λ , we say that it is free ➤ e.g., y is free in λ x.(x+y) ➤ e.g., x is bound in λ x.(x+y) • We can compute the free variables of any term: ➤ FV(x) = {x} think: build out! ➤ FV( λ x.e) = FV(e) \ {x} ➤ FV(e 1 e 2 ) = FV(e 1 ) ∪ FV(e 2 )

  20. Def: free variables (recall) • If a variable is not bound by a λ , we say that it is free ➤ e.g., y is free in λ x.(x+y) ➤ e.g., x is bound in λ x.(x+y) • We can compute the free variables of any term: ➤ FV(x) = {x} think: build out! ➤ FV( λ x.e) = FV(e) \ {x} ➤ FV(e 1 e 2 ) = FV(e 1 ) ∪ FV(e 2 )

  21. Def: Capture-avoiding substitution • Capture-avoiding substitution: ➤ x[x:=e] = e ➤ y[x:=e] = y if y ≠ x ➤ (e 1 e 2 )[x := e] = (e 1 [x := e]) (e 2 [ x:= e]) ➤ ( λ x.e 1 )[x := e] = λ x.e 1 ➤ ( λ y.e 1 )[x := e 2 ] = λ y.e 1 [x := e 2 ] if y ≠ x and y ∉ FV(e 2 ) ➤ Why the if? If y is free in e 2 this would capture it!

  22. Def: Capture-avoiding substitution • Capture-avoiding substitution: ➤ x[x:=e] = e ➤ y[x:=e] = y if y ≠ x ➤ (e 1 e 2 )[x := e] = (e 1 [x := e]) (e 2 [ x:= e]) ➤ ( λ x.e 1 )[x := e] = λ x.e 1 ➤ ( λ y.e 1 )[x := e 2 ] = λ y.e 1 [x := e 2 ] if y ≠ x and y ∉ FV(e 2 ) ➤ Why the if? If y is free in e 2 this would capture it!

  23. Def: Capture-avoiding substitution • Capture-avoiding substitution: ➤ x[x:=e] = e ➤ y[x:=e] = y if y ≠ x ➤ (e 1 e 2 )[x := e] = (e 1 [x := e]) (e 2 [ x:= e]) ➤ ( λ x.e 1 )[x := e] = λ x.e 1 ➤ ( λ y.e 1 )[x := e 2 ] = λ y.e 1 [x := e 2 ] if y ≠ x and y ∉ FV(e 2 ) ➤ Why the if? If y is free in e 2 this would capture it!

  24. Lambda calculus: equational theory • α -renaming or α -conversion ➤ λ x.e = λ y.e[x:=y] where y ∉ FV(e) • β -reduction ➤ ( λ x.e 1 ) e 2 = e 1 [x:=e 2 ] • η -conversion ➤ λ x.(e x) = e where x ∉ FV(e) • We define our → relation using these equations!

  25. Back to old example

  26. Back to old example • Instead of 1, let’s add x to argument (and do it 2x): ➤ ( λ f.( λ x. f (f x))) ( λ y.y+x)

  27. Back to old example • Instead of 1, let’s add x to argument (and do it 2x): ➤ ( λ f.( λ x. f (f x))) ( λ y.y+x) = α ( λ f.( λ z. f (f z))) ( λ y.y+x)

  28. Back to old example • Instead of 1, let’s add x to argument (and do it 2x): ➤ ( λ f.( λ x. f (f x))) ( λ y.y+x) = α ( λ f.( λ z. f (f z))) ( λ y.y+x) = β λ z. ( λ y.y+x) (( λ y.y+x) z)

  29. Back to old example • Instead of 1, let’s add x to argument (and do it 2x): ➤ ( λ 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)

  30. Back to old example • Instead of 1, let’s add x to argument (and do it 2x): ➤ ( λ 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

  31. Today • Recall syntax of λ calculus ✓ • Semantics of λ calculus ✓ ➤ Recall free and bound variables ✓ ➤ Substitution ✓ ➤ Evaluation order

  32. Evaluation order • What should we reduce first in ( λ x.x) (( λ y.y) z)? ➤ A: The outer term: ( λ y.y) z ➤ B: The inner term: ( λ x.x) z • Does it matter? ➤ 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.

  33. Evaluation order • What should we reduce first in ( λ x.x) (( λ y.y) z)? ➤ A: The outer term: ( λ y.y) z ➤ B: The inner term: ( λ x.x) z • Does it matter? ➤ 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.

  34. Evaluation order • What should we reduce first in ( λ x.x) (( λ y.y) z)? ➤ A: The outer term: ( λ y.y) z ➤ B: The inner term: ( λ x.x) z • Does it matter? ➤ 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.

  35. Does evaluation order really not matter?

  36. Does evaluation order really not matter? • Consider a curious term called Ω ➤ Ω ≝ ( λ x.x x) ( λ x.x x)

  37. Does evaluation order really not matter? • Consider a curious term called Ω ➤ Ω ≝ ( λ x.x x) ( λ x.x x) = β (x x)[ x:= ( λ x.x x)]

  38. Does evaluation order really not matter? • Consider a curious term called Ω ➤ Ω ≝ ( λ x.x x) ( λ x.x x) = β (x x)[ x:= ( λ x.x x)] = β ( λ x.x x) ( λ x.x x)

  39. Does evaluation order really not matter? • Consider a curious term called Ω ➤ Ω ≝ ( λ x.x x) ( λ x.x x) = β (x x)[ x:= ( λ x.x x)] = β ( λ x.x x) ( λ x.x x) = Ω Deja vu!

  40. Ω → Ω → Ω → Ω → Ω → Ω → Ω ( Ω has no normal form)

  41. 
 
 Does evaluation order really not matter? • Consider a function that ignores its argument: ( λ x.y) • What happens when we call it on Ω ? 
 ( λ x.y) Ω

  42. 
 
 Does evaluation order really not matter? • Consider a function that ignores its argument: ( λ x.y) • What happens when we call it on Ω ? 
 y ( λ x.y) Ω

  43. 
 
 Does evaluation order really not matter? • Consider a function that ignores its argument: ( λ x.y) • What happens when we call it on Ω ? 
 y ( λ x.y) Ω ( λ x.y) Ω

  44. 
 
 Does evaluation order really not matter? • Consider a function that ignores its argument: ( λ x.y) • What happens when we call it on Ω ? 
 y y ( λ x.y) Ω ( λ x.y) Ω

  45. 
 
 Does evaluation order really not matter? • Consider a function that ignores its argument: ( λ x.y) • What happens when we call it on Ω ? 
 y y ( λ x.y) Ω ( λ x.y) Ω ( λ x.y) Ω

  46. 
 
 Does evaluation order really not matter? • Consider a function that ignores its argument: ( λ x.y) • What happens when we call it on Ω ? 
 y y y y ( λ x.y) Ω ( λ x.y) Ω ( λ x.y) Ω ( λ x.y) Ω

  47. Does evaluation order really not matter? • Nope! Evaluation order does matter!

  48. Call-by-value • Reduce function, then reduce args, then apply ➤ e 1 e 2 → … → ( λ x.e 1 ’) e 2 → … → ( λ x.e 1 ’) n → e 1 ’[x:=n] • JavaScript’s evaluation strategy is call-by-value (ish) ➤ What does this program do? ➤ (x => 33) ((x => x(x)) (x => x(x))) ➤ RangeError: Maximum call stack size exceeded

Recommend


More recommend