-@checkSigMatch@ does the next step in checking signature matching.
-The tau-type part has already been unified. What we do here is to
-check that this unification has not over-constrained the (polymorphic)
-type variables of the original signature type.
-
-The error message here is somewhat unsatisfactory, but it'll do for
-now (ToDo).
-
-\begin{code}
-checkSigMatch top_lvl binder_names mono_ids sigs
- | main_bound_here
- = -- First unify the main_id with IO t, for any old t
- tcSetErrCtxt mainTyCheckCtxt (
- tcLookupTyCon ioTyCon_NAME `thenTc` \ ioTyCon ->
- newTyVarTy boxedTypeKind `thenNF_Tc` \ t_tv ->
- unifyTauTy ((mkTyConApp ioTyCon [t_tv]))
- (idType main_mono_id)
- ) `thenTc_`
-
- -- Now check the signatures
- -- Must do this after the unification with IO t,
- -- in case of a silly signature like
- -- main :: forall a. a
- -- The unification to IO t will bind the type variable 'a',
- -- which is just waht check_one_sig looks for
- mapTc check_one_sig sigs `thenTc_`
- mapTc check_main_ctxt sigs `thenTc_`
-
- returnTc (Just ([], emptyLIE))
-
- | not (null sigs)
- = mapTc check_one_sig sigs `thenTc_`
- mapTc check_one_ctxt all_sigs_but_first `thenTc_`
- returnTc (Just (theta1, sig_lie))
-
- | otherwise
- = returnTc Nothing -- No constraints from type sigs
-
- where
- (TySigInfo _ id1 _ theta1 _ _ _ _ : all_sigs_but_first) = sigs
-
- sig1_dict_tys = mk_dict_tys theta1
- n_sig1_dict_tys = length sig1_dict_tys
- sig_lie = mkLIE [inst | TySigInfo _ _ _ _ _ _ inst _ <- sigs]
-
- maybe_main = find_main top_lvl binder_names mono_ids
- main_bound_here = maybeToBool maybe_main
- Just main_mono_id = maybe_main
-
- -- CHECK THAT THE SIGNATURE TYVARS AND TAU_TYPES ARE OK
- -- Doesn't affect substitution
- check_one_sig (TySigInfo _ id sig_tyvars _ sig_tau _ _ src_loc)
- = tcAddSrcLoc src_loc $
- tcAddErrCtxtM (sigCtxt (sig_msg id) (idType id)) $
- checkSigTyVars sig_tyvars
-
-
- -- CHECK THAT ALL THE SIGNATURE CONTEXTS ARE UNIFIABLE
- -- The type signatures on a mutually-recursive group of definitions
- -- must all have the same context (or none).
- --
- -- We unify them because, with polymorphic recursion, their types
- -- might not otherwise be related. This is a rather subtle issue.
- -- ToDo: amplify
- check_one_ctxt sig@(TySigInfo _ id _ theta _ _ _ src_loc)
- = tcAddSrcLoc src_loc $
- tcAddErrCtxt (sigContextsCtxt id1 id) $
- checkTc (length this_sig_dict_tys == n_sig1_dict_tys)
- sigContextsErr `thenTc_`
- unifyTauTyLists sig1_dict_tys this_sig_dict_tys
- where
- this_sig_dict_tys = mk_dict_tys theta
-
- -- CHECK THAT FOR A GROUP INVOLVING Main.main, all
- -- the signature contexts are empty (what a bore)
- check_main_ctxt sig@(TySigInfo _ id _ theta _ _ _ src_loc)
- = tcAddSrcLoc src_loc $
- checkTc (null theta) (mainContextsErr id)
-
- mk_dict_tys theta = [mkDictTy c ts | (c,ts) <- theta]
-
- sig_msg id tidy_ty = sep [ptext SLIT("When checking the type signature"),
- nest 4 (ppr id <+> dcolon <+> ppr tidy_ty)]
-
- -- Search for Main.main in the binder_names, return corresponding mono_id
- find_main NotTopLevel binder_names mono_ids = Nothing
- find_main TopLevel binder_names mono_ids = go binder_names mono_ids
- go [] [] = Nothing
- go (n:ns) (m:ms) | n == main_NAME = Just m
- | otherwise = go ns ms