APPSEM '98

Program Structuring in Haskell

Mark P Jones

Slide 7

Back to start

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Previous

Next

Of course, in a language with a polymorphic type system, it isn't always possible to know which version of the equality operator will be required. This slide shows the definition of a function that tests for membership in a list. The details of the definition aren't really important, except to notice that it involves the (==) operator, so the function only makes sense when the type of the elements in the list is an equality type. We can capture this by including a predicate "Eq a" in the type of the "elem" function, which represents a deferred constraint. So "elem" is still a polymorphic function because it can be used at many different types, but it is not fully polymorphic because of the restriction to equality types. For this reason, we refer to types containing deferred predicates as "qualified types". Types like this are important because they enable us to write generic definitions, like the one for "elem", that work in a similar way across all instances of a class.

The Eq class is obviously quite closely related to the notion of equality types in Standard ML. Indeed, type classes can be seen as a natural generalization of that idea, which gives programmers the additional facilities to extend existing classes, or to add completely new classes of their own.

In fact most current implementations of Haskell support three significant extensions of the type class mechanisms that I have described here ...

Next...