Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a...

Post on 29-May-2020

5 views 0 download

Transcript of Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a...

Types for Flexible ObjectsBuilding a Typed Scripting Language

Pottayil Harisanker Menon, Zachary Palmer,

Alexander Rozenshteyn, Scott F. Smith

The Johns Hopkins University

November 17th, 2014

Objective

Flexible OO language

+Static typing, inference, etc.

2/1

Objective

Flexible OO language+

Static typing, inference, etc.

2/1

What is “flexible?”

Freely transition between views on data

Freely manipulate data at runtimePython, Javascript, Ruby

Objects are dictionaries (almost seamlessly)Dynamic lookup, update, etc. based on runtimeconditions

Minimal semantic clutter (for readability)

Good for ad-hoc, situational requirements

3/1

What is “flexible?”

Freely transition between views on data

Freely manipulate data at runtime

Python, Javascript, Ruby

Objects are dictionaries (almost seamlessly)Dynamic lookup, update, etc. based on runtimeconditions

Minimal semantic clutter (for readability)

Good for ad-hoc, situational requirements

3/1

What is “flexible?”

Freely transition between views on data

Freely manipulate data at runtimePython, Javascript, Ruby

Objects are dictionaries (almost seamlessly)Dynamic lookup, update, etc. based on runtimeconditions

Minimal semantic clutter (for readability)

Good for ad-hoc, situational requirements

3/1

What is “flexible?”

Freely transition between views on data

Freely manipulate data at runtimePython, Javascript, Ruby

Objects are dictionaries (almost seamlessly)

Dynamic lookup, update, etc. based on runtimeconditions

Minimal semantic clutter (for readability)

Good for ad-hoc, situational requirements

3/1

What is “flexible?”

Freely transition between views on data

Freely manipulate data at runtimePython, Javascript, Ruby

Objects are dictionaries (almost seamlessly)Dynamic lookup, update, etc. based on runtimeconditions

Minimal semantic clutter (for readability)

Good for ad-hoc, situational requirements

3/1

What is “flexible?”

Freely transition between views on data

Freely manipulate data at runtimePython, Javascript, Ruby

Objects are dictionaries (almost seamlessly)Dynamic lookup, update, etc. based on runtimeconditions

Minimal semantic clutter (for readability)

Good for ad-hoc, situational requirements

3/1

What is “flexible?”

Freely transition between views on data

Freely manipulate data at runtimePython, Javascript, Ruby

Objects are dictionaries (almost seamlessly)Dynamic lookup, update, etc. based on runtimeconditions

Minimal semantic clutter (for readability)

Good for ad-hoc, situational requirements

3/1

Examples of Flexibility

Add a method to an existing object:

class A:

def foo (): return 1

a = A()

a.bar = types.MethodType(

lambda self: 2, a)

4/1

Examples of Flexibility

Rely on execution path invariants:

def neg(x):

if type(x) is int:

return -x

else:

return not x

print neg (2) + 3

4/1

Why Types?

Flexible languages are good:

Faster developmentCapture complex ideas with clever patternsStructurally typed (“duck typing”)

Static types are good:

Describe invariants on dataHelp programmer understandingStatically identify programming errorsImprove runtime performanceOther static invariants: immutability, limiting datascope, etc.

We want both!

5/1

Why Types?

Flexible languages are good:Faster development

Capture complex ideas with clever patternsStructurally typed (“duck typing”)

Static types are good:

Describe invariants on dataHelp programmer understandingStatically identify programming errorsImprove runtime performanceOther static invariants: immutability, limiting datascope, etc.

We want both!

5/1

Why Types?

Flexible languages are good:Faster developmentCapture complex ideas with clever patterns

Structurally typed (“duck typing”)

Static types are good:

Describe invariants on dataHelp programmer understandingStatically identify programming errorsImprove runtime performanceOther static invariants: immutability, limiting datascope, etc.

We want both!

5/1

Why Types?

Flexible languages are good:Faster developmentCapture complex ideas with clever patternsStructurally typed (“duck typing”)

Static types are good:

Describe invariants on dataHelp programmer understandingStatically identify programming errorsImprove runtime performanceOther static invariants: immutability, limiting datascope, etc.

We want both!

5/1

Why Types?

Flexible languages are good:Faster developmentCapture complex ideas with clever patternsStructurally typed (“duck typing”)

Static types are good:

Describe invariants on dataHelp programmer understandingStatically identify programming errorsImprove runtime performanceOther static invariants: immutability, limiting datascope, etc.

We want both!

5/1

Why Types?

Flexible languages are good:Faster developmentCapture complex ideas with clever patternsStructurally typed (“duck typing”)

Static types are good:Describe invariants on data

Help programmer understandingStatically identify programming errorsImprove runtime performanceOther static invariants: immutability, limiting datascope, etc.

We want both!

5/1

Why Types?

Flexible languages are good:Faster developmentCapture complex ideas with clever patternsStructurally typed (“duck typing”)

Static types are good:Describe invariants on dataHelp programmer understanding

Statically identify programming errorsImprove runtime performanceOther static invariants: immutability, limiting datascope, etc.

We want both!

5/1

Why Types?

Flexible languages are good:Faster developmentCapture complex ideas with clever patternsStructurally typed (“duck typing”)

Static types are good:Describe invariants on dataHelp programmer understandingStatically identify programming errors

Improve runtime performanceOther static invariants: immutability, limiting datascope, etc.

We want both!

5/1

Why Types?

Flexible languages are good:Faster developmentCapture complex ideas with clever patternsStructurally typed (“duck typing”)

Static types are good:Describe invariants on dataHelp programmer understandingStatically identify programming errorsImprove runtime performance

Other static invariants: immutability, limiting datascope, etc.

We want both!

5/1

Why Types?

Flexible languages are good:Faster developmentCapture complex ideas with clever patternsStructurally typed (“duck typing”)

Static types are good:Describe invariants on dataHelp programmer understandingStatically identify programming errorsImprove runtime performanceOther static invariants: immutability, limiting datascope, etc.

We want both!

5/1

Why Types?

Flexible languages are good:Faster developmentCapture complex ideas with clever patternsStructurally typed (“duck typing”)

Static types are good:Describe invariants on dataHelp programmer understandingStatically identify programming errorsImprove runtime performanceOther static invariants: immutability, limiting datascope, etc.

We want both!

5/1

Options?Powerful traditional type system

, Captures static invariants (monads, dep. types)/ Often incurs semantic clutter/ High barrier to entryNot enough flexibility

Build type inference system for existinglanguage (e.g. DRuby)

, Fundamentally flexible language/ Limited success: language features defy statictyping (e.g. mutable monkeypatching, Pythonlocals)Not enough static typing

Design a language to includestatically-typed flex

6/1

Options?Powerful traditional type system

, Captures static invariants (monads, dep. types)/ Often incurs semantic clutter/ High barrier to entryNot enough flexibility

Build type inference system for existinglanguage (e.g. DRuby)

, Fundamentally flexible language/ Limited success: language features defy statictyping (e.g. mutable monkeypatching, Pythonlocals)Not enough static typing

Design a language to includestatically-typed flex

6/1

Options?Powerful traditional type system

, Captures static invariants (monads, dep. types)/ Often incurs semantic clutter/ High barrier to entryNot enough flexibility

Build type inference system for existinglanguage (e.g. DRuby)

, Fundamentally flexible language/ Limited success: language features defy statictyping (e.g. mutable monkeypatching, Pythonlocals)Not enough static typing

Design a language to includestatically-typed flex

6/1

TinyBang

Start with an ML-like basis

records, variants, patterns, let, refs, inference

Add flexible propertiesAll types inferred – no declarationsStructural subtyping (“duck typing”)Powerful record combinatorsFirst-class match clausesRefinement on pattern matching

Result: flexible language with static typing

7/1

TinyBang

Start with an ML-like basis

records, variants, patterns, let, refs, inference

Add flexible propertiesAll types inferred – no declarationsStructural subtyping (“duck typing”)Powerful record combinatorsFirst-class match clausesRefinement on pattern matching

Result: flexible language with static typing

7/1

TinyBang

Start with an ML-like basis

records, variants, patterns, let, refs, inference

Add flexible propertiesAll types inferred – no declarationsStructural subtyping (“duck typing”)Powerful record combinatorsFirst-class match clausesRefinement on pattern matching

Result: flexible language with static typing

7/1

TinyBang

Start with an ML-like basis

records, variants, patterns, let, refs, inference

Add flexible propertiesAll types inferred – no declarationsStructural subtyping (“duck typing”)Powerful record combinatorsFirst-class match clausesRefinement on pattern matching

Result: flexible language with static typing

7/1

Outline

SemanticsPowerful records and record combinatorsFirst-class match clausesObject encoding using variants

Static typingOverviewUnion eliminationPolymorphism

Summary

8/1

Asymmetric Concatenation

Use type-indexed records [Blume et. al. ’06]

{int = 4, A = {int = 5}}

4 & `A 5

Note: 1-ary record = 1-ary variant

Concatenation asymmetrically prefers leftcomponents

`A 4 & `B 6 & `A 3 ∼= `A 4 & `B 6

Asymmetry is fundamental in several encodings

9/1

Asymmetric Concatenation

Use type-indexed records [Blume et. al. ’06]

{int = 4, A = {int = 5}}

4 & `A 5

Note: 1-ary record = 1-ary variant

Concatenation asymmetrically prefers leftcomponents

`A 4 & `B 6 & `A 3 ∼= `A 4 & `B 6

Asymmetry is fundamental in several encodings

9/1

Asymmetric Concatenation

Use type-indexed records [Blume et. al. ’06]

{int = 4, A = {int = 5}}

4 & `A 5

Note: 1-ary record = 1-ary variant

Concatenation asymmetrically prefers leftcomponents

`A 4 & `B 6 & `A 3 ∼= `A 4 & `B 6

Asymmetry is fundamental in several encodings

9/1

Asymmetric Concatenation

Use type-indexed records [Blume et. al. ’06]

{int = 4, A = {int = 5}}

4 & `A 5

Note: 1-ary record = 1-ary variant

Concatenation asymmetrically prefers leftcomponents

`A 4 & `B 6 & `A 3 ∼= `A 4 & `B 6

Asymmetry is fundamental in several encodings

9/1

Asymmetric Concatenation

Use type-indexed records [Blume et. al. ’06]

{int = 4, A = {int = 5}}

4 & `A 5

Note: 1-ary record = 1-ary variant

Concatenation asymmetrically prefers leftcomponents

`A 4 & `B 6 & `A 3 ∼= `A 4 & `B 6

Asymmetry is fundamental in several encodings

9/1

Asymmetric Concatenation

Use type-indexed records [Blume et. al. ’06]

{int = 4, A = {int = 5}}

4 & `A 5

Note: 1-ary record = 1-ary variant

Concatenation asymmetrically prefers leftcomponents

`A 4 & `B 6 & `A 3 ∼= `A 4 & `B 6

Asymmetry is fundamental in several encodings

9/1

Outline

SemanticsPowerful records and record combinatorsFirst-class match clausesObject encoding using variants

Static typingOverviewUnion eliminationPolymorphism

Summary

10/1

Partial Functions

All TinyBang functions are partial viapattern-matching:

int -> 0 matches all records with an int

componentint & x -> x is integer identity

In patterns, & is conjunctionVariables (e.g. x) match anything and bind it

Pattern-matching takes the leftmost match(`B x -> x+1) (`A 3 & `B 1) ⇒ 2

(`B x -> x+1) (`B 5 & `A 3 & `B 1) ⇒ 6

11/1

Partial Functions

All TinyBang functions are partial viapattern-matching:

int -> 0 matches all records with an int

component

int & x -> x is integer identityIn patterns, & is conjunctionVariables (e.g. x) match anything and bind it

Pattern-matching takes the leftmost match(`B x -> x+1) (`A 3 & `B 1) ⇒ 2

(`B x -> x+1) (`B 5 & `A 3 & `B 1) ⇒ 6

11/1

Partial Functions

All TinyBang functions are partial viapattern-matching:

int -> 0 matches all records with an int

componentint & x -> x is integer identity

In patterns, & is conjunctionVariables (e.g. x) match anything and bind it

Pattern-matching takes the leftmost match(`B x -> x+1) (`A 3 & `B 1) ⇒ 2

(`B x -> x+1) (`B 5 & `A 3 & `B 1) ⇒ 6

11/1

Partial Functions

All TinyBang functions are partial viapattern-matching:

int -> 0 matches all records with an int

componentint & x -> x is integer identity

In patterns, & is conjunctionVariables (e.g. x) match anything and bind it

Pattern-matching takes the leftmost match(`B x -> x+1) (`A 3 & `B 1) ⇒ 2

(`B x -> x+1) (`B 5 & `A 3 & `B 1) ⇒ 6

11/1

Partial Functions

All TinyBang functions are partial viapattern-matching:

int -> 0 matches all records with an int

componentint & x -> x is integer identity

In patterns, & is conjunctionVariables (e.g. x) match anything and bind it

Pattern-matching takes the leftmost match(`B x -> x+1) (`A 3 & `B 1) ⇒ 2

(`B x -> x+1) (`B 5 & `A 3 & `B 1) ⇒ 6

11/1

Extensible match clauses

Single clause: write as function(`l x & `r y -> x + y)

Concatenate functions (&) for multiple clauses(`l x & `r y -> x + y) &

(int & z -> z + 1)

Encodes match!

Operator & uniformly concatenates records,match clauses: (`x 1) & (int -> 0)

12/1

Extensible match clauses

Single clause: write as function(`l x & `r y -> x + y)

Concatenate functions (&) for multiple clauses(`l x & `r y -> x + y) &

(int & z -> z + 1)

Encodes match!

Operator & uniformly concatenates records,match clauses: (`x 1) & (int -> 0)

12/1

Extensible match clauses

Single clause: write as function(`l x & `r y -> x + y)

Concatenate functions (&) for multiple clauses(`l x & `r y -> x + y) &

(int & z -> z + 1)

Encodes match!

Operator & uniformly concatenates records,match clauses: (`x 1) & (int -> 0)

12/1

Extensible match clauses

Single clause: write as function(`l x & `r y -> x + y)

Concatenate functions (&) for multiple clauses(`l x & `r y -> x + y) &

(int & z -> z + 1)

Encodes match!

Operator & uniformly concatenates records,match clauses: (`x 1) & (int -> 0)

12/1

Outline

SemanticsPowerful records and record combinatorsFirst-class match clausesObject encoding using variants

Static typingOverviewUnion eliminationPolymorphism

Summary

13/1

Variant-Based Object Encodinglet rec seal = (see paper ) in

let prePoint =

`x 2 & `y 4 &

(`mg & `self slf -> slf `gx + slf `gy ) &

(`gx & `self slf -> slf.x) & . . .in let point = seal prePoint in

point `mg // returns 6

& can combine data and match clauses

Match clause encoding of objects, not records

Seal is a combinator tying self-reference knot

Can seal, message, extend, reseal, message

All statically typed!

14/1

Variant-Based Object Encodinglet rec seal = (see paper ) in

let prePoint =

`x 2 & `y 4 &

(`mg & `self slf -> slf `gx + slf `gy ) &

(`gx & `self slf -> slf.x) & . . .in let point = seal prePoint in

point `mg // returns 6

& can combine data and match clauses

Match clause encoding of objects, not records

Seal is a combinator tying self-reference knot

Can seal, message, extend, reseal, message

All statically typed!

14/1

Variant-Based Object Encodinglet rec seal = (see paper ) in

let prePoint =

`x 2 & `y 4 &

(`mg & `self slf -> slf `gx + slf `gy ) &

(`gx & `self slf -> slf.x) & . . .in let point = seal prePoint in

point `mg // returns 6

& can combine data and match clauses

Match clause encoding of objects, not records

Seal is a combinator tying self-reference knot

Can seal, message, extend, reseal, message

All statically typed!

14/1

Variant-Based Object Encodinglet rec seal = (see paper ) in

let prePoint =

`x 2 & `y 4 &

(`mg & `self slf -> slf `gx + slf `gy ) &

(`gx & `self slf -> slf.x) & . . .in let point = seal prePoint in

point `mg // returns 6

& can combine data and match clauses

Match clause encoding of objects, not records

Seal is a combinator tying self-reference knot

Can seal, message, extend, reseal, message

All statically typed!

14/1

Variant-Based Object Encodinglet rec seal = (see paper ) in

let prePoint =

`x 2 & `y 4 &

(`mg & `self slf -> slf `gx + slf `gy ) &

(`gx & `self slf -> slf.x) & . . .in let point = seal prePoint in

point `mg // returns 6

& can combine data and match clauses

Match clause encoding of objects, not records

Seal is a combinator tying self-reference knot

Can seal, message, extend, reseal, message

All statically typed!14/1

Other EncodingsTogether, type-indexed records and partial functionscan encode:

Conditionals (`True () and `False ())

Variant-based objects

Operator overloading

Classes, inheritance, subclasses, etc.

Mixins, dynamic functional object extension

First-class cases

Optional arguments

etc.

And we can type them!

15/1

Other EncodingsTogether, type-indexed records and partial functionscan encode:

Conditionals (`True () and `False ())

Variant-based objects

Operator overloading

Classes, inheritance, subclasses, etc.

Mixins, dynamic functional object extension

First-class cases

Optional arguments

etc.

And we can type them!

15/1

Other EncodingsTogether, type-indexed records and partial functionscan encode:

Conditionals (`True () and `False ())

Variant-based objects

Operator overloading

Classes, inheritance, subclasses, etc.

Mixins, dynamic functional object extension

First-class cases

Optional arguments

etc.

And we can type them!15/1

Outline

SemanticsPowerful records and record combinatorsFirst-class match clausesObject encoding using variants

Static typingOverviewUnion eliminationPolymorphism

Summary

16/1

Types

Rooted in subtype constraint inference(Aiken/Heintze/Pottier)

With new notions of

Union type eliminationPolymorphismMetatheory (rule system, soundness, etc)

Inspired by program analysis

Union elimination: path sensitivityPolymorphism: context sensitivityConcession: it’s whole-program typing

17/1

Types

Rooted in subtype constraint inference(Aiken/Heintze/Pottier)

With new notions of

Union type eliminationPolymorphismMetatheory (rule system, soundness, etc)

Inspired by program analysis

Union elimination: path sensitivityPolymorphism: context sensitivityConcession: it’s whole-program typing

17/1

Types

Rooted in subtype constraint inference(Aiken/Heintze/Pottier)

With new notions ofUnion type elimination

PolymorphismMetatheory (rule system, soundness, etc)

Inspired by program analysis

Union elimination: path sensitivityPolymorphism: context sensitivityConcession: it’s whole-program typing

17/1

Types

Rooted in subtype constraint inference(Aiken/Heintze/Pottier)

With new notions ofUnion type eliminationPolymorphism

Metatheory (rule system, soundness, etc)

Inspired by program analysis

Union elimination: path sensitivityPolymorphism: context sensitivityConcession: it’s whole-program typing

17/1

Types

Rooted in subtype constraint inference(Aiken/Heintze/Pottier)

With new notions ofUnion type eliminationPolymorphismMetatheory (rule system, soundness, etc)

Inspired by program analysis

Union elimination: path sensitivityPolymorphism: context sensitivityConcession: it’s whole-program typing

17/1

Types

Rooted in subtype constraint inference(Aiken/Heintze/Pottier)

With new notions ofUnion type eliminationPolymorphismMetatheory (rule system, soundness, etc)

Inspired by program analysis

Union elimination: path sensitivityPolymorphism: context sensitivityConcession: it’s whole-program typing

17/1

Types

Rooted in subtype constraint inference(Aiken/Heintze/Pottier)

With new notions ofUnion type eliminationPolymorphismMetatheory (rule system, soundness, etc)

Inspired by program analysisUnion elimination: path sensitivity

Polymorphism: context sensitivityConcession: it’s whole-program typing

17/1

Types

Rooted in subtype constraint inference(Aiken/Heintze/Pottier)

With new notions ofUnion type eliminationPolymorphismMetatheory (rule system, soundness, etc)

Inspired by program analysisUnion elimination: path sensitivityPolymorphism: context sensitivity

Concession: it’s whole-program typing

17/1

Types

Rooted in subtype constraint inference(Aiken/Heintze/Pottier)

With new notions ofUnion type eliminationPolymorphismMetatheory (rule system, soundness, etc)

Inspired by program analysisUnion elimination: path sensitivityPolymorphism: context sensitivityConcession: it’s whole-program typing

17/1

Outline

SemanticsPowerful records and record combinatorsFirst-class match clausesObject encoding using variants

Static typingOverviewUnion eliminationPolymorphism

Summary

18/1

Union type eliminationExample: patterns `Z and `S `S x on Peanonumber input

Partial

@⌧.⌧ <: ↵0 2 V

↵0G#V�;

V 0 ↵00

Empty Onion

⌧ <: ↵0 2 V () <: ↵00 2 V 0 F = {↵0 <: ↵0

0}↵0

V�F

V 0 ↵00

Label

l ↵1 <: ↵0 2 V l ↵01 <: ↵0

0 2 V 0 ↵1�V�F

V 0 ↵01

↵0�V�F

V 0 ↵00

Conjunction Pattern

↵01 &↵

02 <: ↵0

0 2 V 0 ↵0�1V�F1

V 0 ↵01 ↵0

�2V�F2

V 0 ↵02

↵0min(�1,�2)

V�F1[F2V 0 ↵0

0

Onion Value Left

↵1 &↵2 <: ↵0 2 V ↵1�V�F

V 0 ↵00 � 6= #

↵0�V�F

V 0 ↵00

Onion Value Right

↵1 &↵2 <: ↵0 2 V ↵01 &↵

02 <: ↵0

0 62 V 0 ↵1#V�F 0

V 0 ↵00 ↵2

�V�F

V 0 ↵00

↵0�V�F

V 0 ↵00

Label Mismatch

l ↵1 <: ↵0 2 V⌧ <: ↵0

0 2 V 0 ⌧ = l0 ↵2 only if l 6= l0 ⌧ not of the form ↵0&↵00 or ()

↵0#V�;

V 0 ↵00

Fig. 3.9. Type compatibility: does a type match a pattern?

[↵ =

‘S‘Z

()

argument type

‘Z ()

‘S ‘S ↵

‘S ↵

slice

sliceslice

some possible slices

‘Z ()

‘S ‘S

()

4

G#4

4

patterns

Fig. 3.10. Type compatibility example

Matching Type matching directly parallels expression matching. As in theevaluation system, type matching propagates the result of compatibility and usesthe � place to enforce left precedence of dispatch. Matching ↵0 ↵1

�V0;V1

↵2\C 0

is defined as the least relation satisfying the clauses in Figure 3.11.

Constraint Closure Constraint closure can now be defined; each step of clo-sure represents one forward propagation of constraint information and abstractlymodels a single step of the operational semantics. This closure is implicitly de-fined in terms of an abstract polymorphism framework defined by two functions.The first, �, is analogous to the ↵(�) freshening function of the operationalsemantics. For decidability, however, we do not want � to freshen every vari-able uniquely; it only performs some ↵-substitution on the constrained type.We write �(C,↵) to indicate the freshening of the variables in C. The additionalparameter ↵ describes the call site at which the polyinstantiation took place;this is useful for some polymorphism models.

Adaptively eliminate union to depth of patternSlices specialize argument type

let f = (x:`A () -> x.B) &

(y:`P () -> 1)

in f (`A () & `B 5) + f `P ()

19/1

Union type eliminationExample: patterns `Z and `S `S x on Peanonumber input

Partial

@⌧.⌧ <: ↵0 2 V

↵0G#V�;

V 0 ↵00

Empty Onion

⌧ <: ↵0 2 V () <: ↵00 2 V 0 F = {↵0 <: ↵0

0}↵0

V�F

V 0 ↵00

Label

l ↵1 <: ↵0 2 V l ↵01 <: ↵0

0 2 V 0 ↵1�V�F

V 0 ↵01

↵0�V�F

V 0 ↵00

Conjunction Pattern

↵01 &↵

02 <: ↵0

0 2 V 0 ↵0�1V�F1

V 0 ↵01 ↵0

�2V�F2

V 0 ↵02

↵0min(�1,�2)

V�F1[F2V 0 ↵0

0

Onion Value Left

↵1 &↵2 <: ↵0 2 V ↵1�V�F

V 0 ↵00 � 6= #

↵0�V�F

V 0 ↵00

Onion Value Right

↵1 &↵2 <: ↵0 2 V ↵01 &↵

02 <: ↵0

0 62 V 0 ↵1#V�F 0

V 0 ↵00 ↵2

�V�F

V 0 ↵00

↵0�V�F

V 0 ↵00

Label Mismatch

l ↵1 <: ↵0 2 V⌧ <: ↵0

0 2 V 0 ⌧ = l0 ↵2 only if l 6= l0 ⌧ not of the form ↵0&↵00 or ()

↵0#V�;

V 0 ↵00

Fig. 3.9. Type compatibility: does a type match a pattern?

[↵ =

‘S‘Z

()

argument type

‘Z ()

‘S ‘S ↵

‘S ↵

slice

sliceslice

some possible slices

‘Z ()

‘S ‘S

()

4

G#4

4

patterns

Fig. 3.10. Type compatibility example

Matching Type matching directly parallels expression matching. As in theevaluation system, type matching propagates the result of compatibility and usesthe � place to enforce left precedence of dispatch. Matching ↵0 ↵1

�V0;V1

↵2\C 0

is defined as the least relation satisfying the clauses in Figure 3.11.

Constraint Closure Constraint closure can now be defined; each step of clo-sure represents one forward propagation of constraint information and abstractlymodels a single step of the operational semantics. This closure is implicitly de-fined in terms of an abstract polymorphism framework defined by two functions.The first, �, is analogous to the ↵(�) freshening function of the operationalsemantics. For decidability, however, we do not want � to freshen every vari-able uniquely; it only performs some ↵-substitution on the constrained type.We write �(C,↵) to indicate the freshening of the variables in C. The additionalparameter ↵ describes the call site at which the polyinstantiation took place;this is useful for some polymorphism models.

Adaptively eliminate union to depth of patternSlices specialize argument type

let f = (x:`A () -> x.B) &

(y:`P () -> 1)

in f (`A () & `B 5) + f `P ()

19/1

Union type eliminationExample: patterns `Z and `S `S x on Peanonumber input

Partial

@⌧.⌧ <: ↵0 2 V

↵0G#V�;

V 0 ↵00

Empty Onion

⌧ <: ↵0 2 V () <: ↵00 2 V 0 F = {↵0 <: ↵0

0}↵0

V�F

V 0 ↵00

Label

l ↵1 <: ↵0 2 V l ↵01 <: ↵0

0 2 V 0 ↵1�V�F

V 0 ↵01

↵0�V�F

V 0 ↵00

Conjunction Pattern

↵01 &↵

02 <: ↵0

0 2 V 0 ↵0�1V�F1

V 0 ↵01 ↵0

�2V�F2

V 0 ↵02

↵0min(�1,�2)

V�F1[F2V 0 ↵0

0

Onion Value Left

↵1 &↵2 <: ↵0 2 V ↵1�V�F

V 0 ↵00 � 6= #

↵0�V�F

V 0 ↵00

Onion Value Right

↵1 &↵2 <: ↵0 2 V ↵01 &↵

02 <: ↵0

0 62 V 0 ↵1#V�F 0

V 0 ↵00 ↵2

�V�F

V 0 ↵00

↵0�V�F

V 0 ↵00

Label Mismatch

l ↵1 <: ↵0 2 V⌧ <: ↵0

0 2 V 0 ⌧ = l0 ↵2 only if l 6= l0 ⌧ not of the form ↵0&↵00 or ()

↵0#V�;

V 0 ↵00

Fig. 3.9. Type compatibility: does a type match a pattern?

[↵ =

‘S‘Z

()

argument type

‘Z ()

‘S ‘S ↵

‘S ↵

slice

sliceslice

some possible slices

‘Z ()

‘S ‘S

()

4

G#4

4

patterns

Fig. 3.10. Type compatibility example

Matching Type matching directly parallels expression matching. As in theevaluation system, type matching propagates the result of compatibility and usesthe � place to enforce left precedence of dispatch. Matching ↵0 ↵1

�V0;V1

↵2\C 0

is defined as the least relation satisfying the clauses in Figure 3.11.

Constraint Closure Constraint closure can now be defined; each step of clo-sure represents one forward propagation of constraint information and abstractlymodels a single step of the operational semantics. This closure is implicitly de-fined in terms of an abstract polymorphism framework defined by two functions.The first, �, is analogous to the ↵(�) freshening function of the operationalsemantics. For decidability, however, we do not want � to freshen every vari-able uniquely; it only performs some ↵-substitution on the constrained type.We write �(C,↵) to indicate the freshening of the variables in C. The additionalparameter ↵ describes the call site at which the polyinstantiation took place;this is useful for some polymorphism models.

Adaptively eliminate union to depth of pattern

Slices specialize argument type

let f = (x:`A () -> x.B) &

(y:`P () -> 1)

in f (`A () & `B 5) + f `P ()

19/1

Union type eliminationExample: patterns `Z and `S `S x on Peanonumber input

Partial

@⌧.⌧ <: ↵0 2 V

↵0G#V�;

V 0 ↵00

Empty Onion

⌧ <: ↵0 2 V () <: ↵00 2 V 0 F = {↵0 <: ↵0

0}↵0

V�F

V 0 ↵00

Label

l ↵1 <: ↵0 2 V l ↵01 <: ↵0

0 2 V 0 ↵1�V�F

V 0 ↵01

↵0�V�F

V 0 ↵00

Conjunction Pattern

↵01 &↵

02 <: ↵0

0 2 V 0 ↵0�1V�F1

V 0 ↵01 ↵0

�2V�F2

V 0 ↵02

↵0min(�1,�2)

V�F1[F2V 0 ↵0

0

Onion Value Left

↵1 &↵2 <: ↵0 2 V ↵1�V�F

V 0 ↵00 � 6= #

↵0�V�F

V 0 ↵00

Onion Value Right

↵1 &↵2 <: ↵0 2 V ↵01 &↵

02 <: ↵0

0 62 V 0 ↵1#V�F 0

V 0 ↵00 ↵2

�V�F

V 0 ↵00

↵0�V�F

V 0 ↵00

Label Mismatch

l ↵1 <: ↵0 2 V⌧ <: ↵0

0 2 V 0 ⌧ = l0 ↵2 only if l 6= l0 ⌧ not of the form ↵0&↵00 or ()

↵0#V�;

V 0 ↵00

Fig. 3.9. Type compatibility: does a type match a pattern?

[↵ =

‘S‘Z

()

argument type

‘Z ()

‘S ‘S ↵

‘S ↵

slice

sliceslice

some possible slices

‘Z ()

‘S ‘S

()

4

G#4

4

patterns

Fig. 3.10. Type compatibility example

Matching Type matching directly parallels expression matching. As in theevaluation system, type matching propagates the result of compatibility and usesthe � place to enforce left precedence of dispatch. Matching ↵0 ↵1

�V0;V1

↵2\C 0

is defined as the least relation satisfying the clauses in Figure 3.11.

Constraint Closure Constraint closure can now be defined; each step of clo-sure represents one forward propagation of constraint information and abstractlymodels a single step of the operational semantics. This closure is implicitly de-fined in terms of an abstract polymorphism framework defined by two functions.The first, �, is analogous to the ↵(�) freshening function of the operationalsemantics. For decidability, however, we do not want � to freshen every vari-able uniquely; it only performs some ↵-substitution on the constrained type.We write �(C,↵) to indicate the freshening of the variables in C. The additionalparameter ↵ describes the call site at which the polyinstantiation took place;this is useful for some polymorphism models.

Adaptively eliminate union to depth of patternSlices specialize argument type

let f = (x:`A () -> x.B) &

(y:`P () -> 1)

in f (`A () & `B 5) + f `P ()

19/1

Union type eliminationExample: patterns `Z and `S `S x on Peanonumber input

Partial

@⌧.⌧ <: ↵0 2 V

↵0G#V�;

V 0 ↵00

Empty Onion

⌧ <: ↵0 2 V () <: ↵00 2 V 0 F = {↵0 <: ↵0

0}↵0

V�F

V 0 ↵00

Label

l ↵1 <: ↵0 2 V l ↵01 <: ↵0

0 2 V 0 ↵1�V�F

V 0 ↵01

↵0�V�F

V 0 ↵00

Conjunction Pattern

↵01 &↵

02 <: ↵0

0 2 V 0 ↵0�1V�F1

V 0 ↵01 ↵0

�2V�F2

V 0 ↵02

↵0min(�1,�2)

V�F1[F2V 0 ↵0

0

Onion Value Left

↵1 &↵2 <: ↵0 2 V ↵1�V�F

V 0 ↵00 � 6= #

↵0�V�F

V 0 ↵00

Onion Value Right

↵1 &↵2 <: ↵0 2 V ↵01 &↵

02 <: ↵0

0 62 V 0 ↵1#V�F 0

V 0 ↵00 ↵2

�V�F

V 0 ↵00

↵0�V�F

V 0 ↵00

Label Mismatch

l ↵1 <: ↵0 2 V⌧ <: ↵0

0 2 V 0 ⌧ = l0 ↵2 only if l 6= l0 ⌧ not of the form ↵0&↵00 or ()

↵0#V�;

V 0 ↵00

Fig. 3.9. Type compatibility: does a type match a pattern?

[↵ =

‘S‘Z

()

argument type

‘Z ()

‘S ‘S ↵

‘S ↵

slice

sliceslice

some possible slices

‘Z ()

‘S ‘S

()

4

G#4

4

patterns

Fig. 3.10. Type compatibility example

Matching Type matching directly parallels expression matching. As in theevaluation system, type matching propagates the result of compatibility and usesthe � place to enforce left precedence of dispatch. Matching ↵0 ↵1

�V0;V1

↵2\C 0

is defined as the least relation satisfying the clauses in Figure 3.11.

Constraint Closure Constraint closure can now be defined; each step of clo-sure represents one forward propagation of constraint information and abstractlymodels a single step of the operational semantics. This closure is implicitly de-fined in terms of an abstract polymorphism framework defined by two functions.The first, �, is analogous to the ↵(�) freshening function of the operationalsemantics. For decidability, however, we do not want � to freshen every vari-able uniquely; it only performs some ↵-substitution on the constrained type.We write �(C,↵) to indicate the freshening of the variables in C. The additionalparameter ↵ describes the call site at which the polyinstantiation took place;this is useful for some polymorphism models.

Adaptively eliminate union to depth of patternSlices specialize argument type

let f = (x:`A () -> x.B) &

(y:`P () -> 1)

in f (`A () & `B 5) + f `P ()

19/1

Outline

SemanticsPowerful records and record combinatorsFirst-class match clausesObject encoding using variants

Static typingOverviewUnion eliminationPolymorphism

Summary

20/1

Inferred function polymorphism

let-polymorphism ignores call context

Program analyses have polyvariance, e.g. nCFAbut are brittle to refactoring—arbitrary cutoff at n depth

Our approach: polyvariance approach withregular expression call strings

Limits polymorphism of recursion but nothing elseSimilar to DCPA and ∆CFA, but optimistic

21/1

Inferred function polymorphism

let-polymorphism ignores call context

Program analyses have polyvariance, e.g. nCFAbut are brittle to refactoring—arbitrary cutoff at n depth

Our approach: polyvariance approach withregular expression call strings

Limits polymorphism of recursion but nothing elseSimilar to DCPA and ∆CFA, but optimistic

21/1

Inferred function polymorphism

let-polymorphism ignores call context

Program analyses have polyvariance, e.g. nCFAbut are brittle to refactoring—arbitrary cutoff at n depth

Our approach: polyvariance approach withregular expression call strings

Limits polymorphism of recursion but nothing elseSimilar to DCPA and ∆CFA, but optimistic

21/1

Inferred function polymorphism

let-polymorphism ignores call context

Program analyses have polyvariance, e.g. nCFAbut are brittle to refactoring—arbitrary cutoff at n depth

Our approach: polyvariance approach withregular expression call strings

Limits polymorphism of recursion but nothing else

Similar to DCPA and ∆CFA, but optimistic

21/1

Inferred function polymorphism

let-polymorphism ignores call context

Program analyses have polyvariance, e.g. nCFAbut are brittle to refactoring—arbitrary cutoff at n depth

Our approach: polyvariance approach withregular expression call strings

Limits polymorphism of recursion but nothing elseSimilar to DCPA and ∆CFA, but optimistic

21/1

Outline

SemanticsPowerful records and record combinatorsFirst-class match clausesObject encoding using variants

Static typingOverviewUnion eliminationPolymorphism

Summary

22/1

Summary

Existing scripting languages are fundamentallyuntypeable

So, start with ML and add “principled flex”

TinyBang is our initial result—supports flex but more safely/declaratively

23/1

Summary

Existing scripting languages are fundamentallyuntypeable

So, start with ML and add “principled flex”

TinyBang is our initial result—supports flex but more safely/declaratively

23/1

Summary

Existing scripting languages are fundamentallyuntypeable

So, start with ML and add “principled flex”

TinyBang is our initial result—supports flex but more safely/declaratively

23/1

Bigger Picture: BigBang

TinyBang is the currently implemented core

Building a larger language: code in it

Also developing compile-time dispatchmethodology

http://big-bang-lang.org

24/1

Bigger Picture: BigBang

TinyBang is the currently implemented core

Building a larger language: code in it

Also developing compile-time dispatchmethodology

http://big-bang-lang.org

24/1

Bigger Picture: BigBang

TinyBang is the currently implemented core

Building a larger language: code in it

Also developing compile-time dispatchmethodology

http://big-bang-lang.org

24/1

Bigger Picture: BigBang

TinyBang is the currently implemented core

Building a larger language: code in it

Also developing compile-time dispatchmethodology

http://big-bang-lang.org

24/1

Related Work

Re-sealing objects generalizes [Fisher Bono ’98]

Unifying records and variants from [Pottier ’00]

Type-indexed records [Shields Meijer ’01]

First-class match generalizes [Blume et. al. ’06]

CDuce [Castagna et. al. ’14]

Similar expressiveness in several dimensionsCDuce: type checking; TinyBang: type inference

25/1

Questions?

26/1