class parameters to existential datatypes

Max A . K . max630@mail.ru
Fri, 7 Dec 2001 08:45:11 +0600


On Thu, Dec 06, 2001 at 11:19:31AM -0800, Hal Daume III wrote:
>   2) Any ways to get around it?

Is the following what you want?

> data ShapeType = forall t. (Shape t) => ShapeType t
> instance Shape ShapeType where { area (ShapeType a) = area a }

> 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 :: [ShapeType]
> myList = [ShapeType (Circle 5), ShapeType (Rectangle 2 3)]

and then maybe have a function:

> totalArea :: [ShapeType] -> Double
> totalArea = sum . map area

works OK with ghc -fglasgow-exts

Max.