|
The type system of the Haskell programming language includes a construct called the type class that provides a powerful form of restricted parametric polymorphism. A type class is defined by giving 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. On computer science, a datatype (often simply type) is a name or label for a set of values and some operations which can be performed on that set of values. ...
Haskell logo Haskell is a standardized pure functional programming language with non-strict semantics. ...
Polymorphism refers to features of various programming languages which allow a single piece of source code to operate on a variable whose type is not fixed. ...
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, he may then make his new type an instance of Ord by providing a function to compare values of that type in whatever way he considers appropriate. Once he has done this, he may use a sorting function of the type just given to sort lists of elements of his 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, so 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 =>"). In, object-oriented programming, a class consists of a description of a collection of encapsulated instance variables and methods, possibly with implementation of those types together with a constructor function that can be used to create objects of the class. ...
Another way in which typeclasses differ from OO classes is that typeclasses 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 typeclass 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. The Glasgow Haskell Compiler (or GHC) is an open source Native code Compiler for the functional programming language Haskell which was developed at the University of Glasgow. ...
This article contains information that has not been verified and thus might not be reliable. ...
See also In computer science, polymorphism means allowing a single definition to be used with different types of data (specifically, different classes of objects). ...
Haskell logo Haskell is a standardized pure functional programming language with non-strict semantics. ...
In computer programming, operator overloading (less commonly known as operator ad-hoc polymorphism) is a specific case of polymorphism in which some or all of operators like +, = or == are treated as polymorphic functions and as such have different behaviours depending on the types of their arguments. ...
This article contains information that has not been verified and thus might not be reliable. ...
References - Philip Wadler, Stephen Blott. How to make ad-hoc polymorphism less ad hoc. From Proc. 16th ACM Symposium on Principles of Programming Languages. January, 1989
External links - A Gentle Introduction to Haskell, Version 98, chapter 5. Type Classes and Overloading. June 2000.
- Advanced Functional Programming course at Utrecht University, 74 lecture slides on Advanced Type Classes. 2005-06-07.
- The Haskell Wiki, page Type Classes, Dictionaries, and Type-based Dispatch.
- The Haskell Wiki, page Underestimated Type Classes
- Simon Peyton Jones, Mark Jones, Erik Meijer. Type classes: exploring the design space. Haskell Workshop 1997.
|