Type inference is a feature present in some strongly statically typed programming languages. It is often characteristic of — but not limited to — functional programming languages in general. Some languages that include type inference are: Haskell, Cayenne, Clean, ML, OCaml, Epigram, Scala, Nemerle, D and Boo. This feature is planned for Fortress, C# 3.0 and C++0x.
Type inference refers to the ability to automatically either partially or fully deduce the type of the value derived from the eventual evaluation of an expression. As this process is systematically performed at compile time, the compiler is often able to infer the type of a variable or the type signature of a function, without explicit type annotations having been given. In many cases, it is possible to omit type annotations from a program completely if the type inference system is robust enough, or the program or language simple enough.
To obtain the information required to correctly infer the type of an expression lacking an explicit type annotation, the compiler either gathers this information as an aggregate and subsequent reduction of the type annotations given for its subexpressions (which may themselves be variables or functions), or through an implicit understanding of the type of various atomic values (e.g., () : Unit; true : Bool; 42 : Integer; 3.14159 : Real; etc.). It is through recognition of the eventual reduction of expressions to implicitly typed atomic values that the compiler for a type inferring language is able to compile a program completely without type annotations. In the case of highly complex forms of higher order programming and polymorphism, it is not always possible for the compiler to infer as much, however, and type annotations are occasionally necessary for disambiguation.
length, which may be defined as:
length * = 0
length (first:rest) = 1 + length rest
From this, it is evident that the function handles lists as inputs, and the base case of this recursive function returns an integer (Haskell "Int"). So we can reliably construct a type signature length :: * -> Int
Since there are no ad-hoc polymorphic subfunctions in the function definition, we can declare the function to be parametric polymorphic.
The origin of this algorithm is the type inference algorithm for the simply typed lambda calculus, which was devised by Haskell B. Curry and Robert Feys in 1958.
In 1969 Roger Hindley extended this work and proved that their algorithm always inferred the most general type.
In 1978 Robin Milner, independently of Hindley's work, provided an equivalent algorithm,
In 1985 Luis Damas finally proved that Milner's algorithm is complete and extended it to support systems with polymorphic references.
and
where is a primitive expression (such as "3") and is a primitive type (such as "Integer").
We want to construct a function of type , where is a type environment and is a term. We assume that this function is already defined on primitives. The other cases are:
whenever the binding is in
whenever where and .
where and is extended by the binding .
Note that so far we do not specify what to do when we fail to meet the various conditions. This is because, in the simple type *checking* algorithm, the check simply fails whenever anything goes wrong.
Now, we develop a more sophisticated algorithm that can deal with type variables and constraints on them. Therefore, we extend the set T of primitive types to include an infinite supply of variables, denoted by lowercase Greek letters
This is a limited overview. For now, refer to Types and Programming Languages by Benjamin Pierce, Sections 22.1-4 .
Unifying the empty set of equations is easy enough: , where is the identity substitution.
Unifying a variable with a type goes this way: , where is the substitution composition operator, and is the set of remaining constraints with the new substitution applied to it.
Of course, .
The interesting case remains as .
Typinferenz | Inferencia de tipos | Inférence de types | 型推論 | Модель типизации Хиндли-Милнера
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Type inference".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world