- returnTc (complete_it, lie_req, tvs, ids, lie_avail)
-
- -- Figure out the appropriate kind for the pattern,
- -- and generate a suitable type variable
- kind = case is_rec of
- Recursive -> boxedTypeKind -- Recursive, so no unboxed types
- NonRecursive -> openTypeKind -- Non-recursive, so we permit unboxed types
-\end{code}
-
-%************************************************************************
-%* *
-\subsection{Signatures}
-%* *
-%************************************************************************
-
-@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 :: TopLevelFlag -> [Name] -> [TcId] -> [TcSigInfo] -> TcM (Maybe (TcThetaType, LIE))
-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 ioTyConName `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 (concat [insts | TySigInfo _ _ _ _ _ _ insts _ <- 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_theta sig_tau _ _ src_loc)
- = tcAddSrcLoc src_loc $
- tcAddErrCtxtM (sigCtxt (sig_msg id) sig_tyvars sig_theta sig_tau) $
- checkSigTyVars sig_tyvars (idFreeTyVars id)
-
-
- -- 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 = map mkPredTy theta
-
- sig_msg id = ptext SLIT("When checking the type signature for") <+> quotes (ppr id)
-
- -- 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 `hasKey` mainKey = Just m
- | otherwise = go ns ms
+ returnM (complete_it, tvs, ids, lie_avail)
+
+ -- tc_pat_bndr is used when dealing with a LHS binder in a pattern.
+ -- If there was a type sig for that Id, we want to make it much
+ -- as if that type signature had been on the binder as a SigPatIn.
+ -- We check for a type signature; if there is one, we use the mono_id
+ -- from the signature. This is how we make sure the tau part of the
+ -- signature actually matches the type of the LHS; then tc_mb_pats
+ -- ensures the LHS and RHS have the same type
+
+ tc_pat_bndr name pat_ty
+ = case maybeSig tc_ty_sigs name of
+ Nothing
+ -> newLocalName name `thenM` \ bndr_name ->
+ tcMonoPatBndr bndr_name pat_ty
+
+ Just sig -> addSrcLoc (getSrcLoc name) $
+ tcSubPat (idType mono_id) pat_ty `thenM` \ co_fn ->
+ returnM (co_fn, mono_id)
+ where
+ mono_id = tcSigMonoId sig