[Haskell-cafe] Existencial Types

rodrigo.bonifacio rodrigo.bonifacio at uol.com.br
Thu Dec 3 12:45:48 EST 2009


Dear Luke, thanks for your answers....

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

Sure, as I mentioned, we have different transformations and it would be worth to filter a list of transformations by a particular type or even apply the list of transformations in a particular order considering their type.

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

I understand your idea, but I will have to implement several variations of "toTransformation", one for each kind of transformation. Moreover, I couldn't 
realize how is possible to define a function that could be applied to different transformations without using type classes--- I have to restrict the 
types of argument of such a function. Moreover, I couldn't figure out what are the benefits of your solution. Please, if possible, could you elaborate 
that a bit more, in order that I could understand  why your design is better (I mean, more legible, reusable or concise)?

Thanks in advance,

Rodrigo.

Em 01/12/2009 22:44, Luke Palmer < lrpalmer at gmail.com > escreveu:


On Tue, Dec 1, 2009 at 4:21 PM, rodrigo.bonifacio
 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).

Luke





More information about the Haskell-Cafe mailing list