Type class

From Wikipedia, the free encyclopedia

The type class is a construct of the type system of the Haskell programming language that provides a powerful form of restricted parametric polymorphism. Haskell's support for ad hoc polymorphism, also known as overloading, is based on type classes.

A type class is defined by specifying a set of operations (or "methods") that must be implemented for every type in the class. For example, the predefined class Ord contains types that are ordered, that is, types whose elements may be compared using < or <=. A function to sort a list (using <= for comparison) is given the type (Ord a) => ([a] -> [a]). That is, it is a function that can take a list of elements of any type a and return a list of the same type, provided that a is in the class Ord. The parentheses in the type are unnecessary but make its meaning clearer: this type captures the fact that the sorting function can take elements of many different types (and is therefore polymorphic), but that the elements of a list to be sorted cannot be just anything: it must be possible to compare them.

A programmer can make any type t a member of a given class C using an instance declaration that defines implementations of all of C's methods for the particular type t. For instance, if a programmer defines a complex new data type, she may then make this new type an instance of Ord by providing a function to compare values of that type in whatever way she considers appropriate. Once she has done this, she may use a sorting function of the type just given to sort lists of elements of her type. Programmers may also define new type classes of their own.

Note that type classes are rather different from classes in object-oriented programming languages; in particular, Ord is not a type: there is no such thing as a value of type Ord. Thus the Haskell approach to a generic sorting function as outlined here is quite different from the subtyping-based approach often seen in object-oriented programming. Type classes are in fact much more closely related to parametric polymorphism (note that the type of the sorting function would be the parametrically polymorphic type [a] -> [a] if it were not for the type class constraint "Ord a =>").

Another way in which type classes differ from OO classes is that type classes in general permit multiple type parameters, making them into relations on types. For example, in the GHC library, a general immutable array interface is expressed using a class IArray, where the type class constraint IArray a e expresses the fact that a is an array type capable of holding elements of type e. (Various unboxed array types have restricted polymorphism in this fashion.) In addition to this mechanism, functional dependencies are permitted between the type parameters. That is, one can assert that for a given assignment of some set of the type parameters, another set of them will have a unique solution. As an example, general monads m which carry a state parameter of type s satisfy the type class constraint MonadState s m, where there is a functional dependency m -> s, which means that for a given monad, the state type accessible from this interface is uniquely determined. This aids the system in type inference, as well as providing a fairly expressive mechanism for type-directed programming.

[edit] See also

[edit] References

[edit] External links