On the Power of Coercion Abstraction Julien Cretin Didier Rmy - - PowerPoint PPT Presentation

on the power of coercion abstraction
SMART_READER_LITE
LIVE PREVIEW

On the Power of Coercion Abstraction Julien Cretin Didier Rmy - - PowerPoint PPT Presentation

On the Power of Coercion Abstraction Julien Cretin Didier Rmy INRIA January 26, 2012 1 / 36 Why study coercions? People have often used similar mechanisms, called coercions or type conversions, to explain non-trivial type system features.


slide-1
SLIDE 1

On the Power of Coercion Abstraction

Julien Cretin Didier Rémy INRIA January 26, 2012

1 / 36

slide-2
SLIDE 2

Why study coercions?

People have often used similar mechanisms, called coercions or type conversions, to explain non-trivial type system features.

2 / 36

slide-3
SLIDE 3

Why study coercions?

People have often used similar mechanisms, called coercions or type conversions, to explain non-trivial type system features. These techniques have a lot in common, but also differ in some details. Can we understand them as several instances of the same framework and use it to more easily design new type system features?

2 / 36

slide-4
SLIDE 4

Why study coercions?

People have often used similar mechanisms, called coercions or type conversions, to explain non-trivial type system features. These techniques have a lot in common, but also differ in some details. Can we understand them as several instances of the same framework and use it to more easily design new type system features? In this work, we restrict to erasable coercions (i.e. coercions without computational content).

2 / 36

slide-5
SLIDE 5

Intuition: Goal

Let’s design a type system to type the following untyped lambda term: (λx.x x) (λx.x) We can graphically represent it bottom-up like that: @ λ x x λ x @ x x

3 / 36

slide-6
SLIDE 6

Intuition: Typing rules

The type system necessarily gives typing rules for the untyped constructs:

◮ variable: x ◮ abstraction: λx.M ◮ application: M N

We choose simple types for illustration.

4 / 36

slide-7
SLIDE 7

Intuition: Graphical typing rules

We can annotate the graphical untyped constructs to obtain their graphical typing rule: Γ ⊢ M : τ → σ Γ ⊢ N : τ Γ ⊢ M N : σ @ M N σ Γ τ → σ Γ τ Γ

5 / 36

slide-8
SLIDE 8

Intuition: Graphical typing rules

We can annotate the graphical untyped constructs to obtain their graphical typing rule: Γ, (x : τ) ⊢ M : σ Γ ⊢ λ(x : τ) M : τ → σ λ x : τ M τ → σ Γ σ Γ, (x : τ)

5 / 36

slide-9
SLIDE 9

Intuition: Graphical typing rules

We can annotate the graphical untyped constructs to obtain their graphical typing rule: Γ1, (x : τ), Γ2 ⊢ x : τ x Γ1, (x : τ), Γ2 τ

5 / 36

slide-10
SLIDE 10

Intuition: Simply-typed lambda calculus

λ x @ x x

6 / 36

slide-11
SLIDE 11

Intuition: Simply-typed lambda calculus

λ x : τ @ x x τ → σ Γ σ Γ, (x : τ)

6 / 36

slide-12
SLIDE 12

Intuition: Simply-typed lambda calculus

λ x : τ @ x x τ → σ Γ σ Γ, (x : τ) σ Γ, (x : τ)

6 / 36

slide-13
SLIDE 13

Intuition: Simply-typed lambda calculus

λ x : τ @ x x τ → σ Γ σ Γ, (x : τ)

6 / 36

slide-14
SLIDE 14

Intuition: Simply-typed lambda calculus

λ x : τ @ x x τ → σ Γ σ Γ, (x : τ) ρ → σ Γ, (x : τ) ρ Γ, (x : τ)

6 / 36

slide-15
SLIDE 15

Intuition: Simply-typed lambda calculus

λ x : τ @ x x τ → σ Γ σ Γ, (x : τ) ρ → σ Γ, (x : τ) ρ Γ, (x : τ) τ Γ, (x : τ)

6 / 36

slide-16
SLIDE 16

Intuition: Simply-typed lambda calculus

λ x : τ @ x x τ → σ Γ σ Γ, (x : τ) τ → σ Γ, (x : τ) τ Γ, (x : τ) τ Γ, (x : τ)

6 / 36

slide-17
SLIDE 17

Intuition: Simply-typed lambda calculus

λ x : τ @ x x τ → σ Γ σ Γ, (x : τ) τ → σ Γ, (x : τ) τ Γ, (x : τ)

6 / 36

slide-18
SLIDE 18

Intuition: Simply-typed lambda calculus

λ x : τ @ x x τ → σ Γ σ Γ, (x : τ) τ → σ Γ, (x : τ) τ Γ, (x : τ) τ Γ, (x : τ)

6 / 36

slide-19
SLIDE 19

Intuition: Simply-typed lambda calculus

λ x : τ @ x x τ → σ Γ σ Γ, (x : τ) τ → σ Γ, (x : τ) τ Γ, (x : τ) τ Γ, (x : τ)

ERROR

6 / 36

slide-20
SLIDE 20

Intuition: Type system features

Terms should be allowed to have several types.

7 / 36

slide-21
SLIDE 21

Intuition: Type system features

Terms should be allowed to have several types. Several type system features can represent multiple types:

◮ intersection types, ◮ polymorphism, ◮ subtyping, or ◮ dependent types.

We choose polymorphism for illustration.

7 / 36

slide-22
SLIDE 22

Intuition: ∀-elim

λ x : τ @ ·τ x x τ → τ Γ τ Γ′ τ → τ Γ′ ∀α. α → α Γ′ τ Γ′ Polymorphism elimination can be seen as a coercion (which is an erasable type conversion): Γ′ ⊢ x : ∀α. α → α Γ′ ⊢ x τ : τ → τ With τ ∀α. α → α and Γ′ Γ, (x : τ).

8 / 36

slide-23
SLIDE 23

Intuition: ∀-intro

Polymorphism introduction may extend the environment: so coercions may in fact change the whole typing, not just types! Type system features are typing conversions. λ x x Untyped term: λx.x

9 / 36

slide-24
SLIDE 24

Intuition: ∀-intro

Polymorphism introduction may extend the environment: so coercions may in fact change the whole typing, not just types! Type system features are typing conversions. Λ α λ x : α x ∀α. α → α Γ α → α Γ, α α Γ, α, (x : α) Typing derivation: Γ, α, (x : α) ⊢ x : α Γ, α ⊢ λ(x : α) x : α → α Γ ⊢ Λα λ(x : α) x : ∀α. α → α We can now pass this term to (λx.x x) as wanted.

9 / 36

slide-25
SLIDE 25

Coercions

A one-node coercion P, drawn in red , is a one-node erasable retyping context. P M τ Γ, ∆ σ Γ

◮ retyping:

Γ, ∆ ⊢ M : τ Γ ⊢ P[M] : σ where M and P[M] are explicitly-typed version of the same implicit term.

10 / 36

slide-26
SLIDE 26

Coercions

A one-node coercion P, drawn in red , is a one-node erasable retyping context. P M τ Γ, ∆ σ Γ

◮ retyping:

Γ, ∆ ⊢ M : τ Γ ⊢ P[M] : σ where M and P[M] are explicitly-typed version of the same implicit term.

◮ erasable: P doesn’t modify or block

the reduction. It is purely static.

10 / 36

slide-27
SLIDE 27

Coercions

A coercion G is a sequence of one-node coercions. Λ α Λ β · α → β Λ α Λ β · α → β G We fill the hole with a diamond: G = Λα Λβ ♦ (α → β)

11 / 36

slide-28
SLIDE 28

Erasability

The erasing function ⌊·⌋ keeps the blue parts and removes both the annotations and the red nodes. @ Λ α λ x : α x λ x : τ @ τ x x τ Γ τ Γ α → α Γ, α α Γ, α, (x : α) τ → τ Γ τ Γ, (x : τ) τ → τ Γ, (x : τ) τ Γ, (x : τ) τ Γ, (x : τ)

12 / 36

slide-29
SLIDE 29

Erasability

The erasing function ⌊·⌋ keeps the blue parts and removes both the annotations and the red nodes. @ Λ α λ x : α x λ x : τ @ τ x x

12 / 36

slide-30
SLIDE 30

Erasability

The erasing function ⌊·⌋ keeps the blue parts and removes both the annotations and the red nodes. @ λ x x λ x @ x x

12 / 36

slide-31
SLIDE 31

Bisimulation

The reduction is labelled:

◮ β-reduction involves only blue nodes ◮ ι-reduction involves at least one red node

13 / 36

slide-32
SLIDE 32

Bisimulation

The reduction is labelled:

◮ β-reduction involves only blue nodes ◮ ι-reduction involves at least one red node

We want a bisimulation up to ι-steps: M N ⌊M⌋ ⌊N⌋ β ⌊·⌋ ⌊·⌋ M N ⌊M⌋ ι ⌊·⌋ ⌊·⌋ Forward simulation The forward simulation tells that coercions do not contribute to computation.

13 / 36

slide-33
SLIDE 33

Bisimulation

The reduction is labelled:

◮ β-reduction involves only blue nodes ◮ ι-reduction involves at least one red node

We want a bisimulation up to ι-steps: M N ⌊M⌋ ⌊N⌋ β ⌊·⌋ ⌊·⌋ M N ⌊M⌋ ι ⌊·⌋ ⌊·⌋ M N ⌊M⌋ ⌊N⌋ ι ⋆ β ⌊·⌋ ⌊·⌋ Forward simulation Backward simulation The forward simulation tells that coercions do not contribute to computation. The backward simulation tells that coercions cannot block the

  • computation. (Thus, values remain values after erasure.)

13 / 36

slide-34
SLIDE 34

Coercion judgments

We give the following judgment for coercions: Γ ⊢ G : τ ⊲ σ Γ σ Γ, ∆ τ G

14 / 36

slide-35
SLIDE 35

System F

τ, σ ::= τ → σ | α | ∀α.τ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ G ::= Λα G | G τ Polymorphism: (Λα M) τ ι M[α ← τ] Λα · M ∀α. τ Γ τ Γ, α · τ M σ[α ← τ] Γ ∀α. σ Γ

15 / 36

slide-36
SLIDE 36

System F

η τ, σ ::= τ → σ | α | ∀α.τ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM G ::= Λα G | G τ | G1G2 Coercion application: (we want GM ⋆

ι G[♦ ← M])

G· M σ Γ τ Γ if G σ Γ τ

15 / 36

slide-37
SLIDE 37

System F

η τ, σ ::= τ → σ | α | ∀α.τ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM G ::= Λα G | G τ | G1G2 | ♦τ Reflexivity: ♦τM ι M ♦ τ Γ τ

15 / 36

slide-38
SLIDE 38

System F

η τ, σ ::= τ → σ | α | ∀α.τ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM G ::= Λα G | G τ | G1G2 | ♦τ | G1

τ

→ G2 Arrow congruence (subtyping): (G1

τ ′

1

→ G2)λ(x : τ1) M ι λ(x : τ ′

1) G2M[x ← G1x]

G1 → G2 τ ′

1 → τ ′ 2

Γ τ1 → τ2 if G1 τ1 Γ τ ′

1

and G2 τ ′

2

Γ τ2

15 / 36

slide-39
SLIDE 39

System F

η τ, σ ::= τ → σ | α | ∀α.τ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM G ::= Λα G | G τ | G1G2 | ♦τ | G1

τ

→ G2 | Dist∀α.

τ→σ

It permutes Λα and λ(x : τ) Dist∀α.

τ ′→σ′Λα λ(x : τ) M ι λ(x : τ) Λα M

Dist∀α.

τ→σ

τ → ∀α. σ Γ ∀α. τ → σ with α / ∈ ftv(τ)

15 / 36

slide-40
SLIDE 40

System F

η τ, σ ::= τ → σ | α | ∀α.τ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM G ::= Λα G | G τ | G1G2 | ♦τ | G1

τ

→ G2 | Dist∀α.

τ→σ

We now have described F

η (using an explicit variant of

Mitchell’s presentation). F

η models subtyping which is at the essence of F <:, but it is

not sufficient to model F

<: itself.

We add coercion abstraction for that purpose.

15 / 36

slide-41
SLIDE 41

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM G ::= Λα G | G τ | G1G2 | ♦τ | G1

τ

→ G2 | Dist∀α.

τ→σ

15 / 36

slide-42
SLIDE 42

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM | Λ(c : ϕ) M | M{G} G ::= Λα G | G τ | G1G2 | Λ(c : ϕ) G | G{G ′} | ♦τ | G1

τ

→ G2 | Dist∀α.

τ→σ

15 / 36

slide-43
SLIDE 43

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM | Λ(c : ϕ) M | M{G} G ::= Λα G | G τ | G1G2 | Λ(c : ϕ) G | G{G ′} | ♦τ | G1

τ

→ G2 | Dist∀α.

τ→σ

Coercion abstraction: (Λ(c : ϕ) M){G} ι M[c ← G] Λ(c : ϕ) · M ϕ ⇒ τ Γ τ Γ, (c : ϕ) ·{G} M τ Γ ϕ ⇒ τ Γ with Γ ⊢ G : ϕ

15 / 36

slide-44
SLIDE 44

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM | Λ(c : ϕ) M | M{G} G ::= Λα G | G τ | G1G2 | Λ(c : ϕ) G | G{G ′} | ♦τ | G1

τ

→ G2 | Dist∀α.

τ→σ | c

Coercion variable: c σ Γ1, (c : τ ⊲ σ), Γ2 τ

15 / 36

slide-45
SLIDE 45

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM | Λ(c : ϕ) M | M{G} G ::= Λα G | G τ | G1G2 | Λ(c : ϕ) G | G{G ′} | ♦τ | G1

τ

→ G2 | Dist∀α.

τ→σ | c | Distϕ⇒ τ→σ

It permutes Λ(c : ϕ) and λ(x : τ) Distϕ′⇒

τ ′→σ′Λ(c : ϕ) λ(x : τ) M ι λ(x : τ) Λ(c : ϕ) M

Distϕ⇒

τ→σ

τ → (ϕ ⇒ σ) Γ ϕ ⇒ (τ → σ)

15 / 36

slide-46
SLIDE 46

Properties of F

ι F

ι is well-behaved: it satisfies preservation, progress,

confluence, and normalization.

16 / 36

slide-47
SLIDE 47

Properties of F

ι F

ι is well-behaved: it satisfies preservation, progress,

confluence, and normalization. However, it is not a coercion language: it obeys the forward simulation but not the backward simulation. The backward simulation is necessary for values to correspond before and after erasure: types should not block the computation.

16 / 36

slide-48
SLIDE 48

Losing backward simulation

Λ α λ x : α x ∀α. α → α Γ′ α → α Γ′, α α Γ′, α, (x : α)

17 / 36

slide-49
SLIDE 49

Losing backward simulation

τ Λ α λ x : α x τ → τ Γ′ ∀α. α → α Γ′ α → α Γ′, α α Γ′, α, (x : α)

17 / 36

slide-50
SLIDE 50

Losing backward simulation

@ τ M Λ α λ x : α x τ → τ Γ′ τ Γ′ τ Γ′ ∀α. α → α Γ′ α → α Γ′, α α Γ′, α, (x : α)

17 / 36

slide-51
SLIDE 51

Losing backward simulation

@ τ M Λ α λ x : α x τ → τ Γ′ τ Γ′ τ Γ′ ∀α. α → α Γ′ α → α Γ′, α α Γ′, α, (x : α)

17 / 36

slide-52
SLIDE 52

Losing backward simulation

λ c : ∀α. α → α ⊲ τ → τ @ c M Λ α λ x : α x Γ′ Γ, (c : ∀α. α → α ⊲ τ → τ) τ → τ Γ′ (∀α. α → α ⊲ τ → τ) ⇒ τ Γ τ Γ′ τ Γ′ ∀α. α → α Γ′ α → α Γ′, α α Γ′, α, (x : α)

17 / 36

slide-53
SLIDE 53

A default solution

One solution is to use weak reduction and value restriction on coercion abstraction. However, it delays error detection. We could type any pure lambda term by abstracting over an incoherent set of coercions like U ⊲ (U → U) and (U → U) ⊲ U.

18 / 36

slide-54
SLIDE 54

System Fp

ι MLF and F

<: have some coercion abstraction because of

bounded polymorphism.

19 / 36

slide-55
SLIDE 55

System Fp

ι MLF and F

<: have some coercion abstraction because of

bounded polymorphism. F

<:

MLF Λ(α ≤ τ)M Λ(α ≥ τ)M

19 / 36

slide-56
SLIDE 56

System Fp

ι MLF and F

<: have some coercion abstraction because of

bounded polymorphism. F

<:

MLF Λ(α ≤ τ)M Λ(α ≥ τ)M Λα Λ(c : α ⊲ τ) M Λα Λ(c : τ ⊲ α) M

19 / 36

slide-57
SLIDE 57

System Fp

ι MLF and F

<: have some coercion abstraction because of

bounded polymorphism. F

<:

MLF Λ(α ≤ τ)M Λ(α ≥ τ)M Λα Λ(c : α ⊲ τ) M Λα Λ(c : τ ⊲ α) M Λ(α ⊲ c : τ) M Λ(α ⊳ c : τ) M From F

ι, we replace unrestricted coercion abstraction with

these two features and call the result Fp

ι . We gain backward

simulation and the previous example is ill-formed. Fp

ι is a coercion language (soundness, normalization,

confluence, bisimulation with its erasure).

19 / 36

slide-58
SLIDE 58

Result: Fp

ι subsumes F <:, F η, and MLF Languages F Features ∀=

√ ◮ ∀= is simple polymorphism

20 / 36

slide-59
SLIDE 59

Result: Fp

ι subsumes F <:, F η, and MLF Languages F F

η

Features ∀=

√ √ η

√ ◮ ∀= is simple polymorphism ◮ η

→ is subtyping i.e. the η-expansion for arrow

20 / 36

slide-60
SLIDE 60

Result: Fp

ι subsumes F <:, F η, and MLF Languages F F

η

MLF Features ∀=

√ √ √ η

∀≥

√ ◮ ∀= is simple polymorphism ◮ η

→ is subtyping i.e. the η-expansion for arrow

◮ ∀≥ is lower bounded polymorphism (includes ∀=)

20 / 36

slide-61
SLIDE 61

Result: Fp

ι subsumes F <:, F η, and MLF Languages F F

η

MLF F

<:

Features ∀=

√ √ √ √ η

√ √

∀≥

∀≤

√ ◮ ∀= is simple polymorphism ◮ η

→ is subtyping i.e. the η-expansion for arrow

◮ ∀≥ is lower bounded polymorphism (includes ∀=) ◮ ∀≤ is upper bounded polymorphism (includes ∀=)

20 / 36

slide-62
SLIDE 62

Result: Fp

ι subsumes F <:, F η, and MLF Languages F F

η

MLF F+

<:

Features ∀=

√ √ √ √ η

√ √

∀≥

∀≤

√ ◮ ∀= is simple polymorphism ◮ η

→ is subtyping i.e. the η-expansion for arrow

◮ ∀≥ is lower bounded polymorphism (includes ∀=) ◮ ∀≤ is upper bounded polymorphism (includes ∀=)

F+

<:, the combination of ∀≤ and η

→, also contains deep instantiation and distributivity which are absent from F

<:.

20 / 36

slide-63
SLIDE 63

Result: Fp

ι subsumes F <:, F η, and MLF Languages F F

η

MLF F+

<:

Fp

ι

Features ∀=

√ √ √ √ √ η

√ √ √

∀≥

√ √

∀≤

√ √ ◮ ∀= is simple polymorphism ◮ η

→ is subtyping i.e. the η-expansion for arrow

◮ ∀≥ is lower bounded polymorphism (includes ∀=) ◮ ∀≤ is upper bounded polymorphism (includes ∀=)

F+

<:, the combination of ∀≤ and η

→, also contains deep instantiation and distributivity which are absent from F

<:.

20 / 36

slide-64
SLIDE 64

Future work

◮ See if other type system features can be expressed as

coercions:

◮ recursive types ◮ intersection types ◮ existential types ◮ linear types ◮ type operators ◮ dependent types, etc. 21 / 36

slide-65
SLIDE 65

Future work

◮ See if other type system features can be expressed as

coercions:

◮ recursive types ◮ intersection types ◮ existential types ◮ linear types ◮ type operators ◮ dependent types, etc.

◮ A coercion abstraction less restricted than bounded

polymorphism.

21 / 36

slide-66
SLIDE 66

Future work

◮ See if other type system features can be expressed as

coercions:

◮ recursive types ◮ intersection types ◮ existential types ◮ linear types ◮ type operators ◮ dependent types, etc.

◮ A coercion abstraction less restricted than bounded

polymorphism.

◮ Looking at non erasable coercions.

21 / 36

slide-67
SLIDE 67

Future work

◮ See if other type system features can be expressed as

coercions:

◮ recursive types ◮ intersection types ◮ existential types ◮ linear types ◮ type operators ◮ dependent types, etc.

◮ A coercion abstraction less restricted than bounded

polymorphism.

◮ Looking at non erasable coercions.

Thank you!

21 / 36

slide-68
SLIDE 68

Extra slides

Extra slides

22 / 36

slide-69
SLIDE 69

Push

λ c : ∀α. α → α ⊲ τ → τ @ c M Λ α λ x : α x Γ′ Γ, (c : ∀α. α → α ⊲ τ → τ) τ → τ Γ′ (∀α. α → α ⊲ τ → τ) ⇒ τ Γ τ Γ′ τ Γ′ ∀α. α → α Γ′ α → α Γ′, α α Γ′, α, (x : α)

23 / 36

slide-70
SLIDE 70

Push

RedPushArrow

Gλ(x : τ) M N ι (λ(x : τ ′) (Right G)M[x ← (Left G)x]) N

RedLeftArrow

Left (G1

τ

→ G2) ι G1

RedRightArrow

Right (G1

τ

→ G2) ι G2 Λ(capp : U ⊲ (U → U)) Λ(clam : (U → U) ⊲ U) M

24 / 36

slide-71
SLIDE 71

System F

<: Orthogonal features should easily and fully compose. When combining upper bounded polymorphism and subtyping we naturally get an intermediate language more expressive than the most expressive version of F

<:.

Γ, α <: τ ′ ⊢ σ <: σ′ Γ ⊢ ∀(α <: τ) σ <: ∀(α <: τ ′) σ′ Depending on the variant, the first premise may be:

Kernel-Fsub

τ ′ = τ

25 / 36

slide-72
SLIDE 72

System F

<: Orthogonal features should easily and fully compose. When combining upper bounded polymorphism and subtyping we naturally get an intermediate language more expressive than the most expressive version of F

<:.

Γ, α <: τ ′ ⊢ σ <: σ′ Γ ⊢ ∀(α <: τ) σ <: ∀(α <: τ ′) σ′ Depending on the variant, the first premise may be:

Kernel-Fsub

τ ′ = τ

Full-Fsub

Γ ⊢ τ ′ <: τ

25 / 36

slide-73
SLIDE 73

System F

<: Orthogonal features should easily and fully compose. When combining upper bounded polymorphism and subtyping we naturally get an intermediate language more expressive than the most expressive version of F

<:.

Γ, α <: τ ′ ⊢ σ <: σ′ Γ ⊢ ∀(α <: τ) σ <: ∀(α <: τ ′) σ′ Depending on the variant, the first premise may be:

Kernel-Fsub

τ ′ = τ

Full-Fsub

Γ ⊢ τ ′ <: τ

F-Bounded

Γ, α <: τ ′ ⊢ α <: τ

25 / 36

slide-74
SLIDE 74

System F

<: Orthogonal features should easily and fully compose. When combining upper bounded polymorphism and subtyping we naturally get an intermediate language more expressive than the most expressive version of F

<:.

The typing rule of F

µ<: is derivable in Fp ι using the following

typing rules (absent from F

µ<:):

Γ, (α ⊲ c : τ) ⊢ G : ρ ⊲ σ Γ ⊢ ρ Γ ⊢ λ(α ⊲ c : τ) G : ρ ⊲ ∀(α ⊲ τ) ⇒ σ Γ ⊢ G : ρ ⊲ ∀(α ⊲ τ) ⇒ τ ′ Γ ⊢ G ′ : σ ⊲ τ[α ← σ] Γ ⊢ G{σ ⊲ G ′} : ρ ⊲ τ ′[α ← σ]

25 / 36

slide-75
SLIDE 75

Full distrib

α ⊢ ♦ α : ∀α. τ ⊲ τ α ⊢ (♦ α) → ♦ : τ → σ ⊲ (∀α. τ) → σ α ⊢ ((♦ α) → ♦) ♦ α : ∀α. τ → σ ⊲ (∀α. τ) → σ ⊢ Λα ((♦ α) → ♦)♦ α : ∀α. τ → σ ⊲ ∀α. (∀α. τ) → σ ⊢ Dist Λα ((♦ α) → ♦)♦ α : ∀α. τ → σ ⊲ (∀α. τ) → ∀α. σ

26 / 36

slide-76
SLIDE 76

System F

η examples generalization instantiation η-expansion Λ α M τ Γ, α ∀α. τ Γ [σ] M ∀α. τ Γ τ[α ← σ] Γ λ G2 @ M G1 x x τ → σ σ σ′ τ ′ → σ′ τ ′ τ Λα M M σ λ(x : τ ′) G2[M (G1[x])]

27 / 36

slide-77
SLIDE 77

Pure Lambda Calculus

x, y Variables M ::= x | λx.M | M M Terms C ::= λx.[] | [] M | M [] Reduction contexts

RedContext

M M′ C[M] C[M′]

RedBeta

(λx.M) M′ M[x ← M′]

28 / 36

slide-78
SLIDE 78

Simply-typed lambda calculus

x, y Term variables τ, σ ::= τ → σ Types M, N ::= x | λ(x : τ) M | M N Terms C ::= λ(x : τ) [] | [] M | M [] Reduction contexts

TermVar

x : τ ∈ Γ Γ ⊢ x : τ

TermTermLam

Γ, x : τ ⊢ M : σ Γ ⊢ λ(x : τ) M : τ → σ

TermTermApp

Γ ⊢ M : τ → σ Γ ⊢ N : τ Γ ⊢ M N : σ

RedContextBeta

M β N C[M] β C[N]

RedTerm

(λ(x : τ) M) N β M[x ← N]

29 / 36

slide-79
SLIDE 79

System F: Polymorphism as coercions

The necessary simply-typed lambda calculus is in grey. τ, σ ::= τ → σ | α | ∀α.τ Types M, N ::= x | λ(x : τ) M | M N | P[M] Terms P ::= Λα [] | [] τ One-node coercions

TermTypeLam

Γ, α ⊢ M : τ Γ ⊢ Λα M : ∀α. τ

TermTypeApp

Γ ⊢ M : ∀α. τ Γ ⊢ σ Γ ⊢ M σ : τ[α ← σ]

RedType

(Λα M) τ ι M[α ← τ]

30 / 36

slide-80
SLIDE 80

System F: Polymorphism as coercions

α, β Type variables τ, σ ::= ... | α | ∀α.τ Types M, N ::= ... | P[M] Terms P ::= Λα [] | [] τ Coercion contexts C ::= ... | P Reduction contexts

TermTypeLam

Γ, α ⊢ M : τ Γ ⊢ Λα M : ∀α. τ

TermTypeApp

Γ ⊢ M : ∀α. τ Γ ⊢ M σ : τ[α ← σ]

RedContextIota

M ι N C[M] ι C[N]

RedType

(Λα M) τ ι M[α ← τ]

31 / 36

slide-81
SLIDE 81

System F

η: Subtyping as coercions System F

η is the closure of System F by η-reduction.

Γ ⊢ M : τ M η M′ Γ ⊢ M′ : τ

32 / 36

slide-82
SLIDE 82

System F

η: Subtyping as coercions System F

η is the closure of System F by η-reduction.

Γ ⊢ M : τ M η M′ Γ ⊢ M′ : τ There are two presentations of F

η with coercions: ◮ A lambda-term version: the one we have seen so far,

where judgments are Γ ⊢ G : (∆ · τ) ⊲ σ. The syntax is simple but typing is involved because coercions may bind.

◮ A proof-term version where judgments take the form

Γ ⊢ G : τ ⊲ σ. Typing is simpler but the coercion constructs are less atomic and numerous. We chose a mix presentation to get the best of both.

32 / 36

slide-83
SLIDE 83

System Fp

ι c Coercion variables ⊳⊲ ::= ⊳ | ⊲ Bounds τ, σ ::= ... | ∀(α ⊳⊲ τ) ⇒ σ Types P ::= ... | λ(α ⊳⊲ c : τ) M | M{τ ⊳⊲ G} One-node coercions G ::= ... | Dist∀α⊳⊲ρ⇒

τ→σ

Coercions

TermTCoerLam

Γ, α ⊳⊲ c : τ ⊢ M : σ Γ ⊢ λ(α ⊳⊲ c : τ) M : ∀(α ⊳⊲ τ) ⇒ σ

TermTCoerApp

Γ ⊢ M : ∀(α ⊳⊲ τ) ⇒ τ ′ Γ ⊢ G : σ ⊳⊲ τ[α ← σ] Γ ⊢ M{σ ⊳⊲ G} : τ ′[α ← σ]

RedCoer

(λ(α ⊳⊲ c : τ) M){σ ⊳⊲ G} ι M[α ← σ][c ← G]

33 / 36

slide-84
SLIDE 84

System Fp

ι c Coercion variables ⊳⊲ ::= ⊳ | ⊲ Bounds τ, σ ::= ... | ∀(α ⊳⊲ τ) ⇒ σ Types P ::= ... | λ(α ⊳⊲ c : τ) M | M{τ ⊳⊲ G} One-node coercions G ::= ... | Dist∀α⊳⊲ρ⇒

τ→σ

Coercions

CoerDistTCoerArrow

Γ ⊢ τ Γ, α ⊢ ρ Γ, α ⊢ σ Γ ⊢ Dist∀α⊳⊲ρ⇒

τ→σ

: (∀(α ⊳⊲ ρ) ⇒ τ → σ) ⊲ (τ → ∀(α ⊳⊲ ρ) ⇒ σ)

RedCoerDistCoerArrow

Dist∀α⊳⊲ρ′⇒

τ ′→σ′

λ(α ⊳⊲ c : ρ) λ(x : τ) M ι λ(x : τ) λ(α ⊳⊲ c : ρ) M

33 / 36

slide-85
SLIDE 85

Erasing function

The erasing function removes type annotations, abstractions, and applications. ⌊x⌋ = x ⌊λ(x : τ) M⌋ = λx.⌊M⌋ ⌊M N⌋ = ⌊M⌋ ⌊N⌋ ⌊P[M]⌋ = ⌊M⌋

34 / 36

slide-86
SLIDE 86

Erasing function

The erasing function removes type annotations, abstractions, and applications. ⌊x⌋ = x ⌊λ(x : τ) M⌋ = λx.⌊M⌋ ⌊M N⌋ = ⌊M⌋ ⌊N⌋ ⌊P[M]⌋ = ⌊M⌋ The unfolding of the last line is: ⌊Λα M⌋ = ⌊M⌋ ⌊M σ⌋ = ⌊M⌋

34 / 36

slide-87
SLIDE 87

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ G ::= Λα G | G τ Polymorphism:

TermTypeLam

Γ, α ⊢ M : τ Γ ⊢ Λα M : ∀α. τ

TermTypeApp

Γ ⊢ M : ∀α. τ Γ ⊢ σ Γ ⊢ M σ : τ[α ← σ]

RedType

(Λα M) τ ι M[α ← τ]

35 / 36

slide-88
SLIDE 88

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM G ::= Λα G | G τ | G1G2 Coercion application:

TermCoer

Γ ⊢ G : τ ⊲ σ Γ ⊢ M : τ Γ ⊢ GM : σ

35 / 36

slide-89
SLIDE 89

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM G ::= Λα G | G τ | G1G2 | ♦τ Reflexivity:

CoerDot

Γ ⊢ τ Γ ⊢ ♦τ : τ ⊲ τ

RedCoerDot

♦τM ι M

35 / 36

slide-90
SLIDE 90

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM G ::= Λα G | G τ | G1G2 | ♦τ One-node coercion injection:

P on M

Γ, ∆ ⊢ M : τ Γ ⊢ P[M] : σ

P on G

Γ, ∆ ⊢ G : ρ ⊲ τ Γ ⊢ ρ Γ ⊢ P[G] : ρ ⊲ σ

RedCoerFill

(P[G])M ι P[GM]

35 / 36

slide-91
SLIDE 91

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM G ::= Λα G | G τ | G1G2 | ♦τ | G1

τ

→ G2 Arrow congruence (subtyping):

CoerArrow

Γ ⊢ G1 : τ1 ⊲ τ ′

1

Γ ⊢ G2 : τ2 ⊲ τ ′

2

Γ ⊢ G1

τ1

→ G2 : (τ ′

1 → τ2) ⊲ (τ1 → τ ′ 2) RedCoerArrow

(G1

τ1

→ G2)λ(x : τ ′

1) M ι λ(x : τ1) G2M[x ← G1x]

35 / 36

slide-92
SLIDE 92

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM G ::= Λα G | G τ | G1G2 | ♦τ | G1

τ

→ G2 | Dist∀α.

τ→σ

It permutes Λα and λ(x : τ)

CoerDistTypeArrow

Γ ⊢ τ (i.e. α / ∈ ftv(τ)) Γ, α ⊢ σ Γ ⊢ Dist∀α.

τ→σ : (∀α. τ → σ) ⊲ (τ → ∀α. σ) RedCoerDistTypeArrow

Dist∀α.

τ ′→σ′Λα λ(x : τ) M ι λ(x : τ) Λα M

35 / 36

slide-93
SLIDE 93

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM | Λ(c : ϕ) M | M{G} G ::= Λα G | G τ | G1G2 | Λ(c : ϕ) G | G{G ′} | ♦τ | G1

τ

→ G2 | Dist∀α.

τ→σ

Coercion abstraction:

TermCoerLam

Γ, (c : ϕ) ⊢ M : τ Γ ⊢ Λ(c : ϕ) M : ϕ ⇒ τ

TermCoerApp

Γ ⊢ G : ϕ Γ ⊢ M : ϕ ⇒ τ Γ ⊢ M{G} : τ

RedCoer

(λ(c : ϕ) M){G} ι M[c ← G]

35 / 36

slide-94
SLIDE 94

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM | Λ(c : ϕ) M | M{G} G ::= Λα G | G τ | G1G2 | Λ(c : ϕ) G | G{G ′} | ♦τ | G1

τ

→ G2 | Dist∀α.

τ→σ | c

Coercion variable:

CoerVar

Γ ⊢ ok c : ϕ ∈ Γ Γ ⊢ c : ϕ

35 / 36

slide-95
SLIDE 95

System F

ι τ, σ ::= τ → σ | α | ∀α.τ | ϕ ⇒ τ ϕ ::= τ ⊲ σ M, N ::= x | λ(x : τ) M | M N | Λα M | M τ | GM | Λ(c : ϕ) M | M{G} G ::= Λα G | G τ | G1G2 | Λ(c : ϕ) G | G{G ′} | ♦τ | G1

τ

→ G2 | Dist∀α.

τ→σ | c | Distϕ⇒ τ→σ

It permutes Λ(c : ϕ) and λ(x : τ)

CoerDistCoerArrow

Γ ⊢ τ Γ ⊢ ϕ Γ ⊢ σ Γ ⊢ Distϕ⇒

τ→σ : (ϕ ⇒ (τ → σ)) ⊲ (τ → (ϕ ⇒ σ)) RedCoerDistCoerArrow

Distϕ′⇒

τ ′→σ′Λ(c : ϕ) λ(x : τ) M ι λ(x : τ) Λ(c : ϕ) M

35 / 36

slide-96
SLIDE 96

Why study coercions? Intuition Goal Typing rules Graphical typing rules Simply-typed lambda calculus Type system features Polymorphism Coercions Erasability Bisimulation Coercion judgments Properties of F

ι

Losing backward simulation A default solution System Fp

ι

Result: Fp

ι subsumes F <:, F η, and MLF

Future work Extra slides Push System F

<:

Full distrib System F

η examples

Pure Lambda Calculus Simply-typed lambda calculus System F: Polymorphism as coercions System F: Polymorphism as coercions System F

η: Subtyping as coercions

System Fp

ι

Erasing function System F

ι

36 / 36