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

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Runtime Techniques for Efficient and Reliable Program Execution Harry Xu CS 295 Winter 2012.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
De necessariis pre condiciones consequentia sine machina P. Consobrinus, R. Consobrinus M. Aquilifer, F. Oratio.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
A Program Transformation For Faster Goal-Directed Search Akash Lal, Shaz Qadeer Microsoft Research.
Safe TypeScript Aseem Rastogi University of Maryland, College Park
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
COEN Expressions and Assignment
1 Space-Efficient Gradual Typing David Herman Northeastern University Aaron Tomb, Cormac Flanagan University of California, Santa Cruz.
ABCD: Eliminating Array-Bounds Checks on Demand Rastislav Bodík Rajiv Gupta Vivek Sarkar U of Wisconsin U of Arizona IBM TJ Watson recent experiments.
ISBN Chapter 3 Describing Syntax and Semantics.
Compiler Construction
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
Run time vs. Compile time
Approaches to Typing Programming Languages Robert Dewar.
Declaring and Checking Non-null Types in an Object-Oriented Language Authors: Manuel Fahndrich K. Rustan M. Leino OOPSLA’03 Presenter: Alexander Landau.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Today More on random testing + symbolic constraint solving (“concolic” testing) Using summaries to explore fewer paths (SMART) While preserving level.
Describing Syntax and Semantics
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
Semantics “Semantics” has to do with the meaning of a program. We will consider two types of semantics: –Static semantics: semantics which can be enforced.
Recap from last time We saw various different issues related to program analysis and program transformations You were not expected to know all of these.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Type Systems CS Definitions Program analysis Discovering facts about programs. Dynamic analysis Program analysis by using program executions.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
Chapter 3 Part II Describing Syntax and Semantics.
Semantics In Text: Chapter 3.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
CMSC 330: Organization of Programming Languages Java Generics.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
1 Languages and Compilers (SProg og Oversættere) Compiler Optimizations Bent Thomsen Department of Computer Science Aalborg University With acknowledgement.
Shifting the Blame A blame calculus with delimited control Taro Sekiyama* Atsushi Igarashi Soichiro Ueda Kyoto University Gradual typing.
CMSC 330: Organization of Programming Languages Operational Semantics.
CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Faithful mapping of model classes to mathematical structures Ádám Darvas ETH Zürich Switzerland Peter Müller Microsoft Research Redmond, WA, USA SAVCBS.
State ● What is state? ABCDE. State and Data Structures ABCDE A B C D E ● Data structures encode state. In doing so, they introduce state ● State of data.
Memory Management in Java Mr. Gerb Computer Science 4.
CS5205: Foundation in Programming Languages Type Reconstruction
ActionScript Programming Help
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Data Flow Analysis Suman Jana
Expressions and Control Flow in JavaScript
Amir Kamil and Katherine Yelick
University Of Virginia
Typed Arithmetic Expressions
TS*: Taming the Un-typed Adversary in JavaScript
Space-Efficient Gradual Typing
Continuation Marks A john clements talk.
Amir Kamil and Katherine Yelick
Compiler Construction
Gradual Verification Seamlessly and flexibly combine static and dynamic verification by drawing on the general principles from abstract interpretation.
Compiler Construction
Types and Related Issues
Presentation transcript:

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

Gradually Typed Code 2 Dynamically Typed Code missing annotations = dynamic types Dynamically Typed Code missing annotations = dynamic types Statically Typed Code fully annotated with static types Statically Typed Code fully annotated with static types type casts e.g., a Flash application in ActionScript e.g., a Flash application in ActionScript

Evolution of Scripts to Programs Dynamically Typed “Script” Dynamically Typed “Script” Statically Typed “Program” Statically Typed “Program” incrementally adding annotations for performance Gradual Typing in our experience, a fantasy missing annotations = dynamic types by default, rather than as fallback

Annoying Trade-Off 4 Annotation Burden Performance Penalty Can this trade-off be eliminated?

Our Vision of Evolution Process Partially Typed “Script” Partially Typed “Script” Partially Untyped “Program” Partially Untyped “Program” Infer Types Annotate / Restructure Type Inference: Key Ingredient for Evolution

Our Contributions 6 Type Inference Algorithm for Gradually Typed Languages Gradual Type Inference Improve Performance of Flash Applications Practical Motivation Backward Compatibility Goal Increase Safety / Eliminate Errors Non 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

Gradual Type Inference 7

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; } 8

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; } 9 Missing Type = Dynamic Type

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 10 Missing Type = Dynamic Type

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 11 Missing Type = Dynamic Type

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 12 Missing Type = Dynamic Type

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 13 Missing Type = Dynamic Type

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 14 Missing Type = Dynamic Type

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; } 15 Missing Type = Unknown Type Missing Type = Dynamic Type Unknown types represented as type variables, solved to static types where possible, dynamic type as fallback Unknown types represented as type variables, solved to static types where possible, dynamic type as fallback

Architecture of Type Inference 16 Program Annotated With Type Variables Program With Coercions Flow Relation Over Types Solutions of Type Variables as Types Compilation Closure Computation Solution Derivation

Coercions … are of the form T 1 ▷ T 2 “term of type T 1 flows into context of type T 2 ” T ▷ X is an inflow for type variable X X ▷ T is an outflow for type variable X 17

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 18

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 19

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 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 = Number ▷ S> (S ▷ Number s + I ▷ Number i); i = i + 1; } return s; } Number ▷ S I ▷ Number S ▷ I S ▷ Number 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 = Number ▷ I (I ▷ Number i + 1); } return s; } Number ▷ S I ▷ Number S ▷ Number S ▷ I Number ▷ I 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 ▷ Foo! s; } S ▷ Foo! Number ▷ S I ▷ Number S ▷ Number S ▷ I Number ▷ I 23

Solving for Type Variables 24 X X Inflows Outflows

Solving in a Static Type System 25 X X Inflows L.U.B. Outflows G.L.B. Solution(X) Precision Safety

Solving in a Gradual Type System 26 X X Inflows L.U.B. Solution(X) Precision

Solving in a Gradual Type System 27 X X Inflows L.U.B. Solution(X) Recall: eliminating errors is a non-goal Recall: eliminating errors is a non-goal Precision

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 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 I = Number S = Number S ▷ Foo! Number ▷ S I ▷ Number S ▷ Number S ▷ I Number ▷ I S = Number 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 Foo! = S = Number S ▷ Foo! Number ▷ S I ▷ Number S ▷ Number S ▷ I Number ▷ I I = Number S = Number S = Number 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 S ▷ Foo! Number ▷ S I ▷ Number S ▷ Number S ▷ I Number ▷ I I = Number S = Number S = Number Outflows ignored 31 Foo! = S = Number

Annotated Program 32 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; }

Annotated Program: No Casts 33 function foo(n:Number):Number { var s:Number = 0; var i:Number = s; while(i < n) { s = s + i; i = i + 1; } return s; }

Summarizing… Key Idea (1) Consider Only Inflows to Solve Type Variables 34

Type Inference for Higher Order Types 35 var x:X = function(y:Number):Number { … }; If(b) { x = function(y:Boolean):Number { … }; x(true); }

Type Inference for Higher Order Types 36 var x:X = function(y:Number):Number { … }; If(b) { x = function(y:Boolean):Number { … }; x(true); } Boolean Number ▷ X Number Number ▷ X

Type Inference for Higher Order Types 37 var x:X = function(y:Number):Number { … }; If(b) { x = function(y:Boolean):Number { … }; x(true); } Boolean Number ▷ X Number Number ▷ X X = ⊥ Number

Type Inference for Higher Order Types 38 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

Type Inference for Higher Order Types 39 var x:X = function(y:Number):Number { … }; If(b) { x = function(y:Boolean):Number { … }; x(true); } Boolean Number ▷ X Number Number ▷ X X = * Number

Type Inference for Higher Order Types 40 var x:X = function(y:Number):Number { … }; If(b) { x = function(y:Boolean):Number { … }; x(true); } Boolean Number ▷ X Number Number ▷ X X = * Number Imprecise

Type Inference for Higher Order Types 41 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

42 Higher Order Types: Kinds

43 Suppose that solution of X is of the form _ _ function type Higher Order Types: Kinds

44 The kind of X under this structure is X? X! Higher Order Types: Kinds Suppose that solution of X is of the form _ _

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 _ _

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 “a kind for every type constructor” all parts of a higher-order type solved based on inflows ≈ naïve subtyping

var x:X = function(y:Number):Number { … }; if(b) { x = function(y:Boolean):Number { … }; x(true); } 47 Boolean Number ▷ X Number Number ▷ X Boolean Number ▷ X? X! Number Number ▷ X? X! X? X! ▷ X Type Inference for Higher Order Types

var x:X = function(y:Number):Number { … }; if(b) { x = function(y:Boolean):Number { … }; x(true); } 48 Boolean ▷ X? Boolean Number ▷ X Number Number ▷ X Boolean Number ▷ X? X! Number Number ▷ X? X! X? X! ▷ X 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 ▷ X! 49 Boolean ▷ X? Boolean Number ▷ X Number Number ▷ X Boolean Number ▷ X? X! Type Inference for Higher Order Types Number Number ▷ X? X! X? X! ▷ X

var x:X = function(y:Number):Number { … }; if(b) { x = function(y:Boolean):Number { … }; x(true); } Number ▷ X! X? ▷ Number 50 X? ▷ Boolean Boolean Number ▷ X Number Number ▷ X Number Number ▷ X? X! Type Inference for Higher Order Types Boolean Number ▷ X? X! Boolean ▷ X? X? X! ▷ X

var x:X = function(y:Number):Number { … }; if(b) { x = function(y:Boolean):Number { … }; x(true); } 51 Number ▷ X! X? ▷ Number X? ▷ Boolean Boolean ▷ X? Boolean Number ▷ X Number Number ▷ X X? = Boolean Type Inference for Higher Order Types Boolean Number ▷ X? X! Number Number ▷ X? X! X? X! ▷ X

var x:X = function(y:Number):Number { … }; if(b) { x = function(y:Boolean):Number { … }; x(true); } X! = Number 52 Number ▷ X! X? ▷ Number Boolean Number ▷ X Number 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?

var x:X = function(y:Number):Number { … }; if(b) { x = function(y:Boolean):Number { … }; x(true); } Solution(X) = L.U.B. of Inflowing Kinds 53 Boolean Number ▷ X Number 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?

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 ▷ X Number Number ▷ X Number ▷ X! X? ▷ Number Boolean Number ▷ X? X! Number Number ▷ X? X! X? X! ▷ X X? ▷ Boolean Boolean ▷ X?

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 ▷ X Number Number ▷ X Number ▷ X! X? ▷ Number Boolean Number ▷ X? X! Number Number ▷ X? X! X? X! ▷ X X? ▷ Boolean Boolean ▷ X?

var x:Boolean Number = function(y:Number):Number { … }; if(b) { x = function(y:Boolean):Number { … }; x(true); } 56 Type Inference for Higher Order Types

var x:Boolean Number = function(y:Number):Number { … }; if(b) { x = function(y:Boolean):Number { … }; x(true); } 57 Sound More Precise than * Number More Precise than * Number Type Inference for Higher Order Types

Summarizing… Key Idea (2) Use Kinds to Infer Higher Order Types 58

Public Functions function foo(x:Foo?):Foo! { if(b) { return x + 1; } else { return 0; } foo(1); 59

Public Functions function foo(x:Foo?):Foo! { if(b) { return x + 1; } else { return 0; } foo(1); Foo? ▷ Number Number ▷ Foo! Number ▷ Foo? Foo! = Number Foo? = Number 60

Public Functions function 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); 61

Public Functions function 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); 62 b = false; foo(true); (External Code)

Public Functions function 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); 63 b = false; foo(true); (External Code) Boolean ▷ * true

Public Functions function 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); 64 b = false; foo(true); (External Code) Boolean ▷ * true ✔

Public Functions function 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); 65 b = false; foo(true); (External Code) Boolean ▷ * true ✔ Boolean ▷ Number true

Public Functions function 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); 66 b = false; foo(true); (External Code) Boolean ▷ * true ✔ Boolean ▷ Number true ✗

Public Functions function 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); 67 b = false; foo(true); (External Code) Boolean ▷ Number true ✗ Unsound ! Boolean ▷ * true ✔

Public Functions function 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); 68 b = false; foo(true); (External Code) Unsound ! Seeing all inflows is necessary! Boolean ▷ * true ✔ Boolean ▷ Number true ✗

Where do we not see all inflows? 69 Type variables in negative positions in the type of the program explicit annotation infer * OR Unsafe to infer

Summarizing… Key Idea (3) Seeing All Inflows is Necessary 70

Local Functions function bar(y:Number):Bar! { function foo(x:Foo?):Foo! { if(b) { return x + 1; } else { return 0; } return foo(y); } 71

Local Functions function bar(y:Number):Bar! { function foo(x:Number):Number { if(b) { return x + 1; } else { return 0; } return foo(y); } 72

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 from External Code 73

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; } 74

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; } 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; } Unsound ! b = false; f = bar(1); f(true); (External Code) 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) 77 Need escape analysis?

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; } 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; } 79 Foo? Foo! ▷ Bar!

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 80 Foo? Foo! ▷ Bar! Foo? Foo! ▷ Bar!? Bar!! Bar!? Bar!! ▷ Bar!

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 81 Bar!? ▷ Foo? Foo! ▷ Bar!! Foo? Foo! ▷ Bar! Foo? Foo! ▷ Bar!? Bar!! Bar!? Bar!! ▷ Bar!

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 82 Bar!? ▷ Foo? Foo! ▷ Bar!! Bar!? is in a negative position in type of program Foo? Foo! ▷ Bar! Foo? Foo! ▷ Bar!? Bar!! Bar!? Bar!! ▷ Bar!

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!? = * 83 Bar!? is in a negative position in type of program Bar!? ▷ Foo? Foo! ▷ Bar!! Foo? Foo! ▷ Bar! Foo? Foo! ▷ Bar!? Bar!! Bar!? Bar!! ▷ Bar!

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!? = * ✔ 84 Bar!? = * Bar!? is in a negative position in type of program Bar!? ▷ Foo? Foo! ▷ Bar!! Foo? Foo! ▷ Bar! Foo? Foo! ▷ Bar!? Bar!! Bar!? Bar!! ▷ Bar!

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 85 Foo? = Bar!? = * ✔ Bar!? = * Bar!? is in a negative position in type of program Bar!? ▷ Foo? Foo! ▷ Bar!! Foo? Foo! ▷ Bar! Foo? Foo! ▷ Bar!? Bar!! Bar!? Bar!! ▷ Bar! Foo? = *

No Escape Analysis Necessary + are already sufficient 86 Polarity-based restriction on type of program Closure compuation

Summarizing… Key Idea (4) Flows Encode Escape Analysis 87

Properties of Inference Algorithm Time Complexity: O(N 2 ) (“usually” O(N 3 ) for Languages with Subtyping) Preservation of Runtime Semantics: Programs do not break They can be composed with External Code Proofs for a Calculus of Functions and Objects 88

Implementation and Evaluation Implemented the Algorithm for ActionScript Source Language of Flash Applications 89

Implementation and Evaluation Implemented the Algorithm for ActionScript Evaluated on V8 and SunSpider Benchmarks 90

Implementation and Evaluation ActionScript Compiler (ASC) + ActionScript Virtual Machine (AVM) Partially Typed Code Fully Typed Code Type Inference 91 Only Negative Positions Annotated Manually Annotated

Experiments Average 60% Improvement over Partially Typed Recover ~100% Performance of Fully Typed in 13 / 17 Benchmarks Better Than Fully Typed Benchmark 92

Experiments Array Reads Object Property Reads Number vs int 93 Range Analysis Improves Performance

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? 94 See Our Paper !

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 95