APPSEM '98

Program Structuring in Haskell

Mark P Jones

Slide 8

Back to start

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Previous

Next

The first extension (in fact it is a longstanding feature of Haskell) is to allow programmers to specify hierarchies of classes. The class declaration shown here introduces (a simplified version of) Haskell's Ord class, all of whose instances can be compared using the (<=) operator. The first line of the declaration specifies that every instance of Ord must also be an instance of Eq. Think of the => symbol as superset rather than implication in this particular case, so it tells us that Eq is a superset of Ord. We say that Ord is a subclass of Eq, or that Eq is a superclass of Ord.

A constraint like this makes sense because, if you can compare two values using a (<=) operator, then there is obviously a way of testing them for equality (i.e., by taking (x==y) = (x<=y && y<=x)). In practice, it means that we can often simplify deferred constraints in a type, for example, by removing any constraint of the form Eq a if we also have another constraint of the form Ord a.

It is the task of Haskell compilers/interpreters to ensure that the instances declarations in a program are consistent with the hierarchy of classes. For example, a program containing an instance Ord T, for some type T, without also providing an instance Eq T, would be rejected.

Next...