Type inference

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Type systems

Type safety
Manifest vs. Inferred
Static vs. Dynamic
Strong vs. Weak
Nominal vs. Structural
Duck typing
Dependent typing
Uniqueness typing
Linear typing

Type inference refers to the ability to deduce automatically the type of a value in a programming language. It is a feature present in some strongly statically typed languages. It is often characteristic of — but not limited to — functional programming languages in general. Some languages that include type inference are C# (starting with version 3.0), Clean, Haskell, ML, OCaml, Scala. This feature is also planned for C++0x and Perl 6. The ability to infer types automatically makes many programming tasks easier, leaving the programmer free to omit type annotations while maintaining roughly the same amount of type safety as would be provided with explicit type annotations.

Contents

[edit] Nontechnical explanation

In most programming languages, all values have a type which describes the kind of data a particular value describes. In some languages, the type of an expression is known only at runtime; these languages are dynamically typed. In other languages, the type of an expression is known at compile time; these languages are statically typed. In statically typed languages, the input and output types of functions and local variables ordinarily must be explicitly provided by type annotations. For example, in C:

int addone(int x) {
    int result; /*declare integer result (C language)*/
 
    result = x+1;
    return result;
}

The beginning of this function definition, int addone(int x) declares that addone is a function which takes one argument, an integer, and returns an integer. int result; declares that the local variable result is an integer. In a proposed language where type inference is available, the code might be written like this instead:

addone(x) {
    val result;  /*inferred-type result */
    val result2; /*inferred-type result #2 */

    result = x+1;
    result2 = x+1.0;  /* this line won't work (in the proposed language) */
    return result;
}

This looks very similar to how code is written in a dynamically typed language, but with some extra constraints (described below) it would be possible to infer the types of all the variables at compile time. In the example above, the compiler would infer that result and x have type integer and addone is a function int -> int. The variable result2 isn't used in a legal manner, so it wouldn't have a type.

In the imaginary language in which the last example is written, the compiler would assume that, in the absence of information to the contrary, + takes two integers and returns one integer. (This is how it works in, for example, OCaml). From this, the type inferencer can infer that the type of x+1 is an integer, which means result is an integer and thus the return value of addone is an integer. Similarly, since + requires that both of its arguments be integers, x must be an integer, and therefore addone accepts one integer as an argument.

However, in the subsequent line, result2 is calculated by adding a decimal "1.0" with floating-point arithmetic, causing a conflict in the use of x for both integer and floating-point expressions. Such a situation would generate a compile-time error message. In a different language, result2 might have been implicitly declared as a floating-point variable, and the addition would implicitly convert x to a floating point. However, languages that support type inference to the degree the above example illustrates rarely support such implicit type conversions. Such a situation shows the difference between type inference, which does not involve type conversion, and implicit type conversion, which forces data to a different data type, often without restrictions.

[edit] Technical description

Type inference is the ability to automatically deduce, either partially or fully, the type of an expression 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 is simple enough.

To obtain the information required to infer the type of an expression, the compiler either gathers this information as an aggregate and subsequent reduction of the type annotations given for its subexpressions, or through an implicit understanding of the type of various atomic values (e.g. 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 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.

[edit] Example

For example, let us consider the Haskell function map, which applies a function to each element of a list, and may be defined as:

map f [] = []
map f (first:rest) = f first : map f rest

From this, it is evident that the function map takes a list as its second argument, that its first argument f is a function that can be applied to the type of elements of that list, and that the result of map is constructed as a list with elements that are results of f. So assuming that a list contains elements of the same type, we can reliably construct a type signature

map :: (a -> b) -> [a] -> [b]

where the syntax "a -> b" denotes a function that takes an a as its parameter and produces a b. "a -> b -> c" is equivalent to "a -> (b -> c)".

Note that the inferred type of map is parametrically polymorphic: The type of the arguments and results of f are not inferred, but left as type variables, and so map can be applied to functions and lists of various types, as long as the actual types match in each invocation.

[edit] Hindley–Milner type inference algorithm

The algorithm first used to perform type inference is now informally referred to as the Hindley–Milner algorithm, although the algorithm should properly be attributed to Damas and Milner[1], who called it "Algorithm W."

The origin of this algorithm is the type inference algorithm for the simply typed lambda calculus, which was devised by Haskell Curry and Robert Feys in 1958. In 1969 J. Roger Hindley extended this work and proved that their algorithm always inferred the most general type. In 1978 Robin Milner,[2] independently of Hindley's work, provided an equivalent algorithm, Algorithm W. In 1982 Luis Damas[3] finally proved that Milner's algorithm is complete and extended it to support systems with polymorphic references. The Hindley–Milner algorithm as described here supports a restricted version of System F.

[edit] The algorithm

The algorithm proceeds in two steps. First, we need to generate a number of equations to solve, then we need to solve them.

[edit] Generating the equations

The algorithm used for generating the equations is similar to a regular type checker, so let's consider first a regular type checker for the typed lambda calculus given by

e \, ::= E \mid v \mid (\lambda v:\tau. e) \mid (e\, e)

and

\tau \, ::= T \mid \tau \to \tau

where E is a primitive expression (such as "3") and T is a primitive type (such as "Integer").

We want to construct a function f which maps a pair (ε,t), where ε is a type environment and t is a term, to some type τ. We assume that this function is already defined on primitives. The other cases are:

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 α,β,...

See [4] for more details.

[edit] Solving the equations

Solving the equations proceeds by unification. This is—perhaps surprisingly—a rather simple algorithm. The function u operates on type equations and returns a structure called a "substitution". A substitution is simply a mapping from type variables to types. Substitutions can be composed and extended in the obvious ways.

Unifying the empty set of equations is easy enough: u\, \emptyset = \mathbf{i}, where \mathbf{i} is the identity substitution.

Unifying a variable with a type goes this way: u\, ([\alpha = T] \cup C) = u\, (C') \cdot (\alpha \mapsto T), where \cdot is the substitution composition operator, and C' is the set of remaining constraints C with the new substitution \alpha \mapsto T applied to it.

Of course, u\, ([T = \alpha] \cup C) = u ([\alpha = T] \cup C).

The interesting case remains as u\, ([S \to S' = T \to T']\cup C) = u \, (\{[S = T], [S' = T']\}\cup C).

A simple example would be a[i] = b[i] (assume this to be C-like syntax for this example). First Hindley–Milner would find that i must be of type int, further more that 'a' must be an "array of α" and 'b' must an "array of β". Now, since there is an assignment of β to α, α must be of the same type (assuming no implicit type conversions) as β. In the very least α must be a supertype of β.

[edit] References

  1. ^ Damas, Luis; Milner, Robin (1982), "Principal type-schemes for functional programs", POPL '82: Proceedings of the 9th ACM SIGPLAN-SIGACT symposium on Principles of programming languages, ACM, pp. 207--212, http://groups.csail.mit.edu/pag/6.883/readings/p207-damas.pdf 
  2. ^ Milner, Robin (1978), "A Theory of Type Polymorphism in Programming", Jcss 17: 348–375 
  3. ^ Damas, Luis; Milner, Robin (1982), "Principal type-schemes for functional programs", POPL '82: Proceedings of the 9th ACM SIGPLAN-SIGACT symposium on Principles of programming languages, ACM, pp. 207--212, http://groups.csail.mit.edu/pag/6.883/readings/p207-damas.pdf 
  4. ^ Pierce, Benjamin C. (2002), "Chapter 22", Types and Programming Languages, MIT Press, ISBN 0-262-16209-1 

[edit] External links

Personal tools
Namespaces
Variants
Actions
Navigation
Interaction
Toolbox
Print/export
Languages