Re: Lexically scoped type variables

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 writing 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.

oleg@pobox.com writes:
and use "`asTypeOf` result" wherever we need to refer to resType.
I don't like being left with only asTypeOf, because when the desired type has its structure shuffled, it forces to invent a transformation on unused expressions which yields the given transformation on their types - ugly. It should be possible to name every part of a type (or at least every polymorphic part) with a type variable, and use it in local type signatures. For me it's fine if implicit foralls bring type variables into scope. The only disadvantage is that it's incompatible with Haskell 98. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/
participants (2)
-
Marcin 'Qrczak' Kowalczyk
-
oleg@pobox.com