Chap4_Lambda_Calculus
Chap4_Lambda_Calculus
CO2039
04 CONCLUSION
2
CALCULATING WITH
01 LAMBDA CALCULUS
3
Calculus?
4
A functional program is an expression which is ‘executed’
by evaluating it
For example: (+ 4 5)
+ is a function. We write functions in prefix form.
Another example: (+ (* 5 6) (* 8 3))
Evaluation proceeds by choosing a reducible expression and reducing it.
(+ (* 5 6) (* 8 3)) ⇒ (+ 30 (* 8 3)) ⇒ (+ 30 24) ⇒ 54
Note: There may be more than one order.
5
Function Application and Currying
6
Function Application and Currying (cont.)
7
Writing Your Own Functions - Lambda Abstractions
A way to write expressions that denote functions.
Example: (λx. + x 1)
● λ means here comes a function
● Then comes the parameter x
● Then comes the .
● Then comes the body + x 1
● The function is ended by the ) (or end of string)
Another example: (λx. + x 1) is the increment function.
(λx. + x 1) 5 ⇒ 6
8
Implied Parentheses In Lambda Expressions
9
Bound & Unbound Variables
10
REDUCTION & CHURCH-
02 ROSSER THEOREM
11
β-Reduction (aka λ expression evaluation)
Consider this lambda expression:
(λx. + x 1) 4
This juxtaposition of (λx. + x 1) with 4 means to apply the lambda abstraction
(λx. + x 1) to the argument 4.
Here is how we do it:
“The result of applying a lambda abstraction to an argument is an instance of the
body of the lambda abstraction in which bound occurrences of the formal parameter in the
body are replaced with copies of the argument.”
Example: We put the 4 in for the x in + x 1. The result is + 4 1.
(λx. + x 1) 4 ➞β + 4 1
12
β-Reduction (some more examples)
(λx. + x x) 5 ➞ + 5 5 ➞ 10
(λx. 3) 5 ➞ 3
(λx. (λy. - y x)) 4 5 ➞ (λy. - y 4) 5 ➞ - 5 4 ➞ 1
Note: the currying – we peel off the arguments 4 then 5.
(λf. f 3) (λx. + x 1) ➞ (λx. + x 1) 3 ➞ + 3 1 ➞ 4
(λx. (λx. + (- x 1)) x 3) 9 ➞ (λx. + (- x 1)) 9 3
➞ + ( - 9 1) 3 ➞ 11
13
Other Kinds of Conversions
There are some other (less interesting) kinds of reductions that we shall not
spend much time on.
● α-conversion: (λx. + x x) ➞α (λy. + y y)
14
Attention
15
Attention (cont.)
16
Church-Rosser Theorem
17
IMPLEMENTING BUILT-IN
03 CONSTANTS &
FUNCTIONS
18
Building in Constants and Functions
We can represent things, like for example, numbers and booleans using λ-abstractions.
λx.λy.x λx.λy.y
Let’s look at the first one. What does it do when applied?
(λx.λy.x) a b ➞ a
(λx.λy.y) a b ➞ b
If we call them TRUE and FALSE respectively, then we have replaced our IF construct.
IF FOO B1 B2 replaced by FOO B1 B2
TRUE = λx.λy.x FALSE = λx.λy.y
19
Numbers
0 := λf.λx. x
1 := λf.λx. f x
2 := λf.λx. f (f x)
3 := λf.λx. f (f (f x))
4 := λf.λx. f (f (f (f x)))
SUM := λm.λn.λf.λx. m f (n f x)
PROD := λm.λn.λf.λx. m (n f) x
SUBTRACT := λm.λn. n PRED m
⇒ Apply the predecessor function (PRED) n times to m
⇒ How to compute predecessor? Can implement using pairs!
20
Data Structures - Pair
21
Data Structures - List
Can turn our method of pairing arbitrary normal form expressions into building up lists.
All we need is a way to mark the end of the list (NIL) and a way to tell if we’re at the end
of the list (ISEMPTY).
NIL := λx.TRUE ISEMPTY := λp.p (λx.λy.FALSE)
ISEMPTY NIL = (λp.p (λx.λy.FALSE)) (λx.TRUE)
➞ (λx.TRUE) (λx.λy.FALSE) ➞ TRUE
ISEMPTY (PAIR E F) = (λp.p (λx.λy.FALSE)) (λf. f E F)
➞ (λf. f E F) (λx.λy.FALSE) ➞ (λx.λy.FALSE) E F ➞ FALSE
22
Predecessor
23
04 CONCLUSION
24
Conclusion
25
References
[1] Slides based on More Great Ideas in Theoretical Computer Science
course (Spring 2021).
https://www.cs.cmu.edu/~venkatg/teaching/15252-sp21/.
Last accessed: April 8th, 2025.
26
Thank you for your
attention!
https://www.cse.hcmut.edu.vn