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
int
orstring
) or type variables (likeα
andβ
). - Type constants can either be concrete types (like
int
andstring
) or type constructors (likeMap
andSet
). - Type variables (like
α
andβ
) behave as placeholders for concrete types (likeint
andstring
).
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
int
andstring
) and type variables (likeα
andβ
) are of kind*
. - Type constructors (like
Map
andSet
) are lambda abstractions of types (e.g.Set
is of kind* -> *
andMap
is 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 ∀α₁ . . . ∀αₙ.τ.