[project @ 2000-09-22 15:56:12 by simonpj]
[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          ( TypecheckedPat )
16 import CoreSyn          ( Expr(..), Bind(..) )
17 import Id               ( Id )
18
19 import DsMonad
20 import DsUtils
21
22 import Literal          ( mkMachInt, Literal(..) )
23 import Maybes           ( catMaybes )
24 import Type             ( isUnLiftedType )
25 import Panic            ( panic, assertPanic )
26 \end{code}
27
28 \begin{code}
29 matchLiterals :: [Id]
30               -> [EquationInfo]
31               -> DsM MatchResult
32 \end{code}
33
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@.
40
41 \begin{code}
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 ->
45
46     -- MAKE THE PRIMITIVE CASE
47     returnDs (mkCoPrimCaseMatchResult var prim_alts)
48   where
49     match_prims_used _ [{-no more eqns-}] = returnDs []
50
51     match_prims_used vars eqns_info@(EqnInfo n ctx (pat@(LitPat literal lit_ty):ps1) _ : eqns)
52       = let
53             (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
54               = partitionEqnsByLit pat eqns_info
55         in
56         -- recursive call to make other alts...
57         match_prims_used vars eqns_not_for_this_lit       `thenDs` \ rest_of_alts ->
58
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 ->
62         returnDs (
63             (mk_core_lit literal, match_result)
64             : rest_of_alts
65         )
66       where
67         mk_core_lit :: HsLit -> Literal
68
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)
75                                            MachLitLit s ty
76         mk_core_lit other                = panic "matchLiterals:mk_core_lit:unhandled"
77 \end{code}
78
79 \begin{code}
80 matchLiterals all_vars@(var:vars)
81   eqns_info@(EqnInfo n ctx (pat@(NPat literal lit_ty eq_chk):ps1) _ : eqns)
82   = let
83         (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
84           = partitionEqnsByLit pat eqns_info
85     in
86     dsExpr (HsApp eq_chk (HsVar var))           `thenDs` \ pred_expr ->
87     match vars shifted_eqns_for_this_lit        `thenDs` \ inner_match_result ->
88     let
89         match_result1 = mkGuardedMatchResult pred_expr inner_match_result
90     in
91     if (null eqns_not_for_this_lit)
92     then
93         returnDs match_result1
94     else
95         matchLiterals all_vars eqns_not_for_this_lit      `thenDs` \ match_result2 ->
96         returnDs (combineMatchResults match_result1 match_result2)
97 \end{code}
98
99 For an n+k pattern, we use the various magic expressions we've been given.
100 We generate:
101 \begin{verbatim}
102     if ge var lit then
103         let n = sub var lit
104         in  <expr-for-a-successful-match>
105     else
106         <try-next-pattern-or-whatever>
107 \end{verbatim}
108
109
110 \begin{code}
111 matchLiterals all_vars@(var:vars) eqns_info@(EqnInfo n ctx (pat@(NPlusKPat master_n k ty ge sub):ps1) _ : eqns)
112   = let
113         (shifted_eqns_for_this_lit, eqns_not_for_this_lit)
114           = partitionEqnsByLit pat eqns_info
115     in
116     match vars shifted_eqns_for_this_lit        `thenDs` \ inner_match_result ->
117
118     dsExpr (HsApp ge (HsVar var))               `thenDs` \ ge_expr ->
119     dsExpr (HsApp sub (HsVar var))              `thenDs` \ nminusk_expr ->
120
121     let
122         match_result1 = mkGuardedMatchResult ge_expr $
123                         mkCoLetsMatchResult [NonRec master_n nminusk_expr] $
124                         inner_match_result
125     in
126     if (null eqns_not_for_this_lit)
127     then 
128         returnDs match_result1
129     else 
130         matchLiterals all_vars eqns_not_for_this_lit    `thenDs` \ match_result2 ->
131         returnDs (combineMatchResults match_result1 match_result2)
132 \end{code}
133
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.
137
138 \begin{code}
139 partitionEqnsByLit :: TypecheckedPat
140                    -> [EquationInfo]
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.
146                       )
147
148 partitionEqnsByLit master_pat eqns
149   = ( \ (xs,ys) -> (catMaybes xs, catMaybes ys))
150         (unzip (map (partition_eqn master_pat) eqns))
151   where
152     partition_eqn :: TypecheckedPat -> EquationInfo -> (Maybe EquationInfo, Maybe EquationInfo)
153
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
157
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
161
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
166       where
167         new_match_result | master_n == n' = match_result
168                          | otherwise      = mkCoLetsMatchResult
169                                                [NonRec n' (Var master_n)] match_result
170
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)
175
176         -- Default case; not for this pattern
177     partition_eqn master_pat eqn = (Nothing, Just eqn)
178 \end{code}