skip to main content
research-article

Dynamic inference of static types for ruby

Published:26 January 2011Publication History
Skip Abstract Section

Abstract

There have been several efforts to bring static type inference to object-oriented dynamic languages such as Ruby, Python, and Perl. In our experience, however, such type inference systems are extremely difficult to develop, because dynamic languages are typically complex, poorly specified, and include features, such as eval and reflection, that are hard to analyze.

In this paper, we introduce constraint-based dynamic type inference, a technique that infers static types based on dynamic program executions. In our approach, we wrap each run-time value to associate it with a type variable, and the wrapper generates constraints on this type variable when the wrapped value is used. This technique avoids many of the often overly conservative approximations of static tools, as constraints are generated based on how values are used during actual program runs. Using wrappers is also easy to implement, since we need only write a constraint resolution algorithm and a transformation to introduce the wrappers. The best part is that we can eat our cake, too: our algorithm will infer sound types as long as it observes every path through each method body---note that the number of such paths may be dramatically smaller than the number of paths through the program as a whole.

We have developed Rubydust, an implementation of our algorithm for Ruby. Rubydust takes advantage of Ruby's dynamic features to implement wrappers as a language library. We applied Rubydust to a number of small programs and found it to be both easy to use and useful: Rubydust discovered 1 real type error, and all other inferred types were correct and readable.

Skip Supplemental Material Section

Supplemental Material

41-mpeg-4.mp4

mp4

302.6 MB

References

  1. Rahul Agarwal and Scott D. Stoller. Type Inference for Parameterized Race-Free Java. In VMCAI, 2004.Google ScholarGoogle Scholar
  2. O. Agesen, J. Palsberg, and M.I. Schwartzbach. Type Inference of SELF. ECOOP, 1993. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Jong-hoon (David) An, Avik Chaudhuri, and Jeffrey S. Foster. Static Typing for Ruby on Rails. In ASE, 2009.Google ScholarGoogle Scholar
  4. Davide Ancona, Massimo Ancona, Antonio Cuni, and Nicholas Matsakis. RPython: Reconciling Dynamically and Statically Typed OO Languages. In DLS, 2007. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Christopher Anderson, Paola Giannini, and Sophia Drossopoulou. Towards Type Inference for JavaScript. In ECOOP, 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Shay Artzi, Adam Kiezun, David Glasser, and Michael D. Ernst. Combined static and dynamic mutability analysis. In ASE, 2007. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. John Aycock. Aggressive Type Inference. In International Python Conference, 2000.Google ScholarGoogle Scholar
  8. Christoph Csallner, Nikolai Tillmann, and Yannis Smaragdakis. Dysy: dynamic symbolic execution for invariant inference. In ICSE, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. Michael D. Ernst, Jeff H. Perkins, Philip J. Guo, Stephen McCamant, Carlos Pacheco, Matthew S. Tschantz, and Chen Xiao. The Daikon system for dynamic detection of likely invariants. Sci. Comput. Program., 69(1--3), 2007. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Michael Furr, Jong-hoon (David) An, and Jeffrey S. Foster. Profile-guided static typing for dynamic scripting languages. In OOPSLA, 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. Michael Furr, Jong-hoon (David) An, Jeffrey S. Foster, and Michael Hicks. The Ruby Intermediate Language. In Dynamic Language Symposium, 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Michael Furr, Jong-hoon (David) An, Jeffrey S. Foster, and Michael Hicks. Static Type Inference for Ruby. In OOPS Track, SAC, 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. J. Gronski, K. Knowles, A. Tomb, S. N. Freund, and C. Flanagan. Sage: Hybrid Checking for Flexible Specifications. Scheme and Functional Programming, 2006.Google ScholarGoogle Scholar
  14. Philip J. Guo, Jeff H. Perkins, Stephen McCamant, and Michael D. Ernst. Dynamic inference of abstract types. In ISSTA, 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. Yit Phang Khoo, Bor-Yuh Evan Chang, and Jeffrey S. Foster. Mixing type checking and symbolic execution. In PLDI, 2010. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Kristian Kristensen. Ecstatic -- Type Inference for Ruby Using the Cartesian Product Algorithm. Master's thesis, Aalborg University, 2007.Google ScholarGoogle Scholar
  17. Robin Milner. A Theory of Type Polymorphism in Programming. Journal of Computer and System Sciences, 17, 1978.Google ScholarGoogle Scholar
  18. Jason Morrison. Type Inference in Ruby. Google Summer of Code Project, 2006.Google ScholarGoogle Scholar
  19. Jeremy W. Nimmer and Michael D. Ernst. Invariant Inference for Static Checking: An Empirical Evaluation. In FSE, 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. Pascal Rapicault, Mireille Blay-Fornarino, Stéphane Ducasse, and Anne-Marie Dery. Dynamic type inference to support object-oriented reenginerring in Smalltalk. In ECOOP Workshops, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. rcov. rcov: code coverage for ruby, 2010. http://eigenclass. org/hiki/rcov.Google ScholarGoogle Scholar
  22. James Rose, Nikhil Swamy, and Michael Hicks. Dynamic inference of polymorphic lock types. Sci. Comput. Program., 58(3), 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Ruby Quiz, 2010. http://www.rubyquiz.com.Google ScholarGoogle Scholar
  24. Michael Salib. Starkiller: A Static Type Inferencer and Compiler for Python. Master's thesis, MIT, 2004.Google ScholarGoogle Scholar
  25. Jeremy G. Siek and Walid Taha. Gradual typing for functional languages. In Scheme and Functional Programming Workshop, 2006.Google ScholarGoogle Scholar
  26. Satish Thatte. Quasi-static typing. In POPL, 1990. Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. Peter Thiemann. Towards a type system for analyzing javascript programs. In ESOP, 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. Dave Thomas, Chad Fowler, and Andy Hunt. Programming Ruby: The Pragmatic Programmers' Guide. Pragmatic Bookshelf, 2004. Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. Sam Tobin-Hochstadt and Matthias Felleisen. The Design and Implementation of Typed Scheme. In POPL, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. David A. Wheeler. Sloccount, August 2004. http://www. dwheeler.com/sloccount/.Google ScholarGoogle Scholar
  31. A. K. Wright and R. Cartwright. A practical soft type system for Scheme. ACM TOPLAS, 19(1), 1997. Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. Jinlin Yang, David Evans, Deepali Bhardwaj, Thirumalesh Bhat, and Manuvir Das. Perracotta: mining temporal API rules from imperfect traces. In ICSE, 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Dynamic inference of static types for ruby

        Recommendations

        Comments

        Login options

        Check if you have access through your login credentials or your institution to get full access on this article.

        Sign in

        Full Access

        • Published in

          cover image ACM SIGPLAN Notices
          ACM SIGPLAN Notices  Volume 46, Issue 1
          POPL '11
          January 2011
          624 pages
          ISSN:0362-1340
          EISSN:1558-1160
          DOI:10.1145/1925844
          Issue’s Table of Contents
          • cover image ACM Conferences
            POPL '11: Proceedings of the 38th annual ACM SIGPLAN-SIGACT symposium on Principles of programming languages
            January 2011
            652 pages
            ISBN:9781450304900
            DOI:10.1145/1926385

          Copyright © 2011 ACM

          Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

          Publisher

          Association for Computing Machinery

          New York, NY, United States

          Publication History

          • Published: 26 January 2011

          Check for updates

          Qualifiers

          • research-article

        PDF Format

        View or Download as a PDF file.

        PDF

        eReader

        View online with eReader.

        eReader