[Haskell-cafe] Re: Existencial quantification and polymorphic datatypes (actually, components...)

David Menendez dave at zednenem.com
Tue Jan 20 15:14:56 EST 2009


On Tue, Jan 20, 2009 at 2:51 PM, Mauricio <briqueabraque at yahoo.com> wrote:
>>> But how is this:
>>> data SomeNum = forall a. SN a
>>> different from:
>>> data SomeNum = SN (forall a. a)
>
>> At a glance they look the same to me — but only the first is accepted by
>> ghc.
>
> Following the link you pointed in the last
> message, I found this at 8.8.4.1:
>
> data T a = T1 (forall b. b -> b -> b) a
>
> If I understand properly, it can be activated
> with -XPolymorphicComponents. It's different
> from my example, but I would like to know what
> it does that this can't:
>
> data T a = forall b. T1 (b->b->b) a

In the first example, T stores a function which works for every type.
In the second example, T stores a function which works on a specific
type.

So with the first definition, you can do something like this:

foo :: T1 a -> (Bool, Int)
foo (T1 f _) = (f True False, f 1 2)

But you can't really do anything useful with the second example.

> (The last one I think I understand well after the
> previous message, although I see no use for this
> particular form, since after pattern match there's
> no other 'b' value to which we could aply that
> function field.)

Here's a more useful example of existential quantification:

data Stream a = forall b. Stream (b -> a) (b -> b) b

head :: Stream a -> a
head (Stream h t b) = h b

tail :: Stream a -> Stream a
tail (Stream h t b) = Stream h t (t b)


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


More information about the Haskell-Cafe mailing list