2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[MatchLit]{Pattern-matching literal patterns}
7 module MatchLit ( dsLit, matchLiterals ) where
9 #include "HsVersions.h"
11 import {-# SOURCE #-} Match ( match )
12 import {-# SOURCE #-} DsExpr ( dsExpr )
17 import HsSyn ( HsLit(..), Pat(..), HsExpr(..) )
18 import TcHsSyn ( TypecheckedPat )
21 import TyCon ( tyConDataCons )
22 import TcType ( tcSplitTyConApp, isIntegerTy )
23 import PrelNames ( ratioTyConKey )
24 import Unique ( hasKey )
25 import Literal ( mkMachInt, Literal(..) )
26 import Maybes ( catMaybes )
27 import Panic ( panic, assertPanic )
28 import Ratio ( numerator, denominator )
32 %************************************************************************
35 [used to be in DsExpr, but DsMeta needs it,
36 and it's nice to avoid a loop]
38 %************************************************************************
40 We give int/float literals type @Integer@ and @Rational@, respectively.
41 The typechecker will (presumably) have put \tr{from{Integer,Rational}s}
44 ToDo: put in range checks for when converting ``@i@''
45 (or should that be in the typechecker?)
47 For numeric literals, we try to detect there use at a standard type
48 (@Int@, @Float@, etc.) are directly put in the right constructor.
49 [NB: down with the @App@ conversion.]
51 See also below where we look for @DictApps@ for \tr{plusInt}, etc.
54 dsLit :: HsLit -> DsM CoreExpr
55 dsLit (HsChar c) = returnDs (mkCharExpr c)
56 dsLit (HsCharPrim c) = returnDs (mkLit (MachChar c))
57 dsLit (HsString str) = mkStringLitFS str
58 dsLit (HsStringPrim s) = returnDs (mkLit (MachStr s))
59 dsLit (HsInteger i _) = mkIntegerExpr i
60 dsLit (HsInt i) = returnDs (mkIntExpr i)
61 dsLit (HsIntPrim i) = returnDs (mkIntLit i)
62 dsLit (HsFloatPrim f) = returnDs (mkLit (MachFloat f))
63 dsLit (HsDoublePrim d) = returnDs (mkLit (MachDouble d))
66 = mkIntegerExpr (numerator r) `thenDs` \ num ->
67 mkIntegerExpr (denominator r) `thenDs` \ denom ->
68 returnDs (mkConApp ratio_data_con [Type integer_ty, num, denom])
70 (ratio_data_con, integer_ty)
71 = case tcSplitTyConApp ty of
72 (tycon, [i_ty]) -> ASSERT(isIntegerTy i_ty && tycon `hasKey` ratioTyConKey)
73 (head (tyConDataCons tycon), i_ty)
76 %************************************************************************
78 Pattern matching on literals
80 %************************************************************************
88 This first one is a {\em special case} where the literal patterns are
89 unboxed numbers (NB: the fiddling introduced by @tidyEqnInfo@). We
90 want to avoid using the ``equality'' stuff provided by the
91 typechecker, and do a real ``case'' instead. In that sense, the code
92 is much like @matchConFamily@, which uses @match_cons_used@ to create
93 the alts---here we use @match_prims_used@.
96 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (LitPat literal : ps1) _ : eqns)
97 = -- GENERATE THE ALTS
98 match_prims_used vars eqns_info `thenDs` \ prim_alts ->
100 -- MAKE THE PRIMITIVE CASE
101 returnDs (mkCoPrimCaseMatchResult var prim_alts)
103 match_prims_used _ [{-no more eqns-}] = returnDs []
105 match_prims_used vars eqns_info@(EqnInfo n ctx (pat@(LitPat literal):ps1) _ : eqns)
107 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
108 = partitionEqnsByLit pat eqns_info
110 -- recursive call to make other alts...
111 match_prims_used vars eqns_not_for_this_lit `thenDs` \ rest_of_alts ->
113 -- (prim pats have no args; no selectMatchVars as in match_cons_used)
114 -- now do the business to make the alt for _this_ LitPat ...
115 match vars shifted_eqns_for_this_lit `thenDs` \ match_result ->
117 (mk_core_lit literal, match_result)
121 mk_core_lit :: HsLit -> Literal
123 mk_core_lit (HsIntPrim i) = mkMachInt i
124 mk_core_lit (HsCharPrim c) = MachChar c
125 mk_core_lit (HsStringPrim s) = MachStr s
126 mk_core_lit (HsFloatPrim f) = MachFloat f
127 mk_core_lit (HsDoublePrim d) = MachDouble d
128 mk_core_lit other = panic "matchLiterals:mk_core_lit:unhandled"
132 matchLiterals all_vars@(var:vars)
133 eqns_info@(EqnInfo n ctx (pat@(NPatOut literal lit_ty eq_chk):ps1) _ : eqns)
135 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
136 = partitionEqnsByLit pat eqns_info
138 dsExpr (HsApp eq_chk (HsVar var)) `thenDs` \ pred_expr ->
139 match vars shifted_eqns_for_this_lit `thenDs` \ inner_match_result ->
141 match_result1 = mkGuardedMatchResult pred_expr inner_match_result
143 if (null eqns_not_for_this_lit)
145 returnDs match_result1
147 matchLiterals all_vars eqns_not_for_this_lit `thenDs` \ match_result2 ->
148 returnDs (combineMatchResults match_result1 match_result2)
151 For an n+k pattern, we use the various magic expressions we've been given.
156 in <expr-for-a-successful-match>
158 <try-next-pattern-or-whatever>
163 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (pat@(NPlusKPatOut master_n k ge sub):ps1) _ : eqns)
165 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
166 = partitionEqnsByLit pat eqns_info
168 match vars shifted_eqns_for_this_lit `thenDs` \ inner_match_result ->
170 dsExpr (HsApp ge (HsVar var)) `thenDs` \ ge_expr ->
171 dsExpr (HsApp sub (HsVar var)) `thenDs` \ nminusk_expr ->
174 match_result1 = mkGuardedMatchResult ge_expr $
175 mkCoLetsMatchResult [NonRec master_n nminusk_expr] $
178 if (null eqns_not_for_this_lit)
180 returnDs match_result1
182 matchLiterals all_vars eqns_not_for_this_lit `thenDs` \ match_result2 ->
183 returnDs (combineMatchResults match_result1 match_result2)
186 Given a blob of @LitPat@s/@NPat@s, we want to split them into those
187 that are ``same''/different as one we are looking at. We need to know
188 whether we're looking at a @LitPat@/@NPat@, and what literal we're after.
191 partitionEqnsByLit :: TypecheckedPat
193 -> ([EquationInfo], -- These ones are for this lit, AND
194 -- they've been "shifted" by stripping
195 -- off the first pattern
196 [EquationInfo] -- These are not for this lit; they
197 -- are exactly as fed in.
200 partitionEqnsByLit master_pat eqns
201 = ( \ (xs,ys) -> (catMaybes xs, catMaybes ys))
202 (unzip (map (partition_eqn master_pat) eqns))
204 partition_eqn :: TypecheckedPat -> EquationInfo -> (Maybe EquationInfo, Maybe EquationInfo)
206 partition_eqn (LitPat k1) (EqnInfo n ctx (LitPat k2 : remaining_pats) match_result)
207 | k1 == k2 = (Just (EqnInfo n ctx remaining_pats match_result), Nothing)
208 -- NB the pattern is stripped off the EquationInfo
210 partition_eqn (NPatOut k1 _ _) (EqnInfo n ctx (NPatOut k2 _ _ : remaining_pats) match_result)
211 | k1 == k2 = (Just (EqnInfo n ctx remaining_pats match_result), Nothing)
212 -- NB the pattern is stripped off the EquationInfo
214 partition_eqn (NPlusKPatOut master_n k1 _ _)
215 (EqnInfo n ctx (NPlusKPatOut n' k2 _ _ : remaining_pats) match_result)
216 | k1 == k2 = (Just (EqnInfo n ctx remaining_pats new_match_result), Nothing)
217 -- NB the pattern is stripped off the EquationInfo
219 new_match_result | master_n == n' = match_result
220 | otherwise = mkCoLetsMatchResult
221 [NonRec n' (Var master_n)] match_result
223 -- Wild-card patterns, which will only show up in the shadows,
224 -- go into both groups
225 partition_eqn master_pat eqn@(EqnInfo n ctx (WildPat _ : remaining_pats) match_result)
226 = (Just (EqnInfo n ctx remaining_pats match_result), Just eqn)
228 -- Default case; not for this pattern
229 partition_eqn master_pat eqn = (Nothing, Just eqn)