From acbba5860c9cdf506738685ed2a6d9ea3a0b9b77 Mon Sep 17 00:00:00 2001 From: simonpj Date: Wed, 31 Oct 2001 16:24:43 +0000 Subject: [PATCH] [project @ 2001-10-31 16:24:43 by simonpj] Document scoped type variables --- ghc/docs/users_guide/glasgow_exts.sgml | 103 +++++++++++++++++--------------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/ghc/docs/users_guide/glasgow_exts.sgml b/ghc/docs/users_guide/glasgow_exts.sgml index 5c80925..6a09aa1 100644 --- a/ghc/docs/users_guide/glasgow_exts.sgml +++ b/ghc/docs/users_guide/glasgow_exts.sgml @@ -2175,9 +2175,27 @@ and brings into scope the type variable b. - The type variables thus brought into scope may be mentioned -in ordinary type signatures or pattern type signatures anywhere within -their scope. +The type variable(s) bound by the pattern have the same scope +as the term variable(s) bound by the pattern. For example: + + let + f (x::a) = <...rhs of f...> + (p::b, q::b) = (1,2) + in <...body of let...> + +Here, the type variable a scopes over the right hand side of f, +just like x does; while the type variable b scopes over the +body of the let, and all the other definitions in the let, +just like p and q do. + + + + + + +The type variables bound by the pattern may be +mentioned in ordinary type signatures or pattern +type signatures anywhere within their scope. @@ -2210,11 +2228,26 @@ and that is an incorrect typing. - There is no implicit universal quantification on pattern type -signatures, nor may one write an explicit forall type in a pattern -type signature. The pattern type signature is a monotype. - +The pattern type signature is a monotype: + + + +A pattern type signature cannot contain any explicit forall quantification. + + + +The type variables bound by a pattern type signature can only be instantiated to monotypes, +not to type schemes. + + + +There is no implicit universal quantification on pattern type signatures (in contrast to +ordinary type signatures). + + + + @@ -2292,8 +2325,7 @@ Result type signatures are not yet implemented in Hugs. Where a pattern type signature can occur -A pattern type signature can occur in any pattern, but there -are restrictions on pattern bindings: +A pattern type signature can occur in any pattern. For example: @@ -2374,56 +2406,31 @@ For example: -Pattern type signatures that bind new type variables -may not be used in pattern bindings at all. -So this is illegal: - +Pattern type signatures +can be used in pattern bindings: f x = let (y, z::a) = x in ... + f1 x = let (y, z::Int) = x in ... + f2 (x::(Int,a)) = let (y, z::a) = x in ... + f3 :: (b->b) = \x -> x - -But these are OK, because they do not bind fresh type variables: - - +In all such cases, the binding is not generalised over the pattern-bound +type variables. Thus f3 is monomorphic; f3 +has type b -> b for some type b, +and not forall b. b -> b. +In contrast, the binding - f1 x = let (y, z::Int) = x in ... - f2 (x::(Int,a)) = let (y, z::a) = x in ... + f4 :: b->b + f4 = \x -> x - - -However a single variable is considered a degenerate function binding, -rather than a degerate pattern binding, so this is permitted, even -though it binds a type variable: - - - - f :: (b->b) = \(x::b) -> x - - +makes a polymorphic function, but b is not in scope anywhere +in f4's scope. - - -Such degnerate function bindings do not fall under the monomorphism -restriction. Thus: - - - - - - g :: a -> a -> Bool = \x y. x==y - - - - - -Here g has type forall a. Eq a => a -> a -> Bool, just as if -g had a separate type signature. Lacking a type signature, g -would get a monomorphic type. -- 1.7.10.4