|
The third extension that I want to tell you about is
known as "multiple parameter classes". This is also
based on ideas that have been around for quite a long time,
but they are only now beginning to appear in full Haskell
implementations.
The intuition here is that a multiple parameter type class
corresponds to a predicate on multiple arguments, or, in
simple terms, to an n-ary relation between types and
constructors.
The example on this slide shows a fragment of a library
for working with collection classes, a mainstay
for traditional object-oriented class libraries.
The class relates collection constructors "c" to element
types "a", and provides operations for building empty
collections, inserting new elements, and enumerating the
existing elements. A full version would, of course, provide
many other operations, and would perhaps use a hierarchy of
classes to distinguish different kinds of collection.
The two instance declarations provide implementations of
collections involving lists (presumably, without worrying
about ordering or duplication of elements) and for some
abstract datatype of sets. The element type "a" is completely
unconstrained in the first instance declaration, but is
restricted to equality types in the second. (The
implementation of sets used here requires an equality
operation to build and use "Set" values.) Once again,
we can use the operations of the class to build generic
functions that can be used with any collection type.
In effect, these features just provide an extension of
parametric polymorphism, together with all the benefits
for modularity that polymorphism usually brings.
Next...
|