ABSTRACT
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.
Supplemental Material
- O. Agesen, J. Palsberg, and M.I. Schwartzbach. Type Inference of SELF. ECOOP, 1993. Google ScholarDigital Library
- A. Aiken, E. L. Wimmers, and T. K. Lakshman. Soft Typing with Conditional Types. In POPL, pages 163--173, 1994. Google ScholarDigital Library
- J. D. An, A. Chaudhuri, J. S. Foster, and M. Hicks. Dynamic Inference of Static Types for Ruby. In POPL, pages 459--472. ACM, 2011. Google ScholarDigital Library
- D. Ancona, M. Ancona, A. Cuni, and N. Matsakis. RPython: Reconciling Dynamically and Statically Typed OO Languages. In DLS. ACM, 2007. Google ScholarDigital Library
- C. Anderson, P. Giannini, and S. Drossopoulou. Towards Type Inference for JavaScript. In ECOOP, 2005. Google ScholarDigital Library
- R. Cartwright and M. Fagan. Soft typing. In PLDI, pages 278--292, 1991. Google ScholarDigital Library
- M. Chang, B. Mathiske, E. Smith, A. Chaudhuri, A. Gal, M. Bebenita, C. Wimmer, and M. Franz. The Impact of Optional Type Information on JIT Compilation of Dynamically Typed Languages. In DLS. ACM, 2011. Google ScholarDigital Library
- C. Flanagan. Hybrid Type Checking. In POPL, pages 245--256. ACM, 2006. Google ScholarDigital Library
- M. Furr, J. D. An, J. S. Foster, and M. Hicks. Profile-Guided Static Typing for Dynamic Scripting Languages. In OOPSLA, 2009. Google ScholarDigital Library
- A. Gal, B. Eich, M. Shaver, D. Anderson, D. Mandelin, M. R. Haghighat, B. Kaplan, G. Hoare, B. Zbarsky, J. Orendorff, J. Ruderman, E. W. Smith, R. Reitmaier, M. Bebenita, M. Chang, and M. Franz. Trace-based just-in-time type specialization for dynamic languages. In PLDI, pages 465--478. ACM, 2009. Google ScholarDigital Library
- F. Henglein. Dynamic Typing: Syntax and Proof Theory. Science of Computer Programming, 22 (3): 197 -- 230, 1994. Google ScholarDigital Library
- D. Herman, A. Tomb, and C. Flanagan. Space-Efficient Gradual Typing. Trends in Functional Programming, 2007.Google Scholar
- C. Moock. Essential ActionScript 3.0. O'Reilly, 2007. Google ScholarDigital Library
- J. Palsberg. Efficient Inference of Object Types. In LICS, pages 186--195. IEEE, 1994.Google ScholarCross Ref
- F. Pottier. A Framework for Type Inference with Subtyping. In ICFP, pages 228--238. ACM, 1998. Google ScholarDigital Library
- J. Siek and W. Taha. Gradual Typing for Objects. In ECOOP, pages 2--27. Springer-Verlag, 2007. Google ScholarDigital Library
- J. G. Siek and W. Taha. Gradual Typing for Functional Languages. In Scheme and Functional Programming Workshop, 2006.Google Scholar
- J. G. Siek and M. Vachharajani. Gradual Typing with Unification-Based Inference. In DLS, pages 1--12. ACM, 2008. Google ScholarDigital Library
- J. G. Siek and P. Wadler. Threesomes, With and Without blame. In POPL, pages 365--376. ACM, 2010. Google ScholarDigital Library
- SunSpider Benchmarks, 2010. http://www.webkit.org/perf/sunspider/sunspider.html.Google Scholar
- P. Thiemann. Towards a Type System for Analyzing JavaScript Programs. In ESOP, 2005. Google ScholarDigital Library
- S. Tobin-Hochstadt and M. Felleisen. The Design and Implementation of Typed Scheme. In POPL, 2008. Google ScholarDigital Library
- V8 Benchmarks, 2011. http://code.google.com/apis/v8/benchmarks.html.Google Scholar
- P. Wadler and R. B. Findler. Well-Typed Programs Can't Be Blamed. In ESOP, pages 1--16. Springer-Verlag, 2009. Google ScholarDigital Library
- A. K. Wright and R. Cartwright. A Practical Soft Type System for Scheme. ACM TOPLAS, 19 (1), 1997. Google ScholarDigital Library
- T. Wrigstad, F. Z. Nardelli, S. Lebresne, J. Östlund, and J. Vitek. Integrating Typed and Untyped Code in a Scripting Language. In POPL, pages 377--388. ACM, 2010. Google ScholarDigital Library
Index Terms
- The ins and outs of gradual type inference
Recommendations
Solver-based gradual type migration
Gradually typed languages allow programmers to mix statically and dynamically typed code, enabling them to incrementally reap the benefits of static typing as they add type annotations to their code. However, this type migration process is typically a ...
The ins and outs of gradual type inference
POPL '12Gradual 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 ...
Principal Type Schemes for Gradual Programs
POPL '15: Proceedings of the 42nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming LanguagesGradual typing is a discipline for integrating dynamic checking into a static type system. Since its introduction in functional languages, it has been adapted to a variety of type systems, including object-oriented, security, and substructural. This ...
Comments