[Haskell-cafe] [Haskell] Higher types in contexts

wren ng thornton wren at freegeek.org
Tue Mar 6 06:37:36 CET 2012


On 3/5/12 5:13 PM, AntC wrote:
> I've tried that ListFunc wrapping you suggest:
> [...]
> But I can't 'dig out' the H-R function and apply it (not even
> monomorphically):

That's because the suggestion changed it from a universal quantifier to 
an existential quantifier.

     data Exists f = forall a. Exists (f a)

     data Forall f = Forall (forall a. f a)

With Exists, we're essentially storing a pair of the actual type 'a' and 
then the f a itself. We can't just pull out the f a and use it, because 
we don't know what 'a' is. We can bring it into scope temporarily by 
case matching on the Exists f, which allows us to use polymorphic 
functions, but we still don't actually know what it is so we can *only* 
use polymorphic functions.

Conversely, with Forall we're storing some f a value which is in fact 
polymorphic in 'a'. Here, because it's polymorphic, the caller is free 
to choose what value of 'a' they would like the f a to use. Indeed, they 
can choose multiple different values of 'a' and get an f a for each of them.

-- 
Live well,
~wren



More information about the Haskell-Cafe mailing list