inf551: simply typed lambda-calculus · puttingitalltogether whatwehavedonesofar. 1....

318
INF551: Simply typed λ-calculus Samuel Mimram École Polytechnique

Upload: others

Post on 01-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

INF551: Simply typed λ-calculus

Samuel Mimram

École Polytechnique

Page 2: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Part I

Introduction

1

Page 3: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Putting it all together

What we have done so far.

1. We have seen that types in OCaml could intuitively be interpreted as formulas.

2. We have formally defined what is a formula and a proof.

3. We have formally defined the core of a functional language (λ-calculus).

and now we put it all together:

4. We define a typing system for λ-calculus and show that it corresponds precisely tobuilding proofs.

2

Page 4: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Putting it all together

In other words,

PROGRAM = PROOF

Or, more precisely, there is a bijection between

• types and formulas,

• programs of type A and proofs of A,

• reductions of programs and cut elimination.

3

Page 5: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Putting it all together

In other words,

PROGRAM = PROOF

Or, more precisely, there is a bijection between

• types and formulas,

• programs of type A and proofs of A,

• reductions of programs and cut elimination.

3

Page 6: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The core

In order to have things as simple as possible, we will first focus on functions.

But, we will see that it extends to more realistic programming languages.

4

Page 7: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Part II

Simply typed λ-calculus

5

Page 8: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Simple types

The simple types are generated by the grammar

A,B ::= X | A→ B

where X is a variable.

For instance, we have a type(X → Y )→ X

which roughly corresponds to OCaml’s

(’a -> ’b) -> ’a

By convention, arrows are associated on the right:

X → Y → Z = X → (Y → Z )

6

Page 9: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Simple types

The simple types are generated by the grammar

A,B ::= X | A→ B

where X is a variable.

For instance, we have a type(X → Y )→ X

which roughly corresponds to OCaml’s

(’a -> ’b) -> ’a

By convention, arrows are associated on the right:

X → Y → Z = X → (Y → Z )6

Page 10: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Terms

The programs we considers are λ-terms generated by the grammar

t, u ::= x | t u | λxA.t

where x is a variable and A is a type.

All the abstractions carry the type of the abstracted variable:

λxint.x

corresponds to OCaml’sfun (x : int) -> x

This is called Church style (the other one being Curry style).

7

Page 11: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Terms

The programs we considers are λ-terms generated by the grammar

t, u ::= x | t u | λxA.t

where x is a variable and A is a type.

All the abstractions carry the type of the abstracted variable:

λxint.x

corresponds to OCaml’sfun (x : int) -> x

This is called Church style.7

Page 12: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Terms

The programs we considers are λ-terms generated by the grammar

t, u ::= x | t u | λx .t

where x is a variable and A is a type.

All the abstractions carry the type of the abstracted variable:

λx .x

corresponds to OCaml’sfun (x ) -> x

This is called Church style (the other one being Curry style).7

Page 13: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing

We are now going assign types to terms. For instance, the type of

term typeλxA.x

A→ A

λf A→A.λxA.f (fx) (A→ A)→ A→ A

λxA.xx not well-typed!

As usual, we will formulate this by using inference rules on sequents.

8

Page 14: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing

We are now going assign types to terms. For instance, the type of

term typeλxA.x A→ A

λf A→A.λxA.f (fx) (A→ A)→ A→ A

λxA.xx not well-typed!

As usual, we will formulate this by using inference rules on sequents.

8

Page 15: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing

We are now going assign types to terms. For instance, the type of

term typeλxA.x A→ A

λf A→A.λxA.f (fx)

(A→ A)→ A→ A

λxA.xx not well-typed!

As usual, we will formulate this by using inference rules on sequents.

8

Page 16: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing

We are now going assign types to terms. For instance, the type of

term typeλxA.x A→ A

λf A→A.λxA.f (fx) (A→ A)→ A→ A

λxA.xx not well-typed!

As usual, we will formulate this by using inference rules on sequents.

8

Page 17: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing

We are now going assign types to terms. For instance, the type of

term typeλxA.x A→ A

λf A→A.λxA.f (fx) (A→ A)→ A→ A

λxA.xx

not well-typed!

As usual, we will formulate this by using inference rules on sequents.

8

Page 18: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing

We are now going assign types to terms. For instance, the type of

term typeλxA.x A→ A

λf A→A.λxA.f (fx) (A→ A)→ A→ A

λxA.xx not well-typed!

As usual, we will formulate this by using inference rules on sequents.

8

Page 19: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Contexts

A context Γ is a listx1 : A1, . . . , xn : An

of pairs consisting of a variable xi and a type Ai .

It can be read as “I assume that the variable xi has type Ai for every index i”.

The domain of the context Γ is dom(Γ) = {x1, . . . , xn}.

Given x ∈ dom(Γ) we write Γ(x) for the type of x ∈ Γ:

(Γ, x : A)(x) = A (Γ, y : A)(x) = Γ(x)

(note that a variable might occur multiple times).

9

Page 20: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Contexts

A context Γ is a listx1 : A1, . . . , xn : An

of pairs consisting of a variable xi and a type Ai .

It can be read as “I assume that the variable xi has type Ai for every index i”.

The domain of the context Γ is dom(Γ) = {x1, . . . , xn}.

Given x ∈ dom(Γ) we write Γ(x) for the type of x ∈ Γ:

(Γ, x : A)(x) = A (Γ, y : A)(x) = Γ(x)

(note that a variable might occur multiple times).

9

Page 21: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Contexts

A context Γ is a listx1 : A1, . . . , xn : An

of pairs consisting of a variable xi and a type Ai .

It can be read as “I assume that the variable xi has type Ai for every index i”.

The domain of the context Γ is dom(Γ) = {x1, . . . , xn}.

Given x ∈ dom(Γ) we write Γ(x) for the type of x ∈ Γ:

(Γ, x : A)(x) = A (Γ, y : A)(x) = Γ(x)

(note that a variable might occur multiple times).

9

Page 22: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Sequents

A sequent is a triple notedΓ ` t : A

where

• Γ is a context,

• t is a term,

• A is a type.

Read as “under the typing assumptions for the variables in Γ, the term t has type A”.

10

Page 23: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing rules

The typing rules are

Γ ` x : Γ(x)(ax)

Γ, x : A ` t : B

Γ ` λxA.t : A→ B(→I)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

with x ∈ dom(Γ) for (ax).

Note that depending on the term only one rule applies.

11

Page 24: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing rules

The typing rules are

Γ ` x : Γ(x)(ax)

Γ, x : A ` t : B

Γ ` λxA.t : A→ B(→I)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

with x ∈ dom(Γ) for (ax).

Note that depending on the term only one rule applies.11

Page 25: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing

We say that a term t has type A in context Γ when Γ ` t : A is derivable.

We say that a term t has type A when ` t : A is derivable.

NB: even in this last case, we still need the context to inductively check the type,because of the rule (→I).

12

Page 26: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing

We say that a term t has type A in context Γ when Γ ` t : A is derivable.

We say that a term t has type A when ` t : A is derivable.

NB: even in this last case, we still need the context to inductively check the type,because of the rule (→I).

12

Page 27: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing

We say that a term t has type A in context Γ when Γ ` t : A is derivable.

We say that a term t has type A when ` t : A is derivable.

NB: even in this last case, we still need the context to inductively check the type,because of the rule (→I).

12

Page 28: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

An example of typing derivation

For instance we claim that

λf A→A.λxA.f (fx) has type (A→ A)→ A→ A

which means that` λf A→A.λxA.f (fx) : (A→ A)→ A→ A

is derivable.

13

Page 29: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

An example of typing derivation

` λf A→A.λxA.f (fx) : (A→ A)→ A→ A(→I)

14

Page 30: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

An example of typing derivation

f : A→ A ` λxA.f (fx) : A→ A(→I)

` λf A→A.λxA.f (fx) : (A→ A)→ A→ A(→I)

14

Page 31: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

An example of typing derivation

f : A→ A, x : A ` f (fx) : A(→E)

f : A→ A ` λxA.f (fx) : A→ A(→I)

` λf A→A.λxA.f (fx) : (A→ A)→ A→ A(→I)

14

Page 32: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

An example of typing derivation

Γ ` f : A→ A(ax)

Γ ` fx : A(→E)

f : A→ A, x : A ` f (fx) : A(→E)

f : A→ A ` λxA.f (fx) : A→ A(→I)

` λf A→A.λxA.f (fx) : (A→ A)→ A→ A(→I)

14

Page 33: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

An example of typing derivation

Γ ` f : A→ A(ax)

Γ ` fx : A(→E)

f : A→ A, x : A ` f (fx) : A(→E)

f : A→ A ` λxA.f (fx) : A→ A(→I)

` λf A→A.λxA.f (fx) : (A→ A)→ A→ A(→I)

14

Page 34: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

An example of typing derivation

Γ ` f : A→ A(ax)

Γ ` f : A→ A(ax)

Γ ` x : A(ax)

Γ ` fx : A(→E)

f : A→ A, x : A ` f (fx) : A(→E)

f : A→ A ` λxA.f (fx) : A→ A(→I)

` λf A→A.λxA.f (fx) : (A→ A)→ A→ A(→I)

14

Page 35: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

An example of typing derivation

Γ ` f : A→ A(ax)

Γ ` f : A→ A(ax)

Γ ` x : A(ax)

Γ ` fx : A(→E)

f : A→ A, x : A ` f (fx) : A(→E)

f : A→ A ` λxA.f (fx) : A→ A(→I)

` λf A→A.λxA.f (fx) : (A→ A)→ A→ A(→I)

14

Page 36: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

An example of typing derivation

Γ ` f : A→ A(ax)

Γ ` f : A→ A(ax)

Γ ` x : A(ax)

Γ ` fx : A(→E)

f : A→ A, x : A ` f (fx) : A(→E)

f : A→ A ` λxA.f (fx) : A→ A(→I)

` λf A→A.λxA.f (fx) : (A→ A)→ A→ A(→I)

14

Page 37: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Weakening

The typing system satisfies the usual structural properties.

For instance, the weakening rule is admissible:

PropositionIf Γ ` t : A is derivable then Γ,∆ ` t : A is also derivable,provided that dom(∆) ∩ dom(Γ) = ∅.

15

Page 38: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Uniqueness of typing

A term admits at most one type:

TheoremIf Γ ` t : A and Γ ` t : A′ are derivable then A = A′ (and the two proofs are the same!).

Proof.By induction on the term t.

16

Page 39: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Uniqueness of typing

A term admits at most one type:

TheoremIf Γ ` t : A and Γ ` t : A′ are derivable then A = A′ (and the two proofs are the same!).

Proof.By induction on the term t.

16

Page 40: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Uniqueness of typing

A term admits at most one type:

TheoremIf Γ ` t : A and Γ ` t : A′ are derivable then A = A′ (and the two proofs are the same!).

Proof.By induction on the term t.We recall that the typing rules are:

Γ ` x : Γ(x)(ax)

Γ, x : A ` t : B

Γ ` λxA.t : A→ B(→I)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

16

Page 41: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Uniqueness of typing

A term admits at most one type:

TheoremIf Γ ` t : A and Γ ` t : A′ are derivable then A = A′ (and the two proofs are the same!).

Proof.By induction on the term t.

• If t = x then the two derivations are necessarily

Γ ` x : Γ(x)(ax)

and A = A′ = Γ(x)

16

Page 42: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Uniqueness of typing

A term admits at most one type:

TheoremIf Γ ` t : A and Γ ` t : A′ are derivable then A = A′ (and the two proofs are the same!).

Proof.By induction on the term t.

• If t = λxB .u then the two derivations are necessarily of the form

Γ, x : B ` t : C

Γ ` λxB .t : B → C(→I)

Γ, x : B ` t : C ′

Γ ` λxB .t : B → C ′(→I)

by induction hypothesis we have C = C ′

and thus A = B → C = B → C ′ = A′.

16

Page 43: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Uniqueness of typing

A term admits at most one type:

TheoremIf Γ ` t : A and Γ ` t : A′ are derivable then A = A′ (and the two proofs are the same!).

Proof.By induction on the term t.

• If t = u v then the two derivations are necessarily of the form

Γ ` t : B → A Γ ` u : B

Γ ` t u : A(→E)

Γ ` t : B ′ → A′ Γ ` u : B ′

Γ ` t u : A′(→E)

by induction hypothesis we have B → A = B ′ → A′

and thus A = A′.

16

Page 44: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Uniqueness of typing

The fact that we used Church style is important here!

In Curry style, this is a small variant:

Γ ` x : Γ(x)(ax)

Γ, x : A ` t : B

Γ ` λx .t : A→ B(→I)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

but types are not unique anymore, e.g. λx .x has types

A→ A (A→ B)→ (A→ B) etc.

17

Page 45: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Uniqueness of typing

The fact that we used Church style is important here!

In Curry style, this is a small variant:

Γ ` x : Γ(x)(ax)

Γ, x : A ` t : B

Γ ` λx .t : A→ B(→I)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

but types are not unique anymore, e.g. λx .x has types

A→ A (A→ B)→ (A→ B) etc.

17

Page 46: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Uniqueness of typing

The fact that we used Church style is important here!

In Curry style, this is a small variant:

Γ ` x : Γ(x)(ax)

Γ, x : A ` t : B

Γ ` λx .t : A→ B(→I)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

but types are not unique anymore, e.g. λx .x has types

A→ A (A→ B)→ (A→ B) etc.

17

Page 47: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing problems

As a consequence the three following problems are decidable: given Γ and t,

• type checking: determine whether Γ ` t : A is derivable,

• typability: determine whether there exists an A such that Γ ` t : A is derivable,

• type inference: construct an A such that Γ ` t : A is derivable.

If a term admits a type, there is only one and we can compute it!

18

Page 48: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Type inference and checking

(** Types. *)type ty =

| TVar of string| Arr of ty * ty

(** Terms. *)type term =

| Var of string| App of term * term| Abs of string * ty * term

(** Environments. *)type context = (string * ty) list

19

Page 49: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Type inference and checking

exception Type_error

(** Type inference. *)let rec infer env = function

| Var x -> (try List.assoc x env with Not_found -> raise Type_error)| Abs (x, a, t) -> Arr (a, infer ((x,a)::env) t)| App (t, u) ->

match infer env t with| Arr (a, b) -> if infer env u <> a then raise Type_error; b| _ -> raise Type_error

Γ ` x : Γ(x)(ax)

Γ, x : A ` t : B

Γ ` λxA.t : A→ B(→I)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E) 20

Page 50: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Type inference and checking

(** Type checking. *)let check env t a =

if infer env t <> a then raise Type_error

(** Typability. *)let typable env t =

try let _ = infer env t in truewith Type_error -> false

21

Page 51: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Part III

The Curry-Howardcorrespondence

22

Page 52: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

The Curry-Howard correspondence is the observation that

• a type is the same as a formula in the implicative fragment of logic:

(A→ B)→ A→ B corresponds to (A⇒ B)⇒ A⇒ B

• a typing derivation for simply typed λ-calculus is the same as a proof in NJ(implicative fragment).

23

Page 53: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

typing logic

Γ, x : A, Γ′ ` x : A(ax)

Γ,A, Γ′ ` A(ax)

Γ, x : A ` t : B

Γ ` λxA.t : A→ B(→I)

Γ,A ` B

Γ ` A⇒ B(⇒I)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

Γ ` A⇒ B Γ ` A

Γ ` B(⇒E)

24

Page 54: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

The “term-erasing procedure” consists, starting from a typing derivation, in removingall the variables and terms (and replacing → by ⇒):

f : A→ B, x : A ` f : A→ B(ax)

f : A→ B, x : A ` x : A(ax)

f : A→ B, x : A ` fx : B(→E)

f : A→ B ` λxA.fx : A→ B(→I)

` λf A→B .λxA.fx : (A→ B)→ A→ B(→I)

LemmaGiven a typing derivation, its term-erasure is a valid proof in NJ.

Proof.Immediate induction.

25

Page 55: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

The “term-erasing procedure” consists, starting from a typing derivation, in removingall the variables and terms (and replacing → by ⇒):

f :

A⇒ B,

x :

A `

f :

A⇒ B(ax)

f :

A⇒ B,

x :

A `

x :

A(ax)

f :

A⇒ B,

x :

A `

fx :

B(⇒E)

f :

A⇒ B `

λxA.fx :

A⇒ B(⇒I)

`

λf A⇒B .λxA.fx :

(A⇒ B)⇒ A⇒ B(⇒I)

LemmaGiven a typing derivation, its term-erasure is a valid proof in NJ.

Proof.Immediate induction.

25

Page 56: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

The “term-erasing procedure” consists, starting from a typing derivation, in removingall the variables and terms (and replacing → by ⇒):

f :

A⇒ B,

x :

A `

f :

A⇒ B(ax)

f :

A⇒ B,

x :

A `

x :

A(ax)

f :

A⇒ B,

x :

A `

fx :

B(⇒E)

f :

A⇒ B `

λxA.fx :

A⇒ B(⇒I)

`

λf A⇒B .λxA.fx :

(A⇒ B)⇒ A⇒ B(⇒I)

LemmaGiven a typing derivation, its term-erasure is a valid proof in NJ.

Proof.Immediate induction.

25

Page 57: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

LemmaConversely, given a proof π of A1, . . . ,An ` A in NJ, we can construct a typingderivation of x1 : A1, . . . , xn : An ` t : A, for some term t, whose term-erasure is π.

Proof.By induction on π.

26

Page 58: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

LemmaConversely, given a proof π of A1, . . . ,An ` A in NJ, we can construct a typingderivation of x1 : A1, . . . , xn : An ` t : A, for some term t, whose term-erasure is π.

Proof.It the last rule is

Γ,A, Γ′ ` A(ax)

then we construct

Γ, x : A, Γ′ ` x : A(ax)

26

Page 59: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

LemmaConversely, given a proof π of A1, . . . ,An ` A in NJ, we can construct a typingderivation of x1 : A1, . . . , xn : An ` t : A, for some term t, whose term-erasure is π.

Proof.

If the last rule is

π

Γ ` A⇒ B

π′

Γ ` A

Γ ` B(⇒E)

then, by induction hypothesis, we have

...

Γ ` t : A→ Band

...

Γ ` u : A

and we construct

...

Γ ` t : A→ B

...

Γ ` u : A

Γ ` t u : B(→I).

26

Page 60: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

LemmaConversely, given a proof π of A1, . . . ,An ` A in NJ, we can construct a typingderivation of x1 : A1, . . . , xn : An ` t : A, for some term t, whose term-erasure is π.

Proof.

If the last rule is

π

Γ,A ` B

Γ ` A⇒ B(⇒I)

then by induction hypothesis we have

...

Γ, x : A ` t : B

and we construct

...

Γ, x : A ` t : B

Γ ` λxA.t : A→ B(→I).

26

Page 61: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In the previous proof we did not have any choice for the terms (up to α-conversion):

f :

A⇒ B,

x :

A `

f :

A⇒ B(ax)

f :

A⇒ B,

x :

A `

x :

A(ax)

f :

A⇒ B,

x :

A `

fx :

B(⇒E)

f :

A⇒ B `

λxA.fx :

A⇒ B(⇒I)

`

λf A⇒B .λxA.fx :

(A⇒ B)⇒ A⇒ B(⇒I)

27

Page 62: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In the previous proof we did not have any choice for the terms (up to α-conversion):

f :

A→ B,

x :

A `

f :

A→ B(ax)

f :

A→ B,

x :

A `

x :

A(ax)

f :

A→ B,

x :

A `

fx :

B(→E)

f :

A→ B `

λxA.fx :

A→ B(→I)

`

λf A→B .λxA.fx :

(A→ B)→ A→ B(→I)

27

Page 63: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In the previous proof we did not have any choice for the terms (up to α-conversion):

f : A→ B,

x :

A `

f :

A→ B(ax)

f : A→ B,

x :

A `

x :

A(ax)

f : A→ B,

x :

A `

fx :

B(→E)

f : A→ B `

λxA.fx :

A→ B(→I)

`

λf A→B .λxA.fx :

(A→ B)→ A→ B(→I)

27

Page 64: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In the previous proof we did not have any choice for the terms (up to α-conversion):

f : A→ B, x : A `

f :

A→ B(ax)

f : A→ B, x : A `

x :

A(ax)

f : A→ B, x : A `

fx :

B(→E)

f : A→ B `

λxA.fx :

A→ B(→I)

`

λf A→B .λxA.fx :

(A→ B)→ A→ B(→I)

27

Page 65: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In the previous proof we did not have any choice for the terms (up to α-conversion):

f : A→ B, x : A ` f : A→ B(ax)

f : A→ B, x : A `

x :

A(ax)

f : A→ B, x : A `

fx :

B(→E)

f : A→ B `

λxA.fx :

A→ B(→I)

`

λf A→B .λxA.fx :

(A→ B)→ A→ B(→I)

27

Page 66: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In the previous proof we did not have any choice for the terms (up to α-conversion):

f : A→ B, x : A ` f : A→ B(ax)

f : A→ B, x : A ` x : A(ax)

f : A→ B, x : A `

fx :

B(→E)

f : A→ B `

λxA.fx :

A→ B(→I)

`

λf A→B .λxA.fx :

(A→ B)→ A→ B(→I)

27

Page 67: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In the previous proof we did not have any choice for the terms (up to α-conversion):

f : A→ B, x : A ` f : A→ B(ax)

f : A→ B, x : A ` x : A(ax)

f : A→ B, x : A ` fx : B(→E)

f : A→ B `

λxA.fx :

A→ B(→I)

`

λf A→B .λxA.fx :

(A→ B)→ A→ B(→I)

27

Page 68: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In the previous proof we did not have any choice for the terms (up to α-conversion):

f : A→ B, x : A ` f : A→ B(ax)

f : A→ B, x : A ` x : A(ax)

f : A→ B, x : A ` fx : B(→E)

f : A→ B ` λxA.fx : A→ B(→I)

`

λf A→B .λxA.fx :

(A→ B)→ A→ B(→I)

27

Page 69: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In the previous proof we did not have any choice for the terms (up to α-conversion):

f : A→ B, x : A ` f : A→ B(ax)

f : A→ B, x : A ` x : A(ax)

f : A→ B, x : A ` fx : B(→E)

f : A→ B ` λxA.fx : A→ B(→I)

` λf A→B .λxA.fx : (A→ B)→ A→ B(→I)

27

Page 70: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

TheoremThere is a bijection between

• typable λ-terms (up to α-conversion),

• typing derivations of λ-terms,

• proofs in the implicative fragment of NJ.

In other words,

PROGRAM = PROOF

28

Page 71: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

TheoremThere is a bijection between

• typable λ-terms (up to α-conversion),

• typing derivations of λ-terms,

• proofs in the implicative fragment of NJ.

In other words,

PROGRAM = PROOF

28

Page 72: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In particular, λ-terms can be considered as proof witnesses:

— you: Hey, the formula A is true!

— me: Why should I believe you?

— you: Here is a term t witnessing for that.

— me: Let me typecheck that...

— me: Ok, now I believe you!

29

Page 73: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In particular, λ-terms can be considered as proof witnesses:

— you: Hey, the formula A is true!

— me: Why should I believe you?

— you: Here is a term t witnessing for that.

— me: Let me typecheck that...

— me: Ok, now I believe you!

29

Page 74: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In particular, λ-terms can be considered as proof witnesses:

— you: Hey, the formula A is true!

— me: Why should I believe you?

— you: Here is a term t witnessing for that.

— me: Let me typecheck that...

— me: Ok, now I believe you!

29

Page 75: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In particular, λ-terms can be considered as proof witnesses:

— you: Hey, the formula A is true!

— me: Why should I believe you?

— you: Here is a term t witnessing for that.

— me: Let me typecheck that...

— me: Ok, now I believe you!

29

Page 76: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

In particular, λ-terms can be considered as proof witnesses:

— you: Hey, the formula A is true!

— me: Why should I believe you?

— you: Here is a term t witnessing for that.

— me: Let me typecheck that...

— me: Ok, now I believe you!

29

Page 77: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Part IV

Other connectives

30

Page 78: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Other connectives

The correspondence extends to other logical connectives too!

The general idea is that for each connective we can introduce in λ-calculus

• constructions to create a value of this type (= introduction rules)

• constructions to use a value of this type (= elimination rules)

with appropriate reduction rules (= cut elimination).

31

Page 79: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Products

We extend the syntax of λ-terms with

t, u ::= . . . | 〈t, u〉 | πl(t) | πr(t)

together with the reduction rules

πl (〈t, u〉) −→ t πr (〈t, u〉) −→ u

32

Page 80: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Products

We extend the syntax of λ-terms with

t, u ::= . . . | 〈t, u〉 | πl(t) | πr(t)

together with the reduction rules

πl (〈t, u〉) −→ t πr (〈t, u〉) −→ u

32

Page 81: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Products

We extend the syntax of λ-terms with

t, u ::= . . . | 〈t, u〉 | πl(t) | πr(t)

together with the reduction rules

πl (〈t, u〉) −→ t πr (〈t, u〉) −→ u

32

Page 82: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Products

(** Terms. *)type term =

| Var of string| App of term * term| Abs of string * ty * term

| Pair of term * term| PrjL of term| PrjR of term

33

Page 83: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Products

(** Terms. *)type term =

| Var of string| App of term * term| Abs of string * ty * term

| Pair of term * term| PrjL of term| PrjR of term

33

Page 84: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Products

We extends the syntax of types with

A,B ::= . . . | A× B

and add the typing rules

Γ ` t : A Γ ` u : B

Γ ` 〈t, u〉 : A× B(×I)

Γ ` t : A× B

Γ ` πl(t) : A(×l

E)Γ ` t : A× B

Γ ` πr(t) : B(×r

E)

34

Page 85: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Products

We extends the syntax of types with

A,B ::= . . . | A× B

and add the typing rulesΓ ` t : A Γ ` u : B

Γ ` 〈t, u〉 : A× B(×I)

Γ ` t : A× B

Γ ` πl(t) : A(×l

E)Γ ` t : A× B

Γ ` πr(t) : B(×r

E)

34

Page 86: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Products

We extends the syntax of types with

A,B ::= . . . | A× B

and add the typing rulesΓ `

t :

A Γ `

u :

B

Γ `

〈t, u〉 :

A ∧ B(∧I)

Γ `

t :

A ∧ B

Γ `

πl(t) :

A(∧l

E)Γ `

t :

A ∧ B

Γ `

πr(t) :

B(∧r

E)

34

Page 87: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Unit

We extend the syntax of λ-terms with

t ::= . . . | 〈〉

(no reduction rule), extend the syntax of types with

A ::= . . . | 1

and add the typing rule

Γ ` 〈〉 : 1(1I)

35

Page 88: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Unit

We extend the syntax of λ-terms with

t ::= . . . | 〈〉

(no reduction rule), extend the syntax of types with

A ::= . . . | 1

and add the typing rule

Γ ` 〈〉 : 1(1I)

35

Page 89: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Unit

We extend the syntax of λ-terms with

t ::= . . . | 〈〉

(no reduction rule), extend the syntax of types with

A ::= . . . | 1

and add the typing rule

Γ ` 〈〉 : 1(1I)

35

Page 90: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Unit

We extend the syntax of λ-terms with

t ::= . . . | 〈〉

(no reduction rule), extend the syntax of types with

A ::= . . . | 1

and add the typing rule

Γ `

〈〉 :

>(>I)

35

Page 91: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

We now want to add coproducts types A + B , which corresponds to the formula A ∨ B .

Recall that in OCaml the corresponding type is implemented with

type ('a,'b) coprod =| Left of 'a| Right of 'b

and a typical program using those is of the form

match t with| Left x -> u| Right y -> v

36

Page 92: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

We now want to add coproducts types A + B , which corresponds to the formula A ∨ B .

Recall that in OCaml the corresponding type is implemented with

type ('a,'b) coprod =| Left of 'a| Right of 'b

and a typical program using those is of the form

match t with| Left x -> u| Right y -> v

36

Page 93: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

We now want to add coproducts types A + B , which corresponds to the formula A ∨ B .

Recall that in OCaml the corresponding type is implemented with

type ('a,'b) coprod =| Left of 'a| Right of 'b

and a typical program using those is of the form

match t with| Left x -> u| Right y -> v

36

Page 94: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

If we do not want to use matching, we can program once for all the function

let case t u v =match t with| Left x -> u x| Right y -> v y

37

Page 95: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

We extend the syntax of λ-terms with

t ::= . . . | ιl(t) | ιr(u) | case(t, x 7→ u, y 7→ v)

together with the reduction rules

case(ιl(t), x 7→ u, y 7→ v) −→ u[t/x ]

case(ιr(t), x 7→ u, y 7→ v) −→ v [t/y ]

38

Page 96: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

We extend the syntax of λ-terms with

t ::= . . . | ιl(t) | ιr(u) | case(t, x 7→ u, y 7→ v)

together with the reduction rules

case(ιl(t), x 7→ u, y 7→ v) −→ u[t/x ]

case(ιr(t), x 7→ u, y 7→ v) −→ v [t/y ]

38

Page 97: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

We extend the syntax of λ-terms with

t ::= . . . | ιl(t) | ιr(u) | case(t, x 7→ u, y 7→ v)

together with the reduction rules

case(ιl(t), x 7→ u, y 7→ v) −→ u[t/x ]

case(ιr(t), x 7→ u, y 7→ v) −→ v [t/y ]

38

Page 98: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

The reduction rules thus say that

match Left t with| Left x -> u| Right y -> v

reduces tou[t/x]

and similarly for Right.

39

Page 99: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

We extend the syntax of types with

A,B ::= . . . | A + B

and add the typing rules

Γ ` t : A + B Γ, x : A ` u : C Γ, y : B ` v : C

Γ ` case(t, x 7→ u, y 7→ v) : C(+E)

Γ ` t : A

Γ ` ιl(t) : A + B(+l

I)Γ ` B

Γ ` ιr(t) : A + B(+r

I)

40

Page 100: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

We extend the syntax of types with

A,B ::= . . . | A + B

and add the typing rules

Γ ` t : A + B Γ, x : A ` u : C Γ, y : B ` v : C

Γ ` case(t, x 7→ u, y 7→ v) : C(+E)

Γ ` t : A

Γ ` ιl(t) : A + B(+l

I)Γ ` B

Γ ` ιr(t) : A + B(+r

I)

40

Page 101: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

We extend the syntax of types with

A,B ::= . . . | A + B

and add the typing rules

Γ `

t :

A ∨ B Γ,

x :

A `

u :

C Γ,

y :

B `

v :

C

Γ `

case(t, x 7→ u, y 7→ v) :

C(∨E)

Γ `

t :

A

Γ `

ιl(t) :

A ∨ B(∨l

I)Γ ` B

Γ `

ιr(t) :

A ∨ B(∨r

I)

40

Page 102: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

Note that in OCaml, the type of our function

let case t u v =match t with| Left x -> u x| Right y -> v y

is

('a, 'b) coprod -> ('a -> 'c) -> ('b -> 'c) -> 'c

which can be read logically as

(A ∨ B)⇒ (A⇒ C )⇒ (B ⇒ C )⇒ C

41

Page 103: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

There is a slight problem: what’s wrong if we try to perform type inference?

Γ ` t : A

Γ ` ιl(t) : A + B(+l

I)Γ ` t : B

Γ ` ιr(t) : A + B(+r

I)

For instance, what is the type of ιl(λxA.x)?

It is like Church vs Curry, we need more typing information:

Γ ` t : A

Γ ` ιBl (t) : A + B(+l

I)Γ ` t : B

Γ ` ιAr (t) : A + B(+r

I)

42

Page 104: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

There is a slight problem: what’s wrong if we try to perform type inference?

Γ ` t : A

Γ ` ιl(t) : A + B(+l

I)Γ ` t : B

Γ ` ιr(t) : A + B(+r

I)

For instance, what is the type of ιl(λxA.x)?

It is like Church vs Curry, we need more typing information:

Γ ` t : A

Γ ` ιBl (t) : A + B(+l

I)Γ ` t : B

Γ ` ιAr (t) : A + B(+r

I)

42

Page 105: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

There is a slight problem: what’s wrong if we try to perform type inference?

Γ ` t : A

Γ ` ιl(t) : A + B(+l

I)Γ ` t : B

Γ ` ιr(t) : A + B(+r

I)

For instance, what is the type of ιl(λxA.x)?

It is like Church vs Curry, we need more typing information:

Γ ` t : A

Γ ` ιBl (t) : A + B(+l

I)Γ ` t : B

Γ ` ιAr (t) : A + B(+r

I)

42

Page 106: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

Note that a termcase(t, x 7→ u, y 7→ v)

should be considered up to α-conversion (we can rename x and y), which means extracare when implementing substitution.

Instead, it is often easier to implement the variant with actual functions

case(t, u, v)

which is typed as

Γ ` t : A + ∨B Γ ` u : A→ C Γ ` v : B → C

Γ ` case(t, u, v) : C(+E)

instead of

Γ ` t : A + ∨B Γ, x : A ` u : C Γ, y : B ` v : C

Γ ` case(t, x 7→ u, y 7→ v) : C(+E)

43

Page 107: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

Note that a termcase(t, x 7→ u, y 7→ v)

should be considered up to α-conversion (we can rename x and y), which means extracare when implementing substitution.

Instead, it is often easier to implement the variant with actual functions

case(t, u, v)

which is typed as

Γ ` t : A + ∨B Γ ` u : A→ C Γ ` v : B → C

Γ ` case(t, u, v) : C(+E)

instead of

Γ ` t : A + ∨B Γ, x : A ` u : C Γ, y : B ` v : C

Γ ` case(t, x 7→ u, y 7→ v) : C(+E)

43

Page 108: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

Note that a termcase(t, x 7→ u, y 7→ v)

should be considered up to α-conversion (we can rename x and y), which means extracare when implementing substitution.

Instead, it is often easier to implement the variant with actual functions

case(t, u, v)

which is typed as

Γ ` t : A + ∨B Γ ` u : A→ C Γ ` v : B → C

Γ ` case(t, u, v) : C(+E)

instead of

Γ ` t : A + ∨B Γ, x : A ` u : C Γ, y : B ` v : C

Γ ` case(t, x 7→ u, y 7→ v) : C(+E)

43

Page 109: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Coproducts

Note that a termcase(t, x 7→ u, y 7→ v)

should be considered up to α-conversion (we can rename x and y), which means extracare when implementing substitution.

Instead, it is often easier to implement the variant with actual functions

case(t, u, v)

which is typed as

Γ ` t : A + ∨B Γ ` u : A→ C Γ ` v : B → C

Γ ` case(t, u, v) : C(+E)

instead of

Γ ` t : A + ∨B Γ, x : A ` u : C Γ, y : B ` v : C

Γ ` case(t, x 7→ u, y 7→ v) : C(+E) 43

Page 110: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Empty type

We extend the syntax of λ-terms with

t ::= . . . | case(t)

(no reduction rule), extend the syntax of types with

A ::= . . . | 0

and add the typing rule

Γ ` t : 0

Γ ` case(t) : A(0E)

44

Page 111: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Empty type

We extend the syntax of λ-terms with

t ::= . . . | case(t)

(no reduction rule), extend the syntax of types with

A ::= . . . | 0

and add the typing ruleΓ ` t : 0

Γ ` case(t) : A(0E)

44

Page 112: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Empty type

We extend the syntax of λ-terms with

t ::= . . . | case(t)

(no reduction rule), extend the syntax of types with

A ::= . . . | 0

and add the typing ruleΓ `

t :

⊥Γ `

case(t) :

A(⊥E)

44

Page 113: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

All together

If we add them all together, we want more reduction rules:

caseA→B(t) u −→ caseB(t)

πl(caseA×B(t)) −→ caseA(t)

πr(caseA×B(t)) −→ caseB(t)

case(caseA+B(t), x 7→ u, y 7→ v) −→ caseC (t)

caseA(case0(t)) −→ caseA(t)

case(t, x 7→ u, y 7→ v)w −→ case(t, x 7→ uw , y 7→ vw)

πl(case(t, x 7→ u, y 7→ v)) −→ case(t, x 7→ πl(u), y 7→ πl(v))

πr(case(t, x 7→ u, y 7→ v)) −→ case(t, x 7→ πr(u), y 7→ πr(v))

caseC (case(t, x 7→ u, y 7→ v)) −→ case(t, x 7→ caseC (u), y 7→ caseC (v))

case(case(t, x 7→ u, y 7→ v), x ′ 7→ u′, y ′ 7→ v ′) −→ case(t, x 7→ case(u, x ′ 7→ u′, y ′ 7→ v ′), y 7→ case(v , x ′ 7→ u′, y ′ 7→ v ′))45

Page 114: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Natural numbers

In OCaml, natural numbers can be defined as

type nat =| Zero| Succ of nat

So that factorial can be implemented with

let rec fact n =match n with| Zero -> Succ Zero| Succ n -> mult (Succ n) (fact n)

46

Page 115: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Natural numbers

In OCaml, natural numbers can be defined as

type nat =| Zero| Succ of nat

So that factorial can be implemented with

let rec fact n =match n with| Zero -> Succ Zero| Succ n -> mult (Succ n) (fact n)

46

Page 116: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Natural numbers

The “recurrence principle” / eliminator can then be defined as

let rec recursor n z s =match n with| Zero -> z| Succ n -> s n (recursor n z s)

of type

val recursor : nat -> 'a -> (nat -> 'a -> 'a) -> 'a = <fun>

So that

let fact n =recursor n (Succ Zero) (fun n f -> mult (Succ n) f)

47

Page 117: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Natural numbers

The “recurrence principle” / eliminator can then be defined as

let rec recursor n z s =match n with| Zero -> z| Succ n -> s n (recursor n z s)

of type

val recursor : nat -> 'a -> (nat -> 'a -> 'a) -> 'a = <fun>

So that

let fact n =recursor n (Succ Zero) (fun n f -> mult (Succ n) f)

47

Page 118: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Natural numbers

We extend the syntax of λ-terms with

t ::= . . . | Z | S(t) | rec(t, u, xy 7→ v)

and add the reduction rules

rec(Z, z , xy 7→ s) −→ z

rec(S(n), z , xy 7→ s) −→ s[n/x , rec(n, z , xy 7→ s)/y ]

48

Page 119: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Natural numbers

We extend the syntax of types with

A,B ::= . . . | Nat

and add the typing rules

Γ ` Z : Nat

Γ ` t : Nat

Γ ` S(t) : Nat

Γ ` n : Nat Γ ` z : A Γ, x : Nat, y : A ` s : A

Γ ` rec(n, z , xy 7→ s) : A

49

Page 120: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Natural numbers

We extend the syntax of types with

A,B ::= . . . | Nat

and add the typing rules

Γ `

Z :

Nat

Γ `

t :

Nat

Γ `

S(t) :

Nat

Γ `

n :

Nat Γ `

z :

A Γ,

x :

Nat,

y :

A `

s :

A

Γ `

rec(n, z , xy 7→ s) :

A

49

Page 121: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

For simplicity we get back to arrow types onlybut the following theorems generalize to additional type constructors.

50

Page 122: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Part V

Dynamics of Curry-Howard

51

Page 123: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination

Remember that a cut in a proof is an elimination rule whose principal premise is anintroduction rule.

π

Γ ` A

π′

Γ ` B

Γ ` A ∧ B(∧I)

Γ ` A(∧l

E)

π

Γ,A ` B

Γ ` A⇒ B(⇒I)

π′

Γ ` A

Γ ` B(⇒E)

Such a proof is intuitively doing “useless work” and we have seen that we couldgradually remove all the cuts from a proof.

52

Page 124: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: conjunction

For instance, the cuts related to conjunction can be eliminated with

π

Γ ` A

π′

Γ ` B

Γ ` A ∧ B(∧I)

Γ ` A(∧l

E)

π

Γ ` A

π

Γ ` A

π′

Γ ` B

Γ ` A ∧ B(∧I)

Γ ` B(∧r

E)

π′

Γ ` B

What it the computational contents of this transformation?

53

Page 125: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: conjunction

For instance, the cuts related to conjunction can be eliminated with

π

Γ ` A

π′

Γ ` B

Γ ` A ∧ B(∧I)

Γ ` A(∧l

E) π

Γ ` A

π

Γ ` A

π′

Γ ` B

Γ ` A ∧ B(∧I)

Γ ` B(∧r

E) π′

Γ ` B

What it the computational contents of this transformation?

53

Page 126: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: conjunction

For instance, the cuts related to conjunction can be eliminated with

π

Γ ` A

π′

Γ ` B

Γ ` A ∧ B(∧I)

Γ ` A(∧l

E) π

Γ ` A

π

Γ ` A

π′

Γ ` B

Γ ` A ∧ B(∧I)

Γ ` B(∧r

E) π′

Γ ` B

What it the computational contents of this transformation? 53

Page 127: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: conjunction

One of the cut-elimination rules is

π

Γ `

t :

A

π′

Γ `

u :

B

Γ `

〈t, u〉 :

A ∧ B(∧I)

Γ `

πl(〈t, u〉) :

A(∧l

E) π

Γ `

t :

A

In other words, it transforms a subterm

πl(〈t, u〉) −→β t

which is the reduction rule!

54

Page 128: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: conjunction

One of the cut-elimination rules is

π

Γ ` t : A

π′

Γ `

u :

B

Γ `

〈t, u〉 :

A ∧ B(∧I)

Γ `

πl(〈t, u〉) :

A(∧l

E) π

Γ `

t :

A

In other words, it transforms a subterm

πl(〈t, u〉) −→β t

which is the reduction rule!

54

Page 129: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: conjunction

One of the cut-elimination rules is

π

Γ ` t : A

π′

Γ ` u : B

Γ `

〈t, u〉 :

A ∧ B(∧I)

Γ `

πl(〈t, u〉) :

A(∧l

E) π

Γ `

t :

A

In other words, it transforms a subterm

πl(〈t, u〉) −→β t

which is the reduction rule!

54

Page 130: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: conjunction

One of the cut-elimination rules is

π

Γ ` t : A

π′

Γ ` u : B

Γ ` 〈t, u〉 : A ∧ B(∧I)

Γ `

πl(〈t, u〉) :

A(∧l

E) π

Γ `

t :

A

In other words, it transforms a subterm

πl(〈t, u〉) −→β t

which is the reduction rule!

54

Page 131: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: conjunction

One of the cut-elimination rules is

π

Γ ` t : A

π′

Γ ` u : B

Γ ` 〈t, u〉 : A ∧ B(∧I)

Γ ` πl(〈t, u〉) : A(∧l

E) π

Γ `

t :

A

In other words, it transforms a subterm

πl(〈t, u〉) −→β t

which is the reduction rule!

54

Page 132: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: conjunction

One of the cut-elimination rules is

π

Γ ` t : A

π′

Γ ` u : B

Γ ` 〈t, u〉 : A ∧ B(∧I)

Γ ` πl(〈t, u〉) : A(∧l

E) π

Γ ` t : A

In other words, it transforms a subterm

πl(〈t, u〉) −→β t

which is the reduction rule!

54

Page 133: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: conjunction

One of the cut-elimination rules is

π

Γ ` t : A

π′

Γ ` u : B

Γ ` 〈t, u〉 : A ∧ B(∧I)

Γ ` πl(〈t, u〉) : A(∧l

E) π

Γ ` t : A

In other words, it transforms a subterm

πl(〈t, u〉) −→β t

which is the reduction rule!

54

Page 134: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

The cut elimination rule for ⇒ is

π

Γ,A ` B

Γ ` A⇒ B(⇒I)

π′

Γ ` A

Γ ` B(⇒E)

π[π′/A]

Γ ` B

where π[π′/A] is π where we have replaced all axioms on A

Γ,A, Γ′ ` A(ax) by

w(π′)

Γ,A, Γ′ ` A

where w(π′) is an appropriate weakening of π.

55

Page 135: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

The cut elimination rule for ⇒ is

π

Γ,A ` B

Γ ` A⇒ B(⇒I)

π′

Γ ` A

Γ ` B(⇒E)

π[π′/A]

Γ ` B

where π[π′/A] is π where we have replaced all axioms on A

Γ,A, Γ′ ` A(ax) by

w(π′)

Γ,A, Γ′ ` A

where w(π′) is an appropriate weakening of π.

55

Page 136: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

For instance, we can eliminate the cut

Γ,A ` A(ax)

Γ ` A⇒ A(⇒I)

π

Γ ` A

Γ ` A(⇒E)

π

Γ ` A

In other words,(λxA.x)t −→β t

56

Page 137: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

For instance, we can eliminate the cut

Γ,A ` A(ax)

Γ ` A⇒ A(⇒I)

π

Γ ` A

Γ ` A(⇒E)

π

Γ ` A

In other words,(λxA.x)t −→β t

56

Page 138: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

For instance, we can eliminate the cut

Γ, x : A `

x :

A(ax)

Γ `

λxA.x :

A⇒ A(⇒I)

π

Γ `

t :

A

Γ `

(λxA.x)t :

A(⇒E)

π

Γ `

t :

A

In other words,(λxA.x)t −→β t

56

Page 139: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

For instance, we can eliminate the cut

Γ, x : A ` x : A(ax)

Γ `

λxA.x :

A⇒ A(⇒I)

π

Γ `

t :

A

Γ `

(λxA.x)t :

A(⇒E)

π

Γ `

t :

A

In other words,(λxA.x)t −→β t

56

Page 140: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

For instance, we can eliminate the cut

Γ, x : A ` x : A(ax)

Γ ` λxA.x : A⇒ A(⇒I)

π

Γ `

t :

A

Γ `

(λxA.x)t :

A(⇒E)

π

Γ `

t :

A

In other words,(λxA.x)t −→β t

56

Page 141: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

For instance, we can eliminate the cut

Γ, x : A ` x : A(ax)

Γ ` λxA.x : A⇒ A(⇒I)

π

Γ ` t : A

Γ `

(λxA.x)t :

A(⇒E)

π

Γ `

t :

A

In other words,(λxA.x)t −→β t

56

Page 142: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

For instance, we can eliminate the cut

Γ, x : A ` x : A(ax)

Γ ` λxA.x : A⇒ A(⇒I)

π

Γ ` t : A

Γ ` (λxA.x)t : A(⇒E)

π

Γ `

t :

A

In other words,(λxA.x)t −→β t

56

Page 143: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

For instance, we can eliminate the cut

Γ, x : A ` x : A(ax)

Γ ` λxA.x : A⇒ A(⇒I)

π

Γ ` t : A

Γ ` (λxA.x)t : A(⇒E)

π

Γ ` t : A

In other words,(λxA.x)t −→β t

56

Page 144: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

For instance, we can eliminate the cut

Γ, x : A ` x : A(ax)

Γ ` λxA.x : A⇒ A(⇒I)

π

Γ ` t : A

Γ ` (λxA.x)t : A(⇒E)

π

Γ ` t : A

In other words,(λxA.x)t −→β t

56

Page 145: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

More generally, we have

π

Γ, x : A `

t :

B

Γ `

λxA.t :

A⇒ B(⇒I)

π′

Γ `

u :

A

Γ `

(λxA.t)u :

B(⇒E)

π[π′/A]

Γ `

t[u/x ] :

B

In other words,(λxA.t)u −→β t[u/x ]

57

Page 146: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

More generally, we have

π

Γ, x : A ` t : B

Γ `

λxA.t :

A⇒ B(⇒I)

π′

Γ `

u :

A

Γ `

(λxA.t)u :

B(⇒E)

π[π′/A]

Γ `

t[u/x ] :

B

In other words,(λxA.t)u −→β t[u/x ]

57

Page 147: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

More generally, we have

π

Γ, x : A ` t : B

Γ ` λxA.t : A⇒ B(⇒I)

π′

Γ `

u :

A

Γ `

(λxA.t)u :

B(⇒E)

π[π′/A]

Γ `

t[u/x ] :

B

In other words,(λxA.t)u −→β t[u/x ]

57

Page 148: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

More generally, we have

π

Γ, x : A ` t : B

Γ ` λxA.t : A⇒ B(⇒I)

π′

Γ ` u : A

Γ `

(λxA.t)u :

B(⇒E)

π[π′/A]

Γ `

t[u/x ] :

B

In other words,(λxA.t)u −→β t[u/x ]

57

Page 149: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

More generally, we have

π

Γ, x : A ` t : B

Γ ` λxA.t : A⇒ B(⇒I)

π′

Γ ` u : A

Γ ` (λxA.t)u : B(⇒E)

π[π′/A]

Γ `

t[u/x ] :

B

In other words,(λxA.t)u −→β t[u/x ]

57

Page 150: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

More generally, we have

π

Γ, x : A ` t : B

Γ ` λxA.t : A⇒ B(⇒I)

π′

Γ ` u : A

Γ ` (λxA.t)u : B(⇒E)

π[π′/A]

Γ ` t[u/x ] : B

In other words,(λxA.t)u −→β t[u/x ]

57

Page 151: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination: implication

More generally, we have

π

Γ, x : A ` t : B

Γ ` λxA.t : A⇒ B(⇒I)

π′

Γ ` u : A

Γ ` (λxA.t)u : B(⇒E)

π[π′/A]

Γ ` t[u/x ] : B

In other words,(λxA.t)u −→β t[u/x ]

57

Page 152: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination and reduction

Suppose given a term t of type A in a context Γ, its typing derivation

π

Γ ` t : A

can be seen as a proof in NJ. We have shown that

TheoremThe cut elimination steps of π are in correspondence with the β-reduction steps of t.

This means that for every β-reduction t −→β t ′ there is a derivation π′ of Γ ` t ′ : A

π

Γ ` t : A

π′

Γ ` t ′ : A

which is obtained by a cut elimination step from π, and conversely every cut-eliminationstep from π is of this form.

58

Page 153: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Cut elimination and reduction

Suppose given a term t of type A in a context Γ, its typing derivation

π

Γ ` t : A

can be seen as a proof in NJ. We have shown that

TheoremThe cut elimination steps of π are in correspondence with the β-reduction steps of t.

This means that for every β-reduction t −→β t ′ there is a derivation π′ of Γ ` t ′ : A

π

Γ ` t : A

π′

Γ ` t ′ : A

which is obtained by a cut elimination step from π, and conversely every cut-eliminationstep from π is of this form.

58

Page 154: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Subject reduction

In particular, we have show the subject reduction property:typing is compatible with β-reduction.

TheoremIf Γ ` t : A is derivable and t −→β t ′ then Γ ` t ′ : A is also derivable.

For instance,

Γ, x : A ` x : A(ax)

Γ ` λxA.x : A→ A(→I)

π

Γ ` t : B

` (λx .Ax)t : B(→E)

π

Γ ` t : B

59

Page 155: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Subject reduction

In particular, we have show the subject reduction property:typing is compatible with β-reduction.

TheoremIf Γ ` t : A is derivable and t −→β t ′ then Γ ` t ′ : A is also derivable.

For instance,

Γ, x : A ` x : A(ax)

Γ ` λxA.x : A→ A(→I)

π

Γ ` t : B

` (λx .Ax)t : B(→E)

π

Γ ` t : B

59

Page 156: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

We can add a third level to the correspondence:

TheoremThere is a bijection between

1. types and formulas,

2. λ-terms of type A and proofs of A in NJ,

3. reduction steps and cut elimination steps.

Using a function in programming is the same as using a lemma in mathematics!

60

Page 157: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

We can add a third level to the correspondence:

TheoremThere is a bijection between

1. types and formulas,

2. λ-terms of type A and proofs of A in NJ,

3. reduction steps and cut elimination steps.

Using a function in programming is the same as using a lemma in mathematics!

60

Page 158: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Curry-Howard correspondence

We can add a third level to the correspondence:

TheoremThere is a bijection between

1. types and formulas,

2. λ-terms of type A and proofs of A in NJ,

3. reduction steps and cut elimination steps.

Using a function in programming is the same as using a lemma in mathematics!

60

Page 159: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

A cut is an elimination of an introduction of some connective.What if we try to do the converse?

For implication,

π

Γ ` A⇒ B

Γ,A ` A⇒ B(wk)

Γ,A ` A(ax)

Γ,A ` B(⇒E)

Γ ` A⇒ B(⇒I)

π

Γ ` A⇒ B

In other words, we recover η-reduction:

λxA.tx −→η t

61

Page 160: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

A cut is an elimination of an introduction of some connective.What if we try to do the converse?

For implication,

π

Γ ` A⇒ B

Γ,A ` A⇒ B(wk)

Γ,A ` A(ax)

Γ,A ` B(⇒E)

Γ ` A⇒ B(⇒I)

π

Γ ` A⇒ B

In other words, we recover η-reduction:

λxA.tx −→η t

61

Page 161: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

A cut is an elimination of an introduction of some connective.What if we try to do the converse?

For implication,

π

Γ `

t :

A⇒ B

Γ, x : A `

t :

A⇒ B(wk)

Γ, x : A `

x :

A(ax)

Γ, x : A `

tx :

B(⇒E)

Γ `

λxA.tx :

A⇒ B(⇒I)

π

Γ `

t :

A⇒ B

In other words, we recover η-reduction:

λxA.tx −→η t

61

Page 162: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

A cut is an elimination of an introduction of some connective.What if we try to do the converse?

For implication,

π

Γ ` t : A⇒ B

Γ, x : A `

t :

A⇒ B(wk)

Γ, x : A `

x :

A(ax)

Γ, x : A `

tx :

B(⇒E)

Γ `

λxA.tx :

A⇒ B(⇒I)

π

Γ `

t :

A⇒ B

In other words, we recover η-reduction:

λxA.tx −→η t

61

Page 163: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

A cut is an elimination of an introduction of some connective.What if we try to do the converse?

For implication,

π

Γ ` t : A⇒ B

Γ, x : A ` t : A⇒ B(wk)

Γ, x : A `

x :

A(ax)

Γ, x : A `

tx :

B(⇒E)

Γ `

λxA.tx :

A⇒ B(⇒I)

π

Γ `

t :

A⇒ B

In other words, we recover η-reduction:

λxA.tx −→η t

61

Page 164: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

A cut is an elimination of an introduction of some connective.What if we try to do the converse?

For implication,

π

Γ ` t : A⇒ B

Γ, x : A ` t : A⇒ B(wk)

Γ, x : A ` x : A(ax)

Γ, x : A `

tx :

B(⇒E)

Γ `

λxA.tx :

A⇒ B(⇒I)

π

Γ `

t :

A⇒ B

In other words, we recover η-reduction:

λxA.tx −→η t

61

Page 165: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

A cut is an elimination of an introduction of some connective.What if we try to do the converse?

For implication,

π

Γ ` t : A⇒ B

Γ, x : A ` t : A⇒ B(wk)

Γ, x : A ` x : A(ax)

Γ, x : A ` tx : B(⇒E)

Γ `

λxA.tx :

A⇒ B(⇒I)

π

Γ `

t :

A⇒ B

In other words, we recover η-reduction:

λxA.tx −→η t

61

Page 166: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

A cut is an elimination of an introduction of some connective.What if we try to do the converse?

For implication,

π

Γ ` t : A⇒ B

Γ, x : A ` t : A⇒ B(wk)

Γ, x : A ` x : A(ax)

Γ, x : A ` tx : B(⇒E)

Γ ` λxA.tx : A⇒ B(⇒I)

π

Γ `

t :

A⇒ B

In other words, we recover η-reduction:

λxA.tx −→η t

61

Page 167: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

A cut is an elimination of an introduction of some connective.What if we try to do the converse?

For implication,

π

Γ ` t : A⇒ B

Γ, x : A ` t : A⇒ B(wk)

Γ, x : A ` x : A(ax)

Γ, x : A ` tx : B(⇒E)

Γ ` λxA.tx : A⇒ B(⇒I)

π

Γ ` t : A⇒ B

In other words, we recover η-reduction:

λxA.tx −→η t

61

Page 168: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

A cut is an elimination of an introduction of some connective.What if we try to do the converse?

For implication,

π

Γ ` t : A⇒ B

Γ, x : A ` t : A⇒ B(wk)

Γ, x : A ` x : A(ax)

Γ, x : A ` tx : B(⇒E)

Γ ` λxA.tx : A⇒ B(⇒I)

π

Γ ` t : A⇒ B

In other words, we recover η-reduction:

λxA.tx −→η t61

Page 169: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

This also works for other connectives:

π

Γ `

t :

A ∧ B

Γ `

πl(t) :

A(∧l

E)

π

Γ `

t :

A ∧ B

Γ `

πr(t) :

A(∧r

E)

Γ `

〈πl(t), πr(t)〉 :

A ∧ B(∧I)

π

Γ `

t :

A ∧ B

In other words, the η-reduction rule for products is

〈πl(t), πr(t)〉 −→η t

62

Page 170: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

This also works for other connectives:

π

Γ ` t : A ∧ B

Γ ` πl(t) : A(∧l

E)

π

Γ ` t : A ∧ B

Γ ` πr(t) : A(∧r

E)

Γ ` 〈πl(t), πr(t)〉 : A ∧ B(∧I)

π

Γ ` t : A ∧ B

In other words, the η-reduction rule for products is

〈πl(t), πr(t)〉 −→η t

62

Page 171: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

A variant of cuts

This also works for other connectives:

π

Γ ` t : A ∧ B

Γ ` πl(t) : A(∧l

E)

π

Γ ` t : A ∧ B

Γ ` πr(t) : A(∧r

E)

Γ ` 〈πl(t), πr(t)〉 : A ∧ B(∧I)

π

Γ ` t : A ∧ B

In other words, the η-reduction rule for products is

〈πl(t), πr(t)〉 −→η t

62

Page 172: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Part VI

Classical logic

63

Page 173: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic

For a long time, people thought that this correspondence could not be extended toclassical logic.

It turns out that it actually does (Parigot’s λµ-calculus): classical logic is constructive!

This gives rise to strange languages, relying heavily on a variant of exceptions.

64

Page 174: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic

We have seen that classical logic could be obtained from intuitionistic one by addingthe principle of elimination of double negation:

¬¬A⇒ A

This can be equivalently implemented by adding the rule

Γ ` ¬¬AΓ ` A

(¬¬E)

This suggests that we should add a new construction C.

65

Page 175: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic

We have seen that classical logic could be obtained from intuitionistic one by addingthe principle of elimination of double negation:

¬¬A⇒ A

This can be equivalently implemented by adding the rule

Γ ` ¬¬AΓ ` A

(¬¬E)

This suggests that we should add a new construction C.

65

Page 176: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic

We have seen that classical logic could be obtained from intuitionistic one by addingthe principle of elimination of double negation:

¬¬A⇒ A

This can be equivalently implemented by adding the rule

Γ ` t : ¬¬AΓ ` C(t) : A

(¬¬E)

This suggests that we should add a new construction C.65

Page 177: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic

Remember that¬A = A⇒ ⊥

We already have an introduction rule for double negation:

Γ,

k :

A⇒ ⊥ `

k :

A⇒ ⊥(ax)

π

Γ `

t :

A

Γ,

k :

A⇒ ⊥ `

t :

A(wk)

Γ,

k :

A⇒ ⊥ `

k t :

⊥(⇒E)

Γ `

λkA⇒⊥.k t :

(A⇒ ⊥)⇒ ⊥(⇒I)

66

Page 178: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic

Remember that¬A = A⇒ ⊥

We already have an introduction rule for double negation:

Γ, k : A⇒ ⊥ ` k : A⇒ ⊥(ax)

π

Γ ` t : A

Γ, k : A⇒ ⊥ ` t : A(wk)

Γ, k : A⇒ ⊥ ` k t : ⊥(⇒E)

Γ ` λkA⇒⊥.k t : (A⇒ ⊥)⇒ ⊥(⇒I)

66

Page 179: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: reduction

The cut elimination procedure should give

π

Γ ` A

Γ ` ¬¬A(¬¬I)

Γ ` A(¬¬E)

π

Γ ` A

In other words,C(λk¬A.k t) −→β t

This means that C takes as argument a function (A→ ⊥)→ ⊥, which itself takes afunction k : A→ ⊥ (called a continuation) as argument.

When we apply this function k to some argument t the function cc will discard thecomputation and return the argument t.

Generally, this does not happen immediately:

cc(λk¬A.u) −→β cc(λk¬A.u1) −→β cc(λk¬A.u2) −→β . . . −→β cc(λk¬A.k t) −→β t

A proper description of the reduction is actually more subtle than just that...

67

Page 180: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: reduction

The cut elimination procedure should give

π

Γ ` t : A

Γ ` λk¬A.k t : ¬¬A(¬¬I)

Γ ` C(λk¬A.k t) : A(¬¬E)

π

Γ ` t : A

In other words,C(λk¬A.k t) −→β t

Note that for the reduction to make sense, we need to have k 6∈ FV(t).

This means that C takes as argument a function (A→ ⊥)→ ⊥, which itself takes afunction k : A→ ⊥ (called a continuation) as argument.

When we apply this function k to some argument t the function cc will discard thecomputation and return the argument t.

Generally, this does not happen immediately:

cc(λk¬A.u) −→β cc(λk¬A.u1) −→β cc(λk¬A.u2) −→β . . . −→β cc(λk¬A.k t) −→β t

A proper description of the reduction is actually more subtle than just that...

67

Page 181: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: reduction

In other words,C(λk¬A.k t) −→β t

This means that C takes as argument a function (A→ ⊥)→ ⊥, which itself takes afunction k : A→ ⊥ (called a continuation) as argument.

When we apply this function k to some argument t the function cc will discard thecomputation and return the argument t.

Generally, this does not happen immediately:

cc(λk¬A.u) −→β cc(λk¬A.u1) −→β cc(λk¬A.u2) −→β . . . −→β cc(λk¬A.k t) −→β t

A proper description of the reduction is actually more subtle than just that...

67

Page 182: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: reduction

In other words,C(λk¬A.k t) −→β t

This means that C takes as argument a function (A→ ⊥)→ ⊥, which itself takes afunction k : A→ ⊥ (called a continuation) as argument.

When we apply this function k to some argument t the function cc will discard thecomputation and return the argument t.

Generally, this does not happen immediately:

cc(λk¬A.u) −→β cc(λk¬A.u1) −→β cc(λk¬A.u2) −→β . . . −→β cc(λk¬A.k t) −→β t

A proper description of the reduction is actually more subtle than just that...

67

Page 183: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: reduction

In other words,C(λk¬A.k t) −→β t

This means that C takes as argument a function (A→ ⊥)→ ⊥, which itself takes afunction k : A→ ⊥ (called a continuation) as argument.

When we apply this function k to some argument t the function cc will discard thecomputation and return the argument t.

Generally, this does not happen immediately:

cc(λk¬A.u) −→β cc(λk¬A.u1) −→β cc(λk¬A.u2) −→β . . . −→β cc(λk¬A.k t) −→β t

A proper description of the reduction is actually more subtle than just that...

67

Page 184: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: reduction

In other words,C(λk¬A.k t) −→β t

This means that C takes as argument a function (A→ ⊥)→ ⊥, which itself takes afunction k : A→ ⊥ (called a continuation) as argument.

When we apply this function k to some argument t the function cc will discard thecomputation and return the argument t.

Generally, this does not happen immediately:

cc(λk¬A.u) −→β cc(λk¬A.u1) −→β cc(λk¬A.u2) −→β . . . −→β cc(λk¬A.k t) −→β t

A proper description of the reduction is actually more subtle than just that...67

Page 185: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Call-cc

The operator C is due to Felleisen.

A well known variant is call-cc cc (for call with current continuation) which is typed as

cc : (¬A→ A)→ A

68

Page 186: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A proof for excluded middle is

` ¬A ∨ A(¬¬E)

Page 187: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A proof for excluded middle is

` ¬¬(¬A ∨ A)(¬I)

` ¬A ∨ A(¬¬E)

Page 188: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A proof for excluded middle is

¬(¬A ∨ A) ` ⊥(¬E)

` ¬¬(¬A ∨ A)(¬I)

` ¬A ∨ A(¬¬E)

Page 189: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A proof for excluded middle is

¬(¬A ∨ A) ` ¬A ∨ A(∨l

I)

¬(¬A ∨ A) ` ⊥(¬E)

` ¬¬(¬A ∨ A)(¬I)

` ¬A ∨ A(¬¬E)

Page 190: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A proof for excluded middle is

¬(¬A ∨ A) ` ¬A(¬I)

¬(¬A ∨ A) ` ¬A ∨ A(∨l

I)

¬(¬A ∨ A) ` ⊥(¬E)

` ¬¬(¬A ∨ A)(¬I)

` ¬A ∨ A(¬¬E)

Page 191: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A proof for excluded middle is

¬(¬A ∨ A),A ` ⊥(¬E)

¬(¬A ∨ A) ` ¬A(¬I)

¬(¬A ∨ A) ` ¬A ∨ A(∨l

I)

¬(¬A ∨ A) ` ⊥(¬E)

` ¬¬(¬A ∨ A)(¬I)

` ¬A ∨ A(¬¬E)

Page 192: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A proof for excluded middle is

¬(¬A ∨ A),A ` ¬A ∨ A(∨r

I)

¬(¬A ∨ A),A ` ⊥(¬E)

¬(¬A ∨ A) ` ¬A(¬I)

¬(¬A ∨ A) ` ¬A ∨ A(∨l

I)

¬(¬A ∨ A) ` ⊥(¬E)

` ¬¬(¬A ∨ A)(¬I)

` ¬A ∨ A(¬¬E)

Page 193: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A proof for excluded middle is

¬(¬A ∨ A),A ` A(ax)

¬(¬A ∨ A),A ` ¬A ∨ A(∨r

I)

¬(¬A ∨ A),A ` ⊥(¬E)

¬(¬A ∨ A) ` ¬A(¬I)

¬(¬A ∨ A) ` ¬A ∨ A(∨l

I)

¬(¬A ∨ A) ` ⊥(¬E)

` ¬¬(¬A ∨ A)(¬I)

` ¬A ∨ A(¬¬E)

Page 194: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A proof for excluded middle is

¬(¬A ∨ A),A ` A(ax)

¬(¬A ∨ A),A ` ¬A ∨ A(∨r

I)

¬(¬A ∨ A),A ` ⊥(¬E)

¬(¬A ∨ A) ` ¬A(¬I)

¬(¬A ∨ A) ` ¬A ∨ A(∨l

I)

¬(¬A ∨ A) ` ⊥(¬E)

` ¬¬(¬A ∨ A)(¬I)

` ¬A ∨ A(¬¬E)

Page 195: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A term for excluded middle is

k : ¬(¬A ∨ A), a : A `

a :

A(ax)

k : ¬(¬A ∨ A), a : A `

ιr(a) :

¬A ∨ A(∨r

I)

k : ¬(¬A ∨ A), a : A `

k ιr(a) :

⊥(¬E)

k : ¬(¬A ∨ A) `

λaA.k ιr(a) :

¬A(¬I)

k : ¬(¬A ∨ A) `

ιl(λaA.k ιr(a)) :

¬A ∨ A(∨l

I)

k : ¬(¬A ∨ A) `

k ιl(λaA.k ιr(a)) :

⊥(¬E)

`

λk¬(¬A∨A).k ιl(λaA.k ιr(a)) :

¬¬(¬A ∨ A)(¬¬I)

`

cc(λk¬(¬A∨A).k ιl(λaA.k ιr(a))) :

¬A ∨ A(¬¬E)

69

Page 196: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A term for excluded middle is

k : ¬(¬A ∨ A), a : A ` a : A(ax)

k : ¬(¬A ∨ A), a : A `

ιr(a) :

¬A ∨ A(∨r

I)

k : ¬(¬A ∨ A), a : A `

k ιr(a) :

⊥(¬E)

k : ¬(¬A ∨ A) `

λaA.k ιr(a) :

¬A(¬I)

k : ¬(¬A ∨ A) `

ιl(λaA.k ιr(a)) :

¬A ∨ A(∨l

I)

k : ¬(¬A ∨ A) `

k ιl(λaA.k ιr(a)) :

⊥(¬E)

`

λk¬(¬A∨A).k ιl(λaA.k ιr(a)) :

¬¬(¬A ∨ A)(¬¬I)

`

cc(λk¬(¬A∨A).k ιl(λaA.k ιr(a))) :

¬A ∨ A(¬¬E)

69

Page 197: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A term for excluded middle is

k : ¬(¬A ∨ A), a : A ` a : A(ax)

k : ¬(¬A ∨ A), a : A ` ιr(a) : ¬A ∨ A(∨r

I)

k : ¬(¬A ∨ A), a : A `

k ιr(a) :

⊥(¬E)

k : ¬(¬A ∨ A) `

λaA.k ιr(a) :

¬A(¬I)

k : ¬(¬A ∨ A) `

ιl(λaA.k ιr(a)) :

¬A ∨ A(∨l

I)

k : ¬(¬A ∨ A) `

k ιl(λaA.k ιr(a)) :

⊥(¬E)

`

λk¬(¬A∨A).k ιl(λaA.k ιr(a)) :

¬¬(¬A ∨ A)(¬¬I)

`

cc(λk¬(¬A∨A).k ιl(λaA.k ιr(a))) :

¬A ∨ A(¬¬E)

69

Page 198: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A term for excluded middle is

k : ¬(¬A ∨ A), a : A ` a : A(ax)

k : ¬(¬A ∨ A), a : A ` ιr(a) : ¬A ∨ A(∨r

I)

k : ¬(¬A ∨ A), a : A ` k ιr(a) : ⊥(¬E)

k : ¬(¬A ∨ A) `

λaA.k ιr(a) :

¬A(¬I)

k : ¬(¬A ∨ A) `

ιl(λaA.k ιr(a)) :

¬A ∨ A(∨l

I)

k : ¬(¬A ∨ A) `

k ιl(λaA.k ιr(a)) :

⊥(¬E)

`

λk¬(¬A∨A).k ιl(λaA.k ιr(a)) :

¬¬(¬A ∨ A)(¬¬I)

`

cc(λk¬(¬A∨A).k ιl(λaA.k ιr(a))) :

¬A ∨ A(¬¬E)

69

Page 199: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A term for excluded middle is

k : ¬(¬A ∨ A), a : A ` a : A(ax)

k : ¬(¬A ∨ A), a : A ` ιr(a) : ¬A ∨ A(∨r

I)

k : ¬(¬A ∨ A), a : A ` k ιr(a) : ⊥(¬E)

k : ¬(¬A ∨ A) ` λaA.k ιr(a) : ¬A(¬I)

k : ¬(¬A ∨ A) `

ιl(λaA.k ιr(a)) :

¬A ∨ A(∨l

I)

k : ¬(¬A ∨ A) `

k ιl(λaA.k ιr(a)) :

⊥(¬E)

`

λk¬(¬A∨A).k ιl(λaA.k ιr(a)) :

¬¬(¬A ∨ A)(¬¬I)

`

cc(λk¬(¬A∨A).k ιl(λaA.k ιr(a))) :

¬A ∨ A(¬¬E)

69

Page 200: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A term for excluded middle is

k : ¬(¬A ∨ A), a : A ` a : A(ax)

k : ¬(¬A ∨ A), a : A ` ιr(a) : ¬A ∨ A(∨r

I)

k : ¬(¬A ∨ A), a : A ` k ιr(a) : ⊥(¬E)

k : ¬(¬A ∨ A) ` λaA.k ιr(a) : ¬A(¬I)

k : ¬(¬A ∨ A) ` ιl(λaA.k ιr(a)) : ¬A ∨ A(∨l

I)

k : ¬(¬A ∨ A) `

k ιl(λaA.k ιr(a)) :

⊥(¬E)

`

λk¬(¬A∨A).k ιl(λaA.k ιr(a)) :

¬¬(¬A ∨ A)(¬¬I)

`

cc(λk¬(¬A∨A).k ιl(λaA.k ιr(a))) :

¬A ∨ A(¬¬E)

69

Page 201: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A term for excluded middle is

k : ¬(¬A ∨ A), a : A ` a : A(ax)

k : ¬(¬A ∨ A), a : A ` ιr(a) : ¬A ∨ A(∨r

I)

k : ¬(¬A ∨ A), a : A ` k ιr(a) : ⊥(¬E)

k : ¬(¬A ∨ A) ` λaA.k ιr(a) : ¬A(¬I)

k : ¬(¬A ∨ A) ` ιl(λaA.k ιr(a)) : ¬A ∨ A(∨l

I)

k : ¬(¬A ∨ A) ` k ιl(λaA.k ιr(a)) : ⊥

(¬E)

`

λk¬(¬A∨A).k ιl(λaA.k ιr(a)) :

¬¬(¬A ∨ A)(¬¬I)

`

cc(λk¬(¬A∨A).k ιl(λaA.k ιr(a))) :

¬A ∨ A(¬¬E)

69

Page 202: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A term for excluded middle is

k : ¬(¬A ∨ A), a : A ` a : A(ax)

k : ¬(¬A ∨ A), a : A ` ιr(a) : ¬A ∨ A(∨r

I)

k : ¬(¬A ∨ A), a : A ` k ιr(a) : ⊥(¬E)

k : ¬(¬A ∨ A) ` λaA.k ιr(a) : ¬A(¬I)

k : ¬(¬A ∨ A) ` ιl(λaA.k ιr(a)) : ¬A ∨ A(∨l

I)

k : ¬(¬A ∨ A) ` k ιl(λaA.k ιr(a)) : ⊥

(¬E)

` λk¬(¬A∨A).k ιl(λaA.k ιr(a)) : ¬¬(¬A ∨ A)(¬¬I)

`

cc(λk¬(¬A∨A).k ιl(λaA.k ιr(a))) :

¬A ∨ A(¬¬E)

69

Page 203: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Classical logic: excluded middle

A term for excluded middle is

k : ¬(¬A ∨ A), a : A ` a : A(ax)

k : ¬(¬A ∨ A), a : A ` ιr(a) : ¬A ∨ A(∨r

I)

k : ¬(¬A ∨ A), a : A ` k ιr(a) : ⊥(¬E)

k : ¬(¬A ∨ A) ` λaA.k ιr(a) : ¬A(¬I)

k : ¬(¬A ∨ A) ` ιl(λaA.k ιr(a)) : ¬A ∨ A(∨l

I)

k : ¬(¬A ∨ A) ` k ιl(λaA.k ιr(a)) : ⊥

(¬E)

` λk¬(¬A∨A).k ιl(λaA.k ιr(a)) : ¬¬(¬A ∨ A)(¬¬I)

` cc(λk¬(¬A∨A).k ιl(λaA.k ιr(a))) : ¬A ∨ A

(¬¬E)

69

Page 204: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Part VII

Strong normalization

70

Page 205: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Strong normalization

A term t is strongly normalizing (or SN, or terminating) if there is no infinitesequence of reductions starting from t:

t −→β t1 −→β t2 −→β t3 −→β . . .

71

Page 206: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Strong normalization

Theorem (Strong normalization)The simply-typed λ-calculus is strongly normalizing: given a typable λ-term t, there isno infinite sequence of β-reductions starting from t.

For instance, the λ-term(λx .xx)(λx .xx)

is not typable.

# let omega = (fun x -> x x) (fun x -> x x);;^

Error: This expression has type 'a -> 'bbut an expression was expected of type 'aThe type variable 'a occurs inside 'a -> 'b

72

Page 207: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Strong normalization

Theorem (Strong normalization)The simply-typed λ-calculus is strongly normalizing: given a typable λ-term t, there isno infinite sequence of β-reductions starting from t.

For instance, the λ-term(λx .xx)(λx .xx)

is not typable.

# let omega = (fun x -> x x) (fun x -> x x);;^

Error: This expression has type 'a -> 'bbut an expression was expected of type 'aThe type variable 'a occurs inside 'a -> 'b

72

Page 208: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Strong normalization

Theorem (Strong normalization)The simply-typed λ-calculus is strongly normalizing: given a typable λ-term t, there isno infinite sequence of β-reductions starting from t.

For instance, the λ-term(λx .xx)(λx .xx)

is not typable.

# let omega = (fun x -> x x) (fun x -> x x);;^

Error: This expression has type 'a -> 'bbut an expression was expected of type 'aThe type variable 'a occurs inside 'a -> 'b

72

Page 209: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Deciding β-equivalence

Recall that β-equivalence is the smallest equivalence relation generated by β-reduction.

This means that t ===β u when there exists a sequence of reductions

t∗←− t1

∗−→ t2∗←− t3

∗−→ t4∗←− . . . ∗−→ u

How can we decide whether two terms are β-equivalent or not?

(remember this is undecidable for untyped λ-calculus)

73

Page 210: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Church-Rosser theorem

A first simplification:

Theorem (Church-Rosser)Two terms t and u are β-equivalent iff and only if there exists w such that

t∗−→β wβ

∗←− u

Proof.The only if part is obvious. Suppose that we have

t1 t2 t3 . . . tn

t = u0 u1 u2 un = u

w1 w

∗ ∗ ∗ ∗ ∗ ∗

we show the result by induction on n. For n = 0, t = u and the result is immediate.74

Page 211: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Church-Rosser theorem

A first simplification:

Theorem (Church-Rosser)Two terms t and u are β-equivalent iff and only if there exists w such that

t∗−→β wβ

∗←− u

Proof.The only if part is obvious. Suppose that we have

t1 t2 t3 . . . tn

t = u0 u1 u2 un = u

w1 w

∗ ∗ ∗ ∗ ∗ ∗

Otherwise,

by confluence and induction hypothesis.

74

Page 212: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Church-Rosser theorem

A first simplification:

Theorem (Church-Rosser)Two terms t and u are β-equivalent iff and only if there exists w such that

t∗−→β wβ

∗←− u

Proof.The only if part is obvious. Suppose that we have

t1 t2 t3 . . . tn

t = u0 u1 u2 un = u

w1

w

∗ ∗ ∗ ∗ ∗ ∗

∗ ∗

Otherwise, by confluence

and induction hypothesis.

74

Page 213: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

The Church-Rosser theorem

A first simplification:

Theorem (Church-Rosser)Two terms t and u are β-equivalent iff and only if there exists w such that

t∗−→β wβ

∗←− u

Proof.The only if part is obvious. Suppose that we have

t1 t2 t3 . . . tn

t = u0 u1 u2 un = u

w1 w

∗ ∗ ∗ ∗ ∗ ∗

∗ ∗ ∗∗

Otherwise, by confluence and induction hypothesis.74

Page 214: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Joinability

We thus left with deciding whether two terms t and u are joinable, i.e. there exists wsuch that

t∗−→β wβ

∗←− u

75

Page 215: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Normal forms

A term t is a normal form when there is no t ′ such that t −→β t ′.

LemmaEvery typable term t is β-equivalent to a normal form t.

Proof.Given a term t reduce it as much as possible:

t −→β t1 −→β t2 −→β · · · −→β tn = t

This process will stop because typable terms are strongly normalizing and tn is a normalform.

Strongly normalizing: any sequence of reductions will lead to a normal form.

76

Page 216: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Normal forms

A term t is a normal form when there is no t ′ such that t −→β t ′.

LemmaEvery typable term t is β-equivalent to a normal form t.

Proof.Given a term t reduce it as much as possible:

t −→β t1 −→β t2 −→β · · · −→β tn = t

This process will stop because typable terms are strongly normalizing and tn is a normalform.

Strongly normalizing: any sequence of reductions will lead to a normal form.

76

Page 217: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Normal forms

A term t is a normal form when there is no t ′ such that t −→β t ′.

LemmaEvery typable term t is β-equivalent to a normal form t.

Proof.Given a term t reduce it as much as possible:

t −→β t1 −→β t2 −→β · · · −→β tn = t

This process will stop because typable terms are strongly normalizing and tn is a normalform.

Strongly normalizing: any sequence of reductions will lead to a normal form.

76

Page 218: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Normal forms

LemmaTwo normal forms t and u are β-equivalent iff they are equal.

Proof.The only if part is obvious. For the if part, by the Church-Rosser theorem we have

t∗−→β wβ

∗←− u

but since t and u are normal forms, we actually have

t = w = u

77

Page 219: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Deciding β-equivalence

Suppose given two typable terms t and u. The following are equivalent

• t ===β u

• t ===β u

• t = u

Which can be pictured ast u

t u

∗∗?

?

78

Page 220: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Extensions

This still holds for extensions of λ-calculus: products, coproducts, natural numbers, etc.

In particular, for natural numbers it is important that the recursive calls are performedon smaller numbers, which ensures termination.

79

Page 221: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Part VIII

Type inference à la Curry

80

Page 222: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Curry style λ-calculus

In Curry style, λ-terms aret ::= x | t u | λx .t

and the rules are

Γ ` x : Γ(x)(ax)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

Γ, x : A ` t : B

Γ ` λx .t : A→ B(→I)

A term can have multiple types:

x : A ` x : A(ax)

` λx .x : A→ A(→I)

x : A→ A ` x : A→ A(ax)

` λx .x : (A→ A)→ (A→ A)(→I)

How do we compute all those types?

81

Page 223: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Curry style λ-calculus

In Curry style, λ-terms aret ::= x | t u | λx .t

and the rules are

Γ ` x : Γ(x)(ax)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

Γ, x : A ` t : B

Γ ` λx .t : A→ B(→I)

A term can have multiple types:

x : A ` x : A(ax)

` λx .x : A→ A(→I)

x : A→ A ` x : A→ A(ax)

` λx .x : (A→ A)→ (A→ A)(→I)

How do we compute all those types?

81

Page 224: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Curry style λ-calculus

In Curry style, λ-terms aret ::= x | t u | λx .t

and the rules are

Γ ` x : Γ(x)(ax)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

Γ, x : A ` t : B

Γ ` λx .t : A→ B(→I)

A term can have multiple types:

x : A ` x : A(ax)

` λx .x : A→ A(→I)

x : A→ A ` x : A→ A(ax)

` λx .x : (A→ A)→ (A→ A)(→I)

How do we compute all those types? 81

Page 225: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Substitutions

A substitution is a function which to type variables associate terms. For instance

σ(X ) = A→ B σ(Y ) = A

We write A[σ] for the type A where variables have been replaced according to σ:

(X → Y )[σ] = (A→ B)→ A

82

Page 226: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Substitutions

A substitution is a function which to type variables associate terms. For instance

σ(X ) = A→ B σ(Y ) = A

We write A[σ] for the type A where variables have been replaced according to σ:

(X → Y )[σ] = (A→ B)→ A

82

Page 227: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Type equation systems

A type equation system is a finite set

E = {A1 =? B1, . . . ,An =? Bn}of pairs of types Ai and Bi .

A substitution σ is a solution of E if

Ai [σ] = Bi [σ]

for every index i .

83

Page 228: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Type equation systems

A type equation system is a finite set

E = {A1 =? B1, . . . ,An =? Bn}of pairs of types Ai and Bi .

A substitution σ is a solution of E if

Ai [σ] = Bi [σ]

for every index i .

83

Page 229: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Type equation systems

A type equation system is a finite set

E = {A1 =? B1, . . . ,An =? Bn}of pairs of types Ai and Bi .

A substitution σ is a solution of E if

Ai [σ] = Bi [σ]

for every index i .

For instance, a solution of

{(X → Y ) =? (Z → (Z → Z ))}

is σ(X ) = Z , σ(Y ) = Z → Z .

83

Page 230: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Type equation systems

A type equation system is a finite set

E = {A1 =? B1, . . . ,An =? Bn}of pairs of types Ai and Bi .

A substitution σ is a solution of E if

Ai [σ] = Bi [σ]

for every index i .

For instance, a solution of

{(X → Y ) =? (Z → (Z → Z ))}

is σ(X ) = Z , σ(Y ) = Z → Z .83

Page 231: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Type equation systems

A type equation system is a finite set

E = {A1 =? B1, . . . ,An =? Bn}of pairs of types Ai and Bi .

A substitution σ is a solution of E if

Ai [σ] = Bi [σ]

for every index i .

For instance, a solution of{(X → Y ) =? Y }

does not exist.

83

Page 232: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Type equation systems

A type equation system is a finite set

E = {A1 =? B1, . . . ,An =? Bn}of pairs of types Ai and Bi .

A substitution σ is a solution of E if

Ai [σ] = Bi [σ]

for every index i .

For instance, a solution of{(X → Y ) =? Y }

does not exist.

83

Page 233: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

We are going to associate to each term t

• a type At

• an equation system Et

such that

• t is typable iff Et has a solution σ,

• in which case the At [σ] are the possible types of t.

84

Page 234: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

The rules

Γ ` x : Γ(x)(ax)

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

Γ, x : A ` t : B

Γ ` λx .t : A→ B(→I)

suggest that

• to every term variable x , we associate a type variable Xx ,• to every term t, we associate a type At ,• to every term t, we associate an equation system Et

by induction by

Ex = ∅ Ax = Xx

Et u = Et ∪ Eu ∪ {At =? (Au → X )} At u = X with X fresh

Eλx .t = Et Aλx .t = Xx → At85

Page 235: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Ex = ∅ Ax = Xx

Eλx .x = ∅ Aλx .x = Xx → Xx

Ef (λx .x) = {Xf =? (Xx → Xx)→ X} Af (λx .x) = X

Ef (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Af (f (λx .x)) = Y

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

86

Page 236: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Ex = ∅ Ax = Xx

Eλx .x = ∅ Aλx .x = Xx → Xx

Ef (λx .x) = {Xf =? (Xx → Xx)→ X} Af (λx .x) = X

Ef (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Af (f (λx .x)) = Y

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

86

Page 237: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Ex = ∅ Ax = Xx

Eλx .x = ∅ Aλx .x = Xx → Xx

Ef (λx .x) = {Xf =? (Xx → Xx)→ X} Af (λx .x) = X

Ef (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Af (f (λx .x)) = Y

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

86

Page 238: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Ex = ∅ Ax = Xx

Eλx .x = ∅ Aλx .x = Xx → Xx

Ef (λx .x) = {Xf =? (Xx → Xx)→ X} Af (λx .x) = X

Ef (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Af (f (λx .x)) = Y

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

86

Page 239: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Ex = ∅ Ax = Xx

Eλx .x = ∅ Aλx .x = Xx → Xx

Ef (λx .x) = {Xf =? (Xx → Xx)→ X} Af (λx .x) = X

Ef (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Af (f (λx .x)) = Y

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

86

Page 240: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Ex = ∅ Ax = Xx

Eλx .x = ∅ Aλx .x = Xx → Xx

Ef (λx .x) = {Xf =? (Xx → Xx)→ X} Af (λx .x) = X

Ef (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Af (f (λx .x)) = Y

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

86

Page 241: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

A solution is

σ(Xx) = A σ(X ) = A→ A σ(Y ) = A→ A σ(Xf ) = (A→ A)→ (A→ A)

The resulting type is

(Xf → Y )[σ] = ((A→ A)→ (A→ A))→ A→ A

to be compared with

# fun f -> f (f (fun x -> x));;- : (('a -> 'a) -> 'a -> 'a) -> 'a -> 'a = <fun>

87

Page 242: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

A solution is

σ(Xx) = A

σ(X ) = A→ A σ(Y ) = A→ A σ(Xf ) = (A→ A)→ (A→ A)

The resulting type is

(Xf → Y )[σ] = ((A→ A)→ (A→ A))→ A→ A

to be compared with

# fun f -> f (f (fun x -> x));;- : (('a -> 'a) -> 'a -> 'a) -> 'a -> 'a = <fun>

87

Page 243: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

A solution is

σ(Xx) = A σ(X ) = A→ A

σ(Y ) = A→ A σ(Xf ) = (A→ A)→ (A→ A)

The resulting type is

(Xf → Y )[σ] = ((A→ A)→ (A→ A))→ A→ A

to be compared with

# fun f -> f (f (fun x -> x));;- : (('a -> 'a) -> 'a -> 'a) -> 'a -> 'a = <fun>

87

Page 244: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

A solution is

σ(Xx) = A σ(X ) = A→ A σ(Y ) = A→ A

σ(Xf ) = (A→ A)→ (A→ A)

The resulting type is

(Xf → Y )[σ] = ((A→ A)→ (A→ A))→ A→ A

to be compared with

# fun f -> f (f (fun x -> x));;- : (('a -> 'a) -> 'a -> 'a) -> 'a -> 'a = <fun>

87

Page 245: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

A solution is

σ(Xx) = A σ(X ) = A→ A σ(Y ) = A→ A σ(Xf ) = (A→ A)→ (A→ A)

The resulting type is

(Xf → Y )[σ] = ((A→ A)→ (A→ A))→ A→ A

to be compared with

# fun f -> f (f (fun x -> x));;- : (('a -> 'a) -> 'a -> 'a) -> 'a -> 'a = <fun>

87

Page 246: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

A solution is

σ(Xx) = A σ(X ) = A→ A σ(Y ) = A→ A σ(Xf ) = (A→ A)→ (A→ A)

The resulting type is

(Xf → Y )[σ] = ((A→ A)→ (A→ A))→ A→ A

to be compared with

# fun f -> f (f (fun x -> x));;- : (('a -> 'a) -> 'a -> 'a) -> 'a -> 'a = <fun>

87

Page 247: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

For instance, considert = λf .f (f (λx .x))

we have

Eλf .f (f (λx .x)) = {Xf =? (Xx → Xx)→ X ,Xf =? X → Y } Aλf .f (f (λx .x)) = Xf → Y

A solution is

σ(Xx) = A σ(X ) = A→ A σ(Y ) = A→ A σ(Xf ) = (A→ A)→ (A→ A)

The resulting type is

(Xf → Y )[σ] = ((A→ A)→ (A→ A))→ A→ A

to be compared with

# fun f -> f (f (fun x -> x));;- : (('a -> 'a) -> 'a -> 'a) -> 'a -> 'a = <fun> 87

Page 248: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

Note that the previous solution works for which ever type A we choose.

Therefore there is an infinite number of solutions!

88

Page 249: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Typing as solving constraints

TheoremWe have

• if Γ ` t : A then there is a solution σ of Et such that A = At [σ] and Γ(x) = σ(Xx)

for every variable x ∈ FV(t),

• for every solution σ of Et , if we write Γ for a context such that Γ(x) = σ(x) forevery free variable x ∈ FV(t), then Γ ` t : At [σ] is derivable.

Otherwise said, there is a bijection between

• solutions σ of Et ,

• pairs (Γ,A) such that Γ ` t : A is derivable.

89

Page 250: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Unification

The unification algorithm takes a type equation system E and produces a solution σwhen there exists one, in polynomial time.

Moreover, this solution is the most general one: any other solution τ satisfies

τ = τ ′ ◦ σ

We can therefore compute a most general type for Curry-style λ-terms in P-time!

90

Page 251: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Unification

The unification algorithm takes a type equation system E and produces a solution σwhen there exists one, in polynomial time.

Moreover, this solution is the most general one: any other solution τ satisfies

τ = τ ′ ◦ σ

We can therefore compute a most general type for Curry-style λ-terms in P-time!

90

Page 252: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Unification

The unification algorithm takes a type equation system E and produces a solution σwhen there exists one, in polynomial time.

Moreover, this solution is the most general one: any other solution τ satisfies

τ = τ ′ ◦ σ

We can therefore compute a most general type for Curry-style λ-terms in P-time!

90

Page 253: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Part IX

Bidirectional typechecking

91

Page 254: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Looking closely at the operations we perform during type-checking there are twophases.

• Type inference: we guess the type of a term.

• Type checking: we check that a term has a given type.

For instance, consider the type inference of

x : N ` x : N

` λxN.x : N→ N ` 5 : N

` (λxN.x)5 : N

92

Page 255: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Bidirectional typechecking formalizes this two phases, allowing to add typeannotations (we could mix Church and Curry style).

93

Page 256: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

We consider Curry-style terms with type annotations:

t ::= x | t u | λx .t | (x : A)

We consider two kind of sequents:

• Γ ` t ⇒ A: the term t allows to synthesize the type A (type inference),• Γ ` t ⇐ A: the term t allows checks against the type A (type checking).

We can then orient the typing rules

Γ ` t : A as Γ ` t ⇒ A or Γ ` t :⇐ A

94

Page 257: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

We consider Curry-style terms with type annotations:

t ::= x | t u | λx .t | (x : A)

We consider two kind of sequents:

• Γ ` t ⇒ A: the term t allows to synthesize the type A (type inference),• Γ ` t ⇐ A: the term t allows checks against the type A (type checking).

We can then orient the typing rules

Γ ` t : A as Γ ` t ⇒ A or Γ ` t :⇐ A

94

Page 258: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

We consider Curry-style terms with type annotations:

t ::= x | t u | λx .t | (x : A)

We consider two kind of sequents:

• Γ ` t ⇒ A: the term t allows to synthesize the type A (type inference),• Γ ` t ⇐ A: the term t allows checks against the type A (type checking).

We can then orient the typing rules

Γ ` t : A as Γ ` t ⇒ A or Γ ` t :⇐ A

94

Page 259: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Orientation of the base rules

• variable: we already have the information in the context

Γ ` x ⇒ Γ(x)(ax)

• application: we cannot come up with B , we have to check the type of theargument

Γ ` t ⇒ A→ B Γ ` u ⇐ A

Γ ` t u : B(→E)

• abstraction: we cannot come up with A in Curry style (and typing is not unique)

Γ, x : A ` t ⇐ B

Γ ` λx .t ⇐ A→ B(→I)

95

Page 260: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Orientation of the base rules

• variable: we already have the information in the context

Γ ` x ⇒ Γ(x)(ax)

• application: we cannot come up with B , we have to check the type of theargument

Γ ` t ⇒ A→ B Γ ` u ⇐ A

Γ ` t u : B(→E)

• abstraction: we cannot come up with A in Curry style (and typing is not unique)

Γ, x : A ` t ⇐ B

Γ ` λx .t ⇐ A→ B(→I)

95

Page 261: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Orientation of the base rules

• variable: we already have the information in the context

Γ ` x ⇒ Γ(x)(ax)

• application: we cannot come up with B , we have to check the type of theargument

Γ ` t ⇒ A→ B Γ ` u ⇐ A

Γ ` t u : B(→E)

• abstraction: we cannot come up with A in Curry style (and typing is not unique)

Γ, x : A ` t ⇐ B

Γ ` λx .t ⇐ A→ B(→I) 95

Page 262: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

We have two new rules:

• subsumption: if we can infer then we can check

Γ ` t ⇒ A

Γ ` t ⇐ A

• casting:Γ ` t ⇐ A

Γ ` (t : A)⇒ A

96

Page 263: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

We have two new rules:

• subsumption: if we can infer then we can check

Γ ` t ⇒ A

Γ ` t ⇐ A

• casting:Γ ` t ⇐ A

Γ ` (t : A)⇒ A

96

Page 264: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Γ ` mean (λx .x × x) 5 7⇒ R

97

Page 265: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Γ ` mean (λx .x × x) 5⇒ R→ R Γ ` 7⇐ R

Γ ` mean (λx .x × x) 5 7⇒ R

97

Page 266: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Γ ` mean (λx .x × x) 5⇒ R→ R

Γ ` 7⇒ R

Γ ` 7⇐ R

Γ ` mean (λx .x × x) 5 7⇒ R

97

Page 267: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Γ ` mean (λx .x × x) 5⇒ R→ R

Γ ` 7⇒ R

Γ ` 7⇐ R

Γ ` mean (λx .x × x) 5 7⇒ R

97

Page 268: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Γ ` mean (λx .x × x)⇒ R→ R→ R...

Γ ` mean (λx .x × x) 5⇒ R→ R

Γ ` 7⇒ R

Γ ` 7⇐ R

Γ ` mean (λx .x × x) 5 7⇒ R

97

Page 269: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Γ ` mean⇒ (R→ R)→ R→ R→ R Γ ` λx .x ⇐ R→ R

Γ ` mean (λx .x × x)⇒ R→ R→ R...

Γ ` mean (λx .x × x) 5⇒ R→ R

Γ ` 7⇒ R

Γ ` 7⇐ R

Γ ` mean (λx .x × x) 5 7⇒ R

97

Page 270: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Γ ` mean⇒ (R→ R)→ R→ R→ R

Γ, x : R ` x ⇐ R

Γ ` λx .x ⇐ R→ R

Γ ` mean (λx .x × x)⇒ R→ R→ R...

Γ ` mean (λx .x × x) 5⇒ R→ R

Γ ` 7⇒ R

Γ ` 7⇐ R

Γ ` mean (λx .x × x) 5 7⇒ R

97

Page 271: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Γ ` mean⇒ (R→ R)→ R→ R→ R

Γ, x : R ` x ⇒ R

Γ, x : R ` x ⇐ R

Γ ` λx .x ⇐ R→ R

Γ ` mean (λx .x × x)⇒ R→ R→ R...

Γ ` mean (λx .x × x) 5⇒ R→ R

Γ ` 7⇒ R

Γ ` 7⇐ R

Γ ` mean (λx .x × x) 5 7⇒ R

97

Page 272: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

Γ ` mean⇒ (R→ R)→ R→ R→ R

Γ, x : R ` x ⇒ R

Γ, x : R ` x ⇐ R

Γ ` λx .x ⇐ R→ R

Γ ` mean (λx .x × x)⇒ R→ R→ R...

Γ ` mean (λx .x × x) 5⇒ R→ R

Γ ` 7⇒ R

Γ ` 7⇐ R

Γ ` mean (λx .x × x) 5 7⇒ R

97

Page 273: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

If we try to define the mean function as

λfxy .(f x + f y)/2

we cannot infer its type: we can only check the type of functions(i.e. when they are used as arguments of other functions).

In order to define it, we have to provide its type

mean = (λfxy .(f x + f y)/2 : (R→ R)→ R→ R→ R)

In Agda, the syntax for such definitions will be

mean : (R → R) → R → R → Rmean f x y = (x + y) / 2

98

Page 274: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

If we try to define the mean function as

λfxy .(f x + f y)/2

we cannot infer its type: we can only check the type of functions(i.e. when they are used as arguments of other functions).

In order to define it, we have to provide its type

mean = (λfxy .(f x + f y)/2 : (R→ R)→ R→ R→ R)

In Agda, the syntax for such definitions will be

mean : (R → R) → R → R → Rmean f x y = (x + y) / 2

98

Page 275: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Bidirectional typechecking

If we try to define the mean function as

λfxy .(f x + f y)/2

we cannot infer its type: we can only check the type of functions(i.e. when they are used as arguments of other functions).

In order to define it, we have to provide its type

mean = (λfxy .(f x + f y)/2 : (R→ R)→ R→ R→ R)

In Agda, the syntax for such definitions will be

mean : (R → R) → R → R → Rmean f x y = (x + y) / 2

98

Page 276: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Implementation

The implementation is pretty direct.

We define types

type ty =| TVar of string| Arr of ty * ty

and terms:

type term =| Var of string| App of term * term| Abs of string * term| Cast of term * ty

99

Page 277: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Implementation

The implementation is pretty direct.

We define types

type ty =| TVar of string| Arr of ty * ty

and terms:

type term =| Var of string| App of term * term| Abs of string * term| Cast of term * ty

99

Page 278: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Implementation

(** Type inference. *)let rec infer env = function

| Var x ->(try List.assoc x envwith Not_found ->

raise Type_error)| App (t, u) ->

(match infer env t with| Arr (a, b) -> check env u a; b| _ -> raise Type_error

)| Abs (x, t) -> raise Cannot_infer| Cast (t, a) -> check env t a; a

(** Type checking. *)and check env t a =

match t , a with| Abs (x, t) , Arr (a, b) ->

check ((x, a)::env) t b| _ -> if infer env t <> a then

raise Type_error

100

Page 279: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Implementation

(** Type inference. *)let rec infer env = function

| Var x ->(try List.assoc x envwith Not_found ->

raise Type_error)| App (t, u) ->

(match infer env t with| Arr (a, b) -> check env u a; b| _ -> raise Type_error

)| Abs (x, t) -> raise Cannot_infer| Cast (t, a) -> check env t a; a

(** Type checking. *)and check env t a =

match t , a with| Abs (x, t) , Arr (a, b) ->

check ((x, a)::env) t b| _ -> if infer env t <> a then

raise Type_error

100

Page 280: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Part X

Proof of strong normalization

101

Page 281: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Strong normalization

We now want to prove

TheoremTyped λ-terms are strongly normalizing.

Given a term t which is typable, there is no infinite sequence of reductions

t −→β t1 −→β t2 −→β . . .

102

Page 282: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Proof of strong normalization: first attempt

A naive try would be by induction on the derivation of Γ ` t : A.

If the last rule is

Γ ` x : A(ax)

x is clearly strongly normalizing.

103

Page 283: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Proof of strong normalization: first attempt

A naive try would be by induction on the derivation of Γ ` t : A.

If the last rule is

Γ, x : A ` t : B

Γ ` λxA.t : A→ B(→I)

A sequence of reductions is of the form

λx .t −→β λx .t1 −→β λx .t2 −→β . . .

witht −→β t1 −→β t2 −→β . . .

and we conclude by induction hypothesis.

103

Page 284: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Proof of strong normalization: first attempt

A naive try would be by induction on the derivation of Γ ` t : A.

If the last rule is

Γ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

We cannot say anything: a reduction starting from t u can be of the form

t u −→ t ′ u or t u −→ t u′

but also(λx .t)u −→ t[u/x ]

e.g. I I, for which we cannot say anything.

103

Page 285: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

Instead, we take an optimistic approach and defined, for each type A, a set

RA

of terms, the reducibility candidates for the type A, such that

• for every term t of type A (in whichever context), we have t ∈ RA,

• the terms of RA are obviously terminating.

NB:

• the definition has to be carefully crafted in order to be able to reason by induction,

• the terms of RA are not necessarily of type A (although we could).

104

Page 286: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

We define RA by induction on A by

• in the case of a variable,

RX = {t | t is strongly normalizing}

• in the case of an arrow,

RA→B = {t | for every u ∈ RA, we have t u ∈ RB}

We still have to show that

• a term t ∈ RA is strongly normalizing,• if Γ ` t : A is derivable then t ∈ RA.

105

Page 287: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

We define RA by induction on A by

• in the case of a variable,

RX = {t | t is strongly normalizing}

• in the case of an arrow,

RA→B = {t | for every u ∈ RA, we have t u ∈ RB}

We still have to show that

• a term t ∈ RA is strongly normalizing,• if Γ ` t : A is derivable then t ∈ RA.

105

Page 288: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

We define RA by induction on A by

• in the case of a variable,

RX = {t | t is strongly normalizing}

• in the case of an arrow,

RA→B = {t | for every u ∈ RA, we have t u ∈ RB}

We still have to show that

• a term t ∈ RA is strongly normalizing,• if Γ ` t : A is derivable then t ∈ RA.

105

Page 289: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

We define RA by induction on A by

• in the case of a variable,

RX = {t | t is strongly normalizing}

• in the case of an arrow,

RA→B = {t | for every u ∈ RA, we have t u ∈ RB}

We still have to show that

• a term t ∈ RA is strongly normalizing,• if Γ ` t : A is derivable then t ∈ RA.

105

Page 290: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Induction for SN terms

PropositionSuppose given a property P(t) on terms. Suppose that, for any term t, if P(t ′) forevery t ′ with t −→β t ′, then P(t):

t P(t)

t ′1 t ′2 t ′3 . . . t ′k P(t ′i ) for every i

Then P(t) holds for every SN term t.

Proof.By contraposition. Suppose that P(t) does not hold for some SN term t.

• then there exists t1 with t −→β t1 such that P(t1) does not hold,

• then there exists t2 with t1 −→β t2 such that P(t2) does not hold,

• . . .

Contradiction: we have an infinite sequence of reductions starting from t.

106

Page 291: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Induction for SN terms

PropositionSuppose given a property P(t) on terms. Suppose that, for any term t, if P(t ′) forevery t ′ with t −→β t ′, then P(t):

t P(t)

t ′1 t ′2 t ′3 . . . t ′k P(t ′i ) for every i

Then P(t) holds for every SN term t.Proof.By contraposition. Suppose that P(t) does not hold for some SN term t.

• then there exists t1 with t −→β t1 such that P(t1) does not hold,

• then there exists t2 with t1 −→β t2 such that P(t2) does not hold,

• . . .Contradiction: we have an infinite sequence of reductions starting from t.

106

Page 292: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Induction for SN terms

PropositionSuppose given a property P(t) on terms. Suppose that, for any term t, if P(t ′) forevery t ′ with t −→β t ′, then P(t):

t P(t)

t ′1 t ′2 t ′3 . . . t ′k P(t ′i ) for every i

Then P(t) holds for every SN term t.Proof.By contraposition. Suppose that P(t) does not hold for some SN term t.

• then there exists t1 with t −→β t1 such that P(t1) does not hold,

• then there exists t2 with t1 −→β t2 such that P(t2) does not hold,

• . . .Contradiction: we have an infinite sequence of reductions starting from t.

106

Page 293: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Induction for SN terms

PropositionSuppose given a property P(t) on terms. Suppose that, for any term t, if P(t ′) forevery t ′ with t −→β t ′, then P(t):

t P(t)

t ′1 t ′2 t ′3 . . . t ′k P(t ′i ) for every i

Then P(t) holds for every SN term t.Proof.By contraposition. Suppose that P(t) does not hold for some SN term t.

• then there exists t1 with t −→β t1 such that P(t1) does not hold,

• then there exists t2 with t1 −→β t2 such that P(t2) does not hold,

• . . .Contradiction: we have an infinite sequence of reductions starting from t.

106

Page 294: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Induction for SN terms

PropositionSuppose given a property P(t) on terms. Suppose that, for any term t, if P(t ′) forevery t ′ with t −→β t ′, then P(t):

t P(t)

t ′1 t ′2 t ′3 . . . t ′k P(t ′i ) for every i

Then P(t) holds for every SN term t.Proof.By contraposition. Suppose that P(t) does not hold for some SN term t.

• then there exists t1 with t −→β t1 such that P(t1) does not hold,

• then there exists t2 with t1 −→β t2 such that P(t2) does not hold,

• . . .

Contradiction: we have an infinite sequence of reductions starting from t.

106

Page 295: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Induction for SN terms

PropositionSuppose given a property P(t) on terms. Suppose that, for any term t, if P(t ′) forevery t ′ with t −→β t ′, then P(t):

t P(t)

t ′1 t ′2 t ′3 . . . t ′k P(t ′i ) for every i

Then P(t) holds for every SN term t.Proof.By contraposition. Suppose that P(t) does not hold for some SN term t.

• then there exists t1 with t −→β t1 such that P(t1) does not hold,

• then there exists t2 with t1 −→β t2 such that P(t2) does not hold,

• . . .Contradiction: we have an infinite sequence of reductions starting from t. 106

Page 296: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Neutral terms

A term t is neutral when it is not an abstraction:

t = t1 t2 or t = x

A neutral term does not interact with its context:

LemmaGiven terms t and u with t neutral, the only possible reductions of t u are

• t u −→β t ′ u with t −→β t ′,

• t u −→β t u′ with u −→β u′.

107

Page 297: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Neutral terms

A term t is neutral when it is not an abstraction:

t = t1 t2 or t = x

A neutral term does not interact with its context:

LemmaGiven terms t and u with t neutral, the only possible reductions of t u are

• t u −→β t ′ u with t −→β t ′,

• t u −→β t u′ with u −→β u′.

107

Page 298: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

Lemma

(CR1) If t ∈ RA then t is strongly normalizing.

(CR2) If t ∈ RA and t −→β t ′ then t ′ ∈ RA.

(CR3) If t is neutral, and for every t ′ such that t −→β t ′ we have t ′ ∈ RA, then t ∈ RA.

Proof.

108

Page 299: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

Lemma

(CR1) If t ∈ RA then t is strongly normalizing.

(CR2) If t ∈ RA and t −→β t ′ then t ′ ∈ RA.

(CR3) If t is neutral, and for every t ′ such that t −→β t ′ we have t ′ ∈ RA, then t ∈ RA.

Proof.By induction on A. If A = X is a variable then:

(CR1) is true by definition of RX .

(CR2) If t −→β t ′ and t is SN then t ′ is SN.

(CR3) If t reduces only in SN terms then it is SN.

108

Page 300: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

Lemma

(CR1) If t ∈ RA then t is strongly normalizing.

(CR2) If t ∈ RA and t −→β t ′ then t ′ ∈ RA.

(CR3) If t is neutral, and for every t ′ such that t −→β t ′ we have t ′ ∈ RA, then t ∈ RA.

Proof.Consider the case A→ B .

(CR1) Fix t ∈ RA→B .We have x ∈ RA by (CR3), therefore t x ∈ RB and is thus SN by (CR1).Any infinite reduction t −→β t1 −→β t2 −→β . . . would induce an infinitereduction t x −→β t1 x −→β t2 x −→β . . . with t x ∈ RB , which is impossible.Thus t is SN.

108

Page 301: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

Lemma

(CR1) If t ∈ RA then t is strongly normalizing.

(CR2) If t ∈ RA and t −→β t ′ then t ′ ∈ RA.

(CR3) If t is neutral, and for every t ′ such that t −→β t ′ we have t ′ ∈ RA, then t ∈ RA.

Proof.Consider the case A→ B .

(CR2) Fix t ∈ RA→B with t −→β t ′.Given u ∈ RA, we have t u ∈ RB and t u −→β t ′ u, therefore t ′ u ∈ RB by (CR2).Therefore t ′ ∈ RA→B by definition of RA→B .

108

Page 302: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

Lemma

(CR1) If t ∈ RA then t is strongly normalizing.

(CR2) If t ∈ RA and t −→β t ′ then t ′ ∈ RA.

(CR3) If t is neutral, and for every t ′ such that t −→β t ′ we have t ′ ∈ RA, then t ∈ RA.

Proof.Consider the case A→ B .

(CR3) Fix t neutral satisfying the property.Given u ∈ RA, u is SN by (CR1), and we can reason by induction on it.Since t is neutral the term t u reduces either to

• t ′ u with t −→β t ′: we have t ′ ∈ RA→B and thus t ′ u ∈ RB ,• t u′ with u −→β u′: we have u′ ∈ RA by (CR2), and therefore t u′ ∈ RB by IH.

The term t u is neutral and is thus in RB by (CR3) at type B .108

Page 303: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

LemmaGiven a term t and type A and B , if t u ∈ RB for every u ∈ RA, then λx .t ∈ RA→B .

Proof.We show that (λx .t)u ∈ RB by induction on (t, u). The term (λx .t)u can reduce to

• t[u/x ]: in RB by hypothesis,

• (λx .t ′)u with t −→β t ′: in RB by induction hypothesis,

• (λx .t)u′ with u −→β u′: in RB by induction hypothesis.

The term (λx .t)u is neutral and reduces to terms in RB .By (CR3), it belongs to RB .

109

Page 304: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

LemmaGiven a term t and type A and B , if t u ∈ RB for every u ∈ RA, then λx .t ∈ RA→B .

Proof.We show that (λx .t)u ∈ RB by induction on (t, u). The term (λx .t)u can reduce to

• t[u/x ]: in RB by hypothesis,

• (λx .t ′)u with t −→β t ′: in RB by induction hypothesis,

• (λx .t)u′ with u −→β u′: in RB by induction hypothesis.

The term (λx .t)u is neutral and reduces to terms in RB .By (CR3), it belongs to RB .

109

Page 305: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

LemmaGiven a term t and type A and B , if t u ∈ RB for every u ∈ RA, then λx .t ∈ RA→B .

Proof.We show that (λx .t)u ∈ RB by induction on (t, u). The term (λx .t)u can reduce to

• t[u/x ]: in RB by hypothesis,

• (λx .t ′)u with t −→β t ′: in RB by induction hypothesis,

• (λx .t)u′ with u −→β u′: in RB by induction hypothesis.

The term (λx .t)u is neutral and reduces to terms in RB .By (CR3), it belongs to RB .

109

Page 306: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

LemmaGiven a term t and type A and B , if t u ∈ RB for every u ∈ RA, then λx .t ∈ RA→B .

Proof.We show that (λx .t)u ∈ RB by induction on (t, u). The term (λx .t)u can reduce to

• t[u/x ]: in RB by hypothesis,

• (λx .t ′)u with t −→β t ′: in RB by induction hypothesis,

• (λx .t)u′ with u −→β u′: in RB by induction hypothesis.

The term (λx .t)u is neutral and reduces to terms in RB .By (CR3), it belongs to RB .

109

Page 307: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

LemmaGiven a term t and type A and B , if t u ∈ RB for every u ∈ RA, then λx .t ∈ RA→B .

Proof.We show that (λx .t)u ∈ RB by induction on (t, u). The term (λx .t)u can reduce to

• t[u/x ]: in RB by hypothesis,

• (λx .t ′)u with t −→β t ′: in RB by induction hypothesis,

• (λx .t)u′ with u −→β u′: in RB by induction hypothesis.

The term (λx .t)u is neutral and reduces to terms in RB .By (CR3), it belongs to RB .

109

Page 308: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

Finally, we would like to show that

TheoremGiven t such that Γ ` t : A is derivable, we have t ∈ RA.

Proof.By induction on the derivation of Γ ` t : A.

We actually need a stronger induction hypothesis!

110

Page 309: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

Finally, we would like to show that

TheoremGiven t such that Γ ` t : A is derivable, we have t ∈ RA.

Proof.By induction on the derivation of Γ ` t : A.

• If the last rule is

Γ ` x : A(ax)

Then x is neutral and reduces only to terms in RA (it does not reduce).By (CR3) x ∈ RA.

We actually need a stronger induction hypothesis!

110

Page 310: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

Finally, we would like to show that

TheoremGiven t such that Γ ` t : A is derivable, we have t ∈ RA.

Proof.By induction on the derivation of Γ ` t : A.

• If the last rule isΓ ` t : A→ B Γ ` u : A

Γ ` t u : B(→E)

By IH, we have t ∈ RA→B and u ∈ RA.Therefore t u ∈ RB by definition of RA→B .

We actually need a stronger induction hypothesis!

110

Page 311: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

Finally, we would like to show that

TheoremGiven t such that Γ ` t : A is derivable, we have t ∈ RA.

Proof.By induction on the derivation of Γ ` t : A.

• If the last rule isΓ, x : A ` t : B

Γ ` λx .t : A→ B(→I)

By IH, we have t ∈ RA. And... ????

We actually need a stronger induction hypothesis!

110

Page 312: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

Finally, we would like to show that

TheoremGiven t such that Γ ` t : A is derivable, we have t ∈ RA.

Proof.By induction on the derivation of Γ ` t : A.

• If the last rule isΓ, x : A ` t : B

Γ ` λx .t : A→ B(→I)

By IH, we have t ∈ RA. And... ????

We actually need a stronger induction hypothesis!

110

Page 313: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

PropositionGiven t such that x1 : A1, . . . , xn : An ` t : A is derivable, and for every terms ui ∈ RAi

,we have t[u/x ] = t[u1/x1, . . . , un/xn] ∈ RA.

Proof.By induction on the derivation of Γ ` t : A.

111

Page 314: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

PropositionGiven t such that x1 : A1, . . . , xn : An ` t : A is derivable, and for every terms ui ∈ RAi

,we have t[u/x ] = t[u1/x1, . . . , un/xn] ∈ RA.

Proof.By induction on the derivation of Γ ` t : A.

• If the last rule is

Γ ` xi : A(ax)

The xi [u/x ] = ui ∈ RAi.

111

Page 315: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

PropositionGiven t such that x1 : A1, . . . , xn : An ` t : A is derivable, and for every terms ui ∈ RAi

,we have t[u/x ] = t[u1/x1, . . . , un/xn] ∈ RA.

Proof.By induction on the derivation of Γ ` t : A.

• If the last rule isΓ ` t1 : A→ B Γ ` t2 : A

Γ ` t1 t2 : B(→E)

By IH, we have t1[u/x ] ∈ RA→B and t2[u/x ] ∈ RA.Therefore (t1 t2)[u/x ] = (t1[u/x ])(t2[u/x ]) ∈ RB by definition of RA→B .

111

Page 316: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

PropositionGiven t such that x1 : A1, . . . , xn : An ` t : A is derivable, and for every terms ui ∈ RAi

,we have t[u/x ] = t[u1/x1, . . . , un/xn] ∈ RA.

Proof.By induction on the derivation of Γ ` t : A.

• If the last rule isΓ, x : A ` t : B

Γ ` λx .t : A→ B(→I)

By IH, we have (t[u/x ])[v/x ] = t[u/x , v/x ] ∈ RB for every v ∈ RA.Therefore, λx .t ∈ RA→B by previous lemma.

111

Page 317: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Reducibility candidates

TheoremFor every t such that Γ ` t : A is derivable, we have t ∈ RA.

Proof.Suppose Γ = x1 : A1, . . . , xn : An.By (CR3), we have xi ∈ RAi

.By previous proposition, we have t = t[x1/x1, . . . , xn/xn] ∈ RA.

112

Page 318: INF551: Simply typed lambda-calculus · Puttingitalltogether Whatwehavedonesofar. 1. WehaveseenthattypesinOCamlcouldintuitivelybeinterpretedasformulas. 2. Wehaveformallydefinedwhatisaformulaandaproof

Strong normalizablility

TheoremFor every term t such that Γ ` t : A is derivable, t is strongly normalizable.

Proof.We have t ∈ RA, and thus t is SN by (CR1).

113