class parameters to existential datatypes

Hal Daume III hdaume@ISI.EDU
Thu, 6 Dec 2001 11:19:31 -0800 (PST)


Hi,

I would like to be able to write something like this:

> data Foo c = forall a . c a => Foo a

Unfortunately, this isn't allowed (apparently) because it's an illegal
class assertion (or at least that's what ghc tells me).

My motivation for doing this is I want to have something like List, but a
class list where all you know is that every element is a member of some
class.  I would write something like:

> data CList c = CNil
>              | forall a . (c a) => CCons a (CList c)

that way I could have something like the prototypical:

> class Shape a where { area :: a -> Double }
> data Circle = Circle Double
> data Rectangle = Rectangle Double Double
> instance Shape Circle where { area (Circle r) = 2 * pi * r }
> instance Shape Rectangle where { area (Rectangle l w) = l*w }
> MyList :: CList Shape
> MyList = CCons (Circle 5) (CCons (Rectangle 2 3) CNil)

and then maybe have a function:

> totalArea :: (CList Shape) -> Double
> totalArea = sum . map area

obviously this function declaration is also invalid.

so I have two questions:

  1) Is there any particular reason whey we can't do this (it would
     break some type safety things or whatever)
  2) Any ways to get around it?

Thanks!

 - Hal