[Haskell-cafe] Re: Lexically scoped type variables

oleg at pobox.com oleg at pobox.com
Sat Nov 27 00:58:44 EST 2004

Simon Peyton-Jones wrote:
> In GHC at present, a separate type signature introduces no scoping.  For
> example:
>         f :: forall a. a -> a
>         f x = (x::a)
> would be rejected, because the type signature for 'f' does not make
> anything scope over the right-hand side, so (x::a) means (x::forall
> a.a), which is ill typed.

OTH, `f x = (x::a)' will be accepted if this definition appears in an
instance declaration, which mentions the type variable `a' somewhere
in its `signature'.
Incidentally, Hugs differs from GHC in that matter: Hugs considers type
variables in an instance declaration just like type variables in a
function signature -- having no effect on the local type variables.
One might say that Hugs is more consistent in that matter -- OTH, GHC
is more convenient, IMHO. 

The alternative
>   1b) It's brought into scope even if the forall is implicit; e.g.
>                 f :: a -> a
>                 f x = (x::a)

would seem therefore consistent with the existing behavior of GHC with
respect to `instance signatures'.

> 2b) Get rid of result type signatures altogether; instead,
>       use choice (1a) or (1b), and use a separate type signature

We can always emulate the result type signatures: that is, instead of
	foo a :: resType = body
we can write
	foo a = result where result body
and use "`asTypeOf` result" wherever we need to refer to resType. Or we
can write
	foo a = result where
	  result = cid result
	  cid (_::resType) = body
to reduce the result type signature to the argument type signature. 

OTH, I have personally used result type signatures on many occasions
and found them quite helpful. I would be grateful if there were a way
to keep them.

More information about the Haskell-Cafe mailing list