{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE LambdaCase #-}
module GHC.HsToCore.PmCheck (
checkSingle, checkMatches, checkGuardMatches,
isMatchContextPmChecked,
addTyCsDs, addScrutTmCs
) where
#include "GhclibHsVersions.h"
import GHC.Prelude
import GHC.HsToCore.PmCheck.Types
import GHC.HsToCore.PmCheck.Oracle
import GHC.HsToCore.PmCheck.Ppr
import GHC.Types.Basic (Origin(..), isGenerated)
import GHC.Core (CoreExpr, Expr(Var,App))
import GHC.Data.FastString (unpackFS, lengthFS)
import GHC.Driver.Session
import GHC.Hs
import GHC.Tc.Utils.Zonk (shortCutLit)
import GHC.Types.Id
import GHC.Core.ConLike
import GHC.Types.Name
import GHC.Tc.Instance.Family
import GHC.Builtin.Types
import GHC.Types.SrcLoc
import GHC.Utils.Misc
import GHC.Utils.Outputable
import GHC.Core.DataCon
import GHC.Core.TyCon
import GHC.Types.Var (EvVar)
import GHC.Core.Coercion
import GHC.Tc.Types.Evidence (HsWrapper(..), isIdHsWrapper)
import GHC.Tc.Utils.TcType (evVarPred)
import {-# SOURCE #-} GHC.HsToCore.Expr (dsExpr, dsLExpr, dsSyntaxExpr)
import {-# SOURCE #-} GHC.HsToCore.Binds (dsHsWrapper)
import GHC.HsToCore.Utils (selectMatchVar)
import GHC.HsToCore.Match.Literal (dsLit, dsOverLit)
import GHC.HsToCore.Monad
import GHC.Data.Bag
import GHC.Data.IOEnv (unsafeInterleaveM)
import GHC.Data.OrdList
import GHC.Core.TyCo.Rep
import GHC.Core.Type
import GHC.HsToCore.Utils (isTrueLHsExpr)
import GHC.Data.Maybe
import qualified GHC.LanguageExtensions as LangExt
import GHC.Utils.Monad (concatMapM)
import Control.Monad (when, forM_, zipWithM)
import Data.List (elemIndex)
import qualified Data.Semigroup as Semi
data PmGrd
=
PmCon {
PmGrd -> Id
pm_id :: !Id,
PmGrd -> PmAltCon
pm_con_con :: !PmAltCon,
PmGrd -> [Id]
pm_con_tvs :: ![TyVar],
PmGrd -> [Id]
pm_con_dicts :: ![EvVar],
PmGrd -> [Id]
pm_con_args :: ![Id]
}
| PmBang {
pm_id :: !Id
}
| PmLet {
pm_id :: !Id,
PmGrd -> CoreExpr
_pm_let_expr :: !CoreExpr
}
instance Outputable PmGrd where
ppr :: PmGrd -> SDoc
ppr (PmCon Id
x PmAltCon
alt [Id]
_tvs [Id]
_con_dicts [Id]
con_args)
= [SDoc] -> SDoc
hsep [PmAltCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr PmAltCon
alt, [SDoc] -> SDoc
hsep ((Id -> SDoc) -> [Id] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Id]
con_args), String -> SDoc
text String
"<-", Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
x]
ppr (PmBang Id
x) = Char -> SDoc
char Char
'!' SDoc -> SDoc -> SDoc
<> Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
x
ppr (PmLet Id
x CoreExpr
expr) = [SDoc] -> SDoc
hsep [String -> SDoc
text String
"let", Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
x, String -> SDoc
text String
"=", CoreExpr -> SDoc
forall a. Outputable a => a -> SDoc
ppr CoreExpr
expr]
type GrdVec = [PmGrd]
data Precision = Approximate | Precise
deriving (Precision -> Precision -> Bool
(Precision -> Precision -> Bool)
-> (Precision -> Precision -> Bool) -> Eq Precision
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Precision -> Precision -> Bool
$c/= :: Precision -> Precision -> Bool
== :: Precision -> Precision -> Bool
$c== :: Precision -> Precision -> Bool
Eq, Int -> Precision -> ShowS
[Precision] -> ShowS
Precision -> String
(Int -> Precision -> ShowS)
-> (Precision -> String)
-> ([Precision] -> ShowS)
-> Show Precision
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Precision] -> ShowS
$cshowList :: [Precision] -> ShowS
show :: Precision -> String
$cshow :: Precision -> String
showsPrec :: Int -> Precision -> ShowS
$cshowsPrec :: Int -> Precision -> ShowS
Show)
instance Outputable Precision where
ppr :: Precision -> SDoc
ppr = String -> SDoc
text (String -> SDoc) -> (Precision -> String) -> Precision -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Precision -> String
forall a. Show a => a -> String
show
instance Semi.Semigroup Precision where
Precision
Precise <> :: Precision -> Precision -> Precision
<> Precision
Precise = Precision
Precise
Precision
_ <> Precision
_ = Precision
Approximate
instance Monoid Precision where
mempty :: Precision
mempty = Precision
Precise
mappend :: Precision -> Precision -> Precision
mappend = Precision -> Precision -> Precision
forall a. Semigroup a => a -> a -> a
(Semi.<>)
type RhsInfo = Located SDoc
data GrdTree
= Rhs !RhsInfo
| Guard !PmGrd !GrdTree
| Sequence !GrdTree !GrdTree
| Empty
data AnnotatedTree
= AccessibleRhs !Deltas !RhsInfo
| InaccessibleRhs !RhsInfo
| MayDiverge !AnnotatedTree
| SequenceAnn !AnnotatedTree !AnnotatedTree
| EmptyAnn
pprRhsInfo :: RhsInfo -> SDoc
pprRhsInfo :: RhsInfo -> SDoc
pprRhsInfo (L (RealSrcSpan RealSrcSpan
rss Maybe BufSpan
_) SDoc
_) = Int -> SDoc
forall a. Outputable a => a -> SDoc
ppr (RealSrcSpan -> Int
srcSpanStartLine RealSrcSpan
rss)
pprRhsInfo (L SrcSpan
s SDoc
_) = SrcSpan -> SDoc
forall a. Outputable a => a -> SDoc
ppr SrcSpan
s
instance Outputable GrdTree where
ppr :: GrdTree -> SDoc
ppr (Rhs RhsInfo
info) = String -> SDoc
text String
"->" SDoc -> SDoc -> SDoc
<+> RhsInfo -> SDoc
pprRhsInfo RhsInfo
info
ppr g :: GrdTree
g@Guard{} = [SDoc] -> SDoc
fsep ([SDoc] -> [SDoc]
prefix ((PmGrd -> SDoc) -> [PmGrd] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map PmGrd -> SDoc
forall a. Outputable a => a -> SDoc
ppr [PmGrd]
grds)) SDoc -> SDoc -> SDoc
<+> GrdTree -> SDoc
forall a. Outputable a => a -> SDoc
ppr GrdTree
t
where
(GrdTree
t, [PmGrd]
grds) = GrdTree -> (GrdTree, [PmGrd])
collect_grds GrdTree
g
collect_grds :: GrdTree -> (GrdTree, [PmGrd])
collect_grds (Guard PmGrd
grd GrdTree
t) = (PmGrd
grd PmGrd -> [PmGrd] -> [PmGrd]
forall a. a -> [a] -> [a]
:) ([PmGrd] -> [PmGrd]) -> (GrdTree, [PmGrd]) -> (GrdTree, [PmGrd])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GrdTree -> (GrdTree, [PmGrd])
collect_grds GrdTree
t
collect_grds GrdTree
t = (GrdTree
t, [])
prefix :: [SDoc] -> [SDoc]
prefix [] = []
prefix (SDoc
s:[SDoc]
sdocs) = Char -> SDoc
char Char
'|' SDoc -> SDoc -> SDoc
<+> SDoc
s SDoc -> [SDoc] -> [SDoc]
forall a. a -> [a] -> [a]
: (SDoc -> SDoc) -> [SDoc] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (SDoc
comma SDoc -> SDoc -> SDoc
<+>) [SDoc]
sdocs
ppr t :: GrdTree
t@Sequence{} = SDoc -> SDoc
braces (SDoc
space SDoc -> SDoc -> SDoc
<> [SDoc] -> SDoc
fsep (SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
semi (GrdTree -> [SDoc]
collect_seqs GrdTree
t)) SDoc -> SDoc -> SDoc
<> SDoc
space)
where
collect_seqs :: GrdTree -> [SDoc]
collect_seqs (Sequence GrdTree
l GrdTree
r) = GrdTree -> [SDoc]
collect_seqs GrdTree
l [SDoc] -> [SDoc] -> [SDoc]
forall a. [a] -> [a] -> [a]
++ GrdTree -> [SDoc]
collect_seqs GrdTree
r
collect_seqs GrdTree
t = [GrdTree -> SDoc
forall a. Outputable a => a -> SDoc
ppr GrdTree
t]
ppr GrdTree
Empty = String -> SDoc
text String
"<empty case>"
instance Outputable AnnotatedTree where
ppr :: AnnotatedTree -> SDoc
ppr (AccessibleRhs Deltas
_ RhsInfo
info) = RhsInfo -> SDoc
pprRhsInfo RhsInfo
info
ppr (InaccessibleRhs RhsInfo
info) = String -> SDoc
text String
"inaccessible" SDoc -> SDoc -> SDoc
<+> RhsInfo -> SDoc
pprRhsInfo RhsInfo
info
ppr (MayDiverge AnnotatedTree
t) = String -> SDoc
text String
"div" SDoc -> SDoc -> SDoc
<+> AnnotatedTree -> SDoc
forall a. Outputable a => a -> SDoc
ppr AnnotatedTree
t
ppr t :: AnnotatedTree
t@SequenceAnn{} = SDoc -> SDoc
braces (SDoc
space SDoc -> SDoc -> SDoc
<> [SDoc] -> SDoc
fsep (SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
semi (AnnotatedTree -> [SDoc]
collect_seqs AnnotatedTree
t)) SDoc -> SDoc -> SDoc
<> SDoc
space)
where
collect_seqs :: AnnotatedTree -> [SDoc]
collect_seqs (SequenceAnn AnnotatedTree
l AnnotatedTree
r) = AnnotatedTree -> [SDoc]
collect_seqs AnnotatedTree
l [SDoc] -> [SDoc] -> [SDoc]
forall a. [a] -> [a] -> [a]
++ AnnotatedTree -> [SDoc]
collect_seqs AnnotatedTree
r
collect_seqs AnnotatedTree
t = [AnnotatedTree -> SDoc
forall a. Outputable a => a -> SDoc
ppr AnnotatedTree
t]
ppr AnnotatedTree
EmptyAnn = String -> SDoc
text String
"<empty case>"
addPmCtsDeltas :: Deltas -> PmCts -> DsM Deltas
addPmCtsDeltas :: Deltas -> PmCts -> DsM Deltas
addPmCtsDeltas Deltas
deltas PmCts
cts = (Delta -> IOEnv (Env DsGblEnv DsLclEnv) (Maybe Delta))
-> Deltas -> DsM Deltas
forall (m :: * -> *).
Monad m =>
(Delta -> m (Maybe Delta)) -> Deltas -> m Deltas
liftDeltasM (\Delta
d -> Delta -> PmCts -> IOEnv (Env DsGblEnv DsLclEnv) (Maybe Delta)
addPmCts Delta
d PmCts
cts) Deltas
deltas
addPmCtDeltas :: Deltas -> PmCt -> DsM Deltas
addPmCtDeltas :: Deltas -> PmCt -> DsM Deltas
addPmCtDeltas Deltas
deltas PmCt
ct = Deltas -> PmCts -> DsM Deltas
addPmCtsDeltas Deltas
deltas (PmCt -> PmCts
forall a. a -> Bag a
unitBag PmCt
ct)
isInhabited :: Deltas -> DsM Bool
isInhabited :: Deltas -> DsM Bool
isInhabited (MkDeltas Bag Delta
ds) = Bool -> DsM Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Bool
not (Bag Delta -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Bag Delta
ds))
data CheckResult
= CheckResult
{ CheckResult -> AnnotatedTree
cr_clauses :: !AnnotatedTree
, CheckResult -> Deltas
cr_uncov :: !Deltas
, CheckResult -> Precision
cr_approx :: !Precision
}
instance Outputable CheckResult where
ppr :: CheckResult -> SDoc
ppr (CheckResult AnnotatedTree
c Deltas
unc Precision
pc)
= String -> SDoc
text String
"CheckResult" SDoc -> SDoc -> SDoc
<+> Precision -> SDoc
ppr_precision Precision
pc SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
braces ([SDoc] -> SDoc
fsep
[ String -> AnnotatedTree -> SDoc
forall a. Outputable a => String -> a -> SDoc
field String
"clauses" AnnotatedTree
c SDoc -> SDoc -> SDoc
<> SDoc
comma
, String -> Deltas -> SDoc
forall a. Outputable a => String -> a -> SDoc
field String
"uncov" Deltas
unc])
where
ppr_precision :: Precision -> SDoc
ppr_precision Precision
Precise = SDoc
empty
ppr_precision Precision
Approximate = String -> SDoc
text String
"(Approximate)"
field :: String -> a -> SDoc
field String
name a
value = String -> SDoc
text String
name SDoc -> SDoc -> SDoc
<+> SDoc
equals SDoc -> SDoc -> SDoc
<+> a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
value
checkSingle :: DynFlags -> DsMatchContext -> Id -> Pat GhcTc -> DsM ()
checkSingle :: DynFlags -> DsMatchContext -> Id -> Pat GhcTc -> DsM ()
checkSingle DynFlags
dflags ctxt :: DsMatchContext
ctxt@(DsMatchContext HsMatchContext GhcRn
kind SrcSpan
locn) Id
var Pat GhcTc
p = do
String -> SDoc -> DsM ()
tracePm String
"checkSingle" ([SDoc] -> SDoc
vcat [DsMatchContext -> SDoc
forall a. Outputable a => a -> SDoc
ppr DsMatchContext
ctxt, Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
var, Pat GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr Pat GhcTc
p])
Bool -> DsM () -> DsM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (DynFlags -> HsMatchContext GhcRn -> Bool
forall id. DynFlags -> HsMatchContext id -> Bool
exhaustive DynFlags
dflags HsMatchContext GhcRn
kind) (DsM () -> DsM ()) -> DsM () -> DsM ()
forall a b. (a -> b) -> a -> b
$ do
Deltas
missing <- DsM Deltas
getPmDeltas
String -> SDoc -> DsM ()
tracePm String
"checkSingle: missing" (Deltas -> SDoc
forall a. Outputable a => a -> SDoc
ppr Deltas
missing)
FamInstEnvs
fam_insts <- DsM FamInstEnvs
dsGetFamInstEnvs
GrdTree
grd_tree <- RhsInfo -> [PmGrd] -> GrdTree
mkGrdTreeRhs (SrcSpan -> SDoc -> RhsInfo
forall l e. l -> e -> GenLocated l e
L SrcSpan
locn (SDoc -> RhsInfo) -> SDoc -> RhsInfo
forall a b. (a -> b) -> a -> b
$ Pat GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr Pat GhcTc
p) ([PmGrd] -> GrdTree)
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
-> IOEnv (Env DsGblEnv DsLclEnv) GrdTree
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FamInstEnvs
-> Id -> Pat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translatePat FamInstEnvs
fam_insts Id
var Pat GhcTc
p
CheckResult
res <- GrdTree -> Deltas -> DsM CheckResult
checkGrdTree GrdTree
grd_tree Deltas
missing
DynFlags -> DsMatchContext -> [Id] -> CheckResult -> DsM ()
dsPmWarn DynFlags
dflags DsMatchContext
ctxt [Id
var] CheckResult
res
checkGuardMatches
:: HsMatchContext GhcRn
-> GRHSs GhcTc (LHsExpr GhcTc)
-> DsM [Deltas]
checkGuardMatches :: HsMatchContext GhcRn -> GRHSs GhcTc (LHsExpr GhcTc) -> DsM [Deltas]
checkGuardMatches HsMatchContext GhcRn
hs_ctx guards :: GRHSs GhcTc (LHsExpr GhcTc)
guards@(GRHSs XCGRHSs GhcTc (LHsExpr GhcTc)
_ [LGRHS GhcTc (LHsExpr GhcTc)]
grhss LHsLocalBinds GhcTc
_) = do
let combinedLoc :: SrcSpan
combinedLoc = (SrcSpan -> SrcSpan -> SrcSpan) -> [SrcSpan] -> SrcSpan
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 SrcSpan -> SrcSpan -> SrcSpan
combineSrcSpans ((LGRHS GhcTc (LHsExpr GhcTc) -> SrcSpan)
-> [LGRHS GhcTc (LHsExpr GhcTc)] -> [SrcSpan]
forall a b. (a -> b) -> [a] -> [b]
map LGRHS GhcTc (LHsExpr GhcTc) -> SrcSpan
forall l e. GenLocated l e -> l
getLoc [LGRHS GhcTc (LHsExpr GhcTc)]
grhss)
dsMatchContext :: DsMatchContext
dsMatchContext = HsMatchContext GhcRn -> SrcSpan -> DsMatchContext
DsMatchContext HsMatchContext GhcRn
hs_ctx SrcSpan
combinedLoc
match :: GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))
match = SrcSpan
-> Match GhcTc (LHsExpr GhcTc)
-> GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))
forall l e. l -> e -> GenLocated l e
L SrcSpan
combinedLoc (Match GhcTc (LHsExpr GhcTc)
-> GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc)))
-> Match GhcTc (LHsExpr GhcTc)
-> GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))
forall a b. (a -> b) -> a -> b
$
Match :: forall p body.
XCMatch p body
-> HsMatchContext (NoGhcTc p)
-> [LPat p]
-> GRHSs p body
-> Match p body
Match { m_ext :: XCMatch GhcTc (LHsExpr GhcTc)
m_ext = NoExtField
XCMatch GhcTc (LHsExpr GhcTc)
noExtField
, m_ctxt :: HsMatchContext (NoGhcTc GhcTc)
m_ctxt = HsMatchContext GhcRn
HsMatchContext (NoGhcTc GhcTc)
hs_ctx
, m_pats :: [LPat GhcTc]
m_pats = []
, m_grhss :: GRHSs GhcTc (LHsExpr GhcTc)
m_grhss = GRHSs GhcTc (LHsExpr GhcTc)
guards }
DsMatchContext
-> [Id]
-> [GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))]
-> DsM [Deltas]
checkMatches DsMatchContext
dsMatchContext [] [GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))
match]
checkMatches
:: DsMatchContext
-> [Id]
-> [LMatch GhcTc (LHsExpr GhcTc)]
-> DsM [Deltas]
checkMatches :: DsMatchContext
-> [Id]
-> [GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))]
-> DsM [Deltas]
checkMatches DsMatchContext
ctxt [Id]
vars [GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))]
matches = do
DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
String -> SDoc -> DsM ()
tracePm String
"checkMatches" (SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
vcat [DsMatchContext -> SDoc
forall a. Outputable a => a -> SDoc
ppr DsMatchContext
ctxt
, [Id] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Id]
vars
, String -> SDoc
text String
"Matches:"])
Int
2
([SDoc] -> SDoc
vcat ((GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc)) -> SDoc)
-> [GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr [GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))]
matches)))
Deltas
init_deltas <- DsM Deltas
getPmDeltas
Deltas
missing <- case [GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))]
matches of
[] | [Id
var] <- [Id]
vars -> Deltas -> PmCt -> DsM Deltas
addPmCtDeltas Deltas
init_deltas (Id -> PmCt
PmNotBotCt Id
var)
[GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))]
_ -> Deltas -> DsM Deltas
forall (f :: * -> *) a. Applicative f => a -> f a
pure Deltas
init_deltas
FamInstEnvs
fam_insts <- DsM FamInstEnvs
dsGetFamInstEnvs
GrdTree
grd_tree <- [PmGrd] -> [GrdTree] -> GrdTree
mkGrdTreeMany [] ([GrdTree] -> GrdTree)
-> IOEnv (Env DsGblEnv DsLclEnv) [GrdTree]
-> IOEnv (Env DsGblEnv DsLclEnv) GrdTree
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))
-> IOEnv (Env DsGblEnv DsLclEnv) GrdTree)
-> [GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))]
-> IOEnv (Env DsGblEnv DsLclEnv) [GrdTree]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (FamInstEnvs
-> [Id]
-> GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))
-> IOEnv (Env DsGblEnv DsLclEnv) GrdTree
translateMatch FamInstEnvs
fam_insts [Id]
vars) [GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))]
matches
CheckResult
res <- GrdTree -> Deltas -> DsM CheckResult
checkGrdTree GrdTree
grd_tree Deltas
missing
DynFlags -> DsMatchContext -> [Id] -> CheckResult -> DsM ()
dsPmWarn DynFlags
dflags DsMatchContext
ctxt [Id]
vars CheckResult
res
[Deltas] -> DsM [Deltas]
forall (m :: * -> *) a. Monad m => a -> m a
return (Deltas -> AnnotatedTree -> [Deltas]
extractRhsDeltas Deltas
init_deltas (CheckResult -> AnnotatedTree
cr_clauses CheckResult
res))
extractRhsDeltas :: Deltas -> AnnotatedTree -> [Deltas]
Deltas
orig_deltas = OrdList Deltas -> [Deltas]
forall a. OrdList a -> [a]
fromOL (OrdList Deltas -> [Deltas])
-> (AnnotatedTree -> OrdList Deltas) -> AnnotatedTree -> [Deltas]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnotatedTree -> OrdList Deltas
go
where
go :: AnnotatedTree -> OrdList Deltas
go (AccessibleRhs Deltas
deltas RhsInfo
_) = Deltas -> OrdList Deltas
forall a. a -> OrdList a
unitOL Deltas
deltas
go (InaccessibleRhs RhsInfo
_) = Deltas -> OrdList Deltas
forall a. a -> OrdList a
unitOL Deltas
orig_deltas
go (MayDiverge AnnotatedTree
t) = AnnotatedTree -> OrdList Deltas
go AnnotatedTree
t
go (SequenceAnn AnnotatedTree
l AnnotatedTree
r) = AnnotatedTree -> OrdList Deltas
go AnnotatedTree
l OrdList Deltas -> OrdList Deltas -> OrdList Deltas
forall a. Semigroup a => a -> a -> a
Semi.<> AnnotatedTree -> OrdList Deltas
go AnnotatedTree
r
go AnnotatedTree
EmptyAnn = OrdList Deltas
forall a. OrdList a
nilOL
mkPmLetVar :: Id -> Id -> GrdVec
mkPmLetVar :: Id -> Id -> [PmGrd]
mkPmLetVar Id
x Id
y | Id
x Id -> Id -> Bool
forall a. Eq a => a -> a -> Bool
== Id
y = []
mkPmLetVar Id
x Id
y = [Id -> CoreExpr -> PmGrd
PmLet Id
x (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
y)]
vanillaConGrd :: Id -> DataCon -> [Id] -> PmGrd
vanillaConGrd :: Id -> DataCon -> [Id] -> PmGrd
vanillaConGrd Id
scrut DataCon
con [Id]
arg_ids =
PmCon :: Id -> PmAltCon -> [Id] -> [Id] -> [Id] -> PmGrd
PmCon { pm_id :: Id
pm_id = Id
scrut, pm_con_con :: PmAltCon
pm_con_con = ConLike -> PmAltCon
PmAltConLike (DataCon -> ConLike
RealDataCon DataCon
con)
, pm_con_tvs :: [Id]
pm_con_tvs = [], pm_con_dicts :: [Id]
pm_con_dicts = [], pm_con_args :: [Id]
pm_con_args = [Id]
arg_ids }
mkListGrds :: Id -> [(Id, GrdVec)] -> DsM GrdVec
mkListGrds :: Id -> [(Id, [PmGrd])] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
mkListGrds Id
a [] = [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Id -> DataCon -> [Id] -> PmGrd
vanillaConGrd Id
a DataCon
nilDataCon []]
mkListGrds Id
a ((Id
x, [PmGrd]
head_grds):[(Id, [PmGrd])]
xs) = do
Id
b <- Type -> DsM Id
mkPmId (Id -> Type
idType Id
a)
[PmGrd]
tail_grds <- Id -> [(Id, [PmGrd])] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
mkListGrds Id
b [(Id, [PmGrd])]
xs
[PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a b. (a -> b) -> a -> b
$ Id -> DataCon -> [Id] -> PmGrd
vanillaConGrd Id
a DataCon
consDataCon [Id
x, Id
b] PmGrd -> [PmGrd] -> [PmGrd]
forall a. a -> [a] -> [a]
: [PmGrd]
head_grds [PmGrd] -> [PmGrd] -> [PmGrd]
forall a. [a] -> [a] -> [a]
++ [PmGrd]
tail_grds
mkPmLitGrds :: Id -> PmLit -> DsM GrdVec
mkPmLitGrds :: Id -> PmLit -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
mkPmLitGrds Id
x (PmLit Type
_ (PmLitString FastString
s)) = do
[Id]
vars <- (Type -> DsM Id) -> [Type] -> IOEnv (Env DsGblEnv DsLclEnv) [Id]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Type -> DsM Id
mkPmId (Int -> [Type] -> [Type]
forall a. Int -> [a] -> [a]
take (FastString -> Int
lengthFS FastString
s) (Type -> [Type]
forall a. a -> [a]
repeat Type
charTy))
let mk_char_lit :: Id -> Char -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
mk_char_lit Id
y Char
c = Id -> PmLit -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
mkPmLitGrds Id
y (Type -> PmLitValue -> PmLit
PmLit Type
charTy (Char -> PmLitValue
PmLitChar Char
c))
[[PmGrd]]
char_grdss <- (Id -> Char -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> [Id] -> String -> IOEnv (Env DsGblEnv DsLclEnv) [[PmGrd]]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM Id -> Char -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
mk_char_lit [Id]
vars (FastString -> String
unpackFS FastString
s)
Id -> [(Id, [PmGrd])] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
mkListGrds Id
x ([Id] -> [[PmGrd]] -> [(Id, [PmGrd])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Id]
vars [[PmGrd]]
char_grdss)
mkPmLitGrds Id
x PmLit
lit = do
let grd :: PmGrd
grd = PmCon :: Id -> PmAltCon -> [Id] -> [Id] -> [Id] -> PmGrd
PmCon { pm_id :: Id
pm_id = Id
x
, pm_con_con :: PmAltCon
pm_con_con = PmLit -> PmAltCon
PmAltLit PmLit
lit
, pm_con_tvs :: [Id]
pm_con_tvs = []
, pm_con_dicts :: [Id]
pm_con_dicts = []
, pm_con_args :: [Id]
pm_con_args = [] }
[PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [PmGrd
grd]
translatePat :: FamInstEnvs -> Id -> Pat GhcTc -> DsM GrdVec
translatePat :: FamInstEnvs
-> Id -> Pat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translatePat FamInstEnvs
fam_insts Id
x Pat GhcTc
pat = case Pat GhcTc
pat of
WildPat XWildPat GhcTc
_ty -> [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
VarPat XVarPat GhcTc
_ Located (IdP GhcTc)
y -> [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Id -> Id -> [PmGrd]
mkPmLetVar (GenLocated SrcSpan Id -> Id
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpan Id
Located (IdP GhcTc)
y) Id
x)
ParPat XParPat GhcTc
_ LPat GhcTc
p -> FamInstEnvs
-> Id -> LPat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateLPat FamInstEnvs
fam_insts Id
x LPat GhcTc
p
LazyPat XLazyPat GhcTc
_ LPat GhcTc
_ -> [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
BangPat XBangPat GhcTc
_ LPat GhcTc
p ->
(Id -> PmGrd
PmBang Id
x PmGrd -> [PmGrd] -> [PmGrd]
forall a. a -> [a] -> [a]
:) ([PmGrd] -> [PmGrd])
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FamInstEnvs
-> Id -> LPat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateLPat FamInstEnvs
fam_insts Id
x LPat GhcTc
p
AsPat XAsPat GhcTc
_ (L SrcSpan
_ IdP GhcTc
y) LPat GhcTc
p -> (Id -> Id -> [PmGrd]
mkPmLetVar Id
IdP GhcTc
y Id
x [PmGrd] -> [PmGrd] -> [PmGrd]
forall a. [a] -> [a] -> [a]
++) ([PmGrd] -> [PmGrd])
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FamInstEnvs
-> Id -> LPat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateLPat FamInstEnvs
fam_insts Id
IdP GhcTc
y LPat GhcTc
p
SigPat XSigPat GhcTc
_ LPat GhcTc
p HsPatSigType (NoGhcTc GhcTc)
_ty -> FamInstEnvs
-> Id -> LPat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateLPat FamInstEnvs
fam_insts Id
x LPat GhcTc
p
XPat (CoPat wrapper p _ty)
| HsWrapper -> Bool
isIdHsWrapper HsWrapper
wrapper -> FamInstEnvs
-> Id -> Pat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translatePat FamInstEnvs
fam_insts Id
x Pat GhcTc
p
| WpCast TcCoercionR
co <- HsWrapper
wrapper, TcCoercionR -> Bool
isReflexiveCo TcCoercionR
co -> FamInstEnvs
-> Id -> Pat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translatePat FamInstEnvs
fam_insts Id
x Pat GhcTc
p
| Bool
otherwise -> do
(Id
y, [PmGrd]
grds) <- FamInstEnvs -> Pat GhcTc -> DsM (Id, [PmGrd])
translatePatV FamInstEnvs
fam_insts Pat GhcTc
p
CoreExpr -> CoreExpr
wrap_rhs_y <- HsWrapper -> DsM (CoreExpr -> CoreExpr)
dsHsWrapper HsWrapper
wrapper
[PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Id -> CoreExpr -> PmGrd
PmLet Id
y (CoreExpr -> CoreExpr
wrap_rhs_y (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
x)) PmGrd -> [PmGrd] -> [PmGrd]
forall a. a -> [a] -> [a]
: [PmGrd]
grds)
NPlusKPat XNPlusKPat GhcTc
_pat_ty (L SrcSpan
_ IdP GhcTc
n) Located (HsOverLit GhcTc)
k1 HsOverLit GhcTc
k2 SyntaxExpr GhcTc
ge SyntaxExpr GhcTc
minus -> do
Id
b <- Type -> DsM Id
mkPmId Type
boolTy
let grd_b :: PmGrd
grd_b = Id -> DataCon -> [Id] -> PmGrd
vanillaConGrd Id
b DataCon
trueDataCon []
[CoreExpr
ke1, CoreExpr
ke2] <- (HsOverLit GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr)
-> [HsOverLit GhcTc] -> IOEnv (Env DsGblEnv DsLclEnv) [CoreExpr]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse HsOverLit GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
dsOverLit [Located (HsOverLit GhcTc) -> HsOverLit GhcTc
forall l e. GenLocated l e -> e
unLoc Located (HsOverLit GhcTc)
k1, HsOverLit GhcTc
k2]
CoreExpr
rhs_b <- SyntaxExpr GhcTc
-> [CoreExpr] -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
ge [Id -> CoreExpr
forall b. Id -> Expr b
Var Id
x, CoreExpr
ke1]
CoreExpr
rhs_n <- SyntaxExpr GhcTc
-> [CoreExpr] -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
minus [Id -> CoreExpr
forall b. Id -> Expr b
Var Id
x, CoreExpr
ke2]
[PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Id -> CoreExpr -> PmGrd
PmLet Id
b CoreExpr
rhs_b, PmGrd
grd_b, Id -> CoreExpr -> PmGrd
PmLet Id
IdP GhcTc
n CoreExpr
rhs_n]
ViewPat XViewPat GhcTc
_arg_ty LHsExpr GhcTc
lexpr LPat GhcTc
pat -> do
(Id
y, [PmGrd]
grds) <- FamInstEnvs -> LPat GhcTc -> DsM (Id, [PmGrd])
translateLPatV FamInstEnvs
fam_insts LPat GhcTc
pat
CoreExpr
fun <- LHsExpr GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
dsLExpr LHsExpr GhcTc
lexpr
[PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a b. (a -> b) -> a -> b
$ Id -> CoreExpr -> PmGrd
PmLet Id
y (CoreExpr -> CoreExpr -> CoreExpr
forall b. Expr b -> Expr b -> Expr b
App CoreExpr
fun (Id -> CoreExpr
forall b. Id -> Expr b
Var Id
x)) PmGrd -> [PmGrd] -> [PmGrd]
forall a. a -> [a] -> [a]
: [PmGrd]
grds
ListPat (ListPatTc _elem_ty Nothing) [LPat GhcTc]
ps ->
FamInstEnvs
-> Id -> [LPat GhcTc] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateListPat FamInstEnvs
fam_insts Id
x [LPat GhcTc]
ps
ListPat (ListPatTc elem_ty (Just (pat_ty, to_list))) [LPat GhcTc]
pats -> do
DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
case Type -> Maybe Type
splitListTyConApp_maybe Type
pat_ty of
Just Type
_e_ty
| Bool -> Bool
not (Extension -> DynFlags -> Bool
xopt Extension
LangExt.RebindableSyntax DynFlags
dflags)
-> FamInstEnvs
-> Id -> [LPat GhcTc] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateListPat FamInstEnvs
fam_insts Id
x [LPat GhcTc]
pats
Maybe Type
_ -> do
Id
y <- Type -> DsM Id
mkPmId (Type -> Type
mkListTy Type
elem_ty)
[PmGrd]
grds <- FamInstEnvs
-> Id -> [LPat GhcTc] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateListPat FamInstEnvs
fam_insts Id
y [LPat GhcTc]
pats
CoreExpr
rhs_y <- SyntaxExpr GhcTc
-> [CoreExpr] -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
to_list [Id -> CoreExpr
forall b. Id -> Expr b
Var Id
x]
[PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a b. (a -> b) -> a -> b
$ Id -> CoreExpr -> PmGrd
PmLet Id
y CoreExpr
rhs_y PmGrd -> [PmGrd] -> [PmGrd]
forall a. a -> [a] -> [a]
: [PmGrd]
grds
ConPat { pat_con :: forall p. Pat p -> Located (ConLikeP p)
pat_con = L SrcSpan
_ ConLikeP GhcTc
con
, pat_args :: forall p. Pat p -> HsConPatDetails p
pat_args = HsConPatDetails GhcTc
ps
, pat_con_ext :: forall p. Pat p -> XConPat p
pat_con_ext = ConPatTc
{ cpt_arg_tys = arg_tys
, cpt_tvs = ex_tvs
, cpt_dicts = dicts
}
} -> do
FamInstEnvs
-> Id
-> ConLike
-> [Type]
-> [Id]
-> [Id]
-> HsConPatDetails GhcTc
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateConPatOut FamInstEnvs
fam_insts Id
x ConLike
ConLikeP GhcTc
con [Type]
arg_tys [Id]
ex_tvs [Id]
dicts HsConPatDetails GhcTc
ps
NPat XNPat GhcTc
ty (L SrcSpan
_ HsOverLit GhcTc
olit) Maybe (SyntaxExpr GhcTc)
mb_neg SyntaxExpr GhcTc
_ -> do
DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
CoreExpr
core_expr <- case HsOverLit GhcTc
olit of
OverLit{ ol_val :: forall p. HsOverLit p -> OverLitVal
ol_val = OverLitVal
val, ol_ext :: forall p. HsOverLit p -> XOverLit p
ol_ext = OverLitTc rebindable _ }
| Bool -> Bool
not Bool
rebindable
, Just HsExpr GhcTc
expr <- Platform -> OverLitVal -> Type -> Maybe (HsExpr GhcTc)
shortCutLit Platform
platform OverLitVal
val Type
XNPat GhcTc
ty
-> HsExpr GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
dsExpr HsExpr GhcTc
expr
HsOverLit GhcTc
_ -> HsOverLit GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
dsOverLit HsOverLit GhcTc
olit
let lit :: PmLit
lit = String -> Maybe PmLit -> PmLit
forall a. HasCallStack => String -> Maybe a -> a
expectJust String
"failed to detect OverLit" (CoreExpr -> Maybe PmLit
coreExprAsPmLit CoreExpr
core_expr)
let lit' :: PmLit
lit' = case Maybe (SyntaxExpr GhcTc)
mb_neg of
Just SyntaxExpr GhcTc
_ -> String -> Maybe PmLit -> PmLit
forall a. HasCallStack => String -> Maybe a -> a
expectJust String
"failed to negate lit" (PmLit -> Maybe PmLit
negatePmLit PmLit
lit)
Maybe (SyntaxExpr GhcTc)
Nothing -> PmLit
lit
Id -> PmLit -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
mkPmLitGrds Id
x PmLit
lit'
LitPat XLitPat GhcTc
_ HsLit GhcTc
lit -> do
CoreExpr
core_expr <- HsLit GhcRn -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
dsLit (HsLit GhcTc -> HsLit GhcRn
forall (p1 :: Pass) (p2 :: Pass).
HsLit (GhcPass p1) -> HsLit (GhcPass p2)
convertLit HsLit GhcTc
lit)
let lit :: PmLit
lit = String -> Maybe PmLit -> PmLit
forall a. HasCallStack => String -> Maybe a -> a
expectJust String
"failed to detect Lit" (CoreExpr -> Maybe PmLit
coreExprAsPmLit CoreExpr
core_expr)
Id -> PmLit -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
mkPmLitGrds Id
x PmLit
lit
TuplePat XTuplePat GhcTc
_tys [LPat GhcTc]
pats Boxity
boxity -> do
([Id]
vars, [[PmGrd]]
grdss) <- (Located (Pat GhcTc) -> DsM (Id, [PmGrd]))
-> [Located (Pat GhcTc)]
-> IOEnv (Env DsGblEnv DsLclEnv) ([Id], [[PmGrd]])
forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM (FamInstEnvs -> LPat GhcTc -> DsM (Id, [PmGrd])
translateLPatV FamInstEnvs
fam_insts) [Located (Pat GhcTc)]
[LPat GhcTc]
pats
let tuple_con :: DataCon
tuple_con = Boxity -> Int -> DataCon
tupleDataCon Boxity
boxity ([Id] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Id]
vars)
[PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a b. (a -> b) -> a -> b
$ Id -> DataCon -> [Id] -> PmGrd
vanillaConGrd Id
x DataCon
tuple_con [Id]
vars PmGrd -> [PmGrd] -> [PmGrd]
forall a. a -> [a] -> [a]
: [[PmGrd]] -> [PmGrd]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[PmGrd]]
grdss
SumPat XSumPat GhcTc
_ty LPat GhcTc
p Int
alt Int
arity -> do
(Id
y, [PmGrd]
grds) <- FamInstEnvs -> LPat GhcTc -> DsM (Id, [PmGrd])
translateLPatV FamInstEnvs
fam_insts LPat GhcTc
p
let sum_con :: DataCon
sum_con = Int -> Int -> DataCon
sumDataCon Int
alt Int
arity
[PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a b. (a -> b) -> a -> b
$ Id -> DataCon -> [Id] -> PmGrd
vanillaConGrd Id
x DataCon
sum_con [Id
y] PmGrd -> [PmGrd] -> [PmGrd]
forall a. a -> [a] -> [a]
: [PmGrd]
grds
SplicePat {} -> String -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a. String -> a
panic String
"Check.translatePat: SplicePat"
translatePatV :: FamInstEnvs -> Pat GhcTc -> DsM (Id, GrdVec)
translatePatV :: FamInstEnvs -> Pat GhcTc -> DsM (Id, [PmGrd])
translatePatV FamInstEnvs
fam_insts Pat GhcTc
pat = do
Id
x <- Type -> Pat GhcTc -> DsM Id
selectMatchVar Type
Many Pat GhcTc
pat
[PmGrd]
grds <- FamInstEnvs
-> Id -> Pat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translatePat FamInstEnvs
fam_insts Id
x Pat GhcTc
pat
(Id, [PmGrd]) -> DsM (Id, [PmGrd])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Id
x, [PmGrd]
grds)
translateLPat :: FamInstEnvs -> Id -> LPat GhcTc -> DsM GrdVec
translateLPat :: FamInstEnvs
-> Id -> LPat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateLPat FamInstEnvs
fam_insts Id
x = FamInstEnvs
-> Id -> Pat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translatePat FamInstEnvs
fam_insts Id
x (Pat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> (Located (Pat GhcTc) -> Pat GhcTc)
-> Located (Pat GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Located (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc
translateLPatV :: FamInstEnvs -> LPat GhcTc -> DsM (Id, GrdVec)
translateLPatV :: FamInstEnvs -> LPat GhcTc -> DsM (Id, [PmGrd])
translateLPatV FamInstEnvs
fam_insts = FamInstEnvs -> Pat GhcTc -> DsM (Id, [PmGrd])
translatePatV FamInstEnvs
fam_insts (Pat GhcTc -> DsM (Id, [PmGrd]))
-> (Located (Pat GhcTc) -> Pat GhcTc)
-> Located (Pat GhcTc)
-> DsM (Id, [PmGrd])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Located (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc
translateListPat :: FamInstEnvs -> Id -> [LPat GhcTc] -> DsM GrdVec
translateListPat :: FamInstEnvs
-> Id -> [LPat GhcTc] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateListPat FamInstEnvs
fam_insts Id
x [LPat GhcTc]
pats = do
[(Id, [PmGrd])]
vars_and_grdss <- (Located (Pat GhcTc) -> DsM (Id, [PmGrd]))
-> [Located (Pat GhcTc)]
-> IOEnv (Env DsGblEnv DsLclEnv) [(Id, [PmGrd])]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (FamInstEnvs -> LPat GhcTc -> DsM (Id, [PmGrd])
translateLPatV FamInstEnvs
fam_insts) [Located (Pat GhcTc)]
[LPat GhcTc]
pats
Id -> [(Id, [PmGrd])] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
mkListGrds Id
x [(Id, [PmGrd])]
vars_and_grdss
translateConPatOut :: FamInstEnvs -> Id -> ConLike -> [Type] -> [TyVar]
-> [EvVar] -> HsConPatDetails GhcTc -> DsM GrdVec
translateConPatOut :: FamInstEnvs
-> Id
-> ConLike
-> [Type]
-> [Id]
-> [Id]
-> HsConPatDetails GhcTc
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateConPatOut FamInstEnvs
fam_insts Id
x ConLike
con [Type]
univ_tys [Id]
ex_tvs [Id]
dicts = \case
PrefixCon [LPat GhcTc]
ps -> [(Int, Located (Pat GhcTc))]
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
go_field_pats ([Int] -> [Located (Pat GhcTc)] -> [(Int, Located (Pat GhcTc))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [Located (Pat GhcTc)]
[LPat GhcTc]
ps)
InfixCon LPat GhcTc
p1 LPat GhcTc
p2 -> [(Int, Located (Pat GhcTc))]
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
go_field_pats ([Int] -> [Located (Pat GhcTc)] -> [(Int, Located (Pat GhcTc))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [Located (Pat GhcTc)
LPat GhcTc
p1,Located (Pat GhcTc)
LPat GhcTc
p2])
RecCon (HsRecFields [LHsRecField GhcTc (LPat GhcTc)]
fs Maybe (Located Int)
_) -> [(Int, Located (Pat GhcTc))]
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
go_field_pats ([GenLocated SrcSpan (HsRecField GhcTc (Located (Pat GhcTc)))]
-> [(Int, Located (Pat GhcTc))]
rec_field_ps [GenLocated SrcSpan (HsRecField GhcTc (Located (Pat GhcTc)))]
[LHsRecField GhcTc (LPat GhcTc)]
fs)
where
arg_tys :: [Type]
arg_tys = (Scaled Type -> Type) -> [Scaled Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Scaled Type -> Type
forall a. Scaled a -> a
scaledThing ([Scaled Type] -> [Type]) -> [Scaled Type] -> [Type]
forall a b. (a -> b) -> a -> b
$ ConLike -> [Type] -> [Scaled Type]
conLikeInstOrigArgTys ConLike
con ([Type]
univ_tys [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Id] -> [Type]
mkTyVarTys [Id]
ex_tvs)
rec_field_ps :: [GenLocated SrcSpan (HsRecField GhcTc (Located (Pat GhcTc)))]
-> [(Int, Located (Pat GhcTc))]
rec_field_ps [GenLocated SrcSpan (HsRecField GhcTc (Located (Pat GhcTc)))]
fs = (GenLocated SrcSpan (HsRecField GhcTc (Located (Pat GhcTc)))
-> (Int, Located (Pat GhcTc)))
-> [GenLocated SrcSpan (HsRecField GhcTc (Located (Pat GhcTc)))]
-> [(Int, Located (Pat GhcTc))]
forall a b. (a -> b) -> [a] -> [b]
map (HsRecField GhcTc (Located (Pat GhcTc))
-> (Int, Located (Pat GhcTc))
tagged_pat (HsRecField GhcTc (Located (Pat GhcTc))
-> (Int, Located (Pat GhcTc)))
-> (GenLocated SrcSpan (HsRecField GhcTc (Located (Pat GhcTc)))
-> HsRecField GhcTc (Located (Pat GhcTc)))
-> GenLocated SrcSpan (HsRecField GhcTc (Located (Pat GhcTc)))
-> (Int, Located (Pat GhcTc))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpan (HsRecField GhcTc (Located (Pat GhcTc)))
-> HsRecField GhcTc (Located (Pat GhcTc))
forall l e. GenLocated l e -> e
unLoc) [GenLocated SrcSpan (HsRecField GhcTc (Located (Pat GhcTc)))]
fs
where
tagged_pat :: HsRecField GhcTc (Located (Pat GhcTc))
-> (Int, Located (Pat GhcTc))
tagged_pat HsRecField GhcTc (Located (Pat GhcTc))
f = (Name -> Int
lbl_to_index (GenLocated SrcSpan Id -> Name
forall a. NamedThing a => a -> Name
getName (HsRecField GhcTc (Located (Pat GhcTc)) -> GenLocated SrcSpan Id
forall arg. HsRecField GhcTc arg -> GenLocated SrcSpan Id
hsRecFieldId HsRecField GhcTc (Located (Pat GhcTc))
f)), HsRecField GhcTc (Located (Pat GhcTc)) -> Located (Pat GhcTc)
forall id arg. HsRecField' id arg -> arg
hsRecFieldArg HsRecField GhcTc (Located (Pat GhcTc))
f)
orig_lbls :: [Name]
orig_lbls = (FieldLbl Name -> Name) -> [FieldLbl Name] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map FieldLbl Name -> Name
forall a. FieldLbl a -> a
flSelector ([FieldLbl Name] -> [Name]) -> [FieldLbl Name] -> [Name]
forall a b. (a -> b) -> a -> b
$ ConLike -> [FieldLbl Name]
conLikeFieldLabels ConLike
con
lbl_to_index :: Name -> Int
lbl_to_index Name
lbl = String -> Maybe Int -> Int
forall a. HasCallStack => String -> Maybe a -> a
expectJust String
"lbl_to_index" (Maybe Int -> Int) -> Maybe Int -> Int
forall a b. (a -> b) -> a -> b
$ Name -> [Name] -> Maybe Int
forall a. Eq a => a -> [a] -> Maybe Int
elemIndex Name
lbl [Name]
orig_lbls
go_field_pats :: [(Int, Located (Pat GhcTc))]
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
go_field_pats [(Int, Located (Pat GhcTc))]
tagged_pats = do
let trans_pat :: (Int, Located (Pat GhcTc))
-> IOEnv (Env DsGblEnv DsLclEnv) ((Int, Id), [PmGrd])
trans_pat (Int
n, Located (Pat GhcTc)
pat) = do
(Id
var, [PmGrd]
pvec) <- FamInstEnvs -> LPat GhcTc -> DsM (Id, [PmGrd])
translateLPatV FamInstEnvs
fam_insts Located (Pat GhcTc)
LPat GhcTc
pat
((Int, Id), [PmGrd])
-> IOEnv (Env DsGblEnv DsLclEnv) ((Int, Id), [PmGrd])
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Int
n, Id
var), [PmGrd]
pvec)
([(Int, Id)]
tagged_vars, [[PmGrd]]
arg_grdss) <- ((Int, Located (Pat GhcTc))
-> IOEnv (Env DsGblEnv DsLclEnv) ((Int, Id), [PmGrd]))
-> [(Int, Located (Pat GhcTc))]
-> IOEnv (Env DsGblEnv DsLclEnv) ([(Int, Id)], [[PmGrd]])
forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM (Int, Located (Pat GhcTc))
-> IOEnv (Env DsGblEnv DsLclEnv) ((Int, Id), [PmGrd])
trans_pat [(Int, Located (Pat GhcTc))]
tagged_pats
let get_pat_id :: Int -> Type -> DsM Id
get_pat_id Int
n Type
ty = case Int -> [(Int, Id)] -> Maybe Id
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Int
n [(Int, Id)]
tagged_vars of
Just Id
var -> Id -> DsM Id
forall (f :: * -> *) a. Applicative f => a -> f a
pure Id
var
Maybe Id
Nothing -> Type -> DsM Id
mkPmId Type
ty
[Id]
arg_ids <- (Int -> Type -> DsM Id)
-> [Int] -> [Type] -> IOEnv (Env DsGblEnv DsLclEnv) [Id]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM Int -> Type -> DsM Id
get_pat_id [Int
0..] [Type]
arg_tys
let con_grd :: PmGrd
con_grd = Id -> PmAltCon -> [Id] -> [Id] -> [Id] -> PmGrd
PmCon Id
x (ConLike -> PmAltCon
PmAltConLike ConLike
con) [Id]
ex_tvs [Id]
dicts [Id]
arg_ids
let arg_is_banged :: [Bool]
arg_is_banged = (HsImplBang -> Bool) -> [HsImplBang] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map HsImplBang -> Bool
isBanged ([HsImplBang] -> [Bool]) -> [HsImplBang] -> [Bool]
forall a b. (a -> b) -> a -> b
$ ConLike -> [HsImplBang]
conLikeImplBangs ConLike
con
bang_grds :: [PmGrd]
bang_grds = (Id -> PmGrd) -> [Id] -> [PmGrd]
forall a b. (a -> b) -> [a] -> [b]
map Id -> PmGrd
PmBang ([Id] -> [PmGrd]) -> [Id] -> [PmGrd]
forall a b. (a -> b) -> a -> b
$ [Bool] -> [Id] -> [Id]
forall a. [Bool] -> [a] -> [a]
filterByList [Bool]
arg_is_banged [Id]
arg_ids
let arg_grds :: [PmGrd]
arg_grds = [[PmGrd]] -> [PmGrd]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[PmGrd]]
arg_grdss
[PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PmGrd
con_grd PmGrd -> [PmGrd] -> [PmGrd]
forall a. a -> [a] -> [a]
: [PmGrd]
bang_grds [PmGrd] -> [PmGrd] -> [PmGrd]
forall a. [a] -> [a] -> [a]
++ [PmGrd]
arg_grds)
mkGrdTreeRhs :: Located SDoc -> GrdVec -> GrdTree
mkGrdTreeRhs :: RhsInfo -> [PmGrd] -> GrdTree
mkGrdTreeRhs RhsInfo
sdoc = (PmGrd -> GrdTree -> GrdTree) -> GrdTree -> [PmGrd] -> GrdTree
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr PmGrd -> GrdTree -> GrdTree
Guard (RhsInfo -> GrdTree
Rhs RhsInfo
sdoc)
mkGrdTreeMany :: GrdVec -> [GrdTree] -> GrdTree
mkGrdTreeMany :: [PmGrd] -> [GrdTree] -> GrdTree
mkGrdTreeMany [PmGrd]
_ [] = GrdTree
Empty
mkGrdTreeMany [PmGrd]
grds [GrdTree]
trees = (PmGrd -> GrdTree -> GrdTree) -> GrdTree -> [PmGrd] -> GrdTree
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr PmGrd -> GrdTree -> GrdTree
Guard ((GrdTree -> GrdTree -> GrdTree) -> [GrdTree] -> GrdTree
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 GrdTree -> GrdTree -> GrdTree
Sequence [GrdTree]
trees) [PmGrd]
grds
translateMatch :: FamInstEnvs -> [Id] -> LMatch GhcTc (LHsExpr GhcTc)
-> DsM GrdTree
translateMatch :: FamInstEnvs
-> [Id]
-> GenLocated SrcSpan (Match GhcTc (LHsExpr GhcTc))
-> IOEnv (Env DsGblEnv DsLclEnv) GrdTree
translateMatch FamInstEnvs
fam_insts [Id]
vars (L SrcSpan
match_loc (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 (LHsExpr GhcTc)
grhss })) = do
[PmGrd]
pats' <- [[PmGrd]] -> [PmGrd]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[PmGrd]] -> [PmGrd])
-> IOEnv (Env DsGblEnv DsLclEnv) [[PmGrd]]
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Id
-> Located (Pat GhcTc) -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> [Id]
-> [Located (Pat GhcTc)]
-> IOEnv (Env DsGblEnv DsLclEnv) [[PmGrd]]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (FamInstEnvs
-> Id -> LPat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateLPat FamInstEnvs
fam_insts) [Id]
vars [Located (Pat GhcTc)]
[LPat GhcTc]
pats
[GrdTree]
grhss' <- (LGRHS GhcTc (LHsExpr GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) GrdTree)
-> [LGRHS GhcTc (LHsExpr GhcTc)]
-> IOEnv (Env DsGblEnv DsLclEnv) [GrdTree]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (FamInstEnvs
-> SrcSpan
-> [LPat GhcTc]
-> LGRHS GhcTc (LHsExpr GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) GrdTree
translateLGRHS FamInstEnvs
fam_insts SrcSpan
match_loc [LPat GhcTc]
pats) (GRHSs GhcTc (LHsExpr GhcTc) -> [LGRHS GhcTc (LHsExpr GhcTc)]
forall p body. GRHSs p body -> [LGRHS p body]
grhssGRHSs GRHSs GhcTc (LHsExpr GhcTc)
grhss)
GrdTree -> IOEnv (Env DsGblEnv DsLclEnv) GrdTree
forall (m :: * -> *) a. Monad m => a -> m a
return ([PmGrd] -> [GrdTree] -> GrdTree
mkGrdTreeMany [PmGrd]
pats' [GrdTree]
grhss')
translateLGRHS :: FamInstEnvs -> SrcSpan -> [LPat GhcTc] -> LGRHS GhcTc (LHsExpr GhcTc) -> DsM GrdTree
translateLGRHS :: FamInstEnvs
-> SrcSpan
-> [LPat GhcTc]
-> LGRHS GhcTc (LHsExpr GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) GrdTree
translateLGRHS FamInstEnvs
fam_insts SrcSpan
match_loc [LPat GhcTc]
pats (L SrcSpan
_loc (GRHS XCGRHS GhcTc (LHsExpr GhcTc)
_ [GuardLStmt GhcTc]
gs LHsExpr GhcTc
_)) =
RhsInfo -> [PmGrd] -> GrdTree
mkGrdTreeRhs RhsInfo
loc_sdoc ([PmGrd] -> GrdTree)
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
-> IOEnv (Env DsGblEnv DsLclEnv) GrdTree
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (GuardLStmt GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> [GuardLStmt GhcTc] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (m :: * -> *) a b. Monad m => (a -> m [b]) -> [a] -> m [b]
concatMapM (FamInstEnvs
-> GuardStmt GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateGuard FamInstEnvs
fam_insts (GuardStmt GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> (GuardLStmt GhcTc -> GuardStmt GhcTc)
-> GuardLStmt GhcTc
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GuardLStmt GhcTc -> GuardStmt GhcTc
forall l e. GenLocated l e -> e
unLoc) [GuardLStmt GhcTc]
gs
where
loc_sdoc :: RhsInfo
loc_sdoc
| [GuardLStmt GhcTc] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [GuardLStmt GhcTc]
gs = SrcSpan -> SDoc -> RhsInfo
forall l e. l -> e -> GenLocated l e
L SrcSpan
match_loc ([SDoc] -> SDoc
sep ((Located (Pat GhcTc) -> SDoc) -> [Located (Pat GhcTc)] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Located (Pat GhcTc) -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Located (Pat GhcTc)]
[LPat GhcTc]
pats))
| Bool
otherwise = SrcSpan -> SDoc -> RhsInfo
forall l e. l -> e -> GenLocated l e
L SrcSpan
grd_loc ([SDoc] -> SDoc
sep ((Located (Pat GhcTc) -> SDoc) -> [Located (Pat GhcTc)] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Located (Pat GhcTc) -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Located (Pat GhcTc)]
[LPat GhcTc]
pats) SDoc -> SDoc -> SDoc
<+> SDoc
vbar SDoc -> SDoc -> SDoc
<+> [GuardLStmt GhcTc] -> SDoc
forall a. Outputable a => [a] -> SDoc
interpp'SP [GuardLStmt GhcTc]
gs)
L SrcSpan
grd_loc GuardStmt GhcTc
_ = [GuardLStmt GhcTc] -> GuardLStmt GhcTc
forall a. [a] -> a
head [GuardLStmt GhcTc]
gs
translateGuard :: FamInstEnvs -> GuardStmt GhcTc -> DsM GrdVec
translateGuard :: FamInstEnvs
-> GuardStmt GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateGuard FamInstEnvs
fam_insts GuardStmt GhcTc
guard = case GuardStmt GhcTc
guard of
BodyStmt XBodyStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
e SyntaxExpr GhcTc
_ SyntaxExpr GhcTc
_ -> LHsExpr GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateBoolGuard LHsExpr GhcTc
e
LetStmt XLetStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsLocalBinds GhcTc
binds -> HsLocalBinds GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateLet (LHsLocalBinds GhcTc -> HsLocalBinds GhcTc
forall l e. GenLocated l e -> e
unLoc LHsLocalBinds GhcTc
binds)
BindStmt XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LPat GhcTc
p LHsExpr GhcTc
e -> FamInstEnvs
-> LPat GhcTc
-> LHsExpr GhcTc
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateBind FamInstEnvs
fam_insts LPat GhcTc
p LHsExpr GhcTc
e
LastStmt {} -> String -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a. String -> a
panic String
"translateGuard LastStmt"
ParStmt {} -> String -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a. String -> a
panic String
"translateGuard ParStmt"
TransStmt {} -> String -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a. String -> a
panic String
"translateGuard TransStmt"
RecStmt {} -> String -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a. String -> a
panic String
"translateGuard RecStmt"
ApplicativeStmt {} -> String -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a. String -> a
panic String
"translateGuard ApplicativeLastStmt"
translateLet :: HsLocalBinds GhcTc -> DsM GrdVec
translateLet :: HsLocalBinds GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateLet HsLocalBinds GhcTc
_binds = [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (m :: * -> *) a. Monad m => a -> m a
return []
translateBind :: FamInstEnvs -> LPat GhcTc -> LHsExpr GhcTc -> DsM GrdVec
translateBind :: FamInstEnvs
-> LPat GhcTc
-> LHsExpr GhcTc
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateBind FamInstEnvs
fam_insts LPat GhcTc
p LHsExpr GhcTc
e = LHsExpr GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
dsLExpr LHsExpr GhcTc
e IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
-> (CoreExpr -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Var Id
y
| Maybe DataCon
Nothing <- Id -> Maybe DataCon
isDataConId_maybe Id
y
-> FamInstEnvs
-> Id -> LPat GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateLPat FamInstEnvs
fam_insts Id
y LPat GhcTc
p
CoreExpr
rhs -> do
(Id
x, [PmGrd]
grds) <- FamInstEnvs -> LPat GhcTc -> DsM (Id, [PmGrd])
translateLPatV FamInstEnvs
fam_insts LPat GhcTc
p
[PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Id -> CoreExpr -> PmGrd
PmLet Id
x CoreExpr
rhs PmGrd -> [PmGrd] -> [PmGrd]
forall a. a -> [a] -> [a]
: [PmGrd]
grds)
translateBoolGuard :: LHsExpr GhcTc -> DsM GrdVec
translateBoolGuard :: LHsExpr GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
translateBoolGuard LHsExpr GhcTc
e
| Maybe (CoreExpr -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr) -> Bool
forall a. Maybe a -> Bool
isJust (LHsExpr GhcTc
-> Maybe (CoreExpr -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr)
isTrueLHsExpr LHsExpr GhcTc
e) = [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (m :: * -> *) a. Monad m => a -> m a
return []
| Bool
otherwise = LHsExpr GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
dsLExpr LHsExpr GhcTc
e IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
-> (CoreExpr -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Var Id
y
| Maybe DataCon
Nothing <- Id -> Maybe DataCon
isDataConId_maybe Id
y
-> [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Id -> DataCon -> [Id] -> PmGrd
vanillaConGrd Id
y DataCon
trueDataCon []]
CoreExpr
rhs -> do
Id
x <- Type -> DsM Id
mkPmId Type
boolTy
[PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd])
-> [PmGrd] -> IOEnv (Env DsGblEnv DsLclEnv) [PmGrd]
forall a b. (a -> b) -> a -> b
$ [Id -> CoreExpr -> PmGrd
PmLet Id
x CoreExpr
rhs, Id -> DataCon -> [Id] -> PmGrd
vanillaConGrd Id
x DataCon
trueDataCon []]
throttle :: Int -> Deltas -> Deltas -> (Precision, Deltas)
throttle :: Int -> Deltas -> Deltas -> (Precision, Deltas)
throttle Int
limit old :: Deltas
old@(MkDeltas Bag Delta
old_ds) new :: Deltas
new@(MkDeltas Bag Delta
new_ds)
| Bag Delta -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Bag Delta
new_ds Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
limit (Bag Delta -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Bag Delta
old_ds) = (Precision
Approximate, Deltas
old)
| Bool
otherwise = (Precision
Precise, Deltas
new)
conMatchForces :: PmAltCon -> Bool
conMatchForces :: PmAltCon -> Bool
conMatchForces (PmAltConLike (RealDataCon DataCon
dc))
| TyCon -> Bool
isNewTyCon (DataCon -> TyCon
dataConTyCon DataCon
dc) = Bool
False
conMatchForces PmAltCon
_ = Bool
True
mayDiverge :: AnnotatedTree -> AnnotatedTree
mayDiverge :: AnnotatedTree -> AnnotatedTree
mayDiverge a :: AnnotatedTree
a@(MayDiverge AnnotatedTree
_) = AnnotatedTree
a
mayDiverge AnnotatedTree
a = AnnotatedTree -> AnnotatedTree
MayDiverge AnnotatedTree
a
checkGrdTree' :: GrdTree -> Deltas -> DsM CheckResult
checkGrdTree' :: GrdTree -> Deltas -> DsM CheckResult
checkGrdTree' (Rhs RhsInfo
sdoc) Deltas
deltas = do
Bool
is_covered <- Deltas -> DsM Bool
isInhabited Deltas
deltas
let clauses :: AnnotatedTree
clauses
| Bool
is_covered = Deltas -> RhsInfo -> AnnotatedTree
AccessibleRhs Deltas
deltas RhsInfo
sdoc
| Bool
otherwise = RhsInfo -> AnnotatedTree
InaccessibleRhs RhsInfo
sdoc
CheckResult -> DsM CheckResult
forall (f :: * -> *) a. Applicative f => a -> f a
pure CheckResult :: AnnotatedTree -> Deltas -> Precision -> CheckResult
CheckResult
{ cr_clauses :: AnnotatedTree
cr_clauses = AnnotatedTree
clauses
, cr_uncov :: Deltas
cr_uncov = Bag Delta -> Deltas
MkDeltas Bag Delta
forall a. Bag a
emptyBag
, cr_approx :: Precision
cr_approx = Precision
Precise }
checkGrdTree' (Guard (PmLet Id
x CoreExpr
e) GrdTree
tree) Deltas
deltas = do
Deltas
deltas' <- Deltas -> PmCt -> DsM Deltas
addPmCtDeltas Deltas
deltas (Id -> CoreExpr -> PmCt
PmCoreCt Id
x CoreExpr
e)
GrdTree -> Deltas -> DsM CheckResult
checkGrdTree' GrdTree
tree Deltas
deltas'
checkGrdTree' (Guard (PmBang Id
x) GrdTree
tree) Deltas
deltas = do
Bool
has_diverged <- Deltas -> PmCt -> DsM Deltas
addPmCtDeltas Deltas
deltas (Id -> PmCt
PmBotCt Id
x) DsM Deltas -> (Deltas -> DsM Bool) -> DsM Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Deltas -> DsM Bool
isInhabited
Deltas
deltas' <- Deltas -> PmCt -> DsM Deltas
addPmCtDeltas Deltas
deltas (Id -> PmCt
PmNotBotCt Id
x)
CheckResult
res <- GrdTree -> Deltas -> DsM CheckResult
checkGrdTree' GrdTree
tree Deltas
deltas'
CheckResult -> DsM CheckResult
forall (f :: * -> *) a. Applicative f => a -> f a
pure CheckResult
res{ cr_clauses :: AnnotatedTree
cr_clauses = Bool
-> (AnnotatedTree -> AnnotatedTree)
-> AnnotatedTree
-> AnnotatedTree
forall a. Bool -> (a -> a) -> a -> a
applyWhen Bool
has_diverged AnnotatedTree -> AnnotatedTree
mayDiverge (CheckResult -> AnnotatedTree
cr_clauses CheckResult
res) }
checkGrdTree' (Guard (PmCon Id
x PmAltCon
con [Id]
tvs [Id]
dicts [Id]
args) GrdTree
tree) Deltas
deltas = do
Bool
has_diverged <-
if PmAltCon -> Bool
conMatchForces PmAltCon
con
then Deltas -> PmCt -> DsM Deltas
addPmCtDeltas Deltas
deltas (Id -> PmCt
PmBotCt Id
x) DsM Deltas -> (Deltas -> DsM Bool) -> DsM Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Deltas -> DsM Bool
isInhabited
else Bool -> DsM Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
Deltas
unc_this <- Deltas -> PmCt -> DsM Deltas
addPmCtDeltas Deltas
deltas (Id -> PmAltCon -> PmCt
PmNotConCt Id
x PmAltCon
con)
Deltas
deltas' <- Deltas -> PmCts -> DsM Deltas
addPmCtsDeltas Deltas
deltas (PmCts -> DsM Deltas) -> PmCts -> DsM Deltas
forall a b. (a -> b) -> a -> b
$
[PmCt] -> PmCts
forall a. [a] -> Bag a
listToBag (Type -> PmCt
PmTyCt (Type -> PmCt) -> (Id -> Type) -> Id -> PmCt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id -> Type
evVarPred (Id -> PmCt) -> [Id] -> [PmCt]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Id]
dicts) PmCts -> PmCt -> PmCts
forall a. Bag a -> a -> Bag a
`snocBag` Id -> PmAltCon -> [Id] -> [Id] -> PmCt
PmConCt Id
x PmAltCon
con [Id]
tvs [Id]
args
CheckResult AnnotatedTree
tree' Deltas
unc_inner Precision
prec <- GrdTree -> Deltas -> DsM CheckResult
checkGrdTree' GrdTree
tree Deltas
deltas'
Int
limit <- DynFlags -> Int
maxPmCheckModels (DynFlags -> Int)
-> IOEnv (Env DsGblEnv DsLclEnv) DynFlags
-> IOEnv (Env DsGblEnv DsLclEnv) Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let (Precision
prec', Deltas
unc') = Int -> Deltas -> Deltas -> (Precision, Deltas)
throttle Int
limit Deltas
deltas (Deltas
unc_this Deltas -> Deltas -> Deltas
forall a. Semigroup a => a -> a -> a
Semi.<> Deltas
unc_inner)
CheckResult -> DsM CheckResult
forall (f :: * -> *) a. Applicative f => a -> f a
pure CheckResult :: AnnotatedTree -> Deltas -> Precision -> CheckResult
CheckResult
{ cr_clauses :: AnnotatedTree
cr_clauses = Bool
-> (AnnotatedTree -> AnnotatedTree)
-> AnnotatedTree
-> AnnotatedTree
forall a. Bool -> (a -> a) -> a -> a
applyWhen Bool
has_diverged AnnotatedTree -> AnnotatedTree
mayDiverge AnnotatedTree
tree'
, cr_uncov :: Deltas
cr_uncov = Deltas
unc'
, cr_approx :: Precision
cr_approx = Precision
prec Precision -> Precision -> Precision
forall a. Semigroup a => a -> a -> a
Semi.<> Precision
prec' }
checkGrdTree' (Sequence GrdTree
l GrdTree
r) Deltas
unc_0 = do
CheckResult AnnotatedTree
l' Deltas
unc_1 Precision
prec_l <- GrdTree -> Deltas -> DsM CheckResult
checkGrdTree' GrdTree
l Deltas
unc_0
CheckResult AnnotatedTree
r' Deltas
unc_2 Precision
prec_r <- GrdTree -> Deltas -> DsM CheckResult
checkGrdTree' GrdTree
r Deltas
unc_1
CheckResult -> DsM CheckResult
forall (f :: * -> *) a. Applicative f => a -> f a
pure CheckResult :: AnnotatedTree -> Deltas -> Precision -> CheckResult
CheckResult
{ cr_clauses :: AnnotatedTree
cr_clauses = AnnotatedTree -> AnnotatedTree -> AnnotatedTree
SequenceAnn AnnotatedTree
l' AnnotatedTree
r'
, cr_uncov :: Deltas
cr_uncov = Deltas
unc_2
, cr_approx :: Precision
cr_approx = Precision
prec_l Precision -> Precision -> Precision
forall a. Semigroup a => a -> a -> a
Semi.<> Precision
prec_r }
checkGrdTree' GrdTree
Empty Deltas
unc = do
CheckResult -> DsM CheckResult
forall (f :: * -> *) a. Applicative f => a -> f a
pure CheckResult :: AnnotatedTree -> Deltas -> Precision -> CheckResult
CheckResult
{ cr_clauses :: AnnotatedTree
cr_clauses = AnnotatedTree
EmptyAnn
, cr_uncov :: Deltas
cr_uncov = Deltas
unc
, cr_approx :: Precision
cr_approx = Precision
Precise }
checkGrdTree :: GrdTree -> Deltas -> DsM CheckResult
checkGrdTree :: GrdTree -> Deltas -> DsM CheckResult
checkGrdTree GrdTree
guards Deltas
deltas = do
String -> SDoc -> DsM ()
tracePm String
"checkGrdTree {" (SDoc -> DsM ()) -> SDoc -> DsM ()
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat [ GrdTree -> SDoc
forall a. Outputable a => a -> SDoc
ppr GrdTree
guards
, Deltas -> SDoc
forall a. Outputable a => a -> SDoc
ppr Deltas
deltas ]
CheckResult
res <- GrdTree -> Deltas -> DsM CheckResult
checkGrdTree' GrdTree
guards Deltas
deltas
String -> SDoc -> DsM ()
tracePm String
"checkGrdTree }:" (CheckResult -> SDoc
forall a. Outputable a => a -> SDoc
ppr CheckResult
res)
CheckResult -> DsM CheckResult
forall (m :: * -> *) a. Monad m => a -> m a
return CheckResult
res
locallyExtendPmDelta :: (Deltas -> DsM Deltas) -> DsM a -> DsM a
locallyExtendPmDelta :: (Deltas -> DsM Deltas) -> DsM a -> DsM a
locallyExtendPmDelta Deltas -> DsM Deltas
ext DsM a
k = do
Deltas
deltas <- DsM Deltas
getPmDeltas
Deltas
deltas' <- DsM Deltas -> DsM Deltas
forall env a. IOEnv env a -> IOEnv env a
unsafeInterleaveM (DsM Deltas -> DsM Deltas) -> DsM Deltas -> DsM Deltas
forall a b. (a -> b) -> a -> b
$ do
Deltas
deltas' <- Deltas -> DsM Deltas
ext Deltas
deltas
Bool
inh <- Deltas -> DsM Bool
isInhabited Deltas
deltas'
if Bool
inh
then Deltas -> DsM Deltas
forall (f :: * -> *) a. Applicative f => a -> f a
pure Deltas
deltas'
else Deltas -> DsM Deltas
forall (f :: * -> *) a. Applicative f => a -> f a
pure Deltas
deltas
Deltas -> DsM a -> DsM a
forall a. Deltas -> DsM a -> DsM a
updPmDeltas Deltas
deltas' DsM a
k
addTyCsDs :: Origin -> Bag EvVar -> DsM a -> DsM a
addTyCsDs :: Origin -> Bag Id -> DsM a -> DsM a
addTyCsDs Origin
origin Bag Id
ev_vars DsM a
m = do
DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
Bool -> (DsM a -> DsM a) -> DsM a -> DsM a
forall a. Bool -> (a -> a) -> a -> a
applyWhen (DynFlags -> Origin -> Bool
needToRunPmCheck DynFlags
dflags Origin
origin)
((Deltas -> DsM Deltas) -> DsM a -> DsM a
forall a. (Deltas -> DsM Deltas) -> DsM a -> DsM a
locallyExtendPmDelta (\Deltas
deltas -> Deltas -> PmCts -> DsM Deltas
addPmCtsDeltas Deltas
deltas (Type -> PmCt
PmTyCt (Type -> PmCt) -> (Id -> Type) -> Id -> PmCt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id -> Type
evVarPred (Id -> PmCt) -> Bag Id -> PmCts
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bag Id
ev_vars)))
DsM a
m
addScrutTmCs :: Maybe (LHsExpr GhcTc) -> [Id] -> DsM a -> DsM a
addScrutTmCs :: Maybe (LHsExpr GhcTc) -> [Id] -> DsM a -> DsM a
addScrutTmCs Maybe (LHsExpr GhcTc)
Nothing [Id]
_ DsM a
k = DsM a
k
addScrutTmCs (Just LHsExpr GhcTc
scr) [Id
x] DsM a
k = do
CoreExpr
scr_e <- LHsExpr GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) CoreExpr
dsLExpr LHsExpr GhcTc
scr
(Deltas -> DsM Deltas) -> DsM a -> DsM a
forall a. (Deltas -> DsM Deltas) -> DsM a -> DsM a
locallyExtendPmDelta (\Deltas
deltas -> Deltas -> PmCts -> DsM Deltas
addPmCtsDeltas Deltas
deltas (PmCt -> PmCts
forall a. a -> Bag a
unitBag (Id -> CoreExpr -> PmCt
PmCoreCt Id
x CoreExpr
scr_e))) DsM a
k
addScrutTmCs Maybe (LHsExpr GhcTc)
_ [Id]
_ DsM a
_ = String -> DsM a
forall a. String -> a
panic String
"addScrutTmCs: HsCase with more than one case binder"
isMatchContextPmChecked :: DynFlags -> Origin -> HsMatchContext id -> Bool
isMatchContextPmChecked :: DynFlags -> Origin -> HsMatchContext id -> Bool
isMatchContextPmChecked DynFlags
dflags Origin
origin HsMatchContext id
kind
| Origin -> Bool
isGenerated Origin
origin
= Bool
False
| Bool
otherwise
= WarningFlag -> DynFlags -> Bool
wopt WarningFlag
Opt_WarnOverlappingPatterns DynFlags
dflags Bool -> Bool -> Bool
|| DynFlags -> HsMatchContext id -> Bool
forall id. DynFlags -> HsMatchContext id -> Bool
exhaustive DynFlags
dflags HsMatchContext id
kind
needToRunPmCheck :: DynFlags -> Origin -> Bool
needToRunPmCheck :: DynFlags -> Origin -> Bool
needToRunPmCheck DynFlags
dflags Origin
origin
| Origin -> Bool
isGenerated Origin
origin
= Bool
False
| Bool
otherwise
= [WarningFlag] -> Bool
forall a. [a] -> Bool
notNull ((WarningFlag -> Bool) -> [WarningFlag] -> [WarningFlag]
forall a. (a -> Bool) -> [a] -> [a]
filter (WarningFlag -> DynFlags -> Bool
`wopt` DynFlags
dflags) [WarningFlag]
allPmCheckWarnings)
redundantAndInaccessibleRhss :: AnnotatedTree -> ([RhsInfo], [RhsInfo])
redundantAndInaccessibleRhss :: AnnotatedTree -> ([RhsInfo], [RhsInfo])
redundantAndInaccessibleRhss AnnotatedTree
tree = (OrdList RhsInfo -> [RhsInfo]
forall a. OrdList a -> [a]
fromOL OrdList RhsInfo
ol_red, OrdList RhsInfo -> [RhsInfo]
forall a. OrdList a -> [a]
fromOL OrdList RhsInfo
ol_inacc)
where
(OrdList RhsInfo
_ol_acc, OrdList RhsInfo
ol_inacc, OrdList RhsInfo
ol_red) = AnnotatedTree
-> (OrdList RhsInfo, OrdList RhsInfo, OrdList RhsInfo)
go AnnotatedTree
tree
go :: AnnotatedTree -> (OrdList RhsInfo, OrdList RhsInfo, OrdList RhsInfo)
go :: AnnotatedTree
-> (OrdList RhsInfo, OrdList RhsInfo, OrdList RhsInfo)
go (AccessibleRhs Deltas
_ RhsInfo
info) = (RhsInfo -> OrdList RhsInfo
forall a. a -> OrdList a
unitOL RhsInfo
info, OrdList RhsInfo
forall a. OrdList a
nilOL, OrdList RhsInfo
forall a. OrdList a
nilOL)
go (InaccessibleRhs RhsInfo
info) = (OrdList RhsInfo
forall a. OrdList a
nilOL, OrdList RhsInfo
forall a. OrdList a
nilOL, RhsInfo -> OrdList RhsInfo
forall a. a -> OrdList a
unitOL RhsInfo
info)
go (MayDiverge AnnotatedTree
t) = case AnnotatedTree
-> (OrdList RhsInfo, OrdList RhsInfo, OrdList RhsInfo)
go AnnotatedTree
t of
(OrdList RhsInfo
acc, OrdList RhsInfo
inacc, OrdList RhsInfo
red)
| OrdList RhsInfo -> Bool
forall a. OrdList a -> Bool
isNilOL OrdList RhsInfo
acc Bool -> Bool -> Bool
&& OrdList RhsInfo -> Bool
forall a. OrdList a -> Bool
isNilOL OrdList RhsInfo
inacc -> (OrdList RhsInfo
forall a. OrdList a
nilOL, OrdList RhsInfo
red, OrdList RhsInfo
forall a. OrdList a
nilOL)
(OrdList RhsInfo, OrdList RhsInfo, OrdList RhsInfo)
res -> (OrdList RhsInfo, OrdList RhsInfo, OrdList RhsInfo)
res
go (SequenceAnn AnnotatedTree
l AnnotatedTree
r) = AnnotatedTree
-> (OrdList RhsInfo, OrdList RhsInfo, OrdList RhsInfo)
go AnnotatedTree
l (OrdList RhsInfo, OrdList RhsInfo, OrdList RhsInfo)
-> (OrdList RhsInfo, OrdList RhsInfo, OrdList RhsInfo)
-> (OrdList RhsInfo, OrdList RhsInfo, OrdList RhsInfo)
forall a. Semigroup a => a -> a -> a
Semi.<> AnnotatedTree
-> (OrdList RhsInfo, OrdList RhsInfo, OrdList RhsInfo)
go AnnotatedTree
r
go AnnotatedTree
EmptyAnn = (OrdList RhsInfo
forall a. OrdList a
nilOL, OrdList RhsInfo
forall a. OrdList a
nilOL, OrdList RhsInfo
forall a. OrdList a
nilOL)
dsPmWarn :: DynFlags -> DsMatchContext -> [Id] -> CheckResult -> DsM ()
dsPmWarn :: DynFlags -> DsMatchContext -> [Id] -> CheckResult -> DsM ()
dsPmWarn DynFlags
dflags ctx :: DsMatchContext
ctx@(DsMatchContext HsMatchContext GhcRn
kind SrcSpan
loc) [Id]
vars CheckResult
result
= Bool -> DsM () -> DsM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
flag_i Bool -> Bool -> Bool
|| Bool
flag_u) (DsM () -> DsM ()) -> DsM () -> DsM ()
forall a b. (a -> b) -> a -> b
$ do
[Delta]
unc_examples <- [Id] -> Int -> Deltas -> DsM [Delta]
getNFirstUncovered [Id]
vars (Int
maxPatterns Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Deltas
uncovered
let exists_r :: Bool
exists_r = Bool
flag_i Bool -> Bool -> Bool
&& [RhsInfo] -> Bool
forall a. [a] -> Bool
notNull [RhsInfo]
redundant
exists_i :: Bool
exists_i = Bool
flag_i Bool -> Bool -> Bool
&& [RhsInfo] -> Bool
forall a. [a] -> Bool
notNull [RhsInfo]
inaccessible
exists_u :: Bool
exists_u = Bool
flag_u Bool -> Bool -> Bool
&& [Delta] -> Bool
forall a. [a] -> Bool
notNull [Delta]
unc_examples
approx :: Bool
approx = Precision
precision Precision -> Precision -> Bool
forall a. Eq a => a -> a -> Bool
== Precision
Approximate
Bool -> DsM () -> DsM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
approx Bool -> Bool -> Bool
&& (Bool
exists_u Bool -> Bool -> Bool
|| Bool
exists_i)) (DsM () -> DsM ()) -> DsM () -> DsM ()
forall a b. (a -> b) -> a -> b
$
SrcSpan -> DsM () -> DsM ()
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (WarnReason -> SDoc -> DsM ()
warnDs WarnReason
NoReason SDoc
approx_msg)
Bool -> DsM () -> DsM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
exists_r (DsM () -> DsM ()) -> DsM () -> DsM ()
forall a b. (a -> b) -> a -> b
$ [RhsInfo] -> (RhsInfo -> DsM ()) -> DsM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [RhsInfo]
redundant ((RhsInfo -> DsM ()) -> DsM ()) -> (RhsInfo -> DsM ()) -> DsM ()
forall a b. (a -> b) -> a -> b
$ \(L SrcSpan
l SDoc
q) -> do
SrcSpan -> DsM () -> DsM ()
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
l (WarnReason -> SDoc -> DsM ()
warnDs (WarningFlag -> WarnReason
Reason WarningFlag
Opt_WarnOverlappingPatterns)
(SDoc -> String -> SDoc
pprEqn SDoc
q String
"is redundant"))
Bool -> DsM () -> DsM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
exists_i (DsM () -> DsM ()) -> DsM () -> DsM ()
forall a b. (a -> b) -> a -> b
$ [RhsInfo] -> (RhsInfo -> DsM ()) -> DsM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [RhsInfo]
inaccessible ((RhsInfo -> DsM ()) -> DsM ()) -> (RhsInfo -> DsM ()) -> DsM ()
forall a b. (a -> b) -> a -> b
$ \(L SrcSpan
l SDoc
q) -> do
SrcSpan -> DsM () -> DsM ()
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
l (WarnReason -> SDoc -> DsM ()
warnDs (WarningFlag -> WarnReason
Reason WarningFlag
Opt_WarnOverlappingPatterns)
(SDoc -> String -> SDoc
pprEqn SDoc
q String
"has inaccessible right hand side"))
Bool -> DsM () -> DsM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
exists_u (DsM () -> DsM ()) -> DsM () -> DsM ()
forall a b. (a -> b) -> a -> b
$ SrcSpan -> DsM () -> DsM ()
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (DsM () -> DsM ()) -> DsM () -> DsM ()
forall a b. (a -> b) -> a -> b
$ WarnReason -> SDoc -> DsM ()
warnDs WarnReason
flag_u_reason (SDoc -> DsM ()) -> SDoc -> DsM ()
forall a b. (a -> b) -> a -> b
$
[Id] -> [Delta] -> SDoc
pprEqns [Id]
vars [Delta]
unc_examples
where
CheckResult
{ cr_clauses :: CheckResult -> AnnotatedTree
cr_clauses = AnnotatedTree
clauses
, cr_uncov :: CheckResult -> Deltas
cr_uncov = Deltas
uncovered
, cr_approx :: CheckResult -> Precision
cr_approx = Precision
precision } = CheckResult
result
([RhsInfo]
redundant, [RhsInfo]
inaccessible) = AnnotatedTree -> ([RhsInfo], [RhsInfo])
redundantAndInaccessibleRhss AnnotatedTree
clauses
flag_i :: Bool
flag_i = DynFlags -> HsMatchContext GhcRn -> Bool
forall id. DynFlags -> HsMatchContext id -> Bool
overlapping DynFlags
dflags HsMatchContext GhcRn
kind
flag_u :: Bool
flag_u = DynFlags -> HsMatchContext GhcRn -> Bool
forall id. DynFlags -> HsMatchContext id -> Bool
exhaustive DynFlags
dflags HsMatchContext GhcRn
kind
flag_u_reason :: WarnReason
flag_u_reason = WarnReason
-> (WarningFlag -> WarnReason) -> Maybe WarningFlag -> WarnReason
forall b a. b -> (a -> b) -> Maybe a -> b
maybe WarnReason
NoReason WarningFlag -> WarnReason
Reason (HsMatchContext GhcRn -> Maybe WarningFlag
forall id. HsMatchContext id -> Maybe WarningFlag
exhaustiveWarningFlag HsMatchContext GhcRn
kind)
maxPatterns :: Int
maxPatterns = DynFlags -> Int
maxUncoveredPatterns DynFlags
dflags
pprEqn :: SDoc -> String -> SDoc
pprEqn SDoc
q String
txt = Bool -> DsMatchContext -> SDoc -> ((SDoc -> SDoc) -> SDoc) -> SDoc
pprContext Bool
True DsMatchContext
ctx (String -> SDoc
text String
txt) (((SDoc -> SDoc) -> SDoc) -> SDoc)
-> ((SDoc -> SDoc) -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \SDoc -> SDoc
f ->
SDoc -> SDoc
f (SDoc
q SDoc -> SDoc -> SDoc
<+> HsMatchContext GhcRn -> SDoc
forall p. HsMatchContext p -> SDoc
matchSeparator HsMatchContext GhcRn
kind SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"...")
pprEqns :: [Id] -> [Delta] -> SDoc
pprEqns [Id]
vars [Delta]
deltas = Bool -> DsMatchContext -> SDoc -> ((SDoc -> SDoc) -> SDoc) -> SDoc
pprContext Bool
False DsMatchContext
ctx (String -> SDoc
text String
"are non-exhaustive") (((SDoc -> SDoc) -> SDoc) -> SDoc)
-> ((SDoc -> SDoc) -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \SDoc -> SDoc
_ ->
case [Id]
vars of
[] -> String -> SDoc
text String
"Guards do not cover entire pattern space"
[Id]
_ -> let us :: [SDoc]
us = (Delta -> SDoc) -> [Delta] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (\Delta
delta -> Delta -> [Id] -> SDoc
pprUncovered Delta
delta [Id]
vars) [Delta]
deltas
in SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"Patterns not matched:") Int
4
([SDoc] -> SDoc
vcat (Int -> [SDoc] -> [SDoc]
forall a. Int -> [a] -> [a]
take Int
maxPatterns [SDoc]
us) SDoc -> SDoc -> SDoc
$$ Int -> [SDoc] -> SDoc
forall a. Int -> [a] -> SDoc
dots Int
maxPatterns [SDoc]
us)
approx_msg :: SDoc
approx_msg = [SDoc] -> SDoc
vcat
[ SDoc -> Int -> SDoc -> SDoc
hang
(String -> SDoc
text String
"Pattern match checker ran into -fmax-pmcheck-models="
SDoc -> SDoc -> SDoc
<> Int -> SDoc
int (DynFlags -> Int
maxPmCheckModels DynFlags
dflags)
SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
" limit, so")
Int
2
( SDoc
bullet SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"Redundant clauses might not be reported at all"
SDoc -> SDoc -> SDoc
$$ SDoc
bullet SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"Redundant clauses might be reported as inaccessible"
SDoc -> SDoc -> SDoc
$$ SDoc
bullet SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"Patterns reported as unmatched might actually be matched")
, String -> SDoc
text String
"Increase the limit or resolve the warnings to suppress this message." ]
getNFirstUncovered :: [Id] -> Int -> Deltas -> DsM [Delta]
getNFirstUncovered :: [Id] -> Int -> Deltas -> DsM [Delta]
getNFirstUncovered [Id]
vars Int
n (MkDeltas Bag Delta
deltas) = Int -> [Delta] -> DsM [Delta]
go Int
n (Bag Delta -> [Delta]
forall a. Bag a -> [a]
bagToList Bag Delta
deltas)
where
go :: Int -> [Delta] -> DsM [Delta]
go Int
0 [Delta]
_ = [Delta] -> DsM [Delta]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
go Int
_ [] = [Delta] -> DsM [Delta]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
go Int
n (Delta
delta:[Delta]
deltas) = do
[Delta]
front <- [Id] -> Int -> Delta -> DsM [Delta]
provideEvidence [Id]
vars Int
n Delta
delta
[Delta]
back <- Int -> [Delta] -> DsM [Delta]
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Delta] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Delta]
front) [Delta]
deltas
[Delta] -> DsM [Delta]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Delta]
front [Delta] -> [Delta] -> [Delta]
forall a. [a] -> [a] -> [a]
++ [Delta]
back)
dots :: Int -> [a] -> SDoc
dots :: Int -> [a] -> SDoc
dots Int
maxPatterns [a]
qs
| [a]
qs [a] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthExceeds` Int
maxPatterns = String -> SDoc
text String
"..."
| Bool
otherwise = SDoc
empty
allPmCheckWarnings :: [WarningFlag]
allPmCheckWarnings :: [WarningFlag]
allPmCheckWarnings =
[ WarningFlag
Opt_WarnIncompletePatterns
, WarningFlag
Opt_WarnIncompleteUniPatterns
, WarningFlag
Opt_WarnIncompletePatternsRecUpd
, WarningFlag
Opt_WarnOverlappingPatterns
]
overlapping :: DynFlags -> HsMatchContext id -> Bool
overlapping :: DynFlags -> HsMatchContext id -> Bool
overlapping DynFlags
_ HsMatchContext id
RecUpd = Bool
False
overlapping DynFlags
dflags HsMatchContext id
_ = WarningFlag -> DynFlags -> Bool
wopt WarningFlag
Opt_WarnOverlappingPatterns DynFlags
dflags
exhaustive :: DynFlags -> HsMatchContext id -> Bool
exhaustive :: DynFlags -> HsMatchContext id -> Bool
exhaustive DynFlags
dflags = Bool -> (WarningFlag -> Bool) -> Maybe WarningFlag -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (WarningFlag -> DynFlags -> Bool
`wopt` DynFlags
dflags) (Maybe WarningFlag -> Bool)
-> (HsMatchContext id -> Maybe WarningFlag)
-> HsMatchContext id
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsMatchContext id -> Maybe WarningFlag
forall id. HsMatchContext id -> Maybe WarningFlag
exhaustiveWarningFlag
exhaustiveWarningFlag :: HsMatchContext id -> Maybe WarningFlag
exhaustiveWarningFlag :: HsMatchContext id -> Maybe WarningFlag
exhaustiveWarningFlag (FunRhs {}) = WarningFlag -> Maybe WarningFlag
forall a. a -> Maybe a
Just WarningFlag
Opt_WarnIncompletePatterns
exhaustiveWarningFlag HsMatchContext id
CaseAlt = WarningFlag -> Maybe WarningFlag
forall a. a -> Maybe a
Just WarningFlag
Opt_WarnIncompletePatterns
exhaustiveWarningFlag HsMatchContext id
IfAlt = WarningFlag -> Maybe WarningFlag
forall a. a -> Maybe a
Just WarningFlag
Opt_WarnIncompletePatterns
exhaustiveWarningFlag HsMatchContext id
LambdaExpr = WarningFlag -> Maybe WarningFlag
forall a. a -> Maybe a
Just WarningFlag
Opt_WarnIncompleteUniPatterns
exhaustiveWarningFlag HsMatchContext id
PatBindRhs = WarningFlag -> Maybe WarningFlag
forall a. a -> Maybe a
Just WarningFlag
Opt_WarnIncompleteUniPatterns
exhaustiveWarningFlag HsMatchContext id
PatBindGuards = WarningFlag -> Maybe WarningFlag
forall a. a -> Maybe a
Just WarningFlag
Opt_WarnIncompletePatterns
exhaustiveWarningFlag HsMatchContext id
ProcExpr = WarningFlag -> Maybe WarningFlag
forall a. a -> Maybe a
Just WarningFlag
Opt_WarnIncompleteUniPatterns
exhaustiveWarningFlag HsMatchContext id
RecUpd = WarningFlag -> Maybe WarningFlag
forall a. a -> Maybe a
Just WarningFlag
Opt_WarnIncompletePatternsRecUpd
exhaustiveWarningFlag HsMatchContext id
ThPatSplice = Maybe WarningFlag
forall a. Maybe a
Nothing
exhaustiveWarningFlag HsMatchContext id
PatSyn = Maybe WarningFlag
forall a. Maybe a
Nothing
exhaustiveWarningFlag HsMatchContext id
ThPatQuote = Maybe WarningFlag
forall a. Maybe a
Nothing
exhaustiveWarningFlag (StmtCtxt {}) = Maybe WarningFlag
forall a. Maybe a
Nothing
pprContext :: Bool -> DsMatchContext -> SDoc -> ((SDoc -> SDoc) -> SDoc) -> SDoc
pprContext :: Bool -> DsMatchContext -> SDoc -> ((SDoc -> SDoc) -> SDoc) -> SDoc
pprContext Bool
singular (DsMatchContext HsMatchContext GhcRn
kind SrcSpan
_loc) SDoc
msg (SDoc -> SDoc) -> SDoc
rest_of_msg_fun
= [SDoc] -> SDoc
vcat [String -> SDoc
text String
txt SDoc -> SDoc -> SDoc
<+> SDoc
msg,
[SDoc] -> SDoc
sep [ String -> SDoc
text String
"In" SDoc -> SDoc -> SDoc
<+> SDoc
ppr_match SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
':'
, Int -> SDoc -> SDoc
nest Int
4 ((SDoc -> SDoc) -> SDoc
rest_of_msg_fun SDoc -> SDoc
pref)]]
where
txt :: String
txt | Bool
singular = String
"Pattern match"
| Bool
otherwise = String
"Pattern match(es)"
(SDoc
ppr_match, SDoc -> SDoc
pref)
= case HsMatchContext GhcRn
kind of
FunRhs { mc_fun :: forall p. HsMatchContext p -> LIdP p
mc_fun = L SrcSpan
_ IdP GhcRn
fun }
-> (HsMatchContext GhcRn -> SDoc
forall p. Outputable (IdP p) => HsMatchContext p -> SDoc
pprMatchContext HsMatchContext GhcRn
kind, \ SDoc
pp -> Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
IdP GhcRn
fun SDoc -> SDoc -> SDoc
<+> SDoc
pp)
HsMatchContext GhcRn
_ -> (HsMatchContext GhcRn -> SDoc
forall p. Outputable (IdP p) => HsMatchContext p -> SDoc
pprMatchContext HsMatchContext GhcRn
kind, \ SDoc
pp -> SDoc
pp)