a4ed52d685a89b087bbe6ac27b4288c52ff1846c
[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, Qualifier, 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) )
20
21 import DsMonad
22 import DsUtils
23
24 import Literal          ( mkMachInt, Literal(..) )
25 import Maybes           ( catMaybes )
26 import Type             ( isPrimType )
27 import Util             ( panic, assertPanic )
28 \end{code}
29
30 \begin{code}
31 matchLiterals :: [Id]
32               -> [EquationInfo]
33               -> [EquationInfo]         -- Shadows
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 (LitPat literal lit_ty : ps1) _ : eqns) shadows
46   = -- GENERATE THE ALTS
47     match_prims_used vars eqns_info shadows `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 ((LitPat literal lit_ty):ps1) _ : eqns) shadows
55       = let
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
60         in
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 ->
63
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 ->
67         returnDs (
68             (mk_core_lit lit_ty literal, match_result)
69             : rest_of_alts
70         )
71       where
72         mk_core_lit :: Type -> HsLit -> Literal
73
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"
82 \end{code}
83
84 \begin{code}
85 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo ((NPat literal lit_ty eq_chk):ps1) _ : eqns) shadows
86   = let
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
91     in
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 ->
95
96     if (null eqns_not_for_this_lit)
97     then
98         returnDs match_result1
99     else
100         matchLiterals all_vars eqns_not_for_this_lit shadows_not_for_this_lit   `thenDs` \ match_result2 ->
101         combineMatchResults match_result1 match_result2
102 \end{code}
103
104 For an n+k pattern, we use the various magic expressions we've been given.
105 We generate:
106 \begin{verbatim}
107     if ge var lit then
108         let n = sub var lit
109         in  <expr-for-a-successful-match>
110     else
111         <try-next-pattern-or-whatever>
112 \end{verbatim}
113
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.
117
118 \begin{code}
119 partitionEqnsByLit :: HsLit
120                    -> [EquationInfo]
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.
126                       )
127
128 partitionEqnsByLit lit eqns
129   = ( \ (xs,ys) -> (catMaybes xs, catMaybes ys))
130         (unzip (map (partition_eqn lit) eqns))
131   where
132     partition_eqn :: HsLit -> EquationInfo ->
133                 (Maybe EquationInfo, Maybe EquationInfo)
134
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
138
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
142
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)
146
147         -- Default case; not for this pattern
148     partition_eqn lit eqn = (Nothing, Just eqn)
149
150 -- ToDo: meditate about this equality business...
151
152 eq_lit (HsInt  i1)       (HsInt  i2)       = i1 == i2
153 eq_lit (HsFrac f1)       (HsFrac f2)       = f1 == f2
154
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"
164 \end{code}