[Haskell-cafe] Existencial Types

Luke Palmer lrpalmer at gmail.com
Tue Dec 1 19:44:25 EST 2009

On Tue, Dec 1, 2009 at 4:21 PM, rodrigo.bonifacio
<rodrigo.bonifacio at uol.com.br> wrote:
> Thanks Luke.
> In fact I, will have different implementations of the Transformation type.
> Something like:
> data SelectScenarios = SelectScenarios {
> scIds :: [Id]
> }

What is this different type buying you?  You can never "downcast" to it later.

> And then I should be able to make SelectScenarios a kind of Transformation.
> So I think that I really need a class. What do you think about it?
> instance Transformation SelectScenario where
> (<+>)  ....

So instead of making a type and an instance, just implement it
directly as a Transformation:

selectScenario :: [Id] -> Transformation
selectScenario ids = Transformation {
    (<+>) =  {- whatever implementation you gave for (<+>) above, using ids -}

If the only purpose of SelectScenario (your type) is to be used
polymorphically as a Transformation, then this approach is isomorphic
-- i.e. anything you can do with the existential type trick you can do
with this approach.

If SelectScecario is used for other purposes, then give an explicit
cast function

toTransformation :: SelectScenario -> Transformation
toTransformation (SelectScenario ids) = Transformation {
    (<+>) = {- implementation of (<+>) just as if it were a class method -}

Existential types only buy you power when the quantified variable
appears more than once on the right hand side, for example:  forall a.
Num a => (a,a).  But even those can usually be factored out into more
direct representations (I seem to recall Oleg has a proof that they
always can, actually).


More information about the Haskell-Cafe mailing list