2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
9 module Match ( match, matchEquations, matchWrapper, matchSimply, matchSinglePat ) where
11 #include "HsVersions.h"
40 This function is a wrapper of @match@, it must be called from all the parts where
41 it was called match, but only substitutes the firs call, ....
42 if the associated flags are declared, warnings will be issued.
43 It can not be called matchWrapper because this name already exists :-(
48 matchCheck :: DsMatchContext
49 -> [Id] -- Vars rep'ing the exprs we're matching with
50 -> Type -- Type of the case expression
51 -> [EquationInfo] -- Info about patterns, etc. (type synonym below)
52 -> DsM MatchResult -- Desugared result!
54 matchCheck ctx vars ty qs
55 = getDOptsDs `thenDs` \ dflags ->
56 matchCheck_really dflags ctx vars ty qs
58 matchCheck_really dflags ctx vars ty qs
59 | incomplete && shadow =
60 dsShadowWarn ctx eqns_shadow `thenDs` \ () ->
61 dsIncompleteWarn ctx pats `thenDs` \ () ->
64 dsIncompleteWarn ctx pats `thenDs` \ () ->
67 dsShadowWarn ctx eqns_shadow `thenDs` \ () ->
71 where (pats, eqns_shadow) = check qs
72 incomplete = want_incomplete && (notNull pats)
73 want_incomplete = case ctx of
74 DsMatchContext RecUpd _ ->
75 dopt Opt_WarnIncompletePatternsRecUpd dflags
77 dopt Opt_WarnIncompletePatterns dflags
78 shadow = dopt Opt_WarnOverlappingPatterns dflags
79 && not (null eqns_shadow)
82 This variable shows the maximum number of lines of output generated for warnings.
83 It will limit the number of patterns/equations displayed to@ maximum_output@.
85 (ToDo: add command-line option?)
91 The next two functions create the warning message.
94 dsShadowWarn :: DsMatchContext -> [EquationInfo] -> DsM ()
95 dsShadowWarn ctx@(DsMatchContext kind loc) qs
96 = putSrcSpanDs loc (warnDs warn)
98 warn | qs `lengthExceeds` maximum_output
99 = pp_context ctx (ptext SLIT("are overlapped"))
100 (\ f -> vcat (map (ppr_eqn f kind) (take maximum_output qs)) $$
103 = pp_context ctx (ptext SLIT("are overlapped"))
104 (\ f -> vcat $ map (ppr_eqn f kind) qs)
107 dsIncompleteWarn :: DsMatchContext -> [ExhaustivePat] -> DsM ()
108 dsIncompleteWarn ctx@(DsMatchContext kind loc) pats
109 = putSrcSpanDs loc (warnDs warn)
111 warn = pp_context ctx (ptext SLIT("are non-exhaustive"))
112 (\f -> hang (ptext SLIT("Patterns not matched:"))
113 4 ((vcat $ map (ppr_incomplete_pats kind)
114 (take maximum_output pats))
117 dots | pats `lengthExceeds` maximum_output = ptext SLIT("...")
120 pp_context (DsMatchContext kind _loc) msg rest_of_msg_fun
121 = vcat [ptext SLIT("Pattern match(es)") <+> msg,
122 sep [ptext SLIT("In") <+> ppr_match <> char ':', nest 4 (rest_of_msg_fun pref)]]
126 FunRhs fun -> (pprMatchContext kind, \ pp -> ppr fun <+> pp)
127 other -> (pprMatchContext kind, \ pp -> pp)
129 ppr_pats pats = sep (map ppr pats)
131 ppr_shadow_pats kind pats
132 = sep [ppr_pats pats, matchSeparator kind, ptext SLIT("...")]
134 ppr_incomplete_pats kind (pats,[]) = ppr_pats pats
135 ppr_incomplete_pats kind (pats,constraints) =
136 sep [ppr_pats pats, ptext SLIT("with"),
137 sep (map ppr_constraint constraints)]
140 ppr_constraint (var,pats) = sep [ppr var, ptext SLIT("`notElem`"), ppr pats]
142 ppr_eqn prefixF kind eqn = prefixF (ppr_shadow_pats kind (eqn_pats eqn))
146 %************************************************************************
148 The main matching function
150 %************************************************************************
152 The function @match@ is basically the same as in the Wadler chapter,
153 except it is monadised, to carry around the name supply, info about
156 Notes on @match@'s arguments, assuming $m$ equations and $n$ patterns:
159 A list of $n$ variable names, those variables presumably bound to the
160 $n$ expressions being matched against the $n$ patterns. Using the
161 list of $n$ expressions as the first argument showed no benefit and
165 The second argument, a list giving the ``equation info'' for each of
169 the $n$ patterns for that equation, and
171 a list of Core bindings [@(Id, CoreExpr)@ pairs] to be ``stuck on
172 the front'' of the matching code, as in:
178 and finally: (ToDo: fill in)
180 The right way to think about the ``after-match function'' is that it
181 is an embryonic @CoreExpr@ with a ``hole'' at the end for the
182 final ``else expression''.
185 There is a type synonym, @EquationInfo@, defined in module @DsUtils@.
187 An experiment with re-ordering this information about equations (in
188 particular, having the patterns available in column-major order)
192 A default expression---what to evaluate if the overall pattern-match
193 fails. This expression will (almost?) always be
194 a measly expression @Var@, unless we know it will only be used once
195 (as we do in @glue_success_exprs@).
197 Leaving out this third argument to @match@ (and slamming in lots of
198 @Var "fail"@s) is a positively {\em bad} idea, because it makes it
199 impossible to share the default expressions. (Also, it stands no
200 chance of working in our post-upheaval world of @Locals@.)
203 Note: @match@ is often called via @matchWrapper@ (end of this module),
204 a function that does much of the house-keeping that goes with a call
207 It is also worth mentioning the {\em typical} way a block of equations
208 is desugared with @match@. At each stage, it is the first column of
209 patterns that is examined. The steps carried out are roughly:
212 Tidy the patterns in column~1 with @tidyEqnInfo@ (this may add
213 bindings to the second component of the equation-info):
216 Remove the `as' patterns from column~1.
218 Make all constructor patterns in column~1 into @ConPats@, notably
219 @ListPats@ and @TuplePats@.
221 Handle any irrefutable (or ``twiddle'') @LazyPats@.
224 Now {\em unmix} the equations into {\em blocks} [w/ local function
225 @unmix_eqns@], in which the equations in a block all have variable
226 patterns in column~1, or they all have constructor patterns in ...
227 (see ``the mixture rule'' in SLPJ).
229 Call @matchEqnBlock@ on each block of equations; it will do the
230 appropriate thing for each kind of column-1 pattern, usually ending up
231 in a recursive call to @match@.
234 We are a little more paranoid about the ``empty rule'' (SLPJ, p.~87)
235 than the Wadler-chapter code for @match@ (p.~93, first @match@ clause).
236 And gluing the ``success expressions'' together isn't quite so pretty.
238 This (more interesting) clause of @match@ uses @tidy_and_unmix_eqns@
239 (a)~to get `as'- and `twiddle'-patterns out of the way (tidying), and
240 (b)~to do ``the mixture rule'' (SLPJ, p.~88) [which really {\em
241 un}mixes the equations], producing a list of equation-info
242 blocks, each block having as its first column of patterns either all
243 constructors, or all variables (or similar beasts), etc.
245 @match_unmixed_eqn_blks@ simply takes the place of the @foldr@ in the
246 Wadler-chapter @match@ (p.~93, last clause), and @match_unmixed_blk@
247 corresponds roughly to @matchVarCon@.
250 match :: [Id] -- Variables rep'ing the exprs we're matching with
251 -> Type -- Type of the case expression
252 -> [EquationInfo] -- Info about patterns, etc. (type synonym below)
253 -> DsM MatchResult -- Desugared result!
256 = ASSERT2( not (null eqns), ppr ty )
257 returnDs (foldr1 combineMatchResults match_results)
259 match_results = [ ASSERT( null (eqn_pats eqn) )
263 match vars@(v:_) ty eqns
264 = ASSERT( not (null eqns ) )
265 do { -- Tidy the first pattern, generating
266 -- auxiliary bindings if necessary
267 (aux_binds, tidy_eqns) <- mapAndUnzipM (tidyEqnInfo v) eqns
269 -- Group the equations and match each group in turn
270 ; match_results <- mapM match_group (groupEquations tidy_eqns)
272 ; return (adjustMatchResult (foldr1 (.) aux_binds) $
273 foldr1 combineMatchResults match_results) }
275 dropGroup :: [(PatGroup,EquationInfo)] -> [EquationInfo]
278 match_group :: [(PatGroup,EquationInfo)] -> DsM MatchResult
279 match_group eqns@((group,_) : _)
281 PgAny -> matchVariables vars ty (dropGroup eqns)
282 PgCon _ -> matchConFamily vars ty (subGroups eqns)
283 PgLit _ -> matchLiterals vars ty (subGroups eqns)
284 PgN lit -> matchNPats vars ty (subGroups eqns)
285 PgNpK lit -> matchNPlusKPats vars ty (dropGroup eqns)
286 PgBang -> matchBangs vars ty (dropGroup eqns)
287 PgCo _ -> matchCoercion vars ty (dropGroup eqns)
289 matchVariables :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
290 -- Real true variables, just like in matchVar, SLPJ p 94
291 -- No binding to do: they'll all be wildcards by now (done in tidy)
292 matchVariables (var:vars) ty eqns = match vars ty (shiftEqns eqns)
294 matchBangs :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
295 matchBangs (var:vars) ty eqns
296 = do { match_result <- match (var:vars) ty (map shift eqns)
297 ; return (mkEvalMatchResult var ty match_result) }
299 shift eqn@(EqnInfo { eqn_pats = BangPat pat : pats })
300 = eqn { eqn_pats = unLoc pat : pats }
302 matchCoercion :: [Id] -> Type -> [EquationInfo] -> DsM MatchResult
303 -- Apply the coercion to the match variable and then match that
304 matchCoercion (var:vars) ty (eqn1:eqns)
305 = do { let CoPat co pat _ = firstPat eqn1
306 ; var' <- newUniqueId (idName var) (hsPatType pat)
307 ; match_result <- match (var':vars) ty (map shift (eqn1:eqns))
308 ; rhs <- dsCoercion co (return (Var var))
309 ; return (mkCoLetMatchResult (NonRec var' rhs) match_result) }
311 shift eqn@(EqnInfo { eqn_pats = CoPat _ pat _ : pats })
312 = eqn { eqn_pats = pat : pats }
315 %************************************************************************
319 %************************************************************************
321 Tidy up the leftmost pattern in an @EquationInfo@, given the variable @v@
322 which will be scrutinised. This means:
325 Replace variable patterns @x@ (@x /= v@) with the pattern @_@,
326 together with the binding @x = v@.
328 Replace the `as' pattern @x@@p@ with the pattern p and a binding @x = v@.
330 Removing lazy (irrefutable) patterns (you don't want to know...).
332 Converting explicit tuple-, list-, and parallel-array-pats into ordinary
335 Convert the literal pat "" to [].
338 The result of this tidying is that the column of patterns will include
342 The @VarPat@ information isn't needed any more after this.
345 @ListPats@, @TuplePats@, etc., are all converted into @ConPats@.
347 \item[@LitPats@ and @NPats@:]
348 @LitPats@/@NPats@ of ``known friendly types'' (Int, Char,
349 Float, Double, at least) are converted to unboxed form; e.g.,
350 \tr{(NPat (HsInt i) _ _)} is converted to:
352 (ConPat I# _ _ [LitPat (HsIntPrim i)])
357 tidyEqnInfo :: Id -> EquationInfo
358 -> DsM (DsWrapper, EquationInfo)
359 -- DsM'd because of internal call to dsLHsBinds
360 -- and mkSelectorBinds.
361 -- "tidy1" does the interesting stuff, looking at
362 -- one pattern and fiddling the list of bindings.
364 -- POST CONDITION: head pattern in the EqnInfo is
372 tidyEqnInfo v eqn@(EqnInfo { eqn_pats = pat : pats })
373 = tidy1 v pat `thenDs` \ (wrap, pat') ->
374 returnDs (wrap, eqn { eqn_pats = pat' : pats })
376 tidy1 :: Id -- The Id being scrutinised
377 -> Pat Id -- The pattern against which it is to be matched
378 -> DsM (DsWrapper, -- Extra bindings to do before the match
379 Pat Id) -- Equivalent pattern
381 -------------------------------------------------------
382 -- (pat', mr') = tidy1 v pat mr
383 -- tidies the *outer level only* of pat, giving pat'
384 -- It eliminates many pattern forms (as-patterns, variable patterns,
385 -- list patterns, etc) yielding one of:
392 tidy1 v (ParPat pat) = tidy1 v (unLoc pat)
393 tidy1 v (SigPatOut pat _) = tidy1 v (unLoc pat)
394 tidy1 v (WildPat ty) = returnDs (idDsWrapper, WildPat ty)
396 -- case v of { x -> mr[] }
397 -- = case v of { _ -> let x=v in mr[] }
399 = returnDs (wrapBind var v, WildPat (idType var))
401 tidy1 v (VarPatOut var binds)
402 = do { prs <- dsLHsBinds binds
403 ; return (wrapBind var v . mkDsLet (Rec prs),
404 WildPat (idType var)) }
406 -- case v of { x@p -> mr[] }
407 -- = case v of { p -> let x=v in mr[] }
408 tidy1 v (AsPat (L _ var) pat)
409 = do { (wrap, pat') <- tidy1 v (unLoc pat)
410 ; return (wrapBind var v . wrap, pat') }
412 {- now, here we handle lazy patterns:
413 tidy1 v ~p bs = (v, v1 = case v of p -> v1 :
414 v2 = case v of p -> v2 : ... : bs )
416 where the v_i's are the binders in the pattern.
418 ToDo: in "v_i = ... -> v_i", are the v_i's really the same thing?
420 The case expr for v_i is just: match [v] [(p, [], \ x -> Var v_i)] any_expr
423 tidy1 v (LazyPat pat)
424 = do { sel_prs <- mkSelectorBinds pat (Var v)
425 ; let sel_binds = [NonRec b rhs | (b,rhs) <- sel_prs]
426 ; returnDs (mkDsLets sel_binds, WildPat (idType v)) }
428 tidy1 v (ListPat pats ty)
429 = returnDs (idDsWrapper, unLoc list_ConPat)
431 list_ty = mkListTy ty
432 list_ConPat = foldr (\ x y -> mkPrefixConPat consDataCon [x, y] list_ty)
436 -- Introduce fake parallel array constructors to be able to handle parallel
437 -- arrays with the existing machinery for constructor pattern
438 tidy1 v (PArrPat pats ty)
439 = returnDs (idDsWrapper, unLoc parrConPat)
442 parrConPat = mkPrefixConPat (parrFakeCon arity) pats (mkPArrTy ty)
444 tidy1 v (TuplePat pats boxity ty)
445 = returnDs (idDsWrapper, unLoc tuple_ConPat)
448 tuple_ConPat = mkPrefixConPat (tupleCon boxity arity) pats ty
450 tidy1 v (DictPat dicts methods)
451 = case num_of_d_and_ms of
452 0 -> tidy1 v (TuplePat [] Boxed unitTy)
453 1 -> tidy1 v (unLoc (head dict_and_method_pats))
454 _ -> tidy1 v (mkVanillaTuplePat dict_and_method_pats Boxed)
456 num_of_d_and_ms = length dicts + length methods
457 dict_and_method_pats = map nlVarPat (dicts ++ methods)
459 -- LitPats: we *might* be able to replace these w/ a simpler form
461 = returnDs (idDsWrapper, tidyLitPat lit)
463 -- NPats: we *might* be able to replace these w/ a simpler form
464 tidy1 v (NPat lit mb_neg eq lit_ty)
465 = returnDs (idDsWrapper, tidyNPat lit mb_neg eq lit_ty)
467 -- Everything else goes through unchanged...
469 tidy1 v non_interesting_pat
470 = returnDs (idDsWrapper, non_interesting_pat)
474 {\bf Previous @matchTwiddled@ stuff:}
476 Now we get to the only interesting part; note: there are choices for
477 translation [from Simon's notes]; translation~1:
484 s = case w of [s,t] -> s
485 t = case w of [s,t] -> t
489 Here \tr{w} is a fresh variable, and the \tr{w}-binding prevents multiple
490 evaluation of \tr{e}. An alternative translation (No.~2):
492 [ w = case e of [s,t] -> (s,t)
493 s = case w of (s,t) -> s
494 t = case w of (s,t) -> t
498 %************************************************************************
500 \subsubsection[improved-unmixing]{UNIMPLEMENTED idea for improved unmixing}
502 %************************************************************************
504 We might be able to optimise unmixing when confronted by
505 only-one-constructor-possible, of which tuples are the most notable
513 This definition would normally be unmixed into four equation blocks,
514 one per equation. But it could be unmixed into just one equation
515 block, because if the one equation matches (on the first column),
516 the others certainly will.
518 You have to be careful, though; the example
526 {\em must} be broken into two blocks at the line shown; otherwise, you
527 are forcing unnecessary evaluation. In any case, the top-left pattern
528 always gives the cue. You could then unmix blocks into groups of...
530 \item[all variables:]
532 \item[constructors or variables (mixed):]
533 Need to make sure the right names get bound for the variable patterns.
534 \item[literals or variables (mixed):]
535 Presumably just a variant on the constructor case (as it is now).
538 %************************************************************************
540 %* matchWrapper: a convenient way to call @match@ *
542 %************************************************************************
543 \subsection[matchWrapper]{@matchWrapper@: a convenient interface to @match@}
545 Calls to @match@ often involve similar (non-trivial) work; that work
546 is collected here, in @matchWrapper@. This function takes as
550 Typchecked @Matches@ (of a function definition, or a case or lambda
551 expression)---the main input;
553 An error message to be inserted into any (runtime) pattern-matching
557 As results, @matchWrapper@ produces:
560 A list of variables (@Locals@) that the caller must ``promise'' to
561 bind to appropriate values; and
563 a @CoreExpr@, the desugared output (main result).
566 The main actions of @matchWrapper@ include:
569 Flatten the @[TypecheckedMatch]@ into a suitable list of
572 Create as many new variables as there are patterns in a pattern-list
573 (in any one of the @EquationInfo@s).
575 Create a suitable ``if it fails'' expression---a call to @error@ using
576 the error-string input; the {\em type} of this fail value can be found
577 by examining one of the RHS expressions in one of the @EquationInfo@s.
579 Call @match@ with all of this information!
583 matchWrapper :: HsMatchContext Name -- For shadowing warning messages
584 -> MatchGroup Id -- Matches being desugared
585 -> DsM ([Id], CoreExpr) -- Results
588 There is one small problem with the Lambda Patterns, when somebody
589 writes something similar to:
593 he/she don't want a warning about incomplete patterns, that is done with
594 the flag @opt_WarnSimplePatterns@.
595 This problem also appears in the:
597 \item @do@ patterns, but if the @do@ can fail
598 it creates another equation if the match can fail
599 (see @DsExpr.doDo@ function)
600 \item @let@ patterns, are treated by @matchSimply@
601 List Comprension Patterns, are treated by @matchSimply@ also
604 We can't call @matchSimply@ with Lambda patterns,
605 due to the fact that lambda patterns can have more than
606 one pattern, and match simply only accepts one pattern.
611 matchWrapper ctxt (MatchGroup matches match_ty)
612 = ASSERT( notNull matches )
613 do { eqns_info <- mapM mk_eqn_info matches
614 ; new_vars <- selectMatchVars arg_pats
615 ; result_expr <- matchEquations ctxt new_vars eqns_info rhs_ty
616 ; return (new_vars, result_expr) }
618 arg_pats = map unLoc (hsLMatchPats (head matches))
619 n_pats = length arg_pats
620 (_, rhs_ty) = splitFunTysN n_pats match_ty
622 mk_eqn_info (L _ (Match pats _ grhss))
623 = do { let upats = map unLoc pats
624 ; match_result <- dsGRHSs ctxt upats grhss rhs_ty
625 ; return (EqnInfo { eqn_pats = upats, eqn_rhs = match_result}) }
628 matchEquations :: HsMatchContext Name
629 -> [Id] -> [EquationInfo] -> Type
631 matchEquations ctxt vars eqns_info rhs_ty
632 = do { dflags <- getDOptsDs
633 ; locn <- getSrcSpanDs
634 ; let ds_ctxt = DsMatchContext ctxt locn
635 error_string = matchContextErrString ctxt
637 ; match_result <- match_fun dflags ds_ctxt vars rhs_ty eqns_info
639 ; fail_expr <- mkErrorAppDs pAT_ERROR_ID rhs_ty error_string
640 ; extractMatchResult match_result fail_expr }
642 match_fun dflags ds_ctxt
644 LambdaExpr | dopt Opt_WarnSimplePatterns dflags -> matchCheck ds_ctxt
646 _ -> matchCheck ds_ctxt
649 %************************************************************************
651 \subsection[matchSimply]{@matchSimply@: match a single expression against a single pattern}
653 %************************************************************************
655 @mkSimpleMatch@ is a wrapper for @match@ which deals with the
656 situation where we want to match a single expression against a single
657 pattern. It returns an expression.
660 matchSimply :: CoreExpr -- Scrutinee
661 -> HsMatchContext Name -- Match kind
662 -> LPat Id -- Pattern it should match
663 -> CoreExpr -- Return this if it matches
664 -> CoreExpr -- Return this if it doesn't
667 matchSimply scrut hs_ctx pat result_expr fail_expr
669 match_result = cantFailMatchResult result_expr
670 rhs_ty = exprType fail_expr
671 -- Use exprType of fail_expr, because won't refine in the case of failure!
673 matchSinglePat scrut hs_ctx pat rhs_ty match_result `thenDs` \ match_result' ->
674 extractMatchResult match_result' fail_expr
677 matchSinglePat :: CoreExpr -> HsMatchContext Name -> LPat Id
678 -> Type -> MatchResult -> DsM MatchResult
679 matchSinglePat (Var var) hs_ctx (L _ pat) ty match_result
680 = getDOptsDs `thenDs` \ dflags ->
681 getSrcSpanDs `thenDs` \ locn ->
684 | dopt Opt_WarnSimplePatterns dflags = matchCheck ds_ctx
687 ds_ctx = DsMatchContext hs_ctx locn
689 match_fn dflags [var] ty [EqnInfo { eqn_pats = [pat], eqn_rhs = match_result }]
691 matchSinglePat scrut hs_ctx pat ty match_result
692 = selectSimpleMatchVarL pat `thenDs` \ var ->
693 matchSinglePat (Var var) hs_ctx pat ty match_result `thenDs` \ match_result' ->
694 returnDs (adjustMatchResult (bindNonRec var scrut) match_result')
698 %************************************************************************
700 Pattern classification
702 %************************************************************************
706 = PgAny -- Immediate match: variables, wildcards,
708 | PgCon DataCon -- Constructor patterns (incl list, tuple)
709 | PgLit Literal -- Literal patterns
710 | PgN Literal -- Overloaded literals
711 | PgNpK Literal -- n+k patterns
712 | PgBang -- Bang patterns
713 | PgCo Type -- Coercion patterns; the type is the type
714 -- of the pattern *inside*
717 groupEquations :: [EquationInfo] -> [[(PatGroup, EquationInfo)]]
718 -- If the result is of form [g1, g2, g3],
719 -- (a) all the (pg,eq) pairs in g1 have the same pg
720 -- (b) none of the gi are empty
722 = runs same_gp [(patGroup (firstPat eqn), eqn) | eqn <- eqns]
724 same_gp :: (PatGroup,EquationInfo) -> (PatGroup,EquationInfo) -> Bool
725 (pg1,_) `same_gp` (pg2,_) = pg1 `sameGroup` pg2
727 subGroups :: [(PatGroup, EquationInfo)] -> [[EquationInfo]]
728 -- Input is a particular group. The result sub-groups the
729 -- equations by with particular constructor, literal etc they match.
730 -- The order may be swizzled, so the matching should be order-independent
731 subGroups groups = map (map snd) (equivClasses cmp groups)
733 (pg1, _) `cmp` (pg2, _) = pg1 `cmp_pg` pg2
734 (PgCon c1) `cmp_pg` (PgCon c2) = c1 `compare` c2
735 (PgLit l1) `cmp_pg` (PgLit l2) = l1 `compare` l2
736 (PgN l1) `cmp_pg` (PgN l2) = l1 `compare` l2
737 -- These are the only cases that are every sub-grouped
739 sameGroup :: PatGroup -> PatGroup -> Bool
740 -- Same group means that a single case expression
741 -- or test will suffice to match both, *and* the order
742 -- of testing within the group is insignificant.
743 sameGroup PgAny PgAny = True
744 sameGroup PgBang PgBang = True
745 sameGroup (PgCon _) (PgCon _) = True -- One case expression
746 sameGroup (PgLit _) (PgLit _) = True -- One case expression
747 sameGroup (PgN l1) (PgN l2) = True -- Needs conditionals
748 sameGroup (PgNpK l1) (PgNpK l2) = l1==l2 -- Order is significant
749 -- See Note [Order of n+k]
750 sameGroup (PgCo t1) (PgCo t2) = t1 `coreEqType` t2
751 -- CoPats are in the same goup only if the type of the
752 -- enclosed pattern is the same. The patterns outside the CoPat
753 -- always have the same type, so this boils down to saying that
754 -- the two coercions are identical.
755 sameGroup _ _ = False
757 patGroup :: Pat Id -> PatGroup
758 patGroup (WildPat {}) = PgAny
759 patGroup (BangPat {}) = PgBang
760 patGroup (ConPatOut { pat_con = dc }) = PgCon (unLoc dc)
761 patGroup (LitPat lit) = PgLit (hsLitKey lit)
762 patGroup (NPat olit mb_neg _ _) = PgN (hsOverLitKey olit (isJust mb_neg))
763 patGroup (NPlusKPat _ olit _ _) = PgNpK (hsOverLitKey olit False)
764 patGroup (CoPat _ p _) = PgCo (hsPatType p) -- Type of inner pattern
765 patGroup pat = pprPanic "patGroup" (ppr pat)
776 We can't group the first and third together, because the second may match
777 the same thing as the first. Contrast
781 where we can group the first and third. Hence we don't regard (n+1) and
782 (n+2) as part of the same group.