-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] -- Universally-quantified type variables in the signature
- -> TcTyVarSet -- Tyvars that are free in the type signature
- -- These should *already* be in the global-var set, and are
- -- used here only to improve the error message
- -> TcM s [TcTyVar] -- Zonked signature type variables
-
-checkSigTyVars [] free = returnTc []
-
-checkSigTyVars sig_tyvars free_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) ->
- find_frees tv env1 [] (varSetElems free_tyvars) `thenNF_Tc` \ (env2, frees) ->
- returnNF_Tc (env2, acc, escape_msg sig_tyvar tv globs frees : 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 acc []
- = returnNF_Tc (tidy_env, acc)
-
-find_globals tv tidy_env acc (id:ids)
- | not (isLocallyDefined id) ||
- isEmptyVarSet (idFreeTyVars id)
- = find_globals tv tidy_env acc 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
- acc' = (idName id, id_ty') : acc
- in
- find_globals tv tidy_env' acc' ids
- else
- find_globals tv tidy_env acc ids
-
-find_frees tv tidy_env acc []
- = returnNF_Tc (tidy_env, acc)
-find_frees tv tidy_env acc (ftv:ftvs)
- = zonkTcTyVar ftv `thenNF_Tc` \ ty ->
- if tv `elemVarSet` tyVarsOfType ty then
- let
- (tidy_env', ftv') = tidyTyVar tidy_env ftv
- in
- find_frees tv tidy_env' (ftv':acc) ftvs
- else
- find_frees tv tidy_env acc ftvs
-
-
-escape_msg sig_tv tv globs frees
- = mk_msg sig_tv <+> ptext SLIT("escapes") $$
- if not (null globs) then
- vcat [pp_it <+> ptext SLIT("is mentioned in the environment"),
- ptext SLIT("The following variables in the environment mention") <+> quotes (ppr tv),
- nest 2 (vcat_first 10 [ppr name <+> dcolon <+> ppr ty | (name,ty) <- globs])
- ]
- else if not (null frees) then
- vcat [ptext SLIT("It is reachable from the type variable(s)") <+> pprQuotedList frees,
- nest 2 (ptext SLIT("which") <+> is_are <+> ptext SLIT("free in the signature"))
- ]
- else
- empty -- Sigh. It's really hard to give a good error message
- -- all the time. One bad case is an existential pattern match
- where
- is_are | isSingleton frees = ptext SLIT("is")
- | otherwise = ptext SLIT("are")
- pp_it | sig_tv /= tv = ptext SLIT("It unifies with") <+> quotes (ppr tv) <> comma <+> ptext SLIT("which")
- | otherwise = ptext SLIT("It")
-
- 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
-