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