fundamentals and lambda calculus
play

Fundamentals and lambda calculus Deian Stefan (adopted from my - PowerPoint PPT Presentation

Fundamentals and lambda calculus Deian Stefan (adopted from my & Edward Yangs CSE242 slides) Logistics Assignments: Programming assignment 1 is out Homework 1 will be released tomorrow night Podcasting: everything should be


  1. A more complicated example • Compose function that adds 1 to arg & apply it to 4 ➤ (( λ f.( λ x. f (f x)) ( λ x.x+1)) 4 ➤ ( λ x. ( λ x.x+1) (( λ x.x+1) x)) 4 ➤ ( λ x.x+1) (( λ x.x+1) 4) ➤ ( λ x.x+1) (4+1)

  2. A more complicated example • Compose function that adds 1 to arg & apply it to 4 ➤ (( λ f.( λ x. f (f x)) ( λ x.x+1)) 4 ➤ ( λ x. ( λ x.x+1) (( λ x.x+1) x)) 4 ➤ ( λ x.x+1) (( λ x.x+1) 4) ➤ ( λ x.x+1) (4+1) ➤ 4+1+1

  3. A more complicated example • Compose function that adds 1 to arg & apply it to 4 ➤ (( λ f.( λ x. f (f x)) ( λ x.x+1)) 4 ➤ ( λ x. ( λ x.x+1) (( λ x.x+1) x)) 4 ➤ ( λ x.x+1) (( λ x.x+1) 4) ➤ ( λ x.x+1) (4+1) ➤ 4+1+1 ➤ 6

  4. Let’s make this even more fun!

  5. Let’s make this even more fun! • Instead of 1, let’s add x to argument (& do it 2 times): ➤ ( λ f.( λ x. f (f x)) ( λ y.y+x)

  6. Let’s make this even more fun! • Instead of 1, let’s add x to argument (& do it 2 times): ➤ ( λ f.( λ x. f (f x)) ( λ y.y+x) ➤ λ x. ( λ y.y+x) (( λ y.y+x) x)

  7. Let’s make this even more fun! • Instead of 1, let’s add x to argument (& do it 2 times): ➤ ( λ f.( λ x. f (f x)) ( λ y.y+x) ➤ λ x. ( λ y.y+x) (( λ y.y+x) x) ➤ λ x. ( λ y.y+x) (x+x)

  8. Let’s make this even more fun! • Instead of 1, let’s add x to argument (& do it 2 times): ➤ ( λ f.( λ x. f (f x)) ( λ y.y+x) ➤ λ x. ( λ y.y+x) (( λ y.y+x) x) ➤ λ x. ( λ y.y+x) (x+x) ➤ λ x. (x+x+x)

  9. Let’s make this even more fun! • Instead of 1, let’s add x to argument (& do it 2 times): ➤ ( λ f.( λ x. f (f x)) ( λ y.y+x) ➤ λ x. ( λ y.y+x) (( λ y.y+x) x) ➤ λ x. ( λ y.y+x) (x+x) ???? 
 that’s not a function that adds 
 ➤ λ x. (x+x+x) x to argument two times

  10. Substitution is surprisingly complex • Recall our reduction rule for application: ➤ ( λ x.e 1 ) e 2 → e 1 [x := e 2 ] ➤ This function application reduces to 
 e 1 (the function body) where every x in e 1 
 is substituted with e 2 (value we’re applying func to) ➤ Where did we go wrong? When we substituted: ➤ ( λ f.( λ x. f (f x)) ( λ y.y+x) ➤ ( λ x. ( λ y.y+x) (( λ y.y+x) x) the x is captured !

  11. Another way to see the problem • Syntactic sugar: let x = e 1 in e 2 ≝ ( λ x.e 2 ) e 1 Let syntax makes this easy to see: • ➤ let x = a+b in let x = a+b in 
 let a = 7 in → let a = 7 in 
 x + a (a+b) + a ➤ Very obviously wrong! ➤ But, guess what: your C macro preprocessor does this!

  12. Another way to see the problem • Syntactic sugar: let x = e 1 in e 2 ≝ ( λ x.e 2 ) e 1 Let syntax makes this easy to see: • ➤ let x = a+b in let x = a+b in 
 let a = 7 in → let a = 7 in 
 x + a (a+b) + a ➤ Very obviously wrong! ➤ But, guess what: your C macro preprocessor does this!

  13. Fixing the problem • How can we fix this? 1.Rename variables! ➤ let x = a+b in let x = a+b in let x = a+b in 
 let a = 7 in → let a123 = 7 in → let a123 = 7 in 
 x + a x + a123 (a+b) + a123 2.Do the “dumb” substitution!

  14. Fixing the problem • How can we fix this? 1.Rename variables! ➤ let x = a+b in let x = a+b in let x = a+b in 
 let a = 7 in → let a123 = 7 in → let a123 = 7 in 
 x + a x + a123 (a+b) + a123 2.Do the “dumb” substitution!

  15. Fixing the problem • How can we fix this? 1.Rename variables! ➤ let x = a+b in let x = a+b in let x = a+b in 
 let a = 7 in → let a123 = 7 in → let a123 = 7 in 
 x + a x + a123 (a+b) + a123 2.Do the “dumb” substitution!

  16. Why is this the way to go? • We can always rename bound variables! ➤ Def: variable x is bound in λ x.(x+y) • Bound variables are just “placeholders” ➤ Above: x is not special, we could have used z ➤ We say they are equivalent: λ x.(x+y) = 𝝱 λ z.(z+y) • Renaming amounts to converting bound variable names to avoid capture: e.g., λ x.(x+y) to λ z.(z+y)

  17. Can we rename everything? • Can we rename y in λ x.(x+y)? (A: yes, B: no) ➤ No! We don’t know what y may be, so we must keep it as is! • Intuition: ➤ Can change the name of your function argument variables but not of variables from the outer scope ➤ E.g., ∀ x. P(x, y) or ∑ i ∈ {1,…,10} x i + y

  18. Can we rename everything? • Can we rename y in λ x.(x+y)? (A: yes, B: no) ➤ No! We don’t know what y may be, so we must keep it as is! • Intuition: ➤ Can change the name of your function argument variables but not of variables from the outer scope ➤ E.g., ∀ x. P(x, y) or ∑ i ∈ {1,…,10} x i + y

  19. Can we rename everything? • Can we rename y in λ x.(x+y)? (A: yes, B: no) ➤ No! We don’t know what y may be, so we must keep it as is! • Intuition: ➤ Can change the name of your function argument variables but not of variables from the outer scope ➤ E.g., ∀ x. P(x, y) or ∑ i ∈ {1,…,10} x i + y

  20. Let’s think about this more formally

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

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

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

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

  25. 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!

  26. 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!

  27. 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!

  28. 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!

  29. 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!

  30. 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!

  31. 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!

  32. 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!

  33. 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!

  34. Back to our example (what we should’ve done)

  35. Back to our example (what we should’ve done) • Instead of 1, let’s add x to argument (and do it 2x): ➤ ( λ f.( λ x. f (f x)) ( λ y.y+x)

  36. Back to our example (what we should’ve done) • 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)

  37. Back to our example (what we should’ve done) • 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)

  38. Back to our example (what we should’ve done) • 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)

  39. Back to our example (what we should’ve done) • 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

  40. Today • Syntax of λ calculus ✓ • Semantics of λ calculus ✓ ➤ Free and bound variables ✓ ➤ Substitution ✓ ➤ Evaluation order

  41. 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.

  42. 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.

  43. 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.

  44. Does evaluation order really not matter?

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

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

  47. 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)

  48. 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!

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

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

  51. 
 
 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) Ω

  52. 
 
 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) Ω

  53. 
 
 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) Ω

  54. 
 
 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) Ω

  55. 
 
 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) Ω

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

  57. 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

  58. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend