Erasable coercions: a unified approach to type systems Julien - - PowerPoint PPT Presentation

erasable coercions a unified approach to type systems
SMART_READER_LITE
LIVE PREVIEW

Erasable coercions: a unified approach to type systems Julien - - PowerPoint PPT Presentation

Erasable coercions: a unified approach to type systems Julien Cretin January 30, 2014 1 / 34 Background: machine language A machine executes programs written in machine language 2 / 34 Background: machine language A machine executes


slide-1
SLIDE 1

Erasable coercions: a unified approach to type systems

Julien Cretin January 30, 2014

1 / 34

slide-2
SLIDE 2

Background: machine language

A machine

executes programs written in machine language

2 / 34

slide-3
SLIDE 3

Background: machine language

A machine

executes programs written in machine language

Problem

programs may crash, and programs in machine language are hard to write.

2 / 34

slide-4
SLIDE 4

Background: high-level language

In practice, programs are

written in high-level languages (C, Java, OCaml) compiled to low-level ones (x86, ARM, bytecode)

3 / 34

slide-5
SLIDE 5

Background: high-level language

In practice, programs are

written in high-level languages (C, Java, OCaml) compiled to low-level ones (x86, ARM, bytecode)

Thus, programs are

easier to write more portable

3 / 34

slide-6
SLIDE 6

Background: high-level language

In practice, programs are

written in high-level languages (C, Java, OCaml) compiled to low-level ones (x86, ARM, bytecode)

Thus, programs are

easier to write more portable

Problem

For most high-level languages, programs may still crash.

3 / 34

slide-7
SLIDE 7

Background: type system

Therefore, languages come with a type system.

A type system

defines program type annotations ensures that well-typed programs do not crash

4 / 34

slide-8
SLIDE 8

Background: type system

Therefore, languages come with a type system.

A type system

defines program type annotations ensures that well-typed programs do not crash

The compiler of a typed language

takes as input a program with type annotations, checks that the program is well-typed, erases type annotations, and compiles the program. Compiled well-typed programs do not crash (if the compiler is correct).

4 / 34

slide-9
SLIDE 9

In my PhD

I consider one particular language:

Lambda Calculus

a minimal model of computation at the root of functional programming languages (OCaml, Haskell, ...) Still, our approach is not restricted to the Lambda Calculus.

5 / 34

slide-10
SLIDE 10

Type systems are a maze

There are many type systems just for the Lambda Calculus: ML System F System F-sub System F-omega System F-eta MLF ... (the list grows as we speak)

6 / 34

slide-11
SLIDE 11

Type systems are a maze

There are many type systems just for the Lambda Calculus: ML System F System F-sub System F-omega System F-eta MLF ... (the list grows as we speak) Why does one language need so many type systems?

6 / 34

slide-12
SLIDE 12

Type systems are compromises

Comparison criteria

expressiveness: what is the set of well-typed terms? inference: how many annotations may be omitted? simplicity: are annotations simple to write and understand?

7 / 34

slide-13
SLIDE 13

Type systems are compromises

Comparison criteria

expressiveness: what is the set of well-typed terms? inference: how many annotations may be omitted? simplicity: are annotations simple to write and understand? Inference and simplicity mainly matter for surface type systems do not matter for kernel type systems

7 / 34

slide-14
SLIDE 14

Type systems are compromises

Comparison criteria

expressiveness: what is the set of well-typed terms? inference: how many annotations may be omitted? simplicity: are annotations simple to write and understand? Inference and simplicity mainly matter for surface type systems do not matter for kernel type systems We study kernel type systems. A kernel type system may have different surface type systems. Translating from surface type systems to kernel type systems is easy. Kernel type systems are better suited for meta-theory.

7 / 34

slide-15
SLIDE 15

Kernel type systems

Simply Typed Lambda Calculus Intersection types Linear types ML System F Constraint ML System F-eta System F-sub MLF System F-omega Dependent types System FC (core GHC) System Fp

ι

System F

cc

Type system features

polymorphism, η-expansion, subtyping, and coercions.

8 / 34

slide-16
SLIDE 16

Outline

Prelude Lambda Calculus

◮ definition ◮ example 9 / 34

slide-17
SLIDE 17

Outline

Prelude Lambda Calculus

◮ definition ◮ example

Part I Syntactical approach (System Fp

ι )

◮ identify a set of atomic features ◮ present them as composable coercions ◮ define a unified framework of coercions 9 / 34

slide-18
SLIDE 18

Outline

Prelude Lambda Calculus

◮ definition ◮ example

Part I Syntactical approach (System Fp

ι )

◮ identify a set of atomic features ◮ present them as composable coercions ◮ define a unified framework of coercions

Part II Semantical approach (System F

cc)

◮ general coercion abstraction ◮ first-class coercions 9 / 34

slide-19
SLIDE 19

Lambda Calculus: terms

We only study pairs and functions, but the usual constructs may be added.

Terms

(active edge) Product (gray) Arrow (black) Constructors (output) pairs

, a1 a2

abstractions

λx a

Destructors (input) projections

π1 a π2 a

applications

@ a1 a2

variables

x

10 / 34

slide-20
SLIDE 20

Lambda Calculus: terms

We only study pairs and functions, but the usual constructs may be added.

Terms

(active edge) Product (gray) Arrow (black) Constructors (output) pairs

, a1 a2

abstractions

λx a

Destructors (input) projections

π1 a π2 a

applications

@ a1 a2

variables

x

10 / 34

slide-21
SLIDE 21

Lambda Calculus: terms

We only study pairs and functions, but the usual constructs may be added.

Terms

(active edge) Product (gray) Arrow (black) Constructors (output) pairs

, a1 a2

abstractions

λx a

Destructors (input) projections

π1 a π2 a

applications

@ a1 a2

variables

x

10 / 34

slide-22
SLIDE 22

Lambda Calculus: terms

We only study pairs and functions, but the usual constructs may be added.

Terms

(active edge) Product (gray) Arrow (black) Constructors (output) pairs

, a1 a2

abstractions

λx a

Destructors (input) projections

π1 a π2 a

applications

@ a1 a2

variables

x

10 / 34

slide-23
SLIDE 23

Lambda Calculus: terms

We only study pairs and functions, but the usual constructs may be added.

Terms

(active edge) Product (gray) Arrow (black) Constructors (output) pairs

, a1 a2

abstractions

λx a

Destructors (input) projections

π1 a π2 a

applications

@ a1 a2

variables

x

10 / 34

slide-24
SLIDE 24

Lambda Calculus: terms

We only study pairs and functions, but the usual constructs may be added.

Terms

(active edge) Product (gray) Arrow (black) Constructors (output) pairs

, a1 a2

abstractions

λx a

Destructors (input) projections

π1 a π2 a

applications

@ a1 a2

variables

x

10 / 34

slide-25
SLIDE 25

Lambda Calculus: reduction

Reduction (when two active edges of the same sort meet)

πi , a1 a2

ai

11 / 34

slide-26
SLIDE 26

Lambda Calculus: reduction

Reduction (when two active edges of the same sort meet)

πi , a1 a2

ai

@ λx a b x x

...

a b b

...

11 / 34

slide-27
SLIDE 27

Lambda Calculus: reduction

Reduction (when two active edges of the same sort meet)

πi , a1 a2

ai

@ λx a b x x

...

a b b

...

Errors (when two active edges of different sorts meet)

πi λx a @ , a1 a2 b

11 / 34

slide-28
SLIDE 28

Lambda Calculus: example

πi @ λy @ π1 y π2 y , x1 x2

12 / 34

slide-29
SLIDE 29

Lambda Calculus: example

πi @ λy @ π1 y π2 y , x1 x2

12 / 34

slide-30
SLIDE 30

Lambda Calculus: example

πi @ λy @ π1 y π2 y , x1 x2 πi @ π1 y π2 y

12 / 34

slide-31
SLIDE 31

Lambda Calculus: example

πi @ λy @ π1 y π2 y , x1 x2 πi @ π1 , x1 x2 π2 , x1 x2

12 / 34

slide-32
SLIDE 32

Lambda Calculus: example

πi @ π1 , x1 x2 π2 , x1 x2

12 / 34

slide-33
SLIDE 33

Lambda Calculus: example

πi @ x1 x2 πi @ π1 , x1 x2 π2 , x1 x2

12 / 34

slide-34
SLIDE 34

Strong reduction

Although all programming languages execution is in weak reduction (reduction does not proceed under abstractions), we should study meta-theoretical properties for strong reduction.

13 / 34

slide-35
SLIDE 35

Strong reduction

Although all programming languages execution is in weak reduction (reduction does not proceed under abstractions), we should study meta-theoretical properties for strong reduction.

We may reduce

anywhere (in particular under abstractions) in any order

13 / 34

slide-36
SLIDE 36

Strong reduction

Although all programming languages execution is in weak reduction (reduction does not proceed under abstractions), we should study meta-theoretical properties for strong reduction.

We may reduce

anywhere (in particular under abstractions) in any order Studying strong reduction is much harder, but it gives a better understanding of the language and type system looks at potentially hidden errors as usual type systems do (λx x, x x is an error in strong reduction, but not in weak reduction) justifies a certain class of optimizations describes all reduction strategies (in particular weak ones)

13 / 34

slide-37
SLIDE 37

Part I: Syntactical approach

We now can define our unified framework of coercions identifying a set of atomic features presenting them as composable coercions This approach is syntactical.

14 / 34

slide-38
SLIDE 38

Framework idea: we distinguish two sorts of typing rules

Computational rules (Γ, Σi ⊢ ai : τi)i∈1..n J1 . . . Jm Γ ⊢ node(a1, . . . an) : ρ Erasable rules Γ, Σ ⊢ a : τ J1 . . . Jm Γ ⊢ a : ρ

15 / 34

slide-39
SLIDE 39

Framework idea: we distinguish two sorts of typing rules

Computational rules (Γ, Σi ⊢ ai : τi)i∈1..n J1 . . . Jm Γ ⊢ node(a1, . . . an) : ρ Erasable rules Γ, Σ ⊢ a : τ J1 . . . Jm Γ ⊢ a : ρ We factor all erasable rules into a unique erasable rule: Unique erasable rule

TermCoer

Γ, Σ ⊢ a : τ Γ ⊢ (Σ ⊢ τ) ⊲ ρ Γ ⊢ a : ρ Coercion rules J1 . . . Jm Γ ⊢ (Σ ⊢ τ) ⊲ ρ

15 / 34

slide-40
SLIDE 40

Framework idea: we distinguish two sorts of typing rules

Computational rules (Γ, Σi ⊢ ai : τi)i∈1..n J1 . . . Jm Γ ⊢ node(a1, . . . an) : ρ Erasable rules Γ, Σ ⊢ a : τ J1 . . . Jm Γ ⊢ a : ρ We factor all erasable rules into a unique erasable rule: Unique erasable rule

TermCoer

Γ, Σ ⊢ a : τ Γ ⊢ (Σ ⊢ τ) ⊲ ρ Γ ⊢ a : ρ Coercion rules J1 . . . Jm Γ ⊢ (Σ ⊢ τ) ⊲ ρ The unique erasable rule looks like the usual term subtyping rule. Besides changing the type, coercions may also extend the environment.

15 / 34

slide-41
SLIDE 41

Framework gain: composable features

It looks like we only played with the syntax of typing rules.

16 / 34

slide-42
SLIDE 42

Framework gain: composable features

It looks like we only played with the syntax of typing rules. However, this change makes a clear distinction between terms and type annotations: terms are totally absent from coercion rules. J1 . . . Jm Γ ⊢ (Σ ⊢ τ) ⊲ ρ

16 / 34

slide-43
SLIDE 43

Framework gain: composable features

It looks like we only played with the syntax of typing rules. However, this change makes a clear distinction between terms and type annotations: terms are totally absent from coercion rules. J1 . . . Jm Γ ⊢ (Σ ⊢ τ) ⊲ ρ More essentially, rule TermCoer enforces a unique interface for all erasable features: Γ ⊢ (Σ ⊢ τ) ⊲ ρ this change is a preliminary to have composable features. this enables to decompose existing features into atomic parts.

16 / 34

slide-44
SLIDE 44

Framework: all term typing rules

STLC (canonical, minimal set of typing rules)

Γ τ1 × τ2

, a1

Γ τ1

a2

Γ τ2 Γ τi

πi a

Γ τ1 × τ2 Γ τ → ρ

λx a

Γ, (x : τ) ρ Γ ρ

@ a

Γ τ → ρ

b

Γ τ Γ Γ(x)

x

17 / 34

slide-45
SLIDE 45

Framework: all term typing rules

STLC (canonical, minimal set of typing rules)

Γ τ1 × τ2

, a1

Γ τ1

a2

Γ τ2 Γ τi

πi a

Γ τ1 × τ2 Γ τ → ρ

λx a

Γ, (x : τ) ρ Γ ρ

@ a

Γ τ → ρ

b

Γ τ Γ Γ(x)

x

Term coercion rule

Γ ρ

G

a

Γ, Σ τ +Σ τ Γ ρ

G

a

Γ, Σ τ +Σ τ

    

TermCoer

Γ, Σ ⊢ a : τ

G ⇒ Γ ⊢ (Σ ⊢ τ) ⊲ ρ

Γ ⊢ a : ρ

    

17 / 34

slide-46
SLIDE 46

Feature: polymorphism

We write τ ⊲ ρ for (∅ ⊢ τ) ⊲ ρ. This is actually how subtyping is encoded.

18 / 34

slide-47
SLIDE 47

Feature: polymorphism

We write τ ⊲ ρ for (∅ ⊢ τ) ⊲ ρ. This is actually how subtyping is encoded.

Coercion rules

OldInst

Γ ⊢ a : ∀α τ Γ ⊢ a : τ[α/ρ] Γ τ[α/ρ]

[ρ]

+∅ ∀α τ

Inst

[ρ] ⇒ Γ ⊢ ∀α τ ⊲ τ[α/ρ] 18 / 34

slide-48
SLIDE 48

Feature: polymorphism

We write τ ⊲ ρ for (∅ ⊢ τ) ⊲ ρ. This is actually how subtyping is encoded.

Coercion rules

OldGen

Γ, α ⊢ a : τ Γ ⊢ a : ∀α τ Γ ∀α τ

Λα

+ α τ

Gen

Λα ⇒ Γ ⊢ ( α ⊢ τ ) ⊲ ∀α τ

OldInst

Γ ⊢ a : ∀α τ Γ ⊢ a : τ[α/ρ] Γ τ[α/ρ]

[ρ]

+∅ ∀α τ

Inst

[ρ] ⇒ Γ ⊢ ∀α τ ⊲ τ[α/ρ] 18 / 34

slide-49
SLIDE 49

Feature: polymorphism

We write τ ⊲ ρ for (∅ ⊢ τ) ⊲ ρ. This is actually how subtyping is encoded.

Coercion rules

OldGen

Γ, α ⊢ a : τ Γ ⊢ a : ∀α τ Γ ∀α τ

Λα

+ α τ

Gen

Λα ⇒ Γ ⊢ ( α ⊢ τ ) ⊲ ∀α τ

OldInst

Γ ⊢ a : ∀α τ Γ ⊢ a : τ[α/ρ] Γ τ[α/ρ]

[ρ]

+∅ ∀α τ

Inst

[ρ] ⇒ Γ ⊢ ∀α τ ⊲ τ[α/ρ]

STLC + polymorphism = System F

18 / 34

slide-50
SLIDE 50

Extending the framework

We internalize the reflexivity and transitivity of coercions.

Coercion rules

Γ τ

+∅ τ

Refl

♦ ⇒ Γ ⊢ τ ⊲ τ

These rules are now part of the framework.

19 / 34

slide-51
SLIDE 51

Extending the framework

We internalize the reflexivity and transitivity of coercions.

Coercion rules

Γ τ

+∅ τ

Refl

♦ ⇒ Γ ⊢ τ ⊲ τ

if

Γ τ3

G2 G1

+Σ1 τ1 Γ, Σ2 τ2 +Σ2 τ2

premises then

Γ τ3

G2 ◦ G1

+Σ2, Σ1 τ1

conclusion

Trans

G2 ⇒ Γ ⊢ (Σ2 ⊢ τ2) ⊲ τ3 G1 ⇒ Γ, Σ2 ⊢ (Σ1 ⊢ τ1) ⊲ τ2 G2 ◦ G1 ⇒ Γ ⊢ (Σ2, Σ1 ⊢ τ1) ⊲ τ3

These rules are now part of the framework.

19 / 34

slide-52
SLIDE 52

Extending the framework

We internalize the reflexivity and transitivity of coercions.

Coercion rules

Γ τ

+∅ τ

Refl

♦ ⇒ Γ ⊢ τ ⊲ τ

if

Γ τ3

G2 G1

+Σ1 τ1 Γ, Σ2 τ2 +Σ2 τ2

premises then

Γ τ3

G2 ◦ G1

+Σ2, Σ1 τ1

conclusion

Trans

G2 ⇒ Γ ⊢ (Σ2 ⊢ τ2) ⊲ τ3 G1 ⇒ Γ, Σ2 ⊢ (Σ1 ⊢ τ1) ⊲ τ2 G2 ◦ G1 ⇒ Γ ⊢ (Σ2, Σ1 ⊢ τ1) ⊲ τ3

These rules are now part of the framework.

19 / 34

slide-53
SLIDE 53

Extending the framework

We internalize the reflexivity and transitivity of coercions.

Coercion rules

Γ τ

+∅ τ

Refl

♦ ⇒ Γ ⊢ τ ⊲ τ

if

Γ τ3

G2 G1

+Σ1 τ1 Γ, Σ2 τ2 +Σ2 τ2

premises then

Γ τ3

G2 ◦ G1

+Σ2, Σ1 τ1

conclusion

Trans

G2 ⇒ Γ ⊢ (Σ2 ⊢ τ2) ⊲ τ3 G1 ⇒ Γ, Σ2 ⊢ (Σ1 ⊢ τ1) ⊲ τ2 G2 ◦ G1 ⇒ Γ ⊢ (Σ2, Σ1 ⊢ τ1) ⊲ τ3

These rules are now part of the framework.

19 / 34

slide-54
SLIDE 54

Feature: eta-expansion (rules)

Eta-expansion is at the root of subtyping.

Coercion rules

EtaProd

Γ ⊢ (Σ ⊢ τ1) ⊲ ρ1 Γ ⊢ (Σ ⊢ τ2) ⊲ ρ2 Γ ⊢ (Σ ⊢ τ1 × τ2) ⊲ ρ1 × ρ2

EtaArr

Γ, Σ ⊢ ρ1 ⊲ τ1 Γ ⊢ (Σ ⊢ τ2) ⊲ ρ2 Γ ⊢ (Σ ⊢ τ1 → τ2) ⊲ ρ1 → ρ2 These coercion rules extend the subtyping congruence rules of computational types to our notion of coercions.

Note: You need one such rule for each of your computational types.

20 / 34

slide-55
SLIDE 55

Feature: eta-expansion (rules)

Eta-expansion is at the root of subtyping.

Coercion rules

EtaProd

G1 ⇒ Γ ⊢ (Σ ⊢ τ1) ⊲ ρ1 G2 ⇒ Γ ⊢ (Σ ⊢ τ2) ⊲ ρ2 G1 × G2 ⇒ Γ ⊢ (Σ ⊢ τ1 × τ2) ⊲ ρ1 × ρ2

EtaArr

G1 ⇒ Γ, Σ ⊢ ρ1 ⊲ τ1 G2 ⇒ Γ ⊢ (Σ ⊢ τ2) ⊲ ρ2 G1 → G2 ⇒ Γ ⊢ (Σ ⊢ τ1 → τ2) ⊲ ρ1 → ρ2

These coercion rules extend the subtyping congruence rules of computational types to our notion of coercions.

Note: You need one such rule for each of your computational types.

20 / 34

slide-56
SLIDE 56

Feature: eta-expansion (consequences)

STLC + polymorphism + eta-expansion ≡ System F

η (Mitchell 1988)

21 / 34

slide-57
SLIDE 57

Feature: eta-expansion (consequences)

STLC + polymorphism + eta-expansion ≡ System F

η (Mitchell 1988)

Our presentation is improved:

(thanks to the use of coercions instead of just subtyping)

polymorphic type congruence rule is derivable:

+ α

∀α τ τ

[α]

ρ

G

∀α ρ

Λα

Λα ◦ G ◦ [α] distributivity rules are derivable: ([α] → Λα) ◦ [α] (Λα × Λα) ◦ [α]

21 / 34

slide-58
SLIDE 58

Feature: lower bounded polymorphism

Lower bounded polymorphism is a form of coercion abstraction.

Coercion rules

LowerInst

Γ ⊢ (σ ⊲ α)[α/ρ] Γ ⊢ ∀(α ⊳ σ) τ ⊲ τ[α/ρ]

LowerGen

Γ ⊢ (α ⊳ σ ⊢ τ) ⊲ ∀(α ⊳ σ) τ

22 / 34

slide-59
SLIDE 59

Feature: lower bounded polymorphism

Lower bounded polymorphism is a form of coercion abstraction.

Coercion rules

LowerInst

Γ ⊢ (σ ⊲ α)[α/ρ] Γ ⊢ ∀(α ⊳ σ) τ ⊲ τ[α/ρ]

LowerGen

Γ ⊢ (α ⊳ σ ⊢ τ) ⊲ ∀(α ⊳ σ) τ

22 / 34

slide-60
SLIDE 60

Feature: lower bounded polymorphism

Lower bounded polymorphism is a form of coercion abstraction.

Coercion rules

LowerInst

Γ ⊢ (σ ⊲ α)[α/ρ] Γ ⊢ ∀(α ⊳ σ) τ ⊲ τ[α/ρ]

LowerGen

Γ ⊢ (α ⊳ σ ⊢ τ) ⊲ ∀(α ⊳ σ) τ STLC + lower bounded polymorphism ≡ MLF (Le Botlan, Rémy, Yakobowski, 2003-2010) This permits to prove the strong normalization of MLF.

22 / 34

slide-61
SLIDE 61

Feature: upper bounded polymorphism

Upper bounded polymorphism is also a form of coercion abstraction.

Coercion rules

UpperInst

Γ ⊢ (α ⊲ σ)[α/ρ] Γ ⊢ ∀(α ⊲ σ) τ ⊲ τ[α/ρ]

UpperGen

Γ ⊢ (α ⊲ σ ⊢ τ) ⊲ ∀(α ⊲ σ) τ

23 / 34

slide-62
SLIDE 62

Feature: upper bounded polymorphism

Upper bounded polymorphism is also a form of coercion abstraction.

Coercion rules

UpperInst

Γ ⊢ (α ⊲ σ)[α/ρ] Γ ⊢ ∀(α ⊲ σ) τ ⊲ τ[α/ρ]

UpperGen

Γ ⊢ (α ⊲ σ ⊢ τ) ⊲ ∀(α ⊲ σ) τ

23 / 34

slide-63
SLIDE 63

Feature: upper bounded polymorphism

Upper bounded polymorphism is also a form of coercion abstraction.

Coercion rules

UpperInst

Γ ⊢ (α ⊲ σ)[α/ρ] Γ ⊢ ∀(α ⊲ σ) τ ⊲ τ[α/ρ]

UpperGen

Γ ⊢ (α ⊲ σ ⊢ τ) ⊲ ∀(α ⊲ σ) τ STLC + eta-expansion + upper bounded polymorphism System F

<: (Canning, Cook, Hill, Olthoff, Mitchell, 1989)

Our version is more expressive than all variants of F

<: because coercions

are composable, i.e. distributivity and congruence rules are derivable.

23 / 34

slide-64
SLIDE 64

Properties: bisimulation

We can define a reduction relation M N for explicit terms. We label reduction steps with ι (iota) for erasable steps β (beta) for computational steps

24 / 34

slide-65
SLIDE 65

Properties: bisimulation

We can define a reduction relation M N for explicit terms. We label reduction steps with ι (iota) for erasable steps β (beta) for computational steps

Bisimulation (how explicit and implicit reduction relations relate)

M N a b β explicit: implict: M N a = b ι M N a b ι ⋆ β Forward simulation Backward simulation We write a (resp. b) for the type erasure of M (resp. N).

24 / 34

slide-66
SLIDE 66

Properties: soundness and strong normalization

We prove soundness with the usual scheme: preservation + progress. preservation: reduction preserves well-typedness progress: errors are not well-typed

25 / 34

slide-67
SLIDE 67

Properties: soundness and strong normalization

We prove soundness with the usual scheme: preservation + progress. preservation: reduction preserves well-typedness progress: errors are not well-typed Since explicit terms represent typing derivations, the explicit version of these two lemmas is much simpler to prove than the implicit version.

25 / 34

slide-68
SLIDE 68

Properties: soundness and strong normalization

We prove soundness with the usual scheme: preservation + progress. preservation: reduction preserves well-typedness progress: errors are not well-typed Since explicit terms represent typing derivations, the explicit version of these two lemmas is much simpler to prove than the implicit version. We factor the difficulties in the bisimulation lemma, and prove the implicit versions from the explicit ones.

25 / 34

slide-69
SLIDE 69

Properties: soundness and strong normalization

We prove soundness with the usual scheme: preservation + progress. preservation: reduction preserves well-typedness progress: errors are not well-typed Since explicit terms represent typing derivations, the explicit version of these two lemmas is much simpler to prove than the implicit version. We factor the difficulties in the bisimulation lemma, and prove the implicit versions from the explicit ones. Strong normalization is proved by reification into System F.

25 / 34

slide-70
SLIDE 70

System Fp

ι (POPL 2012)

Erasable feature F F

η

MLF F

<:

Fp

ι

Polymorphism

√ √

Eta-expansion

Bottom

Top

Lower bounded polymorphism

Upper bounded polymorphism

We have a general framework with a clear distinction between programs and type annotations, composable modular coercion rules (for all erasable features), the preservation, progress, and bisimulation properties, strongly normalizing, and generalizing MLF, F

<:, and F η (thus ML and F)

26 / 34

slide-71
SLIDE 71

Part II

In this general approach to coercions, System Fp

ι seems a local optimal

(with respect to coercion abstraction) that is syntactical.

27 / 34

slide-72
SLIDE 72

Part II

In this general approach to coercions, System Fp

ι seems a local optimal

(with respect to coercion abstraction) that is syntactical. System Fp

ι can only model bounded polymorphism with a single bound,

which does not even cover subtyping constraints in ML.

27 / 34

slide-73
SLIDE 73

Part II

In this general approach to coercions, System Fp

ι seems a local optimal

(with respect to coercion abstraction) that is syntactical. System Fp

ι can only model bounded polymorphism with a single bound,

which does not even cover subtyping constraints in ML. We now lift this restriction following a semantical approach.

27 / 34

slide-74
SLIDE 74

Coherent polymorphism

We get general coercion abstraction by simultaneously abstracting over types and coercions.

28 / 34

slide-75
SLIDE 75

Coherent polymorphism

We get general coercion abstraction by simultaneously abstracting over types and coercions. We factor this operation with coherent polymorphic types ∀(α : κ) τ using kinds and propositions. κ ::= 1 | κ × κ | ⋆ | {α : κ | P} Kinds P ::= ⊤ | P ∧ P | (Σ ⊢ τ) ⊲ τ Propositions

28 / 34

slide-76
SLIDE 76

Coherent polymorphism

We get general coercion abstraction by simultaneously abstracting over types and coercions. We factor this operation with coherent polymorphic types ∀(α : κ) τ using kinds and propositions. κ ::= 1 | κ × κ | ⋆ | {α : κ | P} Kinds P ::= ⊤ | P ∧ P | (Σ ⊢ τ) ⊲ τ Propositions

Examples

System F: ∀α ρ becomes ∀(α : ⋆) ρ MLF: ∀(α ⊳ τ) ρ becomes ∀(α : {β : ⋆ | τ ⊲ β}) ρ Constraint ML: ∀(α : {β : ⋆ | τ1 ⊲ β ∧ β ⊲ τ2}) ρ is new ∀(α : {β : ⋆ × ⋆ | π1 β ⊲ (π1 β) → (π2 β)}) ρ is new

28 / 34

slide-77
SLIDE 77

Why coherence?

We write τ ≡ ρ for erasable isomorphisms. It desugars to τ ⊲ ρ ∧ ρ ⊲ τ.

29 / 34

slide-78
SLIDE 78

Why coherence?

We write τ ≡ ρ for erasable isomorphisms. It desugars to τ ⊲ ρ ∧ ρ ⊲ τ. Abstracting over {α : ⋆ | (α ≡ α → α) ∧ (α ≡ α × α)} looses soundness (which we must forbid)

29 / 34

slide-79
SLIDE 79

Why coherence?

We write τ ≡ ρ for erasable isomorphisms. It desugars to τ ⊲ ρ ∧ ρ ⊲ τ. Abstracting over {α : ⋆ | (α ≡ α → α) ∧ (α ≡ α × α)} looses soundness (which we must forbid) {α : ⋆ | α ≡ α → α} looses normalization (which we may want to forbid)

29 / 34

slide-80
SLIDE 80

Why coherence?

We write τ ≡ ρ for erasable isomorphisms. It desugars to τ ⊲ ρ ∧ ρ ⊲ τ. Abstracting over {α : ⋆ | (α ≡ α → α) ∧ (α ≡ α × α)} looses soundness (which we must forbid) {α : ⋆ | α ≡ α → α} looses normalization (which we may want to forbid)

Coherence

A type abstraction over κ is coherent if there is a witness ρ of kind κ.

(if κ is {α : ⋆ | α ⊲ τ}, this implies the existence of a coercion of type ρ ⊲ τ)

Notice that coherence holds by construction in System Fp

ι

(witnesses are either top or bottom).

Coherence is a premise of the type abstraction rule in System F

cc

(it is undecidable in the general case).

29 / 34

slide-81
SLIDE 81

Incoherent polymorphism

Some type system features (such as GADTs) rely on blocking constructs. To study them, we extend our calculus with simple blocking and unblocking constructs.

Blocks (typing rules and reduction rule)

∂· a

Γ, (α : κ) τ Γ Π(α : κ) τ

κ coherent

·♦ a

Γ Π(α : κ) τ Γ τ[α/ρ]

·♦ ∂· a

a This construct is similar to the zero-cost abstraction of FC (Peyton Jones, Vytiniotis, 2011).

Note: This construct breaks confluence, but confluence can easily be restored.

30 / 34

slide-82
SLIDE 82

System F

cc

We have a general framework with a clear distinction between programs and annotations, composable first-class coercion propositions, a quite rich logic (quantifiers and coinduction), soundness and strong normalization, and generalizing Fp

ι and Constraint ML (thus MLF, F <:, F η, ML, and F)

31 / 34

slide-83
SLIDE 83

Features not presented in this talk

Computational features (sum, unit, and void types) are easy.

Erasable features

Type language: top and bottom types (easy) equi-recursive types (requires step-indexed semantics) (partially done) function types as in Fω Proposition language: polymorphic propositions (easy) existential propositions (derives from coherence and constrained kind) coinduction (requires step-indexed semantics)

32 / 34

slide-84
SLIDE 84

Formalization

The soundness and normalization results are shown using a semantic

  • approach. Both proofs are similar, largely shared, and formalized in Coq.

Techniques

normalization uses reducibility candidates (Tait’s method) soundness generalizes step-indexed techniques for recursive types (Appel, McAllester, 2001) to strong reduction

33 / 34

slide-85
SLIDE 85

Future work

There is little work to have a complete kernel type system: Redesign function types Verify that side effects can be added (with value restriction)

34 / 34

slide-86
SLIDE 86

Future work

There is little work to have a complete kernel type system: Redesign function types Verify that side effects can be added (with value restriction) Some interesting research remain to be done: Primitive existential types (call-by-constructor reduction) Higher-order recursive types Which restrictions of System F

cc would allow a syntactical approach?

Intersection types need multi-premises erasable rules (partially explored, design parallel to polymorphism) Dependent type version of the framework?

34 / 34

slide-87
SLIDE 87

Extra slides

Extra slides

34 / 34

slide-88
SLIDE 88

Intersection types

Intro

Γ ⊢ a : τ1 Γ ⊢ a : τ2 Γ ⊢ a : τ1 ∩ τ2

Elim

Γ ⊢ a : τ1 ∩ τ2 Γ ⊢ a : τi Coercions Γ ⊢ (Σ ⊢ τ) ⊲ ρ now become Γ ⊢ {Σi ⊢ τi}i ⊲ ρ which can be understood as Γ ⊢

i (Σi ⊢ τi) ⊲ ρ. Intro

Γ ⊢ {τ1, τ2} ⊲ τ1 ∩ τ2

Elim

Γ ⊢ τ1 ∩ τ2 ⊲ τi

Fusion

Γ ⊢ {τ, . . . τ} ⊲ ρ Γ ⊢ τ ⊲ ρ

Intro

Γ ⊢ (α ⊢ τ) ⊲ ∀α τ

Elim

Γ ⊢ ∀α τ ⊲ τ[α/ρ]

Weak

Γ ⊢ (Σ ⊢ τ) ⊲ ρ Γ ⊢ τ ⊲ ρ

34 / 34

slide-89
SLIDE 89

Intersection types: terms

The term typing rules are done point-wise and written a : {Γi ⊢ τi}i

TermVar

x : {Γi ⊢ Γi(x)}i

TermLam

a : {Γi, (x : τi) ⊢ ρi}i λx a : {Γi ⊢ τi → ρi}i

TermApp

a : {Γi ⊢ τi → ρi}i b : {Γi ⊢ τi}i a b : {Γi ⊢ ρi}i

TermCoer

a : { {Γi, Σj ⊢ τj}j }i {Γi ⊢ {Σj ⊢ τj}j ⊲ ρi}i a : {Γi ⊢ ρi}i

34 / 34

slide-90
SLIDE 90

Intersection types: coercions

Trans

  • Γ, Σ2i ⊢ {Σ1j ⊢ τ1j}j ⊲ τ2i

i

Γ ⊢ {Σ2i ⊢ τ2i}i ⊲ τ3 Γ ⊢

  • {Σ2i, Σ1j ⊢ τ1j}ji ⊲ τ3

Weak

Γ ⊢ {Σ ⊢ τ, . . .} ⊲ ρ Γ ⊢ τ ⊲ ρ

EtaArr

Γ, Σi ⊢ τ ′ ⊲ τi i

Γ ⊢ {Σi ⊢ ρi}i ⊲ ρ′ Γ ⊢ {Σi ⊢ τi → ρi}i ⊲ τ ′ → ρ′ We derive the usual subtyping rules: Γ ⊢ ρ ⊲ τ1 Γ ⊢ ρ ⊲ τ2 Γ ⊢ ρ ⊲ τ1 ∩ τ2 Γ ⊢ (τ → ρ1) ∩ (τ → ρ2) ⊲ τ → ρ1 ∩ ρ2

34 / 34

slide-91
SLIDE 91

Push

Γ′

1 = Γ1, (x : τ1)

Γ′

2 = Γ2, (x : τ2)

M ⇒ a ⊢ Γ′

1 : ρ1

N ⇒ b ⊢ Γ2 : τ2 G ⇒ Γ2 ⊢ (Σ1 ⊢ τ1 → ρ1) ⊲ τ2 → ρ2

@ N G λ G λ M Γ2 Γ1 Γ′

1

ρ2 τ2 τ2 → ρ2 τ1 → ρ1 ρ1 τ1 @ LG N λ M λ @ RG LG Γ2 Γ1 Γ′

1

ρ2 ρ1 τ1 τ2 τ1 → ρ1 ρ1 τ1 @ N λ RG M LG λ M RG LG Γ2 Γ′

2 Γ′ 1

ρ2 τ2 τ2 → ρ2 ρ2 ρ1 τ1 τ2 RG M LG N M RG LG Γ2 Γ1 ρ2 ρ1 τ1 τ2

34 / 34

slide-92
SLIDE 92

GADTs

Existentials

coherent ∃(α : κ) τ

def

= ∀β (∀(α : κ) (τ → β)) → β pack a

def

= λx x a unpack a as x in b

def

= a (λx b) incoherent Σ(α : κ) τ

def

= ∀β (Π(α : κ) (τ → β)) → β ipack a

def

= λx x ♦ a iunpack a as x in b

def

= a (∂ λx b) Term α

def

= Σ(β1, β2 | α ≡ (β1 → β2)) α + ∃β Term (β → α) × Term β Lam x

def

= inl (ipack x) : ∀α ∀β (α → β) → Term (α → β) App y x

def

= inr (pack y, x) : ∀α ∀β Term (α → β) → Term α → Term β eval x = case x of {inl x1 → iunpack x1 as y in y | inr x2 → unpack x2 as y in (eval (π1 y)) (eval (π2 y))}

34 / 34

slide-93
SLIDE 93

Why block abstraction?

We studied an alternate version of F

cc where type and coercion abstraction

are separate constructs. Namely, the usual type abstraction ∀α τ and the coercion abstraction (τ1 ⊲ τ2) ⇒ ρ. However, the test of coherence has to be done by block since some coercions hold only for some particular instances of the context. The additional flexibility we get is thus negligible, since they are just η-expansion variants.

34 / 34

slide-94
SLIDE 94

Weakening

To simplify readability, we add a proof of coercion weakening.

Weak

G ⇒ Γ ⊢ (Σ ⊢ τ) ⊲ ρ ∗G ⇒ Γ ⊢ τ ⊲ ρ

Γ ρ

G

+∅ τ +Σ τ

34 / 34

slide-95
SLIDE 95

Extremes

Top and bottom permit to illustrate subtyping.

Coercion rules

Γ ⊤

+∅ τ

Top

⊤ ⇒ Γ ⊢ τ ⊲ ⊤

Γ τ

⊥τ

+∅ ⊥

Bot

⊥τ ⇒ Γ ⊢ ⊥ ⊲ τ 34 / 34

slide-96
SLIDE 96

Existentials

Existential types are inherently difficult (breaks preservation). (x1 x2 : ∃α (α → Int) × α) ⊢ unpack α, x = b x1 x2 in (π1 x) (π2 x) : Int erases to let x = b x1 x2 in (π1 x) (π2 x) (π1 (b x1 x2)) (π2 (b x1 x2)) There is at least four solutions:

1 keep the calculus and use dependent types, 2 keep the calculus and use a standardization argument (Riba 2007), 3 change the calculus and use a simple argument, or 4 use the CPS encoding.

Only the second solution does not restore preservation.

34 / 34

slide-97
SLIDE 97

Eta-expansion

Γ τ1 × τ2

G1 × G2

+Σ τ ′

1 × τ ′ 2

Γ τ → ρ

G1 → G2

+Σ τ ′ → ρ′ Γ τ1 × τ2

,

G1

π1

τ ′

1 × τ ′ 2

+Σ τ ′

1

Γ τ1

G2

π2

τ ′

1 × τ ′ 2

+Σ τ ′

2

Γ τ2 Γ τ → ρ

λx

G2

@

τ ′ → ρ′

G1

x

τ τ ′ +Σ ρ′ Γ, (x : τ) ρ

34 / 34

slide-98
SLIDE 98

Eta-expansion: distributivity

+ α

∀α τ → ρ τ → ρ

[α]

(∀α τ) → ∀α ρ

[α] → Λα

Λα ⇒ Γ ⊢ ( α ⊢ ρ) ⊲ ∀α ρ [α] ⇒ Γ, α ⊢ ∀α τ ⊲ τ [α] → Λα ⇒ Γ ⊢ ( α ⊢ τ → ρ) ⊲ (∀α τ) → ∀α ρ 34 / 34

slide-99
SLIDE 99

Recursive types

To subsume Constraint ML, we need equi-recursive types. We extend types with recursive types µα τ, coercions with folding and unfolding, and propositions with coinduction. The two rules of equi-recursive types (Amadio, Cardelli, 1993) are admissible:

EquivPeriod

α → ρ : WF Σ ⊢ τ1 ≡ ρ[α/τ1] Σ ⊢ ρ[α/τ2] ≡ τ2 Σ ⊢ τ1 ≡ τ2

EtaMu

Σ, (α : ⋆, β : ⋆ | α ⊲ β) ⊢ τ ⊲ ρ Σ ⊢ µα τ ⊲ µβ ρ

34 / 34

slide-100
SLIDE 100

Type functions

It is possible to extend the type system with arrow kinds. The result kind may mention the argument variable. κ ::= ⋆ | 1 | κ × κ | (α : κ) → κ | {α : κ | P} Kinds The design of this extension is not satisfactory yet.

34 / 34

slide-101
SLIDE 101

Subterm property

We will only consider erasable type system features for type systems satisfying the subterm property.

Subterm property

A type system satisfies the subterm property when the term judgment premises of its term typing rules only consider subterms of the conclusion.

OK

Γ ⊢ τ type Γ, (x : τ) ⊢ a : ρ Γ ⊢ λx a : τ → ρ

OK

Γ, α ⊢ a : τ Γ ⊢ a : ∀α τ

BAD

x / ∈ fv(a) Γ ⊢ λx a x : τ → ρ Γ ⊢ a : τ → ρ Type systems satisfying the subterm property annotate the exact program we are interested in, not an equivalent one.

34 / 34

slide-102
SLIDE 102

Comparison with FC

FC has only type equality coercions: coercions do not bind coercions are structural (head type constructors are equal) (∀α τ → ρ) = (τ → ∀α ρ) not a coercion coercions can be decomposed subject reduction (and bisimulation) holds FC has coherent coercion abstraction only at top-level: coercion abstractions and applications cannot be fully erased FC has a kind equality and an heterogeneous type equality.

34 / 34

slide-103
SLIDE 103

Example with subtyping

Consequence of extending subtyping to first-class coercions:

List module signature

∃(list : ⋆ → ⋆ | ∀(α, β | α ⊲ β) list α ⊲ list β) . . . The notion of variance comes by construction and is not built-in. As such, it can be extended.

34 / 34

slide-104
SLIDE 104

1

Introduction Background Lambda Calculus

2

Part I Polymorphism Eta-expansion Bounded polymorphism Properties System Fp

ι 3

Part II: Semantical approach Coherent polymorphism Incoherent polymorphism System F

cc 4

Future work

5

Extra slides Intersection types Push GADTs Why block abstraction? Weakening Extremes Existentials Eta-expansion Recursive types Type functions Subterm property Comparison with FC

34 / 34