[Haskell-beginners] Class definition syntax
jfredett at gmail.com
Sat Oct 31 12:33:10 EDT 2009
You'll probably need to look at associated types/functional
dependencies. The former is the new hotness, the latter is the old and
not-so-busted. A quick search of the wiki ought to reveal much more
than I can possibly explain, there is an example on the page for
Assoc. Types about generic Map implementation, which is similar to
what you're trying to do.
On Oct 31, 2009, at 12:27 PM, Shawn Willden wrote:
> 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
> information, in others, they're just "newtype" Arrays, so that I can
> 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
> instance of my class for Array. And, ideally, I'd like to use the
> 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
> 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
> (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
> syntax would look like.
> Beginners mailing list
> Beginners at haskell.org
More information about the Beginners