Scoped type variables

Abraham Egnor abe.egnor at gmail.com
Fri Dec 17 15:26:43 EST 2004


Please!  I've lost count of the number of times when I've written code as

f :: a -> b
f (x :: a) = ...

wishing that I didn't have to locally bind the 'a'.

I'm not sure I understand the objection raised by Jon; the 'implicit
declaration' of type variables in type signatures has never bothered
me, and in fact seems quite similar to how names for values don't have
to be declared beforehand but are brought into scope by the binding
(which I also have no problem with).

Abe

On Fri, 17 Dec 2004 19:37:00 +0000, Keean Schupke
<k.schupke at imperial.ac.uk> wrote:
> what about having -fno-lexically-scoped-types for old code?
> 
>     Keean.
> 
> Simon Peyton-Jones wrote:
> 
> >OK, OK, I yield!
> >
> >This message is about lexically scoped type variables.  I've gradually
> >become convinced that if you write
> >
> >       f :: [a] -> [a]
> >       f x = <body>
> >
> >then the type variable 'a' should be in scope in <body>.   At present in
> >GHC you have to write
> >       f (x :: [a]) = <body>
> >to bring 'a' into scope.
> >
> >I've fought against this because it seems funny for a 'forall' in a type
> >signature to bring a type variable into scope in perhaps-distant
> >function body, but it's just so convenient and "natural".  Furthermore,
> >as Martin Sulzmann points out, you might have type variables that are
> >mentioned only in the context of the type:
> >       g :: Foo a b => [a] -> [a]
> >       g = ...
> >GHC provides no way to bring 'b' into scope at the moment, and that
> >seems bad design.
> >
> >
> >If I do this, which I'm inclined to, it could break some programs,
> >because (with -fglasgow-exts) all type signatures will bring scoped type
> >variables into scope in their function body.  Here's an example that
> >will break
> >
> >       f :: [a] -> [a]
> >       f x = my_id x
> >          where
> >              my_id :: a -> a
> >              my_id y = y
> >
> >The type signature for my_id will become monomorphic, since 'a' is now
> >in scope, so the application (my_id x) will fail saying
> >       can't unify 'a' with '[a]'.
> >In some ways that makes sense.  If you used 'b' instead in the defn of
> >my_id, it'd be fine, because my_id would get the type (forall b. b->b).
> >Fixing such breakages is easy.
> >
> >
> >So there it is.   Any one have strong opinions?  (This is in addition to
> >the existing mechanism for bringing scoped type variables into scope via
> >pattern type signatures, of course.)
> >
> >Simon
> >_______________________________________________
> >Glasgow-haskell-users mailing list
> >Glasgow-haskell-users at haskell.org
> >http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
> >
> >
> 
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
>


More information about the Glasgow-haskell-users mailing list