Untyped Lambda Calculus
Structure and Evaluation, Currying, Church Encodings
Sven Tennie
August 9, 2018
Dream IT
https://dreamit.de
Slide 2
Slide 2 text
Introduction
Slide 3
Slide 3 text
Lambda Calculus
• Invented by Alonzo Church (1920s)
• Equally expressive to the Turing Machine(s)
• Formal Language
• Computational Model
• Lisp (1950s)
• ML
• Haskell
• "Lambda Expressions" in almost every modern programming
language
1
Slide 4
Slide 4 text
Why should I care?
• Simple Computational Model
• to describe structure and behaviour (E.g. Operational
Semantics, Type Systems)
• to reason and prove
2
Slide 5
Slide 5 text
Why should I care?
• Simple Computational Model
• to describe structure and behaviour (E.g. Operational
Semantics, Type Systems)
• to reason and prove
• Explains why things in FP are like they are
• Pure Functions
• Higher-Order Functions
• Currying
• Lazy Evaluation
2
Slide 6
Slide 6 text
Why should I care?
• Simple Computational Model
• to describe structure and behaviour (E.g. Operational
Semantics, Type Systems)
• to reason and prove
• Explains why things in FP are like they are
• Pure Functions
• Higher-Order Functions
• Currying
• Lazy Evaluation
• Understand FP Compilers
• Introduce FP stuff into other languages
• Write your own compiler
• GHC uses an enriched Lambda Calculus internally
2
Slide 7
Slide 7 text
Basics
Slide 8
Slide 8 text
Untyped Lambda Calculus
t ::= Terms:
x Variable
λx.t Abstraction
t t Application
3
Slide 9
Slide 9 text
Untyped Lambda Calculus
t ::= Terms:
x Variable
λx.t Abstraction
t t Application
Example - Identity
Lambda Calculus
λx.x
Abstraction
y
Variable
Application
→ y
3
Slide 10
Slide 10 text
Untyped Lambda Calculus
t ::= Terms:
x Variable
λx.t Abstraction
t t Application
Example - Identity
Lambda Calculus
λx.x
Abstraction
y
Variable
Application
→ y
Javascript
(function (x){return x; }
Abstraction
) ( y
Variable
)
Application
3
Slide 11
Slide 11 text
Example - (λx.λy.x y) a b
Abstractions
Think: Function Definitions
(λx.λy.x y) a b
4
Slide 12
Slide 12 text
Example - (λx.λy.x y) a b
Abstractions
Think: Function Definitions
(λx.λy.x y) a b
Variables
Think: Parameters
(λx.λy.x y) a b
4
Slide 13
Slide 13 text
Example - (λx.λy.x y) a b
Abstractions
Think: Function Definitions
(λx.λy.x y) a b
Variables
Think: Parameters
(λx.λy.x y) a b
Applications
Think: Function Calls
(λx.λy.x y) a b
4
Slide 14
Slide 14 text
Example - (λx.λy.x y) a b
(λx. λy.x y) a b
5
Slide 15
Slide 15 text
Example - (λx.λy.x y) a b
(λx. λy.x y) a b Substitute x → a
5
Slide 16
Slide 16 text
Example - (λx.λy.x y) a b
(λx. λy.x y) a b Substitute x → a
5
Slide 17
Slide 17 text
Example - (λx.λy.x y) a b
(λx. λy.x y) a b Substitute x → a
→ (λy.a y) b
5
Slide 18
Slide 18 text
Example - (λx.λy.x y) a b
(λx. λy.x y) a b Substitute x → a
→ (λy.a y) b Substitute y → b
5
Slide 19
Slide 19 text
Example - (λx.λy.x y) a b
(λx. λy.x y) a b Substitute x → a
→ (λy.a y) b Substitute y → b
5
Slide 20
Slide 20 text
Example - (λx.λy.x y) a b
(λx. λy.x y) a b Substitute x → a
→ (λy.a y) b Substitute y → b
→ a b
5
Slide 21
Slide 21 text
Notational Conventions
• We use parentheses to clearify what’s meant
• Applications associate to the left
s t u ≡ (s t) u
• Abstractions expand as much to the right as possible
λx.λy.x y x ≡ λx.(λy.(x y x))
6
Slide 22
Slide 22 text
Scope
λx.λy.x y z
Bound and Free
λy y is bound, x and z are free
λx x and y are bound, z is free
λx, λy binders
7
Slide 23
Slide 23 text
Scope
λx.λy.x y z
Bound and Free
λy y is bound, x and z are free
λx x and y are bound, z is free
λx, λy binders
A term with no free variables is closed
• A combinator
• id ≡ λx.x
• Y, S, K, I . . .
7
Slide 24
Slide 24 text
Higher Order Functions
• Functions that take or return functions
• Are there "by definition"
λx.x
Abstraction
λy.y
Abstraction
Application
→ λy.y
Abstraction
8
Slide 25
Slide 25 text
Currying
Idea
• Take a function with n arguments
• Create a function that takes one argument and returns a
function with n − 1 arguments
9
Slide 26
Slide 26 text
Currying
Idea
• Take a function with n arguments
• Create a function that takes one argument and returns a
function with n − 1 arguments
Example
• (+1) Section in Haskell
• (λx.λy. + x y) 1 → λy. + 1 y
9
Slide 27
Slide 27 text
Currying
Idea
• Take a function with n arguments
• Create a function that takes one argument and returns a
function with n − 1 arguments
Example
• (+1) Section in Haskell
• (λx.λy. + x y) 1 → λy. + 1 y
• Partial Function Application is there "by definition"
• You can use this stunt to "curry" in every language that
supports "Lambda Expressions"
9
Slide 28
Slide 28 text
Reductions and Conversions
Alpha Conversion
λx.x →α λy.y
10
Slide 29
Slide 29 text
Reductions and Conversions
Alpha Conversion
λx.x →α λy.y
Beta Reduction
(λx.x) y →β y
10
Slide 30
Slide 30 text
Reductions and Conversions
Alpha Conversion
λx.x →α λy.y
Beta Reduction
(λx.x) y →β y
Eta Conversion
Iff (if and only if) x is not free in f :
λx.f x →η f
(λx. (λy.y)
f
x) a →η (λy.y)
f
a
10
Slide 31
Slide 31 text
Reductions and Conversions
Alpha Conversion
λx.x →α λy.y
Beta Reduction
(λx.x) y →β y
Eta Conversion
Iff (if and only if) x is not free in f :
λx.f x →η f
(λx. (λy.y)
f
x) a →η (λy.y)
f
a
If x is free in f , η conversion not possible:
λx. (λy.y
Bound
↓
x )
f
x →η (λy.y
Free?!
↓
x )
10
Slide 32
Slide 32 text
Remarks
• Everything (Term) is an Expression
• No statements
• No "destructive" Assignments
• The reason why FP Languages promote pure functions
• But you could invent a built-in function to manipulate
"state". . .
11
Slide 33
Slide 33 text
Evaluation
Slide 34
Slide 34 text
Operational Semantics
• We learned how to write down and talk about Lambda
Calculus Terms
• How to evaluate them?
• Different Strategies
• Interesting outcomes
12
Slide 35
Slide 35 text
Full Beta-Reduction
• RedEx
• Reducible Expression
• Always an Application
(λx.x) ((λy.y) (λz. (λa.a) z
RedEx
)
RedEx
)
RedEx
13
Slide 36
Slide 36 text
Full Beta-Reduction
• RedEx
• Reducible Expression
• Always an Application
(λx.x) ((λy.y) (λz. (λa.a) z
RedEx
)
RedEx
)
RedEx
Full Beta-Reduction
• Any RedEx, Any Time
• Like in Arithmetics
• Too vague for programming. . .
13
Slide 37
Slide 37 text
Normal Order Reduction
(λx.x) ((λy.y) (λz.(λa.a) z))
Normal Order Reduction
• Left-most, Outer-most RedEx
14
Slide 38
Slide 38 text
Normal Order Reduction
(λx.x) ((λy.y) (λz.(λa.a) z))
Normal Order Reduction
• Left-most, Outer-most RedEx
14
Slide 39
Slide 39 text
Normal Order Reduction
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λy.y) (λz.(λa.a) z)
Normal Order Reduction
• Left-most, Outer-most RedEx
14
Slide 40
Slide 40 text
Normal Order Reduction
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λy.y) (λz.(λa.a) z)
Normal Order Reduction
• Left-most, Outer-most RedEx
14
Slide 41
Slide 41 text
Normal Order Reduction
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λy.y) (λz.(λa.a) z)
→λz.(λa.a) z
Normal Order Reduction
• Left-most, Outer-most RedEx
14
Slide 42
Slide 42 text
Normal Order Reduction
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λy.y) (λz.(λa.a) z)
→λz.(λa.a) z
Normal Order Reduction
• Left-most, Outer-most RedEx
14
Slide 43
Slide 43 text
Normal Order Reduction
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λy.y) (λz.(λa.a) z)
→λz.(λa.a) z
→λz.z
Normal Order Reduction
• Left-most, Outer-most RedEx
14
Slide 44
Slide 44 text
Call-by-Name
(λx.x) ((λy.y) (λz.(λa.a) z))
Call-by-Name
• like Normal Order Reduction, but no reductions inside
Abstractions
• Abstractions are values
• lazy, non-strict
• Parameters are not evaluated before they are used
• Optimization: Save results → Call-by-Need
15
Slide 45
Slide 45 text
Call-by-Name
(λx.x) ((λy.y) (λz.(λa.a) z))
Call-by-Name
• like Normal Order Reduction, but no reductions inside
Abstractions
• Abstractions are values
• lazy, non-strict
• Parameters are not evaluated before they are used
• Optimization: Save results → Call-by-Need
15
Slide 46
Slide 46 text
Call-by-Name
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λy.y) (λz.(λa.a) z)
Call-by-Name
• like Normal Order Reduction, but no reductions inside
Abstractions
• Abstractions are values
• lazy, non-strict
• Parameters are not evaluated before they are used
• Optimization: Save results → Call-by-Need
15
Slide 47
Slide 47 text
Call-by-Name
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λy.y) (λz.(λa.a) z)
Call-by-Name
• like Normal Order Reduction, but no reductions inside
Abstractions
• Abstractions are values
• lazy, non-strict
• Parameters are not evaluated before they are used
• Optimization: Save results → Call-by-Need
15
Slide 48
Slide 48 text
Call-by-Name
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λy.y) (λz.(λa.a) z)
→λz.(λa.a) z
Call-by-Name
• like Normal Order Reduction, but no reductions inside
Abstractions
• Abstractions are values
• lazy, non-strict
• Parameters are not evaluated before they are used
• Optimization: Save results → Call-by-Need
15
Slide 49
Slide 49 text
Call-by-Name
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λy.y) (λz.(λa.a) z)
→λz.(λa.a) z
→
Call-by-Name
• like Normal Order Reduction, but no reductions inside
Abstractions
• Abstractions are values
• lazy, non-strict
• Parameters are not evaluated before they are used
• Optimization: Save results → Call-by-Need
15
Slide 50
Slide 50 text
Call-by-Value
(λx.x) ((λy.y) (λz.(λa.a) z))
Call-by-Value
• Outer-most, only if right-hand side was reduced to a value
• No reductions inside Abstractions
• Abstractions are values
• eager, strict
• Parameters are evaluated before they are used
16
Slide 51
Slide 51 text
Call-by-Value
(λx.x) ((λy.y) (λz.(λa.a) z))
Call-by-Value
• Outer-most, only if right-hand side was reduced to a value
• No reductions inside Abstractions
• Abstractions are values
• eager, strict
• Parameters are evaluated before they are used
16
Slide 52
Slide 52 text
Call-by-Value
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λx.x) (λz.(λa.a) z)
Call-by-Value
• Outer-most, only if right-hand side was reduced to a value
• No reductions inside Abstractions
• Abstractions are values
• eager, strict
• Parameters are evaluated before they are used
16
Slide 53
Slide 53 text
Call-by-Value
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λx.x) (λz.(λa.a) z)
Call-by-Value
• Outer-most, only if right-hand side was reduced to a value
• No reductions inside Abstractions
• Abstractions are values
• eager, strict
• Parameters are evaluated before they are used
16
Slide 54
Slide 54 text
Call-by-Value
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λx.x) (λz.(λa.a) z)
→λz.(λa.a) z
Call-by-Value
• Outer-most, only if right-hand side was reduced to a value
• No reductions inside Abstractions
• Abstractions are values
• eager, strict
• Parameters are evaluated before they are used
16
Slide 55
Slide 55 text
Call-by-Value
(λx.x) ((λy.y) (λz.(λa.a) z))
→(λx.x) (λz.(λa.a) z)
→λz.(λa.a) z
→
Call-by-Value
• Outer-most, only if right-hand side was reduced to a value
• No reductions inside Abstractions
• Abstractions are values
• eager, strict
• Parameters are evaluated before they are used
16
Slide 56
Slide 56 text
Church Encodings
Slide 57
Slide 57 text
Church Encodings
• Encode Data into the Lambda Calculus
• To simplify our formulas, let’s say that we have declarations
id ≡ λx.x
id y → y
17
Slide 58
Slide 58 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
18
Slide 59
Slide 59 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
18
Slide 60
Slide 60 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
18
Slide 61
Slide 61 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
18
Slide 62
Slide 62 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
18
Slide 63
Slide 63 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
18
Slide 64
Slide 64 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
→ (λt.λf .true t f ) a b
18
Slide 65
Slide 65 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
→ (λt.λf .true t f ) a b
18
Slide 66
Slide 66 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
→ (λt.λf .true t f ) a b
→ (λf .true a f ) b
18
Slide 67
Slide 67 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
→ (λt.λf .true t f ) a b
→ (λf .true a f ) b
18
Slide 68
Slide 68 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
→ (λt.λf .true t f ) a b
→ (λf .true a f ) b
→true a b
18
Slide 69
Slide 69 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
→ (λt.λf .true t f ) a b
→ (λf .true a f ) b
→true a b
18
Slide 70
Slide 70 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
→ (λt.λf .true t f ) a b
→ (λf .true a f ) b
→true a b
≡(λt.λf .t) a b
18
Slide 71
Slide 71 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
→ (λt.λf .true t f ) a b
→ (λf .true a f ) b
→true a b
≡(λt.λf .t) a b
18
Slide 72
Slide 72 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
→ (λt.λf .true t f ) a b
→ (λf .true a f ) b
→true a b
≡(λt.λf .t) a b
→(λf .a) b
18
Slide 73
Slide 73 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
→ (λt.λf .true t f ) a b
→ (λf .true a f ) b
→true a b
≡(λt.λf .t) a b
→(λf .a) b
18
Slide 74
Slide 74 text
Booleans
true ≡ λt.λf .t
false ≡ λt.λf .f
test ≡ λc.λt.λf .c t f
test true a b
≡ (λc.λt.λf .c t f ) true a b
→ (λt.λf .true t f ) a b
→ (λf .true a f ) b
→true a b
≡(λt.λf .t) a b
→(λf .a) b
→a
18
Slide 75
Slide 75 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
19
Slide 76
Slide 76 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
19
Slide 77
Slide 77 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
19
Slide 78
Slide 78 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
19
Slide 79
Slide 79 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
≡(λp.λq.p q p) true false
19
Slide 80
Slide 80 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
≡(λp.λq.p q p) true false
19
Slide 81
Slide 81 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
≡(λp.λq.p q p) true false
→(λq.true q true) false
19
Slide 82
Slide 82 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
≡(λp.λq.p q p) true false
→(λq.true q true) false
19
Slide 83
Slide 83 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
≡(λp.λq.p q p) true false
→(λq.true q true) false
→true false true
19
Slide 84
Slide 84 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
≡(λp.λq.p q p) true false
→(λq.true q true) false
→true false true
19
Slide 85
Slide 85 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
≡(λp.λq.p q p) true false
→(λq.true q true) false
→true false true
≡(λt.λf .t) false true
19
Slide 86
Slide 86 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
≡(λp.λq.p q p) true false
→(λq.true q true) false
→true false true
≡(λt.λf .t) false true
19
Slide 87
Slide 87 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
≡(λp.λq.p q p) true false
→(λq.true q true) false
→true false true
≡(λt.λf .t) false true
→(λf .false) true
19
Slide 88
Slide 88 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
≡(λp.λq.p q p) true false
→(λq.true q true) false
→true false true
≡(λt.λf .t) false true
→(λf .false) true
19
Slide 89
Slide 89 text
And
true ≡ λt.λf .t
false ≡ λt.λf .f
and ≡ λp.λq.p q p
and true false
≡(λp.λq.p q p) true false
→(λq.true q true) false
→true false true
≡(λt.λf .t) false true
→(λf .false) true
→false
19
Slide 90
Slide 90 text
Or
λp.λq.p p q
20
Slide 91
Slide 91 text
Pairs
pair ≡ λx.λy.λz.z x y
21
Slide 92
Slide 92 text
Pairs
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y
21
Slide 93
Slide 93 text
Pairs
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y
pairAB ≡ pair a b
21
Slide 94
Slide 94 text
Pairs
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y
pairAB ≡ pair a b
21
Slide 95
Slide 95 text
Pairs
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y
pairAB ≡ pair a b
≡ (λx.λy.λz.z x y) a b
21
Slide 96
Slide 96 text
Pairs
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y
pairAB ≡ pair a b
≡ (λx.λy.λz.z x y) a b
21
Slide 97
Slide 97 text
Pairs
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y
pairAB ≡ pair a b
≡ (λx.λy.λz.z x y) a b
→ (λy.λz.z a y) b
21
Slide 98
Slide 98 text
Pairs
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y
pairAB ≡ pair a b
≡ (λx.λy.λz.z x y) a b
→ (λy.λz.z a y) b
21
Slide 99
Slide 99 text
Pairs
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y
pairAB ≡ pair a b
≡ (λx.λy.λz.z x y) a b
→ (λy.λz.z a y) b
→ λz.z a b
21
Slide 100
Slide 100 text
Pairs
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y
pairAB ≡ pair a b
≡ (λx.λy.λz.z x y) a b
→ (λy.λz.z a y) b
→ λz.z a b
≡ pairab
21
Slide 101
Slide 101 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
22
Slide 102
Slide 102 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
22
Slide 103
Slide 103 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
22
Slide 104
Slide 104 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
≡ (λp.p) (λx.λy.x) pairab
22
Slide 105
Slide 105 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
≡ (λp.p) (λx.λy.x) pairab
22
Slide 106
Slide 106 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
≡ (λp.p) (λx.λy.x) pairab
→ pairab
(λx.λy.x)
22
Slide 107
Slide 107 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
≡ (λp.p) (λx.λy.x) pairab
→ pairab
(λx.λy.x)
22
Slide 108
Slide 108 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
≡ (λp.p) (λx.λy.x) pairab
→ pairab
(λx.λy.x)
≡ (λz.z a b) (λx.λy.x)
22
Slide 109
Slide 109 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
≡ (λp.p) (λx.λy.x) pairab
→ pairab
(λx.λy.x)
≡ (λz.z a b) (λx.λy.x)
22
Slide 110
Slide 110 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
≡ (λp.p) (λx.λy.x) pairab
→ pairab
(λx.λy.x)
≡ (λz.z a b) (λx.λy.x)
→ (λx.λy.x) a b
22
Slide 111
Slide 111 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
≡ (λp.p) (λx.λy.x) pairab
→ pairab
(λx.λy.x)
≡ (λz.z a b) (λx.λy.x)
→ (λx.λy.x) a b
22
Slide 112
Slide 112 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
≡ (λp.p) (λx.λy.x) pairab
→ pairab
(λx.λy.x)
≡ (λz.z a b) (λx.λy.x)
→ (λx.λy.x) a b
→ (λy.a) b
22
Slide 113
Slide 113 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
≡ (λp.p) (λx.λy.x) pairab
→ pairab
(λx.λy.x)
≡ (λz.z a b) (λx.λy.x)
→ (λx.λy.x) a b
→ (λy.a) b
22
Slide 114
Slide 114 text
Pairs (continued)
pair ≡ λx.λy.λz.z x y
first ≡ (λp.p) λx.λy.x
pairab
≡ λz.z a b
first pairab
≡ (λp.p) (λx.λy.x) pairab
→ pairab
(λx.λy.x)
≡ (λz.z a b) (λx.λy.x)
→ (λx.λy.x) a b
→ (λy.a) b
→ a
22
Slide 115
Slide 115 text
Numerals
Peano Axioms
Every natural number can be
defined with 0 and a successor
function
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
2 ≡ λf .λx.f (f x)
3 ≡ λf .λx.f (f (f x))
Meaning
0 f is evaluated 0
times
1 f is evaluated
once
x can be every
lambda term
23
Slide 116
Slide 116 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
24
Slide 117
Slide 117 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
24
Slide 118
Slide 118 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
24
Slide 119
Slide 119 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
24
Slide 120
Slide 120 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
≡ (λn.λf .λx.f (n f x)) 1
24
Slide 121
Slide 121 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
≡ (λn.λf .λx.f (n f x)) 1
24
Slide 122
Slide 122 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
≡ (λn.λf .λx.f (n f x)) 1
→ λf .λx.f (1 f x)
24
Slide 123
Slide 123 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
≡ (λn.λf .λx.f (n f x)) 1
→ λf .λx.f (1 f x)
Note
We use Normal Order Reduction to reduce inside abstractions!
24
Slide 124
Slide 124 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
≡ (λn.λf .λx.f (n f x)) 1
→ λf .λx.f (1 f x)
≡ λf .λx.f ((λf .λx.f x) f x)
Note
We use Normal Order Reduction to reduce inside abstractions!
24
Slide 125
Slide 125 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
≡ (λn.λf .λx.f (n f x)) 1
→ λf .λx.f (1 f x)
≡ λf .λx.f ((λf .λx.f x) f x)
Note
We use Normal Order Reduction to reduce inside abstractions!
24
Slide 126
Slide 126 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
≡ (λn.λf .λx.f (n f x)) 1
→ λf .λx.f (1 f x)
≡ λf .λx.f ((λf .λx.f x) f x)
→ λf .λx.f ((λx.f x) x)
Note
We use Normal Order Reduction to reduce inside abstractions!
24
Slide 127
Slide 127 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
≡ (λn.λf .λx.f (n f x)) 1
→ λf .λx.f (1 f x)
≡ λf .λx.f ((λf .λx.f x) f x)
→ λf .λx.f ((λx.f x) x)
Note
We use Normal Order Reduction to reduce inside abstractions!
24
Slide 128
Slide 128 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
≡ (λn.λf .λx.f (n f x)) 1
→ λf .λx.f (1 f x)
≡ λf .λx.f ((λf .λx.f x) f x)
→ λf .λx.f ((λx.f x) x)
→ λf .λx.f (f x)
Note
We use Normal Order Reduction to reduce inside abstractions!
24
Slide 129
Slide 129 text
Numerals Example - Successor
0 ≡ λf .λx.x
1 ≡ λf .λx.f x
successor ≡ λn.λf .λx.f (n f x)
successor 1
≡ (λn.λf .λx.f (n f x)) 1
→ λf .λx.f (1 f x)
≡ λf .λx.f ((λf .λx.f x) f x)
→ λf .λx.f ((λx.f x) x)
→ λf .λx.f (f x)
≡ 2
Note
We use Normal Order Reduction to reduce inside abstractions!
24
Slide 130
Slide 130 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
25
Slide 131
Slide 131 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
25
Slide 132
Slide 132 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
25
Slide 133
Slide 133 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
25
Slide 134
Slide 134 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
25
Slide 135
Slide 135 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
25
Slide 136
Slide 136 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
25
Slide 137
Slide 137 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
25
Slide 138
Slide 138 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
25
Slide 139
Slide 139 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
25
Slide 140
Slide 140 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
25
Slide 141
Slide 141 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
25
Slide 142
Slide 142 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
→ λf .λx.(λx.x) (0 f x)
25
Slide 143
Slide 143 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
→ λf .λx.(λx.x) (0 f x)
25
Slide 144
Slide 144 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
→ λf .λx.(λx.x) (0 f x)
→ λf .λx.0 f x
25
Slide 145
Slide 145 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
→ λf .λx.(λx.x) (0 f x)
→ λf .λx.0 f x
25
Slide 146
Slide 146 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
→ λf .λx.(λx.x) (0 f x)
→ λf .λx.0 f x
≡ λf .λx.(λf .λx.x) f x
25
Slide 147
Slide 147 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
→ λf .λx.(λx.x) (0 f x)
→ λf .λx.0 f x
≡ λf .λx.(λf .λx.x) f x
25
Slide 148
Slide 148 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
→ λf .λx.(λx.x) (0 f x)
→ λf .λx.0 f x
≡ λf .λx.(λf .λx.x) f x
→ λf .λx.(λx.x) x
25
Slide 149
Slide 149 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
→ λf .λx.(λx.x) (0 f x)
→ λf .λx.0 f x
≡ λf .λx.(λf .λx.x) f x
→ λf .λx.(λx.x) x
25
Slide 150
Slide 150 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
→ λf .λx.(λx.x) (0 f x)
→ λf .λx.0 f x
≡ λf .λx.(λf .λx.x) f x
→ λf .λx.(λx.x) x
→ λf .λx.x
25
Slide 151
Slide 151 text
Numerals Example - 0 + 0
0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x)
plus 0 0
≡ (λm.λn.λf .λx.m f (n f x)) 0 0
→ (λn.λf .λx.0 f (n f x)) 0
→ λf .λx.0 f (0 f x)
≡ λf .λx.(λf .λx.x) f (0 f x)
→ λf .λx.(λx.x) (0 f x)
→ λf .λx.0 f x
≡ λf .λx.(λf .λx.x) f x
→ λf .λx.(λx.x) x
→ λf .λx.x
≡ 0
25
Slide 152
Slide 152 text
End
Slide 153
Slide 153 text
Thanks
• Hope you enjoyed this talk and learned something new.
• Hope it wasn’t too much math and dusty formulas . . . :)
26
Slide 154
Slide 154 text
Good Math
"A Geek’s Guide to the Beauty of
Numbers, Logic, and Computation"
• Easy to understand
Slide 155
Slide 155 text
The Implementation of Functional Programming Languages
• How to compile to the Lambda
Calculus?
• Out-of-print, but freely available
• https://www.microsoft.com/en-
us/research/publication/the-
implementation-of-functional-
programming-languages/
Slide 156
Slide 156 text
Types and Programming Languages
• Types systems explained by building
interpreters and proving properties
• Very "mathematical", but very
complete and self-contained