klequis / zz-haskell-notebook

Notes from learning Haskell
1 stars 0 forks source link

Type inference #41

Open klequis opened 2 years ago

klequis commented 2 years ago

Type inference is a faculty some programming languages, most notably Haskell and ML, have to infer principal types from terms without requiring explicit type annotations. There are, in some cases, terms in Haskell that can be well-typed but which have no principal type. In those cases, an explicit type annotation must be added.

The principle type is the most generic type that still type checks. Principal typing holds for that type system if a type can be found for a term in an environment for which all other types for that term are instances of the principal type. Given the inferred types:

a
Num a => a
Int

The principal type here is the parametrically polymorphic a.

Given these types:

(Ord a, Num a) => a
Integer

The principal type is (Ord a, Num a) => a