import {-# SOURCE #-} DsExpr ( dsExpr, dsLet )
import {-# SOURCE #-} Match ( matchSinglePat )
-import HsSyn ( GRHSsAndBinds(..), Stmt(..), HsExpr(..), GRHS(..) )
-import TcHsSyn ( TypecheckedGRHSsAndBinds, TypecheckedGRHS,
- TypecheckedPat, TypecheckedStmt
- )
-import CoreSyn ( CoreExpr, Bind(..) )
+import HsSyn ( Stmt(..), HsExpr(..), GRHSs(..), GRHS(..), HsMatchContext(..) )
+import TcHsSyn ( TypecheckedGRHSs, TypecheckedPat, TypecheckedStmt, TypecheckedMatchContext )
+import CoreSyn ( CoreExpr )
+import Type ( Type )
import DsMonad
import DsUtils
-import PrelVals ( nON_EXHAUSTIVE_GUARDS_ERROR_ID )
-import Unique ( otherwiseIdKey, trueDataConKey, Uniquable(..) )
-import Outputable
+import PrelInfo ( nON_EXHAUSTIVE_GUARDS_ERROR_ID )
+import PrelNames ( otherwiseIdKey, trueDataConKey, hasKey )
\end{code}
@dsGuarded@ is used for both @case@ expressions and pattern bindings.
where binds
\end{verbatim}
producing an expression with a runtime error in the corner if
-necessary. The type argument gives the type of the ei.
+necessary. The type argument gives the type of the @ei@.
\begin{code}
-dsGuarded :: TypecheckedGRHSsAndBinds
- -> DsM CoreExpr
+dsGuarded :: TypecheckedGRHSs -> DsM CoreExpr
-dsGuarded (GRHSsAndBindsOut grhss binds err_ty)
- = dsGRHSs PatBindMatch [] grhss `thenDs` \ match_result ->
+dsGuarded grhss
+ = dsGRHSs PatBindRhs [] grhss `thenDs` \ (err_ty, match_result) ->
mkErrorAppDs nON_EXHAUSTIVE_GUARDS_ERROR_ID err_ty "" `thenDs` \ error_expr ->
- extractMatchResult match_result error_expr `thenDs` \ body ->
- dsLet binds body
+ extractMatchResult match_result error_expr
\end{code}
-Desugar a list of (grhs, expr) pairs [grhs = guarded
-right-hand-side], as in:
-\begin{verbatim}
-p | g1 = e1
- | g2 = e2
- ...
- | gm = em
-\end{verbatim}
-We supply a @CoreExpr@ for the case in which all of
-the guards fail.
+In contrast, @dsGRHSs@ produces a @MatchResult@.
\begin{code}
-dsGRHSs :: DsMatchKind -> [TypecheckedPat] -- These are to build a MatchContext from
- -> [TypecheckedGRHS] -- Guarded RHSs
- -> DsM MatchResult
-
-dsGRHSs kind pats [grhs] = dsGRHS kind pats grhs
-
-dsGRHSs kind pats (grhs:grhss)
- = dsGRHS kind pats grhs `thenDs` \ match_result1 ->
- dsGRHSs kind pats grhss `thenDs` \ match_result2 ->
- returnDs (combineMatchResults match_result1 match_result2)
+dsGRHSs :: TypecheckedMatchContext -> [TypecheckedPat] -- These are to build a MatchContext from
+ -> TypecheckedGRHSs -- Guarded RHSs
+ -> DsM (Type, MatchResult)
+
+dsGRHSs kind pats (GRHSs grhss binds ty)
+ = mapDs (dsGRHS kind pats) grhss `thenDs` \ match_results ->
+ let
+ match_result1 = foldr1 combineMatchResults match_results
+ match_result2 = adjustMatchResultDs (dsLet binds) match_result1
+ -- NB: nested dsLet inside matchResult
+ in
+ returnDs (ty, match_result2)
dsGRHS kind pats (GRHS guard locn)
= matchGuard guard (DsMatchContext kind pats locn)
-> DsMatchContext -- Context
-> DsM MatchResult
-matchGuard (ExprStmt expr locn : should_be_null) ctx
+-- See comments with HsExpr.Stmt re what an ExprStmt means
+-- Here we must be in a guard context (not do-expression, nor list-comp)
+
+matchGuard [ResultStmt expr locn] ctx
= putSrcLocDs locn (dsExpr expr) `thenDs` \ core_expr ->
returnDs (cantFailMatchResult core_expr)
+ -- ExprStmts must be guards
-- Turn an "otherwise" guard is a no-op
-matchGuard (GuardStmt (HsVar v) _ : stmts) ctx
- | uniq == otherwiseIdKey
- || uniq == trueDataConKey
+matchGuard (ExprStmt (HsVar v) _ _ : stmts) ctx
+ | v `hasKey` otherwiseIdKey
+ || v `hasKey` trueDataConKey
= matchGuard stmts ctx
- where
- uniq = getUnique v
-matchGuard (GuardStmt expr locn : stmts) ctx
+matchGuard (ExprStmt expr _ locn : stmts) ctx
= matchGuard stmts ctx `thenDs` \ match_result ->
putSrcLocDs locn (dsExpr expr) `thenDs` \ pred_expr ->
returnDs (mkGuardedMatchResult pred_expr match_result)
matchSinglePat core_rhs ctx pat match_result
\end{code}
--- Should *fail* if e returns D
-
+Should {\em fail} if @e@ returns @D@
+\begin{verbatim}
f x | p <- e', let C y# = e, f y# = r1
| otherwise = r2
+\end{verbatim}