+\begin{code}
+tcMatch :: [(Name,Id)]
+ -> RenamedMatch
+ -> TcType -- Expected result-type of the Match.
+ -- Early unification with this guy gives better error messages
+ -> StmtCtxt
+ -> TcM (TcMatch, LIE)
+
+tcMatch xve1 match@(Match sig_tvs pats maybe_rhs_sig grhss) expected_ty ctxt
+ = tcAddSrcLoc (getMatchLoc match) $
+ tcAddErrCtxt (matchCtxt ctxt match) $
+
+ if null sig_tvs then -- The common case
+ tc_match expected_ty `thenTc` \ (_, match_and_lie) ->
+ returnTc match_and_lie
+
+ else
+ -- If there are sig tvs we must be careful *not* to use
+ -- expected_ty right away, else we'll unify with tyvars free
+ -- in the envt. So invent a fresh tyvar and use that instead
+ newTyVarTy openTypeKind `thenNF_Tc` \ tyvar_ty ->
+
+ -- Extend the tyvar env and check the match itself
+ tcTyVars sig_tvs (mapTc_ kcHsSigType sig_tys) `thenTc` \ sig_tyvars ->
+ tcExtendTyVarEnv sig_tyvars (tc_match tyvar_ty) `thenTc` \ (pat_ids, match_and_lie) ->
+
+ -- Check that the scoped type variables from the patterns
+ -- have not been constrained
+ tcAddErrCtxtM (sigPatCtxt sig_tyvars pat_ids) (
+ checkSigTyVars sig_tyvars emptyVarSet
+ ) `thenTc_`
+
+ -- *Now* we're free to unify with expected_ty
+ unifyTauTy expected_ty tyvar_ty `thenTc_`
+
+ returnTc match_and_lie
+
+ where
+ sig_tys = case maybe_rhs_sig of { Just t -> [t]; Nothing -> [] }
+ ++ collectSigTysFromPats pats
+
+ tc_match expected_ty -- Any sig tyvars are in scope by now
+ = -- STEP 1: Typecheck the patterns
+ tcMatchPats pats expected_ty `thenTc` \ (rhs_ty, pats', lie_req1, ex_tvs, pat_bndrs, lie_avail) ->
+ let
+ xve2 = bagToList pat_bndrs
+ pat_ids = map snd xve2
+ ex_tv_list = bagToList ex_tvs
+ in
+
+ -- STEP 2: Check that the remaining "expected type" is not a rank-2 type
+ -- If it is it'll mess up the unifier when checking the RHS
+ checkTc (isTauTy rhs_ty) lurkingRank2SigErr `thenTc_`
+
+ -- STEP 3: Unify with the rhs type signature if any
+ (case maybe_rhs_sig of
+ Nothing -> returnTc ()
+ Just sig -> tcHsSigType sig `thenTc` \ sig_ty ->
+
+ -- Check that the signature isn't a polymorphic one, which
+ -- we don't permit (at present, anyway)
+ checkTc (isTauTy sig_ty) (polyPatSig sig_ty) `thenTc_`
+ unifyTauTy rhs_ty sig_ty
+ ) `thenTc_`
+
+ -- STEP 4: Typecheck the guarded RHSs and the associated where clause
+ tcExtendLocalValEnv xve1 (tcExtendLocalValEnv xve2 (
+ tcGRHSs grhss rhs_ty ctxt
+ )) `thenTc` \ (grhss', lie_req2) ->
+
+ -- STEP 5: Check for existentially bound type variables
+ tcExtendGlobalTyVars (tyVarsOfType rhs_ty) (
+ tcAddErrCtxtM (sigPatCtxt ex_tv_list pat_ids) $
+ checkSigTyVars ex_tv_list emptyVarSet `thenTc` \ zonked_ex_tvs ->
+ tcSimplifyAndCheck
+ (text ("the existential context of a data constructor"))
+ (mkVarSet zonked_ex_tvs)
+ lie_avail (lie_req1 `plusLIE` lie_req2)
+ ) `thenTc` \ (lie_req', ex_binds) ->
+
+ -- STEP 6 In case there are any polymorpic, overloaded binders in the pattern
+ -- (which can happen in the case of rank-2 type signatures, or data constructors
+ -- with polymorphic arguments), we must do a bindInstsOfLocalFns here
+ bindInstsOfLocalFuns lie_req' pat_ids `thenTc` \ (lie_req'', inst_binds) ->
+
+ -- Phew! All done.
+ let
+ grhss'' = glue_on Recursive ex_binds $
+ glue_on Recursive inst_binds grhss'
+ in
+ returnTc (pat_ids, (Match [] pats' Nothing grhss'', lie_req''))
+
+ -- glue_on just avoids stupid dross
+glue_on _ EmptyMonoBinds grhss = grhss -- The common case
+glue_on is_rec mbinds (GRHSs grhss binds ty)
+ = GRHSs grhss (mkMonoBind mbinds [] is_rec `ThenBinds` binds) ty
+
+tcGRHSs :: RenamedGRHSs
+ -> TcType -> StmtCtxt
+ -> TcM (TcGRHSs, LIE)
+
+tcGRHSs (GRHSs grhss binds _) expected_ty ctxt
+ = tcBindsAndThen glue_on binds (tc_grhss grhss)
+ where
+ tc_grhss grhss
+ = mapAndUnzipTc tc_grhs grhss `thenTc` \ (grhss', lies) ->
+ returnTc (GRHSs grhss' EmptyBinds (Just expected_ty), plusLIEs lies)
+
+ tc_grhs (GRHS guarded locn)
+ = tcAddSrcLoc locn $
+ tcStmts ctxt (\ty -> ty) guarded expected_ty `thenTc` \ (guarded', lie) ->
+ returnTc (GRHS guarded' locn, lie)