{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MonadComprehensions #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}

{-# OPTIONS_GHC -Wno-incomplete-uni-patterns   #-}

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


The @match@ function
-}

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

import GHC.Prelude
import GHC.Platform

import Language.Haskell.Syntax.Basic (Boxity(..))

import {-#SOURCE#-} GHC.HsToCore.Expr (dsExpr)

import GHC.Types.Basic ( Origin(..), isGenerated, requiresPMC )
import GHC.Types.SourceText
import GHC.Driver.DynFlags
import GHC.Hs
import GHC.Hs.Syn.Type
import GHC.Tc.Types.Evidence
import GHC.Tc.Utils.Monad
import GHC.HsToCore.Pmc
import GHC.HsToCore.Pmc.Types ( Nablas )
import GHC.HsToCore.Monad
import GHC.HsToCore.Binds
import GHC.HsToCore.GuardedRHSs
import GHC.HsToCore.Utils
import GHC.HsToCore.Errors.Types
import GHC.HsToCore.Match.Constructor
import GHC.HsToCore.Match.Literal

import GHC.Core
import GHC.Core.Utils
import GHC.Core.Make
import GHC.Core.ConLike
import GHC.Core.DataCon
import GHC.Core.PatSyn
import GHC.Core.Type
import GHC.Core.TyCo.Compare( eqType, eqTypes )
import GHC.Core.Coercion ( eqCoercion )
import GHC.Core.TyCon    ( isNewTyCon )
import GHC.Core.Multiplicity
import GHC.Builtin.Types

import GHC.Types.Id
import GHC.Types.Literal
import GHC.Types.SrcLoc

import GHC.Data.Maybe
import GHC.Utils.Misc
import GHC.Types.Name
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Utils.Panic.Plain
import GHC.Data.FastString
import GHC.Types.Unique
import GHC.Types.Unique.DFM

import Control.Monad ( zipWithM, unless )
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NEL
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 @GHC.HsToCore.Monad@.

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 (#13043).

See also Note [Localise pattern binders] in GHC.HsToCore.Utils
-}

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

match :: [MatchId]        -- ^ Variables rep\'ing the exprs we\'re matching with
                          -- ^ See Note [Match Ids]
                          --
                          -- ^ Note that the Match Ids carry not only a name, but
                          -- ^ also the multiplicity at which each column has been
                          -- ^ type checked.
      -> Type             -- ^ Type of the case expression
      -> [EquationInfo]   -- ^ Info about patterns, etc. (type synonym below)
      -> DsM (MatchResult CoreExpr) -- ^ Desugared result!

match :: [Id] -> Type -> [EquationInfo] -> DsM (MatchResult CoreExpr)
match [] Type
ty [EquationInfo]
eqns
  = Bool
-> SDoc -> DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr)
forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr (Bool -> Bool
not ([EquationInfo] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [EquationInfo]
eqns)) (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty) (DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr))
-> DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$
    MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((MatchResult CoreExpr
 -> MatchResult CoreExpr -> MatchResult CoreExpr)
-> [MatchResult CoreExpr] -> MatchResult CoreExpr
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 MatchResult CoreExpr
-> MatchResult CoreExpr -> MatchResult CoreExpr
combineMatchResults [MatchResult CoreExpr]
match_results)
  where
    match_results :: [MatchResult CoreExpr]
match_results = [ Bool -> MatchResult CoreExpr -> MatchResult CoreExpr
forall a. HasCallStack => Bool -> a -> a
assert ([Pat GhcTc] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (EquationInfo -> [Pat GhcTc]
eqn_pats EquationInfo
eqn)) (MatchResult CoreExpr -> MatchResult CoreExpr)
-> MatchResult CoreExpr -> MatchResult CoreExpr
forall a b. (a -> b) -> a -> b
$
                      EquationInfo -> MatchResult CoreExpr
eqn_rhs EquationInfo
eqn
                    | EquationInfo
eqn <- [EquationInfo]
eqns ]

match (Id
v:[Id]
vs) Type
ty [EquationInfo]
eqns    -- Eqns *can* be empty
  = Bool
-> SDoc -> DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr)
forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr ((Id -> Bool) -> NonEmpty Id -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Name -> Bool
isInternalName (Name -> Bool) -> (Id -> Name) -> Id -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id -> Name
idName) NonEmpty Id
vars) (NonEmpty Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr NonEmpty Id
vars) (DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr))
-> DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$
    do  { DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
        ; let platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
                -- Tidy the first pattern, generating
                -- auxiliary bindings if necessary
        ; ([DsWrapper]
aux_binds, [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 (Id
-> EquationInfo
-> IOEnv (Env DsGblEnv DsLclEnv) (DsWrapper, EquationInfo)
tidyEqnInfo Id
v) [EquationInfo]
eqns
                -- Group the equations and match each group in turn
        ; let grouped :: [NonEmpty (PatGroup, EquationInfo)]
grouped = Platform -> [EquationInfo] -> [NonEmpty (PatGroup, EquationInfo)]
groupEquations Platform
platform [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 ([NonEmpty (PatGroup, EquationInfo)] -> TcRnIf DsGblEnv DsLclEnv ()
forall {t :: * -> *} {b}.
Foldable t =>
[t (PatGroup, b)] -> TcRnIf DsGblEnv DsLclEnv ()
debug [NonEmpty (PatGroup, EquationInfo)]
grouped)

        ; NonEmpty (MatchResult CoreExpr)
match_results <- [NonEmpty (PatGroup, EquationInfo)]
-> DsM (NonEmpty (MatchResult CoreExpr))
match_groups [NonEmpty (PatGroup, EquationInfo)]
grouped
        ; MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (MatchResult CoreExpr -> DsM (MatchResult CoreExpr))
-> MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$ (DsWrapper -> DsWrapper -> DsWrapper)
-> DsWrapper -> [DsWrapper] -> DsWrapper
forall a b. (a -> b -> b) -> b -> [a] -> b
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 DsWrapper -> MatchResult CoreExpr -> MatchResult CoreExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
            (MatchResult CoreExpr
 -> MatchResult CoreExpr -> MatchResult CoreExpr)
-> NonEmpty (MatchResult CoreExpr) -> MatchResult CoreExpr
forall a. (a -> a -> a) -> NonEmpty a -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 MatchResult CoreExpr
-> MatchResult CoreExpr -> MatchResult CoreExpr
combineMatchResults NonEmpty (MatchResult CoreExpr)
match_results
        }
  where
    vars :: NonEmpty Id
vars = Id
v Id -> [Id] -> NonEmpty Id
forall a. a -> [a] -> NonEmpty a
:| [Id]
vs

    dropGroup :: Functor f => f (PatGroup,EquationInfo) -> f EquationInfo
    dropGroup :: forall (f :: * -> *).
Functor f =>
f (PatGroup, EquationInfo) -> f EquationInfo
dropGroup = ((PatGroup, EquationInfo) -> EquationInfo)
-> f (PatGroup, EquationInfo) -> f EquationInfo
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PatGroup, EquationInfo) -> EquationInfo
forall a b. (a, b) -> b
snd

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

    match_group :: NonEmpty (PatGroup,EquationInfo) -> DsM (MatchResult CoreExpr)
    match_group :: NonEmpty (PatGroup, EquationInfo) -> DsM (MatchResult CoreExpr)
match_group eqns :: NonEmpty (PatGroup, EquationInfo)
eqns@((PatGroup
group,EquationInfo
_) :| [(PatGroup, EquationInfo)]
_)
        = case PatGroup
group of
            PgCon {}  -> NonEmpty Id
-> Type
-> NonEmpty (NonEmpty EquationInfo)
-> DsM (MatchResult CoreExpr)
matchConFamily  NonEmpty Id
vars Type
ty ([NonEmpty EquationInfo] -> NonEmpty (NonEmpty EquationInfo)
forall {a}. [a] -> NonEmpty a
ne ([NonEmpty EquationInfo] -> NonEmpty (NonEmpty EquationInfo))
-> [NonEmpty EquationInfo] -> NonEmpty (NonEmpty EquationInfo)
forall a b. (a -> b) -> a -> b
$ [(DataCon, EquationInfo)] -> [NonEmpty EquationInfo]
forall a.
Uniquable a =>
[(a, EquationInfo)] -> [NonEmpty EquationInfo]
subGroupUniq [(DataCon
c,EquationInfo
e) | (PgCon DataCon
c, EquationInfo
e) <- [(PatGroup, EquationInfo)]
eqns'])
            PgSyn {}  -> NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchPatSyn     NonEmpty Id
vars Type
ty (NonEmpty (PatGroup, EquationInfo) -> NonEmpty EquationInfo
forall (f :: * -> *).
Functor f =>
f (PatGroup, EquationInfo) -> f EquationInfo
dropGroup NonEmpty (PatGroup, EquationInfo)
eqns)
            PgLit {}  -> NonEmpty Id
-> Type
-> NonEmpty (NonEmpty EquationInfo)
-> DsM (MatchResult CoreExpr)
matchLiterals   NonEmpty Id
vars Type
ty ([NonEmpty EquationInfo] -> NonEmpty (NonEmpty EquationInfo)
forall {a}. [a] -> NonEmpty a
ne ([NonEmpty EquationInfo] -> NonEmpty (NonEmpty EquationInfo))
-> [NonEmpty EquationInfo] -> NonEmpty (NonEmpty EquationInfo)
forall a b. (a -> b) -> a -> b
$ [(Literal, EquationInfo)] -> [NonEmpty EquationInfo]
forall a. Ord a => [(a, EquationInfo)] -> [NonEmpty EquationInfo]
subGroupOrd [(Literal
l,EquationInfo
e) | (PgLit Literal
l, EquationInfo
e) <- [(PatGroup, EquationInfo)]
eqns'])
            PatGroup
PgAny     -> NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchVariables  NonEmpty Id
vars Type
ty (NonEmpty (PatGroup, EquationInfo) -> NonEmpty EquationInfo
forall (f :: * -> *).
Functor f =>
f (PatGroup, EquationInfo) -> f EquationInfo
dropGroup NonEmpty (PatGroup, EquationInfo)
eqns)
            PgN {}    -> NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchNPats      NonEmpty Id
vars Type
ty (NonEmpty (PatGroup, EquationInfo) -> NonEmpty EquationInfo
forall (f :: * -> *).
Functor f =>
f (PatGroup, EquationInfo) -> f EquationInfo
dropGroup NonEmpty (PatGroup, EquationInfo)
eqns)
            PgOverS {}-> NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchNPats      NonEmpty Id
vars Type
ty (NonEmpty (PatGroup, EquationInfo) -> NonEmpty EquationInfo
forall (f :: * -> *).
Functor f =>
f (PatGroup, EquationInfo) -> f EquationInfo
dropGroup NonEmpty (PatGroup, EquationInfo)
eqns)
            PgNpK {}  -> NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchNPlusKPats NonEmpty Id
vars Type
ty (NonEmpty (PatGroup, EquationInfo) -> NonEmpty EquationInfo
forall (f :: * -> *).
Functor f =>
f (PatGroup, EquationInfo) -> f EquationInfo
dropGroup NonEmpty (PatGroup, EquationInfo)
eqns)
            PatGroup
PgBang    -> NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchBangs      NonEmpty Id
vars Type
ty (NonEmpty (PatGroup, EquationInfo) -> NonEmpty EquationInfo
forall (f :: * -> *).
Functor f =>
f (PatGroup, EquationInfo) -> f EquationInfo
dropGroup NonEmpty (PatGroup, EquationInfo)
eqns)
            PgCo {}   -> NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchCoercion   NonEmpty Id
vars Type
ty (NonEmpty (PatGroup, EquationInfo) -> NonEmpty EquationInfo
forall (f :: * -> *).
Functor f =>
f (PatGroup, EquationInfo) -> f EquationInfo
dropGroup NonEmpty (PatGroup, EquationInfo)
eqns)
            PgView {} -> NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchView       NonEmpty Id
vars Type
ty (NonEmpty (PatGroup, EquationInfo) -> NonEmpty EquationInfo
forall (f :: * -> *).
Functor f =>
f (PatGroup, EquationInfo) -> f EquationInfo
dropGroup NonEmpty (PatGroup, EquationInfo)
eqns)
      where eqns' :: [(PatGroup, EquationInfo)]
eqns' = NonEmpty (PatGroup, EquationInfo) -> [(PatGroup, EquationInfo)]
forall a. NonEmpty a -> [a]
NEL.toList NonEmpty (PatGroup, EquationInfo)
eqns
            ne :: [a] -> NonEmpty a
ne [a]
l = case [a] -> Maybe (NonEmpty a)
forall a. [a] -> Maybe (NonEmpty a)
NEL.nonEmpty [a]
l of
              Just NonEmpty a
nel -> NonEmpty a
nel
              Maybe (NonEmpty a)
Nothing -> String -> SDoc -> NonEmpty a
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"match match_group" (SDoc -> NonEmpty a) -> SDoc -> NonEmpty a
forall a b. (a -> b) -> a -> b
$ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"Empty result should be impossible since input was non-empty"

    -- 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 [t (PatGroup, b)]
eqns =
        let gs :: [[GenLocated SrcSpanAnnA (HsExpr GhcTc)]]
gs = (t (PatGroup, b) -> [GenLocated SrcSpanAnnA (HsExpr GhcTc)])
-> [t (PatGroup, b)] -> [[GenLocated SrcSpanAnnA (HsExpr GhcTc)]]
forall a b. (a -> b) -> [a] -> [b]
map (\t (PatGroup, b)
group -> ((PatGroup, b)
 -> [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
 -> [GenLocated SrcSpanAnnA (HsExpr GhcTc)])
-> [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
-> t (PatGroup, b)
-> [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ (PatGroup
p,b
_) -> \[GenLocated SrcSpanAnnA (HsExpr GhcTc)]
acc ->
                                           case PatGroup
p of PgView LHsExpr GhcTc
e Type
_ -> LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
eGenLocated SrcSpanAnnA (HsExpr GhcTc)
-> [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
-> [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
forall a. a -> [a] -> [a]
:[GenLocated SrcSpanAnnA (HsExpr GhcTc)]
acc
                                                     PatGroup
_ -> [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
acc) [] t (PatGroup, b)
group) [t (PatGroup, b)]
eqns
            maybeWarn :: [[GenLocated SrcSpanAnnA (HsExpr GhcTc)]]
-> TcRnIf DsGblEnv DsLclEnv ()
maybeWarn [] = () -> TcRnIf DsGblEnv DsLclEnv ()
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            maybeWarn [[GenLocated SrcSpanAnnA (HsExpr GhcTc)]]
l  = DsMessage -> TcRnIf DsGblEnv DsLclEnv ()
diagnosticDs ([[LHsExpr GhcTc]] -> DsMessage
DsAggregatedViewExpressions [[LHsExpr GhcTc]]
[[GenLocated SrcSpanAnnA (HsExpr GhcTc)]]
l)
        in
          [[GenLocated SrcSpanAnnA (HsExpr GhcTc)]]
-> TcRnIf DsGblEnv DsLclEnv ()
maybeWarn ([[GenLocated SrcSpanAnnA (HsExpr GhcTc)]]
 -> TcRnIf DsGblEnv DsLclEnv ())
-> [[GenLocated SrcSpanAnnA (HsExpr GhcTc)]]
-> TcRnIf DsGblEnv DsLclEnv ()
forall a b. (a -> b) -> a -> b
$ ([GenLocated SrcSpanAnnA (HsExpr GhcTc)] -> Bool)
-> [[GenLocated SrcSpanAnnA (HsExpr GhcTc)]]
-> [[GenLocated SrcSpanAnnA (HsExpr GhcTc)]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> ([GenLocated SrcSpanAnnA (HsExpr GhcTc)] -> Bool)
-> [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [GenLocated SrcSpanAnnA (HsExpr GhcTc)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) [[GenLocated SrcSpanAnnA (HsExpr GhcTc)]]
gs

matchEmpty :: MatchId -> Type -> DsM (NonEmpty (MatchResult CoreExpr))
-- See Note [Empty case alternatives]
matchEmpty :: Id -> Type -> DsM (NonEmpty (MatchResult CoreExpr))
matchEmpty Id
var Type
res_ty
  = NonEmpty (MatchResult CoreExpr)
-> DsM (NonEmpty (MatchResult CoreExpr))
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [(CoreExpr -> DsM CoreExpr) -> MatchResult CoreExpr
forall a. (CoreExpr -> DsM a) -> MatchResult a
MR_Fallible CoreExpr -> DsM CoreExpr
mk_seq]
  where
    mk_seq :: CoreExpr -> DsM CoreExpr
mk_seq CoreExpr
fail = CoreExpr -> DsM CoreExpr
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> DsM CoreExpr) -> CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ CoreExpr -> Scaled Type -> Type -> [CoreAlt] -> CoreExpr
mkWildCase (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
var) (Id -> Scaled Type
idScaledType Id
var) Type
res_ty
                                      [AltCon -> [Id] -> CoreExpr -> CoreAlt
forall b. AltCon -> [b] -> Expr b -> Alt b
Alt AltCon
DEFAULT [] CoreExpr
fail]

matchVariables :: NonEmpty MatchId -> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
-- 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 :: NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchVariables (Id
_ :| [Id]
vars) Type
ty NonEmpty EquationInfo
eqns = [Id] -> Type -> [EquationInfo] -> DsM (MatchResult CoreExpr)
match [Id]
vars Type
ty ([EquationInfo] -> DsM (MatchResult CoreExpr))
-> [EquationInfo] -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$ NonEmpty EquationInfo -> [EquationInfo]
forall a. NonEmpty a -> [a]
NEL.toList (NonEmpty EquationInfo -> [EquationInfo])
-> NonEmpty EquationInfo -> [EquationInfo]
forall a b. (a -> b) -> a -> b
$ NonEmpty EquationInfo -> NonEmpty EquationInfo
forall (f :: * -> *). Functor f => f EquationInfo -> f EquationInfo
shiftEqns NonEmpty EquationInfo
eqns

matchBangs :: NonEmpty MatchId -> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchBangs :: NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchBangs (Id
var :| [Id]
vars) Type
ty NonEmpty EquationInfo
eqns
  = do  { MatchResult CoreExpr
match_result <- [Id] -> Type -> [EquationInfo] -> DsM (MatchResult CoreExpr)
match (Id
varId -> [Id] -> [Id]
forall a. a -> [a] -> [a]
:[Id]
vars) Type
ty ([EquationInfo] -> DsM (MatchResult CoreExpr))
-> [EquationInfo] -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$ NonEmpty EquationInfo -> [EquationInfo]
forall a. NonEmpty a -> [a]
NEL.toList (NonEmpty EquationInfo -> [EquationInfo])
-> NonEmpty EquationInfo -> [EquationInfo]
forall a b. (a -> b) -> a -> b
$
            (Pat GhcTc -> Pat GhcTc) -> EquationInfo -> EquationInfo
decomposeFirstPat Pat GhcTc -> Pat GhcTc
getBangPat (EquationInfo -> EquationInfo)
-> NonEmpty EquationInfo -> NonEmpty EquationInfo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty EquationInfo
eqns
        ; MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> Type -> MatchResult CoreExpr -> MatchResult CoreExpr
mkEvalMatchResult Id
var Type
ty MatchResult CoreExpr
match_result) }

matchCoercion :: NonEmpty MatchId -> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
-- Apply the coercion to the match variable and then match that
matchCoercion :: NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchCoercion (Id
var :| [Id]
vars) Type
ty (eqns :: NonEmpty EquationInfo
eqns@(EquationInfo
eqn1 :| [EquationInfo]
_))
  = do  { let XPat (CoPat HsWrapper
co Pat GhcTc
pat Type
_) = EquationInfo -> Pat GhcTc
firstPat EquationInfo
eqn1
        ; let pat_ty' :: Type
pat_ty' = Pat GhcTc -> Type
hsPatType Pat GhcTc
pat
        ; Id
var' <- Id -> Type -> Type -> DsM Id
newUniqueId Id
var (Id -> Type
idMult Id
var) Type
pat_ty'
        ; MatchResult CoreExpr
match_result <- [Id] -> Type -> [EquationInfo] -> DsM (MatchResult CoreExpr)
match (Id
var'Id -> [Id] -> [Id]
forall a. a -> [a] -> [a]
:[Id]
vars) Type
ty ([EquationInfo] -> DsM (MatchResult CoreExpr))
-> [EquationInfo] -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$ NonEmpty EquationInfo -> [EquationInfo]
forall a. NonEmpty a -> [a]
NEL.toList (NonEmpty EquationInfo -> [EquationInfo])
-> NonEmpty EquationInfo -> [EquationInfo]
forall a b. (a -> b) -> a -> b
$
            (Pat GhcTc -> Pat GhcTc) -> EquationInfo -> EquationInfo
decomposeFirstPat Pat GhcTc -> Pat GhcTc
getCoPat (EquationInfo -> EquationInfo)
-> NonEmpty EquationInfo -> NonEmpty EquationInfo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty EquationInfo
eqns
        ; HsWrapper
-> (DsWrapper -> DsM (MatchResult CoreExpr))
-> DsM (MatchResult CoreExpr)
forall a. HsWrapper -> (DsWrapper -> DsM a) -> DsM a
dsHsWrapper HsWrapper
co ((DsWrapper -> DsM (MatchResult CoreExpr))
 -> DsM (MatchResult CoreExpr))
-> (DsWrapper -> DsM (MatchResult CoreExpr))
-> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$ \DsWrapper
core_wrap -> do
        { let bind :: Bind Id
bind = Id -> CoreExpr -> Bind Id
forall b. b -> Expr b -> Bind b
NonRec Id
var' (DsWrapper
core_wrap (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
var))
        ; MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bind Id -> MatchResult CoreExpr -> MatchResult CoreExpr
mkCoLetMatchResult Bind Id
bind MatchResult CoreExpr
match_result) } }

matchView :: NonEmpty MatchId -> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
-- Apply the view function to the match variable and then match that
matchView :: NonEmpty Id
-> Type -> NonEmpty EquationInfo -> DsM (MatchResult CoreExpr)
matchView (Id
var :| [Id]
vars) Type
ty (eqns :: NonEmpty EquationInfo
eqns@(EquationInfo
eqn1 :| [EquationInfo]
_))
  = 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 TcViewPat HsExpr GhcTc
viewExpr 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 Pat GhcTc
pat
        ; Id
var' <- Id -> Type -> Type -> DsM Id
newUniqueId Id
var (Id -> Type
idMult Id
var) Type
pat_ty'
        ; MatchResult CoreExpr
match_result <- [Id] -> Type -> [EquationInfo] -> DsM (MatchResult CoreExpr)
match (Id
var'Id -> [Id] -> [Id]
forall a. a -> [a] -> [a]
:[Id]
vars) Type
ty ([EquationInfo] -> DsM (MatchResult CoreExpr))
-> [EquationInfo] -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$ NonEmpty EquationInfo -> [EquationInfo]
forall a. NonEmpty a -> [a]
NEL.toList (NonEmpty EquationInfo -> [EquationInfo])
-> NonEmpty EquationInfo -> [EquationInfo]
forall a b. (a -> b) -> a -> b
$
            (Pat GhcTc -> Pat GhcTc) -> EquationInfo -> EquationInfo
decomposeFirstPat Pat GhcTc -> Pat GhcTc
getViewPat (EquationInfo -> EquationInfo)
-> NonEmpty EquationInfo -> NonEmpty EquationInfo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty EquationInfo
eqns
         -- compile the view expressions
        ; CoreExpr
viewExpr' <- HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
viewExpr
        ; MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> CoreExpr -> MatchResult CoreExpr -> MatchResult CoreExpr
mkViewMatchResult Id
var'
                    (SDoc -> CoreExpr -> DsWrapper
mkCoreAppDs (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"matchView") CoreExpr
viewExpr' (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
var))
                    MatchResult CoreExpr
match_result) }

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

getCoPat, getBangPat, getViewPat :: Pat GhcTc -> Pat GhcTc
getCoPat :: Pat GhcTc -> Pat GhcTc
getCoPat (XPat (CoPat HsWrapper
_ Pat GhcTc
pat Type
_)) = Pat GhcTc
pat
getCoPat Pat GhcTc
_                   = String -> Pat GhcTc
forall a. HasCallStack => String -> a
panic String
"getCoPat"
getBangPat :: Pat GhcTc -> Pat GhcTc
getBangPat (BangPat XBangPat GhcTc
_ LPat GhcTc
pat  ) = GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat
getBangPat Pat GhcTc
_                 = String -> Pat GhcTc
forall a. HasCallStack => String -> a
panic String
"getBangPat"
getViewPat :: Pat GhcTc -> Pat GhcTc
getViewPat (TcViewPat HsExpr GhcTc
_ Pat GhcTc
pat) = Pat GhcTc
pat
getViewPat Pat GhcTc
_                 = String -> Pat GhcTc
forall a. HasCallStack => String -> a
panic String
"getViewPat"

-- | Use this pattern synonym to match on a 'ViewPat'.
--
-- N.B.: View patterns can occur inside HsExpansions.
pattern TcViewPat :: HsExpr GhcTc -> Pat GhcTc -> Pat GhcTc
pattern $mTcViewPat :: forall {r}.
Pat GhcTc -> (HsExpr GhcTc -> Pat GhcTc -> r) -> ((# #) -> r) -> r
TcViewPat viewExpr pat <- (getTcViewPat -> (viewExpr, pat))

getTcViewPat :: Pat GhcTc -> (HsExpr GhcTc, Pat GhcTc)
getTcViewPat :: Pat GhcTc -> (HsExpr GhcTc, Pat GhcTc)
getTcViewPat (ViewPat XViewPat GhcTc
_ LHsExpr GhcTc
viewLExpr LPat GhcTc
pat)  = (GenLocated SrcSpanAnnA (HsExpr GhcTc) -> HsExpr GhcTc
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
viewLExpr, GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat)
getTcViewPat (XPat (ExpansionPat  Pat (GhcPass 'Renamed)
_ Pat GhcTc
p)) = Pat GhcTc -> (HsExpr GhcTc, Pat GhcTc)
getTcViewPat Pat GhcTc
p
getTcViewPat Pat GhcTc
p = String -> SDoc -> (HsExpr GhcTc, Pat GhcTc)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"getTcViewPat" (Pat GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr Pat GhcTc
p)

{-
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 GHC.Core.)

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 the "lifted case" discussion in Note [Case elimination] in GHC.Core.Opt.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 :: Id
-> EquationInfo
-> IOEnv (Env DsGblEnv DsLclEnv) (DsWrapper, EquationInfo)
tidyEqnInfo Id
_ (EqnInfo { eqn_pats :: EquationInfo -> [Pat GhcTc]
eqn_pats = [] })
  = String -> IOEnv (Env DsGblEnv DsLclEnv) (DsWrapper, EquationInfo)
forall a. HasCallStack => String -> a
panic String
"tidyEqnInfo"

tidyEqnInfo Id
v eqn :: EquationInfo
eqn@(EqnInfo { eqn_pats :: EquationInfo -> [Pat GhcTc]
eqn_pats = Pat GhcTc
pat : [Pat GhcTc]
pats, eqn_orig :: EquationInfo -> Origin
eqn_orig = Origin
orig })
  = do { (DsWrapper
wrap, Pat GhcTc
pat') <- Id -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 Id
v Origin
orig Pat GhcTc
pat
       ; (DsWrapper, EquationInfo)
-> IOEnv (Env DsGblEnv DsLclEnv) (DsWrapper, EquationInfo)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
wrap, EquationInfo
eqn { eqn_pats = pat' : 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 :: Id -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 Id
v Origin
o (ParPat XParPat GhcTc
_ LHsToken "(" GhcTc
_ LPat GhcTc
pat LHsToken ")" GhcTc
_)  = Id -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 Id
v Origin
o (GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat)
tidy1 Id
v Origin
o (SigPat XSigPat GhcTc
_ LPat GhcTc
pat HsPatSigType (NoGhcTc GhcTc)
_)    = Id -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 Id
v Origin
o (GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat)
tidy1 Id
_ Origin
_ (WildPat XWildPat GhcTc
ty)        = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, XWildPat GhcTc -> Pat GhcTc
forall p. XWildPat p -> Pat p
WildPat XWildPat GhcTc
ty)
tidy1 Id
v Origin
o (BangPat XBangPat GhcTc
_ (L SrcSpanAnnA
l Pat GhcTc
p)) = Id
-> Origin -> SrcSpanAnnA -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy_bang_pat Id
v Origin
o SrcSpanAnnA
l Pat GhcTc
p

        -- case v of { x -> mr[] }
        -- = case v of { _ -> let x=v in mr[] }
tidy1 Id
v Origin
_ (VarPat XVarPat GhcTc
_ (L SrcSpanAnnN
_ Id
var))
  = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> Id -> DsWrapper
wrapBind Id
var Id
v, XWildPat GhcTc -> Pat GhcTc
forall p. XWildPat p -> Pat p
WildPat (Id -> Type
idType Id
var))

        -- case v of { x@p -> mr[] }
        -- = case v of { p -> let x=v in mr[] }
tidy1 Id
v Origin
o (AsPat XAsPat GhcTc
_ (L SrcSpanAnnN
_ Id
var) LHsToken "@" GhcTc
_ LPat GhcTc
pat)
  = do  { (DsWrapper
wrap, Pat GhcTc
pat') <- Id -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 Id
v Origin
o (GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat)
        ; (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> Id -> DsWrapper
wrapBind Id
var Id
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 Id
v Origin
_ (LazyPat XLazyPat GhcTc
_ LPat 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 :: [Id]
unlifted_bndrs = (Id -> Bool) -> [Id] -> [Id]
forall a. (a -> Bool) -> [a] -> [a]
filter (HasDebugCallStack => Type -> Bool
Type -> Bool
isUnliftedType (Type -> Bool) -> (Id -> Type) -> Id -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id -> Type
idType) (CollectFlag GhcTc -> LPat GhcTc -> [IdP GhcTc]
forall p. CollectPass p => CollectFlag p -> LPat p -> [IdP p]
collectPatBinders CollectFlag GhcTc
forall p. CollectFlag p
CollNoDictBinders LPat GhcTc
pat)
            -- NB: the binders can't be representation-polymorphic, so we're OK to call isUnliftedType
        ; Bool -> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Id] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Id]
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 (GenLocated SrcSpanAnnA (Pat GhcTc) -> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat) (TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ())
-> TcRnIf DsGblEnv DsLclEnv () -> TcRnIf DsGblEnv DsLclEnv ()
forall a b. (a -> b) -> a -> b
$
          DsMessage -> TcRnIf DsGblEnv DsLclEnv ()
diagnosticDs ([Id] -> DsMessage
DsLazyPatCantBindVarsOfUnliftedType [Id]
unlifted_bndrs)

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

tidy1 Id
_ Origin
_ (ListPat XListPat GhcTc
ty [LPat GhcTc]
pats)
  = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnA (Pat GhcTc)
list_ConPat)
  where
    list_ConPat :: GenLocated SrcSpanAnnA (Pat GhcTc)
list_ConPat = (GenLocated SrcSpanAnnA (Pat GhcTc)
 -> GenLocated SrcSpanAnnA (Pat GhcTc)
 -> GenLocated SrcSpanAnnA (Pat GhcTc))
-> GenLocated SrcSpanAnnA (Pat GhcTc)
-> [GenLocated SrcSpanAnnA (Pat GhcTc)]
-> GenLocated SrcSpanAnnA (Pat GhcTc)
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ GenLocated SrcSpanAnnA (Pat GhcTc)
x GenLocated SrcSpanAnnA (Pat GhcTc)
y -> DataCon -> [LPat GhcTc] -> [Type] -> LPat GhcTc
mkPrefixConPat DataCon
consDataCon [Item [GenLocated SrcSpanAnnA (Pat GhcTc)]
GenLocated SrcSpanAnnA (Pat GhcTc)
x, Item [GenLocated SrcSpanAnnA (Pat GhcTc)]
GenLocated SrcSpanAnnA (Pat GhcTc)
y] [Item [Type]
XListPat GhcTc
ty])
                        (Type -> LPat GhcTc
mkNilPat XListPat GhcTc
Type
ty)
                        [LPat GhcTc]
[GenLocated SrcSpanAnnA (Pat GhcTc)]
pats

tidy1 Id
_ Origin
_ (TuplePat XTuplePat GhcTc
tys [LPat GhcTc]
pats Boxity
boxity)
  = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
tuple_ConPat)
  where
    arity :: Int
arity = [GenLocated SrcSpanAnnA (Pat GhcTc)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LPat GhcTc]
[GenLocated SrcSpanAnnA (Pat GhcTc)]
pats
    tuple_ConPat :: LPat GhcTc
tuple_ConPat = DataCon -> [LPat GhcTc] -> [Type] -> LPat GhcTc
mkPrefixConPat (Boxity -> Int -> DataCon
tupleDataCon Boxity
boxity Int
arity) [LPat GhcTc]
pats [Type]
tys'
    tys' :: [Type]
tys' = case Boxity
boxity of
             Boxity
Unboxed -> (Type -> Type) -> [Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep [Type]
XTuplePat GhcTc
tys [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
XTuplePat GhcTc
tys
             Boxity
Boxed   -> [Type]
XTuplePat GhcTc
tys
           -- See Note [Unboxed tuple RuntimeRep vars] in TyCon

tidy1 Id
_ Origin
_ (SumPat XSumPat GhcTc
tys LPat GhcTc
pat Int
alt Int
arity)
  = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
sum_ConPat)
  where
    sum_ConPat :: LPat GhcTc
sum_ConPat = DataCon -> [LPat GhcTc] -> [Type] -> LPat GhcTc
mkPrefixConPat (Int -> Int -> DataCon
sumDataCon Int
alt Int
arity) [Item [GenLocated SrcSpanAnnA (Pat GhcTc)]
LPat GhcTc
pat] ((Type -> Type) -> [Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep [Type]
XSumPat GhcTc
tys [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
XSumPat GhcTc
tys)
                 -- See Note [Unboxed tuple RuntimeRep vars] in TyCon

-- LitPats: we *might* be able to replace these w/ a simpler form
tidy1 Id
_ Origin
o (LitPat XLitPat GhcTc
_ 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 a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
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 Id
_ Origin
o (NPat XNPat GhcTc
ty (L SrcAnn NoEpAnns
_ lit :: HsOverLit GhcTc
lit@OverLit { ol_val :: forall p. HsOverLit p -> OverLitVal
ol_val = OverLitVal
v }) Maybe (SyntaxExpr GhcTc)
mb_neg 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 SyntaxExpr GhcTc
_ <- Maybe (SyntaxExpr GhcTc)
mb_neg = HsOverLit GhcTc
lit{ ol_val = negateOverLitVal v }
                    | Bool
otherwise = HsOverLit GhcTc
lit
           in HsOverLit GhcTc -> TcRnIf DsGblEnv DsLclEnv ()
warnAboutOverflowedOverLit HsOverLit GhcTc
lit'
       ; (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, HsOverLit GhcTc
-> Maybe (SyntaxExpr GhcTc)
-> SyntaxExpr GhcTc
-> Type
-> Pat GhcTc
tidyNPat HsOverLit GhcTc
lit Maybe (SyntaxExpr GhcTc)
mb_neg SyntaxExpr GhcTc
eq XNPat GhcTc
Type
ty) }

-- NPlusKPat: we may want to warn about the literals
tidy1 Id
_ Origin
o n :: Pat GhcTc
n@(NPlusKPat XNPlusKPat GhcTc
_ LIdP GhcTc
_ (L SrcAnn NoEpAnns
_ HsOverLit GhcTc
lit1) HsOverLit GhcTc
lit2 SyntaxExpr GhcTc
_ SyntaxExpr GhcTc
_)
  = 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 HsOverLit GhcTc
lit1
           HsOverLit GhcTc -> TcRnIf DsGblEnv DsLclEnv ()
warnAboutOverflowedOverLit HsOverLit GhcTc
lit2
       ; (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, Pat GhcTc
n) }

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

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

-- Discard par/sig under a bang
tidy_bang_pat :: Id
-> Origin -> SrcSpanAnnA -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy_bang_pat Id
v Origin
o SrcSpanAnnA
_ (ParPat XParPat GhcTc
_ LHsToken "(" GhcTc
_ (L SrcSpanAnnA
l Pat GhcTc
p) LHsToken ")" GhcTc
_) = Id
-> Origin -> SrcSpanAnnA -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy_bang_pat Id
v Origin
o SrcSpanAnnA
l Pat GhcTc
p
tidy_bang_pat Id
v Origin
o SrcSpanAnnA
_ (SigPat XSigPat GhcTc
_ (L SrcSpanAnnA
l Pat GhcTc
p) HsPatSigType (NoGhcTc GhcTc)
_) = Id
-> Origin -> SrcSpanAnnA -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy_bang_pat Id
v Origin
o SrcSpanAnnA
l Pat GhcTc
p

-- Push the bang-pattern inwards, in the hope that
-- it may disappear next time
tidy_bang_pat Id
v Origin
o SrcSpanAnnA
l (AsPat XAsPat GhcTc
x LIdP GhcTc
v' LHsToken "@" GhcTc
at LPat GhcTc
p)
  = Id -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 Id
v Origin
o (XAsPat GhcTc
-> LIdP GhcTc -> LHsToken "@" GhcTc -> LPat GhcTc -> Pat GhcTc
forall p. XAsPat p -> LIdP p -> LHsToken "@" p -> LPat p -> Pat p
AsPat XAsPat GhcTc
x LIdP GhcTc
v' LHsToken "@" GhcTc
at (SrcSpanAnnA -> Pat GhcTc -> GenLocated SrcSpanAnnA (Pat GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (XBangPat GhcTc -> LPat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> Pat p
BangPat XBangPat GhcTc
NoExtField
noExtField LPat GhcTc
p)))
tidy_bang_pat Id
v Origin
o SrcSpanAnnA
l (XPat (CoPat HsWrapper
w Pat GhcTc
p Type
t))
  = Id -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 Id
v Origin
o (XXPat GhcTc -> Pat GhcTc
forall p. XXPat p -> Pat p
XPat (XXPat GhcTc -> Pat GhcTc) -> XXPat GhcTc -> Pat GhcTc
forall a b. (a -> b) -> a -> b
$ HsWrapper -> Pat GhcTc -> Type -> XXPatGhcTc
CoPat HsWrapper
w (XBangPat GhcTc -> LPat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> Pat p
BangPat XBangPat GhcTc
NoExtField
noExtField (SrcSpanAnnA -> Pat GhcTc -> GenLocated SrcSpanAnnA (Pat GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l Pat GhcTc
p)) Type
t)

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

-- Data/newtype constructors
tidy_bang_pat Id
v Origin
o SrcSpanAnnA
l p :: Pat GhcTc
p@(ConPat { pat_con :: forall p. Pat p -> XRec p (ConLikeP p)
pat_con = L SrcSpanAnnN
_ (RealDataCon DataCon
dc)
                              , pat_args :: forall p. Pat p -> HsConPatDetails p
pat_args = HsConPatDetails GhcTc
args
                              , pat_con_ext :: forall p. Pat p -> XConPat p
pat_con_ext = ConPatTc
                                { cpt_arg_tys :: ConPatTc -> [Type]
cpt_arg_tys = [Type]
arg_tys
                                }
                              })
  -- Newtypes: push bang inwards (#9844)
  =
    if TyCon -> Bool
isNewTyCon (DataCon -> TyCon
dataConTyCon DataCon
dc)
      then Id -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 Id
v Origin
o (Pat GhcTc
p { pat_args = push_bang_into_newtype_arg l (scaledThing ty) args })
      else Id -> Origin -> Pat GhcTc -> DsM (DsWrapper, Pat GhcTc)
tidy1 Id
v Origin
o Pat GhcTc
p  -- Data types: discard the bang
    where
      (Scaled Type
ty:[Scaled Type]
_) = DataCon -> [Type] -> [Scaled 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!  (#8952)
--
-- NB: SigPatIn, ConPatIn should not happen

tidy_bang_pat Id
_ Origin
_ SrcSpanAnnA
l Pat GhcTc
p = (DsWrapper, Pat GhcTc) -> DsM (DsWrapper, Pat GhcTc)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (DsWrapper
idDsWrapper, XBangPat GhcTc -> LPat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> Pat p
BangPat XBangPat GhcTc
NoExtField
noExtField (SrcSpanAnnA -> Pat GhcTc -> GenLocated SrcSpanAnnA (Pat GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l Pat GhcTc
p))

-------------------
push_bang_into_newtype_arg :: SrcSpanAnnA
                           -> 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 :: SrcSpanAnnA
-> Type -> HsConPatDetails GhcTc -> HsConPatDetails GhcTc
push_bang_into_newtype_arg SrcSpanAnnA
l Type
_ty (PrefixCon [HsConPatTyArg (NoGhcTc GhcTc)]
ts (LPat GhcTc
arg:[LPat GhcTc]
args))
  = Bool -> HsConPatDetails GhcTc -> HsConPatDetails GhcTc
forall a. HasCallStack => Bool -> a -> a
assert ([GenLocated SrcSpanAnnA (Pat GhcTc)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LPat GhcTc]
[GenLocated SrcSpanAnnA (Pat GhcTc)]
args) (HsConPatDetails GhcTc -> HsConPatDetails GhcTc)
-> HsConPatDetails GhcTc -> HsConPatDetails GhcTc
forall a b. (a -> b) -> a -> b
$
    [HsConPatTyArg (GhcPass 'Renamed)]
-> [GenLocated SrcSpanAnnA (Pat GhcTc)]
-> HsConDetails
     (HsConPatTyArg (GhcPass 'Renamed))
     (GenLocated SrcSpanAnnA (Pat GhcTc))
     (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
forall tyarg arg rec.
[tyarg] -> [arg] -> HsConDetails tyarg arg rec
PrefixCon [HsConPatTyArg (NoGhcTc GhcTc)]
[HsConPatTyArg (GhcPass 'Renamed)]
ts [SrcSpanAnnA -> Pat GhcTc -> GenLocated SrcSpanAnnA (Pat GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (XBangPat GhcTc -> LPat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> Pat p
BangPat XBangPat GhcTc
NoExtField
noExtField LPat GhcTc
arg)]
push_bang_into_newtype_arg SrcSpanAnnA
l Type
_ty (RecCon HsRecFields GhcTc (LPat GhcTc)
rf)
  | HsRecFields { rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = L SrcSpanAnnA
lf HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
  (GenLocated SrcSpanAnnA (Pat GhcTc))
fld : [LHsRecField GhcTc (LPat GhcTc)]
flds } <- HsRecFields GhcTc (LPat GhcTc)
rf
  , HsFieldBind { hfbRHS :: forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS = GenLocated SrcSpanAnnA (Pat GhcTc)
arg } <- HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
  (GenLocated SrcSpanAnnA (Pat GhcTc))
fld
  = Bool -> HsConPatDetails GhcTc -> HsConPatDetails GhcTc
forall a. HasCallStack => Bool -> a -> a
assert ([GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (Pat GhcTc)))]
-> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LHsRecField GhcTc (LPat GhcTc)]
[GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (Pat GhcTc)))]
flds) (HsConPatDetails GhcTc -> HsConPatDetails GhcTc)
-> HsConPatDetails GhcTc -> HsConPatDetails GhcTc
forall a b. (a -> b) -> a -> b
$
    HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))
-> HsConDetails
     (HsConPatTyArg (GhcPass 'Renamed))
     (GenLocated SrcSpanAnnA (Pat GhcTc))
     (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
forall tyarg arg rec. rec -> HsConDetails tyarg arg rec
RecCon (HsRecFields GhcTc (LPat GhcTc)
rf { rec_flds = [L lf (fld { hfbRHS
                                           = L l (BangPat noExtField arg) })] })
push_bang_into_newtype_arg SrcSpanAnnA
l Type
ty (RecCon HsRecFields GhcTc (LPat GhcTc)
rf) -- If a user writes !(T {})
  | HsRecFields { rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [] } <- HsRecFields GhcTc (LPat GhcTc)
rf
  = [HsConPatTyArg (GhcPass 'Renamed)]
-> [GenLocated SrcSpanAnnA (Pat GhcTc)]
-> HsConDetails
     (HsConPatTyArg (GhcPass 'Renamed))
     (GenLocated SrcSpanAnnA (Pat GhcTc))
     (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
forall tyarg arg rec.
[tyarg] -> [arg] -> HsConDetails tyarg arg rec
PrefixCon [] [SrcSpanAnnA -> Pat GhcTc -> GenLocated SrcSpanAnnA (Pat GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (XBangPat GhcTc -> LPat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> Pat p
BangPat XBangPat GhcTc
NoExtField
noExtField (Pat GhcTc -> GenLocated SrcSpanAnnA (Pat GhcTc)
forall a an. a -> LocatedAn an a
noLocA (XWildPat GhcTc -> Pat GhcTc
forall p. XWildPat p -> Pat p
WildPat XWildPat GhcTc
Type
ty)))]
push_bang_into_newtype_arg SrcSpanAnnA
_ Type
_ HsConPatDetails GhcTc
cd
  = String
-> SDoc
-> HsConDetails
     (HsConPatTyArg (GhcPass 'Renamed))
     (GenLocated SrcSpanAnnA (Pat GhcTc))
     (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"push_bang_into_newtype_arg" (HsConPatDetails GhcTc -> SDoc
forall (p :: Pass).
(OutputableBndrId p, Outputable (Anno (IdGhcP 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.  #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}
-}

-- Note [matchWrapper scrutinees]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- There are three possible cases for matchWrapper's scrutinees argument:
--
-- 1. Nothing   Used for FunBind, HsLam, HsLamcase, where there is no explicit scrutinee
--              The MatchGroup may have matchGroupArity of 0 or more. Examples:
--                  f p1 q1 = ... -- matchGroupArity 2
--                  f p2 q2 = ...
--
--                  \cases | g1 -> ... -- matchGroupArity 0
--                         | g2 -> ...
--
-- 2. Just [e]  Used for HsCase, RecordUpd; exactly one scrutinee
--              The MatchGroup has matchGroupArity of exactly 1. Example:
--                  case e of p1 -> e1 -- matchGroupArity 1
--                            p2 -> e2
--
-- 3. Just es   Used for HsCmdLamCase; zero or more scrutinees
--              The MatchGroup has matchGroupArity of (length es). Example:
--                  \cases p1 q1 -> returnA -< ... -- matchGroupArity 2
--                         p2 q2 -> ...

matchWrapper
  :: HsMatchContext GhcTc              -- ^ For shadowing warning messages
  -> Maybe [LHsExpr GhcTc]             -- ^ Scrutinee(s)
                                       -- see Note [matchWrapper scrutinees]
  -> 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 @GHC.HsToCore.Expr.doDo@ function)
\item @let@ patterns, are treated by @matchSimply@
   List Comprehension 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 GhcTc
-> Maybe [LHsExpr GhcTc]
-> MatchGroup GhcTc (LHsExpr GhcTc)
-> DsM ([Id], CoreExpr)
matchWrapper HsMatchContext GhcTc
ctxt Maybe [LHsExpr GhcTc]
scrs (MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts = L SrcSpanAnnL
_ [GenLocated
   SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
matches
                           , mg_ext :: forall p body. MatchGroup p body -> XMG p body
mg_ext = MatchGroupTc [Scaled Type]
arg_tys Type
rhs_ty Origin
origin
                           })
  = do  { DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
        ; SrcSpan
locn   <- DsM SrcSpan
getSrcSpanDs

        ; [Id]
new_vars    <- case [GenLocated
   SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
matches of
                           []    -> [Scaled Type] -> IOEnv (Env DsGblEnv DsLclEnv) [Id]
newSysLocalsDs [Scaled Type]
arg_tys
                           (GenLocated
  SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
m:[GenLocated
   SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
_) ->
                            [(Type, Pat GhcTc)] -> IOEnv (Env DsGblEnv DsLclEnv) [Id]
selectMatchVars (String
-> (Scaled Type
    -> GenLocated SrcSpanAnnA (Pat GhcTc) -> (Type, Pat GhcTc))
-> [Scaled Type]
-> [GenLocated SrcSpanAnnA (Pat GhcTc)]
-> [(Type, Pat GhcTc)]
forall a b c.
HasDebugCallStack =>
String -> (a -> b -> c) -> [a] -> [b] -> [c]
zipWithEqual String
"matchWrapper"
                                              (\Scaled Type
a GenLocated SrcSpanAnnA (Pat GhcTc)
b -> (Scaled Type -> Type
forall a. Scaled a -> Type
scaledMult Scaled Type
a, GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnA (Pat GhcTc)
b))
                                                [Scaled Type]
arg_tys
                                                (LMatch GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> [LPat GhcTc]
forall (id :: Pass) body.
LMatch (GhcPass id) body -> [LPat (GhcPass id)]
hsLMatchPats LMatch GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
GenLocated
  SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
m))

        -- Pattern match check warnings for /this match-group/.
        -- @rhss_nablas@ is a flat list of covered Nablas for each RHS.
        -- Each Match will split off one Nablas for its RHSs from this.
        ; [(Nablas, NonEmpty Nablas)]
matches_nablas <-
            if DynFlags -> Origin -> HsMatchContext GhcTc -> Bool
forall id. DynFlags -> Origin -> HsMatchContext id -> Bool
isMatchContextPmChecked DynFlags
dflags Origin
origin HsMatchContext GhcTc
ctxt

            -- See Note [Long-distance information] in GHC.HsToCore.Pmc
            then [LHsExpr GhcTc]
-> [Id]
-> IOEnv (Env DsGblEnv DsLclEnv) [(Nablas, NonEmpty Nablas)]
-> IOEnv (Env DsGblEnv DsLclEnv) [(Nablas, NonEmpty Nablas)]
forall a. [LHsExpr GhcTc] -> [Id] -> DsM a -> DsM a
addHsScrutTmCs (Maybe [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
-> [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat Maybe [LHsExpr GhcTc]
Maybe [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
scrs) [Id]
new_vars (IOEnv (Env DsGblEnv DsLclEnv) [(Nablas, NonEmpty Nablas)]
 -> IOEnv (Env DsGblEnv DsLclEnv) [(Nablas, NonEmpty Nablas)])
-> IOEnv (Env DsGblEnv DsLclEnv) [(Nablas, NonEmpty Nablas)]
-> IOEnv (Env DsGblEnv DsLclEnv) [(Nablas, NonEmpty Nablas)]
forall a b. (a -> b) -> a -> b
$
                 DsMatchContext
-> [Id]
-> [LMatch GhcTc (LHsExpr GhcTc)]
-> IOEnv (Env DsGblEnv DsLclEnv) [(Nablas, NonEmpty Nablas)]
pmcMatches (HsMatchContext GhcTc -> SrcSpan -> DsMatchContext
DsMatchContext HsMatchContext GhcTc
ctxt SrcSpan
locn) [Id]
new_vars [LMatch GhcTc (LHsExpr GhcTc)]
[GenLocated
   SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
matches

            -- When we're not doing PM checks on the match group,
            -- we still need to propagate long-distance information.
            -- See Note [Long-distance information in matchWrapper]
            else do { Nablas
ldi_nablas <- DsM Nablas
getLdiNablas
                    ; [(Nablas, NonEmpty Nablas)]
-> IOEnv (Env DsGblEnv DsLclEnv) [(Nablas, NonEmpty Nablas)]
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(Nablas, NonEmpty Nablas)]
 -> IOEnv (Env DsGblEnv DsLclEnv) [(Nablas, NonEmpty Nablas)])
-> [(Nablas, NonEmpty Nablas)]
-> IOEnv (Env DsGblEnv DsLclEnv) [(Nablas, NonEmpty Nablas)]
forall a b. (a -> b) -> a -> b
$ Nablas
-> [LMatch GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
-> [(Nablas, NonEmpty Nablas)]
forall b. Nablas -> [LMatch GhcTc b] -> [(Nablas, NonEmpty Nablas)]
initNablasMatches Nablas
ldi_nablas [LMatch GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
[GenLocated
   SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
matches }

        ; [EquationInfo]
eqns_info   <- (GenLocated
   SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
 -> (Nablas, NonEmpty Nablas)
 -> IOEnv (Env DsGblEnv DsLclEnv) EquationInfo)
-> [GenLocated
      SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> [(Nablas, NonEmpty Nablas)]
-> IOEnv (Env DsGblEnv DsLclEnv) [EquationInfo]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM LMatch GhcTc (LHsExpr GhcTc)
-> (Nablas, NonEmpty Nablas)
-> IOEnv (Env DsGblEnv DsLclEnv) EquationInfo
GenLocated
  SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> (Nablas, NonEmpty Nablas)
-> IOEnv (Env DsGblEnv DsLclEnv) EquationInfo
mk_eqn_info [GenLocated
   SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
matches [(Nablas, NonEmpty Nablas)]
matches_nablas

        ; CoreExpr
result_expr <- Origin -> DsM CoreExpr -> DsM CoreExpr
forall {a}. Origin -> DsM a -> DsM a
discard_warnings_if_skip_pmc Origin
origin (DsM CoreExpr -> DsM CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
                         HsMatchContext GhcTc
-> [Id] -> [EquationInfo] -> Type -> DsM CoreExpr
matchEquations HsMatchContext GhcTc
ctxt [Id]
new_vars [EquationInfo]
eqns_info Type
rhs_ty

        ; ([Id], CoreExpr) -> DsM ([Id], CoreExpr)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Id]
new_vars, CoreExpr
result_expr) }
  where
    -- Called once per equation in the match, or alternative in the case
    mk_eqn_info :: LMatch GhcTc (LHsExpr GhcTc) -> (Nablas, NonEmpty Nablas) -> DsM EquationInfo
    mk_eqn_info :: LMatch GhcTc (LHsExpr GhcTc)
-> (Nablas, NonEmpty Nablas)
-> IOEnv (Env DsGblEnv DsLclEnv) EquationInfo
mk_eqn_info (L SrcSpanAnnA
_ (Match { m_pats :: forall p body. Match p body -> [LPat p]
m_pats = [LPat GhcTc]
pats, m_grhss :: forall p body. Match p body -> GRHSs p body
m_grhss = GRHSs GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
grhss })) (Nablas
pat_nablas, NonEmpty Nablas
rhss_nablas)
      = do { DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
           ; let upats :: [Pat GhcTc]
upats = (GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc)
-> [GenLocated SrcSpanAnnA (Pat GhcTc)] -> [Pat GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map (GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc (GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc)
-> (GenLocated SrcSpanAnnA (Pat GhcTc)
    -> GenLocated SrcSpanAnnA (Pat GhcTc))
-> GenLocated SrcSpanAnnA (Pat GhcTc)
-> Pat GhcTc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> LPat GhcTc -> LPat GhcTc
decideBangHood DynFlags
dflags) [LPat GhcTc]
[GenLocated SrcSpanAnnA (Pat GhcTc)]
pats
           -- pat_nablas is the covered set *after* matching the pattern, but
           -- before any of the GRHSs. We extend the environment with pat_nablas
           -- (via updPmNablas) so that the where-clause of 'grhss' can profit
           -- from that knowledge (#18533)
           ; MatchResult CoreExpr
match_result <- Nablas -> DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr)
forall a. Nablas -> DsM a -> DsM a
updPmNablas Nablas
pat_nablas (DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr))
-> DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$
                             HsMatchContext GhcTc
-> GRHSs GhcTc (LHsExpr GhcTc)
-> Type
-> NonEmpty Nablas
-> DsM (MatchResult CoreExpr)
dsGRHSs HsMatchContext GhcTc
ctxt GRHSs GhcTc (LHsExpr GhcTc)
GRHSs GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
grhss Type
rhs_ty NonEmpty Nablas
rhss_nablas
           ; EquationInfo -> IOEnv (Env DsGblEnv DsLclEnv) EquationInfo
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return EqnInfo { eqn_pats :: [Pat GhcTc]
eqn_pats = [Pat GhcTc]
upats
                            , eqn_orig :: Origin
eqn_orig = Origin
FromSource
                            , eqn_rhs :: MatchResult CoreExpr
eqn_rhs  = MatchResult CoreExpr
match_result } }

    discard_warnings_if_skip_pmc :: Origin -> DsM a -> DsM a
discard_warnings_if_skip_pmc Origin
orig =
      if Origin -> Bool
requiresPMC Origin
orig
      then DsM a -> DsM a
forall a. a -> a
id
      else DsM a -> DsM a
forall a. DsM a -> DsM a
discardWarningsDs

    initNablasMatches :: Nablas -> [LMatch GhcTc b] -> [(Nablas, NonEmpty Nablas)]
    initNablasMatches :: forall b. Nablas -> [LMatch GhcTc b] -> [(Nablas, NonEmpty Nablas)]
initNablasMatches Nablas
ldi_nablas [LMatch GhcTc b]
ms
      = (GenLocated (Anno (Match GhcTc b)) (Match GhcTc b)
 -> (Nablas, NonEmpty Nablas))
-> [GenLocated (Anno (Match GhcTc b)) (Match GhcTc b)]
-> [(Nablas, NonEmpty Nablas)]
forall a b. (a -> b) -> [a] -> [b]
map (\(L Anno (Match GhcTc b)
_ Match GhcTc b
m) -> (Nablas
ldi_nablas, Nablas -> GRHSs GhcTc b -> NonEmpty Nablas
forall b. Nablas -> GRHSs GhcTc b -> NonEmpty Nablas
initNablasGRHSs Nablas
ldi_nablas (Match GhcTc b -> GRHSs GhcTc b
forall p body. Match p body -> GRHSs p body
m_grhss Match GhcTc b
m))) [LMatch GhcTc b]
[GenLocated (Anno (Match GhcTc b)) (Match GhcTc b)]
ms

    initNablasGRHSs :: Nablas -> GRHSs GhcTc b -> NonEmpty Nablas
    initNablasGRHSs :: forall b. Nablas -> GRHSs GhcTc b -> NonEmpty Nablas
initNablasGRHSs Nablas
ldi_nablas GRHSs GhcTc b
m
      = String -> Maybe (NonEmpty Nablas) -> NonEmpty Nablas
forall a. HasCallStack => String -> Maybe a -> a
expectJust String
"GRHSs non-empty"
      (Maybe (NonEmpty Nablas) -> NonEmpty Nablas)
-> Maybe (NonEmpty Nablas) -> NonEmpty Nablas
forall a b. (a -> b) -> a -> b
$ [Nablas] -> Maybe (NonEmpty Nablas)
forall a. [a] -> Maybe (NonEmpty a)
NEL.nonEmpty
      ([Nablas] -> Maybe (NonEmpty Nablas))
-> [Nablas] -> Maybe (NonEmpty Nablas)
forall a b. (a -> b) -> a -> b
$ Int -> Nablas -> [Nablas]
forall a. Int -> a -> [a]
replicate ([GenLocated (Anno (GRHS GhcTc b)) (GRHS GhcTc b)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (GRHSs GhcTc b -> [LGRHS GhcTc b]
forall p body. GRHSs p body -> [LGRHS p body]
grhssGRHSs GRHSs GhcTc b
m)) Nablas
ldi_nablas

{- Note [Long-distance information in matchWrapper]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The pattern match checking in matchWrapper is done conditionally, depending
on isMatchContextPmChecked. This means that we don't perform pattern match
checking on e.g. generated pattern matches.

However, when we skip pattern match checking, we still need to keep track
of long-distance information in case we need it in a nested context.

This came up in #23445. For example:

  data GADT a where
    IsUnit :: GADT ()

  data Foo b where
    FooUnit :: Foo ()
    FooInt  :: Foo Int

  data SomeRec = SomeRec { fld :: () }

  bug :: GADT a -> Foo a -> SomeRec -> SomeRec
  bug IsUnit foo r =
    let gen_fld :: ()
        gen_fld = case foo of { FooUnit -> () }
    in case r of { SomeRec _ -> SomeRec gen_fld }

Here the body of 'bug' was generated by 'desugarRecordUpd' from the user-written
record update

  cd { fld = case foo of { FooUnit -> () } }

As a result, we have a generated FunBind gen_fld whose RHS

  case foo of { FooUnit -> () }

is user-written. This all happens after the GADT pattern match on IsUnit,
which brings into scope the Given equality [G] a ~ (). We need to make sure
that this long distance information is visible when pattern match checking the
user-written case statement.

To propagate this long-distance information in 'matchWrapper', when we skip
pattern match checks, we make sure to manually pass the long-distance
information to 'mk_eqn_info', which is responsible for recurring further into
the expression (in this case, it will end up recursively calling 'matchWrapper'
on the user-written case statement).
-}

matchEquations  :: HsMatchContext GhcTc
                -> [MatchId] -> [EquationInfo] -> Type
                -> DsM CoreExpr
matchEquations :: HsMatchContext GhcTc
-> [Id] -> [EquationInfo] -> Type -> DsM CoreExpr
matchEquations HsMatchContext GhcTc
ctxt [Id]
vars [EquationInfo]
eqns_info Type
rhs_ty
  = do  { MatchResult CoreExpr
match_result <- [Id] -> Type -> [EquationInfo] -> DsM (MatchResult CoreExpr)
match [Id]
vars Type
rhs_ty [EquationInfo]
eqns_info

        ; CoreExpr
fail_expr <- HsMatchContext GhcTc -> Type -> DsM CoreExpr
mkFailExpr HsMatchContext GhcTc
ctxt Type
rhs_ty

        ; MatchResult CoreExpr -> CoreExpr -> DsM CoreExpr
extractMatchResult MatchResult CoreExpr
match_result CoreExpr
fail_expr }

-- | @matchSimply@ 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 GhcTc     -- ^ Match kind
            -> LPat GhcTc               -- ^ Pattern it should match
            -> CoreExpr                 -- ^ Return this if it matches
            -> CoreExpr                 -- ^ Return this if it doesn't
            -> DsM CoreExpr
-- Some reasons 'matchSimply' is not defined using 'matchWrapper' (#18572):
--   * Some call sites like in 'deBindComp' specify a @fail_expr@ that isn't a
--     straight @patError@
--   * It receives an already desugared 'CoreExpr' for the scrutinee, not an
--     'HsExpr' like 'matchWrapper' expects
--   * Filling in all the phony fields for the 'MatchGroup' for a single pattern
--     match is awkward
--   * And we still export 'matchSinglePatVar', so not much is gained if we
--     don't also implement it in terms of 'matchWrapper'
matchSimply :: CoreExpr
-> HsMatchContext GhcTc
-> LPat GhcTc
-> CoreExpr
-> CoreExpr
-> DsM CoreExpr
matchSimply CoreExpr
scrut HsMatchContext GhcTc
hs_ctx LPat GhcTc
pat CoreExpr
result_expr CoreExpr
fail_expr = do
    let
      match_result :: MatchResult CoreExpr
match_result = CoreExpr -> MatchResult CoreExpr
cantFailMatchResult CoreExpr
result_expr
      rhs_ty :: Type
rhs_ty       = HasDebugCallStack => CoreExpr -> Type
CoreExpr -> Type
exprType CoreExpr
fail_expr
        -- Use exprType of fail_expr, because won't refine in the case of failure!
    MatchResult CoreExpr
match_result' <- CoreExpr
-> HsMatchContext GhcTc
-> LPat GhcTc
-> Type
-> MatchResult CoreExpr
-> DsM (MatchResult CoreExpr)
matchSinglePat CoreExpr
scrut HsMatchContext GhcTc
hs_ctx LPat GhcTc
pat Type
rhs_ty MatchResult CoreExpr
match_result
    MatchResult CoreExpr -> CoreExpr -> DsM CoreExpr
extractMatchResult MatchResult CoreExpr
match_result' CoreExpr
fail_expr

matchSinglePat :: CoreExpr -> HsMatchContext GhcTc -> LPat GhcTc
               -> Type -> MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
-- 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 GhcTc
-> LPat GhcTc
-> Type
-> MatchResult CoreExpr
-> DsM (MatchResult CoreExpr)
matchSinglePat (Var Id
var) HsMatchContext GhcTc
ctx LPat GhcTc
pat Type
ty MatchResult CoreExpr
match_result
  | Bool -> Bool
not (Name -> Bool
isExternalName (Id -> Name
idName Id
var))
  = Id
-> Maybe CoreExpr
-> HsMatchContext GhcTc
-> LPat GhcTc
-> Type
-> MatchResult CoreExpr
-> DsM (MatchResult CoreExpr)
matchSinglePatVar Id
var Maybe CoreExpr
forall a. Maybe a
Nothing HsMatchContext GhcTc
ctx LPat GhcTc
pat Type
ty MatchResult CoreExpr
match_result

matchSinglePat CoreExpr
scrut HsMatchContext GhcTc
hs_ctx LPat GhcTc
pat Type
ty MatchResult CoreExpr
match_result
  = do { Id
var           <- Type -> LPat GhcTc -> DsM Id
selectSimpleMatchVarL Type
ManyTy LPat GhcTc
pat
                            -- matchSinglePat is only used in matchSimply, which
                            -- is used in list comprehension, arrow notation,
                            -- and to create field selectors. All of which only
                            -- bind unrestricted variables, hence the 'Many'
                            -- above.
       ; MatchResult CoreExpr
match_result' <- Id
-> Maybe CoreExpr
-> HsMatchContext GhcTc
-> LPat GhcTc
-> Type
-> MatchResult CoreExpr
-> DsM (MatchResult CoreExpr)
matchSinglePatVar Id
var (CoreExpr -> Maybe CoreExpr
forall a. a -> Maybe a
Just CoreExpr
scrut) HsMatchContext GhcTc
hs_ctx LPat GhcTc
pat Type
ty MatchResult CoreExpr
match_result
       ; MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall a. a -> IOEnv (Env DsGblEnv DsLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (MatchResult CoreExpr -> DsM (MatchResult CoreExpr))
-> MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$ HasDebugCallStack => Id -> CoreExpr -> DsWrapper
Id -> CoreExpr -> DsWrapper
bindNonRec Id
var CoreExpr
scrut DsWrapper -> MatchResult CoreExpr -> MatchResult CoreExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MatchResult CoreExpr
match_result'
       }

matchSinglePatVar :: Id   -- See Note [Match Ids]
                  -> Maybe CoreExpr -- ^ The scrutinee the match id is bound to
                  -> HsMatchContext GhcTc -> LPat GhcTc
                  -> Type -> MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
matchSinglePatVar :: Id
-> Maybe CoreExpr
-> HsMatchContext GhcTc
-> LPat GhcTc
-> Type
-> MatchResult CoreExpr
-> DsM (MatchResult CoreExpr)
matchSinglePatVar Id
var Maybe CoreExpr
mb_scrut HsMatchContext GhcTc
ctx LPat GhcTc
pat Type
ty MatchResult CoreExpr
match_result
  = Bool
-> SDoc -> DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr)
forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr (Name -> Bool
isInternalName (Id -> Name
idName Id
var)) (Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
var) (DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr))
-> DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$
    do { DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
       ; SrcSpan
locn   <- DsM SrcSpan
getSrcSpanDs
       -- Pattern match check warnings.
       -- See Note [Long-distance information in matchWrapper] and
       -- Note [Long-distance information in do notation] in GHC.HsToCore.Expr.
       ; Nablas
ldi_nablas <-
         if  DynFlags -> Origin -> HsMatchContext GhcTc -> LPat GhcTc -> Bool
forall id.
DynFlags -> Origin -> HsMatchContext id -> LPat GhcTc -> Bool
isMatchContextPmChecked_SinglePat DynFlags
dflags Origin
FromSource HsMatchContext GhcTc
ctx LPat GhcTc
pat
         then [CoreExpr] -> [Id] -> DsM Nablas -> DsM Nablas
forall a. [CoreExpr] -> [Id] -> DsM a -> DsM a
addCoreScrutTmCs (Maybe CoreExpr -> [CoreExpr]
forall a. Maybe a -> [a]
maybeToList Maybe CoreExpr
mb_scrut) [Item [Id]
Id
var] (DsM Nablas -> DsM Nablas) -> DsM Nablas -> DsM Nablas
forall a b. (a -> b) -> a -> b
$
              DsMatchContext -> Id -> Pat GhcTc -> DsM Nablas
pmcPatBind (HsMatchContext GhcTc -> SrcSpan -> DsMatchContext
DsMatchContext HsMatchContext GhcTc
ctx SrcSpan
locn) Id
var (GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat)
         else DsM Nablas
getLdiNablas

       ; let eqn_info :: EquationInfo
eqn_info = EqnInfo { eqn_pats :: [Pat GhcTc]
eqn_pats = [GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc (DynFlags -> LPat GhcTc -> LPat GhcTc
decideBangHood DynFlags
dflags LPat GhcTc
pat)]
                                , eqn_orig :: Origin
eqn_orig = Origin
FromSource
                                , eqn_rhs :: MatchResult CoreExpr
eqn_rhs  =
               Nablas -> MatchResult CoreExpr -> MatchResult CoreExpr
forall r. Nablas -> MatchResult r -> MatchResult r
updPmNablasMatchResult Nablas
ldi_nablas MatchResult CoreExpr
match_result }
               -- See Note [Long-distance information in do notation]
               -- in GHC.HsToCore.Expr.

       ; [Id] -> Type -> [EquationInfo] -> DsM (MatchResult CoreExpr)
match [Item [Id]
Id
var] Type
ty [Item [EquationInfo]
EquationInfo
eqn_info] }

updPmNablasMatchResult :: Nablas -> MatchResult r -> MatchResult r
updPmNablasMatchResult :: forall r. Nablas -> MatchResult r -> MatchResult r
updPmNablasMatchResult Nablas
nablas = \case
  MR_Infallible DsM r
body_fn -> DsM r -> MatchResult r
forall a. DsM a -> MatchResult a
MR_Infallible (DsM r -> MatchResult r) -> DsM r -> MatchResult r
forall a b. (a -> b) -> a -> b
$
    Nablas -> DsM r -> DsM r
forall a. Nablas -> DsM a -> DsM a
updPmNablas Nablas
nablas DsM r
body_fn
  MR_Fallible CoreExpr -> DsM r
body_fn -> (CoreExpr -> DsM r) -> MatchResult r
forall a. (CoreExpr -> DsM a) -> MatchResult a
MR_Fallible ((CoreExpr -> DsM r) -> MatchResult r)
-> (CoreExpr -> DsM r) -> MatchResult r
forall a b. (a -> b) -> a -> b
$ \CoreExpr
fail ->
    Nablas -> DsM r -> DsM r
forall a. Nablas -> DsM a -> DsM a
updPmNablas Nablas
nablas (DsM r -> DsM r) -> DsM r -> DsM r
forall a b. (a -> b) -> a -> b
$ CoreExpr -> DsM r
body_fn CoreExpr
fail

{-
************************************************************************
*                                                                      *
                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   FractionalLit -- 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)

{- 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 FractionalLit if numeric, and add a PgOverStr constructor
for overloaded strings.
-}

groupEquations :: Platform -> [EquationInfo] -> [NonEmpty (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 :: Platform -> [EquationInfo] -> [NonEmpty (PatGroup, EquationInfo)]
groupEquations Platform
platform [EquationInfo]
eqns
  = ((PatGroup, EquationInfo) -> (PatGroup, EquationInfo) -> Bool)
-> [(PatGroup, EquationInfo)]
-> [NonEmpty (PatGroup, EquationInfo)]
forall (f :: * -> *) a.
Foldable f =>
(a -> a -> Bool) -> f a -> [NonEmpty a]
NEL.groupBy (PatGroup, EquationInfo) -> (PatGroup, EquationInfo) -> Bool
same_gp ([(PatGroup, EquationInfo)] -> [NonEmpty (PatGroup, EquationInfo)])
-> [(PatGroup, EquationInfo)]
-> [NonEmpty (PatGroup, EquationInfo)]
forall a b. (a -> b) -> a -> b
$ [(Platform -> Pat GhcTc -> PatGroup
patGroup Platform
platform (EquationInfo -> Pat GhcTc
firstPat EquationInfo
eqn), EquationInfo
eqn) | EquationInfo
eqn <- [EquationInfo]
eqns]
  -- comprehension on NonEmpty
  where
    same_gp :: (PatGroup,EquationInfo) -> (PatGroup,EquationInfo) -> Bool
    (PatGroup
pg1,EquationInfo
_) same_gp :: (PatGroup, EquationInfo) -> (PatGroup, EquationInfo) -> Bool
`same_gp` (PatGroup
pg2,EquationInfo
_) = PatGroup
pg1 PatGroup -> PatGroup -> Bool
`sameGroup` PatGroup
pg2

-- TODO Make subGroup1 using a NonEmptyMap
subGroup :: (m -> [NonEmpty EquationInfo]) -- Map.elems
         -> m -- Map.empty
         -> (a -> m -> Maybe (NonEmpty EquationInfo)) -- Map.lookup
         -> (a -> NonEmpty EquationInfo -> m -> m) -- Map.insert
         -> [(a, EquationInfo)] -> [NonEmpty 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 :: forall m a.
(m -> [NonEmpty EquationInfo])
-> m
-> (a -> m -> Maybe (NonEmpty EquationInfo))
-> (a -> NonEmpty EquationInfo -> m -> m)
-> [(a, EquationInfo)]
-> [NonEmpty EquationInfo]
subGroup m -> [NonEmpty EquationInfo]
elems m
empty a -> m -> Maybe (NonEmpty EquationInfo)
lookup a -> NonEmpty EquationInfo -> m -> m
insert [(a, EquationInfo)]
group
    = (NonEmpty EquationInfo -> NonEmpty EquationInfo)
-> [NonEmpty EquationInfo] -> [NonEmpty EquationInfo]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NonEmpty EquationInfo -> NonEmpty EquationInfo
forall a. NonEmpty a -> NonEmpty a
NEL.reverse ([NonEmpty EquationInfo] -> [NonEmpty EquationInfo])
-> [NonEmpty EquationInfo] -> [NonEmpty EquationInfo]
forall a b. (a -> b) -> a -> b
$ m -> [NonEmpty EquationInfo]
elems (m -> [NonEmpty EquationInfo]) -> m -> [NonEmpty EquationInfo]
forall a b. (a -> b) -> a -> b
$ (m -> (a, EquationInfo) -> m) -> m -> [(a, EquationInfo)] -> m
forall b a. (b -> a -> b) -> b -> [a] -> b
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 m
pg_map (a
pg, EquationInfo
eqn)
      = case a -> m -> Maybe (NonEmpty EquationInfo)
lookup a
pg m
pg_map of
          Just NonEmpty EquationInfo
eqns -> a -> NonEmpty EquationInfo -> m -> m
insert a
pg (EquationInfo -> NonEmpty EquationInfo -> NonEmpty EquationInfo
forall a. a -> NonEmpty a -> NonEmpty a
NEL.cons EquationInfo
eqn NonEmpty EquationInfo
eqns) m
pg_map
          Maybe (NonEmpty EquationInfo)
Nothing   -> a -> NonEmpty EquationInfo -> m -> m
insert a
pg [Item (NonEmpty EquationInfo)
EquationInfo
eqn] m
pg_map
    -- pg_map :: Map a [EquationInfo]
    -- Equations seen so far in reverse order of appearance

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

subGroupUniq :: Uniquable a => [(a, EquationInfo)] -> [NonEmpty EquationInfo]
subGroupUniq :: forall a.
Uniquable a =>
[(a, EquationInfo)] -> [NonEmpty EquationInfo]
subGroupUniq =
  (UniqDFM a (NonEmpty EquationInfo) -> [NonEmpty EquationInfo])
-> UniqDFM a (NonEmpty EquationInfo)
-> (a
    -> UniqDFM a (NonEmpty EquationInfo)
    -> Maybe (NonEmpty EquationInfo))
-> (a
    -> NonEmpty EquationInfo
    -> UniqDFM a (NonEmpty EquationInfo)
    -> UniqDFM a (NonEmpty EquationInfo))
-> [(a, EquationInfo)]
-> [NonEmpty EquationInfo]
forall m a.
(m -> [NonEmpty EquationInfo])
-> m
-> (a -> m -> Maybe (NonEmpty EquationInfo))
-> (a -> NonEmpty EquationInfo -> m -> m)
-> [(a, EquationInfo)]
-> [NonEmpty EquationInfo]
subGroup UniqDFM a (NonEmpty EquationInfo) -> [NonEmpty EquationInfo]
forall key elt. UniqDFM key elt -> [elt]
eltsUDFM UniqDFM a (NonEmpty EquationInfo)
forall key elt. UniqDFM key elt
emptyUDFM ((UniqDFM a (NonEmpty EquationInfo)
 -> a -> Maybe (NonEmpty EquationInfo))
-> a
-> UniqDFM a (NonEmpty EquationInfo)
-> Maybe (NonEmpty EquationInfo)
forall a b c. (a -> b -> c) -> b -> a -> c
flip UniqDFM a (NonEmpty EquationInfo)
-> a -> Maybe (NonEmpty EquationInfo)
forall key elt.
Uniquable key =>
UniqDFM key elt -> key -> Maybe elt
lookupUDFM) (\a
k NonEmpty EquationInfo
v UniqDFM a (NonEmpty EquationInfo)
m -> UniqDFM a (NonEmpty EquationInfo)
-> a -> NonEmpty EquationInfo -> UniqDFM a (NonEmpty EquationInfo)
forall key elt.
Uniquable key =>
UniqDFM key elt -> key -> elt -> UniqDFM key elt
addToUDFM UniqDFM a (NonEmpty EquationInfo)
m a
k NonEmpty 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 (#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 instantiating 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 PatGroup
PgAny         PatGroup
PgAny         = Bool
True
sameGroup PatGroup
PgBang        PatGroup
PgBang        = Bool
True
sameGroup (PgCon DataCon
_)     (PgCon DataCon
_)     = Bool
True    -- One case expression
sameGroup (PgSyn PatSyn
p1 [Type]
t1) (PgSyn PatSyn
p2 [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 Literal
_)     (PgLit Literal
_)     = Bool
True    -- One case expression
sameGroup (PgN FractionalLit
l1)      (PgN FractionalLit
l2)      = FractionalLit
l1FractionalLit -> FractionalLit -> Bool
forall a. Eq a => a -> a -> Bool
==FractionalLit
l2  -- Order is significant
        -- Order is significant, match PgN after PgLit
        -- If the exponents are small check for value equality rather than syntactic equality
        -- This is implemented in the Eq instance for FractionalLit, we do this to avoid
        -- computing the value of excessively large rationals.
sameGroup (PgOverS FastString
s1)  (PgOverS FastString
s2)  = FastString
s1FastString -> FastString -> Bool
forall a. Eq a => a -> a -> Bool
==FastString
s2
sameGroup (PgNpK Integer
l1)    (PgNpK Integer
l2)    = Integer
l1Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
==Integer
l2  -- See Note [Grouping overloaded literal patterns]
sameGroup (PgCo Type
t1)     (PgCo Type
t2)     = Type
t1 Type -> Type -> Bool
`eqType` Type
t2
        -- CoPats are in the same group 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 LHsExpr GhcTc
e1 Type
t1) (PgView LHsExpr GhcTc
e2 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 PatGroup
_          PatGroup
_          = 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 (LHsExpr GhcTc
e1,Type
_) (LHsExpr GhcTc
e2,Type
_) = 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 LHsExpr GhcTc
e LHsExpr GhcTc
e' = HsExpr GhcTc -> HsExpr GhcTc -> Bool
exp (GenLocated SrcSpanAnnA (HsExpr GhcTc) -> HsExpr GhcTc
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
e) (GenLocated SrcSpanAnnA (HsExpr GhcTc) -> HsExpr GhcTc
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
e')

    ---------
    exp :: HsExpr GhcTc -> HsExpr GhcTc -> Bool
    -- real comparison is on HsExpr's
    -- strip parens
    exp :: HsExpr GhcTc -> HsExpr GhcTc -> Bool
exp (HsPar XPar GhcTc
_ LHsToken "(" GhcTc
_ (L SrcSpanAnnA
_ HsExpr GhcTc
e) LHsToken ")" GhcTc
_) HsExpr GhcTc
e' = HsExpr GhcTc -> HsExpr GhcTc -> Bool
exp HsExpr GhcTc
e HsExpr GhcTc
e'
    exp HsExpr GhcTc
e (HsPar XPar GhcTc
_ LHsToken "(" GhcTc
_ (L SrcSpanAnnA
_ HsExpr GhcTc
e') LHsToken ")" GhcTc
_) = HsExpr GhcTc -> HsExpr GhcTc -> Bool
exp HsExpr GhcTc
e HsExpr GhcTc
e'
    -- because the expressions do not necessarily have the same type,
    -- we have to compare the wrappers
    exp (XExpr (WrapExpr (HsWrap HsWrapper
h HsExpr GhcTc
e))) (XExpr (WrapExpr (HsWrap  HsWrapper
h' 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 (XExpr (ExpansionExpr (HsExpanded HsExpr (GhcPass 'Renamed)
_ HsExpr GhcTc
b))) (XExpr (ExpansionExpr (HsExpanded HsExpr (GhcPass 'Renamed)
_ HsExpr GhcTc
b'))) =
      HsExpr GhcTc -> HsExpr GhcTc -> Bool
exp HsExpr GhcTc
b HsExpr GhcTc
b'
    exp (HsVar XVar GhcTc
_ LIdP GhcTc
i) (HsVar XVar GhcTc
_ LIdP GhcTc
i') =  LIdP GhcTc
GenLocated SrcSpanAnnN Id
i GenLocated SrcSpanAnnN Id -> GenLocated SrcSpanAnnN Id -> Bool
forall a. Eq a => a -> a -> Bool
== LIdP GhcTc
GenLocated SrcSpanAnnN Id
i'
    exp (XExpr (ConLikeTc ConLike
c [Id]
_ [Scaled Type]
_)) (XExpr (ConLikeTc ConLike
c' [Id]
_ [Scaled Type]
_)) = 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 XIPVar GhcTc
_ HsIPName
i) (HsIPVar XIPVar GhcTc
_ HsIPName
i') = HsIPName
i HsIPName -> HsIPName -> Bool
forall a. Eq a => a -> a -> Bool
== HsIPName
i'
    exp (HsOverLit XOverLitE GhcTc
_ HsOverLit GhcTc
l) (HsOverLit XOverLitE GhcTc
_ 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 XApp GhcTc
_ LHsExpr GhcTc
e1 LHsExpr GhcTc
e2) (HsApp XApp GhcTc
_ LHsExpr GhcTc
e1' 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 XOpApp GhcTc
_ LHsExpr GhcTc
l LHsExpr GhcTc
o LHsExpr GhcTc
ri) (OpApp XOpApp GhcTc
_ LHsExpr GhcTc
l' LHsExpr GhcTc
o' 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 XNegApp GhcTc
_ LHsExpr GhcTc
e SyntaxExpr GhcTc
n) (NegApp XNegApp GhcTc
_ LHsExpr GhcTc
e' 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 XSectionL GhcTc
_ LHsExpr GhcTc
e1 LHsExpr GhcTc
e2) (SectionL XSectionL GhcTc
_ LHsExpr GhcTc
e1' 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 XSectionR GhcTc
_ LHsExpr GhcTc
e1 LHsExpr GhcTc
e2) (SectionR XSectionR GhcTc
_ LHsExpr GhcTc
e1' 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 XExplicitTuple GhcTc
_ [HsTupArg GhcTc]
es1 Boxity
_) (ExplicitTuple XExplicitTuple GhcTc
_ [HsTupArg GhcTc]
es2 Boxity
_) =
        (HsTupArg GhcTc -> HsTupArg GhcTc -> Bool)
-> [HsTupArg GhcTc] -> [HsTupArg GhcTc] -> Bool
forall a. (a -> a -> Bool) -> [a] -> [a] -> Bool
eq_list HsTupArg GhcTc -> HsTupArg GhcTc -> Bool
tup_arg [HsTupArg GhcTc]
es1 [HsTupArg GhcTc]
es2
    exp (ExplicitSum XExplicitSum GhcTc
_ Int
_ Int
_ LHsExpr GhcTc
e) (ExplicitSum XExplicitSum GhcTc
_ Int
_ Int
_ LHsExpr GhcTc
e') = LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e LHsExpr GhcTc
e'
    exp (HsIf XIf GhcTc
_ LHsExpr GhcTc
e LHsExpr GhcTc
e1 LHsExpr GhcTc
e2) (HsIf XIf GhcTc
_ LHsExpr GhcTc
e' LHsExpr GhcTc
e1' 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 HsExpr GhcTc
_ HsExpr GhcTc
_  = Bool
False

    ---------
    syn_exp :: SyntaxExpr GhcTc -> SyntaxExpr GhcTc -> Bool
    syn_exp :: SyntaxExpr GhcTc -> SyntaxExpr GhcTc -> Bool
syn_exp (SyntaxExprTc { syn_expr :: SyntaxExprTc -> HsExpr GhcTc
syn_expr      = HsExpr GhcTc
expr1
                          , syn_arg_wraps :: SyntaxExprTc -> [HsWrapper]
syn_arg_wraps = [HsWrapper]
arg_wraps1
                          , syn_res_wrap :: SyntaxExprTc -> HsWrapper
syn_res_wrap  = HsWrapper
res_wrap1 })
            (SyntaxExprTc { syn_expr :: SyntaxExprTc -> HsExpr GhcTc
syn_expr      = HsExpr GhcTc
expr2
                          , syn_arg_wraps :: SyntaxExprTc -> [HsWrapper]
syn_arg_wraps = [HsWrapper]
arg_wraps2
                          , syn_res_wrap :: SyntaxExprTc -> 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.
HasDebugCallStack =>
String -> (a -> b -> c) -> [a] -> [b] -> [c]
zipWithEqual String
"viewLExprEq" HsWrapper -> HsWrapper -> Bool
wrap [HsWrapper]
arg_wraps1 [HsWrapper]
arg_wraps2) Bool -> Bool -> Bool
&&
        HsWrapper -> HsWrapper -> Bool
wrap HsWrapper
res_wrap1 HsWrapper
res_wrap2
    syn_exp SyntaxExpr GhcTc
SyntaxExprTc
NoSyntaxExprTc SyntaxExpr GhcTc
SyntaxExprTc
NoSyntaxExprTc = Bool
True
    syn_exp SyntaxExpr GhcTc
_              SyntaxExpr GhcTc
_              = Bool
False

    ---------
    tup_arg :: HsTupArg GhcTc -> HsTupArg GhcTc -> Bool
tup_arg (Present XPresent GhcTc
_ LHsExpr GhcTc
e1)           (Present XPresent GhcTc
_ LHsExpr GhcTc
e2)         = LHsExpr GhcTc -> LHsExpr GhcTc -> Bool
lexp LHsExpr GhcTc
e1 LHsExpr GhcTc
e2
    tup_arg (Missing (Scaled Type
_ Type
t1)) (Missing (Scaled Type
_ Type
t2)) = Type -> Type -> Bool
eqType Type
t1 Type
t2
    tup_arg HsTupArg GhcTc
_ HsTupArg GhcTc
_ = 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 HsWrapper
WpHole HsWrapper
WpHole = Bool
True
    wrap (WpCompose HsWrapper
w1 HsWrapper
w2) (WpCompose HsWrapper
w1' HsWrapper
w2') = HsWrapper -> HsWrapper -> Bool
wrap HsWrapper
w1 HsWrapper
w1' Bool -> Bool -> Bool
&& HsWrapper -> HsWrapper -> Bool
wrap HsWrapper
w2 HsWrapper
w2'
    wrap (WpFun HsWrapper
w1 HsWrapper
w2 Scaled Type
_)   (WpFun HsWrapper
w1' HsWrapper
w2' Scaled Type
_)   = HsWrapper -> HsWrapper -> Bool
wrap HsWrapper
w1 HsWrapper
w1' Bool -> Bool -> Bool
&& HsWrapper -> HsWrapper -> Bool
wrap HsWrapper
w2 HsWrapper
w2'
    wrap (WpCast TcCoercionR
co)       (WpCast TcCoercionR
co')        = TcCoercionR
co TcCoercionR -> TcCoercionR -> Bool
`eqCoercion` TcCoercionR
co'
    wrap (WpEvApp EvTerm
et1)     (WpEvApp EvTerm
et2)       = EvTerm
et1 EvTerm -> EvTerm -> Bool
`ev_term` EvTerm
et2
    wrap (WpTyApp Type
t)       (WpTyApp 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 HsWrapper
_ HsWrapper
_ = Bool
False

    ---------
    ev_term :: EvTerm -> EvTerm -> Bool
    ev_term :: EvTerm -> EvTerm -> Bool
ev_term (EvExpr (Var Id
a)) (EvExpr  (Var Id
b))
      = Id -> Type
idType Id
a Type -> Type -> Bool
`eqType` Id -> Type
idType Id
b
        -- The /type/ of the evidence matters, not its precise proof term.
        -- Caveat: conceivably a sufficiently exotic use of incoherent instances
        -- could make a difference, but remember this is only used within the
        -- pattern matches for a single function, so it's hard to see how that
        -- could really happen.  And we don't want accidentally different proofs
        -- to prevent spotting equalities, and hence degrade pattern-match
        -- overlap checking.
    ev_term (EvExpr (Coercion TcCoercionR
a)) (EvExpr (Coercion TcCoercionR
b))
      = TcCoercionR
a TcCoercionR -> TcCoercionR -> Bool
`eqCoercion` TcCoercionR
b
    ev_term EvTerm
_ EvTerm
_ = Bool
False

    ---------
    eq_list :: (a->a->Bool) -> [a] -> [a] -> Bool
    eq_list :: forall a. (a -> a -> Bool) -> [a] -> [a] -> Bool
eq_list a -> a -> Bool
_  []     []     = Bool
True
    eq_list a -> a -> Bool
_  []     (a
_:[a]
_)  = Bool
False
    eq_list a -> a -> Bool
_  (a
_:[a]
_)  []     = Bool
False
    eq_list a -> a -> Bool
eq (a
x:[a]
xs) (a
y:[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 :: Platform -> Pat GhcTc -> PatGroup
patGroup :: Platform -> Pat GhcTc -> PatGroup
patGroup Platform
_ (ConPat { pat_con :: forall p. Pat p -> XRec p (ConLikeP p)
pat_con = L SrcSpanAnnN
_ ConLike
con
                   , pat_con_ext :: forall p. Pat p -> XConPat p
pat_con_ext = ConPatTc { cpt_arg_tys :: ConPatTc -> [Type]
cpt_arg_tys = [Type]
tys }
                   })
 | RealDataCon DataCon
dcon <- ConLike
con              = DataCon -> PatGroup
PgCon DataCon
dcon
 | PatSynCon PatSyn
psyn <- ConLike
con                = PatSyn -> [Type] -> PatGroup
PgSyn PatSyn
psyn [Type]
tys
patGroup Platform
_ (WildPat {})                 = PatGroup
PgAny
patGroup Platform
_ (BangPat {})                 = PatGroup
PgBang
patGroup Platform
_ (NPat XNPat GhcTc
_ (L SrcAnn NoEpAnns
_ (OverLit {ol_val :: forall p. HsOverLit p -> OverLitVal
ol_val=OverLitVal
oval})) Maybe (SyntaxExpr GhcTc)
mb_neg SyntaxExpr GhcTc
_) =
  case (OverLitVal
oval, Maybe SyntaxExprTc -> Bool
forall a. Maybe a -> Bool
isJust Maybe (SyntaxExpr GhcTc)
Maybe SyntaxExprTc
mb_neg) of
    (HsIntegral   IntegralLit
i, Bool
is_neg) -> FractionalLit -> PatGroup
PgN (Bool -> Integer -> FractionalLit
integralFractionalLit Bool
is_neg (if Bool
is_neg
                                                                    then Integer -> Integer
forall a. Num a => a -> a
negate (IntegralLit -> Integer
il_value IntegralLit
i)
                                                                    else IntegralLit -> Integer
il_value IntegralLit
i))
    (HsFractional FractionalLit
f, Bool
is_neg)
      | Bool
is_neg    -> FractionalLit -> PatGroup
PgN (FractionalLit -> PatGroup) -> FractionalLit -> PatGroup
forall a b. (a -> b) -> a -> b
$! FractionalLit -> FractionalLit
negateFractionalLit FractionalLit
f
      | Bool
otherwise -> FractionalLit -> PatGroup
PgN FractionalLit
f
    (HsIsString SourceText
_ FastString
s, Bool
_) -> Bool -> PatGroup -> PatGroup
forall a. HasCallStack => Bool -> a -> a
assert (Maybe SyntaxExprTc -> Bool
forall a. Maybe a -> Bool
isNothing Maybe (SyntaxExpr GhcTc)
Maybe SyntaxExprTc
mb_neg) (PatGroup -> PatGroup) -> PatGroup -> PatGroup
forall a b. (a -> b) -> a -> b
$
                            FastString -> PatGroup
PgOverS FastString
s
patGroup Platform
_ (NPlusKPat XNPlusKPat GhcTc
_ LIdP GhcTc
_ (L SrcAnn NoEpAnns
_ (OverLit {ol_val :: forall p. HsOverLit p -> OverLitVal
ol_val=OverLitVal
oval})) HsOverLit GhcTc
_ SyntaxExpr GhcTc
_ SyntaxExpr GhcTc
_) =
  case OverLitVal
oval of
   HsIntegral IntegralLit
i -> Integer -> PatGroup
PgNpK (IntegralLit -> Integer
il_value IntegralLit
i)
   OverLitVal
_ -> String -> SDoc -> PatGroup
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"patGroup NPlusKPat" (OverLitVal -> SDoc
forall a. Outputable a => a -> SDoc
ppr OverLitVal
oval)
patGroup Platform
_ (ViewPat XViewPat GhcTc
_ LHsExpr GhcTc
expr LPat GhcTc
p)           = LHsExpr GhcTc -> Type -> PatGroup
PgView LHsExpr GhcTc
expr (Pat GhcTc -> Type
hsPatType (GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
p))
patGroup Platform
platform (LitPat XLitPat GhcTc
_ HsLit GhcTc
lit)        = Literal -> PatGroup
PgLit (Platform -> HsLit GhcTc -> Literal
hsLitKey Platform
platform HsLit GhcTc
lit)
patGroup Platform
platform (XPat XXPat GhcTc
ext) = case XXPat GhcTc
ext of
  CoPat HsWrapper
_ Pat GhcTc
p Type
_      -> Type -> PatGroup
PgCo (Pat GhcTc -> Type
hsPatType Pat GhcTc
p) -- Type of innelexp pattern
  ExpansionPat Pat (GhcPass 'Renamed)
_ Pat GhcTc
p -> Platform -> Pat GhcTc -> PatGroup
patGroup Platform
platform Pat GhcTc
p
patGroup Platform
_ Pat GhcTc
pat                          = String -> SDoc -> PatGroup
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"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.
-}