You are on page 1of 696

Type Systems

and Functional Programming

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

I Study the characteristics of functional programming,


such as lazy evaluation and type systems of different
strengths

I Learn advanced mechanisms of the Haskell


language, which are impossible or difficult to simulate
in other languages

I Apply this apparatus to model practical problems

6 / 210
Course objectives

I Study the characteristics of functional programming,


such as lazy evaluation and type systems of different
strengths

I Learn advanced mechanisms of the Haskell


language, which are impossible or difficult to simulate
in other languages

I Apply this apparatus to model practical problems, e.g.


program synthesis, lazy search, probability spaces

6 / 210
Main lab outcome

An evaluator for a functional language,


equipped with a type synthesizer

7 / 210
Contents

Objectives

Functional programming

8 / 210
Functional programming features

I Mathematical functions, as value transformers

9 / 210
Functional programming features

I Mathematical functions, as value transformers


I Functions as first-class values

9 / 210
Functional programming features

I Mathematical functions, as value transformers


I Functions as first-class values
I No side effects or state

9 / 210
Functional flow

in1 f1

f3

in2 f2 f5 out

in3 f4

10 / 210
Stateless computation

Output dependent on input exlcusively:

t0

11 / 210
Stateless computation

Output dependent on input exlcusively:

x f y

t1

11 / 210
Stateless computation

Output dependent on input exlcusively:

x f y

t2

11 / 210
Stateful computation

Output dependent on input and time:

t0

12 / 210
Stateful computation

Output dependent on input and time:

x f y

t1

12 / 210
Stateful computation

Output dependent on input and time:

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

in2 f2 state f5 out

in3 f4

13 / 210
Functional programming features

I Mathematical functions, as value transformers


I Functions as first-class values
I No side effects or state

14 / 210
Functional programming features

I Mathematical functions, as value transformers


I Functions as first-class values
I No side effects or state
I Immutability

14 / 210
Functional programming features

I Mathematical functions, as value transformers


I Functions as first-class values
I No side effects or state
I Immutability
I Referential transparency

14 / 210
Functional programming features

I Mathematical functions, as value transformers


I Functions as first-class values
I No side effects or state
I Immutability
I Referential transparency
I Recursion

14 / 210
Functional programming features

I Mathematical functions, as value transformers


I Functions as first-class values
I No side effects or state
I Immutability
I Referential transparency
I Recursion
I Higher-order functions

14 / 210
Functional programming features

I Mathematical functions, as value transformers


I Functions as first-class values
I No side effects or state
I Immutability
I Referential transparency
I Recursion
I Higher-order functions
I Lazy evaluation

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

Untyped Lambda Calculus

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

I Model of computation — Alonzo Church, 1932

19 / 210
Untyped lambda calculus

I Model of computation — Alonzo Church, 1932

I Equivalent to the Turing machine (see the


Church-Turing thesis)

19 / 210
Untyped lambda calculus

I Model of computation — Alonzo Church, 1932

I Equivalent to the Turing machine (see the


Church-Turing thesis)

I Main building block: the function

19 / 210
Untyped lambda calculus

I Model of computation — Alonzo Church, 1932

I Equivalent to the Turing machine (see the


Church-Turing thesis)

I Main building block: the function

I Computation: evaluation of function applications,


through textual substitution

19 / 210
Untyped lambda calculus

I Model of computation — Alonzo Church, 1932

I Equivalent to the Turing machine (see the


Church-Turing thesis)

I Main building block: the function

I Computation: evaluation of function applications,


through textual substitution

I Evaluate = obtain a value (a function)!

19 / 210
Untyped lambda calculus

I Model of computation — Alonzo Church, 1932

I Equivalent to the Turing machine (see the


Church-Turing thesis)

I Main building block: the function

I Computation: evaluation of function applications,


through textual substitution

I Evaluate = obtain a value (a function)!

I No side effects or state

19 / 210
Applications

I Theoretical basis of numerous languages:


I LISP I ML I Clojure
I Scheme I F# I Scala
I Haskell I Clean I Erlang

20 / 210
Applications

I Theoretical basis of numerous languages:


I LISP I ML I Clojure
I Scheme I F# I Scala
I Haskell I Clean I Erlang

I Formal program verification, due to its simple


execution model

20 / 210
Contents

Introduction

Lambda expressions

Reduction

Normal forms

Evaluation order

21 / 210
λ -expressions
Definition

Definition 4.1 (λ -expression).


I Variable: a variable x is a λ -expression

22 / 210
λ -expressions
Definition

Definition 4.1 (λ -expression).


I Variable: a variable x is a λ -expression
I Function: if x is a variable and E is a λ -expression,
then λ x .E is a λ -expression, which stands for an
anonymous, unary function, with the
formal parameter x and the body E

22 / 210
λ -expressions
Definition

Definition 4.1 (λ -expression).


I Variable: a variable x is a λ -expression
I Function: if x is a variable and E is a λ -expression,
then λ x .E is a λ -expression, which stands for an
anonymous, unary function, with the
formal parameter x and the body E
I Application: if E and A are λ -expressions, then (E A)
is a λ -expression, which stands for the application of
the expression E onto the actual argument A.

22 / 210
λ -expressions
Examples

Example 4.2 (λ -expressions).


I x → variable x

23 / 210
λ -expressions
Examples

Example 4.2 (λ -expressions).


I x → variable x
I λ x .x : the identity function

23 / 210
λ -expressions
Examples

Example 4.2 (λ -expressions).


I x → variable x
I λ x .x : the identity function

I λ x .λ y .x : a function with another function as body!

23 / 210
λ -expressions
Examples

Example 4.2 (λ -expressions).


I x → variable x
I λ x .x : the identity function

I λ x .λ y .x : a function with another function as body!

I (λ x .x y ) : the application of the identity function onto


the actual argument y

23 / 210
λ -expressions
Examples

Example 4.2 (λ -expressions).


I x → variable x
I λ x .x : the identity function

I λ x .λ y .x : a function with another function as body!

I (λ x .x y ) : the application of the identity function onto


the actual argument y
I (λ x .(x x ) λ x .x )

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

Definition 4.3 (Bound occurrence).


An occurrence xn of a variable x is bound in the
expression E iff:

25 / 210
Variable occurrences
Definitions

Definition 4.3 (Bound occurrence).


An occurrence xn of a variable x is bound in the
expression E iff:
I E = λ x .F or

25 / 210
Variable occurrences
Definitions

Definition 4.3 (Bound occurrence).


An occurrence xn of a variable x is bound in the
expression E iff:
I E = λ x .F or

I E = . . . λ xn .F . . . or

25 / 210
Variable occurrences
Definitions

Definition 4.3 (Bound occurrence).


An occurrence xn of a variable x is bound in the
expression E iff:
I E = λ x .F or

I E = . . . λ xn .F . . . or

I E = . . . λ x .F . . . and xn appears in F .

25 / 210
Variable occurrences
Definitions

Definition 4.3 (Bound occurrence).


An occurrence xn of a variable x is bound in the
expression E iff:
I E = λ x .F or

I E = . . . λ xn .F . . . or

I E = . . . λ x .F . . . and xn appears in F .

Definition 4.4 (Free occurrence).


A variable occurrence is free in an expression iff it is not
bound in that expression.

25 / 210
Variable occurrences
Definitions

Definition 4.3 (Bound occurrence).


An occurrence xn of a variable x is bound in the
expression E iff:
I E = λ x .F or

I E = . . . λ xn .F . . . or

I E = . . . λ x .F . . . and xn appears in F .

Definition 4.4 (Free occurrence).


A variable occurrence is free in an expression iff it is not
bound in that expression.

Bound/ free occurrence w.r.t. a given expression!

25 / 210
Variable occurrences
Examples

Example 4.5 (Bound and free variables).


In the expression E = (λ x .x x ), we emphasize the
occurrences of x:

E = (λ x1 . x2 x3 ).
|{z}
F

26 / 210
Variable occurrences
Examples

Example 4.5 (Bound and free variables).


In the expression E = (λ x .x x ), we emphasize the
occurrences of x:

E = (λ x1 . x2 x3 ).
|{z}
F

I x1 , x2 bound in E

26 / 210
Variable occurrences
Examples

Example 4.5 (Bound and free variables).


In the expression E = (λ x .x x ), we emphasize the
occurrences of x:

E = (λ x1 . x2 x3 ).
|{z}
F

I x1 , x2 bound in E
I x3 free in E

26 / 210
Variable occurrences
Examples

Example 4.5 (Bound and free variables).


In the expression E = (λ x .x x ), we emphasize the
occurrences of x:

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

Definition 4.7 (Bound variable).


A variable is bound in an expression iff all its occurrences
are bound in that expression.

28 / 210
Variables
Definitions

Definition 4.7 (Bound variable).


A variable is bound in an expression iff all its occurrences
are bound in that expression.

Definition 4.8 (Free variable).


A variable is free in an expression iff it is not bound in that
expression i.e., iff at least one of its occurrences is free in
that expression.

28 / 210
Variables
Definitions

Definition 4.7 (Bound variable).


A variable is bound in an expression iff all its occurrences
are bound in that expression.

Definition 4.8 (Free variable).


A variable is free in an expression iff it is not bound in that
expression i.e., iff at least one of its occurrences is free in
that expression.

Bound/ free variable w.r.t. a given expression!

28 / 210
Variable occurrences
Examples

Example 4.5 (Bound and free variables).


In the expression E = (λ x .x x ), we emphasize the
occurrences of x:

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

Example 4.5 (Bound and free variables).


In the expression E = (λ x .x x ), we emphasize the
occurrences of x:

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

Definition 5.1 (β -reduction).


The evaluation of the application (λ x .E A), by substituting
every free occurrence of the formal argument, x, in the
function body, E, with the actual argument, A:
(λ x .E A) →β E[A/x ] .

34 / 210
β -reduction
Definitions

Definition 5.1 (β -reduction).


The evaluation of the application (λ x .E A), by substituting
every free occurrence of the formal argument, x, in the
function body, E, with the actual argument, A:
(λ x .E A) →β E[A/x ] .

Definition 5.2 (β -redex).


The application (λ x .E A).

34 / 210
β -reduction
Examples

Example 5.3 (β -reduction).


I (λ x .x y )

35 / 210
β -reduction
Examples

Example 5.3 (β -reduction).


I (λ x .x y ) →β x [y /x ]

35 / 210
β -reduction
Examples

Example 5.3 (β -reduction).


I (λ x .x y ) →β x [y /x ] → y
I (λ x .λ x .x y )

35 / 210
β -reduction
Examples

Example 5.3 (β -reduction).


I (λ x .x y ) →β x [y /x ] → y
I (λ x .λ x .x y ) →β λ x .x[y /x ]

35 / 210
β -reduction
Examples

Example 5.3 (β -reduction).


I (λ x .x y ) →β x [y /x ] → y
I (λ x .λ x .x y ) →β λ x .x[y /x ] → λ x .x
I (λ x .λ y .x y )

35 / 210
β -reduction
Examples

Example 5.3 (β -reduction).


I (λ x .x y ) →β x [y /x ] → y
I (λ x .λ x .x y ) →β λ x .x[y /x ] → λ x .x
I (λ x .λ y .x y ) →β λ y .x [y /x ]

35 / 210
β -reduction
Examples

Example 5.3 (β -reduction).


I (λ x .x y ) →β x [y /x ] → y
I (λ x .λ x .x y ) →β λ x .x[y /x ] → λ x .x
I (λ x .λ y .x y ) →β λ y .x [y /x ] → λ y .y

35 / 210
β -reduction
Examples

Example 5.3 (β -reduction).


I (λ x .x y ) →β x [y /x ] → y
I (λ x .λ x .x y ) →β λ x .x[y /x ] → λ x .x
I (λ x .λ y .x y ) →β λ y .x [y /x ] → λ y .y

35 / 210
β -reduction
Examples

Example 5.3 (β -reduction).


I (λ x .x y ) →β x [y /x ] → y
I (λ x .λ x .x y ) →β λ x .x[y /x ] → λ x .x
I (λ x .λ y .x y ) →β λ y .x [y /x ] → λ y .y
Wrong! The free variable y becomes bound,
changing its meaning!

35 / 210
β -reduction
Collisions

I Problem: within the expression (λ x .E A):

36 / 210
β -reduction
Collisions

I Problem: within the expression (λ x .E A):


I FV (A) ∩ BV (E ) = 0/ ⇒ correct reduction always

36 / 210
β -reduction
Collisions

I Problem: within the expression (λ x .E A):


I FV (A) ∩ BV (E ) = 0/ ⇒ correct reduction always
I FV (A) ∩ BV (E ) 6= 0/ ⇒ potentially wrong reduction

36 / 210
β -reduction
Collisions

I Problem: within the expression (λ x .E A):


I FV (A) ∩ BV (E ) = 0/ ⇒ correct reduction always
I FV (A) ∩ BV (E ) 6= 0/ ⇒ potentially wrong reduction

I Solution: rename the bound variables in E,


that are free in A

36 / 210
β -reduction
Collisions

I Problem: within the expression (λ x .E A):


I FV (A) ∩ BV (E ) = 0/ ⇒ correct reduction always
I FV (A) ∩ BV (E ) 6= 0/ ⇒ potentially wrong reduction

I Solution: rename the bound variables in E,


that are free in A

Example 5.4 (Bound variable renaming).


(λ x .λ y .x y )

36 / 210
β -reduction
Collisions

I Problem: within the expression (λ x .E A):


I FV (A) ∩ BV (E ) = 0/ ⇒ correct reduction always
I FV (A) ∩ BV (E ) 6= 0/ ⇒ potentially wrong reduction

I Solution: rename the bound variables in E,


that are free in A

Example 5.4 (Bound variable renaming).


(λ x .λ y .x y ) → (λ x .λ z .x y )

36 / 210
β -reduction
Collisions

I Problem: within the expression (λ x .E A):


I FV (A) ∩ BV (E ) = 0/ ⇒ correct reduction always
I FV (A) ∩ BV (E ) 6= 0/ ⇒ potentially wrong reduction

I Solution: rename the bound variables in E,


that are free in A

Example 5.4 (Bound variable renaming).


(λ x .λ y .x y ) → (λ x .λ z .x y ) →β λ z .x [y /x ]

36 / 210
β -reduction
Collisions

I Problem: within the expression (λ x .E A):


I FV (A) ∩ BV (E ) = 0/ ⇒ correct reduction always
I FV (A) ∩ BV (E ) 6= 0/ ⇒ potentially wrong reduction

I Solution: rename the bound variables in E,


that are free in A

Example 5.4 (Bound variable renaming).


(λ x .λ y .x y ) → (λ x .λ z .x y ) →β λ z .x [y /x ] → λ z .y

36 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y →α λ y .y[y /x ]

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y →α λ y .y[y /x ] → λ y .y

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y →α λ y .y[y /x ] → λ y .y

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y →α λ y .y[y /x ] → λ y .y : Wrong!
I λ x .λ y .x

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y →α λ y .y[y /x ] → λ y .y : Wrong!
I λ x .λ y .x →α λ y .λ y .x[y /x ]

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y →α λ y .y[y /x ] → λ y .y : Wrong!
I λ x .λ y .x →α λ y .λ y .x[y /x ] → λ y .λ y .y

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y →α λ y .y[y /x ] → λ y .y : Wrong!
I λ x .λ y .x →α λ y .λ y .x[y /x ] → λ y .λ y .y

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y →α λ y .y[y /x ] → λ y .y : Wrong!
I λ x .λ y .x →α λ y .λ y .x[y /x ] → λ y .λ y .y : Wrong!

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y →α λ y .y[y /x ] → λ y .y : Wrong!
I λ x .λ y .x →α λ y .λ y .x[y /x ] → λ y .λ y .y : Wrong!

Conditions:

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y →α λ y .y[y /x ] → λ y .y : Wrong!
I λ x .λ y .x →α λ y .λ y .x[y /x ] → λ y .λ y .y : Wrong!

Conditions:
I y is not free in E

37 / 210
α -conversion
Definition

Definition 5.5 (α -conversion).


Systematic relabeling of bound variables in a function:
λ x .E →α λ y .E[y /x ] . Two conditions must be met.

Example 5.6 (α -conversion).


I λ x .y →α λ y .y[y /x ] → λ y .y : Wrong!
I λ x .λ y .x →α λ y .λ y .x[y /x ] → λ y .λ y .y : Wrong!

Conditions:
I y is not free in E
I a free occurrence in E stays free in E[y /x ]

37 / 210
α -conversion
Examples

Example 5.7 (α -conversion).


I λ x .(x y ) →α λ z .(z y )

38 / 210
α -conversion
Examples

Example 5.7 (α -conversion).


I λ x .(x y ) →α λ z .(z y ) : Correct!

I λ x .λ x .(x y ) →α λ y .λ x .(x y )

38 / 210
α -conversion
Examples

Example 5.7 (α -conversion).


I λ x .(x y ) →α λ z .(z y ) : Correct!

I λ x .λ x .(x y ) →α λ y .λ x .(x y ) : Wrong!


y is free in λ x .(x y ).
I λ x .λ y .(y x ) →α λ y .λ y .(y y )

38 / 210
α -conversion
Examples

Example 5.7 (α -conversion).


I λ x .(x y ) →α λ z .(z y ) : Correct!

I λ x .λ x .(x y ) →α λ y .λ x .(x y ) : Wrong!


y is free in λ x .(x y ).
I λ x .λ y .(y x ) →α λ y .λ y .(y y ) : Wrong!
The free occurrence of x in λ y .(y x ) becomes bound,
after substitution, in λ y .(y y ).
I λ x .λ y .(y y ) →α λ y .λ y .(y y )

38 / 210
α -conversion
Examples

Example 5.7 (α -conversion).


I λ x .(x y ) →α λ z .(z y ) : Correct!

I λ x .λ x .(x y ) →α λ y .λ x .(x y ) : Wrong!


y is free in λ x .(x y ).
I λ x .λ y .(y x ) →α λ y .λ y .(y y ) : Wrong!
The free occurrence of x in λ y .(y x ) becomes bound,
after substitution, in λ y .(y y ).
I λ x .λ y .(y y ) →α λ y .λ y .(y y ) : Correct!

38 / 210
Reduction
Definitions

Definition 5.8 (Reduction step).


A sequence made of a possible α -conversion, followed by
a β -reduction, such that the second produces no
collisions: E1 → E2 ≡ E1 →α E3 →β E2 .

39 / 210
Reduction
Definitions

Definition 5.8 (Reduction step).


A sequence made of a possible α -conversion, followed by
a β -reduction, such that the second produces no
collisions: E1 → E2 ≡ E1 →α E3 →β E2 .

Definition 5.9 (Reduction sequence).


A string of zero or more reduction steps: E1 →∗ E2 . It is an
element of the reflexive transitive closure of relation →.

39 / 210
Reduction
Examples

Example 5.10 (Reduction).


I ((λ x .λ y .(y x ) y ) λ x .x )

40 / 210
Reduction
Examples

Example 5.10 (Reduction).


I ((λ x .λ y .(y x ) y ) λ x .x )
→ (λ z .(z y ) λ x .x )

40 / 210
Reduction
Examples

Example 5.10 (Reduction).


I ((λ x .λ y .(y x ) y ) λ x .x )
→ (λ z .(z y ) λ x .x )
→ (λ x .x y )

40 / 210
Reduction
Examples

Example 5.10 (Reduction).


I ((λ x .λ y .(y x ) y ) λ x .x )
→ (λ z .(z y ) λ x .x )
→ (λ x .x y )
→y

40 / 210
Reduction
Examples

Example 5.10 (Reduction).


I ((λ x .λ y .(y x ) y ) λ x .x )
→ (λ z .(z y ) λ x .x )
→ (λ x .x y )
→y

I ((λ x .λ y .(y x ) y ) λ x .x ) →∗ y

40 / 210
Reduction
Properties

I Reduction step = reduction sequence:

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

1. When does the computation terminate?


Does it always?

43 / 210
Questions

1. When does the computation terminate?


Does it always?

2. Does the answer depend on the reduction sequence?

43 / 210
Questions

1. When does the computation terminate?


Does it always?

2. Does the answer depend on the reduction sequence?

3. If the computation terminates for distinct reduction


sequences, do we always get the same result?

43 / 210
Questions

1. When does the computation terminate?


Does it always?

2. Does the answer depend on the reduction sequence?

3. If the computation terminates for distinct reduction


sequences, do we always get the same result?

4. If the result is unique, how do we safely obtain it?

43 / 210
Normal forms

Definition 6.1 (Normal form).


The form of an expression that cannot be reduced i.e.,
that contains no β -redexes.

44 / 210
Normal forms

Definition 6.1 (Normal form).


The form of an expression that cannot be reduced i.e.,
that contains no β -redexes.
Definition 6.2 (Functional normal form, FNF).
λ x .E, even if E contains β -redexes.

44 / 210
Normal forms

Definition 6.1 (Normal form).


The form of an expression that cannot be reduced i.e.,
that contains no β -redexes.
Definition 6.2 (Functional normal form, FNF).
λ x .E, even if E contains β -redexes.

Example 6.3 (Normal forms).


(λ x .λ y .(x y ) λ x .x ) →FNF λ y .(λ x .x y ) →NF λ y .y

44 / 210
Normal forms

Definition 6.1 (Normal form).


The form of an expression that cannot be reduced i.e.,
that contains no β -redexes.
Definition 6.2 (Functional normal form, FNF).
λ x .E, even if E contains β -redexes.

Example 6.3 (Normal forms).


(λ x .λ y .(x y ) λ x .x ) →FNF λ y .(λ x .x y ) →NF λ y .y

FNF is used in programming, where the function body


is evaluated only when the function is effectively applied.

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.

Definition 6.5 (Reducible expression).


An expression that has 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.

Definition 6.5 (Reducible expression).


An expression that has a terminating reduction sequence.

Ω is irreducible.

45 / 210
Questions

1. When does the computation terminate?


Does it always?

2. Does the answer depend on the reduction sequence?

3. If the computation terminates for distinct reduction


sequences, do we always get the same result?

4. If the result is unique, how do we safely obtain it?

46 / 210
Questions

1. When does the computation terminate?


Does it always?
I NO

2. Does the answer depend on the reduction sequence?

3. If the computation terminates for distinct reduction


sequences, do we always get the same result?

4. If the result is unique, how do we safely obtain it?

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

I E has a nonterminating reduction sequence, but still


has a normal form, y . E is reducible, Ω is not.

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

I E has a nonterminating reduction sequence, but still


has a normal form, y . E is reducible, Ω is not.
I The length of terminating reduction sequences
is unbounded.

47 / 210
Questions

1. When does the computation terminate?


Does it always?
I NO

2. Does the answer depend on the reduction sequence?

3. If the computation terminates for distinct reduction


sequences, do we always get the same result?

4. If the result is unique, how do we safely obtain it?

48 / 210
Questions

1. When does the computation terminate?


Does it always?
I NO

2. Does the answer depend on the reduction sequence?


I YES

3. If the computation terminates for distinct reduction


sequences, do we always get the same result?

4. If the result is unique, how do we safely obtain it?

48 / 210
Normal form uniqueness
Results

Theorem 6.7 (Church-Rosser / diamond).


If E →∗ E1 and E →∗ E2 , then there is an E3 such that
E1 →∗ E3 and E2 →∗ E3 .

∗ E1 ∗
E E3
∗ E2 ∗

49 / 210
Normal form uniqueness
Results

Theorem 6.7 (Church-Rosser / diamond).


If E →∗ E1 and E →∗ E2 , then there is an E3 such that
E1 →∗ E3 and E2 →∗ E3 .

∗ E1 ∗
E E3
∗ E2 ∗

Corollary 6.8 (Normal form uniqueness).


If an expression is reducible, its normal form is unique. It
corresponds to the value of that expression.

49 / 210
Normal form uniqueness
Examples

Example 6.9 (Normal form uniqueness).

(λ x .λ y .(x y ) (λ x .x y ))

50 / 210
Normal form uniqueness
Examples

Example 6.9 (Normal form uniqueness).

(λ x .λ y .(x y ) (λ x .x y ))

I → λ z .((λ x .x y ) z ) → λ z .(y z )

50 / 210
Normal form uniqueness
Examples

Example 6.9 (Normal form uniqueness).

(λ 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

Example 6.9 (Normal form uniqueness).

(λ x .λ y .(x y ) (λ x .x y ))

I → λ z .((λ x .x y ) z ) → λ z .(y z ) →α λ a.(y a)


I → (λ x .λ y .(x y ) y ) → λ w .(y w ) →α λ a.(y a)

50 / 210
Normal form uniqueness
Examples

Example 6.9 (Normal form uniqueness).

(λ x .λ y .(x y ) (λ x .x y ))

I → λ z .((λ x .x y ) z ) → λ z .(y z ) →α λ a.(y a)


I → (λ x .λ y .(x y ) y ) → λ w .(y w ) →α λ a.(y a)

I Normal form: class of expressions, equivalent under


systematic relabeling

50 / 210
Normal form uniqueness
Examples

Example 6.9 (Normal form uniqueness).

(λ x .λ y .(x y ) (λ x .x y ))

I → λ z .((λ x .x y ) z ) → λ z .(y z ) →α λ a.(y a)


I → (λ x .λ y .(x y ) y ) → λ w .(y w ) →α λ a.(y a)

I Normal form: class of expressions, equivalent under


systematic relabeling
I Value: distinguished member of this class

50 / 210
Structural equivalence

Definition 6.10 (Structural equivalence).


Two expressions are structurally equivalent iff they both
reduce to the same expression.

Example 6.11 (Structural equivalence).


λ z .((λ x .x y ) z ) and (λ x .λ y .(x y ) y ) in Example 6.9.

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

1. When does the computation terminate?


Does it always?
I NO

2. Does the answer depend on the reduction sequence?


I YES

3. If the computation terminates for distinct reduction


sequences, do we always get the same result?

4. If the result is unique, how do we safely obtain it?

53 / 210
Questions

1. When does the computation terminate?


Does it always?
I NO

2. Does the answer depend on the reduction sequence?


I YES

3. If the computation terminates for distinct reduction


sequences, do we always get the same result?
I YES

4. If the result is unique, how do we safely obtain it?

53 / 210
Reduction order
Definitions and examples

Definition 6.14 (Left-to-right reduction step).


The reduction of the outermost leftmost β -redex.
Example 6.15 (Left-to-right reduction).
((λ x .x λ x .y ) (λ x .(x x ) λ x .(x x ))) → (λ x .y Ω) → y

54 / 210
Reduction order
Definitions and examples

Definition 6.14 (Left-to-right reduction step).


The reduction of the outermost leftmost β -redex.
Example 6.15 (Left-to-right reduction).
((λ x .x λ x .y ) (λ x .(x x ) λ x .(x x ))) → (λ x .y Ω) → y

Definition 6.16 (Right-to-left reduction step).


The reduction of the innermost rightmost β -redex.
Example 6.17 (Right-to-left reduction).
((λ x .x λ x .y ) (λ x .(x x ) λ x .(x x ))) → (λ x .y Ω) → . . .

54 / 210
Reduction order
Which one is better?

Theorem 6.18 (Normalization).


If an expression is reducible, its left-to-right reduction
terminates.

The theorem does not guarantee the termination for any


expression, but only for reducible ones!

55 / 210
Questions

1. When does the computation terminate?


Does it always?
I NO

2. Does the answer depend on the reduction sequence?


I YES

3. If the computation terminates for distinct reduction


sequences, do we always get the same result?
I YES

4. If the result is unique, how do we safely obtain it?

56 / 210
Questions

1. When does the computation terminate?


Does it always?
I NO

2. Does the answer depend on the reduction sequence?


I YES

3. If the computation terminates for distinct reduction


sequences, do we always get the same result?
I YES

4. If the result is unique, how do we safely obtain it?


I Left-to-right reduction

56 / 210
Contents

Introduction

Lambda expressions

Reduction

Normal forms

Evaluation order

57 / 210
Evaluation order

Definition 7.1 (Applicative-order evaluation).


Corresponds to right-to-left reduction. Function
arguments are evaluated before the function is applied.

58 / 210
Evaluation order

Definition 7.1 (Applicative-order evaluation).


Corresponds to right-to-left reduction. Function
arguments are evaluated before the function is applied.
Definition 7.2 (Strict function).
A function that uses applicative-order evaluation.

58 / 210
Evaluation order

Definition 7.1 (Applicative-order evaluation).


Corresponds to right-to-left reduction. Function
arguments are evaluated before the function is applied.
Definition 7.2 (Strict function).
A function that uses applicative-order evaluation.
Definition 7.3 (Normal-order evaluation).
Corresponds to left-to-right reduction. Function
arguments are evaluated when needed.

58 / 210
Evaluation order

Definition 7.1 (Applicative-order evaluation).


Corresponds to right-to-left reduction. Function
arguments are evaluated before the function is applied.
Definition 7.2 (Strict function).
A function that uses applicative-order evaluation.
Definition 7.3 (Normal-order evaluation).
Corresponds to left-to-right reduction. Function
arguments are evaluated when needed.
Definition 7.4 (Non-strict function).
A function that uses normal-order evaluation.

58 / 210
In practice I

Applicative-order evaluation employed in most


programming languages, due to efficiency — one-time
evaluation of arguments: C, Java, Scheme, PHP, etc.

Example 7.5 (Applicative-order evaluation in


Scheme).
((λ (x) (+ x x)) (+ 2 3))
→ ((λ (x) (+ x x)) 5)
→ (+ 5 5)
→ 10

59 / 210
In practice II

Lazy evaluation (a kind of normal-order evaluation) in


Haskell: on-demand evaluation of arguments, allowing for
interesting constructions

Example 7.6 (Lazy evaluation in Haskell).


((\x -> x + x) (2 + 3))
→ (2 + 3) + (2 + 3)
→ 5 + 5
→ 10

Need for non-strict functions, even in applicative


languages: if, and, or, etc.

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

Abstract data types (ADTs)

Implementation

Recursion

Language specification

63 / 210
Contents

The λ0 language

Abstract data types (ADTs)

Implementation

Recursion

Language specification

64 / 210
Purpose

I Proving the expressive power of lambda calculus

65 / 210
Purpose

I Proving the expressive power of lambda calculus

I Hypothetical λ -machine

65 / 210
Purpose

I Proving the expressive power of lambda calculus

I Hypothetical λ -machine

I Machine code: λ -expressions — the λ0 language

65 / 210
Purpose

I Proving the expressive power of lambda calculus

I Hypothetical λ -machine

I Machine code: λ -expressions — the λ0 language

I Instead of
I bits
I bit operations,
we have

65 / 210
Purpose

I Proving the expressive power of lambda calculus

I Hypothetical λ -machine

I Machine code: λ -expressions — the λ0 language

I Instead of
I bits
I bit operations,
we have
I structured strings of symbols

65 / 210
Purpose

I Proving the expressive power of lambda calculus

I Hypothetical λ -machine

I Machine code: λ -expressions — the λ0 language

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

I Values represented as functions

66 / 210
λ0 features
I Instructions:
I λ -expressions
I top-level variable bindings: variable ≡def expression
e.g., true ≡def λ x .λ y .x

I Values represented as functions


I Expressions brought to the closed form,
prior to evaluation

66 / 210
λ0 features
I Instructions:
I λ -expressions
I top-level variable bindings: variable ≡def expression
e.g., true ≡def λ x .λ y .x

I Values represented as functions


I Expressions brought to the closed form,
prior to evaluation
I Normal-order evaluation

66 / 210
λ0 features
I Instructions:
I λ -expressions
I top-level variable bindings: variable ≡def expression
e.g., true ≡def λ x .λ y .x

I Values represented as functions


I Expressions brought to the closed form,
prior to evaluation
I Normal-order evaluation
I Functional normal form (see Definition 6.2)

66 / 210
λ0 features
I Instructions:
I λ -expressions
I top-level variable bindings: variable ≡def expression
e.g., true ≡def λ x .λ y .x

I Values represented as functions


I Expressions brought to the closed form,
prior to evaluation
I Normal-order evaluation
I Functional normal form (see Definition 6.2)
I No predefined types!

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

I Way of expressing the programmer’s intent

68 / 210
Purpose of types

I Way of expressing the programmer’s intent

I Documentation: which operators act


onto which objects

68 / 210
Purpose of types

I Way of expressing the programmer’s intent

I Documentation: which operators act


onto which objects

I Particular representation for values of different types:


1, “Hello”, #t, etc.

68 / 210
Purpose of types

I Way of expressing the programmer’s intent

I Documentation: which operators act


onto which objects

I Particular representation for values of different types:


1, “Hello”, #t, etc.

I Optimization of specific operations

68 / 210
Purpose of types

I Way of expressing the programmer’s intent

I Documentation: which operators act


onto which objects

I Particular representation for values of different types:


1, “Hello”, #t, etc.

I Optimization of specific operations

I Error prevention

68 / 210
Purpose of types

I Way of expressing the programmer’s intent

I Documentation: which operators act


onto which objects

I Particular representation for values of different types:


1, “Hello”, #t, etc.

I Optimization of specific operations

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

I Both values and operators represented by functions


— context-dependent meaning
number 3 → λ x .λ y .x ← operator car

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

I Both values and operators represented by functions


— context-dependent meaning
number 3 → λ x .λ y .x ← operator car

I Value applicable onto another value, as an operator!


x f y

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

I Both values and operators represented by functions


— context-dependent meaning
number 3 → λ x .λ y .x ← operator car

I Value applicable onto another value, as an operator!


x f y z

x0
69 / 210
No types
How is correctness affected?

I Inability of the λ machine to


I interpret the meaning of expressions
I ensure their correctness

70 / 210
No types
How is correctness affected?

I Inability of the λ machine to


I interpret the meaning of expressions
I ensure their correctness

I Every operator applicable onto every value

70 / 210
No types
How is correctness affected?

I Inability of the λ machine to


I interpret the meaning of expressions
I ensure their correctness

I Every operator applicable onto every value

I Both aspects above delegated to the programmer

70 / 210
No types
How is correctness affected?

I Inability of the λ machine to


I interpret the meaning of expressions
I ensure their correctness

I Every operator applicable onto every value

I Both aspects above delegated to the programmer

I Erroneus constructs accepted without warning,


but computation ended with

70 / 210
No types
How is correctness affected?

I Inability of the λ machine to


I interpret the meaning of expressions
I ensure their correctness

I Every operator applicable onto every value

I Both aspects above delegated to the programmer

I Erroneus constructs accepted without warning,


but computation ended with
I values with no meaning or

70 / 210
No types
How is correctness affected?

I Inability of the λ machine to


I interpret the meaning of expressions
I ensure their correctness

I Every operator applicable onto every value

I Both aspects above delegated to the programmer

I Erroneus constructs accepted without warning,


but computation ended with
I values with no meaning or
I expressions that are neither values, nor reducible
e.g., (x x )
70 / 210
No types
Consequences

I Enhanced representational flexibility

71 / 210
No types
Consequences

I Enhanced representational flexibility

I Useful when the uniform representation of objects,


as lists de symbols, is convenient

71 / 210
No types
Consequences

I Enhanced representational flexibility

I Useful when the uniform representation of objects,


as lists de symbols, is convenient

I Increased error-proneness

71 / 210
No types
Consequences

I Enhanced representational flexibility

I Useful when the uniform representation of objects,


as lists de symbols, is convenient

I Increased error-proneness

I Program instability

71 / 210
No types
Consequences

I Enhanced representational flexibility

I Useful when the uniform representation of objects,


as lists de symbols, is convenient

I Increased error-proneness

I Program instability

I Difficulty of verification and maintenance

71 / 210
So...

I How do we employ the λ0 language in everyday


programming?

72 / 210
So...

I How do we employ the λ0 language in everyday


programming?

I How do we represent usual values — numbers,


booleans, lists, etc. — and their corresponding
operators?

72 / 210
Contents

The λ0 language

Abstract data types (ADTs)

Implementation

Recursion

Language specification

73 / 210
Definition

Definition 9.1 (Abstract data type, ADT).


Mathematical model of a set of values and their
corresponding operations.

74 / 210
Definition

Definition 9.1 (Abstract data type, ADT).


Mathematical model of a set of values and their
corresponding operations.

Example 9.2 (ADTs).


Natural, Bool, List, Set, Stack , Tree, ... λ -expression!

74 / 210
Definition

Definition 9.1 (Abstract data type, ADT).


Mathematical model of a set of values and their
corresponding operations.

Example 9.2 (ADTs).


Natural, Bool, List, Set, Stack , Tree, ... λ -expression!

Components:
I base constructors: how are values built

I operators: what can be done with these values

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

I One axiom for each (operator, base constructor) pair

77 / 210
Providing axioms

I One axiom for each (operator, base constructor) pair

I More — useless

77 / 210
Providing axioms

I One axiom for each (operator, base constructor) pair

I More — useless

I Less — insufficient for completely specifying


the operators

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

I Proving ADT correctness


— structural induction

79 / 210
From ADTs to functional programming
Discussion

I Proving ADT correctness


— structural induction
I Proving properties of λ -expressions, seen as values
of an ADT with 3 base constructors!

79 / 210
From ADTs to functional programming
Discussion

I Proving ADT correctness


— structural induction
I Proving properties of λ -expressions, seen as values
of an ADT with 3 base constructors!
I Functional programming
— reflection of mathematical specifications

79 / 210
From ADTs to functional programming
Discussion

I Proving ADT correctness


— structural induction
I Proving properties of λ -expressions, seen as values
of an ADT with 3 base constructors!
I Functional programming
— reflection of mathematical specifications
I Recursion
— natural instrument, inherited from axioms

79 / 210
From ADTs to functional programming
Discussion

I Proving ADT correctness


— structural induction
I Proving properties of λ -expressions, seen as values
of an ADT with 3 base constructors!
I Functional programming
— reflection of mathematical specifications
I Recursion
— natural instrument, inherited from axioms
I Applying formal methods on the recursive code,
taking advantage of the lack of side effects

79 / 210
Contents

The λ0 language

Abstract data types (ADTs)

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 Intuition: selecting one of the two values, true or false

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 and ≡def λ xy .(x y F )


I (and T a) → (λ xy .(x y F ) T a) → (T a F ) → a
I (and F a) → (λ xy .(x y F ) F a) → (F a F ) → 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 λ xy .(x y F )


I (and T a) → (λ xy .(x y F ) T a) → (T a F ) → a
I (and F a) → (λ xy .(x y F ) F a) → (F a F ) → F

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 and ≡def λ xy .(x y F )


I (and T a) → (λ xy .(x y F ) T a) → (T a F ) → a
I (and F a) → (λ xy .(x y F ) F a) → (F a F ) → F

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 λ cte.(c t e) non-strict!


I (if T a b ) → (λ cte.(c t e) T a b ) → (T a b ) → a
I (if F a b ) → (λ cte.(c t e) F a b ) → (F a b ) → b
84 / 210
The Pair ADT
Specification

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 Intuition: a pair = a function that expects a selector, in


order to apply it onto its components

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 Intuition: a pair = a function that expects a selector, in


order to apply it onto its components

I pair ≡def λ xys.(s x y )


I (pair a b ) → (λ xys .(s x y ) a b ) → λ s .(s 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 Intuition: a pair = a function that expects a selector, in


order to apply it onto its components

I pair ≡def λ xys.(s x y )


I (pair a b ) → (λ xys .(s x y ) a b ) → λ s .(s a b )

I fst ≡def λ p.(p T )


I (fst (pair a b )) → (λ p .(p T ) λ s .(s a b )) →
(λ s .(s a b ) T ) → (T a b ) → a

I snd ≡def
I (snd (pair a b ))
→b

86 / 210
The Pair ADT
Implementation

I Intuition: a pair = a function that expects a selector, in


order to apply it onto its components

I pair ≡def λ xys.(s x y )


I (pair a b ) → (λ xys .(s x y ) a b ) → λ s .(s a b )

I fst ≡def λ p.(p T )


I (fst (pair a b )) → (λ p .(p T ) λ s .(s a b )) →
(λ s .(s a b ) T ) → (T a b ) → a

I snd ≡def λ p.(p F )


I (snd (pair a b )) → (λ p .(p F ) λ s .(s a b )) →
(λ s .(s a b ) F ) → (F a b ) → b

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 append ≡def . . . no closed form


λ AB .(if (null ? A) B (cons (car A) (append (cdr A) B )))
89 / 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))

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

I Intuition: a number = a list having the number value


as its length
I zero ≡def
I succ ≡def
I zero? ≡def
I pred ≡def
I add ≡def

91 / 210
The Natural ADT
Implementation

I Intuition: a number = a list having the number value


as its length
I zero ≡def null
I succ ≡def
I zero? ≡def
I pred ≡def
I add ≡def

91 / 210
The Natural ADT
Implementation

I Intuition: a number = a list having the number value


as its length
I zero ≡def null
I succ ≡def λ n.(cons null n)
I zero? ≡def
I pred ≡def
I add ≡def

91 / 210
The Natural ADT
Implementation

I Intuition: a number = a list having the number value


as its length
I zero ≡def null
I succ ≡def λ n.(cons null n)
I zero? ≡def null ?
I pred ≡def
I add ≡def

91 / 210
The Natural ADT
Implementation

I Intuition: a number = a list having the number value


as its length
I zero ≡def null
I succ ≡def λ n.(cons null n)
I zero? ≡def null ?
I pred ≡def cdr
I add ≡def

91 / 210
The Natural ADT
Implementation

I Intuition: a number = a list having the number value


as its length
I zero ≡def null
I succ ≡def λ n.(cons null n)
I zero? ≡def null ?
I pred ≡def cdr
I add ≡def append

91 / 210
Contents

The λ0 language

Abstract data types (ADTs)

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

I Infinitely many textual representations for the same


function

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

I Infinitely many textual representations for the same


function

I Then. . . what is a function?

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

I Infinitely many textual representations for the same


function

I Then. . . what is a function? A relation between inputs


and outputs, independent of any textual
representation e.g.,
id = {(0, 0), (1, 1), (2, 2), . . .}
93 / 210
Perspectives on recursion

I Textual: a function that refers itself,


using its name

94 / 210
Perspectives on recursion

I Textual: a function that refers itself,


using its name

I Constructivist: recursive functions as values of an


ADT, with specific ways of building them

94 / 210
Perspectives on recursion

I Textual: a function that refers itself,


using its name

I Constructivist: recursive functions as values of an


ADT, with specific ways of building them

I Semantic: the mathematical object designated


by a recursive function

94 / 210
Implementing length
Problem

I Length of a list:

length ≡def λ L.(if (null ? L) zero (succ (length (cdr L))))

95 / 210
Implementing length
Problem

I Length of a list:

length ≡def λ L.(if (null ? L) zero (succ (length (cdr L))))

I What do we replace the underlined area with,


to avoid textual recursion?

95 / 210
Implementing length
Problem

I Length of a list:

length ≡def λ L.(if (null ? L) zero (succ (length (cdr L))))

I What do we replace the underlined area with,


to avoid textual recursion?
I Rewrite the definition as a fixed-point equation

Length ≡def λ f L.(if (null ? L) zero (succ (f (cdr L))))


(Length length) → length

95 / 210
Implementing length
Problem

I Length of a list:

length ≡def λ L.(if (null ? L) zero (succ (length (cdr L))))

I What do we replace the underlined area with,


to avoid textual recursion?
I Rewrite the definition as a fixed-point equation

Length ≡def λ f L.(if (null ? L) zero (succ (f (cdr L))))


(Length length) → length

I How do we compute the fixed point? (see code


archive)

95 / 210
Contents

The λ0 language

Abstract data types (ADTs)

Implementation

Recursion

Language specification

96 / 210
Axiomatization benefits

I Disambiguation

I Proof of properties

I Implementation skeleton

97 / 210
Syntax
I Variable:

Var ::= any symbol distinct from λ , ., (, )

98 / 210
Syntax
I Variable:

Var ::= any symbol distinct from λ , ., (, )

I Expression:

Expr :: = Var
| λ Var .Expr
| (Expr Expr )

98 / 210
Syntax
I Variable:

Var ::= any symbol distinct from λ , ., (, )

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

I hλ y .ei[e0 /x ] = λ y .e[e0 /x ] , y 6= x ∧ y 6∈ FV (e0 )

I hλ y .ei[e0 /x ] = λ z .e[z /y ][e0 /x ] ,


y 6= x ∧ y ∈ FV (e0 ) ∧ z 6∈ FV (e) ∪ FV (e0 )

I (e0 e00 )[e/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 ∧ y 6∈ FV (e0 )

I hλ y .ei[e0 /x ] = λ z .e[z /y ][e0 /x ] ,


y 6= x ∧ y ∈ FV (e0 ) ∧ z 6∈ FV (e) ∪ FV (e0 )

I (e0 e00 )[e/x ] = (e[0e/x ] e[00e/x ] )

101 / 210
Semantics for normal-order evaluation
Free variables

I FV (x ) = {x }

I FV (λ x .e) = FV (e) \ {x }

I FV ((e0 e00 )) = FV (e0 ) ∪ FV (e00 )

102 / 210
Semantics for normal-order evaluation
Example

Example 12.1 (Evaluation rules).

((λ x .λ y .y a) b )

( )

103 / 210
Semantics for normal-order evaluation
Example

Example 12.1 (Evaluation rules).

((λ x .λ y .y a) b )

(λ x .λ y .y a) → λ y .y (Reduce)
( )

103 / 210
Semantics for normal-order evaluation
Example

Example 12.1 (Evaluation rules).

((λ 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

Example 12.1 (Evaluation rules).

((λ 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

Proposition 12.2 (Free and bound variables).

∀e ∈ Expr • BV (e) ∩ FV (e) = 0/

105 / 210
Formal proof

Proposition 12.2 (Free and bound variables).

∀e ∈ Expr • BV (e) ∩ FV (e) = 0/

Proof.
Structural induction, according to the different forms of
λ -expressions (see the lecture notes).

105 / 210
Summary

I Practical usage of the untyped lambda calculus,


as a programming language

I Formal specifications, for different evaluation


semantics

106 / 210
Part IV

Typed Lambda Calculus

107 / 210
Contents

Introduction

Simply Typed Lambda Calculus (STLC, System F1 )

Extending STLC

Polymorphic Lambda Calculus (PSTLC, System F)

Type reconstruction

Higher-Order Polymorphic Lambda Calculus (HPSTLC,


System Fω )

108 / 210
Contents

Introduction

Simply Typed Lambda Calculus (STLC, System F1 )

Extending STLC

Polymorphic Lambda Calculus (PSTLC, System F)

Type reconstruction

Higher-Order Polymorphic Lambda Calculus (HPSTLC,


System Fω )

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)

I No canonical representation for the values


of a given type e.g., both a tree and a set
having the same representation

110 / 210
Drawbacks of the absence of types
I Meaningless expressions e.g., (car 3)

I No canonical representation for the values


of a given type e.g., both a tree and a set
having the same representation

I Impossibility of translating certain expressions


into certain typed languages e.g., (x x ), Ω, Fix

110 / 210
Drawbacks of the absence of types
I Meaningless expressions e.g., (car 3)

I No canonical representation for the values


of a given type e.g., both a tree and a set
having the same representation

I Impossibility of translating certain expressions


into certain typed languages e.g., (x x ), Ω, Fix

I Potential irreducibility of expressions — inconsistent


representation of equivalent values

λ x .(Fix x ) → λ x .(x (Fix x )) → λ x .(x (x (Fix x ))) → . . .

110 / 210
Solution

I Restricted ways of constructing expressions,


depending on the types of their parts

111 / 210
Solution

I Restricted ways of constructing expressions,


depending on the types of their parts

I Sacrificed expressivity in change for soundness

111 / 210
Desired properties

Definition 13.1 (Progress).


A well-typed expression is either a value or is subject to at
least one reduction step.

112 / 210
Desired properties

Definition 13.1 (Progress).


A well-typed expression is either a value or is subject to at
least one reduction step.

Definition 13.2 (Preservation).


The result obtained by reducing a well-typed expression
is well-typed. Usually, the type is the same.

112 / 210
Desired properties

Definition 13.1 (Progress).


A well-typed expression is either a value or is subject to at
least one reduction step.

Definition 13.2 (Preservation).


The result obtained by reducing a well-typed expression
is well-typed. Usually, the type is the same.

Definition 13.3 (Strong normalization).


The evaluation of a well-typed expression terminates.

112 / 210
Contents

Introduction

Simply Typed Lambda Calculus (STLC, System F1 )

Extending STLC

Polymorphic Lambda Calculus (PSTLC, System F)

Type reconstruction

Higher-Order Polymorphic Lambda Calculus (HPSTLC,


System Fω )

113 / 210
Base and simple types

Definition 14.1 (Base type).


An atomic type e.g., numbers, booleans etc.

114 / 210
Base and simple types

Definition 14.1 (Base type).


An atomic type e.g., numbers, booleans etc.
Definition 14.2 (Simple type).
A type built from existing types e.g., σ → τ , where σ and τ
are types.

114 / 210
Base and simple types

Definition 14.1 (Base type).


An atomic type e.g., numbers, booleans etc.
Definition 14.2 (Simple type).
A type built from existing types e.g., σ → τ , where σ and τ
are types.

Notation:
I e : τ : “expression e has type τ ”

114 / 210
Base and simple types

Definition 14.1 (Base type).


An atomic type e.g., numbers, booleans etc.
Definition 14.2 (Simple type).
A type built from existing types e.g., σ → τ , where σ and τ
are types.

Notation:
I e : τ : “expression e has type τ ”

I v ∈ τ : “v is a value of type τ ”

114 / 210
Base and simple types

Definition 14.1 (Base type).


An atomic type e.g., numbers, booleans etc.
Definition 14.2 (Simple type).
A type built from existing types e.g., σ → τ , where σ and τ
are 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

Definition 14.1 (Base type).


An atomic type e.g., numbers, booleans etc.
Definition 14.2 (Simple type).
A type built from existing types e.g., σ → τ , where σ and τ
are 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

Definition 14.3 (λt -expression).


I Base value: a base value b ∈ τb is a λt -expression.

115 / 210
Typed λ -expressions

Definition 14.3 (λt -expression).


I Base value: a base value b ∈ τb is a λt -expression.
I Typed variable: an (explicitly) typed variable x : τ is a
λt -expression.

115 / 210
Typed λ -expressions

Definition 14.3 (λt -expression).


I Base value: a base value b ∈ τb is a λt -expression.
I Typed variable: an (explicitly) typed variable x : τ is a
λt -expression.

I Function: if x : σ is a typed variable and e : τ is a


λt -expression, then λ x : σ .e : σ → τ is a λt -expression,
which stands for . . . .

115 / 210
Typed λ -expressions

Definition 14.3 (λt -expression).


I Base value: a base value b ∈ τb is a λt -expression.
I Typed variable: an (explicitly) typed variable x : τ is a
λt -expression.

I Function: if x : σ is a typed variable and e : τ is a


λt -expression, then λ x : σ .e : σ → τ is a λt -expression,
which stands for . . . .
I Application: if f : σ → τ and a : σ are λt -expressions,
then (f a) : τ is a λt -expression, which stands for . . . .

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:

Expr ::= Val


| Var
| (Expr Expr )

117 / 210
Syntax
Expressions

I Variables:
Var ::= ...

I Expressions:

Expr ::= Val


| Var
| (Expr Expr )

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 )

The type annotations are ignored,


since typing precedes evaluation.

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

Example 14.4 (Typing).

λ x : τ1 .λ y : τ2 .x : (τ1 → (τ2 → τ1 ))

Blackboard!

121 / 210
Type systems

Definition 14.5 (Type system).


The set of rules and mechanisms used in a programming
language to organize, build and handle the types
accepted in the language.

122 / 210
Type systems

Definition 14.5 (Type system).


The set of rules and mechanisms used in a programming
language to organize, build and handle the types
accepted in the language.
Definition 14.6 (Soundness).
The type system of a language is sound if any well-typed
expression in the language has the progress and
preservation properties.

122 / 210
Type systems

Definition 14.5 (Type system).


The set of rules and mechanisms used in a programming
language to organize, build and handle the types
accepted in the language.
Definition 14.6 (Soundness).
The type system of a language is sound if any well-typed
expression in the language has the progress and
preservation properties.
Proposition 14.7.
STLC is sound and possesses the strong normalization
property.

122 / 210
Contents

Introduction

Simply Typed Lambda Calculus (STLC, System F1 )

Extending STLC

Polymorphic Lambda Calculus (PSTLC, System F)

Type reconstruction

Higher-Order Polymorphic Lambda Calculus (HPSTLC,


System Fω )

123 / 210
Ways of extending STLC

1. Particular base types

124 / 210
Ways of extending STLC

1. Particular base types

2. n-ary type constructors, n ≥ 1, which build


simple types

124 / 210
The product type
Algebraic specification

I Base constructors i.e., canonical values:

125 / 210
The product type
Algebraic specification

I Base constructors i.e., canonical values:


I τ ∗ τ 0 ::= (τ, τ 0 )

125 / 210
The product type
Algebraic specification

I Base constructors i.e., canonical values:


I τ ∗ τ 0 ::= (τ, τ 0 )

I Operators:

125 / 210
The product type
Algebraic specification

I Base constructors i.e., canonical values:


I τ ∗ τ 0 ::= (τ, τ 0 )

I Operators:
I fst : τ ∗ τ 0 → τ

125 / 210
The product type
Algebraic specification

I Base constructors i.e., canonical values:


I τ ∗ τ 0 ::= (τ, τ 0 )

I Operators:
I fst : τ ∗ τ 0 → τ
I snd : τ ∗ τ 0 → τ 0

125 / 210
The product type
Algebraic specification

I Base constructors i.e., canonical values:


I τ ∗ τ 0 ::= (τ, τ 0 )

I Operators:
I fst : τ ∗ τ 0 → τ
I snd : τ ∗ τ 0 → τ 0

I Axioms (e : τ , e0 : τ 0 ):

125 / 210
The product type
Algebraic specification

I Base constructors i.e., canonical values:


I τ ∗ τ 0 ::= (τ, τ 0 )

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 Base constructors i.e., canonical values:


I τ ∗ τ 0 ::= (τ, τ 0 )

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

Expr ::= ...


| (fst Expr )
| (snd Expr )
| (Expr , Expr )

126 / 210
The product type
Syntax

Expr ::= ...


| (fst Expr )
| (snd Expr )
| (Expr , Expr )

BaseVal ::= ...


| ProductVal

126 / 210
The product type
Syntax

Expr ::= ...


| (fst Expr )
| (snd Expr )
| (Expr , Expr )

BaseVal ::= ...


| ProductVal

ProductVal ::= (Val , Val )

126 / 210
The product type
Syntax

Expr ::= ...


| (fst Expr )
| (snd Expr )
| (Expr , Expr )

BaseVal ::= ...


| ProductVal

ProductVal ::= (Val , Val )

Type ::= ...


| (Type ∗ Type)

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

Example 15.1 (Typing).

Γ ` λ x : ((ρ ∗ τ) → σ ).λ y : ρ.λ z : τ.(x (y , z ))


: ((ρ ∗ τ) → σ ) → ρ → τ → σ

Blackboard!

129 / 210
The Bool type
Algebraic specification

I Base constructors i.e., canonical values:

130 / 210
The Bool type
Algebraic specification

I Base constructors i.e., canonical values:


I Bool ::= True | False

130 / 210
The Bool type
Algebraic specification

I Base constructors i.e., canonical values:


I Bool ::= True | False

I Operators:

130 / 210
The Bool type
Algebraic specification

I Base constructors i.e., canonical values:


I Bool ::= True | False

I Operators:
I not : Bool → Bool

130 / 210
The Bool type
Algebraic specification

I Base constructors i.e., canonical values:


I Bool ::= True | False

I Operators:
I not : Bool → Bool
I and : Bool 2 → Bool

130 / 210
The Bool type
Algebraic specification

I Base constructors i.e., canonical values:


I Bool ::= True | False

I Operators:
I not : Bool → Bool
I and : Bool 2 → Bool
I or : Bool 2 → Bool

130 / 210
The Bool type
Algebraic specification

I Base constructors i.e., canonical values:


I Bool ::= True | False

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 Base constructors i.e., canonical values:


I Bool ::= True | False

I Operators:
I not : Bool → Bool
I and : Bool 2 → Bool
I or : Bool 2 → Bool
I if : Bool × τ × τ → τ

I Axioms: see slide 81

130 / 210
The Bool type
Syntax

131 / 210
The Bool type
Syntax

Expr ::= ...


| (if Expr Expr Expr )

131 / 210
The Bool type
Syntax

Expr ::= ...


| (if Expr Expr Expr )

BaseVal ::= ...


| BoolVal

131 / 210
The Bool type
Syntax

Expr ::= ...


| (if Expr Expr Expr )

BaseVal ::= ...


| BoolVal

BoolVal ::= True | False

131 / 210
The Bool type
Syntax

Expr ::= ...


| (if Expr Expr Expr )

BaseVal ::= ...


| BoolVal

BoolVal ::= True | False

BaseType ::= ...


| Bool

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

I not ≡ λ x : Bool .(if x False True)

134 / 210
The Bool type
Top-level variable bindings

I not ≡ λ x : Bool .(if x False True)

I and ≡ λ x : Bool .λ y : Bool .(if x y False)

134 / 210
The Bool type
Top-level variable bindings

I not ≡ λ x : Bool .(if x False True)

I and ≡ λ x : Bool .λ y : Bool .(if x y False)

I or ≡ λ x : Bool .λ y : Bool .(if x True y )

134 / 210
The N type
Algebraic specification

I Base constructors i.e., canonical values:

135 / 210
The N type
Algebraic specification

I Base constructors i.e., canonical values:


I N ::= 0 | (succ N)

135 / 210
The N type
Algebraic specification

I Base constructors i.e., canonical values:


I N ::= 0 | (succ N)

I Operators:

135 / 210
The N type
Algebraic specification

I Base constructors i.e., canonical values:


I N ::= 0 | (succ N)

I Operators:
I + : N2 → N

135 / 210
The N type
Algebraic specification

I Base constructors i.e., canonical values:


I N ::= 0 | (succ N)

I Operators:
I + : N2 → N
I zero? : N → Bool

135 / 210
The N type
Algebraic specification

I Base constructors i.e., canonical values:


I N ::= 0 | (succ N)

I Operators:
I + : N2 → N
I zero? : N → Bool

I Axioms (m, n ∈ N):

135 / 210
The N type
Algebraic specification

I Base constructors i.e., canonical values:


I N ::= 0 | (succ N)

I Operators:
I + : N2 → N
I zero? : N → Bool

I Axioms (m, n ∈ N):


I (+ 0 n) = n

135 / 210
The N type
Algebraic specification

I Base constructors i.e., canonical values:


I N ::= 0 | (succ N)

I Operators:
I + : N2 → N
I zero? : N → Bool

I Axioms (m, n ∈ N):


I (+ 0 n) = n
I (+ (succ m) n) = (succ (+ m n))

135 / 210
The N type
Algebraic specification

I Base constructors i.e., canonical values:


I N ::= 0 | (succ N)

I Operators:
I + : N2 → N
I zero? : N → Bool

I Axioms (m, n ∈ N):


I (+ 0 n) = n
I (+ (succ m) n) = (succ (+ m n))
I (zero ? 0) = True

135 / 210
The N type
Algebraic specification

I Base constructors i.e., canonical values:


I N ::= 0 | (succ N)

I Operators:
I + : N2 → N
I zero? : N → Bool

I Axioms (m, n ∈ N):


I (+ 0 n) = n
I (+ (succ m) n) = (succ (+ m n))
I (zero ? 0) = True
I (zero ? (succ n)) = False

135 / 210
The N type
Operator semantics

I How to avoid defining evaluation and typing rules


for each operator of N?

136 / 210
The N type
Operator semantics

I How to avoid defining evaluation and typing rules


for each operator of N?

I Introduce the primitive recursor for N, prec N ,


which allows for defining any primitive recursive
function on natural numbers

136 / 210
The N type
Operator semantics

I How to avoid defining evaluation and typing rules


for each operator of N?

I Introduce the primitive recursor for N, prec N ,


which allows for defining any primitive recursive
function on natural numbers

I Define the operators using the primitive recursor

136 / 210
The N type
Syntax

137 / 210
The N type
Syntax

Expr ::= ...


| (succ Expr )
| (prec N Expr Expr Expr )

137 / 210
The N type
Syntax

Expr ::= ...


| (succ Expr )
| (prec N Expr Expr Expr )

BaseVal ::= ...


| NVal

137 / 210
The N type
Syntax

Expr ::= ...


| (succ Expr )
| (prec N Expr Expr Expr )

BaseVal ::= ...


| NVal

NVal ::= 0
| (succ NVal )

137 / 210
The N type
Syntax

Expr ::= ...


| (succ Expr )
| (prec N Expr Expr Expr )

BaseVal ::= ...


| NVal

NVal ::= 0
| (succ NVal )

BaseType ::= ...


| N
137 / 210
The N type
Evaluation
I EvalSucc:
e → e0
(succ e) → (succ e0 )

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

I zero? ≡ λ n : N.(prec N True λ x : N.λ y : Bool .False n)

140 / 210
The N type
Top-level variable bindings

I zero? ≡ λ n : N.(prec N True λ x : N.λ y : Bool .False n)

I + ≡ λ m : N.λ n : N.(prec N n λ x : N.λ y : N.(succ y ) m)

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

I Axioms (h ∈ τ , t ∈ (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

I Axioms (h ∈ τ , t ∈ (List τ)):


I (head (cons h t )) = h

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

I Axioms (h ∈ τ , t ∈ (List τ)):


I (head (cons h t )) = h
I (tail (cons h t )) = t

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

I Axioms (h ∈ τ , t ∈ (List τ)):


I (head (cons h t )) = h
I (tail (cons h t )) = t
I (length []) = 0

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

I Axioms (h ∈ τ , t ∈ (List τ)):


I (head (cons h t )) = h
I (tail (cons h t )) = t
I (length []) = 0
I (length (cons h t )) = (succ (length t ))
141 / 210
The (List τ) type
Syntax

142 / 210
The (List τ) type
Syntax

Expr ::= ...


| (cons Expr Expr )
| (prec L Expr Expr Expr )

142 / 210
The (List τ) type
Syntax

Expr ::= ...


| (cons Expr Expr )
| (prec L Expr Expr Expr )

BaseVal ::= ...


| ListVal

142 / 210
The (List τ) type
Syntax

Expr ::= ...


| (cons Expr Expr )
| (prec L Expr Expr Expr )

BaseVal ::= ...


| ListVal

ListVal ::= []
| (cons Value ListVal )

142 / 210
The (List τ) type
Syntax

Expr ::= ...


| (cons Expr Expr )
| (prec L Expr Expr Expr )

BaseVal ::= ...


| ListVal

ListVal ::= []
| (cons Value ListVal )

Type ::= ...


| (List Type)
142 / 210
The (List τ) type
Evaluation
I EvalCons:
e → e0
(cons e e00 ) → (cons e0 e00 )

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

I empty ? ≡ λ l : (List τ).(prec L True f l ),


f ≡ λ h : τ.λ t : (List τ).λ r : Bool .False

145 / 210
The (List τ) type
Top-level variable bindings

I empty ? ≡ λ l : (List τ).(prec L True f l ),


f ≡ λ h : τ.λ t : (List τ).λ r : Bool .False

I length ≡ λ l : (List τ).(prec L 0 f l ),


f ≡ λ h : τ.λ t : (List τ).λ r : N.(succ r )

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

I Introduce the operator fix i.e., a fixed-point


combinator

146 / 210
General recursion

I Primitive recursion
I induces strong normalization
I insufficient for capturing effectively computable
functions

I Introduce the operator fix i.e., a fixed-point


combinator

I Gain computation power at the expense


of strong normalization

146 / 210
fix
Syntax

147 / 210
fix
Syntax

Expr ::= ...


| (fix Expr )

147 / 210
fix
Evaluation

I EvalFix:

(fix λ x : τ.e) → e[(fix λ x :τ.e)/x ] = (f (fix f ))

148 / 210
fix
Evaluation

I EvalFix:

(fix λ x : τ.e) → e[(fix λ x :τ.e)/x ] = (f (fix f ))

I EvalFix 0 :
e → e0
(fix e) → (fix e0 )

148 / 210
fix
Typing

I TFix:
Γ ` e : (τ → τ)
Γ ` (fix e) : τ

149 / 210
fix
Example

Example 15.2 (The remainder function).

remainder = λ m : N.λ n : N.
((fix λ f : (N → N).λ p : N.
(if p < n then p else (f (p − n)))) m)

The evaluation of (remainder 3 0) does not terminate.

150 / 210
Monomorphism

I Within the types (τ ∗ τ 0 ) and (List τ), τ and τ 0


designate specific types e.g., Bool, N, (List N), etc.

151 / 210
Monomorphism

I Within the types (τ ∗ τ 0 ) and (List τ), τ and τ 0


designate specific types e.g., Bool, N, (List N), etc.

I Dedicated operators for each simple type

151 / 210
Monomorphism

I Within the types (τ ∗ τ 0 ) and (List τ), τ and τ 0


designate specific types e.g., Bool, N, (List N), etc.

I Dedicated operators for each simple type

I fst N,Bool , fst Bool ,N , . . .

151 / 210
Monomorphism

I Within the types (τ ∗ τ 0 ) and (List τ), τ and τ 0


designate specific types e.g., Bool, N, (List N), etc.

I Dedicated operators for each simple type

I fst N,Bool , fst Bool ,N , . . .

I []N , []Bool , . . .

151 / 210
Monomorphism

I Within the types (τ ∗ τ 0 ) and (List τ), τ and τ 0


designate specific types e.g., Bool, N, (List N), etc.

I Dedicated operators for each simple type

I fst N,Bool , fst Bool ,N , . . .

I []N , []Bool , . . .

I empty ?N , empty ?Bool , . . .

151 / 210
Contents

Introduction

Simply Typed Lambda Calculus (STLC, System F1 )

Extending STLC

Polymorphic Lambda Calculus (PSTLC, System F)

Type reconstruction

Higher-Order Polymorphic Lambda Calculus (HPSTLC,


System Fω )

152 / 210
Idea

I Monomorphic identity function for type N:

id N ≡ λ x : N.x : (N → N)

153 / 210
Idea

I Monomorphic identity function for type N:

id N ≡ λ x : N.x : (N → N)

I Polymorphic identity function — type variables:

id ≡ λ X .λ x : X .x : ∀X .(X → X )

153 / 210
Idea

I Monomorphic identity function for type N:

id N ≡ λ x : N.x : (N → N)

I Polymorphic identity function — type variables:

id ≡ λ X .λ x : X .x : ∀X .(X → X )

I Type coercion prior to function application:

(id [N] 5) → (id N 5) → 5

153 / 210
Syntax

I Program variables: stand for program values

Var ::= ...

154 / 210
Syntax

I Program variables: stand for program values

Var ::= ...

I Type variables: stand for types

TypeVar ::= ...

154 / 210
Syntax
I Expressions:

Expr ::= Value


| Var
| (Expr Expr )
| Expr [Type]

155 / 210
Syntax
I Expressions:

Expr ::= Value


| Var
| (Expr Expr )
| Expr [Type]

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

I TAbs2 — polymorphic expressions have


universal types:
Γ, X ` e : τ
Γ ` λ X .e : ∀X .τ

159 / 210
Semantics
Typing

I TAbs2 — polymorphic expressions have


universal types:
Γ, X ` e : τ
Γ ` λ X .e : ∀X .τ

I TApp2 :
Γ ` e : ∀X .τ
Γ ` e[τ 0 ] : τ[τ 0 /X ]

159 / 210
Semantics
Substitution and free variables

I Expr [Expr /Var ]

160 / 210
Semantics
Substitution and free variables

I Expr [Expr /Var ]

I Expr [Type/TypeVar ]

160 / 210
Semantics
Substitution and free variables

I Expr [Expr /Var ]

I Expr [Type/TypeVar ]

I Type[Type/TypeVar ]

160 / 210
Semantics
Substitution and free variables

I Expr [Expr /Var ]

I Expr [Type/TypeVar ]

I Type[Type/TypeVar ]

I Free program variables

160 / 210
Semantics
Substitution and free variables

I Expr [Expr /Var ]

I Expr [Type/TypeVar ]

I Type[Type/TypeVar ]

I Free program variables

I Free type variables

160 / 210
Typing example

Example 16.1 (Typing).

Γ ` λ f : ∀X .(X → X ).λ Y .λ x : Y .(f [Y ] x )


: (∀X .(X → X ) → ∀Y .(Y → Y ))

161 / 210
Typing example

Example 16.1 (Typing).

Γ ` λ f : ∀X .(X → X ).λ Y .λ x : Y .(f [Y ] x )


: (∀X .(X → X ) → ∀Y .(Y → Y ))

Monomorphic function
with polymorphic argument and result!

161 / 210
Typing example

Example 16.1 (Typing).

Γ ` λ f : ∀X .(X → X ).λ Y .λ x : Y .(f [Y ] x )


: (∀X .(X → X ) → ∀Y .(Y → Y ))

Monomorphic function
with polymorphic argument and result!

Blackboard!

161 / 210
Examples of polymorphic expressions

Example 16.2 (Doubling a computation).

double ≡

162 / 210
Examples of polymorphic expressions

Example 16.2 (Doubling a computation).

double ≡ λ X .λ f : (X → X ).λ x : X .(f (f x ))

162 / 210
Examples of polymorphic expressions

Example 16.2 (Doubling a computation).

double ≡ λ X .λ f : (X → X ).λ x : X .(f (f x ))


: ∀X .((X → X ) → (X → X ))

162 / 210
Examples of polymorphic expressions

Example 16.2 (Doubling a computation).

double ≡ λ X .λ f : (X → X ).λ x : X .(f (f x ))


: ∀X .((X → X ) → (X → X ))

Example 16.3 (Quadrupling a computation).

quadruple ≡

162 / 210
Examples of polymorphic expressions

Example 16.2 (Doubling a computation).

double ≡ λ X .λ f : (X → X ).λ x : X .(f (f x ))


: ∀X .((X → X ) → (X → X ))

Example 16.3 (Quadrupling a computation).

quadruple ≡ λ X .(double[X → X ] double[X ])

162 / 210
Examples of polymorphic expressions

Example 16.2 (Doubling a computation).

double ≡ λ X .λ f : (X → X ).λ x : X .(f (f x ))


: ∀X .((X → X ) → (X → X ))

Example 16.3 (Quadrupling a computation).

quadruple ≡ λ X .(double[X → X ] double[X ])


: ∀X .((X → X ) → (X → X ))

162 / 210
Examples of polymorphic expressions

Example 16.4 (Reflexive computation).

reflexive ≡

163 / 210
Examples of polymorphic expressions

Example 16.4 (Reflexive computation).

reflexive ≡ λ f : ∀X .(X → X ).(f [∀X .(X → X )] f )

163 / 210
Examples of polymorphic expressions

Example 16.4 (Reflexive computation).

reflexive ≡ λ f : ∀X .(X → X ).(f [∀X .(X → X )] f )


: (∀X .(X → X ) → ∀X .(X → X ))

163 / 210
Examples of polymorphic expressions

Example 16.4 (Reflexive computation).

reflexive ≡ λ f : ∀X .(X → X ).(f [∀X .(X → X )] f )


: (∀X .(X → X ) → ∀X .(X → X ))

Example 16.5 (Fixed-point combinator).

Fix ≡

163 / 210
Examples of polymorphic expressions

Example 16.4 (Reflexive computation).

reflexive ≡ λ f : ∀X .(X → X ).(f [∀X .(X → X )] f )


: (∀X .(X → X ) → ∀X .(X → X ))

Example 16.5 (Fixed-point combinator).

Fix ≡ λ X .λ f : (X → X ).(f (Fix [X ] f ))

163 / 210
Examples of polymorphic expressions

Example 16.4 (Reflexive computation).

reflexive ≡ λ f : ∀X .(X → X ).(f [∀X .(X → X )] f )


: (∀X .(X → X ) → ∀X .(X → X ))

Example 16.5 (Fixed-point combinator).

Fix ≡ λ X .λ f : (X → X ).(f (Fix [X ] f ))


: ∀X .((X → X ) → X )

163 / 210
Contents

Introduction

Simply Typed Lambda Calculus (STLC, System F1 )

Extending STLC

Polymorphic Lambda Calculus (PSTLC, System F)

Type reconstruction

Higher-Order Polymorphic Lambda Calculus (HPSTLC,


System Fω )

164 / 210
Motivation

165 / 210
Contents

Introduction

Simply Typed Lambda Calculus (STLC, System F1 )

Extending STLC

Polymorphic Lambda Calculus (PSTLC, System F)

Type reconstruction

Higher-Order Polymorphic Lambda Calculus (HPSTLC,


System Fω )

166 / 210
Problem
I Polymorphic identity function, on objects of a type
built using 1-ary type constructors e.g., List:

f ≡ λ C .λ X .λ x : (C X ).x : ∀C .∀X .((C X ) → (C X ))

167 / 210
Problem
I Polymorphic identity function, on objects of a type
built using 1-ary type constructors e.g., List:

f ≡ λ C .λ X .λ x : (C X ).x : ∀C .∀X .((C X ) → (C X ))

I C stands for a 1-ary type constructor, X stands for a


type of program values i.e., a proper type

167 / 210
Problem
I Polymorphic identity function, on objects of a type
built using 1-ary type constructors e.g., List:

f ≡ λ C .λ X .λ x : (C X ).x : ∀C .∀X .((C X ) → (C X ))

I C stands for a 1-ary type constructor, X stands for a


type of program values i.e., a proper type

I Monomorphic identity function for type (List N):

f [List ][N] → λ x : (List N).x : ((List N) → (List N))

167 / 210
Problem
I Polymorphic identity function, on objects of a type
built using 1-ary type constructors e.g., List:

f ≡ λ C .λ X .λ x : (C X ).x : ∀C .∀X .((C X ) → (C X ))

I C stands for a 1-ary type constructor, X stands for a


type of program values i.e., a proper type

I Monomorphic identity function for type (List N):

f [List ][N] → λ x : (List N).x : ((List N) → (List N))

I How do we prevent erroneous situations e.g.,


f [N][N], f [List ][List ]?

167 / 210
Solution

I Two categories of types: proper types, and type


constructors i.e., λ TypeVar .Type

168 / 210
Solution

I Two categories of types: proper types, and type


constructors i.e., λ TypeVar .Type

I Type not only program variables, but also type


variables

168 / 210
Solution

I Two categories of types: proper types, and type


constructors i.e., λ TypeVar .Type

I Type not only program variables, but also type


variables

I The type of a type: kind

168 / 210
Kinds
Notation

I The kind of a proper type: ∗

169 / 210
Kinds
Notation

I The kind of a proper type: ∗

I The kind of a 1-ary type constructor: (∗ ⇒ ∗)

169 / 210
Kinds
Notation

I The kind of a proper type: ∗

I The kind of a 1-ary type constructor: (∗ ⇒ ∗)

I The kind of an n-ary type constructor, n ≥ 1: k1 ⇒ k2

169 / 210
Kinds
Notation

I The kind of a proper type: ∗

I The kind of a 1-ary type constructor: (∗ ⇒ ∗)

I The kind of an n-ary type constructor, n ≥ 1: k1 ⇒ k2

I The kind k of a type τ : τ :: k

169 / 210
Kinds
Examples

Example 18.1 (Kinds).


I N

170 / 210
Kinds
Examples

Example 18.1 (Kinds).


I N :: ∗

I List

170 / 210
Kinds
Examples

Example 18.1 (Kinds).


I N :: ∗

I List :: (∗ ⇒ ∗)

I f ≡ λ C :: (∗ ⇒ ∗).λ X :: ∗.λ x : (C X ).x

170 / 210
Kinds
Examples

Example 18.1 (Kinds).


I N :: ∗

I List :: (∗ ⇒ ∗)

I f ≡ λ C :: (∗ ⇒ ∗).λ X :: ∗.λ x : (C X ).x


f : ∀C :: (∗ ⇒ ∗).∀X :: ∗.((C X ) → (C X ))

170 / 210
Levels of expressions
Levels of expressions

Expressions
Levels of expressions

0
Expressions
Levels of expressions

0 (0, True)
Expressions
Levels of expressions

0 (0, True) [][N]


Expressions
Levels of expressions

0 (0, True) [][N] λ x : N.x


Expressions
Levels of expressions

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

N
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

(N ∗ Bool )

N
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

(N ∗ Bool )

N (List N)
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

(N ∗ Bool ) (N → N)

N (List N)
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

(N ∗ Bool ) (N → N)

N (List N) ∀X :: ∗.(X → X )
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

List
(N ∗ Bool ) (N → N)

N (List N) ∀X :: ∗.(X → X )
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

List (_ ∗ _)
(N ∗ Bool ) (N → N)

N (List N) ∀X :: ∗.(X → X )
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))

N (List N) ∀X :: ∗.(X → X )
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

Kinds

List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))

N (List N) ∀X :: ∗.(X → X )
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions


Kinds

List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))

N (List N) ∀X :: ∗.(X → X )
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions


(∗ ⇒ ∗)
Kinds

List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))

N (List N) ∀X :: ∗.(X → X )
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

∗ (∗ ⇒ (∗ ⇒ ∗))
(∗ ⇒ ∗)
Kinds

List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))

N (List N) ∀X :: ∗.(X → X )
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions
Levels of expressions

((∗ ⇒ ∗) ⇒ (∗ ⇒ ∗))
∗ (∗ ⇒ (∗ ⇒ ∗))
(∗ ⇒ ∗)
Kinds

List (_ ∗ _)
(N ∗ Bool ) (N → N) λ C :: (∗ ⇒ ∗).λ X :: ∗.(X → (C X ))

N (List N) ∀X :: ∗.(X → X )
Types

0 (0, True) [][N] λ x : N.x λ X :: ∗.λ x : X .x


Expressions

171 / 210
Type equivalence

I Two syntactically distinct types:

τ1 ≡ ((List N) → (List N))


τ2 ≡ (λ X :: ∗.((List X ) → (List X )) N)

172 / 210
Type equivalence

I Two syntactically distinct types:

τ1 ≡ ((List N) → (List N))


τ2 ≡ (λ X :: ∗.((List X ) → (List X )) N)

I Semantically, they denote the same type i.e., they are


equivalent: τ1 ≡ τ2

172 / 210
Syntax
I Expressions:

Expr ::= Value


| Var
| (Expr Expr )
| Expr [Type]

173 / 210
Syntax
I Expressions:

Expr ::= Value


| Var
| (Expr Expr )
| Expr [Type]

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

Example 18.2 (Kinding).

∀X :: ∗.(X → ((List X ) → (Tree X )))

184 / 210
Kinding example

Example 18.2 (Kinding).

∀X :: ∗.(X → ((List X ) → (Tree X ))) :: ∗

Blackboard!

184 / 210
Part V

Constructive Type Theory

185 / 210
Contents

Constructive paradigm

Syntax and semantics

186 / 210
Contents

Constructive paradigm

Syntax and semantics

187 / 210
Classical logic

I Example: prove ∃x .P (x )

188 / 210
Classical logic

I Example: prove ∃x .P (x )

I Perhaps, proof by contradiction: assume ¬∃x .P (x )


and reach a contradiction

188 / 210
Classical logic

I Example: prove ∃x .P (x )

I Perhaps, proof by contradiction: assume ¬∃x .P (x )


and reach a contradiction

I Assumption: ∃x .P (x ) ∨ ¬∃x .P (x )
(law of excluded middle)

188 / 210
Classical logic

I Example: prove ∃x .P (x )

I Perhaps, proof by contradiction: assume ¬∃x .P (x )


and reach a contradiction

I Assumption: ∃x .P (x ) ∨ ¬∃x .P (x )
(law of excluded middle)

I Problem: possibly no actual evidence regarding either


sentence i.e., some a s.t. either P (a) or ¬P (a) is true

188 / 210
Constructive logic

I Prove ∃x .P (x ) by computing an object a


s.t. P (a) is true

189 / 210
Constructive logic

I Prove ∃x .P (x ) by computing an object a


s.t. P (a) is true

I Not always possible

189 / 210
Constructive logic

I Prove ∃x .P (x ) by computing an object a


s.t. P (a) is true

I Not always possible

I However, not being able to compute a does not mean


that ∃x .P (x ) is false

189 / 210
Constructive logic

I Prove ∃x .P (x ) by computing an object a


s.t. P (a) is true

I Not always possible

I However, not being able to compute a does not mean


that ∃x .P (x ) is false

I Law of excluded middle — not an axiom


in constructive logic

189 / 210
Constructive type theory

I Bridge between constructive logic and typed lambda


calculus

190 / 210
Constructive type theory

I Bridge between constructive logic and typed lambda


calculus

I Correspondences:

190 / 210
Constructive type theory

I Bridge between constructive logic and typed lambda


calculus

I Correspondences:
I sentence ↔ type

190 / 210
Constructive type theory

I Bridge between constructive logic and typed lambda


calculus

I Correspondences:
I sentence ↔ type
I logical connective ↔ type constructor

190 / 210
Constructive type theory

I Bridge between constructive logic and typed lambda


calculus

I Correspondences:
I sentence ↔ type
I logical connective ↔ type constructor
I proof ↔ function with that type

190 / 210
Constructive type theory

I Bridge between constructive logic and typed lambda


calculus

I Correspondences:
I sentence ↔ type
I logical connective ↔ type constructor
I proof ↔ function with that type

I Application: synthesize a program by proving


the sentence that corresponds to its specification

190 / 210
The Curry-Howard isomorphism

Sentence Type

Proof Value
inference synthesis

Proof Value

191 / 210
Contents

Constructive paradigm

Syntax and semantics

192 / 210
Two views

a:A

I Type-theoretic: “a is a value of type A”


I Logical: “a is a proof of sentence A”

193 / 210
Definitional rules

Rule Logical view Type-theoretic view

194 / 210
Definitional rules

Rule Logical view Type-theoretic view


Formation How a connective re- How a type construc-
lates two sentences tor is used

194 / 210
Definitional rules

Rule Logical view Type-theoretic view


Formation How a connective re- How a type construc-
lates two sentences tor is used
Introduction/ How a proof is derived How a value is con-
elimination structed

194 / 210
Definitional rules

Rule Logical view Type-theoretic view


Formation How a connective re- How a type construc-
lates two sentences tor is used
Introduction/ How a proof is derived How a value is con-
elimination structed
Computation How a proof is simpli- How an expression is
fied evaluated

194 / 210
Other logic-type correspondences

Logical view Type-theoretic view

195 / 210
Other logic-type correspondences

Logical view Type-theoretic view


Truth (>) One-element type, containing the
trivial proof

195 / 210
Other logic-type correspondences

Logical view Type-theoretic view


Truth (>) One-element type, containing the
trivial proof
Falsity (⊥) No-element type, containing no
proof

195 / 210
Other logic-type correspondences

Logical view Type-theoretic view


Truth (>) One-element type, containing the
trivial proof
Falsity (⊥) No-element type, containing no
proof
Proof by induction Definition by recursion

195 / 210
Logical conjunction / product 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):


a:A b:B
(a , b ) : A ∧ B

196 / 210
Logical conjunction / product type constructor II

I Elimination rules (∧E1,2 ):

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

I Elimination rule (⇒ E):

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

I Formation rule (∨F ):

A is a sentence/ type B is a sentence/ type


A ∨ B is a sentence/ type

I Introduction rules (∨I1,2 ):

a:A b:B
inl a : A ∨ B inr b : A ∨ B

200 / 210
Logical disjunction / sum type constructor II

I Elimination rule (∨E):

p : A∨B f :A⇒C g:B⇒C


cases p f g : C

I Computation rules:

cases (inl a) f g → f a
cases (inr b) f g → g b

201 / 210
Absurd sentence / empty type I

I Formation rule (⊥F ):

⊥ is a sentence/ type

I Introduction rule: none — there is no proof of the


absurd sentence

202 / 210
Absurd sentence / empty type II

I Elimination rule (⊥E)


(a proof of the absurd sentence can prove anything):
p:⊥
abort A p : A

I Computation rule: none

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 Introduction rule (∀I):


[x : A]
..
.
b:B
(λ x : A).b : (∀x : A).B
206 / 210
Universal quantification / generalized function
type constructor II

I Elimination rule (∀E):

a:A f : (∀x : A).B


(f a) : B[a/x ]

I Computation rule:

((λ x : A).b a) → b[a/x ]

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

I Introduction rule (∃I):


a:A b : B[a/x ]
(a, b ) : (∃x : A).B

208 / 210
Existential quantification / generalized product
type constructor II
I Elimination rules (∃E1,2 ):

p : (∃x : A).B p : (∃x : A).B


Fst p : A Snd p : B[Fst p/x ]

I Computation rules:

Fst (a, b) → a
Snd (a, b) → b

209 / 210
Example proofs

I (∀x : A).(B ⇒ C ) ⇒ (∀x : A).B ⇒ (∀x : A).C

210 / 210
Example proofs

I (∀x : A).(B ⇒ C ) ⇒ (∀x : A).B ⇒ (∀x : A).C

I (∃x : X ).¬P ⇒ ¬(∀x : X ).P (converse?)

210 / 210
Example proofs

I (∀x : A).(B ⇒ C ) ⇒ (∀x : A).B ⇒ (∀x : A).C

I (∃x : X ).¬P ⇒ ¬(∀x : X ).P (converse?)

I (∃y : Y ).(∀x : X ).P ⇒ (∀x : X ).(∃y : Y ).P (converse?)

210 / 210

You might also like