| Type Inference |
Article Index for Type |
Website Links For Type |
Information AboutType Inference |
| CATEGORIES ABOUT TYPE INFERENCE | |
| type theory | |
|
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., () : and Polymorphism , it is not always possible for the compiler to infer as much, however, and type annotations are occasionally necessary for disambiguation. EXAMPLE For example, let us consider the Haskell function length, which may be defined as:length {Link without Title} = 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 :: {Link without Title} -> Int Since there are no Ad-hoc Polymorphic subfunctions in the function definition, we can declare the function to be Parametric Polymorphic . HINDLEY-MILNER TYPE INFERENCE ALGORITHM The common algorithm used to perform the type inference is the one now commonly referred to as Hindley-Milner or Damas-Milner algorithm. 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. 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. 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 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 .
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 . Solving the equations Solving the equations proceeds by unification. This is - maybe surprisingly - a rather simple algorithm. The function 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: , 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 . REFERENCES EXTERNAL LINK
|
|
|