[Haskell-cafe] Rank N type tutorial?
rossberg at ps.uni-sb.de
rossberg at ps.uni-sb.de
Fri Oct 27 13:53:17 EDT 2006
"Greg Buchholz" <haskell at sleepingsquirrel.org> wrote:
> I'm not quite sure why this is illegal...
>
>> foo :: Integer -> (forall a. Show a => a)
>> foo 2 = ["foo"]
>> foo x = x
The type signature promises that foo returns a function that has type a
*for all a* (that are in Show). But neither a string list nor an integer
can have all types. Rather, they are very specific. That is, what you have
implemented actually could have type
foo :: Integer -> (exists a. Show a => a)
(if GHC supported such types).
Note that only bottom can have a type like forall a.a.
> ...while this is just fine...
>
>> bar :: Integer -> (forall a. Show a => a->b) -> b
>> bar 2 k = k ["bar"]
>> bar x k = k x
Here, you declare bar to *take* a function that delivers some b for any a.
That is, the burden of defining such a function now is on the caller, not
bar itself.
However, in this case, defining such an argument function actually is
easy, for another reason: the signature says k returns a result of type b,
but b is fixed. So it is always possible to come up with a trivial
instance of such a function (which will just ignore its argument). For
instance,
bar 2 (\_ -> "boo")
But try to call this for a different example: ;-)
baz :: Integer -> (forall a b. Show a => a->b) -> c
baz 2 k = k ["baz"]
baz x k = k x
Hope this helps,
- Andreas
More information about the Haskell-Cafe
mailing list