Class or parameterized type?

Graham Klyne gk@ninebynine.org
Mon, 17 Mar 2003 12:18:30 +0000


I'm trying to figure how to use parameterized types and/or classes.

Example: I wish to define a structured container type, let's call it a 
"RatsNest", that is type-variable in two ways:

(a) it is parameterized by a type of some class, let's call it "RatsTail", 
such that a RatsNest is a structure of things that have common 
properties.  Certain operations between RatsNest values (e.g. a merge) are 
valid only if they are containers for the same kind of RatsTail.  Such 
operations would not be different operations due to variation of thye 
contained type.

(b) it can be implemented in various ways;  e.g. a simple version is an 
in-memory data structure, while another may be implemented in database 
storage.  This corresponds to the Haskell idea of overloading, or classes.

This seems to call for a type-parameterized class specification, e.g.

[[
class (Eq n, RatsTail t) => (RatsNest t) n where
     merge :: ( (RatsNest t1) n1, (RatsNest t1) l2) => n1 -> n2 -> n2
]]

But this, as far as I can tell, is not valid Haskell.  Is there a way to 
achieve this effect?

What I can't figure out is a way to impose that the merge operation is not 
valid between *any* instances of RatsNest, but only those that are based on 
the same member element type, but *can* be used between different 
implementations (e.g. disk-based and memory-based).

#g


-------------------
Graham Klyne
<GK@NineByNine.org>
PGP: 0FAA 69FF C083 000B A2E9  A131 01B9 1C7A DBCA CB5E