[Haskell-cafe] Existencial Types

David Menendez dave at zednenem.com
Tue Dec 1 13:21:56 EST 2009


On Tue, Dec 1, 2009 at 1:00 PM, rodrigo.bonifacio
<rodrigo.bonifacio at uol.com.br> wrote:
> Dear all, I wrote the following  types:
>
>> class Transformation t where
>>  (<+>) :: t -> SPLModel  -> InstanceModel -> InstanceModel
>
>> data Configuration = forall t . Transformation t => Configuration
>> (FeatureExpression, [t])
>> type ConfigurationKnowledge = [Configuration]
>
>
>
> I tried to write a function that retrieves the list of transformations of a
> configuration. Bellow a code snip of such a function.
>
>> transformations ck fc = concat [snd c | (Configuration c) <- ck, eval fc
>> (fst c)]
>
> However, compiling this I got:
>
> ---
> Inferred type is less polymorphic than expected
> Quantified type variable `t' escapes
> When checking an existential match that binds
> c :: (FeatureModel.Types.FeatureExpression, [t])
> The pattern(s) have type(s): Configuration
> The body has type: [t]
> In a stmt of a list comprehension: (Configuration c) <- ck
> In the first argument of `concat', namely
> `[snd c | (Configuration c) <- ck, eval fc (fst c)]'
>
> ---
>
>
>
> How can I fix this problem?

The problem is that transformations is creating a heterogenous list,
i.e., there is no guarantee that the contents of the list all have the
same type.

You can get around this by declaring a type representing any transformation,

data SomeTransformation = forall t. Transformation t => ST t

and having transformation return a list of those.

However, if Transformation really only has one member function, you'd
be better off eliminating the existential types entirely.

e.g.,

data Configuration = Configuration FeatureExpression (SPLModel ->
InstanceModel -> InstanceModel)

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Haskell-Cafe mailing list