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