compiler: de-lhs deSugar/
[ghc.git] / compiler / deSugar / MatchCon.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5
6 Pattern-matching constructors
7 -}
8
9 {-# LANGUAGE CPP #-}
10
11 module MatchCon ( matchConFamily, matchPatSyn ) where
12
13 #include "HsVersions.h"
14
15 import {-# SOURCE #-} Match ( match )
16
17 import HsSyn
18 import DsBinds
19 import ConLike
20 import DataCon
21 import PatSyn
22 import TcType
23 import DsMonad
24 import DsUtils
25 import MkCore ( mkCoreLets )
26 import Util
27 import ListSetOps ( runs )
28 import Id
29 import NameEnv
30 import SrcLoc
31 import DynFlags
32 import Outputable
33 import Control.Monad(liftM)
34
35 {-
36 We are confronted with the first column of patterns in a set of
37 equations, all beginning with constructors from one ``family'' (e.g.,
38 @[]@ and @:@ make up the @List@ ``family''). We want to generate the
39 alternatives for a @Case@ expression. There are several choices:
40 \begin{enumerate}
41 \item
42 Generate an alternative for every constructor in the family, whether
43 they are used in this set of equations or not; this is what the Wadler
44 chapter does.
45 \begin{description}
46 \item[Advantages:]
47 (a)~Simple. (b)~It may also be that large sparsely-used constructor
48 families are mainly handled by the code for literals.
49 \item[Disadvantages:]
50 (a)~Not practical for large sparsely-used constructor families, e.g.,
51 the ASCII character set. (b)~Have to look up a list of what
52 constructors make up the whole family.
53 \end{description}
54
55 \item
56 Generate an alternative for each constructor used, then add a default
57 alternative in case some constructors in the family weren't used.
58 \begin{description}
59 \item[Advantages:]
60 (a)~Alternatives aren't generated for unused constructors. (b)~The
61 STG is quite happy with defaults. (c)~No lookup in an environment needed.
62 \item[Disadvantages:]
63 (a)~A spurious default alternative may be generated.
64 \end{description}
65
66 \item
67 ``Do it right:'' generate an alternative for each constructor used,
68 and add a default alternative if all constructors in the family
69 weren't used.
70 \begin{description}
71 \item[Advantages:]
72 (a)~You will get cases with only one alternative (and no default),
73 which should be amenable to optimisation. Tuples are a common example.
74 \item[Disadvantages:]
75 (b)~Have to look up constructor families in TDE (as above).
76 \end{description}
77 \end{enumerate}
78
79 We are implementing the ``do-it-right'' option for now. The arguments
80 to @matchConFamily@ are the same as to @match@; the extra @Int@
81 returned is the number of constructors in the family.
82
83 The function @matchConFamily@ is concerned with this
84 have-we-used-all-the-constructors? question; the local function
85 @match_cons_used@ does all the real work.
86 -}
87
88 matchConFamily :: [Id]
89 -> Type
90 -> [[EquationInfo]]
91 -> DsM MatchResult
92 -- Each group of eqns is for a single constructor
93 matchConFamily (var:vars) ty groups
94 = do dflags <- getDynFlags
95 alts <- mapM (fmap toRealAlt . matchOneConLike vars ty) groups
96 return (mkCoAlgCaseMatchResult dflags var ty alts)
97 where
98 toRealAlt alt = case alt_pat alt of
99 RealDataCon dcon -> alt{ alt_pat = dcon }
100 _ -> panic "matchConFamily: not RealDataCon"
101 matchConFamily [] _ _ = panic "matchConFamily []"
102
103 matchPatSyn :: [Id]
104 -> Type
105 -> [EquationInfo]
106 -> DsM MatchResult
107 matchPatSyn (var:vars) ty eqns
108 = do alt <- fmap toSynAlt $ matchOneConLike vars ty eqns
109 return (mkCoSynCaseMatchResult var ty alt)
110 where
111 toSynAlt alt = case alt_pat alt of
112 PatSynCon psyn -> alt{ alt_pat = psyn }
113 _ -> panic "matchPatSyn: not PatSynCon"
114 matchPatSyn _ _ _ = panic "matchPatSyn []"
115
116 type ConArgPats = HsConDetails (LPat Id) (HsRecFields Id (LPat Id))
117
118 matchOneConLike :: [Id]
119 -> Type
120 -> [EquationInfo]
121 -> DsM (CaseAlt ConLike)
122 matchOneConLike vars ty (eqn1 : eqns) -- All eqns for a single constructor
123 = do { arg_vars <- selectConMatchVars val_arg_tys args1
124 -- Use the first equation as a source of
125 -- suggestions for the new variables
126
127 -- Divide into sub-groups; see Note [Record patterns]
128 ; let groups :: [[(ConArgPats, EquationInfo)]]
129 groups = runs compatible_pats [ (pat_args (firstPat eqn), eqn)
130 | eqn <- eqn1:eqns ]
131
132 ; match_results <- mapM (match_group arg_vars) groups
133
134 ; return $ MkCaseAlt{ alt_pat = con1,
135 alt_bndrs = tvs1 ++ dicts1 ++ arg_vars,
136 alt_wrapper = wrapper1,
137 alt_result = foldr1 combineMatchResults match_results } }
138 where
139 ConPatOut { pat_con = L _ con1, pat_arg_tys = arg_tys, pat_wrap = wrapper1,
140 pat_tvs = tvs1, pat_dicts = dicts1, pat_args = args1 }
141 = firstPat eqn1
142 fields1 = case con1 of
143 RealDataCon dcon1 -> dataConFieldLabels dcon1
144 PatSynCon{} -> []
145
146 val_arg_tys = case con1 of
147 RealDataCon dcon1 -> dataConInstOrigArgTys dcon1 inst_tys
148 PatSynCon psyn1 -> patSynInstArgTys psyn1 inst_tys
149 inst_tys = ASSERT( tvs1 `equalLength` ex_tvs )
150 arg_tys ++ mkTyVarTys tvs1
151 -- dataConInstOrigArgTys takes the univ and existential tyvars
152 -- and returns the types of the *value* args, which is what we want
153
154 ex_tvs = case con1 of
155 RealDataCon dcon1 -> dataConExTyVars dcon1
156 PatSynCon psyn1 -> patSynExTyVars psyn1
157
158 match_group :: [Id] -> [(ConArgPats, EquationInfo)] -> DsM MatchResult
159 -- All members of the group have compatible ConArgPats
160 match_group arg_vars arg_eqn_prs
161 = ASSERT( notNull arg_eqn_prs )
162 do { (wraps, eqns') <- liftM unzip (mapM shift arg_eqn_prs)
163 ; let group_arg_vars = select_arg_vars arg_vars arg_eqn_prs
164 ; match_result <- match (group_arg_vars ++ vars) ty eqns'
165 ; return (adjustMatchResult (foldr1 (.) wraps) match_result) }
166
167 shift (_, eqn@(EqnInfo { eqn_pats = ConPatOut{ pat_tvs = tvs, pat_dicts = ds,
168 pat_binds = bind, pat_args = args
169 } : pats }))
170 = do ds_bind <- dsTcEvBinds bind
171 return ( wrapBinds (tvs `zip` tvs1)
172 . wrapBinds (ds `zip` dicts1)
173 . mkCoreLets ds_bind
174 , eqn { eqn_pats = conArgPats val_arg_tys args ++ pats }
175 )
176 shift (_, (EqnInfo { eqn_pats = ps })) = pprPanic "matchOneCon/shift" (ppr ps)
177
178 -- Choose the right arg_vars in the right order for this group
179 -- Note [Record patterns]
180 select_arg_vars arg_vars ((arg_pats, _) : _)
181 | RecCon flds <- arg_pats
182 , let rpats = rec_flds flds
183 , not (null rpats) -- Treated specially; cf conArgPats
184 = ASSERT2( length fields1 == length arg_vars,
185 ppr con1 $$ ppr fields1 $$ ppr arg_vars )
186 map lookup_fld rpats
187 | otherwise
188 = arg_vars
189 where
190 fld_var_env = mkNameEnv $ zipEqual "get_arg_vars" fields1 arg_vars
191 lookup_fld (L _ rpat) = lookupNameEnv_NF fld_var_env
192 (idName (unLoc (hsRecFieldId rpat)))
193 select_arg_vars _ [] = panic "matchOneCon/select_arg_vars []"
194 matchOneConLike _ _ [] = panic "matchOneCon []"
195
196 -----------------
197 compatible_pats :: (ConArgPats,a) -> (ConArgPats,a) -> Bool
198 -- Two constructors have compatible argument patterns if the number
199 -- and order of sub-matches is the same in both cases
200 compatible_pats (RecCon flds1, _) (RecCon flds2, _) = same_fields flds1 flds2
201 compatible_pats (RecCon flds1, _) _ = null (rec_flds flds1)
202 compatible_pats _ (RecCon flds2, _) = null (rec_flds flds2)
203 compatible_pats _ _ = True -- Prefix or infix con
204
205 same_fields :: HsRecFields Id (LPat Id) -> HsRecFields Id (LPat Id) -> Bool
206 same_fields flds1 flds2
207 = all2 (\(L _ f1) (L _ f2)
208 -> unLoc (hsRecFieldId f1) == unLoc (hsRecFieldId f2))
209 (rec_flds flds1) (rec_flds flds2)
210
211
212 -----------------
213 selectConMatchVars :: [Type] -> ConArgPats -> DsM [Id]
214 selectConMatchVars arg_tys (RecCon {}) = newSysLocalsDs arg_tys
215 selectConMatchVars _ (PrefixCon ps) = selectMatchVars (map unLoc ps)
216 selectConMatchVars _ (InfixCon p1 p2) = selectMatchVars [unLoc p1, unLoc p2]
217
218 conArgPats :: [Type] -- Instantiated argument types
219 -- Used only to fill in the types of WildPats, which
220 -- are probably never looked at anyway
221 -> ConArgPats
222 -> [Pat Id]
223 conArgPats _arg_tys (PrefixCon ps) = map unLoc ps
224 conArgPats _arg_tys (InfixCon p1 p2) = [unLoc p1, unLoc p2]
225 conArgPats arg_tys (RecCon (HsRecFields { rec_flds = rpats }))
226 | null rpats = map WildPat arg_tys
227 -- Important special case for C {}, which can be used for a
228 -- datacon that isn't declared to have fields at all
229 | otherwise = map (unLoc . hsRecFieldArg . unLoc) rpats
230
231 {-
232 Note [Record patterns]
233 ~~~~~~~~~~~~~~~~~~~~~~
234 Consider
235 data T = T { x,y,z :: Bool }
236
237 f (T { y=True, x=False }) = ...
238
239 We must match the patterns IN THE ORDER GIVEN, thus for the first
240 one we match y=True before x=False. See Trac #246; or imagine
241 matching against (T { y=False, x=undefined }): should fail without
242 touching the undefined.
243
244 Now consider:
245
246 f (T { y=True, x=False }) = ...
247 f (T { x=True, y= False}) = ...
248
249 In the first we must test y first; in the second we must test x
250 first. So we must divide even the equations for a single constructor
251 T into sub-goups, based on whether they match the same field in the
252 same order. That's what the (runs compatible_pats) grouping.
253
254 All non-record patterns are "compatible" in this sense, because the
255 positional patterns (T a b) and (a `T` b) all match the arguments
256 in order. Also T {} is special because it's equivalent to (T _ _).
257 Hence the (null rpats) checks here and there.
258
259
260 Note [Existentials in shift_con_pat]
261 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
262 Consider
263 data T = forall a. Ord a => T a (a->Int)
264
265 f (T x f) True = ...expr1...
266 f (T y g) False = ...expr2..
267
268 When we put in the tyvars etc we get
269
270 f (T a (d::Ord a) (x::a) (f::a->Int)) True = ...expr1...
271 f (T b (e::Ord b) (y::a) (g::a->Int)) True = ...expr2...
272
273 After desugaring etc we'll get a single case:
274
275 f = \t::T b::Bool ->
276 case t of
277 T a (d::Ord a) (x::a) (f::a->Int)) ->
278 case b of
279 True -> ...expr1...
280 False -> ...expr2...
281
282 *** We have to substitute [a/b, d/e] in expr2! **
283 Hence
284 False -> ....((/\b\(e:Ord b).expr2) a d)....
285
286 Originally I tried to use
287 (\b -> let e = d in expr2) a
288 to do this substitution. While this is "correct" in a way, it fails
289 Lint, because e::Ord b but d::Ord a.
290 -}