Headline

Constraint on the type parameter of a type class or an instance

Illustration

Type-class constraints define bounds on type parameters used in the declaration of type classes or type-class instances (à la Language:Haskell). In this manner, type-class constraints feed into another form of bounded polymorphism.

Consider, for example, the following type-class instance for equality of pairs:

-- Equality of pairs
instance (Eq a, Eq b) => Eq (a,b)
  where
    x == y = fst x == fst y && snd x == snd y

Clearly, such equality needs to be defined in a component-wise manner: for the first ("fst") and the second ("snd") project of a pair. In the interest of polymorphism, the type of the components should not be fixed, but the availability of equality needs to be assumed for the component types. Thus, the two constraints in the head of the instance:

instance (Eq a, Eq b) => ...

Likewise, a type class may also be constrained. Consider, for example, the following type class for comparison:

class Eq a => Ord a where
  compare :: a -> a -> Ordering
  (<) :: a -> a -> Bool
  (>=) :: a -> a -> Bool
  (>) :: a -> a -> Bool
  (<=) :: a -> a -> Bool
  max :: a -> a -> a
  min :: a -> a -> a

Please observe the constraint:

class Eq a => ...

This class contains a constraint such that total order (comparison) can only be defined for types with equality. This is effectively a sanity check for the programmer because comparison subsumes the case for equality, conceptually. Without the constraint, a programmer may accidentally forget to implement equality, explicitly.

Importantly, type-class constraints propagate through (inferred) types of expressions. Consider these illustrations of inferring types of expressions at the interpreter prompt:

> :t (==)
(==) :: Eq a => a -> a -> Bool
> :t (==42)
(==42) :: (Eq a, Num a) => a -> Bool

Metadata


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.