{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998


The @match@ function
-}

{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}

module Match ( match, matchEquations, matchWrapper, matchSimply
             , matchSinglePat, matchSinglePatVar ) where

#include "HsVersions.h"

import GhcPrelude

import {-#SOURCE#-} DsExpr (dsLExpr, dsSyntaxExpr)

import BasicTypes ( Origin(..) )
import DynFlags
import HsSyn
import TcHsSyn
import TcEvidence
import TcRnMonad
import Check
import CoreSyn
import Literal
import CoreUtils
import MkCore
import DsMonad
import DsBinds
import DsGRHSs
import DsUtils
import Id
import ConLike
import DataCon
import PatSyn
import MatchCon
import MatchLit
import Type
import Coercion ( eqCoercion )
import TyCon( isNewTyCon )
import TysWiredIn
import SrcLoc
import Maybes
import Util
import Name
import Outputable
import BasicTypes ( isGenerated, il_value, fl_value )
import FastString
import Unique
import UniqDFM

import Control.Monad( when, unless )
import Data.List ( groupBy )
import qualified Data.Map as Map

{-
************************************************************************
*                                                                      *
                The main matching function
*                                                                      *
************************************************************************

The function @match@ is basically the same as in the Wadler chapter
from "The Implementation of Functional Programming Languages",
except it is monadised, to carry around the name supply, info about
annotations, etc.

Notes on @match@'s arguments, assuming $m$ equations and $n$ patterns:
\begin{enumerate}
\item
A list of $n$ variable names, those variables presumably bound to the
$n$ expressions being matched against the $n$ patterns.  Using the
list of $n$ expressions as the first argument showed no benefit and
some inelegance.

\item
The second argument, a list giving the ``equation info'' for each of
the $m$ equations:
\begin{itemize}
\item
the $n$ patterns for that equation, and
\item
a list of Core bindings [@(Id, CoreExpr)@ pairs] to be ``stuck on
the front'' of the matching code, as in:
\begin{verbatim}
let <binds>
in  <matching-code>
\end{verbatim}
\item
and finally: (ToDo: fill in)

The right way to think about the ``after-match function'' is that it
is an embryonic @CoreExpr@ with a ``hole'' at the end for the
final ``else expression''.
\end{itemize}

There is a data type, @EquationInfo@, defined in module @DsMonad@.

An experiment with re-ordering this information about equations (in
particular, having the patterns available in column-major order)
showed no benefit.

\item
A default expression---what to evaluate if the overall pattern-match
fails.  This expression will (almost?) always be
a measly expression @Var@, unless we know it will only be used once
(as we do in @glue_success_exprs@).

Leaving out this third argument to @match@ (and slamming in lots of
@Var "fail"@s) is a positively {\em bad} idea, because it makes it
impossible to share the default expressions.  (Also, it stands no
chance of working in our post-upheaval world of @Locals@.)
\end{enumerate}

Note: @match@ is often called via @matchWrapper@ (end of this module),
a function that does much of the house-keeping that goes with a call
to @match@.

It is also worth mentioning the {\em typical} way a block of equations
is desugared with @match@.  At each stage, it is the first column of
patterns that is examined.  The steps carried out are roughly:
\begin{enumerate}
\item
Tidy the patterns in column~1 with @tidyEqnInfo@ (this may add
bindings to the second component of the equation-info):
\item
Now {\em unmix} the equations into {\em blocks} [w\/ local function
@match_groups@], in which the equations in a block all have the same
 match group.
(see ``the mixture rule'' in SLPJ).
\item
Call the right match variant on each block of equations; it will do the
appropriate thing for each kind of column-1 pattern.
\end{enumerate}

We are a little more paranoid about the ``empty rule'' (SLPJ, p.~87)
than the Wadler-chapter code for @match@ (p.~93, first @match@ clause).
And gluing the ``success expressions'' together isn't quite so pretty.

This  @match@ uses @tidyEqnInfo@
to get `as'- and `twiddle'-patterns out of the way (tidying), before
applying ``the mixture rule'' (SLPJ, p.~88) [which really {\em
un}mixes the equations], producing a list of equation-info
blocks, each block having as its first column patterns compatible with each other.

Note [Match Ids]
~~~~~~~~~~~~~~~~
Most of the matching functions take an Id or [Id] as argument.  This Id
is the scrutinee(s) of the match. The desugared expression may
sometimes use that Id in a local binding or as a case binder.  So it
should not have an External name; Lint rejects non-top-level binders
with External names (Trac #13043).

See also Note [Localise pattern binders] in DsUtils
-}

type MatchId = Id   -- See Note [Match Ids]

match :: [MatchId]        -- ^ Variables rep\'ing the exprs we\'re matching with
                          -- ^ See Note [Match Ids]
      -> Type             -- ^ Type of the case expression
      -> [EquationInfo]   -- ^ Info about patterns, etc. (type synonym below)
      -> DsM MatchResult  -- ^ Desugared result!

match :: [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
match [] ty :: Type
ty eqns :: [EquationInfo]
eqns
  = ASSERT2( not (null eqns), ppr ty )
    MatchResult -> DsM MatchResult
forall (m :: * -> *) a. Monad m => a -> m a
return ((MatchResult -> MatchResult -> MatchResult)
-> [MatchResult] -> MatchResult
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 MatchResult -> MatchResult -> MatchResult
combineMatchResults [MatchResult]
match_results)
  where
    match_results :: [MatchResult]
match_results = [ ASSERT( null (eqn_pats eqn) )
                      EquationInfo -> MatchResult
eqn_rhs EquationInfo
eqn
                    | EquationInfo
eqn <- [EquationInfo]
eqns ]

match vars :: [MatchId]
vars@(v :: MatchId
v:_) ty :: Type
ty eqns :: [EquationInfo]
eqns    -- Eqns *can* be empty
  = ASSERT2( all (isInternalName . idName) vars, ppr vars )
    do  { DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
                -- Tidy the first pattern, generating
                -- auxiliary bindings if necessary
        ; (aux_binds :: [DsWrapper]
aux_binds, tidy_eqns :: [EquationInfo]
tidy_eqns) <- (EquationInfo
 -> IOEnv (Env DsGblEnv DsLclEnv) (DsWrapper, EquationInfo))
-> [EquationInfo]
-> IOEnv (Env DsGblEnv DsLclEnv) ([DsWrapper], [EquationInfo])
forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM (MatchId
-> EquationInfo
-> IOEnv (Env DsGblEnv DsLclEnv) (DsWrapper, EquationInfo)
tidyEqnInfo MatchId
v) [EquationInfo]
eqns

                -- Group the equations and match each group in turn
        ; let grouped :: [[(PatGroup, EquationInfo)]]
grouped = DynFlags -> [EquationInfo] -> [[(PatGroup, EquationInfo)]]
groupEquations DynFlags
dflags [EquationInfo]
tidy_eqns

         -- print the view patterns that are commoned up to help debug
        ; DumpFlag
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall gbl lcl. DumpFlag -> TcRnIf gbl lcl () -> TcRnIf gbl lcl ()
whenDOptM DumpFlag
Opt_D_dump_view_pattern_commoning ([[(PatGroup, EquationInfo)]] -> TcRnIf DsGblEnv DsLclEnv ()
forall (t :: * -> *) b.
Foldable t =>
[t (PatGroup, b)] -> TcRnIf DsGblEnv DsLclEnv ()
debug [[(PatGroup, EquationInfo)]]
grouped)

        ; [MatchResult]
match_results <- [[(PatGroup, EquationInfo)]] -> DsM [MatchResult]
match_groups [[(PatGroup, EquationInfo)]]
grouped
        ; MatchResult -> DsM MatchResult
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper -> MatchResult -> MatchResult
adjustMatchResult ((DsWrapper -> DsWrapper -> DsWrapper)
-> DsWrapper -> [DsWrapper] -> DsWrapper
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr DsWrapper -> DsWrapper -> DsWrapper
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) DsWrapper
forall a. a -> a
id [DsWrapper]
aux_binds) (MatchResult -> MatchResult) -> MatchResult -> MatchResult
forall a b. (a -> b) -> a -> b
$
                  (MatchResult -> MatchResult -> MatchResult)
-> [MatchResult] -> MatchResult
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 MatchResult -> MatchResult -> MatchResult
combineMatchResults [MatchResult]
match_results) }
  where
    dropGroup :: [(PatGroup,EquationInfo)] -> [EquationInfo]
    dropGroup :: [(PatGroup, EquationInfo)] -> [EquationInfo]
dropGroup = ((PatGroup, EquationInfo) -> EquationInfo)
-> [(PatGroup, EquationInfo)] -> [EquationInfo]
forall a b. (a -> b) -> [a] -> [b]
map (PatGroup, EquationInfo) -> EquationInfo
forall a b. (a, b) -> b
snd

    match_groups :: [[(PatGroup,EquationInfo)]] -> DsM [MatchResult]
    -- Result list of [MatchResult] is always non-empty
    match_groups :: [[(PatGroup, EquationInfo)]] -> DsM [MatchResult]
match_groups [] = MatchId -> Type -> DsM [MatchResult]
matchEmpty MatchId
v Type
ty
    match_groups gs :: [[(PatGroup, EquationInfo)]]
gs = ([(PatGroup, EquationInfo)] -> DsM MatchResult)
-> [[(PatGroup, EquationInfo)]] -> DsM [MatchResult]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM [(PatGroup, EquationInfo)] -> DsM MatchResult
match_group [[(PatGroup, EquationInfo)]]
gs

    match_group :: [(PatGroup,EquationInfo)] -> DsM MatchResult
    match_group :: [(PatGroup, EquationInfo)] -> DsM MatchResult
match_group [] = String -> DsM MatchResult
forall a. String -> a
panic "match_group"
    match_group eqns :: [(PatGroup, EquationInfo)]
eqns@((group :: PatGroup
group,_) : _)
        = case PatGroup
group of
            PgCon {}  -> [MatchId] -> Type -> [[EquationInfo]] -> DsM MatchResult
matchConFamily  [MatchId]
vars Type
ty ([(DataCon, EquationInfo)] -> [[EquationInfo]]
forall a. Uniquable a => [(a, EquationInfo)] -> [[EquationInfo]]
subGroupUniq [(DataCon
c,EquationInfo
e) | (PgCon c :: DataCon
c, e :: EquationInfo
e) <- [(PatGroup, EquationInfo)]
eqns])
            PgSyn {}  -> [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchPatSyn     [MatchId]
vars Type
ty ([(PatGroup, EquationInfo)] -> [EquationInfo]
dropGroup [(PatGroup, EquationInfo)]
eqns)
            PgLit {}  -> [MatchId] -> Type -> [[EquationInfo]] -> DsM MatchResult
matchLiterals   [MatchId]
vars Type
ty ([(Literal, EquationInfo)] -> [[EquationInfo]]
forall a. Ord a => [(a, EquationInfo)] -> [[EquationInfo]]
subGroupOrd [(Literal
l,EquationInfo
e) | (PgLit l :: Literal
l, e :: EquationInfo
e) <- [(PatGroup, EquationInfo)]
eqns])
            PgAny     -> [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchVariables  [MatchId]
vars Type
ty ([(PatGroup, EquationInfo)] -> [EquationInfo]
dropGroup [(PatGroup, EquationInfo)]
eqns)
            PgN {}    -> [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchNPats      [MatchId]
vars Type
ty ([(PatGroup, EquationInfo)] -> [EquationInfo]
dropGroup [(PatGroup, EquationInfo)]
eqns)
            PgOverS {}-> [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchNPats      [MatchId]
vars Type
ty ([(PatGroup, EquationInfo)] -> [EquationInfo]
dropGroup [(PatGroup, EquationInfo)]
eqns)
            PgNpK {}  -> [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchNPlusKPats [MatchId]
vars Type
ty ([(PatGroup, EquationInfo)] -> [EquationInfo]
dropGroup [(PatGroup, EquationInfo)]
eqns)
            PgBang    -> [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchBangs      [MatchId]
vars Type
ty ([(PatGroup, EquationInfo)] -> [EquationInfo]
dropGroup [(PatGroup, EquationInfo)]
eqns)
            PgCo {}   -> [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchCoercion   [MatchId]
vars Type
ty ([(PatGroup, EquationInfo)] -> [EquationInfo]
dropGroup [(PatGroup, EquationInfo)]
eqns)
            PgView {} -> [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchView       [MatchId]
vars Type
ty ([(PatGroup, EquationInfo)] -> [EquationInfo]
dropGroup [(PatGroup, EquationInfo)]
eqns)
            PgOverloadedList -> [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchOverloadedList [MatchId]
vars Type
ty ([(PatGroup, EquationInfo)] -> [EquationInfo]
dropGroup [(PatGroup, EquationInfo)]
eqns)

    -- FIXME: we should also warn about view patterns that should be
    -- commoned up but are not

    -- print some stuff to see what's getting grouped
    -- use -dppr-debug to see the resolution of overloaded literals
    debug :: [t (PatGroup, b)] -> TcRnIf DsGblEnv DsLclEnv ()
debug eqns :: [t (PatGroup, b)]
eqns =
        let gs :: [[LHsExpr GhcTc]]
gs = (t (PatGroup, b) -> [LHsExpr GhcTc])
-> [t (PatGroup, b)] -> [[LHsExpr GhcTc]]
forall a b. (a -> b) -> [a] -> [b]
map (\group :: t (PatGroup, b)
group -> ((PatGroup, b) -> [LHsExpr GhcTc] -> [LHsExpr GhcTc])
-> [LHsExpr GhcTc] -> t (PatGroup, b) -> [LHsExpr GhcTc]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ (p :: PatGroup
p,_) -> \acc :: [LHsExpr GhcTc]
acc ->
                                           case PatGroup
p of PgView e :: LHsExpr GhcTc
e _ -> LHsExpr GhcTc
eLHsExpr GhcTc -> [LHsExpr GhcTc] -> [LHsExpr GhcTc]
forall a. a -> [a] -> [a]
:[LHsExpr GhcTc]
acc
                                                     _ -> [LHsExpr GhcTc]
acc) [] t (PatGroup, b)
group) [t (PatGroup, b)]
eqns
            maybeWarn :: [SDoc] -> TcRnIf DsGblEnv DsLclEnv ()
maybeWarn [] = () -> TcRnIf DsGblEnv DsLclEnv ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            maybeWarn l :: [SDoc]
l = WarnReason -> SDoc -> TcRnIf DsGblEnv DsLclEnv ()
warnDs WarnReason
NoReason ([SDoc] -> SDoc
vcat [SDoc]
l)
        in
          [SDoc] -> TcRnIf DsGblEnv DsLclEnv ()
maybeWarn ([SDoc] -> TcRnIf DsGblEnv DsLclEnv ())
-> [SDoc] -> TcRnIf DsGblEnv DsLclEnv ()
forall a b. (a -> b) -> a -> b
$ (([LHsExpr GhcTc] -> SDoc) -> [[LHsExpr GhcTc]] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (\g :: [LHsExpr GhcTc]
g -> String -> SDoc
text "Putting these view expressions into the same case:" SDoc -> SDoc -> SDoc
<+> ([LHsExpr GhcTc] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [LHsExpr GhcTc]
g))
                       (([LHsExpr GhcTc] -> Bool) -> [[LHsExpr GhcTc]] -> [[LHsExpr GhcTc]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> ([LHsExpr GhcTc] -> Bool) -> [LHsExpr GhcTc] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [LHsExpr GhcTc] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) [[LHsExpr GhcTc]]
gs))

matchEmpty :: MatchId -> Type -> DsM [MatchResult]
-- See Note [Empty case expressions]
matchEmpty :: MatchId -> Type -> DsM [MatchResult]
matchEmpty var :: MatchId
var res_ty :: Type
res_ty
  = [MatchResult] -> DsM [MatchResult]
forall (m :: * -> *) a. Monad m => a -> m a
return [CanItFail -> (CoreExpr -> DsM CoreExpr) -> MatchResult
MatchResult CanItFail
CanFail CoreExpr -> DsM CoreExpr
mk_seq]
  where
    mk_seq :: CoreExpr -> DsM CoreExpr
mk_seq fail :: CoreExpr
fail = CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> DsM CoreExpr) -> CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ CoreExpr -> Type -> Type -> [CoreAlt] -> CoreExpr
mkWildCase (MatchId -> CoreExpr
forall b. MatchId -> Expr b
Var MatchId
var) (MatchId -> Type
idType MatchId
var) Type
res_ty
                                      [(AltCon
DEFAULT, [], CoreExpr
fail)]

matchVariables :: [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
-- Real true variables, just like in matchVar, SLPJ p 94
-- No binding to do: they'll all be wildcards by now (done in tidy)
matchVariables :: [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchVariables (_:vars :: [MatchId]
vars) ty :: Type
ty eqns :: [EquationInfo]
eqns = [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
match [MatchId]
vars Type
ty ([EquationInfo] -> [EquationInfo]
shiftEqns [EquationInfo]
eqns)
matchVariables [] _ _ = String -> DsM MatchResult
forall a. String -> a
panic "matchVariables"

matchBangs :: [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchBangs :: [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchBangs (var :: MatchId
var:vars :: [MatchId]
vars) ty :: Type
ty eqns :: [EquationInfo]
eqns
  = do  { MatchResult
match_result <- [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
match (MatchId
varMatchId -> [MatchId] -> [MatchId]
forall a. a -> [a] -> [a]
:[MatchId]
vars) Type
ty ([EquationInfo] -> DsM MatchResult)
-> [EquationInfo] -> DsM MatchResult
forall a b. (a -> b) -> a -> b
$
                          (EquationInfo -> EquationInfo) -> [EquationInfo] -> [EquationInfo]
forall a b. (a -> b) -> [a] -> [b]
map ((Pat GhcTc -> Pat GhcTc) -> EquationInfo -> EquationInfo
decomposeFirstPat Pat GhcTc -> Pat GhcTc
getBangPat) [EquationInfo]
eqns
        ; MatchResult -> DsM MatchResult
forall (m :: * -> *) a. Monad m => a -> m a
return (MatchId -> Type -> MatchResult -> MatchResult
mkEvalMatchResult MatchId
var Type
ty MatchResult
match_result) }
matchBangs [] _ _ = String -> DsM MatchResult
forall a. String -> a
panic "matchBangs"

matchCoercion :: [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
-- Apply the coercion to the match variable and then match that
matchCoercion :: [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchCoercion (var :: MatchId
var:vars :: [MatchId]
vars) ty :: Type
ty (eqns :: [EquationInfo]
eqns@(eqn1 :: EquationInfo
eqn1:_))
  = do  { let CoPat _ co :: HsWrapper
co pat :: Pat GhcTc
pat _ = EquationInfo -> Pat GhcTc
firstPat EquationInfo
eqn1
        ; let pat_ty' :: Type
pat_ty' = Pat GhcTc -> Type
hsPatType Pat GhcTc
pat
        ; MatchId
var' <- MatchId -> Type -> DsM MatchId
newUniqueId MatchId
var Type
pat_ty'
        ; MatchResult
match_result <- [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
match (MatchId
var'MatchId -> [MatchId] -> [MatchId]
forall a. a -> [a] -> [a]
:[MatchId]
vars) Type
ty ([EquationInfo] -> DsM MatchResult)
-> [EquationInfo] -> DsM MatchResult
forall a b. (a -> b) -> a -> b
$
                          (EquationInfo -> EquationInfo) -> [EquationInfo] -> [EquationInfo]
forall a b. (a -> b) -> [a] -> [b]
map ((Pat GhcTc -> Pat GhcTc) -> EquationInfo -> EquationInfo
decomposeFirstPat Pat GhcTc -> Pat GhcTc
getCoPat) [EquationInfo]
eqns
        ; DsWrapper
core_wrap <- HsWrapper -> DsM DsWrapper
dsHsWrapper HsWrapper
co
        ; let bind :: Bind MatchId
bind = MatchId -> CoreExpr -> Bind MatchId
forall b. b -> Expr b -> Bind b
NonRec MatchId
var' (DsWrapper
core_wrap (MatchId -> CoreExpr
forall b. MatchId -> Expr b
Var MatchId
var))
        ; MatchResult -> DsM MatchResult
forall (m :: * -> *) a. Monad m => a -> m a
return (Bind MatchId -> MatchResult -> MatchResult
mkCoLetMatchResult Bind MatchId
bind MatchResult
match_result) }
matchCoercion _ _ _ = String -> DsM MatchResult
forall a. String -> a
panic "matchCoercion"

matchView :: [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
-- Apply the view function to the match variable and then match that
matchView :: [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchView (var :: MatchId
var:vars :: [MatchId]
vars) ty :: Type
ty (eqns :: [EquationInfo]
eqns@(eqn1 :: EquationInfo
eqn1:_))
  = do  { -- we could pass in the expr from the PgView,
         -- but this needs to extract the pat anyway
         -- to figure out the type of the fresh variable
         let ViewPat _ viewExpr :: LHsExpr GhcTc
viewExpr (Pat GhcTc -> Located (SrcSpanLess (Pat GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ pat :: SrcSpanLess (Pat GhcTc)
pat) = EquationInfo -> Pat GhcTc
firstPat EquationInfo
eqn1
         -- do the rest of the compilation
        ; let pat_ty' :: Type
pat_ty' = Pat GhcTc -> Type
hsPatType SrcSpanLess (Pat GhcTc)
Pat GhcTc
pat
        ; MatchId
var' <- MatchId -> Type -> DsM MatchId
newUniqueId MatchId
var Type
pat_ty'
        ; MatchResult
match_result <- [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
match (MatchId
var'MatchId -> [MatchId] -> [MatchId]
forall a. a -> [a] -> [a]
:[MatchId]
vars) Type
ty ([EquationInfo] -> DsM MatchResult)
-> [EquationInfo] -> DsM MatchResult
forall a b. (a -> b) -> a -> b
$
                          (EquationInfo -> EquationInfo) -> [EquationInfo] -> [EquationInfo]
forall a b. (a -> b) -> [a] -> [b]
map ((Pat GhcTc -> Pat GhcTc) -> EquationInfo -> EquationInfo
decomposeFirstPat Pat GhcTc -> Pat GhcTc
getViewPat) [EquationInfo]
eqns
         -- compile the view expressions
        ; CoreExpr
viewExpr' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
viewExpr
        ; MatchResult -> DsM MatchResult
forall (m :: * -> *) a. Monad m => a -> m a
return (MatchId -> CoreExpr -> MatchResult -> MatchResult
mkViewMatchResult MatchId
var'
                    (SDoc -> CoreExpr -> DsWrapper
mkCoreAppDs (String -> SDoc
text "matchView") CoreExpr
viewExpr' (MatchId -> CoreExpr
forall b. MatchId -> Expr b
Var MatchId
var))
                    MatchResult
match_result) }
matchView _ _ _ = String -> DsM MatchResult
forall a. String -> a
panic "matchView"

matchOverloadedList :: [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchOverloadedList :: [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
matchOverloadedList (var :: MatchId
var:vars :: [MatchId]
vars) ty :: Type
ty (eqns :: [EquationInfo]
eqns@(eqn1 :: EquationInfo
eqn1:_))
-- Since overloaded list patterns are treated as view patterns,
-- the code is roughly the same as for matchView
  = do { let ListPat (ListPatTc elt_ty (Just (_,e))) _ = EquationInfo -> Pat GhcTc
firstPat EquationInfo
eqn1
       ; MatchId
var' <- MatchId -> Type -> DsM MatchId
newUniqueId MatchId
var (Type -> Type
mkListTy Type
elt_ty)  -- we construct the overall type by hand
       ; MatchResult
match_result <- [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
match (MatchId
var'MatchId -> [MatchId] -> [MatchId]
forall a. a -> [a] -> [a]
:[MatchId]
vars) Type
ty ([EquationInfo] -> DsM MatchResult)
-> [EquationInfo] -> DsM MatchResult
forall a b. (a -> b) -> a -> b
$
                            (EquationInfo -> EquationInfo) -> [EquationInfo] -> [EquationInfo]
forall a b. (a -> b) -> [a] -> [b]
map ((Pat GhcTc -> Pat GhcTc) -> EquationInfo -> EquationInfo
decomposeFirstPat Pat GhcTc -> Pat GhcTc
getOLPat) [EquationInfo]
eqns -- getOLPat builds the pattern inside as a non-overloaded version of the overloaded list pattern
       ; CoreExpr
e' <- SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
e [MatchId -> CoreExpr
forall b. MatchId -> Expr b
Var MatchId
var]
       ; MatchResult -> DsM MatchResult
forall (m :: * -> *) a. Monad m => a -> m a
return (MatchId -> CoreExpr -> MatchResult -> MatchResult
mkViewMatchResult MatchId
var' CoreExpr
e' MatchResult
match_result) }
matchOverloadedList _ _ _ = String -> DsM MatchResult
forall a. String -> a
panic "matchOverloadedList"

-- decompose the first pattern and leave the rest alone
decomposeFirstPat :: (Pat GhcTc -> Pat GhcTc) -> EquationInfo -> EquationInfo
decomposeFirstPat :: (Pat GhcTc -> Pat GhcTc) -> EquationInfo -> EquationInfo
decomposeFirstPat extractpat :: Pat GhcTc -> Pat GhcTc
extractpat (eqn :: EquationInfo
eqn@(EqnInfo { eqn_pats :: EquationInfo -> [Pat GhcTc]
eqn_pats = pat :: Pat GhcTc
pat : pats :: [Pat GhcTc]
pats }))
        = EquationInfo
eqn { eqn_pats :: [Pat GhcTc]
eqn_pats = Pat GhcTc -> Pat GhcTc
extractpat Pat GhcTc
pat Pat GhcTc -> [Pat GhcTc] -> [Pat GhcTc]
forall a. a -> [a] -> [a]
: [Pat GhcTc]
pats}
decomposeFirstPat _ _ = String -> EquationInfo
forall a. String -> a
panic "decomposeFirstPat"

getCoPat, getBangPat, getViewPat, getOLPat :: Pat GhcTc -> Pat GhcTc
getCoPat :: Pat GhcTc -> Pat GhcTc
getCoPat (CoPat _ _ pat :: Pat GhcTc
pat _)   = Pat GhcTc
pat
getCoPat _                   = String -> Pat GhcTc
forall a. String -> a
panic "getCoPat"
getBangPat :: Pat GhcTc -> Pat GhcTc
getBangPat (BangPat _ pat :: Pat GhcTc
pat  ) = Pat GhcTc -> SrcSpanLess (Pat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Pat GhcTc
pat
getBangPat _                 = String -> Pat GhcTc
forall a. String -> a
panic "getBangPat"
getViewPat :: Pat GhcTc -> Pat GhcTc
getViewPat (ViewPat _ _ pat :: Pat GhcTc
pat) = Pat GhcTc -> SrcSpanLess (Pat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Pat GhcTc
pat
getViewPat _                 = String -> Pat GhcTc
forall a. String -> a
panic "getViewPat"
getOLPat :: Pat GhcTc -> Pat GhcTc
getOLPat (ListPat (ListPatTc ty (Just _)) pats :: [Pat GhcTc]
pats)
        = XListPat GhcTc -> [Pat GhcTc] -> Pat GhcTc
forall p. XListPat p -> [LPat p] -> LPat p
ListPat (Type -> Maybe (Type, SyntaxExpr GhcTc) -> ListPatTc
ListPatTc Type
ty Maybe (Type, SyntaxExpr GhcTc)
forall a. Maybe a
Nothing)  [Pat GhcTc]
pats
getOLPat _                   = String -> Pat GhcTc
forall a. String -> a
panic "getOLPat"

{-
Note [Empty case alternatives]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The list of EquationInfo can be empty, arising from
    case x of {}   or    \case {}
In that situation we desugar to
    case x of { _ -> error "pattern match failure" }
The *desugarer* isn't certain whether there really should be no
alternatives, so it adds a default case, as it always does.  A later
pass may remove it if it's inaccessible.  (See also Note [Empty case
alternatives] in CoreSyn.)

We do *not* desugar simply to
   error "empty case"
or some such, because 'x' might be bound to (error "hello"), in which
case we want to see that "hello" exception, not (error "empty case").
See also Note [Case elimination: lifted case] in Simplify.


************************************************************************
*                                                                      *
                Tidying patterns
*                                                                      *
************************************************************************

Tidy up the leftmost pattern in an @EquationInfo@, given the variable @v@
which will be scrutinised.

This makes desugaring the pattern match simpler by transforming some of
the patterns to simpler forms. (Tuples to Constructor Patterns)

Among other things in the resulting Pattern:
* Variables and irrefutable(lazy) patterns are replaced by Wildcards
* As patterns are replaced by the patterns they wrap.

The bindings created by the above patterns are put into the returned wrapper
instead.

This means a definition of the form:
  f x = rhs
when called with v get's desugared to the equivalent of:
  let x = v
  in
  f _ = rhs

The same principle holds for as patterns (@) and
irrefutable/lazy patterns (~).
In the case of irrefutable patterns the irrefutable pattern is pushed into
the binding.

Pattern Constructors which only represent syntactic sugar are converted into
their desugared representation.
This usually means converting them to Constructor patterns but for some
depends on enabled extensions. (Eg OverloadedLists)

GHC also tries to convert overloaded Literals into regular ones.

The result of this tidying is that the column of patterns will include
only these which can be assigned a PatternGroup (see patGroup).

-}

tidyEqnInfo :: Id -> EquationInfo
            -> DsM (DsWrapper, EquationInfo)
        -- DsM'd because of internal call to dsLHsBinds
        --      and mkSelectorBinds.
        -- "tidy1" does the interesting stuff, looking at
        -- one pattern and fiddling the list of bindings.
        --
        -- POST CONDITION: head pattern in the EqnInfo is
        --      one of these for which patGroup is defined.

tidyEqnInfo :: MatchId
-> EquationInfo
-> IOEnv (Env DsGblEnv DsLclEnv) (DsWrapper, EquationInfo)
tidyEqnInfo _ (EqnInfo { eqn_pats :: EquationInfo -> [Pat GhcTc]
eqn_pats = [] })
  = String -> IOEnv (Env DsGblEnv DsLclEnv) (DsWrapper, EquationInfo)
forall a. String -> a
panic "tidyEqnInfo"

tidyEqnInfo v :: MatchId
v eqn :: EquationInfo
eqn@(EqnInfo { eqn_pats :: EquationInfo -> [Pat GhcTc]
eqn_pats = pat :: Pat GhcTc
pat : pats :: [Pat GhcTc]
pats, eqn_orig :: EquationInfo -> Origin
eqn_orig = Origin
orig })
  = do { (wrap :: DsWrapper
wrap, pat' :: Pat GhcTc
pat') <- MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
orig Pat GhcTc
pat
       ; (DsWrapper, EquationInfo)
-> IOEnv (Env DsGblEnv DsLclEnv) (DsWrapper, EquationInfo)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
wrap, EquationInfo
eqn { eqn_pats :: [Pat GhcTc]
eqn_pats = do Pat GhcTc
pat' Pat GhcTc -> [Pat GhcTc] -> [Pat GhcTc]
forall a. a -> [a] -> [a]
: [Pat GhcTc]
pats }) }

tidy1 :: Id                  -- The Id being scrutinised
      -> Origin              -- Was this a pattern the user wrote?
      -> Pat GhcTc           -- The pattern against which it is to be matched
      -> DsM (DsWrapper,     -- Extra bindings to do before the match
              Pat GhcTc)     -- Equivalent pattern

-------------------------------------------------------
--      (pat', mr') = tidy1 v pat mr
-- tidies the *outer level only* of pat, giving pat'
-- It eliminates many pattern forms (as-patterns, variable patterns,
-- list patterns, etc) and returns any created bindings in the wrapper.

tidy1 :: MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 v :: MatchId
v o :: Origin
o (ParPat _ pat :: Pat GhcTc
pat)      = MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
o (Pat GhcTc -> SrcSpanLess (Pat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Pat GhcTc
pat)
tidy1 v :: MatchId
v o :: Origin
o (SigPat _ pat :: Pat GhcTc
pat _)    = MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
o (Pat GhcTc -> SrcSpanLess (Pat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Pat GhcTc
pat)
tidy1 _ _ (WildPat ty :: XWildPat GhcTc
ty)        = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, XWildPat GhcTc -> Pat GhcTc
forall p. XWildPat p -> LPat p
WildPat XWildPat GhcTc
ty)
tidy1 v :: MatchId
v o :: Origin
o (BangPat _ (Pat GhcTc -> Located (SrcSpanLess (Pat GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L l :: SrcSpan
l p :: SrcSpanLess (Pat GhcTc)
p)) = MatchId
-> Origin -> SrcSpan -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy_bang_pat MatchId
v Origin
o SrcSpan
l SrcSpanLess (Pat GhcTc)
Pat GhcTc
p

        -- case v of { x -> mr[] }
        -- = case v of { _ -> let x=v in mr[] }
tidy1 v :: MatchId
v _ (VarPat _ (Located (IdP GhcTc) -> Located (SrcSpanLess (Located MatchId))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ var :: SrcSpanLess (Located MatchId)
var))
  = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (MatchId -> MatchId -> DsWrapper
wrapBind SrcSpanLess (Located MatchId)
MatchId
var MatchId
v, XWildPat GhcTc -> Pat GhcTc
forall p. XWildPat p -> LPat p
WildPat (MatchId -> Type
idType SrcSpanLess (Located MatchId)
MatchId
var))

        -- case v of { x@p -> mr[] }
        -- = case v of { p -> let x=v in mr[] }
tidy1 v :: MatchId
v o :: Origin
o (AsPat _ (Located (IdP GhcTc) -> Located (SrcSpanLess (Located MatchId))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ var :: SrcSpanLess (Located MatchId)
var) pat :: Pat GhcTc
pat)
  = do  { (wrap :: DsWrapper
wrap, pat' :: Pat GhcTc
pat') <- MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
o (Pat GhcTc -> SrcSpanLess (Pat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Pat GhcTc
pat)
        ; (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (MatchId -> MatchId -> DsWrapper
wrapBind SrcSpanLess (Located MatchId)
MatchId
var MatchId
v DsWrapper -> DsWrapper -> DsWrapper
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DsWrapper
wrap, Pat GhcTc
pat') }

{- now, here we handle lazy patterns:
    tidy1 v ~p bs = (v, v1 = case v of p -> v1 :
                        v2 = case v of p -> v2 : ... : bs )

    where the v_i's are the binders in the pattern.

    ToDo: in "v_i = ... -> v_i", are the v_i's really the same thing?

    The case expr for v_i is just: match [v] [(p, [], \ x -> Var v_i)] any_expr
-}

tidy1 v :: MatchId
v _ (LazyPat _ pat :: Pat GhcTc
pat)
    -- This is a convenient place to check for unlifted types under a lazy pattern.
    -- Doing this check during type-checking is unsatisfactory because we may
    -- not fully know the zonked types yet. We sure do here.
  = do  { let unlifted_bndrs :: [MatchId]
unlifted_bndrs = (MatchId -> Bool) -> [MatchId] -> [MatchId]
forall a. (a -> Bool) -> [a] -> [a]
filter (HasDebugCallStack => Type -> Bool
Type -> Bool
isUnliftedType (Type -> Bool) -> (MatchId -> Type) -> MatchId -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatchId -> Type
idType) (Pat GhcTc -> [IdP GhcTc]
forall (p :: Pass). LPat (GhcPass p) -> [IdP (GhcPass p)]
collectPatBinders Pat GhcTc
pat)
        ; Bool -> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([MatchId] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [MatchId]
unlifted_bndrs) (TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ())
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall a b. (a -> b) -> a -> b
$
          SrcSpan
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs (Pat GhcTc -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
getLoc Pat GhcTc
pat) (TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ())
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall a b. (a -> b) -> a -> b
$
          SDoc -> TcRnIf DsGblEnv DsLclEnv ()
errDs (SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text "A lazy (~) pattern cannot bind variables of unlifted type." SDoc -> SDoc -> SDoc
$$
                       String -> SDoc
text "Unlifted variables:")
                    2 ([SDoc] -> SDoc
vcat ((MatchId -> SDoc) -> [MatchId] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (\id :: MatchId
id -> MatchId -> SDoc
forall a. Outputable a => a -> SDoc
ppr MatchId
id SDoc -> SDoc -> SDoc
<+> SDoc
dcolon SDoc -> SDoc -> SDoc
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr (MatchId -> Type
idType MatchId
id))
                                 [MatchId]
unlifted_bndrs)))

        ; (_,sel_prs :: [(MatchId, CoreExpr)]
sel_prs) <- [[Tickish MatchId]]
-> Pat GhcTc -> CoreExpr -> DsM (MatchId, [(MatchId, CoreExpr)])
mkSelectorBinds [] Pat GhcTc
pat (MatchId -> CoreExpr
forall b. MatchId -> Expr b
Var MatchId
v)
        ; let sel_binds :: [Bind MatchId]
sel_binds =  [MatchId -> CoreExpr -> Bind MatchId
forall b. b -> Expr b -> Bind b
NonRec MatchId
b CoreExpr
rhs | (b :: MatchId
b,rhs :: CoreExpr
rhs) <- [(MatchId, CoreExpr)]
sel_prs]
        ; (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Bind MatchId] -> DsWrapper
mkCoreLets [Bind MatchId]
sel_binds, XWildPat GhcTc -> Pat GhcTc
forall p. XWildPat p -> LPat p
WildPat (MatchId -> Type
idType MatchId
v)) }

tidy1 _ _ (ListPat (ListPatTc ty Nothing) pats :: [Pat GhcTc]
pats )
  = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, Pat GhcTc -> SrcSpanLess (Pat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Pat GhcTc
list_ConPat)
  where
    list_ConPat :: Pat GhcTc
list_ConPat = (Pat GhcTc -> Pat GhcTc -> Pat GhcTc)
-> Pat GhcTc -> [Pat GhcTc] -> Pat GhcTc
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ x :: Pat GhcTc
x y :: Pat GhcTc
y -> DataCon -> [Pat GhcTc] -> [Type] -> Pat GhcTc
forall (p :: Pass).
DataCon -> [OutPat (GhcPass p)] -> [Type] -> OutPat (GhcPass p)
mkPrefixConPat DataCon
consDataCon [Pat GhcTc
x, Pat GhcTc
y] [Type
ty])
                        (Type -> Pat GhcTc
forall (p :: Pass). Type -> OutPat (GhcPass p)
mkNilPat Type
ty)
                        [Pat GhcTc]
pats

tidy1 _ _ (TuplePat tys :: XTuplePat GhcTc
tys pats :: [Pat GhcTc]
pats boxity :: Boxity
boxity)
  = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, Pat GhcTc -> SrcSpanLess (Pat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Pat GhcTc
tuple_ConPat)
  where
    arity :: Int
arity = [Pat GhcTc] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Pat GhcTc]
pats
    tuple_ConPat :: Pat GhcTc
tuple_ConPat = DataCon -> [Pat GhcTc] -> [Type] -> Pat GhcTc
forall (p :: Pass).
DataCon -> [OutPat (GhcPass p)] -> [Type] -> OutPat (GhcPass p)
mkPrefixConPat (Boxity -> Int -> DataCon
tupleDataCon Boxity
boxity Int
arity) [Pat GhcTc]
pats [Type]
XTuplePat GhcTc
tys

tidy1 _ _ (SumPat tys :: XSumPat GhcTc
tys pat :: Pat GhcTc
pat alt :: Int
alt arity :: Int
arity)
  = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, Pat GhcTc -> SrcSpanLess (Pat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Pat GhcTc
sum_ConPat)
  where
    sum_ConPat :: Pat GhcTc
sum_ConPat = DataCon -> [Pat GhcTc] -> [Type] -> Pat GhcTc
forall (p :: Pass).
DataCon -> [OutPat (GhcPass p)] -> [Type] -> OutPat (GhcPass p)
mkPrefixConPat (Int -> Int -> DataCon
sumDataCon Int
alt Int
arity) [Pat GhcTc
pat] [Type]
XSumPat GhcTc
tys

-- LitPats: we *might* be able to replace these w/ a simpler form
tidy1 _ o :: Origin
o (LitPat _ lit :: HsLit GhcTc
lit)
  = do { Bool -> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Origin -> Bool
isGenerated Origin
o) (TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ())
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall a b. (a -> b) -> a -> b
$
           HsLit GhcTc -> TcRnIf DsGblEnv DsLclEnv ()
warnAboutOverflowedLit HsLit GhcTc
lit
       ; (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, HsLit GhcTc -> Pat GhcTc
tidyLitPat HsLit GhcTc
lit) }

-- NPats: we *might* be able to replace these w/ a simpler form
tidy1 _ o :: Origin
o (NPat ty :: XNPat GhcTc
ty (Located (HsOverLit GhcTc)
-> Located (SrcSpanLess (Located (HsOverLit GhcTc)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ lit :: SrcSpanLess (Located (HsOverLit GhcTc))
lit@OverLit { ol_val = v }) mb_neg :: Maybe (SyntaxExpr GhcTc)
mb_neg eq :: SyntaxExpr GhcTc
eq)
  = do { Bool -> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Origin -> Bool
isGenerated Origin
o) (TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ())
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall a b. (a -> b) -> a -> b
$
           let lit' :: HsOverLit GhcTc
lit' | Just _ <- Maybe (SyntaxExpr GhcTc)
mb_neg = SrcSpanLess (Located (HsOverLit GhcTc))
HsOverLit GhcTc
lit{ ol_val :: OverLitVal
ol_val = OverLitVal -> OverLitVal
negateOverLitVal OverLitVal
v }
                    | Bool
otherwise = SrcSpanLess (Located (HsOverLit GhcTc))
HsOverLit GhcTc
lit
           in HsOverLit GhcTc -> TcRnIf DsGblEnv DsLclEnv ()
warnAboutOverflowedOverLit HsOverLit GhcTc
lit'
       ; (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, HsOverLit GhcTc
-> Maybe (SyntaxExpr GhcTc)
-> SyntaxExpr GhcTc
-> Type
-> Pat GhcTc
tidyNPat SrcSpanLess (Located (HsOverLit GhcTc))
HsOverLit GhcTc
lit Maybe (SyntaxExpr GhcTc)
mb_neg SyntaxExpr GhcTc
eq Type
XNPat GhcTc
ty) }

-- NPlusKPat: we may want to warn about the literals
tidy1 _ o :: Origin
o n :: Pat GhcTc
n@(NPlusKPat _ _ (Located (HsOverLit GhcTc)
-> Located (SrcSpanLess (Located (HsOverLit GhcTc)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ lit1 :: SrcSpanLess (Located (HsOverLit GhcTc))
lit1) lit2 :: HsOverLit GhcTc
lit2 _ _)
  = do { Bool -> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Origin -> Bool
isGenerated Origin
o) (TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ())
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall a b. (a -> b) -> a -> b
$ do
           HsOverLit GhcTc -> TcRnIf DsGblEnv DsLclEnv ()
warnAboutOverflowedOverLit SrcSpanLess (Located (HsOverLit GhcTc))
HsOverLit GhcTc
lit1
           HsOverLit GhcTc -> TcRnIf DsGblEnv DsLclEnv ()
warnAboutOverflowedOverLit HsOverLit GhcTc
lit2
       ; (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, Pat GhcTc
n) }

-- Everything else goes through unchanged...
tidy1 _ _ non_interesting_pat :: Pat GhcTc
non_interesting_pat
  = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, Pat GhcTc
non_interesting_pat)

--------------------
tidy_bang_pat :: Id -> Origin -> SrcSpan -> Pat GhcTc
              -> DsM (DsWrapper, Pat GhcTc)

-- Discard par/sig under a bang
tidy_bang_pat :: MatchId
-> Origin -> SrcSpan -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy_bang_pat v :: MatchId
v o :: Origin
o _ (ParPat _ (Pat GhcTc -> Located (SrcSpanLess (Pat GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L l :: SrcSpan
l p :: SrcSpanLess (Pat GhcTc)
p)) = MatchId
-> Origin -> SrcSpan -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy_bang_pat MatchId
v Origin
o SrcSpan
l SrcSpanLess (Pat GhcTc)
Pat GhcTc
p
tidy_bang_pat v :: MatchId
v o :: Origin
o _ (SigPat _ (Pat GhcTc -> Located (SrcSpanLess (Pat GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L l :: SrcSpan
l p :: SrcSpanLess (Pat GhcTc)
p) _) = MatchId
-> Origin -> SrcSpan -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy_bang_pat MatchId
v Origin
o SrcSpan
l SrcSpanLess (Pat GhcTc)
Pat GhcTc
p

-- Push the bang-pattern inwards, in the hope that
-- it may disappear next time
tidy_bang_pat v :: MatchId
v o :: Origin
o l :: SrcSpan
l (AsPat x :: XAsPat GhcTc
x v' :: Located (IdP GhcTc)
v' p :: Pat GhcTc
p)
  = MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
o (XAsPat GhcTc -> Located (IdP GhcTc) -> Pat GhcTc -> Pat GhcTc
forall p. XAsPat p -> Located (IdP p) -> LPat p -> LPat p
AsPat XAsPat GhcTc
x Located (IdP GhcTc)
v' (SrcSpan -> SrcSpanLess (Pat GhcTc) -> Pat GhcTc
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
l (XBangPat GhcTc -> Pat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> LPat p
BangPat XBangPat GhcTc
NoExt
noExt Pat GhcTc
p)))
tidy_bang_pat v :: MatchId
v o :: Origin
o l :: SrcSpan
l (CoPat x :: XCoPat GhcTc
x w :: HsWrapper
w p :: Pat GhcTc
p t :: Type
t)
  = MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
o (XCoPat GhcTc -> HsWrapper -> Pat GhcTc -> Type -> Pat GhcTc
forall p. XCoPat p -> HsWrapper -> LPat p -> Type -> LPat p
CoPat XCoPat GhcTc
x HsWrapper
w (XBangPat GhcTc -> Pat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> LPat p
BangPat XBangPat GhcTc
NoExt
noExt (SrcSpan -> SrcSpanLess (Pat GhcTc) -> Pat GhcTc
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
l SrcSpanLess (Pat GhcTc)
Pat GhcTc
p)) Type
t)

-- Discard bang around strict pattern
tidy_bang_pat v :: MatchId
v o :: Origin
o _ p :: Pat GhcTc
p@(LitPat {})    = MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
o Pat GhcTc
p
tidy_bang_pat v :: MatchId
v o :: Origin
o _ p :: Pat GhcTc
p@(ListPat {})   = MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
o Pat GhcTc
p
tidy_bang_pat v :: MatchId
v o :: Origin
o _ p :: Pat GhcTc
p@(TuplePat {})  = MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
o Pat GhcTc
p
tidy_bang_pat v :: MatchId
v o :: Origin
o _ p :: Pat GhcTc
p@(SumPat {})    = MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
o Pat GhcTc
p

-- Data/newtype constructors
tidy_bang_pat v :: MatchId
v o :: Origin
o l :: SrcSpan
l p :: Pat GhcTc
p@(ConPatOut { pat_con :: forall p. LPat p -> Located ConLike
pat_con = (Located ConLike -> Located (SrcSpanLess (Located ConLike))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (RealDataCon dc))
                                 , pat_args :: forall p. LPat p -> HsConPatDetails p
pat_args = HsConPatDetails GhcTc
args
                                 , pat_arg_tys :: forall p. LPat p -> [Type]
pat_arg_tys = [Type]
arg_tys })
  -- Newtypes: push bang inwards (Trac #9844)
  =
    if TyCon -> Bool
isNewTyCon (DataCon -> TyCon
dataConTyCon DataCon
dc)
      then MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
o (Pat GhcTc
p { pat_args :: HsConPatDetails GhcTc
pat_args = SrcSpan -> Type -> HsConPatDetails GhcTc -> HsConPatDetails GhcTc
push_bang_into_newtype_arg SrcSpan
l Type
ty HsConPatDetails GhcTc
args })
      else MatchId -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 MatchId
v Origin
o Pat GhcTc
p  -- Data types: discard the bang
    where
      (ty :: Type
ty:_) = DataCon -> [Type] -> [Type]
dataConInstArgTys DataCon
dc [Type]
arg_tys

-------------------
-- Default case, leave the bang there:
--    VarPat,
--    LazyPat,
--    WildPat,
--    ViewPat,
--    pattern synonyms (ConPatOut with PatSynCon)
--    NPat,
--    NPlusKPat
--
-- For LazyPat, remember that it's semantically like a VarPat
--  i.e.  !(~p) is not like ~p, or p!  (Trac #8952)
--
-- NB: SigPatIn, ConPatIn should not happen

tidy_bang_pat _ _ l :: SrcSpan
l p :: Pat GhcTc
p = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, XBangPat GhcTc -> Pat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> LPat p
BangPat XBangPat GhcTc
NoExt
noExt (SrcSpan -> SrcSpanLess (Pat GhcTc) -> Pat GhcTc
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
l SrcSpanLess (Pat GhcTc)
Pat GhcTc
p))

-------------------
push_bang_into_newtype_arg :: SrcSpan
                           -> Type -- The type of the argument we are pushing
                                   -- onto
                           -> HsConPatDetails GhcTc -> HsConPatDetails GhcTc
-- See Note [Bang patterns and newtypes]
-- We are transforming   !(N p)   into   (N !p)
push_bang_into_newtype_arg :: SrcSpan -> Type -> HsConPatDetails GhcTc -> HsConPatDetails GhcTc
push_bang_into_newtype_arg l :: SrcSpan
l _ty :: Type
_ty (PrefixCon (arg :: Pat GhcTc
arg:args :: [Pat GhcTc]
args))
  = ASSERT( null args)
    [Pat GhcTc] -> HsConPatDetails GhcTc
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [SrcSpan -> SrcSpanLess (Pat GhcTc) -> Pat GhcTc
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
l (XBangPat GhcTc -> Pat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> LPat p
BangPat XBangPat GhcTc
NoExt
noExt Pat GhcTc
arg)]
push_bang_into_newtype_arg l :: SrcSpan
l _ty :: Type
_ty (RecCon rf :: HsRecFields GhcTc (Pat GhcTc)
rf)
  | HsRecFields { rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = (LHsRecField GhcTc (Pat GhcTc)
-> Located (SrcSpanLess (LHsRecField GhcTc (Pat GhcTc)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L lf :: SrcSpan
lf fld :: SrcSpanLess (LHsRecField GhcTc (Pat GhcTc))
fld) : flds :: [LHsRecField GhcTc (Pat GhcTc)]
flds } <- HsRecFields GhcTc (Pat GhcTc)
rf
  , HsRecField { hsRecFieldArg = arg } <- SrcSpanLess (LHsRecField GhcTc (Pat GhcTc))
fld
  = ASSERT( null flds)
    HsRecFields GhcTc (Pat GhcTc) -> HsConPatDetails GhcTc
forall arg rec. rec -> HsConDetails arg rec
RecCon (HsRecFields GhcTc (Pat GhcTc)
rf { rec_flds :: [LHsRecField GhcTc (Pat GhcTc)]
rec_flds = [SrcSpan
-> SrcSpanLess (LHsRecField GhcTc (Pat GhcTc))
-> LHsRecField GhcTc (Pat GhcTc)
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
lf (SrcSpanLess (LHsRecField GhcTc (Pat GhcTc))
HsRecField' (FieldOcc GhcTc) (Pat GhcTc)
fld { hsRecFieldArg :: Pat GhcTc
hsRecFieldArg
                                           = SrcSpan -> SrcSpanLess (Pat GhcTc) -> Pat GhcTc
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
l (XBangPat GhcTc -> Pat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> LPat p
BangPat XBangPat GhcTc
NoExt
noExt Pat GhcTc
arg) })] })
push_bang_into_newtype_arg l :: SrcSpan
l ty :: Type
ty (RecCon rf :: HsRecFields GhcTc (Pat GhcTc)
rf) -- If a user writes !(T {})
  | HsRecFields { rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [] } <- HsRecFields GhcTc (Pat GhcTc)
rf
  = [Pat GhcTc] -> HsConPatDetails GhcTc
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [SrcSpan -> SrcSpanLess (Pat GhcTc) -> Pat GhcTc
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
l (XBangPat GhcTc -> Pat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> LPat p
BangPat XBangPat GhcTc
NoExt
noExt (SrcSpanLess (Pat GhcTc) -> Pat GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XWildPat GhcTc -> Pat GhcTc
forall p. XWildPat p -> LPat p
WildPat Type
XWildPat GhcTc
ty)))]
push_bang_into_newtype_arg _ _ cd :: HsConPatDetails GhcTc
cd
  = String -> SDoc -> HsConPatDetails GhcTc
forall a. HasCallStack => String -> SDoc -> a
pprPanic "push_bang_into_newtype_arg" (HsConPatDetails GhcTc -> SDoc
forall (p :: Pass).
OutputableBndrId (GhcPass p) =>
HsConPatDetails (GhcPass p) -> SDoc
pprConArgs HsConPatDetails GhcTc
cd)

{-
Note [Bang patterns and newtypes]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For the pattern  !(Just pat)  we can discard the bang, because
the pattern is strict anyway. But for !(N pat), where
  newtype NT = N Int
we definitely can't discard the bang.  Trac #9844.

So what we do is to push the bang inwards, in the hope that it will
get discarded there.  So we transform
   !(N pat)   into    (N !pat)

But what if there is nothing to push the bang onto? In at least one instance
a user has written !(N {}) which we translate into (N !_). See #13215


\noindent
{\bf Previous @matchTwiddled@ stuff:}

Now we get to the only interesting part; note: there are choices for
translation [from Simon's notes]; translation~1:
\begin{verbatim}
deTwiddle [s,t] e
\end{verbatim}
returns
\begin{verbatim}
[ w = e,
  s = case w of [s,t] -> s
  t = case w of [s,t] -> t
]
\end{verbatim}

Here \tr{w} is a fresh variable, and the \tr{w}-binding prevents multiple
evaluation of \tr{e}.  An alternative translation (No.~2):
\begin{verbatim}
[ w = case e of [s,t] -> (s,t)
  s = case w of (s,t) -> s
  t = case w of (s,t) -> t
]
\end{verbatim}

************************************************************************
*                                                                      *
\subsubsection[improved-unmixing]{UNIMPLEMENTED idea for improved unmixing}
*                                                                      *
************************************************************************

We might be able to optimise unmixing when confronted by
only-one-constructor-possible, of which tuples are the most notable
examples.  Consider:
\begin{verbatim}
f (a,b,c) ... = ...
f d ... (e:f) = ...
f (g,h,i) ... = ...
f j ...       = ...
\end{verbatim}
This definition would normally be unmixed into four equation blocks,
one per equation.  But it could be unmixed into just one equation
block, because if the one equation matches (on the first column),
the others certainly will.

You have to be careful, though; the example
\begin{verbatim}
f j ...       = ...
-------------------
f (a,b,c) ... = ...
f d ... (e:f) = ...
f (g,h,i) ... = ...
\end{verbatim}
{\em must} be broken into two blocks at the line shown; otherwise, you
are forcing unnecessary evaluation.  In any case, the top-left pattern
always gives the cue.  You could then unmix blocks into groups of...
\begin{description}
\item[all variables:]
As it is now.
\item[constructors or variables (mixed):]
Need to make sure the right names get bound for the variable patterns.
\item[literals or variables (mixed):]
Presumably just a variant on the constructor case (as it is now).
\end{description}

************************************************************************
*                                                                      *
*  matchWrapper: a convenient way to call @match@                      *
*                                                                      *
************************************************************************
\subsection[matchWrapper]{@matchWrapper@: a convenient interface to @match@}

Calls to @match@ often involve similar (non-trivial) work; that work
is collected here, in @matchWrapper@.  This function takes as
arguments:
\begin{itemize}
\item
Typechecked @Matches@ (of a function definition, or a case or lambda
expression)---the main input;
\item
An error message to be inserted into any (runtime) pattern-matching
failure messages.
\end{itemize}

As results, @matchWrapper@ produces:
\begin{itemize}
\item
A list of variables (@Locals@) that the caller must ``promise'' to
bind to appropriate values; and
\item
a @CoreExpr@, the desugared output (main result).
\end{itemize}

The main actions of @matchWrapper@ include:
\begin{enumerate}
\item
Flatten the @[TypecheckedMatch]@ into a suitable list of
@EquationInfo@s.
\item
Create as many new variables as there are patterns in a pattern-list
(in any one of the @EquationInfo@s).
\item
Create a suitable ``if it fails'' expression---a call to @error@ using
the error-string input; the {\em type} of this fail value can be found
by examining one of the RHS expressions in one of the @EquationInfo@s.
\item
Call @match@ with all of this information!
\end{enumerate}
-}

matchWrapper
  :: HsMatchContext Name               -- ^ For shadowing warning messages
  -> Maybe (LHsExpr GhcTc)             -- ^ Scrutinee, if we check a case expr
  -> MatchGroup GhcTc (LHsExpr GhcTc)  -- ^ Matches being desugared
  -> DsM ([Id], CoreExpr)              -- ^ Results (usually passed to 'match')

{-
 There is one small problem with the Lambda Patterns, when somebody
 writes something similar to:
\begin{verbatim}
    (\ (x:xs) -> ...)
\end{verbatim}
 he/she don't want a warning about incomplete patterns, that is done with
 the flag @opt_WarnSimplePatterns@.
 This problem also appears in the:
\begin{itemize}
\item @do@ patterns, but if the @do@ can fail
      it creates another equation if the match can fail
      (see @DsExpr.doDo@ function)
\item @let@ patterns, are treated by @matchSimply@
   List Comprension Patterns, are treated by @matchSimply@ also
\end{itemize}

We can't call @matchSimply@ with Lambda patterns,
due to the fact that lambda patterns can have more than
one pattern, and match simply only accepts one pattern.

JJQC 30-Nov-1997
-}

matchWrapper :: HsMatchContext Name
-> Maybe (LHsExpr GhcTc)
-> MatchGroup GhcTc (LHsExpr GhcTc)
-> DsM ([MatchId], CoreExpr)
matchWrapper ctxt :: HsMatchContext Name
ctxt mb_scr :: Maybe (LHsExpr GhcTc)
mb_scr (MG { mg_alts :: forall p body. MatchGroup p body -> Located [LMatch p body]
mg_alts = (Located [LMatch GhcTc (LHsExpr GhcTc)]
-> Located (SrcSpanLess (Located [LMatch GhcTc (LHsExpr GhcTc)]))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ matches :: SrcSpanLess (Located [LMatch GhcTc (LHsExpr GhcTc)])
matches)
                             , mg_ext :: forall p body. MatchGroup p body -> XMG p body
mg_ext = MatchGroupTc arg_tys rhs_ty
                             , mg_origin :: forall p body. MatchGroup p body -> Origin
mg_origin = Origin
origin })
  = do  { DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
        ; SrcSpan
locn   <- DsM SrcSpan
getSrcSpanDs

        ; [MatchId]
new_vars    <- case SrcSpanLess (Located [LMatch GhcTc (LHsExpr GhcTc)])
matches of
                           []    -> (Type -> DsM MatchId)
-> [Type] -> IOEnv (Env DsGblEnv DsLclEnv) [MatchId]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM MatchId
newSysLocalDsNoLP [Type]
arg_tys
                           (m:_) -> [Pat GhcTc] -> IOEnv (Env DsGblEnv DsLclEnv) [MatchId]
selectMatchVars ((Pat GhcTc -> Pat GhcTc) -> [Pat GhcTc] -> [Pat GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map Pat GhcTc -> Pat GhcTc
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc (LMatch GhcTc (LHsExpr GhcTc) -> [Pat GhcTc]
forall id body. LMatch id body -> [LPat id]
hsLMatchPats LMatch GhcTc (LHsExpr GhcTc)
m))

        ; [EquationInfo]
eqns_info   <- (LMatch GhcTc (LHsExpr GhcTc)
 -> IOEnv (Env DsGblEnv DsLclEnv) EquationInfo)
-> [LMatch GhcTc (LHsExpr GhcTc)]
-> IOEnv (Env DsGblEnv DsLclEnv) [EquationInfo]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([MatchId]
-> LMatch GhcTc (LHsExpr GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) EquationInfo
mk_eqn_info [MatchId]
new_vars) [LMatch GhcTc (LHsExpr GhcTc)]
SrcSpanLess (Located [LMatch GhcTc (LHsExpr GhcTc)])
matches

        -- pattern match check warnings
        ; Bool -> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Origin -> Bool
isGenerated Origin
origin) (TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ())
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall a b. (a -> b) -> a -> b
$
          Bool -> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (DynFlags -> DsMatchContext -> Bool
isAnyPmCheckEnabled DynFlags
dflags (HsMatchContext Name -> SrcSpan -> DsMatchContext
DsMatchContext HsMatchContext Name
ctxt SrcSpan
locn)) (TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ())
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall a b. (a -> b) -> a -> b
$
          Bag SimpleEq
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall a. Bag SimpleEq -> DsM a -> DsM a
addTmCsDs (Maybe (LHsExpr GhcTc) -> [MatchId] -> Bag SimpleEq
genCaseTmCs1 Maybe (LHsExpr GhcTc)
mb_scr [MatchId]
new_vars) (TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ())
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall a b. (a -> b) -> a -> b
$
              -- See Note [Type and Term Equality Propagation]
          DynFlags
-> DsMatchContext
-> [MatchId]
-> [LMatch GhcTc (LHsExpr GhcTc)]
-> TcRnIf DsGblEnv DsLclEnv ()
checkMatches DynFlags
dflags (HsMatchContext Name -> SrcSpan -> DsMatchContext
DsMatchContext HsMatchContext Name
ctxt SrcSpan
locn) [MatchId]
new_vars [LMatch GhcTc (LHsExpr GhcTc)]
SrcSpanLess (Located [LMatch GhcTc (LHsExpr GhcTc)])
matches

        ; CoreExpr
result_expr <- DsM CoreExpr -> DsM CoreExpr
handleWarnings (DsM CoreExpr -> DsM CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
                         HsMatchContext Name
-> [MatchId] -> [EquationInfo] -> Type -> DsM CoreExpr
matchEquations HsMatchContext Name
ctxt [MatchId]
new_vars [EquationInfo]
eqns_info Type
rhs_ty
        ; ([MatchId], CoreExpr) -> DsM ([MatchId], CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return ([MatchId]
new_vars, CoreExpr
result_expr) }
  where
    mk_eqn_info :: [MatchId]
-> LMatch GhcTc (LHsExpr GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) EquationInfo
mk_eqn_info vars :: [MatchId]
vars (LMatch GhcTc (LHsExpr GhcTc)
-> Located (SrcSpanLess (LMatch GhcTc (LHsExpr GhcTc)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (Match { m_pats = pats, m_grhss = grhss }))
      = do { DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
           ; let upats :: [Pat GhcTc]
upats = (Pat GhcTc -> Pat GhcTc) -> [Pat GhcTc] -> [Pat GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map (Pat GhcTc -> Pat GhcTc
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc (Pat GhcTc -> Pat GhcTc)
-> (Pat GhcTc -> Pat GhcTc) -> Pat GhcTc -> Pat GhcTc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> Pat GhcTc -> Pat GhcTc
decideBangHood DynFlags
dflags) [Pat GhcTc]
pats
                 dicts :: Bag MatchId
dicts = [Pat GhcTc] -> Bag MatchId
collectEvVarsPats [Pat GhcTc]
upats
           ; Bag SimpleEq
tm_cs <- Maybe (LHsExpr GhcTc)
-> [Pat GhcTc] -> [MatchId] -> DsM (Bag SimpleEq)
genCaseTmCs2 Maybe (LHsExpr GhcTc)
mb_scr [Pat GhcTc]
upats [MatchId]
vars
           ; MatchResult
match_result <- Bag MatchId -> DsM MatchResult -> DsM MatchResult
forall a. Bag MatchId -> DsM a -> DsM a
addDictsDs Bag MatchId
dicts (DsM MatchResult -> DsM MatchResult)
-> DsM MatchResult -> DsM MatchResult
forall a b. (a -> b) -> a -> b
$ -- See Note [Type and Term Equality Propagation]
                             Bag SimpleEq -> DsM MatchResult -> DsM MatchResult
forall a. Bag SimpleEq -> DsM a -> DsM a
addTmCsDs Bag SimpleEq
tm_cs  (DsM MatchResult -> DsM MatchResult)
-> DsM MatchResult -> DsM MatchResult
forall a b. (a -> b) -> a -> b
$ -- See Note [Type and Term Equality Propagation]
                             HsMatchContext Name
-> GRHSs GhcTc (LHsExpr GhcTc) -> Type -> DsM MatchResult
dsGRHSs HsMatchContext Name
ctxt GRHSs GhcTc (LHsExpr GhcTc)
grhss Type
rhs_ty
           ; EquationInfo -> IOEnv (Env DsGblEnv DsLclEnv) EquationInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (EqnInfo :: [Pat GhcTc] -> Origin -> MatchResult -> EquationInfo
EqnInfo { eqn_pats :: [Pat GhcTc]
eqn_pats = [Pat GhcTc]
upats
                             , eqn_orig :: Origin
eqn_orig = Origin
FromSource
                             , eqn_rhs :: MatchResult
eqn_rhs = MatchResult
match_result }) }
    mk_eqn_info _ (LMatch GhcTc (LHsExpr GhcTc)
-> Located (SrcSpanLess (LMatch GhcTc (LHsExpr GhcTc)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (XMatch _)) = String -> IOEnv (Env DsGblEnv DsLclEnv) EquationInfo
forall a. String -> a
panic "matchWrapper"
    mk_eqn_info _ _  = String -> IOEnv (Env DsGblEnv DsLclEnv) EquationInfo
forall a. String -> a
panic "mk_eqn_info: Impossible Match" -- due to #15884

    handleWarnings :: DsM CoreExpr -> DsM CoreExpr
handleWarnings = if Origin -> Bool
isGenerated Origin
origin
                     then DsM CoreExpr -> DsM CoreExpr
forall a. DsM a -> DsM a
discardWarningsDs
                     else DsM CoreExpr -> DsM CoreExpr
forall a. a -> a
id
matchWrapper _ _ (XMatchGroup _) = String -> DsM ([MatchId], CoreExpr)
forall a. String -> a
panic "matchWrapper"

matchEquations  :: HsMatchContext Name
                -> [MatchId] -> [EquationInfo] -> Type
                -> DsM CoreExpr
matchEquations :: HsMatchContext Name
-> [MatchId] -> [EquationInfo] -> Type -> DsM CoreExpr
matchEquations ctxt :: HsMatchContext Name
ctxt vars :: [MatchId]
vars eqns_info :: [EquationInfo]
eqns_info rhs_ty :: Type
rhs_ty
  = do  { let error_doc :: SDoc
error_doc = HsMatchContext Name -> SDoc
forall id. Outputable id => HsMatchContext id -> SDoc
matchContextErrString HsMatchContext Name
ctxt

        ; MatchResult
match_result <- [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
match [MatchId]
vars Type
rhs_ty [EquationInfo]
eqns_info

        ; CoreExpr
fail_expr <- MatchId -> Type -> SDoc -> DsM CoreExpr
mkErrorAppDs MatchId
pAT_ERROR_ID Type
rhs_ty SDoc
error_doc
        ; MatchResult -> CoreExpr -> DsM CoreExpr
extractMatchResult MatchResult
match_result CoreExpr
fail_expr }

{-
************************************************************************
*                                                                      *
\subsection[matchSimply]{@matchSimply@: match a single expression against a single pattern}
*                                                                      *
************************************************************************

@mkSimpleMatch@ is a wrapper for @match@ which deals with the
situation where we want to match a single expression against a single
pattern. It returns an expression.
-}

matchSimply :: CoreExpr                 -- ^ Scrutinee
            -> HsMatchContext Name      -- ^ Match kind
            -> LPat GhcTc               -- ^ Pattern it should match
            -> CoreExpr                 -- ^ Return this if it matches
            -> CoreExpr                 -- ^ Return this if it doesn't
            -> DsM CoreExpr
-- Do not warn about incomplete patterns; see matchSinglePat comments
matchSimply :: CoreExpr
-> HsMatchContext Name
-> Pat GhcTc
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimply scrut :: CoreExpr
scrut hs_ctx :: HsMatchContext Name
hs_ctx pat :: Pat GhcTc
pat result_expr :: CoreExpr
result_expr fail_expr :: CoreExpr
fail_expr = do
    let
      match_result :: MatchResult
match_result = CoreExpr -> MatchResult
cantFailMatchResult CoreExpr
result_expr
      rhs_ty :: Type
rhs_ty       = CoreExpr -> Type
exprType CoreExpr
fail_expr
        -- Use exprType of fail_expr, because won't refine in the case of failure!
    MatchResult
match_result' <- CoreExpr
-> HsMatchContext Name
-> Pat GhcTc
-> Type
-> MatchResult
-> DsM MatchResult
matchSinglePat CoreExpr
scrut HsMatchContext Name
hs_ctx Pat GhcTc
pat Type
rhs_ty MatchResult
match_result
    MatchResult -> CoreExpr -> DsM CoreExpr
extractMatchResult MatchResult
match_result' CoreExpr
fail_expr

matchSinglePat :: CoreExpr -> HsMatchContext Name -> LPat GhcTc
               -> Type -> MatchResult -> DsM MatchResult
-- matchSinglePat ensures that the scrutinee is a variable
-- and then calls matchSinglePatVar
--
-- matchSinglePat does not warn about incomplete patterns
-- Used for things like [ e | pat <- stuff ], where
-- incomplete patterns are just fine

matchSinglePat :: CoreExpr
-> HsMatchContext Name
-> Pat GhcTc
-> Type
-> MatchResult
-> DsM MatchResult
matchSinglePat (Var var :: MatchId
var) ctx :: HsMatchContext Name
ctx pat :: Pat GhcTc
pat ty :: Type
ty match_result :: MatchResult
match_result
  | Bool -> Bool
not (Name -> Bool
isExternalName (MatchId -> Name
idName MatchId
var))
  = MatchId
-> HsMatchContext Name
-> Pat GhcTc
-> Type
-> MatchResult
-> DsM MatchResult
matchSinglePatVar MatchId
var HsMatchContext Name
ctx Pat GhcTc
pat Type
ty MatchResult
match_result

matchSinglePat scrut :: CoreExpr
scrut hs_ctx :: HsMatchContext Name
hs_ctx pat :: Pat GhcTc
pat ty :: Type
ty match_result :: MatchResult
match_result
  = do { MatchId
var           <- Pat GhcTc -> DsM MatchId
selectSimpleMatchVarL Pat GhcTc
pat
       ; MatchResult
match_result' <- MatchId
-> HsMatchContext Name
-> Pat GhcTc
-> Type
-> MatchResult
-> DsM MatchResult
matchSinglePatVar MatchId
var HsMatchContext Name
hs_ctx Pat GhcTc
pat Type
ty MatchResult
match_result
       ; MatchResult -> DsM MatchResult
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper -> MatchResult -> MatchResult
adjustMatchResult (MatchId -> CoreExpr -> DsWrapper
bindNonRec MatchId
var CoreExpr
scrut) MatchResult
match_result') }

matchSinglePatVar :: Id   -- See Note [Match Ids]
                  -> HsMatchContext Name -> LPat GhcTc
                  -> Type -> MatchResult -> DsM MatchResult
matchSinglePatVar :: MatchId
-> HsMatchContext Name
-> Pat GhcTc
-> Type
-> MatchResult
-> DsM MatchResult
matchSinglePatVar var :: MatchId
var ctx :: HsMatchContext Name
ctx pat :: Pat GhcTc
pat ty :: Type
ty match_result :: MatchResult
match_result
  = ASSERT2( isInternalName (idName var), ppr var )
    do { DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
       ; SrcSpan
locn   <- DsM SrcSpan
getSrcSpanDs

                    -- Pattern match check warnings
       ; DynFlags
-> DsMatchContext
-> MatchId
-> Pat GhcTc
-> TcRnIf DsGblEnv DsLclEnv ()
checkSingle DynFlags
dflags (HsMatchContext Name -> SrcSpan -> DsMatchContext
DsMatchContext HsMatchContext Name
ctx SrcSpan
locn) MatchId
var (Pat GhcTc -> SrcSpanLess (Pat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Pat GhcTc
pat)

       ; let eqn_info :: EquationInfo
eqn_info = EqnInfo :: [Pat GhcTc] -> Origin -> MatchResult -> EquationInfo
EqnInfo { eqn_pats :: [Pat GhcTc]
eqn_pats = [Pat GhcTc -> SrcSpanLess (Pat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc (DynFlags -> Pat GhcTc -> Pat GhcTc
decideBangHood DynFlags
dflags Pat GhcTc
pat)]
                                , eqn_orig :: Origin
eqn_orig = Origin
FromSource
                                , eqn_rhs :: MatchResult
eqn_rhs  = MatchResult
match_result }
       ; [MatchId] -> Type -> [EquationInfo] -> DsM MatchResult
match [MatchId
var] Type
ty [EquationInfo
eqn_info] }


{-
************************************************************************
*                                                                      *
                Pattern classification
*                                                                      *
************************************************************************
-}

data PatGroup
  = PgAny               -- Immediate match: variables, wildcards,
                        --                  lazy patterns
  | PgCon DataCon       -- Constructor patterns (incl list, tuple)
  | PgSyn PatSyn [Type] -- See Note [Pattern synonym groups]
  | PgLit Literal       -- Literal patterns
  | PgN   Rational      -- Overloaded numeric literals;
                        -- see Note [Don't use Literal for PgN]
  | PgOverS FastString  -- Overloaded string literals
  | PgNpK Integer       -- n+k patterns
  | PgBang              -- Bang patterns
  | PgCo Type           -- Coercion patterns; the type is the type
                        --      of the pattern *inside*
  | PgView (LHsExpr GhcTc) -- view pattern (e -> p):
                        -- the LHsExpr is the expression e
           Type         -- the Type is the type of p (equivalently, the result type of e)
  | PgOverloadedList

{- Note [Don't use Literal for PgN]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Previously we had, as PatGroup constructors

  | ...
  | PgN   Literal       -- Overloaded literals
  | PgNpK Literal       -- n+k patterns
  | ...

But Literal is really supposed to represent an *unboxed* literal, like Int#.
We were sticking the literal from, say, an overloaded numeric literal pattern
into a LitInt constructor. This didn't really make sense; and we now have
the invariant that value in a LitInt must be in the range of the target
machine's Int# type, and an overloaded literal could meaningfully be larger.

Solution: For pattern grouping purposes, just store the literal directly in
the PgN constructor as a Rational if numeric, and add a PgOverStr constructor
for overloaded strings.
-}

groupEquations :: DynFlags -> [EquationInfo] -> [[(PatGroup, EquationInfo)]]
-- If the result is of form [g1, g2, g3],
-- (a) all the (pg,eq) pairs in g1 have the same pg
-- (b) none of the gi are empty
-- The ordering of equations is unchanged
groupEquations :: DynFlags -> [EquationInfo] -> [[(PatGroup, EquationInfo)]]
groupEquations dflags :: DynFlags
dflags eqns :: [EquationInfo]
eqns
  = ((PatGroup, EquationInfo) -> (PatGroup, EquationInfo) -> Bool)
-> [(PatGroup, EquationInfo)] -> [[(PatGroup, EquationInfo)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (PatGroup, EquationInfo) -> (PatGroup, EquationInfo) -> Bool
same_gp [(DynFlags -> Pat GhcTc -> PatGroup
patGroup DynFlags
dflags (EquationInfo -> Pat GhcTc
firstPat EquationInfo
eqn), EquationInfo
eqn) | EquationInfo
eqn <- [EquationInfo]
eqns]
  where
    same_gp :: (PatGroup,EquationInfo) -> (PatGroup,EquationInfo) -> Bool
    (pg1 :: PatGroup
pg1,_) same_gp :: (PatGroup, EquationInfo) -> (PatGroup, EquationInfo) -> Bool
`same_gp` (pg2 :: PatGroup
pg2,_) = PatGroup
pg1 PatGroup -> PatGroup -> Bool
`sameGroup` PatGroup
pg2

subGroup :: (m -> [[EquationInfo]]) -- Map.elems
         -> m -- Map.empty
         -> (a -> m -> Maybe [EquationInfo]) -- Map.lookup
         -> (a -> [EquationInfo] -> m -> m) -- Map.insert
         -> [(a, EquationInfo)] -> [[EquationInfo]]
-- Input is a particular group.  The result sub-groups the
-- equations by with particular constructor, literal etc they match.
-- Each sub-list in the result has the same PatGroup
-- See Note [Take care with pattern order]
-- Parameterized by map operations to allow different implementations
-- and constraints, eg. types without Ord instance.
subGroup :: (m -> [[EquationInfo]])
-> m
-> (a -> m -> Maybe [EquationInfo])
-> (a -> [EquationInfo] -> m -> m)
-> [(a, EquationInfo)]
-> [[EquationInfo]]
subGroup elems :: m -> [[EquationInfo]]
elems empty :: m
empty lookup :: a -> m -> Maybe [EquationInfo]
lookup insert :: a -> [EquationInfo] -> m -> m
insert group :: [(a, EquationInfo)]
group
    = ([EquationInfo] -> [EquationInfo])
-> [[EquationInfo]] -> [[EquationInfo]]
forall a b. (a -> b) -> [a] -> [b]
map [EquationInfo] -> [EquationInfo]
forall a. [a] -> [a]
reverse ([[EquationInfo]] -> [[EquationInfo]])
-> [[EquationInfo]] -> [[EquationInfo]]
forall a b. (a -> b) -> a -> b
$ m -> [[EquationInfo]]
elems (m -> [[EquationInfo]]) -> m -> [[EquationInfo]]
forall a b. (a -> b) -> a -> b
$ (m -> (a, EquationInfo) -> m) -> m -> [(a, EquationInfo)] -> m
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' m -> (a, EquationInfo) -> m
accumulate m
empty [(a, EquationInfo)]
group
  where
    accumulate :: m -> (a, EquationInfo) -> m
accumulate pg_map :: m
pg_map (pg :: a
pg, eqn :: EquationInfo
eqn)
      = case a -> m -> Maybe [EquationInfo]
lookup a
pg m
pg_map of
          Just eqns :: [EquationInfo]
eqns -> a -> [EquationInfo] -> m -> m
insert a
pg (EquationInfo
eqnEquationInfo -> [EquationInfo] -> [EquationInfo]
forall a. a -> [a] -> [a]
:[EquationInfo]
eqns) m
pg_map
          Nothing   -> a -> [EquationInfo] -> m -> m
insert a
pg [EquationInfo
eqn]      m
pg_map
    -- pg_map :: Map a [EquationInfo]
    -- Equations seen so far in reverse order of appearance

subGroupOrd :: Ord a => [(a, EquationInfo)] -> [[EquationInfo]]
subGroupOrd :: [(a, EquationInfo)] -> [[EquationInfo]]
subGroupOrd = (Map a [EquationInfo] -> [[EquationInfo]])
-> Map a [EquationInfo]
-> (a -> Map a [EquationInfo] -> Maybe [EquationInfo])
-> (a
    -> [EquationInfo] -> Map a [EquationInfo] -> Map a [EquationInfo])
-> [(a, EquationInfo)]
-> [[EquationInfo]]
forall m a.
(m -> [[EquationInfo]])
-> m
-> (a -> m -> Maybe [EquationInfo])
-> (a -> [EquationInfo] -> m -> m)
-> [(a, EquationInfo)]
-> [[EquationInfo]]
subGroup Map a [EquationInfo] -> [[EquationInfo]]
forall k a. Map k a -> [a]
Map.elems Map a [EquationInfo]
forall k a. Map k a
Map.empty a -> Map a [EquationInfo] -> Maybe [EquationInfo]
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup a -> [EquationInfo] -> Map a [EquationInfo] -> Map a [EquationInfo]
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert

subGroupUniq :: Uniquable a => [(a, EquationInfo)] -> [[EquationInfo]]
subGroupUniq :: [(a, EquationInfo)] -> [[EquationInfo]]
subGroupUniq =
  (UniqDFM [EquationInfo] -> [[EquationInfo]])
-> UniqDFM [EquationInfo]
-> (a -> UniqDFM [EquationInfo] -> Maybe [EquationInfo])
-> (a
    -> [EquationInfo]
    -> UniqDFM [EquationInfo]
    -> UniqDFM [EquationInfo])
-> [(a, EquationInfo)]
-> [[EquationInfo]]
forall m a.
(m -> [[EquationInfo]])
-> m
-> (a -> m -> Maybe [EquationInfo])
-> (a -> [EquationInfo] -> m -> m)
-> [(a, EquationInfo)]
-> [[EquationInfo]]
subGroup UniqDFM [EquationInfo] -> [[EquationInfo]]
forall elt. UniqDFM elt -> [elt]
eltsUDFM UniqDFM [EquationInfo]
forall elt. UniqDFM elt
emptyUDFM ((UniqDFM [EquationInfo] -> a -> Maybe [EquationInfo])
-> a -> UniqDFM [EquationInfo] -> Maybe [EquationInfo]
forall a b c. (a -> b -> c) -> b -> a -> c
flip UniqDFM [EquationInfo] -> a -> Maybe [EquationInfo]
forall key elt. Uniquable key => UniqDFM elt -> key -> Maybe elt
lookupUDFM) (\k :: a
k v :: [EquationInfo]
v m :: UniqDFM [EquationInfo]
m -> UniqDFM [EquationInfo]
-> a -> [EquationInfo] -> UniqDFM [EquationInfo]
forall key elt.
Uniquable key =>
UniqDFM elt -> key -> elt -> UniqDFM elt
addToUDFM UniqDFM [EquationInfo]
m a
k [EquationInfo]
v)

{- Note [Pattern synonym groups]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If we see
  f (P a) = e1
  f (P b) = e2
    ...
where P is a pattern synonym, can we put (P a -> e1) and (P b -> e2) in the
same group?  We can if P is a constructor, but /not/ if P is a pattern synonym.
Consider (Trac #11224)
   -- readMaybe :: Read a => String -> Maybe a
   pattern PRead :: Read a => () => a -> String
   pattern PRead a <- (readMaybe -> Just a)

   f (PRead (x::Int))  = e1
   f (PRead (y::Bool)) = e2
This is all fine: we match the string by trying to read an Int; if that
fails we try to read a Bool. But clearly we can't combine the two into a single
match.

Conclusion: we can combine when we invoke PRead /at the same type/.  Hence
in PgSyn we record the instantiaing types, and use them in sameGroup.

Note [Take care with pattern order]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the subGroup function we must be very careful about pattern re-ordering,
Consider the patterns [ (True, Nothing), (False, x), (True, y) ]
Then in bringing together the patterns for True, we must not
swap the Nothing and y!
-}

sameGroup :: PatGroup -> PatGroup -> Bool
-- Same group means that a single case expression
-- or test will suffice to match both, *and* the order
-- of testing within the group is insignificant.
sameGroup :: PatGroup -> PatGroup -> Bool
sameGroup PgAny         PgAny         = Bool
True
sameGroup PgBang        PgBang        = Bool
True
sameGroup (PgCon _)     (PgCon _)     = Bool
True    -- One case expression
sameGroup (PgSyn p1 :: PatSyn
p1 t1 :: [Type]
t1) (PgSyn p2 :: PatSyn
p2 t2 :: [Type]
t2) = PatSyn
p1PatSyn -> PatSyn -> Bool
forall a. Eq a => a -> a -> Bool
==PatSyn
p2 Bool -> Bool -> Bool
&& [Type] -> [Type] -> Bool
eqTypes [Type]
t1 [Type]
t2
                                                -- eqTypes: See Note [Pattern synonym groups]
sameGroup (PgLit _)     (PgLit _)     = Bool
True    -- One case expression
sameGroup (PgN l1 :: Rational
l1)      (PgN l2 :: Rational
l2)      = Rational
l1Rational -> Rational -> Bool
forall a. Eq a => a -> a -> Bool
==Rational
l2  -- Order is significant
sameGroup (PgOverS s1 :: FastString
s1)  (PgOverS s2 :: FastString
s2)  = FastString
s1FastString -> FastString -> Bool
forall a. Eq a => a -> a -> Bool
==FastString
s2
sameGroup (PgNpK l1 :: Integer
l1)    (PgNpK l2 :: Integer
l2)    = Integer
l1Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
==Integer
l2  -- See Note [Grouping overloaded literal patterns]
sameGroup (PgCo t1 :: Type
t1)     (PgCo t2 :: Type
t2)     = Type
t1 Type -> Type -> Bool
`eqType` Type
t2
        -- CoPats are in the same goup only if the type of the
        -- enclosed pattern is the same. The patterns outside the CoPat
        -- always have the same type, so this boils down to saying that
        -- the two coercions are identical.
sameGroup (PgView e1 :: LHsExpr GhcTc
e1 t1 :: Type
t1) (PgView e2 :: LHsExpr GhcTc
e2 t2 :: Type
t2) = (LHsExpr GhcTc, Type) -> (LHsExpr GhcTc, Type) -> Bool
viewLExprEq (LHsExpr GhcTc
e1,Type
t1) (LHsExpr GhcTc
e2,Type
t2)
       -- ViewPats are in the same group iff the expressions
       -- are "equal"---conservatively, we use syntactic equality
sameGroup _          _          = Bool
False

-- An approximation of syntactic equality used for determining when view
-- exprs are in the same group.
-- This function can always safely return false;
-- but doing so will result in the application of the view function being repeated.
--
-- Currently: compare applications of literals and variables
--            and anything else that we can do without involving other
--            HsSyn types in the recursion
--
-- NB we can't assume that the two view expressions have the same type.  Consider
--   f (e1 -> True) = ...
--   f (e2 -> "hi") = ...
viewLExprEq :: (LHsExpr GhcTc,Type) -> (LHsExpr GhcTc,Type) -> Bool
viewLExprEq :: (LHsExpr GhcTc, Type) -> (LHsExpr GhcTc, Type) -> Bool
viewLExprEq (e1 :: LHsExpr GhcTc
e1,_) (e2 :: LHsExpr GhcTc
e2,_) = LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e1 LHsExpr GhcTc
e2
  where
    lexp :: LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
    lexp :: LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp e :: LHsExpr GhcTc
e e' :: LHsExpr GhcTc
e' = HsExpr GhcTc -> HsExpr GhcTc -> Bool
exp (LHsExpr GhcTc -> SrcSpanLess (LHsExpr GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc LHsExpr GhcTc
e) (LHsExpr GhcTc -> SrcSpanLess (LHsExpr GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc LHsExpr GhcTc
e')

    ---------
    exp :: HsExpr GhcTc -> HsExpr GhcTc -> Bool
    -- real comparison is on HsExpr's
    -- strip parens
    exp :: HsExpr GhcTc -> HsExpr GhcTc -> Bool
exp (HsPar _ (LHsExpr GhcTc -> Located (SrcSpanLess (LHsExpr GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ e :: SrcSpanLess (LHsExpr GhcTc)
e)) e' :: HsExpr GhcTc
e'   = HsExpr GhcTc -> HsExpr GhcTc -> Bool
exp SrcSpanLess (LHsExpr GhcTc)
HsExpr GhcTc
e HsExpr GhcTc
e'
    exp e :: HsExpr GhcTc
e (HsPar _ (LHsExpr GhcTc -> Located (SrcSpanLess (LHsExpr GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ e' :: SrcSpanLess (LHsExpr GhcTc)
e'))   = HsExpr GhcTc -> HsExpr GhcTc -> Bool
exp HsExpr GhcTc
e SrcSpanLess (LHsExpr GhcTc)
HsExpr GhcTc
e'
    -- because the expressions do not necessarily have the same type,
    -- we have to compare the wrappers
    exp (HsWrap _ h :: HsWrapper
h e :: HsExpr GhcTc
e) (HsWrap _ h' :: HsWrapper
h' e' :: HsExpr GhcTc
e') = HsWrapper -> HsWrapper -> Bool
wrap HsWrapper
h HsWrapper
h' Bool -> Bool -> Bool
&& HsExpr GhcTc -> HsExpr GhcTc -> Bool
exp HsExpr GhcTc
e HsExpr GhcTc
e'
    exp (HsVar _ i :: Located (IdP GhcTc)
i) (HsVar _ i' :: Located (IdP GhcTc)
i') =  Located MatchId
Located (IdP GhcTc)
i Located MatchId -> Located MatchId -> Bool
forall a. Eq a => a -> a -> Bool
== Located MatchId
Located (IdP GhcTc)
i'
    exp (HsConLikeOut _ c :: ConLike
c) (HsConLikeOut _ c' :: ConLike
c') = ConLike
c ConLike -> ConLike -> Bool
forall a. Eq a => a -> a -> Bool
== ConLike
c'
    -- the instance for IPName derives using the id, so this works if the
    -- above does
    exp (HsIPVar _ i :: HsIPName
i) (HsIPVar _ i' :: HsIPName
i') = HsIPName
i HsIPName -> HsIPName -> Bool
forall a. Eq a => a -> a -> Bool
== HsIPName
i'
    exp (HsOverLabel _ l :: Maybe (IdP GhcTc)
l x :: FastString
x) (HsOverLabel _ l' :: Maybe (IdP GhcTc)
l' x' :: FastString
x') = Maybe MatchId
Maybe (IdP GhcTc)
l Maybe MatchId -> Maybe MatchId -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe MatchId
Maybe (IdP GhcTc)
l' Bool -> Bool -> Bool
&& FastString
x FastString -> FastString -> Bool
forall a. Eq a => a -> a -> Bool
== FastString
x'
    exp (HsOverLit _ l :: HsOverLit GhcTc
l) (HsOverLit _ l' :: HsOverLit GhcTc
l') =
        -- Overloaded lits are equal if they have the same type
        -- and the data is the same.
        -- this is coarser than comparing the SyntaxExpr's in l and l',
        -- which resolve the overloading (e.g., fromInteger 1),
        -- because these expressions get written as a bunch of different variables
        -- (presumably to improve sharing)
        Type -> Type -> Bool
eqType (HsOverLit GhcTc -> Type
overLitType HsOverLit GhcTc
l) (HsOverLit GhcTc -> Type
overLitType HsOverLit GhcTc
l') Bool -> Bool -> Bool
&& HsOverLit GhcTc
l HsOverLit GhcTc -> HsOverLit GhcTc -> Bool
forall a. Eq a => a -> a -> Bool
== HsOverLit GhcTc
l'
    exp (HsApp _ e1 :: LHsExpr GhcTc
e1 e2 :: LHsExpr GhcTc
e2) (HsApp _ e1' :: LHsExpr GhcTc
e1' e2' :: LHsExpr GhcTc
e2') = LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e1 LHsExpr GhcTc
e1' Bool -> Bool -> Bool
&& LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e2 LHsExpr GhcTc
e2'
    -- the fixities have been straightened out by now, so it's safe
    -- to ignore them?
    exp (OpApp _ l :: LHsExpr GhcTc
l o :: LHsExpr GhcTc
o ri :: LHsExpr GhcTc
ri) (OpApp _ l' :: LHsExpr GhcTc
l' o' :: LHsExpr GhcTc
o' ri' :: LHsExpr GhcTc
ri') =
        LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
l LHsExpr GhcTc
l' Bool -> Bool -> Bool
&& LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
o LHsExpr GhcTc
o' Bool -> Bool -> Bool
&& LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
ri LHsExpr GhcTc
ri'
    exp (NegApp _ e :: LHsExpr GhcTc
e n :: SyntaxExpr GhcTc
n) (NegApp _ e' :: LHsExpr GhcTc
e' n' :: SyntaxExpr GhcTc
n') = LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e LHsExpr GhcTc
e' Bool -> Bool -> Bool
&& SyntaxExpr GhcTc -> SyntaxExpr GhcTc -> Bool
syn_exp SyntaxExpr GhcTc
n SyntaxExpr GhcTc
n'
    exp (SectionL _ e1 :: LHsExpr GhcTc
e1 e2 :: LHsExpr GhcTc
e2) (SectionL _ e1' :: LHsExpr GhcTc
e1' e2' :: LHsExpr GhcTc
e2') =
        LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e1 LHsExpr GhcTc
e1' Bool -> Bool -> Bool
&& LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e2 LHsExpr GhcTc
e2'
    exp (SectionR _ e1 :: LHsExpr GhcTc
e1 e2 :: LHsExpr GhcTc
e2) (SectionR _ e1' :: LHsExpr GhcTc
e1' e2' :: LHsExpr GhcTc
e2') =
        LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e1 LHsExpr GhcTc
e1' Bool -> Bool -> Bool
&& LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e2 LHsExpr GhcTc
e2'
    exp (ExplicitTuple _ es1 :: [LHsTupArg GhcTc]
es1 _) (ExplicitTuple _ es2 :: [LHsTupArg GhcTc]
es2 _) =
        (LHsTupArg GhcTc -> LHsTupArg GhcTc -> Bool)
-> [LHsTupArg GhcTc] -> [LHsTupArg GhcTc] -> Bool
forall a. (a -> a -> Bool) -> [a] -> [a] -> Bool
eq_list LHsTupArg GhcTc -> LHsTupArg GhcTc -> Bool
forall a a.
(HasSrcSpan a, HasSrcSpan a, SrcSpanLess a ~ HsTupArg GhcTc,
 SrcSpanLess a ~ HsTupArg GhcTc) =>
a -> a -> Bool
tup_arg [LHsTupArg GhcTc]
es1 [LHsTupArg GhcTc]
es2
    exp (ExplicitSum _ _ _ e :: LHsExpr GhcTc
e) (ExplicitSum _ _ _ e' :: LHsExpr GhcTc
e') = LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e LHsExpr GhcTc
e'
    exp (HsIf _ _ e :: LHsExpr GhcTc
e e1 :: LHsExpr GhcTc
e1 e2 :: LHsExpr GhcTc
e2) (HsIf _ _ e' :: LHsExpr GhcTc
e' e1' :: LHsExpr GhcTc
e1' e2' :: LHsExpr GhcTc
e2') =
        LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e LHsExpr GhcTc
e' Bool -> Bool -> Bool
&& LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e1 LHsExpr GhcTc
e1' Bool -> Bool -> Bool
&& LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e2 LHsExpr GhcTc
e2'

    -- Enhancement: could implement equality for more expressions
    --   if it seems useful
    -- But no need for HsLit, ExplicitList, ExplicitTuple,
    -- because they cannot be functions
    exp _ _  = Bool
False

    ---------
    syn_exp :: SyntaxExpr GhcTc -> SyntaxExpr GhcTc -> Bool
    syn_exp :: SyntaxExpr GhcTc -> SyntaxExpr GhcTc -> Bool
syn_exp (SyntaxExpr { syn_expr :: forall p. SyntaxExpr p -> HsExpr p
syn_expr      = HsExpr GhcTc
expr1
                        , syn_arg_wraps :: forall p. SyntaxExpr p -> [HsWrapper]
syn_arg_wraps = [HsWrapper]
arg_wraps1
                        , syn_res_wrap :: forall p. SyntaxExpr p -> HsWrapper
syn_res_wrap  = HsWrapper
res_wrap1 })
            (SyntaxExpr { syn_expr :: forall p. SyntaxExpr p -> HsExpr p
syn_expr      = HsExpr GhcTc
expr2
                        , syn_arg_wraps :: forall p. SyntaxExpr p -> [HsWrapper]
syn_arg_wraps = [HsWrapper]
arg_wraps2
                        , syn_res_wrap :: forall p. SyntaxExpr p -> HsWrapper
syn_res_wrap  = HsWrapper
res_wrap2 })
      = HsExpr GhcTc -> HsExpr GhcTc -> Bool
exp HsExpr GhcTc
expr1 HsExpr GhcTc
expr2 Bool -> Bool -> Bool
&&
        [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and (String
-> (HsWrapper -> HsWrapper -> Bool)
-> [HsWrapper]
-> [HsWrapper]
-> [Bool]
forall a b c. String -> (a -> b -> c) -> [a] -> [b] -> [c]
zipWithEqual "viewLExprEq" HsWrapper -> HsWrapper -> Bool
wrap [HsWrapper]
arg_wraps1 [HsWrapper]
arg_wraps2) Bool -> Bool -> Bool
&&
        HsWrapper -> HsWrapper -> Bool
wrap HsWrapper
res_wrap1 HsWrapper
res_wrap2

    ---------
    tup_arg :: a -> a -> Bool
tup_arg (a -> Located (SrcSpanLess a)
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (Present _ e1)) (a -> Located (SrcSpanLess a)
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (Present _ e2)) = LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e1 LHsExpr GhcTc
e2
    tup_arg (a -> Located (SrcSpanLess a)
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (Missing t1))   (a -> Located (SrcSpanLess a)
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (Missing t2))   = Type -> Type -> Bool
eqType Type
XMissing GhcTc
t1 Type
XMissing GhcTc
t2
    tup_arg _ _ = Bool
False

    ---------
    wrap :: HsWrapper -> HsWrapper -> Bool
    -- Conservative, in that it demands that wrappers be
    -- syntactically identical and doesn't look under binders
    --
    -- Coarser notions of equality are possible
    -- (e.g., reassociating compositions,
    --        equating different ways of writing a coercion)
    wrap :: HsWrapper -> HsWrapper -> Bool
wrap WpHole WpHole = Bool
True
    wrap (WpCompose w1 :: HsWrapper
w1 w2 :: HsWrapper
w2) (WpCompose w1' :: HsWrapper
w1' w2' :: HsWrapper
w2') = HsWrapper -> HsWrapper -> Bool
wrap HsWrapper
w1 HsWrapper
w1' Bool -> Bool -> Bool
&& HsWrapper -> HsWrapper -> Bool
wrap HsWrapper
w2 HsWrapper
w2'
    wrap (WpFun w1 :: HsWrapper
w1 w2 :: HsWrapper
w2 _ _) (WpFun w1' :: HsWrapper
w1' w2' :: HsWrapper
w2' _ _) = HsWrapper -> HsWrapper -> Bool
wrap HsWrapper
w1 HsWrapper
w1' Bool -> Bool -> Bool
&& HsWrapper -> HsWrapper -> Bool
wrap HsWrapper
w2 HsWrapper
w2'
    wrap (WpCast co :: TcCoercionR
co)       (WpCast co' :: TcCoercionR
co')        = TcCoercionR
co TcCoercionR -> TcCoercionR -> Bool
`eqCoercion` TcCoercionR
co'
    wrap (WpEvApp et1 :: EvTerm
et1)     (WpEvApp et2 :: EvTerm
et2)       = EvTerm
et1 EvTerm -> EvTerm -> Bool
`ev_term` EvTerm
et2
    wrap (WpTyApp t :: Type
t)       (WpTyApp t' :: Type
t')        = Type -> Type -> Bool
eqType Type
t Type
t'
    -- Enhancement: could implement equality for more wrappers
    --   if it seems useful (lams and lets)
    wrap _ _ = Bool
False

    ---------
    ev_term :: EvTerm -> EvTerm -> Bool
    ev_term :: EvTerm -> EvTerm -> Bool
ev_term (EvExpr (Var a :: MatchId
a)) (EvExpr  (Var b :: MatchId
b)) = MatchId
aMatchId -> MatchId -> Bool
forall a. Eq a => a -> a -> Bool
==MatchId
b
    ev_term (EvExpr (Coercion a :: TcCoercionR
a)) (EvExpr (Coercion b :: TcCoercionR
b)) = TcCoercionR
a TcCoercionR -> TcCoercionR -> Bool
`eqCoercion` TcCoercionR
b
    ev_term _ _ = Bool
False

    ---------
    eq_list :: (a->a->Bool) -> [a] -> [a] -> Bool
    eq_list :: (a -> a -> Bool) -> [a] -> [a] -> Bool
eq_list _  []     []     = Bool
True
    eq_list _  []     (_:_)  = Bool
False
    eq_list _  (_:_)  []     = Bool
False
    eq_list eq :: a -> a -> Bool
eq (x :: a
x:xs :: [a]
xs) (y :: a
y:ys :: [a]
ys) = a -> a -> Bool
eq a
x a
y Bool -> Bool -> Bool
&& (a -> a -> Bool) -> [a] -> [a] -> Bool
forall a. (a -> a -> Bool) -> [a] -> [a] -> Bool
eq_list a -> a -> Bool
eq [a]
xs [a]
ys

patGroup :: DynFlags -> Pat GhcTc -> PatGroup
patGroup :: DynFlags -> Pat GhcTc -> PatGroup
patGroup _ (ConPatOut { pat_con :: forall p. LPat p -> Located ConLike
pat_con = (Located ConLike -> Located (SrcSpanLess (Located ConLike))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ con :: SrcSpanLess (Located ConLike)
con)
                      , pat_arg_tys :: forall p. LPat p -> [Type]
pat_arg_tys = [Type]
tys })
 | RealDataCon dcon <- SrcSpanLess (Located ConLike)
con              = DataCon -> PatGroup
PgCon DataCon
dcon
 | PatSynCon psyn <- SrcSpanLess (Located ConLike)
con                = PatSyn -> [Type] -> PatGroup
PgSyn PatSyn
psyn [Type]
tys
patGroup _ (WildPat {})                 = PatGroup
PgAny
patGroup _ (BangPat {})                 = PatGroup
PgBang
patGroup _ (NPat _ (Located (HsOverLit GhcTc)
-> Located (SrcSpanLess (Located (HsOverLit GhcTc)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (OverLit {ol_val=oval})) mb_neg :: Maybe (SyntaxExpr GhcTc)
mb_neg _) =
  case (OverLitVal
oval, Maybe (SyntaxExpr GhcTc) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (SyntaxExpr GhcTc)
mb_neg) of
   (HsIntegral   i :: IntegralLit
i, False) -> Rational -> PatGroup
PgN (Integer -> Rational
forall a. Num a => Integer -> a
fromInteger (IntegralLit -> Integer
il_value IntegralLit
i))
   (HsIntegral   i :: IntegralLit
i, True ) -> Rational -> PatGroup
PgN (-Integer -> Rational
forall a. Num a => Integer -> a
fromInteger (IntegralLit -> Integer
il_value IntegralLit
i))
   (HsFractional r :: FractionalLit
r, False) -> Rational -> PatGroup
PgN (FractionalLit -> Rational
fl_value FractionalLit
r)
   (HsFractional r :: FractionalLit
r, True ) -> Rational -> PatGroup
PgN (-FractionalLit -> Rational
fl_value FractionalLit
r)
   (HsIsString _ s :: FastString
s, _) -> ASSERT(isNothing mb_neg)
                          FastString -> PatGroup
PgOverS FastString
s
patGroup _ (NPlusKPat _ _ (Located (HsOverLit GhcTc)
-> Located (SrcSpanLess (Located (HsOverLit GhcTc)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (OverLit {ol_val=oval})) _ _ _) =
  case OverLitVal
oval of
   HsIntegral i :: IntegralLit
i -> Integer -> PatGroup
PgNpK (IntegralLit -> Integer
il_value IntegralLit
i)
   _ -> String -> SDoc -> PatGroup
forall a. HasCallStack => String -> SDoc -> a
pprPanic "patGroup NPlusKPat" (OverLitVal -> SDoc
forall a. Outputable a => a -> SDoc
ppr OverLitVal
oval)
patGroup _ (CoPat _ _ p :: Pat GhcTc
p _)              = Type -> PatGroup
PgCo  (Pat GhcTc -> Type
hsPatType Pat GhcTc
p)
                                                    -- Type of innelexp pattern
patGroup _ (ViewPat _ expr :: LHsExpr GhcTc
expr p :: Pat GhcTc
p)           = LHsExpr GhcTc -> Type -> PatGroup
PgView LHsExpr GhcTc
expr (Pat GhcTc -> Type
hsPatType (Pat GhcTc -> SrcSpanLess (Pat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc Pat GhcTc
p))
patGroup _ (ListPat (ListPatTc _ (Just _)) _) = PatGroup
PgOverloadedList
patGroup dflags :: DynFlags
dflags (LitPat _ lit :: HsLit GhcTc
lit)          = Literal -> PatGroup
PgLit (DynFlags -> HsLit GhcTc -> Literal
hsLitKey DynFlags
dflags HsLit GhcTc
lit)
patGroup _ pat :: Pat GhcTc
pat                          = String -> SDoc -> PatGroup
forall a. HasCallStack => String -> SDoc -> a
pprPanic "patGroup" (Pat GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr Pat GhcTc
pat)

{-
Note [Grouping overloaded literal patterns]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
WATCH OUT!  Consider

        f (n+1) = ...
        f (n+2) = ...
        f (n+1) = ...

We can't group the first and third together, because the second may match
the same thing as the first.  Same goes for *overloaded* literal patterns
        f 1 True = ...
        f 2 False = ...
        f 1 False = ...
If the first arg matches '1' but the second does not match 'True', we
cannot jump to the third equation!  Because the same argument might
match '2'!
Hence we don't regard 1 and 2, or (n+1) and (n+2), as part of the same group.
-}