[Haskell-beginners] forall confusion

Isaac Dupree ml at isaac.cedarswampstudios.org
Sat May 29 16:30:25 EDT 2010


On 05/29/10 11:35, Philip Scott wrote:
> Hi all,
>
> I was wondering if someone could give me an intuitive explanation of why in
>
> f :: forall a.(Show a) => a -> String
> f x = show x
>
> "forall a.(Show a) => a" appears to translate into "Any a, as long as it
> is an instance of Show"

The function must be type-correctly defined for "any a in Show".  (the 
caller/user "chooses" which type "a" is desired.)

> but if I use forall in an type qualifier in an assignment:
>
> myList = [] :: [forall a.(Show a) => a]

Each element of this list must be "any a in Show".  (the caller/user 
"chooses" which type "a" is desired.)  In this case, the only way to 
generate a value that is truly "any a in Show" is "undefined".*

*some classes, like Read, might provide another possible value, as in 
[read "123"] which could be [forall a.(Read a) => a] -- albeit only a 
few types "a", such as Integer and Double, would actually have 
non-bottom values in this case, but it's easy to construct less-awful 
examples.

Why were you poring over Existentially Quantified Types? -- you didn't 
use any existentials above -- did you want to? (or I should ask -- can 
you tell, yet, whether you wanted to?)

-Isaac


More information about the Beginners mailing list