2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 Pattern-matching literal patterns
9 module MatchLit ( dsLit, dsOverLit, hsLitKey, hsOverLitKey,
11 matchLiterals, matchNPlusKPats, matchNPats ) where
13 #include "HsVersions.h"
15 import {-# SOURCE #-} Match ( match )
16 import {-# SOURCE #-} DsExpr ( dsExpr )
39 %************************************************************************
42 [used to be in DsExpr, but DsMeta needs it,
43 and it's nice to avoid a loop]
45 %************************************************************************
47 We give int/float literals type @Integer@ and @Rational@, respectively.
48 The typechecker will (presumably) have put \tr{from{Integer,Rational}s}
51 ToDo: put in range checks for when converting ``@i@''
52 (or should that be in the typechecker?)
54 For numeric literals, we try to detect there use at a standard type
55 (@Int@, @Float@, etc.) are directly put in the right constructor.
56 [NB: down with the @App@ conversion.]
58 See also below where we look for @DictApps@ for \tr{plusInt}, etc.
61 dsLit :: HsLit -> DsM CoreExpr
62 dsLit (HsStringPrim s) = returnDs (mkLit (MachStr s))
63 dsLit (HsCharPrim c) = returnDs (mkLit (MachChar c))
64 dsLit (HsIntPrim i) = returnDs (mkLit (MachInt i))
65 dsLit (HsFloatPrim f) = returnDs (mkLit (MachFloat f))
66 dsLit (HsDoublePrim d) = returnDs (mkLit (MachDouble d))
68 dsLit (HsChar c) = returnDs (mkCharExpr c)
69 dsLit (HsString str) = mkStringExprFS str
70 dsLit (HsInteger i _) = mkIntegerExpr i
71 dsLit (HsInt i) = returnDs (mkIntExpr i)
74 = mkIntegerExpr (numerator r) `thenDs` \ num ->
75 mkIntegerExpr (denominator r) `thenDs` \ denom ->
76 returnDs (mkConApp ratio_data_con [Type integer_ty, num, denom])
78 (ratio_data_con, integer_ty)
79 = case tcSplitTyConApp ty of
80 (tycon, [i_ty]) -> ASSERT(isIntegerTy i_ty && tycon `hasKey` ratioTyConKey)
81 (head (tyConDataCons tycon), i_ty)
83 dsOverLit :: HsOverLit Id -> DsM CoreExpr
84 -- Post-typechecker, the SyntaxExpr field of an OverLit contains
85 -- (an expression for) the literal value itself
86 dsOverLit (HsIntegral _ lit) = dsExpr lit
87 dsOverLit (HsFractional _ lit) = dsExpr lit
88 dsOverLit (HsIsString _ lit) = dsExpr lit
92 hsLitKey :: HsLit -> Literal
93 -- Get a Core literal to use (only) a grouping key
94 -- Hence its type doesn't need to match the type of the original literal
95 -- (and doesn't for strings)
96 -- It only works for primitive types and strings;
97 -- others have been removed by tidy
98 hsLitKey (HsIntPrim i) = mkMachInt i
99 hsLitKey (HsCharPrim c) = MachChar c
100 hsLitKey (HsStringPrim s) = MachStr s
101 hsLitKey (HsFloatPrim f) = MachFloat f
102 hsLitKey (HsDoublePrim d) = MachDouble d
103 hsLitKey (HsString s) = MachStr s
105 hsOverLitKey :: HsOverLit a -> Bool -> Literal
106 -- Ditto for HsOverLit; the boolean indicates to negate
107 hsOverLitKey (HsIntegral i _) False = MachInt i
108 hsOverLitKey (HsIntegral i _) True = MachInt (-i)
109 hsOverLitKey (HsFractional r _) False = MachFloat r
110 hsOverLitKey (HsFractional r _) True = MachFloat (-r)
111 hsOverLitKey (HsIsString s _) False = MachStr s
112 -- negated string should never happen
115 %************************************************************************
119 %************************************************************************
122 tidyLitPat :: HsLit -> Pat Id
123 -- Result has only the following HsLits:
124 -- HsIntPrim, HsCharPrim, HsFloatPrim
125 -- HsDoublePrim, HsStringPrim, HsString
126 -- * HsInteger, HsRat, HsInt can't show up in LitPats
127 -- * We get rid of HsChar right here
128 tidyLitPat (HsChar c) = unLoc (mkCharLitPat c)
129 tidyLitPat (HsString s)
130 | lengthFS s <= 1 -- Short string literals only
131 = unLoc $ foldr (\c pat -> mkPrefixConPat consDataCon [mkCharLitPat c, pat] stringTy)
132 (mkNilPat stringTy) (unpackFS s)
133 -- The stringTy is the type of the whole pattern, not
134 -- the type to instantiate (:) or [] with!
135 tidyLitPat lit = LitPat lit
138 tidyNPat :: HsOverLit Id -> Maybe (SyntaxExpr Id) -> SyntaxExpr Id
140 tidyNPat over_lit mb_neg eq lit_ty
141 | isIntTy lit_ty = mk_con_pat intDataCon (HsIntPrim int_val)
142 | isFloatTy lit_ty = mk_con_pat floatDataCon (HsFloatPrim rat_val)
143 | isDoubleTy lit_ty = mk_con_pat doubleDataCon (HsDoublePrim rat_val)
144 -- | isStringTy lit_ty = mk_con_pat stringDataCon (HsStringPrim str_val)
145 | otherwise = NPat over_lit mb_neg eq lit_ty
147 mk_con_pat :: DataCon -> HsLit -> Pat Id
148 mk_con_pat con lit = unLoc (mkPrefixConPat con [noLoc $ LitPat lit] lit_ty)
149 neg_lit = case (mb_neg, over_lit) of
150 (Nothing, _) -> over_lit
151 (Just _, HsIntegral i s) -> HsIntegral (-i) s
152 (Just _, HsFractional f s) -> HsFractional (-f) s
155 int_val = case neg_lit of
157 HsFractional f _ -> panic "tidyNPat"
160 rat_val = case neg_lit of
161 HsIntegral i _ -> fromInteger i
162 HsFractional f _ -> f
164 str_val :: FastString
165 str_val = case neg_lit of
167 _ -> error "tidyNPat"
171 %************************************************************************
173 Pattern matching on LitPat
175 %************************************************************************
178 matchLiterals :: [Id]
179 -> Type -- Type of the whole case expression
180 -> [[EquationInfo]] -- All PgLits
183 matchLiterals (var:vars) ty sub_groups
184 = ASSERT( all notNull sub_groups )
185 do { -- Deal with each group
186 ; alts <- mapM match_group sub_groups
188 -- Combine results. For everything except String
189 -- we can use a case expression; for String we need
190 -- a chain of if-then-else
191 ; if isStringTy (idType var) then
192 do { eq_str <- dsLookupGlobalId eqStringName
193 ; mrs <- mapM (wrap_str_guard eq_str) alts
194 ; return (foldr1 combineMatchResults mrs) }
196 return (mkCoPrimCaseMatchResult var ty alts)
199 match_group :: [EquationInfo] -> DsM (Literal, MatchResult)
201 = do { let LitPat hs_lit = firstPat (head eqns)
202 ; match_result <- match vars ty (shiftEqns eqns)
203 ; return (hsLitKey hs_lit, match_result) }
205 wrap_str_guard :: Id -> (Literal,MatchResult) -> DsM MatchResult
206 -- Equality check for string literals
207 wrap_str_guard eq_str (MachStr s, mr)
208 = do { lit <- mkStringExprFS s
209 ; let pred = mkApps (Var eq_str) [Var var, lit]
210 ; return (mkGuardedMatchResult pred mr) }
214 %************************************************************************
216 Pattern matching on NPat
218 %************************************************************************
221 matchNPats :: [Id] -> Type -> [[EquationInfo]] -> DsM MatchResult
222 -- All NPats, but perhaps for different literals
223 matchNPats vars ty groups
224 = do { match_results <- mapM (matchOneNPat vars ty) groups
225 ; return (foldr1 combineMatchResults match_results) }
227 matchOneNPat (var:vars) ty (eqn1:eqns) -- All for the same literal
228 = do { let NPat lit mb_neg eq_chk _ = firstPat eqn1
229 ; lit_expr <- dsOverLit lit
230 ; neg_lit <- case mb_neg of
231 Nothing -> return lit_expr
232 Just neg -> do { neg_expr <- dsExpr neg
233 ; return (App neg_expr lit_expr) }
234 ; eq_expr <- dsExpr eq_chk
235 ; let pred_expr = mkApps eq_expr [Var var, neg_lit]
236 ; match_result <- match vars ty (shiftEqns (eqn1:eqns))
237 ; return (mkGuardedMatchResult pred_expr match_result) }
241 %************************************************************************
243 Pattern matching on n+k patterns
245 %************************************************************************
247 For an n+k pattern, we use the various magic expressions we've been given.
252 in <expr-for-a-successful-match>
254 <try-next-pattern-or-whatever>
259 matchNPlusKPats :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
260 -- All NPlusKPats, for the *same* literal k
261 matchNPlusKPats all_vars@(var:vars) ty (eqn1:eqns)
262 = do { let NPlusKPat (L _ n1) lit ge minus = firstPat eqn1
263 ; ge_expr <- dsExpr ge
264 ; minus_expr <- dsExpr minus
265 ; lit_expr <- dsOverLit lit
266 ; let pred_expr = mkApps ge_expr [Var var, lit_expr]
267 minusk_expr = mkApps minus_expr [Var var, lit_expr]
268 (wraps, eqns') = mapAndUnzip (shift n1) (eqn1:eqns)
269 ; match_result <- match vars ty eqns'
270 ; return (mkGuardedMatchResult pred_expr $
271 mkCoLetMatchResult (NonRec n1 minusk_expr) $
272 adjustMatchResult (foldr1 (.) wraps) $
275 shift n1 eqn@(EqnInfo { eqn_pats = NPlusKPat (L _ n) _ _ _ : pats })
276 = (wrapBind n n1, eqn { eqn_pats = pats })
277 -- The wrapBind is a no-op for the first equation