What is type inference?
Historically, type inference (or type reconstruction) meant that all types in a program can be derived without requiring essentially any explicit type annotation. However, in recent years, it has become en vogue in the programming language mainstream to label even the most trivial forms of bottom-up type deduction as "type inference" (e.g., the new auto
declarations of C++11). So people have started adding "full" to refer to the "real" thing.
What is full type inference?
There is a broad spectrum to what degree a language can infer types, and in practice, almost no language supports "full" type inference in the strictest sense (core ML is about the only example). But the main distinguishing factor is whether types can be derived for bindings that do not have a "definition" attached to them — in particular, parameters of functions. If you can write, say,
f(x) = x + 1
and the type system figures out that f e.g. has type Int → Int, then it makes sense to call this type inference. Moreover, we talk about polymorphic type inference when, e.g.,
g(x) = x
is assigned the generic type ∀(t) t → t automatically.
Type inference was invented in the context of the simply-typed lambda calculus, and polymorphic type inference (aka Hindley/Milner type inference, invented in the 1970s) is the claim to fame of the ML family of languages (Standard ML, OCaml, and arguably Haskell).
What are the limits of full type inference?
Core ML has the luxury of "full" polymorphic type inference. But it hinges on certain limitations of polymorphism in its type system. In particular, only definitions can be generic, not function arguments. That is,
id(x) = x;
id(5);
id(True)
works fine, because id
can be given polymorphic type when the definition is known. But
f(id) = (id(5); id(True))
does not type check in ML, because id
cannot be polymorphic as a function argument. In other words, the type system does allow polymorphic types like ∀(t) t → t, but not so-called higher-rank polymorphic types like (∀(t) t → t) → Bool, where polymorphic values are used in a first-class manner (which, just to be clear, even very few explicitly typed languages allow).
The polymorphic lambda calculus (also called "System F"), which is explicitly typed, allows the latter. But it is a standard result in type theory that type reconstruction for full System F is undecidable. Hindley/Milner hits a sweet spot of a slightly less expressive type system for which type reconstruction still is decidable.
There are more advanced type system features that also make full type reconstruction undecidable. And there are others that keep it decidable but still make it infeasible, e.g. the presence of ad-hoc overloading or subtyping, because that leads to combinatorial explosion.