- returnDs (MatchResult CantFail ty expr_fn (DsMatchContext kind pats locn))
- )
-
-dsGRHS ty kind pats (GRHS guard expr locn)
- = putSrcLocDs locn (
- dsExpr guard `thenDs` \ core_guard ->
- dsExpr expr `thenDs` \ core_expr ->
- let
- expr_fn = \ fail -> mkCoreIfThenElse core_guard core_expr fail
- in
- returnDs (MatchResult CanFail ty expr_fn (DsMatchContext kind pats locn))
- )
+ returnDs (ty, match_result2)
+
+dsGRHS kind pats (GRHS guard locn)
+ = matchGuard guard (DsMatchContext kind pats locn)
+\end{code}
+
+
+%************************************************************************
+%* *
+%* matchGuard : make a MatchResult from a guarded RHS *
+%* *
+%************************************************************************
+
+\begin{code}
+matchGuard :: [TypecheckedStmt] -- Guard
+ -> DsMatchContext -- Context
+ -> DsM MatchResult
+
+matchGuard (ExprStmt expr locn : should_be_null) ctx
+ = putSrcLocDs locn (dsExpr expr) `thenDs` \ core_expr ->
+ returnDs (cantFailMatchResult core_expr)
+
+ -- Turn an "otherwise" guard is a no-op
+matchGuard (GuardStmt (HsVar v) _ : stmts) ctx
+ | uniq == otherwiseIdKey
+ || uniq == trueDataConKey
+ = matchGuard stmts ctx
+ where
+ uniq = getUnique v
+
+matchGuard (GuardStmt expr locn : stmts) ctx
+ = matchGuard stmts ctx `thenDs` \ match_result ->
+ putSrcLocDs locn (dsExpr expr) `thenDs` \ pred_expr ->
+ returnDs (mkGuardedMatchResult pred_expr match_result)
+
+matchGuard (LetStmt binds : stmts) ctx
+ = matchGuard stmts ctx `thenDs` \ match_result ->
+ returnDs (adjustMatchResultDs (dsLet binds) match_result)
+ -- NB the dsLet occurs inside the match_result
+
+matchGuard (BindStmt pat rhs locn : stmts) ctx
+ = matchGuard stmts ctx `thenDs` \ match_result ->
+ putSrcLocDs locn (dsExpr rhs) `thenDs` \ core_rhs ->
+ matchSinglePat core_rhs ctx pat match_result