types for flexible objects - swarthmore collegezpalmer/... · types for flexible objects building a...

88
Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer , Alexander Rozenshteyn, Scott F. Smith The Johns Hopkins University November 17th, 2014

Upload: others

Post on 29-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 2: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

Objective

Flexible OO language

+Static typing, inference, etc.

2/1

Page 3: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

Objective

Flexible OO language+

Static typing, inference, etc.

2/1

Page 4: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 5: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 6: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 7: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 8: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 9: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 10: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 11: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 12: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 13: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 14: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 15: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 16: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 17: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 18: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 19: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 20: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 21: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 22: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 23: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 24: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 25: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 26: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 27: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 28: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 29: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 30: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 31: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

Outline

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

Static typingOverviewUnion eliminationPolymorphism

Summary

8/1

Page 32: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 33: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 34: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 35: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 36: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 37: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 38: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

Outline

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

Static typingOverviewUnion eliminationPolymorphism

Summary

10/1

Page 39: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 40: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 41: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 42: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 43: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 44: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 45: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 46: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 47: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 48: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

Outline

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

Static typingOverviewUnion eliminationPolymorphism

Summary

13/1

Page 49: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 50: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 51: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 52: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 53: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 54: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 55: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 56: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 57: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

Outline

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

Static typingOverviewUnion eliminationPolymorphism

Summary

16/1

Page 58: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 59: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 60: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 61: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 62: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 63: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 64: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 65: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 66: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 67: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

Outline

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

Static typingOverviewUnion eliminationPolymorphism

Summary

18/1

Page 68: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 69: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 70: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 71: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 72: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 73: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

Outline

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

Static typingOverviewUnion eliminationPolymorphism

Summary

20/1

Page 74: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 75: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 76: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 77: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 78: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 79: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

Outline

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

Static typingOverviewUnion eliminationPolymorphism

Summary

22/1

Page 80: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 81: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 82: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 83: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 84: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 85: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 86: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 87: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

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

Page 88: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,

Questions?

26/1