types for flexible objects - swarthmore collegezpalmer/... · types for flexible objects building a...
TRANSCRIPT
![Page 1: Types for Flexible Objects - Swarthmore Collegezpalmer/... · Types for Flexible Objects Building a Typed Scripting Language Pottayil Harisanker Menon, Zachary Palmer, ... Python,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/1.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/2.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/3.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/4.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/5.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/6.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/7.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/8.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/9.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/10.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/11.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/12.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/13.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/14.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/15.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/16.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/17.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/18.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/19.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/20.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/21.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/22.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/23.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/24.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/25.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/26.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/27.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/28.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/29.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/30.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/31.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/32.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/33.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/34.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/35.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/36.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/37.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/38.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/39.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/40.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/41.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/42.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/43.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/44.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/45.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/46.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/47.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/48.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/49.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/50.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/51.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/52.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/53.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/54.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/55.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/56.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/57.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/58.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/59.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/60.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/61.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/62.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/63.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/64.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/65.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/66.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/67.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/68.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/69.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/70.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/71.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/72.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/73.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/74.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/75.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/76.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/77.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/78.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/79.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/80.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/81.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/82.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/83.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/84.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/85.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/86.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/87.jpg)
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,](https://reader034.vdocuments.net/reader034/viewer/2022042309/5ed5c001b81c373d543298b3/html5/thumbnails/88.jpg)
Questions?
26/1