0% found this document useful (0 votes)
66 views8 pages

Equivalent Computers: Lambda Calculus

The document summarizes lambda calculus, which consists of rules for manipulating symbolic expressions that can be interpreted as representing computations. Specifically, it discusses the concepts of α-reduction (renaming variables), β-reduction (substituting arguments for parameters), evaluating expressions by repeatedly applying reductions until reaching normal form, and provides some examples of lambda expressions and their reductions.

Uploaded by

emanuel maya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views8 pages

Equivalent Computers: Lambda Calculus

The document summarizes lambda calculus, which consists of rules for manipulating symbolic expressions that can be interpreted as representing computations. Specifically, it discusses the concepts of α-reduction (renaming variables), β-reduction (substituting arguments for parameters), evaluating expressions by repeatedly applying reductions until reaching normal form, and provides some examples of lambda expressions and their reductions.

Uploaded by

emanuel maya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Equivalent Computers

Lecture 39: z z z z z z z ... term = variable


Lambda | term term
| (term)
Calculus ¬), #, R
), X, L

| λ 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 ]

Turing Machine Lambda Calculus


CS150: Computer Science
University of Virginia David Evans
Computer Science http://www.cs.virginia.edu/evans Lecture 39: Lambda Calculus 2

What is Calculus? Real Definition


• A calculus is just a bunch of rules for
• In High School:
manipulating symbols.
d/dx xn = nxn-1 [Power Rule]
• People can give meaning to those
d/dx (f + g) = d/dx f + d/dx g [Sum Rule] symbols, but that’s not part of the
calculus.
Calculus is a branch of mathematics that • Differential calculus is a bunch of rules
deals with limits and the differentiation for manipulating symbols. There is an
and integration of functions of one or interpretation of those symbols
more variables... corresponds with physics, slopes, etc.
Lecture 39: Lambda Calculus 3 Lecture 39: Lambda Calculus 4

Lambda Calculus Why?


• Rules for manipulating strings of • Once we have precise and formal rules for
symbols in the language: manipulating symbols, we can use it to
term = variable reason with.
| term term • Since we can interpret the symbols as
| (term) representing computations, we can use it
| λ variable . term to reason about programs.
• Humans can give meaning to those
symbols in a way that corresponds to
computations.
Lecture 39: Lambda Calculus 5 Lecture 39: Lambda Calculus 6

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

Evaluating Lambda Expressions Some Simple Functions


• redex: Term of the form (λx. M)N I ≡ λx.x
Something that can be β-reduced C ≡ λxy.yx
Abbreviation for λx.(λy. yx)
• An expression is in normal form if it
contains no redexes (redices). CII = (λx.(λy. yx)) (λx.x) (λx.x)
→β (λy. y (λx.x)) (λx.x)
• To evaluate a lambda expression, keep
→β λx.x (λx.x)
doing reductions until you get to normal
form. →β λx.x
=I

Lecture 39: Lambda Calculus 9 Lecture 39: Lambda Calculus 10

Example Possible Answer

(λ f. ((λ x.f (xx)) (λ x. f (xx)))) (λz.z)


λ f. ((λ x.f (xx)) (λ x. f (xx))) →β (λx.(λz.z)(xx)) (λ x. (λz.z)(xx))
→β (λz.z) (λ x.(λz.z)(xx)) (λ x.(λz.z)(xx))
→β (λx.(λz.z)(xx)) (λ x.(λz.z)(xx))
→β (λz.z) (λ x.(λz.z)(xx)) (λ x.(λz.z)(xx))
→β (λx.(λz.z)(xx)) (λ x.(λz.z)(xx))
→β ...

Lecture 39: Lambda Calculus 11 Lecture 39: Lambda Calculus 12

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)
→β ...

Lecture 39: Lambda Calculus 13 Lecture 39: Lambda Calculus 14

Take on Faith (until CS655) Universal Language


• All ways of choosing reductions that reduce • Is Lambda Calculus a universal language?
a lambda expression to normal form will – Can we compute any computable algorithm
produce the same normal form (but some using Lambda Calculus?
might never produce a normal form). • To prove it isn’t:
• If we always apply the outermost lambda – Find some Turing Machine that cannot be
first, we will find the normal form if there is simulated with Lambda Calculus
one. • To prove it is:
– This is normal order reduction – corresponds to – Show you can simulate every Turing Machine
normal order (lazy) evaluation using Lambda Calculus

Lecture 39: Lambda Calculus 15 Lecture 39: Lambda Calculus 16

Simulating Every Turing


Simulating Computation
Machine z z z z z z z z z z z z z z z z z z z z

• A Universal Turing Machine can simulate • Lambda expression


every Turing Machine ¬), #, R
), X, L

¬(, #, L
corresponds to a computation:
input on the tape is
2: look
1
for (

Start

transformed into a lambda


(, X, R

• So, to show Lambda Calculus can simulate #, 1, -


HALT
#, 0, -

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?

Lecture 39: Lambda Calculus 17 Lecture 39: Lambda Calculus 18

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

(, X, R Finite State Machine


#, 1, -
HALT
#, 0, -
Numbers
Finite State Machine
Processing
Way to make decisions (if)
Way to keep going

Lecture 39: Lambda Calculus 19 Lecture 39: Lambda Calculus 20

In search of the truth? Don’t search for T, search for if

• What does true mean? T ≡ λx (λy. x)


• True is something that when used as the
first operand of if, makes the value of the
≡ λxy. x
if the value of its second operand:
if T M N → M
F ≡ λx (λ y. y))
if ≡ λpca . pca
Lecture 39: Lambda Calculus 21 Lecture 39: Lambda Calculus 22

Finding the Truth and and or?


T ≡ λx . (λy. x)
F ≡ λx . (λy. y) and ≡ λx (λy. if x y F))
if ≡ λp . (λc . (λa . pca))) Is the if necessary? or ≡ λx (λy. if x T y))
if T M N
((λpca . pca) (λxy. x)) M N
→β (λca . (λx.(λy. x)) ca)) M N
→β →β (λx.(λy. x)) M N
→β (λy. M )) N →β M

Lecture 39: Lambda Calculus 23 Lecture 39: Lambda Calculus 24

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

• Finite State Machine


2: look
1
for (

Start

(, X, R
Numbers
HALT
#, 0, - • Processing
#, 1, -

Finite State Machine Way to make decisions (if)


Way to keep going
XLII
cuarenta y dos

Lecture 39: Lambda Calculus 25 Lecture 39: Lambda Calculus 26

Meaning of Numbers Meaning of Numbers


• “42-ness” is something who’s pred (succ N) → N
successor is “43-ness”
succ (pred N) → N
• “42-ness” is something who’s
predecessor is “41-ness” succ (pred (succ N)) → succ N
• “Zero” is special. It has a successor
“one-ness”, but no predecessor. zero? zero → T
zero? (succ zero) → F

Lecture 39: Lambda Calculus 27 Lecture 39: Lambda Calculus 28

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?

Lecture 39: Lambda Calculus 29 Lecture 39: Lambda Calculus 30

5
Numbers are Lists... Making Pairs

zero? ≡ null? (define (make-pair x y)


pred ≡ cdr (lambda (selector) (if selector x y)))

succ ≡ λ x . cons F x (define (car-of-pair p) (p #t))


(define (cdr-of-pair p) (p #f))
The length of the list corresponds to the number value.

Lecture 39: Lambda Calculus 31 Lecture 39: Lambda Calculus 32

cons and car cdr too!


cons ≡ λx.λy.λz.zxy cons ≡ λxyz.zxy
cons M N = (λx.λy.λz.zxy) M N
car ≡ λp.p T
→ β (λy.λz.zMy) N
→ β λz.zMN cdr ≡ λp.p F
car ≡ λp.p T T ≡ λxy. x cdr cons M N
car (cons M N) ≡ car (λz.zMN) ≡ (λp.p T) (λz.zMN) cdr λz.zMN = (λp.p F) λz.zMN
→ β (λz.zMN) T → β TMN → β (λz.zMN) F
→ β (λxy. x) MN
→ β FMN
→ β (λy. M)N
→βN
→βM

Lecture 39: Lambda Calculus 33 Lecture 39: Lambda Calculus 34

Null and null? Null and null?


null ≡ λx.T null ≡ λx.T
null? ≡ λx.(x λy.λz.F) null? ≡ λx.(x λy.λz.F)

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

Lecture 39: Lambda Calculus 35 Lecture 39: Lambda Calculus 36

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

Lambda Calculus is a Universal Computer Way to Keep Going


z z z z z z z z z z z z z z z z z z z z

(λ f. ((λ x.f (xx)) (λ x. f (xx)))) (λz.z)


), X, L
¬), #, R

2: look
¬(, #, L →β (λx.(λz.z)(xx)) (λ x. (λz.z)(xx))
1
for (

• Read/Write Infinite Tape →β (λz.z) (λ x.(λz.z)(xx)) (λ x.(λz.z)(xx))


Start

(, X, R

 Mutable Lists
→β (λx.(λz.z)(xx)) (λ x.(λz.z)(xx))
HALT
#, 1, - #, 0, -

Finite State Machine


• Finite State Machine
 Numbers to keep track of state →β (λz.z) (λ x.(λz.z)(xx)) (λ x.(λz.z)(xx))
• Processing
We have this, but →β (λx.(λz.z)(xx))
This (λ x.(λz.z)(xx))
should give you some belief that we
 Way of making decisions (if)
we cheated using ≡ might be able to do it. We won’t cover
☯ Way to keep going
to make recursive →β ... the details of why this works in this class.
definitions!

Lecture 39: Lambda Calculus 39 Lecture 39: Lambda Calculus 40

Lambda Calculus is a Universal Computer Equivalent Computers


z z z z z z z z z z z z z z z z z z z z

z z z z z z z ... term = variable


¬), #, R
), X, L | term term
1
2: look
for (
¬(, #, L

), X, L
| (term)
Start

• Read/Write Infinite Tape


¬), #, R
| λ variable
#, 1, -
(, X, R

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

Lecture 39: Lambda Calculus 41 Lecture 39: Lambda Calculus 42

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

You might also like