Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf ·...
Transcript of Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf ·...
![Page 1: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/1.jpg)
Dependent Types for JavaScript
Ravi Chugh Ranjit Jhala
University of California, San Diego
David Herman
Mozilla Research
![Page 2: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/2.jpg)
Dependent Types for JavaScript
Why ?
Pervasive
Used at Massive Scale
![Page 3: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/3.jpg)
Why Add Types?
3
var x = {};x.f;x.f.f;
produces undefined rather than error…
… but this raises TypeError
![Page 4: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/4.jpg)
Why Add Types?
4
There Are Run-‐Gme Errors reading from undefined, applying non-‐funcGon values, …
Worse: Browsers Hide Them!
var x = {};x.f;x.f.f;
![Page 5: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/5.jpg)
Why Add Types?
5
Prevent Errors
Prevent the Unexpected
![Page 6: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/6.jpg)
Okay, But Who Cares?
6
Programmers JSLint, Closure Compiler, TypeScript, Dart
Browser Vendors Compete based on performance, security, reliability
Standards CommiSees AcGvely evolving JavaScript and Web standards
![Page 7: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/7.jpg)
Dependent Types for JavaScript
Why ?
Isn’t the Language Terrible?
![Page 8: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/8.jpg)
8
JavaScript
implicit global object
scope manipulaGon
var liYing
‘,,,’ == new Array(4)
![Page 9: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/9.jpg)
9
JavaScript
implicit global object
scope manipulaGon
var liYing
‘,,,’ == new Array(4)
objects prototypes
lambdas type-‐tests
![Page 10: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/10.jpg)
10
JavaScript
“The Good Parts”
implicit global object
scope manipulaGon
var liYing
‘,,,’ == new Array(4)
objects prototypes
lambdas type-‐tests
![Page 11: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/11.jpg)
11
JavaScript
“The Good Parts”
Prior Type
Systems
Key Idea: Use Logic!
Our Approach
![Page 12: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/12.jpg)
12
JavaScript
“The Good Parts”
Dependent JavaScript DJS
![Page 13: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/13.jpg)
13
Outline
MoGvaGon
Our Approach: Logic!
EvaluaGon
![Page 14: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/14.jpg)
14
typeof true // “boolean”
typeof 0.1 // “number” typeof 0 // “number”
typeof {} // “object” typeof [] // “object” typeof null // “object”
![Page 15: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/15.jpg)
15
typeof returns run-‐Gme “tags”
Tags are very coarse-‐grained types
“undefined”
“boolean”
“string”
“number”
“object”
“function”
![Page 16: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/16.jpg)
Refinement Types
16
{ x | p }“set of values x s.t. formula p is true”
{ n | tag(n) = “number” }Num ≡
{ v | tag(v) = “number” ∨ tag(v) = “boolean” }NumOrBool ≡
{ i | tag(i) = “number” ∧ integer(i) }Int ≡
{ x | true }Any ≡
![Page 17: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/17.jpg)
17
{ n | tag(n) = “number” }Num ≡
{ v | tag(v) = “number” ∨ tag(v) = “boolean” }NumOrBool ≡
{ i | tag(i) = “number” ∧ integer(i) }Int ≡
{ x | true }Any ≡
Refinement Types
SyntacGc Sugar for Common Types
![Page 18: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/18.jpg)
Refinement Types
18
3 :: { n | n = 3 } { n | n > 0 } 3 ::{ n | tag(n) = “number” ∧ integer(n) } 3 ::{ n | tag(n) = “number” } 3 ::
![Page 19: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/19.jpg)
Refinement Types
19
{ n | n = 3 } { n | n > 0 } { n | tag(n) = “number” ∧ integer(n) } { n | tag(n) = “number” }
<:<:<:<:
Subtyping is ImplicaGon
![Page 20: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/20.jpg)
Refinement Types
20
{ n | n = 3 } { n | n > 0 } { n | tag(n) = “number” ∧ integer(n) } { n | tag(n) = “number” }
⇒⇒⇒⇒
Subtyping is ImplicaGon
![Page 21: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/21.jpg)
21
Arrays Prototypes Mutable Objects Duck Typing Tag-‐Tests
![Page 22: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/22.jpg)
22
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
negate( )
!true
true
// false
![Page 23: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/23.jpg)
23
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
negate( )
0 - 2 // -2
2
![Page 24: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/24.jpg)
24
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
negate( )
0 - [] // 0WAT?!
[]
![Page 25: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/25.jpg)
25
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;} Use types to prevent implicit coercion
(-) :: (Num,Num) → Num
![Page 26: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/26.jpg)
26
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
FuncGon type annotaGon inside
comments
//: negate :: (x:Any) → Any
![Page 27: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/27.jpg)
27
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
//: negate :: (x:Any) → Any
so negaGon is well-‐typed
x is boolean...
DJS is Path SensiGve
✓
![Page 28: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/28.jpg)
28
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
//: negate :: (x:Any) → Any
x is arbitrary non-‐boolean value…so DJS signals error!
✗
DJS is Path SensiGve
✗
![Page 29: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/29.jpg)
29
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
//: negate :: (x:NumOrBool) → Any
✓
![Page 30: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/30.jpg)
30
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
//: negate :: (x:NumOrBool) → Any
this Gme,x is a number… ✓ so subtracGon is well-‐typed
![Page 31: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/31.jpg)
31
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
//: negate :: (x:NumOrBool) → Any✓
but return type is imprecise
![Page 32: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/32.jpg)
32
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
//: negate :: (x:NumOrBool) → NumOrBool
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
✓
![Page 33: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/33.jpg)
33
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
/*: negate :: (x:NumOrBool) → { v | tag(v) = tag(x) } */var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
✓
output type depends on input value
![Page 34: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/34.jpg)
34
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
/*: negate :: (x:NumOrBool) → { v | tag(v) = tag(x) } */
Programmer chooses degree of precision in specificaGon
![Page 35: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/35.jpg)
35
Duck Typing Arrays Prototypes Mutable Objects Tag-‐Tests
if (duck.quack) return “Duck says ” + duck.quack();else return “This duck can’t quack!”;
What is “Duck Typing”?
![Page 36: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/36.jpg)
36
Duck Typing Arrays Prototypes Mutable Objects Tag-‐Tests
if (duck.quack) return “Duck says ” + duck.quack();else return “This duck can’t quack!”;
What is “Duck Typing”?
(+) :: (Num,Num) → Num(+) :: (Str,Str) → Str
![Page 37: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/37.jpg)
37
Duck Typing Arrays Prototypes Mutable Objects Tag-‐Tests
if (duck.quack) return “Duck says ” + duck.quack();else return “This duck can’t quack!”;
What is “Duck Typing”?
Can dynamically test the presence of a method
but not its type
![Page 38: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/38.jpg)
38
Duck Typing Arrays Prototypes Mutable Objects Tag-‐Tests
if (duck.quack) return “Duck says ” + duck.quack();else return “This duck can’t quack!”;
{ d | tag(d) = “Dict” ∧ { v | has(d,“quack”) ⇒{ v | sel(d,“quack”) :: Unit → Str }
Operators from McCarthy theory of arrays
![Page 39: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/39.jpg)
39
Duck Typing Arrays Prototypes Mutable Objects Tag-‐Tests
if (duck.quack) return “Duck says ” + duck.quack();else return “This duck can’t quack!”;
{ d | tag(d) = “Dict” ∧ { v | has(d,“quack”) ⇒{ v | sel(d,“quack”) :: Unit → Str }
Call produces Str, so concat well-‐typed
✓
![Page 40: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/40.jpg)
40
Mutable Objects Arrays Prototypes Tag-‐Tests Duck Typing
var x = {};x.f = 7;x.f + 2;
x0:Empty
x1:{d|d = upd(x0,“f”,7)}
DJS is Flow SensiGve
McCarthy operator DJS verifies that x.f is definitely a number
![Page 41: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/41.jpg)
41
Mutable Objects Arrays Prototypes Tag-‐Tests Duck Typing
var x = {};x.f = 7;x.f + 2;
x0:Empty
x1:{d|d = upd(x0,“f”,7)}
DJS is Flow SensiGve
Strong updates to singleton objects
Weak updates to collecGons of objects
![Page 42: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/42.jpg)
42
Arrays Prototypes Tag-‐Tests Duck Typing Mutable Objects
![Page 43: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/43.jpg)
43
Arrays Prototypes Tag-‐Tests Duck Typing Mutable Objects
Typical “Dynamic” Features
![Page 44: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/44.jpg)
44
Arrays Prototypes Tag-‐Tests Duck Typing Mutable Objects
Typical “Dynamic” Features
JavaScript
tl;drHarder, but
DJS handles them
![Page 45: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/45.jpg)
45
Prototypes Arrays Tag-‐Tests Mutable Objects Duck Typing
child
parent
...
grandpa
null
Upon construcGon, each object links to a prototype object
![Page 46: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/46.jpg)
46
Prototypes Arrays Tag-‐Tests Mutable Objects Duck Typing
If child contains k, then Read k from child
Else if parent contains k, then Read k from parent
Else if grandpa contains k, then Read k from grandpa
Else if …
Else Return undefined
child
parent
...
grandpa
null
var k = “first”; child[k];SemanGcs of Key Lookup
![Page 47: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/47.jpg)
...
47
Prototypes Arrays Tag-‐Tests Mutable Objects Duck Typing
child
parent
grandpa
null
H
(Rest of Heap)
var k = “first”; child[k];SemanGcs of Key Lookup
If child contains k, then Read k from child
Else if parent contains k, then Read k from parent
Else if grandpa contains k, then Read k from grandpa
Else if …
Else Return undefined
{ v | if has(child,k) then{ v | ifv = sel(child,k)
![Page 48: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/48.jpg)
...
48
Prototypes Arrays Tag-‐Tests Mutable Objects Duck Typing
child
parent
grandpa
null
H
(Rest of Heap)
var k = “first”; child[k];SemanGcs of Key Lookup
If child contains k, then Read k from child
Else if parent contains k, then Read k from parent
Else if grandpa contains k, then Read k from grandpa
Else if …
Else Return undefined
{ v | if has(child,k) then{ v | ifv = sel(child,k)
{ v | else if has(parent,k) then{ v | ifv = sel(parent,k)
![Page 49: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/49.jpg)
If child contains k, then Read k from child
Else if parent contains k, then Read k from parent
Else if grandpa contains k, then Read k from grandpa
Else if …
Else Return undefined
...
49
Prototypes Arrays Tag-‐Tests Mutable Objects Duck Typing
{ v | if has(child,k) then{ v | ifv = sel(child,k)
{ v | else if has(parent,k) then{ v | ifv = sel(parent,k)
child
parent
grandpa ???
null
H
(Rest of Heap)
var k = “first”; child[k];SemanGcs of Key Lookup
![Page 50: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/50.jpg)
...
50
Prototypes Arrays Tag-‐Tests Mutable Objects Duck Typing
{ v | if has(child,k) then{ v | ifv = sel(child,k)
{ v | else if has(parent,k) then{ v | ifv = sel(parent,k)
{ v | else{ v | ifv = HeapSel(H,grandpa,k)) }
child
parent
grandpa ???
null
H
(Rest of Heap)
var k = “first”; child[k];SemanGcs of Key Lookup
Abstract predicate to summarize the unknown porIon
of the prototype chain
![Page 51: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/51.jpg)
...
51
Prototypes Arrays Tag-‐Tests Mutable Objects Duck Typing
{ v | if has(child,k) then{ v | ifv = sel(child,k)
{ v | else if has(parent,k) then{ v | ifv = sel(parent,k)
{ v | else{ v | ifv = HeapSel(H,grandpa,k)) }
{ “first” : “John” }child
parent { “first” : “Ida” , “last” : “McCarthy” }
grandpa ???
null
H
(Rest of Heap)
<:
{ v | v = “John” }
var k = “first”; child[k];
![Page 52: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/52.jpg)
...
52
Prototypes Arrays Tag-‐Tests Mutable Objects Duck Typing
{ v | if has(child,k) then{ v | ifv = sel(child,k)
{ v | else if has(parent,k) then{ v | ifv = sel(parent,k)
{ v | else{ v | ifv = HeapSel(H,grandpa,k)) }
{ “first” : “John” }child
parent { “first” : “Ida” , “last” : “McCarthy” }
grandpa ???
null
H
(Rest of Heap)
var k = “last”; child[k];
<:
{ v | v = “McCarthy” }
![Page 53: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/53.jpg)
53
Prototypes Arrays Tag-‐Tests Mutable Objects Duck Typing
Key Idea:
Reduce prototype semanGcs to decidable
theory of arrays
Prototype Chain Unrolling
![Page 54: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/54.jpg)
54
Arrays Tag-‐Tests Prototypes Mutable Objects Duck Typing
var nums = [0,1,2]while (…) { nums[nums.length] = 17}
A finite tuple…
… extended to unbounded collecGon
![Page 55: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/55.jpg)
55
Arrays Tag-‐Tests Prototypes Mutable Objects Duck Typing
var nums = [0,1,2]while (…) { nums[nums.length] = 17}
delete nums[1]
for (i = 0; i < nums.length; i++) sum += nums[i]
A “hole” in the array
Missing element within “length”
![Page 56: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/56.jpg)
56
Arrays Tag-‐Tests Prototypes Mutable Objects Duck Typing
Track types, “packedness,” and length of arrays where possible
{ a | a :: Arr(T) { a |∧ packed(a)
{ a |∧ len(a) = 10 }
X T T T T … X … …
T? T? T? T? T? … T? … …
T? ≡ { x | T(x) ∨ x = undefined }
-‐1 0 1 2 len(a)
X ≡ { x | x = undefined }
![Page 57: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/57.jpg)
57
Arrays Tag-‐Tests Prototypes Mutable Objects Duck Typing
{ a | a :: Arr(Any) { a | ∧ packed(a) ∧ len(a) = 2
{ a | ∧ Int(sel(a,0)) { a | ∧ Str(sel(a,1)) }
Encode tuples as arrays
var tup = [17, “cacti”]
![Page 58: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/58.jpg)
58
Arrays Tag-‐Tests Prototypes Mutable Objects Duck Typing
var tup = [17, “cacti”]tup[tup.length] = true
{ a | a :: Arr(Any) { a | ∧ packed(a) ∧ len(a) = 3
{ a | ∧ … }
![Page 59: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/59.jpg)
59
Arrays Tag-‐Tests Prototypes Mutable Objects Duck Typing
DJS handles other quirks:
Special length property Array.prototype Non-‐integer keys
![Page 60: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/60.jpg)
60
Tag-‐Tests Prototypes Mutable Objects Duck Typing Arrays
![Page 61: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/61.jpg)
What About eval?
61
Arbitrary code loading
…
eval(“…”);
…
Old Types
![Page 62: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/62.jpg)
What About eval?
62
…
eval(“…”);//: #assume …
Old Types
New Types New Types
“Contract Checking” at Run-‐Gme aka “Gradual Typing”
![Page 63: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/63.jpg)
Recap of DJS Techniques
63
Logic!
Path and Flow SensiGvity
Prototype Unrolling
SyntacGc Sugar
![Page 64: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/64.jpg)
64
Outline
MoGvaGon
Our Approach: Logic!
EvaluaGon
![Page 65: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/65.jpg)
65
“The Good Parts”
objects prototypes
lambdas type-‐tests
arrays
13 Benchmarks
![Page 66: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/66.jpg)
66
“The Good Parts”
objects prototypes
lambdas type-‐tests
arrays
13 Benchmarks
![Page 67: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/67.jpg)
67
“The Good Parts”
objects prototypes
lambdas type-‐tests
arrays
13 Benchmarks
Four inheritance paSerns from Crockford’s JS: The Good Parts
![Page 68: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/68.jpg)
68
“The Good Parts”
objects prototypes
lambdas type-‐tests
arrays
13 Benchmarks
Array-‐manipulaGng examples from SunSpider
![Page 69: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/69.jpg)
69
“The Good Parts”
objects prototypes
lambdas type-‐tests
arrays
13 Benchmarks
typeOf() funcGon to replace typeof from Closure Compiler
![Page 70: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/70.jpg)
70
“The Good Parts”
objects prototypes
lambdas type-‐tests
arrays
13 Benchmarks
Well-‐typed programs don’t have run-‐Gme errors
![Page 71: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/71.jpg)
Desugared Program
DJS Program
Desugarer Based on Guha et al.
[ECOOP 2010]
ImplementaIon
JavaScript λ-‐Calculus + References + Prototypes
![Page 72: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/72.jpg)
Desugared Program
Z3 SMT Solver
Type Checker
DJS Program
Desugarer Based on Guha et al.
[ECOOP 2010]
ImplementaIon Programmer Chooses Warnings or Errors
Local Type Inference
Subtyping w/o Z3 If Possible
![Page 73: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/73.jpg)
AnnotaIon Burden
+= ~300 LOC to start += ~100 LOC annotaGons
=+ ~400 LOC total
(Improved since paper)
33% AnnotaGon Overhead
Common Cases Simplified via SyntacGc Sugar and Local Type Inference
![Page 74: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/74.jpg)
Performance (Improved since paper)
Total benchmark suite:
~10 seconds ~1100 Z3 queries
11/13 benchmarks in 0-‐1 s
Common Cases Fast via SyntacGc Reasoning When Possible
![Page 75: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/75.jpg)
Future Work
Syntax, Inference, Performance
Larger Examples
Type Checker in JS; Run in Browser
IDE Support for Refactoring, etc.
75
![Page 76: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/76.jpg)
76
JavaScript
“The Good Parts”
Types via Logic! DJS
![Page 77: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/77.jpg)
Thanks!
77
ravichugh.com/djs
D!::!PS: I’m on the Job Market
![Page 78: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/78.jpg)
Extra Slides
78
![Page 79: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/79.jpg)
Coq
refinement types
dependent types
Expressivity
“Usability”
JS
+ nested refinements
System D [POPL ’12]
DJS
Dependent JavaScript [this paper]
syntacGc types
…
occurrence types
∨, ∧
F≤
![Page 80: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/80.jpg)
System D [POPL 2012] + Types for JS PrimiGves + Strong Updates + Quirky JS Arrays + Prototype Inheritance
Dependent JavaScript (DJS)
![Page 81: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/81.jpg)
81
/*: x:NumOrBool → {ite Num(x) Num(v) Bool(v)} */function negate(x) { x = (typeof x == “number”) ? 0 – x : !x return x}
/*: x:Any → {v iff falsy(x)} */function negate(x) { x = (typeof x == “number”) ? 0 – x : !x return x}
FuncIon Types and Objects {p} ≡ {v|p}
![Page 82: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/82.jpg)
82
ObjHas(d,k,H,d’) ≡ has(d,k) ∨ HeapHas(H,d’,k)
/*: x:Ref / [x |-> d:Dict |> x.pro] → {v iff ObjHas(d,”f”,curHeap,x.pro)} / sameHeap */function hasF(x) { return “f” in x}
x:T1/H1 → T2/H2
FuncIon Types and Objects
output type input heap
input type output heap
![Page 83: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/83.jpg)
83
ObjSel(d,k,H,d’) ≡ ite has(d,k) sel(d,k) HeapSel(H,d’,k)
x:T1/H1 → T2/H2
FuncIon Types and Objects
output type input heap
input type output heap
/*: x:Ref / [x |-> d:Dict |> x.pro] → {v = ObjSel(d,”f”,curHeap,x.pro)} / sameHeap */function readF(x) { return x.f}
![Page 84: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/84.jpg)
84
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
negate( )
0 - “2” // -2
“2”
Whoa, but perhaps okay…
![Page 85: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/85.jpg)
85
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
negate( )
0 - undefined // NaN
undefinedError would be nicer,
but okay…
![Page 86: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/86.jpg)
86
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
negate( )
0 - {} // NaN
{}
Seems about right…
![Page 87: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/87.jpg)
87
Tag-‐Tests Arrays Prototypes Mutable Objects Duck Typing
var negate = function(x) { if (typeof x == “boolean”) return !x; else return 0 - x;}
negate( )
0 - [] // 0
[]
WAT?!
negate has good intenGons But too many corner cases in JS semanGcs!
![Page 88: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/88.jpg)
88
Duck Typing Arrays Prototypes Mutable Objects Tag-‐Tests
if (duck.quack) return “Duck says ” + duck.quack();else return “This duck can’t quack!”;
{ d | tag(d) = “Dict” ∧ { v | has(d,“quack”) ⇒{ v | sel(d,“quack”) :: Unit → Str }
FuncGon types nested inside refinements
[POPL 2012]
![Page 89: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/89.jpg)
89
Arrays Prototypes Tag-‐Tests Duck Typing
var x = {};x.f;x.f.f; Programmer configures
DJS to report either warnings or errors for:
1) Possible unbound keys
Mutable Objects
![Page 90: Dependent’ Types’for’JavaScript’people.cs.uchicago.edu/.../talks/oopsla12-djs.pptx.pdf · · 2015-01-2014% typeof true // “boolean” typeof 0.1 // “number” typeof](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9b825b9318800fa6ad1e3/html5/thumbnails/90.jpg)
90
Arrays Prototypes Tag-‐Tests Duck Typing
var x = {};x.f;x.f.f; Programmer configures
DJS to report either warnings or errors for:
1) Possible unbound keys
2) Possible run-‐Gme errors
Mutable Objects