Major Overhaul of Pattern Match Checking (Fixes #595)
[ghc.git] / compiler / deSugar / Match.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5
6 The @match@ function
7 -}
8
9 {-# LANGUAGE CPP #-}
10
11 module Match ( match, matchEquations, matchWrapper, matchSimply, matchSinglePat ) where
12
13 #include "HsVersions.h"
14
15 import {-#SOURCE#-} DsExpr (dsLExpr, dsExpr)
16
17 import DynFlags
18 import HsSyn
19 import TcHsSyn
20 import TcEvidence
21 import TcRnMonad
22 import Check
23 import CoreSyn
24 import Literal
25 import CoreUtils
26 import MkCore
27 import DsMonad
28 import DsBinds
29 import DsGRHSs
30 import DsUtils
31 import Id
32 import ConLike
33 import DataCon
34 import PatSyn
35 import MatchCon
36 import MatchLit
37 import Type
38 import TcType ( toTcTypeBag )
39 import TyCon( isNewTyCon )
40 import TysWiredIn
41 import ListSetOps
42 import SrcLoc
43 import Maybes
44 import Util
45 import Name
46 import Outputable
47 import BasicTypes ( isGenerated )
48
49 import Control.Monad( unless )
50 import qualified Data.Map as Map
51
52 {-
53 ************************************************************************
54 * *
55 The main matching function
56 * *
57 ************************************************************************
58
59 The function @match@ is basically the same as in the Wadler chapter,
60 except it is monadised, to carry around the name supply, info about
61 annotations, etc.
62
63 Notes on @match@'s arguments, assuming $m$ equations and $n$ patterns:
64 \begin{enumerate}
65 \item
66 A list of $n$ variable names, those variables presumably bound to the
67 $n$ expressions being matched against the $n$ patterns. Using the
68 list of $n$ expressions as the first argument showed no benefit and
69 some inelegance.
70
71 \item
72 The second argument, a list giving the ``equation info'' for each of
73 the $m$ equations:
74 \begin{itemize}
75 \item
76 the $n$ patterns for that equation, and
77 \item
78 a list of Core bindings [@(Id, CoreExpr)@ pairs] to be ``stuck on
79 the front'' of the matching code, as in:
80 \begin{verbatim}
81 let <binds>
82 in <matching-code>
83 \end{verbatim}
84 \item
85 and finally: (ToDo: fill in)
86
87 The right way to think about the ``after-match function'' is that it
88 is an embryonic @CoreExpr@ with a ``hole'' at the end for the
89 final ``else expression''.
90 \end{itemize}
91
92 There is a type synonym, @EquationInfo@, defined in module @DsUtils@.
93
94 An experiment with re-ordering this information about equations (in
95 particular, having the patterns available in column-major order)
96 showed no benefit.
97
98 \item
99 A default expression---what to evaluate if the overall pattern-match
100 fails. This expression will (almost?) always be
101 a measly expression @Var@, unless we know it will only be used once
102 (as we do in @glue_success_exprs@).
103
104 Leaving out this third argument to @match@ (and slamming in lots of
105 @Var "fail"@s) is a positively {\em bad} idea, because it makes it
106 impossible to share the default expressions. (Also, it stands no
107 chance of working in our post-upheaval world of @Locals@.)
108 \end{enumerate}
109
110 Note: @match@ is often called via @matchWrapper@ (end of this module),
111 a function that does much of the house-keeping that goes with a call
112 to @match@.
113
114 It is also worth mentioning the {\em typical} way a block of equations
115 is desugared with @match@. At each stage, it is the first column of
116 patterns that is examined. The steps carried out are roughly:
117 \begin{enumerate}
118 \item
119 Tidy the patterns in column~1 with @tidyEqnInfo@ (this may add
120 bindings to the second component of the equation-info):
121 \begin{itemize}
122 \item
123 Remove the `as' patterns from column~1.
124 \item
125 Make all constructor patterns in column~1 into @ConPats@, notably
126 @ListPats@ and @TuplePats@.
127 \item
128 Handle any irrefutable (or ``twiddle'') @LazyPats@.
129 \end{itemize}
130 \item
131 Now {\em unmix} the equations into {\em blocks} [w\/ local function
132 @unmix_eqns@], in which the equations in a block all have variable
133 patterns in column~1, or they all have constructor patterns in ...
134 (see ``the mixture rule'' in SLPJ).
135 \item
136 Call @matchEqnBlock@ on each block of equations; it will do the
137 appropriate thing for each kind of column-1 pattern, usually ending up
138 in a recursive call to @match@.
139 \end{enumerate}
140
141 We are a little more paranoid about the ``empty rule'' (SLPJ, p.~87)
142 than the Wadler-chapter code for @match@ (p.~93, first @match@ clause).
143 And gluing the ``success expressions'' together isn't quite so pretty.
144
145 This (more interesting) clause of @match@ uses @tidy_and_unmix_eqns@
146 (a)~to get `as'- and `twiddle'-patterns out of the way (tidying), and
147 (b)~to do ``the mixture rule'' (SLPJ, p.~88) [which really {\em
148 un}mixes the equations], producing a list of equation-info
149 blocks, each block having as its first column of patterns either all
150 constructors, or all variables (or similar beasts), etc.
151
152 @match_unmixed_eqn_blks@ simply takes the place of the @foldr@ in the
153 Wadler-chapter @match@ (p.~93, last clause), and @match_unmixed_blk@
154 corresponds roughly to @matchVarCon@.
155 -}
156
157 match :: [Id] -- Variables rep\'ing the exprs we\'re matching with
158 -> Type -- Type of the case expression
159 -> [EquationInfo] -- Info about patterns, etc. (type synonym below)
160 -> DsM MatchResult -- Desugared result!
161
162 match [] ty eqns
163 = ASSERT2( not (null eqns), ppr ty )
164 return (foldr1 combineMatchResults match_results)
165 where
166 match_results = [ ASSERT( null (eqn_pats eqn) )
167 eqn_rhs eqn
168 | eqn <- eqns ]
169
170 match vars@(v:_) ty eqns -- Eqns *can* be empty
171 = do { dflags <- getDynFlags
172 -- Tidy the first pattern, generating
173 -- auxiliary bindings if necessary
174 ; (aux_binds, tidy_eqns) <- mapAndUnzipM (tidyEqnInfo v) eqns
175
176 -- Group the equations and match each group in turn
177 ; let grouped = groupEquations dflags tidy_eqns
178
179 -- print the view patterns that are commoned up to help debug
180 ; whenDOptM Opt_D_dump_view_pattern_commoning (debug grouped)
181
182 ; match_results <- match_groups grouped
183 ; return (adjustMatchResult (foldr (.) id aux_binds) $
184 foldr1 combineMatchResults match_results) }
185 where
186 dropGroup :: [(PatGroup,EquationInfo)] -> [EquationInfo]
187 dropGroup = map snd
188
189 match_groups :: [[(PatGroup,EquationInfo)]] -> DsM [MatchResult]
190 -- Result list of [MatchResult] is always non-empty
191 match_groups [] = matchEmpty v ty
192 match_groups gs = mapM match_group gs
193
194 match_group :: [(PatGroup,EquationInfo)] -> DsM MatchResult
195 match_group [] = panic "match_group"
196 match_group eqns@((group,_) : _)
197 = case group of
198 PgCon _ -> matchConFamily vars ty (subGroup [(c,e) | (PgCon c, e) <- eqns])
199 PgSyn _ -> matchPatSyn vars ty (dropGroup eqns)
200 PgLit _ -> matchLiterals vars ty (subGroup [(l,e) | (PgLit l, e) <- eqns])
201 PgAny -> matchVariables vars ty (dropGroup eqns)
202 PgN _ -> matchNPats vars ty (dropGroup eqns)
203 PgNpK _ -> matchNPlusKPats vars ty (dropGroup eqns)
204 PgBang -> matchBangs vars ty (dropGroup eqns)
205 PgCo _ -> matchCoercion vars ty (dropGroup eqns)
206 PgView _ _ -> matchView vars ty (dropGroup eqns)
207 PgOverloadedList -> matchOverloadedList vars ty (dropGroup eqns)
208
209 -- FIXME: we should also warn about view patterns that should be
210 -- commoned up but are not
211
212 -- print some stuff to see what's getting grouped
213 -- use -dppr-debug to see the resolution of overloaded literals
214 debug eqns =
215 let gs = map (\group -> foldr (\ (p,_) -> \acc ->
216 case p of PgView e _ -> e:acc
217 _ -> acc) [] group) eqns
218 maybeWarn [] = return ()
219 maybeWarn l = warnDs (vcat l)
220 in
221 maybeWarn $ (map (\g -> text "Putting these view expressions into the same case:" <+> (ppr g))
222 (filter (not . null) gs))
223
224 matchEmpty :: Id -> Type -> DsM [MatchResult]
225 -- See Note [Empty case expressions]
226 matchEmpty var res_ty
227 = return [MatchResult CanFail mk_seq]
228 where
229 mk_seq fail = return $ mkWildCase (Var var) (idType var) res_ty
230 [(DEFAULT, [], fail)]
231
232 matchVariables :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
233 -- Real true variables, just like in matchVar, SLPJ p 94
234 -- No binding to do: they'll all be wildcards by now (done in tidy)
235 matchVariables (_:vars) ty eqns = match vars ty (shiftEqns eqns)
236 matchVariables [] _ _ = panic "matchVariables"
237
238 matchBangs :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
239 matchBangs (var:vars) ty eqns
240 = do { match_result <- match (var:vars) ty $
241 map (decomposeFirstPat getBangPat) eqns
242 ; return (mkEvalMatchResult var ty match_result) }
243 matchBangs [] _ _ = panic "matchBangs"
244
245 matchCoercion :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
246 -- Apply the coercion to the match variable and then match that
247 matchCoercion (var:vars) ty (eqns@(eqn1:_))
248 = do { let CoPat co pat _ = firstPat eqn1
249 ; var' <- newUniqueId var (hsPatType pat)
250 ; match_result <- match (var':vars) ty $
251 map (decomposeFirstPat getCoPat) eqns
252 ; rhs' <- dsHsWrapper co (Var var)
253 ; return (mkCoLetMatchResult (NonRec var' rhs') match_result) }
254 matchCoercion _ _ _ = panic "matchCoercion"
255
256 matchView :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
257 -- Apply the view function to the match variable and then match that
258 matchView (var:vars) ty (eqns@(eqn1:_))
259 = do { -- we could pass in the expr from the PgView,
260 -- but this needs to extract the pat anyway
261 -- to figure out the type of the fresh variable
262 let ViewPat viewExpr (L _ pat) _ = firstPat eqn1
263 -- do the rest of the compilation
264 ; var' <- newUniqueId var (hsPatType pat)
265 ; match_result <- match (var':vars) ty $
266 map (decomposeFirstPat getViewPat) eqns
267 -- compile the view expressions
268 ; viewExpr' <- dsLExpr viewExpr
269 ; return (mkViewMatchResult var' viewExpr' var match_result) }
270 matchView _ _ _ = panic "matchView"
271
272 matchOverloadedList :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
273 matchOverloadedList (var:vars) ty (eqns@(eqn1:_))
274 -- Since overloaded list patterns are treated as view patterns,
275 -- the code is roughly the same as for matchView
276 = do { let ListPat _ elt_ty (Just (_,e)) = firstPat eqn1
277 ; var' <- newUniqueId var (mkListTy elt_ty) -- we construct the overall type by hand
278 ; match_result <- match (var':vars) ty $
279 map (decomposeFirstPat getOLPat) eqns -- getOLPat builds the pattern inside as a non-overloaded version of the overloaded list pattern
280 ; e' <- dsExpr e
281 ; return (mkViewMatchResult var' e' var match_result) }
282 matchOverloadedList _ _ _ = panic "matchOverloadedList"
283
284 -- decompose the first pattern and leave the rest alone
285 decomposeFirstPat :: (Pat Id -> Pat Id) -> EquationInfo -> EquationInfo
286 decomposeFirstPat extractpat (eqn@(EqnInfo { eqn_pats = pat : pats }))
287 = eqn { eqn_pats = extractpat pat : pats}
288 decomposeFirstPat _ _ = panic "decomposeFirstPat"
289
290 getCoPat, getBangPat, getViewPat, getOLPat :: Pat Id -> Pat Id
291 getCoPat (CoPat _ pat _) = pat
292 getCoPat _ = panic "getCoPat"
293 getBangPat (BangPat pat ) = unLoc pat
294 getBangPat _ = panic "getBangPat"
295 getViewPat (ViewPat _ pat _) = unLoc pat
296 getViewPat _ = panic "getViewPat"
297 getOLPat (ListPat pats ty (Just _)) = ListPat pats ty Nothing
298 getOLPat _ = panic "getOLPat"
299
300 {-
301 Note [Empty case alternatives]
302 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
303 The list of EquationInfo can be empty, arising from
304 case x of {} or \case {}
305 In that situation we desugar to
306 case x of { _ -> error "pattern match failure" }
307 The *desugarer* isn't certain whether there really should be no
308 alternatives, so it adds a default case, as it always does. A later
309 pass may remove it if it's inaccessible. (See also Note [Empty case
310 alternatives] in CoreSyn.)
311
312 We do *not* desugar simply to
313 error "empty case"
314 or some such, because 'x' might be bound to (error "hello"), in which
315 case we want to see that "hello" exception, not (error "empty case").
316 See also Note [Case elimination: lifted case] in Simplify.
317
318
319 ************************************************************************
320 * *
321 Tidying patterns
322 * *
323 ************************************************************************
324
325 Tidy up the leftmost pattern in an @EquationInfo@, given the variable @v@
326 which will be scrutinised. This means:
327 \begin{itemize}
328 \item
329 Replace variable patterns @x@ (@x /= v@) with the pattern @_@,
330 together with the binding @x = v@.
331 \item
332 Replace the `as' pattern @x@@p@ with the pattern p and a binding @x = v@.
333 \item
334 Removing lazy (irrefutable) patterns (you don't want to know...).
335 \item
336 Converting explicit tuple-, list-, and parallel-array-pats into ordinary
337 @ConPats@.
338 \item
339 Convert the literal pat "" to [].
340 \end{itemize}
341
342 The result of this tidying is that the column of patterns will include
343 {\em only}:
344 \begin{description}
345 \item[@WildPats@:]
346 The @VarPat@ information isn't needed any more after this.
347
348 \item[@ConPats@:]
349 @ListPats@, @TuplePats@, etc., are all converted into @ConPats@.
350
351 \item[@LitPats@ and @NPats@:]
352 @LitPats@/@NPats@ of ``known friendly types'' (Int, Char,
353 Float, Double, at least) are converted to unboxed form; e.g.,
354 \tr{(NPat (HsInt i) _ _)} is converted to:
355 \begin{verbatim}
356 (ConPat I# _ _ [LitPat (HsIntPrim i)])
357 \end{verbatim}
358 \end{description}
359 -}
360
361 tidyEqnInfo :: Id -> EquationInfo
362 -> DsM (DsWrapper, EquationInfo)
363 -- DsM'd because of internal call to dsLHsBinds
364 -- and mkSelectorBinds.
365 -- "tidy1" does the interesting stuff, looking at
366 -- one pattern and fiddling the list of bindings.
367 --
368 -- POST CONDITION: head pattern in the EqnInfo is
369 -- WildPat
370 -- ConPat
371 -- NPat
372 -- LitPat
373 -- NPlusKPat
374 -- but no other
375
376 tidyEqnInfo _ (EqnInfo { eqn_pats = [] })
377 = panic "tidyEqnInfo"
378
379 tidyEqnInfo v eqn@(EqnInfo { eqn_pats = pat : pats })
380 = do { (wrap, pat') <- tidy1 v pat
381 ; return (wrap, eqn { eqn_pats = do pat' : pats }) }
382
383 tidy1 :: Id -- The Id being scrutinised
384 -> Pat Id -- The pattern against which it is to be matched
385 -> DsM (DsWrapper, -- Extra bindings to do before the match
386 Pat Id) -- Equivalent pattern
387
388 -------------------------------------------------------
389 -- (pat', mr') = tidy1 v pat mr
390 -- tidies the *outer level only* of pat, giving pat'
391 -- It eliminates many pattern forms (as-patterns, variable patterns,
392 -- list patterns, etc) yielding one of:
393 -- WildPat
394 -- ConPatOut
395 -- LitPat
396 -- NPat
397 -- NPlusKPat
398
399 tidy1 v (ParPat pat) = tidy1 v (unLoc pat)
400 tidy1 v (SigPatOut pat _) = tidy1 v (unLoc pat)
401 tidy1 _ (WildPat ty) = return (idDsWrapper, WildPat ty)
402 tidy1 v (BangPat (L l p)) = tidy_bang_pat v l p
403
404 -- case v of { x -> mr[] }
405 -- = case v of { _ -> let x=v in mr[] }
406 tidy1 v (VarPat (L _ var))
407 = return (wrapBind var v, WildPat (idType var))
408
409 -- case v of { x@p -> mr[] }
410 -- = case v of { p -> let x=v in mr[] }
411 tidy1 v (AsPat (L _ var) pat)
412 = do { (wrap, pat') <- tidy1 v (unLoc pat)
413 ; return (wrapBind var v . wrap, pat') }
414
415 {- now, here we handle lazy patterns:
416 tidy1 v ~p bs = (v, v1 = case v of p -> v1 :
417 v2 = case v of p -> v2 : ... : bs )
418
419 where the v_i's are the binders in the pattern.
420
421 ToDo: in "v_i = ... -> v_i", are the v_i's really the same thing?
422
423 The case expr for v_i is just: match [v] [(p, [], \ x -> Var v_i)] any_expr
424 -}
425
426 tidy1 v (LazyPat pat)
427 = do { (_,sel_prs) <- mkSelectorBinds False [] pat (Var v)
428 ; let sel_binds = [NonRec b rhs | (b,rhs) <- sel_prs]
429 ; return (mkCoreLets sel_binds, WildPat (idType v)) }
430
431 tidy1 _ (ListPat pats ty Nothing)
432 = return (idDsWrapper, unLoc list_ConPat)
433 where
434 list_ConPat = foldr (\ x y -> mkPrefixConPat consDataCon [x, y] [ty])
435 (mkNilPat ty)
436 pats
437
438 -- Introduce fake parallel array constructors to be able to handle parallel
439 -- arrays with the existing machinery for constructor pattern
440 tidy1 _ (PArrPat pats ty)
441 = return (idDsWrapper, unLoc parrConPat)
442 where
443 arity = length pats
444 parrConPat = mkPrefixConPat (parrFakeCon arity) pats [ty]
445
446 tidy1 _ (TuplePat pats boxity tys)
447 = return (idDsWrapper, unLoc tuple_ConPat)
448 where
449 arity = length pats
450 tuple_ConPat = mkPrefixConPat (tupleDataCon boxity arity) pats tys
451
452 -- LitPats: we *might* be able to replace these w/ a simpler form
453 tidy1 _ (LitPat lit)
454 = return (idDsWrapper, tidyLitPat lit)
455
456 -- NPats: we *might* be able to replace these w/ a simpler form
457 tidy1 _ (NPat (L _ lit) mb_neg eq)
458 = return (idDsWrapper, tidyNPat tidyLitPat lit mb_neg eq)
459
460 -- Everything else goes through unchanged...
461
462 tidy1 _ non_interesting_pat
463 = return (idDsWrapper, non_interesting_pat)
464
465 --------------------
466 tidy_bang_pat :: Id -> SrcSpan -> Pat Id -> DsM (DsWrapper, Pat Id)
467
468 -- Discard par/sig under a bang
469 tidy_bang_pat v _ (ParPat (L l p)) = tidy_bang_pat v l p
470 tidy_bang_pat v _ (SigPatOut (L l p) _) = tidy_bang_pat v l p
471
472 -- Push the bang-pattern inwards, in the hope that
473 -- it may disappear next time
474 tidy_bang_pat v l (AsPat v' p) = tidy1 v (AsPat v' (L l (BangPat p)))
475 tidy_bang_pat v l (CoPat w p t) = tidy1 v (CoPat w (BangPat (L l p)) t)
476
477 -- Discard bang around strict pattern
478 tidy_bang_pat v _ p@(LitPat {}) = tidy1 v p
479 tidy_bang_pat v _ p@(ListPat {}) = tidy1 v p
480 tidy_bang_pat v _ p@(TuplePat {}) = tidy1 v p
481 tidy_bang_pat v _ p@(PArrPat {}) = tidy1 v p
482
483 -- Data/newtype constructors
484 tidy_bang_pat v l p@(ConPatOut { pat_con = L _ (RealDataCon dc), pat_args = args })
485 | isNewTyCon (dataConTyCon dc) -- Newtypes: push bang inwards (Trac #9844)
486 = tidy1 v (p { pat_args = push_bang_into_newtype_arg l args })
487 | otherwise -- Data types: discard the bang
488 = tidy1 v p
489
490 -------------------
491 -- Default case, leave the bang there:
492 -- VarPat,
493 -- LazyPat,
494 -- WildPat,
495 -- ViewPat,
496 -- pattern synonyms (ConPatOut with PatSynCon)
497 -- NPat,
498 -- NPlusKPat
499 --
500 -- For LazyPat, remember that it's semantically like a VarPat
501 -- i.e. !(~p) is not like ~p, or p! (Trac #8952)
502 --
503 -- NB: SigPatIn, ConPatIn should not happen
504
505 tidy_bang_pat _ l p = return (idDsWrapper, BangPat (L l p))
506
507 -------------------
508 push_bang_into_newtype_arg :: SrcSpan -> HsConPatDetails Id -> HsConPatDetails Id
509 -- See Note [Bang patterns and newtypes]
510 -- We are transforming !(N p) into (N !p)
511 push_bang_into_newtype_arg l (PrefixCon (arg:args))
512 = ASSERT( null args)
513 PrefixCon [L l (BangPat arg)]
514 push_bang_into_newtype_arg l (RecCon rf)
515 | HsRecFields { rec_flds = L lf fld : flds } <- rf
516 , HsRecField { hsRecFieldArg = arg } <- fld
517 = ASSERT( null flds)
518 RecCon (rf { rec_flds = [L lf (fld { hsRecFieldArg = L l (BangPat arg) })] })
519 push_bang_into_newtype_arg _ cd
520 = pprPanic "push_bang_into_newtype_arg" (pprConArgs cd)
521
522 {-
523 Note [Bang patterns and newtypes]
524 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
525 For the pattern !(Just pat) we can discard the bang, because
526 the pattern is strict anyway. But for !(N pat), where
527 newtype NT = N Int
528 we definitely can't discard the bang. Trac #9844.
529
530 So what we do is to push the bang inwards, in the hope that it will
531 get discarded there. So we transform
532 !(N pat) into (N !pat)
533
534
535 \noindent
536 {\bf Previous @matchTwiddled@ stuff:}
537
538 Now we get to the only interesting part; note: there are choices for
539 translation [from Simon's notes]; translation~1:
540 \begin{verbatim}
541 deTwiddle [s,t] e
542 \end{verbatim}
543 returns
544 \begin{verbatim}
545 [ w = e,
546 s = case w of [s,t] -> s
547 t = case w of [s,t] -> t
548 ]
549 \end{verbatim}
550
551 Here \tr{w} is a fresh variable, and the \tr{w}-binding prevents multiple
552 evaluation of \tr{e}. An alternative translation (No.~2):
553 \begin{verbatim}
554 [ w = case e of [s,t] -> (s,t)
555 s = case w of (s,t) -> s
556 t = case w of (s,t) -> t
557 ]
558 \end{verbatim}
559
560 ************************************************************************
561 * *
562 \subsubsection[improved-unmixing]{UNIMPLEMENTED idea for improved unmixing}
563 * *
564 ************************************************************************
565
566 We might be able to optimise unmixing when confronted by
567 only-one-constructor-possible, of which tuples are the most notable
568 examples. Consider:
569 \begin{verbatim}
570 f (a,b,c) ... = ...
571 f d ... (e:f) = ...
572 f (g,h,i) ... = ...
573 f j ... = ...
574 \end{verbatim}
575 This definition would normally be unmixed into four equation blocks,
576 one per equation. But it could be unmixed into just one equation
577 block, because if the one equation matches (on the first column),
578 the others certainly will.
579
580 You have to be careful, though; the example
581 \begin{verbatim}
582 f j ... = ...
583 -------------------
584 f (a,b,c) ... = ...
585 f d ... (e:f) = ...
586 f (g,h,i) ... = ...
587 \end{verbatim}
588 {\em must} be broken into two blocks at the line shown; otherwise, you
589 are forcing unnecessary evaluation. In any case, the top-left pattern
590 always gives the cue. You could then unmix blocks into groups of...
591 \begin{description}
592 \item[all variables:]
593 As it is now.
594 \item[constructors or variables (mixed):]
595 Need to make sure the right names get bound for the variable patterns.
596 \item[literals or variables (mixed):]
597 Presumably just a variant on the constructor case (as it is now).
598 \end{description}
599
600 ************************************************************************
601 * *
602 * matchWrapper: a convenient way to call @match@ *
603 * *
604 ************************************************************************
605 \subsection[matchWrapper]{@matchWrapper@: a convenient interface to @match@}
606
607 Calls to @match@ often involve similar (non-trivial) work; that work
608 is collected here, in @matchWrapper@. This function takes as
609 arguments:
610 \begin{itemize}
611 \item
612 Typchecked @Matches@ (of a function definition, or a case or lambda
613 expression)---the main input;
614 \item
615 An error message to be inserted into any (runtime) pattern-matching
616 failure messages.
617 \end{itemize}
618
619 As results, @matchWrapper@ produces:
620 \begin{itemize}
621 \item
622 A list of variables (@Locals@) that the caller must ``promise'' to
623 bind to appropriate values; and
624 \item
625 a @CoreExpr@, the desugared output (main result).
626 \end{itemize}
627
628 The main actions of @matchWrapper@ include:
629 \begin{enumerate}
630 \item
631 Flatten the @[TypecheckedMatch]@ into a suitable list of
632 @EquationInfo@s.
633 \item
634 Create as many new variables as there are patterns in a pattern-list
635 (in any one of the @EquationInfo@s).
636 \item
637 Create a suitable ``if it fails'' expression---a call to @error@ using
638 the error-string input; the {\em type} of this fail value can be found
639 by examining one of the RHS expressions in one of the @EquationInfo@s.
640 \item
641 Call @match@ with all of this information!
642 \end{enumerate}
643 -}
644
645 matchWrapper :: HsMatchContext Name -- For shadowing warning messages
646 -> Maybe (LHsExpr Id) -- The scrutinee, if we check a case expr
647 -> MatchGroup Id (LHsExpr Id) -- Matches being desugared
648 -> DsM ([Id], CoreExpr) -- Results
649
650 {-
651 There is one small problem with the Lambda Patterns, when somebody
652 writes something similar to:
653 \begin{verbatim}
654 (\ (x:xs) -> ...)
655 \end{verbatim}
656 he/she don't want a warning about incomplete patterns, that is done with
657 the flag @opt_WarnSimplePatterns@.
658 This problem also appears in the:
659 \begin{itemize}
660 \item @do@ patterns, but if the @do@ can fail
661 it creates another equation if the match can fail
662 (see @DsExpr.doDo@ function)
663 \item @let@ patterns, are treated by @matchSimply@
664 List Comprension Patterns, are treated by @matchSimply@ also
665 \end{itemize}
666
667 We can't call @matchSimply@ with Lambda patterns,
668 due to the fact that lambda patterns can have more than
669 one pattern, and match simply only accepts one pattern.
670
671 JJQC 30-Nov-1997
672 -}
673
674 matchWrapper ctxt mb_scr (MG { mg_alts = L _ matches
675 , mg_arg_tys = arg_tys
676 , mg_res_ty = rhs_ty
677 , mg_origin = origin })
678 = do { dflags <- getDynFlags
679 ; locn <- getSrcSpanDs
680
681 ; new_vars <- case matches of
682 [] -> mapM newSysLocalDs arg_tys
683 (m:_) -> selectMatchVars (map unLoc (hsLMatchPats m))
684
685 ; eqns_info <- mapM (mk_eqn_info new_vars) matches
686
687 -- pattern match check warnings
688 ; unless (isGenerated origin) $
689 -- See Note [Type and Term Equality Propagation]
690 addTmCsDs (genCaseTmCs1 mb_scr new_vars) $
691 dsPmWarn dflags (DsMatchContext ctxt locn) $
692 checkMatches new_vars matches
693
694 ; result_expr <- handleWarnings $
695 matchEquations ctxt new_vars eqns_info rhs_ty
696 ; return (new_vars, result_expr) }
697 where
698 mk_eqn_info vars (L _ (Match _ pats _ grhss))
699 = do { dflags <- getDynFlags
700 ; let upats = map (strictify dflags) pats
701 dicts = toTcTypeBag (collectEvVarsPats upats) -- Only TcTyVars
702 ; tm_cs <- genCaseTmCs2 mb_scr upats vars
703 ; match_result <- addDictsDs dicts $ -- See Note [Type and Term Equality Propagation]
704 addTmCsDs tm_cs $ -- See Note [Type and Term Equality Propagation]
705 dsGRHSs ctxt upats grhss rhs_ty
706 ; return (EqnInfo { eqn_pats = upats, eqn_rhs = match_result}) }
707
708 strictify dflags pat =
709 let (is_strict, pat') = getUnBangedLPat dflags pat
710 in if is_strict then BangPat pat' else unLoc pat'
711
712 handleWarnings = if isGenerated origin
713 then discardWarningsDs
714 else id
715
716
717 matchEquations :: HsMatchContext Name
718 -> [Id] -> [EquationInfo] -> Type
719 -> DsM CoreExpr
720 matchEquations ctxt vars eqns_info rhs_ty
721 = do { let error_doc = matchContextErrString ctxt
722
723 ; match_result <- match vars rhs_ty eqns_info
724
725 ; fail_expr <- mkErrorAppDs pAT_ERROR_ID rhs_ty error_doc
726 ; extractMatchResult match_result fail_expr }
727
728 {-
729 ************************************************************************
730 * *
731 \subsection[matchSimply]{@matchSimply@: match a single expression against a single pattern}
732 * *
733 ************************************************************************
734
735 @mkSimpleMatch@ is a wrapper for @match@ which deals with the
736 situation where we want to match a single expression against a single
737 pattern. It returns an expression.
738 -}
739
740 matchSimply :: CoreExpr -- Scrutinee
741 -> HsMatchContext Name -- Match kind
742 -> LPat Id -- Pattern it should match
743 -> CoreExpr -- Return this if it matches
744 -> CoreExpr -- Return this if it doesn't
745 -> DsM CoreExpr
746 -- Do not warn about incomplete patterns; see matchSinglePat comments
747 matchSimply scrut hs_ctx pat result_expr fail_expr = do
748 let
749 match_result = cantFailMatchResult result_expr
750 rhs_ty = exprType fail_expr
751 -- Use exprType of fail_expr, because won't refine in the case of failure!
752 match_result' <- matchSinglePat scrut hs_ctx pat rhs_ty match_result
753 extractMatchResult match_result' fail_expr
754
755 matchSinglePat :: CoreExpr -> HsMatchContext Name -> LPat Id
756 -> Type -> MatchResult -> DsM MatchResult
757 -- Do not warn about incomplete patterns
758 -- Used for things like [ e | pat <- stuff ], where
759 -- incomplete patterns are just fine
760 matchSinglePat (Var var) ctx (L _ pat) ty match_result
761 = do { dflags <- getDynFlags
762 ; locn <- getSrcSpanDs
763
764 -- pattern match check warnings
765 ; dsPmWarn dflags (DsMatchContext ctx locn) (checkSingle var pat)
766
767 ; match [var] ty
768 [EqnInfo { eqn_pats = [pat], eqn_rhs = match_result }] }
769
770 matchSinglePat scrut hs_ctx pat ty match_result
771 = do { var <- selectSimpleMatchVarL pat
772 ; match_result' <- matchSinglePat (Var var) hs_ctx pat ty match_result
773 ; return (adjustMatchResult (bindNonRec var scrut) match_result') }
774
775 {-
776 ************************************************************************
777 * *
778 Pattern classification
779 * *
780 ************************************************************************
781 -}
782
783 data PatGroup
784 = PgAny -- Immediate match: variables, wildcards,
785 -- lazy patterns
786 | PgCon DataCon -- Constructor patterns (incl list, tuple)
787 | PgSyn PatSyn
788 | PgLit Literal -- Literal patterns
789 | PgN Literal -- Overloaded literals
790 | PgNpK Literal -- n+k patterns
791 | PgBang -- Bang patterns
792 | PgCo Type -- Coercion patterns; the type is the type
793 -- of the pattern *inside*
794 | PgView (LHsExpr Id) -- view pattern (e -> p):
795 -- the LHsExpr is the expression e
796 Type -- the Type is the type of p (equivalently, the result type of e)
797 | PgOverloadedList
798
799 groupEquations :: DynFlags -> [EquationInfo] -> [[(PatGroup, EquationInfo)]]
800 -- If the result is of form [g1, g2, g3],
801 -- (a) all the (pg,eq) pairs in g1 have the same pg
802 -- (b) none of the gi are empty
803 -- The ordering of equations is unchanged
804 groupEquations dflags eqns
805 = runs same_gp [(patGroup dflags (firstPat eqn), eqn) | eqn <- eqns]
806 where
807 same_gp :: (PatGroup,EquationInfo) -> (PatGroup,EquationInfo) -> Bool
808 (pg1,_) `same_gp` (pg2,_) = pg1 `sameGroup` pg2
809
810 subGroup :: Ord a => [(a, EquationInfo)] -> [[EquationInfo]]
811 -- Input is a particular group. The result sub-groups the
812 -- equations by with particular constructor, literal etc they match.
813 -- Each sub-list in the result has the same PatGroup
814 -- See Note [Take care with pattern order]
815 subGroup group
816 = map reverse $ Map.elems $ foldl accumulate Map.empty group
817 where
818 accumulate pg_map (pg, eqn)
819 = case Map.lookup pg pg_map of
820 Just eqns -> Map.insert pg (eqn:eqns) pg_map
821 Nothing -> Map.insert pg [eqn] pg_map
822
823 -- pg_map :: Map a [EquationInfo]
824 -- Equations seen so far in reverse order of appearance
825
826 {-
827 Note [Take care with pattern order]
828 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
829 In the subGroup function we must be very careful about pattern re-ordering,
830 Consider the patterns [ (True, Nothing), (False, x), (True, y) ]
831 Then in bringing together the patterns for True, we must not
832 swap the Nothing and y!
833 -}
834
835 sameGroup :: PatGroup -> PatGroup -> Bool
836 -- Same group means that a single case expression
837 -- or test will suffice to match both, *and* the order
838 -- of testing within the group is insignificant.
839 sameGroup PgAny PgAny = True
840 sameGroup PgBang PgBang = True
841 sameGroup (PgCon _) (PgCon _) = True -- One case expression
842 sameGroup (PgSyn p1) (PgSyn p2) = p1==p2
843 sameGroup (PgLit _) (PgLit _) = True -- One case expression
844 sameGroup (PgN l1) (PgN l2) = l1==l2 -- Order is significant
845 sameGroup (PgNpK l1) (PgNpK l2) = l1==l2 -- See Note [Grouping overloaded literal patterns]
846 sameGroup (PgCo t1) (PgCo t2) = t1 `eqType` t2
847 -- CoPats are in the same goup only if the type of the
848 -- enclosed pattern is the same. The patterns outside the CoPat
849 -- always have the same type, so this boils down to saying that
850 -- the two coercions are identical.
851 sameGroup (PgView e1 t1) (PgView e2 t2) = viewLExprEq (e1,t1) (e2,t2)
852 -- ViewPats are in the same group iff the expressions
853 -- are "equal"---conservatively, we use syntactic equality
854 sameGroup _ _ = False
855
856 -- An approximation of syntactic equality used for determining when view
857 -- exprs are in the same group.
858 -- This function can always safely return false;
859 -- but doing so will result in the application of the view function being repeated.
860 --
861 -- Currently: compare applications of literals and variables
862 -- and anything else that we can do without involving other
863 -- HsSyn types in the recursion
864 --
865 -- NB we can't assume that the two view expressions have the same type. Consider
866 -- f (e1 -> True) = ...
867 -- f (e2 -> "hi") = ...
868 viewLExprEq :: (LHsExpr Id,Type) -> (LHsExpr Id,Type) -> Bool
869 viewLExprEq (e1,_) (e2,_) = lexp e1 e2
870 where
871 lexp :: LHsExpr Id -> LHsExpr Id -> Bool
872 lexp e e' = exp (unLoc e) (unLoc e')
873
874 ---------
875 exp :: HsExpr Id -> HsExpr Id -> Bool
876 -- real comparison is on HsExpr's
877 -- strip parens
878 exp (HsPar (L _ e)) e' = exp e e'
879 exp e (HsPar (L _ e')) = exp e e'
880 -- because the expressions do not necessarily have the same type,
881 -- we have to compare the wrappers
882 exp (HsWrap h e) (HsWrap h' e') = wrap h h' && exp e e'
883 exp (HsVar i) (HsVar i') = i == i'
884 -- the instance for IPName derives using the id, so this works if the
885 -- above does
886 exp (HsIPVar i) (HsIPVar i') = i == i'
887 exp (HsOverLabel l) (HsOverLabel l') = l == l'
888 exp (HsOverLit l) (HsOverLit l') =
889 -- Overloaded lits are equal if they have the same type
890 -- and the data is the same.
891 -- this is coarser than comparing the SyntaxExpr's in l and l',
892 -- which resolve the overloading (e.g., fromInteger 1),
893 -- because these expressions get written as a bunch of different variables
894 -- (presumably to improve sharing)
895 eqType (overLitType l) (overLitType l') && l == l'
896 exp (HsApp e1 e2) (HsApp e1' e2') = lexp e1 e1' && lexp e2 e2'
897 -- the fixities have been straightened out by now, so it's safe
898 -- to ignore them?
899 exp (OpApp l o _ ri) (OpApp l' o' _ ri') =
900 lexp l l' && lexp o o' && lexp ri ri'
901 exp (NegApp e n) (NegApp e' n') = lexp e e' && exp n n'
902 exp (SectionL e1 e2) (SectionL e1' e2') =
903 lexp e1 e1' && lexp e2 e2'
904 exp (SectionR e1 e2) (SectionR e1' e2') =
905 lexp e1 e1' && lexp e2 e2'
906 exp (ExplicitTuple es1 _) (ExplicitTuple es2 _) =
907 eq_list tup_arg es1 es2
908 exp (HsIf _ e e1 e2) (HsIf _ e' e1' e2') =
909 lexp e e' && lexp e1 e1' && lexp e2 e2'
910
911 -- Enhancement: could implement equality for more expressions
912 -- if it seems useful
913 -- But no need for HsLit, ExplicitList, ExplicitTuple,
914 -- because they cannot be functions
915 exp _ _ = False
916
917 ---------
918 tup_arg (L _ (Present e1)) (L _ (Present e2)) = lexp e1 e2
919 tup_arg (L _ (Missing t1)) (L _ (Missing t2)) = eqType t1 t2
920 tup_arg _ _ = False
921
922 ---------
923 wrap :: HsWrapper -> HsWrapper -> Bool
924 -- Conservative, in that it demands that wrappers be
925 -- syntactically identical and doesn't look under binders
926 --
927 -- Coarser notions of equality are possible
928 -- (e.g., reassociating compositions,
929 -- equating different ways of writing a coercion)
930 wrap WpHole WpHole = True
931 wrap (WpCompose w1 w2) (WpCompose w1' w2') = wrap w1 w1' && wrap w2 w2'
932 wrap (WpFun w1 w2 _ _) (WpFun w1' w2' _ _) = wrap w1 w1' && wrap w2 w2'
933 wrap (WpCast co) (WpCast co') = co `eq_co` co'
934 wrap (WpEvApp et1) (WpEvApp et2) = et1 `ev_term` et2
935 wrap (WpTyApp t) (WpTyApp t') = eqType t t'
936 -- Enhancement: could implement equality for more wrappers
937 -- if it seems useful (lams and lets)
938 wrap _ _ = False
939
940 ---------
941 ev_term :: EvTerm -> EvTerm -> Bool
942 ev_term (EvId a) (EvId b) = a==b
943 ev_term (EvCoercion a) (EvCoercion b) = a `eq_co` b
944 ev_term _ _ = False
945
946 ---------
947 eq_list :: (a->a->Bool) -> [a] -> [a] -> Bool
948 eq_list _ [] [] = True
949 eq_list _ [] (_:_) = False
950 eq_list _ (_:_) [] = False
951 eq_list eq (x:xs) (y:ys) = eq x y && eq_list eq xs ys
952
953 ---------
954 eq_co :: TcCoercion -> TcCoercion -> Bool
955 -- Just some simple cases (should the r1 == r2 rather be an ASSERT?)
956 eq_co (TcRefl r1 t1) (TcRefl r2 t2) = r1 == r2 && eqType t1 t2
957 eq_co (TcCoVarCo v1) (TcCoVarCo v2) = v1==v2
958 eq_co (TcSymCo co1) (TcSymCo co2) = co1 `eq_co` co2
959 eq_co (TcTyConAppCo r1 tc1 cos1) (TcTyConAppCo r2 tc2 cos2) = r1 == r2 && tc1==tc2 && eq_list eq_co cos1 cos2
960 eq_co _ _ = False
961
962 patGroup :: DynFlags -> Pat Id -> PatGroup
963 patGroup _ (WildPat {}) = PgAny
964 patGroup _ (BangPat {}) = PgBang
965 patGroup _ (ConPatOut { pat_con = con }) = case unLoc con of
966 RealDataCon dcon -> PgCon dcon
967 PatSynCon psyn -> PgSyn psyn
968 patGroup dflags (LitPat lit) = PgLit (hsLitKey dflags lit)
969 patGroup _ (NPat (L _ olit) mb_neg _)
970 = PgN (hsOverLitKey olit (isJust mb_neg))
971 patGroup _ (NPlusKPat _ (L _ olit) _ _) = PgNpK (hsOverLitKey olit False)
972 patGroup _ (CoPat _ p _) = PgCo (hsPatType p) -- Type of innelexp pattern
973 patGroup _ (ViewPat expr p _) = PgView expr (hsPatType (unLoc p))
974 patGroup _ (ListPat _ _ (Just _)) = PgOverloadedList
975 patGroup _ pat = pprPanic "patGroup" (ppr pat)
976
977 {-
978 Note [Grouping overloaded literal patterns]
979 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
980 WATCH OUT! Consider
981
982 f (n+1) = ...
983 f (n+2) = ...
984 f (n+1) = ...
985
986 We can't group the first and third together, because the second may match
987 the same thing as the first. Same goes for *overloaded* literal patterns
988 f 1 True = ...
989 f 2 False = ...
990 f 1 False = ...
991 If the first arg matches '1' but the second does not match 'True', we
992 cannot jump to the third equation! Because the same argument might
993 match '2'!
994 Hence we don't regard 1 and 2, or (n+1) and (n+2), as part of the same group.
995 -}