[Haskell-beginners] Class definition syntax

Shawn Willden shawn-haskell at willden.org
Sat Oct 31 12:27:10 EDT 2009

I have a program that makes use of various data types built on top of Arrays.  
In some cases, they're data types that contain an Array plus some additonal 
information, in others, they're just "newtype" Arrays, so that I can use 
typechecking to make sure that I'm not using the wrong kind of object.

I'd really like to define an "ArrayOps" class with all of the operations I 
need, and define instances for all of the specific types.  I also use 
some "raw" Array objects, so it would be even better if I could make an 
instance of my class for Array.  And, ideally, I'd like to use the Array 
operations for my class operations.

So, I want something like:

class ArrayOps a where
    (!)    :: a -> i -> e
    (//)   :: a -> (i,e) -> a
    bounds :: a -> (i,i)
    range  :: a -> [i]

'i' and 'e' are the index and element types, respectively.

Obviously, the signatures above reference type variables that aren't declared, 
and really must be constrained to be the 'i' and 'e' that were used in 
building the type 'a' (which is an Array i e).  Something like the following 
(though this obviously doesn't work):

class ((Array.Array i e) a) => ArrayOps a where ...

I'm sure there must be a way to do this, but I can't figure out what the 
syntax would look like.



More information about the Beginners mailing list