0% found this document useful (0 votes)
2 views

Chap4_Lambda_Calculus

This document covers Lambda calculus, a foundational concept in functional programming, detailing its syntax, function application, and evaluation through β-reduction. It discusses the Church-Rosser theorem, which states that no expression can be converted into two distinct normal forms, and explores the implementation of built-in constants and functions. The conclusion emphasizes the power of Lambda calculus in encoding various computational concepts and its ability to simulate infinite loops.

Uploaded by

nhiep9145
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chap4_Lambda_Calculus

This document covers Lambda calculus, a foundational concept in functional programming, detailing its syntax, function application, and evaluation through β-reduction. It discusses the Church-Rosser theorem, which states that no expression can be converted into two distinct normal forms, and explores the implementation of built-in constants and functions. The conclusion emphasizes the power of Lambda calculus in encoding various computational concepts and its ability to simulate infinite loops.

Uploaded by

nhiep9145
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Advanced Programming

CO2039

Chapter 4: Lambda calculus


Part of the Functional Programming paradigm

ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH


TRƯỜNG ĐẠI HỌC BÁCH KHOA
TP.HCM, 03/01/2025
CONTENT

01 CALCULATING WITH LAMBDA CALCULUS

02 REDUCTION & CHURCH-ROSSER THEOREM

03 IMPLEMENTING BUILT-IN CONSTANTS & FUNCTIONS

04 CONCLUSION

2
CALCULATING WITH
01 LAMBDA CALCULUS

3
Calculus?

● Calculus = just a bunch of rules for manipulating symbols.


● Lambda calculus: rules for manipulating expressions of form
<exp> ::= <constant> Built-in constants & functions
| <variable> Variable names
| <exp> <exp> Application
| λ <variable> . <exp> Lambda abstractions
| (<exp>) Parens

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

Function application is indicated by juxtaposition: f x


“The function f applied to the argument x”
Q: What if we want a function of MORE than one argument?
A: We could invent a notation f(x, y), but there’s an alternative!
⇒ To express the sum of 3 and 4 we write: ((+ 3) 4)
The expression (+ 3) denotes the function that adds 3 to its argument.

6
Function Application and Currying (cont.)

All functions take ONE argument.


⇒ When we wrote (+ 3 4) this is shorthand for ((+ 3) 4).
Note: The arguments are associated to the left.
⇒ This mechanism is known as currying (in honor of Haskell Curry).
Parentheses can be added for clarity.
Example: ((f ((+ 4) 3)) (g x)) is identical to f (+ 4 3) (g x)

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

Functions associate to the left: f g x stands for (f g) x


Lambdas associate to the right:
λx. x 3 stands for λx. (x 3)
● Function which takes x and applies it to 3
Note: (λx. x) 3 = 3 (identity function applied to 3)

Example: λx.λy. x y (λz. z y)≡ λx.λy. ((x y) (λz. (z y)))


Note: Sometimes abbreviate λx.λy by λxy.

9
Bound & Unbound Variables

Consider this lambda expression:


(λx. + x y) 4
● x is a bound variable, because it is inside the body of the λx expression.
● y is an unbound or free variable, because it is not in a λy expression.
Q: Identify which variables is bound/unbound in this expression
+ x ((λx. + x 1) 4)

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)

(just renaming locally bound variables to avoid confusion)


● δ-reductions: applying the built in functions
● η-reductions: λf. Λx. f x ➞η λf. f

14
Attention

Some λ-calculus terms can be β-reduced forever!


Example:
● Let D be this expression: (λx. x x).
What is the value of this expression (D D)?
(D D) = (λx. x x) (λx. x x) ➞ (λx. x x) (λx. x x) ➞

⇒ It NEVER terminates!
The order in which you choose to do the reductions might change the result!

15
Attention (cont.)

Now consider this expression: (λx. 3) (D D)


1. Lazy Evaluation (Call-by-name): Only evaluate the argument if it’s needed.
⇒ No need to evaluate (D D) because x is not used inside the body 3
⇒ Skip evaluating D D entirely and return 3 immediately.
2. Eager Evaluation (Call-by-value): Evaluate the argument before calling the
function
⇒ Before applying (λx. 3) to (D D), we try to compute (D D)...

16
Church-Rosser Theorem

Church-Rosser Theorem: No expression can be converted into two distinct


normal forms.
(Modulo renaming .. i.e (λx. + x x) and (λy. + y y) the same.)
Note: evaluations for some ordering may not terminate, but will never terminate
leading to a different normal form.
Normal Order: do the leftmost outermost reduction first.
Church-Rosser Theorem 2: if E1 ➞* E2 and E2 is in normal form, then you can get
to E2 by reducing in normal order.

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

Let E, F be arbitrary normal form expressions.


A pair (E, F) can be represented as the normal form lambda expression:
λf. f E F
The function to construct a pair is then:
PAIR := λx.λy.λf. f x y
If p is a pair (E, F), i.e., λf. f E F then how do we get its parts?
⇒ p TRUE gives us E, and p FALSE gives us F.
FIRST := λp.p TRUE SECOND := λp.p FALSE

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

To compute m-n, apply the predecessor function n times to m.


SUBTRACT := λm.λn.n PRED m
Predecessor using pairs:
PRED := λn. FIRST (n NEXT (PAIR 0 0))
NEXT := λp. PAIR (SECOND p) (SUM 1 (SECOND p))

23
04 CONCLUSION

24
Conclusion

● Lambda calculus is a minimal yet powerful model of computation.


● Encodes numbers, booleans, data structures, and even control flow.
● β-reduction is the key computation step — function application.
● Church-Rosser Theorem ensures that different reduction paths converge, if
a normal form exists.
● Some expressions never reduce to a normal form – lambda calculus can
simulate infinite loops.

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

ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH


TRƯỜNG ĐẠI HỌC BÁCH KHOA

You might also like