• Aucun résultat trouvé

Simply Typed λ-Calculus

N/A
N/A
Protected

Academic year: 2022

Partager "Simply Typed λ-Calculus"

Copied!
82
0
0

Texte intégral

(1)

Simply Typed λ-Calculus

Akim Demailleakim@lrde.epita.fr

EPITA — École Pour l’Informatique et les Techniques Avancées

June 10, 2016

(2)

About these lecture notes

Many of these slides are largely inspired from Andrew D. Ker’s lecture notes [Ker, 2005a, Ker, 2005b]. Some slides are even straightforward copies.

A. Demaille Simply Typedλ-Calculus 2 / 46

(3)

Simply Typed λ-Calculus

1 Types

2 λ: Type Assignments

A. Demaille Simply Typedλ-Calculus 3 / 46

(4)

Types

1 Types

Untypedλ-calculus Paradoxes

Church vs. Curry

2 λ: Type Assignments

A. Demaille Simply Typedλ-Calculus 4 / 46

(5)

Types

Alonzo Church (1903–1995) Haskell Curry (1900–1982)

A. Demaille Simply Typedλ-Calculus 5 / 46

(6)

Types

Types first appeared with

Curry (1934) for Combinatory Logic Church (1940)

Types are syntactic objects assigned to terms:

M :A M has typeA For instance:

I :A→A

A. Demaille Simply Typedλ-Calculus 6 / 46

(7)

Types

Types first appeared with

Curry (1934) for Combinatory Logic Church (1940)

Types are syntactic objects assigned to terms:

M :A M has typeA For instance:

I :A→A

A. Demaille Simply Typedλ-Calculus 6 / 46

(8)

Untyped λ-calculus

1 Types

Untypedλ-calculus Paradoxes

Church vs. Curry

2 λ: Type Assignments

A. Demaille Simply Typedλ-Calculus 7 / 46

(9)

λ-terms

Λ, set ofλ-terms x ∈ V x ∈Λ

M ∈Λ N∈Λ (MN)∈Λ

M ∈Λ

x∈ V (λx·M)∈Λ

A. Demaille Simply Typedλ-Calculus 8 / 46

(10)

λβ

The λβ Formal System M =M

M =N N =M

M =N N=L M =L M =M0 N =N0

MN =M0N0

M =N λx·M =λx·N

(λx·M)N= [N/x]M

A. Demaille Simply Typedλ-Calculus 9 / 46

(11)

Properties of λβ

β-reduction is Church-Rosser.

Any term has (at most) a unique NF.

β-reduction is notnormalizing.

Some terms have no NF (Ω).

A. Demaille Simply Typedλ-Calculus 10 / 46

(12)

Properties of λβ

β-reduction is Church-Rosser.

Any term has (at most) a unique NF.

β-reduction is notnormalizing.

Some terms have no NF (Ω).

A. Demaille Simply Typedλ-Calculus 10 / 46

(13)

Properties of λβ

β-reduction is Church-Rosser.

Any term has (at most) a unique NF.

β-reduction is notnormalizing.

Some terms have no NF (Ω).

A. Demaille Simply Typedλ-Calculus 10 / 46

(14)

Properties of λβ

β-reduction is Church-Rosser.

Any term has (at most) a unique NF.

β-reduction is notnormalizing.

Some terms have no NF (Ω).

A. Demaille Simply Typedλ-Calculus 10 / 46

(15)

Paradoxes

1 Types

Untypedλ-calculus Paradoxes

Church vs. Curry

2 λ: Type Assignments

A. Demaille Simply Typedλ-Calculus 11 / 46

(16)

Self application

What is the computational meaning of λx·xx?

Stop considering anything can be applied to anything A function and its argument have different behaviors

A. Demaille Simply Typedλ-Calculus 12 / 46

(17)

Self application

What is the computational meaning of λx·xx?

Stop considering anything can be applied to anything A function and its argument have different behaviors

A. Demaille Simply Typedλ-Calculus 12 / 46

(18)

Church vs. Curry

1 Types

Untypedλ-calculus Paradoxes

Church vs. Curry

2 λ: Type Assignments

A. Demaille Simply Typedλ-Calculus 13 / 46

(19)

Simple Types

A set of type variables α, β, . . .

A symbol→ for functions

α→α,α →(β→γ),(α→β)→γ, . . . Possibly constants for “primitive” types ιfor integers, etc.

A. Demaille Simply Typedλ-Calculus 14 / 46

(20)

Simple Types

A set of type variables α, β, . . .

A symbol→ for functions

α→α,α →(β→γ),(α→β)→γ, . . . Possibly constants for “primitive” types ιfor integers, etc.

A. Demaille Simply Typedλ-Calculus 14 / 46

(21)

Simple Types

A set of type variables α, β, . . .

A symbol→ for functions

α→α,α →(β→γ),(α→β)→γ, . . . Possibly constants for “primitive” types ιfor integers, etc.

A. Demaille Simply Typedλ-Calculus 14 / 46

(22)

Simple Types

By convention→ is right-associative:

α→β →γ =α→(β →γ)

This matches the right-associativity of λ:

λx·λy·M =λx·(λy·M)

A. Demaille Simply Typedλ-Calculus 15 / 46

(23)

Simple Types

By convention→ is right-associative:

α→β →γ =α→(β →γ)

This matches the right-associativity of λ:

λx·λy·M =λx·(λy·M)

A. Demaille Simply Typedλ-Calculus 15 / 46

(24)

Alonzo Style, or Haskell Way?

Church:

Typed λ-calculus x:α λxα·x :α→α

Curry:

λ-calculus with Types x :α λx·x:α→α

A. Demaille Simply Typedλ-Calculus 16 / 46

(25)

λ

: Type Assignments

1 Types

2 λ: Type Assignments Types

Type Deductions

Subject Reduction Theorem Reducibility

Typability

A. Demaille Simply Typedλ-Calculus 17 / 46

(26)

Types

1 Types

2 λ: Type Assignments Types

Type Deductions

Subject Reduction Theorem Reducibility

Typability

A. Demaille Simply Typedλ-Calculus 18 / 46

(27)

Simple Types

T V a set of type variables α, β, . . . Simple Types

The set T of typesσ, τ, . . .:

α∈ T V α ∈ T

σ ∈ T τ ∈ T (σ →τ)∈ T

A. Demaille Simply Typedλ-Calculus 19 / 46

(28)

Type Contexts

Statement

Astatement M :σ is a pair withM ∈Λ, σ∈ T. M is the subject,σ thepredicate.

Type Context, Basis

Atype context Γ is a finite set of statements over distinct variables {x11, . . .}.

Assignment

The variable x isassigned the typeσ in Γiff x:σ∈Γ.

A. Demaille Simply Typedλ-Calculus 20 / 46

(29)

Type Contexts

Statement

Astatement M :σ is a pair withM ∈Λ, σ∈ T. M is the subject,σ thepredicate.

Type Context, Basis

Atype context Γ is a finite set of statements over distinct variables {x11, . . .}.

Assignment

The variable x isassigned the typeσ in Γiff x:σ∈Γ.

A. Demaille Simply Typedλ-Calculus 20 / 46

(30)

Type Contexts

Statement

Astatement M :σ is a pair withM ∈Λ, σ∈ T. M is the subject,σ thepredicate.

Type Context, Basis

Atype context Γ is a finite set of statements over distinct variables {x11, . . .}.

Assignment

The variable x isassigned the typeσ in Γiff x:σ∈Γ.

A. Demaille Simply Typedλ-Calculus 20 / 46

(31)

Type Contexts

Type Context Restrictions

Γ−x is theΓwith all assignment x :σ removed.

ΓM is Γ−FV(M).

A. Demaille Simply Typedλ-Calculus 21 / 46

(32)

Type Deductions

1 Types

2 λ: Type Assignments Types

Type Deductions

Subject Reduction Theorem Reducibility

Typability

A. Demaille Simply Typedλ-Calculus 22 / 46

(33)

A Natural Presentation

Type derivations are trees built from the following nodes.

M :σ→τ N:σ

MN :τ

[

x :σ

]

··

· M :τ

λx·M :σ →τ

A. Demaille Simply Typedλ-Calculus 23 / 46

(34)

A Natural Presentation

Type derivations are trees built from the following nodes.

M :σ→τ N:σ MN :τ

[

x :σ

]

··

· M :τ

λx·M :σ →τ

A. Demaille Simply Typedλ-Calculus 23 / 46

(35)

A Natural Presentation

Type derivations are trees built from the following nodes.

M :σ→τ N:σ MN :τ

[

x :σ

]

··

· M :τ

λx·M :σ →τ

A. Demaille Simply Typedλ-Calculus 23 / 46

(36)

A Natural Presentation

Type derivations are trees built from the following nodes.

M :σ→τ N:σ MN :τ

[x :σ]

··

· M :τ λx·M :σ →τ

A. Demaille Simply Typedλ-Calculus 23 / 46

(37)

Type Statement

Type Statement

A statementM :σ isderivablefrom the type context Γ, Γ`M :σ

if there is a derivation of M :σ whose non-canceled assumptions are inΓ.

A. Demaille Simply Typedλ-Calculus 24 / 46

(38)

Type Statements

Prove

`λfx·f(fx) : (σ→σ)→σ→σ

[f :σ →σ](2)

[f :σ→σ](2) [x :σ](1) fx:σ

f(fx) :σ

(1) λx·f(fx) :σ →σ

(2)

λfx·f(fx) : (σ →σ)→σ→σ

A. Demaille Simply Typedλ-Calculus 25 / 46

(39)

Type Statements

Prove

`λfx·f(fx) : (σ→σ)→σ→σ

[f :σ →σ](2)

[f :σ→σ](2) [x:σ](1) fx:σ

f(fx) :σ

(1) λx·f(fx) :σ →σ

(2)

λfx·f(fx) : (σ →σ)→σ→σ

A. Demaille Simply Typedλ-Calculus 25 / 46

(40)

Type Statements

Prove

`λfx·f(fx) : (σ→σ)→σ→σ

[f :σ →σ](2)

[f :σ→σ](2) [x:σ](1) fx:σ

f(fx) :σ

(1) λx·f(fx) :σ →σ

(2) λfx·f(fx) : (σ →σ)→σ→σ

A. Demaille Simply Typedλ-Calculus 25 / 46

(41)

Type Statements

Prove

`λxy·x :σ→τ →σ

[x:σ](1) λy·x :τ →σ

(1)

λxy ·x :σ→τ →σ

A. Demaille Simply Typedλ-Calculus 26 / 46

(42)

Type Statements

Prove

`λxy·x :σ→τ →σ

[x :σ](1) λy·x :τ →σ

(1)

λxy ·x :σ→τ →σ

A. Demaille Simply Typedλ-Calculus 26 / 46

(43)

Type Statements

Prove

`λxy·x :σ→τ →σ

[x :σ](1) λy·x :τ →σ

(1) λxy ·x :σ→τ →σ

A. Demaille Simply Typedλ-Calculus 26 / 46

(44)

Alternative Presentation of Type Derivations

Type Derivations

{x :σ} 7→x :σ Γ7→M :σ→τ ∆7→N:σ

Γ∪∆7→MN :τ

Γ,∆consistent

Γ7→M :τ

Γ\ {x :σ} 7→λx·M :σ →τ Γ,{x :σ}consistent Γ7→M :τ

Γ,∆`M :τ

A. Demaille Simply Typedλ-Calculus 27 / 46

(45)

Alternative Presentation of Type Derivations

Type Derivations

{x :σ} 7→x :σ Γ7→M :σ→τ ∆7→N:σ

Γ∪∆7→MN :τ

Γ,∆consistent

Γ7→M :τ

Γ\ {x :σ} 7→λx·M :σ →τ Γ,{x :σ}consistent Γ7→M :τ

Γ,∆`M :τ

A. Demaille Simply Typedλ-Calculus 27 / 46

(46)

Type Statements

Prove

`λxy·x :σ→τ →σ

{x:σ} 7→x:σ {x :σ} 7→λy·x:τ →σ

7→λxy ·x :σ→τ →σ

[x:σ](1) λy·x :τ →σ

(1) λxy·x:σ →τ →σ

A. Demaille Simply Typedλ-Calculus 28 / 46

(47)

Type Statements

Prove

`λxy·x :σ→τ →σ

{x :σ} 7→x:σ {x:σ} 7→λy·x:τ →σ

7→λxy ·x :σ→τ →σ

[x:σ](1) λy·x :τ →σ

(1) λxy·x:σ →τ →σ

A. Demaille Simply Typedλ-Calculus 28 / 46

(48)

Type Statements

Prove

`λxy·x :σ→τ →σ

{x :σ} 7→x:σ {x:σ} 7→λy·x:τ →σ

7→λxy ·x :σ→τ →σ

[x:σ](1) λy·x :τ →σ

(1) λxy·x:σ →τ →σ

A. Demaille Simply Typedλ-Calculus 28 / 46

(49)

Type Statements

Prove

`λxy·x :σ→τ →σ

{x :σ} 7→x:σ {x:σ} 7→λy·x:τ →σ

7→λxy ·x :σ→τ →σ

[x:σ](1) λy·x :τ →σ

(1) λxy·x :σ →τ →σ

A. Demaille Simply Typedλ-Calculus 28 / 46

(50)

Type Statements

Type ω=λx·xx.

A. Demaille Simply Typedλ-Calculus 29 / 46

(51)

Type Statements

Type ω=λx·xx.

··

· 7→λx·xx :σ

A. Demaille Simply Typedλ-Calculus 29 / 46

(52)

Type Statements

Type ω=λx·xx.

··

·

{x :σ1} 7→xx :σ2

σ =σ1 →σ2

7→λx·xx :σ

A. Demaille Simply Typedλ-Calculus 29 / 46

(53)

Type Statements

Type ω=λx·xx.

··

·

{x :σ1} 7→x:τ →σ2

··

·

{x :σ1} 7→x :τ {x :σ1} 7→xx :σ2

σ =σ1→σ2 7→λx·xx :σ

A. Demaille Simply Typedλ-Calculus 29 / 46

(54)

Typability

Typability

A termM is typable if there exists a typeσ such that `M :σ.

ω,Ωare not typable.

S,K,I are typable.

Y is not typable!

A. Demaille Simply Typedλ-Calculus 30 / 46

(55)

Typability

Typability

A termM is typable if there exists a typeσ such that `M :σ.

ω,Ωare not typable.

S,K,I are typable.

Y is not typable!

A. Demaille Simply Typedλ-Calculus 30 / 46

(56)

Typability

Typability

A termM is typable if there exists a typeσ such that `M :σ.

ω,Ωare not typable.

S,K,I are typable.

Y is not typable!

A. Demaille Simply Typedλ-Calculus 30 / 46

(57)

Typability

Typability

A termM is typable if there exists a typeσ such that `M :σ.

ω,Ωare not typable.

S,K,I are typable.

Y is not typable!

A. Demaille Simply Typedλ-Calculus 30 / 46

(58)

Subject Construction Lemma I

Consider the derivationπ forM :σ.

If M =x, then Γ ={x :σ} and

π ={x :σ} 7→x :σ If M =NL, then

π =ΓN7→N:τ →σ ΓL7→L:τ Γ7→NL:σ

A. Demaille Simply Typedλ-Calculus 31 / 46

(59)

Subject Construction Lemma I

Consider the derivationπ forM :σ.

If M =x, then Γ ={x :σ} and

π ={x :σ} 7→x :σ If M =NL, then

π =ΓN7→N:τ →σ ΓL7→L:τ Γ7→NL:σ

A. Demaille Simply Typedλ-Calculus 31 / 46

(60)

Subject Construction Lemma I

Consider the derivationπ forM :σ.

If M =λx·N, thenσ=σ1 →σ2 and Ifx FV(N)

π=

Γ∪ {x:σ1} 7→N:σ2

Γ7→λx·N:σ1σ2

Ifx 6∈FV(N)

π=

Γ7→N:σ2

Γ7→λx·N:σ1σ2

A. Demaille Simply Typedλ-Calculus 32 / 46

(61)

Subject Construction Lemma I

Consider the derivationπ forM :σ.

If M =λx·N, thenσ=σ1 →σ2 and Ifx FV(N)

π=

Γ∪ {x:σ1} 7→N:σ2

Γ7→λx·N:σ1σ2

Ifx 6∈FV(N)

π=

Γ7→N:σ2

Γ7→λx·N:σ1σ2

A. Demaille Simply Typedλ-Calculus 32 / 46

(62)

Subject Construction Lemma I

Consider the derivationπ forM :σ.

If M =λx·N, thenσ=σ1 →σ2 and Ifx FV(N)

π=

Γ∪ {x:σ1} 7→N:σ2

Γ7→λx·N:σ1σ2

Ifx 6∈FV(N)

π=

Γ7→N:σ2

Γ7→λx·N:σ1σ2

A. Demaille Simply Typedλ-Calculus 32 / 46

(63)

Derivations are not Unique

They are for β-normal forms.

A. Demaille Simply Typedλ-Calculus 33 / 46

(64)

Subject Reduction Theorem

1 Types

2 λ: Type Assignments Types

Type Deductions

Subject Reduction Theorem Reducibility

Typability

A. Demaille Simply Typedλ-Calculus 34 / 46

(65)

Conversions and Types

α-Invariance

If Γ7→M :σ andM ≡αN thenΓ7→N :σ.

Substitution If

Γ∪ {x :τ}`M :σ,

∆`N:τ, Γ,∆consistent, x 6∈FV(N) then

Γ∪∆`[N/x]M :σ

A. Demaille Simply Typedλ-Calculus 35 / 46

(66)

Conversions and Types

α-Invariance

If Γ7→M :σ andM ≡αN thenΓ7→N :σ.

Substitution If

Γ∪ {x :τ}`M :σ,

∆`N:τ, Γ,∆consistent, x 6∈FV(N) then

Γ∪∆`[N/x]M :σ

A. Demaille Simply Typedλ-Calculus 35 / 46

(67)

Conversions and Types

α-Invariance

If Γ7→M :σ andM ≡αN thenΓ7→N :σ.

Substitution If

Γ∪ {x :τ}`M :σ,

∆`N:τ, Γ,∆consistent, x 6∈FV(N) then

Γ∪∆`[N/x]M :σ

A. Demaille Simply Typedλ-Calculus 35 / 46

(68)

Subject Reduction Theorem

Subject Reduction Theorem

If Γ`M :σ andM →β N thenΓ`N:σ.

What about the converse?

Subject Expansion

If Γ`N :σ andM →β N thenΓ`M :σ.

Ω is not typable, butKIΩ→I.

A. Demaille Simply Typedλ-Calculus 36 / 46

(69)

Subject Reduction Theorem

Subject Reduction Theorem

If Γ`M :σ andM →β N thenΓ`N:σ.

What about the converse?

Subject Expansion

If Γ`N :σ andM →β N thenΓ`M :σ.

Ω is not typable, butKIΩ→I.

A. Demaille Simply Typedλ-Calculus 36 / 46

(70)

Subject Reduction Theorem

Subject Reduction Theorem

If Γ`M :σ andM →β N thenΓ`N:σ.

What about the converse?

Subject Expansion

If Γ`N :σ andM →β N thenΓ`M :σ.

Ω is not typable, butKIΩ→I.

A. Demaille Simply Typedλ-Calculus 36 / 46

(71)

Subject Reduction Theorem

Subject Reduction Theorem

If Γ`M :σ andM →β N thenΓ`N:σ.

What about the converse?

Subject Expansion

If Γ`N :σ andM →β N thenΓ`M :σ.

Ωis not typable, but KIΩ→I.

A. Demaille Simply Typedλ-Calculus 36 / 46

(72)

Reducibility

1 Types

2 λ: Type Assignments Types

Type Deductions

Subject Reduction Theorem Reducibility

Typability

A. Demaille Simply Typedλ-Calculus 37 / 46

(73)

Types and Normalization

λ is Strongly Normalizing

All typable terms are β-strongly normalizing.

A. Demaille Simply Typedλ-Calculus 38 / 46

(74)

Typability

1 Types

2 λ: Type Assignments Types

Type Deductions

Subject Reduction Theorem Reducibility

Typability

A. Demaille Simply Typedλ-Calculus 39 / 46

(75)

` M : σ suffices

Note that with Γ ={x11, . . . ,xnn} Γ`M :τ is equivalent to

`λx1. . .xn·M :σ1 →. . .→σn→τ

A. Demaille Simply Typedλ-Calculus 40 / 46

(76)

Decidability of Type Assignment

Type checking GivenM, σ, does `M :σ?

`M :σ?

Typability GivenM, does there exist σ such that `M :σ?

`M :?

Inhabitation Givenσ, does there exist M such that `M :σ?

`? :σ

A. Demaille Simply Typedλ-Calculus 41 / 46

(77)

Decidability of Type Assignment

Type Checking is Decidable

It is decidable whether a statement of λ is provable.

Typability is Decidable

It is decidable whether a term of λ has a type.

A. Demaille Simply Typedλ-Calculus 42 / 46

(78)

Decidability of Type Assignment

Type Checking is Decidable

It is decidable whether a statement of λ is provable.

Typability is Decidable

It is decidable whether a term of λ has a type.

A. Demaille Simply Typedλ-Calculus 42 / 46

(79)

Types are not Unique...

Typable terms do not have unique types.

{x:σ} 7→x :σ {x :σ} 7→λy·x :τ →σ

7→K :σ →τ →σ

is valid for anyσ, τ, including σ=σ0→σ00, τ = (τ0 →τ00)→τ0 etc.

A. Demaille Simply Typedλ-Calculus 43 / 46

(80)

Types are not Unique. . .

but some are more Unique than others ...

All the types of K are instances ofσ →τ →σ.

A. Demaille Simply Typedλ-Calculus 44 / 46

(81)

Bibliography Notes

[Ker, 2005a] Complete and readable lecture notes on λ-calculus. Uses conventions different from ours.

[Ker, 2005b] Additional information, including slides.

[Barendregt and Barendsen, 2000] A classical introduction to λ-calculus.

A. Demaille Simply Typedλ-Calculus 45 / 46

(82)

Bibliography I

Barendregt, H. and Barendsen, E. (2000).

Introduction to lambda calculus.

http:

//www.cs.ru.nl/~erikb/onderwijs/T3/materiaal/lambda.pdf.

Ker, A. D. (2005a).

Lambda calculus and types.

http://web.comlab.ox.ac.uk/oucl/work/andrew.ker/

lambda-calculus-notes-full-v3.pdf.

Ker, A. D. (2005b).

Lambda calculus notes.

http://web.comlab.ox.ac.uk/oucl/work/andrew.ker/.

A. Demaille Simply Typedλ-Calculus 46 / 46

Références

Documents relatifs

The (search) rule asserts that the type of the extracted method body of n is a func- tion that accept as input argument an object, which will contains the methods m, n, together

The strong normalization theorem of second order classical natural deduction [20] is based on a lemma known as the correctness result, which stipulates that each term is in

Since the proofs in the implicative propositional classical logic can be coded by Parigot’s λµ-terms and the cut elimination corresponds to the λµ-reduction, the result can be seen as

Stop considering anything can be applied to anything A function and its argument have different

A revised completeness result for the simply typed λµ-calculus using realizability semantics.. Karim Nour,

Delia KESNER IRIF, CNRS et Universit ´e Paris kesner@irif.fr www.irif.fr/˜kesner.. Typed

Let t be a typed term and let σ be a type preserving substitution mapping all the free variables of t to terms in SC.. Since σ ∪ {x\x} verifies the second hypothesis of the lemma,

Abstraction is always binary: the names (or atoms) a that appear on the left-hand side are bound, and their scope is the expression e that appears on the right-hand side.... A