|Version 9 (modified by ross@…, 9 years ago) (diff)|
Scoped Type Variables
In Haskell 98, it is sometimes impossible to give a signature for a locally defined variable, e.g. cmp in
sortImage :: Ord b => (a -> b) -> [a] -> [a] sortImage f = sortBy cmp where cmp x y = compare (f x) (f y)
The argument type of cmp is the type a in the signature of sortImage, but there is no way to refer to it in a type signature. Quantification of type variables over expressions is needed.
GHC provides three extensions that bind type variables:
- Explicit foralls in type signature declarations.
The bound variables scope over the function body, e.g.
sortImage :: forall a b. Ord b => (a -> b) -> [a] -> [a] sortImage f = sortBy cmp where cmp :: a -> a -> Ordering cmp x y = compare (f x) (f y)
- Pattern type signatures.
Free variables in the type stand for new types in the scope of the pattern, e.g.
sortImage (f::a->b) = sortBy cmp where cmp :: a -> a -> Ordering cmp x y = compare (f x) (f y)
- Result type signatures, giving the type of both sides of the equation.
Free variables in the type stand for new types in the scope, e.g.
sortImage f :: [a] -> [a] = sortBy cmp where cmp :: a -> a -> Ordering cmp x y = compare (f x) (f y)
In the latter two cases, the variable can stand for any type, not necessarily a type variable is in these examples, i.e. the variable is existentially quantified. Hugs supports only pattern type signatures, with the type variables universally quantified.
In GHC, type variables in instances heads also scope over the body of the instance.
Note that although GHC and Hugs use the same syntax, the meaning of type variables is quite different, and there are other differences too.
- Allows better documentation (without them, some expressions cannot be annotated with their types).
- Extensions such as RankNTypes and GADTs require such annotations, so even more important in conjunction with them.
- Many different forms of scoped type variables in GHC makes them hard to reason about.
f :: a -> a f = \x -> (x :: a)is rejected but
g = let f :: a -> a = \x -> (x :: a) in fis allowed.
- A rule like ExplicitQuantification would most likely be needed if these were put into the standard.
Both let-bound and lambda-bound type variables are in scope in the body of a function, and can be used in expression signatures. However, just as a let-binding can shadow other values of the same name, let-bound type variables may shadow other type variables. Thus no type variables are ever already in scope in a let-bound signature. Lambda-bound type variables (e.g. in a pattern) do not shadow but rather refer to the same type. ExplicitQuantification is required for all expression type signatures but not let-bound signatures.
This proposal tries to strike a balance between backwards compatibility, avoiding accidental type errors, and simplicity. Let-bound type signatures always create a new scope, lambda-bound ones are always in the same scope, and it is clear from expression type signatures which are the scoped type vars.
(perhaps this text can be cleaned up further? what is a better term for expression type signature?)