2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[MatchLit]{Pattern-matching literal and n+k patterns}
7 #include "HsVersions.h"
13 import AbsSyn -- the stuff being desugared
14 import PlainCore -- the output of desugaring;
15 -- importing this module also gets all the
16 -- CoreSyn utility functions
17 import DsMonad -- the monadery used in the desugarer
19 import AbsUniType ( isPrimType, getUniDataTyCon, kindFromType )
20 import BasicLit ( mkMachInt, BasicLit(..), PrimKind )
21 import DsExpr ( dsExpr )
23 import Maybes ( Maybe(..), catMaybes )
24 import Match ( match )
25 import Id ( getIdUniType, eqId )
32 -> [EquationInfo] -- Shadows
36 This first one is a {\em special case} where the literal patterns are
37 unboxed numbers (NB: the fiddling introduced by @tidyEqnInfo@). We
38 want to avoid using the ``equality'' stuff provided by the
39 typechecker, and do a real ``case'' instead. In that sense, the code
40 is much like @matchConFamily@, which uses @match_cons_used@ to create
41 the alts---here we use @match_prims_used@.
44 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo (LitPat literal lit_ty : ps1) _ : eqns) shadows
45 = -- GENERATE THE ALTS
46 match_prims_used vars eqns_info shadows `thenDs` \ prim_alts ->
48 -- MAKE THE PRIMITIVE CASE
49 mkCoPrimCaseMatchResult var prim_alts
51 match_prims_used _ [{-no more eqns-}] _ = returnDs []
53 match_prims_used vars eqns_info@(EqnInfo ((LitPat literal _):ps1) _ : eqns) shadows
55 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
56 = partitionEqnsByLit Nothing literal eqns_info
57 (shifted_shadows_for_this_lit, shadows_not_for_this_lit)
58 = partitionEqnsByLit Nothing literal shadows
60 -- recursive call to make other alts...
61 match_prims_used vars eqns_not_for_this_lit shadows_not_for_this_lit `thenDs` \ rest_of_alts ->
63 -- (prim pats have no args; no selectMatchVars as in match_cons_used)
64 -- now do the business to make the alt for _this_ LitPat ...
65 match vars shifted_eqns_for_this_lit shifted_shadows_for_this_lit `thenDs` \ match_result ->
67 (mk_core_lit literal, match_result)
71 mk_core_lit :: Literal -> BasicLit
73 mk_core_lit (IntPrimLit i) = mkMachInt i
74 mk_core_lit (CharPrimLit c) = MachChar c
75 mk_core_lit (StringPrimLit s) = MachStr s
76 mk_core_lit (FloatPrimLit f) = MachFloat f
77 mk_core_lit (DoublePrimLit d) = MachDouble d
78 mk_core_lit (LitLitLit s t) = ASSERT(isPrimType t)
79 MachLitLit s (kindFromType t)
80 mk_core_lit other = panic "matchLiterals:mk_core_lit:unhandled"
84 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo ((NPat literal lit_ty eq_chk):ps1) _ : eqns) shadows
86 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
87 = partitionEqnsByLit Nothing literal eqns_info
88 (shifted_shadows_for_this_lit, shadows_not_for_this_lit)
89 = partitionEqnsByLit Nothing literal shadows
91 dsExpr (App eq_chk (Var var)) `thenDs` \ pred_expr ->
92 match vars shifted_eqns_for_this_lit shifted_shadows_for_this_lit `thenDs` \ inner_match_result ->
93 mkGuardedMatchResult pred_expr inner_match_result `thenDs` \ match_result1 ->
95 if (null eqns_not_for_this_lit)
97 returnDs match_result1
99 matchLiterals all_vars eqns_not_for_this_lit shadows_not_for_this_lit `thenDs` \ match_result2 ->
100 combineMatchResults match_result1 match_result2
103 For an n+k pattern, we use the various magic expressions we've been given.
108 in <expr-for-a-successful-match>
110 <try-next-pattern-or-whatever>
114 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo ((NPlusKPat master_n k ty from_lit ge sub):ps1) _ : eqns) shadows
116 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
117 = partitionEqnsByLit (Just master_n) k eqns_info
118 (shifted_shadows_for_this_lit, shadows_not_for_this_lit)
119 = partitionEqnsByLit (Just master_n) k shadows
121 match vars shifted_eqns_for_this_lit shifted_shadows_for_this_lit `thenDs` \ inner_match_result ->
123 dsExpr from_lit `thenDs` \ core_lit ->
124 dsExpr (App ge (Var var)) `thenDs` \ var_ge ->
125 dsExpr (App sub (Var var)) `thenDs` \ var_sub ->
126 mkCoAppDs var_ge core_lit `thenDs` \ var_ge_lit ->
127 mkCoAppDs var_sub core_lit `thenDs` \ var_sub_lit ->
131 (mkCoLetsMatchResult [CoNonRec master_n var_sub_lit] inner_match_result)
132 `thenDs` \ match_result1 ->
134 if (null eqns_not_for_this_lit)
136 returnDs match_result1
138 matchLiterals all_vars eqns_not_for_this_lit shadows_not_for_this_lit `thenDs` \ match_result2 ->
139 combineMatchResults match_result1 match_result2
142 Given a blob of LitPats/NPats/NPlusKPats, we want to split them into those
143 that are ``same''/different as one we are looking at. We need to know
144 whether we're looking at a LitPat/NPat or NPlusKPat (initial Bool arg is
145 @True@ for the latter), and what literal we're after.
148 partitionEqnsByLit :: Maybe Id -- (Just v) for N-plus-K patterns, where v
149 -- is the "master" variable;
150 -- Nothing for NPats and LitPats
153 -> ([EquationInfo], -- These ones are for this lit, AND
154 -- they've been "shifted" by stripping
155 -- off the first pattern
156 [EquationInfo] -- These are not for this lit; they
157 -- are exactly as fed in.
160 partitionEqnsByLit want_NPlusK lit eqns
161 = ( \ (xs,ys) -> (catMaybes xs, catMaybes ys))
162 (unzip (map (partition_eqn want_NPlusK lit) eqns))
164 partition_eqn :: Maybe Id -> Literal -> EquationInfo ->
165 (Maybe EquationInfo, Maybe EquationInfo)
167 partition_eqn Nothing lit (EqnInfo (LitPat k _ : remaining_pats) match_result)
168 | lit `eq_lit` k = (Just (EqnInfo remaining_pats match_result), Nothing)
169 -- NB the pattern is stripped off thhe EquationInfo
171 partition_eqn Nothing lit (EqnInfo (NPat k _ _ : remaining_pats) match_result)
172 | lit `eq_lit` k = (Just (EqnInfo remaining_pats match_result), Nothing)
173 -- NB the pattern is stripped off thhe EquationInfo
175 partition_eqn (Just master_n) lit (EqnInfo (NPlusKPat n k _ _ _ _ : remaining_pats) match_result)
176 | lit `eq_lit` k = (Just (EqnInfo remaining_pats new_match_result), Nothing)
177 -- NB the pattern is stripped off thhe EquationInfo
179 new_match_result = if master_n `eqId` n then
182 mkCoLetsMatchResult [CoNonRec n (CoVar master_n)] match_result
184 -- Wild-card patterns, which will only show up in the shadows, go into both groups
185 partition_eqn wantNPlusK lit eqn@(EqnInfo (WildPat _ : remaining_pats) match_result)
186 = (Just (EqnInfo remaining_pats match_result), Just eqn)
188 -- Default case; not for this pattern
189 partition_eqn wantNPlusK lit eqn = (Nothing, Just eqn)
191 -- ToDo: meditate about this equality business...
193 eq_lit (IntLit i1) (IntLit i2) = i1 == i2
194 eq_lit (FracLit f1) (FracLit f2) = f1 == f2
196 eq_lit (IntPrimLit i1) (IntPrimLit i2) = i1 == i2
197 eq_lit (FloatPrimLit f1) (FloatPrimLit f2) = f1 == f2
198 eq_lit (DoublePrimLit d1) (DoublePrimLit d2) = d1 == d2
199 eq_lit (CharLit c1) (CharLit c2) = c1 == c2
200 eq_lit (CharPrimLit c1) (CharPrimLit c2) = c1 == c2
201 eq_lit (StringLit s1) (StringLit s2) = s1 == s2
202 eq_lit (StringPrimLit s1) (StringPrimLit s2) = s1 == s2
203 eq_lit (LitLitLit s1 _) (LitLitLit s2 _) = s1 == s2 -- ToDo: ??? (dubious)
204 eq_lit other1 other2 = panic "matchLiterals:eq_lit"