Type inference

Most modern languages do not require the programmer to explicitly declare the type of an expression, they are able to infer the type, if one exists.

The expression is decomposed into smaller sub-expressions, and when an atomic expression is found, the available information is taken from the predefinied constant that forms the expression, or if it is not a predefined expression, the most general type possible is assigned to it. The way that different components are put together to form the expression indicates the constraints that the type variables must satisfy.

For example:


square x = x * x

square square 3
-- This expression is rejected, since the inferred type of square is
square :: Integer -> Integer
-- ... and therefore its argument cannot be a function.