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 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
13 IMPORT_DELOOPER(DsLoop) -- break dsExpr/dsBinds-ish loop
15 import {-# SOURCE #-} DsExpr ( dsExpr )
16 import {-# SOURCE #-} DsBinds ( dsBinds )
17 import {-# SOURCE #-} Match ( match )
20 import HsSyn ( GRHSsAndBinds(..), GRHS(..),
21 HsExpr(..), HsBinds, Stmt(..),
22 HsLit, Match, Fixity, DoOrListComp, HsType, ArithSeqInfo
24 import TcHsSyn ( SYN_IE(TypecheckedGRHSsAndBinds), SYN_IE(TypecheckedGRHS),
25 SYN_IE(TypecheckedPat), SYN_IE(TypecheckedHsBinds),
26 SYN_IE(TypecheckedHsExpr), SYN_IE(TypecheckedStmt)
28 import CoreSyn ( SYN_IE(CoreBinding), GenCoreBinding(..), SYN_IE(CoreExpr), mkCoLetsAny )
33 #if __GLASGOW_HASKELL__ < 200
36 import CoreUtils ( coreExprType, mkCoreIfThenElse )
37 import PrelVals ( nON_EXHAUSTIVE_GUARDS_ERROR_ID )
38 import Outputable ( PprStyle(..) )
39 import SrcLoc ( SrcLoc{-instance-} )
40 import Type ( SYN_IE(Type) )
41 import Unique ( Unique, otherwiseIdKey )
42 import UniqFM ( Uniquable(..) )
46 @dsGuarded@ is used for both @case@ expressions and pattern bindings.
54 producing an expression with a runtime error in the corner if
55 necessary. The type argument gives the type of the ei.
58 dsGuarded :: TypecheckedGRHSsAndBinds
61 dsGuarded (GRHSsAndBindsOut grhss binds err_ty)
62 = dsBinds False{-don't auto scc-} binds `thenDs` \ core_binds ->
63 dsGRHSs err_ty PatBindMatch [] grhss `thenDs` \ (MatchResult can_it_fail _ core_grhss_fn _) ->
65 CantFail -> returnDs (mkCoLetsAny core_binds (core_grhss_fn (panic "It can't fail")))
66 CanFail -> mkErrorAppDs nON_EXHAUSTIVE_GUARDS_ERROR_ID err_ty "" `thenDs` \ error_expr ->
67 returnDs (mkCoLetsAny core_binds (core_grhss_fn error_expr))
70 Desugar a list of (grhs, expr) pairs [grhs = guarded
71 right-hand-side], as in:
78 We supply a @CoreExpr@ for the case in which all of
82 dsGRHSs :: Type -- Type of RHSs
83 -> DsMatchKind -> [TypecheckedPat] -- These are to build a MatchContext from
84 -> [TypecheckedGRHS] -- Guarded RHSs
87 dsGRHSs ty kind pats [grhs] = dsGRHS ty kind pats grhs
89 dsGRHSs ty kind pats (grhs:grhss)
90 = dsGRHS ty kind pats grhs `thenDs` \ match_result1 ->
91 dsGRHSs ty kind pats grhss `thenDs` \ match_result2 ->
92 combineGRHSMatchResults match_result1 match_result2
94 dsGRHS ty kind pats (OtherwiseGRHS expr locn)
96 dsExpr expr `thenDs` \ core_expr ->
98 expr_fn = \ ignore -> core_expr
100 returnDs (MatchResult CantFail ty expr_fn (DsMatchContext kind pats locn))
102 dsGRHS ty kind pats (GRHS guard expr locn)
104 dsExpr expr `thenDs` \ core_expr ->
106 expr_fn = \ ignore -> core_expr
108 matchGuard guard (MatchResult CantFail ty expr_fn (DsMatchContext kind pats locn))
114 %************************************************************************
116 %* matchGuard : make a MatchResult from a guarded RHS *
118 %************************************************************************
121 matchGuard :: [TypecheckedStmt] -- Guard
122 -> MatchResult -- What to do if the guard succeeds
125 matchGuard [] body_result = returnDs body_result
127 -- Turn an "otherwise" guard is a no-op
128 matchGuard (GuardStmt (HsVar v) _ : stmts) body_result
129 | uniqueOf v == otherwiseIdKey
130 = matchGuard stmts body_result
132 matchGuard (GuardStmt expr _ : stmts) body_result
133 = matchGuard stmts body_result `thenDs` \ (MatchResult _ ty body_fn cxt) ->
134 dsExpr expr `thenDs` \ core_expr ->
136 expr_fn = \ fail -> mkCoreIfThenElse core_expr (body_fn fail) fail
138 returnDs (MatchResult CanFail ty expr_fn cxt)
140 matchGuard (LetStmt binds : stmts) body_result
141 = matchGuard stmts body_result `thenDs` \ match_result ->
142 dsBinds False{-don't auto scc-} binds `thenDs` \ core_binds ->
143 returnDs (mkCoLetsMatchResult core_binds match_result)
145 matchGuard (BindStmt pat rhs _ : stmts) body_result
146 = matchGuard stmts body_result `thenDs` \ match_result ->
147 dsExpr rhs `thenDs` \ core_rhs ->
148 newSysLocalDs (coreExprType core_rhs) `thenDs` \ scrut_var ->
149 match [scrut_var] [EqnInfo [pat] match_result] [] `thenDs` \ match_result' ->
150 returnDs (mkCoLetsMatchResult [NonRec scrut_var core_rhs] match_result')