[Haskell-cafe] Help with Type Class

Alson Kemp alson at alsonkemp.com
Tue Feb 28 19:26:44 EST 2006

  Although the discussion about Array refactoring died
down quickly on the Haskell' mailing list, I've been
noodling on refactoring the various Collections in
Haskell.  In doing so, I've bumped into a problem with
type classes that I can't resolve.  The issue is as

  I'm designing a Collections class heirarchy that is
a blend between that of Java and of Haskell. The
problem is that, whereas in OOP it is easy to do so,
Haskell class mechanism seems to make it difficult to
add *new* member variable to subclasses.

  Simple goal: create a top-level Collection with an
*indexed* Array subclass (and various other
subclasses).  The problem I'm running into is
Collection has no need of an "index" variable and I
can't seem to figure out how to add an "index" to
Array when subclassing from Collection  Mock up:

  class CollectionClass c e where
    -- every Collection supports toList...
    toList :: c e -> [e]

  class (CollectionClass a e)=> ArrayClass a e where

  data Array i e = Array i i 

  instance CollectionClass Array e where
    -- Since Array is a Collection
    -- toList :: c e -> [e]
    -- but with an Array the type would be
    -- toList :: a i e -> [e]
    toList = ...


  I think that the problem goes away if:
    class CollectionClass c x e where ...
  with "x" used an index for Array or a key for Map or
() for Set, but it doesn't seem clean to scatter
member variables in the parent class in case the
subclass requires them...

  Another possible solution that I couldn't get to
work would be to use (Array i) as the type for "c" in
"Collection c":
  instance CollectionClass (Array i) e where
    toList = ... -- (Array i) e -> [e] ?

  This seems clean because it says the Collection
holds "e"s and is organized by an (Array i). 
Similarly, Set would be a Collection of "e"s organized
by Set and Map would be a Collection of "e"s organized
by (Map k).

  Undoubtedly, I've missspoken some crucial aspect of
the type/kind/class/instance magical incantation. 

  - Alson

More information about the Haskell-Cafe mailing list