This semester, we have been type-annotating all of our declarations and functions, which gives them a concrete type that we can them use them at.
In general, however, some functions that we write have the property that
they could be used more generally than we annotate them to be. For instance,
we can write the
length : int list -> int function, but if not for
the fact that we specified it to be for
int lists, there's no reason why
it should not also be able to be used for
This lecture explores that idea by introducing parametric polymorphism, which is a language feature in SML that allows polymorphic types which makes use of type variables, which allow a given value to be used at multiple different types. This allows us the power of writing code which can be used generically, without needing to constrain it to particular use cases.
Our ability to make use of polymorphic types is also greatly aided by the fact that Standard ML can perform type inference, which allows it to determine the types of expressions, without any annotations needed. Type annotations turn out to actually be totally optional, which allows us to take full power of the SML type system.
We finished off the lecture by discussing parameterized types,
such as the
list type constructor, which allow us to create types from arbitrary
other types. We also implemented a generic
sort function, which achieved a
polymorphic type by taking in a function, which was itself a generic comparison