CS150: Computer Science
University of Virginia
Computer Science
Lecture 39:
Lambda
Calculus
David Evans http://www.cs.virginia.edu/evans
Equivalent Computers z z z z z z z ...
), #, R
Start
#, 1, -
1
), X, L
(, #, L
2: look for (
(, X, R
HALT
#, 0, -
Finite State Machine
Turing Machine
term = variable
| term term
| (term)
|
variable
. term
y. M
v. ( M [ y v ]) where v does not occur in M .
(
x. M ) N
M [ x N ]
Lambda Calculus
Lecture 39: Lambda Calculus 2
What is Calculus?
• In High School: d/dx x n = nx n-1 [Power Rule] d/dx (f + g) = d/dx f + d/dx g [Sum Rule]
Calculus is a branch of mathematics that deals with limits and the differentiation and integration of functions of one or more variables...
Lecture 39: Lambda Calculus 3
Real Definition
• A calculus is just a bunch of rules for manipulating symbols.
• People can give meaning to those symbols, but that’s not part of the calculus.
• Differential calculus is a bunch of rules for manipulating symbols. There is an interpretation of those symbols corresponds with physics, slopes, etc.
Lecture 39: Lambda Calculus 4
Lambda Calculus
• Rules for manipulating strings of symbols in the language: term = variable
| term term
| (term)
|
variable . term
• Humans can give meaning to those symbols in a way that corresponds to computations.
Lecture 39: Lambda Calculus 5
Why?
• Once we have precise and formal rules for manipulating symbols, we can use it to reason with.
• Since we can interpret the symbols as representing computations, we can use it to reason about programs.
Lecture 39: Lambda Calculus 6
Evaluation Rules
-reduction
y. M
(renaming)
v. ( M [ y v ]) where v does not occur in M .
-reduction
(
x. M ) N
(substitution)
M [ x N ]
Note the syntax is different from Scheme:
(
x .
M ) N
((lambda (x) M) N)
Lecture 39: Lambda Calculus 7
Reduction
(the source of all computation)
(
x. M ) N
M [ x N ]
Replace all x
’s in
M witn
N
Lecture 39: Lambda Calculus 8
Evaluating Lambda Expressions
• redex : Term of the form
(
x. M ) N
Something that can be reduced
• An expression is in normal form contains no redexes ( redices ).
if it
• To evaluate a lambda expression, keep doing reductions until you get to normal form .
Lecture 39: Lambda Calculus 9
Some Simple Functions
I
x .
x
C
xy .
yx
Abbreviation for
x .(
y . yx )
CII = (
x .(
y . yx )) (
x .
x ) (
x .
x )
(
x x y
.
.
. x x y
(
(
x .
x x
.
) x )) (
x .
x )
= I
Lecture 39: Lambda Calculus 10
Example
f. ((
x
.
f ( xx )) (
x. f ( xx )))
Lecture 39: Lambda Calculus 11
Possible Answer
...
(
f. ((
x .
f ( xx )) (
x. f ( xx )))) (
z.z
)
(
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 12
Alternate Answer
(
(
(
(
f. ((
x
.
f ( xx )) (
x. f ( xx )))) (
z.z
)
(
x
.
xx ) (
x.xx
)
...
x x x
.
.
.
(
xx xx z.z
) (
) (
)(
xx x.
(
)) (
x.xx
) z.z
)( x. xx
(
))
z.z
)( xx ))
Lecture 39: Lambda Calculus 13
Be Very Afraid!
• Some -calculus terms can be -reduced forever!
• The order in which you choose to do the reductions might change the result!
Lecture 39: Lambda Calculus 14
Take on Faith (until CS655)
• All ways of choosing reductions that reduce a lambda expression to normal form will produce the same normal form (but some might never produce a normal form).
• If we always apply the outermost lambda first , we will find the normal form if there is one.
– This is normal order reduction normal order (lazy) evaluation
– corresponds to
Lecture 39: Lambda Calculus 15
Universal Language
• Is Lambda Calculus a universal language ?
– Can we compute any computable algorithm using Lambda Calculus?
• To prove it isn’t:
– Find some Turing Machine that cannot be simulated with Lambda Calculus
• To prove it is:
– Show you can simulate using Lambda Calculus every Turing Machine
Lecture 39: Lambda Calculus 16
Simulating Every Turing Machine
• A Universal Turing Machine can simulate every Turing Machine
• So, to show Lambda Calculus can simulate every Turing Machine, all we need to do is show it can simulate a Universal Turing
Machine
Lecture 39: Lambda Calculus 17
z z z z
Simulating Computation z z z z z z z z z z z z z z z z
), #, R
Start
#, 1, -
1
), X, L
(, X, R
2: look for
(
(, #, L
HALT
#, 0, -
Finite State Machine
• Lambda expression corresponds to a computation: input on the tape is transformed into a lambda expression
• Normal form is that value of that computation: output is the normal form
• How do we simulate the FSM?
Lecture 39: Lambda Calculus 18
z z z z
Simulating Computation z z z z z z z z z z z z z z z z
), #, R
Start
#, 1, -
1
), X, L
(, X, R
2: look for
(
(, #, L
HALT
#, 0, -
Finite State Machine
Read/Write Infinite Tape
Mutable Lists
Finite State Machine
Numbers
Processing
Way to make decisions (if)
Way to keep going
Lecture 39: Lambda Calculus 19
Making “Primitives” from Only Glue ( )
Lecture 39: Lambda Calculus 20
In search of
?
• What does true mean?
• True is something that when used as the first operand of if, makes the value of the
if the value of its second operand: if T M N M
Lecture 39: Lambda Calculus 21
Don’t search for T, search for if
T
x (
y . x )
xy . x
F
x (
y . y )) if
pca . pca
Lecture 39: Lambda Calculus 22
Finding the Truth
T
x . (
y . x )
F
x . (
y . y ) if
p . (
c . (
a . pca ))) Is the if necessary?
if T M N
(( pca . pca ) (
xy . x )) M N
(
ca . (
x .(
y . x )) ca )) M N
(
x .(
y . x ))
(
y . M )) N
M N
M
Lecture 39: Lambda Calculus 23
and and or?
and
x (
y . if x y F )) or
x (
y . if x T y ))
Lecture 39: Lambda Calculus 24
Lambda Calculus is a Universal Computer?
z z z z z z z z z z z z z z z z z z z z
), #, R
Start
#, 1, -
1
), X, L
(, X, R
2: look for
(
(, #, L
HALT
#, 0, -
Finite State Machine
• Read/Write Infinite Tape
Mutable Lists
• Finite State Machine
Numbers
• Processing
Way to make decisions (if)
Way to keep going
Lecture 39: Lambda Calculus
...to be continued Wednesday...
25