HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

Type inference

Categories: Glossary

Type inference is a feature of the type system which means that concrete types are deduced by the type system whereever it is obvious. If you add an integer variable x to a numeric literal 2, then the type system concludes that 2, which in principle can represent 2 for every number type, must also be an integer, since + supports only addition of numbers of the same type. (This restriction is a good thing, as we explain for the idea of a Generic number type.)

Another example: There are the following standard functions:

map :: (a -> b) -> [a] -> [b]
Char.ord :: (Char -> Int)

For the expression map ord the type checker finds out that the type variable a must be bound to the type Char and b must be bound to Int and thus it concludes

map ord :: [Char] -> [Int]

You can play with the type inference mechanism in Hugs and GHCi. See Determining the type of an expression.

See also

The type inference mechanism is very similar to unification in PROLOG.

Retrieved from "http://www.haskell.org/haskellwiki/Type_inference"

This page has been accessed 505 times. This page was last modified 07:35, 29 November 2007. Recent content is available under a simple permissive license.