+checkSigTyVars :: [TcTyVar] -- Universally-quantified type variables in the signature
+ -> TcTyVarSet -- Tyvars that are free in the type signature
+ -- Not necessarily zonked
+ -- These should *already* be in the free-in-env set,
+ -- and are used here only to improve the error message
+ -> TcM [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 (allDistinctTyVars sig_tys globals)
+ (complain sig_tys globals) `thenTc_`
+
+ returnTc (map (tcGetTyVar "checkSigTyVars") sig_tys)
+
+ where
+ 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
+ tcGetEnv `thenNF_Tc` \ env ->
+ let
+ in_scope_tvs = tcEnvTyVars env
+ in
+ 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 <- [tcGetTyVar_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) = tidyOpenTyVars emptyTidyEnv sig_tyvars
+ (env2, tidy_tys) = tidyOpenTypes env1 sig_tys
+
+ main_msg = ptext SLIT("Inferred type is less polymorphic than expected")
+
+ check (tidy_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 tcGetTyVar_maybe ty of {
+ Nothing -> -- Error (a)!
+ returnNF_Tc (tidy_env, acc, unify_msg sig_tyvar (quotes (ppr ty)) : msgs) ;
+
+ Just tv ->
+
+ case lookupVarEnv acc tv of {
+ Just sig_tyvar' -> -- Error (b) or (d)!
+ returnNF_Tc (tidy_env, acc, unify_msg sig_tyvar thing : msgs)
+ where
+ thing = ptext SLIT("another quantified type variable") <+> quotes (ppr sig_tyvar')
+
+ ; Nothing ->
+
+ if tv `elemVarSet` globals -- Error (c)! Type variable escapes
+ -- The least comprehensible, so put it last
+ -- Game plan:
+ -- a) get the local TcIds from the environment,
+ -- and pass them to find_globals (they might have tv free)
+ -- b) similarly, find any free_tyvars that mention tv
+ then tcGetEnv `thenNF_Tc` \ ve ->
+ find_globals tv tidy_env [] (tcEnvTcIds ve) `thenNF_Tc` \ (tidy_env1, globs) ->
+ find_frees tv tidy_env1 [] (varSetElems free_tyvars) `thenNF_Tc` \ (tidy_env2, frees) ->
+ returnNF_Tc (tidy_env2, acc, escape_msg sig_tyvar tv globs frees : msgs)
+
+ else -- All OK
+ returnNF_Tc (tidy_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 :: Var
+ -> TidyEnv
+ -> [(Name,Type)]
+ -> [Id]
+ -> NF_TcM (TidyEnv,[(Name,Type)])
+
+find_globals tv tidy_env acc []
+ = returnNF_Tc (tidy_env, acc)
+
+find_globals tv tidy_env acc (id:ids)
+ | 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') = tidyOpenTyVar 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") <+> thing
+mk_msg tv = ptext SLIT("Quantified type variable") <+> quotes (ppr tv)