[Haskell-beginners] User-defined polymorphic data type: heterogeneous list?

Ertugrul Soeylemez es at ertes.de
Wed Jul 13 00:39:02 CEST 2011


Arlen Cuss <celtic at sairyx.org> wrote:

> As Mats pointed out, an existential quantification will let you define
> your polymorphic element (and to quote Mats):
>
> > {-# LANGUAGE ExistentialQuantification #-}
> >
> > data HeteroElement = forall a. Element a
> >
> > list = [Element 1, Element 'a', Element True]
>
> The question is, what can you do with this list? You can't "show" it,
> because there's no requirement on HeteroElement's "a" type of it
> having a Show instance (adding 'deriving Show' to the data statement
> will cause an error, as it cannot be done for all 'a'!). You can't
> find out their types. Indeed, you can't do anything at all with an
> Element, simply because there's no restriction placed on their
> value. They could contain anything at all.

This definition is indeed not very useful, but you can have something
like the following:

    class Renderable a
    class Updatable a

    data GameObj = forall a. (Renderable a, Updatable a) => GameObj a

    list :: [GameObj]
    list = [ GameObj SpaceMarine,
             GameObj Zombie,
             GameObj BFG9000 ]


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/





More information about the Beginners mailing list