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 )
15 import DsCCall ( resultWrapper )
18 import HsSyn ( HsLit(..), Pat(..), HsExpr(..) )
19 import TcHsSyn ( TypecheckedPat )
22 import TyCon ( tyConDataCons )
23 import TcType ( tcSplitTyConApp, isIntegerTy )
25 import PrelNames ( ratioTyConKey )
26 import Unique ( hasKey )
27 import Literal ( mkMachInt, Literal(..) )
28 import Maybes ( catMaybes )
29 import Type ( isUnLiftedType )
30 import Panic ( panic, assertPanic )
31 import Maybe ( isJust )
32 import Ratio ( numerator, denominator )
35 %************************************************************************
38 [used to be in DsExpr, but DsMeta needs it,
39 and it's nice to avoid a loop]
41 %************************************************************************
43 We give int/float literals type @Integer@ and @Rational@, respectively.
44 The typechecker will (presumably) have put \tr{from{Integer,Rational}s}
47 ToDo: put in range checks for when converting ``@i@''
48 (or should that be in the typechecker?)
50 For numeric literals, we try to detect there use at a standard type
51 (@Int@, @Float@, etc.) are directly put in the right constructor.
52 [NB: down with the @App@ conversion.]
54 See also below where we look for @DictApps@ for \tr{plusInt}, etc.
57 dsLit :: HsLit -> DsM CoreExpr
58 dsLit (HsChar c) = returnDs (mkCharExpr c)
59 dsLit (HsCharPrim c) = returnDs (mkLit (MachChar c))
60 dsLit (HsString str) = mkStringLitFS str
61 dsLit (HsStringPrim s) = returnDs (mkLit (MachStr s))
62 dsLit (HsInteger i) = mkIntegerExpr i
63 dsLit (HsInt i) = returnDs (mkIntExpr i)
64 dsLit (HsIntPrim i) = returnDs (mkIntLit i)
65 dsLit (HsFloatPrim f) = returnDs (mkLit (MachFloat f))
66 dsLit (HsDoublePrim d) = returnDs (mkLit (MachDouble d))
67 dsLit (HsLitLit str ty)
68 = ASSERT( isJust maybe_ty )
69 returnDs (wrap_fn (mkLit (MachLitLit str rep_ty)))
71 (maybe_ty, wrap_fn) = resultWrapper ty
72 Just rep_ty = maybe_ty
75 = mkIntegerExpr (numerator r) `thenDs` \ num ->
76 mkIntegerExpr (denominator r) `thenDs` \ denom ->
77 returnDs (mkConApp ratio_data_con [Type integer_ty, num, denom])
79 (ratio_data_con, integer_ty)
80 = case tcSplitTyConApp ty of
81 (tycon, [i_ty]) -> ASSERT(isIntegerTy i_ty && tycon `hasKey` ratioTyConKey)
82 (head (tyConDataCons tycon), i_ty)
85 %************************************************************************
87 Pattern matching on literals
89 %************************************************************************
97 This first one is a {\em special case} where the literal patterns are
98 unboxed numbers (NB: the fiddling introduced by @tidyEqnInfo@). We
99 want to avoid using the ``equality'' stuff provided by the
100 typechecker, and do a real ``case'' instead. In that sense, the code
101 is much like @matchConFamily@, which uses @match_cons_used@ to create
102 the alts---here we use @match_prims_used@.
105 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (LitPat literal : ps1) _ : eqns)
106 = -- GENERATE THE ALTS
107 match_prims_used vars eqns_info `thenDs` \ prim_alts ->
109 -- MAKE THE PRIMITIVE CASE
110 returnDs (mkCoPrimCaseMatchResult var prim_alts)
112 match_prims_used _ [{-no more eqns-}] = returnDs []
114 match_prims_used vars eqns_info@(EqnInfo n ctx (pat@(LitPat literal):ps1) _ : eqns)
116 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
117 = partitionEqnsByLit pat eqns_info
119 -- recursive call to make other alts...
120 match_prims_used vars eqns_not_for_this_lit `thenDs` \ rest_of_alts ->
122 -- (prim pats have no args; no selectMatchVars as in match_cons_used)
123 -- now do the business to make the alt for _this_ LitPat ...
124 match vars shifted_eqns_for_this_lit `thenDs` \ match_result ->
126 (mk_core_lit literal, match_result)
130 mk_core_lit :: HsLit -> Literal
132 mk_core_lit (HsIntPrim i) = mkMachInt i
133 mk_core_lit (HsCharPrim c) = MachChar c
134 mk_core_lit (HsStringPrim s) = MachStr s
135 mk_core_lit (HsFloatPrim f) = MachFloat f
136 mk_core_lit (HsDoublePrim d) = MachDouble d
137 mk_core_lit (HsLitLit s ty) = ASSERT(isUnLiftedType ty)
139 mk_core_lit other = panic "matchLiterals:mk_core_lit:unhandled"
143 matchLiterals all_vars@(var:vars)
144 eqns_info@(EqnInfo n ctx (pat@(NPatOut literal lit_ty eq_chk):ps1) _ : eqns)
146 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
147 = partitionEqnsByLit pat eqns_info
149 dsExpr (HsApp eq_chk (HsVar var)) `thenDs` \ pred_expr ->
150 match vars shifted_eqns_for_this_lit `thenDs` \ inner_match_result ->
152 match_result1 = mkGuardedMatchResult pred_expr inner_match_result
154 if (null eqns_not_for_this_lit)
156 returnDs match_result1
158 matchLiterals all_vars eqns_not_for_this_lit `thenDs` \ match_result2 ->
159 returnDs (combineMatchResults match_result1 match_result2)
162 For an n+k pattern, we use the various magic expressions we've been given.
167 in <expr-for-a-successful-match>
169 <try-next-pattern-or-whatever>
174 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (pat@(NPlusKPatOut master_n k ge sub):ps1) _ : eqns)
176 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
177 = partitionEqnsByLit pat eqns_info
179 match vars shifted_eqns_for_this_lit `thenDs` \ inner_match_result ->
181 dsExpr (HsApp ge (HsVar var)) `thenDs` \ ge_expr ->
182 dsExpr (HsApp sub (HsVar var)) `thenDs` \ nminusk_expr ->
185 match_result1 = mkGuardedMatchResult ge_expr $
186 mkCoLetsMatchResult [NonRec master_n nminusk_expr] $
189 if (null eqns_not_for_this_lit)
191 returnDs match_result1
193 matchLiterals all_vars eqns_not_for_this_lit `thenDs` \ match_result2 ->
194 returnDs (combineMatchResults match_result1 match_result2)
197 Given a blob of @LitPat@s/@NPat@s, we want to split them into those
198 that are ``same''/different as one we are looking at. We need to know
199 whether we're looking at a @LitPat@/@NPat@, and what literal we're after.
202 partitionEqnsByLit :: TypecheckedPat
204 -> ([EquationInfo], -- These ones are for this lit, AND
205 -- they've been "shifted" by stripping
206 -- off the first pattern
207 [EquationInfo] -- These are not for this lit; they
208 -- are exactly as fed in.
211 partitionEqnsByLit master_pat eqns
212 = ( \ (xs,ys) -> (catMaybes xs, catMaybes ys))
213 (unzip (map (partition_eqn master_pat) eqns))
215 partition_eqn :: TypecheckedPat -> EquationInfo -> (Maybe EquationInfo, Maybe EquationInfo)
217 partition_eqn (LitPat k1) (EqnInfo n ctx (LitPat k2 : remaining_pats) match_result)
218 | k1 == k2 = (Just (EqnInfo n ctx remaining_pats match_result), Nothing)
219 -- NB the pattern is stripped off the EquationInfo
221 partition_eqn (NPatOut k1 _ _) (EqnInfo n ctx (NPatOut k2 _ _ : remaining_pats) match_result)
222 | k1 == k2 = (Just (EqnInfo n ctx remaining_pats match_result), Nothing)
223 -- NB the pattern is stripped off the EquationInfo
225 partition_eqn (NPlusKPatOut master_n k1 _ _)
226 (EqnInfo n ctx (NPlusKPatOut n' k2 _ _ : remaining_pats) match_result)
227 | k1 == k2 = (Just (EqnInfo n ctx remaining_pats new_match_result), Nothing)
228 -- NB the pattern is stripped off the EquationInfo
230 new_match_result | master_n == n' = match_result
231 | otherwise = mkCoLetsMatchResult
232 [NonRec n' (Var master_n)] match_result
234 -- Wild-card patterns, which will only show up in the shadows,
235 -- go into both groups
236 partition_eqn master_pat eqn@(EqnInfo n ctx (WildPat _ : remaining_pats) match_result)
237 = (Just (EqnInfo n ctx remaining_pats match_result), Just eqn)
239 -- Default case; not for this pattern
240 partition_eqn master_pat eqn = (Nothing, Just eqn)