Type class

Type classes are a construct of functional programming for the implementation of ad-hoc polymorphism. Type classes have been developed for the language Haskell. They are similar in principle to the concept of interfaces, but have nothing to do with the classes of the object-oriented programming.

History

Type classes were originally developed to overloaded mathematical operators in a systematic way. The approach proved to be very versatile, so you quickly to other things, like used it eg monads. Nowadays, type classes are one of the most important tools of the language Haskell and found in almost all areas of application, usually interface definition or generalization of libraries.

Introduction

Type classes define functions that can be called for each instance of the type class. You can create an instance for each type by defining the functions of the type class for each type. A simple example is the operator (==), the two variables checked for equality. ( In Haskell operators are the same as functions and are not treated separately, but they are used in infix notation ) The associated type class Eq (. Engl of equality ) is defined as follows:

Class Eq a where    ( ==) :: A -> a -> Bool    ( / =) :: A -> a -> Bool      a == b = not (a / = b)    a / = b = not ( a == b ) The class defines two functions that each have two variables of type a, the type of the class, as arguments parameters: ( ==) is an operator that checks two variables for equality, the operator ( / = ) tests for inequality. Its symbol is derived from the mathematical sign. In addition to the definition of the operators (lines 2 and 3), in which its type is specified, you can specify a default implementation of the operators. This is for example useful if some functions are potentially redundant, but is more efficient for certain instances of a particular implementation. In the case of Eq both functions are defined by each other, so it is sufficient only to implement.

To define an instance of a type class, you write the following ( here the example of type Bool):

Instance Eq Bool where    True == True = True    False == False = True    _ == _ = False The instance overloads only the function ( ==). The test for inequality, as described above, already automatically defined as the negation of equality. By definition of the instance checking for equality can now be used for boolean values.

The trick now is that you do not need to know about which data types are to apply a function of type class for him. It is sufficient that an instance of the type class exists. This information can be added in Haskell on an extension of the type system. Here, for example, the function nub: it removes duplicates from a list. About the elements of the list is known only that they are instances of the type class Eq. This is communicated via the context Eq a front of the type signature:

Nub :: Eq a = > [ a] -> [ a] nub [ ] = [] nub (x: xs ) = x: nub (filter ( / = x) xs ) Examples of use

Type classes are used in the language Haskell for many purposes, for example:

Implementation

There are several ways to implement type classes. The original and, used in most implementations, including the Glasgow Haskell Compiler implementation of type classes is explained in the following.

Normally, you implement type classes by each type class is replaced by a data type. It contains fields than the individual methods of the type class. Now, if a function requires an instance of a type class for one of the parameters, so an object of the desired type class is passed the associated data type, which represents the instance. In this way, no expansion of the existing type system is needed for the final code. One can think of the example of the above-mentioned class Eq as follows:

The type class Eq is translated into a data type Eq. ( In a real implementation may have a different name is used) This takes a type parameter, contrary to to instantiate types and has fields of type classes as methods:

Data Eq a = Eq (a -> a -> Bool) (a -> a -> Bool) For each instance now a variable that is generated by the type Eq:

InstanceEqBool :: Eq Bool instanceEqBool = Eq eqBool ( \ ab - > not ( eqBool ab) ) where    eqBool True True = True    eqBool False False = True    eqBool __ = False   - Here is another example: the unit type ( ) instanceEqUnit :: eq () instanceEqUnit = Eq ( \ __ -> True) ( \ __ -> False) If now a function of the context Eq a required, this will be translated into an additional parameter of type Eq a. For this parameter, the required methods are then invoked. If a called from this function function context also required as it is passed. The type system guarantees that the supplied functions have the correct type:

Nub :: Eq a = > [ a] -> [ a] - Is to nub :: Eq a - > [ a] -> [ a] nub _ [ ] = [] nub (Eq _ ( / =)) (x: xs ) = x: nub (filter ( / = x) xs ) References

788225
de