2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[MatchLit]{Pattern-matching literal patterns}
7 module MatchLit ( matchLiterals ) where
9 #include "HsVersions.h"
11 import {-# SOURCE #-} Match ( match )
12 import {-# SOURCE #-} DsExpr ( dsExpr )
14 import HsSyn ( HsLit(..), OutPat(..), HsExpr(..) )
15 import TcHsSyn ( TypecheckedPat )
16 import CoreSyn ( Expr(..), Bind(..) )
22 import Literal ( mkMachInt, Literal(..) )
23 import Maybes ( catMaybes )
24 import Type ( isUnLiftedType )
25 import Panic ( panic, assertPanic )
34 This first one is a {\em special case} where the literal patterns are
35 unboxed numbers (NB: the fiddling introduced by @tidyEqnInfo@). We
36 want to avoid using the ``equality'' stuff provided by the
37 typechecker, and do a real ``case'' instead. In that sense, the code
38 is much like @matchConFamily@, which uses @match_cons_used@ to create
39 the alts---here we use @match_prims_used@.
42 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (LitPat literal lit_ty : ps1) _ : eqns)
43 = -- GENERATE THE ALTS
44 match_prims_used vars eqns_info `thenDs` \ prim_alts ->
46 -- MAKE THE PRIMITIVE CASE
47 returnDs (mkCoPrimCaseMatchResult var prim_alts)
49 match_prims_used _ [{-no more eqns-}] = returnDs []
51 match_prims_used vars eqns_info@(EqnInfo n ctx (pat@(LitPat literal lit_ty):ps1) _ : eqns)
53 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
54 = partitionEqnsByLit pat eqns_info
56 -- recursive call to make other alts...
57 match_prims_used vars eqns_not_for_this_lit `thenDs` \ rest_of_alts ->
59 -- (prim pats have no args; no selectMatchVars as in match_cons_used)
60 -- now do the business to make the alt for _this_ LitPat ...
61 match vars shifted_eqns_for_this_lit `thenDs` \ match_result ->
63 (mk_core_lit literal, match_result)
67 mk_core_lit :: HsLit -> Literal
69 mk_core_lit (HsIntPrim i) = mkMachInt i
70 mk_core_lit (HsCharPrim c) = MachChar c
71 mk_core_lit (HsStringPrim s) = MachStr s
72 mk_core_lit (HsFloatPrim f) = MachFloat f
73 mk_core_lit (HsDoublePrim d) = MachDouble d
74 mk_core_lit (HsLitLit s ty) = ASSERT(isUnLiftedType ty)
76 mk_core_lit other = panic "matchLiterals:mk_core_lit:unhandled"
80 matchLiterals all_vars@(var:vars)
81 eqns_info@(EqnInfo n ctx (pat@(NPat literal lit_ty eq_chk):ps1) _ : eqns)
83 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
84 = partitionEqnsByLit pat eqns_info
86 dsExpr (HsApp eq_chk (HsVar var)) `thenDs` \ pred_expr ->
87 match vars shifted_eqns_for_this_lit `thenDs` \ inner_match_result ->
89 match_result1 = mkGuardedMatchResult pred_expr inner_match_result
91 if (null eqns_not_for_this_lit)
93 returnDs match_result1
95 matchLiterals all_vars eqns_not_for_this_lit `thenDs` \ match_result2 ->
96 returnDs (combineMatchResults match_result1 match_result2)
99 For an n+k pattern, we use the various magic expressions we've been given.
104 in <expr-for-a-successful-match>
106 <try-next-pattern-or-whatever>
111 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (pat@(NPlusKPat master_n k ty ge sub):ps1) _ : eqns)
113 (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
114 = partitionEqnsByLit pat eqns_info
116 match vars shifted_eqns_for_this_lit `thenDs` \ inner_match_result ->
118 dsExpr (HsApp ge (HsVar var)) `thenDs` \ ge_expr ->
119 dsExpr (HsApp sub (HsVar var)) `thenDs` \ nminusk_expr ->
122 match_result1 = mkGuardedMatchResult ge_expr $
123 mkCoLetsMatchResult [NonRec master_n nminusk_expr] $
126 if (null eqns_not_for_this_lit)
128 returnDs match_result1
130 matchLiterals all_vars eqns_not_for_this_lit `thenDs` \ match_result2 ->
131 returnDs (combineMatchResults match_result1 match_result2)
134 Given a blob of @LitPat@s/@NPat@s, we want to split them into those
135 that are ``same''/different as one we are looking at. We need to know
136 whether we're looking at a @LitPat@/@NPat@, and what literal we're after.
139 partitionEqnsByLit :: TypecheckedPat
141 -> ([EquationInfo], -- These ones are for this lit, AND
142 -- they've been "shifted" by stripping
143 -- off the first pattern
144 [EquationInfo] -- These are not for this lit; they
145 -- are exactly as fed in.
148 partitionEqnsByLit master_pat eqns
149 = ( \ (xs,ys) -> (catMaybes xs, catMaybes ys))
150 (unzip (map (partition_eqn master_pat) eqns))
152 partition_eqn :: TypecheckedPat -> EquationInfo -> (Maybe EquationInfo, Maybe EquationInfo)
154 partition_eqn (LitPat k1 _) (EqnInfo n ctx (LitPat k2 _ : remaining_pats) match_result)
155 | k1 == k2 = (Just (EqnInfo n ctx remaining_pats match_result), Nothing)
156 -- NB the pattern is stripped off the EquationInfo
158 partition_eqn (NPat k1 _ _) (EqnInfo n ctx (NPat k2 _ _ : remaining_pats) match_result)
159 | k1 == k2 = (Just (EqnInfo n ctx remaining_pats match_result), Nothing)
160 -- NB the pattern is stripped off the EquationInfo
162 partition_eqn (NPlusKPat master_n k1 _ _ _)
163 (EqnInfo n ctx (NPlusKPat n' k2 _ _ _ : remaining_pats) match_result)
164 | k1 == k2 = (Just (EqnInfo n ctx remaining_pats new_match_result), Nothing)
165 -- NB the pattern is stripped off the EquationInfo
167 new_match_result | master_n == n' = match_result
168 | otherwise = mkCoLetsMatchResult
169 [NonRec n' (Var master_n)] match_result
171 -- Wild-card patterns, which will only show up in the shadows,
172 -- go into both groups
173 partition_eqn master_pat eqn@(EqnInfo n ctx (WildPat _ : remaining_pats) match_result)
174 = (Just (EqnInfo n ctx remaining_pats match_result), Just eqn)
176 -- Default case; not for this pattern
177 partition_eqn master_pat eqn = (Nothing, Just eqn)