Lambda calculus (also written as λ-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation that can be used to simulate any Turing machine and was first introduced by mathematician Alonzo Church in the 1930s as part of his research of the foundations of mathematics.
Lambda calculus consists of constructing lambda terms and performing reduction operations on them
2. • History
• Definition
• Syntax λ calculus
• Function in Lambda Calculus
• Reduction
• Encoding data type in λ calculus
• Order of Evaluation
• Functional Programming
2
3. History
It was introduced in 1930s by Alonzo Church
as a way of formalizing the concept of
effective computability.
λ calculus is a universal model of
computation equivalent to a Turing machine.
It also can be called the smallest universal
programming language of the world.
3
4. History
All function programming language can be
viewed as syncretic variation of λ calculus.
Functional languages such as Lisp, Scheme, FP,
ML, Miranda, and Haskell are an attempt to
realize Church's lambda calculus in practical
form as a programming language.
4
5. Definition
Lambda expressions are composed of:
Variables
The abstraction symbols lambda 'λ' and dot '.'
parentheses ‘(‘ , ‘)'
Note : variable is an identifier which can be any
of the letters a, b, c.
5
6. Syntax λ calculus
The set of λ-terms (notation Λ) is built up from
an infantine set of variables 𝑉 = {𝑣, 𝑣′
, 𝑣′′
, . . }
i. 𝑥 ∈ 𝑉
ii. 𝑀, 𝑁 ∈ Λ → 𝑀𝑁 ∈ Λ
iii. 𝑀 ∈ Λ, 𝑥 ∈ 𝑉 → λ𝑥𝑀 ∈ Λ
rule 2 are known as application and rule 3 are
known as abstraction .
6
8. Syntax λ calculus
Application associate from the left.
– x y z : (x y) z
Abstraction associate from the right.
– lx. x ly. x y z : l x. (x (ly. ((x y) z)))
Outermost parentheses are dropped.
8
9. Function in Lambda Calculus
Function in the λ calculus
focus on the rule for going from an argument to
result.
Ignore the issues of naming the function and its
domain and range.
Mathematical function : ∀𝑥 ∈ 𝐴, 𝑓 𝑥 = 𝑥
In the λ calculus : (λ𝑥. 𝑥)
Argument is called x, the output of the function is
x.
9
10. Function in Lambda Calculus
The application of the identity function to a :
λ𝑥. 𝑥 𝑎
The argument for the identify itself:
λ𝑥. 𝑥 λ𝑥. 𝑥
Function take an argument, ignore it and return the
identity function:
λy .(λ𝑥. 𝑥)
Function take an argument, return a function that
ignore its own argument and return the argument
original function:
λy .(λ𝑥. 𝑦)
10
11. Free Variables
The set of free variables M denoted by 𝐹𝑉 𝑀
𝐹𝑉 𝑥 = 𝑥
𝐹𝑉 λ𝑥. 𝑀 = 𝐹𝑉 𝑀 𝑥
𝐹𝑉 𝑀 𝑁 = 𝐹𝑉 𝑀 ∪ 𝐹𝑉 𝑁
Note : Variables that fall within the scope of an
abstraction are said to be bound. All other
variables are called free.
11
12. Substitution
Substituting N for the free occurrences of x in M
Denoted by M[x := N]
x[x := N] ≡ N
y[x := N] ≡ y, if x ≠ y
(M1 M2)[x := N] ≡ (M1[x := N]) (M2[x := N])
(λx.M)[x := N] ≡ λx.M(λy.M)[x := N] ≡ λy.(M[x := N]), if x ≠ y, provided y ∉ FV(N)
12
13. Lambda Reduction
𝛼 − 𝑟𝑒𝑑𝑢𝑐𝑡𝑖𝑜𝑛
allows bound variable names to be changed.
if x and y is variable and M is a λ expression:
λx . M → 𝛼 λy . M[x → 𝑦]
Example :
λ𝑦 . λ𝑓 . 𝑓 𝑥 𝑦 → 𝛼 λ𝑧 . λ𝑓 . 𝑓 𝑥 𝑧
λ𝑧 . λ𝑓 . 𝑓 𝑥 𝑧 → 𝛼 λ𝑧 . λ𝑔 . 𝑔 𝑥 𝑧
13
14. Lambda Reduction
𝛽 − 𝑟𝑒𝑑𝑢𝑐𝑡𝑖𝑜𝑛
applying functions to their arguments.
If x is variable and M and N are λ expression:
( λ𝑥. 𝑀 ) 𝑁 → 𝛽 𝑀[ 𝑥 → 𝑁]
Example:
( ( λn . n∗x ) y) → y∗x
14
16. Lambda Reduction
ɳ − 𝑟𝑒𝑑𝑢𝑐𝑡𝑖𝑜𝑛
which captures a notion of extensionality.
If x is variables and M is a λ expression , and x has
no free occurrence in M :
λ𝑥 . 𝐸 𝑥 →ɳ 𝐸
Example:
λ𝑥 . 𝑠𝑞𝑟 𝑥 →ɳ 𝑠𝑞𝑟
16
17. Lambda Reduction
The general reduction relation n is the union
of 𝛼 , 𝛽 and ɳ :
𝑛 = 𝛼 ∪ 𝛽 ∪ ɳ
When we use → 𝑛
𝛼
emphasize that
(→ 𝑛= → 𝑛
𝛼 ∪ → 𝑛
𝛽
∪ → 𝑛
ɳ
)
17
18. Encoding data type in λ calculus
Booleans
Boolean is a function that give two choices selects
one of them.
𝑡𝑟𝑢𝑒 = l 𝑥. l 𝑦. 𝑥
𝑓𝑎𝑙𝑠𝑒 = l 𝑥. l 𝑦. 𝑦
18
19. Encoding data type in λ calculus
Logic Operator
with two false term and true term, we can define
some logic operator:
and = λ𝑝. λ𝑞. 𝑝 𝑞 𝑝
or = λ𝑝. λ𝑞. 𝑝 𝑝 𝑞
not = λ𝑝. λ𝑎. λ𝑏. 𝑝 𝑏 𝑎
𝑖𝑓 = l 𝑣. l 𝑡. l 𝑓. 𝑣 𝑡 𝑓
Symbol f and t correspond with to “false” and
“true” .
19
20. Encoding data type in λ calculus
Logic Operator
For example :
𝑖𝑓 𝑡𝑟𝑢𝑒 𝑀 𝑁 = 𝑛 𝑀
𝑖𝑓 𝑡𝑟𝑢𝑒 𝑀 𝑁 = l 𝑣. l 𝑡. l 𝑓. 𝑣 𝑡 𝑓 l 𝑥. l 𝑦. 𝑥 𝑀 𝑁
→ 𝑛
𝑏 l 𝑡. l 𝑓. l 𝑥. l 𝑦. 𝑥 𝑡 𝑓 𝑀 𝑁
→ 𝑛
𝑏 l 𝑓. l 𝑥. l 𝑦. 𝑥 𝑀 𝑓 𝑁
→ 𝑛
𝑏
l 𝑥. l 𝑦. 𝑥 𝑀 𝑁
→ 𝑛
𝑏 l 𝑦. 𝑀 𝑁
→ 𝑛
𝑏 𝑀
20
21. Encoding data type in λ calculus
Natural Number
A natural number n is encoded by a function of
two arguments, f and x, where the function
applies f to x, n times.
0 ≔ λ𝑓. λ𝑥. 𝑥
1 ≔ λ𝑓. λ𝑥. 𝑓 𝑥
2 ≔ λ𝑓. λ𝑥. 𝑓 𝑓 𝑥
3 ≔ λ𝑓. λ𝑥. 𝑓 (𝑓 (𝑓 𝑥))
21
22. Encoding data type in λ calculus
Natural Number
the function add1 represent a number n and
produce a number n+1.
𝑎𝑑𝑑1 = λ𝑛. λ𝑓. λ𝑥. 𝑓 (𝑛 𝑓 𝑥 )
The function applies first argument to second
argument n+1.
22
23. Encoding data type in λ calculus
Natural Number
To add to numbers n and m, we apply add1 to n m
time
𝑎𝑑𝑑 = λ𝑛. λ𝑚. 𝑚 𝑎𝑑𝑑1 𝑛
Multiplication can be defined as
𝑚𝑢𝑙𝑡 = 𝑚. 𝑛. 𝑚 𝑎𝑑𝑑 𝑛 0
Iszero also
𝑖𝑠𝑧𝑒𝑟𝑜 = λ𝑛. 𝑛 λ𝑥. 𝑓𝑎𝑙𝑠𝑒 𝑡𝑟𝑢𝑒
23
24. Encoding data type in λ calculus
Pair
< 𝑀, 𝑁 > = λ𝑠. 𝑠 𝑀 𝑁
𝑚𝑘𝑝𝑎𝑖𝑟 = λ𝑥. λ𝑦. λ𝑠. 𝑠 𝑥 𝑦
𝑓𝑠𝑡 = λ𝑝. 𝑝 𝑡𝑟𝑢𝑒
𝑠𝑐𝑛𝑑 = λ𝑝. 𝑝 𝑓𝑎𝑙𝑠𝑒
A linked list can be defined as either NIL for the empty list, or
the pair of an element and a smaller list.
The predicate NULL tests for the value NIL.
𝑛𝑢𝑙𝑙 = λ𝑝. 𝑝 (λ𝑥. 𝑦. 𝑓𝑎𝑙𝑠𝑒)
𝑛𝑖𝑙𝑙 = λ𝑥. 𝑡𝑟𝑢𝑒
24
25. Encoding data type in λ calculus
Recursive Function
Is the definition of a function using the function itself.
𝐹 𝑛 = 1, 𝑖𝑓 𝑛 = 0; 𝑒𝑙𝑠𝑒 𝑛 ∗ 𝐹 𝑛 − 1
Can be defined in the λ calculus using a function
which calls a function y and generates itself.
𝑌 = (λ 𝑓. λ 𝑥. 𝑓 𝑥𝑥 λ 𝑥. 𝑓 (𝑥𝑥) )
25
26. Encoding data type in λ calculus
Recursive Function
Y f is the fixed point of f expand to :
𝑌 𝑓
λℎ. λ𝑥. ℎ 𝑥 𝑥 λ𝑥. ℎ 𝑥 𝑥 𝑓
λ𝑥. 𝑓 𝑥 𝑥 (λ𝑥. 𝑓 (𝑥 𝑥))
𝑓 ( λ𝑥. 𝑓 𝑥 𝑥 (λ𝑥. 𝑓 (𝑥 𝑥)))
𝑓 (𝑌 𝑓)
26
27. Encoding data type in λ calculus
Recursive Function
For example, given n=4 to factorial function:
(Y F) 4
F (Y F) 4
(λr.λn.(1, if n = 0; else n × (r (n−1)))) (Y F) 4
(λn.(1, if n = 0; else n × ((Y F) (n−1)))) 4
1, if 4 = 0; else 4 × ((Y F) (4−1))
4 × (F (Y F) (4−1))
4 × ((λn.(1, if n = 0; else n × ((Y F) (n−1)))) (4−1))
4 × (1, if 3 = 0; else 3 × ((Y F) (3−1)))
4 × (3 × (F (Y F) (3−1)))
27
28. Encoding data type in λ calculus
Recursive Function
4 × (3 × ((λn.(1, if n = 0; else n × ((Y F) (n−1)))) (3−1)))
4 × (3 × (1, if 2 = 0; else 2 × ((Y F) (2−1))))
4 × (3 × (2 × (F (Y F) (2−1))))
4 × (3 × (2 × ((λn.(1, if n = 0; else n × ((Y F) (n−1)))) (2−1))))
4 × (3 × (2 × (1, if 1 = 0; else 1 × ((Y F) (1−1)))))
4 × (3 × (2 × (1 × (F (Y F) (1−1)))))
4 × (3 × (2 × (1 × ((λn.(1, if n = 0; else n × ((Y F) (n−1)))) (1−1)))))
4 × (3 × (2 × (1 × (1, if 0 = 0; else 0 × ((Y F) (0−1))))))
4 × (3 × (2 × (1 × (1))))
24
28
29. Normal Form
An expression is a normal form if it can’t be
reduced by → 𝑛
𝛽
or → 𝑛
ɳ
.
M has normal form 𝑀 = 𝑛 𝑁 and N is a
normal form.
If 𝐿 = 𝑛 𝑀, 𝐿 = 𝑛 𝑁, and both M and N are
normal form, then 𝑀 = 𝛼 𝑁.
If an expression has a normal form, then may
be an infinite reduction sequence for the
expression that never reaches normal form.
29
30. Church-Rosser Theorem
Reduction in any way can eventually produce
the same result.
If 𝐸1 ↔ 𝐸2, then there exist an E such that
𝐸1 → 𝐸 and 𝐸2 → 𝐸.
If 𝐸1 → 𝐸2, and is a normal form, then there is
a normal-order reduction of 𝐸1to 𝐸2.
Normal-order reduction will always produce a
normal form, if one exists.
30
31. Order of Evaluation
In programming languages, we do not reduce the
bodies of functions (under a l).
Functions are considered values.
Call by Name
No reduction are performed inside abstraction .
Also call left-most, lazy evaluation.
Call by Value
Reduced only when its right hand side has reduced to a
value (variable or lambda abstraction).
Also call Eager evaluation.
31
32. Order of Evaluation
Call by Name
– Difficult to implement.
– Order of side effects not predictable.
Call by Value
– Easy to implement efficiently.
– Might not terminate even if CBN might terminate.
– Example: (lx. l z.z) ((ly. yy) (lu. uu))
Outside the functional programming language
community, only CBV is used.
32
33. Functional Programming
• The lambda calculus is a prototypical
functional programming language.
– Higher order function
– No side-effect
• In practice, many functional programming
language are not “pure”, they permit.
– Supposed to avoid them
33
34. Functional Programming
Two main camps
– Haskell - Pure, lazy function language , no side-
effects
– ml (SML-OCaml) call- by- value with side-effects
Old still around : lisp scheme
– Disadvantage : no static typing
34
35. Functional Programming
• Functional ideas move to other languages.
• Garbage collection was designed for lisp ,now no
mast new languages us GC.
• Generics in C++/Java com from ML polymorphism
,or Haskell type classes
• Higher-order function and closures (used in Ruby,
exist in C# proposed to be in java soon) are
everywhere in function languages.
• Many object-oriented abstraction principles
come from ML’s module system.
35