Fundamentals and lambda calculus
Deian Stefan (adopted from my & Edward Yang’s CSE242 slides)
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
Deian Stefan (adopted from my & Edward Yang’s CSE242 slides)
➤ Programming assignment 1 is out ➤ Homework 1 will be released tomorrow night
➤ We’ll use them today
➤ Syntax is a bit ugly/terse when you want to use
functions as values; recall block scoping: (function () { // ... do something }) ();
➤ Semantics not always the same (this has different
meaning), but for this class should always be safe to use
➤ Only has one feature: functions
➤ Good system for studying the concept of variable
binding that appears in almost all languages
➤ Competing model of computation introduced by
Church as an alternative to Turing machines: substitution (you’ll see this today) = symbolic comp
➤ Influenced Lisp (thus JS), ML, Haskell, C++, etc.
➤ You can use lambda calculus and extended it in
different ways to study languages and features
➤ E.g., we can study the difference between strict
languages like JavaScript and lazy ones like Haskell
➤ λ + evaluation strategy ➤ E.g., we can study different kinds of type systems ➤ Simply-typed λ calculus, polymorphic, etc.
➤ Understanding λ will help you interpret what you are
reading in PL research papers
➤ Understanding λ will help you get started with other
formal/theoretical foundations:
➤ Operational semantics ➤ Denotational semantics
➤ The symbols used to write a program ➤ E.g., (x + y) is a grammatical expression
➤ The actions that occur when a program is executed
➤ The symbols used to write a program ➤ E.g., (x + y) is a grammatical expression
➤ The actions that occur when a program is executed
➤ The symbols used to write a program ➤ E.g., (x + y) is a grammatical expression
➤ The actions that occur when a program is executed
➤ Free and bound variables ➤ Substitution ➤ Evaluation order
➤ Expressions: e ::= x | λx.e | e1 e2
➤ Variables: x ➤ Functions or λ abstractions: λx.e ➤ This is the same as x => e in JavaScript! ➤ Function application: e1 e2 ➤ This is the same as e1 (e2) in JavaScript!
➤ λx.(2+x) ➤ Same as: x => (2 + x) ➤ (λx.(2 + x)) 5 ➤ Same as: (x => (2 + x)) (5) ➤ (λf.(f 3)) (λx.(x + 1)) ➤ Same as: (f => (f (3))) (x => (x+1))
➤ λx.(2+x) ➤ Same as: x => (2 + x) ➤ (λx.(2 + x)) 5 ➤ Same as: (x => (2 + x)) (5) ➤ (λf.(f 3)) (λx.(x + 1)) ➤ Same as: (f => (f (3))) (x => (x+1))
LIES! What is this “2” and “+”? (Sugar.)
➤ λx.(2+x) ➤ Same as: x => (2 + x) ➤ (λx.(2 + x)) 5 ➤ Same as: (x => (2 + x)) (5) ➤ (λf.(f 3)) (λx.(x + 1)) ➤ Same as: (f => (f (3))) (x => (x+1))
➤ λx.(2+x) ➤ Same as: x => (2 + x) ➤ (λx.(2 + x)) 5 ➤ Same as: (x => (2 + x)) (5) ➤ (λf.(f 3)) (λx.(x + 1)) ➤ Same as: (f => (f (3))) (x => (x+1))
➤ λx.(2+x) ➤ Same as: x => (2 + x) ➤ (λx.(2 + x)) 5 ➤ Same as: (x => (2 + x)) (5) ➤ (λf.(f 3)) (λx.(x + 1)) ➤ Same as: (f => (f (3))) (x => (x+1))
➤ f => (x => f (f (x))) ➤ ((f => (x => f (f (x)))) (x => x+1)) (4)
➤ λf.(λx. f (f x)) ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4
➤ f => (x => f (f (x))) ➤ ((f => (x => f (f (x)))) (x => x+1)) (4)
➤ λf.(λx. f (f x)) ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4
➤ f => (x => f (f (x))) ➤ ((f => (x => f (f (x)))) (x => x+1)) (4)
➤ λf.(λx. f (f x)) ➤ ((λf.(λx. f (f x)) (λx.x+1)) 4
➤ Free and bound variables ➤ Substitution ➤ Evaluation order
➤ If we can’t reduce it any further, the term is said to
be in normal form
➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7
→ ((λx.(x + 1)) 3) → (3 + 1) → 4
➤ In JavaScript: (f => (f (3))) (x => (x+1))
→ ((x => (x+1)) (3) → (3+1) → 4
➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7
→ ((λx.(x + 1)) 3) → (3 + 1) → 4
➤ In JavaScript: (f => (f (3))) (x => (x+1))
→ ((x => (x+1)) (3) → (3+1) → 4
➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7
→ ((λx.(x + 1)) 3) → (3 + 1) → 4
➤ In JavaScript: (f => (f (3))) (x => (x+1))
→ ((x => (x+1)) (3) → (3+1) → 4
➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7
→ ((λx.(x + 1)) 3) → (3 + 1) → 4
➤ In JavaScript: (f => (f (3))) (x => (x+1))
→ ((x => (x+1)) (3) → (3+1) → 4
➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7
→ ((λx.(x + 1)) 3) → (3 + 1) → 4
➤ In JavaScript: (f => (f (3))) (x => (x+1))
→ ((x => (x+1)) (3) → (3+1) → 4
➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7
→ ((λx.(x + 1)) 3) → (3 + 1) → 4
➤ In JavaScript: (f => (f (3))) (x => (x+1))
→ ((x => (x+1)) (3) → (3+1) → 4
➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7
→ ((λx.(x + 1)) 3) → (3 + 1) → 4
➤ In JavaScript: (f => (f (3))) (x => (x+1))
→ ((x => (x+1)) (3) → (3+1) → 4
➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7
→ ((λx.(x + 1)) 3) → (3 + 1) → 4
➤ In JavaScript: (f => (f (3))) (x => (x+1))
→ ((x => (x+1)) (3) → (3+1) → 4
➤ In JavaScript: (x => (2 + x)) (5) → (2 + 5) → 7
→ ((λx.(x + 1)) 3) → (3 + 1) → 4
➤ In JavaScript: (f => (f (3))) (x => (x+1))
→ ((x => (x+1)) (3) → (3+1) → 4
➤ Replace every occurrence of x in e1 with e2
➤ (λx.e1) e2 → e1 [x := e2] ➤ Function application rewritten to
e1 (the function body) with every x in e1 substituted with e2 (argument)
➤ ((λf.(λx. f (f x)) (λx.x+1)) 4
➤ ((λf.(λx. f (f x)) (λx.x+1)) 4 ➤ (λx. (λx.x+1) ((λx.x+1) x)) 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)
➤ ((λ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)
➤ ((λ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
➤ ((λ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
➤ (λf.(λx. f (f x)) (λy.y+x)
➤ (λf.(λx. f (f x)) (λy.y+x) ➤ λx. (λy.y+x) ((λy.y+x) x)
➤ (λf.(λx. f (f x)) (λy.y+x) ➤ λx. (λy.y+x) ((λy.y+x) x) ➤ λx. (λy.y+x) (x+x)
➤ (λ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)
➤ (λ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)
???? that’s not a function that adds x to argument two times
➤ (λx.e1) e2 → e1 [x := e2] ➤ This function application reduces to
e1 (the function body) where every x in e1 is substituted with e2 (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!
➤ 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!
➤ 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!
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!
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!
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!
➤ Def: variable x is bound in λx.(x+y)
➤ Above: x is not special, we could have used z ➤ We say they are equivalent: λx.(x+y) =𝝱 λz.(z+y)
names to avoid capture: e.g., λx.(x+y) to λz.(z+y)
➤ No! We don’t know what y may be, so we must keep
it as is!
➤ 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} xi + y
➤ No! We don’t know what y may be, so we must keep
it as is!
➤ 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} xi + y
➤ No! We don’t know what y may be, so we must keep
it as is!
➤ 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} xi + y
➤ e.g., y is free in λx.(x+y) ➤ is x free? No! We say x is bound in λx.(x+y)
➤ FV(x) = {x} ➤ FV(λx.e) = FV(e) \ {x} ➤ FV(e1 e2) = FV(e1) ∪ FV(e2)
➤ e.g., y is free in λx.(x+y) ➤ is x free? No! We say x is bound in λx.(x+y)
➤ FV(x) = {x} ➤ FV(λx.e) = FV(e) \ {x} ➤ FV(e1 e2) = FV(e1) ∪ FV(e2)
➤ e.g., y is free in λx.(x+y) ➤ is x free? No! We say x is bound in λx.(x+y)
➤ FV(x) = {x} ➤ FV(λx.e) = FV(e) \ {x} ➤ FV(e1 e2) = FV(e1) ∪ FV(e2)
➤ e.g., y is free in λx.(x+y) ➤ is x free? No! We say x is bound in λx.(x+y)
➤ FV(x) = {x} ➤ FV(λx.e) = FV(e) \ {x} ➤ FV(e1 e2) = FV(e1) ∪ FV(e2)
➤ 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[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[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
➤ 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
➤ Free and bound variables ✓ ➤ Substitution ✓ ➤ Evaluation order ✓
➤ “Simplest reasonable programming language”-Ramsey ➤ Binders show up everywhere! ➤ Know your capture-avoiding substitution!