I find it easy to understand this distinction by writing out the types of the constructors and case expressions for these objects, in a language like system F:<br><br>(here, {x :: t} means a type argument with name x of kind t)<br>
<br>Exists :: {f :: *->*} -> {a :: *} -> f a -> Exists f<br>Forall :: {f :: *->*} -> ({a :: *} -> f a) -> Forall f<br><br>Notice the higher rank type in the constructor 'Forall'.<br><br>Similarly, the case deconstruction for these:<br>
<br>caseExists :: {r :: *} -> {f :: *->*} -> ({a :: *} -> f a -> r) -> Exists f -> r<br>caseForall :: {r :: *} -> {f :: *->*} -> (({a :: *} -> f a) -> r) -> Forall f -> r<br><br>The function passed to caseExists needs to be able to handle any type 'a' we throw at it, whereas the function passed to caseForall can choose what 'a' it wants to use (and can choose multiple different 'a's by calling the ({a::*} -> f a) parameter function multiple times. In the simple case that the case function only instantiates 'a' at a single type, we can simplify this:<br>
<br>caseForall' :: {r :: *} -> {f :: * -> *} -> {a :: *} -> (f a -> r) -> Forall f -> r<br><br>and this function is definable in terms of caseForall:<br><br>caseForall' {r} {f} {a} k v = caseForall {r} {f} (\mk_fa -> k (mk_fa {a})) v<br>
<br> -- ryan<br><br><div class="gmail_quote">On Mon, Mar 5, 2012 at 9:37 PM, wren ng thornton <span dir="ltr"><<a href="mailto:wren@freegeek.org">wren@freegeek.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 3/5/12 5:13 PM, AntC wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
I've tried that ListFunc wrapping you suggest:<br></div>
[...]<div class="im"><br>
But I can't 'dig out' the H-R function and apply it (not even<br>
monomorphically):<br>
</div></blockquote>
<br>
That's because the suggestion changed it from a universal quantifier to an existential quantifier.<br>
<br>
data Exists f = forall a. Exists (f a)<br>
<br>
data Forall f = Forall (forall a. f a)<br>
<br>
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.<br>
<br>
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.<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Live well,<br>
~wren</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
______________________________<u></u>_________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br>