Equivalent Computers: Lambda Calculus
Equivalent Computers: Lambda Calculus
| λ variable
Start
1
(, X, R
2: look
for (
¬(, #, L
≡ . term
λy. M ⇒α λv. (M [y α v])
#, 1, -
HALT
#, 0, -
where v does not occur in M.
Finite State Machine
(λx. M)N ⇒ β M [ x α N ]
1
β-Reduction
Evaluation Rules (the source of all computation)
α-reduction (renaming)
λy. M ⇒α λv. (M [y α v]) (λx. M)N → M [ x α N ]
where v does not occur in M.
Replace all x’s in M
witn N
β-reduction (substitution)
(λx. M)N ⇒ β M [ x α N ]
Note the syntax is different from Scheme:
(λx.M)N → ((lambda (x) M) N)
Lecture 39: Lambda Calculus 7 Lecture 39: Lambda Calculus 8
2
Alternate Answer Be Very Afraid!
(λ f. ((λ x.f (xx)) (λ x. f (xx)))) (λz.z) • Some λ-calculus terms can be β-reduced
→β (λx.(λz.z)(xx)) (λ x. (λz.z)(xx)) forever!
→β (λx.xx) (λx.(λz.z)(xx)) • The order in which you choose to do the
→β (λx.xx) (λx.xx) reductions might change the result!
→β (λx.xx) (λx.xx)
→β ...
¬(, #, L
corresponds to a computation:
input on the tape is
2: look
1
for (
Start
expression
every Turing Machine, all we need to do is Finite State Machine
• Normal form is that value of
show it can simulate a Universal Turing that computation: output is
Machine the normal form
• How do we simulate the FSM?
3
Simulating Computation
z z z z z z z z z z z z z z z z z z z z Making “Primitives”
from Only Glue (λ)
¬), #, R
), X, L
Read/Write Infinite Tape
Mutable Lists
¬(, #, L
2: look
1
for (
Start
4
Lambda Calculus is a Universal Computer? What is 42?
z z z z z z z z z z z z z z z z z z z z
¬), #, R
), X, L
• Read/Write Infinite Tape
Mutable Lists 42
forty-two
¬(, #, L
Start
(, X, R
Numbers
HALT
#, 0, - • Processing
#, 1, -
Is this enough?
Can we define add with pred, succ, Can we define lambda terms
zero? and zero?
that behave like
zero, zero?, pred and succ?
add ≡ λxy.if (zero? x) y
(add (pred x) (succ y)) Hint: what if we had cons, car and cdr?
5
Numbers are Lists... Making Pairs
null? null → λx.(x λy.λz.F) (λx. T) null? (cons M N) → λx.(x λy.λz.F) λz.zMN
→ β (λx. T)(λy.λz.F) → β (λz.z MN)(λy.λz.F)
→βT → β (λy.λz.F) MN
→βF
6
42 = λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
Counting λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
0 ≡ null λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
1 ≡ cons F 0 λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
2 ≡ cons F 1 λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
3 ≡ cons F 2 λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
... λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
succ ≡ λx.cons F x λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
pred ≡ λx.cdr x λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y λxy.(λz.z xy) λxy. y
λxy.(λz.z xy) λxy. y λx.T
Lecture 39: Lambda Calculus 37 Lecture 39: Lambda Calculus 38
2: look
¬(, #, L →β (λx.(λz.z)(xx)) (λ x. (λz.z)(xx))
1
for (
(, X, R
Mutable Lists
→β (λx.(λz.z)(xx)) (λ x.(λz.z)(xx))
HALT
#, 1, - #, 0, -
), X, L
| (term)
Start
HALT
#, 0, -
Mutable Lists
• Finite State Machine
Start
1
(, X, R
2: look
for (
¬(, #, L
≡ . term
λy. M ⇒α λv. (M [y α v])
Finite State Machine
Numbers to keep track of state #, 1, -
HALT
#, 0, -
where v does not occur in M.
• Processing Finite State Machine
(λx. M)N ⇒ β M [ x α N ]
Way of making decisions (if)
Way to keep going Turing Machine Lambda Calculus
7
Universal Computer Charge
• Lambda Calculus can simulate a Turing
Machine • Wednesday: Non-Deterministic Computing
– Everything a Turing Machine can compute, (P vs. NP question)
Lambda Calculus can compute also
• Turing Machine can simulate Lambda • Qualify for ps9 presentation by midnight
Calculus (we didn’t prove this) Friday night
– Everything Lambda Calculus can compute, a
Turing Machine can compute also
• Church-Turing Thesis: this is true for any
other mechanical computer also
Lecture 39: Lambda Calculus 43 Lecture 39: Lambda Calculus 44