Hostname: page-component-8448b6f56d-cfpbc Total loading time: 0 Render date: 2024-04-20T09:54:35.464Z Has data issue: false hasContentIssue false

Practical type inference for arbitrary-rank types

Published online by Cambridge University Press:  01 January 2007

SIMON PEYTON JONES
Affiliation:
Microsoft Research
DIMITRIOS VYTINIOTIS
Affiliation:
University of Pennsylvania
STEPHANIE WEIRICH
Affiliation:
University of Pennsylvania
MARK SHIELDS
Affiliation:
Microsoft Research
Rights & Permissions [Opens in a new window]

Abstract

Core share and HTML view are not available for this content. However, as you have access to this content, a full PDF is available via the ‘Save PDF’ action button.

Haskell's popularity has driven the need for ever more expressive type system features, most of which threaten the decidability and practicality of Damas-Milner type inference. One such feature is the ability to write functions with higher-rank types – that is, functions that take polymorphic functions as their arguments. Complete type inference is known to be undecidable for higher-rank (impredicative) type systems, but in practice programmers are more than willing to add type annotations to guide the type inference engine, and to document their code. However, the choice of just what annotations are required, and what changes are required in the type system and its inference algorithm, has been an ongoing topic of research. We take as our starting point a λ-calculus proposed by Odersky and Läufer. Their system supports arbitrary-rank polymorphism through the exploitation of type annotations on λ-bound arguments and arbitrary sub-terms. Though elegant, and more convenient than some other proposals, Odersky and Läufer's system requires many annotations. We show how to use local type inference (invented by Pierce and Turner) to greatly reduce the annotation burden, to the point where higher-rank types become eminently usable. Higher-rank types have a very modest impact on type inference. We substantiate this claim in a very concrete way, by presenting a complete type-inference engine, written in Haskell, for a traditional Damas-Milner type system, and then showing how to extend it for higher-rank types. We write the type-inference engine using a monadic framework: it turns out to be a particularly compelling example of monads in action. The paper is long, but is strongly tutorial in style. Although we use Haskell as our example source language, and our implementation language, much of our work is directly applicable to any ML-like functional language.

Type
Article
Copyright
Copyright © Cambridge University Press 2006

References

Baars, Arthur L, & Swierstra, S. Doaitse. (2002). Typing dynamic typing. Pages 157166 of: ACM SIGPLAN International Conference on Functional Programming (ICFP'02). Pittsburgh: ACM.Google Scholar
Bird, Richard, & Paterson, Ross. (1999). De Bruijn notation as a nested datatype. Journal of Functional Programming, 9 (1), 7791.CrossRefGoogle Scholar
Cardelli, L. (1987). Basic polymorphic typechecking. Science of Computer Programming, 8 (2), 147172.Google Scholar
Clement, D, Despeyroux, J, Despeyroux, T, & Kahn, G. (1986). A simple applicative language: Mini-ML. Pages 1327 of: ACM Symposium on Lisp and Functional Programming. ACM.Google Scholar
Damas, Luis, & Milner, Robin. (1982). Principal type-schemes for functional programs. Pages 207–12 of: Conference record of the 9th annual acm symposium on principles of programming languages. New York: ACM Press.Google Scholar
Garrigue, Jacques, & Remy, Didier. (1999). Semi-explicit first-class polymorphism for ML. Journal of information and computation, 155, 134169.Google Scholar
Gill, A, Launchbury, J, & Peyton Jones, SL. (1993). A short cut to deforestation. Pages 223232 of: ACM Conference on Functional Programming and Computer Architecture (FPCA'93). Cophenhagen: ACM Press. ISBN 0-89791-595-X.Google Scholar
Girard, J-Y. (1990). The system F of variable types: fifteen years later. Huet, G (ed), Logical foundations of functional programming. Addison-Wesley.Google Scholar
Hancock, P. (1987). A type checker. Pages 163182 of: Peyton Jones, SL (ed), The implementation of functional programming languages. Prentice Hall.Google Scholar
Heeren, B, Hage, J, & Swierstra, SD. (2003). Scripting the type inference process. In: CFP03, 2003.Google Scholar
Hinze, Ralf. (2000). A new approach to generic functional programming. Pages 119132 of: 27th ACM Symposium on Principles of Programming Languages (POPL'00). Boston: ACM.Google Scholar
Hinze, Ralf. (2001). Manufacturing datatypes. Journal of Functional Programming, 1.Google Scholar
Huet, G. (2002). Higher order unification 30 years later. 15th international workshop on higher order logic theorem proving and its applications (IWHOLTP'02). LNCS.Google Scholar
ICFP03. (2003). ACM SIGPLAN International Conference on Functional Programming (ICFP'03). Uppsala, Sweden: ACM.Google Scholar
ICFP05. (2005). ACM SIGPLAN International Conference on Functional Programming (ICFP'05). Tallinn, Estonia: ACM.Google Scholar
Jones, Mark. (1999). Typing Haskell in Haskell. Meijer, Erik (ed), Proceedings of the 1999 haskell workshop. Technical Reports, nos. UU–CS–1999–28. Available at ftp://ftp.cs.uu.nl/pub/RUU/CS/techreps/CS-1999/1999-28.pdf.Google Scholar
Kfoury, AJ, & Tiuryn, J. (1992). Type reconstruction in finite rank fragments of second-order lambda calculus. Information and Computation, 98 (2), 228257.Google Scholar
Kfoury, AJ, & Wells, JB. (1994). A direct algorithm for type inference in the rank-2 fragment of the second-order lambda calculus. Pages 196207 of: ACM Symposium on Lisp and Functional Programming. Orlando, Florida: ACM.Google Scholar
Lämmel, Ralf, & Peyton Jones, Simon. (2003). Scrap your boilerplate: a practical approach to generic programming. Pages 2637 of: ACM SIGPLAN International Workshop on Types in Language Design and Implementation (TLDI'03). New Orleans: ACM Press.Google Scholar
Launchbury, J, & Peyton Jones, SL. (1995). State in Haskell. Lisp and Symbolic Computation, 8 (4), 293342.Google Scholar
Le Botlan, D, & émy, D. (2003). MLF: raising ML to the power of System F. In: ICFP03, 2003.Google Scholar
Lee, Oukseh, & Yi, Kwangkeun. (1998). Proofs about a folklore let-polymorphic type inference algorithm. ACM Transactions on Programming Languages and Systems, 20 (4), 707723.Google Scholar
Leijen, Daan, & Löh, Andres. (2005). Qualified types for MLF. In: ICFP05, 2005.Google Scholar
Longo, Giuseppe, Milsted, Kathleen, & Soloviev, Sergei. (1995). A logic of subtyping (extended abstract). Pages 292–299 of: |lics95|.Google Scholar
Miller, Dale. (1992). Unification under a mixed prefix. J. symb. comput., 14 (4), 321358.Google Scholar
Milner, R. (1978). A theory of type polymorphism in programming. Jcss, 13 (3).Google Scholar
Mitchell, John C. (1988). Polymorphic type inference and containment. Inf. comput., 76 (2–3), 211249.Google Scholar
Morrisett, G. 1995 (Dec.). Compiling with types. Ph.D. thesis, Carnegie Mellon University.Google Scholar
Odersky, M, & Läufer, K. (1996). Putting type annotations to work. Pages 5467 of: 23rd ACM Symposium on Principles of Programming Languages (POPL'96). St Petersburg Beach, Florida: ACM.Google Scholar
Odersky, Martin, Zenger, Matthias, & Zenger, Christoph. (2001). Colored local type inference. 28th ACM Symposium on Principles of Programming Languages (POPL'01). London: ACM.Google Scholar
Okasaki, C. (1999). From fast exponentiation to square matrices: an adventure in types. Pages 2835 of: ACM SIGPLAN International Conference on Functional Programming (ICFP'99). Paris: ACM.Google Scholar
Peyton Jones, Simon. (2001). Tackling the awkward squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell. Pages 4796 of: Hoare, CAR, Broy, M, & Steinbrueggen, R (eds), Engineering theories of software construction, Marktoberdorf Summer School 2000. NATO ASI Series. IOS Press.Google Scholar
Peyton Jones, SL, & Santos, A. (1998). A transformation-based optimiser for Haskell. Science of Computer Programming, 32 (1–3), 347.CrossRefGoogle Scholar
Pierce, Benjamin. (2002). Types and programming languages. MIT Press.Google Scholar
Pierce, Benjamin C., & Turner, David N. (1998). Local type inference. Pages 252265 of: 25th ACM Symposium on Principles of Programming Languages (POPL'98). San Diego: ACM.Google Scholar
Pottier, F, & Rémy, D. (2004). ML. Pierce, BC (ed), Advanced topics in types and programming languages. MIT Press.Google Scholar
Rémy, Didier. (2005). Simple, partial type inference for System F, based on type containment. In: ICFP05, 2005.Google Scholar
Shao, Zhong. 1997 (June). An overview of the FLINT/ML compiler. Proc. 1997 ACM SIGPLAN workshop on types in compilation (TIC'97).Google Scholar
Shields, Mark, & Peyton Jones, Simon. (2002). Lexically scoped type variables. Microsoft Research.Google Scholar
Tarditi, D, Morrisett, G, Cheng, P, Stone, C, Harper, R, & Lee, P. (1996). TIL: A type-directed optimizing compiler for ML. Pages 181192 of: ACM Conference on Programming Languages Design and Implementation (PLDI'96). Philadelphia: ACM.Google Scholar
Tiuryn, J, & Urzyczyn, P. (1996). The subtyping problem for second order types is undecidable. Proc. IEEE Symposium on Logic in Computer Science (LICS'96).CrossRefGoogle Scholar
Tiuryn, Jerzy. (2001). A sequent calculus for subtyping polymorphic types. Inf. comput., 164 (2), 345369.CrossRefGoogle Scholar
Vytiniotis, Dimitrios, Weirich, Stephanie, & Peyton Jones, Simon. 2005 (July). Practical type inference for arbitrary-rank types, Technical Appendix. Tech. rept. MS-CIS-05-14. University of Pennsylvania.Google Scholar
Wells, JB. (1999). Typability and type checking in system F are equivalent and undecidable. Ann. Pure Appl. Logic, 98, 111156.Google Scholar
Submit a response

Discussions

No Discussions have been published for this article.