Simply Typed -Calculus Akim Demaille akim@lrde.epita.fr EPITA cole - - PowerPoint PPT Presentation

simply typed calculus
SMART_READER_LITE
LIVE PREVIEW

Simply Typed -Calculus Akim Demaille akim@lrde.epita.fr EPITA cole - - PowerPoint PPT Presentation

Simply Typed -Calculus Akim Demaille akim@lrde.epita.fr EPITA cole Pour lInformatique et les Techniques Avances June 10, 2016 About these lecture notes Many of these slides are largely inspired from Andrew D. Kers lecture notes


slide-1
SLIDE 1

Simply Typed λ-Calculus

Akim Demaille akim@lrde.epita.fr

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

June 10, 2016

slide-2
SLIDE 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

slide-3
SLIDE 3

Simply Typed λ-Calculus

1

Types

2

λ→: Type Assignments

  • A. Demaille

Simply Typed λ-Calculus 3 / 46

slide-4
SLIDE 4

Types

1

Types Untyped λ-calculus Paradoxes Church vs. Curry

2

λ→: Type Assignments

  • A. Demaille

Simply Typed λ-Calculus 4 / 46

slide-5
SLIDE 5

Types

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

  • A. Demaille

Simply Typed λ-Calculus 5 / 46

slide-6
SLIDE 6

Types

Types first appeared with Curry (1934) for Combinatory Logic Church (1940) Types are syntactic objects assigned to terms: M : A M has type A For instance: I : A → A

  • A. Demaille

Simply Typed λ-Calculus 6 / 46

slide-7
SLIDE 7

Types

Types first appeared with Curry (1934) for Combinatory Logic Church (1940) Types are syntactic objects assigned to terms: M : A M has type A For instance: I : A → A

  • A. Demaille

Simply Typed λ-Calculus 6 / 46

slide-8
SLIDE 8

Untyped λ-calculus

1

Types Untyped λ-calculus Paradoxes Church vs. Curry

2

λ→: Type Assignments

  • A. Demaille

Simply Typed λ-Calculus 7 / 46

slide-9
SLIDE 9

λ-terms

Λ, set of λ-terms

x ∈ V x ∈ Λ M ∈ Λ N ∈ Λ (MN) ∈ Λ M ∈ Λ x ∈ V (λx · M) ∈ Λ

  • A. Demaille

Simply Typed λ-Calculus 8 / 46

slide-10
SLIDE 10

λβ

The λβ Formal System

M = M M = N N = M M = N N = L M = L M = M′ N = N′ MN = M′N′ M = N λx · M = λx · N (λx · M)N = [N/x]M

  • A. Demaille

Simply Typed λ-Calculus 9 / 46

slide-11
SLIDE 11

Properties of λβ

β-reduction is Church-Rosser. Any term has (at most) a unique NF. β-reduction is not normalizing. Some terms have no NF (Ω).

  • A. Demaille

Simply Typed λ-Calculus 10 / 46

slide-12
SLIDE 12

Properties of λβ

β-reduction is Church-Rosser. Any term has (at most) a unique NF. β-reduction is not normalizing. Some terms have no NF (Ω).

  • A. Demaille

Simply Typed λ-Calculus 10 / 46

slide-13
SLIDE 13

Properties of λβ

β-reduction is Church-Rosser. Any term has (at most) a unique NF. β-reduction is not normalizing. Some terms have no NF (Ω).

  • A. Demaille

Simply Typed λ-Calculus 10 / 46

slide-14
SLIDE 14

Properties of λβ

β-reduction is Church-Rosser. Any term has (at most) a unique NF. β-reduction is not normalizing. Some terms have no NF (Ω).

  • A. Demaille

Simply Typed λ-Calculus 10 / 46

slide-15
SLIDE 15

Paradoxes

1

Types Untyped λ-calculus Paradoxes Church vs. Curry

2

λ→: Type Assignments

  • A. Demaille

Simply Typed λ-Calculus 11 / 46

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 18

Church vs. Curry

1

Types Untyped λ-calculus Paradoxes Church vs. Curry

2

λ→: Type Assignments

  • A. Demaille

Simply Typed λ-Calculus 13 / 46

slide-19
SLIDE 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

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 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

slide-25
SLIDE 25

λ→: Type Assignments

1

Types

2

λ→: Type Assignments Types Type Deductions Subject Reduction Theorem Reducibility Typability

  • A. Demaille

Simply Typed λ-Calculus 17 / 46

slide-26
SLIDE 26

Types

1

Types

2

λ→: Type Assignments Types Type Deductions Subject Reduction Theorem Reducibility Typability

  • A. Demaille

Simply Typed λ-Calculus 18 / 46

slide-27
SLIDE 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

slide-28
SLIDE 28

Type Contexts

Statement

A statement M : σ is a pair with M ∈ Λ, σ ∈ T . M is the subject, σ the predicate.

Type Context, Basis

A type context Γ is a finite set of statements over distinct variables {x1 : σ1, . . .}.

Assignment

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

  • A. Demaille

Simply Typed λ-Calculus 20 / 46

slide-29
SLIDE 29

Type Contexts

Statement

A statement M : σ is a pair with M ∈ Λ, σ ∈ T . M is the subject, σ the predicate.

Type Context, Basis

A type context Γ is a finite set of statements over distinct variables {x1 : σ1, . . .}.

Assignment

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

  • A. Demaille

Simply Typed λ-Calculus 20 / 46

slide-30
SLIDE 30

Type Contexts

Statement

A statement M : σ is a pair with M ∈ Λ, σ ∈ T . M is the subject, σ the predicate.

Type Context, Basis

A type context Γ is a finite set of statements over distinct variables {x1 : σ1, . . .}.

Assignment

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

  • A. Demaille

Simply Typed λ-Calculus 20 / 46

slide-31
SLIDE 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

slide-32
SLIDE 32

Type Deductions

1

Types

2

λ→: Type Assignments Types Type Deductions Subject Reduction Theorem Reducibility Typability

  • A. Demaille

Simply Typed λ-Calculus 22 / 46

slide-33
SLIDE 33

A Natural Presentation

Type derivations are trees built from the following nodes. M : σ → τ N : σ

  • A. Demaille

Simply Typed λ-Calculus 23 / 46

slide-34
SLIDE 34

A Natural Presentation

Type derivations are trees built from the following nodes. M : σ → τ N : σ MN : τ

  • A. Demaille

Simply Typed λ-Calculus 23 / 46

slide-35
SLIDE 35

A Natural Presentation

Type derivations are trees built from the following nodes. M : σ → τ N : σ MN : τ x : σ · · · M : τ

  • A. Demaille

Simply Typed λ-Calculus 23 / 46

slide-36
SLIDE 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

slide-37
SLIDE 37

Type Statement

Type Statement

A statement M : σ is derivable from the type context Γ, Γ ⊢ M : σ if there is a derivation of M : σ whose non-canceled assumptions are in Γ.

  • A. Demaille

Simply Typed λ-Calculus 24 / 46

slide-38
SLIDE 38

Type Statements

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

  • A. Demaille

Simply Typed λ-Calculus 25 / 46

slide-39
SLIDE 39

Type Statements

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

  • A. Demaille

Simply Typed λ-Calculus 25 / 46

slide-40
SLIDE 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

slide-41
SLIDE 41

Type Statements

Prove ⊢ λxy · x : σ → τ → σ

  • A. Demaille

Simply Typed λ-Calculus 26 / 46

slide-42
SLIDE 42

Type Statements

Prove ⊢ λxy · x : σ → τ → σ λxy · x : σ → τ → σ

  • A. Demaille

Simply Typed λ-Calculus 26 / 46

slide-43
SLIDE 43

Type Statements

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

  • A. Demaille

Simply Typed λ-Calculus 26 / 46

slide-44
SLIDE 44

Alternative Presentation of Type Derivations

Type Derivations

{x : σ} → x : σ Γ → M : σ → τ ∆ → N : σ Γ ∪ ∆ → MN : τ Γ, ∆ consistent Γ → M : τ Γ \ {x : σ} → λx · M : σ → τ Γ, {x : σ} consistent Γ → M : τ Γ, ∆ ⊢ M : τ

  • A. Demaille

Simply Typed λ-Calculus 27 / 46

slide-45
SLIDE 45

Alternative Presentation of Type Derivations

Type Derivations

{x : σ} → x : σ Γ → M : σ → τ ∆ → N : σ Γ ∪ ∆ → MN : τ Γ, ∆ consistent Γ → M : τ Γ \ {x : σ} → λx · M : σ → τ Γ, {x : σ} consistent Γ → M : τ Γ, ∆ ⊢ M : τ

  • A. Demaille

Simply Typed λ-Calculus 27 / 46

slide-46
SLIDE 46

Type Statements

Prove ⊢ λxy · x : σ → τ → σ

  • A. Demaille

Simply Typed λ-Calculus 28 / 46

slide-47
SLIDE 47

Type Statements

Prove ⊢ λxy · x : σ → τ → σ → λxy · x : σ → τ → σ

  • A. Demaille

Simply Typed λ-Calculus 28 / 46

slide-48
SLIDE 48

Type Statements

Prove ⊢ λxy · x : σ → τ → σ {x : σ} → x : σ {x : σ} → λy · x : τ → σ → λxy · x : σ → τ → σ

  • A. Demaille

Simply Typed λ-Calculus 28 / 46

slide-49
SLIDE 49

Type Statements

Prove ⊢ λxy · x : σ → τ → σ {x : σ} → x : σ {x : σ} → λy · x : τ → σ → λxy · x : σ → τ → σ [x : σ](1) λy · x : τ → σ (1) λxy · x : σ → τ → σ

  • A. Demaille

Simply Typed λ-Calculus 28 / 46

slide-50
SLIDE 50

Type Statements

Type ω = λx · xx.

  • A. Demaille

Simply Typed λ-Calculus 29 / 46

slide-51
SLIDE 51

Type Statements

Type ω = λx · xx. · · · → λx · xx : σ

  • A. Demaille

Simply Typed λ-Calculus 29 / 46

slide-52
SLIDE 52

Type Statements

Type ω = λx · xx. · · · {x : σ1} → xx : σ2 σ = σ1 → σ2 → λx · xx : σ

  • A. Demaille

Simply Typed λ-Calculus 29 / 46

slide-53
SLIDE 53

Type Statements

Type ω = λx · xx. · · · {x : σ1} → x : τ → σ2 · · · {x : σ1} → x : τ {x : σ1} → xx : σ2 σ = σ1 → σ2 → λx · xx : σ

  • A. Demaille

Simply Typed λ-Calculus 29 / 46

slide-54
SLIDE 54

Typability

Typability

A term M 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

slide-55
SLIDE 55

Typability

Typability

A term M 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

slide-56
SLIDE 56

Typability

Typability

A term M 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

slide-57
SLIDE 57

Typability

Typability

A term M 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

slide-58
SLIDE 58

Subject Construction Lemma I

Consider the derivation π for M : σ. If M = x, then Γ = {x : σ} and π = {x : σ} → x : σ If M = NL, then π = Γ ↾ N → N : τ → σ Γ ↾ L → L : τ Γ → NL : σ

  • A. Demaille

Simply Typed λ-Calculus 31 / 46

slide-59
SLIDE 59

Subject Construction Lemma I

Consider the derivation π for M : σ. If M = x, then Γ = {x : σ} and π = {x : σ} → x : σ If M = NL, then π = Γ ↾ N → N : τ → σ Γ ↾ L → L : τ Γ → NL : σ

  • A. Demaille

Simply Typed λ-Calculus 31 / 46

slide-60
SLIDE 60

Subject Construction Lemma I

Consider the derivation π for M : σ. If M = λx · N, then σ = σ1 → σ2 and

If x ∈ FV(N) π = Γ ∪ {x : σ1} → N : σ2 Γ → λx · N : σ1 → σ2 If x ∈ FV(N) π = Γ → N : σ2 Γ → λx · N : σ1 → σ2

  • A. Demaille

Simply Typed λ-Calculus 32 / 46

slide-61
SLIDE 61

Subject Construction Lemma I

Consider the derivation π for M : σ. If M = λx · N, then σ = σ1 → σ2 and

If x ∈ FV(N) π = Γ ∪ {x : σ1} → N : σ2 Γ → λx · N : σ1 → σ2 If x ∈ FV(N) π = Γ → N : σ2 Γ → λx · N : σ1 → σ2

  • A. Demaille

Simply Typed λ-Calculus 32 / 46

slide-62
SLIDE 62

Subject Construction Lemma I

Consider the derivation π for M : σ. If M = λx · N, then σ = σ1 → σ2 and

If x ∈ FV(N) π = Γ ∪ {x : σ1} → N : σ2 Γ → λx · N : σ1 → σ2 If x ∈ FV(N) π = Γ → N : σ2 Γ → λx · N : σ1 → σ2

  • A. Demaille

Simply Typed λ-Calculus 32 / 46

slide-63
SLIDE 63

Derivations are not Unique

They are for β-normal forms.

  • A. Demaille

Simply Typed λ-Calculus 33 / 46

slide-64
SLIDE 64

Subject Reduction Theorem

1

Types

2

λ→: Type Assignments Types Type Deductions Subject Reduction Theorem Reducibility Typability

  • A. Demaille

Simply Typed λ-Calculus 34 / 46

slide-65
SLIDE 65

Conversions and Types

α-Invariance

If Γ → M : σ and M ≡α N then Γ → N : σ.

Substitution

If Γ ∪ {x : τ} ⊢ M : σ, ∆ ⊢ N : τ, Γ, ∆ consistent, x ∈ FV(N) then Γ ∪ ∆ ⊢ [N/x]M : σ

  • A. Demaille

Simply Typed λ-Calculus 35 / 46

slide-66
SLIDE 66

Conversions and Types

α-Invariance

If Γ → M : σ and M ≡α N then Γ → N : σ.

Substitution

If Γ ∪ {x : τ} ⊢ M : σ, ∆ ⊢ N : τ, Γ, ∆ consistent, x ∈ FV(N) then Γ ∪ ∆ ⊢ [N/x]M : σ

  • A. Demaille

Simply Typed λ-Calculus 35 / 46

slide-67
SLIDE 67

Conversions and Types

α-Invariance

If Γ → M : σ and M ≡α N then Γ → N : σ.

Substitution

If Γ ∪ {x : τ} ⊢ M : σ, ∆ ⊢ N : τ, Γ, ∆ consistent, x ∈ FV(N) then Γ ∪ ∆ ⊢ [N/x]M : σ

  • A. Demaille

Simply Typed λ-Calculus 35 / 46

slide-68
SLIDE 68

Subject Reduction Theorem

Subject Reduction Theorem

If Γ ⊢ M : σ and M →β N then Γ ⊢ N : σ. What about the converse?

Subject Expansion

If Γ ⊢ N : σ and M →β N then Γ ⊢ M : σ.

  • A. Demaille

Simply Typed λ-Calculus 36 / 46

slide-69
SLIDE 69

Subject Reduction Theorem

Subject Reduction Theorem

If Γ ⊢ M : σ and M →β N then Γ ⊢ N : σ. What about the converse?

Subject Expansion

If Γ ⊢ N : σ and M →β N then Γ ⊢ M : σ.

  • A. Demaille

Simply Typed λ-Calculus 36 / 46

slide-70
SLIDE 70

Subject Reduction Theorem

Subject Reduction Theorem

If Γ ⊢ M : σ and M →β N then Γ ⊢ N : σ. What about the converse?

Subject Expansion

If Γ ⊢ N : σ and M →β N then Γ ⊢ M : σ.

  • A. Demaille

Simply Typed λ-Calculus 36 / 46

slide-71
SLIDE 71

Subject Reduction Theorem

Subject Reduction Theorem

If Γ ⊢ M : σ and M →β N then Γ ⊢ N : σ. What about the converse?

Subject Expansion

If Γ ⊢ N : σ and M →β N then Γ ⊢ M : σ. Ω is not typable, but KIΩ → I.

  • A. Demaille

Simply Typed λ-Calculus 36 / 46

slide-72
SLIDE 72

Reducibility

1

Types

2

λ→: Type Assignments Types Type Deductions Subject Reduction Theorem Reducibility Typability

  • A. Demaille

Simply Typed λ-Calculus 37 / 46

slide-73
SLIDE 73

Types and Normalization

λ→ is Strongly Normalizing

All typable terms are β-strongly normalizing.

  • A. Demaille

Simply Typed λ-Calculus 38 / 46

slide-74
SLIDE 74

Typability

1

Types

2

λ→: Type Assignments Types Type Deductions Subject Reduction Theorem Reducibility Typability

  • A. Demaille

Simply Typed λ-Calculus 39 / 46

slide-75
SLIDE 75

⊢ M : σ suffices

Note that with Γ = {x1 : σ1, . . . , xn : σn} Γ ⊢ M : τ is equivalent to ⊢ λx1 . . . xn · M : σ1 → . . . → σn → τ

  • A. Demaille

Simply Typed λ-Calculus 40 / 46

slide-76
SLIDE 76

Decidability of Type Assignment

Type checking Given M, σ, does ⊢ M : σ? ⊢ M : σ? Typability Given M, does there exist σ such that ⊢ M : σ? ⊢ M :? Inhabitation Given σ, does there exist M such that ⊢ M : σ? ⊢? : σ

  • A. Demaille

Simply Typed λ-Calculus 41 / 46

slide-77
SLIDE 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

slide-78
SLIDE 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

slide-79
SLIDE 79

Types are not Unique...

Typable terms do not have unique types. {x : σ} → x : σ {x : σ} → λy · x : τ → σ → K : σ → τ → σ is valid for any σ, τ, including σ = σ′ → σ′′, τ = (τ ′ → τ ′′) → τ ′ etc.

  • A. Demaille

Simply Typed λ-Calculus 43 / 46

slide-80
SLIDE 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

slide-81
SLIDE 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

slide-82
SLIDE 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