ENCYCLOPEDIA 4U .com



Encyclopedia Home Page

Google
  Web Encyclopedia4u.com

 

Type inference

Type inference is a feature in functional programming languages such as Haskell and ML.

Type inference automatically assigns a type signature onto a function if it is not given. In a sense, the type signature is reconstructed from the compile/interpreter's understanding of the function's subfunctions with well defined type signatures, and thus the input/output type can be ascertained.

For example, let us consider the Haskell function length, and it is 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 :: [a] -> a

Since there are no ad-hoc polymorphic subfunctions in the function definition, we can declare the function to be parametric polymorphic.




Content on this web site is provided for informational purposes only. We accept no responsibility for any loss, injury or inconvenience sustained by any person resulting from information published on this site. We encourage you to verify any critical information with the relevant authorities.



Copyright © 2005 Par Web Solutions All Rights reserved.
| Privacy

This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Type inference".