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, Uniquable(..) )
45 @dsGuarded@ is used for both @case@ expressions and pattern bindings.
53 producing an expression with a runtime error in the corner if
54 necessary. The type argument gives the type of the ei.
57 dsGuarded :: TypecheckedGRHSsAndBinds
60 dsGuarded (GRHSsAndBindsOut grhss binds err_ty)
61 = dsBinds False{-don't auto scc-} binds `thenDs` \ core_binds ->
62 dsGRHSs err_ty PatBindMatch [] grhss `thenDs` \ (MatchResult can_it_fail _ core_grhss_fn _) ->
64 CantFail -> returnDs (mkCoLetsAny core_binds (core_grhss_fn (panic "It can't fail")))
65 CanFail -> mkErrorAppDs nON_EXHAUSTIVE_GUARDS_ERROR_ID err_ty "" `thenDs` \ error_expr ->
66 returnDs (mkCoLetsAny core_binds (core_grhss_fn error_expr))
69 Desugar a list of (grhs, expr) pairs [grhs = guarded
70 right-hand-side], as in:
77 We supply a @CoreExpr@ for the case in which all of
81 dsGRHSs :: Type -- Type of RHSs
82 -> DsMatchKind -> [TypecheckedPat] -- These are to build a MatchContext from
83 -> [TypecheckedGRHS] -- Guarded RHSs
86 dsGRHSs ty kind pats [grhs] = dsGRHS ty kind pats grhs
88 dsGRHSs ty kind pats (grhs:grhss)
89 = dsGRHS ty kind pats grhs `thenDs` \ match_result1 ->
90 dsGRHSs ty kind pats grhss `thenDs` \ match_result2 ->
91 combineGRHSMatchResults match_result1 match_result2
93 dsGRHS ty kind pats (OtherwiseGRHS expr locn)
95 dsExpr expr `thenDs` \ core_expr ->
97 expr_fn = \ ignore -> core_expr
99 returnDs (MatchResult CantFail ty expr_fn (DsMatchContext kind pats locn))
101 dsGRHS ty kind pats (GRHS guard expr locn)
103 dsExpr expr `thenDs` \ core_expr ->
105 expr_fn = \ ignore -> core_expr
107 matchGuard guard (MatchResult CantFail ty expr_fn (DsMatchContext kind pats locn))
113 %************************************************************************
115 %* matchGuard : make a MatchResult from a guarded RHS *
117 %************************************************************************
120 matchGuard :: [TypecheckedStmt] -- Guard
121 -> MatchResult -- What to do if the guard succeeds
124 matchGuard [] body_result = returnDs body_result
126 -- Turn an "otherwise" guard is a no-op
127 matchGuard (GuardStmt (HsVar v) _ : stmts) body_result
128 | uniqueOf v == otherwiseIdKey
129 = matchGuard stmts body_result
131 matchGuard (GuardStmt expr _ : stmts) body_result
132 = matchGuard stmts body_result `thenDs` \ (MatchResult _ ty body_fn cxt) ->
133 dsExpr expr `thenDs` \ core_expr ->
135 expr_fn = \ fail -> mkCoreIfThenElse core_expr (body_fn fail) fail
137 returnDs (MatchResult CanFail ty expr_fn cxt)
139 matchGuard (LetStmt binds : stmts) body_result
140 = matchGuard stmts body_result `thenDs` \ match_result ->
141 dsBinds False{-don't auto scc-} binds `thenDs` \ core_binds ->
142 returnDs (mkCoLetsMatchResult core_binds match_result)
144 matchGuard (BindStmt pat rhs _ : stmts) body_result
145 = matchGuard stmts body_result `thenDs` \ match_result ->
146 dsExpr rhs `thenDs` \ core_rhs ->
147 newSysLocalDs (coreExprType core_rhs) `thenDs` \ scrut_var ->
148 match [scrut_var] [EqnInfo [pat] match_result] [] `thenDs` \ match_result' ->
149 returnDs (mkCoLetsMatchResult [NonRec scrut_var core_rhs] match_result')