Skip to main content Accessibility help

Practical type inference for arbitrary-rank types



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.



Hide All
Baars, Arthur L, & Swierstra, S. Doaitse. (2002). Typing dynamic typing. Pages 157166 of: ACM SIGPLAN International Conference on Functional Programming (ICFP'02). Pittsburgh: ACM.
Bird, Richard, & Paterson, Ross. (1999). De Bruijn notation as a nested datatype. Journal of Functional Programming, 9 (1), 7791.
Cardelli, L. (1987). Basic polymorphic typechecking. Science of Computer Programming, 8 (2), 147172.
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.
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.
Garrigue, Jacques, & Remy, Didier. (1999). Semi-explicit first-class polymorphism for ML. Journal of information and computation, 155, 134169.
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.
Girard, J-Y. (1990). The system F of variable types: fifteen years later. Huet, G (ed), Logical foundations of functional programming. Addison-Wesley.
Hancock, P. (1987). A type checker. Pages 163182 of: Peyton Jones, SL (ed), The implementation of functional programming languages. Prentice Hall.
Heeren, B, Hage, J, & Swierstra, SD. (2003). Scripting the type inference process. In: CFP03, 2003.
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.
Hinze, Ralf. (2001). Manufacturing datatypes. Journal of Functional Programming, 1.
Huet, G. (2002). Higher order unification 30 years later. 15th international workshop on higher order logic theorem proving and its applications (IWHOLTP'02). LNCS.
ICFP03. (2003). ACM SIGPLAN International Conference on Functional Programming (ICFP'03). Uppsala, Sweden: ACM.
ICFP05. (2005). ACM SIGPLAN International Conference on Functional Programming (ICFP'05). Tallinn, Estonia: ACM.
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
Kfoury, AJ, & Tiuryn, J. (1992). Type reconstruction in finite rank fragments of second-order lambda calculus. Information and Computation, 98 (2), 228257.
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.
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.
Launchbury, J, & Peyton Jones, SL. (1995). State in Haskell. Lisp and Symbolic Computation, 8 (4), 293342.
Le Botlan, D, & émy, D. (2003). MLF: raising ML to the power of System F. In: ICFP03, 2003.
Lee, Oukseh, & Yi, Kwangkeun. (1998). Proofs about a folklore let-polymorphic type inference algorithm. ACM Transactions on Programming Languages and Systems, 20 (4), 707723.
Leijen, Daan, & Löh, Andres. (2005). Qualified types for MLF. In: ICFP05, 2005.
Longo, Giuseppe, Milsted, Kathleen, & Soloviev, Sergei. (1995). A logic of subtyping (extended abstract). Pages 292–299 of: |lics95|.
Miller, Dale. (1992). Unification under a mixed prefix. J. symb. comput., 14 (4), 321358.
Milner, R. (1978). A theory of type polymorphism in programming. Jcss, 13 (3).
Mitchell, John C. (1988). Polymorphic type inference and containment. Inf. comput., 76 (2–3), 211249.
Morrisett, G. 1995 (Dec.). Compiling with types. Ph.D. thesis, Carnegie Mellon University.
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.
Odersky, Martin, Zenger, Matthias, & Zenger, Christoph. (2001). Colored local type inference. 28th ACM Symposium on Principles of Programming Languages (POPL'01). London: ACM.
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.
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.
Peyton Jones, SL, & Santos, A. (1998). A transformation-based optimiser for Haskell. Science of Computer Programming, 32 (1–3), 347.
Pierce, Benjamin. (2002). Types and programming languages. MIT Press.
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.
Pottier, F, & Rémy, D. (2004). ML. Pierce, BC (ed), Advanced topics in types and programming languages. MIT Press.
Rémy, Didier. (2005). Simple, partial type inference for System F, based on type containment. In: ICFP05, 2005.
Shao, Zhong. 1997 (June). An overview of the FLINT/ML compiler. Proc. 1997 ACM SIGPLAN workshop on types in compilation (TIC'97).
Shields, Mark, & Peyton Jones, Simon. (2002). Lexically scoped type variables. Microsoft Research.
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.
Tiuryn, J, & Urzyczyn, P. (1996). The subtyping problem for second order types is undecidable. Proc. IEEE Symposium on Logic in Computer Science (LICS'96).
Tiuryn, Jerzy. (2001). A sequent calculus for subtyping polymorphic types. Inf. comput., 164 (2), 345369.
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.
Wells, JB. (1999). Typability and type checking in system F are equivalent and undecidable. Ann. Pure Appl. Logic, 98, 111156.


Full text views

Total number of HTML views: 0
Total number of PDF views: 0 *
Loading metrics...

Abstract views

Total abstract views: 0 *
Loading metrics...

* Views captured on Cambridge Core between <date>. This data will be updated every 24 hours.

Usage data cannot currently be displayed

Practical type inference for arbitrary-rank types

Submit a response


No Discussions have been published for this article.


Reply to: Submit a response

Your details

Conflicting interests

Do you have any conflicting interests? *