{-# LANGUAGE CPP #-}
module TcSimplify(
simplifyInfer, InferMode(..),
growThetaTyVars,
simplifyAmbiguityCheck,
simplifyDefault,
simplifyTop, simplifyTopImplic,
simplifyInteractive,
solveEqualities, solveLocalEqualities, solveLocalEqualitiesX,
simplifyWantedsTcM,
tcCheckSatisfiability,
tcNormalise,
captureTopConstraints,
simpl_top,
promoteTyVar,
promoteTyVarSet,
solveWanteds, solveWantedsAndDrop,
approximateWC, runTcSDeriveds
) where
#include "HsVersions.h"
import GhcPrelude
import Bag
import Class ( Class, classKey, classTyCon )
import DynFlags ( WarningFlag ( Opt_WarnMonomorphism )
, WarnReason ( Reason )
, DynFlags( solverIterations ) )
import HsExpr ( UnboundVar(..) )
import Id ( idType, mkLocalId )
import Inst
import ListSetOps
import Name
import Outputable
import PrelInfo
import PrelNames
import RdrName ( emptyGlobalRdrEnv )
import TcErrors
import TcEvidence
import TcInteract
import TcCanonical ( makeSuperClasses, solveCallStack )
import TcMType as TcM
import TcRnMonad as TcM
import TcSMonad as TcS
import TcType
import TrieMap ()
import Type
import TysWiredIn ( liftedRepTy )
import Unify ( tcMatchTyKi )
import Util
import Var
import VarSet
import UniqSet
import BasicTypes ( IntWithInf, intGtLimit )
import ErrUtils ( emptyMessages )
import qualified GHC.LanguageExtensions as LangExt
import Control.Monad
import Data.Foldable ( toList )
import Data.List ( partition )
import Data.List.NonEmpty ( NonEmpty(..) )
import Maybes ( isJust )
captureTopConstraints :: TcM a -> TcM (a, WantedConstraints)
captureTopConstraints :: TcM a -> TcM (a, WantedConstraints)
captureTopConstraints thing_inside :: TcM a
thing_inside
= do { TcRef WantedConstraints
static_wc_var <- WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv (TcRef WantedConstraints)
forall a gbl lcl. a -> TcRnIf gbl lcl (TcRef a)
TcM.newTcRef WantedConstraints
emptyWC ;
; (mb_res :: Either IOEnvFailure a
mb_res, lie :: WantedConstraints
lie) <- (TcGblEnv -> TcGblEnv)
-> TcRnIf
TcGblEnv TcLclEnv (Either IOEnvFailure a, WantedConstraints)
-> TcRnIf
TcGblEnv TcLclEnv (Either IOEnvFailure a, WantedConstraints)
forall gbl lcl a.
(gbl -> gbl) -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
TcM.updGblEnv (\env :: TcGblEnv
env -> TcGblEnv
env { tcg_static_wc :: TcRef WantedConstraints
tcg_static_wc = TcRef WantedConstraints
static_wc_var } ) (TcRnIf
TcGblEnv TcLclEnv (Either IOEnvFailure a, WantedConstraints)
-> TcRnIf
TcGblEnv TcLclEnv (Either IOEnvFailure a, WantedConstraints))
-> TcRnIf
TcGblEnv TcLclEnv (Either IOEnvFailure a, WantedConstraints)
-> TcRnIf
TcGblEnv TcLclEnv (Either IOEnvFailure a, WantedConstraints)
forall a b. (a -> b) -> a -> b
$
TcM a
-> TcRnIf
TcGblEnv TcLclEnv (Either IOEnvFailure a, WantedConstraints)
forall a. TcM a -> TcM (Either IOEnvFailure a, WantedConstraints)
TcM.tryCaptureConstraints TcM a
thing_inside
; WantedConstraints
stWC <- TcRef WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
TcM.readTcRef TcRef WantedConstraints
static_wc_var
; case Either IOEnvFailure a
mb_res of
Right res :: a
res -> (a, WantedConstraints) -> TcM (a, WantedConstraints)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
res, WantedConstraints
lie WantedConstraints -> WantedConstraints -> WantedConstraints
`andWC` WantedConstraints
stWC)
Left {} -> do { Bag EvBind
_ <- WantedConstraints -> TcM (Bag EvBind)
reportUnsolved WantedConstraints
lie; TcM (a, WantedConstraints)
forall env a. IOEnv env a
failM } }
simplifyTopImplic :: Bag Implication -> TcM ()
simplifyTopImplic :: Bag Implication -> TcM ()
simplifyTopImplic implics :: Bag Implication
implics
= do { Bag EvBind
empty_binds <- WantedConstraints -> TcM (Bag EvBind)
simplifyTop (Bag Implication -> WantedConstraints
mkImplicWC Bag Implication
implics)
; MASSERT2( isEmptyBag empty_binds, ppr empty_binds )
; () -> TcM ()
forall (m :: * -> *) a. Monad m => a -> m a
return () }
simplifyTop :: WantedConstraints -> TcM (Bag EvBind)
simplifyTop :: WantedConstraints -> TcM (Bag EvBind)
simplifyTop wanteds :: WantedConstraints
wanteds
= do { String -> SDoc -> TcM ()
traceTc "simplifyTop {" (SDoc -> TcM ()) -> SDoc -> TcM ()
forall a b. (a -> b) -> a -> b
$ String -> SDoc
text "wanted = " SDoc -> SDoc -> SDoc
<+> WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
wanteds
; ((final_wc :: WantedConstraints
final_wc, unsafe_ol :: Cts
unsafe_ol), binds1 :: EvBindMap
binds1) <- TcS (WantedConstraints, Cts)
-> TcM ((WantedConstraints, Cts), EvBindMap)
forall a. TcS a -> TcM (a, EvBindMap)
runTcS (TcS (WantedConstraints, Cts)
-> TcM ((WantedConstraints, Cts), EvBindMap))
-> TcS (WantedConstraints, Cts)
-> TcM ((WantedConstraints, Cts), EvBindMap)
forall a b. (a -> b) -> a -> b
$
do { WantedConstraints
final_wc <- WantedConstraints -> TcS WantedConstraints
simpl_top WantedConstraints
wanteds
; Cts
unsafe_ol <- TcS Cts
getSafeOverlapFailures
; (WantedConstraints, Cts) -> TcS (WantedConstraints, Cts)
forall (m :: * -> *) a. Monad m => a -> m a
return (WantedConstraints
final_wc, Cts
unsafe_ol) }
; String -> SDoc -> TcM ()
traceTc "End simplifyTop }" SDoc
empty
; Bag EvBind
binds2 <- WantedConstraints -> TcM (Bag EvBind)
reportUnsolved WantedConstraints
final_wc
; String -> SDoc -> TcM ()
traceTc "reportUnsolved (unsafe overlapping) {" SDoc
empty
; Bool -> TcM () -> TcM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Cts -> Bool
isEmptyCts Cts
unsafe_ol) (TcM () -> TcM ()) -> TcM () -> TcM ()
forall a b. (a -> b) -> a -> b
$ do {
; TcRef Messages
errs_var <- TcRn (TcRef Messages)
getErrsVar
; Messages
saved_msg <- TcRef Messages -> TcRnIf TcGblEnv TcLclEnv Messages
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
TcM.readTcRef TcRef Messages
errs_var
; TcRef Messages -> Messages -> TcM ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
TcM.writeTcRef TcRef Messages
errs_var Messages
emptyMessages
; WantedConstraints -> TcM ()
warnAllUnsolved (WantedConstraints -> TcM ()) -> WantedConstraints -> TcM ()
forall a b. (a -> b) -> a -> b
$ WC :: Cts -> Bag Implication -> WantedConstraints
WC { wc_simple :: Cts
wc_simple = Cts
unsafe_ol
, wc_impl :: Bag Implication
wc_impl = Bag Implication
forall a. Bag a
emptyBag }
; WarningMessages
whyUnsafe <- Messages -> WarningMessages
forall a b. (a, b) -> a
fst (Messages -> WarningMessages)
-> TcRnIf TcGblEnv TcLclEnv Messages
-> IOEnv (Env TcGblEnv TcLclEnv) WarningMessages
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRef Messages -> TcRnIf TcGblEnv TcLclEnv Messages
forall a gbl lcl. TcRef a -> TcRnIf gbl lcl a
TcM.readTcRef TcRef Messages
errs_var
; TcRef Messages -> Messages -> TcM ()
forall a gbl lcl. TcRef a -> a -> TcRnIf gbl lcl ()
TcM.writeTcRef TcRef Messages
errs_var Messages
saved_msg
; WarningMessages -> TcM ()
recordUnsafeInfer WarningMessages
whyUnsafe
}
; String -> SDoc -> TcM ()
traceTc "reportUnsolved (unsafe overlapping) }" SDoc
empty
; Bag EvBind -> TcM (Bag EvBind)
forall (m :: * -> *) a. Monad m => a -> m a
return (EvBindMap -> Bag EvBind
evBindMapBinds EvBindMap
binds1 Bag EvBind -> Bag EvBind -> Bag EvBind
forall a. Bag a -> Bag a -> Bag a
`unionBags` Bag EvBind
binds2) }
solveLocalEqualities :: String -> TcM a -> TcM a
solveLocalEqualities :: String -> TcM a -> TcM a
solveLocalEqualities callsite :: String
callsite thing_inside :: TcM a
thing_inside
= do { (wanted :: WantedConstraints
wanted, res :: a
res) <- String -> TcM a -> TcM (WantedConstraints, a)
forall a. String -> TcM a -> TcM (WantedConstraints, a)
solveLocalEqualitiesX String
callsite TcM a
thing_inside
; WantedConstraints -> TcM ()
emitConstraints WantedConstraints
wanted
; Bool -> TcM () -> TcM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (WantedConstraints -> Bool
insolubleWC WantedConstraints
wanted) (TcM () -> TcM ()) -> TcM () -> TcM ()
forall a b. (a -> b) -> a -> b
$
TcM ()
forall env a. IOEnv env a
failM
; a -> TcM a
forall (m :: * -> *) a. Monad m => a -> m a
return a
res }
solveLocalEqualitiesX :: String -> TcM a -> TcM (WantedConstraints, a)
solveLocalEqualitiesX :: String -> TcM a -> TcM (WantedConstraints, a)
solveLocalEqualitiesX callsite :: String
callsite thing_inside :: TcM a
thing_inside
= do { String -> SDoc -> TcM ()
traceTc "solveLocalEqualitiesX {" ([SDoc] -> SDoc
vcat [ String -> SDoc
text "Called from" SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
callsite ])
; (result :: a
result, wanted :: WantedConstraints
wanted) <- TcM a -> TcM (a, WantedConstraints)
forall a. TcM a -> TcM (a, WantedConstraints)
captureConstraints TcM a
thing_inside
; String -> SDoc -> TcM ()
traceTc "solveLocalEqualities: running solver" (WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
wanted)
; WantedConstraints
residual_wanted <- TcS WantedConstraints -> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall a. TcS a -> TcM a
runTcSEqualities (WantedConstraints -> TcS WantedConstraints
solveWanteds WantedConstraints
wanted)
; String -> SDoc -> TcM ()
traceTc "solveLocalEqualitiesX end }" (SDoc -> TcM ()) -> SDoc -> TcM ()
forall a b. (a -> b) -> a -> b
$
String -> SDoc
text "residual_wanted =" SDoc -> SDoc -> SDoc
<+> WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
residual_wanted
; (WantedConstraints, a) -> TcM (WantedConstraints, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (WantedConstraints
residual_wanted, a
result) }
solveEqualities :: TcM a -> TcM a
solveEqualities :: TcM a -> TcM a
solveEqualities thing_inside :: TcM a
thing_inside
= TcM a -> TcM a
forall r. TcM r -> TcM r
checkNoErrs (TcM a -> TcM a) -> TcM a -> TcM a
forall a b. (a -> b) -> a -> b
$
do { TcLevel
lvl <- TcM TcLevel
TcM.getTcLevel
; String -> SDoc -> TcM ()
traceTc "solveEqualities {" (String -> SDoc
text "level =" SDoc -> SDoc -> SDoc
<+> TcLevel -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcLevel
lvl)
; (result :: a
result, wanted :: WantedConstraints
wanted) <- TcM a -> TcM (a, WantedConstraints)
forall a. TcM a -> TcM (a, WantedConstraints)
captureConstraints TcM a
thing_inside
; String -> SDoc -> TcM ()
traceTc "solveEqualities: running solver" (SDoc -> TcM ()) -> SDoc -> TcM ()
forall a b. (a -> b) -> a -> b
$ String -> SDoc
text "wanted = " SDoc -> SDoc -> SDoc
<+> WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
wanted
; WantedConstraints
final_wc <- TcS WantedConstraints -> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall a. TcS a -> TcM a
runTcSEqualities (TcS WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints)
-> TcS WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall a b. (a -> b) -> a -> b
$ WantedConstraints -> TcS WantedConstraints
simpl_top WantedConstraints
wanted
; String -> SDoc -> TcM ()
traceTc "End solveEqualities }" SDoc
empty
; WantedConstraints -> TcM ()
reportAllUnsolved WantedConstraints
final_wc
; a -> TcM a
forall (m :: * -> *) a. Monad m => a -> m a
return a
result }
simpl_top :: WantedConstraints -> TcS WantedConstraints
simpl_top :: WantedConstraints -> TcS WantedConstraints
simpl_top wanteds :: WantedConstraints
wanteds
= do { WantedConstraints
wc_first_go <- TcS WantedConstraints -> TcS WantedConstraints
forall a. TcS a -> TcS a
nestTcS (WantedConstraints -> TcS WantedConstraints
solveWantedsAndDrop WantedConstraints
wanteds)
; WantedConstraints -> TcS WantedConstraints
try_tyvar_defaulting WantedConstraints
wc_first_go }
where
try_tyvar_defaulting :: WantedConstraints -> TcS WantedConstraints
try_tyvar_defaulting :: WantedConstraints -> TcS WantedConstraints
try_tyvar_defaulting wc :: WantedConstraints
wc
| WantedConstraints -> Bool
isEmptyWC WantedConstraints
wc
= WantedConstraints -> TcS WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return WantedConstraints
wc
| Bool
otherwise
= do { [TcTyCoVar]
free_tvs <- [TcTyCoVar] -> TcS [TcTyCoVar]
TcS.zonkTyCoVarsAndFVList (WantedConstraints -> [TcTyCoVar]
tyCoVarsOfWCList WantedConstraints
wc)
; let meta_tvs :: [TcTyCoVar]
meta_tvs = (TcTyCoVar -> Bool) -> [TcTyCoVar] -> [TcTyCoVar]
forall a. (a -> Bool) -> [a] -> [a]
filter (TcTyCoVar -> Bool
isTyVar (TcTyCoVar -> Bool) -> (TcTyCoVar -> Bool) -> TcTyCoVar -> Bool
forall (f :: * -> *). Applicative f => f Bool -> f Bool -> f Bool
<&&> TcTyCoVar -> Bool
isMetaTyVar) [TcTyCoVar]
free_tvs
; [Bool]
defaulted <- (TcTyCoVar -> TcS Bool) -> [TcTyCoVar] -> TcS [Bool]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TcTyCoVar -> TcS Bool
defaultTyVarTcS [TcTyCoVar]
meta_tvs
; if [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Bool]
defaulted
then do { WantedConstraints
wc_residual <- TcS WantedConstraints -> TcS WantedConstraints
forall a. TcS a -> TcS a
nestTcS (WantedConstraints -> TcS WantedConstraints
solveWanteds WantedConstraints
wc)
; WantedConstraints -> TcS WantedConstraints
try_class_defaulting WantedConstraints
wc_residual }
else WantedConstraints -> TcS WantedConstraints
try_class_defaulting WantedConstraints
wc }
try_class_defaulting :: WantedConstraints -> TcS WantedConstraints
try_class_defaulting :: WantedConstraints -> TcS WantedConstraints
try_class_defaulting wc :: WantedConstraints
wc
| WantedConstraints -> Bool
isEmptyWC WantedConstraints
wc
= WantedConstraints -> TcS WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return WantedConstraints
wc
| Bool
otherwise
= do { Bool
something_happened <- WantedConstraints -> TcS Bool
applyDefaultingRules WantedConstraints
wc
; if Bool
something_happened
then do { WantedConstraints
wc_residual <- TcS WantedConstraints -> TcS WantedConstraints
forall a. TcS a -> TcS a
nestTcS (WantedConstraints -> TcS WantedConstraints
solveWantedsAndDrop WantedConstraints
wc)
; WantedConstraints -> TcS WantedConstraints
try_class_defaulting WantedConstraints
wc_residual }
else WantedConstraints -> TcS WantedConstraints
try_callstack_defaulting WantedConstraints
wc }
try_callstack_defaulting :: WantedConstraints -> TcS WantedConstraints
try_callstack_defaulting :: WantedConstraints -> TcS WantedConstraints
try_callstack_defaulting wc :: WantedConstraints
wc
| WantedConstraints -> Bool
isEmptyWC WantedConstraints
wc
= WantedConstraints -> TcS WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return WantedConstraints
wc
| Bool
otherwise
= WantedConstraints -> TcS WantedConstraints
defaultCallStacks WantedConstraints
wc
defaultCallStacks :: WantedConstraints -> TcS WantedConstraints
defaultCallStacks :: WantedConstraints -> TcS WantedConstraints
defaultCallStacks wanteds :: WantedConstraints
wanteds
= do Cts
simples <- Cts -> TcS Cts
handle_simples (WantedConstraints -> Cts
wc_simple WantedConstraints
wanteds)
Bag (Maybe Implication)
mb_implics <- (Implication -> TcS (Maybe Implication))
-> Bag Implication -> TcS (Bag (Maybe Implication))
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Bag a -> m (Bag b)
mapBagM Implication -> TcS (Maybe Implication)
handle_implic (WantedConstraints -> Bag Implication
wc_impl WantedConstraints
wanteds)
WantedConstraints -> TcS WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return (WantedConstraints
wanteds { wc_simple :: Cts
wc_simple = Cts
simples
, wc_impl :: Bag Implication
wc_impl = Bag (Maybe Implication) -> Bag Implication
forall a. Bag (Maybe a) -> Bag a
catBagMaybes Bag (Maybe Implication)
mb_implics })
where
handle_simples :: Cts -> TcS Cts
handle_simples simples :: Cts
simples
= Bag (Maybe Ct) -> Cts
forall a. Bag (Maybe a) -> Bag a
catBagMaybes (Bag (Maybe Ct) -> Cts) -> TcS (Bag (Maybe Ct)) -> TcS Cts
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Ct -> TcS (Maybe Ct)) -> Cts -> TcS (Bag (Maybe Ct))
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Bag a -> m (Bag b)
mapBagM Ct -> TcS (Maybe Ct)
defaultCallStack Cts
simples
handle_implic :: Implication -> TcS (Maybe Implication)
handle_implic :: Implication -> TcS (Maybe Implication)
handle_implic implic :: Implication
implic
| ImplicStatus -> Bool
isSolvedStatus (Implication -> ImplicStatus
ic_status Implication
implic)
= Maybe Implication -> TcS (Maybe Implication)
forall (m :: * -> *) a. Monad m => a -> m a
return (Implication -> Maybe Implication
forall a. a -> Maybe a
Just Implication
implic)
| Bool
otherwise
= do { WantedConstraints
wanteds <- EvBindsVar -> TcS WantedConstraints -> TcS WantedConstraints
forall a. EvBindsVar -> TcS a -> TcS a
setEvBindsTcS (Implication -> EvBindsVar
ic_binds Implication
implic) (TcS WantedConstraints -> TcS WantedConstraints)
-> TcS WantedConstraints -> TcS WantedConstraints
forall a b. (a -> b) -> a -> b
$
WantedConstraints -> TcS WantedConstraints
defaultCallStacks (Implication -> WantedConstraints
ic_wanted Implication
implic)
; Implication -> TcS (Maybe Implication)
setImplicationStatus (Implication
implic { ic_wanted :: WantedConstraints
ic_wanted = WantedConstraints
wanteds }) }
defaultCallStack :: Ct -> TcS (Maybe Ct)
defaultCallStack ct :: Ct
ct
| ClassPred cls :: Class
cls tys :: [Type]
tys <- Type -> PredTree
classifyPredType (Ct -> Type
ctPred Ct
ct)
, Just {} <- Class -> [Type] -> Maybe FastString
isCallStackPred Class
cls [Type]
tys
= do { CtEvidence -> EvCallStack -> TcS ()
solveCallStack (Ct -> CtEvidence
ctEvidence Ct
ct) EvCallStack
EvCsEmpty
; Maybe Ct -> TcS (Maybe Ct)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Ct
forall a. Maybe a
Nothing }
defaultCallStack ct :: Ct
ct
= Maybe Ct -> TcS (Maybe Ct)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ct -> Maybe Ct
forall a. a -> Maybe a
Just Ct
ct)
simplifyAmbiguityCheck :: Type -> WantedConstraints -> TcM ()
simplifyAmbiguityCheck :: Type -> WantedConstraints -> TcM ()
simplifyAmbiguityCheck ty :: Type
ty wanteds :: WantedConstraints
wanteds
= do { String -> SDoc -> TcM ()
traceTc "simplifyAmbiguityCheck {" (String -> SDoc
text "type = " SDoc -> SDoc -> SDoc
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty SDoc -> SDoc -> SDoc
$$ String -> SDoc
text "wanted = " SDoc -> SDoc -> SDoc
<+> WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
wanteds)
; (final_wc :: WantedConstraints
final_wc, _) <- TcS WantedConstraints -> TcM (WantedConstraints, EvBindMap)
forall a. TcS a -> TcM (a, EvBindMap)
runTcS (TcS WantedConstraints -> TcM (WantedConstraints, EvBindMap))
-> TcS WantedConstraints -> TcM (WantedConstraints, EvBindMap)
forall a b. (a -> b) -> a -> b
$ WantedConstraints -> TcS WantedConstraints
solveWantedsAndDrop WantedConstraints
wanteds
; String -> SDoc -> TcM ()
traceTc "End simplifyAmbiguityCheck }" SDoc
empty
; Bool
allow_ambiguous <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.AllowAmbiguousTypes
; String -> SDoc -> TcM ()
traceTc "reportUnsolved(ambig) {" SDoc
empty
; Bool -> TcM () -> TcM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool
allow_ambiguous Bool -> Bool -> Bool
&& Bool -> Bool
not (WantedConstraints -> Bool
insolubleWC WantedConstraints
final_wc))
(TcM (Bag EvBind) -> TcM ()
forall a. TcM a -> TcM ()
discardResult (WantedConstraints -> TcM (Bag EvBind)
reportUnsolved WantedConstraints
final_wc))
; String -> SDoc -> TcM ()
traceTc "reportUnsolved(ambig) }" SDoc
empty
; () -> TcM ()
forall (m :: * -> *) a. Monad m => a -> m a
return () }
simplifyInteractive :: WantedConstraints -> TcM (Bag EvBind)
simplifyInteractive :: WantedConstraints -> TcM (Bag EvBind)
simplifyInteractive wanteds :: WantedConstraints
wanteds
= String -> SDoc -> TcM ()
traceTc "simplifyInteractive" SDoc
empty TcM () -> TcM (Bag EvBind) -> TcM (Bag EvBind)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
WantedConstraints -> TcM (Bag EvBind)
simplifyTop WantedConstraints
wanteds
simplifyDefault :: ThetaType
-> TcM ()
simplifyDefault :: [Type] -> TcM ()
simplifyDefault theta :: [Type]
theta
= do { String -> SDoc -> TcM ()
traceTc "simplifyDefault" SDoc
empty
; [CtEvidence]
wanteds <- CtOrigin -> [Type] -> TcM [CtEvidence]
newWanteds CtOrigin
DefaultOrigin [Type]
theta
; WantedConstraints
unsolved <- TcS WantedConstraints -> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall a. TcS a -> TcM a
runTcSDeriveds (WantedConstraints -> TcS WantedConstraints
solveWantedsAndDrop ([CtEvidence] -> WantedConstraints
mkSimpleWC [CtEvidence]
wanteds))
; WantedConstraints -> TcM ()
reportAllUnsolved WantedConstraints
unsolved
; () -> TcM ()
forall (m :: * -> *) a. Monad m => a -> m a
return () }
tcCheckSatisfiability :: Bag EvVar -> TcM Bool
tcCheckSatisfiability :: Bag TcTyCoVar -> TcRnIf TcGblEnv TcLclEnv Bool
tcCheckSatisfiability given_ids :: Bag TcTyCoVar
given_ids
= do { TcLclEnv
lcl_env <- TcRnIf TcGblEnv TcLclEnv TcLclEnv
forall gbl lcl. TcRnIf gbl lcl lcl
TcM.getLclEnv
; let given_loc :: CtLoc
given_loc = TcLevel -> SkolemInfo -> TcLclEnv -> CtLoc
mkGivenLoc TcLevel
topTcLevel SkolemInfo
UnkSkol TcLclEnv
lcl_env
; (res :: Bool
res, _ev_binds :: EvBindMap
_ev_binds) <- TcS Bool -> TcM (Bool, EvBindMap)
forall a. TcS a -> TcM (a, EvBindMap)
runTcS (TcS Bool -> TcM (Bool, EvBindMap))
-> TcS Bool -> TcM (Bool, EvBindMap)
forall a b. (a -> b) -> a -> b
$
do { String -> SDoc -> TcS ()
traceTcS "checkSatisfiability {" (Bag TcTyCoVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bag TcTyCoVar
given_ids)
; let given_cts :: [Ct]
given_cts = CtLoc -> [TcTyCoVar] -> [Ct]
mkGivens CtLoc
given_loc (Bag TcTyCoVar -> [TcTyCoVar]
forall a. Bag a -> [a]
bagToList Bag TcTyCoVar
given_ids)
; [Ct] -> TcS ()
solveSimpleGivens [Ct]
given_cts
; Cts
insols <- TcS Cts
getInertInsols
; Cts
insols <- Cts -> TcS Cts
try_harder Cts
insols
; String -> SDoc -> TcS ()
traceTcS "checkSatisfiability }" (Cts -> SDoc
forall a. Outputable a => a -> SDoc
ppr Cts
insols)
; Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Cts -> Bool
forall a. Bag a -> Bool
isEmptyBag Cts
insols) }
; Bool -> TcRnIf TcGblEnv TcLclEnv Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
res }
where
try_harder :: Cts -> TcS Cts
try_harder :: Cts -> TcS Cts
try_harder insols :: Cts
insols
| Bool -> Bool
not (Cts -> Bool
forall a. Bag a -> Bool
isEmptyBag Cts
insols)
= Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return Cts
insols
| Bool
otherwise
= do { [Ct]
pending_given <- TcS [Ct]
getPendingGivenScs
; [Ct]
new_given <- [Ct] -> TcS [Ct]
makeSuperClasses [Ct]
pending_given
; [Ct] -> TcS ()
solveSimpleGivens [Ct]
new_given
; TcS Cts
getInertInsols }
tcNormalise :: Bag EvVar -> Type -> TcM Type
tcNormalise :: Bag TcTyCoVar -> Type -> TcM Type
tcNormalise given_ids :: Bag TcTyCoVar
given_ids ty :: Type
ty
= do { TcLclEnv
lcl_env <- TcRnIf TcGblEnv TcLclEnv TcLclEnv
forall gbl lcl. TcRnIf gbl lcl lcl
TcM.getLclEnv
; let given_loc :: CtLoc
given_loc = TcLevel -> SkolemInfo -> TcLclEnv -> CtLoc
mkGivenLoc TcLevel
topTcLevel SkolemInfo
UnkSkol TcLclEnv
lcl_env
; Ct
wanted_ct <- TcM Ct
mk_wanted_ct
; (res :: Type
res, _ev_binds :: EvBindMap
_ev_binds) <- TcS Type -> TcM (Type, EvBindMap)
forall a. TcS a -> TcM (a, EvBindMap)
runTcS (TcS Type -> TcM (Type, EvBindMap))
-> TcS Type -> TcM (Type, EvBindMap)
forall a b. (a -> b) -> a -> b
$
do { String -> SDoc -> TcS ()
traceTcS "tcNormalise {" (Bag TcTyCoVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bag TcTyCoVar
given_ids)
; let given_cts :: [Ct]
given_cts = CtLoc -> [TcTyCoVar] -> [Ct]
mkGivens CtLoc
given_loc (Bag TcTyCoVar -> [TcTyCoVar]
forall a. Bag a -> [a]
bagToList Bag TcTyCoVar
given_ids)
; [Ct] -> TcS ()
solveSimpleGivens [Ct]
given_cts
; WantedConstraints
wcs <- Cts -> TcS WantedConstraints
solveSimpleWanteds (Ct -> Cts
forall a. a -> Bag a
unitBag Ct
wanted_ct)
; let ty' :: Type
ty' = case Cts -> [Ct]
forall a. Bag a -> [a]
bagToList (WantedConstraints -> Cts
wc_simple WantedConstraints
wcs) of
(ct :: Ct
ct:_) -> CtEvidence -> Type
ctEvPred (Ct -> CtEvidence
ctEvidence Ct
ct)
cts :: [Ct]
cts -> String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic "tcNormalise" ([Ct] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Ct]
cts)
; String -> SDoc -> TcS ()
traceTcS "tcNormalise }" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty')
; Type -> TcS Type
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
ty' }
; Type -> TcM Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
res }
where
mk_wanted_ct :: TcM Ct
mk_wanted_ct :: TcM Ct
mk_wanted_ct = do
let occ :: OccName
occ = String -> OccName
mkVarOcc "$tcNorm"
Name
name <- OccName -> TcRnIf TcGblEnv TcLclEnv Name
forall gbl lcl. OccName -> TcRnIf gbl lcl Name
newSysName OccName
occ
let ev :: TcTyCoVar
ev = Name -> Type -> TcTyCoVar
mkLocalId Name
name Type
ty
hole :: Hole
hole = UnboundVar -> Hole
ExprHole (UnboundVar -> Hole) -> UnboundVar -> Hole
forall a b. (a -> b) -> a -> b
$ OccName -> GlobalRdrEnv -> UnboundVar
OutOfScope OccName
occ GlobalRdrEnv
emptyGlobalRdrEnv
Hole -> TcTyCoVar -> Type -> TcM Ct
newHoleCt Hole
hole TcTyCoVar
ev Type
ty
data InferMode = ApplyMR
| EagerDefaulting
| NoRestrictions
instance Outputable InferMode where
ppr :: InferMode -> SDoc
ppr ApplyMR = String -> SDoc
text "ApplyMR"
ppr EagerDefaulting = String -> SDoc
text "EagerDefaulting"
ppr NoRestrictions = String -> SDoc
text "NoRestrictions"
simplifyInfer :: TcLevel
-> InferMode
-> [TcIdSigInst]
-> [(Name, TcTauType)]
-> WantedConstraints
-> TcM ([TcTyVar],
[EvVar],
TcEvBinds,
WantedConstraints,
Bool)
simplifyInfer :: TcLevel
-> InferMode
-> [TcIdSigInst]
-> [(Name, Type)]
-> WantedConstraints
-> TcM
([TcTyCoVar], [TcTyCoVar], TcEvBinds, WantedConstraints, Bool)
simplifyInfer rhs_tclvl :: TcLevel
rhs_tclvl infer_mode :: InferMode
infer_mode sigs :: [TcIdSigInst]
sigs name_taus :: [(Name, Type)]
name_taus wanteds :: WantedConstraints
wanteds
| WantedConstraints -> Bool
isEmptyWC WantedConstraints
wanteds
= do {
let psig_tv_tys :: [Type]
psig_tv_tys = [ TcTyCoVar -> Type
mkTyVarTy TcTyCoVar
tv | TcIdSigInst
sig <- [TcIdSigInst]
partial_sigs
, (_,tv :: TcTyCoVar
tv) <- TcIdSigInst -> [(Name, TcTyCoVar)]
sig_inst_skols TcIdSigInst
sig ]
psig_theta :: [Type]
psig_theta = [ Type
pred | TcIdSigInst
sig <- [TcIdSigInst]
partial_sigs
, Type
pred <- TcIdSigInst -> [Type]
sig_inst_theta TcIdSigInst
sig ]
; TcTyVarSet
gbl_tvs <- TcM TcTyVarSet
tcGetGlobalTyCoVars
; CandidatesQTvs
dep_vars <- [Type] -> TcM CandidatesQTvs
candidateQTyVarsOfTypes ([Type]
psig_tv_tys [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
psig_theta [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ ((Name, Type) -> Type) -> [(Name, Type)] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Type) -> Type
forall a b. (a, b) -> b
snd [(Name, Type)]
name_taus)
; [TcTyCoVar]
qtkvs <- TcTyVarSet -> CandidatesQTvs -> TcM [TcTyCoVar]
quantifyTyVars TcTyVarSet
gbl_tvs CandidatesQTvs
dep_vars
; String -> SDoc -> TcM ()
traceTc "simplifyInfer: empty WC" ([(Name, Type)] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [(Name, Type)]
name_taus SDoc -> SDoc -> SDoc
$$ [TcTyCoVar] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcTyCoVar]
qtkvs)
; ([TcTyCoVar], [TcTyCoVar], TcEvBinds, WantedConstraints, Bool)
-> TcM
([TcTyCoVar], [TcTyCoVar], TcEvBinds, WantedConstraints, Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return ([TcTyCoVar]
qtkvs, [], TcEvBinds
emptyTcEvBinds, WantedConstraints
emptyWC, Bool
False) }
| Bool
otherwise
= do { String -> SDoc -> TcM ()
traceTc "simplifyInfer {" (SDoc -> TcM ()) -> SDoc -> TcM ()
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat
[ String -> SDoc
text "sigs =" SDoc -> SDoc -> SDoc
<+> [TcIdSigInst] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcIdSigInst]
sigs
, String -> SDoc
text "binds =" SDoc -> SDoc -> SDoc
<+> [(Name, Type)] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [(Name, Type)]
name_taus
, String -> SDoc
text "rhs_tclvl =" SDoc -> SDoc -> SDoc
<+> TcLevel -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcLevel
rhs_tclvl
, String -> SDoc
text "infer_mode =" SDoc -> SDoc -> SDoc
<+> InferMode -> SDoc
forall a. Outputable a => a -> SDoc
ppr InferMode
infer_mode
, String -> SDoc
text "(unzonked) wanted =" SDoc -> SDoc -> SDoc
<+> WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
wanteds
]
; let psig_theta :: [Type]
psig_theta = (TcIdSigInst -> [Type]) -> [TcIdSigInst] -> [Type]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap TcIdSigInst -> [Type]
sig_inst_theta [TcIdSigInst]
partial_sigs
; Env TcGblEnv TcLclEnv
tc_env <- IOEnv (Env TcGblEnv TcLclEnv) (Env TcGblEnv TcLclEnv)
forall env. IOEnv env env
TcM.getEnv
; EvBindsVar
ev_binds_var <- TcM EvBindsVar
TcM.newTcEvBinds
; [TcTyCoVar]
psig_theta_vars <- (Type -> IOEnv (Env TcGblEnv TcLclEnv) TcTyCoVar)
-> [Type] -> TcM [TcTyCoVar]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> IOEnv (Env TcGblEnv TcLclEnv) TcTyCoVar
forall gbl lcl. Type -> TcRnIf gbl lcl TcTyCoVar
TcM.newEvVar [Type]
psig_theta
; WantedConstraints
wanted_transformed_incl_derivs
<- TcLevel
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall a. TcLevel -> TcM a -> TcM a
setTcLevel TcLevel
rhs_tclvl (TcRnIf TcGblEnv TcLclEnv WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints)
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall a b. (a -> b) -> a -> b
$
EvBindsVar
-> TcS WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall a. EvBindsVar -> TcS a -> TcM a
runTcSWithEvBinds EvBindsVar
ev_binds_var (TcS WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints)
-> TcS WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall a b. (a -> b) -> a -> b
$
do { let loc :: CtLoc
loc = TcLevel -> SkolemInfo -> TcLclEnv -> CtLoc
mkGivenLoc TcLevel
rhs_tclvl SkolemInfo
UnkSkol (TcLclEnv -> CtLoc) -> TcLclEnv -> CtLoc
forall a b. (a -> b) -> a -> b
$
Env TcGblEnv TcLclEnv -> TcLclEnv
forall gbl lcl. Env gbl lcl -> lcl
env_lcl Env TcGblEnv TcLclEnv
tc_env
psig_givens :: [Ct]
psig_givens = CtLoc -> [TcTyCoVar] -> [Ct]
mkGivens CtLoc
loc [TcTyCoVar]
psig_theta_vars
; ()
_ <- [Ct] -> TcS ()
solveSimpleGivens [Ct]
psig_givens
; WantedConstraints -> TcS WantedConstraints
solveWanteds WantedConstraints
wanteds }
; WantedConstraints
wanted_transformed_incl_derivs <- WantedConstraints -> TcRnIf TcGblEnv TcLclEnv WantedConstraints
TcM.zonkWC WantedConstraints
wanted_transformed_incl_derivs
; let definite_error :: Bool
definite_error = WantedConstraints -> Bool
insolubleWC WantedConstraints
wanted_transformed_incl_derivs
wanted_transformed :: WantedConstraints
wanted_transformed = WantedConstraints -> WantedConstraints
dropDerivedWC WantedConstraints
wanted_transformed_incl_derivs
quant_pred_candidates :: [Type]
quant_pred_candidates
| Bool
definite_error = []
| Bool
otherwise = Cts -> [Type]
ctsPreds (Bool -> WantedConstraints -> Cts
approximateWC Bool
False WantedConstraints
wanted_transformed)
; (qtvs :: [TcTyCoVar]
qtvs, bound_theta :: [Type]
bound_theta, co_vars :: TcTyVarSet
co_vars) <- InferMode
-> TcLevel
-> [(Name, Type)]
-> [TcIdSigInst]
-> [Type]
-> TcM ([TcTyCoVar], [Type], TcTyVarSet)
decideQuantification InferMode
infer_mode TcLevel
rhs_tclvl
[(Name, Type)]
name_taus [TcIdSigInst]
partial_sigs
[Type]
quant_pred_candidates
; [TcTyCoVar]
bound_theta_vars <- (Type -> IOEnv (Env TcGblEnv TcLclEnv) TcTyCoVar)
-> [Type] -> TcM [TcTyCoVar]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> IOEnv (Env TcGblEnv TcLclEnv) TcTyCoVar
forall gbl lcl. Type -> TcRnIf gbl lcl TcTyCoVar
TcM.newEvVar [Type]
bound_theta
; CtLoc
ct_loc <- CtOrigin -> Maybe TypeOrKind -> TcM CtLoc
getCtLocM CtOrigin
AnnOrigin Maybe TypeOrKind
forall a. Maybe a
Nothing
; let psig_wanted :: [CtEvidence]
psig_wanted = [ CtWanted :: Type -> TcEvDest -> ShadowInfo -> CtLoc -> CtEvidence
CtWanted { ctev_pred :: Type
ctev_pred = TcTyCoVar -> Type
idType TcTyCoVar
psig_theta_var
, ctev_dest :: TcEvDest
ctev_dest = TcTyCoVar -> TcEvDest
EvVarDest TcTyCoVar
psig_theta_var
, ctev_nosh :: ShadowInfo
ctev_nosh = ShadowInfo
WDeriv
, ctev_loc :: CtLoc
ctev_loc = CtLoc
ct_loc }
| TcTyCoVar
psig_theta_var <- [TcTyCoVar]
psig_theta_vars ]
; WantedConstraints
residual_wanted <- TcLevel
-> Env TcGblEnv TcLclEnv
-> EvBindsVar
-> [(Name, Type)]
-> TcTyVarSet
-> [TcTyCoVar]
-> [TcTyCoVar]
-> WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
mkResidualConstraints TcLevel
rhs_tclvl Env TcGblEnv TcLclEnv
tc_env EvBindsVar
ev_binds_var
[(Name, Type)]
name_taus TcTyVarSet
co_vars [TcTyCoVar]
qtvs [TcTyCoVar]
bound_theta_vars
(WantedConstraints
wanted_transformed WantedConstraints -> WantedConstraints -> WantedConstraints
`andWC` [CtEvidence] -> WantedConstraints
mkSimpleWC [CtEvidence]
psig_wanted)
; String -> SDoc -> TcM ()
traceTc "} simplifyInfer/produced residual implication for quantification" (SDoc -> TcM ()) -> SDoc -> TcM ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text "quant_pred_candidates =" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
quant_pred_candidates
, String -> SDoc
text "psig_theta =" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
psig_theta
, String -> SDoc
text "bound_theta =" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
bound_theta
, String -> SDoc
text "qtvs =" SDoc -> SDoc -> SDoc
<+> [TcTyCoVar] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcTyCoVar]
qtvs
, String -> SDoc
text "definite_error =" SDoc -> SDoc -> SDoc
<+> Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bool
definite_error ]
; ([TcTyCoVar], [TcTyCoVar], TcEvBinds, WantedConstraints, Bool)
-> TcM
([TcTyCoVar], [TcTyCoVar], TcEvBinds, WantedConstraints, Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return ( [TcTyCoVar]
qtvs, [TcTyCoVar]
bound_theta_vars, EvBindsVar -> TcEvBinds
TcEvBinds EvBindsVar
ev_binds_var
, WantedConstraints
residual_wanted, Bool
definite_error ) }
where
partial_sigs :: [TcIdSigInst]
partial_sigs = (TcIdSigInst -> Bool) -> [TcIdSigInst] -> [TcIdSigInst]
forall a. (a -> Bool) -> [a] -> [a]
filter TcIdSigInst -> Bool
isPartialSig [TcIdSigInst]
sigs
mkResidualConstraints :: TcLevel -> Env TcGblEnv TcLclEnv -> EvBindsVar
-> [(Name, TcTauType)]
-> VarSet -> [TcTyVar] -> [EvVar]
-> WantedConstraints -> TcM WantedConstraints
mkResidualConstraints :: TcLevel
-> Env TcGblEnv TcLclEnv
-> EvBindsVar
-> [(Name, Type)]
-> TcTyVarSet
-> [TcTyCoVar]
-> [TcTyCoVar]
-> WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
mkResidualConstraints rhs_tclvl :: TcLevel
rhs_tclvl tc_env :: Env TcGblEnv TcLclEnv
tc_env ev_binds_var :: EvBindsVar
ev_binds_var
name_taus :: [(Name, Type)]
name_taus co_vars :: TcTyVarSet
co_vars qtvs :: [TcTyCoVar]
qtvs full_theta_vars :: [TcTyCoVar]
full_theta_vars wanteds :: WantedConstraints
wanteds
| WantedConstraints -> Bool
isEmptyWC WantedConstraints
wanteds
= WantedConstraints -> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return WantedConstraints
wanteds
| Bool
otherwise
= do { Cts
wanted_simple <- Cts -> TcM Cts
TcM.zonkSimples (WantedConstraints -> Cts
wc_simple WantedConstraints
wanteds)
; let (outer_simple :: Cts
outer_simple, inner_simple :: Cts
inner_simple) = (Ct -> Bool) -> Cts -> (Cts, Cts)
forall a. (a -> Bool) -> Bag a -> (Bag a, Bag a)
partitionBag Ct -> Bool
is_mono Cts
wanted_simple
is_mono :: Ct -> Bool
is_mono ct :: Ct
ct = Ct -> Bool
isWantedCt Ct
ct Bool -> Bool -> Bool
&& Ct -> TcTyCoVar
ctEvId Ct
ct TcTyCoVar -> TcTyVarSet -> Bool
`elemVarSet` TcTyVarSet
co_vars
; (Bool, TcTyVarSet)
_ <- TcTyVarSet -> TcM (Bool, TcTyVarSet)
promoteTyVarSet (Cts -> TcTyVarSet
tyCoVarsOfCts Cts
outer_simple)
; let inner_wanted :: WantedConstraints
inner_wanted = WantedConstraints
wanteds { wc_simple :: Cts
wc_simple = Cts
inner_simple }
; WantedConstraints -> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return (WC :: Cts -> Bag Implication -> WantedConstraints
WC { wc_simple :: Cts
wc_simple = Cts
outer_simple
, wc_impl :: Bag Implication
wc_impl = WantedConstraints -> Bag Implication
mk_implic WantedConstraints
inner_wanted })}
where
mk_implic :: WantedConstraints -> Bag Implication
mk_implic inner_wanted :: WantedConstraints
inner_wanted
| WantedConstraints -> Bool
isEmptyWC WantedConstraints
inner_wanted
= Bag Implication
forall a. Bag a
emptyBag
| Bool
otherwise
= Implication -> Bag Implication
forall a. a -> Bag a
unitBag (Implication
implicationPrototype { ic_tclvl :: TcLevel
ic_tclvl = TcLevel
rhs_tclvl
, ic_skols :: [TcTyCoVar]
ic_skols = [TcTyCoVar]
qtvs
, ic_telescope :: Maybe SDoc
ic_telescope = Maybe SDoc
forall a. Maybe a
Nothing
, ic_given :: [TcTyCoVar]
ic_given = [TcTyCoVar]
full_theta_vars
, ic_wanted :: WantedConstraints
ic_wanted = WantedConstraints
inner_wanted
, ic_binds :: EvBindsVar
ic_binds = EvBindsVar
ev_binds_var
, ic_no_eqs :: Bool
ic_no_eqs = Bool
False
, ic_info :: SkolemInfo
ic_info = SkolemInfo
skol_info
, ic_env :: Env TcGblEnv TcLclEnv
ic_env = Env TcGblEnv TcLclEnv
tc_env })
full_theta :: [Type]
full_theta = (TcTyCoVar -> Type) -> [TcTyCoVar] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map TcTyCoVar -> Type
idType [TcTyCoVar]
full_theta_vars
skol_info :: SkolemInfo
skol_info = [(Name, Type)] -> SkolemInfo
InferSkol [ (Name
name, [TyCoVarBinder] -> [Type] -> Type -> Type
mkSigmaTy [] [Type]
full_theta Type
ty)
| (name :: Name
name, ty :: Type
ty) <- [(Name, Type)]
name_taus ]
ctsPreds :: Cts -> [PredType]
ctsPreds :: Cts -> [Type]
ctsPreds cts :: Cts
cts = [ CtEvidence -> Type
ctEvPred CtEvidence
ev | Ct
ct <- Cts -> [Ct]
forall a. Bag a -> [a]
bagToList Cts
cts
, let ev :: CtEvidence
ev = Ct -> CtEvidence
ctEvidence Ct
ct ]
decideQuantification
:: InferMode
-> TcLevel
-> [(Name, TcTauType)]
-> [TcIdSigInst]
-> [PredType]
-> TcM ( [TcTyVar]
, [PredType]
, VarSet)
decideQuantification :: InferMode
-> TcLevel
-> [(Name, Type)]
-> [TcIdSigInst]
-> [Type]
-> TcM ([TcTyCoVar], [Type], TcTyVarSet)
decideQuantification infer_mode :: InferMode
infer_mode rhs_tclvl :: TcLevel
rhs_tclvl name_taus :: [(Name, Type)]
name_taus psigs :: [TcIdSigInst]
psigs candidates :: [Type]
candidates
= do {
; (mono_tvs :: TcTyVarSet
mono_tvs, candidates :: [Type]
candidates, co_vars :: TcTyVarSet
co_vars) <- InferMode
-> [(Name, Type)]
-> [TcIdSigInst]
-> [Type]
-> TcM (TcTyVarSet, [Type], TcTyVarSet)
decideMonoTyVars InferMode
infer_mode
[(Name, Type)]
name_taus [TcIdSigInst]
psigs [Type]
candidates
; [Type]
candidates <- TcLevel -> TcTyVarSet -> [Type] -> TcM [Type]
defaultTyVarsAndSimplify TcLevel
rhs_tclvl TcTyVarSet
mono_tvs [Type]
candidates
; [TcTyCoVar]
qtvs <- TcTyVarSet
-> [(Name, Type)] -> [TcIdSigInst] -> [Type] -> TcM [TcTyCoVar]
decideQuantifiedTyVars TcTyVarSet
mono_tvs [(Name, Type)]
name_taus [TcIdSigInst]
psigs [Type]
candidates
; [Type]
candidates <- [Type] -> TcM [Type]
TcM.zonkTcTypes [Type]
candidates
; [Type]
psig_theta <- [Type] -> TcM [Type]
TcM.zonkTcTypes ((TcIdSigInst -> [Type]) -> [TcIdSigInst] -> [Type]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap TcIdSigInst -> [Type]
sig_inst_theta [TcIdSigInst]
psigs)
; let quantifiable_candidates :: [Type]
quantifiable_candidates
= TcTyVarSet -> [Type] -> [Type]
pickQuantifiablePreds ([TcTyCoVar] -> TcTyVarSet
mkVarSet [TcTyCoVar]
qtvs) [Type]
candidates
theta :: [Type]
theta = (Type -> Type) -> [Type] -> [Type]
forall a. (a -> Type) -> [a] -> [a]
mkMinimalBySCs Type -> Type
forall a. a -> a
id ([Type] -> [Type]) -> [Type] -> [Type]
forall a b. (a -> b) -> a -> b
$
([Type]
psig_theta [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
quantifiable_candidates)
; String -> SDoc -> TcM ()
traceTc "decideQuantification"
([SDoc] -> SDoc
vcat [ String -> SDoc
text "infer_mode:" SDoc -> SDoc -> SDoc
<+> InferMode -> SDoc
forall a. Outputable a => a -> SDoc
ppr InferMode
infer_mode
, String -> SDoc
text "candidates:" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
candidates
, String -> SDoc
text "psig_theta:" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
psig_theta
, String -> SDoc
text "mono_tvs:" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
mono_tvs
, String -> SDoc
text "co_vars:" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
co_vars
, String -> SDoc
text "qtvs:" SDoc -> SDoc -> SDoc
<+> [TcTyCoVar] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcTyCoVar]
qtvs
, String -> SDoc
text "theta:" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
theta ])
; ([TcTyCoVar], [Type], TcTyVarSet)
-> TcM ([TcTyCoVar], [Type], TcTyVarSet)
forall (m :: * -> *) a. Monad m => a -> m a
return ([TcTyCoVar]
qtvs, [Type]
theta, TcTyVarSet
co_vars) }
decideMonoTyVars :: InferMode
-> [(Name,TcType)]
-> [TcIdSigInst]
-> [PredType]
-> TcM (TcTyCoVarSet, [PredType], CoVarSet)
decideMonoTyVars :: InferMode
-> [(Name, Type)]
-> [TcIdSigInst]
-> [Type]
-> TcM (TcTyVarSet, [Type], TcTyVarSet)
decideMonoTyVars infer_mode :: InferMode
infer_mode name_taus :: [(Name, Type)]
name_taus psigs :: [TcIdSigInst]
psigs candidates :: [Type]
candidates
= do { (no_quant :: [Type]
no_quant, maybe_quant :: [Type]
maybe_quant) <- InferMode -> [Type] -> TcM ([Type], [Type])
pick InferMode
infer_mode [Type]
candidates
; [TcTyCoVar]
psig_qtvs <- (TcTyCoVar -> IOEnv (Env TcGblEnv TcLclEnv) TcTyCoVar)
-> [TcTyCoVar] -> TcM [TcTyCoVar]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM HasDebugCallStack =>
TcTyCoVar -> IOEnv (Env TcGblEnv TcLclEnv) TcTyCoVar
TcTyCoVar -> IOEnv (Env TcGblEnv TcLclEnv) TcTyCoVar
zonkTcTyVarToTyVar ([TcTyCoVar] -> TcM [TcTyCoVar]) -> [TcTyCoVar] -> TcM [TcTyCoVar]
forall a b. (a -> b) -> a -> b
$
(TcIdSigInst -> [TcTyCoVar]) -> [TcIdSigInst] -> [TcTyCoVar]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (((Name, TcTyCoVar) -> TcTyCoVar)
-> [(Name, TcTyCoVar)] -> [TcTyCoVar]
forall a b. (a -> b) -> [a] -> [b]
map (Name, TcTyCoVar) -> TcTyCoVar
forall a b. (a, b) -> b
snd ([(Name, TcTyCoVar)] -> [TcTyCoVar])
-> (TcIdSigInst -> [(Name, TcTyCoVar)])
-> TcIdSigInst
-> [TcTyCoVar]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TcIdSigInst -> [(Name, TcTyCoVar)]
sig_inst_skols) [TcIdSigInst]
psigs
; [Type]
psig_theta <- (Type -> TcM Type) -> [Type] -> TcM [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> TcM Type
TcM.zonkTcType ([Type] -> TcM [Type]) -> [Type] -> TcM [Type]
forall a b. (a -> b) -> a -> b
$
(TcIdSigInst -> [Type]) -> [TcIdSigInst] -> [Type]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap TcIdSigInst -> [Type]
sig_inst_theta [TcIdSigInst]
psigs
; [Type]
taus <- ((Name, Type) -> TcM Type) -> [(Name, Type)] -> TcM [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Type -> TcM Type
TcM.zonkTcType (Type -> TcM Type)
-> ((Name, Type) -> Type) -> (Name, Type) -> TcM Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Name, Type) -> Type
forall a b. (a, b) -> b
snd) [(Name, Type)]
name_taus
; TcTyVarSet
mono_tvs0 <- TcM TcTyVarSet
tcGetGlobalTyCoVars
; let psig_tys :: [Type]
psig_tys = [TcTyCoVar] -> [Type]
mkTyVarTys [TcTyCoVar]
psig_qtvs [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
psig_theta
co_vars :: TcTyVarSet
co_vars = [Type] -> TcTyVarSet
coVarsOfTypes ([Type]
psig_tys [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
taus)
co_var_tvs :: TcTyVarSet
co_var_tvs = TcTyVarSet -> TcTyVarSet
closeOverKinds TcTyVarSet
co_vars
mono_tvs1 :: TcTyVarSet
mono_tvs1 = TcTyVarSet
mono_tvs0 TcTyVarSet -> TcTyVarSet -> TcTyVarSet
`unionVarSet` TcTyVarSet
co_var_tvs
eq_constraints :: [Type]
eq_constraints = (Type -> Bool) -> [Type] -> [Type]
forall a. (a -> Bool) -> [a] -> [a]
filter Type -> Bool
isEqPred [Type]
candidates
mono_tvs2 :: TcTyVarSet
mono_tvs2 = [Type] -> TcTyVarSet -> TcTyVarSet
growThetaTyVars [Type]
eq_constraints TcTyVarSet
mono_tvs1
constrained_tvs :: TcTyVarSet
constrained_tvs = ([Type] -> TcTyVarSet -> TcTyVarSet
growThetaTyVars [Type]
eq_constraints
([Type] -> TcTyVarSet
tyCoVarsOfTypes [Type]
no_quant)
TcTyVarSet -> TcTyVarSet -> TcTyVarSet
`minusVarSet` TcTyVarSet
mono_tvs2)
TcTyVarSet -> [TcTyCoVar] -> TcTyVarSet
`delVarSetList` [TcTyCoVar]
psig_qtvs
mono_tvs :: TcTyVarSet
mono_tvs = TcTyVarSet
mono_tvs2 TcTyVarSet -> TcTyVarSet -> TcTyVarSet
`unionVarSet` TcTyVarSet
constrained_tvs
; Bool
warn_mono <- WarningFlag -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. WarningFlag -> TcRnIf gbl lcl Bool
woptM WarningFlag
Opt_WarnMonomorphism
; Bool -> TcM () -> TcM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (case InferMode
infer_mode of { ApplyMR -> Bool
warn_mono; _ -> Bool
False}) (TcM () -> TcM ()) -> TcM () -> TcM ()
forall a b. (a -> b) -> a -> b
$
WarnReason -> Bool -> SDoc -> TcM ()
warnTc (WarningFlag -> WarnReason
Reason WarningFlag
Opt_WarnMonomorphism)
(TcTyVarSet
constrained_tvs TcTyVarSet -> TcTyVarSet -> Bool
`intersectsVarSet` [Type] -> TcTyVarSet
tyCoVarsOfTypes [Type]
taus)
SDoc
mr_msg
; String -> SDoc -> TcM ()
traceTc "decideMonoTyVars" (SDoc -> TcM ()) -> SDoc -> TcM ()
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat
[ String -> SDoc
text "mono_tvs0 =" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
mono_tvs0
, String -> SDoc
text "mono_tvs1 =" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
mono_tvs1
, String -> SDoc
text "no_quant =" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
no_quant
, String -> SDoc
text "maybe_quant =" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
maybe_quant
, String -> SDoc
text "eq_constraints =" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
eq_constraints
, String -> SDoc
text "mono_tvs =" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
mono_tvs
, String -> SDoc
text "co_vars =" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
co_vars ]
; (TcTyVarSet, [Type], TcTyVarSet)
-> TcM (TcTyVarSet, [Type], TcTyVarSet)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcTyVarSet
mono_tvs, [Type]
maybe_quant, TcTyVarSet
co_vars) }
where
pick :: InferMode -> [PredType] -> TcM ([PredType], [PredType])
pick :: InferMode -> [Type] -> TcM ([Type], [Type])
pick NoRestrictions cand :: [Type]
cand = ([Type], [Type]) -> TcM ([Type], [Type])
forall (m :: * -> *) a. Monad m => a -> m a
return ([], [Type]
cand)
pick ApplyMR cand :: [Type]
cand = ([Type], [Type]) -> TcM ([Type], [Type])
forall (m :: * -> *) a. Monad m => a -> m a
return ([Type]
cand, [])
pick EagerDefaulting cand :: [Type]
cand = do { Bool
os <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.OverloadedStrings
; ([Type], [Type]) -> TcM ([Type], [Type])
forall (m :: * -> *) a. Monad m => a -> m a
return ((Type -> Bool) -> [Type] -> ([Type], [Type])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (Bool -> Type -> Bool
is_int_ct Bool
os) [Type]
cand) }
is_int_ct :: Bool -> Type -> Bool
is_int_ct ovl_strings :: Bool
ovl_strings pred :: Type
pred
| Just (cls :: Class
cls, _) <- Type -> Maybe (Class, [Type])
getClassPredTys_maybe Type
pred
= Bool -> Class -> Bool
isInteractiveClass Bool
ovl_strings Class
cls
| Bool
otherwise
= Bool
False
pp_bndrs :: SDoc
pp_bndrs = ((Name, Type) -> SDoc) -> [(Name, Type)] -> SDoc
forall a. (a -> SDoc) -> [a] -> SDoc
pprWithCommas (SDoc -> SDoc
quotes (SDoc -> SDoc) -> ((Name, Type) -> SDoc) -> (Name, Type) -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr (Name -> SDoc) -> ((Name, Type) -> Name) -> (Name, Type) -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Name, Type) -> Name
forall a b. (a, b) -> a
fst) [(Name, Type)]
name_taus
mr_msg :: SDoc
mr_msg =
SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
sep [ String -> SDoc
text "The Monomorphism Restriction applies to the binding"
SDoc -> SDoc -> SDoc
<> [(Name, Type)] -> SDoc
forall a. [a] -> SDoc
plural [(Name, Type)]
name_taus
, String -> SDoc
text "for" SDoc -> SDoc -> SDoc
<+> SDoc
pp_bndrs ])
2 ([SDoc] -> SDoc
hsep [ String -> SDoc
text "Consider giving"
, String -> SDoc
text (if [(Name, Type)] -> Bool
forall a. [a] -> Bool
isSingleton [(Name, Type)]
name_taus then "it" else "them")
, String -> SDoc
text "a type signature"])
defaultTyVarsAndSimplify :: TcLevel
-> TyCoVarSet
-> [PredType]
-> TcM [PredType]
defaultTyVarsAndSimplify :: TcLevel -> TcTyVarSet -> [Type] -> TcM [Type]
defaultTyVarsAndSimplify rhs_tclvl :: TcLevel
rhs_tclvl mono_tvs :: TcTyVarSet
mono_tvs candidates :: [Type]
candidates
= do {
; String -> SDoc -> TcM ()
traceTc "decideMonoTyVars: promotion:" (TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
mono_tvs)
; (prom :: Bool
prom, _) <- TcTyVarSet -> TcM (Bool, TcTyVarSet)
promoteTyVarSet TcTyVarSet
mono_tvs
; DV {dv_kvs :: CandidatesQTvs -> DTyVarSet
dv_kvs = DTyVarSet
cand_kvs, dv_tvs :: CandidatesQTvs -> DTyVarSet
dv_tvs = DTyVarSet
cand_tvs}
<- [Type] -> TcM CandidatesQTvs
candidateQTyVarsOfTypes [Type]
candidates
; Bool
poly_kinds <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.PolyKinds
; [Bool]
default_kvs <- (TcTyCoVar -> TcRnIf TcGblEnv TcLclEnv Bool)
-> [TcTyCoVar] -> IOEnv (Env TcGblEnv TcLclEnv) [Bool]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Bool -> Bool -> TcTyCoVar -> TcRnIf TcGblEnv TcLclEnv Bool
default_one Bool
poly_kinds Bool
True)
(DTyVarSet -> [TcTyCoVar]
dVarSetElems DTyVarSet
cand_kvs)
; [Bool]
default_tvs <- (TcTyCoVar -> TcRnIf TcGblEnv TcLclEnv Bool)
-> [TcTyCoVar] -> IOEnv (Env TcGblEnv TcLclEnv) [Bool]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Bool -> Bool -> TcTyCoVar -> TcRnIf TcGblEnv TcLclEnv Bool
default_one Bool
poly_kinds Bool
False)
(DTyVarSet -> [TcTyCoVar]
dVarSetElems (DTyVarSet
cand_tvs DTyVarSet -> DTyVarSet -> DTyVarSet
`minusDVarSet` DTyVarSet
cand_kvs))
; let some_default :: Bool
some_default = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Bool]
default_kvs Bool -> Bool -> Bool
|| [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Bool]
default_tvs
; case () of
_ | Bool
some_default -> [Type] -> TcM [Type]
simplify_cand [Type]
candidates
| Bool
prom -> (Type -> TcM Type) -> [Type] -> TcM [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> TcM Type
TcM.zonkTcType [Type]
candidates
| Bool
otherwise -> [Type] -> TcM [Type]
forall (m :: * -> *) a. Monad m => a -> m a
return [Type]
candidates
}
where
default_one :: Bool -> Bool -> TcTyCoVar -> TcRnIf TcGblEnv TcLclEnv Bool
default_one poly_kinds :: Bool
poly_kinds is_kind_var :: Bool
is_kind_var tv :: TcTyCoVar
tv
| Bool -> Bool
not (TcTyCoVar -> Bool
isMetaTyVar TcTyCoVar
tv)
= Bool -> TcRnIf TcGblEnv TcLclEnv Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
| TcTyCoVar
tv TcTyCoVar -> TcTyVarSet -> Bool
`elemVarSet` TcTyVarSet
mono_tvs
= Bool -> TcRnIf TcGblEnv TcLclEnv Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
| Bool
otherwise
= Bool -> TcTyCoVar -> TcRnIf TcGblEnv TcLclEnv Bool
defaultTyVar (Bool -> Bool
not Bool
poly_kinds Bool -> Bool -> Bool
&& Bool
is_kind_var) TcTyCoVar
tv
simplify_cand :: [Type] -> TcM [Type]
simplify_cand candidates :: [Type]
candidates
= do { [CtEvidence]
clone_wanteds <- CtOrigin -> [Type] -> TcM [CtEvidence]
newWanteds CtOrigin
DefaultOrigin [Type]
candidates
; WC { wc_simple :: WantedConstraints -> Cts
wc_simple = Cts
simples } <- TcLevel
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall a. TcLevel -> TcM a -> TcM a
setTcLevel TcLevel
rhs_tclvl (TcRnIf TcGblEnv TcLclEnv WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints)
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
-> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall a b. (a -> b) -> a -> b
$
[CtEvidence] -> TcRnIf TcGblEnv TcLclEnv WantedConstraints
simplifyWantedsTcM [CtEvidence]
clone_wanteds
; let new_candidates :: [Type]
new_candidates = Cts -> [Type]
ctsPreds Cts
simples
; String -> SDoc -> TcM ()
traceTc "Simplified after defaulting" (SDoc -> TcM ()) -> SDoc -> TcM ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text "Before:" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
candidates
, String -> SDoc
text "After:" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
new_candidates ]
; [Type] -> TcM [Type]
forall (m :: * -> *) a. Monad m => a -> m a
return [Type]
new_candidates }
decideQuantifiedTyVars
:: TyCoVarSet
-> [(Name,TcType)]
-> [TcIdSigInst]
-> [PredType]
-> TcM [TyVar]
decideQuantifiedTyVars :: TcTyVarSet
-> [(Name, Type)] -> [TcIdSigInst] -> [Type] -> TcM [TcTyCoVar]
decideQuantifiedTyVars mono_tvs :: TcTyVarSet
mono_tvs name_taus :: [(Name, Type)]
name_taus psigs :: [TcIdSigInst]
psigs candidates :: [Type]
candidates
= do {
; [Type]
psig_tv_tys <- (TcTyCoVar -> TcM Type) -> [TcTyCoVar] -> TcM [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TcTyCoVar -> TcM Type
TcM.zonkTcTyVar [ TcTyCoVar
tv | TcIdSigInst
sig <- [TcIdSigInst]
psigs
, (_,tv :: TcTyCoVar
tv) <- TcIdSigInst -> [(Name, TcTyCoVar)]
sig_inst_skols TcIdSigInst
sig ]
; [Type]
psig_theta <- (Type -> TcM Type) -> [Type] -> TcM [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> TcM Type
TcM.zonkTcType [ Type
pred | TcIdSigInst
sig <- [TcIdSigInst]
psigs
, Type
pred <- TcIdSigInst -> [Type]
sig_inst_theta TcIdSigInst
sig ]
; [Type]
tau_tys <- ((Name, Type) -> TcM Type) -> [(Name, Type)] -> TcM [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Type -> TcM Type
TcM.zonkTcType (Type -> TcM Type)
-> ((Name, Type) -> Type) -> (Name, Type) -> TcM Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Name, Type) -> Type
forall a b. (a, b) -> b
snd) [(Name, Type)]
name_taus
; TcTyVarSet
mono_tvs <- TcTyVarSet -> TcM TcTyVarSet
TcM.zonkTyCoVarsAndFV TcTyVarSet
mono_tvs
; let
psig_tys :: [Type]
psig_tys = [Type]
psig_tv_tys [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
psig_theta
seed_tys :: [Type]
seed_tys = [Type]
psig_tys [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
tau_tys
grown_tcvs :: TcTyVarSet
grown_tcvs = [Type] -> TcTyVarSet -> TcTyVarSet
growThetaTyVars [Type]
candidates ([Type] -> TcTyVarSet
tyCoVarsOfTypes [Type]
seed_tys)
; dv :: CandidatesQTvs
dv@DV {dv_kvs :: CandidatesQTvs -> DTyVarSet
dv_kvs = DTyVarSet
cand_kvs, dv_tvs :: CandidatesQTvs -> DTyVarSet
dv_tvs = DTyVarSet
cand_tvs} <- [Type] -> TcM CandidatesQTvs
candidateQTyVarsOfTypes ([Type] -> TcM CandidatesQTvs) -> [Type] -> TcM CandidatesQTvs
forall a b. (a -> b) -> a -> b
$
[Type]
psig_tys [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
candidates [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
tau_tys
; let pick :: DTyVarSet -> DTyVarSet
pick = (DTyVarSet -> TcTyVarSet -> DTyVarSet
`dVarSetIntersectVarSet` TcTyVarSet
grown_tcvs)
dvs_plus :: CandidatesQTvs
dvs_plus = CandidatesQTvs
dv { dv_kvs :: DTyVarSet
dv_kvs = DTyVarSet -> DTyVarSet
pick DTyVarSet
cand_kvs, dv_tvs :: DTyVarSet
dv_tvs = DTyVarSet -> DTyVarSet
pick DTyVarSet
cand_tvs }
; String -> SDoc -> TcM ()
traceTc "decideQuantifiedTyVars" ([SDoc] -> SDoc
vcat
[ String -> SDoc
text "candidates =" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
candidates
, String -> SDoc
text "tau_tys =" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
tau_tys
, String -> SDoc
text "seed_tys =" SDoc -> SDoc -> SDoc
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
seed_tys
, String -> SDoc
text "seed_tcvs =" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr ([Type] -> TcTyVarSet
tyCoVarsOfTypes [Type]
seed_tys)
, String -> SDoc
text "grown_tcvs =" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
grown_tcvs
, String -> SDoc
text "dvs =" SDoc -> SDoc -> SDoc
<+> CandidatesQTvs -> SDoc
forall a. Outputable a => a -> SDoc
ppr CandidatesQTvs
dvs_plus])
; TcTyVarSet -> CandidatesQTvs -> TcM [TcTyCoVar]
quantifyTyVars TcTyVarSet
mono_tvs CandidatesQTvs
dvs_plus }
growThetaTyVars :: ThetaType -> TyCoVarSet -> TyCoVarSet
growThetaTyVars :: [Type] -> TcTyVarSet -> TcTyVarSet
growThetaTyVars theta :: [Type]
theta tcvs :: TcTyVarSet
tcvs
| [Type] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Type]
theta = TcTyVarSet
tcvs
| Bool
otherwise = (TcTyVarSet -> TcTyVarSet) -> TcTyVarSet -> TcTyVarSet
transCloVarSet TcTyVarSet -> TcTyVarSet
mk_next TcTyVarSet
seed_tcvs
where
seed_tcvs :: TcTyVarSet
seed_tcvs = TcTyVarSet
tcvs TcTyVarSet -> TcTyVarSet -> TcTyVarSet
`unionVarSet` [Type] -> TcTyVarSet
tyCoVarsOfTypes [Type]
ips
(ips :: [Type]
ips, non_ips :: [Type]
non_ips) = (Type -> Bool) -> [Type] -> ([Type], [Type])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition Type -> Bool
isIPPred [Type]
theta
mk_next :: VarSet -> VarSet
mk_next :: TcTyVarSet -> TcTyVarSet
mk_next so_far :: TcTyVarSet
so_far = (Type -> TcTyVarSet -> TcTyVarSet)
-> TcTyVarSet -> [Type] -> TcTyVarSet
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (TcTyVarSet -> Type -> TcTyVarSet -> TcTyVarSet
grow_one TcTyVarSet
so_far) TcTyVarSet
emptyVarSet [Type]
non_ips
grow_one :: TcTyVarSet -> Type -> TcTyVarSet -> TcTyVarSet
grow_one so_far :: TcTyVarSet
so_far pred :: Type
pred tcvs :: TcTyVarSet
tcvs
| TcTyVarSet
pred_tcvs TcTyVarSet -> TcTyVarSet -> Bool
`intersectsVarSet` TcTyVarSet
so_far = TcTyVarSet
tcvs TcTyVarSet -> TcTyVarSet -> TcTyVarSet
`unionVarSet` TcTyVarSet
pred_tcvs
| Bool
otherwise = TcTyVarSet
tcvs
where
pred_tcvs :: TcTyVarSet
pred_tcvs = Type -> TcTyVarSet
tyCoVarsOfType Type
pred
simplifyWantedsTcM :: [CtEvidence] -> TcM WantedConstraints
simplifyWantedsTcM :: [CtEvidence] -> TcRnIf TcGblEnv TcLclEnv WantedConstraints
simplifyWantedsTcM wanted :: [CtEvidence]
wanted
= do { String -> SDoc -> TcM ()
traceTc "simplifyWantedsTcM {" ([CtEvidence] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [CtEvidence]
wanted)
; (result :: WantedConstraints
result, _) <- TcS WantedConstraints -> TcM (WantedConstraints, EvBindMap)
forall a. TcS a -> TcM (a, EvBindMap)
runTcS (WantedConstraints -> TcS WantedConstraints
solveWantedsAndDrop ([CtEvidence] -> WantedConstraints
mkSimpleWC [CtEvidence]
wanted))
; WantedConstraints
result <- WantedConstraints -> TcRnIf TcGblEnv TcLclEnv WantedConstraints
TcM.zonkWC WantedConstraints
result
; String -> SDoc -> TcM ()
traceTc "simplifyWantedsTcM }" (WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
result)
; WantedConstraints -> TcRnIf TcGblEnv TcLclEnv WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return WantedConstraints
result }
solveWantedsAndDrop :: WantedConstraints -> TcS WantedConstraints
solveWantedsAndDrop :: WantedConstraints -> TcS WantedConstraints
solveWantedsAndDrop wanted :: WantedConstraints
wanted
= do { WantedConstraints
wc <- WantedConstraints -> TcS WantedConstraints
solveWanteds WantedConstraints
wanted
; WantedConstraints -> TcS WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return (WantedConstraints -> WantedConstraints
dropDerivedWC WantedConstraints
wc) }
solveWanteds :: WantedConstraints -> TcS WantedConstraints
solveWanteds :: WantedConstraints -> TcS WantedConstraints
solveWanteds wc :: WantedConstraints
wc@(WC { wc_simple :: WantedConstraints -> Cts
wc_simple = Cts
simples, wc_impl :: WantedConstraints -> Bag Implication
wc_impl = Bag Implication
implics })
= do { TcLevel
cur_lvl <- TcS TcLevel
TcS.getTcLevel
; String -> SDoc -> TcS ()
traceTcS "solveWanteds {" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text "Level =" SDoc -> SDoc -> SDoc
<+> TcLevel -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcLevel
cur_lvl
, WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
wc ]
; WantedConstraints
wc1 <- Cts -> TcS WantedConstraints
solveSimpleWanteds Cts
simples
; (floated_eqs :: Cts
floated_eqs, implics2 :: Bag Implication
implics2) <- Bag Implication -> TcS (Cts, Bag Implication)
solveNestedImplications (Bag Implication -> TcS (Cts, Bag Implication))
-> Bag Implication -> TcS (Cts, Bag Implication)
forall a b. (a -> b) -> a -> b
$
Bag Implication
implics Bag Implication -> Bag Implication -> Bag Implication
forall a. Bag a -> Bag a -> Bag a
`unionBags` WantedConstraints -> Bag Implication
wc_impl WantedConstraints
wc1
; DynFlags
dflags <- TcS DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
; WantedConstraints
final_wc <- Int
-> IntWithInf -> Cts -> WantedConstraints -> TcS WantedConstraints
simpl_loop 0 (DynFlags -> IntWithInf
solverIterations DynFlags
dflags) Cts
floated_eqs
(WantedConstraints
wc1 { wc_impl :: Bag Implication
wc_impl = Bag Implication
implics2 })
; EvBindsVar
ev_binds_var <- TcS EvBindsVar
getTcEvBindsVar
; EvBindMap
bb <- EvBindsVar -> TcS EvBindMap
TcS.getTcEvBindsMap EvBindsVar
ev_binds_var
; String -> SDoc -> TcS ()
traceTcS "solveWanteds }" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text "final wc =" SDoc -> SDoc -> SDoc
<+> WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
final_wc
, String -> SDoc
text "current evbinds =" SDoc -> SDoc -> SDoc
<+> Bag EvBind -> SDoc
forall a. Outputable a => a -> SDoc
ppr (EvBindMap -> Bag EvBind
evBindMapBinds EvBindMap
bb) ]
; WantedConstraints -> TcS WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return WantedConstraints
final_wc }
simpl_loop :: Int -> IntWithInf -> Cts
-> WantedConstraints -> TcS WantedConstraints
simpl_loop :: Int
-> IntWithInf -> Cts -> WantedConstraints -> TcS WantedConstraints
simpl_loop n :: Int
n limit :: IntWithInf
limit floated_eqs :: Cts
floated_eqs wc :: WantedConstraints
wc@(WC { wc_simple :: WantedConstraints -> Cts
wc_simple = Cts
simples })
| Int
n Int -> IntWithInf -> Bool
`intGtLimit` IntWithInf
limit
= do {
SDoc -> TcS ()
addErrTcS (SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text "solveWanteds: too many iterations"
SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
parens (String -> SDoc
text "limit =" SDoc -> SDoc -> SDoc
<+> IntWithInf -> SDoc
forall a. Outputable a => a -> SDoc
ppr IntWithInf
limit))
2 ([SDoc] -> SDoc
vcat [ String -> SDoc
text "Unsolved:" SDoc -> SDoc -> SDoc
<+> WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
wc
, Bool -> SDoc -> SDoc
ppUnless (Cts -> Bool
forall a. Bag a -> Bool
isEmptyBag Cts
floated_eqs) (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$
String -> SDoc
text "Floated equalities:" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
forall a. Outputable a => a -> SDoc
ppr Cts
floated_eqs
, String -> SDoc
text "Set limit with -fconstraint-solver-iterations=n; n=0 for no limit"
]))
; WantedConstraints -> TcS WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return WantedConstraints
wc }
| Bool -> Bool
not (Cts -> Bool
forall a. Bag a -> Bool
isEmptyBag Cts
floated_eqs)
= Int
-> IntWithInf -> Bool -> WantedConstraints -> TcS WantedConstraints
simplify_again Int
n IntWithInf
limit Bool
True (WantedConstraints
wc { wc_simple :: Cts
wc_simple = Cts
floated_eqs Cts -> Cts -> Cts
forall a. Bag a -> Bag a -> Bag a
`unionBags` Cts
simples })
| WantedConstraints -> Bool
superClassesMightHelp WantedConstraints
wc
=
do { [Ct]
pending_given <- TcS [Ct]
getPendingGivenScs
; let (pending_wanted :: [Ct]
pending_wanted, simples1 :: Cts
simples1) = Cts -> ([Ct], Cts)
getPendingWantedScs Cts
simples
; if [Ct] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Ct]
pending_given Bool -> Bool -> Bool
&& [Ct] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Ct]
pending_wanted
then WantedConstraints -> TcS WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return WantedConstraints
wc
else
do { [Ct]
new_given <- [Ct] -> TcS [Ct]
makeSuperClasses [Ct]
pending_given
; [Ct]
new_wanted <- [Ct] -> TcS [Ct]
makeSuperClasses [Ct]
pending_wanted
; [Ct] -> TcS ()
solveSimpleGivens [Ct]
new_given
; Int
-> IntWithInf -> Bool -> WantedConstraints -> TcS WantedConstraints
simplify_again Int
n IntWithInf
limit ([Ct] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Ct]
pending_given)
WantedConstraints
wc { wc_simple :: Cts
wc_simple = Cts
simples1 Cts -> Cts -> Cts
forall a. Bag a -> Bag a -> Bag a
`unionBags` [Ct] -> Cts
forall a. [a] -> Bag a
listToBag [Ct]
new_wanted } } }
| Bool
otherwise
= WantedConstraints -> TcS WantedConstraints
forall (m :: * -> *) a. Monad m => a -> m a
return WantedConstraints
wc
simplify_again :: Int -> IntWithInf -> Bool
-> WantedConstraints -> TcS WantedConstraints
simplify_again :: Int
-> IntWithInf -> Bool -> WantedConstraints -> TcS WantedConstraints
simplify_again n :: Int
n limit :: IntWithInf
limit no_new_given_scs :: Bool
no_new_given_scs
wc :: WantedConstraints
wc@(WC { wc_simple :: WantedConstraints -> Cts
wc_simple = Cts
simples, wc_impl :: WantedConstraints -> Bag Implication
wc_impl = Bag Implication
implics })
= do { SDoc -> TcS ()
csTraceTcS (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
String -> SDoc
text "simpl_loop iteration=" SDoc -> SDoc -> SDoc
<> Int -> SDoc
int Int
n
SDoc -> SDoc -> SDoc
<+> (SDoc -> SDoc
parens (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
hsep [ String -> SDoc
text "no new given superclasses =" SDoc -> SDoc -> SDoc
<+> Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bool
no_new_given_scs SDoc -> SDoc -> SDoc
<> SDoc
comma
, Int -> SDoc
int (Cts -> Int
forall a. Bag a -> Int
lengthBag Cts
simples) SDoc -> SDoc -> SDoc
<+> String -> SDoc
text "simples to solve" ])
; String -> SDoc -> TcS ()
traceTcS "simpl_loop: wc =" (WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
wc)
; (unifs1 :: Int
unifs1, wc1 :: WantedConstraints
wc1) <- TcS WantedConstraints -> TcS (Int, WantedConstraints)
forall a. TcS a -> TcS (Int, a)
reportUnifications (TcS WantedConstraints -> TcS (Int, WantedConstraints))
-> TcS WantedConstraints -> TcS (Int, WantedConstraints)
forall a b. (a -> b) -> a -> b
$
Cts -> TcS WantedConstraints
solveSimpleWanteds (Cts -> TcS WantedConstraints) -> Cts -> TcS WantedConstraints
forall a b. (a -> b) -> a -> b
$
Cts
simples
; let new_implics :: Bag Implication
new_implics = WantedConstraints -> Bag Implication
wc_impl WantedConstraints
wc1
; if Int
unifs1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0 Bool -> Bool -> Bool
&&
Bool
no_new_given_scs Bool -> Bool -> Bool
&&
Bag Implication -> Bool
forall a. Bag a -> Bool
isEmptyBag Bag Implication
new_implics
then
Int
-> IntWithInf -> Cts -> WantedConstraints -> TcS WantedConstraints
simpl_loop (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) IntWithInf
limit Cts
forall a. Bag a
emptyBag (WantedConstraints
wc1 { wc_impl :: Bag Implication
wc_impl = Bag Implication
implics })
else
do { (floated_eqs2 :: Cts
floated_eqs2, implics2 :: Bag Implication
implics2) <- Bag Implication -> TcS (Cts, Bag Implication)
solveNestedImplications (Bag Implication -> TcS (Cts, Bag Implication))
-> Bag Implication -> TcS (Cts, Bag Implication)
forall a b. (a -> b) -> a -> b
$
Bag Implication
implics Bag Implication -> Bag Implication -> Bag Implication
forall a. Bag a -> Bag a -> Bag a
`unionBags` Bag Implication
new_implics
; Int
-> IntWithInf -> Cts -> WantedConstraints -> TcS WantedConstraints
simpl_loop (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) IntWithInf
limit Cts
floated_eqs2 (WantedConstraints
wc1 { wc_impl :: Bag Implication
wc_impl = Bag Implication
implics2 })
} }
solveNestedImplications :: Bag Implication
-> TcS (Cts, Bag Implication)
solveNestedImplications :: Bag Implication -> TcS (Cts, Bag Implication)
solveNestedImplications implics :: Bag Implication
implics
| Bag Implication -> Bool
forall a. Bag a -> Bool
isEmptyBag Bag Implication
implics
= (Cts, Bag Implication) -> TcS (Cts, Bag Implication)
forall (m :: * -> *) a. Monad m => a -> m a
return (Cts
forall a. Bag a
emptyBag, Bag Implication
forall a. Bag a
emptyBag)
| Bool
otherwise
= do { String -> SDoc -> TcS ()
traceTcS "solveNestedImplications starting {" SDoc
empty
; (floated_eqs_s :: Bag Cts
floated_eqs_s, unsolved_implics :: Bag (Maybe Implication)
unsolved_implics) <- (Implication -> TcS (Cts, Maybe Implication))
-> Bag Implication -> TcS (Bag Cts, Bag (Maybe Implication))
forall (m :: * -> *) a b c.
Monad m =>
(a -> m (b, c)) -> Bag a -> m (Bag b, Bag c)
mapAndUnzipBagM Implication -> TcS (Cts, Maybe Implication)
solveImplication Bag Implication
implics
; let floated_eqs :: Cts
floated_eqs = Bag Cts -> Cts
forall a. Bag (Bag a) -> Bag a
concatBag Bag Cts
floated_eqs_s
; String -> SDoc -> TcS ()
traceTcS "solveNestedImplications end }" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text "all floated_eqs =" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
forall a. Outputable a => a -> SDoc
ppr Cts
floated_eqs
, String -> SDoc
text "unsolved_implics =" SDoc -> SDoc -> SDoc
<+> Bag (Maybe Implication) -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bag (Maybe Implication)
unsolved_implics ]
; (Cts, Bag Implication) -> TcS (Cts, Bag Implication)
forall (m :: * -> *) a. Monad m => a -> m a
return (Cts
floated_eqs, Bag (Maybe Implication) -> Bag Implication
forall a. Bag (Maybe a) -> Bag a
catBagMaybes Bag (Maybe Implication)
unsolved_implics) }
solveImplication :: Implication
-> TcS (Cts,
Maybe Implication)
solveImplication :: Implication -> TcS (Cts, Maybe Implication)
solveImplication imp :: Implication
imp@(Implic { ic_tclvl :: Implication -> TcLevel
ic_tclvl = TcLevel
tclvl
, ic_binds :: Implication -> EvBindsVar
ic_binds = EvBindsVar
ev_binds_var
, ic_skols :: Implication -> [TcTyCoVar]
ic_skols = [TcTyCoVar]
skols
, ic_given :: Implication -> [TcTyCoVar]
ic_given = [TcTyCoVar]
given_ids
, ic_wanted :: Implication -> WantedConstraints
ic_wanted = WantedConstraints
wanteds
, ic_info :: Implication -> SkolemInfo
ic_info = SkolemInfo
info
, ic_status :: Implication -> ImplicStatus
ic_status = ImplicStatus
status })
| ImplicStatus -> Bool
isSolvedStatus ImplicStatus
status
= (Cts, Maybe Implication) -> TcS (Cts, Maybe Implication)
forall (m :: * -> *) a. Monad m => a -> m a
return (Cts
emptyCts, Implication -> Maybe Implication
forall a. a -> Maybe a
Just Implication
imp)
| Bool
otherwise
= do { InertSet
inerts <- TcS InertSet
getTcSInerts
; String -> SDoc -> TcS ()
traceTcS "solveImplication {" (Implication -> SDoc
forall a. Outputable a => a -> SDoc
ppr Implication
imp SDoc -> SDoc -> SDoc
$$ String -> SDoc
text "Inerts" SDoc -> SDoc -> SDoc
<+> InertSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr InertSet
inerts)
; (no_given_eqs :: Bool
no_given_eqs, given_insols :: Cts
given_insols, residual_wanted :: WantedConstraints
residual_wanted)
<- EvBindsVar
-> TcLevel
-> TcS (Bool, Cts, WantedConstraints)
-> TcS (Bool, Cts, WantedConstraints)
forall a. EvBindsVar -> TcLevel -> TcS a -> TcS a
nestImplicTcS EvBindsVar
ev_binds_var TcLevel
tclvl (TcS (Bool, Cts, WantedConstraints)
-> TcS (Bool, Cts, WantedConstraints))
-> TcS (Bool, Cts, WantedConstraints)
-> TcS (Bool, Cts, WantedConstraints)
forall a b. (a -> b) -> a -> b
$
do { let loc :: CtLoc
loc = TcLevel -> SkolemInfo -> TcLclEnv -> CtLoc
mkGivenLoc TcLevel
tclvl SkolemInfo
info (Implication -> TcLclEnv
implicLclEnv Implication
imp)
givens :: [Ct]
givens = CtLoc -> [TcTyCoVar] -> [Ct]
mkGivens CtLoc
loc [TcTyCoVar]
given_ids
; [Ct] -> TcS ()
solveSimpleGivens [Ct]
givens
; WantedConstraints
residual_wanted <- WantedConstraints -> TcS WantedConstraints
solveWanteds WantedConstraints
wanteds
; (no_eqs :: Bool
no_eqs, given_insols :: Cts
given_insols) <- TcLevel -> [TcTyCoVar] -> TcS (Bool, Cts)
getNoGivenEqs TcLevel
tclvl [TcTyCoVar]
skols
; (Bool, Cts, WantedConstraints)
-> TcS (Bool, Cts, WantedConstraints)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
no_eqs, Cts
given_insols, WantedConstraints
residual_wanted) }
; (floated_eqs :: Cts
floated_eqs, residual_wanted :: WantedConstraints
residual_wanted)
<- [TcTyCoVar]
-> [TcTyCoVar]
-> EvBindsVar
-> Bool
-> WantedConstraints
-> TcS (Cts, WantedConstraints)
floatEqualities [TcTyCoVar]
skols [TcTyCoVar]
given_ids EvBindsVar
ev_binds_var
Bool
no_given_eqs WantedConstraints
residual_wanted
; String -> SDoc -> TcS ()
traceTcS "solveImplication 2"
(Cts -> SDoc
forall a. Outputable a => a -> SDoc
ppr Cts
given_insols SDoc -> SDoc -> SDoc
$$ WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
residual_wanted)
; let final_wanted :: WantedConstraints
final_wanted = WantedConstraints
residual_wanted WantedConstraints -> Cts -> WantedConstraints
`addInsols` Cts
given_insols
; Maybe Implication
res_implic <- Implication -> TcS (Maybe Implication)
setImplicationStatus (Implication
imp { ic_no_eqs :: Bool
ic_no_eqs = Bool
no_given_eqs
, ic_wanted :: WantedConstraints
ic_wanted = WantedConstraints
final_wanted })
; EvBindMap
evbinds <- EvBindsVar -> TcS EvBindMap
TcS.getTcEvBindsMap EvBindsVar
ev_binds_var
; TcTyVarSet
tcvs <- EvBindsVar -> TcS TcTyVarSet
TcS.getTcEvTyCoVars EvBindsVar
ev_binds_var
; String -> SDoc -> TcS ()
traceTcS "solveImplication end }" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat
[ String -> SDoc
text "no_given_eqs =" SDoc -> SDoc -> SDoc
<+> Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bool
no_given_eqs
, String -> SDoc
text "floated_eqs =" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
forall a. Outputable a => a -> SDoc
ppr Cts
floated_eqs
, String -> SDoc
text "res_implic =" SDoc -> SDoc -> SDoc
<+> Maybe Implication -> SDoc
forall a. Outputable a => a -> SDoc
ppr Maybe Implication
res_implic
, String -> SDoc
text "implication evbinds =" SDoc -> SDoc -> SDoc
<+> Bag EvBind -> SDoc
forall a. Outputable a => a -> SDoc
ppr (EvBindMap -> Bag EvBind
evBindMapBinds EvBindMap
evbinds)
, String -> SDoc
text "implication tvcs =" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
tcvs ]
; (Cts, Maybe Implication) -> TcS (Cts, Maybe Implication)
forall (m :: * -> *) a. Monad m => a -> m a
return (Cts
floated_eqs, Maybe Implication
res_implic) }
where
setImplicationStatus :: Implication -> TcS (Maybe Implication)
setImplicationStatus :: Implication -> TcS (Maybe Implication)
setImplicationStatus implic :: Implication
implic@(Implic { ic_status :: Implication -> ImplicStatus
ic_status = ImplicStatus
status
, ic_info :: Implication -> SkolemInfo
ic_info = SkolemInfo
info
, ic_wanted :: Implication -> WantedConstraints
ic_wanted = WantedConstraints
wc
, ic_given :: Implication -> [TcTyCoVar]
ic_given = [TcTyCoVar]
givens })
| ASSERT2( not (isSolvedStatus status ), ppr info )
Bool -> Bool
not (WantedConstraints -> Bool
isSolvedWC WantedConstraints
pruned_wc)
= do { String -> SDoc -> TcS ()
traceTcS "setImplicationStatus(not-all-solved) {" (Implication -> SDoc
forall a. Outputable a => a -> SDoc
ppr Implication
implic)
; Implication
implic <- Implication -> TcS Implication
neededEvVars Implication
implic
; let new_status :: ImplicStatus
new_status | WantedConstraints -> Bool
insolubleWC WantedConstraints
pruned_wc = ImplicStatus
IC_Insoluble
| Bool
otherwise = ImplicStatus
IC_Unsolved
new_implic :: Implication
new_implic = Implication
implic { ic_status :: ImplicStatus
ic_status = ImplicStatus
new_status
, ic_wanted :: WantedConstraints
ic_wanted = WantedConstraints
pruned_wc }
; String -> SDoc -> TcS ()
traceTcS "setImplicationStatus(not-all-solved) }" (Implication -> SDoc
forall a. Outputable a => a -> SDoc
ppr Implication
new_implic)
; Maybe Implication -> TcS (Maybe Implication)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Implication -> TcS (Maybe Implication))
-> Maybe Implication -> TcS (Maybe Implication)
forall a b. (a -> b) -> a -> b
$ Implication -> Maybe Implication
forall a. a -> Maybe a
Just Implication
new_implic }
| Bool
otherwise
= do { String -> SDoc -> TcS ()
traceTcS "setImplicationStatus(all-solved) {" (Implication -> SDoc
forall a. Outputable a => a -> SDoc
ppr Implication
implic)
; implic :: Implication
implic@(Implic { ic_need_inner :: Implication -> TcTyVarSet
ic_need_inner = TcTyVarSet
need_inner
, ic_need_outer :: Implication -> TcTyVarSet
ic_need_outer = TcTyVarSet
need_outer }) <- Implication -> TcS Implication
neededEvVars Implication
implic
; Bool
bad_telescope <- Implication -> TcS Bool
checkBadTelescope Implication
implic
; let dead_givens :: [TcTyCoVar]
dead_givens | SkolemInfo -> Bool
warnRedundantGivens SkolemInfo
info
= (TcTyCoVar -> Bool) -> [TcTyCoVar] -> [TcTyCoVar]
forall a. (a -> Bool) -> [a] -> [a]
filterOut (TcTyCoVar -> TcTyVarSet -> Bool
`elemVarSet` TcTyVarSet
need_inner) [TcTyCoVar]
givens
| Bool
otherwise = []
discard_entire_implication :: Bool
discard_entire_implication
= [TcTyCoVar] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TcTyCoVar]
dead_givens
Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
bad_telescope
Bool -> Bool -> Bool
&& WantedConstraints -> Bool
isEmptyWC WantedConstraints
pruned_wc
Bool -> Bool -> Bool
&& TcTyVarSet -> Bool
isEmptyVarSet TcTyVarSet
need_outer
final_status :: ImplicStatus
final_status
| Bool
bad_telescope = ImplicStatus
IC_BadTelescope
| Bool
otherwise = IC_Solved :: [TcTyCoVar] -> ImplicStatus
IC_Solved { ics_dead :: [TcTyCoVar]
ics_dead = [TcTyCoVar]
dead_givens }
final_implic :: Implication
final_implic = Implication
implic { ic_status :: ImplicStatus
ic_status = ImplicStatus
final_status
, ic_wanted :: WantedConstraints
ic_wanted = WantedConstraints
pruned_wc }
; String -> SDoc -> TcS ()
traceTcS "setImplicationStatus(all-solved) }" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text "discard:" SDoc -> SDoc -> SDoc
<+> Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bool
discard_entire_implication
, String -> SDoc
text "new_implic:" SDoc -> SDoc -> SDoc
<+> Implication -> SDoc
forall a. Outputable a => a -> SDoc
ppr Implication
final_implic ]
; Maybe Implication -> TcS (Maybe Implication)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Implication -> TcS (Maybe Implication))
-> Maybe Implication -> TcS (Maybe Implication)
forall a b. (a -> b) -> a -> b
$ if Bool
discard_entire_implication
then Maybe Implication
forall a. Maybe a
Nothing
else Implication -> Maybe Implication
forall a. a -> Maybe a
Just Implication
final_implic }
where
WC { wc_simple :: WantedConstraints -> Cts
wc_simple = Cts
simples, wc_impl :: WantedConstraints -> Bag Implication
wc_impl = Bag Implication
implics } = WantedConstraints
wc
pruned_simples :: Cts
pruned_simples = Cts -> Cts
dropDerivedSimples Cts
simples
pruned_implics :: Bag Implication
pruned_implics = (Implication -> Bool) -> Bag Implication -> Bag Implication
forall a. (a -> Bool) -> Bag a -> Bag a
filterBag Implication -> Bool
keep_me Bag Implication
implics
pruned_wc :: WantedConstraints
pruned_wc = WC :: Cts -> Bag Implication -> WantedConstraints
WC { wc_simple :: Cts
wc_simple = Cts
pruned_simples
, wc_impl :: Bag Implication
wc_impl = Bag Implication
pruned_implics }
keep_me :: Implication -> Bool
keep_me :: Implication -> Bool
keep_me ic :: Implication
ic
| IC_Solved { ics_dead :: ImplicStatus -> [TcTyCoVar]
ics_dead = [TcTyCoVar]
dead_givens } <- Implication -> ImplicStatus
ic_status Implication
ic
, [TcTyCoVar] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TcTyCoVar]
dead_givens
, Bag Implication -> Bool
forall a. Bag a -> Bool
isEmptyBag (WantedConstraints -> Bag Implication
wc_impl (Implication -> WantedConstraints
ic_wanted Implication
ic))
= Bool
False
| Bool
otherwise
= Bool
True
checkBadTelescope :: Implication -> TcS Bool
checkBadTelescope :: Implication -> TcS Bool
checkBadTelescope (Implic { ic_telescope :: Implication -> Maybe SDoc
ic_telescope = Maybe SDoc
m_telescope
, ic_skols :: Implication -> [TcTyCoVar]
ic_skols = [TcTyCoVar]
skols })
| Maybe SDoc -> Bool
forall a. Maybe a -> Bool
isJust Maybe SDoc
m_telescope
= do{ [TcTyCoVar]
skols <- (TcTyCoVar -> TcS TcTyCoVar) -> [TcTyCoVar] -> TcS [TcTyCoVar]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TcTyCoVar -> TcS TcTyCoVar
TcS.zonkTyCoVarKind [TcTyCoVar]
skols
; Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (TcTyVarSet -> [TcTyCoVar] -> Bool
go TcTyVarSet
emptyVarSet ([TcTyCoVar] -> [TcTyCoVar]
forall a. [a] -> [a]
reverse [TcTyCoVar]
skols))}
| Bool
otherwise
= Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
where
go :: TyVarSet
-> [TcTyVar]
-> Bool
go :: TcTyVarSet -> [TcTyCoVar] -> Bool
go _ [] = Bool
False
go later_skols :: TcTyVarSet
later_skols (one_skol :: TcTyCoVar
one_skol : earlier_skols :: [TcTyCoVar]
earlier_skols)
| Type -> TcTyVarSet
tyCoVarsOfType (TcTyCoVar -> Type
tyVarKind TcTyCoVar
one_skol) TcTyVarSet -> TcTyVarSet -> Bool
`intersectsVarSet` TcTyVarSet
later_skols
= Bool
True
| Bool
otherwise
= TcTyVarSet -> [TcTyCoVar] -> Bool
go (TcTyVarSet
later_skols TcTyVarSet -> TcTyCoVar -> TcTyVarSet
`extendVarSet` TcTyCoVar
one_skol) [TcTyCoVar]
earlier_skols
warnRedundantGivens :: SkolemInfo -> Bool
warnRedundantGivens :: SkolemInfo -> Bool
warnRedundantGivens (SigSkol ctxt :: UserTypeCtxt
ctxt _ _)
= case UserTypeCtxt
ctxt of
FunSigCtxt _ warn_redundant :: Bool
warn_redundant -> Bool
warn_redundant
ExprSigCtxt -> Bool
True
_ -> Bool
False
warnRedundantGivens (InstSkol {}) = Bool
True
warnRedundantGivens _ = Bool
False
neededEvVars :: Implication -> TcS Implication
neededEvVars :: Implication -> TcS Implication
neededEvVars implic :: Implication
implic@(Implic { ic_given :: Implication -> [TcTyCoVar]
ic_given = [TcTyCoVar]
givens
, ic_binds :: Implication -> EvBindsVar
ic_binds = EvBindsVar
ev_binds_var
, ic_wanted :: Implication -> WantedConstraints
ic_wanted = WC { wc_impl :: WantedConstraints -> Bag Implication
wc_impl = Bag Implication
implics }
, ic_need_inner :: Implication -> TcTyVarSet
ic_need_inner = TcTyVarSet
old_needs })
= do { EvBindMap
ev_binds <- EvBindsVar -> TcS EvBindMap
TcS.getTcEvBindsMap EvBindsVar
ev_binds_var
; TcTyVarSet
tcvs <- EvBindsVar -> TcS TcTyVarSet
TcS.getTcEvTyCoVars EvBindsVar
ev_binds_var
; let seeds1 :: TcTyVarSet
seeds1 = (Implication -> TcTyVarSet -> TcTyVarSet)
-> TcTyVarSet -> Bag Implication -> TcTyVarSet
forall a r. (a -> r -> r) -> r -> Bag a -> r
foldrBag Implication -> TcTyVarSet -> TcTyVarSet
add_implic_seeds TcTyVarSet
old_needs Bag Implication
implics
seeds2 :: TcTyVarSet
seeds2 = (EvBind -> TcTyVarSet -> TcTyVarSet)
-> TcTyVarSet -> EvBindMap -> TcTyVarSet
forall a. (EvBind -> a -> a) -> a -> EvBindMap -> a
foldEvBindMap EvBind -> TcTyVarSet -> TcTyVarSet
add_wanted TcTyVarSet
seeds1 EvBindMap
ev_binds
seeds3 :: TcTyVarSet
seeds3 = TcTyVarSet
seeds2 TcTyVarSet -> TcTyVarSet -> TcTyVarSet
`unionVarSet` TcTyVarSet
tcvs
need_inner :: TcTyVarSet
need_inner = EvBindMap -> TcTyVarSet -> TcTyVarSet
findNeededEvVars EvBindMap
ev_binds TcTyVarSet
seeds3
live_ev_binds :: EvBindMap
live_ev_binds = (EvBind -> Bool) -> EvBindMap -> EvBindMap
filterEvBindMap (TcTyVarSet -> EvBind -> Bool
needed_ev_bind TcTyVarSet
need_inner) EvBindMap
ev_binds
need_outer :: TcTyVarSet
need_outer = (EvBind -> TcTyVarSet -> TcTyVarSet)
-> TcTyVarSet -> EvBindMap -> TcTyVarSet
forall a. (EvBind -> a -> a) -> a -> EvBindMap -> a
foldEvBindMap EvBind -> TcTyVarSet -> TcTyVarSet
del_ev_bndr TcTyVarSet
need_inner EvBindMap
live_ev_binds
TcTyVarSet -> [TcTyCoVar] -> TcTyVarSet
`delVarSetList` [TcTyCoVar]
givens
; EvBindsVar -> EvBindMap -> TcS ()
TcS.setTcEvBindsMap EvBindsVar
ev_binds_var EvBindMap
live_ev_binds
; String -> SDoc -> TcS ()
traceTcS "neededEvVars" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text "old_needs:" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
old_needs
, String -> SDoc
text "seeds3:" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
seeds3
, String -> SDoc
text "tcvs:" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
tcvs
, String -> SDoc
text "ev_binds:" SDoc -> SDoc -> SDoc
<+> EvBindMap -> SDoc
forall a. Outputable a => a -> SDoc
ppr EvBindMap
ev_binds
, String -> SDoc
text "live_ev_binds:" SDoc -> SDoc -> SDoc
<+> EvBindMap -> SDoc
forall a. Outputable a => a -> SDoc
ppr EvBindMap
live_ev_binds ]
; Implication -> TcS Implication
forall (m :: * -> *) a. Monad m => a -> m a
return (Implication
implic { ic_need_inner :: TcTyVarSet
ic_need_inner = TcTyVarSet
need_inner
, ic_need_outer :: TcTyVarSet
ic_need_outer = TcTyVarSet
need_outer }) }
where
add_implic_seeds :: Implication -> TcTyVarSet -> TcTyVarSet
add_implic_seeds (Implic { ic_need_outer :: Implication -> TcTyVarSet
ic_need_outer = TcTyVarSet
needs }) acc :: TcTyVarSet
acc
= TcTyVarSet
needs TcTyVarSet -> TcTyVarSet -> TcTyVarSet
`unionVarSet` TcTyVarSet
acc
needed_ev_bind :: TcTyVarSet -> EvBind -> Bool
needed_ev_bind needed :: TcTyVarSet
needed (EvBind { eb_lhs :: EvBind -> TcTyCoVar
eb_lhs = TcTyCoVar
ev_var
, eb_is_given :: EvBind -> Bool
eb_is_given = Bool
is_given })
| Bool
is_given = TcTyCoVar
ev_var TcTyCoVar -> TcTyVarSet -> Bool
`elemVarSet` TcTyVarSet
needed
| Bool
otherwise = Bool
True
del_ev_bndr :: EvBind -> VarSet -> VarSet
del_ev_bndr :: EvBind -> TcTyVarSet -> TcTyVarSet
del_ev_bndr (EvBind { eb_lhs :: EvBind -> TcTyCoVar
eb_lhs = TcTyCoVar
v }) needs :: TcTyVarSet
needs = TcTyVarSet -> TcTyCoVar -> TcTyVarSet
delVarSet TcTyVarSet
needs TcTyCoVar
v
add_wanted :: EvBind -> VarSet -> VarSet
add_wanted :: EvBind -> TcTyVarSet -> TcTyVarSet
add_wanted (EvBind { eb_is_given :: EvBind -> Bool
eb_is_given = Bool
is_given, eb_rhs :: EvBind -> EvTerm
eb_rhs = EvTerm
rhs }) needs :: TcTyVarSet
needs
| Bool
is_given = TcTyVarSet
needs
| Bool
otherwise = EvTerm -> TcTyVarSet
evVarsOfTerm EvTerm
rhs TcTyVarSet -> TcTyVarSet -> TcTyVarSet
`unionVarSet` TcTyVarSet
needs
promoteTyVar :: TcTyVar -> TcM (Bool, TcTyVar)
promoteTyVar :: TcTyCoVar -> TcM (Bool, TcTyCoVar)
promoteTyVar tv :: TcTyCoVar
tv
= do { TcLevel
tclvl <- TcM TcLevel
TcM.getTcLevel
; if (TcLevel -> TcTyCoVar -> Bool
isFloatedTouchableMetaTyVar TcLevel
tclvl TcTyCoVar
tv)
then do { TcTyCoVar
cloned_tv <- TcTyCoVar -> IOEnv (Env TcGblEnv TcLclEnv) TcTyCoVar
TcM.cloneMetaTyVar TcTyCoVar
tv
; let rhs_tv :: TcTyCoVar
rhs_tv = TcTyCoVar -> TcLevel -> TcTyCoVar
setMetaTyVarTcLevel TcTyCoVar
cloned_tv TcLevel
tclvl
; TcTyCoVar -> Type -> TcM ()
TcM.writeMetaTyVar TcTyCoVar
tv (TcTyCoVar -> Type
mkTyVarTy TcTyCoVar
rhs_tv)
; (Bool, TcTyCoVar) -> TcM (Bool, TcTyCoVar)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
True, TcTyCoVar
rhs_tv) }
else (Bool, TcTyCoVar) -> TcM (Bool, TcTyCoVar)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False, TcTyCoVar
tv) }
promoteTyVarSet :: TcTyVarSet -> TcM (Bool, TcTyVarSet)
promoteTyVarSet :: TcTyVarSet -> TcM (Bool, TcTyVarSet)
promoteTyVarSet tvs :: TcTyVarSet
tvs
= do { (bools :: [Bool]
bools, tyvars :: [TcTyCoVar]
tyvars) <- (TcTyCoVar -> TcM (Bool, TcTyCoVar))
-> [TcTyCoVar]
-> IOEnv (Env TcGblEnv TcLclEnv) ([Bool], [TcTyCoVar])
forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM TcTyCoVar -> TcM (Bool, TcTyCoVar)
promoteTyVar (TcTyVarSet -> [TcTyCoVar]
forall elt. UniqSet elt -> [elt]
nonDetEltsUniqSet TcTyVarSet
tvs)
; (Bool, TcTyVarSet) -> TcM (Bool, TcTyVarSet)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Bool]
bools, [TcTyCoVar] -> TcTyVarSet
mkVarSet [TcTyCoVar]
tyvars) }
promoteTyVarTcS :: TcTyVar -> TcS ()
promoteTyVarTcS :: TcTyCoVar -> TcS ()
promoteTyVarTcS tv :: TcTyCoVar
tv
= do { TcLevel
tclvl <- TcS TcLevel
TcS.getTcLevel
; Bool -> TcS () -> TcS ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TcLevel -> TcTyCoVar -> Bool
isFloatedTouchableMetaTyVar TcLevel
tclvl TcTyCoVar
tv) (TcS () -> TcS ()) -> TcS () -> TcS ()
forall a b. (a -> b) -> a -> b
$
do { TcTyCoVar
cloned_tv <- TcTyCoVar -> TcS TcTyCoVar
TcS.cloneMetaTyVar TcTyCoVar
tv
; let rhs_tv :: TcTyCoVar
rhs_tv = TcTyCoVar -> TcLevel -> TcTyCoVar
setMetaTyVarTcLevel TcTyCoVar
cloned_tv TcLevel
tclvl
; TcTyCoVar -> Type -> TcS ()
unifyTyVar TcTyCoVar
tv (TcTyCoVar -> Type
mkTyVarTy TcTyCoVar
rhs_tv) } }
defaultTyVarTcS :: TcTyVar -> TcS Bool
defaultTyVarTcS :: TcTyCoVar -> TcS Bool
defaultTyVarTcS the_tv :: TcTyCoVar
the_tv
| TcTyCoVar -> Bool
isRuntimeRepVar TcTyCoVar
the_tv
, Bool -> Bool
not (TcTyCoVar -> Bool
isTyVarTyVar TcTyCoVar
the_tv)
= do { String -> SDoc -> TcS ()
traceTcS "defaultTyVarTcS RuntimeRep" (TcTyCoVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyCoVar
the_tv)
; TcTyCoVar -> Type -> TcS ()
unifyTyVar TcTyCoVar
the_tv Type
liftedRepTy
; 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
approximateWC :: Bool -> WantedConstraints -> Cts
approximateWC :: Bool -> WantedConstraints -> Cts
approximateWC float_past_equalities :: Bool
float_past_equalities wc :: WantedConstraints
wc
= TcTyVarSet -> WantedConstraints -> Cts
float_wc TcTyVarSet
emptyVarSet WantedConstraints
wc
where
float_wc :: TcTyCoVarSet -> WantedConstraints -> Cts
float_wc :: TcTyVarSet -> WantedConstraints -> Cts
float_wc trapping_tvs :: TcTyVarSet
trapping_tvs (WC { wc_simple :: WantedConstraints -> Cts
wc_simple = Cts
simples, wc_impl :: WantedConstraints -> Bag Implication
wc_impl = Bag Implication
implics })
= (Ct -> Bool) -> Cts -> Cts
forall a. (a -> Bool) -> Bag a -> Bag a
filterBag (TcTyVarSet -> Ct -> Bool
is_floatable TcTyVarSet
trapping_tvs) Cts
simples Cts -> Cts -> Cts
forall a. Bag a -> Bag a -> Bag a
`unionBags`
(Implication -> Cts) -> Bag Implication -> Cts
forall a c. (a -> Bag c) -> Bag a -> Bag c
do_bag (TcTyVarSet -> Implication -> Cts
float_implic TcTyVarSet
trapping_tvs) Bag Implication
implics
where
float_implic :: TcTyCoVarSet -> Implication -> Cts
float_implic :: TcTyVarSet -> Implication -> Cts
float_implic trapping_tvs :: TcTyVarSet
trapping_tvs imp :: Implication
imp
| Bool
float_past_equalities Bool -> Bool -> Bool
|| Implication -> Bool
ic_no_eqs Implication
imp
= TcTyVarSet -> WantedConstraints -> Cts
float_wc TcTyVarSet
new_trapping_tvs (Implication -> WantedConstraints
ic_wanted Implication
imp)
| Bool
otherwise
= Cts
emptyCts
where
new_trapping_tvs :: TcTyVarSet
new_trapping_tvs = TcTyVarSet
trapping_tvs TcTyVarSet -> [TcTyCoVar] -> TcTyVarSet
`extendVarSetList` Implication -> [TcTyCoVar]
ic_skols Implication
imp
do_bag :: (a -> Bag c) -> Bag a -> Bag c
do_bag :: (a -> Bag c) -> Bag a -> Bag c
do_bag f :: a -> Bag c
f = (a -> Bag c -> Bag c) -> Bag c -> Bag a -> Bag c
forall a r. (a -> r -> r) -> r -> Bag a -> r
foldrBag (Bag c -> Bag c -> Bag c
forall a. Bag a -> Bag a -> Bag a
unionBags(Bag c -> Bag c -> Bag c) -> (a -> Bag c) -> a -> Bag c -> Bag c
forall b c a. (b -> c) -> (a -> b) -> a -> c
.a -> Bag c
f) Bag c
forall a. Bag a
emptyBag
is_floatable :: TcTyVarSet -> Ct -> Bool
is_floatable skol_tvs :: TcTyVarSet
skol_tvs ct :: Ct
ct
| Ct -> Bool
isGivenCt Ct
ct = Bool
False
| Ct -> Bool
isHoleCt Ct
ct = Bool
False
| Ct -> Bool
insolubleEqCt Ct
ct = Bool
False
| Bool
otherwise = Ct -> TcTyVarSet
tyCoVarsOfCt Ct
ct TcTyVarSet -> TcTyVarSet -> Bool
`disjointVarSet` TcTyVarSet
skol_tvs
floatEqualities :: [TcTyVar] -> [EvId] -> EvBindsVar -> Bool
-> WantedConstraints
-> TcS (Cts, WantedConstraints)
floatEqualities :: [TcTyCoVar]
-> [TcTyCoVar]
-> EvBindsVar
-> Bool
-> WantedConstraints
-> TcS (Cts, WantedConstraints)
floatEqualities skols :: [TcTyCoVar]
skols given_ids :: [TcTyCoVar]
given_ids ev_binds_var :: EvBindsVar
ev_binds_var no_given_eqs :: Bool
no_given_eqs
wanteds :: WantedConstraints
wanteds@(WC { wc_simple :: WantedConstraints -> Cts
wc_simple = Cts
simples })
| Bool -> Bool
not Bool
no_given_eqs
= (Cts, WantedConstraints) -> TcS (Cts, WantedConstraints)
forall (m :: * -> *) a. Monad m => a -> m a
return (Cts
forall a. Bag a
emptyBag, WantedConstraints
wanteds)
| Bool
otherwise
= do {
Cts
simples <- Cts -> TcS Cts
TcS.zonkSimples Cts
simples
; EvBindMap
binds <- EvBindsVar -> TcS EvBindMap
TcS.getTcEvBindsMap EvBindsVar
ev_binds_var
; let (candidate_eqs :: Cts
candidate_eqs, no_float_cts :: Cts
no_float_cts) = (Ct -> Bool) -> Cts -> (Cts, Cts)
forall a. (a -> Bool) -> Bag a -> (Bag a, Bag a)
partitionBag Ct -> Bool
is_float_eq_candidate Cts
simples
seed_skols :: TcTyVarSet
seed_skols = [TcTyCoVar] -> TcTyVarSet
mkVarSet [TcTyCoVar]
skols TcTyVarSet -> TcTyVarSet -> TcTyVarSet
`unionVarSet`
[TcTyCoVar] -> TcTyVarSet
mkVarSet [TcTyCoVar]
given_ids TcTyVarSet -> TcTyVarSet -> TcTyVarSet
`unionVarSet`
(Ct -> TcTyVarSet -> TcTyVarSet) -> TcTyVarSet -> Cts -> TcTyVarSet
forall a r. (a -> r -> r) -> r -> Bag a -> r
foldrBag Ct -> TcTyVarSet -> TcTyVarSet
add_non_flt_ct TcTyVarSet
emptyVarSet Cts
no_float_cts TcTyVarSet -> TcTyVarSet -> TcTyVarSet
`unionVarSet`
(EvBind -> TcTyVarSet -> TcTyVarSet)
-> TcTyVarSet -> EvBindMap -> TcTyVarSet
forall a. (EvBind -> a -> a) -> a -> EvBindMap -> a
foldEvBindMap EvBind -> TcTyVarSet -> TcTyVarSet
add_one_bind TcTyVarSet
emptyVarSet EvBindMap
binds
extended_skols :: TcTyVarSet
extended_skols = (TcTyVarSet -> TcTyVarSet) -> TcTyVarSet -> TcTyVarSet
transCloVarSet (Cts -> TcTyVarSet -> TcTyVarSet
add_captured_ev_ids Cts
candidate_eqs) TcTyVarSet
seed_skols
(flt_eqs :: Cts
flt_eqs, no_flt_eqs :: Cts
no_flt_eqs) = (Ct -> Bool) -> Cts -> (Cts, Cts)
forall a. (a -> Bool) -> Bag a -> (Bag a, Bag a)
partitionBag (TcTyVarSet -> Ct -> Bool
is_floatable TcTyVarSet
extended_skols)
Cts
candidate_eqs
remaining_simples :: Cts
remaining_simples = Cts
no_float_cts Cts -> Cts -> Cts
`andCts` Cts
no_flt_eqs
; (TcTyCoVar -> TcS ()) -> [TcTyCoVar] -> TcS ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ TcTyCoVar -> TcS ()
promoteTyVarTcS (Cts -> [TcTyCoVar]
tyCoVarsOfCtsList Cts
flt_eqs)
; String -> SDoc -> TcS ()
traceTcS "floatEqualities" ([SDoc] -> SDoc
vcat [ String -> SDoc
text "Skols =" SDoc -> SDoc -> SDoc
<+> [TcTyCoVar] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcTyCoVar]
skols
, String -> SDoc
text "Extended skols =" SDoc -> SDoc -> SDoc
<+> TcTyVarSet -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVarSet
extended_skols
, String -> SDoc
text "Simples =" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
forall a. Outputable a => a -> SDoc
ppr Cts
simples
, String -> SDoc
text "Candidate eqs =" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
forall a. Outputable a => a -> SDoc
ppr Cts
candidate_eqs
, String -> SDoc
text "Floated eqs =" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
forall a. Outputable a => a -> SDoc
ppr Cts
flt_eqs])
; (Cts, WantedConstraints) -> TcS (Cts, WantedConstraints)
forall (m :: * -> *) a. Monad m => a -> m a
return ( Cts
flt_eqs, WantedConstraints
wanteds { wc_simple :: Cts
wc_simple = Cts
remaining_simples } ) }
where
add_one_bind :: EvBind -> VarSet -> VarSet
add_one_bind :: EvBind -> TcTyVarSet -> TcTyVarSet
add_one_bind bind :: EvBind
bind acc :: TcTyVarSet
acc = TcTyVarSet -> TcTyCoVar -> TcTyVarSet
extendVarSet TcTyVarSet
acc (EvBind -> TcTyCoVar
evBindVar EvBind
bind)
add_non_flt_ct :: Ct -> VarSet -> VarSet
add_non_flt_ct :: Ct -> TcTyVarSet -> TcTyVarSet
add_non_flt_ct ct :: Ct
ct acc :: TcTyVarSet
acc | Ct -> Bool
isDerivedCt Ct
ct = TcTyVarSet
acc
| Bool
otherwise = TcTyVarSet -> TcTyCoVar -> TcTyVarSet
extendVarSet TcTyVarSet
acc (Ct -> TcTyCoVar
ctEvId Ct
ct)
is_floatable :: VarSet -> Ct -> Bool
is_floatable :: TcTyVarSet -> Ct -> Bool
is_floatable skols :: TcTyVarSet
skols ct :: Ct
ct
| Ct -> Bool
isDerivedCt Ct
ct = Bool -> Bool
not (Ct -> TcTyVarSet
tyCoVarsOfCt Ct
ct TcTyVarSet -> TcTyVarSet -> Bool
`intersectsVarSet` TcTyVarSet
skols)
| Bool
otherwise = Bool -> Bool
not (Ct -> TcTyCoVar
ctEvId Ct
ct TcTyCoVar -> TcTyVarSet -> Bool
`elemVarSet` TcTyVarSet
skols)
add_captured_ev_ids :: Cts -> VarSet -> VarSet
add_captured_ev_ids :: Cts -> TcTyVarSet -> TcTyVarSet
add_captured_ev_ids cts :: Cts
cts skols :: TcTyVarSet
skols = (Ct -> TcTyVarSet -> TcTyVarSet) -> TcTyVarSet -> Cts -> TcTyVarSet
forall a r. (a -> r -> r) -> r -> Bag a -> r
foldrBag Ct -> TcTyVarSet -> TcTyVarSet
extra_skol TcTyVarSet
emptyVarSet Cts
cts
where
extra_skol :: Ct -> TcTyVarSet -> TcTyVarSet
extra_skol ct :: Ct
ct acc :: TcTyVarSet
acc
| Ct -> Bool
isDerivedCt Ct
ct = TcTyVarSet
acc
| Ct -> TcTyVarSet
tyCoVarsOfCt Ct
ct TcTyVarSet -> TcTyVarSet -> Bool
`intersectsVarSet` TcTyVarSet
skols = TcTyVarSet -> TcTyCoVar -> TcTyVarSet
extendVarSet TcTyVarSet
acc (Ct -> TcTyCoVar
ctEvId Ct
ct)
| Bool
otherwise = TcTyVarSet
acc
is_float_eq_candidate :: Ct -> Bool
is_float_eq_candidate ct :: Ct
ct
| Type
pred <- Ct -> Type
ctPred Ct
ct
, EqPred NomEq ty1 :: Type
ty1 ty2 :: Type
ty2 <- Type -> PredTree
classifyPredType Type
pred
, HasDebugCallStack => Type -> Type
Type -> Type
tcTypeKind Type
ty1 HasDebugCallStack => Type -> Type -> Bool
Type -> Type -> Bool
`tcEqType` HasDebugCallStack => Type -> Type
Type -> Type
tcTypeKind Type
ty2
= case (Type -> Maybe TcTyCoVar
tcGetTyVar_maybe Type
ty1, Type -> Maybe TcTyCoVar
tcGetTyVar_maybe Type
ty2) of
(Just tv1 :: TcTyCoVar
tv1, _) -> TcTyCoVar -> Type -> Bool
float_tv_eq_candidate TcTyCoVar
tv1 Type
ty2
(_, Just tv2 :: TcTyCoVar
tv2) -> TcTyCoVar -> Type -> Bool
float_tv_eq_candidate TcTyCoVar
tv2 Type
ty1
_ -> Bool
False
| Bool
otherwise = Bool
False
float_tv_eq_candidate :: TcTyCoVar -> Type -> Bool
float_tv_eq_candidate tv1 :: TcTyCoVar
tv1 ty2 :: Type
ty2
= TcTyCoVar -> Bool
isMetaTyVar TcTyCoVar
tv1
Bool -> Bool -> Bool
&& (Bool -> Bool
not (TcTyCoVar -> Bool
isTyVarTyVar TcTyCoVar
tv1) Bool -> Bool -> Bool
|| Type -> Bool
isTyVarTy Type
ty2)
applyDefaultingRules :: WantedConstraints -> TcS Bool
applyDefaultingRules :: WantedConstraints -> TcS Bool
applyDefaultingRules wanteds :: WantedConstraints
wanteds
| WantedConstraints -> Bool
isEmptyWC WantedConstraints
wanteds
= Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
| Bool
otherwise
= do { info :: ([Type], (Bool, Bool))
info@(default_tys :: [Type]
default_tys, _) <- TcS ([Type], (Bool, Bool))
getDefaultInfo
; WantedConstraints
wanteds <- WantedConstraints -> TcS WantedConstraints
TcS.zonkWC WantedConstraints
wanteds
; let groups :: [(TcTyCoVar, [Ct])]
groups = ([Type], (Bool, Bool)) -> WantedConstraints -> [(TcTyCoVar, [Ct])]
findDefaultableGroups ([Type], (Bool, Bool))
info WantedConstraints
wanteds
; String -> SDoc -> TcS ()
traceTcS "applyDefaultingRules {" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text "wanteds =" SDoc -> SDoc -> SDoc
<+> WantedConstraints -> SDoc
forall a. Outputable a => a -> SDoc
ppr WantedConstraints
wanteds
, String -> SDoc
text "groups =" SDoc -> SDoc -> SDoc
<+> [(TcTyCoVar, [Ct])] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [(TcTyCoVar, [Ct])]
groups
, String -> SDoc
text "info =" SDoc -> SDoc -> SDoc
<+> ([Type], (Bool, Bool)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr ([Type], (Bool, Bool))
info ]
; [Bool]
something_happeneds <- ((TcTyCoVar, [Ct]) -> TcS Bool)
-> [(TcTyCoVar, [Ct])] -> TcS [Bool]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([Type] -> (TcTyCoVar, [Ct]) -> TcS Bool
disambigGroup [Type]
default_tys) [(TcTyCoVar, [Ct])]
groups
; String -> SDoc -> TcS ()
traceTcS "applyDefaultingRules }" ([Bool] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Bool]
something_happeneds)
; Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return ([Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Bool]
something_happeneds) }
findDefaultableGroups
:: ( [Type]
, (Bool,Bool) )
-> WantedConstraints
-> [(TyVar, [Ct])]
findDefaultableGroups :: ([Type], (Bool, Bool)) -> WantedConstraints -> [(TcTyCoVar, [Ct])]
findDefaultableGroups (default_tys :: [Type]
default_tys, (ovl_strings :: Bool
ovl_strings, extended_defaults :: Bool
extended_defaults)) wanteds :: WantedConstraints
wanteds
| [Type] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Type]
default_tys
= []
| Bool
otherwise
= [ (TcTyCoVar
tv, ((Ct, Class, TcTyCoVar) -> Ct) -> [(Ct, Class, TcTyCoVar)] -> [Ct]
forall a b. (a -> b) -> [a] -> [b]
map (Ct, Class, TcTyCoVar) -> Ct
forall a b c. (a, b, c) -> a
fstOf3 [(Ct, Class, TcTyCoVar)]
group)
| group' :: NonEmpty (Ct, Class, TcTyCoVar)
group'@((_,_,tv :: TcTyCoVar
tv) :| _) <- [NonEmpty (Ct, Class, TcTyCoVar)]
unary_groups
, let group :: [(Ct, Class, TcTyCoVar)]
group = NonEmpty (Ct, Class, TcTyCoVar) -> [(Ct, Class, TcTyCoVar)]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty (Ct, Class, TcTyCoVar)
group'
, TcTyCoVar -> Bool
defaultable_tyvar TcTyCoVar
tv
, [Class] -> Bool
defaultable_classes (((Ct, Class, TcTyCoVar) -> Class)
-> [(Ct, Class, TcTyCoVar)] -> [Class]
forall a b. (a -> b) -> [a] -> [b]
map (Ct, Class, TcTyCoVar) -> Class
forall a b c. (a, b, c) -> b
sndOf3 [(Ct, Class, TcTyCoVar)]
group) ]
where
simples :: Cts
simples = Bool -> WantedConstraints -> Cts
approximateWC Bool
True WantedConstraints
wanteds
(unaries :: [(Ct, Class, TcTyCoVar)]
unaries, non_unaries :: [Ct]
non_unaries) = (Ct -> Either (Ct, Class, TcTyCoVar) Ct)
-> [Ct] -> ([(Ct, Class, TcTyCoVar)], [Ct])
forall a b c. (a -> Either b c) -> [a] -> ([b], [c])
partitionWith Ct -> Either (Ct, Class, TcTyCoVar) Ct
find_unary (Cts -> [Ct]
forall a. Bag a -> [a]
bagToList Cts
simples)
unary_groups :: [NonEmpty (Ct, Class, TcTyCoVar)]
unary_groups = ((Ct, Class, TcTyCoVar) -> (Ct, Class, TcTyCoVar) -> Ordering)
-> [(Ct, Class, TcTyCoVar)] -> [NonEmpty (Ct, Class, TcTyCoVar)]
forall a. (a -> a -> Ordering) -> [a] -> [NonEmpty a]
equivClasses (Ct, Class, TcTyCoVar) -> (Ct, Class, TcTyCoVar) -> Ordering
forall a a b a b. Ord a => (a, b, a) -> (a, b, a) -> Ordering
cmp_tv [(Ct, Class, TcTyCoVar)]
unaries
unary_groups :: [NonEmpty (Ct, Class, TcTyVar)]
unaries :: [(Ct, Class, TcTyVar)]
non_unaries :: [Ct]
find_unary :: Ct -> Either (Ct, Class, TyVar) Ct
find_unary :: Ct -> Either (Ct, Class, TcTyCoVar) Ct
find_unary cc :: Ct
cc
| Just (cls :: Class
cls,tys :: [Type]
tys) <- Type -> Maybe (Class, [Type])
getClassPredTys_maybe (Ct -> Type
ctPred Ct
cc)
, [ty :: Type
ty] <- TyCon -> [Type] -> [Type]
filterOutInvisibleTypes (Class -> TyCon
classTyCon Class
cls) [Type]
tys
, Just tv :: TcTyCoVar
tv <- Type -> Maybe TcTyCoVar
tcGetTyVar_maybe Type
ty
, TcTyCoVar -> Bool
isMetaTyVar TcTyCoVar
tv
= (Ct, Class, TcTyCoVar) -> Either (Ct, Class, TcTyCoVar) Ct
forall a b. a -> Either a b
Left (Ct
cc, Class
cls, TcTyCoVar
tv)
find_unary cc :: Ct
cc = Ct -> Either (Ct, Class, TcTyCoVar) Ct
forall a b. b -> Either a b
Right Ct
cc
bad_tvs :: TcTyCoVarSet
bad_tvs :: TcTyVarSet
bad_tvs = (Ct -> TcTyVarSet) -> [Ct] -> TcTyVarSet
forall a. (a -> TcTyVarSet) -> [a] -> TcTyVarSet
mapUnionVarSet Ct -> TcTyVarSet
tyCoVarsOfCt [Ct]
non_unaries
cmp_tv :: (a, b, a) -> (a, b, a) -> Ordering
cmp_tv (_,_,tv1 :: a
tv1) (_,_,tv2 :: a
tv2) = a
tv1 a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` a
tv2
defaultable_tyvar :: TcTyVar -> Bool
defaultable_tyvar :: TcTyCoVar -> Bool
defaultable_tyvar tv :: TcTyCoVar
tv
= let b1 :: Bool
b1 = TcTyCoVar -> Bool
isTyConableTyVar TcTyCoVar
tv
b2 :: Bool
b2 = Bool -> Bool
not (TcTyCoVar
tv TcTyCoVar -> TcTyVarSet -> Bool
`elemVarSet` TcTyVarSet
bad_tvs)
in Bool
b1 Bool -> Bool -> Bool
&& (Bool
b2 Bool -> Bool -> Bool
|| Bool
extended_defaults)
defaultable_classes :: [Class] -> Bool
defaultable_classes :: [Class] -> Bool
defaultable_classes clss :: [Class]
clss
| Bool
extended_defaults = (Class -> Bool) -> [Class] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Bool -> Class -> Bool
isInteractiveClass Bool
ovl_strings) [Class]
clss
| Bool
otherwise = (Class -> Bool) -> [Class] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Class -> Bool
is_std_class [Class]
clss Bool -> Bool -> Bool
&& ((Class -> Bool) -> [Class] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Bool -> Class -> Bool
isNumClass Bool
ovl_strings) [Class]
clss)
is_std_class :: Class -> Bool
is_std_class cls :: Class
cls = Class -> Bool
isStandardClass Class
cls Bool -> Bool -> Bool
||
(Bool
ovl_strings Bool -> Bool -> Bool
&& (Class
cls Class -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
isStringClassKey))
disambigGroup :: [Type]
-> (TcTyVar, [Ct])
-> TcS Bool
disambigGroup :: [Type] -> (TcTyCoVar, [Ct]) -> TcS Bool
disambigGroup [] _
= Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
disambigGroup (default_ty :: Type
default_ty:default_tys :: [Type]
default_tys) group :: (TcTyCoVar, [Ct])
group@(the_tv :: TcTyCoVar
the_tv, wanteds :: [Ct]
wanteds)
= do { String -> SDoc -> TcS ()
traceTcS "disambigGroup {" ([SDoc] -> SDoc
vcat [ Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
default_ty, TcTyCoVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyCoVar
the_tv, [Ct] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Ct]
wanteds ])
; EvBindsVar
fake_ev_binds_var <- TcS EvBindsVar
TcS.newTcEvBinds
; TcLevel
tclvl <- TcS TcLevel
TcS.getTcLevel
; Bool
success <- EvBindsVar -> TcLevel -> TcS Bool -> TcS Bool
forall a. EvBindsVar -> TcLevel -> TcS a -> TcS a
nestImplicTcS EvBindsVar
fake_ev_binds_var (TcLevel -> TcLevel
pushTcLevel TcLevel
tclvl) TcS Bool
try_group
; if Bool
success then
do { TcTyCoVar -> Type -> TcS ()
unifyTyVar TcTyCoVar
the_tv Type
default_ty
; TcM () -> TcS ()
forall a. TcM a -> TcS a
wrapWarnTcS (TcM () -> TcS ()) -> TcM () -> TcS ()
forall a b. (a -> b) -> a -> b
$ [Ct] -> Type -> TcM ()
warnDefaulting [Ct]
wanteds Type
default_ty
; String -> SDoc -> TcS ()
traceTcS "disambigGroup succeeded }" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
default_ty)
; Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True }
else
do { String -> SDoc -> TcS ()
traceTcS "disambigGroup failed, will try other default types }"
(Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
default_ty)
; [Type] -> (TcTyCoVar, [Ct]) -> TcS Bool
disambigGroup [Type]
default_tys (TcTyCoVar, [Ct])
group } }
where
try_group :: TcS Bool
try_group
| Just subst :: TCvSubst
subst <- Maybe TCvSubst
mb_subst
= do { TcLclEnv
lcl_env <- TcS TcLclEnv
TcS.getLclEnv
; TcLevel
tc_lvl <- TcS TcLevel
TcS.getTcLevel
; let loc :: CtLoc
loc = TcLevel -> SkolemInfo -> TcLclEnv -> CtLoc
mkGivenLoc TcLevel
tc_lvl SkolemInfo
UnkSkol TcLclEnv
lcl_env
; [CtEvidence]
wanted_evs <- (Ct -> TcS CtEvidence) -> [Ct] -> TcS [CtEvidence]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (CtLoc -> Type -> TcS CtEvidence
newWantedEvVarNC CtLoc
loc (Type -> TcS CtEvidence) -> (Ct -> Type) -> Ct -> TcS CtEvidence
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy TCvSubst
subst (Type -> Type) -> (Ct -> Type) -> Ct -> Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ct -> Type
ctPred)
[Ct]
wanteds
; (WantedConstraints -> Bool) -> TcS WantedConstraints -> TcS Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap WantedConstraints -> Bool
isEmptyWC (TcS WantedConstraints -> TcS Bool)
-> TcS WantedConstraints -> TcS Bool
forall a b. (a -> b) -> a -> b
$
Cts -> TcS WantedConstraints
solveSimpleWanteds (Cts -> TcS WantedConstraints) -> Cts -> TcS WantedConstraints
forall a b. (a -> b) -> a -> b
$ [Ct] -> Cts
forall a. [a] -> Bag a
listToBag ([Ct] -> Cts) -> [Ct] -> Cts
forall a b. (a -> b) -> a -> b
$
(CtEvidence -> Ct) -> [CtEvidence] -> [Ct]
forall a b. (a -> b) -> [a] -> [b]
map CtEvidence -> Ct
mkNonCanonical [CtEvidence]
wanted_evs }
| Bool
otherwise
= Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
the_ty :: Type
the_ty = TcTyCoVar -> Type
mkTyVarTy TcTyCoVar
the_tv
mb_subst :: Maybe TCvSubst
mb_subst = Type -> Type -> Maybe TCvSubst
tcMatchTyKi Type
the_ty Type
default_ty
isInteractiveClass :: Bool
-> Class -> Bool
isInteractiveClass :: Bool -> Class -> Bool
isInteractiveClass ovl_strings :: Bool
ovl_strings cls :: Class
cls
= Bool -> Class -> Bool
isNumClass Bool
ovl_strings Class
cls Bool -> Bool -> Bool
|| (Class -> Unique
classKey Class
cls Unique -> [Unique] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Unique]
interactiveClassKeys)
isNumClass :: Bool
-> Class -> Bool
isNumClass :: Bool -> Class -> Bool
isNumClass ovl_strings :: Bool
ovl_strings cls :: Class
cls
= Class -> Bool
isNumericClass Class
cls Bool -> Bool -> Bool
|| (Bool
ovl_strings Bool -> Bool -> Bool
&& (Class
cls Class -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
isStringClassKey))