type classes, superclass of different kind

Robert Will robertw at stud.tu-ilmenau.de
Thu Dec 11 11:16:31 EST 2003


Hello,

As you will have noticed, I'm designing a little library of Abstract Data
Structuresm here is a small excerpt to get an idea:

class Collection coll a where
    ...
    (<+>) :: coll a -> coll a -> coll a
    reduce :: (a -> b) -> b
              -> coll a -> b
    ...

class Map map a b where
    ...
    (<+) :: map a b -> map a b -> map a b
    at :: map a b
          -> a -> b
    ...

Note that the classes don't only share similar types, they also have
similar algebraic laws: both <+> and <+ are associative, and neither is
commutative.

Now I would like to have Collection to be a superclass of Map yielding the
following typing

    reduce :: (Map map a b) =>
              ((a, b) -> c) -> c
              -> map a b -> c

Note that in an OO programming language with generic classes (which is in
general much less expressive than real polymorphism), I can write

class MAP[A, B] inherit COLLECTION[TUPLE[A, B]]

which has exactly the desired effect (and that's what I do in the
imperative version of my little library).

There seems to be no direct way to achieve the same thing with Haskell
type classes (or any extension I'm aware of).  Here is a quesion for the
most creative of thinkers: which is the design (in proper Haskell or a
wide-spread extension) possibly include much intermediate type classes and
other stuff, that comes nearest to my desire?

I believe this question to be important and profound.  (We shouldn't
make our functional designs more different from the OO ones, than they
need to be.)  If I err, someone will tell me :->


Robert


More information about the Haskell mailing list