University of Virginia, Department of Computer Science CS200: Computer Science, Spring 2002

Notes: 22 April 2002

Schedule
• Today and tomorrow: Problem Set 8 (Progress Meetings)
• Monday, 29 April: Problem Set 8 (Final)
• Monday, 6 May: Final Due (will be handed out Monday 29 April)
Lambda Calculus

Lambda Calculus Term Grammar

term ::= variable | term term | ( term ) | λ variable . term

Rules

Alpha Reduction: (renaming variables)
λ y . Mα λ v . M [y |→ v]) where v does not occur in M.
We can can change the name of a lambda variable, but replacing all occurances of the variable in the body term with a new name that does not appear in the body term.

Beta Reduction: (substitution)

x . M) Nβ M [ x |→ N ]

Making "Primitives" out of nothing but Glue

T ≡ λ xy . x)
F ≡ λ xy . y
if ≡ λ pca . pca

cons ≡ λ xy .z . zxy)
car ≡ λ p . p T
cdr ≡ λ p . p F

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

0null
1cons F null
2cons 1 null

succ ≡ λ x . cons F x
pred ≡ λ x cdr x
zero?null?

Fixed Points

What is a fixed point?

All Lambda Calculus Terms Have Fixed Points

For any Lambda Calculus term F, there exists a Lambda Calculus Term X such that FX = X.

Proof: W = λ x. F(xx)
X = WW
X = (λ x. F (xx)) (λ x. F (xx))
⇒β F ((λ x. F (xx)) (λ x. F (xx))) = FX

Y-Operator
Y ≡ λ f.x.f (xx)) (λ x.f (xx))
The Y-Operator calculates the fixed point of any Lambda Calculus term!

Try this in Scheme:

```(define Y
(lambda (f)
((lambda (x) (f (lambda (y) ((x x) y))))
(lambda (x) (f (lambda (y) ((x x) y)))))))
(define f (lambda (g) (lambda (n) (if (= n 0) 1 (* n (g (- n 1)))))))
((Y f) 5)
```
Why do we need the lambda (y) to make this work in Scheme?

Hint: In LazyScheme, it would work with:

```(define Y
(lambda (f)
((lambda (x) (f (x x)))
(lambda (x) (f (x x))))))
```
How do the evaluation rules of regular Scheme differ from those of Lambda Calculus?