I'm reading the Wikipedia article on Hindley–Milner Type Inference trying to make some sense out of it. So far this is what I've understood:
- Types are classified as either monotypes or polytypes.
- Monotypes are further classified as either type constants (like
intorstring) or type variables (likeαandβ). - Type constants can either be concrete types (like
intandstring) or type constructors (likeMapandSet). - Type variables (like
αandβ) behave as placeholders for concrete types (likeintandstring).
Now I'm having a little difficulty understanding polytypes but after learning a bit of Haskell this is what I make of it:
- Types themselves have types. Formally types of types are called kinds (i.e. there are different kinds of types).
- Concrete types (like
intandstring) and type variables (likeαandβ) are of kind*. - Type constructors (like
MapandSet) are lambda abstractions of types (e.g.Setis of kind* -> *andMapis of kind* -> * -> *).
What I don't understand is what do qualifiers signify. For example what does ∀α.σ represent? I can't seem to make heads or tails of it and the more I read the following paragraph the more confused I get:
A function with polytype ∀α.α -> α by contrast can map any value of the same type to itself, and the identity function is a value for this type. As another example ∀α.(Set α) -> int is the type of a function mapping all finite sets to integers. The count of members is a value for this type. Note that qualifiers can only appear top level, i.e. a type ∀α.α -> ∀α.α for instance, is excluded by syntax of types and that monotypes are included in the polytypes, thus a type has the general form ∀α₁ . . . ∀αₙ.τ.