Professional Documents
Culture Documents
Mihnea Muraru
mihnea.muraru@upb.ro
Fall 2023
1 / 210
Part I
Introduction
2 / 210
Contents
Objectives
Functional programming
3 / 210
Contents
Objectives
Functional programming
4 / 210
Grading
I Lab: 60, ≥ 30
I Exam: 40, ≥ 20
I Final grade ≥ 50
5 / 210
Course objectives
6 / 210
Course objectives
6 / 210
Main lab outcome
7 / 210
Contents
Objectives
Functional programming
8 / 210
Functional programming features
9 / 210
Functional programming features
9 / 210
Functional programming features
9 / 210
Functional flow
in1 f1
f3
in2 f2 f5 out
in3 f4
10 / 210
Stateless computation
t0
11 / 210
Stateless computation
x f y
t1
11 / 210
Stateless computation
x f y
t2
11 / 210
Stateful computation
t0
12 / 210
Stateful computation
x f y
t1
12 / 210
Stateful computation
x f y 0 6= y
t2
12 / 210
Functional flow
Pure
in1 f1
f3
in2 f2 f5 out
in3 f4
13 / 210
Functional flow
Impure
in1 f1
f3
in3 f4
13 / 210
Functional programming features
14 / 210
Functional programming features
14 / 210
Functional programming features
14 / 210
Functional programming features
14 / 210
Functional programming features
14 / 210
Functional programming features
14 / 210
Why functional programming?
I Simple evaluation model; equational reasoning
15 / 210
Why functional programming?
I Simple evaluation model; equational reasoning
I Declarative
15 / 210
Why functional programming?
I Simple evaluation model; equational reasoning
I Declarative
I Modularity, composability, reuse (lazy evaluation
as glue)
15 / 210
Why functional programming?
I Simple evaluation model; equational reasoning
I Declarative
I Modularity, composability, reuse (lazy evaluation
as glue)
I Exploration of huge or formally infinite search spaces
15 / 210
Why functional programming?
I Simple evaluation model; equational reasoning
I Declarative
I Modularity, composability, reuse (lazy evaluation
as glue)
I Exploration of huge or formally infinite search spaces
I Embedded Domain Specific Languages (EDSLs)
15 / 210
Why functional programming?
I Simple evaluation model; equational reasoning
I Declarative
I Modularity, composability, reuse (lazy evaluation
as glue)
I Exploration of huge or formally infinite search spaces
I Embedded Domain Specific Languages (EDSLs)
I Massive parallelization
15 / 210
Why functional programming?
I Simple evaluation model; equational reasoning
I Declarative
I Modularity, composability, reuse (lazy evaluation
as glue)
I Exploration of huge or formally infinite search spaces
I Embedded Domain Specific Languages (EDSLs)
I Massive parallelization
I Type systems and logic, inextricably linked
15 / 210
Why functional programming?
I Simple evaluation model; equational reasoning
like natural language
I Declarative
I Modularity, composability, reuse (lazy evaluation
as glue)
I Exploration of huge or formally infinite search spaces
I Embedded Domain Specific Languages (EDSLs)
I Massive parallelization
I Type systems and logic, inextricably linked
I Automatic program verification and synthesis
15 / 210
last two are point-free style
foldr foldr like,
you cannot compute intermediate
foldr sums, you need the entire trace
only when coming back from
recursion can you actually
do the sums
Part II
16 / 210
Contents
Introduction
Lambda expressions
Reduction
Normal forms
Evaluation order
17 / 210
Contents
Introduction
Lambda expressions
Reduction
Normal forms
Evaluation order
18 / 210
Untyped lambda calculus
19 / 210
Untyped lambda calculus
19 / 210
Untyped lambda calculus
19 / 210
Untyped lambda calculus
19 / 210
Untyped lambda calculus
19 / 210
Untyped lambda calculus
19 / 210
Applications
20 / 210
Applications
20 / 210
Contents
Introduction
Lambda expressions
Reduction
Normal forms
Evaluation order
21 / 210
λ -expressions
Definition
22 / 210
λ -expressions
Definition
22 / 210
λ -expressions
Definition
22 / 210
λ -expressions
Examples
23 / 210
λ -expressions
Examples
23 / 210
λ -expressions
Examples
23 / 210
λ -expressions
Examples
23 / 210
λ -expressions
Examples
23 / 210
Intuition on application evaluation
(λ x . x y )
24 / 210
Intuition on application evaluation
(λ x . x y )
24 / 210
Intuition on application evaluation
(λ x . x y )
24 / 210
Intuition on application evaluation
(λ x . x y )
24 / 210
Intuition on application evaluation
(λ x . x y )
24 / 210
Intuition on application evaluation
(λ x . x y )
24 / 210
Intuition on application evaluation
(λ x . x y )→y
24 / 210
Variable occurrences
Definitions
25 / 210
Variable occurrences
Definitions
25 / 210
Variable occurrences
Definitions
I E = . . . λ xn .F . . . or
25 / 210
Variable occurrences
Definitions
I E = . . . λ xn .F . . . or
I E = . . . λ x .F . . . and xn appears in F .
25 / 210
Variable occurrences
Definitions
I E = . . . λ xn .F . . . or
I E = . . . λ x .F . . . and xn appears in F .
25 / 210
Variable occurrences
Definitions
I E = . . . λ xn .F . . . or
I E = . . . λ x .F . . . and xn appears in F .
25 / 210
Variable occurrences
Examples
E = (λ x1 . x2 x3 ).
|{z}
F
26 / 210
Variable occurrences
Examples
E = (λ x1 . x2 x3 ).
|{z}
F
I x1 , x2 bound in E
26 / 210
Variable occurrences
Examples
E = (λ x1 . x2 x3 ).
|{z}
F
I x1 , x2 bound in E
I x3 free in E
26 / 210
Variable occurrences
Examples
E = (λ x1 . x2 x3 ).
|{z}
F
I x1 , x2 bound in E
I x3 free in E
I x2 free in F !
26 / 210
Variable occurrences
Examples
Example 4.6 (Bound and free variables).
In the expression E = (λ x .λ z .(z x ) (z y )), we emphasize
the occurrences of x, y , z:
F
z }| {
E = (λ x1 . λ z1 .(z2 x2 ) (z3 y1 )).
27 / 210
Variable occurrences
Examples
Example 4.6 (Bound and free variables).
In the expression E = (λ x .λ z .(z x ) (z y )), we emphasize
the occurrences of x, y , z:
F
z }| {
E = (λ x1 . λ z1 .(z2 x2 ) (z3 y1 )).
I x1 , x2 , z1 , z2 bound in E
27 / 210
Variable occurrences
Examples
Example 4.6 (Bound and free variables).
In the expression E = (λ x .λ z .(z x ) (z y )), we emphasize
the occurrences of x, y , z:
F
z }| {
E = (λ x1 . λ z1 .(z2 x2 ) (z3 y1 )).
I x1 , x2 , z1 , z2 bound in E
I y1 , z3 free in E
27 / 210
Variable occurrences
Examples
Example 4.6 (Bound and free variables).
In the expression E = (λ x .λ z .(z x ) (z y )), we emphasize
the occurrences of x, y , z:
F
z }| {
E = (λ x1 . λ z1 .(z2 x2 ) (z3 y1 )).
I x1 , x2 , z1 , z2 bound in E
I y1 , z3 free in E
I z1 , z2 bound in F
27 / 210
Variable occurrences
Examples
Example 4.6 (Bound and free variables).
In the expression E = (λ x .λ z .(z x ) (z y )), we emphasize
the occurrences of x, y , z:
F
z }| {
E = (λ x1 . λ z1 .(z2 x2 ) (z3 y1 )).
I x1 , x2 , z1 , z2 bound in E
I y1 , z3 free in E
I z1 , z2 bound in F
I x2 free in F
27 / 210
Variables
Definitions
28 / 210
Variables
Definitions
28 / 210
Variables
Definitions
28 / 210
Variable occurrences
Examples
E = (λ x1 . x2 x3 ).
|{z}
F
I x1 , x2 bound in E
I x3 free in E
I x2 free in F !
29 / 210
Variable occurrences
Examples
E = (λ x1 . x2 x3 ).
|{z}
F
I x1 , x2 bound in E
I x3 free in E
I x2 free in F !
I x free in E and F
29 / 210
Variable occurrences
Examples
Example 4.6 (Bound and free variables).
In the expression E = (λ x .λ z .(z x ) (z y )), we emphasize
the occurrences of x, y , z:
F
z }| {
E = (λ x1 . λ z1 .(z2 x2 ) (z3 y1 )).
I x1 , x2 , z1 , z2 bound in E
I y1 , z3 free in E
I z1 , z2 bound in F
I x2 free in F
30 / 210
Variable occurrences
Examples
Example 4.6 (Bound and free variables).
In the expression E = (λ x .λ z .(z x ) (z y )), we emphasize
the occurrences of x, y , z:
F
z }| {
E = (λ x1 . λ z1 .(z2 x2 ) (z3 y1 )).
I x1 , x2 , z1 , z2 bound in E
I y1 , z3 free in E
I z1 , z2 bound in F
I x2 free in F
I x bound in E, but free in F
30 / 210
Variable occurrences
Examples
Example 4.6 (Bound and free variables).
In the expression E = (λ x .λ z .(z x ) (z y )), we emphasize
the occurrences of x, y , z:
F
z }| {
E = (λ x1 . λ z1 .(z2 x2 ) (z3 y1 )).
I x1 , x2 , z1 , z2 bound in E
I y1 , z3 free in E
I z1 , z2 bound in F
I x2 free in F
I x bound in E, but free in F
I y free in E
30 / 210
Variable occurrences
Examples
Example 4.6 (Bound and free variables).
In the expression E = (λ x .λ z .(z x ) (z y )), we emphasize
the occurrences of x, y , z:
F
z }| {
E = (λ x1 . λ z1 .(z2 x2 ) (z3 y1 )).
I x1 , x2 , z1 , z2 bound in E
I y1 , z3 free in E
I z1 , z2 bound in F
I x2 free in F
I x bound in E, but free in F
I y free in E
I z free in E, but bound in F
30 / 210
Free and bound variables
Free variables
I FV (x ) =
31 / 210
Free and bound variables
Free variables
I FV (x ) = {x }
I FV (λ x .E ) =
31 / 210
Free and bound variables
Free variables
I FV (x ) = {x }
I FV (λ x .E ) = FV (E ) \ {x }
I FV ((E1 E2 )) =
31 / 210
Free and bound variables
Free variables
I FV (x ) = {x }
I FV (λ x .E ) = FV (E ) \ {x }
I FV ((E1 E2 )) = FV (E1 ) ∪ FV (E2 )
Bound variables
I BV (x ) =
31 / 210
Free and bound variables
Free variables
I FV (x ) = {x }
I FV (λ x .E ) = FV (E ) \ {x }
I FV ((E1 E2 )) = FV (E1 ) ∪ FV (E2 )
Bound variables
I BV (x ) = 0/
I BV (λ x .E ) =
31 / 210
Free and bound variables
Free variables
I FV (x ) = {x }
I FV (λ x .E ) = FV (E ) \ {x }
I FV ((E1 E2 )) = FV (E1 ) ∪ FV (E2 )
Bound variables
I BV (x ) = 0/
I BV (λ x .E ) = BV (E ) ∪ {x }
I BV ((E1 E2 )) =
31 / 210
Free and bound variables
Free variables
I FV (x ) = {x }
I FV (λ x .E ) = FV (E ) \ {x }
I FV ((E1 E2 )) = FV (E1 ) ∪ FV (E2 )
Bound variables
I BV (x ) = 0/
I BV (λ x .E ) = BV (E ) ∪ {x }
I BV ((E1 E2 )) = BV (E1 ) \ FV (E2 ) ∪ BV (E2 ) \ FV (E1 )
31 / 210
Closed expressions
Definition 4.9 (Closed expression).
An expression that does not contain any free variables.
32 / 210
Closed expressions
Definition 4.9 (Closed expression).
An expression that does not contain any free variables.
Example 4.10 (Closed expressions).
I (λ x .x λ x .λ y .x )
32 / 210
Closed expressions
Definition 4.9 (Closed expression).
An expression that does not contain any free variables.
Example 4.10 (Closed expressions).
I (λ x .x λ x .λ y .x ) : closed
I (λ x .x a)
32 / 210
Closed expressions
Definition 4.9 (Closed expression).
An expression that does not contain any free variables.
Example 4.10 (Closed expressions).
I (λ x .x λ x .λ y .x ) : closed
I (λ x .x a) : open, since a is free
Remarks:
I Free variables may stand for other λ -expressions,
as in λ x .((+ x ) 1).
I Before evaluation, an expression must be brought
to the closed form.
I The substitution process must terminate.
32 / 210
Contents
Introduction
Lambda expressions
Reduction
Normal forms
Evaluation order
33 / 210
β -reduction
Definitions
34 / 210
β -reduction
Definitions
34 / 210
β -reduction
Examples
35 / 210
β -reduction
Examples
35 / 210
β -reduction
Examples
35 / 210
β -reduction
Examples
35 / 210
β -reduction
Examples
35 / 210
β -reduction
Examples
35 / 210
β -reduction
Examples
35 / 210
β -reduction
Examples
35 / 210
β -reduction
Examples
35 / 210
β -reduction
Collisions
36 / 210
β -reduction
Collisions
36 / 210
β -reduction
Collisions
36 / 210
β -reduction
Collisions
36 / 210
β -reduction
Collisions
36 / 210
β -reduction
Collisions
36 / 210
β -reduction
Collisions
36 / 210
β -reduction
Collisions
36 / 210
α -conversion
Definition
37 / 210
α -conversion
Definition
37 / 210
α -conversion
Definition
37 / 210
α -conversion
Definition
37 / 210
α -conversion
Definition
37 / 210
α -conversion
Definition
37 / 210
α -conversion
Definition
37 / 210
α -conversion
Definition
37 / 210
α -conversion
Definition
37 / 210
α -conversion
Definition
37 / 210
α -conversion
Definition
Conditions:
37 / 210
α -conversion
Definition
Conditions:
I y is not free in E
37 / 210
α -conversion
Definition
Conditions:
I y is not free in E
I a free occurrence in E stays free in E[y /x ]
37 / 210
α -conversion
Examples
38 / 210
α -conversion
Examples
I λ x .λ x .(x y ) →α λ y .λ x .(x y )
38 / 210
α -conversion
Examples
38 / 210
α -conversion
Examples
38 / 210
α -conversion
Examples
38 / 210
Reduction
Definitions
39 / 210
Reduction
Definitions
39 / 210
Reduction
Examples
40 / 210
Reduction
Examples
40 / 210
Reduction
Examples
40 / 210
Reduction
Examples
40 / 210
Reduction
Examples
I ((λ x .λ y .(y x ) y ) λ x .x ) →∗ y
40 / 210
Reduction
Properties
E1 → E2 ⇒ E1 →∗ E2
I Reflexivity:
E →∗ E
I Transitivity:
E1 →∗ E2 ∧ E2 →∗ E3 ⇒ E1 →∗ E3
41 / 210
Contents
Introduction
Lambda expressions
Reduction
Normal forms
Evaluation order
42 / 210
Questions
43 / 210
Questions
43 / 210
Questions
43 / 210
Questions
43 / 210
Normal forms
44 / 210
Normal forms
44 / 210
Normal forms
44 / 210
Normal forms
44 / 210
Reduction termination (reducibility)
Example 6.4.
Ω ≡ (λ x .(x x ) λ x .(x x ))
45 / 210
Reduction termination (reducibility)
Example 6.4.
Ω ≡ (λ x .(x x ) λ x .(x x )) → (λ x .(x x ) λ x .(x x ))
45 / 210
Reduction termination (reducibility)
Example 6.4.
Ω ≡ (λ x .(x x ) λ x .(x x )) → (λ x .(x x ) λ x .(x x )) →∗ . . .
Ω does not have a terminating reduction sequence.
45 / 210
Reduction termination (reducibility)
Example 6.4.
Ω ≡ (λ x .(x x ) λ x .(x x )) → (λ x .(x x ) λ x .(x x )) →∗ . . .
Ω does not have a terminating reduction sequence.
45 / 210
Reduction termination (reducibility)
Example 6.4.
Ω ≡ (λ x .(x x ) λ x .(x x )) → (λ x .(x x ) λ x .(x x )) →∗ . . .
Ω does not have a terminating reduction sequence.
Ω is irreducible.
45 / 210
Questions
46 / 210
Questions
46 / 210
Reduction sequences
Example 6.6 (Reduction sequences).
E = (λ x .y Ω)
47 / 210
Reduction sequences
Example 6.6 (Reduction sequences).
E = (λ x .y Ω)
1
I →y
−
47 / 210
Reduction sequences
Example 6.6 (Reduction sequences).
E = (λ x .y Ω)
1
I →y
−
2 1
I →E −
− →y
47 / 210
Reduction sequences
Example 6.6 (Reduction sequences).
E = (λ x .y Ω)
1
I →y
−
2 1
I →E −
− →y
2 2 1
I →E −
− →E −
→y
47 / 210
Reduction sequences
Example 6.6 (Reduction sequences).
E = (λ x .y Ω)
1
I →y
−
2 1
I →E −
− →y
2 2 1
I →E −
− →E −
→y
I ...
47 / 210
Reduction sequences
Example 6.6 (Reduction sequences).
E = (λ x .y Ω)
1
I →y
−
∗
2 1 2n 1
I →E −
− →y I −−→ y , n ≥ 0
2 2 1
I →E −
− →E −
→y
I ...
47 / 210
Reduction sequences
Example 6.6 (Reduction sequences).
E = (λ x .y Ω)
1
I →y
−
∗
2 1 2n 1
I →E −
− →y I −−→ y , n ≥ 0
2 2 1 2∞ ∗
I →E −
− →E −
→y I −→ . . .
I ...
47 / 210
Reduction sequences
Example 6.6 (Reduction sequences).
E = (λ x .y Ω)
1
I →y
−
∗
2 1 2n 1
I →E −
− →y I −−→ y , n ≥ 0
2 2 1 2∞ ∗
I →E −
− →E −
→y I −→ . . .
I ...
47 / 210
Reduction sequences
Example 6.6 (Reduction sequences).
E = (λ x .y Ω)
1
I →y
−
∗
2 1 2n 1
I →E −
− →y I −−→ y , n ≥ 0
2 2 1 2∞ ∗
I →E −
− →E −
→y I −→ . . .
I ...
47 / 210
Questions
48 / 210
Questions
48 / 210
Normal form uniqueness
Results
∗ E1 ∗
E E3
∗ E2 ∗
49 / 210
Normal form uniqueness
Results
∗ E1 ∗
E E3
∗ E2 ∗
49 / 210
Normal form uniqueness
Examples
(λ x .λ y .(x y ) (λ x .x y ))
50 / 210
Normal form uniqueness
Examples
(λ x .λ y .(x y ) (λ x .x y ))
I → λ z .((λ x .x y ) z ) → λ z .(y z )
50 / 210
Normal form uniqueness
Examples
(λ x .λ y .(x y ) (λ x .x y ))
I → λ z .((λ x .x y ) z ) → λ z .(y z )
I → (λ x .λ y .(x y ) y ) → λ w .(y w )
50 / 210
Normal form uniqueness
Examples
(λ x .λ y .(x y ) (λ x .x y ))
50 / 210
Normal form uniqueness
Examples
(λ x .λ y .(x y ) (λ x .x y ))
50 / 210
Normal form uniqueness
Examples
(λ x .λ y .(x y ) (λ x .x y ))
50 / 210
Structural equivalence
51 / 210
Computational equivalence
Definition 6.12 (Computational equivalence).
Two expressions are computationally equivalent iff they
the behave in the same way when applied onto the same
arguments.
Example 6.13 (Computational equivalence).
E1 = λ y .λ x .(y x )
E2 = λ x .x
I ((E1 a) b ) →∗ (a b )
I ((E2 a) b ) →∗ (a b )
52 / 210
Computational equivalence
Definition 6.12 (Computational equivalence).
Two expressions are computationally equivalent iff they
the behave in the same way when applied onto the same
arguments.
Example 6.13 (Computational equivalence).
E1 = λ y .λ x .(y x )
E2 = λ x .x
I ((E1 a) b ) →∗ (a b )
I ((E2 a) b ) →∗ (a b )
I E1 6→∗ E2 and E2 6→∗ E1 (not structurally equivalent)
52 / 210
Questions
53 / 210
Questions
53 / 210
Reduction order
Definitions and examples
54 / 210
Reduction order
Definitions and examples
54 / 210
Reduction order
Which one is better?
55 / 210
Questions
56 / 210
Questions
56 / 210
Contents
Introduction
Lambda expressions
Reduction
Normal forms
Evaluation order
57 / 210
Evaluation order
58 / 210
Evaluation order
58 / 210
Evaluation order
58 / 210
Evaluation order
58 / 210
In practice I
59 / 210
In practice II
60 / 210
Summary
I Lambda calculus: model of computation,
underpinned by functions and textual substitution
I Bound/free variables and variable occurrences w.r.t.
an expression
I β -reduction, α -conversion, reduction step, reduction
sequence, reduction order, normal forms
I Left-to-right reduction (normal-order evaluation):
always terminates for reducible expressions
I Right-to-left reduction (applicative-order evaluation):
more efficient but no guarantee on termination even
for reducible expressions!
61 / 210
Part III
Lambda Calculus
as a Programming Language
62 / 210
Contents
The λ0 language
Implementation
Recursion
Language specification
63 / 210
Contents
The λ0 language
Implementation
Recursion
Language specification
64 / 210
Purpose
65 / 210
Purpose
I Hypothetical λ -machine
65 / 210
Purpose
I Hypothetical λ -machine
65 / 210
Purpose
I Hypothetical λ -machine
I Instead of
I bits
I bit operations,
we have
65 / 210
Purpose
I Hypothetical λ -machine
I Instead of
I bits
I bit operations,
we have
I structured strings of symbols
65 / 210
Purpose
I Hypothetical λ -machine
I Instead of
I bits
I bit operations,
we have
I structured strings of symbols
I reduction — textual substitution
65 / 210
λ0 features
I Instructions:
66 / 210
λ0 features
I Instructions:
I λ -expressions
66 / 210
λ0 features
I Instructions:
I λ -expressions
I top-level variable bindings: variable ≡def expression
e.g., true ≡def λ x .λ y .x
66 / 210
λ0 features
I Instructions:
I λ -expressions
I top-level variable bindings: variable ≡def expression
e.g., true ≡def λ x .λ y .x
66 / 210
λ0 features
I Instructions:
I λ -expressions
I top-level variable bindings: variable ≡def expression
e.g., true ≡def λ x .λ y .x
66 / 210
λ0 features
I Instructions:
I λ -expressions
I top-level variable bindings: variable ≡def expression
e.g., true ≡def λ x .λ y .x
66 / 210
λ0 features
I Instructions:
I λ -expressions
I top-level variable bindings: variable ≡def expression
e.g., true ≡def λ x .λ y .x
66 / 210
λ0 features
I Instructions:
I λ -expressions
I top-level variable bindings: variable ≡def expression
e.g., true ≡def λ x .λ y .x
66 / 210
Shorthands
I λ x1 .λ x2 .λ . . . .λ xn .E → λ x1 x2 . . . xn .E
I ((. . . ((E A1 ) A2 ) . . .) An ) → (E A1 A2 . . . An )
67 / 210
Purpose of types
68 / 210
Purpose of types
68 / 210
Purpose of types
68 / 210
Purpose of types
68 / 210
Purpose of types
I Error prevention
68 / 210
Purpose of types
I Error prevention
I Formal verification
68 / 210
No types
How are objects represented?
I A number, list or tree potentially designated
by the same value e.g.,
number 3 → λ x .λ y .x ← list (() () ())
69 / 210
No types
How are objects represented?
I A number, list or tree potentially designated
by the same value e.g.,
number 3 → λ x .λ y .x ← list (() () ())
69 / 210
No types
How are objects represented?
I A number, list or tree potentially designated
by the same value e.g.,
number 3 → λ x .λ y .x ← list (() () ())
69 / 210
No types
How are objects represented?
I A number, list or tree potentially designated
by the same value e.g.,
number 3 → λ x .λ y .x ← list (() () ())
x0
69 / 210
No types
How is correctness affected?
70 / 210
No types
How is correctness affected?
70 / 210
No types
How is correctness affected?
70 / 210
No types
How is correctness affected?
70 / 210
No types
How is correctness affected?
70 / 210
No types
How is correctness affected?
71 / 210
No types
Consequences
71 / 210
No types
Consequences
I Increased error-proneness
71 / 210
No types
Consequences
I Increased error-proneness
I Program instability
71 / 210
No types
Consequences
I Increased error-proneness
I Program instability
71 / 210
So...
72 / 210
So...
72 / 210
Contents
The λ0 language
Implementation
Recursion
Language specification
73 / 210
Definition
74 / 210
Definition
74 / 210
Definition
Components:
I base constructors: how are values built
I axioms: how
74 / 210
The Natural ADT
Base constructors and operators
I Base constructors:
I Operators:
75 / 210
The Natural ADT
Base constructors and operators
I Base constructors:
I zero : → Natural
I Operators:
75 / 210
The Natural ADT
Base constructors and operators
I Base constructors:
I zero : → Natural
I succ : Natural → Natural
I Operators:
75 / 210
The Natural ADT
Base constructors and operators
I Base constructors:
I zero : → Natural
I succ : Natural → Natural
I Operators:
I zero? : Natural → Bool
75 / 210
The Natural ADT
Base constructors and operators
I Base constructors:
I zero : → Natural
I succ : Natural → Natural
I Operators:
I zero? : Natural → Bool
I pred : Natural \ {zero} → Natural
75 / 210
The Natural ADT
Base constructors and operators
I Base constructors:
I zero : → Natural
I succ : Natural → Natural
I Operators:
I zero? : Natural → Bool
I pred : Natural \ {zero} → Natural
I add : Natural 2 → Natural
75 / 210
The Natural ADT
Axioms
I zero?
I pred
I add
76 / 210
The Natural ADT
Axioms
I zero?
I (zero ? zero ) = T
I pred
I add
76 / 210
The Natural ADT
Axioms
I zero?
I (zero ? zero ) = T
I (zero ? (succ n)) = F
I pred
I add
76 / 210
The Natural ADT
Axioms
I zero?
I (zero ? zero ) = T
I (zero ? (succ n)) = F
I pred
I (pred (succ n)) = n
I add
76 / 210
The Natural ADT
Axioms
I zero?
I (zero ? zero ) = T
I (zero ? (succ n)) = F
I pred
I (pred (succ n)) = n
I add
I (add zero n) = n
76 / 210
The Natural ADT
Axioms
I zero?
I (zero ? zero ) = T
I (zero ? (succ n)) = F
I pred
I (pred (succ n)) = n
I add
I (add zero n) = n
I (add (succ m) n) = (succ (add m n))
76 / 210
Providing axioms
77 / 210
Providing axioms
I More — useless
77 / 210
Providing axioms
I More — useless
77 / 210
From ADTs to functional programming
Exemple
I Axiome:
I add (zero, n) = n
I add (succ (m), n) = succ (add (m, n))
I Scheme:
1 (define add
2 (lambda (m n)
3 (if (zero? m) n
4 (+ 1 (add (- m 1) n)))))
I Haskell:
1 add 0 n = n
2 add (m + 1) n = 1 + (add m n)
78 / 210
From ADTs to functional programming
Discussion
79 / 210
From ADTs to functional programming
Discussion
79 / 210
From ADTs to functional programming
Discussion
79 / 210
From ADTs to functional programming
Discussion
79 / 210
From ADTs to functional programming
Discussion
79 / 210
Contents
The λ0 language
Implementation
Recursion
Language specification
80 / 210
The Bool ADT
Base contrsuctors and operators
I Base constructors:
I Operators:
81 / 210
The Bool ADT
Base contrsuctors and operators
I Base constructors:
I T : → Bool
I Operators:
81 / 210
The Bool ADT
Base contrsuctors and operators
I Base constructors:
I T : → Bool
I F : → Bool
I Operators:
81 / 210
The Bool ADT
Base contrsuctors and operators
I Base constructors:
I T : → Bool
I F : → Bool
I Operators:
I not : Bool → Bool
81 / 210
The Bool ADT
Base contrsuctors and operators
I Base constructors:
I T : → Bool
I F : → Bool
I Operators:
I not : Bool → Bool
I and : Bool 2 → Bool
81 / 210
The Bool ADT
Base contrsuctors and operators
I Base constructors:
I T : → Bool
I F : → Bool
I Operators:
I not : Bool → Bool
I and : Bool 2 → Bool
I or : Bool 2 → Bool
81 / 210
The Bool ADT
Base contrsuctors and operators
I Base constructors:
I T : → Bool
I F : → Bool
I Operators:
I not : Bool → Bool
I and : Bool 2 → Bool
I or : Bool 2 → Bool
I if : Bool × T × T → T
81 / 210
The Bool ADT
Axioms
I not
I and
I or
I if
82 / 210
The Bool ADT
Axioms
I not
I (not T ) = F
I and
I or
I if
82 / 210
The Bool ADT
Axioms
I not
I (not T ) = F
I (not F ) = T
I and
I or
I if
82 / 210
The Bool ADT
Axioms
I not
I (not T ) = F
I (not F ) = T
I and
I (and T a) = a
I or
I if
82 / 210
The Bool ADT
Axioms
I not
I (not T ) = F
I (not F ) = T
I and
I (and T a) = a
I (and F a) = F
I or
I if
82 / 210
The Bool ADT
Axioms
I not
I (not T ) = F
I (not F ) = T
I and
I (and T a) = a
I (and F a) = F
I or
I (or T a) = T
I if
82 / 210
The Bool ADT
Axioms
I not
I (not T ) = F
I (not F ) = T
I and
I (and T a) = a
I (and F a) = F
I or
I (or T a) = T
I (or F a) = a
I if
82 / 210
The Bool ADT
Axioms
I not
I (not T ) = F
I (not F ) = T
I and
I (and T a) = a
I (and F a) = F
I or
I (or T a) = T
I (or F a) = a
I if
I (if T a b ) = a
82 / 210
The Bool ADT
Axioms
I not
I (not T ) = F
I (not F ) = T
I and
I (and T a) = a
I (and F a) = F
I or
I (or T a) = T
I (or F a) = a
I if
I (if T a b ) = a
I (if F a b ) = b
82 / 210
The Bool ADT
Base constructor implementation
I T ≡def λ xy .x
I F ≡def λ xy .y
I Selector-like behavior:
I (T a b ) → (λ xy .x a b ) → a
I (F a b ) → (λ xy .y a b ) → b
83 / 210
The Bool ADT
Operator implementation
I not ≡def
I (not T ) →F
I (not F ) →T
I and ≡def
I (and T a) →a
I (and F a) →F
I or ≡def
I (or T a) →T
I (or F a) →a
I if ≡def
I (if T a b ) →a
I (if F a b ) →b
84 / 210
The Bool ADT
Operator implementation
I not ≡def λ x .(x F T )
I (not T ) → (λ x .(x F T ) T ) → (T F T ) → F
I (not F ) → (λ x .(x F T ) F ) → (F F T ) → T
I and ≡def
I (and T a) →a
I (and F a) →F
I or ≡def
I (or T a) →T
I (or F a) →a
I if ≡def
I (if T a b ) →a
I (if F a b ) →b
84 / 210
The Bool ADT
Operator implementation
I not ≡def λ x .(x F T )
I (not T ) → (λ x .(x F T ) T ) → (T F T ) → F
I (not F ) → (λ x .(x F T ) F ) → (F F T ) → T
I or ≡def
I (or T a) →T
I (or F a) →a
I if ≡def
I (if T a b ) →a
I (if F a b ) →b
84 / 210
The Bool ADT
Operator implementation
I not ≡def λ x .(x F T )
I (not T ) → (λ x .(x F T ) T ) → (T F T ) → F
I (not F ) → (λ x .(x F T ) F ) → (F F T ) → T
I or ≡def λ xy .(x T y )
I (or T a) → (λ xy .(x T y ) T a) → (T T a) → T
I (or F a) → (λ xy .(x T y ) F a) → (F T a) → a
I if ≡def
I (if T a b ) →a
I (if F a b ) →b
84 / 210
The Bool ADT
Operator implementation
I not ≡def λ x .(x F T )
I (not T ) → (λ x .(x F T ) T ) → (T F T ) → F
I (not F ) → (λ x .(x F T ) F ) → (F F T ) → T
I or ≡def λ xy .(x T y )
I (or T a) → (λ xy .(x T y ) T a) → (T T a) → T
I (or F a) → (λ xy .(x T y ) F a) → (F T a) → a
I Base constructors:
I Operators:
I Axioms:
85 / 210
The Pair ADT
Specification
I Base constructors:
I pair : A × B → Pair
I Operators:
I Axioms:
85 / 210
The Pair ADT
Specification
I Base constructors:
I pair : A × B → Pair
I Operators:
I fst : Pair → A
I Axioms:
85 / 210
The Pair ADT
Specification
I Base constructors:
I pair : A × B → Pair
I Operators:
I fst : Pair → A
I snd : Pair → B
I Axioms:
85 / 210
The Pair ADT
Specification
I Base constructors:
I pair : A × B → Pair
I Operators:
I fst : Pair → A
I snd : Pair → B
I Axioms:
I (fst (pair a b )) = a
85 / 210
The Pair ADT
Specification
I Base constructors:
I pair : A × B → Pair
I Operators:
I fst : Pair → A
I snd : Pair → B
I Axioms:
I (fst (pair a b )) = a
I (snd (pair a b )) = b
85 / 210
The Pair ADT
Implementation
I pair ≡def
I (pair a b )
I fst ≡def
I (fst (pair a b ))
→a
I snd ≡def
I (snd (pair a b ))
→b
86 / 210
The Pair ADT
Implementation
I fst ≡def
I (fst (pair a b ))
→a
I snd ≡def
I (snd (pair a b ))
→b
86 / 210
The Pair ADT
Implementation
I snd ≡def
I (snd (pair a b ))
→b
86 / 210
The Pair ADT
Implementation
86 / 210
The List ADT
Base constructors and operators
I Base constructors:
I Operators:
87 / 210
The List ADT
Base constructors and operators
I Base constructors:
I null : → List
I Operators:
87 / 210
The List ADT
Base constructors and operators
I Base constructors:
I null : → List
I cons : A × List → List
I Operators:
87 / 210
The List ADT
Base constructors and operators
I Base constructors:
I null : → List
I cons : A × List → List
I Operators:
I car : List \ {null } → A
87 / 210
The List ADT
Base constructors and operators
I Base constructors:
I null : → List
I cons : A × List → List
I Operators:
I car : List \ {null } → A
I cdr : List \ {null } → List
87 / 210
The List ADT
Base constructors and operators
I Base constructors:
I null : → List
I cons : A × List → List
I Operators:
I car : List \ {null } → A
I cdr : List \ {null } → List
I null ? : List → Bool
87 / 210
The List ADT
Base constructors and operators
I Base constructors:
I null : → List
I cons : A × List → List
I Operators:
I car : List \ {null } → A
I cdr : List \ {null } → List
I null ? : List → Bool
I append : List 2 → List
87 / 210
The List ADT
Axioms
I car
I cdr
I null ?
I append
88 / 210
The List ADT
Axioms
I car
I (car (cons e L)) = e
I cdr
I null ?
I append
88 / 210
The List ADT
Axioms
I car
I (car (cons e L)) = e
I cdr
I (cdr (cons e L)) = L
I null ?
I append
88 / 210
The List ADT
Axioms
I car
I (car (cons e L)) = e
I cdr
I (cdr (cons e L)) = L
I null ?
I (null ? null ) = T
I append
88 / 210
The List ADT
Axioms
I car
I (car (cons e L)) = e
I cdr
I (cdr (cons e L)) = L
I null ?
I (null ? null ) = T
I (null ? (cons e L)) = F
I append
88 / 210
The List ADT
Axioms
I car
I (car (cons e L)) = e
I cdr
I (cdr (cons e L)) = L
I null ?
I (null ? null ) = T
I (null ? (cons e L)) = F
I append
I (append null B ) = B
88 / 210
The List ADT
Axioms
I car
I (car (cons e L)) = e
I cdr
I (cdr (cons e L)) = L
I null ?
I (null ? null ) = T
I (null ? (cons e L)) = F
I append
I (append null B ) = B
I (append (cons e A) B ) = (cons e (append A B ))
88 / 210
The List ADT
Implementation
I Intuition:
I null ≡def
I cons ≡def
I car ≡def
I cdr ≡def
I null ? ≡def
I (null ? null ) →T
I (null ? (cons e L))
→F
I append ≡def
89 / 210
The List ADT
Implementation
I Intuition: a list = a (head, tail) pair
I null ≡def
I cons ≡def
I car ≡def
I cdr ≡def
I null ? ≡def
I (null ? null ) →T
I (null ? (cons e L))
→F
I append ≡def
89 / 210
The List ADT
Implementation
I Intuition: a list = a (head, tail) pair
I null ≡def λ x .T
I cons ≡def
I car ≡def
I cdr ≡def
I null ? ≡def
I (null ? null ) →T
I (null ? (cons e L))
→F
I append ≡def
89 / 210
The List ADT
Implementation
I Intuition: a list = a (head, tail) pair
I null ≡def λ x .T
I cons ≡def pair
I car ≡def
I cdr ≡def
I null ? ≡def
I (null ? null ) →T
I (null ? (cons e L))
→F
I append ≡def
89 / 210
The List ADT
Implementation
I Intuition: a list = a (head, tail) pair
I null ≡def λ x .T
I cons ≡def pair
I car ≡def fst
I cdr ≡def
I null ? ≡def
I (null ? null ) →T
I (null ? (cons e L))
→F
I append ≡def
89 / 210
The List ADT
Implementation
I Intuition: a list = a (head, tail) pair
I null ≡def λ x .T
I cons ≡def pair
I car ≡def fst
I cdr ≡def snd
I null ? ≡def
I (null ? null ) →T
I (null ? (cons e L))
→F
I append ≡def
89 / 210
The List ADT
Implementation
I Intuition: a list = a (head, tail) pair
I null ≡def λ x .T
I cons ≡def pair
I car ≡def fst
I cdr ≡def snd
I null ? ≡def λ L.(L λ xy .F )
I (null ? null ) → (λ L.(L λ xy .F ) λ x .T ) → (λ x .T . . .) → T
I (null ? (cons e L)) → (λ L.(L λ xy .F ) λ s .(s e L)) →
(λ s .(s e L) λ xy .F ) → (λ xy .F e L) → F
I append ≡def
89 / 210
The List ADT
Implementation
I Intuition: a list = a (head, tail) pair
I null ≡def λ x .T
I cons ≡def pair
I car ≡def fst
I cdr ≡def snd
I null ? ≡def λ L.(L λ xy .F )
I (null ? null ) → (λ L.(L λ xy .F ) λ x .T ) → (λ x .T . . .) → T
I (null ? (cons e L)) → (λ L.(L λ xy .F ) λ s .(s e L)) →
(λ s .(s e L) λ xy .F ) → (λ xy .F e L) → F
I append ≡def
λ AB .(if (null ? A) B (cons (car A) (append (cdr A) B )))
89 / 210
The List ADT
Implementation
I Intuition: a list = a (head, tail) pair
I null ≡def λ x .T
I cons ≡def pair
I car ≡def fst
I cdr ≡def snd
I null ? ≡def λ L.(L λ xy .F )
I (null ? null ) → (λ L.(L λ xy .F ) λ x .T ) → (λ x .T . . .) → T
I (null ? (cons e L)) → (λ L.(L λ xy .F ) λ s .(s e L)) →
(λ s .(s e L) λ xy .F ) → (λ xy .F e L) → F
I zero?
I (zero ? zero ) = T
I (zero ? (succ n)) = F
I pred
I (pred (succ n)) = n
I add
I (add zero n) = n
I (add (succ m) n) = (succ (add m n))
90 / 210
The Natural ADT
Implementation
I Intuition:
I zero ≡def
I succ ≡def
I zero? ≡def
I pred ≡def
I add ≡def
91 / 210
The Natural ADT
Implementation
91 / 210
The Natural ADT
Implementation
91 / 210
The Natural ADT
Implementation
91 / 210
The Natural ADT
Implementation
91 / 210
The Natural ADT
Implementation
91 / 210
The Natural ADT
Implementation
91 / 210
Contents
The λ0 language
Implementation
Recursion
Language specification
92 / 210
Functions
I Several possible definitions of the identity function:
93 / 210
Functions
I Several possible definitions of the identity function:
I id (n) = n
93 / 210
Functions
I Several possible definitions of the identity function:
I id (n) = n
I id (n) = n + 1 − 1
93 / 210
Functions
I Several possible definitions of the identity function:
I id (n) = n
I id (n) = n + 1 − 1
I id (n) = n + 2 − 2
93 / 210
Functions
I Several possible definitions of the identity function:
I id (n) = n
I id (n) = n + 1 − 1
I id (n) = n + 2 − 2
I ...
93 / 210
Functions
I Several possible definitions of the identity function:
I id (n) = n
I id (n) = n + 1 − 1
I id (n) = n + 2 − 2
I ...
93 / 210
Functions
I Several possible definitions of the identity function:
I id (n) = n
I id (n) = n + 1 − 1
I id (n) = n + 2 − 2
I ...
93 / 210
Functions
I Several possible definitions of the identity function:
I id (n) = n
I id (n) = n + 1 − 1
I id (n) = n + 2 − 2
I ...
94 / 210
Perspectives on recursion
94 / 210
Perspectives on recursion
94 / 210
Implementing length
Problem
I Length of a list:
95 / 210
Implementing length
Problem
I Length of a list:
95 / 210
Implementing length
Problem
I Length of a list:
95 / 210
Implementing length
Problem
I Length of a list:
95 / 210
Contents
The λ0 language
Implementation
Recursion
Language specification
96 / 210
Axiomatization benefits
I Disambiguation
I Proof of properties
I Implementation skeleton
97 / 210
Syntax
I Variable:
98 / 210
Syntax
I Variable:
I Expression:
Expr :: = Var
| λ Var .Expr
| (Expr Expr )
98 / 210
Syntax
I Variable:
I Expression:
Expr :: = Var
| λ Var .Expr
| (Expr Expr )
I Value:
Val ::= λ Var .Expr
98 / 210
Evaluation rules
Rule name:
precondition1 , . . . , preconditionn
conclusion
99 / 210
Semantics for normal-order evaluation
Evaluation
I Reduce:
(λ x .e e0 ) → e[e0 /x ]
100 / 210
Semantics for normal-order evaluation
Evaluation
I Reduce:
(λ x .e e0 ) → e[e0 /x ]
I Eval:
e → e0
(e e00 ) → (e0 e00 )
100 / 210
Semantics for normal-order evaluation
Substitution
I x[e/x ] =
101 / 210
Semantics for normal-order evaluation
Substitution
I x[e/x ] = e
I y[e/x ] =
101 / 210
Semantics for normal-order evaluation
Substitution
I x[e/x ] = e
I y[e/x ] = y
101 / 210
Semantics for normal-order evaluation
Substitution
I x[e/x ] = e
I y[e/x ] = y , y 6= x
I hλ x .ei[e0 /x ] =
101 / 210
Semantics for normal-order evaluation
Substitution
I x[e/x ] = e
I y[e/x ] = y , y 6= x
I hλ x .ei[e0 /x ] = λ x .e
I hλ y .ei[e0 /x ] =
101 / 210
Semantics for normal-order evaluation
Substitution
I x[e/x ] = e
I y[e/x ] = y , y 6= x
I hλ x .ei[e0 /x ] = λ x .e
I hλ y .ei[e0 /x ] = λ y .e[e0 /x ]
101 / 210
Semantics for normal-order evaluation
Substitution
I x[e/x ] = e
I y[e/x ] = y , y 6= x
I hλ x .ei[e0 /x ] = λ x .e
I hλ y .ei[e0 /x ] = λ y .e[e0 /x ] , y 6= x
101 / 210
Semantics for normal-order evaluation
Substitution
I x[e/x ] = e
I y[e/x ] = y , y 6= x
I hλ x .ei[e0 /x ] = λ x .e
101 / 210
Semantics for normal-order evaluation
Substitution
I x[e/x ] = e
I y[e/x ] = y , y 6= x
I hλ x .ei[e0 /x ] = λ x .e
101 / 210
Semantics for normal-order evaluation
Free variables
I FV (x ) = {x }
I FV (λ x .e) = FV (e) \ {x }
102 / 210
Semantics for normal-order evaluation
Example
((λ x .λ y .y a) b )
( )
103 / 210
Semantics for normal-order evaluation
Example
((λ x .λ y .y a) b )
(λ x .λ y .y a) → λ y .y (Reduce)
( )
103 / 210
Semantics for normal-order evaluation
Example
((λ x .λ y .y a) b )
(λ x .λ y .y a) → λ y .y (Reduce)
(Eval )
((λ x .λ y .y a) b ) → (λ y .y b )
103 / 210
Semantics for normal-order evaluation
Example
((λ x .λ y .y a) b )
(λ x .λ y .y a) → λ y .y (Reduce)
(Eval )
((λ x .λ y .y a) b ) → (λ y .y b )
(λ y .y b ) → b (Reduce)
103 / 210
Semantics for applicative-order evaluation
Evaluation
I Reduce (v ∈ Val):
(λ x .e v ) → e[v /x ]
104 / 210
Semantics for applicative-order evaluation
Evaluation
I Reduce (v ∈ Val):
(λ x .e v ) → e[v /x ]
I Eval 1 :
e → e0
(e e00 ) → (e0 e00 )
104 / 210
Semantics for applicative-order evaluation
Evaluation
I Reduce (v ∈ Val):
(λ x .e v ) → e[v /x ]
I Eval 1 :
e → e0
(e e00 ) → (e0 e00 )
I Eval 2 (e 6∈ Val):
e → e0
(λ x .e00 e) → (λ x .e00 e0 )
104 / 210
Formal proof
105 / 210
Formal proof
Proof.
Structural induction, according to the different forms of
λ -expressions (see the lecture notes).
105 / 210
Summary
106 / 210
Part IV
107 / 210
Contents
Introduction
Extending STLC
Type reconstruction
108 / 210
Contents
Introduction
Extending STLC
Type reconstruction
109 / 210
Drawbacks of the absence of types
I Meaningless expressions e.g., (car 3)
110 / 210
Drawbacks of the absence of types
I Meaningless expressions e.g., (car 3)
110 / 210
Drawbacks of the absence of types
I Meaningless expressions e.g., (car 3)
110 / 210
Drawbacks of the absence of types
I Meaningless expressions e.g., (car 3)
110 / 210
Solution
111 / 210
Solution
111 / 210
Desired properties
112 / 210
Desired properties
112 / 210
Desired properties
112 / 210
Contents
Introduction
Extending STLC
Type reconstruction
113 / 210
Base and simple types
114 / 210
Base and simple types
114 / 210
Base and simple types
Notation:
I e : τ : “expression e has type τ ”
114 / 210
Base and simple types
Notation:
I e : τ : “expression e has type τ ”
I v ∈ τ : “v is a value of type τ ”
114 / 210
Base and simple types
Notation:
I e : τ : “expression e has type τ ”
I v ∈ τ : “v is a value of type τ ”
I e∈τ ⇒e:τ
114 / 210
Base and simple types
Notation:
I e : τ : “expression e has type τ ”
I v ∈ τ : “v is a value of type τ ”
I e∈τ ⇒e:τ
I e : τ 6⇒ e ∈ τ
114 / 210
Typed λ -expressions
115 / 210
Typed λ -expressions
115 / 210
Typed λ -expressions
115 / 210
Typed λ -expressions
115 / 210
Relation to untyped lambda calculus
Similarities
I β -reduction
I α -conversion
I normal forms
I Church-Rosser theorem
116 / 210
Relation to untyped lambda calculus
Similarities
I β -reduction
I α -conversion
I normal forms
I Church-Rosser theorem
Differences
I (x : τ x : τ) invalid
116 / 210
Relation to untyped lambda calculus
Similarities
I β -reduction
I α -conversion
I normal forms
I Church-Rosser theorem
Differences
I (x : τ x : τ) invalid
I some fixed-point combinators are invalid
116 / 210
Syntax
Expressions
I Variables:
Var ::= ...
117 / 210
Syntax
Expressions
I Variables:
Var ::= ...
I Expressions:
117 / 210
Syntax
Expressions
I Variables:
Var ::= ...
I Expressions:
I Values:
Val ::= BaseVal
| λ Var : Type.Expr
117 / 210
Syntax
Types
I Types:
Type ::= BaseType
| (Type → Type)
118 / 210
Syntax
Types
I Types:
Type ::= BaseType
| (Type → Type)
I Typing contexts:
TypingContext ::= 0/
| TypingContext , Var : Type
118 / 210
Syntax
Types
I Types:
Type ::= BaseType
| (Type → Type)
I Typing contexts:
I include variable-type associations
i.e., typing hypotheses
TypingContext ::= 0/
| TypingContext , Var : Type
118 / 210
Semantics for normal-order evaluation
Evaluation
I Reduce:
(λ x : τ.e e0 ) → e[e0 /x ]
119 / 210
Semantics for normal-order evaluation
Evaluation
I Reduce:
(λ x : τ.e e0 ) → e[e0 /x ]
I Eval:
e → e0
(e e00 ) → (e0 e00 )
119 / 210
Semantics for normal-order evaluation
Evaluation
I Reduce:
(λ x : τ.e e0 ) → e[e0 /x ]
I Eval:
e → e0
(e e00 ) → (e0 e00 )
119 / 210
Semantics
Typing
I TBaseVal:
v ∈ τb
Γ ` v : τb
120 / 210
Semantics
Typing
I TBaseVal:
v ∈ τb
Γ ` v : τb
I TVar :
x :τ ∈Γ
Γ ` x :τ
120 / 210
Semantics
Typing
I TBaseVal:
v ∈ τb
Γ ` v : τb
I TVar :
x :τ ∈Γ
Γ ` x :τ
I TAbs:
Γ, x : τ ` e : τ 0
Γ ` λ x : τ.e : (τ → τ 0 )
120 / 210
Semantics
Typing
I TBaseVal:
v ∈ τb
Γ ` v : τb
I TVar :
x :τ ∈Γ
Γ ` x :τ
I TAbs:
Γ, x : τ ` e : τ 0
Γ ` λ x : τ.e : (τ → τ 0 )
I TApp:
Γ ` e : (τ 0 → τ) Γ ` e0 : τ 0
Γ ` (e e 0 ) : τ
120 / 210
Typing example
λ x : τ1 .λ y : τ2 .x : (τ1 → (τ2 → τ1 ))
Blackboard!
121 / 210
Type systems
122 / 210
Type systems
122 / 210
Type systems
122 / 210
Contents
Introduction
Extending STLC
Type reconstruction
123 / 210
Ways of extending STLC
124 / 210
Ways of extending STLC
124 / 210
The product type
Algebraic specification
125 / 210
The product type
Algebraic specification
125 / 210
The product type
Algebraic specification
I Operators:
125 / 210
The product type
Algebraic specification
I Operators:
I fst : τ ∗ τ 0 → τ
125 / 210
The product type
Algebraic specification
I Operators:
I fst : τ ∗ τ 0 → τ
I snd : τ ∗ τ 0 → τ 0
125 / 210
The product type
Algebraic specification
I Operators:
I fst : τ ∗ τ 0 → τ
I snd : τ ∗ τ 0 → τ 0
I Axioms (e : τ , e0 : τ 0 ):
125 / 210
The product type
Algebraic specification
I Operators:
I fst : τ ∗ τ 0 → τ
I snd : τ ∗ τ 0 → τ 0
I Axioms (e : τ , e0 : τ 0 ):
I (fst (e, e0 )) → e
125 / 210
The product type
Algebraic specification
I Operators:
I fst : τ ∗ τ 0 → τ
I snd : τ ∗ τ 0 → τ 0
I Axioms (e : τ , e0 : τ 0 ):
I (fst (e, e0 )) → e
I (snd (e, e0 )) → e0
125 / 210
The product type
Syntax
126 / 210
The product type
Syntax
126 / 210
The product type
Syntax
126 / 210
The product type
Syntax
126 / 210
The product type
Syntax
126 / 210
The product type
Evaluation
I EvalFst:
(fst (e, e0 )) → e
127 / 210
The product type
Evaluation
I EvalFst:
(fst (e, e0 )) → e
I EvalSnd:
(snd (e, e0 )) → e0
127 / 210
The product type
Evaluation
I EvalFst:
(fst (e, e0 )) → e
I EvalSnd:
(snd (e, e0 )) → e0
I EvalFstApp:
e → e0
(fst e) → (fst e0 )
127 / 210
The product type
Evaluation
I EvalFst:
(fst (e, e0 )) → e
I EvalSnd:
(snd (e, e0 )) → e0
I EvalFstApp:
e → e0
(fst e) → (fst e0 )
I EvalSndApp:
e → e0
(snd e) → (snd e0 )
127 / 210
The product type
Typing
I TProduct:
Γ ` e:τ Γ ` e0 : τ 0
Γ ` (e, e ) : (τ ∗ τ 0 )
0
128 / 210
The product type
Typing
I TProduct:
Γ ` e:τ Γ ` e0 : τ 0
Γ ` (e, e ) : (τ ∗ τ 0 )
0
I TFst:
Γ ` e : (τ ∗ τ 0 )
Γ ` (fst e) : τ
128 / 210
The product type
Typing
I TProduct:
Γ ` e:τ Γ ` e0 : τ 0
Γ ` (e, e ) : (τ ∗ τ 0 )
0
I TFst:
Γ ` e : (τ ∗ τ 0 )
Γ ` (fst e) : τ
I TSnd:
Γ ` e : (τ ∗ τ 0 )
Γ ` (snd e) : τ 0
128 / 210
The product type
Typing example
Blackboard!
129 / 210
The Bool type
Algebraic specification
130 / 210
The Bool type
Algebraic specification
130 / 210
The Bool type
Algebraic specification
I Operators:
130 / 210
The Bool type
Algebraic specification
I Operators:
I not : Bool → Bool
130 / 210
The Bool type
Algebraic specification
I Operators:
I not : Bool → Bool
I and : Bool 2 → Bool
130 / 210
The Bool type
Algebraic specification
I Operators:
I not : Bool → Bool
I and : Bool 2 → Bool
I or : Bool 2 → Bool
130 / 210
The Bool type
Algebraic specification
I Operators:
I not : Bool → Bool
I and : Bool 2 → Bool
I or : Bool 2 → Bool
I if : Bool × τ × τ → τ
130 / 210
The Bool type
Algebraic specification
I Operators:
I not : Bool → Bool
I and : Bool 2 → Bool
I or : Bool 2 → Bool
I if : Bool × τ × τ → τ
130 / 210
The Bool type
Syntax
131 / 210
The Bool type
Syntax
131 / 210
The Bool type
Syntax
131 / 210
The Bool type
Syntax
131 / 210
The Bool type
Syntax
131 / 210
The Bool type
Evaluation
I EvalIfT :
(if True e e0 ) → e
132 / 210
The Bool type
Evaluation
I EvalIfT :
(if True e e0 ) → e
I EvalIfF :
(if False e e0 ) → e0
132 / 210
The Bool type
Evaluation
I EvalIfT :
(if True e e0 ) → e
I EvalIfF :
(if False e e0 ) → e0
I EvalIf :
e → e0
(if e e1 e2 ) → (if e0 e1 e2 )
132 / 210
The Bool type
Typing
I TTrue:
Γ ` True : Bool
133 / 210
The Bool type
Typing
I TTrue:
Γ ` True : Bool
I TFalse:
Γ ` False : Bool
133 / 210
The Bool type
Typing
I TTrue:
Γ ` True : Bool
I TFalse:
Γ ` False : Bool
I TIf :
Γ ` e : Bool Γ ` e1 : τ Γ ` e2 : τ
Γ ` (if e e1 e2 ) : τ
133 / 210
The Bool type
Top-level variable bindings
134 / 210
The Bool type
Top-level variable bindings
134 / 210
The Bool type
Top-level variable bindings
134 / 210
The Bool type
Top-level variable bindings
134 / 210
The N type
Algebraic specification
135 / 210
The N type
Algebraic specification
135 / 210
The N type
Algebraic specification
I Operators:
135 / 210
The N type
Algebraic specification
I Operators:
I + : N2 → N
135 / 210
The N type
Algebraic specification
I Operators:
I + : N2 → N
I zero? : N → Bool
135 / 210
The N type
Algebraic specification
I Operators:
I + : N2 → N
I zero? : N → Bool
135 / 210
The N type
Algebraic specification
I Operators:
I + : N2 → N
I zero? : N → Bool
135 / 210
The N type
Algebraic specification
I Operators:
I + : N2 → N
I zero? : N → Bool
135 / 210
The N type
Algebraic specification
I Operators:
I + : N2 → N
I zero? : N → Bool
135 / 210
The N type
Algebraic specification
I Operators:
I + : N2 → N
I zero? : N → Bool
135 / 210
The N type
Operator semantics
136 / 210
The N type
Operator semantics
136 / 210
The N type
Operator semantics
136 / 210
The N type
Syntax
137 / 210
The N type
Syntax
137 / 210
The N type
Syntax
137 / 210
The N type
Syntax
NVal ::= 0
| (succ NVal )
137 / 210
The N type
Syntax
NVal ::= 0
| (succ NVal )
138 / 210
The N type
Evaluation
I EvalSucc:
e → e0
(succ e) → (succ e0 )
I EvalPrec N0 :
(prec N e0 f 0) → e0
138 / 210
The N type
Evaluation
I EvalSucc:
e → e0
(succ e) → (succ e0 )
I EvalPrec N0 :
(prec N e0 f 0) → e0
I EvalPrec N1 (n ∈ N):
(prec N e0 f (succ n)) → (f n (prec N e0 f n))
138 / 210
The N type
Evaluation
I EvalSucc:
e → e0
(succ e) → (succ e0 )
I EvalPrec N0 :
(prec N e0 f 0) → e0
I EvalPrec N1 (n ∈ N):
(prec N e0 f (succ n)) → (f n (prec N e0 f n))
I EvalPrec N2 :
e → e0
(prec N e0 f e) → (prec N e0 f e0 )
138 / 210
The N type
Typing
I TZero:
Γ ` 0:N
139 / 210
The N type
Typing
I TZero:
Γ ` 0:N
I TSucc:
Γ ` e:N
Γ ` (succ e) : N
139 / 210
The N type
Typing
I TZero:
Γ ` 0:N
I TSucc:
Γ ` e:N
Γ ` (succ e) : N
I TPrec N :
Γ ` e0 : τ Γ ` f :N→τ →τ Γ ` e:N
Γ ` (prec N e0 f e) : τ
139 / 210
The N type
Top-level variable bindings
140 / 210
The N type
Top-level variable bindings
140 / 210
The N type
Top-level variable bindings
140 / 210
The (List τ) type
Algebraic specification
I Base constructors i.e., canonical values:
141 / 210
The (List τ) type
Algebraic specification
I Base constructors i.e., canonical values:
I (List τ) ::= []τ | (cons τ (List τ))
141 / 210
The (List τ) type
Algebraic specification
I Base constructors i.e., canonical values:
I (List τ) ::= []τ | (cons τ (List τ))
I Operators:
141 / 210
The (List τ) type
Algebraic specification
I Base constructors i.e., canonical values:
I (List τ) ::= []τ | (cons τ (List τ))
I Operators:
I head : (List τ) \ {[]} → τ
141 / 210
The (List τ) type
Algebraic specification
I Base constructors i.e., canonical values:
I (List τ) ::= []τ | (cons τ (List τ))
I Operators:
I head : (List τ) \ {[]} → τ
I tail : (List τ) \ {[]} → (List τ)
141 / 210
The (List τ) type
Algebraic specification
I Base constructors i.e., canonical values:
I (List τ) ::= []τ | (cons τ (List τ))
I Operators:
I head : (List τ) \ {[]} → τ
I tail : (List τ) \ {[]} → (List τ)
I length : (List τ) → N
141 / 210
The (List τ) type
Algebraic specification
I Base constructors i.e., canonical values:
I (List τ) ::= []τ | (cons τ (List τ))
I Operators:
I head : (List τ) \ {[]} → τ
I tail : (List τ) \ {[]} → (List τ)
I length : (List τ) → N
141 / 210
The (List τ) type
Algebraic specification
I Base constructors i.e., canonical values:
I (List τ) ::= []τ | (cons τ (List τ))
I Operators:
I head : (List τ) \ {[]} → τ
I tail : (List τ) \ {[]} → (List τ)
I length : (List τ) → N
141 / 210
The (List τ) type
Algebraic specification
I Base constructors i.e., canonical values:
I (List τ) ::= []τ | (cons τ (List τ))
I Operators:
I head : (List τ) \ {[]} → τ
I tail : (List τ) \ {[]} → (List τ)
I length : (List τ) → N
141 / 210
The (List τ) type
Algebraic specification
I Base constructors i.e., canonical values:
I (List τ) ::= []τ | (cons τ (List τ))
I Operators:
I head : (List τ) \ {[]} → τ
I tail : (List τ) \ {[]} → (List τ)
I length : (List τ) → N
141 / 210
The (List τ) type
Algebraic specification
I Base constructors i.e., canonical values:
I (List τ) ::= []τ | (cons τ (List τ))
I Operators:
I head : (List τ) \ {[]} → τ
I tail : (List τ) \ {[]} → (List τ)
I length : (List τ) → N
142 / 210
The (List τ) type
Syntax
142 / 210
The (List τ) type
Syntax
142 / 210
The (List τ) type
Syntax
ListVal ::= []
| (cons Value ListVal )
142 / 210
The (List τ) type
Syntax
ListVal ::= []
| (cons Value ListVal )
143 / 210
The (List τ) type
Evaluation
I EvalCons:
e → e0
(cons e e00 ) → (cons e0 e00 )
I EvalPrec L0 :
(prec L e0 f []) → e0
143 / 210
The (List τ) type
Evaluation
I EvalCons:
e → e0
(cons e e00 ) → (cons e0 e00 )
I EvalPrec L0 :
(prec L e0 f []) → e0
I EvalPrec L1 (v ∈ Value):
(prec L e0 f (cons v e)) → (f v e (prec L e0 f e))
143 / 210
The (List τ) type
Evaluation
I EvalCons:
e → e0
(cons e e00 ) → (cons e0 e00 )
I EvalPrec L0 :
(prec L e0 f []) → e0
I EvalPrec L1 (v ∈ Value):
(prec L e0 f (cons v e)) → (f v e (prec L e0 f e))
I EvalPrec L2 :
e → e0
(prec L e0 f e) → (prec L e0 f e0 )
143 / 210
The (List τ) type
Typing
I TEmpty :
Γ ` []τ : (List τ)
144 / 210
The (List τ) type
Typing
I TEmpty :
Γ ` []τ : (List τ)
I TCons:
Γ ` e:τ Γ ` e0 : (List τ)
Γ ` (cons e e0 ) : (List τ)
144 / 210
The (List τ) type
Typing
I TEmpty :
Γ ` []τ : (List τ)
I TCons:
Γ ` e:τ Γ ` e0 : (List τ)
Γ ` (cons e e0 ) : (List τ)
I TPrec L :
Γ ` e0 : τ 0 Γ ` f : τ → (List τ) → τ 0 → τ 0 Γ ` e : (List τ)
Γ ` (prec L e0 f e) : τ 0
144 / 210
The (List τ) type
Top-level variable bindings
145 / 210
The (List τ) type
Top-level variable bindings
145 / 210
The (List τ) type
Top-level variable bindings
145 / 210
General recursion
I Primitive recursion
146 / 210
General recursion
I Primitive recursion
I induces strong normalization
146 / 210
General recursion
I Primitive recursion
I induces strong normalization
I insufficient for capturing effectively computable
functions
146 / 210
General recursion
I Primitive recursion
I induces strong normalization
I insufficient for capturing effectively computable
functions
146 / 210
General recursion
I Primitive recursion
I induces strong normalization
I insufficient for capturing effectively computable
functions
146 / 210
fix
Syntax
147 / 210
fix
Syntax
147 / 210
fix
Evaluation
I EvalFix:
148 / 210
fix
Evaluation
I EvalFix:
I EvalFix 0 :
e → e0
(fix e) → (fix e0 )
148 / 210
fix
Typing
I TFix:
Γ ` e : (τ → τ)
Γ ` (fix e) : τ
149 / 210
fix
Example
remainder = λ m : N.λ n : N.
((fix λ f : (N → N).λ p : N.
(if p < n then p else (f (p − n)))) m)
150 / 210
Monomorphism
151 / 210
Monomorphism
151 / 210
Monomorphism
151 / 210
Monomorphism
I []N , []Bool , . . .
151 / 210
Monomorphism
I []N , []Bool , . . .
151 / 210
Contents
Introduction
Extending STLC
Type reconstruction
152 / 210
Idea
id N ≡ λ x : N.x : (N → N)
153 / 210
Idea
id N ≡ λ x : N.x : (N → N)
id ≡ λ X .λ x : X .x : ∀X .(X → X )
153 / 210
Idea
id N ≡ λ x : N.x : (N → N)
id ≡ λ X .λ x : X .x : ∀X .(X → X )
153 / 210
Syntax
154 / 210
Syntax
154 / 210
Syntax
I Expressions:
155 / 210
Syntax
I Expressions:
I Values:
Value ::= BaseValue
| λ Var : Type.Expr
| λ TypeVar .Expr
155 / 210
Syntax
I Types:
Type ::= BaseType
| TypeVar
| (Type → Type)
| ∀TypeVar .Type
156 / 210
Syntax
I Types:
Type ::= BaseType
| TypeVar
| (Type → Type)
| ∀TypeVar .Type
I Typing contexts:
TypingContext ::= 0/
| TypingContext , Var : Type
| TypingContext , TypeVar
156 / 210
Semantics
Evaluation
I Reduce1 :
(λ x : τ.e e0 ) → e[e0 /x ]
157 / 210
Semantics
Evaluation
I Reduce1 :
(λ x : τ.e e0 ) → e[e0 /x ]
I Reduce2 :
λ X .e[τ] → e[τ/X ]
157 / 210
Semantics
Evaluation
I Reduce1 :
(λ x : τ.e e0 ) → e[e0 /x ]
I Reduce2 :
λ X .e[τ] → e[τ/X ]
I Eval 1 :
e → e0
(e e00 ) → (e0 e00 )
157 / 210
Semantics
Evaluation
I Reduce1 :
(λ x : τ.e e0 ) → e[e0 /x ]
I Reduce2 :
λ X .e[τ] → e[τ/X ]
I Eval 1 :
e → e0
(e e00 ) → (e0 e00 )
I Eval 2 :
e → e0
e[τ] → e0 [τ]
157 / 210
Semantics
Typing
I TBaseValue:
v ∈ τb
Γ ` v : τb
158 / 210
Semantics
Typing
I TBaseValue:
v ∈ τb
Γ ` v : τb
I TVar :
x :τ ∈Γ
Γ ` x :τ
158 / 210
Semantics
Typing
I TBaseValue:
v ∈ τb
Γ ` v : τb
I TVar :
x :τ ∈Γ
Γ ` x :τ
I TAbs1 :
Γ, x : τ ` e : τ 0
Γ ` λ x : τ.e : (τ → τ 0 )
158 / 210
Semantics
Typing
I TBaseValue:
v ∈ τb
Γ ` v : τb
I TVar :
x :τ ∈Γ
Γ ` x :τ
I TAbs1 :
Γ, x : τ ` e : τ 0
Γ ` λ x : τ.e : (τ → τ 0 )
I TApp1 :
Γ ` e : (τ 0 → τ) Γ ` e0 : τ 0
Γ ` (e e 0 ) : τ
158 / 210
Semantics
Typing
159 / 210
Semantics
Typing
I TApp2 :
Γ ` e : ∀X .τ
Γ ` e[τ 0 ] : τ[τ 0 /X ]
159 / 210
Semantics
Substitution and free variables
160 / 210
Semantics
Substitution and free variables
I Expr [Type/TypeVar ]
160 / 210
Semantics
Substitution and free variables
I Expr [Type/TypeVar ]
I Type[Type/TypeVar ]
160 / 210
Semantics
Substitution and free variables
I Expr [Type/TypeVar ]
I Type[Type/TypeVar ]
160 / 210
Semantics
Substitution and free variables
I Expr [Type/TypeVar ]
I Type[Type/TypeVar ]
160 / 210
Typing example
161 / 210
Typing example
Monomorphic function
with polymorphic argument and result!
161 / 210
Typing example
Monomorphic function
with polymorphic argument and result!
Blackboard!
161 / 210
Examples of polymorphic expressions
double ≡
162 / 210
Examples of polymorphic expressions
162 / 210
Examples of polymorphic expressions
162 / 210
Examples of polymorphic expressions
quadruple ≡
162 / 210
Examples of polymorphic expressions
162 / 210
Examples of polymorphic expressions
162 / 210
Examples of polymorphic expressions
reflexive ≡
163 / 210
Examples of polymorphic expressions
163 / 210
Examples of polymorphic expressions
163 / 210
Examples of polymorphic expressions
Fix ≡
163 / 210
Examples of polymorphic expressions
163 / 210
Examples of polymorphic expressions
163 / 210
Contents
Introduction
Extending STLC
Type reconstruction
164 / 210
Motivation
165 / 210
Contents
Introduction
Extending STLC
Type reconstruction
166 / 210
Problem
I Polymorphic identity function, on objects of a type
built using 1-ary type constructors e.g., List:
167 / 210
Problem
I Polymorphic identity function, on objects of a type
built using 1-ary type constructors e.g., List:
167 / 210
Problem
I Polymorphic identity function, on objects of a type
built using 1-ary type constructors e.g., List:
167 / 210
Problem
I Polymorphic identity function, on objects of a type
built using 1-ary type constructors e.g., List:
167 / 210
Solution
168 / 210
Solution
168 / 210
Solution
168 / 210
Kinds
Notation
169 / 210
Kinds
Notation
169 / 210
Kinds
Notation
169 / 210
Kinds
Notation
169 / 210
Kinds
Examples
170 / 210
Kinds
Examples
I List
170 / 210
Kinds
Examples
I List :: (∗ ⇒ ∗)
170 / 210
Kinds
Examples
I List :: (∗ ⇒ ∗)
170 / 210
Levels of expressions
Levels of expressions
Expressions
Levels of expressions
0
Expressions
Levels of expressions
0 (0, True)
Expressions
Levels of expressions
Types
N
Types
(N ∗ Bool )
N
Types
(N ∗ Bool )
N (List N)
Types
(N ∗ Bool ) (N → N)
N (List N)
Types
(N ∗ Bool ) (N → N)
N (List N) ∀X :: ∗.(X → X )
Types
List
(N ∗ Bool ) (N → N)
N (List N) ∀X :: ∗.(X → X )
Types
List (_ ∗ _)
(N ∗ Bool ) (N → N)
N (List N) ∀X :: ∗.(X → X )
Types
List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))
N (List N) ∀X :: ∗.(X → X )
Types
Kinds
List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))
N (List N) ∀X :: ∗.(X → X )
Types
∗
Kinds
List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))
N (List N) ∀X :: ∗.(X → X )
Types
∗
(∗ ⇒ ∗)
Kinds
List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))
N (List N) ∀X :: ∗.(X → X )
Types
∗ (∗ ⇒ (∗ ⇒ ∗))
(∗ ⇒ ∗)
Kinds
List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))
N (List N) ∀X :: ∗.(X → X )
Types
((∗ ⇒ ∗) ⇒ (∗ ⇒ ∗))
∗ (∗ ⇒ (∗ ⇒ ∗))
(∗ ⇒ ∗)
Kinds
List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))
N (List N) ∀X :: ∗.(X → X )
Types
171 / 210
Type equivalence
172 / 210
Type equivalence
172 / 210
Syntax
I Expressions:
173 / 210
Syntax
I Expressions:
I Values:
Value ::= BaseValue
| λ Var : Type.Expr
| λ TypeVar :: Kind .Expr
173 / 210
Syntax
I Types:
Type ::= BaseType
| TypeVar
| (Type → Type)
| ∀TypeVar :: Kind .Type
| λ TypeVar :: Kind .Type
| (Type Type)
174 / 210
Syntax
I Types:
Type ::= BaseType
| TypeVar
| (Type → Type)
| ∀TypeVar :: Kind .Type
| λ TypeVar :: Kind .Type
| (Type Type)
I Typing contexts:
TypingContext ::= 0/
| TypingContext , Var : Type
| TypingContext , TypeVar :: Kind
174 / 210
Syntax
I Kinds:
Kind ::= ∗
| (Kind ⇒ Kind )
175 / 210
Semantics
Evaluation
I Reduce1 :
(λ x : τ.e e0 ) → e[e0 /x ]
176 / 210
Semantics
Evaluation
I Reduce1 :
(λ x : τ.e e0 ) → e[e0 /x ]
I Reduce2 :
λ X :: K .e[τ] → e[τ/X ]
176 / 210
Semantics
Evaluation
I Reduce1 :
(λ x : τ.e e0 ) → e[e0 /x ]
I Reduce2 :
λ X :: K .e[τ] → e[τ/X ]
I Eval 1 :
e → e0
(e e00 ) → (e0 e00 )
176 / 210
Semantics
Evaluation
I Reduce1 :
(λ x : τ.e e0 ) → e[e0 /x ]
I Reduce2 :
λ X :: K .e[τ] → e[τ/X ]
I Eval 1 :
e → e0
(e e00 ) → (e0 e00 )
I Eval 2 :
e → e0
e[τ] → e0 [τ]
176 / 210
Semantics
Typing
I TBaseValue:
v ∈ τb
Γ ` v : τb
177 / 210
Semantics
Typing
I TBaseValue:
v ∈ τb
Γ ` v : τb
I TVar :
x :τ ∈Γ
Γ ` x :τ
177 / 210
Semantics
Typing
I TBaseValue:
v ∈ τb
Γ ` v : τb
I TVar :
x :τ ∈Γ
Γ ` x :τ
I TAbs1 :
Γ, x : τ ` e : τ 0
Γ ` λ x .e : (τ → τ 0 )
177 / 210
Semantics
Typing
I TBaseValue:
v ∈ τb
Γ ` v : τb
I TVar :
x :τ ∈Γ
Γ ` x :τ
I TAbs1 :
Γ, x : τ ` e : τ 0
Γ ` λ x .e : (τ → τ 0 )
I TApp1 :
Γ ` e : (τ 0 → τ) Γ ` e0 : τ 0
Γ ` (e e 0 ) : τ
177 / 210
Semantics
Typing
I TAbs2 :
Γ, X :: K ` e : τ
Γ ` λ X :: K .e : ∀X :: K .τ
178 / 210
Semantics
Typing
I TAbs2 :
Γ, X :: K ` e : τ
Γ ` λ X :: K .e : ∀X :: K .τ
I TApp2 :
Γ ` e : ∀X :: K .τ Γ ` τ 0 :: K
Γ ` e[τ 0 ] : τ[τ 0 /X ]
178 / 210
Semantics
Kinding
I KBaseType:
Γ ` τb :: ∗
179 / 210
Semantics
Kinding
I KBaseType:
Γ ` τb :: ∗
I KTypeVar :
X :: K ∈ Γ
Γ ` X :: K
179 / 210
Semantics
Kinding
I KBaseType:
Γ ` τb :: ∗
I KTypeVar :
X :: K ∈ Γ
Γ ` X :: K
I KTypeAbs:
Γ, X :: K ` τ :: K 0
Γ ` λ X :: K .τ :: (K ⇒ K 0 )
179 / 210
Semantics
Kinding
I KBaseType:
Γ ` τb :: ∗
I KTypeVar :
X :: K ∈ Γ
Γ ` X :: K
I KTypeAbs:
Γ, X :: K ` τ :: K 0
Γ ` λ X :: K .τ :: (K ⇒ K 0 )
I KTypeApp:
Γ ` τ :: (K 0 ⇒ K ) Γ ` τ 0 :: K 0
Γ ` (τ τ 0 ) :: K
179 / 210
Semantics
Kinding
I KAbs1 :
Γ ` τ :: ∗ Γ ` τ 0 :: ∗
Γ ` (τ → τ 0 ) :: ∗
180 / 210
Semantics
Kinding
I KAbs1 :
Γ ` τ :: ∗ Γ ` τ 0 :: ∗
Γ ` (τ → τ 0 ) :: ∗
I KAbs2 :
Γ, X :: K ` τ :: ∗
Γ ` ∀X :: K .τ :: ∗
180 / 210
Semantics
Type equivalence
I EqReflexivity :
τ ≡τ
181 / 210
Semantics
Type equivalence
I EqReflexivity :
τ ≡τ
I EqSymmetry :
τ ≡ τ0
τ0 ≡ τ
181 / 210
Semantics
Type equivalence
I EqReflexivity :
τ ≡τ
I EqSymmetry :
τ ≡ τ0
τ0 ≡ τ
I EqTransitivity :
τ ≡ τ0 τ 0 ≡ τ 00
τ ≡ τ 00
181 / 210
Semantics
Type equivalence
I EqReflexivity :
τ ≡τ
I EqSymmetry :
τ ≡ τ0
τ0 ≡ τ
I EqTransitivity :
τ ≡ τ0 τ 0 ≡ τ 00
τ ≡ τ 00
I EqTypeReduce:
(λ X :: K .τ τ 0 ) ≡ τ[τ 0 /X ]
181 / 210
Semantics
Type equivalence
I EqTypeAbs:
τ ≡ τ0
λ X :: K .τ ≡ λ X :: K .τ 0
182 / 210
Semantics
Type equivalence
I EqTypeAbs:
τ ≡ τ0
λ X :: K .τ ≡ λ X :: K .τ 0
I EqTypeApp:
τ ≡ τ0 σ ≡ σ0
(τ σ ) ≡ (τ 0 σ 0 )
182 / 210
Semantics
Type equivalence
I EqTypeAbs:
τ ≡ τ0
λ X :: K .τ ≡ λ X :: K .τ 0
I EqTypeApp:
τ ≡ τ0 σ ≡ σ0
(τ σ ) ≡ (τ 0 σ 0 )
I EqAbs1 :
τ ≡ τ0 σ ≡ σ0
(τ → σ ) ≡ (τ 0 → σ 0 )
182 / 210
Semantics
Type equivalence
I EqTypeAbs:
τ ≡ τ0
λ X :: K .τ ≡ λ X :: K .τ 0
I EqTypeApp:
τ ≡ τ0 σ ≡ σ0
(τ σ ) ≡ (τ 0 σ 0 )
I EqAbs1 :
τ ≡ τ0 σ ≡ σ0
(τ → σ ) ≡ (τ 0 → σ 0 )
I EqAbs2 :
τ ≡ τ0
∀X :: K .τ ≡ ∀X :: K .τ 0
182 / 210
Semantics
Type equivalence
I TypeEquivalence:
Γ ` e:τ τ ≡ τ0
Γ ` e : τ0
183 / 210
Kinding example
184 / 210
Kinding example
Blackboard!
184 / 210
Part V
185 / 210
Contents
Constructive paradigm
186 / 210
Contents
Constructive paradigm
187 / 210
Classical logic
I Example: prove ∃x .P (x )
188 / 210
Classical logic
I Example: prove ∃x .P (x )
188 / 210
Classical logic
I Example: prove ∃x .P (x )
I Assumption: ∃x .P (x ) ∨ ¬∃x .P (x )
(law of excluded middle)
188 / 210
Classical logic
I Example: prove ∃x .P (x )
I Assumption: ∃x .P (x ) ∨ ¬∃x .P (x )
(law of excluded middle)
188 / 210
Constructive logic
189 / 210
Constructive logic
189 / 210
Constructive logic
189 / 210
Constructive logic
189 / 210
Constructive type theory
190 / 210
Constructive type theory
I Correspondences:
190 / 210
Constructive type theory
I Correspondences:
I sentence ↔ type
190 / 210
Constructive type theory
I Correspondences:
I sentence ↔ type
I logical connective ↔ type constructor
190 / 210
Constructive type theory
I Correspondences:
I sentence ↔ type
I logical connective ↔ type constructor
I proof ↔ function with that type
190 / 210
Constructive type theory
I Correspondences:
I sentence ↔ type
I logical connective ↔ type constructor
I proof ↔ function with that type
190 / 210
The Curry-Howard isomorphism
Sentence Type
Proof Value
inference synthesis
Proof Value
191 / 210
Contents
Constructive paradigm
192 / 210
Two views
a:A
193 / 210
Definitional rules
194 / 210
Definitional rules
194 / 210
Definitional rules
194 / 210
Definitional rules
194 / 210
Other logic-type correspondences
195 / 210
Other logic-type correspondences
195 / 210
Other logic-type correspondences
195 / 210
Other logic-type correspondences
195 / 210
Logical conjunction / product type constructor I
196 / 210
Logical conjunction / product type constructor II
p : A∧B p : A∧B
fst p : A snd p : B
I Computation rules:
fst (a, b) → a
snd (a, b) → b
197 / 210
Logical implication / function type constructor I
I Formation rule (⇒ F ):
A is a sentence/ type B is a sentence/ type
A ⇒ B is a sentence/ type
I Introduction rule (⇒ I)
(square brackets = discharged assumption):
[x : A]
..
.
b:B
λ x : A.b : A ⇒ B
198 / 210
Logical implication / function type constructor II
a:A f :A⇒B
(f a) : B
I Computation rule:
(λ x : A.b a) → b[a/x ]
199 / 210
Logical disjunction / sum type constructor I
a:A b:B
inl a : A ∨ B inr b : A ∨ B
200 / 210
Logical disjunction / sum type constructor II
I Computation rules:
cases (inl a) f g → f a
cases (inr b) f g → g b
201 / 210
Absurd sentence / empty type I
⊥ is a sentence/ type
202 / 210
Absurd sentence / empty type II
203 / 210
Logical negation and equivalence
I Logical negation:
¬A ≡ A ⇒ ⊥
I Logical equivalence:
A ⇔ B ≡ (A ⇒ B ) ∧ (B ⇒ A)
204 / 210
Example proofs
I A⇒A
205 / 210
Example proofs
I A⇒A
I A ⇒ ¬¬A (converse?)
205 / 210
Example proofs
I A⇒A
I A ⇒ ¬¬A (converse?)
I ((A ∧ B ) ⇒ C ) ⇒ A ⇒ B ⇒ C
205 / 210
Example proofs
I A⇒A
I A ⇒ ¬¬A (converse?)
I ((A ∧ B ) ⇒ C ) ⇒ A ⇒ B ⇒ C
I (A ⇒ B ) ⇒ (B ⇒ C ) ⇒ (A ⇒ C )
205 / 210
Example proofs
I A⇒A
I A ⇒ ¬¬A (converse?)
I ((A ∧ B ) ⇒ C ) ⇒ A ⇒ B ⇒ C
I (A ⇒ B ) ⇒ (B ⇒ C ) ⇒ (A ⇒ C )
I (A ⇒ B ) ⇒ (¬B ⇒ ¬A)
205 / 210
Example proofs
I A⇒A
I A ⇒ ¬¬A (converse?)
I ((A ∧ B ) ⇒ C ) ⇒ A ⇒ B ⇒ C
I (A ⇒ B ) ⇒ (B ⇒ C ) ⇒ (A ⇒ C )
I (A ⇒ B ) ⇒ (¬B ⇒ ¬A)
I (A ∨ B ) ⇒ ¬(¬A ∧ ¬B )
205 / 210
Universal quantification / generalized function
type constructor I
I Formation rule (∀F )
(square brackets = discharged assumption):
[x : A]
..
.
A is a sentence/ type B is a sentence/ type
(∀x : A).B is a sentence/ type
I Computation rule:
207 / 210
Existential quantification / generalized product
type constructor I
I Formation rule (∃F )
(square brackets = discharged assumption):
[x : A]
..
.
A is a sentence/ type B is a sentence/ type
(∃x : A).B is a sentence/ type
208 / 210
Existential quantification / generalized product
type constructor II
I Elimination rules (∃E1,2 ):
I Computation rules:
Fst (a, b) → a
Snd (a, b) → b
209 / 210
Example proofs
210 / 210
Example proofs
210 / 210
Example proofs
210 / 210