F A graphical presentation of ML types with a linear-time - - PowerPoint PPT Presentation

f a graphical presentation of ml types with a linear time
SMART_READER_LITE
LIVE PREVIEW

F A graphical presentation of ML types with a linear-time - - PowerPoint PPT Presentation

F A graphical presentation of ML types with a linear-time unification algorithm Didier R emy, Boris Yakobowski INRIA Rocquencourt F A brief presentation of ML [Le Botlan-R emy, ICFP 2003] [Le Botlan, 2003] F? Why ML 3(1)/?? ML


slide-1
SLIDE 1

A graphical presentation of ML F types with a linear-time unification algorithm

Didier R´ emy, Boris Yakobowski

INRIA Rocquencourt

slide-2
SLIDE 2

A brief presentation of ML F

[Le Botlan-R´ emy, ICFP 2003] [Le Botlan, 2003]

slide-3
SLIDE 3

Why ML F?

3(1)/??

ML System F Outer ∀ Inner (1st class) ∀ (Good)

∀αβ. (α → β) → α t → β t λ(f : ∀α.α → α)(f [int] 1, f [bool] ′b′)

Full type inference Explicitely typed (Good) (undecidable type inference) Fully annotated terms are (too) verbose

⇒ Need for partial type inference

slide-4
SLIDE 4

ML F features

4(1)/??

Conservative extension of both ML and System F

◮ ML programs need no annotations (type inference) ◮ F terms need fewer annotations

type abstraction and applications are inferred

◮ Annotations are only required on λ-abstractions that are used

polymorphically used

slide-5
SLIDE 5

ML F features

4(2)/??

Conservative extension of both ML and System F

◮ ML programs need no annotations (type inference) ◮ F terms need fewer annotations

type abstraction and applications are inferred

◮ Annotations are only required on λ-abstractions that are used

polymorphically used Principal types (taking user-provided annotations into account) Robust to small program transformations e.g. if E[a1 a2] is typable so is E[apply a1 a2]

(where apply is λf.λx.f x)

slide-6
SLIDE 6

Example: type of choose id

5(1)/??

Term Type

id = λx.x ∀α. α → α (τid) choose = λx.λy.if b then x else y ∀γ. γ → γ → γ

slide-7
SLIDE 7

Example: type of choose id

5(2)/??

In System F, two different typings for choose id:

choose [∀α · α → α] id : τid → τid F1 Λα · choose [α → α] (id α) : ∀α · (α → α) → (α → α) F2

slide-8
SLIDE 8

Example: type of choose id

5(3)/??

In System F, two different typings for choose id:

choose [∀α · α → α] id : τid → τid F1 Λα · choose [α → α] (id α) : ∀α · (α → α) → (α → α) F2

In ML F (note the absence of type annotations):

choose id : ∀ (β = τid) β → β τ1 : ∀ (α) ∀ (β = α → α) β → β τ2

But

τ = ∀ (β ≥ τid) β → β

is another, principal, typing:

τ ⊑        ∀ (β ≥ int → int) β → β

(i.e. (int → int) → (int → int))

∀ (β = ∀ (η = τid) η → η) β → β

(i.e. (τid → τid) → (τid → τid))

τ1, τ2

slide-9
SLIDE 9

Syntactic presentation

6(1)/??

A lot of administrative rules

◮ Hides the underlying principles ◮ Heavy proofs ◮ Makes extensions difficult

Is the instance relation the best within the framework? Expensive unification (and hence type inference) algorithms.

Would it scale up to large or automatically generated programs?

slide-10
SLIDE 10

Contributions

7(1)/??

Graphs are used instead of trees to represent types.

Graphs had already been proposed as a simpler representation, but were not formalized

◮ Simpler presentation, strongly related to first-order types ◮ Proofs are shorter and simpler ◮ Unification has good complexity

slide-11
SLIDE 11

Representing first and second-order types

slide-12
SLIDE 12

Representing first-order types

9(1)/??

(α → β) → (α → β) as: a tree → → α β → α β

slide-13
SLIDE 13

Representing first-order types

9(2)/??

(α → β) → (α → β) as: a tree a dag → → α → β

All occurrences of a variable are shared.

slide-14
SLIDE 14

Representing first-order types

9(3)/??

(α → β) → (α → β) as: a tree an anonymous dag → → ⊥ → ⊥

Variables can be α-converted and do not need to be named

slide-15
SLIDE 15

Representing first-order types

9(4)/??

(α → β) → (α → β) as: a tree an anonymous dag with sharing → → ⊥ ⊥

Non-variable nodes may be also shared

slide-16
SLIDE 16

Representing terms with binders

10(1)/??

Binders are represented with explicit ∀ nodes

int → (∀αβ.(α → β) → (α → β))

→ int ∀ ∀ → → ⊥ → ⊥

Problem: commuting or instantiating binders change the structure of the type

slide-17
SLIDE 17

Representing terms with binders

10(2)/??

With bindings edges, between a variable and the node where the variable is introduced.

int → (∀αβ.(α → β) → (α → β))

→ int → → ⊥ → ⊥

Commutation of binders comes for free!

slide-18
SLIDE 18

ML F types, graphically

slide-19
SLIDE 19

ML F graphic types

12(1)/??

∀ (α = ∀ (β ≥ ⊥) ∀ (η = ∀ (δ ≥ ⊥) β → δ) ∀ (ǫ ≥ ⊥) η → ǫ) α → α

As a graphic type:

→ → → ⊥ ⊥ ⊥

slide-20
SLIDE 20

ML F graphic types

12(2)/??

α : β : ⊥ η : δ : ⊥ β → δ ǫ : ⊥ η → ǫ α → α

As a graphic type:

→ → α → η ⊥ β ⊥ δ ⊥ ǫ

A first-order term graph...

slide-21
SLIDE 21

ML F graphic types

12(3)/??

∀ α = ∀ β ≥ ⊥ ∀ η = ∀ δ ≥ ⊥ β → δ ∀ ǫ ≥ ⊥ η → ǫ α → α

As a graphic type:

→ → α → η ⊥ β ⊥ δ ⊥ ǫ

...plus a binding tree...

slide-22
SLIDE 22

ML F graphic types

12(4)/??

∀ (α = ∀ (β ≥ ⊥) ∀ (η = ∀ (δ ≥ ⊥) β → δ) ∀ (ǫ ≥ ⊥) η → ǫ) α → α

As a graphic type:

→ → → ⊥ ⊥ ⊥

...superposed

slide-23
SLIDE 23

Well-formedness of graphic types

13(1)/??

→ ǫ → α → β ⊥ η ⊥ δ

Syntactically:

◮ ∀ (α ≥ ∀ (δ) β → β ) ∀ (β ≥ ∀ (η) η → δ ) α → α ◮ There is a mutual dependency between α and β ⇒ Not a ML

F type

slide-24
SLIDE 24

Well-formedness of graphic types

13(2)/??

→ ǫ → α → β ⊥ η ⊥ δ

Graphically:

◮ The binder of a node n must dominate n in all the mixed

paths between n and the root ǫ

◮ There is a path between δ and ǫ which does not contain α ⇒ This graph is not a type

slide-25
SLIDE 25

Instance between graphic types

slide-26
SLIDE 26

Instance

15(1)/??

Only four different transformations on graphic types:

      

Grafting Merging

  • change the structure of the type

Raising Weakening

  • change the binding tree of the type

Plus some permissions on nodes governing the set of transformations that can be applied to a node

slide-27
SLIDE 27

Flags and permissions

16(1)/??

◮ Transformations whose inverse can be unsound: allowed on

flexible nodes

◮ Transformations whose inverse is sound, but that cannot

be made implicit while retaining type inference: allowed on rigid nodes:

→ → → ⊥ → → ⊥ → → ⊥ → ⊥

Binding path Permissions

≥∗

Flexible

(≥|=)∗=

Rigid Others Locked

slide-28
SLIDE 28

Grafting

17(1)/??

⊥ ⊑G τ ◮ Similar to the ML instance rule + generalization ∀ α.τ ∀ β.τ [α/τ ′] ◮ Replaces a variable node by a type ◮ Irreversible transformation (the shape of the type changes),

the node must be flexible

slide-29
SLIDE 29

Merging

18(1)/??

       ⋄ τ ⋄ τ ⊑M ⋄ τ ◮ Partly similar to the ML instance ∀αβ. α → β ∀α. α → α ◮ Merges together two identical subgraphs bound on the same

node with the same flag

◮ The nodes must be flexible or rigid

slide-30
SLIDE 30

Raising

19(1)/??

⋄ ⊑R ⋄ ◮ Scope extrusion (τ → (∀α. τ ′) ∀α. τ → τ ′ , α not free in τ) ◮ Used to prove that the type ∀ (β ≥ ∀ (α) α → α) β → β of choose id can be instantiated into ∀ (α) ∀ (β ≥ α → α) β → β ◮ The node must be flexible or rigid

slide-31
SLIDE 31

Weakening

20(1)/??

n ≥ ⊑W n = ◮ Forbids some (irreversible) transformations under a node ◮ Used to require some polymorphism

slide-32
SLIDE 32

Full example of instance

21(1)/??

→ → → ⊥ → ⊥ ⊥

slide-33
SLIDE 33

Full example of instance

21(2)/??

→ → → ⊥ → ⊥ ⊥ → → ⊥ → ⊥ ⊥

Grafting

slide-34
SLIDE 34

Full example of instance

21(3)/??

→ → → ⊥ → ⊥ → → ⊥ → ⊥ ⊥

slide-35
SLIDE 35

Full example of instance

21(4)/??

→ → → ⊥ → ⊥ → → ⊥ → ⊥ ⊥

Raising

slide-36
SLIDE 36

Full example of instance

21(5)/??

→ → → ⊥ → ⊥ → → ⊥ → ⊥ ⊥

slide-37
SLIDE 37

Full example of instance

21(6)/??

→ → → ⊥ → ⊥ → → ⊥ → ⊥ ⊥

Raising

slide-38
SLIDE 38

Full example of instance

21(7)/??

→ → → ⊥ → ⊥ → → ⊥ → ⊥ ⊥

slide-39
SLIDE 39

Full example of instance

21(8)/??

→ → → ⊥ → ⊥ → → ⊥ → ⊥ ⊥

Weakening

slide-40
SLIDE 40

Full example of instance

21(9)/??

→ → → ⊥ → ⊥ → → ⊥ → ⊥ ⊥

slide-41
SLIDE 41

Full example of instance

21(10)/??

→ → → ⊥ → ⊥ → → ⊥ → ⊥ ⊥

Merging

slide-42
SLIDE 42

Full example of instance

21(11)/??

→ → → ⊥ → ⊥ → → ⊥ → ⊥

slide-43
SLIDE 43

Full example of instance

21(12)/??

→ → → ⊥ → ⊥ → → ⊥ → ⊥

Merging

slide-44
SLIDE 44

Full example of instance

21(13)/??

→ → → ⊥ → ⊥

slide-45
SLIDE 45

Instance properties

22(1)/??

Definition: The instance relation ⊑ is (⊑G ∪ ⊑M ∪ ⊑R ∪ ⊑W)∗ Commutation: ⊑ is equal to ⊑G ; ⊑R ; ⊑MW

(⊑MW is (⊑M ∪ ⊑W )∗)

Drastically simplifies proofs and reasonings on instance derivations

slide-46
SLIDE 46

Unification

slide-47
SLIDE 47

Unification

24(1)/??

Unification problem: Given two types τ1 and τ2, find τu such that τ1 ⊑ τu and τ2 ⊑ τu

slide-48
SLIDE 48

Unification

24(2)/??

The unification algorithm proceeds in three steps:

1: Computes the structure of τu, by performing first-order unification on the structure of τ1 and τ2. Cost O(n) (or O(nα(n)), depending on the algorithm).

slide-49
SLIDE 49

Unification

24(3)/??

The unification algorithm proceeds in three steps:

1: Computes the structure of τu, by performing first-order unification on the structure of τ1 and τ2. 2: Computes the binding tree of τu. If the nodes n1, ..., nk of τ1 and τ2 are merged into n in τu:

◮ The binding edges of n1, ..., nk are raised until they are all

bound at the same level.

◮ The flag for n is the least permissive flag on n1, . . . , nk.

Cost O(n): a top down visit. Quite involved step. Uses an amortized O(1) algorithm for computing least-common ancestors.

slide-50
SLIDE 50

Unification

24(4)/??

The unification algorithm proceeds in three steps:

1: Computes the structure of τu, by performing first-order unification on the structure of τ1 and τ2. 2: Computes the binding tree of τu. 3: Checks the permissions for the merging operations performed in step 1. Cost O(n), slightly involved visit of τ1, τ2 and τu.

slide-51
SLIDE 51

Unification algorithm

25(1)/??

◮ Sound: τu is always an instance of τ1 and τ2 ◮ Complete: ⊲ always returns an unifier if one exists ⊲ the unifier returned is principal (i.e. more general for ⊑)

than any other unifier. Thus it computes all unifiers

◮ Good complexity: linear in max(|τ1|, |τ2|)

Extension to linear in min(|τ1|, |τ2|) in practice

slide-52
SLIDE 52

Conclusion

26(1)/??

◮ Simpler relations and proofs ◮ Presentation more semantic, thanks to permissions.

⊲ New (relaxed) instance relation. ⊲ Not easily transposable on syntactic types

◮ Good complexity for unification

slide-53
SLIDE 53

Conclusion

26(2)/??

◮ Simpler relations and proofs ◮ Presentation more semantic, thanks to permissions.

⊲ New (relaxed) instance relation. ⊲ Not easily transposable on syntactic types

◮ Good complexity for unification

Future works

◮ Revisit type inference using graphs ◮ Recursive types ◮ . . .