- tc_grhss pats' rhs_ty
- = -- 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` \_ ->
-
- -- Deal with the result signature
- -- It "wraps" the rest of the body typecheck because it may
- -- bring into scope the type variables in the signature
- tc_result_sig maybe_rhs_sig rhs_ty $
-
- -- Typecheck the body
- tcExtendLocalValEnv xve1 $
- tcGRHSs ctxt grhss rhs_ty `thenTc` \ (grhss', lie) ->
- returnTc ((pats', grhss'), lie)
-
- tc_result_sig Nothing rhs_ty thing_inside
- = thing_inside
- tc_result_sig (Just sig) rhs_ty thing_inside
- = tcAddScopedTyVars [sig] $
- tcHsSigType ResSigCtxt 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 sig_ty rhs_ty `thenTc_`
- thing_inside
-
-
- -- 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)
+ lift_grhs (GRHS stmts) = GRHS (map lift_stmt stmts)
+
+ lift_stmt (L loc (ResultStmt e)) = L loc (ResultStmt (fmap (co_fn <$>) e))
+ lift_stmt stmt = stmt
+
+-------------
+tcGRHSs :: TcMatchCtxt -> GRHSs Name
+ -> Expected TcRhoType
+ -> TcM (GRHSs TcId)
+
+ -- Special case when there is just one equation with a degenerate
+ -- guard; then we pass in the full Expected type, so that we get
+ -- good inference from simple things like
+ -- f = \(x::forall a.a->a) -> <stuff>
+ -- This is a consequence of the fact that tcStmts takes a TcType,
+ -- not a Expected TcType, a decision we could revisit if necessary
+tcGRHSs ctxt (GRHSs [L loc1 (GRHS [L loc2 (ResultStmt rhs)])] binds) exp_ty
+ = tcBindsAndThen glueBindsOnGRHSs binds $
+ mc_body ctxt rhs exp_ty `thenM` \ rhs' ->
+ returnM (GRHSs [L loc1 (GRHS [L loc2 (ResultStmt rhs')])] [])
+
+tcGRHSs ctxt (GRHSs grhss binds) exp_ty
+ = tcBindsAndThen glueBindsOnGRHSs binds $
+ zapExpectedType exp_ty openTypeKind `thenM` \ exp_ty' ->
+ -- Even if there is only one guard, we zap the RHS type to
+ -- a monotype. Reason: it makes tcStmts much easier,
+ -- and even a one-armed guard has a notional second arm
+ let
+ stmt_ctxt = SC { sc_what = PatGuard (mc_what ctxt),
+ sc_rhs = tcInferRho,
+ sc_body = sc_body,
+ sc_ty = exp_ty' }
+ sc_body body = mc_body ctxt body (Check exp_ty')
+
+ tc_grhs (GRHS guarded)
+ = tcStmts stmt_ctxt guarded `thenM` \ guarded' ->
+ returnM (GRHS guarded')
+ in
+ mappM (wrapLocM tc_grhs) grhss `thenM` \ grhss' ->
+ returnM (GRHSs grhss' [])
+\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.