The ins and outs of gradual type inference

Gradual typing lets programmers evolve their dynamically typed programs by gradually adding explicit type annotations, which confer benefits like improved performance and fewer run-time failures. However, we argue that such evolution often requires a giant leap, and that type inference can offer a crucial missing step. If omitted type annotations are interpreted as unknown types, rather than the dynamic type, then static types can often be inferred, thereby removing unnecessary assumptions of the dynamic type. The remaining assumptions of the dynamic type may then be removed by either reasoning outside the static type system, or restructuring the code. We present a type inference algorithm that can improve the performance of existing gradually typed programs without introducing any new run-time failures. To account for dynamic typing, types that flow in to an unknown type are treated in a fundamentally different manner than types that flow out. Furthermore, in the interests of backward-compatibility, an escape analysis is conducted to decide which types are safe to infer. We have implemented our algorithm for ActionScript, and evaluated it on the SunSpider and V8 benchmark suites. We demonstrate that our algorithm can improve the performance of unannotated programs as well as recover most of the type annotations in annotated programs.

[1]  Davide Ancona,et al.  RPython: a step towards reconciling dynamically and statically typed OO languages , 2007, DLS '07.

[2]  Fritz Henglein,et al.  Dynamic Typing: Syntax and Proof Theory , 1994, Sci. Comput. Program..

[3]  Avik Chaudhuri,et al.  The impact of optional type information on jit compilation of dynamically typed languages , 2011, DLS '11.

[4]  Philip Wadler,et al.  Well-Typed Programs Can't Be Blamed , 2009, ESOP.

[5]  Alexander Aiken,et al.  Soft typing with conditional types , 1994, POPL '94.

[6]  Peter Thiemann Towards a Type System for Analyzing JavaScript Programs , 2005, ESOP.

[7]  Mason Chang,et al.  Trace-based just-in-time type specialization for dynamic languages , 2009, PLDI '09.

[8]  Jan Vitek,et al.  Integrating typed and untyped code in a scripting language , 2010, POPL '10.

[9]  Jens Palsberg Efficient Inference of Object Types , 1995, Inf. Comput..

[10]  Manish Vachharajani,et al.  Gradual typing with unification-based inference , 2008, DLS '08.

[11]  Walid Taha,et al.  Gradual Typing for Objects , 2007, ECOOP.

[12]  Jens Palsberg,et al.  Type Inference of SELF , 1993, ECOOP.

[13]  Jeremy G. Siek Gradual Typing for Functional Languages , 2006 .

[14]  François Pottier,et al.  A framework for type inference with subtyping , 1998, ICFP '98.

[15]  Cormac Flanagan,et al.  Space-efficient gradual typing , 2010, High. Order Symb. Comput..

[16]  Sam Tobin-Hochstadt,et al.  The design and implementation of typed scheme , 2008, POPL '08.

[17]  Avik Chaudhuri,et al.  Dynamic inference of static types for ruby , 2011, POPL '11.

[18]  Jeffrey S. Foster,et al.  Profile-guided static typing for dynamic scripting languages , 2009, OOPSLA.

[19]  Robert Cartwright,et al.  Soft typing , 2004, SIGP.

[20]  Philip Wadler,et al.  Threesomes, with and without blame , 2010, POPL '10.

[21]  Sophia Drossopoulou,et al.  Towards Type Inference for JavaScript , 2005, ECOOP.

[22]  Colin Moock Essential ActionScript 3.0 , 2007 .

[23]  Robert Cartwright,et al.  A practical soft type system for scheme , 1997, TOPL.

[24]  Cormac Flanagan,et al.  Hybrid type checking , 2006, POPL '06.