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 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 -> DsM CoreExpr
45 = dsGRHSs PatBindRhs [] grhss `thenDs` \ (err_ty, match_result) ->
46 mkErrorAppDs nON_EXHAUSTIVE_GUARDS_ERROR_ID err_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 -> DsM (Type, MatchResult)
57 dsGRHSs kind pats (GRHSs grhss binds ty)
58 = mappM (dsGRHS kind pats) grhss `thenDs` \ match_results ->
60 match_result1 = foldr1 combineMatchResults match_results
61 match_result2 = adjustMatchResultDs (dsLet binds) match_result1
62 -- NB: nested dsLet inside matchResult
64 returnDs (ty, match_result2)
66 dsGRHS kind pats (L loc (GRHS guard))
67 = matchGuard (map unLoc guard) (DsMatchContext kind pats loc)
71 %************************************************************************
73 %* matchGuard : make a MatchResult from a guarded RHS *
75 %************************************************************************
78 matchGuard :: [Stmt Id] -- Guard
79 -> DsMatchContext -- Context
82 -- See comments with HsExpr.Stmt re what an ExprStmt means
83 -- Here we must be in a guard context (not do-expression, nor list-comp)
85 matchGuard [ResultStmt expr] ctx
86 = dsLExpr expr `thenDs` \ core_expr ->
87 returnDs (cantFailMatchResult core_expr)
89 -- ExprStmts must be guards
90 -- Turn an "otherwise" guard is a no-op
91 matchGuard (ExprStmt (L _ (HsVar v)) _ : stmts) ctx
92 | v `hasKey` otherwiseIdKey
93 || v `hasKey` getUnique trueDataConId
94 -- trueDataConId doesn't have the same
95 -- unique as trueDataCon
96 = matchGuard stmts ctx
98 matchGuard (ExprStmt expr _ : stmts) ctx
99 = matchGuard stmts ctx `thenDs` \ match_result ->
100 dsLExpr expr `thenDs` \ pred_expr ->
101 returnDs (mkGuardedMatchResult pred_expr match_result)
103 matchGuard (LetStmt binds : stmts) ctx
104 = matchGuard stmts ctx `thenDs` \ match_result ->
105 returnDs (adjustMatchResultDs (dsLet binds) match_result)
106 -- NB the dsLet occurs inside the match_result
108 matchGuard (BindStmt pat rhs : stmts) ctx
109 = matchGuard stmts ctx `thenDs` \ match_result ->
110 dsLExpr rhs `thenDs` \ core_rhs ->
111 matchSinglePat core_rhs ctx pat match_result
114 Should {\em fail} if @e@ returns @D@
116 f x | p <- e', let C y# = e, f y# = r1