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

Eugene Kirpichov ekirpichov at gmail.com
Wed Sep 2 01:57:18 EDT 2009


2009/9/2 zaxis <z_axis at 163.com>:
>
> Isnot it clear without the 'forall' ?
> data Branch tok st a = Branch (PermParser tok st (b -> a)) (GenParser tok st
> b)
>
> thanks!
>

The situation is not so simple.
Consider, for example, a procedure that takes as input a *generic*
sorting algorithm:

sortThem :: (forall a. Ord a => [a] -> [a]) -> [Int] -> [String] ->
([Int], [String])
sortThem sortAlgo ints strings = (sortAlgo ints, sortAlgo strings)

Here you can't omit the 'forall' because if you do, then inside the
body of the sortThem function the 'a' type variable has a fixed value
and can't be both Int and String!

This is a somewhat esoteric example, but one could consider, for
instance, a procedure that takes as input some trees of different
types and a generic tree traversal algorithm. Well, have a look at the
haskellwiki page, there's a pile of examples :)

P.S. I tried to write up the difference between datatype and function
declarations in this respect, but my explanations turned into a mess,
so I erased them in the hope that someone will explain it better than
me.

>
> jkff wrote:
>>
>> This means that for any type 'b' you can construct a value of type
>> 'Branch tok st a' by passing to Branch an argument of type
>> '(PermParser tok st (b -> a))' and 'GenParser tok st b'.
>> This also means that when you're given a value of type Branch tok st
>> a, you don't know what that 'b' type was; the only thing you know is
>> that the 'b' in 'b -> a' in the first argument of Branch is the same
>> as the 'b' in 'GenParser tok st b'.
>>
>> See also: the haskellwiki page on existential types.
>>
>> 2009/9/2 zaxis <z_axis at 163.com>:
>>>
>>> data Branch tok st a = forall b. Branch (PermParser tok st (b -> a))
>>> (GenParser tok st b)
>>>
>>> please shed a light on me, thanks!
>>> --
>>> View this message in context:
>>> http://www.nabble.com/How-to-understand-the-%27forall%27---tp25250783p25250783.html
>>> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>>>
>>> _______________________________________________
>>> Haskell-Cafe mailing list
>>> Haskell-Cafe at haskell.org
>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>>
>>
>>
>>
>> --
>> Eugene Kirpichov
>> Web IR developer, market.yandex.ru
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>>
>
> --
> View this message in context: http://www.nabble.com/How-to-understand-the-%27forall%27---tp25250783p25251783.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



-- 
Eugene Kirpichov
Web IR developer, market.yandex.ru


More information about the Haskell-Cafe mailing list