{-# LANGUAGE CPP #-}
module TcInteract (
solveSimpleGivens,
solveSimpleWanteds,
) where
#include "HsVersions.h"
import GhcPrelude
import BasicTypes ( SwapFlag(..), isSwapped,
infinity, IntWithInf, intGtLimit )
import TcCanonical
import TcFlatten
import TcUnify( canSolveByUnification )
import VarSet
import Type
import InstEnv( DFunInstType )
import CoAxiom( sfInteractTop, sfInteractInert )
import Var
import TcType
import PrelNames ( coercibleTyConKey,
heqTyConKey, eqTyConKey, ipClassKey )
import CoAxiom ( TypeEqn, CoAxiom(..), CoAxBranch(..), fromBranches )
import Class
import TyCon
import FunDeps
import FamInst
import ClsInst( InstanceWhat(..), safeOverlap )
import FamInstEnv
import Unify ( tcUnifyTyWithTFs, ruleMatchTyKiX )
import TcEvidence
import Outputable
import TcRnTypes
import Constraint
import Predicate
import TcOrigin
import TcSMonad
import Bag
import MonadUtils ( concatMapM, foldlM )
import CoreSyn
import Data.List( partition, deleteFirstsBy )
import SrcLoc
import VarEnv
import Control.Monad
import Maybes( isJust )
import Pair (Pair(..))
import Unique( hasKey )
import DynFlags
import Util
import qualified GHC.LanguageExtensions as LangExt
import Control.Monad.Trans.Class
import Control.Monad.Trans.Maybe
solveSimpleGivens :: [Ct] -> TcS ()
solveSimpleGivens :: [Ct] -> TcS ()
solveSimpleGivens [Ct]
givens
| [Ct] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Ct]
givens
= () -> TcS ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise
= do { String -> SDoc -> TcS ()
traceTcS String
"solveSimpleGivens {" ([Ct] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Ct]
givens)
; [Ct] -> TcS ()
go [Ct]
givens
; String -> SDoc -> TcS ()
traceTcS String
"End solveSimpleGivens }" SDoc
empty }
where
go :: [Ct] -> TcS ()
go [Ct]
givens = do { Cts -> TcS ()
solveSimples ([Ct] -> Cts
forall a. [a] -> Bag a
listToBag [Ct]
givens)
; [Ct]
new_givens <- TcS [Ct]
runTcPluginsGiven
; Bool -> TcS () -> TcS ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Ct] -> Bool
forall a. [a] -> Bool
notNull [Ct]
new_givens) (TcS () -> TcS ()) -> TcS () -> TcS ()
forall a b. (a -> b) -> a -> b
$
[Ct] -> TcS ()
go [Ct]
new_givens }
solveSimpleWanteds :: Cts -> TcS WantedConstraints
solveSimpleWanteds :: Cts -> TcS WantedConstraints
solveSimpleWanteds Cts
simples
= do { String -> SDoc -> TcS ()
traceTcS String
"solveSimpleWanteds {" (Cts -> SDoc
forall a. Outputable a => a -> SDoc
ppr Cts
simples)
; DynFlags
dflags <- TcS DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
; (Int
n,WantedConstraints
wc) <- Int
-> IntWithInf -> WantedConstraints -> TcS (Int, WantedConstraints)
go Int
1 (DynFlags -> IntWithInf
solverIterations DynFlags
dflags) (WantedConstraints
emptyWC { wc_simple :: Cts
wc_simple = Cts
simples })
; String -> SDoc -> TcS ()
traceTcS String
"solveSimpleWanteds end }" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text String
"iterations =" SDoc -> SDoc -> SDoc
<+> Int -> SDoc
forall a. Outputable a => a -> SDoc
ppr Int
n
, String -> SDoc
text String
"residual =" SDoc -> SDoc -> SDoc
<+> WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
wc ]
; WantedConstraints -> TcS WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return WantedConstraints
wc }
where
go :: Int -> IntWithInf -> WantedConstraints -> TcS (Int, WantedConstraints)
go :: Int
-> IntWithInf -> WantedConstraints -> TcS (Int, WantedConstraints)
go Int
n IntWithInf
limit WantedConstraints
wc
| Int
n Int -> IntWithInf -> Bool
`intGtLimit` IntWithInf
limit
= SDoc -> TcS (Int, WantedConstraints)
forall a. SDoc -> TcS a
failTcS (SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"solveSimpleWanteds: too many iterations"
SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
parens (String -> SDoc
text String
"limit =" SDoc -> SDoc -> SDoc
<+> IntWithInf -> SDoc
forall a. Outputable a => a -> SDoc
ppr IntWithInf
limit))
Int
2 ([SDoc] -> SDoc
vcat [ String -> SDoc
text String
"Set limit with -fconstraint-solver-iterations=n; n=0 for no limit"
, String -> SDoc
text String
"Simples =" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
forall a. Outputable a => a -> SDoc
ppr Cts
simples
, String -> SDoc
text String
"WC =" SDoc -> SDoc -> SDoc
<+> WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
wc ]))
| Cts -> Bool
forall a. Bag a -> Bool
isEmptyBag (WantedConstraints -> Cts
wc_simple WantedConstraints
wc)
= (Int, WantedConstraints) -> TcS (Int, WantedConstraints)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n,WantedConstraints
wc)
| Bool
otherwise
= do {
(Int
unif_count, WantedConstraints
wc1) <- WantedConstraints -> TcS (Int, WantedConstraints)
solve_simple_wanteds WantedConstraints
wc
; (Bool
rerun_plugin, WantedConstraints
wc2) <- WantedConstraints -> TcS (Bool, WantedConstraints)
runTcPluginsWanted WantedConstraints
wc1
; if Int
unif_count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
rerun_plugin
then (Int, WantedConstraints) -> TcS (Int, WantedConstraints)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n, WantedConstraints
wc2)
else do { String -> SDoc -> TcS ()
traceTcS String
"solveSimple going round again:" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
Int -> SDoc
forall a. Outputable a => a -> SDoc
ppr Int
unif_count SDoc -> SDoc -> SDoc
$$ Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bool
rerun_plugin
; Int
-> IntWithInf -> WantedConstraints -> TcS (Int, WantedConstraints)
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) IntWithInf
limit WantedConstraints
wc2 } }
solve_simple_wanteds :: WantedConstraints -> TcS (Int, WantedConstraints)
solve_simple_wanteds :: WantedConstraints -> TcS (Int, WantedConstraints)
solve_simple_wanteds (WC { wc_simple :: WantedConstraints -> Cts
wc_simple = Cts
simples1, wc_impl :: WantedConstraints -> Bag Implication
wc_impl = Bag Implication
implics1 })
= TcS (Int, WantedConstraints) -> TcS (Int, WantedConstraints)
forall a. TcS a -> TcS a
nestTcS (TcS (Int, WantedConstraints) -> TcS (Int, WantedConstraints))
-> TcS (Int, WantedConstraints) -> TcS (Int, WantedConstraints)
forall a b. (a -> b) -> a -> b
$
do { Cts -> TcS ()
solveSimples Cts
simples1
; (Bag Implication
implics2, Cts
tv_eqs, Cts
fun_eqs, Cts
others) <- TcS (Bag Implication, Cts, Cts, Cts)
getUnsolvedInerts
; (Int
unif_count, Cts
unflattened_eqs) <- TcS Cts -> TcS (Int, Cts)
forall a. TcS a -> TcS (Int, a)
reportUnifications (TcS Cts -> TcS (Int, Cts)) -> TcS Cts -> TcS (Int, Cts)
forall a b. (a -> b) -> a -> b
$
Cts -> Cts -> TcS Cts
unflattenWanteds Cts
tv_eqs Cts
fun_eqs
; (Int, WantedConstraints) -> TcS (Int, WantedConstraints)
forall (m :: * -> *) a. Monad m => a -> m a
return ( Int
unif_count
, WC :: Cts -> Bag Implication -> WantedConstraints
WC { wc_simple :: Cts
wc_simple = Cts
others Cts -> Cts -> Cts
`andCts` Cts
unflattened_eqs
, wc_impl :: Bag Implication
wc_impl = Bag Implication
implics1 Bag Implication -> Bag Implication -> Bag Implication
forall a. Bag a -> Bag a -> Bag a
`unionBags` Bag Implication
implics2 }) }
solveSimples :: Cts -> TcS ()
solveSimples :: Cts -> TcS ()
solveSimples Cts
cts
= {-# SCC "solveSimples" #-}
do { (WorkList -> WorkList) -> TcS ()
updWorkListTcS (\WorkList
wl -> (Ct -> WorkList -> WorkList) -> WorkList -> Cts -> WorkList
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Ct -> WorkList -> WorkList
extendWorkListCt WorkList
wl Cts
cts)
; TcS ()
solve_loop }
where
solve_loop :: TcS ()
solve_loop
= {-# SCC "solve_loop" #-}
do { Maybe Ct
sel <- TcS (Maybe Ct)
selectNextWorkItem
; case Maybe Ct
sel of
Maybe Ct
Nothing -> () -> TcS ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just Ct
ct -> do { [(String, SimplifierStage)] -> Ct -> TcS ()
runSolverPipeline [(String, SimplifierStage)]
thePipeline Ct
ct
; TcS ()
solve_loop } }
runTcPluginsGiven :: TcS [Ct]
runTcPluginsGiven :: TcS [Ct]
runTcPluginsGiven
= do { [TcPluginSolver]
plugins <- TcS [TcPluginSolver]
getTcPlugins
; if [TcPluginSolver] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TcPluginSolver]
plugins then [Ct] -> TcS [Ct]
forall (m :: * -> *) a. Monad m => a -> m a
return [] else
do { [Ct]
givens <- TcS [Ct]
getInertGivens
; if [Ct] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Ct]
givens then [Ct] -> TcS [Ct]
forall (m :: * -> *) a. Monad m => a -> m a
return [] else
do { TcPluginProgress
p <- [TcPluginSolver] -> SplitCts -> TcS TcPluginProgress
runTcPlugins [TcPluginSolver]
plugins ([Ct]
givens,[],[])
; let ([Ct]
solved_givens, [Ct]
_, [(EvTerm, Ct)]
_) = TcPluginProgress -> ([Ct], [Ct], [(EvTerm, Ct)])
pluginSolvedCts TcPluginProgress
p
insols :: [Ct]
insols = TcPluginProgress -> [Ct]
pluginBadCts TcPluginProgress
p
; (InertCans -> InertCans) -> TcS ()
updInertCans ([Ct] -> InertCans -> InertCans
removeInertCts [Ct]
solved_givens)
; (Cts -> Cts) -> TcS ()
updInertIrreds (\Cts
irreds -> Cts -> [Ct] -> Cts
extendCtsList Cts
irreds [Ct]
insols)
; [Ct] -> TcS [Ct]
forall (m :: * -> *) a. Monad m => a -> m a
return (TcPluginProgress -> [Ct]
pluginNewCts TcPluginProgress
p) } } }
runTcPluginsWanted :: WantedConstraints -> TcS (Bool, WantedConstraints)
runTcPluginsWanted :: WantedConstraints -> TcS (Bool, WantedConstraints)
runTcPluginsWanted wc :: WantedConstraints
wc@(WC { wc_simple :: WantedConstraints -> Cts
wc_simple = Cts
simples1, wc_impl :: WantedConstraints -> Bag Implication
wc_impl = Bag Implication
implics1 })
| Cts -> Bool
forall a. Bag a -> Bool
isEmptyBag Cts
simples1
= (Bool, WantedConstraints) -> TcS (Bool, WantedConstraints)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False, WantedConstraints
wc)
| Bool
otherwise
= do { [TcPluginSolver]
plugins <- TcS [TcPluginSolver]
getTcPlugins
; if [TcPluginSolver] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TcPluginSolver]
plugins then (Bool, WantedConstraints) -> TcS (Bool, WantedConstraints)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False, WantedConstraints
wc) else
do { [Ct]
given <- TcS [Ct]
getInertGivens
; Cts
simples1 <- Cts -> TcS Cts
zonkSimples Cts
simples1
; let ([Ct]
wanted, [Ct]
derived) = (Ct -> Bool) -> [Ct] -> ([Ct], [Ct])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition Ct -> Bool
isWantedCt (Cts -> [Ct]
forall a. Bag a -> [a]
bagToList Cts
simples1)
; TcPluginProgress
p <- [TcPluginSolver] -> SplitCts -> TcS TcPluginProgress
runTcPlugins [TcPluginSolver]
plugins ([Ct]
given, [Ct]
derived, [Ct]
wanted)
; let ([Ct]
_, [Ct]
_, [(EvTerm, Ct)]
solved_wanted) = TcPluginProgress -> ([Ct], [Ct], [(EvTerm, Ct)])
pluginSolvedCts TcPluginProgress
p
([Ct]
_, [Ct]
unsolved_derived, [Ct]
unsolved_wanted) = TcPluginProgress -> SplitCts
pluginInputCts TcPluginProgress
p
new_wanted :: [Ct]
new_wanted = TcPluginProgress -> [Ct]
pluginNewCts TcPluginProgress
p
insols :: [Ct]
insols = TcPluginProgress -> [Ct]
pluginBadCts TcPluginProgress
p
; ((EvTerm, Ct) -> TcS ()) -> [(EvTerm, Ct)] -> TcS ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (EvTerm, Ct) -> TcS ()
setEv [(EvTerm, Ct)]
solved_wanted
; (Bool, WantedConstraints) -> TcS (Bool, WantedConstraints)
forall (m :: * -> *) a. Monad m => a -> m a
return ( [Ct] -> Bool
forall a. [a] -> Bool
notNull (TcPluginProgress -> [Ct]
pluginNewCts TcPluginProgress
p)
, WC :: Cts -> Bag Implication -> WantedConstraints
WC { wc_simple :: Cts
wc_simple = [Ct] -> Cts
forall a. [a] -> Bag a
listToBag [Ct]
new_wanted Cts -> Cts -> Cts
`andCts`
[Ct] -> Cts
forall a. [a] -> Bag a
listToBag [Ct]
unsolved_wanted Cts -> Cts -> Cts
`andCts`
[Ct] -> Cts
forall a. [a] -> Bag a
listToBag [Ct]
unsolved_derived Cts -> Cts -> Cts
`andCts`
[Ct] -> Cts
forall a. [a] -> Bag a
listToBag [Ct]
insols
, wc_impl :: Bag Implication
wc_impl = Bag Implication
implics1 } ) } }
where
setEv :: (EvTerm,Ct) -> TcS ()
setEv :: (EvTerm, Ct) -> TcS ()
setEv (EvTerm
ev,Ct
ct) = case Ct -> CtEvidence
ctEvidence Ct
ct of
CtWanted { ctev_dest :: CtEvidence -> TcEvDest
ctev_dest = TcEvDest
dest } -> TcEvDest -> EvTerm -> TcS ()
setWantedEvTerm TcEvDest
dest EvTerm
ev
CtEvidence
_ -> String -> TcS ()
forall a. String -> a
panic String
"runTcPluginsWanted.setEv: attempt to solve non-wanted!"
type SplitCts = ([Ct], [Ct], [Ct])
type SolvedCts = ([Ct], [Ct], [(EvTerm,Ct)])
data TcPluginProgress = TcPluginProgress
{ TcPluginProgress -> SplitCts
pluginInputCts :: SplitCts
, TcPluginProgress -> ([Ct], [Ct], [(EvTerm, Ct)])
pluginSolvedCts :: SolvedCts
, TcPluginProgress -> [Ct]
pluginBadCts :: [Ct]
, TcPluginProgress -> [Ct]
pluginNewCts :: [Ct]
}
getTcPlugins :: TcS [TcPluginSolver]
getTcPlugins :: TcS [TcPluginSolver]
getTcPlugins = do { TcGblEnv
tcg_env <- TcS TcGblEnv
getGblEnv; [TcPluginSolver] -> TcS [TcPluginSolver]
forall (m :: * -> *) a. Monad m => a -> m a
return (TcGblEnv -> [TcPluginSolver]
tcg_tc_plugins TcGblEnv
tcg_env) }
runTcPlugins :: [TcPluginSolver] -> SplitCts -> TcS TcPluginProgress
runTcPlugins :: [TcPluginSolver] -> SplitCts -> TcS TcPluginProgress
runTcPlugins [TcPluginSolver]
plugins SplitCts
all_cts
= (TcPluginProgress -> TcPluginSolver -> TcS TcPluginProgress)
-> TcPluginProgress -> [TcPluginSolver] -> TcS TcPluginProgress
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM TcPluginProgress -> TcPluginSolver -> TcS TcPluginProgress
do_plugin TcPluginProgress
initialProgress [TcPluginSolver]
plugins
where
do_plugin :: TcPluginProgress -> TcPluginSolver -> TcS TcPluginProgress
do_plugin :: TcPluginProgress -> TcPluginSolver -> TcS TcPluginProgress
do_plugin TcPluginProgress
p TcPluginSolver
solver = do
TcPluginResult
result <- TcPluginM TcPluginResult -> TcS TcPluginResult
forall a. TcPluginM a -> TcS a
runTcPluginTcS (TcPluginSolver -> SplitCts -> TcPluginM TcPluginResult
forall a b c d. (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 TcPluginSolver
solver (TcPluginProgress -> SplitCts
pluginInputCts TcPluginProgress
p))
TcPluginProgress -> TcS TcPluginProgress
forall (m :: * -> *) a. Monad m => a -> m a
return (TcPluginProgress -> TcS TcPluginProgress)
-> TcPluginProgress -> TcS TcPluginProgress
forall a b. (a -> b) -> a -> b
$ TcPluginProgress -> TcPluginResult -> TcPluginProgress
progress TcPluginProgress
p TcPluginResult
result
progress :: TcPluginProgress -> TcPluginResult -> TcPluginProgress
progress :: TcPluginProgress -> TcPluginResult -> TcPluginProgress
progress TcPluginProgress
p (TcPluginContradiction [Ct]
bad_cts) =
TcPluginProgress
p { pluginInputCts :: SplitCts
pluginInputCts = [Ct] -> SplitCts -> SplitCts
discard [Ct]
bad_cts (TcPluginProgress -> SplitCts
pluginInputCts TcPluginProgress
p)
, pluginBadCts :: [Ct]
pluginBadCts = [Ct]
bad_cts [Ct] -> [Ct] -> [Ct]
forall a. [a] -> [a] -> [a]
++ TcPluginProgress -> [Ct]
pluginBadCts TcPluginProgress
p
}
progress TcPluginProgress
p (TcPluginOk [(EvTerm, Ct)]
solved_cts [Ct]
new_cts) =
TcPluginProgress
p { pluginInputCts :: SplitCts
pluginInputCts = [Ct] -> SplitCts -> SplitCts
discard (((EvTerm, Ct) -> Ct) -> [(EvTerm, Ct)] -> [Ct]
forall a b. (a -> b) -> [a] -> [b]
map (EvTerm, Ct) -> Ct
forall a b. (a, b) -> b
snd [(EvTerm, Ct)]
solved_cts) (TcPluginProgress -> SplitCts
pluginInputCts TcPluginProgress
p)
, pluginSolvedCts :: ([Ct], [Ct], [(EvTerm, Ct)])
pluginSolvedCts = [(EvTerm, Ct)]
-> ([Ct], [Ct], [(EvTerm, Ct)]) -> ([Ct], [Ct], [(EvTerm, Ct)])
add [(EvTerm, Ct)]
solved_cts (TcPluginProgress -> ([Ct], [Ct], [(EvTerm, Ct)])
pluginSolvedCts TcPluginProgress
p)
, pluginNewCts :: [Ct]
pluginNewCts = [Ct]
new_cts [Ct] -> [Ct] -> [Ct]
forall a. [a] -> [a] -> [a]
++ TcPluginProgress -> [Ct]
pluginNewCts TcPluginProgress
p
}
initialProgress :: TcPluginProgress
initialProgress = SplitCts
-> ([Ct], [Ct], [(EvTerm, Ct)]) -> [Ct] -> [Ct] -> TcPluginProgress
TcPluginProgress SplitCts
all_cts ([], [], []) [] []
discard :: [Ct] -> SplitCts -> SplitCts
discard :: [Ct] -> SplitCts -> SplitCts
discard [Ct]
cts ([Ct]
xs, [Ct]
ys, [Ct]
zs) =
([Ct]
xs [Ct] -> [Ct] -> [Ct]
`without` [Ct]
cts, [Ct]
ys [Ct] -> [Ct] -> [Ct]
`without` [Ct]
cts, [Ct]
zs [Ct] -> [Ct] -> [Ct]
`without` [Ct]
cts)
without :: [Ct] -> [Ct] -> [Ct]
without :: [Ct] -> [Ct] -> [Ct]
without = (Ct -> Ct -> Bool) -> [Ct] -> [Ct] -> [Ct]
forall a. (a -> a -> Bool) -> [a] -> [a] -> [a]
deleteFirstsBy Ct -> Ct -> Bool
eqCt
eqCt :: Ct -> Ct -> Bool
eqCt :: Ct -> Ct -> Bool
eqCt Ct
c Ct
c' = Ct -> CtFlavour
ctFlavour Ct
c CtFlavour -> CtFlavour -> Bool
forall a. Eq a => a -> a -> Bool
== Ct -> CtFlavour
ctFlavour Ct
c'
Bool -> Bool -> Bool
&& Ct -> PredType
ctPred Ct
c HasDebugCallStack => PredType -> PredType -> Bool
PredType -> PredType -> Bool
`tcEqType` Ct -> PredType
ctPred Ct
c'
add :: [(EvTerm,Ct)] -> SolvedCts -> SolvedCts
add :: [(EvTerm, Ct)]
-> ([Ct], [Ct], [(EvTerm, Ct)]) -> ([Ct], [Ct], [(EvTerm, Ct)])
add [(EvTerm, Ct)]
xs ([Ct], [Ct], [(EvTerm, Ct)])
scs = (([Ct], [Ct], [(EvTerm, Ct)])
-> (EvTerm, Ct) -> ([Ct], [Ct], [(EvTerm, Ct)]))
-> ([Ct], [Ct], [(EvTerm, Ct)])
-> [(EvTerm, Ct)]
-> ([Ct], [Ct], [(EvTerm, Ct)])
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ([Ct], [Ct], [(EvTerm, Ct)])
-> (EvTerm, Ct) -> ([Ct], [Ct], [(EvTerm, Ct)])
addOne ([Ct], [Ct], [(EvTerm, Ct)])
scs [(EvTerm, Ct)]
xs
addOne :: SolvedCts -> (EvTerm,Ct) -> SolvedCts
addOne :: ([Ct], [Ct], [(EvTerm, Ct)])
-> (EvTerm, Ct) -> ([Ct], [Ct], [(EvTerm, Ct)])
addOne ([Ct]
givens, [Ct]
deriveds, [(EvTerm, Ct)]
wanteds) (EvTerm
ev,Ct
ct) = case Ct -> CtEvidence
ctEvidence Ct
ct of
CtGiven {} -> (Ct
ctCt -> [Ct] -> [Ct]
forall a. a -> [a] -> [a]
:[Ct]
givens, [Ct]
deriveds, [(EvTerm, Ct)]
wanteds)
CtDerived{} -> ([Ct]
givens, Ct
ctCt -> [Ct] -> [Ct]
forall a. a -> [a] -> [a]
:[Ct]
deriveds, [(EvTerm, Ct)]
wanteds)
CtWanted {} -> ([Ct]
givens, [Ct]
deriveds, (EvTerm
ev,Ct
ct)(EvTerm, Ct) -> [(EvTerm, Ct)] -> [(EvTerm, Ct)]
forall a. a -> [a] -> [a]
:[(EvTerm, Ct)]
wanteds)
type WorkItem = Ct
type SimplifierStage = WorkItem -> TcS (StopOrContinue Ct)
runSolverPipeline :: [(String,SimplifierStage)]
-> WorkItem
-> TcS ()
runSolverPipeline :: [(String, SimplifierStage)] -> Ct -> TcS ()
runSolverPipeline [(String, SimplifierStage)]
pipeline Ct
workItem
= do { WorkList
wl <- TcS WorkList
getWorkList
; InertSet
inerts <- TcS InertSet
getTcSInerts
; TcLevel
tclevel <- TcS TcLevel
getTcLevel
; String -> SDoc -> TcS ()
traceTcS String
"----------------------------- " SDoc
empty
; String -> SDoc -> TcS ()
traceTcS String
"Start solver pipeline {" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text String
"tclevel =" SDoc -> SDoc -> SDoc
<+> TcLevel -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcLevel
tclevel
, String -> SDoc
text String
"work item =" SDoc -> SDoc -> SDoc
<+> Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
workItem
, String -> SDoc
text String
"inerts =" SDoc -> SDoc -> SDoc
<+> InertSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr InertSet
inerts
, String -> SDoc
text String
"rest of worklist =" SDoc -> SDoc -> SDoc
<+> WorkList -> SDoc
forall a. Outputable a => a -> SDoc
ppr WorkList
wl ]
; TcS ()
bumpStepCountTcS
; StopOrContinue Ct
final_res <- [(String, SimplifierStage)]
-> StopOrContinue Ct -> TcS (StopOrContinue Ct)
run_pipeline [(String, SimplifierStage)]
pipeline (Ct -> StopOrContinue Ct
forall a. a -> StopOrContinue a
ContinueWith Ct
workItem)
; case StopOrContinue Ct
final_res of
Stop CtEvidence
ev SDoc
s -> do { CtEvidence -> SDoc -> TcS ()
traceFireTcS CtEvidence
ev SDoc
s
; String -> SDoc -> TcS ()
traceTcS String
"End solver pipeline (discharged) }" SDoc
empty
; () -> TcS ()
forall (m :: * -> *) a. Monad m => a -> m a
return () }
ContinueWith Ct
ct -> do { Ct -> TcS ()
addInertCan Ct
ct
; CtEvidence -> SDoc -> TcS ()
traceFireTcS (Ct -> CtEvidence
ctEvidence Ct
ct) (String -> SDoc
text String
"Kept as inert")
; String -> SDoc -> TcS ()
traceTcS String
"End solver pipeline (kept as inert) }" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
(String -> SDoc
text String
"final_item =" SDoc -> SDoc -> SDoc
<+> Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct) }
}
where run_pipeline :: [(String,SimplifierStage)] -> StopOrContinue Ct
-> TcS (StopOrContinue Ct)
run_pipeline :: [(String, SimplifierStage)]
-> StopOrContinue Ct -> TcS (StopOrContinue Ct)
run_pipeline [] StopOrContinue Ct
res = StopOrContinue Ct -> TcS (StopOrContinue Ct)
forall (m :: * -> *) a. Monad m => a -> m a
return StopOrContinue Ct
res
run_pipeline [(String, SimplifierStage)]
_ (Stop CtEvidence
ev SDoc
s) = StopOrContinue Ct -> TcS (StopOrContinue Ct)
forall (m :: * -> *) a. Monad m => a -> m a
return (CtEvidence -> SDoc -> StopOrContinue Ct
forall a. CtEvidence -> SDoc -> StopOrContinue a
Stop CtEvidence
ev SDoc
s)
run_pipeline ((String
stg_name,SimplifierStage
stg):[(String, SimplifierStage)]
stgs) (ContinueWith Ct
ct)
= do { String -> SDoc -> TcS ()
traceTcS (String
"runStage " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
stg_name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" {")
(String -> SDoc
text String
"workitem = " SDoc -> SDoc -> SDoc
<+> Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)
; StopOrContinue Ct
res <- SimplifierStage
stg Ct
ct
; String -> SDoc -> TcS ()
traceTcS (String
"end stage " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
stg_name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" }") SDoc
empty
; [(String, SimplifierStage)]
-> StopOrContinue Ct -> TcS (StopOrContinue Ct)
run_pipeline [(String, SimplifierStage)]
stgs StopOrContinue Ct
res }
thePipeline :: [(String,SimplifierStage)]
thePipeline :: [(String, SimplifierStage)]
thePipeline = [ (String
"canonicalization", SimplifierStage
TcCanonical.canonicalize)
, (String
"interact with inerts", SimplifierStage
interactWithInertsStage)
, (String
"top-level reactions", SimplifierStage
topReactionsStage) ]
interactWithInertsStage :: WorkItem -> TcS (StopOrContinue Ct)
interactWithInertsStage :: SimplifierStage
interactWithInertsStage Ct
wi
= do { InertSet
inerts <- TcS InertSet
getTcSInerts
; let ics :: InertCans
ics = InertSet -> InertCans
inert_cans InertSet
inerts
; case Ct
wi of
CTyEqCan {} -> InertCans -> SimplifierStage
interactTyVarEq InertCans
ics Ct
wi
CFunEqCan {} -> InertCans -> SimplifierStage
interactFunEq InertCans
ics Ct
wi
CIrredCan {} -> InertCans -> SimplifierStage
interactIrred InertCans
ics Ct
wi
CDictCan {} -> InertCans -> SimplifierStage
interactDict InertCans
ics Ct
wi
Ct
_ -> String -> SDoc -> TcS (StopOrContinue Ct)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"interactWithInerts" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
wi) }
data InteractResult
= KeepInert
| KeepWork
instance Outputable InteractResult where
ppr :: InteractResult -> SDoc
ppr InteractResult
KeepInert = String -> SDoc
text String
"keep inert"
ppr InteractResult
KeepWork = String -> SDoc
text String
"keep work-item"
solveOneFromTheOther :: CtEvidence
-> CtEvidence
-> TcS InteractResult
solveOneFromTheOther :: CtEvidence -> CtEvidence -> TcS InteractResult
solveOneFromTheOther CtEvidence
ev_i CtEvidence
ev_w
| CtEvidence -> Bool
isDerived CtEvidence
ev_w
= InteractResult -> TcS InteractResult
forall (m :: * -> *) a. Monad m => a -> m a
return InteractResult
KeepInert
| CtEvidence -> Bool
isDerived CtEvidence
ev_i
= InteractResult -> TcS InteractResult
forall (m :: * -> *) a. Monad m => a -> m a
return InteractResult
KeepWork
| CtWanted { ctev_loc :: CtEvidence -> CtLoc
ctev_loc = CtLoc
loc_w } <- CtEvidence
ev_w
, CtLoc -> CtLoc -> Bool
prohibitedSuperClassSolve (CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev_i) CtLoc
loc_w
=
do { String -> SDoc -> TcS ()
traceTcS String
"prohibitedClassSolve1" (CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
ev_i SDoc -> SDoc -> SDoc
$$ CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
ev_w)
; InteractResult -> TcS InteractResult
forall (m :: * -> *) a. Monad m => a -> m a
return InteractResult
KeepWork }
| CtWanted {} <- CtEvidence
ev_w
= InteractResult -> TcS InteractResult
forall (m :: * -> *) a. Monad m => a -> m a
return InteractResult
KeepInert
| CtWanted { ctev_loc :: CtEvidence -> CtLoc
ctev_loc = CtLoc
loc_i } <- CtEvidence
ev_i
, CtLoc -> CtLoc -> Bool
prohibitedSuperClassSolve (CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev_w) CtLoc
loc_i
= do { String -> SDoc -> TcS ()
traceTcS String
"prohibitedClassSolve2" (CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
ev_i SDoc -> SDoc -> SDoc
$$ CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
ev_w)
; InteractResult -> TcS InteractResult
forall (m :: * -> *) a. Monad m => a -> m a
return InteractResult
KeepInert }
| CtWanted {} <- CtEvidence
ev_i
= InteractResult -> TcS InteractResult
forall (m :: * -> *) a. Monad m => a -> m a
return InteractResult
KeepWork
| TcLevel
lvl_i TcLevel -> TcLevel -> Bool
forall a. Eq a => a -> a -> Bool
== TcLevel
lvl_w
= do { EvBindsVar
ev_binds_var <- TcS EvBindsVar
getTcEvBindsVar
; EvBindMap
binds <- EvBindsVar -> TcS EvBindMap
getTcEvBindsMap EvBindsVar
ev_binds_var
; InteractResult -> TcS InteractResult
forall (m :: * -> *) a. Monad m => a -> m a
return (EvBindMap -> InteractResult
same_level_strategy EvBindMap
binds) }
| Bool
otherwise
= InteractResult -> TcS InteractResult
forall (m :: * -> *) a. Monad m => a -> m a
return InteractResult
different_level_strategy
where
pred :: PredType
pred = CtEvidence -> PredType
ctEvPred CtEvidence
ev_i
loc_i :: CtLoc
loc_i = CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev_i
loc_w :: CtLoc
loc_w = CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev_w
lvl_i :: TcLevel
lvl_i = CtLoc -> TcLevel
ctLocLevel CtLoc
loc_i
lvl_w :: TcLevel
lvl_w = CtLoc -> TcLevel
ctLocLevel CtLoc
loc_w
ev_id_i :: EvVar
ev_id_i = CtEvidence -> EvVar
ctEvEvId CtEvidence
ev_i
ev_id_w :: EvVar
ev_id_w = CtEvidence -> EvVar
ctEvEvId CtEvidence
ev_w
different_level_strategy :: InteractResult
different_level_strategy
| PredType -> Bool
isIPPred PredType
pred = if TcLevel
lvl_w TcLevel -> TcLevel -> Bool
forall a. Ord a => a -> a -> Bool
> TcLevel
lvl_i then InteractResult
KeepWork else InteractResult
KeepInert
| Bool
otherwise = if TcLevel
lvl_w TcLevel -> TcLevel -> Bool
forall a. Ord a => a -> a -> Bool
> TcLevel
lvl_i then InteractResult
KeepInert else InteractResult
KeepWork
same_level_strategy :: EvBindMap -> InteractResult
same_level_strategy EvBindMap
binds
| GivenOrigin (InstSC IntWithInf
s_i) <- CtLoc -> CtOrigin
ctLocOrigin CtLoc
loc_i
= case CtLoc -> CtOrigin
ctLocOrigin CtLoc
loc_w of
GivenOrigin (InstSC IntWithInf
s_w) | IntWithInf
s_w IntWithInf -> IntWithInf -> Bool
forall a. Ord a => a -> a -> Bool
< IntWithInf
s_i -> InteractResult
KeepWork
| Bool
otherwise -> InteractResult
KeepInert
CtOrigin
_ -> InteractResult
KeepWork
| GivenOrigin (InstSC {}) <- CtLoc -> CtOrigin
ctLocOrigin CtLoc
loc_w
= InteractResult
KeepInert
| EvBindMap -> EvVar -> Bool
has_binding EvBindMap
binds EvVar
ev_id_w
, Bool -> Bool
not (EvBindMap -> EvVar -> Bool
has_binding EvBindMap
binds EvVar
ev_id_i)
, Bool -> Bool
not (EvVar
ev_id_i EvVar -> VarSet -> Bool
`elemVarSet` EvBindMap -> VarSet -> VarSet
findNeededEvVars EvBindMap
binds (EvVar -> VarSet
unitVarSet EvVar
ev_id_w))
= InteractResult
KeepWork
| Bool
otherwise
= InteractResult
KeepInert
has_binding :: EvBindMap -> EvVar -> Bool
has_binding EvBindMap
binds EvVar
ev_id = Maybe EvBind -> Bool
forall a. Maybe a -> Bool
isJust (EvBindMap -> EvVar -> Maybe EvBind
lookupEvBind EvBindMap
binds EvVar
ev_id)
interactIrred :: InertCans -> Ct -> TcS (StopOrContinue Ct)
interactIrred :: InertCans -> SimplifierStage
interactIrred InertCans
inerts workItem :: Ct
workItem@(CIrredCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev_w, cc_insol :: Ct -> Bool
cc_insol = Bool
insoluble })
| Bool
insoluble
, Bool -> Bool
not (Ct -> Bool
isDroppableCt Ct
workItem)
= SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
workItem
| let (Bag (Ct, SwapFlag)
matching_irreds, Cts
others) = Cts -> CtEvidence -> (Bag (Ct, SwapFlag), Cts)
findMatchingIrreds (InertCans -> Cts
inert_irreds InertCans
inerts) CtEvidence
ev_w
, ((Ct
ct_i, SwapFlag
swap) : [(Ct, SwapFlag)]
_rest) <- Bag (Ct, SwapFlag) -> [(Ct, SwapFlag)]
forall a. Bag a -> [a]
bagToList Bag (Ct, SwapFlag)
matching_irreds
, let ev_i :: CtEvidence
ev_i = Ct -> CtEvidence
ctEvidence Ct
ct_i
= do { InteractResult
what_next <- CtEvidence -> CtEvidence -> TcS InteractResult
solveOneFromTheOther CtEvidence
ev_i CtEvidence
ev_w
; String -> SDoc -> TcS ()
traceTcS String
"iteractIrred" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
workItem SDoc -> SDoc -> SDoc
$$ InteractResult -> SDoc
forall a. Outputable a => a -> SDoc
ppr InteractResult
what_next SDoc -> SDoc -> SDoc
$$ Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct_i)
; case InteractResult
what_next of
InteractResult
KeepInert -> do { CtEvidence -> EvTerm -> TcS ()
setEvBindIfWanted CtEvidence
ev_w (SwapFlag -> CtEvidence -> EvTerm
swap_me SwapFlag
swap CtEvidence
ev_i)
; StopOrContinue Ct -> TcS (StopOrContinue Ct)
forall (m :: * -> *) a. Monad m => a -> m a
return (CtEvidence -> SDoc -> StopOrContinue Ct
forall a. CtEvidence -> SDoc -> StopOrContinue a
Stop CtEvidence
ev_w (String -> SDoc
text String
"Irred equal" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
parens (InteractResult -> SDoc
forall a. Outputable a => a -> SDoc
ppr InteractResult
what_next))) }
InteractResult
KeepWork -> do { CtEvidence -> EvTerm -> TcS ()
setEvBindIfWanted CtEvidence
ev_i (SwapFlag -> CtEvidence -> EvTerm
swap_me SwapFlag
swap CtEvidence
ev_w)
; (Cts -> Cts) -> TcS ()
updInertIrreds (\Cts
_ -> Cts
others)
; SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
workItem } }
| Bool
otherwise
= SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
workItem
where
swap_me :: SwapFlag -> CtEvidence -> EvTerm
swap_me :: SwapFlag -> CtEvidence -> EvTerm
swap_me SwapFlag
swap CtEvidence
ev
= case SwapFlag
swap of
SwapFlag
NotSwapped -> CtEvidence -> EvTerm
ctEvTerm CtEvidence
ev
SwapFlag
IsSwapped -> TcCoercion -> EvTerm
evCoercion (TcCoercion -> TcCoercion
mkTcSymCo (EvTerm -> TcCoercion
evTermCoercion (CtEvidence -> EvTerm
ctEvTerm CtEvidence
ev)))
interactIrred InertCans
_ Ct
wi = String -> SDoc -> TcS (StopOrContinue Ct)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"interactIrred" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
wi)
findMatchingIrreds :: Cts -> CtEvidence -> (Bag (Ct, SwapFlag), Bag Ct)
findMatchingIrreds :: Cts -> CtEvidence -> (Bag (Ct, SwapFlag), Cts)
findMatchingIrreds Cts
irreds CtEvidence
ev
| EqPred EqRel
eq_rel1 PredType
lty1 PredType
rty1 <- PredType -> Pred
classifyPredType PredType
pred
= (Ct -> Either (Ct, SwapFlag) Ct)
-> Cts -> (Bag (Ct, SwapFlag), Cts)
forall a b c. (a -> Either b c) -> Bag a -> (Bag b, Bag c)
partitionBagWith (EqRel -> PredType -> PredType -> Ct -> Either (Ct, SwapFlag) Ct
match_eq EqRel
eq_rel1 PredType
lty1 PredType
rty1) Cts
irreds
| Bool
otherwise
= (Ct -> Either (Ct, SwapFlag) Ct)
-> Cts -> (Bag (Ct, SwapFlag), Cts)
forall a b c. (a -> Either b c) -> Bag a -> (Bag b, Bag c)
partitionBagWith Ct -> Either (Ct, SwapFlag) Ct
match_non_eq Cts
irreds
where
pred :: PredType
pred = CtEvidence -> PredType
ctEvPred CtEvidence
ev
match_non_eq :: Ct -> Either (Ct, SwapFlag) Ct
match_non_eq Ct
ct
| Ct -> PredType
ctPred Ct
ct PredType -> PredType -> Bool
`tcEqTypeNoKindCheck` PredType
pred = (Ct, SwapFlag) -> Either (Ct, SwapFlag) Ct
forall a b. a -> Either a b
Left (Ct
ct, SwapFlag
NotSwapped)
| Bool
otherwise = Ct -> Either (Ct, SwapFlag) Ct
forall a b. b -> Either a b
Right Ct
ct
match_eq :: EqRel -> PredType -> PredType -> Ct -> Either (Ct, SwapFlag) Ct
match_eq EqRel
eq_rel1 PredType
lty1 PredType
rty1 Ct
ct
| EqPred EqRel
eq_rel2 PredType
lty2 PredType
rty2 <- PredType -> Pred
classifyPredType (Ct -> PredType
ctPred Ct
ct)
, EqRel
eq_rel1 EqRel -> EqRel -> Bool
forall a. Eq a => a -> a -> Bool
== EqRel
eq_rel2
, Just SwapFlag
swap <- PredType -> PredType -> PredType -> PredType -> Maybe SwapFlag
match_eq_help PredType
lty1 PredType
rty1 PredType
lty2 PredType
rty2
= (Ct, SwapFlag) -> Either (Ct, SwapFlag) Ct
forall a b. a -> Either a b
Left (Ct
ct, SwapFlag
swap)
| Bool
otherwise
= Ct -> Either (Ct, SwapFlag) Ct
forall a b. b -> Either a b
Right Ct
ct
match_eq_help :: PredType -> PredType -> PredType -> PredType -> Maybe SwapFlag
match_eq_help PredType
lty1 PredType
rty1 PredType
lty2 PredType
rty2
| PredType
lty1 PredType -> PredType -> Bool
`tcEqTypeNoKindCheck` PredType
lty2, PredType
rty1 PredType -> PredType -> Bool
`tcEqTypeNoKindCheck` PredType
rty2
= SwapFlag -> Maybe SwapFlag
forall a. a -> Maybe a
Just SwapFlag
NotSwapped
| PredType
lty1 PredType -> PredType -> Bool
`tcEqTypeNoKindCheck` PredType
rty2, PredType
rty1 PredType -> PredType -> Bool
`tcEqTypeNoKindCheck` PredType
lty2
= SwapFlag -> Maybe SwapFlag
forall a. a -> Maybe a
Just SwapFlag
IsSwapped
| Bool
otherwise
= Maybe SwapFlag
forall a. Maybe a
Nothing
interactDict :: InertCans -> Ct -> TcS (StopOrContinue Ct)
interactDict :: InertCans -> SimplifierStage
interactDict InertCans
inerts workItem :: Ct
workItem@(CDictCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev_w, cc_class :: Ct -> Class
cc_class = Class
cls, cc_tyargs :: Ct -> [PredType]
cc_tyargs = [PredType]
tys })
| Just CtEvidence
ev_i <- InertCans -> CtLoc -> Class -> [PredType] -> Maybe CtEvidence
lookupInertDict InertCans
inerts (CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev_w) Class
cls [PredType]
tys
=
do {
DynFlags
dflags <- TcS DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
; Bool
short_cut_worked <- DynFlags -> CtEvidence -> CtEvidence -> TcS Bool
shortCutSolver DynFlags
dflags CtEvidence
ev_w CtEvidence
ev_i
; if Bool
short_cut_worked
then CtEvidence -> String -> TcS (StopOrContinue Ct)
forall a. CtEvidence -> String -> TcS (StopOrContinue a)
stopWith CtEvidence
ev_w String
"interactDict/solved from instance"
else
do {
InteractResult
what_next <- CtEvidence -> CtEvidence -> TcS InteractResult
solveOneFromTheOther CtEvidence
ev_i CtEvidence
ev_w
; String -> SDoc -> TcS ()
traceTcS String
"lookupInertDict" (InteractResult -> SDoc
forall a. Outputable a => a -> SDoc
ppr InteractResult
what_next)
; case InteractResult
what_next of
InteractResult
KeepInert -> do { CtEvidence -> EvTerm -> TcS ()
setEvBindIfWanted CtEvidence
ev_w (CtEvidence -> EvTerm
ctEvTerm CtEvidence
ev_i)
; StopOrContinue Ct -> TcS (StopOrContinue Ct)
forall (m :: * -> *) a. Monad m => a -> m a
return (StopOrContinue Ct -> TcS (StopOrContinue Ct))
-> StopOrContinue Ct -> TcS (StopOrContinue Ct)
forall a b. (a -> b) -> a -> b
$ CtEvidence -> SDoc -> StopOrContinue Ct
forall a. CtEvidence -> SDoc -> StopOrContinue a
Stop CtEvidence
ev_w (String -> SDoc
text String
"Dict equal" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
parens (InteractResult -> SDoc
forall a. Outputable a => a -> SDoc
ppr InteractResult
what_next)) }
InteractResult
KeepWork -> do { CtEvidence -> EvTerm -> TcS ()
setEvBindIfWanted CtEvidence
ev_i (CtEvidence -> EvTerm
ctEvTerm CtEvidence
ev_w)
; (DictMap Ct -> DictMap Ct) -> TcS ()
updInertDicts ((DictMap Ct -> DictMap Ct) -> TcS ())
-> (DictMap Ct -> DictMap Ct) -> TcS ()
forall a b. (a -> b) -> a -> b
$ \ DictMap Ct
ds -> DictMap Ct -> Class -> [PredType] -> DictMap Ct
forall a. DictMap a -> Class -> [PredType] -> DictMap a
delDict DictMap Ct
ds Class
cls [PredType]
tys
; SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
workItem } } }
| Class
cls Class -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
ipClassKey
, CtEvidence -> Bool
isGiven CtEvidence
ev_w
= InertCans -> SimplifierStage
interactGivenIP InertCans
inerts Ct
workItem
| Bool
otherwise
= do { InertCans -> CtEvidence -> Class -> TcS ()
addFunDepWork InertCans
inerts CtEvidence
ev_w Class
cls
; SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
workItem }
interactDict InertCans
_ Ct
wi = String -> SDoc -> TcS (StopOrContinue Ct)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"interactDict" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
wi)
shortCutSolver :: DynFlags
-> CtEvidence
-> CtEvidence
-> TcS Bool
shortCutSolver :: DynFlags -> CtEvidence -> CtEvidence -> TcS Bool
shortCutSolver DynFlags
dflags CtEvidence
ev_w CtEvidence
ev_i
| CtEvidence -> Bool
isWanted CtEvidence
ev_w
Bool -> Bool -> Bool
&& CtEvidence -> Bool
isGiven CtEvidence
ev_i
Bool -> Bool -> Bool
&& Bool -> Bool
not (Extension -> DynFlags -> Bool
xopt Extension
LangExt.IncoherentInstances DynFlags
dflags)
Bool -> Bool -> Bool
&& GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_SolveConstantDicts DynFlags
dflags
= do { EvBindsVar
ev_binds_var <- TcS EvBindsVar
getTcEvBindsVar
; EvBindMap
ev_binds <- ASSERT2( not (isCoEvBindsVar ev_binds_var ), ppr ev_w )
EvBindsVar -> TcS EvBindMap
getTcEvBindsMap EvBindsVar
ev_binds_var
; DictMap CtEvidence
solved_dicts <- TcS (DictMap CtEvidence)
getSolvedDicts
; Maybe (EvBindMap, DictMap CtEvidence)
mb_stuff <- MaybeT TcS (EvBindMap, DictMap CtEvidence)
-> TcS (Maybe (EvBindMap, DictMap CtEvidence))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT TcS (EvBindMap, DictMap CtEvidence)
-> TcS (Maybe (EvBindMap, DictMap CtEvidence)))
-> MaybeT TcS (EvBindMap, DictMap CtEvidence)
-> TcS (Maybe (EvBindMap, DictMap CtEvidence))
forall a b. (a -> b) -> a -> b
$ (EvBindMap, DictMap CtEvidence)
-> CtEvidence -> MaybeT TcS (EvBindMap, DictMap CtEvidence)
try_solve_from_instance
(EvBindMap
ev_binds, DictMap CtEvidence
solved_dicts) CtEvidence
ev_w
; case Maybe (EvBindMap, DictMap CtEvidence)
mb_stuff of
Maybe (EvBindMap, DictMap CtEvidence)
Nothing -> Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Just (EvBindMap
ev_binds', DictMap CtEvidence
solved_dicts')
-> do { EvBindsVar -> EvBindMap -> TcS ()
setTcEvBindsMap EvBindsVar
ev_binds_var EvBindMap
ev_binds'
; DictMap CtEvidence -> TcS ()
setSolvedDicts DictMap CtEvidence
solved_dicts'
; Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True } }
| Bool
otherwise
= Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
where
loc_w :: CtLoc
loc_w = CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev_w
try_solve_from_instance
:: (EvBindMap, DictMap CtEvidence) -> CtEvidence
-> MaybeT TcS (EvBindMap, DictMap CtEvidence)
try_solve_from_instance :: (EvBindMap, DictMap CtEvidence)
-> CtEvidence -> MaybeT TcS (EvBindMap, DictMap CtEvidence)
try_solve_from_instance (EvBindMap
ev_binds, DictMap CtEvidence
solved_dicts) CtEvidence
ev
| let pred :: PredType
pred = CtEvidence -> PredType
ctEvPred CtEvidence
ev
loc :: CtLoc
loc = CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev
, ClassPred Class
cls [PredType]
tys <- PredType -> Pred
classifyPredType PredType
pred
= do { ClsInstResult
inst_res <- TcS ClsInstResult -> MaybeT TcS ClsInstResult
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TcS ClsInstResult -> MaybeT TcS ClsInstResult)
-> TcS ClsInstResult -> MaybeT TcS ClsInstResult
forall a b. (a -> b) -> a -> b
$ DynFlags -> Bool -> Class -> [PredType] -> TcS ClsInstResult
matchGlobalInst DynFlags
dflags Bool
True Class
cls [PredType]
tys
; case ClsInstResult
inst_res of
OneInst { cir_new_theta :: ClsInstResult -> [PredType]
cir_new_theta = [PredType]
preds
, cir_mk_ev :: ClsInstResult -> [EvExpr] -> EvTerm
cir_mk_ev = [EvExpr] -> EvTerm
mk_ev
, cir_what :: ClsInstResult -> InstanceWhat
cir_what = InstanceWhat
what }
| InstanceWhat -> Bool
safeOverlap InstanceWhat
what
, (PredType -> Bool) -> [PredType] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all PredType -> Bool
isTyFamFree [PredType]
preds
-> do { let solved_dicts' :: DictMap CtEvidence
solved_dicts' = DictMap CtEvidence
-> Class -> [PredType] -> CtEvidence -> DictMap CtEvidence
forall a. DictMap a -> Class -> [PredType] -> a -> DictMap a
addDict DictMap CtEvidence
solved_dicts Class
cls [PredType]
tys CtEvidence
ev
; TcS () -> MaybeT TcS ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TcS () -> MaybeT TcS ()) -> TcS () -> MaybeT TcS ()
forall a b. (a -> b) -> a -> b
$ String -> SDoc -> TcS ()
traceTcS String
"shortCutSolver: found instance" ([PredType] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [PredType]
preds)
; CtLoc
loc' <- TcS CtLoc -> MaybeT TcS CtLoc
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TcS CtLoc -> MaybeT TcS CtLoc) -> TcS CtLoc -> MaybeT TcS CtLoc
forall a b. (a -> b) -> a -> b
$ CtLoc -> InstanceWhat -> PredType -> TcS CtLoc
checkInstanceOK CtLoc
loc InstanceWhat
what PredType
pred
; [MaybeNew]
evc_vs <- (PredType -> MaybeT TcS MaybeNew)
-> [PredType] -> MaybeT TcS [MaybeNew]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (CtLoc -> DictMap CtEvidence -> PredType -> MaybeT TcS MaybeNew
new_wanted_cached CtLoc
loc' DictMap CtEvidence
solved_dicts') [PredType]
preds
; let ev_tm :: EvTerm
ev_tm = [EvExpr] -> EvTerm
mk_ev ((MaybeNew -> EvExpr) -> [MaybeNew] -> [EvExpr]
forall a b. (a -> b) -> [a] -> [b]
map MaybeNew -> EvExpr
getEvExpr [MaybeNew]
evc_vs)
ev_binds' :: EvBindMap
ev_binds' = EvBindMap -> EvBind -> EvBindMap
extendEvBinds EvBindMap
ev_binds (EvBind -> EvBindMap) -> EvBind -> EvBindMap
forall a b. (a -> b) -> a -> b
$
EvVar -> EvTerm -> EvBind
mkWantedEvBind (CtEvidence -> EvVar
ctEvEvId CtEvidence
ev) EvTerm
ev_tm
; ((EvBindMap, DictMap CtEvidence)
-> CtEvidence -> MaybeT TcS (EvBindMap, DictMap CtEvidence))
-> (EvBindMap, DictMap CtEvidence)
-> [CtEvidence]
-> MaybeT TcS (EvBindMap, DictMap CtEvidence)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM (EvBindMap, DictMap CtEvidence)
-> CtEvidence -> MaybeT TcS (EvBindMap, DictMap CtEvidence)
try_solve_from_instance
(EvBindMap
ev_binds', DictMap CtEvidence
solved_dicts')
([MaybeNew] -> [CtEvidence]
freshGoals [MaybeNew]
evc_vs) }
ClsInstResult
_ -> MaybeT TcS (EvBindMap, DictMap CtEvidence)
forall (m :: * -> *) a. MonadPlus m => m a
mzero }
| Bool
otherwise = MaybeT TcS (EvBindMap, DictMap CtEvidence)
forall (m :: * -> *) a. MonadPlus m => m a
mzero
new_wanted_cached :: CtLoc -> DictMap CtEvidence -> TcPredType -> MaybeT TcS MaybeNew
new_wanted_cached :: CtLoc -> DictMap CtEvidence -> PredType -> MaybeT TcS MaybeNew
new_wanted_cached CtLoc
loc DictMap CtEvidence
cache PredType
pty
| ClassPred Class
cls [PredType]
tys <- PredType -> Pred
classifyPredType PredType
pty
= TcS MaybeNew -> MaybeT TcS MaybeNew
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TcS MaybeNew -> MaybeT TcS MaybeNew)
-> TcS MaybeNew -> MaybeT TcS MaybeNew
forall a b. (a -> b) -> a -> b
$ case DictMap CtEvidence
-> CtLoc -> Class -> [PredType] -> Maybe CtEvidence
forall a. DictMap a -> CtLoc -> Class -> [PredType] -> Maybe a
findDict DictMap CtEvidence
cache CtLoc
loc_w Class
cls [PredType]
tys of
Just CtEvidence
ctev -> MaybeNew -> TcS MaybeNew
forall (m :: * -> *) a. Monad m => a -> m a
return (MaybeNew -> TcS MaybeNew) -> MaybeNew -> TcS MaybeNew
forall a b. (a -> b) -> a -> b
$ EvExpr -> MaybeNew
Cached (CtEvidence -> EvExpr
ctEvExpr CtEvidence
ctev)
Maybe CtEvidence
Nothing -> CtEvidence -> MaybeNew
Fresh (CtEvidence -> MaybeNew) -> TcS CtEvidence -> TcS MaybeNew
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CtLoc -> PredType -> TcS CtEvidence
newWantedNC CtLoc
loc PredType
pty
| Bool
otherwise = MaybeT TcS MaybeNew
forall (m :: * -> *) a. MonadPlus m => m a
mzero
addFunDepWork :: InertCans -> CtEvidence -> Class -> TcS ()
addFunDepWork :: InertCans -> CtEvidence -> Class -> TcS ()
addFunDepWork InertCans
inerts CtEvidence
work_ev Class
cls
| CtEvidence -> Bool
isImprovable CtEvidence
work_ev
= (Ct -> TcS ()) -> Cts -> TcS ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> Bag a -> m ()
mapBagM_ Ct -> TcS ()
add_fds (DictMap Ct -> Class -> Cts
forall a. DictMap a -> Class -> Bag a
findDictsByClass (InertCans -> DictMap Ct
inert_dicts InertCans
inerts) Class
cls)
| Bool
otherwise
= () -> TcS ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
where
work_pred :: PredType
work_pred = CtEvidence -> PredType
ctEvPred CtEvidence
work_ev
work_loc :: CtLoc
work_loc = CtEvidence -> CtLoc
ctEvLoc CtEvidence
work_ev
add_fds :: Ct -> TcS ()
add_fds Ct
inert_ct
| CtEvidence -> Bool
isImprovable CtEvidence
inert_ev
= do { String -> SDoc -> TcS ()
traceTcS String
"addFunDepWork" ([SDoc] -> SDoc
vcat
[ CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
work_ev
, CtLoc -> SDoc
pprCtLoc CtLoc
work_loc, Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CtLoc -> Bool
isGivenLoc CtLoc
work_loc)
, CtLoc -> SDoc
pprCtLoc CtLoc
inert_loc, Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CtLoc -> Bool
isGivenLoc CtLoc
inert_loc)
, CtLoc -> SDoc
pprCtLoc CtLoc
derived_loc, Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CtLoc -> Bool
isGivenLoc CtLoc
derived_loc) ]) ;
[FunDepEqn CtLoc] -> TcS ()
emitFunDepDeriveds ([FunDepEqn CtLoc] -> TcS ()) -> [FunDepEqn CtLoc] -> TcS ()
forall a b. (a -> b) -> a -> b
$
CtLoc -> PredType -> PredType -> [FunDepEqn CtLoc]
forall loc. loc -> PredType -> PredType -> [FunDepEqn loc]
improveFromAnother CtLoc
derived_loc PredType
inert_pred PredType
work_pred
}
| Bool
otherwise
= () -> TcS ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
where
inert_ev :: CtEvidence
inert_ev = Ct -> CtEvidence
ctEvidence Ct
inert_ct
inert_pred :: PredType
inert_pred = CtEvidence -> PredType
ctEvPred CtEvidence
inert_ev
inert_loc :: CtLoc
inert_loc = CtEvidence -> CtLoc
ctEvLoc CtEvidence
inert_ev
derived_loc :: CtLoc
derived_loc = CtLoc
work_loc { ctl_depth :: SubGoalDepth
ctl_depth = CtLoc -> SubGoalDepth
ctl_depth CtLoc
work_loc SubGoalDepth -> SubGoalDepth -> SubGoalDepth
`maxSubGoalDepth`
CtLoc -> SubGoalDepth
ctl_depth CtLoc
inert_loc
, ctl_origin :: CtOrigin
ctl_origin = PredType
-> CtOrigin
-> RealSrcSpan
-> PredType
-> CtOrigin
-> RealSrcSpan
-> CtOrigin
FunDepOrigin1 PredType
work_pred
(CtLoc -> CtOrigin
ctLocOrigin CtLoc
work_loc)
(CtLoc -> RealSrcSpan
ctLocSpan CtLoc
work_loc)
PredType
inert_pred
(CtLoc -> CtOrigin
ctLocOrigin CtLoc
inert_loc)
(CtLoc -> RealSrcSpan
ctLocSpan CtLoc
inert_loc) }
interactGivenIP :: InertCans -> Ct -> TcS (StopOrContinue Ct)
interactGivenIP :: InertCans -> SimplifierStage
interactGivenIP InertCans
inerts workItem :: Ct
workItem@(CDictCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev, cc_class :: Ct -> Class
cc_class = Class
cls
, cc_tyargs :: Ct -> [PredType]
cc_tyargs = tys :: [PredType]
tys@(PredType
ip_str:[PredType]
_) })
= do { (InertCans -> InertCans) -> TcS ()
updInertCans ((InertCans -> InertCans) -> TcS ())
-> (InertCans -> InertCans) -> TcS ()
forall a b. (a -> b) -> a -> b
$ \InertCans
cans -> InertCans
cans { inert_dicts :: DictMap Ct
inert_dicts = DictMap Ct -> Class -> [PredType] -> Ct -> DictMap Ct
forall a. DictMap a -> Class -> [PredType] -> a -> DictMap a
addDict DictMap Ct
filtered_dicts Class
cls [PredType]
tys Ct
workItem }
; CtEvidence -> String -> TcS (StopOrContinue Ct)
forall a. CtEvidence -> String -> TcS (StopOrContinue a)
stopWith CtEvidence
ev String
"Given IP" }
where
dicts :: DictMap Ct
dicts = InertCans -> DictMap Ct
inert_dicts InertCans
inerts
ip_dicts :: Cts
ip_dicts = DictMap Ct -> Class -> Cts
forall a. DictMap a -> Class -> Bag a
findDictsByClass DictMap Ct
dicts Class
cls
other_ip_dicts :: Cts
other_ip_dicts = (Ct -> Bool) -> Cts -> Cts
forall a. (a -> Bool) -> Bag a -> Bag a
filterBag (Bool -> Bool
not (Bool -> Bool) -> (Ct -> Bool) -> Ct -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ct -> Bool
is_this_ip) Cts
ip_dicts
filtered_dicts :: DictMap Ct
filtered_dicts = DictMap Ct -> Class -> Cts -> DictMap Ct
addDictsByClass DictMap Ct
dicts Class
cls Cts
other_ip_dicts
is_this_ip :: Ct -> Bool
is_this_ip (CDictCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev, cc_tyargs :: Ct -> [PredType]
cc_tyargs = PredType
ip_str':[PredType]
_ })
= CtEvidence -> Bool
isGiven CtEvidence
ev Bool -> Bool -> Bool
&& PredType
ip_str HasDebugCallStack => PredType -> PredType -> Bool
PredType -> PredType -> Bool
`tcEqType` PredType
ip_str'
is_this_ip Ct
_ = Bool
False
interactGivenIP InertCans
_ Ct
wi = String -> SDoc -> TcS (StopOrContinue Ct)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"interactGivenIP" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
wi)
interactFunEq :: InertCans -> Ct -> TcS (StopOrContinue Ct)
interactFunEq :: InertCans -> SimplifierStage
interactFunEq InertCans
inerts work_item :: Ct
work_item@(CFunEqCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev, cc_fun :: Ct -> TyCon
cc_fun = TyCon
tc
, cc_tyargs :: Ct -> [PredType]
cc_tyargs = [PredType]
args, cc_fsk :: Ct -> EvVar
cc_fsk = EvVar
fsk })
| Just inert_ct :: Ct
inert_ct@(CFunEqCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev_i
, cc_fsk :: Ct -> EvVar
cc_fsk = EvVar
fsk_i })
<- DictMap Ct -> TyCon -> [PredType] -> Maybe Ct
forall a. FunEqMap a -> TyCon -> [PredType] -> Maybe a
findFunEq (InertCans -> DictMap Ct
inert_funeqs InertCans
inerts) TyCon
tc [PredType]
args
, pr :: (SwapFlag, Bool)
pr@(SwapFlag
swap_flag, Bool
upgrade_flag) <- CtEvidence
ev_i CtEvidence -> CtEvidence -> (SwapFlag, Bool)
`funEqCanDischarge` CtEvidence
ev
= do { String -> SDoc -> TcS ()
traceTcS String
"reactFunEq (rewrite inert item):" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text String
"work_item =" SDoc -> SDoc -> SDoc
<+> Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
work_item
, String -> SDoc
text String
"inertItem=" SDoc -> SDoc -> SDoc
<+> CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
ev_i
, String -> SDoc
text String
"(swap_flag, upgrade)" SDoc -> SDoc -> SDoc
<+> (SwapFlag, Bool) -> SDoc
forall a. Outputable a => a -> SDoc
ppr (SwapFlag, Bool)
pr ]
; if SwapFlag -> Bool
isSwapped SwapFlag
swap_flag
then do {
let work_item' :: Ct
work_item' | Bool
upgrade_flag = Ct -> Ct
upgradeWanted Ct
work_item
| Bool
otherwise = Ct
work_item
; (DictMap Ct -> DictMap Ct) -> TcS ()
updInertFunEqs ((DictMap Ct -> DictMap Ct) -> TcS ())
-> (DictMap Ct -> DictMap Ct) -> TcS ()
forall a b. (a -> b) -> a -> b
$ \ DictMap Ct
feqs -> DictMap Ct -> TyCon -> [PredType] -> Ct -> DictMap Ct
forall a. FunEqMap a -> TyCon -> [PredType] -> a -> FunEqMap a
insertFunEq DictMap Ct
feqs TyCon
tc [PredType]
args Ct
work_item'
; CtEvidence -> EvVar -> CtEvidence -> EvVar -> TcS ()
reactFunEq CtEvidence
ev EvVar
fsk CtEvidence
ev_i EvVar
fsk_i
; CtEvidence -> String -> TcS (StopOrContinue Ct)
forall a. CtEvidence -> String -> TcS (StopOrContinue a)
stopWith CtEvidence
ev String
"Work item rewrites inert" }
else do {
; Bool -> TcS () -> TcS ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
upgrade_flag (TcS () -> TcS ()) -> TcS () -> TcS ()
forall a b. (a -> b) -> a -> b
$
(DictMap Ct -> DictMap Ct) -> TcS ()
updInertFunEqs ((DictMap Ct -> DictMap Ct) -> TcS ())
-> (DictMap Ct -> DictMap Ct) -> TcS ()
forall a b. (a -> b) -> a -> b
$ \ DictMap Ct
feqs -> DictMap Ct -> TyCon -> [PredType] -> Ct -> DictMap Ct
forall a. FunEqMap a -> TyCon -> [PredType] -> a -> FunEqMap a
insertFunEq DictMap Ct
feqs TyCon
tc [PredType]
args
(Ct -> Ct
upgradeWanted Ct
inert_ct)
; CtEvidence -> EvVar -> CtEvidence -> EvVar -> TcS ()
reactFunEq CtEvidence
ev_i EvVar
fsk_i CtEvidence
ev EvVar
fsk
; CtEvidence -> String -> TcS (StopOrContinue Ct)
forall a. CtEvidence -> String -> TcS (StopOrContinue a)
stopWith CtEvidence
ev String
"Inert rewrites work item" } }
| Bool
otherwise
= do { CtEvidence -> InertCans -> TyCon -> [PredType] -> EvVar -> TcS ()
improveLocalFunEqs CtEvidence
ev InertCans
inerts TyCon
tc [PredType]
args EvVar
fsk
; SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
work_item }
interactFunEq InertCans
_ Ct
work_item = String -> SDoc -> TcS (StopOrContinue Ct)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"interactFunEq" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
work_item)
upgradeWanted :: Ct -> Ct
upgradeWanted :: Ct -> Ct
upgradeWanted Ct
ct = Ct
ct { cc_ev :: CtEvidence
cc_ev = CtEvidence -> CtEvidence
upgrade_ev (Ct -> CtEvidence
cc_ev Ct
ct) }
where
upgrade_ev :: CtEvidence -> CtEvidence
upgrade_ev CtEvidence
ev = ASSERT2( isWanted ev, ppr ct )
CtEvidence
ev { ctev_nosh :: ShadowInfo
ctev_nosh = ShadowInfo
WDeriv }
improveLocalFunEqs :: CtEvidence -> InertCans -> TyCon -> [TcType] -> TcTyVar
-> TcS ()
improveLocalFunEqs :: CtEvidence -> InertCans -> TyCon -> [PredType] -> EvVar -> TcS ()
improveLocalFunEqs CtEvidence
work_ev InertCans
inerts TyCon
fam_tc [PredType]
args EvVar
fsk
| CtEvidence -> Bool
isGiven CtEvidence
work_ev
Bool -> Bool -> Bool
|| Bool -> Bool
not (CtEvidence -> Bool
isImprovable CtEvidence
work_ev)
= () -> TcS ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise
= do { [FunDepEqn CtLoc]
eqns <- TcS [FunDepEqn CtLoc]
improvement_eqns
; if Bool -> Bool
not ([FunDepEqn CtLoc] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FunDepEqn CtLoc]
eqns)
then do { String -> SDoc -> TcS ()
traceTcS String
"interactFunEq improvements: " (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text String
"Eqns:" SDoc -> SDoc -> SDoc
<+> [FunDepEqn CtLoc] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [FunDepEqn CtLoc]
eqns
, String -> SDoc
text String
"Candidates:" SDoc -> SDoc -> SDoc
<+> [Ct] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Ct]
funeqs_for_tc
, String -> SDoc
text String
"Inert eqs:" SDoc -> SDoc -> SDoc
<+> InertEqs -> SDoc
forall a. Outputable a => a -> SDoc
ppr (InertCans -> InertEqs
inert_eqs InertCans
inerts) ]
; [FunDepEqn CtLoc] -> TcS ()
emitFunDepDeriveds [FunDepEqn CtLoc]
eqns }
else () -> TcS ()
forall (m :: * -> *) a. Monad m => a -> m a
return () }
where
funeqs :: DictMap Ct
funeqs = InertCans -> DictMap Ct
inert_funeqs InertCans
inerts
funeqs_for_tc :: [Ct]
funeqs_for_tc = DictMap Ct -> TyCon -> [Ct]
forall a. FunEqMap a -> TyCon -> [a]
findFunEqsByTyCon DictMap Ct
funeqs TyCon
fam_tc
work_loc :: CtLoc
work_loc = CtEvidence -> CtLoc
ctEvLoc CtEvidence
work_ev
work_pred :: PredType
work_pred = CtEvidence -> PredType
ctEvPred CtEvidence
work_ev
fam_inj_info :: Injectivity
fam_inj_info = TyCon -> Injectivity
tyConInjectivityInfo TyCon
fam_tc
improvement_eqns :: TcS [FunDepEqn CtLoc]
improvement_eqns :: TcS [FunDepEqn CtLoc]
improvement_eqns
| Just BuiltInSynFamily
ops <- TyCon -> Maybe BuiltInSynFamily
isBuiltInSynFamTyCon_maybe TyCon
fam_tc
=
do { PredType
rhs <- EvVar -> TcS PredType
rewriteTyVar EvVar
fsk
; (Ct -> TcS [FunDepEqn CtLoc]) -> [Ct] -> TcS [FunDepEqn CtLoc]
forall (m :: * -> *) a b. Monad m => (a -> m [b]) -> [a] -> m [b]
concatMapM (BuiltInSynFamily -> PredType -> Ct -> TcS [FunDepEqn CtLoc]
do_one_built_in BuiltInSynFamily
ops PredType
rhs) [Ct]
funeqs_for_tc }
| Injective [Bool]
injective_args <- Injectivity
fam_inj_info
=
do { PredType
rhs <- EvVar -> TcS PredType
rewriteTyVar EvVar
fsk
; (Ct -> TcS [FunDepEqn CtLoc]) -> [Ct] -> TcS [FunDepEqn CtLoc]
forall (m :: * -> *) a b. Monad m => (a -> m [b]) -> [a] -> m [b]
concatMapM ([Bool] -> PredType -> Ct -> TcS [FunDepEqn CtLoc]
do_one_injective [Bool]
injective_args PredType
rhs) [Ct]
funeqs_for_tc }
| Bool
otherwise
= [FunDepEqn CtLoc] -> TcS [FunDepEqn CtLoc]
forall (m :: * -> *) a. Monad m => a -> m a
return []
do_one_built_in :: BuiltInSynFamily -> PredType -> Ct -> TcS [FunDepEqn CtLoc]
do_one_built_in BuiltInSynFamily
ops PredType
rhs (CFunEqCan { cc_tyargs :: Ct -> [PredType]
cc_tyargs = [PredType]
iargs, cc_fsk :: Ct -> EvVar
cc_fsk = EvVar
ifsk, cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
inert_ev })
= do { PredType
inert_rhs <- EvVar -> TcS PredType
rewriteTyVar EvVar
ifsk
; [FunDepEqn CtLoc] -> TcS [FunDepEqn CtLoc]
forall (m :: * -> *) a. Monad m => a -> m a
return ([FunDepEqn CtLoc] -> TcS [FunDepEqn CtLoc])
-> [FunDepEqn CtLoc] -> TcS [FunDepEqn CtLoc]
forall a b. (a -> b) -> a -> b
$ CtEvidence -> [TypeEqn] -> [FunDepEqn CtLoc]
mk_fd_eqns CtEvidence
inert_ev (BuiltInSynFamily
-> [PredType] -> PredType -> [PredType] -> PredType -> [TypeEqn]
sfInteractInert BuiltInSynFamily
ops [PredType]
args PredType
rhs [PredType]
iargs PredType
inert_rhs) }
do_one_built_in BuiltInSynFamily
_ PredType
_ Ct
_ = String -> SDoc -> TcS [FunDepEqn CtLoc]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"interactFunEq 1" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
fam_tc)
do_one_injective :: [Bool] -> PredType -> Ct -> TcS [FunDepEqn CtLoc]
do_one_injective [Bool]
inj_args PredType
rhs (CFunEqCan { cc_tyargs :: Ct -> [PredType]
cc_tyargs = [PredType]
inert_args
, cc_fsk :: Ct -> EvVar
cc_fsk = EvVar
ifsk, cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
inert_ev })
| CtEvidence -> Bool
isImprovable CtEvidence
inert_ev
= do { PredType
inert_rhs <- EvVar -> TcS PredType
rewriteTyVar EvVar
ifsk
; [FunDepEqn CtLoc] -> TcS [FunDepEqn CtLoc]
forall (m :: * -> *) a. Monad m => a -> m a
return ([FunDepEqn CtLoc] -> TcS [FunDepEqn CtLoc])
-> [FunDepEqn CtLoc] -> TcS [FunDepEqn CtLoc]
forall a b. (a -> b) -> a -> b
$ if PredType
rhs HasDebugCallStack => PredType -> PredType -> Bool
PredType -> PredType -> Bool
`tcEqType` PredType
inert_rhs
then CtEvidence -> [TypeEqn] -> [FunDepEqn CtLoc]
mk_fd_eqns CtEvidence
inert_ev ([TypeEqn] -> [FunDepEqn CtLoc]) -> [TypeEqn] -> [FunDepEqn CtLoc]
forall a b. (a -> b) -> a -> b
$
[ PredType -> PredType -> TypeEqn
forall a. a -> a -> Pair a
Pair PredType
arg PredType
iarg
| (PredType
arg, PredType
iarg, Bool
True) <- [PredType] -> [PredType] -> [Bool] -> [(PredType, PredType, Bool)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [PredType]
args [PredType]
inert_args [Bool]
inj_args ]
else [] }
| Bool
otherwise
= [FunDepEqn CtLoc] -> TcS [FunDepEqn CtLoc]
forall (m :: * -> *) a. Monad m => a -> m a
return []
do_one_injective [Bool]
_ PredType
_ Ct
_ = String -> SDoc -> TcS [FunDepEqn CtLoc]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"interactFunEq 2" (TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
fam_tc)
mk_fd_eqns :: CtEvidence -> [TypeEqn] -> [FunDepEqn CtLoc]
mk_fd_eqns :: CtEvidence -> [TypeEqn] -> [FunDepEqn CtLoc]
mk_fd_eqns CtEvidence
inert_ev [TypeEqn]
eqns
| [TypeEqn] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TypeEqn]
eqns = []
| Bool
otherwise = [ FDEqn :: forall loc.
[EvVar]
-> [TypeEqn] -> PredType -> PredType -> loc -> FunDepEqn loc
FDEqn { fd_qtvs :: [EvVar]
fd_qtvs = [], fd_eqs :: [TypeEqn]
fd_eqs = [TypeEqn]
eqns
, fd_pred1 :: PredType
fd_pred1 = PredType
work_pred
, fd_pred2 :: PredType
fd_pred2 = CtEvidence -> PredType
ctEvPred CtEvidence
inert_ev
, fd_loc :: CtLoc
fd_loc = CtLoc
loc } ]
where
inert_loc :: CtLoc
inert_loc = CtEvidence -> CtLoc
ctEvLoc CtEvidence
inert_ev
loc :: CtLoc
loc = CtLoc
inert_loc { ctl_depth :: SubGoalDepth
ctl_depth = CtLoc -> SubGoalDepth
ctl_depth CtLoc
inert_loc SubGoalDepth -> SubGoalDepth -> SubGoalDepth
`maxSubGoalDepth`
CtLoc -> SubGoalDepth
ctl_depth CtLoc
work_loc }
reactFunEq :: CtEvidence -> TcTyVar
-> CtEvidence -> TcTyVar
-> TcS ()
reactFunEq :: CtEvidence -> EvVar -> CtEvidence -> EvVar -> TcS ()
reactFunEq CtEvidence
from_this EvVar
fsk1 CtEvidence
solve_this EvVar
fsk2
= do { String -> SDoc -> TcS ()
traceTcS String
"reactFunEq"
([SDoc] -> SDoc
vcat [CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
from_this, EvVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr EvVar
fsk1, CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
solve_this, EvVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr EvVar
fsk2])
; CtEvidence -> EvVar -> TcCoercion -> PredType -> TcS ()
dischargeFunEq CtEvidence
solve_this EvVar
fsk2 (HasDebugCallStack => CtEvidence -> TcCoercion
CtEvidence -> TcCoercion
ctEvCoercion CtEvidence
from_this) (EvVar -> PredType
mkTyVarTy EvVar
fsk1)
; String -> SDoc -> TcS ()
traceTcS String
"reactFunEq done" (CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
from_this SDoc -> SDoc -> SDoc
$$ EvVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr EvVar
fsk1 SDoc -> SDoc -> SDoc
$$
CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
solve_this SDoc -> SDoc -> SDoc
$$ EvVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr EvVar
fsk2) }
inertsCanDischarge :: InertCans -> TcTyVar -> TcType -> CtFlavourRole
-> Maybe ( CtEvidence
, SwapFlag
, Bool)
inertsCanDischarge :: InertCans
-> EvVar
-> PredType
-> CtFlavourRole
-> Maybe (CtEvidence, SwapFlag, Bool)
inertsCanDischarge InertCans
inerts EvVar
tv PredType
rhs CtFlavourRole
fr
| (CtEvidence
ev_i : [CtEvidence]
_) <- [ CtEvidence
ev_i | CTyEqCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev_i, cc_rhs :: Ct -> PredType
cc_rhs = PredType
rhs_i
, cc_eq_rel :: Ct -> EqRel
cc_eq_rel = EqRel
eq_rel }
<- InertCans -> EvVar -> [Ct]
findTyEqs InertCans
inerts EvVar
tv
, (CtEvidence -> CtFlavour
ctEvFlavour CtEvidence
ev_i, EqRel
eq_rel) CtFlavourRole -> CtFlavourRole -> Bool
`eqCanDischargeFR` CtFlavourRole
fr
, PredType
rhs_i HasDebugCallStack => PredType -> PredType -> Bool
PredType -> PredType -> Bool
`tcEqType` PredType
rhs ]
=
(CtEvidence, SwapFlag, Bool) -> Maybe (CtEvidence, SwapFlag, Bool)
forall a. a -> Maybe a
Just (CtEvidence
ev_i, SwapFlag
NotSwapped, CtEvidence -> Bool
keep_deriv CtEvidence
ev_i)
| Just EvVar
tv_rhs <- PredType -> Maybe EvVar
getTyVar_maybe PredType
rhs
, (CtEvidence
ev_i : [CtEvidence]
_) <- [ CtEvidence
ev_i | CTyEqCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev_i, cc_rhs :: Ct -> PredType
cc_rhs = PredType
rhs_i
, cc_eq_rel :: Ct -> EqRel
cc_eq_rel = EqRel
eq_rel }
<- InertCans -> EvVar -> [Ct]
findTyEqs InertCans
inerts EvVar
tv_rhs
, (CtEvidence -> CtFlavour
ctEvFlavour CtEvidence
ev_i, EqRel
eq_rel) CtFlavourRole -> CtFlavourRole -> Bool
`eqCanDischargeFR` CtFlavourRole
fr
, PredType
rhs_i HasDebugCallStack => PredType -> PredType -> Bool
PredType -> PredType -> Bool
`tcEqType` EvVar -> PredType
mkTyVarTy EvVar
tv ]
=
(CtEvidence, SwapFlag, Bool) -> Maybe (CtEvidence, SwapFlag, Bool)
forall a. a -> Maybe a
Just (CtEvidence
ev_i, SwapFlag
IsSwapped, CtEvidence -> Bool
keep_deriv CtEvidence
ev_i)
| Bool
otherwise
= Maybe (CtEvidence, SwapFlag, Bool)
forall a. Maybe a
Nothing
where
keep_deriv :: CtEvidence -> Bool
keep_deriv CtEvidence
ev_i
| Wanted ShadowInfo
WOnly <- CtEvidence -> CtFlavour
ctEvFlavour CtEvidence
ev_i
, (Wanted ShadowInfo
WDeriv, EqRel
_) <- CtFlavourRole
fr
= Bool
True
| Bool
otherwise
= Bool
False
interactTyVarEq :: InertCans -> Ct -> TcS (StopOrContinue Ct)
interactTyVarEq :: InertCans -> SimplifierStage
interactTyVarEq InertCans
inerts workItem :: Ct
workItem@(CTyEqCan { cc_tyvar :: Ct -> EvVar
cc_tyvar = EvVar
tv
, cc_rhs :: Ct -> PredType
cc_rhs = PredType
rhs
, cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev
, cc_eq_rel :: Ct -> EqRel
cc_eq_rel = EqRel
eq_rel })
| Just (CtEvidence
ev_i, SwapFlag
swapped, Bool
keep_deriv)
<- InertCans
-> EvVar
-> PredType
-> CtFlavourRole
-> Maybe (CtEvidence, SwapFlag, Bool)
inertsCanDischarge InertCans
inerts EvVar
tv PredType
rhs (CtEvidence -> CtFlavour
ctEvFlavour CtEvidence
ev, EqRel
eq_rel)
= do { CtEvidence -> EvTerm -> TcS ()
setEvBindIfWanted CtEvidence
ev (EvTerm -> TcS ()) -> EvTerm -> TcS ()
forall a b. (a -> b) -> a -> b
$
TcCoercion -> EvTerm
evCoercion (SwapFlag -> TcCoercion -> TcCoercion
maybeSym SwapFlag
swapped (TcCoercion -> TcCoercion) -> TcCoercion -> TcCoercion
forall a b. (a -> b) -> a -> b
$
Role -> Role -> TcCoercion -> TcCoercion
tcDowngradeRole (EqRel -> Role
eqRelRole EqRel
eq_rel)
(CtEvidence -> Role
ctEvRole CtEvidence
ev_i)
(HasDebugCallStack => CtEvidence -> TcCoercion
CtEvidence -> TcCoercion
ctEvCoercion CtEvidence
ev_i))
; let deriv_ev :: CtEvidence
deriv_ev = CtDerived :: PredType -> CtLoc -> CtEvidence
CtDerived { ctev_pred :: PredType
ctev_pred = CtEvidence -> PredType
ctEvPred CtEvidence
ev
, ctev_loc :: CtLoc
ctev_loc = CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev }
; Bool -> TcS () -> TcS ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
keep_deriv (TcS () -> TcS ()) -> TcS () -> TcS ()
forall a b. (a -> b) -> a -> b
$
[Ct] -> TcS ()
emitWork [Ct
workItem { cc_ev :: CtEvidence
cc_ev = CtEvidence
deriv_ev }]
; CtEvidence -> String -> TcS (StopOrContinue Ct)
forall a. CtEvidence -> String -> TcS (StopOrContinue a)
stopWith CtEvidence
ev String
"Solved from inert" }
| EqRel
ReprEq <- EqRel
eq_rel
= do { String -> SDoc -> TcS ()
traceTcS String
"Not unifying representational equality" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
workItem)
; SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
workItem }
| CtEvidence -> Bool
isGiven CtEvidence
ev
= SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
workItem
| Bool
otherwise
= do { TcLevel
tclvl <- TcS TcLevel
getTcLevel
; if TcLevel -> EvVar -> PredType -> Bool
canSolveByUnification TcLevel
tclvl EvVar
tv PredType
rhs
then do { CtEvidence -> EvVar -> PredType -> TcS ()
solveByUnification CtEvidence
ev EvVar
tv PredType
rhs
; Int
n_kicked <- EvVar -> TcS Int
kickOutAfterUnification EvVar
tv
; StopOrContinue Ct -> TcS (StopOrContinue Ct)
forall (m :: * -> *) a. Monad m => a -> m a
return (CtEvidence -> SDoc -> StopOrContinue Ct
forall a. CtEvidence -> SDoc -> StopOrContinue a
Stop CtEvidence
ev (String -> SDoc
text String
"Solved by unification" SDoc -> SDoc -> SDoc
<+> Int -> SDoc
pprKicked Int
n_kicked)) }
else SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
workItem }
interactTyVarEq InertCans
_ Ct
wi = String -> SDoc -> TcS (StopOrContinue Ct)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"interactTyVarEq" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
wi)
solveByUnification :: CtEvidence -> TcTyVar -> Xi -> TcS ()
solveByUnification :: CtEvidence -> EvVar -> PredType -> TcS ()
solveByUnification CtEvidence
wd EvVar
tv PredType
xi
= do { let tv_ty :: PredType
tv_ty = EvVar -> PredType
mkTyVarTy EvVar
tv
; String -> SDoc -> TcS ()
traceTcS String
"Sneaky unification:" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [String -> SDoc
text String
"Unifies:" SDoc -> SDoc -> SDoc
<+> EvVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr EvVar
tv SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
":=" SDoc -> SDoc -> SDoc
<+> PredType -> SDoc
forall a. Outputable a => a -> SDoc
ppr PredType
xi,
String -> SDoc
text String
"Coercion:" SDoc -> SDoc -> SDoc
<+> PredType -> PredType -> SDoc
pprEq PredType
tv_ty PredType
xi,
String -> SDoc
text String
"Left Kind is:" SDoc -> SDoc -> SDoc
<+> PredType -> SDoc
forall a. Outputable a => a -> SDoc
ppr (HasDebugCallStack => PredType -> PredType
PredType -> PredType
tcTypeKind PredType
tv_ty),
String -> SDoc
text String
"Right Kind is:" SDoc -> SDoc -> SDoc
<+> PredType -> SDoc
forall a. Outputable a => a -> SDoc
ppr (HasDebugCallStack => PredType -> PredType
PredType -> PredType
tcTypeKind PredType
xi) ]
; EvVar -> PredType -> TcS ()
unifyTyVar EvVar
tv PredType
xi
; CtEvidence -> EvTerm -> TcS ()
setEvBindIfWanted CtEvidence
wd (TcCoercion -> EvTerm
evCoercion (PredType -> TcCoercion
mkTcNomReflCo PredType
xi)) }
emitFunDepDeriveds :: [FunDepEqn CtLoc] -> TcS ()
emitFunDepDeriveds :: [FunDepEqn CtLoc] -> TcS ()
emitFunDepDeriveds [FunDepEqn CtLoc]
fd_eqns
= (FunDepEqn CtLoc -> TcS ()) -> [FunDepEqn CtLoc] -> TcS ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ FunDepEqn CtLoc -> TcS ()
do_one_FDEqn [FunDepEqn CtLoc]
fd_eqns
where
do_one_FDEqn :: FunDepEqn CtLoc -> TcS ()
do_one_FDEqn (FDEqn { fd_qtvs :: forall loc. FunDepEqn loc -> [EvVar]
fd_qtvs = [EvVar]
tvs, fd_eqs :: forall loc. FunDepEqn loc -> [TypeEqn]
fd_eqs = [TypeEqn]
eqs, fd_loc :: forall loc. FunDepEqn loc -> loc
fd_loc = CtLoc
loc })
| [EvVar] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [EvVar]
tvs
= do { String -> SDoc -> TcS ()
traceTcS String
"emitFunDepDeriveds 1" (SubGoalDepth -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CtLoc -> SubGoalDepth
ctl_depth CtLoc
loc) SDoc -> SDoc -> SDoc
$$ [TypeEqn] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TypeEqn]
eqs SDoc -> SDoc -> SDoc
$$ Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CtLoc -> Bool
isGivenLoc CtLoc
loc))
; (TypeEqn -> TcS ()) -> [TypeEqn] -> TcS ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (CtLoc -> Role -> TypeEqn -> TcS ()
unifyDerived CtLoc
loc Role
Nominal) [TypeEqn]
eqs }
| Bool
otherwise
= do { String -> SDoc -> TcS ()
traceTcS String
"emitFunDepDeriveds 2" (SubGoalDepth -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CtLoc -> SubGoalDepth
ctl_depth CtLoc
loc) SDoc -> SDoc -> SDoc
$$ [EvVar] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [EvVar]
tvs SDoc -> SDoc -> SDoc
$$ [TypeEqn] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TypeEqn]
eqs)
; TCvSubst
subst <- [EvVar] -> TcS TCvSubst
instFlexi [EvVar]
tvs
; (TypeEqn -> TcS ()) -> [TypeEqn] -> TcS ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (CtLoc -> TCvSubst -> TypeEqn -> TcS ()
do_one_eq CtLoc
loc TCvSubst
subst) [TypeEqn]
eqs }
do_one_eq :: CtLoc -> TCvSubst -> TypeEqn -> TcS ()
do_one_eq CtLoc
loc TCvSubst
subst (Pair PredType
ty1 PredType
ty2)
= CtLoc -> Role -> TypeEqn -> TcS ()
unifyDerived CtLoc
loc Role
Nominal (TypeEqn -> TcS ()) -> TypeEqn -> TcS ()
forall a b. (a -> b) -> a -> b
$
PredType -> PredType -> TypeEqn
forall a. a -> a -> Pair a
Pair (TCvSubst -> PredType -> PredType
Type.substTyUnchecked TCvSubst
subst PredType
ty1) (TCvSubst -> PredType -> PredType
Type.substTyUnchecked TCvSubst
subst PredType
ty2)
topReactionsStage :: WorkItem -> TcS (StopOrContinue Ct)
topReactionsStage :: SimplifierStage
topReactionsStage Ct
work_item
= do { String -> SDoc -> TcS ()
traceTcS String
"doTopReact" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
work_item)
; case Ct
work_item of
CDictCan {} -> do { InertSet
inerts <- TcS InertSet
getTcSInerts
; InertSet -> SimplifierStage
doTopReactDict InertSet
inerts Ct
work_item }
CFunEqCan {} -> SimplifierStage
doTopReactFunEq Ct
work_item
CIrredCan {} -> SimplifierStage
doTopReactOther Ct
work_item
CTyEqCan {} -> SimplifierStage
doTopReactOther Ct
work_item
Ct
_ ->
SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
work_item }
doTopReactOther :: Ct -> TcS (StopOrContinue Ct)
doTopReactOther :: SimplifierStage
doTopReactOther Ct
work_item
| CtEvidence -> Bool
isGiven CtEvidence
ev
= SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
work_item
| EqPred EqRel
eq_rel PredType
t1 PredType
t2 <- PredType -> Pred
classifyPredType PredType
pred
=
case EqRel -> PredType -> PredType -> Maybe (Class, [PredType])
boxEqPred EqRel
eq_rel PredType
t1 PredType
t2 of
Maybe (Class, [PredType])
Nothing -> SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
work_item
Just (Class
cls, [PredType]
tys)
-> do { ClsInstResult
res <- PredType -> CtLoc -> TcS ClsInstResult
matchLocalInst (Class -> [PredType] -> PredType
mkClassPred Class
cls [PredType]
tys) CtLoc
loc
; case ClsInstResult
res of
OneInst { cir_mk_ev :: ClsInstResult -> [EvExpr] -> EvTerm
cir_mk_ev = [EvExpr] -> EvTerm
mk_ev }
-> Ct -> ClsInstResult -> TcS (StopOrContinue Ct)
chooseInstance Ct
work_item
(ClsInstResult
res { cir_mk_ev :: [EvExpr] -> EvTerm
cir_mk_ev = Class -> [PredType] -> ([EvExpr] -> EvTerm) -> [EvExpr] -> EvTerm
forall t. Class -> [PredType] -> (t -> EvTerm) -> t -> EvTerm
mk_eq_ev Class
cls [PredType]
tys [EvExpr] -> EvTerm
mk_ev })
where
ClsInstResult
_ -> SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
work_item }
| Bool
otherwise
= do { ClsInstResult
res <- PredType -> CtLoc -> TcS ClsInstResult
matchLocalInst PredType
pred CtLoc
loc
; case ClsInstResult
res of
OneInst {} -> Ct -> ClsInstResult -> TcS (StopOrContinue Ct)
chooseInstance Ct
work_item ClsInstResult
res
ClsInstResult
_ -> SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
work_item }
where
ev :: CtEvidence
ev = Ct -> CtEvidence
ctEvidence Ct
work_item
loc :: CtLoc
loc = CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev
pred :: PredType
pred = CtEvidence -> PredType
ctEvPred CtEvidence
ev
mk_eq_ev :: Class -> [PredType] -> (t -> EvTerm) -> t -> EvTerm
mk_eq_ev Class
cls [PredType]
tys t -> EvTerm
mk_ev t
evs
= case (t -> EvTerm
mk_ev t
evs) of
EvExpr EvExpr
e -> EvExpr -> EvTerm
EvExpr (EvVar -> EvExpr
forall b. EvVar -> Expr b
Var EvVar
sc_id EvExpr -> [PredType] -> EvExpr
forall b. Expr b -> [PredType] -> Expr b
`mkTyApps` [PredType]
tys EvExpr -> EvExpr -> EvExpr
forall b. Expr b -> Expr b -> Expr b
`App` EvExpr
e)
EvTerm
ev -> String -> SDoc -> EvTerm
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"mk_eq_ev" (EvTerm -> SDoc
forall a. Outputable a => a -> SDoc
ppr EvTerm
ev)
where
[EvVar
sc_id] = Class -> [EvVar]
classSCSelIds Class
cls
doTopReactFunEq :: Ct -> TcS (StopOrContinue Ct)
doTopReactFunEq :: SimplifierStage
doTopReactFunEq work_item :: Ct
work_item@(CFunEqCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
old_ev, cc_fun :: Ct -> TyCon
cc_fun = TyCon
fam_tc
, cc_tyargs :: Ct -> [PredType]
cc_tyargs = [PredType]
args, cc_fsk :: Ct -> EvVar
cc_fsk = EvVar
fsk })
| EvVar
fsk EvVar -> VarSet -> Bool
`elemVarSet` [PredType] -> VarSet
tyCoVarsOfTypes [PredType]
args
= TcS (StopOrContinue Ct)
no_reduction
| Bool
otherwise
= do { Maybe (TcCoercion, PredType)
match_res <- TyCon -> [PredType] -> TcS (Maybe (TcCoercion, PredType))
matchFam TyCon
fam_tc [PredType]
args
; case Maybe (TcCoercion, PredType)
match_res of
Maybe (TcCoercion, PredType)
Nothing -> TcS (StopOrContinue Ct)
no_reduction
Just (TcCoercion, PredType)
match_info -> CtEvidence
-> EvVar -> (TcCoercion, PredType) -> TcS (StopOrContinue Ct)
reduce_top_fun_eq CtEvidence
old_ev EvVar
fsk (TcCoercion, PredType)
match_info }
where
no_reduction :: TcS (StopOrContinue Ct)
no_reduction
= do { CtEvidence -> TyCon -> [PredType] -> EvVar -> TcS ()
improveTopFunEqs CtEvidence
old_ev TyCon
fam_tc [PredType]
args EvVar
fsk
; SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
work_item }
doTopReactFunEq Ct
w = String -> SDoc -> TcS (StopOrContinue Ct)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"doTopReactFunEq" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
w)
reduce_top_fun_eq :: CtEvidence -> TcTyVar -> (TcCoercion, TcType)
-> TcS (StopOrContinue Ct)
reduce_top_fun_eq :: CtEvidence
-> EvVar -> (TcCoercion, PredType) -> TcS (StopOrContinue Ct)
reduce_top_fun_eq CtEvidence
old_ev EvVar
fsk (TcCoercion
ax_co, PredType
rhs_ty)
| Bool -> Bool
not (CtEvidence -> Bool
isDerived CtEvidence
old_ev)
, Just (TyCon
tc, [PredType]
tc_args) <- HasCallStack => PredType -> Maybe (TyCon, [PredType])
PredType -> Maybe (TyCon, [PredType])
tcSplitTyConApp_maybe PredType
rhs_ty
, TyCon -> Bool
isTypeFamilyTyCon TyCon
tc
, [PredType]
tc_args [PredType] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthIs` TyCon -> Int
tyConArity TyCon
tc
=
do { CtEvidence -> EvVar -> TcCoercion -> TyCon -> [PredType] -> TcS ()
shortCutReduction CtEvidence
old_ev EvVar
fsk TcCoercion
ax_co TyCon
tc [PredType]
tc_args
; CtEvidence -> String -> TcS (StopOrContinue Ct)
forall a. CtEvidence -> String -> TcS (StopOrContinue a)
stopWith CtEvidence
old_ev String
"Fun/Top (shortcut)" }
| Bool
otherwise
= ASSERT2( not (fsk `elemVarSet` tyCoVarsOfType rhs_ty)
, ppr old_ev $$ ppr rhs_ty )
do { (PredType
rhs_xi, TcCoercion
flatten_co) <- FlattenMode -> CtEvidence -> PredType -> TcS (PredType, TcCoercion)
flatten FlattenMode
FM_FlattenAll CtEvidence
old_ev PredType
rhs_ty
; let total_co :: TcCoercion
total_co = TcCoercion
ax_co TcCoercion -> TcCoercion -> TcCoercion
`mkTcTransCo` TcCoercion -> TcCoercion
mkTcSymCo TcCoercion
flatten_co
; CtEvidence -> EvVar -> TcCoercion -> PredType -> TcS ()
dischargeFunEq CtEvidence
old_ev EvVar
fsk TcCoercion
total_co PredType
rhs_xi
; String -> SDoc -> TcS ()
traceTcS String
"doTopReactFunEq" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text String
"old_ev:" SDoc -> SDoc -> SDoc
<+> CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
old_ev
, Int -> SDoc -> SDoc
nest Int
2 (String -> SDoc
text String
":=") SDoc -> SDoc -> SDoc
<+> TcCoercion -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcCoercion
ax_co ]
; CtEvidence -> String -> TcS (StopOrContinue Ct)
forall a. CtEvidence -> String -> TcS (StopOrContinue a)
stopWith CtEvidence
old_ev String
"Fun/Top" }
improveTopFunEqs :: CtEvidence -> TyCon -> [TcType] -> TcTyVar -> TcS ()
improveTopFunEqs :: CtEvidence -> TyCon -> [PredType] -> EvVar -> TcS ()
improveTopFunEqs CtEvidence
ev TyCon
fam_tc [PredType]
args EvVar
fsk
| CtEvidence -> Bool
isGiven CtEvidence
ev
Bool -> Bool -> Bool
|| Bool -> Bool
not (CtEvidence -> Bool
isImprovable CtEvidence
ev)
= () -> TcS ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise
= do { (FamInstEnv, FamInstEnv)
fam_envs <- TcS (FamInstEnv, FamInstEnv)
getFamInstEnvs
; PredType
rhs <- EvVar -> TcS PredType
rewriteTyVar EvVar
fsk
; [TypeEqn]
eqns <- (FamInstEnv, FamInstEnv)
-> TyCon -> [PredType] -> PredType -> TcS [TypeEqn]
improve_top_fun_eqs (FamInstEnv, FamInstEnv)
fam_envs TyCon
fam_tc [PredType]
args PredType
rhs
; String -> SDoc -> TcS ()
traceTcS String
"improveTopFunEqs" ([SDoc] -> SDoc
vcat [ TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
fam_tc SDoc -> SDoc -> SDoc
<+> [PredType] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [PredType]
args SDoc -> SDoc -> SDoc
<+> PredType -> SDoc
forall a. Outputable a => a -> SDoc
ppr PredType
rhs
, [TypeEqn] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TypeEqn]
eqns ])
; (TypeEqn -> TcS ()) -> [TypeEqn] -> TcS ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (CtLoc -> Role -> TypeEqn -> TcS ()
unifyDerived CtLoc
loc Role
Nominal) [TypeEqn]
eqns }
where
loc :: CtLoc
loc = CtLoc -> CtLoc
bumpCtLocDepth (CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev)
improve_top_fun_eqs :: FamInstEnvs
-> TyCon -> [TcType] -> TcType
-> TcS [TypeEqn]
improve_top_fun_eqs :: (FamInstEnv, FamInstEnv)
-> TyCon -> [PredType] -> PredType -> TcS [TypeEqn]
improve_top_fun_eqs (FamInstEnv, FamInstEnv)
fam_envs TyCon
fam_tc [PredType]
args PredType
rhs_ty
| Just BuiltInSynFamily
ops <- TyCon -> Maybe BuiltInSynFamily
isBuiltInSynFamTyCon_maybe TyCon
fam_tc
= [TypeEqn] -> TcS [TypeEqn]
forall (m :: * -> *) a. Monad m => a -> m a
return (BuiltInSynFamily -> [PredType] -> PredType -> [TypeEqn]
sfInteractTop BuiltInSynFamily
ops [PredType]
args PredType
rhs_ty)
| TyCon -> Bool
isOpenTypeFamilyTyCon TyCon
fam_tc
, Injective [Bool]
injective_args <- TyCon -> Injectivity
tyConInjectivityInfo TyCon
fam_tc
, let fam_insts :: [FamInst]
fam_insts = (FamInstEnv, FamInstEnv) -> TyCon -> [FamInst]
lookupFamInstEnvByTyCon (FamInstEnv, FamInstEnv)
fam_envs TyCon
fam_tc
=
do { let improvs :: [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
improvs = [FamInst]
-> (FamInst -> [EvVar])
-> (FamInst -> [PredType])
-> (FamInst -> PredType)
-> (FamInst -> Maybe CoAxBranch)
-> [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
forall a.
[a]
-> (a -> [EvVar])
-> (a -> [PredType])
-> (a -> PredType)
-> (a -> Maybe CoAxBranch)
-> [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
buildImprovementData [FamInst]
fam_insts
FamInst -> [EvVar]
fi_tvs FamInst -> [PredType]
fi_tys FamInst -> PredType
fi_rhs (Maybe CoAxBranch -> FamInst -> Maybe CoAxBranch
forall a b. a -> b -> a
const Maybe CoAxBranch
forall a. Maybe a
Nothing)
; String -> SDoc -> TcS ()
traceTcS String
"improve_top_fun_eqs2" ([([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
improvs)
; (([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)
-> TcS [TypeEqn])
-> [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
-> TcS [TypeEqn]
forall (m :: * -> *) a b. Monad m => (a -> m [b]) -> [a] -> m [b]
concatMapM ([Bool]
-> ([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)
-> TcS [TypeEqn]
injImproveEqns [Bool]
injective_args) ([([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
-> TcS [TypeEqn])
-> [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
-> TcS [TypeEqn]
forall a b. (a -> b) -> a -> b
$
Int
-> [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
-> [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
forall a. Int -> [a] -> [a]
take Int
1 [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
improvs }
| Just CoAxiom Branched
ax <- TyCon -> Maybe (CoAxiom Branched)
isClosedSynFamilyTyConWithAxiom_maybe TyCon
fam_tc
, Injective [Bool]
injective_args <- TyCon -> Injectivity
tyConInjectivityInfo TyCon
fam_tc
= (([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)
-> TcS [TypeEqn])
-> [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
-> TcS [TypeEqn]
forall (m :: * -> *) a b. Monad m => (a -> m [b]) -> [a] -> m [b]
concatMapM ([Bool]
-> ([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)
-> TcS [TypeEqn]
injImproveEqns [Bool]
injective_args) ([([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
-> TcS [TypeEqn])
-> [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
-> TcS [TypeEqn]
forall a b. (a -> b) -> a -> b
$
[CoAxBranch]
-> (CoAxBranch -> [EvVar])
-> (CoAxBranch -> [PredType])
-> (CoAxBranch -> PredType)
-> (CoAxBranch -> Maybe CoAxBranch)
-> [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
forall a.
[a]
-> (a -> [EvVar])
-> (a -> [PredType])
-> (a -> PredType)
-> (a -> Maybe CoAxBranch)
-> [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
buildImprovementData (Branches Branched -> [CoAxBranch]
forall (br :: BranchFlag). Branches br -> [CoAxBranch]
fromBranches (CoAxiom Branched -> Branches Branched
forall (br :: BranchFlag). CoAxiom br -> Branches br
co_ax_branches CoAxiom Branched
ax))
CoAxBranch -> [EvVar]
cab_tvs CoAxBranch -> [PredType]
cab_lhs CoAxBranch -> PredType
cab_rhs CoAxBranch -> Maybe CoAxBranch
forall a. a -> Maybe a
Just
| Bool
otherwise
= [TypeEqn] -> TcS [TypeEqn]
forall (m :: * -> *) a. Monad m => a -> m a
return []
where
buildImprovementData
:: [a]
-> (a -> [TyVar])
-> (a -> [Type])
-> (a -> Type)
-> (a -> Maybe CoAxBranch)
-> [( [Type], TCvSubst, [TyVar], Maybe CoAxBranch )]
buildImprovementData :: [a]
-> (a -> [EvVar])
-> (a -> [PredType])
-> (a -> PredType)
-> (a -> Maybe CoAxBranch)
-> [([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)]
buildImprovementData [a]
axioms a -> [EvVar]
axiomTVs a -> [PredType]
axiomLHS a -> PredType
axiomRHS a -> Maybe CoAxBranch
wrap =
[ ([PredType]
ax_args, TCvSubst
subst, [EvVar]
unsubstTvs, a -> Maybe CoAxBranch
wrap a
axiom)
| a
axiom <- [a]
axioms
, let ax_args :: [PredType]
ax_args = a -> [PredType]
axiomLHS a
axiom
ax_rhs :: PredType
ax_rhs = a -> PredType
axiomRHS a
axiom
ax_tvs :: [EvVar]
ax_tvs = a -> [EvVar]
axiomTVs a
axiom
, Just TCvSubst
subst <- [Bool -> PredType -> PredType -> Maybe TCvSubst
tcUnifyTyWithTFs Bool
False PredType
ax_rhs PredType
rhs_ty]
, let notInSubst :: EvVar -> Bool
notInSubst EvVar
tv = Bool -> Bool
not (EvVar
tv EvVar -> VarEnv PredType -> Bool
forall a. EvVar -> VarEnv a -> Bool
`elemVarEnv` TCvSubst -> VarEnv PredType
getTvSubstEnv TCvSubst
subst)
unsubstTvs :: [EvVar]
unsubstTvs = (EvVar -> Bool) -> [EvVar] -> [EvVar]
forall a. (a -> Bool) -> [a] -> [a]
filter (EvVar -> Bool
notInSubst (EvVar -> Bool) -> (EvVar -> Bool) -> EvVar -> Bool
forall (f :: * -> *). Applicative f => f Bool -> f Bool -> f Bool
<&&> EvVar -> Bool
isTyVar) [EvVar]
ax_tvs ]
injImproveEqns :: [Bool]
-> ([Type], TCvSubst, [TyCoVar], Maybe CoAxBranch)
-> TcS [TypeEqn]
injImproveEqns :: [Bool]
-> ([PredType], TCvSubst, [EvVar], Maybe CoAxBranch)
-> TcS [TypeEqn]
injImproveEqns [Bool]
inj_args ([PredType]
ax_args, TCvSubst
subst, [EvVar]
unsubstTvs, Maybe CoAxBranch
cabr)
= do { TCvSubst
subst <- TCvSubst -> [EvVar] -> TcS TCvSubst
instFlexiX TCvSubst
subst [EvVar]
unsubstTvs
; [TypeEqn] -> TcS [TypeEqn]
forall (m :: * -> *) a. Monad m => a -> m a
return [ PredType -> PredType -> TypeEqn
forall a. a -> a -> Pair a
Pair (TCvSubst -> PredType -> PredType
substTyUnchecked TCvSubst
subst PredType
ax_arg) PredType
arg
| case Maybe CoAxBranch
cabr of
Just CoAxBranch
cabr' -> [PredType] -> CoAxBranch -> Bool
apartnessCheck (HasCallStack => TCvSubst -> [PredType] -> [PredType]
TCvSubst -> [PredType] -> [PredType]
substTys TCvSubst
subst [PredType]
ax_args) CoAxBranch
cabr'
Maybe CoAxBranch
_ -> Bool
True
, (PredType
ax_arg, PredType
arg, Bool
True) <- [PredType] -> [PredType] -> [Bool] -> [(PredType, PredType, Bool)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [PredType]
ax_args [PredType]
args [Bool]
inj_args ] }
shortCutReduction :: CtEvidence -> TcTyVar -> TcCoercion
-> TyCon -> [TcType] -> TcS ()
shortCutReduction :: CtEvidence -> EvVar -> TcCoercion -> TyCon -> [PredType] -> TcS ()
shortCutReduction CtEvidence
old_ev EvVar
fsk TcCoercion
ax_co TyCon
fam_tc [PredType]
tc_args
= ASSERT( ctEvEqRel old_ev == NomEq)
do { CtEvidence
new_ev <- case CtEvidence -> CtFlavour
ctEvFlavour CtEvidence
old_ev of
CtFlavour
Given -> CtLoc -> (PredType, EvTerm) -> TcS CtEvidence
newGivenEvVar CtLoc
deeper_loc
( PredType -> PredType -> PredType
mkPrimEqPred (TyCon -> [PredType] -> PredType
mkTyConApp TyCon
fam_tc [PredType]
tc_args) (EvVar -> PredType
mkTyVarTy EvVar
fsk)
, TcCoercion -> EvTerm
evCoercion (TcCoercion -> TcCoercion
mkTcSymCo TcCoercion
ax_co
TcCoercion -> TcCoercion -> TcCoercion
`mkTcTransCo` HasDebugCallStack => CtEvidence -> TcCoercion
CtEvidence -> TcCoercion
ctEvCoercion CtEvidence
old_ev) )
Wanted {} ->
do { (CtEvidence
new_ev, TcCoercion
new_co) <- CtLoc
-> Role -> PredType -> PredType -> TcS (CtEvidence, TcCoercion)
newWantedEq CtLoc
deeper_loc Role
Nominal
(TyCon -> [PredType] -> PredType
mkTyConApp TyCon
fam_tc [PredType]
tc_args) (EvVar -> PredType
mkTyVarTy EvVar
fsk)
; TcEvDest -> TcCoercion -> TcS ()
setWantedEq (CtEvidence -> TcEvDest
ctev_dest CtEvidence
old_ev) (TcCoercion -> TcS ()) -> TcCoercion -> TcS ()
forall a b. (a -> b) -> a -> b
$ TcCoercion
ax_co TcCoercion -> TcCoercion -> TcCoercion
`mkTcTransCo` TcCoercion
new_co
; CtEvidence -> TcS CtEvidence
forall (m :: * -> *) a. Monad m => a -> m a
return CtEvidence
new_ev }
CtFlavour
Derived -> String -> SDoc -> TcS CtEvidence
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"shortCutReduction" (CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
old_ev)
; let new_ct :: Ct
new_ct = CFunEqCan :: CtEvidence -> TyCon -> [PredType] -> EvVar -> Ct
CFunEqCan { cc_ev :: CtEvidence
cc_ev = CtEvidence
new_ev, cc_fun :: TyCon
cc_fun = TyCon
fam_tc
, cc_tyargs :: [PredType]
cc_tyargs = [PredType]
tc_args, cc_fsk :: EvVar
cc_fsk = EvVar
fsk }
; (WorkList -> WorkList) -> TcS ()
updWorkListTcS (Ct -> WorkList -> WorkList
extendWorkListFunEq Ct
new_ct) }
where
deeper_loc :: CtLoc
deeper_loc = CtLoc -> CtLoc
bumpCtLocDepth (CtEvidence -> CtLoc
ctEvLoc CtEvidence
old_ev)
doTopReactDict :: InertSet -> Ct -> TcS (StopOrContinue Ct)
doTopReactDict :: InertSet -> SimplifierStage
doTopReactDict InertSet
inerts work_item :: Ct
work_item@(CDictCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev, cc_class :: Ct -> Class
cc_class = Class
cls
, cc_tyargs :: Ct -> [PredType]
cc_tyargs = [PredType]
xis })
| CtEvidence -> Bool
isGiven CtEvidence
ev
= do { TcS ()
try_fundep_improvement
; SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
work_item }
| Just CtEvidence
solved_ev <- InertSet -> CtLoc -> Class -> [PredType] -> Maybe CtEvidence
lookupSolvedDict InertSet
inerts CtLoc
dict_loc Class
cls [PredType]
xis
= do { CtEvidence -> EvTerm -> TcS ()
setEvBindIfWanted CtEvidence
ev (CtEvidence -> EvTerm
ctEvTerm CtEvidence
solved_ev)
; CtEvidence -> String -> TcS (StopOrContinue Ct)
forall a. CtEvidence -> String -> TcS (StopOrContinue a)
stopWith CtEvidence
ev String
"Dict/Top (cached)" }
| Bool
otherwise
= do { DynFlags
dflags <- TcS DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
; ClsInstResult
lkup_res <- DynFlags
-> InertSet -> Class -> [PredType] -> CtLoc -> TcS ClsInstResult
matchClassInst DynFlags
dflags InertSet
inerts Class
cls [PredType]
xis CtLoc
dict_loc
; case ClsInstResult
lkup_res of
OneInst { cir_what :: ClsInstResult -> InstanceWhat
cir_what = InstanceWhat
what }
-> do { InstanceWhat -> Ct -> TcS ()
insertSafeOverlapFailureTcS InstanceWhat
what Ct
work_item
; InstanceWhat -> CtEvidence -> Class -> [PredType] -> TcS ()
addSolvedDict InstanceWhat
what CtEvidence
ev Class
cls [PredType]
xis
; Ct -> ClsInstResult -> TcS (StopOrContinue Ct)
chooseInstance Ct
work_item ClsInstResult
lkup_res }
ClsInstResult
_ ->
do { Bool -> TcS () -> TcS ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CtEvidence -> Bool
isImprovable CtEvidence
ev) (TcS () -> TcS ()) -> TcS () -> TcS ()
forall a b. (a -> b) -> a -> b
$
TcS ()
try_fundep_improvement
; SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
work_item } }
where
dict_pred :: PredType
dict_pred = Class -> [PredType] -> PredType
mkClassPred Class
cls [PredType]
xis
dict_loc :: CtLoc
dict_loc = CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev
dict_origin :: CtOrigin
dict_origin = CtLoc -> CtOrigin
ctLocOrigin CtLoc
dict_loc
try_fundep_improvement :: TcS ()
try_fundep_improvement
= do { String -> SDoc -> TcS ()
traceTcS String
"try_fundeps" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
work_item)
; InstEnvs
instEnvs <- TcS InstEnvs
getInstEnvs
; [FunDepEqn CtLoc] -> TcS ()
emitFunDepDeriveds ([FunDepEqn CtLoc] -> TcS ()) -> [FunDepEqn CtLoc] -> TcS ()
forall a b. (a -> b) -> a -> b
$
InstEnvs
-> (PredType -> SrcSpan -> CtLoc) -> PredType -> [FunDepEqn CtLoc]
forall loc.
InstEnvs
-> (PredType -> SrcSpan -> loc) -> PredType -> [FunDepEqn loc]
improveFromInstEnv InstEnvs
instEnvs PredType -> SrcSpan -> CtLoc
mk_ct_loc PredType
dict_pred }
mk_ct_loc :: PredType
-> SrcSpan
-> CtLoc
mk_ct_loc :: PredType -> SrcSpan -> CtLoc
mk_ct_loc PredType
inst_pred SrcSpan
inst_loc
= CtLoc
dict_loc { ctl_origin :: CtOrigin
ctl_origin = PredType -> CtOrigin -> PredType -> SrcSpan -> CtOrigin
FunDepOrigin2 PredType
dict_pred CtOrigin
dict_origin
PredType
inst_pred SrcSpan
inst_loc }
doTopReactDict InertSet
_ Ct
w = String -> SDoc -> TcS (StopOrContinue Ct)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"doTopReactDict" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
w)
chooseInstance :: Ct -> ClsInstResult -> TcS (StopOrContinue Ct)
chooseInstance :: Ct -> ClsInstResult -> TcS (StopOrContinue Ct)
chooseInstance Ct
work_item
(OneInst { cir_new_theta :: ClsInstResult -> [PredType]
cir_new_theta = [PredType]
theta
, cir_what :: ClsInstResult -> InstanceWhat
cir_what = InstanceWhat
what
, cir_mk_ev :: ClsInstResult -> [EvExpr] -> EvTerm
cir_mk_ev = [EvExpr] -> EvTerm
mk_ev })
= do { String -> SDoc -> TcS ()
traceTcS String
"doTopReact/found instance for" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$ CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
ev
; CtLoc
deeper_loc <- CtLoc -> InstanceWhat -> PredType -> TcS CtLoc
checkInstanceOK CtLoc
loc InstanceWhat
what PredType
pred
; if CtEvidence -> Bool
isDerived CtEvidence
ev then CtLoc -> [PredType] -> TcS (StopOrContinue Ct)
forall a. CtLoc -> [PredType] -> TcS (StopOrContinue a)
finish_derived CtLoc
deeper_loc [PredType]
theta
else CtLoc
-> [PredType] -> ([EvExpr] -> EvTerm) -> TcS (StopOrContinue Ct)
finish_wanted CtLoc
deeper_loc [PredType]
theta [EvExpr] -> EvTerm
mk_ev }
where
ev :: CtEvidence
ev = Ct -> CtEvidence
ctEvidence Ct
work_item
pred :: PredType
pred = CtEvidence -> PredType
ctEvPred CtEvidence
ev
loc :: CtLoc
loc = CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev
finish_wanted :: CtLoc -> [TcPredType]
-> ([EvExpr] -> EvTerm) -> TcS (StopOrContinue Ct)
finish_wanted :: CtLoc
-> [PredType] -> ([EvExpr] -> EvTerm) -> TcS (StopOrContinue Ct)
finish_wanted CtLoc
loc [PredType]
theta [EvExpr] -> EvTerm
mk_ev
= do { EvBindsVar
evb <- TcS EvBindsVar
getTcEvBindsVar
; if EvBindsVar -> Bool
isCoEvBindsVar EvBindsVar
evb
then
SimplifierStage
forall a. a -> TcS (StopOrContinue a)
continueWith Ct
work_item
else
do { [MaybeNew]
evc_vars <- (PredType -> TcS MaybeNew) -> [PredType] -> TcS [MaybeNew]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (CtLoc -> PredType -> TcS MaybeNew
newWanted CtLoc
loc) [PredType]
theta
; CtEvidence -> EvTerm -> TcS ()
setEvBindIfWanted CtEvidence
ev ([EvExpr] -> EvTerm
mk_ev ((MaybeNew -> EvExpr) -> [MaybeNew] -> [EvExpr]
forall a b. (a -> b) -> [a] -> [b]
map MaybeNew -> EvExpr
getEvExpr [MaybeNew]
evc_vars))
; [CtEvidence] -> TcS ()
emitWorkNC ([MaybeNew] -> [CtEvidence]
freshGoals [MaybeNew]
evc_vars)
; CtEvidence -> String -> TcS (StopOrContinue Ct)
forall a. CtEvidence -> String -> TcS (StopOrContinue a)
stopWith CtEvidence
ev String
"Dict/Top (solved wanted)" } }
finish_derived :: CtLoc -> [PredType] -> TcS (StopOrContinue a)
finish_derived CtLoc
loc [PredType]
theta
=
do { CtLoc -> [PredType] -> TcS ()
emitNewDeriveds CtLoc
loc [PredType]
theta
; String -> SDoc -> TcS ()
traceTcS String
"finish_derived" (SubGoalDepth -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CtLoc -> SubGoalDepth
ctl_depth CtLoc
loc))
; CtEvidence -> String -> TcS (StopOrContinue a)
forall a. CtEvidence -> String -> TcS (StopOrContinue a)
stopWith CtEvidence
ev String
"Dict/Top (solved derived)" }
chooseInstance Ct
work_item ClsInstResult
lookup_res
= String -> SDoc -> TcS (StopOrContinue Ct)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"chooseInstance" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
work_item SDoc -> SDoc -> SDoc
$$ ClsInstResult -> SDoc
forall a. Outputable a => a -> SDoc
ppr ClsInstResult
lookup_res)
checkInstanceOK :: CtLoc -> InstanceWhat -> TcPredType -> TcS CtLoc
checkInstanceOK :: CtLoc -> InstanceWhat -> PredType -> TcS CtLoc
checkInstanceOK CtLoc
loc InstanceWhat
what PredType
pred
= do { CtLoc -> InstanceWhat -> PredType -> TcS ()
checkWellStagedDFun CtLoc
loc InstanceWhat
what PredType
pred
; CtLoc -> PredType -> TcS ()
checkReductionDepth CtLoc
deeper_loc PredType
pred
; CtLoc -> TcS CtLoc
forall (m :: * -> *) a. Monad m => a -> m a
return CtLoc
deeper_loc }
where
deeper_loc :: CtLoc
deeper_loc = CtLoc -> CtLoc
zap_origin (CtLoc -> CtLoc
bumpCtLocDepth CtLoc
loc)
origin :: CtOrigin
origin = CtLoc -> CtOrigin
ctLocOrigin CtLoc
loc
zap_origin :: CtLoc -> CtLoc
zap_origin CtLoc
loc
| ScOrigin {} <- CtOrigin
origin
= CtLoc -> CtOrigin -> CtLoc
setCtLocOrigin CtLoc
loc (IntWithInf -> CtOrigin
ScOrigin IntWithInf
infinity)
| Bool
otherwise
= CtLoc
loc
matchClassInst :: DynFlags -> InertSet
-> Class -> [Type]
-> CtLoc -> TcS ClsInstResult
matchClassInst :: DynFlags
-> InertSet -> Class -> [PredType] -> CtLoc -> TcS ClsInstResult
matchClassInst DynFlags
dflags InertSet
inerts Class
clas [PredType]
tys CtLoc
loc
| Bool -> Bool
not (Extension -> DynFlags -> Bool
xopt Extension
LangExt.IncoherentInstances DynFlags
dflags)
, Bool -> Bool
not (Class -> Bool
naturallyCoherentClass Class
clas)
, let matchable_givens :: Cts
matchable_givens = CtLoc -> PredType -> InertSet -> Cts
matchableGivens CtLoc
loc PredType
pred InertSet
inerts
, Bool -> Bool
not (Cts -> Bool
forall a. Bag a -> Bool
isEmptyBag Cts
matchable_givens)
= do { String -> SDoc -> TcS ()
traceTcS String
"Delaying instance application" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text String
"Work item=" SDoc -> SDoc -> SDoc
<+> Class -> [PredType] -> SDoc
pprClassPred Class
clas [PredType]
tys
, String -> SDoc
text String
"Potential matching givens:" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
forall a. Outputable a => a -> SDoc
ppr Cts
matchable_givens ]
; ClsInstResult -> TcS ClsInstResult
forall (m :: * -> *) a. Monad m => a -> m a
return ClsInstResult
NotSure }
| Bool
otherwise
= do { String -> SDoc -> TcS ()
traceTcS String
"matchClassInst" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$ String -> SDoc
text String
"pred =" SDoc -> SDoc -> SDoc
<+> PredType -> SDoc
forall a. Outputable a => a -> SDoc
ppr PredType
pred SDoc -> SDoc -> SDoc
<+> Char -> SDoc
char Char
'{'
; ClsInstResult
local_res <- PredType -> CtLoc -> TcS ClsInstResult
matchLocalInst PredType
pred CtLoc
loc
; case ClsInstResult
local_res of
OneInst {} ->
do { String -> SDoc -> TcS ()
traceTcS String
"} matchClassInst local match" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$ ClsInstResult -> SDoc
forall a. Outputable a => a -> SDoc
ppr ClsInstResult
local_res
; ClsInstResult -> TcS ClsInstResult
forall (m :: * -> *) a. Monad m => a -> m a
return ClsInstResult
local_res }
ClsInstResult
NotSure ->
do { String -> SDoc -> TcS ()
traceTcS String
"} matchClassInst local not sure" SDoc
empty
; ClsInstResult -> TcS ClsInstResult
forall (m :: * -> *) a. Monad m => a -> m a
return ClsInstResult
local_res }
ClsInstResult
NoInstance
-> do { ClsInstResult
global_res <- DynFlags -> Bool -> Class -> [PredType] -> TcS ClsInstResult
matchGlobalInst DynFlags
dflags Bool
False Class
clas [PredType]
tys
; String -> SDoc -> TcS ()
traceTcS String
"} matchClassInst global result" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$ ClsInstResult -> SDoc
forall a. Outputable a => a -> SDoc
ppr ClsInstResult
global_res
; ClsInstResult -> TcS ClsInstResult
forall (m :: * -> *) a. Monad m => a -> m a
return ClsInstResult
global_res } }
where
pred :: PredType
pred = Class -> [PredType] -> PredType
mkClassPred Class
clas [PredType]
tys
naturallyCoherentClass :: Class -> Bool
naturallyCoherentClass :: Class -> Bool
naturallyCoherentClass Class
cls
= Class -> Bool
isCTupleClass Class
cls
Bool -> Bool -> Bool
|| Class
cls Class -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
heqTyConKey
Bool -> Bool -> Bool
|| Class
cls Class -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqTyConKey
Bool -> Bool -> Bool
|| Class
cls Class -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
coercibleTyConKey
matchLocalInst :: TcPredType -> CtLoc -> TcS ClsInstResult
matchLocalInst :: PredType -> CtLoc -> TcS ClsInstResult
matchLocalInst PredType
pred CtLoc
loc
= do { InertCans
ics <- TcS InertCans
getInertCans
; case [QCInst] -> ([(CtEvidence, [DFunInstType])], Bool)
match_local_inst (InertCans -> [QCInst]
inert_insts InertCans
ics) of
([], Bool
False) -> ClsInstResult -> TcS ClsInstResult
forall (m :: * -> *) a. Monad m => a -> m a
return ClsInstResult
NoInstance
([(CtEvidence
dfun_ev, [DFunInstType]
inst_tys)], Bool
unifs)
| Bool -> Bool
not Bool
unifs
-> do { let dfun_id :: EvVar
dfun_id = CtEvidence -> EvVar
ctEvEvId CtEvidence
dfun_ev
; ([PredType]
tys, [PredType]
theta) <- EvVar -> [DFunInstType] -> TcS ([PredType], [PredType])
instDFunType EvVar
dfun_id [DFunInstType]
inst_tys
; ClsInstResult -> TcS ClsInstResult
forall (m :: * -> *) a. Monad m => a -> m a
return (ClsInstResult -> TcS ClsInstResult)
-> ClsInstResult -> TcS ClsInstResult
forall a b. (a -> b) -> a -> b
$ OneInst :: [PredType] -> ([EvExpr] -> EvTerm) -> InstanceWhat -> ClsInstResult
OneInst { cir_new_theta :: [PredType]
cir_new_theta = [PredType]
theta
, cir_mk_ev :: [EvExpr] -> EvTerm
cir_mk_ev = EvVar -> [PredType] -> [EvExpr] -> EvTerm
evDFunApp EvVar
dfun_id [PredType]
tys
, cir_what :: InstanceWhat
cir_what = InstanceWhat
LocalInstance } }
([(CtEvidence, [DFunInstType])], Bool)
_ -> ClsInstResult -> TcS ClsInstResult
forall (m :: * -> *) a. Monad m => a -> m a
return ClsInstResult
NotSure }
where
pred_tv_set :: VarSet
pred_tv_set = PredType -> VarSet
tyCoVarsOfType PredType
pred
match_local_inst :: [QCInst]
-> ( [(CtEvidence, [DFunInstType])]
, Bool )
match_local_inst :: [QCInst] -> ([(CtEvidence, [DFunInstType])], Bool)
match_local_inst []
= ([], Bool
False)
match_local_inst (qci :: QCInst
qci@(QCI { qci_tvs :: QCInst -> [EvVar]
qci_tvs = [EvVar]
qtvs, qci_pred :: QCInst -> PredType
qci_pred = PredType
qpred
, qci_ev :: QCInst -> CtEvidence
qci_ev = CtEvidence
ev })
: [QCInst]
qcis)
| let in_scope :: InScopeSet
in_scope = VarSet -> InScopeSet
mkInScopeSet (VarSet
qtv_set VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
pred_tv_set)
, Just VarEnv PredType
tv_subst <- VarSet
-> RnEnv2
-> VarEnv PredType
-> PredType
-> PredType
-> Maybe (VarEnv PredType)
ruleMatchTyKiX VarSet
qtv_set (InScopeSet -> RnEnv2
mkRnEnv2 InScopeSet
in_scope)
VarEnv PredType
emptyTvSubstEnv PredType
qpred PredType
pred
, let match :: (CtEvidence, [DFunInstType])
match = (CtEvidence
ev, (EvVar -> DFunInstType) -> [EvVar] -> [DFunInstType]
forall a b. (a -> b) -> [a] -> [b]
map (VarEnv PredType -> EvVar -> DFunInstType
forall a. VarEnv a -> EvVar -> Maybe a
lookupVarEnv VarEnv PredType
tv_subst) [EvVar]
qtvs)
= ((CtEvidence, [DFunInstType])
match(CtEvidence, [DFunInstType])
-> [(CtEvidence, [DFunInstType])] -> [(CtEvidence, [DFunInstType])]
forall a. a -> [a] -> [a]
:[(CtEvidence, [DFunInstType])]
matches, Bool
unif)
| Bool
otherwise
= ASSERT2( disjointVarSet qtv_set (tyCoVarsOfType pred)
, ppr qci $$ ppr pred )
([(CtEvidence, [DFunInstType])]
matches, Bool
unif Bool -> Bool -> Bool
|| Bool
this_unif)
where
qtv_set :: VarSet
qtv_set = [EvVar] -> VarSet
mkVarSet [EvVar]
qtvs
this_unif :: Bool
this_unif = PredType -> CtLoc -> PredType -> CtLoc -> Bool
mightMatchLater PredType
qpred (CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev) PredType
pred CtLoc
loc
([(CtEvidence, [DFunInstType])]
matches, Bool
unif) = [QCInst] -> ([(CtEvidence, [DFunInstType])], Bool)
match_local_inst [QCInst]
qcis