I am also not an expert on existential types but I was yesterday playing 
with them.

I think that you want to have a Finite Map with items 
of different type. It is not possible in Haskell (you cannot define
a list with elements of different types). But you could do a little 
trick using type classes.

To simplify, I define a FiniteMap as a list:

> type FiniteMap a b = [(a,b)]

> addToFM :: FiniteMap a b -> a -> b -> FiniteMap a b
> addToFM xs x y = (x,y):xs

> data K = K1 | K2 | K3 
>       deriving (Eq, Show)

The trick is to define a type class which relates the elements that you 
can insert in your finite map

> class Item i 

<You could add some common methods if you want>

Now, you can define some instances:

> instance Item Char
> instance Item (Char,Char)

And define an ITEM as:

> data ITEM = forall i . Item i => MkItem i

Finally, your KTab will store values of type ITEM

> type KTab = FiniteMap K ITEM

And now your example works:

> f :: KTab -> KTab 
> f t = (addToFM (addToFM t K1 (MkItem 'a')) K2 (MkItem ('a','b'))) 

There is a paper by Koen Claessen about "Type Classes and Existential 
Types" which describes this trick and has a first part very readable. At this 
moment, I have not the reference, but I can try to look for it, if you want.

Best Regards, Jose E. Labra
http://lsi.uniovi.es/~labra



Reply via email to