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

Ryan Ingram ryani.spam at gmail.com
Wed Sep 2 01:17:20 EDT 2009


You can also look at it in terms of the "deconstruction" operation on
this data type.

For example, lists:

> data List a = Nil | Cons a (List a)
>
> case_list :: r -> (a -> List a -> r) -> List a -> r
> case_list nil_case cons_case Nil = nil_case
> case_list nil_case cons_case (Cons x xs) = cons_case x xs

Now, given the "Branch" type:

> data Branch tok st a = forall b. Branch (PermParser tok st (b -> a)) (GenParser tok st b)

what is the type of its "case" construct?  Here's the answer:

> case_branch :: (forall b. PermParser tok st (b -> a) -> GenParser tok st b -> r) -> Branch tok st a -> r
> case_branch k (Branch pparser gparser) = k pparser gparser

Notice the higher-rank type on the argument k; it has to be able to
accept *any* type b, because the constructor hid an object of *some*
type which is no longer known.  So unless you can accept *any* b, you
can't operate on this object.

  -- ryan


More information about the Haskell-Cafe mailing list