skip to main content

The simple essence of algebraic subtyping: principal type inference with subtyping made easy (functional pearl)

Published:03 August 2020Publication History
Skip Abstract Section

Abstract

MLsub extends traditional Hindley-Milner type inference with subtyping while preserving compact principal types, an exciting new development. However, its specification in terms of biunification is difficult to understand, relying on the new concepts of bisubstitution and polar types, and making use of advanced notions from abstract algebra. In this paper, we show that these are in fact not essential to understanding the mechanisms at play in MLsub. We propose an alternative algorithm called Simple-sub, which can be implemented efficiently in under 500 lines of code (including parsing, simplification, and pretty-printing), looks more familiar, and is easier to understand.

We present an experimental evaluation of Simple-sub against MLsub on a million randomly-generated well-scoped expressions, showing that the two systems agree. The mutable automaton-based implementation of MLsub is quite far from its algebraic specification, leaving a lot of space for errors; in fact, our evaluation uncovered several bugs in it. We sketch more straightforward soundness and completeness arguments for Simple-sub, based on a syntactic specification of the type system.

This paper is meant to be light in formalism, rich in insights, and easy to consume for prospective designers of new type systems and programming languages. In particular, no abstract algebra is inflicted on readers.

Skip Supplemental Material Section

Supplemental Material

Presentation at ICFP '20

References

  1. Roberto M. Amadio and Luca Cardelli. 1993. Subtyping Recursive Types. ACM Trans. Program. Lang. Syst. 15, 4 (Sept. 1993 ), 575-631. https://doi.org/10.1145/155183.155231 Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Nada Amin, Samuel Grütter, Martin Odersky, Tiark Rompf, and Sandro Stucki. 2016. The Essence of Dependent Object Types. Springer International Publishing, Cham, 249-272. https://doi.org/10.1007/978-3-319-30936-1_14 Google ScholarGoogle Scholar
  3. Andrew W. Appel, Paul-André Melliès, Christopher D. Richards, and Jérôme Vouillon. 2007. A Very Modal Model of a Modern, Major, General Type System. In Proceedings of the 34th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (Nice, France) (POPL '07). Association for Computing Machinery, New York, NY, USA, 109-122. https://doi.org/10.1145/1190216.1190235 Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Giuseppe Castagna, Tommaso Petrucciani, and Kim Nguyen. 2016. Set-theoretic types for polymorphic variants. In Proceedings of the 21st ACM SIGPLAN International Conference on Functional Programming (ICFP 2016 ). Association for Computing Machinery, Nara, Japan, 378-391. https://doi.org/10.1145/2951913.2951928 Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Nathanaël Courant. 2018. Safely typing algebraic efects (Gagallium Blog). http://gallium.inria.fr/blog/safely-typingalgebraic-efects/. Accessed: 2020-06-30.Google ScholarGoogle Scholar
  6. Luis Damas and Robin Milner. 1982. Principal type-schemes for functional programs. In Proceedings of the 9th ACM SIGPLAN-SIGACT symposium on Principles of programming languages (POPL '82). Association for Computing Machinery, Albuquerque, New Mexico, 207-212. https://doi.org/10.1145/582153.582176 Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Stephen Dolan. 2017. Algebraic subtyping. Ph.D. Dissertation.Google ScholarGoogle Scholar
  8. Stephen Dolan and Alan Mycroft. 2017. Polymorphism, subtyping, and type inference in MLsub. ACM SIGPLAN Notices 52, 1 (Jan. 2017 ), 60-72. https://doi.org/10.1145/3093333.3009882 Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. Joshua Dunfield and Neelakantan R. Krishnaswami. 2013. Complete and Easy Bidirectional Typechecking for Higher-Rank Polymorphism. SIGPLAN Not. 48, 9 (Sept. 2013 ), 429-442. https://doi.org/10.1145/2544174.2500582 Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Tim Freeman and Frank Pfenning. 1991. Refinement types for ML. In Proceedings of the ACM SIGPLAN 1991 conference on Programming language design and implementation. 268-277.Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. Alain Frisch, Giuseppe Castagna, and Véronique Benzaken. 2008. Semantic Subtyping: Dealing Set-Theoretically with Function, Union, Intersection, and Negation Types. J. ACM 55, 4, Article 19 ( Sept. 2008 ), 64 pages. https://doi.org/10. 1145/1391289.1391293 Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Jacques Garrigue. 1998. Programming with polymorphic variants. In ML Workshop, Vol. 13. Baltimore, 7.Google ScholarGoogle Scholar
  13. Roger Hindley. 1969. The Principal Type-Scheme of an Object in Combinatory Logic. Trans. Amer. Math. Soc. 146 ( 1969 ), 29-60. https://doi.org/10.2307/1995158 Publisher: American Mathematical Society. Google ScholarGoogle ScholarCross RefCross Ref
  14. Haruo Hosoya, Jérôme Vouillon, and Benjamin C. Pierce. 2005. Regular Expression Types for XML. ACM Trans. Program. Lang. Syst. 27, 1 (Jan. 2005 ), 46-90. https://doi.org/10.1145/1053468.1053470 Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. DeLesley S. Hutchins. 2010. Pure Subtype Systems. In Proceedings of the 37th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (Madrid, Spain) (POPL '10). Association for Computing Machinery, New York, NY, USA, 287-298. https://doi.org/10.1145/1706299.1706334 Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Oleg Kiselyov. 2013. Eficient generalization with levels (Okmij Blog). http://okmij.org/ftp/ML/generalization.html#levels. Accessed: 2020-06-30.Google ScholarGoogle Scholar
  17. Robin Milner. 1978. A theory of type polymorphism in programming. J. Comput. System Sci. 17, 3 (Dec. 1978 ), 348-375. https://doi.org/10.1016/ 0022-0000 ( 78 ) 90014-4 Google ScholarGoogle ScholarCross RefCross Ref
  18. Martin Odersky and Konstantin Läufer. 1996. Putting Type Annotations to Work. In Proceedings of the 23rd ACM SIGPLANSIGACT Symposium on Principles of Programming Languages (St. Petersburg Beach, Florida, USA) ( POPL '96). Association for Computing Machinery, New York, NY, USA, 54-67. https://doi.org/10.1145/237721.237729 Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. David J. Pearce. 2013. Sound and Complete Flow Typing with Unions, Intersections and Negations. In Verification, Model Checking, and Abstract Interpretation (Lecture Notes in Computer Science), Roberto Giacobazzi, Josh Berdine, and Isabella Mastroeni (Eds.). Springer, Berlin, Heidelberg, 335-354. https://doi.org/10.1007/978-3-642-35873-9_21 Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. Benjamin C. Pierce. 2002. Types and programming languages. MIT press.Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. François Pottier. 1998. Type Inference in the Presence of Subtyping: from Theory to Practice. Research Report RR-3483. INRIA. https://hal.inria.fr/inria-00073205Google ScholarGoogle Scholar
  22. Patrick M. Rondon, Ming Kawaguci, and Ranjit Jhala. 2008. Liquid Types. In Proceedings of the 29th ACM SIGPLAN Conference on Programming Language Design and Implementation (Tucson, AZ, USA) ( PLDI '08). Association for Computing Machinery, New York, NY, USA, 159-169. https://doi.org/10.1145/1375581.1375602 Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Andreas Rossberg. 2015. 1ML-Core and Modules United (F-Ing First-Class Modules ). In Proceedings of the 20th ACM SIGPLAN International Conference on Functional Programming (Vancouver, BC, Canada) ( ICFP 2015 ). Association for Computing Machinery, New York, NY, USA, 35-47. https://doi.org/10.1145/2784731.2784738 Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. John Rushby, Sam Owre, and Natarajan Shankar. 1998. Subtypes for Specifications: Predicate Subtyping in PVS. IEEE Trans. Softw. Eng. 24, 9 (Sept. 1998 ), 709-720. https://doi.org/10.1109/32.713327 Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Sam Tobin-Hochstadt and Matthias Felleisen. 2010. Logical Types for Untyped Languages. SIGPLAN Not. 45, 9 (Sept. 2010 ), 117-128. https://doi.org/10.1145/1932681.1863561 Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Niki Vazou, Eric L. Seidel, Ranjit Jhala, Dimitrios Vytiniotis, and Simon Peyton-Jones. 2014. Refinement Types for Haskell. In Proceedings of the 19th ACM SIGPLAN International Conference on Functional Programming (Gothenburg, Sweden) (ICFP '14). Association for Computing Machinery, New York, NY, USA, 269-282. https://doi.org/10.1145/2628136.2628161 Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. The simple essence of algebraic subtyping: principal type inference with subtyping made easy (functional pearl)

      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

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader
      About Cookies On This Site

      We use cookies to ensure that we give you the best experience on our website.

      Learn more

      Got it!