2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[DsGRHSs]{Matching guarded right-hand-sides (GRHSs)}
7 module DsGRHSs ( dsGuarded, dsGRHSs ) where
9 #include "HsVersions.h"
11 import {-# SOURCE #-} DsExpr ( dsLExpr, dsLet )
12 import {-# SOURCE #-} Match ( matchSinglePat )
14 import HsSyn ( Stmt(..), HsExpr(..), GRHSs(..), GRHS(..),
15 LHsExpr, HsMatchContext(..), Pat(..) )
16 import CoreSyn ( CoreExpr )
22 import Unique ( Uniquable(..) )
23 import PrelInfo ( nON_EXHAUSTIVE_GUARDS_ERROR_ID )
24 import TysWiredIn ( trueDataConId )
25 import PrelNames ( otherwiseIdKey, hasKey )
27 import SrcLoc ( unLoc, Located(..) )
30 @dsGuarded@ is used for both @case@ expressions and pattern bindings.
38 producing an expression with a runtime error in the corner if
39 necessary. The type argument gives the type of the @ei@.
42 dsGuarded :: GRHSs Id -> Type -> DsM CoreExpr
44 dsGuarded grhss rhs_ty
45 = dsGRHSs PatBindRhs [] grhss rhs_ty `thenDs` \ match_result ->
46 mkErrorAppDs nON_EXHAUSTIVE_GUARDS_ERROR_ID rhs_ty "" `thenDs` \ error_expr ->
47 extractMatchResult match_result error_expr
50 In contrast, @dsGRHSs@ produces a @MatchResult@.
53 dsGRHSs :: HsMatchContext Name -> [Pat Id] -- These are to build a MatchContext from
54 -> GRHSs Id -- Guarded RHSs
55 -> Type -- Type of RHS
58 dsGRHSs kind pats (GRHSs grhss binds) rhs_ty
59 = mappM (dsGRHS kind pats rhs_ty) grhss `thenDs` \ match_results ->
61 match_result1 = foldr1 combineMatchResults match_results
62 match_result2 = adjustMatchResultDs (dsLet binds) match_result1
63 -- NB: nested dsLet inside matchResult
65 returnDs match_result2
67 dsGRHS kind pats rhs_ty (L loc (GRHS guards rhs))
68 = matchGuard (map unLoc guards) (DsMatchContext kind pats loc)
73 %************************************************************************
75 %* matchGuard : make a MatchResult from a guarded RHS *
77 %************************************************************************
80 matchGuard :: [Stmt Id] -- Guard
81 -> DsMatchContext -- Context
83 -> Type -- Type of RHS of guard
86 -- See comments with HsExpr.Stmt re what an ExprStmt means
87 -- Here we must be in a guard context (not do-expression, nor list-comp)
89 matchGuard [] ctx rhs rhs_ty
90 = do { core_rhs <- dsLExpr rhs
91 ; return (cantFailMatchResult core_rhs) }
93 -- ExprStmts must be guards
94 -- Turn an "otherwise" guard is a no-op
95 matchGuard (ExprStmt (L _ (HsVar v)) _ _ : stmts) ctx rhs rhs_ty
96 | v `hasKey` otherwiseIdKey
97 || v `hasKey` getUnique trueDataConId
98 -- trueDataConId doesn't have the same
99 -- unique as trueDataCon
100 = matchGuard stmts ctx rhs rhs_ty
102 matchGuard (ExprStmt expr _ _ : stmts) ctx rhs rhs_ty
103 = matchGuard stmts ctx rhs rhs_ty `thenDs` \ match_result ->
104 dsLExpr expr `thenDs` \ pred_expr ->
105 returnDs (mkGuardedMatchResult pred_expr match_result)
107 matchGuard (LetStmt binds : stmts) ctx rhs rhs_ty
108 = matchGuard stmts ctx rhs rhs_ty `thenDs` \ match_result ->
109 returnDs (adjustMatchResultDs (dsLet binds) match_result)
110 -- NB the dsLet occurs inside the match_result
111 -- Reason: dsLet takes the body expression as its argument
112 -- so we can't desugar the bindings without the
113 -- body expression in hand
115 matchGuard (BindStmt pat bind_rhs _ _ : stmts) ctx rhs rhs_ty
116 = matchGuard stmts ctx rhs rhs_ty `thenDs` \ match_result ->
117 dsLExpr bind_rhs `thenDs` \ core_rhs ->
118 matchSinglePat core_rhs ctx pat rhs_ty match_result
121 Should {\em fail} if @e@ returns @D@
123 f x | p <- e', let C y# = e, f y# = r1