[Haskell-cafe] Rank N type tutorial?
Benjamin.Rudiak-Gould at cl.cam.ac.uk
Fri Oct 27 13:41:37 EDT 2006
Greg Buchholz wrote:
>I'm not quite sure why this is illegal...
>>foo :: Integer -> (forall a. Show a => a)
>>foo 2 = ["foo"]
>>foo x = x
>...while this is just fine...
>>bar :: Integer -> (forall a. Show a => a->b) -> b
>>bar 2 k = k ["bar"]
>>bar x k = k x
The way to think about it is that foralls are extra function arguments. Your
first example is like
foo :: Integer -> (a::Type -> Show a -> a)
so a is chosen by the caller, not by you. The second case is like
bar :: Integer -> (a::Type -> Show a -> a -> b) -> b
In order for the first case to work as you expect, you'd need the type
foo :: Integer -> (a::Type, Show a, a)
which is traditionally written
foo :: Integer -> (exists a. Show a => a)
More information about the Haskell-Cafe