0% found this document useful (0 votes)
5 views30 pages

Chap4 Lambda Calculus

This document provides an overview of lambda calculus, a foundational concept in functional programming, detailing its history, principles, and applications. It covers topics such as calculating with lambda calculus, reduction techniques, and the implementation of built-in constants and functions. The conclusion emphasizes the power and minimalism of lambda calculus as a model of computation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views30 pages

Chap4 Lambda Calculus

This document provides an overview of lambda calculus, a foundational concept in functional programming, detailing its history, principles, and applications. It covers topics such as calculating with lambda calculus, reduction techniques, and the implementation of built-in constants and functions. The conclusion emphasizes the power and minimalism of lambda calculus as a model of computation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

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

03 IMPLEMENTING BUILT-IN CONSTANTS & FUNCTIONS

04 CONCLUSION

2
Brief History

● Origin: formal theory of substitution


○ For first-order logic, etc.
● More successful with computable functions
○ Substitution → symbolic computation
○ Church-Turing thesis
● Influenced design of Lisp, ML, other languages
Alonzo Church invented
● Important part of CS history and foundations lambda calculus in 1936.

3
Church-Turing Test

Any natural / reasonable notion of computation


realizable in the physical world can be simulated by
a TM (or equivalently, by lambda calculus).

4
CALCULATING WITH
01 LAMBDA CALCULUS

5
What is Functional Language?

● “No side effects”


● Pure functional language: a language with functions, but
without side effects or other imperative features

6
No-Side-Effects Language Test
Within the scope of specific declarations of x1, x2, …, xn, all occurrences of

an expression e containing only variables x1, x2, …, xn, must have the same
value.
begin
integer x=3; integer y=4;
5*(x+y)-3
… // no new declaration of x or y //
4*(x+y)+1
end
7
Expressions and Functions
● Expressions
x + y x + 2*y + z
● Functions
λx.(x+y) λz.(x+2*y+z)
● Application
(λx.(x+y)) 3 = 3 + y
(λz.(x+2*y+z)) 5 = x + 2*y + 5
● Parsing: λx.f (f x) = λx.(f (f(x)))

8
Terminology

● λx.t

the scope of x is the term t

● λx.x y
y is free in the term
λx.x y
x is bound in the term
λx.x y

9
Free & Bound Variables (1)

● Bound variable is a “placeholder”


○ Variable x is bound in λx.(x+y)
○ Function λx.(x+y) is same function as λz.(z+y)
● Name of free (i.e., unbound) variable matters!
○ Variable y is free in λx.(x+y)
○ Function λx.(x+y) is not same as λx.(x+z)
● Occurrences: y is free and bound in λx.((λy.y+2) x) + y

10
Free & Bound Variables (2)

● In λ Calculus all variables are local to function definitions.


● Examples:
○ λx.xy: x is bound, while y is free;
○ (λx.x)(λy.yx): x is bound in the first function, but free in the
second function
○ λx.(λy.yx): x and y are both bound variables. (it can be
abbreviated as λxy.yx)

11
02 REDUCTION

12
CBV Operational Semantics

● Single-step, call-by-value OS: t → t’


○ Values are: v ::= λx.t
○ Primary rule (β-reduction): (λx.t)v → t[v/x]
○ t[v/x] is the term in which all occurrences of x in t are replaced
with v
○ This replacement operation is called substitution

13
Examples

Example 1: (λx.x x)(λy.y) → x x [λy.y/x]


== (λy.y)(λy.y)
→ y [λy.y/y]
== λy.y
Example 2: (λx.x x)(λx.x x) → x x [λx.x x/x]
== (λx.x x)(λx.x x)

14
Higher-order Functions

● Given function f, return function f ○ f: λf.λx.f (f x)


● How does this work?
(λf.λx.f (f x))(λy.y+1)
= λx.(λy.y+1)((λy.y+1)x)
= λx.(λy.y+1)(x+1)
= λx.(x+1)+1

15
Declaration of “Syntactic Sugar”

def f(x):
return x + 2
print(f(5)) # Output: 7
(λf.f(5))(λx.x+2)

block body declared func

16
Rules

● α-reduction (renaming)
○ λy.M ⇒α λv.(M [y v])
where v does not occur in M.
● β-reduction (substitution)
○ (λx.M) N ⇒β M [x N]

17
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!

18
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)...

19
IMPLEMENTING BUILT-IN
03 CONSTANTS &
FUNCTIONS

20
Building in Constants and Functions (1)
We can represent things, like for example, numbers and booleans using λ-abstractions.
Example:
● We can encode booleans, consider representing “true” and “false” as functions
named “tru” and “fls”
● How do we define these functions? Think about how “true” and “false” can be used.
● They can be used by a testing function:
○ “test b then else” returns “then” if b is true and returns “else” if b is false.
○ The only thing the implementation of test will do with b is to apply it.
○ The functions “tru” and “fls” must distinguish themselves when they are
applied.
21
Building in Constants and Functions (2)

The encoding:
tru = λt.λf.t
fls = λt.λf.f
test = λx.λthen.λelse.x then else
Example:
test tru (λx.t1) (λx.t2)
→* (λt.λf.t) (λx.t1) (λx.t2)
→* λx.t1

22
Building in Constants and Functions (3)

tru = λt.λf.t fls = λt.λf.f


and = λb.λc.b c fls
and tru tru
→* tru tru fls
→* tru
and fls fls
→* fls tru fls
→* fls

23
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!
24
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

25
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

26
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))

27
04 CONCLUSION

28
Conclusion

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


● Encodes numbers, booleans, data structures, and even control flow.
● In lambda calculus, some expressions can lead to infinite loops.

29
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy