[Haskell-beginners] Class definition syntax

Daniel Fischer daniel.is.fischer at web.de
Sat Oct 31 12:50:10 EDT 2009


Am Samstag 31 Oktober 2009 17:33:10 schrieb Joe Fredette:
> 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.

Or perhaps he should look at the class IArray from Data.Array.IArray, maybe he can just 
declare instances of IArray for his datatypes.
Without more information, I can't tell which way to go.

>
> 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
> > 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.
> >
> > Thanks,
> >
> >        Shawn.




More information about the Beginners mailing list