the ins and outs of gradual type inference

95
The Ins and Outs of Gradual Type Inference Avik Chaudhuri Basil Hosmer Adobe Systems Aseem Rastogi Stony Brook University

Upload: liseli

Post on 23-Feb-2016

38 views

Category:

Documents


0 download

DESCRIPTION

The Ins and Outs of Gradual Type Inference. Aseem Rastogi Stony Brook University. Avik Chaudhuri Basil Hosmer Adobe Systems. Gradually Typed Code. e.g., a Flash application in ActionScript. Statically Typed Code fully annotated w ith static types. Dynamically Typed Code - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Ins and Outs of  Gradual Type Inference

The Ins and Outs of Gradual Type Inference

Avik Chaudhuri Basil HosmerAdobe Systems

Aseem RastogiStony Brook University

Page 2: The Ins and Outs of  Gradual Type Inference

2

Gradually Typed Code

Dynamically Typed Code

missing annotations = dynamic types

Statically Typed Code

fully annotated with static types

type casts

e.g., a Flash applicationin ActionScript

Page 3: The Ins and Outs of  Gradual Type Inference

Evolution of Scripts to Programs

Dynamically Typed“Script”

Statically Typed“Program”

incrementally addingannotations for performance

Gradual Typing

in our experience, a

fantasy

missing annotations = dynamic types by default, rather than as fallback

Page 4: The Ins and Outs of  Gradual Type Inference

4

Annoying Trade-Off

Annotation Burden

Performance Penalty

Can this trade-off be eliminated?

Page 5: The Ins and Outs of  Gradual Type Inference

Our Vision of Evolution Process

Partially Typed“Script”

Partially Untyped“Program”

Infer Types

Annotate / Restructure

Type Inference: Key Ingredient for Evolution

Page 6: The Ins and Outs of  Gradual Type Inference

6

Our Contributions• Type Inference Algorithm for

Gradually Typed LanguagesGradual Type

Inference

• Improve Performance of Flash Applications

Practical Motivation

• Backward CompatibilityGoal

• Increase Safety / Eliminate ErrorsNon Goal

… by eliminating as many type casts as

possible

programs should not break, even when run in arbitrary environments

must admit reasoning outside

the static type system

Page 7: The Ins and Outs of  Gradual Type Inference

7

Gradual Type Inference

Page 8: The Ins and Outs of  Gradual Type Inference

8

A Gradually Typed Program

function foo(n:Number){

var s = 0;var i = s;while(i < n) {

s = s + i;i = i + 1;

}return s;

}

Page 9: The Ins and Outs of  Gradual Type Inference

9

Gradual Typing: No Type Inference

function foo(n:Number):*{

var s:* = 0;var i:* = s;while(i < n) {

s = s + i;i = i + 1;

}return s;

} Missing Type = Dynamic Type

Page 10: The Ins and Outs of  Gradual Type Inference

10

Gradual Typing: No Type Inference

function foo(n:Number):*{

var s:* = Number * ⟨ ▷ ⟩ 0;var i:* = s;while(i < n) {

s = s + i;i = i + 1;

}return s;

}

< : (Number, Number) Boolean+ : (Number, Number) Number

Type Casts

Missing Type = Dynamic Type

Page 11: The Ins and Outs of  Gradual Type Inference

11

Gradual Typing: No Type Inference

function foo(n:Number):*{

var s:* = Number * ⟨ ▷ ⟩ 0;var i:* = s;while( * Number⟨ ▷ ⟩ i < n) {

s = s + i;i = i + 1;

}return s;

}

< : (Number, Number) Boolean+ : (Number, Number) Number

Missing Type = Dynamic Type

Page 12: The Ins and Outs of  Gradual Type Inference

12

Gradual Typing: No Type Inference

function foo(n:Number):*{

var s:* = Number * ⟨ ▷ ⟩ 0;var i:* = s;while( * Number⟨ ▷ ⟩ i < n) {

s = Number * ⟨ ▷ ⟩ ( * Number ⟨ ▷ ⟩ s + * ⟨ ▷Number ⟩ i);

i = i + 1;}return s;

}

< : (Number, Number) Boolean+ : (Number, Number) Number

Missing Type = Dynamic Type

Page 13: The Ins and Outs of  Gradual Type Inference

13

Gradual Typing: No Type Inference

function foo(n:Number):*{

var s:* = Number * ⟨ ▷ ⟩ 0;var i:* = s;while( * Number⟨ ▷ ⟩ i < n) {

s = Number * ⟨ ▷ ⟩ ( * Number ⟨ ▷ ⟩ s + * ⟨ ▷Number ⟩ i);

i = Number * ⟨ ▷ ⟩ ( * Number ⟨ ▷ ⟩ i + 1);}return s;

}

< : (Number, Number) Boolean+ : (Number, Number) Number

Missing Type = Dynamic Type

Page 14: The Ins and Outs of  Gradual Type Inference

14

Gradual Typing: No Type Inference

function foo(n:Number):*{

var s:* = Number * ⟨ ▷ ⟩ 0;var i:* = s;while( * Number⟨ ▷ ⟩ i < n) {

s = Number * ⟨ ▷ ⟩ ( * Number ⟨ ▷ ⟩ s + * ⟨ ▷Number ⟩ i);

i = Number * ⟨ ▷ ⟩ ( * Number ⟨ ▷ ⟩ i + 1);}return s;

}

Performance Penalty

Missing Type = Dynamic Type

Page 15: The Ins and Outs of  Gradual Type Inference

15

Gradual Typing: Type Inference

function foo(n:Number):Foo!{

var s:S = 0;var i:I = s;while(i < n) {

s = s + i;i = i + 1;

}return s;

}

Missing Type = Unknown Type

Missing Type = Dynamic Type

Unknown types represented as type variables,

solved to static types where possible,

dynamic type as fallback

Page 16: The Ins and Outs of  Gradual Type Inference

16

Architecture of Type Inference

Program Annotated With Type Variables

Program With Coercions

Flow Relation Over Types

Solutions of Type Variables

as Types

Compilation

Closure Computation

Solution Derivation

Page 17: The Ins and Outs of  Gradual Type Inference

17

Coercions

… are of the form T1 T▷ 2

“term of type T1 flows into context of type T2”

T ▷ X is an inflow for type variable XX T is an ▷ outflow for type variable X

Page 18: The Ins and Outs of  Gradual Type Inference

18

Generating Coercions

function foo(n:Number):Foo!{

var s:S = Number ⟨ ▷ S ⟩ 0;var i:I = s;while(i < n) {

s = s + i;i = i + 1;

}return s;

}

Number ▷ S

Page 19: The Ins and Outs of  Gradual Type Inference

19

Generating Coercions

function foo(n:Number):Foo!{

var s:S = Number ⟨ ▷ S 0;⟩var i:I = ⟨S ▷ I⟩ s;while(i < n) {

s = s + i;i = i + 1;

}return s;

}

Number ▷ S

S ▷ I

Page 20: The Ins and Outs of  Gradual Type Inference

20

Generating Coercions

function foo(n:Number):Foo!{

var s:S = Number ⟨ ▷ S 0;⟩var i:I = ⟨S ▷ I s;⟩while(⟨I Number▷ ⟩ i < n) {

s = s + i;i = i + 1;

}return s;

}

Number ▷ S

S ▷ I

I Number▷

Page 21: The Ins and Outs of  Gradual Type Inference

21

Generating Coercions

function foo(n:Number):Foo!{

var s:S = Number ⟨ ▷ S 0;⟩var i:I = ⟨S ▷ I s;⟩while(⟨I Number i < n) {▷ ⟩

s = Number ⟨ ▷ S> (⟨S Number ▷ ⟩ s + ⟨I ▷Number ⟩ i);

i = i + 1;}return s;

}

Number ▷ S

I Number▷

S ▷ I

S Number▷

Page 22: The Ins and Outs of  Gradual Type Inference

22

Generating Coercions

function foo(n:Number):Foo!{

var s:S = Number ⟨ ▷ S 0;⟩var i:I = ⟨S ▷ I s;⟩while(⟨I Number i < n) {▷ ⟩

s = Number ⟨ ▷ S (⟩ ⟨S Number s + ▷ ⟩ ⟨I ▷Number i);⟩

i = Number ⟨ ▷ I ⟩ (⟨I Number▷ ⟩ i + 1);}return s;

}

Number ▷ S

I Number▷

S Number▷

S ▷ I

Number ▷ I

Page 23: The Ins and Outs of  Gradual Type Inference

23

Generating Coercions

function foo(n:Number):Foo!{

var s:S = Number ⟨ ▷ S 0;⟩var i:I = ⟨S ▷ I s;⟩while(⟨I Number i < n) {▷ ⟩

s = Number ⟨ ▷ S (⟩ ⟨S Number s + ▷ ⟩ ⟨I ▷Number i);⟩

i = Number ⟨ ▷ I (⟩ ⟨I Number i + 1);▷ ⟩}return ⟨S ▷ Foo! ⟩ s;

}

S ▷ Foo!

Number ▷ S

I Number▷

S Number▷

S ▷ I

Number ▷ I

Page 24: The Ins and Outs of  Gradual Type Inference

24

Solving for Type Variables

X

Inflows Outflows

Page 25: The Ins and Outs of  Gradual Type Inference

25

Solving in a Static Type System

X

Inflows

L.U.B.

Outflows

G.L.B. Solution(X)Precision Safety

Page 26: The Ins and Outs of  Gradual Type Inference

26

Solving in a Gradual Type System

X

Inflows

L.U.B. Solution(X)Precision

Page 27: The Ins and Outs of  Gradual Type Inference

27

Solving in a Gradual Type System

X

Inflows

L.U.B. Solution(X)

Recall: eliminating errors

is a non-goal

Precision

Page 28: The Ins and Outs of  Gradual Type Inference

28

function foo(n:Number):Foo!{

var s:S = Number ⟨ ▷ S 0;⟩var i:I = ⟨S ▷ I s;⟩while(⟨I Number i < n) {▷ ⟩

s = Number ⟨ ▷ S (⟩ ⟨S Number s + ▷ ⟩ ⟨I ▷Number i);⟩

i = Number ⟨ ▷ I (⟩ ⟨I Number i + 1);▷ ⟩}return ⟨S ▷ Foo! s;⟩

}

Solving for Type Variables

S = Number

S ▷ Foo!

Number ▷ S

I Number▷

S Number▷

S ▷ I

Number ▷ I

Page 29: The Ins and Outs of  Gradual Type Inference

29

function foo(n:Number):Foo!{

var s:S = Number ⟨ ▷ S 0;⟩var i:I = ⟨S ▷ I s;⟩while(⟨I Number i < n) {▷ ⟩

s = Number ⟨ ▷ S (⟩ ⟨S Number s + ▷ ⟩ ⟨I ▷Number i);⟩

i = Number ⟨ ▷ I (⟩ ⟨I Number i + 1);▷ ⟩}return ⟨S ▷ Foo! s;⟩

}

Solving for Type Variables

I = Number S⨆ = Number

S ▷ Foo!

Number ▷ S

I Number▷

S Number▷

S ▷ I

Number ▷ I

S = Number

Page 30: The Ins and Outs of  Gradual Type Inference

30

function foo(n:Number):Foo!{

var s:S = Number ⟨ ▷ S 0;⟩var i:I = ⟨S ▷ I s;⟩while(⟨I Number i < n) {▷ ⟩

s = Number ⟨ ▷ S (⟩ ⟨S Number s + ▷ ⟩ ⟨I ▷Number i);⟩

i = Number ⟨ ▷ I (⟩ ⟨I Number i + 1);▷ ⟩}return ⟨S ▷ Foo! s;⟩

}

Solving for Type Variables

Foo! = S=

Number

S ▷ Foo!

Number ▷ S

I Number▷

S Number▷

S ▷ I

Number ▷ I

I = Number S⨆ = Number

S = Number

Page 31: The Ins and Outs of  Gradual Type Inference

31

function foo(n:Number):Foo!{

var s:S = Number ⟨ ▷ S 0;⟩var i:I = ⟨S ▷ I s;⟩while(⟨I Number i < n) {▷ ⟩

s = Number ⟨ ▷ S (⟩ ⟨S Number s + ▷ ⟩ ⟨I ▷Number i);⟩

i = Number ⟨ ▷ I (⟩ ⟨I Number i + 1);▷ ⟩}return ⟨S ▷ Foo! s;⟩

}

Solving for Type Variables

S ▷ Foo!

Number ▷ S

I Number▷

S Number▷

S ▷ I

Number ▷ I

I = Number S⨆ = Number

S = Number

Outflows ignored

Foo! = S = Number

Page 32: The Ins and Outs of  Gradual Type Inference

32

Annotated Program

function foo(n:Number):Number{

var s:Number = Number Number⟨ ▷ ⟩ 0;var i:Number = Number Number⟨ ▷ ⟩ s;while( Number Number⟨ ▷ ⟩ i < n) {

s = Number Number⟨ ▷ ⟩ ( Number Number⟨ ▷ ⟩ s + Number⟨ ▷Number⟩ i);

i = Number Number⟨ ▷ ⟩ ( Number Number⟨ ▷ ⟩ i + 1);

}return Number Number ⟨ ▷ ⟩ s;

}

Page 33: The Ins and Outs of  Gradual Type Inference

33

Annotated Program: No Casts

function foo(n:Number):Number{

var s:Number = 0;var i:Number = s;while(i < n) {

s = s + i;i = i + 1;

}return s;

}

Page 34: The Ins and Outs of  Gradual Type Inference

34

Summarizing… Key Idea (1)

Consider Only Inflows to Solve Type Variables

Page 35: The Ins and Outs of  Gradual Type Inference

35

Type Inference for Higher Order Types

var x:X = function(y:Number):Number { … };If(b) {

x = function(y:Boolean):Number { … };x(true);

}

Page 36: The Ins and Outs of  Gradual Type Inference

36

Type Inference for Higher Order Types

var x:X = function(y:Number):Number { … };If(b) {

x = function(y:Boolean):Number { … };x(true);

}Boolean Number ▷ X

Number Number ▷ X

Page 37: The Ins and Outs of  Gradual Type Inference

37

Type Inference for Higher Order Types

var x:X = function(y:Number):Number { … };If(b) {

x = function(y:Boolean):Number { … };x(true);

}Boolean Number ▷ X

Number Number ▷ X

X = Number⊥

Page 38: The Ins and Outs of  Gradual Type Inference

38

Type Inference for Higher Order Types

var x:X = function(y:Number):Number { … };If(b) {

x = function(y:Boolean):Number { … };x(true);

}Boolean Number ▷ X

Number Number ▷ X

X = Number⊥Unsound

Page 39: The Ins and Outs of  Gradual Type Inference

39

Type Inference for Higher Order Types

var x:X = function(y:Number):Number { … };If(b) {

x = function(y:Boolean):Number { … };x(true);

}Boolean Number ▷ X

Number Number ▷ X

X = * Number

Page 40: The Ins and Outs of  Gradual Type Inference

40

Type Inference for Higher Order Types

var x:X = function(y:Number):Number { … };If(b) {

x = function(y:Boolean):Number { … };x(true);

}Boolean Number ▷ X

Number Number ▷ X

X = * NumberImprecise

Page 41: The Ins and Outs of  Gradual Type Inference

41

Type Inference for Higher Order Types

var x:X = function(y:Number):Number { … };If(b) {

x = function(y:Boolean):Number { … };x(true);

}

X = Boolean Number

Boolean Number ▷ X

Number Number ▷ X

Page 42: The Ins and Outs of  Gradual Type Inference

42

Higher Order Types: Kinds

Page 43: The Ins and Outs of  Gradual Type Inference

43

Suppose that solution of X is of the form _ _

function type

Higher Order Types: Kinds

Page 44: The Ins and Outs of  Gradual Type Inference

44

The kind of X under this structure is X? X!

Higher Order Types: Kinds

Suppose that solution of X is of the form _ _

Page 45: The Ins and Outs of  Gradual Type Inference

45

Infer X? and X! based on their inflows

function applications

of X

Higher Order Types: Kinds

The kind of X under this structure is X? X!

Suppose that solution of X is of the form _ _

Page 46: The Ins and Outs of  Gradual Type Inference

46

Infer X? and X! based on their Inflows

Higher Order Types: Kinds

The kind of X under this structure is X? X!

Suppose that solution of X is of the form _ _

“a kind for every type constructor”

all parts of a higher-order type solved based on inflows

≈ naïve subtyping

Page 47: The Ins and Outs of  Gradual Type Inference

47

var x:X = function(y:Number):Number { … };if(b) {

x = function(y:Boolean):Number { … };x(true);

}

Boolean Number ▷XNumber Number ▷X

Boolean Number ▷ X? X!

Number Number ▷ X? X!

X? X! ▷ X

Type Inference for Higher Order Types

Page 48: The Ins and Outs of  Gradual Type Inference

48

var x:X = function(y:Number):Number { … };if(b) {

x = function(y:Boolean):Number { … };x(true);

}Boolean ▷ X?

Boolean Number ▷XNumber Number ▷X

Boolean Number ▷ X? X!Number Number ▷ X? X!

X? X! ▷ X

Type Inference for Higher Order Types

Page 49: The Ins and Outs of  Gradual Type Inference

49

var x:X = function(y:Number):Number { … };if(b) {

x = function(y:Boolean):Number { … };x(true);

}

X? ▷BooleanNumber ▷ X!

Boolean ▷X?

Boolean Number ▷XNumber Number ▷X

Boolean Number ▷ X? X!

Type Inference for Higher Order Types

Number Number ▷ X? X!

X? X! ▷ X

Page 50: The Ins and Outs of  Gradual Type Inference

50

var x:X = function(y:Number):Number { … };if(b) {

x = function(y:Boolean):Number { … };x(true);

}

Number ▷ X!

X? Number▷

X? Boolean▷

Boolean Number ▷XNumber Number ▷X

Number Number ▷ X? X!

Type Inference for Higher Order Types

Boolean Number ▷ X? X!

Boolean ▷ X?

X? X! ▷ X

Page 51: The Ins and Outs of  Gradual Type Inference

51

var x:X = function(y:Number):Number { … };if(b) {

x = function(y:Boolean):Number { … };x(true);

}

Number ▷ X!

X? Number▷

X? Boolean▷

Boolean ▷ X?

Boolean Number ▷XNumber Number ▷X

X? = Boolean

Type Inference for Higher Order Types

Boolean Number ▷ X? X!

Number Number ▷ X? X!

X? X! ▷ X

Page 52: The Ins and Outs of  Gradual Type Inference

52

var x:X = function(y:Number):Number { … };if(b) {

x = function(y:Boolean):Number { … };x(true);

}

X! = Number Number ▷ X!

X? Number▷

Boolean Number ▷XNumber Number ▷X

X? = Boolean

Type Inference for Higher Order Types

Boolean Number ▷ X? X!Number Number ▷ X? X!

X? X! ▷ X

X? Boolean▷

Boolean ▷ X?

Page 53: The Ins and Outs of  Gradual Type Inference

53

var x:X = function(y:Number):Number { … };if(b) {

x = function(y:Boolean):Number { … };x(true);

}

Solution(X) = L.U.B. of Inflowing Kinds

Boolean Number ▷XNumber Number ▷X

X! = Number

Type Inference for Higher Order Types

X? = Boolean

Number ▷ X!

X? Number▷

Boolean Number ▷ X? X!Number Number ▷ X? X!

X? X! ▷ X

X? Boolean▷

Boolean ▷ X?

Page 54: The Ins and Outs of  Gradual Type Inference

var x:X = function(y:Number):Number { … };if(b) {

x = function(y:Boolean):Number { … };x(true);

} X = X? X!

Type Inference for Higher Order Types

X! = Number

X? = Boolean

Solution(X) = L.U.B. of Inflowing Kinds

Boolean Number ▷XNumber Number ▷X

Number ▷ X!

X? Number▷

Boolean Number ▷ X? X!Number Number ▷ X? X!

X? X! ▷ X

X? Boolean▷

Boolean ▷ X?

Page 55: The Ins and Outs of  Gradual Type Inference

var x:X = function(y:Number):Number { … };if(b) {

x = function(y:Boolean):Number { … };x(true);

}

Type Inference for Higher Order Types

X! = Number

X? = Boolean

X = Boolean Number

Solution(X) = L.U.B. of Inflowing Kinds

Boolean Number ▷XNumber Number ▷X

Number ▷ X!

X? Number▷

Boolean Number ▷ X? X!Number Number ▷ X? X!

X? X! ▷ X

X? Boolean▷

Boolean ▷ X?

Page 56: The Ins and Outs of  Gradual Type Inference

56

var x:Boolean Number = function(y:Number):Number { … };if(b) {

x = function(y:Boolean):Number { … };x(true);

}

Type Inference for Higher Order Types

Page 57: The Ins and Outs of  Gradual Type Inference

57

var x:Boolean Number = function(y:Number):Number { … };if(b) {

x = function(y:Boolean):Number { … };x(true);

}

Sound More Precise than* Number

Type Inference for Higher Order Types

Page 58: The Ins and Outs of  Gradual Type Inference

58

Summarizing… Key Idea (2)

Use Kinds to Infer Higher Order Types

Page 59: The Ins and Outs of  Gradual Type Inference

59

Public Functions

function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}

foo(1);

Page 60: The Ins and Outs of  Gradual Type Inference

60

Public Functions

function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}

foo(1);

Foo? Number ▷

Number ▷ Foo!

Number ▷ Foo!

Number ▷ Foo?

Foo! = Number

Foo? = Number

Page 61: The Ins and Outs of  Gradual Type Inference

61

Public Functionsfunction foo(x:*):*{

if(b) {return x

+ 1;} else {

return 0;}

}

foo(1);

function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}

foo(1);

Page 62: The Ins and Outs of  Gradual Type Inference

62

Public Functionsfunction foo(x:*):*{

if(b) {return x

+ 1;} else {

return 0;}

}

foo(1);

function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}

foo(1);

b = false;foo(true);

(External Code)

Page 63: The Ins and Outs of  Gradual Type Inference

63

Public Functionsfunction foo(x:*):*{

if(b) {return x

+ 1;} else {

return 0;}

}

foo(1);

function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}

foo(1);

b = false;foo(true);

(External Code)

⟨Boolean * true▷ ⟩

Page 64: The Ins and Outs of  Gradual Type Inference

64

Public Functionsfunction foo(x:*):*{

if(b) {return x

+ 1;} else {

return 0;}

}

foo(1);

function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}

foo(1);

b = false;foo(true);

(External Code)⟨Boolean * ▷ ⟩ true ✔

Page 65: The Ins and Outs of  Gradual Type Inference

65

Public Functionsfunction foo(x:*):*{

if(b) {return x

+ 1;} else {

return 0;}

}

foo(1);

function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}

foo(1);

b = false;foo(true);

(External Code)⟨Boolean * ▷ ⟩ true ✔

⟨Boolean Number true ▷ ⟩

Page 66: The Ins and Outs of  Gradual Type Inference

66

Public Functionsfunction foo(x:*):*{

if(b) {return x

+ 1;} else {

return 0;}

}

foo(1);

function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}

foo(1);

b = false;foo(true);

(External Code)⟨Boolean * ▷ ⟩ true ✔

⟨Boolean Number ▷ ⟩ true ✗

Page 67: The Ins and Outs of  Gradual Type Inference

67

Public Functionsfunction foo(x:*):*{

if(b) {return x

+ 1;} else {

return 0;}

}

foo(1);

function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}

foo(1);

b = false;foo(true);

(External Code)

⟨Boolean Number ▷ ⟩ true ✗

Unsound !

⟨Boolean * ▷ ⟩ true ✔

Page 68: The Ins and Outs of  Gradual Type Inference

68

Public Functionsfunction foo(x:*):*{

if(b) {return x

+ 1;} else {

return 0;}

}

foo(1);

function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}

foo(1);

b = false;foo(true);

(External Code)

Unsound !Seeing all inflows is

necessary!

⟨Boolean * ▷ ⟩ true ✔

⟨Boolean Number ▷ ⟩ true ✗

Page 69: The Ins and Outs of  Gradual Type Inference

69

Where do we not see all inflows?

Type variables in negative positions in the type of the program

explicit annotation infer *OR

Unsafe to infer

Page 70: The Ins and Outs of  Gradual Type Inference

70

Summarizing… Key Idea (3)

Seeing All Inflows is Necessary

Page 71: The Ins and Outs of  Gradual Type Inference

71

Local Functions

function bar(y:Number):Bar!{

function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}return foo(y);

}

Page 72: The Ins and Outs of  Gradual Type Inference

72

Local Functions

function bar(y:Number):Bar!{

function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}return foo(y);

}

Page 73: The Ins and Outs of  Gradual Type Inference

73

Local Functions

function bar(y:Number):Bar!{

function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}return foo(y);

}

Sound !

foo can’t be called fromExternal Code

Page 74: The Ins and Outs of  Gradual Type Inference

74

Local Functions that Escape

function bar(y:Number):Bar! {function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Page 75: The Ins and Outs of  Gradual Type Inference

75

Local Functions that Escape

function bar(y:Number):Bar! {function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Page 76: The Ins and Outs of  Gradual Type Inference

76

Local Functions that Escape

function bar(y:Number):Bar! {function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Unsound !

b = false;f = bar(1);f(true);

(External Code)

Page 77: The Ins and Outs of  Gradual Type Inference

77

Local Functions that Escape

function bar(y:Number):Bar! {function foo(x:Number):Number{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Unsound !

b = false;f = bar(1);f(true);

(External Code)

Need escape

analysis?

Page 78: The Ins and Outs of  Gradual Type Inference

78

Local Functions that Escape

function bar(y:Number):Bar! {function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Page 79: The Ins and Outs of  Gradual Type Inference

79

Local Functions that Escape

function bar(y:Number):Bar! {function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Foo? Foo! ▷ Bar!

Page 80: The Ins and Outs of  Gradual Type Inference

80

function bar(y:Number):Bar! {function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Local Functions that EscapeFoo? Foo! ▷ Bar!

Foo? Foo! ▷ Bar!? Bar!!

Bar!? Bar!! ▷ Bar!

Page 81: The Ins and Outs of  Gradual Type Inference

81

function bar(y:Number):Bar! {function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Local Functions that Escape

Bar!? ▷ Foo?

Foo! ▷ Bar!!

Foo? Foo! ▷Bar!

Foo? Foo! ▷ Bar!? Bar!!

Bar!? Bar!! ▷ Bar!

Page 82: The Ins and Outs of  Gradual Type Inference

82

function bar(y:Number):Bar! {function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Local Functions that Escape

Bar!? ▷ Foo?

Foo! ▷ Bar!!

Bar!? is in a negative positionin type of program

Foo? Foo! ▷Bar!

Foo? Foo! ▷ Bar!? Bar!!

Bar!? Bar!! ▷ Bar!

Page 83: The Ins and Outs of  Gradual Type Inference

83

function bar(y:Number):Bar! {function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Local Functions that Escape

Bar!? = *

Bar!? is in a negative positionin type of program

Bar!? ▷ Foo?

Foo! ▷ Bar!!

Foo? Foo! ▷Bar!

Foo? Foo! ▷ Bar!? Bar!!

Bar!? Bar!! ▷ Bar!

Page 84: The Ins and Outs of  Gradual Type Inference

84

function bar(y:Number):Bar! {function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Local Functions that Escape

Foo? = Bar!? = * ✔

Bar!? = *

Bar!? is in a negative positionin type of program

Bar!? ▷ Foo?

Foo! ▷ Bar!!

Foo? Foo! ▷Bar!

Foo? Foo! ▷ Bar!? Bar!!

Bar!? Bar!! ▷ Bar!

Page 85: The Ins and Outs of  Gradual Type Inference

85

function bar(y:Number):Bar! {function foo(x:Foo?):Foo!{

if(b) {return x + 1;

} else {return 0;

}}foo(y);return foo;

}

Local Functions that Escape

Foo? = Bar!? = * ✔

Bar!? = *

Bar!? is in a negative positionin type of program

Bar!? ▷ Foo?

Foo! ▷ Bar!!

Foo? Foo! ▷Bar!

Foo? Foo! ▷ Bar!? Bar!!

Bar!? Bar!! ▷ Bar!Foo? = *

Page 86: The Ins and Outs of  Gradual Type Inference

86

No Escape Analysis Necessary

+

are already sufficient

Polarity-based restriction on type of program

Closure compuation

Page 87: The Ins and Outs of  Gradual Type Inference

87

Summarizing… Key Idea (4)

Flows Encode Escape Analysis

Page 88: The Ins and Outs of  Gradual Type Inference

88

Properties of Inference Algorithm

Time Complexity: O(N2)(“usually” O(N3) for Languages with Subtyping)

Preservation of Runtime Semantics:Programs do not breakThey can be composed with External CodeProofs for a

Calculus of Functions and

Objects

Page 89: The Ins and Outs of  Gradual Type Inference

89

Implementation and Evaluation

Implemented the Algorithm for ActionScript

Source Language of

Flash Applications

Page 90: The Ins and Outs of  Gradual Type Inference

90

Implementation and Evaluation

Implemented the Algorithm for ActionScript

Evaluated on V8 and SunSpider Benchmarks

Page 91: The Ins and Outs of  Gradual Type Inference

91

Implementation and Evaluation

ActionScriptCompiler

(ASC)

+

ActionScriptVirtual

Machine(AVM)

Partially Typed Code

Partially Typed Code

Fully Typed Code

Type Inference

Only Negative Positions Annotated

Manually Annotated

Page 92: The Ins and Outs of  Gradual Type Inference

92

Experiments

sunspider\access-fankkuchsunspider\access-nsieve

sunspider\bitops-bits-in-bytesunspider\bitops-bitwise-andsunspider\bitops-nsieve-bits

sunspider\crypto-aessunspider\crypto-md5sunspider\crypto-sha1sunspider\math-cordic

sunspider\math-partial-sumssunspider\math-spectral-normsunspider\string-unpack-code

sunspider\string-validate-inputsunspider\s3d-morph

v8\deltabluev8\raytracev8\richards

0 20 40 60 80 100 120

Partially Typed Benchmark Our Algorithm

Performance % (100% = Fully Typed Benchmark)

Average 60% Improvement over Partially

Typed

Recover ~100% Performance of Fully Typed in

13 / 17 Benchmarks

Better Than Fully Typed Benchmark

Page 93: The Ins and Outs of  Gradual Type Inference

93

Experiments

sunspider\access-fankkuchsunspider\access-nsieve

sunspider\bitops-bits-in-bytesunspider\bitops-bitwise-andsunspider\bitops-nsieve-bits

sunspider\crypto-aessunspider\crypto-md5sunspider\crypto-sha1sunspider\math-cordic

sunspider\math-partial-sumssunspider\math-spectral-normsunspider\string-unpack-code

sunspider\string-validate-inputsunspider\s3d-morph

v8\deltabluev8\raytracev8\richards

0 20 40 60 80 100 120

Partially Typed Benchmark Our Algorithm

Performance % (100% = Fully Typed Benchmark)

Array Reads

Object Property

Reads

Number vs

int

Range Analysis

Improves Performance

Page 94: The Ins and Outs of  Gradual Type Inference

94

Things We Did Not Talk About

• How Do We Tighten Closure Computation?• Do We Provide Blame Guarantees?• Can Precise Types Decrease Performance?• Are the Types We Infer Optimal?

See Our Paper !

Page 95: The Ins and Outs of  Gradual Type Inference

95

Conclusion

• Type Inference for Gradually Typed Languages• Improve performance of Flash Applications– Infer Precise Static Types for Missing Types– Consider Only Inflows for Solutions– Kinds as Solutions of Higher-Order Types

• Goal: Backward Compatibility– Polarity-based Restriction on Type of Program– Escape Analysis via Closure Computation