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