2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[MatchLit]{Pattern-matching literal patterns}
7 #include "HsVersions.h"
9 module MatchLit ( matchLiterals ) where
12 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
13 IMPORT_DELOOPER(DsLoop) -- break match-ish and dsExpr-ish loops
15 import {-# SOURCE #-} Match
16 import {-# SOURCE #-} DsExpr ( dsExpr )
19 import HsSyn ( HsLit(..), OutPat(..), HsExpr(..), Fixity,
20 Match, HsBinds, Stmt(..), DoOrListComp, HsType, ArithSeqInfo )
21 import TcHsSyn ( SYN_IE(TypecheckedHsExpr), SYN_IE(TypecheckedHsBinds),
22 SYN_IE(TypecheckedPat)
24 import CoreSyn ( SYN_IE(CoreExpr), SYN_IE(CoreBinding), GenCoreExpr(..), GenCoreBinding(..) )
25 import Id ( GenId {- instance Eq -}, SYN_IE(Id) )
30 import Literal ( mkMachInt, Literal(..) )
31 import Maybes ( catMaybes )
32 import Type ( isPrimType, SYN_IE(Type) )
33 import Util ( panic, assertPanic )
42 This first one is a {\em special case} where the literal patterns are
43 unboxed numbers (NB: the fiddling introduced by @tidyEqnInfo@). We
44 want to avoid using the ``equality'' stuff provided by the
45 typechecker, and do a real ``case'' instead. In that sense, the code
46 is much like @matchConFamily@, which uses @match_cons_used@ to create
47 the alts---here we use @match_prims_used@.
50 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (LitPat literal lit_ty : ps1) _ : eqns)
51 = -- GENERATE THE ALTS
52 match_prims_used vars eqns_info `thenDs` \ prim_alts ->
54 -- MAKE THE PRIMITIVE CASE
55 mkCoPrimCaseMatchResult var prim_alts
57 match_prims_used _ [{-no more eqns-}] = returnDs []
59 match_prims_used vars eqns_info@(EqnInfo n ctx ((LitPat literal lit_ty):ps1) _ : eqns)
61 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
62 = partitionEqnsByLit Nothing literal eqns_info
64 -- recursive call to make other alts...
65 match_prims_used vars eqns_not_for_this_lit `thenDs` \ rest_of_alts ->
67 -- (prim pats have no args; no selectMatchVars as in match_cons_used)
68 -- now do the business to make the alt for _this_ LitPat ...
69 match vars shifted_eqns_for_this_lit `thenDs` \ match_result ->
71 (mk_core_lit lit_ty literal, match_result)
75 mk_core_lit :: Type -> HsLit -> Literal
77 mk_core_lit ty (HsIntPrim i) = mkMachInt i
78 mk_core_lit ty (HsCharPrim c) = MachChar c
79 mk_core_lit ty (HsStringPrim s) = MachStr s
80 mk_core_lit ty (HsFloatPrim f) = MachFloat f
81 mk_core_lit ty (HsDoublePrim d) = MachDouble d
82 mk_core_lit ty (HsLitLit s) = ASSERT(isPrimType ty)
83 MachLitLit s (panic "MatchLit.matchLiterals:mk_core_lit:HsLitLit; typePrimRep???")
84 mk_core_lit ty other = panic "matchLiterals:mk_core_lit:unhandled"
88 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx ((NPat literal lit_ty eq_chk):ps1) _ : eqns)
90 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
91 = partitionEqnsByLit Nothing literal eqns_info
93 dsExpr (HsApp eq_chk (HsVar var)) `thenDs` \ pred_expr ->
94 match vars shifted_eqns_for_this_lit `thenDs` \ inner_match_result ->
95 mkGuardedMatchResult pred_expr inner_match_result `thenDs` \ match_result1 ->
97 if (null eqns_not_for_this_lit)
99 returnDs match_result1
101 matchLiterals all_vars eqns_not_for_this_lit `thenDs` \ match_result2 ->
102 combineMatchResults match_result1 match_result2
105 For an n+k pattern, we use the various magic expressions we've been given.
110 in <expr-for-a-successful-match>
112 <try-next-pattern-or-whatever>
117 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx ((NPlusKPat master_n k ty ge sub):ps1) _ : eqns)
119 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
120 = partitionEqnsByLit (Just master_n) k eqns_info
122 match vars shifted_eqns_for_this_lit `thenDs` \ inner_match_result ->
124 dsExpr (HsApp ge (HsVar var)) `thenDs` \ ge_expr ->
125 dsExpr (HsApp sub (HsVar var)) `thenDs` \ nminusk_expr ->
129 (mkCoLetsMatchResult [NonRec master_n nminusk_expr] inner_match_result)
130 `thenDs` \ match_result1 ->
132 if (null eqns_not_for_this_lit)
134 returnDs match_result1
136 matchLiterals all_vars eqns_not_for_this_lit `thenDs` \ match_result2 ->
137 combineMatchResults match_result1 match_result2
140 Given a blob of LitPats/NPats, we want to split them into those
141 that are ``same''/different as one we are looking at. We need to know
142 whether we're looking at a LitPat/NPat, and what literal we're after.
145 partitionEqnsByLit :: Maybe Id -- (Just v) for N-plus-K patterns, where v
146 -- is the "master" variable;
147 -- Nothing for NPats and LitPats
150 -> ([EquationInfo], -- These ones are for this lit, AND
151 -- they've been "shifted" by stripping
152 -- off the first pattern
153 [EquationInfo] -- These are not for this lit; they
154 -- are exactly as fed in.
157 partitionEqnsByLit nPlusK lit eqns
158 = ( \ (xs,ys) -> (catMaybes xs, catMaybes ys))
159 (unzip (map (partition_eqn nPlusK lit) eqns))
161 partition_eqn :: Maybe Id -> HsLit -> EquationInfo ->
162 (Maybe EquationInfo, Maybe EquationInfo)
164 partition_eqn Nothing lit (EqnInfo n ctx (LitPat k _ : remaining_pats) match_result)
165 | lit `eq_lit` k = (Just (EqnInfo n ctx remaining_pats match_result), Nothing)
166 -- NB the pattern is stripped off the EquationInfo
168 partition_eqn Nothing lit (EqnInfo n ctx (NPat k _ _ : remaining_pats) match_result)
169 | lit `eq_lit` k = (Just (EqnInfo n ctx remaining_pats match_result), Nothing)
170 -- NB the pattern is stripped off the EquationInfo
172 partition_eqn (Just master_n) lit (EqnInfo n ctx (NPlusKPat n' k _ _ _ : remaining_pats) match_result)
173 | lit `eq_lit` k = (Just (EqnInfo n ctx remaining_pats new_match_result), Nothing)
174 -- NB the pattern is stripped off the EquationInfo
176 new_match_result | master_n == n' = match_result
177 | otherwise = mkCoLetsMatchResult [NonRec n' (Var master_n)] match_result
179 -- Wild-card patterns, which will only show up in the shadows, go into both groups
180 partition_eqn nPlusK lit eqn@(EqnInfo n ctx (WildPat _ : remaining_pats) match_result)
181 = (Just (EqnInfo n ctx remaining_pats match_result), Just eqn)
183 -- Default case; not for this pattern
184 partition_eqn nPlusK lit eqn = (Nothing, Just eqn)
186 -- ToDo: meditate about this equality business...
188 eq_lit (HsInt i1) (HsInt i2) = i1 == i2
189 eq_lit (HsFrac f1) (HsFrac f2) = f1 == f2
191 eq_lit (HsIntPrim i1) (HsIntPrim i2) = i1 == i2
192 eq_lit (HsFloatPrim f1) (HsFloatPrim f2) = f1 == f2
193 eq_lit (HsDoublePrim d1) (HsDoublePrim d2) = d1 == d2
194 eq_lit (HsChar c1) (HsChar c2) = c1 == c2
195 eq_lit (HsCharPrim c1) (HsCharPrim c2) = c1 == c2
196 eq_lit (HsString s1) (HsString s2) = s1 == s2
197 eq_lit (HsStringPrim s1) (HsStringPrim s2) = s1 == s2
198 eq_lit (HsLitLit s1) (HsLitLit s2) = s1 == s2 -- ToDo: ??? (dubious)
199 eq_lit other1 other2 = panic "matchLiterals:eq_lit"