[project @ 1997-03-14 07:52:06 by simonpj]
[ghc-hetmet.git] / ghc / compiler / deSugar / MatchLit.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[MatchLit]{Pattern-matching literal patterns}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module MatchLit ( matchLiterals ) where
10
11 IMP_Ubiq()
12 IMPORT_DELOOPER(DsLoop)         -- break match-ish and dsExpr-ish loops
13
14 import HsSyn            ( HsLit(..), OutPat(..), HsExpr(..), Fixity,
15                           Match, HsBinds, Stmt(..), DoOrListComp, HsType, ArithSeqInfo )
16 import TcHsSyn          ( SYN_IE(TypecheckedHsExpr), SYN_IE(TypecheckedHsBinds),
17                           SYN_IE(TypecheckedPat)
18                         )
19 import CoreSyn          ( SYN_IE(CoreExpr), SYN_IE(CoreBinding), GenCoreExpr(..), GenCoreBinding(..) )
20 import Id               ( GenId {- instance Eq -} )
21
22 import DsMonad
23 import DsUtils
24
25 import Literal          ( mkMachInt, Literal(..) )
26 import Maybes           ( catMaybes )
27 import Type             ( isPrimType )
28 import Util             ( panic, assertPanic )
29 \end{code}
30
31 \begin{code}
32 matchLiterals :: [Id]
33               -> [EquationInfo]
34               -> [EquationInfo]         -- Shadows
35               -> DsM MatchResult
36 \end{code}
37
38 This first one is a {\em special case} where the literal patterns are
39 unboxed numbers (NB: the fiddling introduced by @tidyEqnInfo@).  We
40 want to avoid using the ``equality'' stuff provided by the
41 typechecker, and do a real ``case'' instead.  In that sense, the code
42 is much like @matchConFamily@, which uses @match_cons_used@ to create
43 the alts---here we use @match_prims_used@.
44
45 \begin{code}
46 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo (LitPat literal lit_ty : ps1) _ : eqns) shadows
47   = -- GENERATE THE ALTS
48     match_prims_used vars eqns_info shadows `thenDs` \ prim_alts ->
49
50     -- MAKE THE PRIMITIVE CASE
51     mkCoPrimCaseMatchResult var prim_alts
52   where
53     match_prims_used _ [{-no more eqns-}] _ = returnDs []
54
55     match_prims_used vars eqns_info@(EqnInfo ((LitPat literal lit_ty):ps1) _ : eqns) shadows
56       = let
57             (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
58               = partitionEqnsByLit Nothing literal eqns_info
59             (shifted_shadows_for_this_lit, shadows_not_for_this_lit)
60               = partitionEqnsByLit Nothing literal shadows
61         in
62         -- recursive call to make other alts...
63         match_prims_used vars eqns_not_for_this_lit shadows_not_for_this_lit    `thenDs` \ rest_of_alts ->
64
65         -- (prim pats have no args; no selectMatchVars as in match_cons_used)
66         -- now do the business to make the alt for _this_ LitPat ...
67         match vars shifted_eqns_for_this_lit shifted_shadows_for_this_lit       `thenDs` \ match_result ->
68         returnDs (
69             (mk_core_lit lit_ty literal, match_result)
70             : rest_of_alts
71         )
72       where
73         mk_core_lit :: Type -> HsLit -> Literal
74
75         mk_core_lit ty (HsIntPrim     i) = mkMachInt  i
76         mk_core_lit ty (HsCharPrim    c) = MachChar   c
77         mk_core_lit ty (HsStringPrim  s) = MachStr    s
78         mk_core_lit ty (HsFloatPrim   f) = MachFloat  f
79         mk_core_lit ty (HsDoublePrim  d) = MachDouble d
80         mk_core_lit ty (HsLitLit      s) = ASSERT(isPrimType ty)
81                                            MachLitLit s (panic "MatchLit.matchLiterals:mk_core_lit:HsLitLit; typePrimRep???")
82         mk_core_lit ty other             = panic "matchLiterals:mk_core_lit:unhandled"
83 \end{code}
84
85 \begin{code}
86 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo ((NPat literal lit_ty eq_chk):ps1) _ : eqns) shadows
87   = let
88         (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
89           = partitionEqnsByLit Nothing literal eqns_info
90         (shifted_shadows_for_this_lit, shadows_not_for_this_lit)
91           = partitionEqnsByLit Nothing literal shadows
92     in
93     dsExpr (HsApp eq_chk (HsVar var))                                   `thenDs` \ pred_expr ->
94     match vars shifted_eqns_for_this_lit shifted_shadows_for_this_lit   `thenDs` \ inner_match_result ->
95     mkGuardedMatchResult pred_expr inner_match_result                   `thenDs` \ match_result1 ->
96
97     if (null eqns_not_for_this_lit)
98     then
99         returnDs match_result1
100     else
101         matchLiterals all_vars eqns_not_for_this_lit shadows_not_for_this_lit   `thenDs` \ match_result2 ->
102         combineMatchResults match_result1 match_result2
103 \end{code}
104
105 For an n+k pattern, we use the various magic expressions we've been given.
106 We generate:
107 \begin{verbatim}
108     if ge var lit then
109         let n = sub var lit
110         in  <expr-for-a-successful-match>
111     else
112         <try-next-pattern-or-whatever>
113 \end{verbatim}
114
115
116 \begin{code}
117 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo ((NPlusKPat master_n k ty ge sub):ps1) _ : eqns) shadows
118   = let
119         (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
120           = partitionEqnsByLit (Just master_n) k eqns_info
121         (shifted_shadows_for_this_lit, shadows_not_for_this_lit)
122           = partitionEqnsByLit (Just master_n) k shadows
123     in
124     match vars shifted_eqns_for_this_lit shifted_shadows_for_this_lit   `thenDs` \ inner_match_result ->
125
126     dsExpr (HsApp ge (HsVar var))               `thenDs` \ ge_expr ->
127     dsExpr (HsApp sub (HsVar var))              `thenDs` \ nminusk_expr ->
128
129     mkGuardedMatchResult
130         ge_expr
131         (mkCoLetsMatchResult [NonRec master_n nminusk_expr] inner_match_result)
132                                         `thenDs` \ match_result1 ->
133
134     if (null eqns_not_for_this_lit)
135     then 
136         returnDs match_result1
137     else 
138         matchLiterals all_vars eqns_not_for_this_lit shadows_not_for_this_lit   `thenDs` \ match_result2 ->
139         combineMatchResults match_result1 match_result2
140 \end{code}
141
142 Given a blob of LitPats/NPats, we want to split them into those
143 that are ``same''/different as one we are looking at.  We need to know
144 whether we're looking at a LitPat/NPat, and what literal we're after.
145
146 \begin{code}
147 partitionEqnsByLit :: Maybe Id  -- (Just v) for N-plus-K patterns, where v
148                                 -- is the "master" variable;
149                                 -- Nothing for NPats and LitPats
150                    -> HsLit
151                    -> [EquationInfo]
152                    -> ([EquationInfo],  -- These ones are for this lit, AND
153                                         -- they've been "shifted" by stripping
154                                         -- off the first pattern
155                        [EquationInfo]   -- These are not for this lit; they
156                                         -- are exactly as fed in.
157                       )
158
159 partitionEqnsByLit nPlusK lit eqns
160   = ( \ (xs,ys) -> (catMaybes xs, catMaybes ys))
161         (unzip (map (partition_eqn nPlusK lit) eqns))
162   where
163     partition_eqn :: Maybe Id -> HsLit -> EquationInfo ->
164                 (Maybe EquationInfo, Maybe EquationInfo)
165
166     partition_eqn Nothing lit (EqnInfo (LitPat k _ : remaining_pats) match_result)
167       | lit `eq_lit` k  = (Just (EqnInfo remaining_pats match_result), Nothing)
168                           -- NB the pattern is stripped off the EquationInfo
169
170     partition_eqn Nothing lit (EqnInfo (NPat k _ _ : remaining_pats) match_result)
171       | lit `eq_lit` k  = (Just (EqnInfo remaining_pats match_result), Nothing)
172                           -- NB the pattern is stripped off the EquationInfo
173
174     partition_eqn (Just master_n) lit  (EqnInfo (NPlusKPat n k _ _ _ : remaining_pats) match_result)
175       | lit `eq_lit` k  = (Just (EqnInfo remaining_pats new_match_result), Nothing)
176                           -- NB the pattern is stripped off the EquationInfo
177       where
178         new_match_result | master_n == n = match_result
179                          | otherwise     = mkCoLetsMatchResult [NonRec n (Var master_n)] match_result
180
181         -- Wild-card patterns, which will only show up in the shadows, go into both groups
182     partition_eqn nPlusK lit eqn@(EqnInfo (WildPat _ : remaining_pats) match_result)
183                         = (Just (EqnInfo remaining_pats match_result), Just eqn)
184
185         -- Default case; not for this pattern
186     partition_eqn nPlusK lit eqn = (Nothing, Just eqn)
187
188 -- ToDo: meditate about this equality business...
189
190 eq_lit (HsInt  i1)       (HsInt  i2)       = i1 == i2
191 eq_lit (HsFrac f1)       (HsFrac f2)       = f1 == f2
192
193 eq_lit (HsIntPrim i1)    (HsIntPrim i2)    = i1 == i2
194 eq_lit (HsFloatPrim f1)  (HsFloatPrim f2)  = f1 == f2
195 eq_lit (HsDoublePrim d1) (HsDoublePrim d2) = d1 == d2
196 eq_lit (HsChar c1)       (HsChar c2)       = c1 == c2
197 eq_lit (HsCharPrim c1)   (HsCharPrim c2)   = c1 == c2
198 eq_lit (HsString s1)     (HsString s2)     = s1 == s2
199 eq_lit (HsStringPrim s1) (HsStringPrim s2) = s1 == s2
200 eq_lit (HsLitLit s1)     (HsLitLit s2)     = s1 == s2 -- ToDo: ??? (dubious)
201 eq_lit other1            other2            = panic "matchLiterals:eq_lit"
202 \end{code}