- match_ctxt = mc_what ctxt
- stmt_ctxt = PatGuard match_ctxt
-\end{code}
-
-
-\begin{code}
-tcThingWithSig :: TcSigmaType -- Type signature
- -> (TcRhoType -> TcM r) -- How to type check the thing inside
- -> Expected TcRhoType -- Overall expected result type
- -> TcM (ExprCoFn, r)
--- Used for expressions with a type signature, and for result type signatures
-
-tcThingWithSig sig_ty thing_inside res_ty
- | not (isSigmaTy sig_ty)
- = thing_inside sig_ty `thenM` \ result ->
- tcSubExp res_ty sig_ty `thenM` \ co_fn ->
- returnM (co_fn, result)
-
- | otherwise -- The signature has some outer foralls
- = -- Must instantiate the outer for-alls of sig_tc_ty
- -- else we risk instantiating a ? res_ty to a forall-type
- -- which breaks the invariant that tcMonoExpr only returns phi-types
- tcGen sig_ty emptyVarSet thing_inside `thenM` \ (gen_fn, result) ->
- tcInstCall InstSigOrigin sig_ty `thenM` \ (inst_fn, _, inst_sig_ty) ->
- tcSubExp res_ty inst_sig_ty `thenM` \ co_fn ->
- returnM (co_fn <.> inst_fn <.> gen_fn, result)
- -- Note that we generalise, then instantiate. Ah well.
-\end{code}
-
-
-%************************************************************************
-%* *
-\subsection{tcMatchPats}
-%* *
-%************************************************************************
-
-\begin{code}
-tcMatchPats :: [LPat Name]
- -> [Expected TcSigmaType] -- Pattern types
- -> Expected TcRhoType -- Result type;
- -- used only to check existential escape
- -> TcM a
- -> TcM ([LPat TcId], a)
--- Typecheck the patterns, extend the environment to bind the variables,
--- do the thing inside, use any existentially-bound dictionaries to
--- discharge parts of the returning LIE, and deal with pattern type
--- signatures
-
-tcMatchPats pats tys body_ty thing_inside
- = do { (pats', ex_tvs, res) <- tcPats LamPat pats tys thing_inside
- ; tcCheckExistentialPat pats' ex_tvs tys body_ty
- ; returnM (pats', res) }
-
-tcCheckExistentialPat :: [LPat TcId] -- Patterns (just for error message)
- -> [TcTyVar] -- Existentially quantified tyvars bound by pattern
- -> [Expected TcSigmaType] -- Types of the patterns
- -> Expected TcRhoType -- Type of the body of the match
- -- Tyvars in either of these must not escape
- -> TcM ()
- -- NB: we *must* pass "pats_tys" not just "body_ty" to tcCheckExistentialPat
- -- For example, we must reject this program:
- -- data C = forall a. C (a -> Int)
- -- f (C g) x = g x
- -- Here, result_ty will be simply Int, but expected_ty is (C -> a -> Int).
-
-tcCheckExistentialPat pats [] pat_tys body_ty
- = return () -- Short cut for case when there are no existentials
-
-tcCheckExistentialPat pats ex_tvs pat_tys body_ty
- = do { tys <- mapM readExpectedType (body_ty : pat_tys)
- ; addErrCtxtM (sigPatCtxt (collectPatsBinders pats) ex_tvs tys) $
- checkSigTyVarsWrt (tyVarsOfTypes tys) ex_tvs }