[Haskell-cafe] How to understand the 'forall' ?

Cristiano Paris frodo at theshire.org
Tue Sep 15 17:13:59 EDT 2009


On Wed, Sep 2, 2009 at 7:16 AM, zaxis <z_axis at 163.com> wrote:
>
> Isnot it clear without the 'forall' ?
> data Branch tok st a = Branch (PermParser tok st (b -> a)) (GenParser tok st
> b)
>
> thanks!

I elaborated on this and I wish to add my personal way of figuring out
what the "forall" keyword means.

When you define:

foo :: a -> a

you are actually defining a _function for every type of a_, which can
be read: for every a there exists a function foo which can operate on
it (universal quantification).

When you define something like:

foo :: forall a. a -> a

you are actually defining a _single_ function which must work for
every a (that's why we use the "forall" keyword). The difference is
subtle but the direct consequences of this are: a) that one function
can't use any information about a apart from the fact that it
eventually belongs to the type classes specified in the context, b) in
the case of [a] (or any other type of higher kind, * -> *, * -> * -> *
and so on) you can mix values of different types.

I hope I haven't written anything wrong :)

Cristiano


More information about the Haskell-Cafe mailing list