Graphical types and constraints Second-order polymorphism and - - PowerPoint PPT Presentation

graphical types and constraints
SMART_READER_LITE
LIVE PREVIEW

Graphical types and constraints Second-order polymorphism and - - PowerPoint PPT Presentation

Graphical types and constraints Second-order polymorphism and inference Boris Yakobowski, under the supervision of Didier R Who? emy Where? INRIA Rocquencourt, project Gallium 17th December, 2008 When? Outline 1 Introduction:


slide-1
SLIDE 1

Graphical types and constraints

Second-order polymorphism and inference

Who?

Boris Yakobowski, under the supervision of Didier R´

emy Where?

INRIA Rocquencourt, project Gallium

When?

17th December, 2008

slide-2
SLIDE 2

Outline

1 Introduction: polymorphism in programming languages 2 Graphic types and MLF instance 3 Type inference through graphic constraints 4 A Church-style language for MLF 5 Conclusion

slide-3
SLIDE 3

Types in programs

Context

Safety of software

Expressivity of programming languages

3/51

slide-4
SLIDE 4

Types in programs

Context

Safety of software

Expressivity of programming languages A key tool for this: Typing

Prevents the programmer from writing some forms of erroneous code e.g. 1 + ”I am a string”

(Of course, semantically incorrect code is still possible)

3/51

slide-5
SLIDE 5

Types in programs

Context

Safety of software

Expressivity of programming languages A key tool for this: Typing

Prevents the programmer from writing some forms of erroneous code e.g. 1 + ”I am a string”

(Of course, semantically incorrect code is still possible)

Static typing is important

if (...) then x := x+1; else // rarely executed code print_string(x)

3/51

slide-6
SLIDE 6

Type inference

The compiler infers the types of the expressions of the program

Removes the need to write (often redundant) type annotations Node n = new Node();

Facilitates rapid prototyping

Can infer types more general than the ones the programmer had in mind

4/51

slide-7
SLIDE 7

Type inference issues

Which type should we give to functions admitting more than one possible type? Example: finding the length of a list let rec length = function | [] -> 0 | _ :: q -> 1 + length q length: int list → int float list → int

5/51

slide-8
SLIDE 8

ML-style polymorphism

Functions no longer receive monomorphic types, but type schemes sort: ∀α. α list → α list

An alternative way of saying “for any type α, sort has type α list → α list” The symbol ∀ introduces universal quantification

6/51

slide-9
SLIDE 9

ML-style polymorphism

Functions no longer receive monomorphic types, but type schemes sort: ∀α. α list → α list

An alternative way of saying “for any type α, sort has type α list → α list” The symbol ∀ introduces universal quantification

ML Polymorphism

One of the key reasons of the success of ML as a language

Full type inference

(annotations are never needed in programs)

Sometimes a bit limited universal quantification only in front of the type

6/51

slide-10
SLIDE 10

Second-order polymorphism

Universal quantification under arrows is allowed λ(f ) f (λ(x) x) : ∀α. ((∀β. β → β) → α) → α

Many uses:

Encoding existential types Polymorphic iterators over polymorphic structures State encapsulation runST :: ∀α. (∀β. ST β α) → α . . .

7/51

slide-11
SLIDE 11

Second-order polymorphism

Universal quantification under arrows is allowed λ(f ) f (λ(x) x) : ∀α. ((∀β. β → β) → α) → α

Many uses:

Encoding existential types Polymorphic iterators over polymorphic structures State encapsulation runST :: ∀α. (∀β. ST β α) → α . . .

We want at least the expressivity of System F But type inference in System F is undecidable!

7/51

slide-12
SLIDE 12

System F as a programming language

System F does not have principal types Example: id

  • λ(x) x

: ∀β. β → β choose

  • λ(x) λ(y) x

: ∀α. α → α → α

8/51

slide-13
SLIDE 13

System F as a programming language

System F does not have principal types Example: id

  • λ(x) x

: ∀β. β → β choose

  • λ(x) λ(y) x

: ∀α. α → α → α choose id :

  • (∀β. β → β) → (∀β. β → β)

α = ∀β. β → β ∀γ. (γ → γ) → (γ → γ) α = γ → γ No type is more general than the other This is a fundamental limitation of System-F (and more generally of System-F types)

8/51

slide-14
SLIDE 14

Adding flexible quantification to types

Flexible quantification

MLF types extend System F types with an instance-bounded quantification of the form ∀ (α τ) τ ′:

Both τ and τ ′ can be instantiated inside ∀ (α τ) τ ′

All occurrences of α in τ ′ must pick the same instance of τ

9/51

slide-15
SLIDE 15

Adding flexible quantification to types

Flexible quantification

MLF types extend System F types with an instance-bounded quantification of the form ∀ (α τ) τ ′:

Both τ and τ ′ can be instantiated inside ∀ (α τ) τ ′

All occurrences of α in τ ′ must pick the same instance of τ

Example: choose id : ∀ (α ∀β. β → β) α → α ⊑ (∀β. β → β) → (∀β. β → β)

  • r

⊑ ∀γ. (γ → γ) → (γ → γ)

9/51

slide-16
SLIDE 16

Adding rigid quantification

Flexible quantification solves the problem of principality

But not the fact that type inference is undecidable

10/51

slide-17
SLIDE 17

Adding rigid quantification

Flexible quantification solves the problem of principality

But not the fact that type inference is undecidable

Rigid quantification

Instance-bounded quantification, of the form ∀ (α = τ) τ ′

τ cannot (really) be instantiated inside ∀ (α = τ) τ ′

But ∀ (α = τ) α → α and ∀ (α = τ) ∀ (α′ = τ) α → α′ are different as far as type inference is concerned

10/51

slide-18
SLIDE 18

MLF as a type system

Extends ML and System F, and combines the benefits of both

Compared to ML

The expressivity of second-order polymorphism is available

All ML programs remain typable unchanged

Compared to System F

MLF has type inference

Programs have principal types (given their type annotations) Moreover:

in practice, programs require very few type annotations

typable programs are stable under a wide range of program transformations

11/51

slide-19
SLIDE 19

How to improve MLF

Limitations

Instance-bounded quantification makes equivalence and instance between types unwieldy

Meta-theoretical results dense and non-modular

Algorithmic inefficiency of type inference

Not suitable for use in a typed compiler, by lack of a language to describe reduction

My work

Use graphic types and constraints to improve the presentation

Study efficient type inference

Define an internal language for MLF

12/51

slide-20
SLIDE 20

Outline

1 Introduction: polymorphism in programming languages 2 Graphic types and MLF instance 3 Type inference through graphic constraints 4 A Church-style language for MLF 5 Conclusion

slide-21
SLIDE 21

Graphic types: an alternative representation of types

A graphic type

A term-dag, representing the skeleton of the type

Sharing is important, but only for variables Variables are anonymous

→ → α ⊥ ⊥ β → → → α ⊥ ⊥ β

  • (α→β)→(α→β)

→ → ⊥ γ → → → ⊥ γ

  • (γ→γ)→(γ→γ)

14/51

slide-22
SLIDE 22

Graphic types: an alternative representation of types

A graphic type

A term-dag, representing the skeleton of the type

Sharing is important, but only for variables Variables are anonymous

A binding tree, indicating where variables are bound

→ → α ⊥ ⊥ β

∀α. (∀β1. α → β1) → (∀β2. α → β2))

14/51

slide-23
SLIDE 23

Graphic types: an alternative representation of types

A graphic type

A term-dag, representing the skeleton of the type

Sharing is important, but only for variables Variables are anonymous

A binding tree, indicating where variables are bound

Some well-scopedness properties

→ → α ⊥

int

(∀α1. α1 → int) → ( α2 → int) Ill-scoped!

14/51

slide-24
SLIDE 24

Graphic types: an alternative representation of types

A graphic type

A term-dag, representing the skeleton of the type

Sharing is important, but only for variables Variables are anonymous

A binding tree, indicating where variables are bound

Some well-scopedness properties Advantages of graphic types:

Commutation of binders, no α-conversion, no useless quantification. . .

Bring closer theory and implementation

Same formalism for different systems: ML, System F, MLF, F≤, . . .

14/51

slide-25
SLIDE 25

Graphic MLF types

Two kind of binding edges, for flexible and rigid quantification

Non-variables nodes can be bound

→ γ → α ⊥ ⊥ β

∀ (α ⊥) ∀ (γ = ∀ (β ⊥) α → β) γ → γ

15/51

slide-26
SLIDE 26

Graphic MLF types

Two kind of binding edges, for flexible and rigid quantification

Non-variables nodes can be bound

Sharing of non-variable nodes becomes important

→ → ⊥

∀ (α σid) α → α Possible type for λ(x) x =

→ → ⊥ → ⊥

∀ (α σid) ∀ (β σid) α → β Incorrect for λ(x) x

15/51

slide-27
SLIDE 27

Instance on graphic MLF types

The instance relation ⊑

Four atomic operations on graphs:

16/51

slide-28
SLIDE 28

Instance on graphic MLF types

The instance relation ⊑

Four atomic operations on graphs:

Grafting: replacing a variable by a closed type (variable substitution)

→ ⊥

→ → ⊥

16/51

slide-29
SLIDE 29

Instance on graphic MLF types

The instance relation ⊑

Four atomic operations on graphs:

Grafting: replacing a variable by a closed type (variable substitution) Merging: fusing two identical subgraphs (correlates the two corresponding subtypes)

→ → ⊥ → ⊥

→ → ⊥

16/51

slide-30
SLIDE 30

Instance on graphic MLF types

The instance relation ⊑

Four atomic operations on graphs:

Grafting: replacing a variable by a closed type (variable substitution) Merging: fusing two identical subgraphs (correlates the two corresponding subtypes) Raising: edge extrusion (removes the possibility to introduce universal quantification)

→ → ⊥

→ → ⊥

16/51

slide-31
SLIDE 31

Instance on graphic MLF types

The instance relation ⊑

Four atomic operations on graphs:

Grafting: replacing a variable by a closed type (variable substitution) Merging: fusing two identical subgraphs (correlates the two corresponding subtypes) Raising: edge extrusion (removes the possibility to introduce universal quantification) Weakening: turns a flexible edge into a rigid one (forbids further instantiation of the corresponding type)

→ → ⊥

→ → ⊥

16/51

slide-32
SLIDE 32

Instance on graphic MLF types

The instance relation ⊑

Four atomic operations on graphs:

Grafting: replacing a variable by a closed type (variable substitution) Merging: fusing two identical subgraphs (correlates the two corresponding subtypes) Raising: edge extrusion (removes the possibility to introduce universal quantification) Weakening: turns a flexible edge into a rigid one (forbids further instantiation of the corresponding type)

A control of permissions rejecting some unsafe instances

16/51

slide-33
SLIDE 33

Permissions on nodes

Some instances on types would be unsound Example: e

  • λ(x : ∀α. ∀β. α → β) x

→ → ⊥ ⊥ → ⊥ ⊥

Correct type for e

17/51

slide-34
SLIDE 34

Permissions on nodes

Some instances on types would be unsound Example: e

  • λ(x : ∀α. ∀β. α → β) x

→ → ⊥ ⊥ → ⊥ ⊥

Correct type for e ⊑

→ → ⊥ → ⊥ ⊥

Incorrect type for e: e (λ(y) y) would have type ∀α. ∀β. α → β

17/51

slide-35
SLIDE 35

Permissions on nodes

Some instances on types would be unsound

Nodes receive permissions according to the binding structure above and below them

Permissions are represented by colors

→ → ⊥ → ⊥

int

All forms of instance are forbidden on red nodes, as well as grafting on orange ones

This ensures type soundness

17/51

slide-36
SLIDE 36

Unification on MLF graphic types

Unification on graphic types:

Finds the most general type τ such that τ1 ⊑ τ and τ2 ⊑ τ

Or unifies two nodes in a certain type (more general)

18/51

slide-37
SLIDE 37

Unification on MLF graphic types

Unification on graphic types:

Finds the most general type τ such that τ1 ⊑ τ and τ2 ⊑ τ

Or unifies two nodes in a certain type (more general)

Unification algorithm

First-order unification on the skeleton Minimal raising and weakening so that the binding trees match Control of permissions

18/51

slide-38
SLIDE 38

Unification on MLF graphic types

Unification on graphic types:

Finds the most general type τ such that τ1 ⊑ τ and τ2 ⊑ τ

Or unifies two nodes in a certain type (more general)

Unification algorithm

First-order unification on the skeleton Minimal raising and weakening so that the binding trees match Control of permissions

Unification

is principal on all useful problems

has linear complexity

18/51

slide-39
SLIDE 39

Outline

1 Introduction: polymorphism in programming languages 2 Graphic types and MLF instance 3 Type inference through graphic constraints 4 A Church-style language for MLF 5 Conclusion

slide-40
SLIDE 40

Type inference in graphic MLF

Constraints are an elegant way to present type inference

Scale better to non-toy languages More general than an algorithm

Graphic constraints as an extension of graphic types

Can be used to perform type inference on graphic types

Permit type inference for ML, MLF, and probably other systems

20/51

slide-41
SLIDE 41

Graphic constraints

Graphic types extended with four new constructs

Unification edges Force two nodes to be equal Existential nodes “Floating” nodes, used only to introduce other constraints Generalization nodes G Instantiation edges

Same instance relation as on graphic types

Meta-theoretical results can be reused unchanged

21/51

slide-42
SLIDE 42

Type generalization

Type generalization is essential in MLF, just as in ML

Gen nodes are used to promote types into type schemes G g

→ ⊥ α

g : ∀α. α → α

22/51

slide-43
SLIDE 43

Type generalization

Type generalization is essential in MLF, just as in ML

Gen nodes are used to promote types into type schemes G g G g′

β ⊥

→ ⊥ α

g : ∀α. α → α g′ : ∀β. β → α α is free at the level of g′

22/51

slide-44
SLIDE 44

Type generalization

Type generalization is essential in MLF, just as in ML

Gen nodes are used to promote types into type schemes G g G g′

β ⊥

→ ⊥ α

g : ∀α. α → α g′ : ∀β. β → α α is free at the level of g′

Gen nodes also delimit generalization scopes

22/51

slide-45
SLIDE 45

Instantiation edges

Constrain a node to be an instance of a type scheme G G g

β ⊥

→ n ⊥ α

e

e constrains n to be an instance of g

23/51

slide-46
SLIDE 46

Instantiation edges

Constrain a node to be an instance of a type scheme G G g

β ⊥

→ n ⊥ α

e g : ∀β. β → α n : α → α e is solved (take β = α)

e constrains n to be an instance of g

23/51

slide-47
SLIDE 47

Instantiation edges

Constrain a node to be an instance of a type scheme G G g

β ⊥

→ n ⊥ α

e g : β → α n : α → α e is not solved (β = α)

e constrains n to be an instance of g

23/51

slide-48
SLIDE 48

Semantics of constraints

Presolutions

A presolution of a constraint χ is an instance of χ in which all the instantiation and unification edges are solved.

Presolutions correspond to typing derivations, and are in correspondance with Church-style λ-terms G

→ ⊥

G

⊥ ⊥

G

G

24/51

slide-49
SLIDE 49

Semantics of constraints

Presolutions

A presolution of a constraint χ is an instance of χ in which all the instantiation and unification edges are solved.

Presolutions correspond to typing derivations, and are in correspondance with Church-style λ-terms

Solutions

A solution of a constraint is the type scheme represented by a presolutions of a constraint.

G

→ ⊥

G

⊥ ⊥

G

G

⊥ → ⊥

24/51

slide-50
SLIDE 50

Typing constraints

Source language:

(MLF only)

a ::= x | λ(x) a | a a | let x = a in a | (a : τ) | λ(x : τ) a

25/51

slide-51
SLIDE 51

Typing constraints

Source language:

(MLF only)

a ::= x | λ(x) a | a a | let x = a in a | (a : τ) | λ(x : τ) a

λ-terms are translated into constraints compositionnally

a represents the typing constraint for a the blue arrows are constraint edges for the free variables of a

25/51

slide-52
SLIDE 52

Typing constraints

Source language:

(MLF only)

a ::= x | λ(x) a | a a | let x = a in a | (a : τ) | λ(x : τ) a

λ-terms are translated into constraints compositionnally

a represents the typing constraint for a the blue arrows are constraint edges for the free variables of a

One generalization scope by subexpression

in ML, only needed for let; in MLF, needed everywhere

Same typing constraints for ML and MLF

the superfluous gen nodes can be removed in ML MLF constraints can be instantiated by the more general types of MLF

25/51

slide-53
SLIDE 53

Typing constraint for an abstraction

λ(x) a

  • G

→ ⊥

α a

β x

λ(x) a can receive type α → β, provided

α is the (common) type of all the occurrences of x in a β is an instance of the type of a.

26/51

slide-54
SLIDE 54

Typing constraint for an application

a b

  • G

a b

→ ⊥

α

β

a b can receive type β, provided there exists α such that

a → β is an instance of the type of a α is an instance of the type of b

27/51

slide-55
SLIDE 55

Typing constraint for a let

let x = a in b

  • b

a x

As in ML

Each occurrence of x in b must have a (possibly different) instance of the type of a

28/51

slide-56
SLIDE 56

Typing constraint for variables

x X

  • G

x ∈ X

the variable node is constrained by the appropriate edge from the typing environment

29/51

slide-57
SLIDE 57

Acyclic constraints

Constraints can encode problems with polymorphic recursion let rec x = a in b

  • b

x a x

Restriction to constraints with an acyclic dependency relation

Dependency relation

g depends on g′ if g′ is in the scope of g, or if g′ n with n in the scope of g

All typing constraints are acyclic

30/51

slide-58
SLIDE 58

Solving acyclic constraints

Demo

31/51

slide-59
SLIDE 59

Solving acyclic constraints

Demo

Principal presolutions and solutions

31/51

slide-60
SLIDE 60

Complexity of type inference

ML : type inference is DExp-Time complete

(if types are not printed)

[McAllester 2003]: type inference in O(kn(d + α(kn)))

k is the maximal size of type schemes d is the maximal nesting of type schemes

32/51

slide-61
SLIDE 61

Complexity of type inference

ML : type inference is DExp-Time complete

(if types are not printed)

[McAllester 2003]: type inference in O(kn(d + α(kn)))

k is the maximal size of type schemes d is the maximal nesting of type schemes

In ML, d is the maximal left-nesting of let

(i.e. let x = (let y = . . . in . . .) in . . .)

32/51

slide-62
SLIDE 62

Complexity of type inference

ML : type inference is DExp-Time complete

(if types are not printed)

[McAllester 2003]: type inference in O(kn(d + α(kn)))

k is the maximal size of type schemes d is the maximal nesting of type schemes

In MLF, unification has the same complexity as in ML, but we introduce more type schemes Still, d is invariant by right-nesting of let

Complexity of MLF type inference

Under the hypothesis that programs are composed of a cascade of toplevel let declarations, type inference in MLF has linear complexity.

32/51

slide-63
SLIDE 63

Outline

1 Introduction: polymorphism in programming languages 2 Graphic types and MLF instance 3 Type inference through graphic constraints 4 A Church-style language for MLF 5 Conclusion

slide-64
SLIDE 64

An explicit langage for MLF

Study subject reduction in MLF

To be used inside a typed compiler MLF types are more expressive than F ones

System F cannot be used as a target langage

Need for a core, Church-style, langage for MLF, called xMLF

34/51

slide-65
SLIDE 65

From System F to xMLF

xMLF generalizes System F

Types: σ ::= ⊥ | ∀ (α σ) σ | α | σ → σ

Rigid quantification is only needed for type inference, and is inlined in xMLF

Terms : a ::= x | λ(x : σ) a | a a | let x = a in a | Λ(α σ) a | a[ϕ]

Typing rules are the same as in System F, except for type application TApp Γ ⊢ a : σ Γ ⊢ ϕ : σ ≤ σ′ Γ ⊢ a[ϕ] : σ′

35/51

slide-66
SLIDE 66

Type computations

Instance is explicitely witnessed through the use of type computations ϕ ::= ε | ϕ; ϕ | ⊲ σ | α ⊳ | ∀ ( ϕ) | ∀ (α ) ϕ | |

36/51

slide-67
SLIDE 67

Type computations

Instance is explicitely witnessed through the use of type computations ϕ ::= ε | ϕ; ϕ | ⊲ σ | α ⊳ | ∀ ( ϕ) | ∀ (α ) ϕ | |

Inst-Reflex Γ ⊢ ε : σ ≤ σ Inst-Trans Γ ⊢ ϕ1 : σ1 ≤ σ2 Γ ⊢ ϕ2 : σ2 ≤ σ3 Γ ⊢ ϕ1; ϕ2 : σ1 ≤ σ3 Inst-Bot Γ ⊢ ⊲ σ : ⊥ ≤ σ Inst-Hyp α σ ∈ Γ Γ ⊢ α ⊳ : σ ≤ α Inst-Inner Γ ⊢ ϕ : σ1 ≤ σ2 Γ ⊢ ∀ ( ϕ): ∀ (α σ1) σ ≤ ∀ (α σ2) σ Inst-Outer Γ, ϕ : α σ ⊢ ϕ : σ1 ≤ σ2 Γ ⊢ ∀ (α ) ϕ : ∀ (α σ) σ1 ≤ ∀ (α σ) σ2 Inst-Quant-Elim Γ ⊢ : ∀ (α σ) σ′ ≤ σ′{α ← σ} Inst-Quant-Intro α / ∈ ftv(σ) Γ ⊢ : σ ≤ ∀ (α ⊥) σ

36/51

slide-68
SLIDE 68

Example: back to choose id

choose Λ(α ⊥) λ(x : α) λ(y : α) x : ∀ (α ⊥) α → α → α id Λ(β ⊥) λ(x : β) x : ∀ (β ⊥) β → β

To make choose id well-typed, we must choose a type into which α must be instantiated

37/51

slide-69
SLIDE 69

Example: back to choose id

choose Λ(α ⊥) λ(x : α) λ(y : α) x : ∀ (α ⊥) α → α → α id Λ(β ⊥) λ(x : β) x : ∀ (β ⊥) β → β

To make choose id well-typed, we must choose a type into which α must be instantiated

e Λ(γ σid) (choose[∀ ( ⊲ γ); ])

  • γ→γ

(id[γ ⊳])

γ

: ∀ (γ σid) γ → γ

37/51

slide-70
SLIDE 70

Example: back to choose id

choose Λ(α ⊥) λ(x : α) λ(y : α) x : ∀ (α ⊥) α → α → α id Λ(β ⊥) λ(x : β) x : ∀ (β ⊥) β → β

To make choose id well-typed, we must choose a type into which α must be instantiated

e Λ(γ σid) (choose[∀ ( ⊲ γ); ])

  • γ→γ

(id[γ ⊳])

γ

: ∀ (γ σid) γ → γ

e[] : σid → σid e[; ∀ (δ ) (∀ ( ∀ ( ⊲ δ); ); )] : ∀ (δ ⊥) (δ → δ) → (δ → δ)

37/51

slide-71
SLIDE 71

Reducing expressions

Usual β-reduction

(λ(x : τ) a1) a2 − → a1{x ← a2} let x = a2 in a1 − → a1{x ← a2}

38/51

slide-72
SLIDE 72

Reducing expressions

Usual β-reduction

6 specific rules to reduce type applications

(λ(x : τ) a1) a2 − → a1{x ← a2} let x = a2 in a1 − → a1{x ← a2} a[ε] − → a a[ϕ; ϕ′] − → a[ϕ][ϕ′] a[] − → Λ(α ⊥) a if α / ∈ ftv(a) (Λ(α τ) a)[] − → a{α ⊳ ← ε}{α ← τ} (Λ(α τ) a)[∀ ( ϕ)] − → Λ(α τ[ϕ]) a{α ⊳ ← ϕ; α ⊳} (Λ(α τ) a)[∀ (α ) ϕ] − → Λ(α τ) (a[ϕ])

38/51

slide-73
SLIDE 73

Reducing expressions

Usual β-reduction

6 specific rules to reduce type applications

Context rule

(λ(x : τ) a1) a2 − → a1{x ← a2} let x = a2 in a1 − → a1{x ← a2} a[ε] − → a a[ϕ; ϕ′] − → a[ϕ][ϕ′] a[] − → Λ(α ⊥) a if α / ∈ ftv(a) (Λ(α τ) a)[] − → a{α ⊳ ← ε}{α ← τ} (Λ(α τ) a)[∀ ( ϕ)] − → Λ(α τ[ϕ]) a{α ⊳ ← ϕ; α ⊳} (Λ(α τ) a)[∀ (α ) ϕ] − → Λ(α τ) (a[ϕ]) E{a} − → E{a′} if a − → a′

38/51

slide-74
SLIDE 74

Results on xMLF

Correctness:

Subject reduction, for all contexts (including under λ and Λ)

Progress for call-by-value with or without the value restriction, and for call-by-name

This is the first time that MLF is proven sound for call-by-name

Mechanized proof of a previous version of the system

39/51

slide-75
SLIDE 75

Results on xMLF

Correctness:

Subject reduction, for all contexts (including under λ and Λ)

Progress for call-by-value with or without the value restriction, and for call-by-name

This is the first time that MLF is proven sound for call-by-name

Mechanized proof of a previous version of the system

Confluence of strong reduction

The reduction rule of System F for type applications is derivable (Λ(α) a)[σ] − → a{α ← σ}

(when a is a System F term, and σ a System F type)

39/51

slide-76
SLIDE 76

From presolutions to xMLF terms

MLF presolutions can be algorithmically translated into well-typed xMLF terms This ensures the type soundness of our type inference framework

40/51

slide-77
SLIDE 77

From presolutions to xMLF terms

MLF presolutions can be algorithmically translated into well-typed xMLF terms This ensures the type soundness of our type inference framework

Nodes flexibly bound on gen nodes are translated into xMLF type abstractions

The fact that an instantiation edge is solved is translated into a type computation

40/51

slide-78
SLIDE 78

From presolutions to xMLF terms: example

G

α ⊥ G

β ⊥

→ → ⊥

e A presolution for K λ(x) λ(y) x K : ∀ (α) α → σid → α

41/51

slide-79
SLIDE 79

From presolutions to xMLF terms: example

G

α ⊥ G

β ⊥

→ → ⊥

e A presolution for K λ(x) λ(y) x K : ∀ (α) α → σid → α Λ(α) λ(x : α) (Λ(β) λ(y : β) x)

  • ∀ (β) β→α
  • α→σid→α

41/51

slide-80
SLIDE 80

From presolutions to xMLF terms: example

G

α ⊥ G

β ⊥

→ → ⊥

e A presolution for K λ(x) λ(y) x K : ∀ (α) α → σid → α Λ(α) λ(x : α) (Λ(β) λ(y : β) x)

  • ∀ (β) β→α

T (e)

  • [∀ ( ⊲ σid); ]
  • α→σid→α

41/51

slide-81
SLIDE 81

Outline

1 Introduction: polymorphism in programming languages 2 Graphic types and MLF instance 3 Type inference through graphic constraints 4 A Church-style language for MLF 5 Conclusion

slide-82
SLIDE 82

Related works

Bringing System F and ML closer

restriction to predicative fragment higher-order unification local type inference boxy types FPH, HML

Typing constraints for ML

Encoding MLF into System F

43/51

slide-83
SLIDE 83

Contributions

Graphic types and constraints are the good way to study MLF

Presentation of MLF well-understood, and modular

Generic type inference framework: works indifferently for ML or MLF

Optimal theoretical complexity, and excellent practical complexity for type inference Graphs can be used to explain type inference in a simple way, and not only for MLF

xMLF makes MLF suitable for use in a typed compiler

44/51

slide-84
SLIDE 84

Perspectives

Extensions to advanced typing features

qualified types GADTs, recursive types dependent types F ω

Revisit HML and FPH using our inference framework

45/51

slide-85
SLIDE 85

Thanks

46/51

slide-86
SLIDE 86

Equivalence and instance on types

⊑ permits only more sharing/raising/weakening

exactly corresponds to implementation simpler to reason about

47/51

slide-87
SLIDE 87

Equivalence and instance on types

⊑ permits only more sharing/raising/weakening

exactly corresponds to implementation simpler to reason about

≈ identifies monomorphic subparts represented differently

→ → ⊥ →

int int ≈

→ → ⊥ →

int

47/51

slide-88
SLIDE 88

Equivalence and instance on types

⊑ permits only more sharing/raising/weakening

exactly corresponds to implementation simpler to reason about

≈ identifies monomorphic subparts represented differently

⊑≈ is ⊑ modulo ≈

monomorphic subparts need not be bound at all same expressivity as ⊑

47/51

slide-89
SLIDE 89

Equivalence and instance on types

⊑ permits only more sharing/raising/weakening

exactly corresponds to implementation simpler to reason about

≈ identifies monomorphic subparts represented differently

⊑≈ is ⊑ modulo ≈

monomorphic subparts need not be bound at all same expressivity as ⊑

⊏ − ⊐ − views types up to rigid quantification and ≈

→ → ⊥ → ⊥

⊏ − ⊐ −

→ → ⊥

47/51

slide-90
SLIDE 90

Equivalence and instance on types

⊑ permits only more sharing/raising/weakening

exactly corresponds to implementation simpler to reason about

≈ identifies monomorphic subparts represented differently

⊑≈ is ⊑ modulo ≈

monomorphic subparts need not be bound at all same expressivity as ⊑

⊏ − ⊐ − views types up to rigid quantification and ≈

⊑⊏

− ⊐ − is ⊑ modulo ⊏

− ⊐ −

most expressive system undecidable type inference terms typable for ⊑⊏

− ⊐ − are typable for ⊑ through type annotations

47/51

slide-91
SLIDE 91

Expansion

Expansion takes a fresh instance of a type scheme G g G g′

→ ⊥ ⊥

48/51

slide-92
SLIDE 92

Expansion

Expansion takes a fresh instance of a type scheme G g G g′

→ ⊥ ⊥ → ⊥ ⊥ ◮

The structure of the type scheme is copied

48/51

slide-93
SLIDE 93

Expansion

Expansion takes a fresh instance of a type scheme G g G g′

→ ⊥ ⊥ → ⊥ ⊥ ◮

The structure of the type scheme is copied

The nodes that are not local to the scheme are shared between the copy and the scheme

48/51

slide-94
SLIDE 94

Expansion

Expansion takes a fresh instance of a type scheme G g G g′

→ ⊥ ⊥ → ⊥ ⊥ ◮

The structure of the type scheme is copied

The nodes that are not local to the scheme are shared between the copy and the scheme

Where to bind nodes?

in MLF, inner polymorphism

48/51

slide-95
SLIDE 95

Expansion

Expansion takes a fresh instance of a type scheme G g G g′

→ ⊥ ⊥ → ⊥ ⊥ ◮

The structure of the type scheme is copied

The nodes that are not local to the scheme are shared between the copy and the scheme

Where to bind nodes?

in MLF, inner polymorphism in ML, to the gen node at which the copy is bound (less general)

48/51

slide-96
SLIDE 96

Propagation

Used to enforce the constraints imposed by an instantiation edge G G g

α ⊥

β ⊥

n

γ g : ∀α. α → (β → β) n : ∀γ. γ → γ

49/51

slide-97
SLIDE 97

Propagation

Used to enforce the constraints imposed by an instantiation edge

We copy the type scheme G G g

α ⊥

β ⊥

n

γ

→ ⊥ → ⊥

g : ∀α. α → (β → β) n : ∀γ. γ → γ

49/51

slide-98
SLIDE 98

Propagation

Used to enforce the constraints imposed by an instantiation edge

We copy the type scheme, and add an unification edge between the constrained node and the copy G G g

α ⊥

β ⊥

n

γ

→ ⊥ → ⊥

g : ∀α. α → (β → β) n : ∀γ. γ → γ

49/51

slide-99
SLIDE 99

Propagation

Used to enforce the constraints imposed by an instantiation edge

We copy the type scheme, and add an unification edge between the constrained node and the copy G G g

α ⊥

β ⊥

n

g : ∀α. α → (β → β) n : (β → β) → (β → β)

Solving the unification edges enforces the constraint

49/51

slide-100
SLIDE 100

Coercions

Annotated terms are not primitive, but syntactic sugar

(a : τ)

  • cτ a

λ(x : τ) a

  • λ(x) let x = (x : τ) in a

Coercion functions

Primitives of the typing environment cτ :

τ τ

The domain of the arrow is frozen The codomain can be freely instantiated

50/51

slide-101
SLIDE 101

Solving acyclic constraints

Solving an acyclic constraint χ

1. Solve the initial unification edges (by unification) 2. Order the instantiation edges according to the dependency relation 3. Propagate the first unsolved instantiation edge e, then solve the unification edges created

This solves e, and does not break the already solved instantiation edges

4. Iterate step 3 until all the instantiation edge are solved

51/51

slide-102
SLIDE 102

Solving acyclic constraints

Solving an acyclic constraint χ

1. Solve the initial unification edges (by unification) 2. Order the instantiation edges according to the dependency relation 3. Propagate the first unsolved instantiation edge e, then solve the unification edges created

This solves e, and does not break the already solved instantiation edges

4. Iterate step 3 until all the instantiation edge are solved

Correctness

This algorithm computes a principal presolution of χ

51/51