Type Inference, Principal Typings, and Let-Polymorphism for - - PowerPoint PPT Presentation

type inference principal typings and let polymorphism for
SMART_READER_LITE
LIVE PREVIEW

Type Inference, Principal Typings, and Let-Polymorphism for - - PowerPoint PPT Presentation

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules Henning Makholm and Joe Wells Technical University of Denmark and Heriot-Watt University ICFP05 Tallinn, Estonia September 27, 2005 U seful Work


slide-1
SLIDE 1

Useful Logics, Types, Rewriting, and their Automation

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules

Henning Makholm and Joe Wells Technical University of Denmark and Heriot-Watt University

ICFP’05 – Tallinn, Estonia – September 27, 2005 Work supported by EU/IST/FET grant 2001-33477 (DART)

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.1/17

slide-2
SLIDE 2

Useful Logics, Types, Rewriting, and their Automation

What is this all about?

The m-calculus: Variables:

x ::= x | y | z | · · ·

Field labels:

ℓ ::= f | g | h | · · ·

Terms:

M, N ::= x | M.ℓ | M ⊕ N | { [γ1; . . . ; γk] }

Module items: γ ::= local x = M

| export ℓ ⊲ x = M | import ℓ ⊲ x

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.2/17

slide-3
SLIDE 3

Useful Logics, Types, Rewriting, and their Automation

What is this all about?

The m-calculus: Variables:

x ::= x | y | z | · · ·

Field labels:

ℓ ::= f | g | h | · · ·

Terms:

M, N ::= x | M.ℓ | M ⊕ N | { [γ1; . . . ; γk] }

Module items: γ ::= local x = M

| export ℓ ⊲ x = M | import ℓ ⊲ x

We recognize variables, field selection, some kind of fancy sum operator, but what on earth is “{

[γ1, . . . , γk] }”?

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.2/17

slide-4
SLIDE 4

Useful Logics, Types, Rewriting, and their Automation

What is this all about?

The m-calculus: Variables:

x ::= x | y | z | · · ·

Field labels:

ℓ ::= f | g | h | · · ·

Terms:

M, N ::= x | M.ℓ | M ⊕ N | { [γ1; . . . ; γk] }

Module items: γ ::= local x = M

| export ℓ ⊲ x = M | import ℓ ⊲ x

We recognize variables, field selection, some kind of fancy sum operator, but what on earth is “{

[γ1, . . . , γk] }”?

Is it a record...?

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.2/17

slide-5
SLIDE 5

Useful Logics, Types, Rewriting, and their Automation

What is this all about?

The m-calculus: Variables:

x ::= x | y | z | · · ·

Field labels:

ℓ ::= f | g | h | · · ·

Terms:

M, N ::= x | M.ℓ | M ⊕ N | { [γ1; . . . ; γk] }

Module items: γ ::= local x = M

| export ℓ ⊲ x = M | import ℓ ⊲ x

We recognize variables, field selection, some kind of fancy sum operator, but what on earth is “{

[γ1, . . . , γk] }”?

Is it a record...? Is it a letrec...?

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.2/17

slide-6
SLIDE 6

Useful Logics, Types, Rewriting, and their Automation

What is this all about?

The m-calculus: Variables:

x ::= x | y | z | · · ·

Field labels:

ℓ ::= f | g | h | · · ·

Terms:

M, N ::= x | M.ℓ | M ⊕ N | { [γ1; . . . ; γk] }

Module items: γ ::= local x = M

| export ℓ ⊲ x = M | import ℓ ⊲ x

We recognize variables, field selection, some kind of fancy sum operator, but what on earth is “{

[γ1, . . . , γk] }”?

Is it a record...? Is it a letrec...? Is it a closure...?

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.2/17

slide-7
SLIDE 7

Useful Logics, Types, Rewriting, and their Automation

What is this all about?

The m-calculus: Variables:

x ::= x | y | z | · · ·

Field labels:

ℓ ::= f | g | h | · · ·

Terms:

M, N ::= x | M.ℓ | M ⊕ N | { [γ1; . . . ; γk] }

Module items: γ ::= local x = M

| export ℓ ⊲ x = M | import ℓ ⊲ x

We recognize variables, field selection, some kind of fancy sum operator, but what on earth is “{

[γ1, . . . , γk] }”?

Is it a record...? Is it a letrec...? Is it a closure...? It is ... SUPERMAN! SUPERMAN!

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.2/17

slide-8
SLIDE 8

Useful Logics, Types, Rewriting, and their Automation

What is this all about?

The m-calculus: Variables:

x ::= x | y | z | · · ·

Field labels:

ℓ ::= f | g | h | · · ·

Terms:

M, N ::= x | M.ℓ | M ⊕ N | { [γ1; . . . ; γk] }

Module items: γ ::= local x = M

| export ℓ ⊲ x = M | import ℓ ⊲ x

We recognize variables, field selection, some kind of fancy sum operator, but what on earth is “{

[γ1, . . . , γk] }”?

Is it a record...? Is it a letrec...? Is it a closure...? It is a mixin module!

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.2/17

slide-9
SLIDE 9

Useful Logics, Types, Rewriting, and their Automation

Anatomy of a mixin module

The m-calculus: Variables:

x ::= x | y | z | · · ·

Field labels:

ℓ ::= f | g | h | · · ·

Terms:

M, N ::= x | M.ℓ | M ⊕ N | { [γ1; . . . ; γk] }

Module items: γ ::= local x = M

| export ℓ ⊲ x = M | import ℓ ⊲ x

A mixin module consists of three kinds of items

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.3/17

slide-10
SLIDE 10

Useful Logics, Types, Rewriting, and their Automation

Anatomy of a mixin module

The m-calculus: Variables:

x ::= x | y | z | · · ·

Field labels:

ℓ ::= f | g | h | · · ·

Terms:

M, N ::= x | M.ℓ | M ⊕ N | { [γ1; . . . ; γk] }

Module items: γ ::= local x = M

| export ℓ ⊲ x = M | import ℓ ⊲ x

A mixin module consists of three kinds of items: A local definition binds an internal name x to an expression M

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.3/17

slide-11
SLIDE 11

Useful Logics, Types, Rewriting, and their Automation

Anatomy of a mixin module

The m-calculus: Variables:

x ::= x | y | z | · · ·

Field labels:

ℓ ::= f | g | h | · · ·

Terms:

M, N ::= x | M.ℓ | M ⊕ N | { [γ1; . . . ; γk] }

Module items: γ ::= local x = M

| export ℓ ⊲ x = M | import ℓ ⊲ x

A mixin module consists of three kinds of items: A local definition binds an internal name x to an expression M An export binds an external label ℓ to an expression M (while also providing a local definition for x)

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.3/17

slide-12
SLIDE 12

Useful Logics, Types, Rewriting, and their Automation

Anatomy of a mixin module

The m-calculus: Variables:

x ::= x | y | z | · · ·

Field labels:

ℓ ::= f | g | h | · · ·

Terms:

M, N ::= x | M.ℓ | M ⊕ N | { [γ1; . . . ; γk] }

Module items: γ ::= local x = M

| export ℓ ⊲ x = M | import ℓ ⊲ x

A mixin module consists of three kinds of items: A local definition binds an internal name x to an expression M An export binds an external label ℓ to an expression M (while also providing a local definition for x) An import binds an internal name x to a linking promise represented by the external label ℓ.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.3/17

slide-13
SLIDE 13

Useful Logics, Types, Rewriting, and their Automation

An example

{ [ local d = λx.x − 1; export odd ⊲ o = λx.if x = 0 then false else not(e(d(x))); import even ⊲ e; ] } ⊕ { [ export even ⊲ v = λx.if x = 0 then true else not(p (x − 1)); import odd ⊲ p; ] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.4/17

slide-14
SLIDE 14

Useful Logics, Types, Rewriting, and their Automation

An example

{ [ local d = λx

  • .x
  • − 1;

export odd ⊲ o = λx

  • .if x
  • = 0 then false else not(e(d(x
  • )));

import even ⊲ e; ] } ⊕ { [ export even ⊲ v = λx

  • .if x
  • = 0 then true else not(p (x
  • − 1));

import odd ⊲ p; ] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.4/17

slide-15
SLIDE 15

Useful Logics, Types, Rewriting, and their Automation

An example

{ [ local d

  • = λx
  • .x
  • − 1;

export odd ⊲ o

  • = λx
  • .if x
  • = 0 then false else not(e(d
  • (x
  • )));

import even ⊲ e; ] } ⊕ { [ export even ⊲ v

  • = λx
  • .if x
  • = 0 then true else not(p (x
  • − 1));

import odd ⊲ p; ] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.4/17

slide-16
SLIDE 16

Useful Logics, Types, Rewriting, and their Automation

An example

{ [ local d

  • = λx
  • .x
  • − 1;

export odd ⊲ o

  • = λx
  • .if x
  • = 0 then false else not(e
  • (d
  • (x
  • )));

import even ⊲ e

  • ; ]

} ⊕ { [ export even ⊲ v

  • = λx
  • .if x
  • = 0 then true else not(p
  • (x
  • − 1));

import odd ⊲ p

  • ; ]

}

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.4/17

slide-17
SLIDE 17

Useful Logics, Types, Rewriting, and their Automation

An example

{ [ local d

  • = λx
  • .x
  • − 1;

export odd ⊲ o

  • = λx
  • .if x
  • = 0 then false else not(e
  • (d
  • (x
  • )));

import even ⊲ e

  • ; ]

} ⊕ { [ export even ⊲ v

  • = λx
  • .if x
  • = 0 then true else not(p
  • (x
  • − 1));

import odd ⊲ p

  • ; ]

}

Linking uses one operand’s exports to bind the other one’s imports

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.4/17

slide-18
SLIDE 18

Useful Logics, Types, Rewriting, and their Automation

An example

{ [ local d

  • = λx
  • .x
  • − 1;

export odd ⊲ o

  • = λx
  • .if x
  • = 0 then false else not(e
  • (d
  • (x
  • )));

import even ⊲ e

  • ; ]

} ⊕ { [ export even ⊲ v

  • = λx
  • .if x
  • = 0 then true else not(p
  • (x
  • − 1));

import odd ⊲ p

  • ; ]

}

Linking uses one operand’s exports to bind the other one’s imports ... and vice versa: Linking is symmetric

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.4/17

slide-19
SLIDE 19

Useful Logics, Types, Rewriting, and their Automation

An example

{ [ local d

  • = λx
  • .x
  • − 1;

export odd ⊲ o

  • = λx
  • .if x
  • = 0 then false else not(e
  • (d
  • (x
  • )));

import even ⊲ e

  • ; ]

} ⊕ { [ export even ⊲ v

  • = λx
  • .if x
  • = 0 then true else not(p
  • (x
  • − 1));

import odd ⊲ p

  • ; ]

}

Linking uses one operand’s exports to bind the other one’s imports ... and vice versa: Linking is symmetric

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.4/17

slide-20
SLIDE 20

Useful Logics, Types, Rewriting, and their Automation

An example

{ [ local d

  • = λx
  • .x
  • − 1;

export odd ⊲ o

  • = λx
  • .if x
  • = 0 then false else not(v
  • (d
  • (x
  • )));

import even ⊲ v

  • ; ]

} ⊕ { [ export even ⊲ v

  • = λx
  • .if x
  • = 0 then true else not(o
  • (x
  • − 1));

import odd ⊲ o

  • ; ]

}

Linking uses one operand’s exports to bind the other one’s imports ... and vice versa: Linking is symmetric

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.4/17

slide-21
SLIDE 21

Useful Logics, Types, Rewriting, and their Automation

An example

{ [ local d

  • = λx
  • .x
  • − 1;

export odd ⊲ o

  • = λx
  • .if x
  • = 0 then false else not(v
  • (d
  • (x
  • )));

import even ⊲ v

  • ;

export even ⊲ v

  • = λx
  • .if x
  • = 0 then true else not(o
  • (x
  • − 1));

import odd ⊲ o

  • ; ]

}

Linking uses one operand’s exports to bind the other one’s imports ... and vice versa: Linking is symmetric

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.4/17

slide-22
SLIDE 22

Useful Logics, Types, Rewriting, and their Automation

An example

{ [ local d

  • = λx
  • .x
  • − 1;

export odd ⊲ o

  • = λx
  • .if x
  • = 0 then false else not(v
  • (d
  • (x
  • )));

import even ⊲ v

  • ;

export even ⊲ v

  • = λx
  • .if x
  • = 0 then true else not(o
  • (x
  • − 1));

import odd ⊲ o

  • ; ]

}

Linking uses one operand’s exports to bind the other one’s imports ... and vice versa: Linking is symmetric Recursion may arise “spontaneously”

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.4/17

slide-23
SLIDE 23

Useful Logics, Types, Rewriting, and their Automation

An example

{ [ local d

  • = λx
  • .x
  • − 1;

export odd ⊲ o

  • = λx
  • .if x
  • = 0 then false else not(v
  • (d
  • (x
  • )));

import even ⊲ v

  • ;

export even ⊲ v

  • = λx
  • .if x
  • = 0 then true else not(o
  • (x
  • − 1));

import odd ⊲ o

  • ; ]

}

Linking uses one operand’s exports to bind the other one’s imports ... and vice versa: Linking is symmetric Recursion may arise “spontaneously” Well-known examples of symmetric linking include C compilation units, but not ML modules

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.4/17

slide-24
SLIDE 24

Useful Logics, Types, Rewriting, and their Automation

Other module operations

Field extraction

{ [ local d = λx.x − 1; export odd ⊲ o = λx.if x = 0 then false else not(v(d(x))); export even ⊲ v = λx.if x = 0 then true else not(o(x − 1)); ] }.even

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.5/17

slide-25
SLIDE 25

Useful Logics, Types, Rewriting, and their Automation

Other module operations

Field extraction

{ [ local d = λx.x − 1; export odd ⊲ o = λx.if x = 0 then false else not(v(d(x))); export even ⊲ v = λx.if x = 0 then true else not(o(x − 1)); ] }.even(42)

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.5/17

slide-26
SLIDE 26

Useful Logics, Types, Rewriting, and their Automation

Other module operations

Field extraction

{ [ local d = λx.x − 1; export odd ⊲ o = λx.if x = 0 then false else not(v(d(x))); export even ⊲ v = λx.if x = 0 then true else not(o(x − 1)); ] }.even(42) ֒ →∗ true

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.5/17

slide-27
SLIDE 27

Useful Logics, Types, Rewriting, and their Automation

Other module operations

Field extraction

{ [ local d = λx.x − 1; export odd ⊲ o = λx.if x = 0 then false else not(v(d(x))); export even ⊲ v = λx.if x = 0 then true else not(o(x − 1)); ] }.even(42) ֒ →∗ true

Field hiding

{ [ export f ⊲ x = 5; export g ⊲ y = x + 17; ] } \ \ {f}

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.5/17

slide-28
SLIDE 28

Useful Logics, Types, Rewriting, and their Automation

Other module operations

Field extraction

{ [ local d = λx.x − 1; export odd ⊲ o = λx.if x = 0 then false else not(v(d(x))); export even ⊲ v = λx.if x = 0 then true else not(o(x − 1)); ] }.even(42) ֒ →∗ true

Field hiding

{ [ export f ⊲ x

  • = 5;

export g ⊲ y = x

  • + 17;

] } \ \ {f}

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.5/17

slide-29
SLIDE 29

Useful Logics, Types, Rewriting, and their Automation

Other module operations

Field extraction

{ [ local d = λx.x − 1; export odd ⊲ o = λx.if x = 0 then false else not(v(d(x))); export even ⊲ v = λx.if x = 0 then true else not(o(x − 1)); ] }.even(42) ֒ →∗ true

Field hiding makes exports into locals

{ [ export f ⊲ x = 5; export g ⊲ y = x + 17; ] } \ \ {f} ֒ → { [ local x = 5; export g ⊲ y = x + 17; ] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.5/17

slide-30
SLIDE 30

Useful Logics, Types, Rewriting, and their Automation

Modules as first-class citizens

Modules can be bound to variables and can be imported/exported from other modules

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.6/17

slide-31
SLIDE 31

Useful Logics, Types, Rewriting, and their Automation

Modules as first-class citizens

Modules can be bound to variables and can be imported/exported from other modules Expressions in module constructions can mention variables that are bound in an enclosing scope

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.6/17

slide-32
SLIDE 32

Useful Logics, Types, Rewriting, and their Automation

Modules as first-class citizens

Modules can be bound to variables and can be imported/exported from other modules Expressions in module constructions can mention variables that are bound in an enclosing scope

{ [local x = 5; export f ⊲ _ = { [export g ⊲ _ = x + 7] }] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.6/17

slide-33
SLIDE 33

Useful Logics, Types, Rewriting, and their Automation

Modules as first-class citizens

Modules can be bound to variables and can be imported/exported from other modules Expressions in module constructions can mention variables that are bound in an enclosing scope

{ [local x = 5; export f ⊲ _ = { [export g ⊲ _ = x + 7] }] }

Constructs from lambda calculus can be simulated directly:

(λx.M) = ⇒ { [import in ⊲ x; export out ⊲ _ = M] } M N = ⇒ (M ⊕ { [export in ⊲ _ = N] }).out

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.6/17

slide-34
SLIDE 34

Useful Logics, Types, Rewriting, and their Automation

Modules as first-class citizens

Modules can be bound to variables and can be imported/exported from other modules Expressions in module constructions can mention variables that are bound in an enclosing scope

{ [local x = 5; export f ⊲ _ = { [export g ⊲ _ = x + 7] }] }

Constructs from lambda calculus can be simulated directly:

(λx.M) = ⇒ { [import in ⊲ x; export out ⊲ _ = M] } M N = ⇒ (M ⊕ { [export in ⊲ _ = N] }).out

Records are a restricted case of modules:

{ [f = 42; g = "foo"] } = ⇒ { [export f ⊲ _ = 42; export g ⊲ _ = "foo"] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.6/17

slide-35
SLIDE 35

Useful Logics, Types, Rewriting, and their Automation

Modules as first-class citizens

Modules can be bound to variables and can be imported/exported from other modules Expressions in module constructions can mention variables that are bound in an enclosing scope

{ [local x = 5; export f ⊲ _ = { [export g ⊲ _ = x + 7] }] }

Constructs from lambda calculus can be simulated directly:

(λx.M) = ⇒ { [import in ⊲ x; export out ⊲ _ = M] } M N = ⇒ (M ⊕ { [export in ⊲ _ = N] }).out

Records are a restricted case of modules:

{ [f = 42; g = "foo"] } = ⇒ { [export f ⊲ _ = 42; export g ⊲ _ = "foo"] }

In this case, linking becomes record concatenation.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.6/17

slide-36
SLIDE 36

Useful Logics, Types, Rewriting, and their Automation

The Martini type system: design goals

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.7/17

slide-37
SLIDE 37

Useful Logics, Types, Rewriting, and their Automation

The Martini type system: design goals

Type safety by progress & subject reduction

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.7/17

slide-38
SLIDE 38

Useful Logics, Types, Rewriting, and their Automation

The Martini type system: design goals

Type safety by progress & subject reduction No extraction of a field that is not exported No extraction from a module with unsatisfied imports No linking of two modules that export the same name No hiding of names that the module imports

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.7/17

slide-39
SLIDE 39

Useful Logics, Types, Rewriting, and their Automation

The Martini type system: design goals

Type safety by progress & subject reduction No extraction of a field that is not exported No extraction from a module with unsatisfied imports No linking of two modules that export the same name No hiding of names that the module imports Tractable automatic type inference. (First for mixin modules!) Not trivial: The most straightforward typing rules for linking lead to an NP-complete typing problem

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.7/17

slide-40
SLIDE 40

Useful Logics, Types, Rewriting, and their Automation

The Martini type system: design goals

Type safety by progress & subject reduction No extraction of a field that is not exported No extraction from a module with unsatisfied imports No linking of two modules that export the same name No hiding of names that the module imports Tractable automatic type inference. (First for mixin modules!) Not trivial: The most straightforward typing rules for linking lead to an NP-complete typing problem Principal typings for compositional type inference

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.7/17

slide-41
SLIDE 41

Useful Logics, Types, Rewriting, and their Automation

The Martini type system: design goals

Type safety by progress & subject reduction No extraction of a field that is not exported No extraction from a module with unsatisfied imports No linking of two modules that export the same name No hiding of names that the module imports Tractable automatic type inference. (First for mixin modules!) Not trivial: The most straightforward typing rules for linking lead to an NP-complete typing problem Principal typings for compositional type inference Principal types: Given M and Γ, find “best” τ s.t. Γ ⊢ M : τ Principal typings: Given M, find “best” (Γ, τ) s.t. Γ ⊢ M : τ ML types have principal types, but not principal typings To emphasize principal typings, we write M : Γ ⊢ τ

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.7/17

slide-42
SLIDE 42

Useful Logics, Types, Rewriting, and their Automation

Martini types explained by example

Let M = {

[ local d = λx.x − 1; export odd ⊲ _ = λx.if x = 0 then false else not(e(d(x))); import even ⊲ e; ] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.8/17

slide-43
SLIDE 43

Useful Logics, Types, Rewriting, and their Automation

Martini types explained by example

Let M = {

[ local d = λx.x − 1; export odd ⊲ _ = λx.if x = 0 then false else not(e(d(x))); import even ⊲ e; ] }

Some valid types for M are:

{ [ even:int→bool, odd:int→bool

  • Types of all imports and exports

/ {even}

Imports

⇒{odd}

Exports

] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.8/17

slide-44
SLIDE 44

Useful Logics, Types, Rewriting, and their Automation

Martini types explained by example

Let M = {

[ local d = λx.x − 1; export odd ⊲ _ = λx.if x = 0 then false else not(e(d(x))); import even ⊲ e; ] }

Some valid types for M are:

{ [ even:int→bool, odd:int→bool

  • Types of all imports and exports

/ {even}

Imports

⇒{odd}

Exports

] } { [ even:int→bool, odd:int→bool, k:string

  • More types than necessary are OK

/ {even} ⇒{odd} ] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.8/17

slide-45
SLIDE 45

Useful Logics, Types, Rewriting, and their Automation

Martini types explained by example

Let M = {

[ local d = λx.x − 1; export odd ⊲ _ = λx.if x = 0 then false else not(e(d(x))); import even ⊲ e; ] }

Some valid types for M are:

{ [ even:int→bool, odd:int→bool

  • Types of all imports and exports

/ {even}

Imports

⇒{odd}

Exports

] } { [ even:int→bool, odd:int→bool, k:string

  • More types than necessary are OK

/ {even} ⇒{odd} ] } { [ even:int→bool, odd:int→bool, k:string / {even, k}

  • Overapproximation here OK

⇒{odd} ] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.8/17

slide-46
SLIDE 46

Useful Logics, Types, Rewriting, and their Automation

Martini types explained by example

Let M = {

[ local d = λx.x − 1; export odd ⊲ _ = λx.if x = 0 then false else not(e(d(x))); import even ⊲ e; ] }

Some valid types for M are:

{ [ even:int→bool, odd:int→bool

  • Types of all imports and exports

/ {even}

Imports

⇒{odd}

Exports

] } { [ even:int→bool, odd:int→bool, k:string

  • More types than necessary are OK

/ {even} ⇒{odd} ] } { [ even:int→bool, odd:int→bool, k:string / {even, k}

  • Overapproximation here OK

⇒{odd} ] } { [ even:int→bool, odd:int→bool, r0

  • Any row defining even and odd

/ {even, k} ⇒{odd} ] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.8/17

slide-47
SLIDE 47

Useful Logics, Types, Rewriting, and their Automation

Martini types explained by example

Let M = {

[ local d = λx.x − 1; export odd ⊲ _ = λx.if x = 0 then false else not(e(d(x))); import even ⊲ e; ] }

Some valid types for M are:

{ [ even:int→bool, odd:int→bool

  • Types of all imports and exports

/ {even}

Imports

⇒{odd}

Exports

] } { [ even:int→bool, odd:int→bool, k:string

  • More types than necessary are OK

/ {even} ⇒{odd} ] } { [ even:int→bool, odd:int→bool, k:string / {even, k}

  • Overapproximation here OK

⇒{odd} ] } { [ even:int→bool, odd:int→bool, r0

  • Any row defining even and odd

/ {even, k} ⇒{odd} ] } { [ even:int→bool, odd:int→bool, r0 / q0 ⇒{odd} ] }

whenever the constraint set {q0 ⊇ {even}} is satisfied

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.8/17

slide-48
SLIDE 48

Useful Logics, Types, Rewriting, and their Automation

Another example (principal) typing

Let N = λx.λy.(x ⊕ y).f + 42

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.9/17

slide-49
SLIDE 49

Useful Logics, Types, Rewriting, and their Automation

Another example (principal) typing

Let N = λx.λy.(x ⊕ y).f + 42 Giving this a principal typing is not trivial: The input modules x and y may need to exchange values before one of them can calculate a value for f. We don’t even know which of x and y will export f. Exactly one of them must.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.9/17

slide-50
SLIDE 50

Useful Logics, Types, Rewriting, and their Automation

Another example (principal) typing

Let N = λx.λy.(x ⊕ y).f + 42 Giving this a principal typing is not trivial: The input modules x and y may need to exchange values before one of them can calculate a value for f. We don’t even know which of x and y will export f. Exactly one of them must. Row overapproximation to the rescue!

N has type { [ f:int, r / q1 ⇒s1 ] } → { [ f:int, r / q2 ⇒s2 ] } → int

with constraint set {s = s1 ⊎ s2, {f} ⊆ s, s2 ⊇ q1, s1 ⊇ q2}

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.9/17

slide-51
SLIDE 51

Useful Logics, Types, Rewriting, and their Automation

Another example (principal) typing

Let N = λx.λy.(x ⊕ y).f + 42 Giving this a principal typing is not trivial: The input modules x and y may need to exchange values before one of them can calculate a value for f. We don’t even know which of x and y will export f. Exactly one of them must. Row overapproximation to the rescue!

N has type { [ f:int, r / q1 ⇒s1 ] } → { [ f:int, r / q2 ⇒s2 ] } → int

with constraint set {s = s1 ⊎ s2, {f} ⊆ s, s2 ⊇ q1, s1 ⊇ q2} Both rows contain “f:int” ... meaning if the module exports f, then it has type int

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.9/17

slide-52
SLIDE 52

Useful Logics, Types, Rewriting, and their Automation

Another example (principal) typing

Let N = λx.λy.(x ⊕ y).f + 42 Giving this a principal typing is not trivial: The input modules x and y may need to exchange values before one of them can calculate a value for f. We don’t even know which of x and y will export f. Exactly one of them must. Row overapproximation to the rescue!

N has type { [ f:int, r / q1 ⇒s1 ] } → { [ f:int, r / q2 ⇒s2 ] } → int

with constraint set {s = s1 ⊎ s2, {f} ⊆ s, s2 ⊇ q1, s1 ⊇ q2} Both rows contain “f:int” ... meaning if the module exports f, then it has type int Constraints ensure that exactly one of s1 and s2 contain f

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.9/17

slide-53
SLIDE 53

Useful Logics, Types, Rewriting, and their Automation

Another example (principal) typing

Let N = λx.λy.(x ⊕ y).f + 42 Giving this a principal typing is not trivial: The input modules x and y may need to exchange values before one of them can calculate a value for f. We don’t even know which of x and y will export f. Exactly one of them must. Row overapproximation to the rescue!

N has type { [ f:int, r / q1 ⇒s1 ] } → { [ f:int, r / q2 ⇒s2 ] } → int

with constraint set {s = s1 ⊎ s2, {f} ⊆ s, s2 ⊇ q1, s1 ⊇ q2} Both rows contain “f:int” ... meaning if the module exports f, then it has type int Constraints ensure that exactly one of s1 and s2 contain f More constraint to ensure that x’s imports are satisfied by

y’s exports, and vice versa

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.9/17

slide-54
SLIDE 54

Useful Logics, Types, Rewriting, and their Automation

Grammar for Martini types

Label sets:

L ::= {ℓ1, . . . , ℓk} (k ≥ 0)

Type rows:

R ::= r0 | r1 | r2 | · · · | ℓ:τ, R | ⋄

Import set expressions: Q ::= q0 | q1 | q2 | · · · | L Export set expressions: S ::= s0 | s1 | s2 | · · · | L | ⊥ Types:

τ ::= ’a | ’b | ’c | . . . | { [ R / Q ⇒S ] }

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.10/17

slide-55
SLIDE 55

Useful Logics, Types, Rewriting, and their Automation

Grammar for Martini types

Label sets:

L ::= {ℓ1, . . . , ℓk} (k ≥ 0)

Type rows:

R ::= r0 | r1 | r2 | · · · | ℓ:τ, R | ⋄

Import set expressions: Q ::= q0 | q1 | q2 | · · · | L Export set expressions: S ::= s0 | s1 | s2 | · · · | L | ⊥ Types:

τ ::= ’a | ’b | ’c | . . . | { [ R / Q ⇒S ] }

Each of the classes R, Q, S, τ has an infinity of variables

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.10/17

slide-56
SLIDE 56

Useful Logics, Types, Rewriting, and their Automation

Grammar for Martini types

Label sets:

L ::= {ℓ1, . . . , ℓk} (k ≥ 0)

Type rows:

R ::= r0 | r1 | r2 | · · · | ℓ:τ, R | ⋄

Import set expressions: Q ::= q0 | q1 | q2 | · · · | L Export set expressions: S ::= s0 | s1 | s2 | · · · | L | ⊥ Types:

τ ::= ’a | ’b | ’c | . . . | { [ R / Q ⇒S ] }

Each of the classes R, Q, S, τ has an infinity of variables The concrete form of Q and S is just a set of field names.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.10/17

slide-57
SLIDE 57

Useful Logics, Types, Rewriting, and their Automation

Grammar for Martini types

Label sets:

L ::= {ℓ1, . . . , ℓk} (k ≥ 0)

Type rows:

R ::= r0 | r1 | r2 | · · · | ℓ:τ, R | ⋄

Import set expressions: Q ::= q0 | q1 | q2 | · · · | L Export set expressions: S ::= s0 | s1 | s2 | · · · | L | ⊥ Types:

τ ::= ’a | ’b | ’c | . . . | { [ R / Q ⇒S ] }

Each of the classes R, Q, S, τ has an infinity of variables The concrete form of Q and S is just a set of field names. An S can also be ⊥; the type {

[ R / Q ⇒⊥ ] } is always void

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.10/17

slide-58
SLIDE 58

Useful Logics, Types, Rewriting, and their Automation

Grammar for Martini types

Label sets:

L ::= {ℓ1, . . . , ℓk} (k ≥ 0)

Type rows:

R ::= r0 | r1 | r2 | · · · | ℓ:τ, R | ⋄

Import set expressions: Q ::= q0 | q1 | q2 | · · · | L Export set expressions: S ::= s0 | s1 | s2 | · · · | L | ⊥ Types:

τ ::= ’a | ’b | ’c | . . . | { [ R / Q ⇒S ] }

Each of the classes R, Q, S, τ has an infinity of variables The concrete form of Q and S is just a set of field names. An S can also be ⊥; the type {

[ R / Q ⇒⊥ ] } is always void

A row R is a series of field–type bindings, possibly ending with a row variable

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.10/17

slide-59
SLIDE 59

Useful Logics, Types, Rewriting, and their Automation

Grammar for Martini types

Label sets:

L ::= {ℓ1, . . . , ℓk} (k ≥ 0)

Type rows:

R ::= r0 | r1 | r2 | · · · | ℓ:τ, R | ⋄

Import set expressions: Q ::= q0 | q1 | q2 | · · · | L Export set expressions: S ::= s0 | s1 | s2 | · · · | L | ⊥ Types:

τ ::= ’a | ’b | ’c | . . . | { [ R / Q ⇒S ] }

Each of the classes R, Q, S, τ has an infinity of variables The concrete form of Q and S is just a set of field names. An S can also be ⊥; the type {

[ R / Q ⇒⊥ ] } is always void

A row R is a series of field–type bindings, possibly ending with a row variable For all ℓ1 = ℓ2 we impose the equation

(ℓ1:τ1, ℓ2:τ2, R) = (ℓ2:τ2, ℓ1:τ1, R)

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.10/17

slide-60
SLIDE 60

Useful Logics, Types, Rewriting, and their Automation

Grammar for Martini types

Label sets:

L ::= {ℓ1, . . . , ℓk} (k ≥ 0)

Type rows:

R ::= r0 | r1 | r2 | · · · | ℓ:τ, R

Import set expressions: Q ::= q0 | q1 | q2 | · · · | L Export set expressions: S ::= s0 | s1 | s2 | · · · | L | ⊥ Types:

τ ::= ’a | ’b | ’c | . . . | { [ R / Q ⇒S ] }

Each of the classes R, Q, S, τ has an infinity of variables The concrete form of Q and S is just a set of field names. An S can also be ⊥; the type {

[ R / Q ⇒⊥ ] } is always void

A row R is a series of field–type bindings, always ending with a row variable For all ℓ1 = ℓ2 we impose the equation

(ℓ1:τ1, ℓ2:τ2, R) = (ℓ2:τ2, ℓ1:τ1, R)

There is no syntax for an empty row – a row variable can always be used instead

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.10/17

slide-61
SLIDE 61

Useful Logics, Types, Rewriting, and their Automation

Constraint language

S constraints:

cS ::= S = S1 ⊎ S2 | S = S1 \ L

Q constraints: cQ ::= Q ⊇ Q1 \ S | Q # L Constraints:

c ::= cS | cQ

Constraint sets: C ::= {c1, . . . , ck}

(k ≥ 0)

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.11/17

slide-62
SLIDE 62

Useful Logics, Types, Rewriting, and their Automation

Constraint language

S constraints:

cS ::= S = S1 ⊎ S2 | S = S1 \ L

Q constraints: cQ ::= Q ⊇ Q1 \ S | Q # L Constraints:

c ::= cS | cQ

Constraint sets: C ::= {c1, . . . , ck}

(k ≥ 0)

Central principle: Constraints speak about label sets only. (In particular, about possible instantiations of set variables). The structure of rows and types is independent of constraints.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.11/17

slide-63
SLIDE 63

Useful Logics, Types, Rewriting, and their Automation

Constraint language

S constraints:

cS ::= S = S1 ⊎ S2 | S = S1 \ L

Q constraints: cQ ::= Q ⊇ Q1 \ S | Q # L Constraints:

c ::= cS | cQ

Constraint sets: C ::= {c1, . . . , ck}

(k ≥ 0)

Central principle: Constraints speak about label sets only. (In particular, about possible instantiations of set variables). The structure of rows and types is independent of constraints. Sets on a constraint RHS come from operand types. The set on the LHS is part of the result type.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.11/17

slide-64
SLIDE 64

Useful Logics, Types, Rewriting, and their Automation

Constraint language

S constraints:

cS ::= S = S1 ⊎ S2 | S = S1 \ L

Q constraints: cQ ::= Q ⊇ Q1 \ S | Q # L Constraints:

c ::= cS | cQ

Constraint sets: C ::= {c1, . . . , ck}

(k ≥ 0)

Central principle: Constraints speak about label sets only. (In particular, about possible instantiations of set variables). The structure of rows and types is independent of constraints. Sets on a constraint RHS come from operand types. The set on the LHS is part of the result type. In S constraints the operands exactly determine the result set. Q constraints allow the result to overapproximate.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.11/17

slide-65
SLIDE 65

Useful Logics, Types, Rewriting, and their Automation

Constraint language

S constraints:

cS ::= S = S1 ⊎ S2 | S = S1 \ L

Q constraints: cQ ::= Q ⊇ Q1 \ S | Q # L Constraints:

c ::= cS | cQ

Constraint sets: C ::= {c1, . . . , ck}

(k ≥ 0)

Central principle: Constraints speak about label sets only. (In particular, about possible instantiations of set variables). The structure of rows and types is independent of constraints. Sets on a constraint RHS come from operand types. The set on the LHS is part of the result type. In S constraints the operands exactly determine the result set. Q constraints allow the result to overapproximate. A constraint with ⊥ on its RHS comes from an expression whose operand can never exist at run-time. Such a constraint is by definition solved.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.11/17

slide-66
SLIDE 66

Useful Logics, Types, Rewriting, and their Automation

Judgements and rules

Typing judgements have the form M : Γ ⊢ τ | C An example rule:

M : Γ ⊢ { [ R / Q1⇒S1] } | C N : Γ ⊢ { [ R / Q2⇒S2] } | C C {Q ⊇ Q1 \ S2, S = S1 ⊎ S2, Q ⊇ Q2 \ S1} M ⊕ N : Γ ⊢ { [ R / Q ⇒S ] } | C

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.12/17

slide-67
SLIDE 67

Useful Logics, Types, Rewriting, and their Automation

Judgements and rules

Typing judgements have the form M : Γ ⊢ τ | C An example rule:

M : Γ ⊢ { [ R / Q1⇒S1] } | C N : Γ ⊢ { [ R / Q2⇒S2] } | C C {Q ⊇ Q1 \ S2, S = S1 ⊎ S2, Q ⊇ Q2 \ S1} M ⊕ N : Γ ⊢ { [ R / Q ⇒S ] } | C

where C1 C2 if all substitutions that solve C1 also solve C2.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.12/17

slide-68
SLIDE 68

Useful Logics, Types, Rewriting, and their Automation

Judgements and rules

Typing judgements have the form M : Γ ⊢ τ | C An example rule:

M : Γ ⊢ { [ R / Q1⇒S1] } | C N : Γ ⊢ { [ R / Q2⇒S2] } | C C {Q ⊇ Q1 \ S2, S = S1 ⊎ S2, Q ⊇ Q2 \ S1} M ⊕ N : Γ ⊢ { [ R / Q ⇒S ] } | C

where C1 C2 if all substitutions that solve C1 also solve C2. The same C is used in the entire derivation.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.12/17

slide-69
SLIDE 69

Useful Logics, Types, Rewriting, and their Automation

Judgements and rules

Typing judgements have the form M : Γ ⊢ τ | C An example rule:

M : Γ ⊢ { [ R / Q1⇒S1] } | C N : Γ ⊢ { [ R / Q2⇒S2] } | C C {Q ⊇ Q1 \ S2, S = S1 ⊎ S2, Q ⊇ Q2 \ S1} M ⊕ N : Γ ⊢ { [ R / Q ⇒S ] } | C

where C1 C2 if all substitutions that solve C1 also solve C2. The same C is used in the entire derivation. The concrete form of Q’s and S’s matters only in constraints.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.12/17

slide-70
SLIDE 70

Useful Logics, Types, Rewriting, and their Automation

Type inference for Martini

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.13/17

slide-71
SLIDE 71

Useful Logics, Types, Rewriting, and their Automation

Type inference for Martini

Phase 1a: Ignore all rule premises of the form C {· · ·} Then inference can be done by standard techniques using first-order unification in the presence of rows During this phase Q’s and S’s are always variables Set variables may get unified but get no more structure Fixes the type/row structure of the principal typing

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.13/17

slide-72
SLIDE 72

Useful Logics, Types, Rewriting, and their Automation

Type inference for Martini

Phase 1a: Ignore all rule premises of the form C {· · ·} Then inference can be done by standard techniques using first-order unification in the presence of rows During this phase Q’s and S’s are always variables Set variables may get unified but get no more structure Fixes the type/row structure of the principal typing Phase 1b: Let C be union of the right-hand-side of all the ignored C {· · ·} premises This C is the most liberal constraint set that entails all of the right-hand-sides

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.13/17

slide-73
SLIDE 73

Useful Logics, Types, Rewriting, and their Automation

Type inference for Martini

Phase 1a: Ignore all rule premises of the form C {· · ·} Then inference can be done by standard techniques using first-order unification in the presence of rows During this phase Q’s and S’s are always variables Set variables may get unified but get no more structure Fixes the type/row structure of the principal typing Phase 1b: Let C be union of the right-hand-side of all the ignored C {· · ·} premises This C is the most liberal constraint set that entails all of the right-hand-sides Theorem: The typing after Phase 1b is principal

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.13/17

slide-74
SLIDE 74

Useful Logics, Types, Rewriting, and their Automation

Type inference for Martini

Phase 1a: Ignore all rule premises of the form C {· · ·} Then inference can be done by standard techniques using first-order unification in the presence of rows During this phase Q’s and S’s are always variables Set variables may get unified but get no more structure Fixes the type/row structure of the principal typing Phase 1b: Let C be union of the right-hand-side of all the ignored C {· · ·} premises This C is the most liberal constraint set that entails all of the right-hand-sides Theorem: The typing after Phase 1b is principal Phase 2: Solve the collected C

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.13/17

slide-75
SLIDE 75

Useful Logics, Types, Rewriting, and their Automation

Type inference for Martini

Phase 1a: Ignore all rule premises of the form C {· · ·} Then inference can be done by standard techniques using first-order unification in the presence of rows During this phase Q’s and S’s are always variables Set variables may get unified but get no more structure Fixes the type/row structure of the principal typing Phase 1b: Let C be union of the right-hand-side of all the ignored C {· · ·} premises This C is the most liberal constraint set that entails all of the right-hand-sides Theorem: The typing after Phase 1b is principal Phase 2: Solve the collected C In practice most of the three phases will be interleaved (This gives more readable principal typings)

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.13/17

slide-76
SLIDE 76

Useful Logics, Types, Rewriting, and their Automation

Formal properties

Subject reduction holds

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.14/17

slide-77
SLIDE 77

Useful Logics, Types, Rewriting, and their Automation

Formal properties

Subject reduction holds If M : ∅ ⊢ τ | C for C solvable, then M does not go wrong

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.14/17

slide-78
SLIDE 78

Useful Logics, Types, Rewriting, and their Automation

Formal properties

Subject reduction holds If M : ∅ ⊢ τ | C for C solvable, then M does not go wrong

C can be checked for solvability in time O((number of constraints) · (number of different field labels))

Solutions sometimes contain ⊥

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.14/17

slide-79
SLIDE 79

Useful Logics, Types, Rewriting, and their Automation

Formal properties

Subject reduction holds If M : ∅ ⊢ τ | C for C solvable, then M does not go wrong

C can be checked for solvability in time O((number of constraints) · (number of different field labels))

Solutions sometimes contain ⊥ Type inference and constraint solving takes time

O((length of program) · (number of different field labels) · α(· · ·))

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.14/17

slide-80
SLIDE 80

Useful Logics, Types, Rewriting, and their Automation

Formal properties

Subject reduction holds If M : ∅ ⊢ τ | C for C solvable, then M does not go wrong

C can be checked for solvability in time O((number of constraints) · (number of different field labels))

Solutions sometimes contain ⊥ Type inference and constraint solving takes time

O((length of program) · (number of different field labels) · α(· · ·))

Oops! C1 C2 is hard to decide. But luckily we don’t have to.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.14/17

slide-81
SLIDE 81

Useful Logics, Types, Rewriting, and their Automation

Reasons to read our paper

(beyond wanting to see the formalism behind the previous slides)

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.15/17

slide-82
SLIDE 82

Useful Logics, Types, Rewriting, and their Automation

Reasons to read our paper

(beyond wanting to see the formalism behind the previous slides) How to solve constraints in our system and how to restrict the solving such as not to lose principality

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.15/17

slide-83
SLIDE 83

Useful Logics, Types, Rewriting, and their Automation

Reasons to read our paper

(beyond wanting to see the formalism behind the previous slides) How to solve constraints in our system and how to restrict the solving such as not to lose principality Extension of Martini with let-polymorphism by the Hindley–Milner procedure

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.15/17

slide-84
SLIDE 84

Useful Logics, Types, Rewriting, and their Automation

Reasons to read our paper

(beyond wanting to see the formalism behind the previous slides) How to solve constraints in our system and how to restrict the solving such as not to lose principality Extension of Martini with let-polymorphism by the Hindley–Milner procedure A robust proof that ⊥-less typing rules for linking/concatenation operators make typability NP-hard. (Even just concatenable records in a first-order language)

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.15/17

slide-85
SLIDE 85

Useful Logics, Types, Rewriting, and their Automation

Reasons to read our paper

(beyond wanting to see the formalism behind the previous slides) How to solve constraints in our system and how to restrict the solving such as not to lose principality Extension of Martini with let-polymorphism by the Hindley–Milner procedure A robust proof that ⊥-less typing rules for linking/concatenation operators make typability NP-hard. (Even just concatenable records in a first-order language) First written down presentation of an efficient algorithm for row unification

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.15/17

slide-86
SLIDE 86

Useful Logics, Types, Rewriting, and their Automation

Reasons to read our paper

(beyond wanting to see the formalism behind the previous slides) How to solve constraints in our system and how to restrict the solving such as not to lose principality Extension of Martini with let-polymorphism by the Hindley–Milner procedure A robust proof that ⊥-less typing rules for linking/concatenation operators make typability NP-hard. (Even just concatenable records in a first-order language) First written down presentation of an efficient algorithm for row unification URL for our online type inference implementation

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.15/17

slide-87
SLIDE 87

Useful Logics, Types, Rewriting, and their Automation

Shortcomings and future work

Programming in the large: Not nearly strong enough to subsume ML modules (yet!) Needs polymorphism of local bindings in modules Needs type components of modules Needs type abstractions for modules

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.16/17

slide-88
SLIDE 88

Useful Logics, Types, Rewriting, and their Automation

Shortcomings and future work

Programming in the large: Not nearly strong enough to subsume ML modules (yet!) Needs polymorphism of local bindings in modules Needs type components of modules Needs type abstractions for modules Row pollution: When a module is constructed, the row part

  • f its type will list types of all fields the module may ever be

linked with. Not that bad: The list will be inferred Can be mitigated by polymorphism or by programmer annotations. Perhaps stratifed type inference could also be useful.

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.16/17

slide-89
SLIDE 89

Useful Logics, Types, Rewriting, and their Automation

The end

Thank you

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.17/17

slide-90
SLIDE 90

Useful Logics, Types, Rewriting, and their Automation

The end

Thank you Questions?

Type Inference, Principal Typings, and Let-Polymorphism for First-Class Mixin Modules – p.17/17