2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1994
4 \section[DsGRHSs]{Matching guarded right-hand-sides (GRHSs)}
7 #include "HsVersions.h"
9 module DsGRHSs ( dsGuarded, dsGRHSs ) where
12 import DsLoop -- break dsExpr/dsBinds-ish loop
14 import HsSyn ( GRHSsAndBinds(..), GRHS(..),
16 import TcHsSyn ( TypecheckedGRHSsAndBinds(..), TypecheckedGRHS(..),
17 TypecheckedPat(..), TypecheckedHsBinds(..),
18 TypecheckedHsExpr(..) )
19 import CoreSyn ( CoreBinding(..), CoreExpr(..), mkCoLetsAny )
24 import CoreUtils ( mkCoreIfThenElse )
25 import PrelInfo ( stringTy, nON_EXHAUSTIVE_GUARDS_ERROR_ID )
26 import PprStyle ( PprStyle(..) )
27 import Pretty ( ppShow )
28 import SrcLoc ( SrcLoc{-instance-} )
32 @dsGuarded@ is used for both @case@ expressions and pattern bindings.
40 producing an expression with a runtime error in the corner if
41 necessary. The type argument gives the type of the ei.
44 dsGuarded :: TypecheckedGRHSsAndBinds
47 dsGuarded (GRHSsAndBindsOut grhss binds err_ty)
48 = dsBinds binds `thenDs` \ core_binds ->
49 dsGRHSs err_ty PatBindMatch [] grhss `thenDs` \ (MatchResult can_it_fail _ core_grhss_fn _) ->
51 CantFail -> returnDs (mkCoLetsAny core_binds (core_grhss_fn (panic "It can't fail")))
52 CanFail -> mkErrorAppDs nON_EXHAUSTIVE_GUARDS_ERROR_ID err_ty "" `thenDs` \ error_expr ->
53 returnDs (mkCoLetsAny core_binds (core_grhss_fn error_expr))
56 Desugar a list of (grhs, expr) pairs [grhs = guarded
57 right-hand-side], as in:
64 We supply a @CoreExpr@ for the case in which all of
68 dsGRHSs :: Type -- Type of RHSs
69 -> DsMatchKind -> [TypecheckedPat] -- These are to build a MatchContext from
70 -> [TypecheckedGRHS] -- Guarded RHSs
73 dsGRHSs ty kind pats [grhs] = dsGRHS ty kind pats grhs
75 dsGRHSs ty kind pats (grhs:grhss)
76 = dsGRHS ty kind pats grhs `thenDs` \ match_result1 ->
77 dsGRHSs ty kind pats grhss `thenDs` \ match_result2 ->
78 combineGRHSMatchResults match_result1 match_result2
80 dsGRHS ty kind pats (OtherwiseGRHS expr locn)
82 dsExpr expr `thenDs` \ core_expr ->
84 expr_fn = \ ignore -> core_expr
86 returnDs (MatchResult CantFail ty expr_fn (DsMatchContext kind pats locn))
89 dsGRHS ty kind pats (GRHS guard expr locn)
91 dsExpr guard `thenDs` \ core_guard ->
92 dsExpr expr `thenDs` \ core_expr ->
94 expr_fn = \ fail -> mkCoreIfThenElse core_guard core_expr fail
96 returnDs (MatchResult CanFail ty expr_fn (DsMatchContext kind pats locn))