[Haskell-cafe] Named function fields vs. type classes

Derek Elkins ddarius at hotpop.com
Tue Dec 14 13:52:59 EST 2004

> > On the other hand, it's difficult or impossible to make a list of a
> > bunch of different types of things that have nothing in common save
> > being members of the class.
> I've recently been playing with making, for each class C, a
> "interface" datatype IC (appropriately universally and existentially
> qualified so as to include a dictionary for class C), and then making
> this IC an instance of class C.  Then I can wrap any instance of C up
> in an IC, and make a list of those.
> The casts get a bit annoying, though; the compiler can't figure out
> that this IC is in some sense the maximum type in class C, and so
> can't resolve things like
> f :: MyClass a => [a] -> b
> f = ...
> upcast :: MyClass a => a -> IMyClass  -- usually defined as an
> instance of class Cast upcast x = IMyClass x
> f [upcast a, upcast b]  -- yields type error
> Instead, you have to redefine f as follows:
> f' :: [IMyClass] -> b
> which is a bit annoying.
> HTH.
> --KW 8-)

Not surprisingly, the wiki
(http://www.haskell.org/hawiki/ExistentialTypes) has some discussion
about this as well, though not too much to add to what has been said.

More information about the Haskell-Cafe mailing list