GHC 6.10.1 type puzzler

Chung-chieh Shan ccshan at post.harvard.edu
Wed Nov 19 12:29:55 EST 2008


Dave Bayer <bayer at cpw.math.columbia.edu> wrote in article <BC2C1B5E-6F08-4C17-B20A-F47512F36559 at math.columbia.edu> in gmane.comp.lang.haskell.glasgow.user:
> test1 :: Box a -> a -> [a]
> test1 box x = go box x
>  where
>    go :: Box a -> a -> [a]
>    go b y = [(val b), y]

The type signature "go :: Box a -> a -> [a]"
  is equivalent to "go :: Box b -> b -> [b]"
                or "go :: forall a. Box a -> a -> [a]"
                or "go :: forall b. Box b -> b -> [b]"
because the type variable "a" in the type signature for "test1" does not
scope over the type signature for "go".  Fortunately, "go" here does
have that type.

> test2 :: Box a -> a -> [a]
> test2 box x = go x
>  where
> --  go :: a -> [a]
>    go y = [(val box), y]

Here "go" does not have the type "forall a. a -> [a]"; it is not
polymorphic enough.

To write the signature for "go" inside "test2", you need lexically
scoped type variables:
http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#scoped-type-variables

-- 
Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
2008-11-20 Universal Children's Day              http://unicef.org/
1948-12-10 Universal Declaration of Human Rights http://everyhumanhasrights.org



More information about the Glasgow-haskell-users mailing list