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 IMPORT_DELOOPER(DsLoop) -- break match-ish and dsExpr-ish loops
14 import HsSyn ( HsLit(..), OutPat(..), HsExpr(..),
15 Match, HsBinds, Stmt, Qualifier, PolyType, ArithSeqInfo )
16 import TcHsSyn ( SYN_IE(TypecheckedHsExpr), SYN_IE(TypecheckedHsBinds),
17 SYN_IE(TypecheckedPat)
19 import CoreSyn ( SYN_IE(CoreExpr), SYN_IE(CoreBinding) )
24 import Literal ( mkMachInt, Literal(..) )
25 import Maybes ( catMaybes )
26 import Type ( isPrimType )
27 import Util ( panic, assertPanic )
33 -> [EquationInfo] -- Shadows
37 This first one is a {\em special case} where the literal patterns are
38 unboxed numbers (NB: the fiddling introduced by @tidyEqnInfo@). We
39 want to avoid using the ``equality'' stuff provided by the
40 typechecker, and do a real ``case'' instead. In that sense, the code
41 is much like @matchConFamily@, which uses @match_cons_used@ to create
42 the alts---here we use @match_prims_used@.
45 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo (LitPat literal lit_ty : ps1) _ : eqns) shadows
46 = -- GENERATE THE ALTS
47 match_prims_used vars eqns_info shadows `thenDs` \ prim_alts ->
49 -- MAKE THE PRIMITIVE CASE
50 mkCoPrimCaseMatchResult var prim_alts
52 match_prims_used _ [{-no more eqns-}] _ = returnDs []
54 match_prims_used vars eqns_info@(EqnInfo ((LitPat literal lit_ty):ps1) _ : eqns) shadows
56 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
57 = partitionEqnsByLit literal eqns_info
58 (shifted_shadows_for_this_lit, shadows_not_for_this_lit)
59 = partitionEqnsByLit literal shadows
61 -- recursive call to make other alts...
62 match_prims_used vars eqns_not_for_this_lit shadows_not_for_this_lit `thenDs` \ rest_of_alts ->
64 -- (prim pats have no args; no selectMatchVars as in match_cons_used)
65 -- now do the business to make the alt for _this_ LitPat ...
66 match vars shifted_eqns_for_this_lit shifted_shadows_for_this_lit `thenDs` \ match_result ->
68 (mk_core_lit lit_ty literal, match_result)
72 mk_core_lit :: Type -> HsLit -> Literal
74 mk_core_lit ty (HsIntPrim i) = mkMachInt i
75 mk_core_lit ty (HsCharPrim c) = MachChar c
76 mk_core_lit ty (HsStringPrim s) = MachStr s
77 mk_core_lit ty (HsFloatPrim f) = MachFloat f
78 mk_core_lit ty (HsDoublePrim d) = MachDouble d
79 mk_core_lit ty (HsLitLit s) = ASSERT(isPrimType ty)
80 MachLitLit s (panic "MatchLit.matchLiterals:mk_core_lit:HsLitLit; typePrimRep???")
81 mk_core_lit ty other = panic "matchLiterals:mk_core_lit:unhandled"
85 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo ((NPat literal lit_ty eq_chk):ps1) _ : eqns) shadows
87 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
88 = partitionEqnsByLit literal eqns_info
89 (shifted_shadows_for_this_lit, shadows_not_for_this_lit)
90 = partitionEqnsByLit literal shadows
92 dsExpr (HsApp eq_chk (HsVar var)) `thenDs` \ pred_expr ->
93 match vars shifted_eqns_for_this_lit shifted_shadows_for_this_lit `thenDs` \ inner_match_result ->
94 mkGuardedMatchResult pred_expr inner_match_result `thenDs` \ match_result1 ->
96 if (null eqns_not_for_this_lit)
98 returnDs match_result1
100 matchLiterals all_vars eqns_not_for_this_lit shadows_not_for_this_lit `thenDs` \ match_result2 ->
101 combineMatchResults match_result1 match_result2
104 For an n+k pattern, we use the various magic expressions we've been given.
109 in <expr-for-a-successful-match>
111 <try-next-pattern-or-whatever>
114 Given a blob of LitPats/NPats, we want to split them into those
115 that are ``same''/different as one we are looking at. We need to know
116 whether we're looking at a LitPat/NPat, and what literal we're after.
119 partitionEqnsByLit :: HsLit
121 -> ([EquationInfo], -- These ones are for this lit, AND
122 -- they've been "shifted" by stripping
123 -- off the first pattern
124 [EquationInfo] -- These are not for this lit; they
125 -- are exactly as fed in.
128 partitionEqnsByLit lit eqns
129 = ( \ (xs,ys) -> (catMaybes xs, catMaybes ys))
130 (unzip (map (partition_eqn lit) eqns))
132 partition_eqn :: HsLit -> EquationInfo ->
133 (Maybe EquationInfo, Maybe EquationInfo)
135 partition_eqn lit (EqnInfo (LitPat k _ : remaining_pats) match_result)
136 | lit `eq_lit` k = (Just (EqnInfo remaining_pats match_result), Nothing)
137 -- NB the pattern is stripped off thhe EquationInfo
139 partition_eqn lit (EqnInfo (NPat k _ _ : remaining_pats) match_result)
140 | lit `eq_lit` k = (Just (EqnInfo remaining_pats match_result), Nothing)
141 -- NB the pattern is stripped off thhe EquationInfo
143 -- Wild-card patterns, which will only show up in the shadows, go into both groups
144 partition_eqn lit eqn@(EqnInfo (WildPat _ : remaining_pats) match_result)
145 = (Just (EqnInfo remaining_pats match_result), Just eqn)
147 -- Default case; not for this pattern
148 partition_eqn lit eqn = (Nothing, Just eqn)
150 -- ToDo: meditate about this equality business...
152 eq_lit (HsInt i1) (HsInt i2) = i1 == i2
153 eq_lit (HsFrac f1) (HsFrac f2) = f1 == f2
155 eq_lit (HsIntPrim i1) (HsIntPrim i2) = i1 == i2
156 eq_lit (HsFloatPrim f1) (HsFloatPrim f2) = f1 == f2
157 eq_lit (HsDoublePrim d1) (HsDoublePrim d2) = d1 == d2
158 eq_lit (HsChar c1) (HsChar c2) = c1 == c2
159 eq_lit (HsCharPrim c1) (HsCharPrim c2) = c1 == c2
160 eq_lit (HsString s1) (HsString s2) = s1 == s2
161 eq_lit (HsStringPrim s1) (HsStringPrim s2) = s1 == s2
162 eq_lit (HsLitLit s1) (HsLitLit s2) = s1 == s2 -- ToDo: ??? (dubious)
163 eq_lit other1 other2 = panic "matchLiterals:eq_lit"