+
+
+%************************************************************************
+%* *
+\subsection{Checking signature type variables}
+%* *
+%************************************************************************
+
+@checkSigTyVars@ is used after the type in a type signature has been unified with
+the actual type found. It then checks that the type variables of the type signature
+are
+ (a) Still all type variables
+ eg matching signature [a] against inferred type [(p,q)]
+ [then a will be unified to a non-type variable]
+
+ (b) Still all distinct
+ eg matching signature [(a,b)] against inferred type [(p,p)]
+ [then a and b will be unified together]
+
+ (c) Not mentioned in the environment
+ eg the signature for f in this:
+
+ g x = ... where
+ f :: a->[a]
+ f y = [x,y]
+
+ Here, f is forced to be monorphic by the free occurence of x.
+
+ (d) Not (unified with another type variable that is) in scope.
+ eg f x :: (r->r) = (\y->y) :: forall a. a->r
+ when checking the expression type signature, we find that
+ even though there is nothing in scope whose type mentions r,
+ nevertheless the type signature for the expression isn't right.
+
+ Another example is in a class or instance declaration:
+ class C a where
+ op :: forall b. a -> b
+ op x = x
+ Here, b gets unified with a
+
+Before doing this, the substitution is applied to the signature type variable.
+
+We used to have the notion of a "DontBind" type variable, which would
+only be bound to itself or nothing. Then points (a) and (b) were
+self-checking. But it gave rise to bogus consequential error messages.
+For example:
+
+ f = (*) -- Monomorphic
+
+ g :: Num a => a -> a
+ g x = f x x
+
+Here, we get a complaint when checking the type signature for g,
+that g isn't polymorphic enough; but then we get another one when
+dealing with the (Num x) context arising from f's definition;
+we try to unify x with Int (to default it), but find that x has already
+been unified with the DontBind variable "a" from g's signature.
+This is really a problem with side-effecting unification; we'd like to
+undo g's effects when its type signature fails, but unification is done
+by side effect, so we can't (easily).
+
+So we revert to ordinary type variables for signatures, and try to
+give a helpful message in checkSigTyVars.
+
+\begin{code}
+checkSigTyVars :: [TcTyVar] -- The original signature type variables
+ -> TcM s [TcTyVar] -- Zonked signature type variables
+
+checkSigTyVars [] = returnTc []
+
+checkSigTyVars sig_tyvars
+ = zonkTcTyVars sig_tyvars `thenNF_Tc` \ sig_tys ->
+ tcGetGlobalTyVars `thenNF_Tc` \ globals ->
+
+ checkTcM (all_ok sig_tys globals)
+ (complain sig_tys globals) `thenTc_`
+
+ returnTc (map (getTyVar "checkSigTyVars") sig_tys)
+
+ where
+ all_ok [] acc = True
+ all_ok (ty:tys) acc = case getTyVar_maybe ty of
+ Nothing -> False -- Point (a)
+ Just tv | tv `elemVarSet` acc -> False -- Point (b) or (c)
+ | otherwise -> all_ok tys (acc `extendVarSet` tv)
+
+
+ complain sig_tys globals
+ = -- For the in-scope ones, zonk them and construct a map
+ -- from the zonked tyvar to the in-scope one
+ -- If any of the in-scope tyvars zonk to a type, then ignore them;
+ -- that'll be caught later when we back up to their type sig
+ tcGetInScopeTyVars `thenNF_Tc` \ in_scope_tvs ->
+ zonkTcTyVars in_scope_tvs `thenNF_Tc` \ in_scope_tys ->
+ let
+ in_scope_assoc = [ (zonked_tv, in_scope_tv)
+ | (z_ty, in_scope_tv) <- in_scope_tys `zip` in_scope_tvs,
+ Just zonked_tv <- [getTyVar_maybe z_ty]
+ ]
+ in_scope_env = mkVarEnv in_scope_assoc
+ in
+
+ -- "check" checks each sig tyvar in turn
+ foldlNF_Tc check
+ (env2, in_scope_env, [])
+ (tidy_tvs `zip` tidy_tys) `thenNF_Tc` \ (env3, _, msgs) ->
+
+ failWithTcM (env3, main_msg $$ nest 4 (vcat msgs))
+ where
+ (env1, tidy_tvs) = mapAccumL tidyTyVar emptyTidyEnv sig_tyvars
+ (env2, tidy_tys) = tidyOpenTypes env1 sig_tys
+
+ main_msg = ptext SLIT("Inferred type is less polymorphic than expected")
+
+ check (env, acc, msgs) (sig_tyvar,ty)
+ -- sig_tyvar is from the signature;
+ -- ty is what you get if you zonk sig_tyvar and then tidy it
+ --
+ -- acc maps a zonked type variable back to a signature type variable
+ = case getTyVar_maybe ty of {
+ Nothing -> -- Error (a)!
+ returnNF_Tc (env, acc, unify_msg sig_tyvar (ppr ty) : msgs) ;
+
+ Just tv ->
+
+ case lookupVarEnv acc tv of {
+ Just sig_tyvar' -> -- Error (b) or (d)!
+ returnNF_Tc (env, acc, unify_msg sig_tyvar (ppr sig_tyvar') : msgs) ;
+
+ Nothing ->
+
+ if tv `elemVarSet` globals -- Error (c)! Type variable escapes
+ -- The least comprehensible, so put it last
+ then tcGetValueEnv `thenNF_Tc` \ ve ->
+ find_globals tv env (valueEnvIds ve) `thenNF_Tc` \ (env1, globs) ->
+ returnNF_Tc (env1, acc, escape_msg sig_tyvar tv globs : msgs)
+
+ else -- All OK
+ returnNF_Tc (env, extendVarEnv acc tv sig_tyvar, msgs)
+ }}
+
+-- find_globals looks at the value environment and finds values
+-- whose types mention the offending type variable. It has to be
+-- careful to zonk the Id's type first, so it has to be in the monad.
+-- We must be careful to pass it a zonked type variable, too.
+find_globals tv tidy_env ids
+ | null ids
+ = returnNF_Tc (tidy_env, [])
+
+find_globals tv tidy_env (id:ids)
+ | not (isLocallyDefined id) ||
+ isEmptyVarSet (idFreeTyVars id)
+ = find_globals tv tidy_env ids
+
+ | otherwise
+ = zonkTcType (idType id) `thenNF_Tc` \ id_ty ->
+ if tv `elemVarSet` tyVarsOfType id_ty then
+ let
+ (tidy_env', id_ty') = tidyOpenType tidy_env id_ty
+ in
+ find_globals tv tidy_env' ids `thenNF_Tc` \ (tidy_env'', globs) ->
+ returnNF_Tc (tidy_env'', (idName id, id_ty') : globs)
+ else
+ find_globals tv tidy_env ids
+
+escape_msg sig_tv tv globs
+ = vcat [mk_msg sig_tv <+> ptext SLIT("escapes"),
+ pp_escape,
+ ptext SLIT("The following variables in the environment mention") <+> quotes (ppr tv),
+ nest 4 (vcat_first 10 [ppr name <+> dcolon <+> ppr ty | (name,ty) <- globs])
+ ]
+ where
+ pp_escape | sig_tv /= tv = ptext SLIT("It unifies with") <+>
+ quotes (ppr tv) <> comma <+>
+ ptext SLIT("which is mentioned in the environment")
+ | otherwise = ptext SLIT("It is mentioned in the environment")
+
+ vcat_first :: Int -> [SDoc] -> SDoc
+ vcat_first n [] = empty
+ vcat_first 0 (x:xs) = text "...others omitted..."
+ vcat_first n (x:xs) = x $$ vcat_first (n-1) xs
+
+unify_msg tv thing = mk_msg tv <+> ptext SLIT("is unified with") <+> quotes thing
+mk_msg tv = ptext SLIT("Quantified type variable") <+> quotes (ppr tv)
+\end{code}
+
+These two context are used with checkSigTyVars
+
+\begin{code}
+sigCtxt :: (Type -> Message) -> Type
+ -> TidyEnv -> NF_TcM s (TidyEnv, Message)
+sigCtxt mk_msg sig_ty tidy_env
+ = let
+ (env1, tidy_sig_ty) = tidyOpenType tidy_env sig_ty
+ in
+ returnNF_Tc (env1, mk_msg tidy_sig_ty)
+
+sigPatCtxt bound_tvs bound_ids tidy_env
+ = returnNF_Tc (env1,
+ sep [ptext SLIT("When checking a pattern that binds"),
+ nest 4 (vcat (zipWith ppr_id show_ids tidy_tys))])
+ where
+ show_ids = filter is_interesting bound_ids
+ is_interesting id = any (`elemVarSet` idFreeTyVars id) bound_tvs
+
+ (env1, tidy_tys) = tidyOpenTypes tidy_env (map idType show_ids)
+ ppr_id id ty = ppr id <+> dcolon <+> ppr ty
+ -- Don't zonk the types so we get the separate, un-unified versions
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{Errors and contexts}
+%* *
+%************************************************************************
+