- -> TcM s (TcMatch s,LIE s) -- NB No type returned, because it was passed
- -- in instead!
-
-tcMatchExpected expected_ty the_match@(PatMatch pat match)
- = case getFunTy_maybe expected_ty of
-
- Nothing -> -- Not a function type (eg type variable)
- -- So use tcMatch instead
- tcMatch the_match `thenTc` \ (match', lie_match, match_ty) ->
- unifyTauTy match_ty expected_ty `thenTc_`
- returnTc (match', lie_match)
-
- Just (arg_ty,rest_ty) -> -- It's a function type!
- let binders = collectPatBinders pat
- in
- newMonoIds binders mkTypeKind (\ _ ->
- tcPat pat `thenTc` \ (pat', lie_pat, pat_ty) ->
- unifyTauTy arg_ty pat_ty `thenTc_`
- tcMatchExpected rest_ty match `thenTc` \ (match', lie_match) ->
- returnTc (PatMatch pat' match',
- plusLIE lie_pat lie_match)
- )
-
-tcMatchExpected expected_ty (GRHSMatch grhss_and_binds)
- = tcGRHSsAndBinds grhss_and_binds `thenTc` \ (grhss_and_binds', lie, grhss_ty) ->
- unifyTauTy grhss_ty expected_ty `thenTc_`
- returnTc (GRHSMatch grhss_and_binds', lie)
-
-tcMatch :: RenamedMatch -> TcM s (TcMatch s, LIE s, TcType s)
-
-tcMatch (PatMatch pat match)
- = let binders = collectPatBinders pat
+ -> TcType -- Expected result-type of the Match.
+ -- Early unification with this guy gives better error messages
+ -- We regard the Match as having type
+ -- (ty1 -> ... -> tyn -> result_ty)
+ -- where there are n patterns.
+ -> TcM (TcMatch, LIE)
+
+tcMatch xve1 ctxt match@(Match pats maybe_rhs_sig grhss) expected_ty
+ = tcAddSrcLoc (getMatchLoc match) $ -- At one stage I removed this;
+ tcAddErrCtxt (matchCtxt ctxt match) $ -- I'm not sure why, so I put it back
+ tcMatchPats pats expected_ty tc_grhss `thenTc` \ (pats', grhss', lie, ex_binds) ->
+ returnTc (Match pats' Nothing (glue_on Recursive ex_binds grhss'), lie)
+
+ where
+ tc_grhss rhs_ty
+ = tcExtendLocalValEnv2 xve1 $
+
+ -- Deal with the result signature
+ case maybe_rhs_sig of
+ Nothing -> tcGRHSs ctxt grhss rhs_ty
+
+ Just sig -> tcAddScopedTyVars [sig] $
+ -- Bring into scope the type variables in the signature
+ tcHsSigType ResSigCtxt sig `thenTc` \ sig_ty ->
+ tcGRHSs ctxt grhss sig_ty `thenTc` \ (grhss', lie1) ->
+ tcSubExp rhs_ty sig_ty `thenTc` \ (co_fn, lie2) ->
+ returnTc (lift_grhss co_fn rhs_ty grhss',
+ lie1 `plusLIE` lie2)
+
+-- lift_grhss pushes the coercion down to the right hand sides,
+-- because there is no convenient place to hang it otherwise.
+lift_grhss co_fn rhs_ty grhss
+ | isIdCoercion co_fn = grhss
+lift_grhss co_fn rhs_ty (GRHSs grhss binds ty)
+ = GRHSs (map lift_grhs grhss) binds rhs_ty -- Change the type, since we
+ where
+ lift_grhs (GRHS stmts loc) = GRHS (map lift_stmt stmts) loc
+
+ lift_stmt (ResultStmt e l) = ResultStmt (co_fn <$> e) l
+ lift_stmt stmt = stmt
+
+-- 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 :: RenamedMatchContext -> RenamedGRHSs
+ -> TcType
+ -> TcM (TcGRHSs, LIE)
+
+tcGRHSs ctxt (GRHSs grhss binds _) expected_ty
+ = tcBindsAndThen glue_on binds (tc_grhss grhss)
+ where
+ tc_grhss grhss
+ = mapAndUnzipTc tc_grhs grhss `thenTc` \ (grhss', lies) ->
+ returnTc (GRHSs grhss' EmptyBinds expected_ty, plusLIEs lies)
+
+ tc_grhs (GRHS guarded locn)
+ = tcAddSrcLoc locn $
+ tcStmts ctxt (\ty -> ty, expected_ty) guarded `thenTc` \ (guarded', lie) ->
+ returnTc (GRHS guarded' locn, lie)
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{tcMatchPats}
+%* *
+%************************************************************************
+
+\begin{code}
+tcMatchPats
+ :: [RenamedPat] -> TcType
+ -> (TcType -> TcM (a, LIE))
+ -> TcM ([TypecheckedPat], a, LIE, TcDictBinds)
+-- 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 expected_ty thing_inside
+ = -- STEP 1: Bring pattern-signature type variables into scope
+ tcAddScopedTyVars (collectSigTysFromPats pats) (
+
+ -- STEP 2: Typecheck the patterns themselves, gathering all the stuff
+ -- then do the thing inside
+ tc_match_pats pats expected_ty thing_inside
+
+ ) `thenTc` \ (pats', lie_req, ex_tvs, ex_ids, ex_lie, result) ->
+
+ -- STEP 4: Check for existentially bound type variables
+ -- Do this *outside* the scope of the tcAddScopedTyVars, else checkSigTyVars
+ -- complains that 'a' is captured by the inscope 'a'! (Test (d) in checkSigTyVars.)
+ --
+ -- I'm a bit concerned that lie_req1 from an 'inner' pattern in the list
+ -- might need (via lie_req2) something made available from an 'outer'
+ -- pattern. But it's inconvenient to deal with, and I can't find an example
+ tcCheckExistentialPat ex_tvs ex_ids ex_lie lie_req expected_ty `thenTc` \ (lie_req', ex_binds) ->
+ -- NB: we *must* pass "expected_ty" not "result_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 (a -> Int).
+
+ returnTc (pats', result, lie_req', ex_binds)
+
+tc_match_pats [] expected_ty thing_inside
+ = thing_inside expected_ty `thenTc` \ (answer, lie) ->
+ returnTc ([], lie, emptyBag, [], emptyLIE, answer)
+
+tc_match_pats (pat:pats) expected_ty thing_inside
+ = subFunTy expected_ty $ \ arg_ty rest_ty ->
+ -- This is the unique place we call subFunTy
+ -- The point is that if expected_y is a "hole", we want
+ -- to make arg_ty and rest_ty as "holes" too.
+ tcPat tcMonoPatBndr pat arg_ty `thenTc` \ (pat', lie_req, ex_tvs, pat_bndrs, ex_lie) ->
+ let
+ xve = bagToList pat_bndrs
+ ex_ids = [id | (_, id) <- xve]
+ -- ex_ids is all the pattern-bound Ids, a superset
+ -- of the existential Ids used in checkExistentialPat