Lambda 2
Lambda 2
Stephen A. Edwards
Columbia University
Fall 2012
Lambda Expressions
Function application written in prefix form. “Add four and five” is
(+ 4 5)
(+ (∗ 5 6) (∗ 8 3)) → (+ (∗ 5 6) 24)
→ (+ 30 24)
→ 54
f x
(+ x)
(+ 3 4) = ((+ 3) 4)
→ 7
Lambda Abstraction
(λx . + x 1)
( λ x . + x 1)
↑ ↑ ↑ ↑ ↑ ↑
That function of x that adds x to 1
The Syntax of the Lambda Calculus
λx. f g x = λx.( f g )x
¡ ¢
Beta-Reduction
(λx . + x x) 4 → (+ 4 4)
→ 8
or not at all
(λx . 3) 5 → 3
Beta-Reduction
(λ f . f 3) (λx . + x 1) → (λx . + x 1) 3
→ (+ 3 1)
→ 4
Free and Bound Variables
(λx . + x y) 4
(λx . + x y)
(+ x y)
Beta-Reduction More Formally
(λx . E ) F →β E 0
(λx . + (− x 1)) x 3
so
(λx . (λx . + (− x 1)) x 3) 9 → (λ x . + (− x 1)) 9 3
→ + (− 9 1) 3
→ +83
→ 11
Another Example
³ ¢´ ³ ¢´
λx . λy . + x (λx . − x 3) y 5 6 → λy . + 5 (λx . − x 3) y 6
¡ ¡
¡ ¢
→ + 5 (λx . − x 3) 6
→ + 5 (− 6 3)
→ +53
→ 8
Alpha-Conversion
One way to confuse yourself less is to do α-conversion: renaming a
λ argument and its bound variables.
Formal parameters are only names: they are correct if they are
consistent.
(λx . (λx . + (− x 1)) x 3) 9 ↔ (λx . (λy . + (− y 1)) x 3) 9
→ ((λy . + (− y 1)) 9 3)
→ (+ (− 9 1) 3)
→ (+ 8 3)
→ 11
+ 4 1 ← (λx . + x 1) 4
(λx . + 1 x) ↔η (+ 1)
(λx . F x) ↔η F
(λz . z z) (λz . z z)
(λx . λy . y) ( · · · )
However,
(λx . λy . y) ( · · · ) → (λy . y)
Normal Form
λy . y
(λz . z z) (λz . z z)
Not all expressions have normal forms, but is there a reliable way to
find the normal form if it exists?
µ³ ¶
¢´ ¡
λx . (λw . λz . + w z) 1 (λx . x x) (λx . x x) (λy . + y 1) (+ 2 3)
¡ ¢ ¡ ¢
leftmost outermost
λx λy
leftmost innermost λx λx 3
λw 1 1 + 2
x x x x + y
λz
z
+ w
Boolean Logic in the Lambda Calculus
“Church Booleans”
true = λx . λy . x
false = λx . λy . y
ifelse = λp . λa . λb . p a b
E.g.,
and = λp . λq . p q p
or = λp . λq . p p q
not = λp . λa . λb . p b a
0 = λf . λx . x
1 = λf . λx . f x
2 = λf . λx . f ( f x)
λf . λx . f
¡ ¢
3 = f ( f x)
succ = λn . λ f . λx . f (n f x)
succ 2 = λn . λ f . λx . f (n f x) 2
¡ ¢
→ λ f . λx . f (2 f x)
³¡ ´
= λ f . λx . f λ f . λx . f ( f x) f x
¢
→ λ f . λx . f f ( f x)
¡ ¢
= 3
Adding Church Numerals
λm.λn.λ f .λx. m f ( n f x) 3 2
¡ ¢
plus 3 2 =
→ λ f .λx. 3 f ( 2 f x)
→ λ f .λx. f ( f ( f (2 f x)))
→ λ f .λx. f ( f ( f ( f ( f x))))
= 5
mult = λm.λn.λ f .m (n f )
λm.λn.λ f .m (n f ) 2 3
¡ ¢
mult 2 3 =
→ λ f .2 (3 f )
= λ f . 2 (λx. f ( f ( f x)))
↔α λ f . 2 (λy. f ( f ( f y)))
→ λ f . λx. (λy. f ( f ( f y))) ((λy. f ( f ( f y))) x)
→ λ f . λx. (λy. f ( f ( f y))) ( f ( f ( f x)))
→ λ f . λx. f ( f ( f ( f ( f ( f x))) ))
= 6
This does not work: functions are unnamed in the lambda calculus.
But it is possible to express recursion as a function.
H = λ f . λn . if (= n 0) 1 (∗ n ( f (− n 1)))
Recursion
Let’s invent a function Y that computes fac from H , i.e., fac = Y H :
fac = H fac
Y H = H (Y H )
fac 1 = Y H1
= H (Y H ) 1
= (λ f . λn . if (= n 0) 1 (∗ n ( f (− n 1)))) (Y H ) 1
→ (λn . if (= n 0) 1 (∗ n ((Y H ) (− n 1)))) 1
→ if (= 1 0) 1 (∗ 1 ((Y H ) (− 1 1)))
→ ∗ 1 (Y H 0)
= ∗ 1 (H (Y H ) 0)
= ∗ 1 ((λ f . λn . if (= n 0) 1 (∗ n ( f (− n 1)))) (Y H ) 0)
→ ∗ 1 ((λn . if (= n 0) 1 (∗ n (Y H (− n 1)))) 0)
→ ∗ 1 (if (= 0 0) 1 (∗ 0 (Y H (− 0 1))))
→ ∗11
→ 1
The Y Combinator
Here’s the eye-popping part: Y can be a simple lambda expression.
Y =
= λ f . λx . f (x x) λx . f (x x)
¡ ¢¡ ¢
³ ¢´
Y H = λ f . λx . f (x x) λx . f (x x) H
¡ ¢¡
→ λx³ . H (x x) λx . H (x x)
¡ ¢¡ ¢
¢´
→ H λx . H (x x) λx . H (x x)
¡ ¢¡
³³ ¢´ ´
↔ H λ f . λx . f (x x) λx . f (x x) H
¡ ¢¡
= H (Y H )
1903–1995
Professor at Princeton (1929–1967)
and UCLA (1967–1990)
Invented the Lambda Calculus
†
Turing award winners
Turing Machines vs. Lambda Calculus
In 1936,
Ï Alan Turing invented the Turing
machine
Ï Alonzo Church invented the
lambda calculus
In 1937, Turing proved that the two models were equivalent, i.e.,
that they define the same class of computable functions.
Modern processors are just overblown Turing machines.
Functional languages are just the lambda calculus with a more
palatable syntax.