{-# LANGUAGE CPP, DeriveFunctor, ViewPatterns, BangPatterns #-}
{-# OPTIONS_GHC -Wno-incomplete-record-updates #-}
module GHC.Tc.Solver.Flatten(
FlattenMode(..),
flatten, flattenKind, flattenArgsNom,
rewriteTyVar, flattenType,
unflattenWanteds
) where
#include "HsVersions.h"
import GHC.Prelude
import GHC.Tc.Types
import GHC.Core.TyCo.Ppr ( pprTyVar )
import GHC.Tc.Types.Constraint
import GHC.Core.Predicate
import GHC.Tc.Utils.TcType
import GHC.Core.Type
import GHC.Tc.Types.Evidence
import GHC.Core.TyCon
import GHC.Core.TyCo.Rep
import GHC.Core.Coercion
import GHC.Types.Var
import GHC.Types.Var.Set
import GHC.Types.Var.Env
import GHC.Utils.Outputable
import GHC.Tc.Solver.Monad as TcS
import GHC.Types.Basic( SwapFlag(..) )
import GHC.Utils.Misc
import GHC.Data.Bag
import Control.Monad
import GHC.Utils.Monad ( zipWith3M )
import Data.Foldable ( foldrM )
import Control.Arrow ( first )
type FlatWorkListRef = TcRef [Ct]
data FlattenEnv
= FE { FlattenEnv -> FlattenMode
fe_mode :: !FlattenMode
, FlattenEnv -> CtLoc
fe_loc :: CtLoc
, FlattenEnv -> CtFlavour
fe_flavour :: !CtFlavour
, FlattenEnv -> EqRel
fe_eq_rel :: !EqRel
, FlattenEnv -> FlatWorkListRef
fe_work :: !FlatWorkListRef }
data FlattenMode
= FM_FlattenAll
| FM_SubstOnly
instance Outputable FlattenMode where
ppr :: FlattenMode -> SDoc
ppr FlattenMode
FM_FlattenAll = String -> SDoc
text String
"FM_FlattenAll"
ppr FlattenMode
FM_SubstOnly = String -> SDoc
text String
"FM_SubstOnly"
eqFlattenMode :: FlattenMode -> FlattenMode -> Bool
eqFlattenMode :: FlattenMode -> FlattenMode -> Bool
eqFlattenMode FlattenMode
FM_FlattenAll FlattenMode
FM_FlattenAll = Bool
True
eqFlattenMode FlattenMode
FM_SubstOnly FlattenMode
FM_SubstOnly = Bool
True
eqFlattenMode FlattenMode
_ FlattenMode
_ = Bool
False
newtype FlatM a
= FlatM { forall a. FlatM a -> FlattenEnv -> TcS a
runFlatM :: FlattenEnv -> TcS a }
deriving ((forall a b. (a -> b) -> FlatM a -> FlatM b)
-> (forall a b. a -> FlatM b -> FlatM a) -> Functor FlatM
forall a b. a -> FlatM b -> FlatM a
forall a b. (a -> b) -> FlatM a -> FlatM b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> FlatM b -> FlatM a
$c<$ :: forall a b. a -> FlatM b -> FlatM a
fmap :: forall a b. (a -> b) -> FlatM a -> FlatM b
$cfmap :: forall a b. (a -> b) -> FlatM a -> FlatM b
Functor)
instance Monad FlatM where
FlatM a
m >>= :: forall a b. FlatM a -> (a -> FlatM b) -> FlatM b
>>= a -> FlatM b
k = (FlattenEnv -> TcS b) -> FlatM b
forall a. (FlattenEnv -> TcS a) -> FlatM a
FlatM ((FlattenEnv -> TcS b) -> FlatM b)
-> (FlattenEnv -> TcS b) -> FlatM b
forall a b. (a -> b) -> a -> b
$ \FlattenEnv
env ->
do { a
a <- FlatM a -> FlattenEnv -> TcS a
forall a. FlatM a -> FlattenEnv -> TcS a
runFlatM FlatM a
m FlattenEnv
env
; FlatM b -> FlattenEnv -> TcS b
forall a. FlatM a -> FlattenEnv -> TcS a
runFlatM (a -> FlatM b
k a
a) FlattenEnv
env }
instance Applicative FlatM where
pure :: forall a. a -> FlatM a
pure a
x = (FlattenEnv -> TcS a) -> FlatM a
forall a. (FlattenEnv -> TcS a) -> FlatM a
FlatM ((FlattenEnv -> TcS a) -> FlatM a)
-> (FlattenEnv -> TcS a) -> FlatM a
forall a b. (a -> b) -> a -> b
$ TcS a -> FlattenEnv -> TcS a
forall a b. a -> b -> a
const (a -> TcS a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
<*> :: forall a b. FlatM (a -> b) -> FlatM a -> FlatM b
(<*>) = FlatM (a -> b) -> FlatM a -> FlatM b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
liftTcS :: TcS a -> FlatM a
liftTcS :: forall a. TcS a -> FlatM a
liftTcS TcS a
thing_inside
= (FlattenEnv -> TcS a) -> FlatM a
forall a. (FlattenEnv -> TcS a) -> FlatM a
FlatM ((FlattenEnv -> TcS a) -> FlatM a)
-> (FlattenEnv -> TcS a) -> FlatM a
forall a b. (a -> b) -> a -> b
$ TcS a -> FlattenEnv -> TcS a
forall a b. a -> b -> a
const TcS a
thing_inside
emitFlatWork :: Ct -> FlatM ()
emitFlatWork :: Ct -> FlatM ()
emitFlatWork Ct
ct = (FlattenEnv -> TcS ()) -> FlatM ()
forall a. (FlattenEnv -> TcS a) -> FlatM a
FlatM ((FlattenEnv -> TcS ()) -> FlatM ())
-> (FlattenEnv -> TcS ()) -> FlatM ()
forall a b. (a -> b) -> a -> b
$ \FlattenEnv
env -> FlatWorkListRef -> ([Ct] -> [Ct]) -> TcS ()
forall a. TcRef a -> (a -> a) -> TcS ()
updTcRef (FlattenEnv -> FlatWorkListRef
fe_work FlattenEnv
env) (Ct
ct Ct -> [Ct] -> [Ct]
forall a. a -> [a] -> [a]
:)
runFlattenCtEv :: FlattenMode -> CtEvidence -> FlatM a -> TcS a
runFlattenCtEv :: forall a. FlattenMode -> CtEvidence -> FlatM a -> TcS a
runFlattenCtEv FlattenMode
mode CtEvidence
ev
= FlattenMode -> CtLoc -> CtFlavour -> EqRel -> FlatM a -> TcS a
forall a.
FlattenMode -> CtLoc -> CtFlavour -> EqRel -> FlatM a -> TcS a
runFlatten FlattenMode
mode (CtEvidence -> CtLoc
ctEvLoc CtEvidence
ev) (CtEvidence -> CtFlavour
ctEvFlavour CtEvidence
ev) (CtEvidence -> EqRel
ctEvEqRel CtEvidence
ev)
runFlatten :: FlattenMode -> CtLoc -> CtFlavour -> EqRel -> FlatM a -> TcS a
runFlatten :: forall a.
FlattenMode -> CtLoc -> CtFlavour -> EqRel -> FlatM a -> TcS a
runFlatten FlattenMode
mode CtLoc
loc CtFlavour
flav EqRel
eq_rel FlatM a
thing_inside
= do { FlatWorkListRef
flat_ref <- [Ct] -> TcS FlatWorkListRef
forall a. a -> TcS (TcRef a)
newTcRef []
; let fmode :: FlattenEnv
fmode = FE :: FlattenMode
-> CtLoc -> CtFlavour -> EqRel -> FlatWorkListRef -> FlattenEnv
FE { fe_mode :: FlattenMode
fe_mode = FlattenMode
mode
, fe_loc :: CtLoc
fe_loc = CtLoc -> CtLoc
bumpCtLocDepth CtLoc
loc
, fe_flavour :: CtFlavour
fe_flavour = CtFlavour
flav
, fe_eq_rel :: EqRel
fe_eq_rel = EqRel
eq_rel
, fe_work :: FlatWorkListRef
fe_work = FlatWorkListRef
flat_ref }
; a
res <- FlatM a -> FlattenEnv -> TcS a
forall a. FlatM a -> FlattenEnv -> TcS a
runFlatM FlatM a
thing_inside FlattenEnv
fmode
; [Ct]
new_flats <- FlatWorkListRef -> TcS [Ct]
forall a. TcRef a -> TcS a
readTcRef FlatWorkListRef
flat_ref
; (WorkList -> WorkList) -> TcS ()
updWorkListTcS ([Ct] -> WorkList -> WorkList
add_flats [Ct]
new_flats)
; a -> TcS a
forall (m :: * -> *) a. Monad m => a -> m a
return a
res }
where
add_flats :: [Ct] -> WorkList -> WorkList
add_flats [Ct]
new_flats WorkList
wl
= WorkList
wl { wl_funeqs :: [Ct]
wl_funeqs = [Ct] -> [Ct] -> [Ct]
forall {a}. [a] -> [a] -> [a]
add_funeqs [Ct]
new_flats (WorkList -> [Ct]
wl_funeqs WorkList
wl) }
add_funeqs :: [a] -> [a] -> [a]
add_funeqs [] [a]
wl = [a]
wl
add_funeqs (a
f:[a]
fs) [a]
wl = [a] -> [a] -> [a]
add_funeqs [a]
fs (a
fa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
wl)
traceFlat :: String -> SDoc -> FlatM ()
traceFlat :: String -> SDoc -> FlatM ()
traceFlat String
herald SDoc
doc = TcS () -> FlatM ()
forall a. TcS a -> FlatM a
liftTcS (TcS () -> FlatM ()) -> TcS () -> FlatM ()
forall a b. (a -> b) -> a -> b
$ String -> SDoc -> TcS ()
traceTcS String
herald SDoc
doc
{-# INLINE traceFlat #-}
getFlatEnvField :: (FlattenEnv -> a) -> FlatM a
getFlatEnvField :: forall a. (FlattenEnv -> a) -> FlatM a
getFlatEnvField FlattenEnv -> a
accessor
= (FlattenEnv -> TcS a) -> FlatM a
forall a. (FlattenEnv -> TcS a) -> FlatM a
FlatM ((FlattenEnv -> TcS a) -> FlatM a)
-> (FlattenEnv -> TcS a) -> FlatM a
forall a b. (a -> b) -> a -> b
$ \FlattenEnv
env -> a -> TcS a
forall (m :: * -> *) a. Monad m => a -> m a
return (FlattenEnv -> a
accessor FlattenEnv
env)
getEqRel :: FlatM EqRel
getEqRel :: FlatM EqRel
getEqRel = (FlattenEnv -> EqRel) -> FlatM EqRel
forall a. (FlattenEnv -> a) -> FlatM a
getFlatEnvField FlattenEnv -> EqRel
fe_eq_rel
getRole :: FlatM Role
getRole :: FlatM Role
getRole = EqRel -> Role
eqRelRole (EqRel -> Role) -> FlatM EqRel -> FlatM Role
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FlatM EqRel
getEqRel
getFlavour :: FlatM CtFlavour
getFlavour :: FlatM CtFlavour
getFlavour = (FlattenEnv -> CtFlavour) -> FlatM CtFlavour
forall a. (FlattenEnv -> a) -> FlatM a
getFlatEnvField FlattenEnv -> CtFlavour
fe_flavour
getFlavourRole :: FlatM CtFlavourRole
getFlavourRole :: FlatM CtFlavourRole
getFlavourRole
= do { CtFlavour
flavour <- FlatM CtFlavour
getFlavour
; EqRel
eq_rel <- FlatM EqRel
getEqRel
; CtFlavourRole -> FlatM CtFlavourRole
forall (m :: * -> *) a. Monad m => a -> m a
return (CtFlavour
flavour, EqRel
eq_rel) }
getMode :: FlatM FlattenMode
getMode :: FlatM FlattenMode
getMode = (FlattenEnv -> FlattenMode) -> FlatM FlattenMode
forall a. (FlattenEnv -> a) -> FlatM a
getFlatEnvField FlattenEnv -> FlattenMode
fe_mode
getLoc :: FlatM CtLoc
getLoc :: FlatM CtLoc
getLoc = (FlattenEnv -> CtLoc) -> FlatM CtLoc
forall a. (FlattenEnv -> a) -> FlatM a
getFlatEnvField FlattenEnv -> CtLoc
fe_loc
checkStackDepth :: Type -> FlatM ()
checkStackDepth :: TcType -> FlatM ()
checkStackDepth TcType
ty
= do { CtLoc
loc <- FlatM CtLoc
getLoc
; TcS () -> FlatM ()
forall a. TcS a -> FlatM a
liftTcS (TcS () -> FlatM ()) -> TcS () -> FlatM ()
forall a b. (a -> b) -> a -> b
$ CtLoc -> TcType -> TcS ()
checkReductionDepth CtLoc
loc TcType
ty }
setEqRel :: EqRel -> FlatM a -> FlatM a
setEqRel :: forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
new_eq_rel FlatM a
thing_inside
= (FlattenEnv -> TcS a) -> FlatM a
forall a. (FlattenEnv -> TcS a) -> FlatM a
FlatM ((FlattenEnv -> TcS a) -> FlatM a)
-> (FlattenEnv -> TcS a) -> FlatM a
forall a b. (a -> b) -> a -> b
$ \FlattenEnv
env ->
if EqRel
new_eq_rel EqRel -> EqRel -> Bool
forall a. Eq a => a -> a -> Bool
== FlattenEnv -> EqRel
fe_eq_rel FlattenEnv
env
then FlatM a -> FlattenEnv -> TcS a
forall a. FlatM a -> FlattenEnv -> TcS a
runFlatM FlatM a
thing_inside FlattenEnv
env
else FlatM a -> FlattenEnv -> TcS a
forall a. FlatM a -> FlattenEnv -> TcS a
runFlatM FlatM a
thing_inside (FlattenEnv
env { fe_eq_rel :: EqRel
fe_eq_rel = EqRel
new_eq_rel })
setMode :: FlattenMode -> FlatM a -> FlatM a
setMode :: forall a. FlattenMode -> FlatM a -> FlatM a
setMode FlattenMode
new_mode FlatM a
thing_inside
= (FlattenEnv -> TcS a) -> FlatM a
forall a. (FlattenEnv -> TcS a) -> FlatM a
FlatM ((FlattenEnv -> TcS a) -> FlatM a)
-> (FlattenEnv -> TcS a) -> FlatM a
forall a b. (a -> b) -> a -> b
$ \FlattenEnv
env ->
if FlattenMode
new_mode FlattenMode -> FlattenMode -> Bool
`eqFlattenMode` FlattenEnv -> FlattenMode
fe_mode FlattenEnv
env
then FlatM a -> FlattenEnv -> TcS a
forall a. FlatM a -> FlattenEnv -> TcS a
runFlatM FlatM a
thing_inside FlattenEnv
env
else FlatM a -> FlattenEnv -> TcS a
forall a. FlatM a -> FlattenEnv -> TcS a
runFlatM FlatM a
thing_inside (FlattenEnv
env { fe_mode :: FlattenMode
fe_mode = FlattenMode
new_mode })
noBogusCoercions :: FlatM a -> FlatM a
noBogusCoercions :: forall a. FlatM a -> FlatM a
noBogusCoercions FlatM a
thing_inside
= (FlattenEnv -> TcS a) -> FlatM a
forall a. (FlattenEnv -> TcS a) -> FlatM a
FlatM ((FlattenEnv -> TcS a) -> FlatM a)
-> (FlattenEnv -> TcS a) -> FlatM a
forall a b. (a -> b) -> a -> b
$ \FlattenEnv
env ->
let !env' :: FlattenEnv
env' = case FlattenEnv -> CtFlavour
fe_flavour FlattenEnv
env of
CtFlavour
Derived -> FlattenEnv
env { fe_flavour :: CtFlavour
fe_flavour = ShadowInfo -> CtFlavour
Wanted ShadowInfo
WDeriv }
CtFlavour
_ -> FlattenEnv
env
in
FlatM a -> FlattenEnv -> TcS a
forall a. FlatM a -> FlattenEnv -> TcS a
runFlatM FlatM a
thing_inside FlattenEnv
env'
bumpDepth :: FlatM a -> FlatM a
bumpDepth :: forall a. FlatM a -> FlatM a
bumpDepth (FlatM FlattenEnv -> TcS a
thing_inside)
= (FlattenEnv -> TcS a) -> FlatM a
forall a. (FlattenEnv -> TcS a) -> FlatM a
FlatM ((FlattenEnv -> TcS a) -> FlatM a)
-> (FlattenEnv -> TcS a) -> FlatM a
forall a b. (a -> b) -> a -> b
$ \FlattenEnv
env -> do
{ let !env' :: FlattenEnv
env' = FlattenEnv
env { fe_loc :: CtLoc
fe_loc = CtLoc -> CtLoc
bumpCtLocDepth (FlattenEnv -> CtLoc
fe_loc FlattenEnv
env) }
; FlattenEnv -> TcS a
thing_inside FlattenEnv
env' }
flatten :: FlattenMode -> CtEvidence -> TcType
-> TcS (Xi, TcCoercion)
flatten :: FlattenMode -> CtEvidence -> TcType -> TcS (TcType, CoercionN)
flatten FlattenMode
mode CtEvidence
ev TcType
ty
= do { String -> SDoc -> TcS ()
traceTcS String
"flatten {" (FlattenMode -> SDoc
forall a. Outputable a => a -> SDoc
ppr FlattenMode
mode SDoc -> SDoc -> SDoc
<+> TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcType
ty)
; (TcType
ty', CoercionN
co) <- FlattenMode
-> CtEvidence
-> FlatM (TcType, CoercionN)
-> TcS (TcType, CoercionN)
forall a. FlattenMode -> CtEvidence -> FlatM a -> TcS a
runFlattenCtEv FlattenMode
mode CtEvidence
ev (TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
ty)
; String -> SDoc -> TcS ()
traceTcS String
"flatten }" (TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcType
ty')
; (TcType, CoercionN) -> TcS (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
ty', CoercionN
co) }
rewriteTyVar :: TcTyVar -> TcS TcType
rewriteTyVar :: TcTyVar -> TcS TcType
rewriteTyVar TcTyVar
tv
= do { String -> SDoc -> TcS ()
traceTcS String
"rewriteTyVar {" (TcTyVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVar
tv)
; (TcType
ty, CoercionN
_) <- FlattenMode
-> CtLoc
-> CtFlavour
-> EqRel
-> FlatM (TcType, CoercionN)
-> TcS (TcType, CoercionN)
forall a.
FlattenMode -> CtLoc -> CtFlavour -> EqRel -> FlatM a -> TcS a
runFlatten FlattenMode
FM_SubstOnly CtLoc
forall {a}. a
fake_loc CtFlavour
Derived EqRel
NomEq (FlatM (TcType, CoercionN) -> TcS (TcType, CoercionN))
-> FlatM (TcType, CoercionN) -> TcS (TcType, CoercionN)
forall a b. (a -> b) -> a -> b
$
TcTyVar -> FlatM (TcType, CoercionN)
flattenTyVar TcTyVar
tv
; String -> SDoc -> TcS ()
traceTcS String
"rewriteTyVar }" (TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcType
ty)
; TcType -> TcS TcType
forall (m :: * -> *) a. Monad m => a -> m a
return TcType
ty }
where
fake_loc :: a
fake_loc = String -> SDoc -> a
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"rewriteTyVar used a CtLoc" (TcTyVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVar
tv)
flattenKind :: CtLoc -> CtFlavour -> TcType -> TcS (Xi, TcCoercionN)
flattenKind :: CtLoc -> CtFlavour -> TcType -> TcS (TcType, CoercionN)
flattenKind CtLoc
loc CtFlavour
flav TcType
ty
= do { String -> SDoc -> TcS ()
traceTcS String
"flattenKind {" (CtFlavour -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtFlavour
flav SDoc -> SDoc -> SDoc
<+> TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcType
ty)
; let flav' :: CtFlavour
flav' = case CtFlavour
flav of
CtFlavour
Derived -> ShadowInfo -> CtFlavour
Wanted ShadowInfo
WDeriv
CtFlavour
_ -> CtFlavour
flav
; (TcType
ty', CoercionN
co) <- FlattenMode
-> CtLoc
-> CtFlavour
-> EqRel
-> FlatM (TcType, CoercionN)
-> TcS (TcType, CoercionN)
forall a.
FlattenMode -> CtLoc -> CtFlavour -> EqRel -> FlatM a -> TcS a
runFlatten FlattenMode
FM_FlattenAll CtLoc
loc CtFlavour
flav' EqRel
NomEq (TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
ty)
; String -> SDoc -> TcS ()
traceTcS String
"flattenKind }" (TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcType
ty' SDoc -> SDoc -> SDoc
$$ CoercionN -> SDoc
forall a. Outputable a => a -> SDoc
ppr CoercionN
co)
; (TcType, CoercionN) -> TcS (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
ty', CoercionN
co) }
flattenArgsNom :: CtEvidence -> TyCon -> [TcType] -> TcS ([Xi], [TcCoercion], TcCoercionN)
flattenArgsNom :: CtEvidence
-> TyCon -> [TcType] -> TcS ([TcType], [CoercionN], CoercionN)
flattenArgsNom CtEvidence
ev TyCon
tc [TcType]
tys
= do { String -> SDoc -> TcS ()
traceTcS String
"flatten_args {" ([SDoc] -> SDoc
vcat ((TcType -> SDoc) -> [TcType] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcType]
tys))
; ([TcType]
tys', [CoercionN]
cos, CoercionN
kind_co)
<- FlattenMode
-> CtEvidence
-> FlatM ([TcType], [CoercionN], CoercionN)
-> TcS ([TcType], [CoercionN], CoercionN)
forall a. FlattenMode -> CtEvidence -> FlatM a -> TcS a
runFlattenCtEv FlattenMode
FM_FlattenAll CtEvidence
ev (TyCon
-> [Role] -> [TcType] -> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args_tc TyCon
tc (Role -> [Role]
forall a. a -> [a]
repeat Role
Nominal) [TcType]
tys)
; String -> SDoc -> TcS ()
traceTcS String
"flatten }" ([SDoc] -> SDoc
vcat ((TcType -> SDoc) -> [TcType] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcType]
tys'))
; ([TcType], [CoercionN], CoercionN)
-> TcS ([TcType], [CoercionN], CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return ([TcType]
tys', [CoercionN]
cos, CoercionN
kind_co) }
flattenType :: CtLoc -> TcType -> TcS TcType
flattenType :: CtLoc -> TcType -> TcS TcType
flattenType CtLoc
loc TcType
ty
= do { (TcType
xi, CoercionN
_) <- FlattenMode
-> CtLoc
-> CtFlavour
-> EqRel
-> FlatM (TcType, CoercionN)
-> TcS (TcType, CoercionN)
forall a.
FlattenMode -> CtLoc -> CtFlavour -> EqRel -> FlatM a -> TcS a
runFlatten FlattenMode
FM_SubstOnly CtLoc
loc CtFlavour
Given EqRel
NomEq (FlatM (TcType, CoercionN) -> TcS (TcType, CoercionN))
-> FlatM (TcType, CoercionN) -> TcS (TcType, CoercionN)
forall a b. (a -> b) -> a -> b
$
TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
ty
; TcType -> TcS TcType
forall (m :: * -> *) a. Monad m => a -> m a
return TcType
xi }
{-# INLINE flatten_args_tc #-}
flatten_args_tc
:: TyCon
-> [Role]
-> [Type]
-> FlatM ( [Xi]
, [Coercion]
, CoercionN)
flatten_args_tc :: TyCon
-> [Role] -> [TcType] -> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args_tc TyCon
tc = [TyCoBinder]
-> Bool
-> TcType
-> TcTyCoVarSet
-> [Role]
-> [TcType]
-> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args [TyCoBinder]
all_bndrs Bool
any_named_bndrs TcType
inner_ki TcTyCoVarSet
emptyVarSet
where
([TyCoBinder]
bndrs, Bool
named)
= [TyConBinder] -> ([TyCoBinder], Bool)
ty_con_binders_ty_binders' (TyCon -> [TyConBinder]
tyConBinders TyCon
tc)
([TyCoBinder]
inner_bndrs, TcType
inner_ki, Bool
inner_named) = TcType -> ([TyCoBinder], TcType, Bool)
split_pi_tys' (TyCon -> TcType
tyConResKind TyCon
tc)
!all_bndrs :: [TyCoBinder]
all_bndrs = [TyCoBinder]
bndrs [TyCoBinder] -> [TyCoBinder] -> [TyCoBinder]
forall {a}. [a] -> [a] -> [a]
`chkAppend` [TyCoBinder]
inner_bndrs
!any_named_bndrs :: Bool
any_named_bndrs = Bool
named Bool -> Bool -> Bool
|| Bool
inner_named
{-# INLINE flatten_args #-}
flatten_args :: [TyCoBinder] -> Bool
-> Kind -> TcTyCoVarSet
-> [Role] -> [Type]
-> FlatM ([Xi], [Coercion], CoercionN)
flatten_args :: [TyCoBinder]
-> Bool
-> TcType
-> TcTyCoVarSet
-> [Role]
-> [TcType]
-> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args [TyCoBinder]
orig_binders
Bool
any_named_bndrs
TcType
orig_inner_ki
TcTyCoVarSet
orig_fvs
[Role]
orig_roles
[TcType]
orig_tys
= if Bool
any_named_bndrs
then [TyCoBinder]
-> TcType
-> TcTyCoVarSet
-> [Role]
-> [TcType]
-> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args_slow [TyCoBinder]
orig_binders
TcType
orig_inner_ki
TcTyCoVarSet
orig_fvs
[Role]
orig_roles
[TcType]
orig_tys
else [TyCoBinder]
-> TcType
-> [Role]
-> [TcType]
-> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args_fast [TyCoBinder]
orig_binders TcType
orig_inner_ki [Role]
orig_roles [TcType]
orig_tys
{-# INLINE flatten_args_fast #-}
flatten_args_fast :: [TyCoBinder]
-> Kind
-> [Role]
-> [Type]
-> FlatM ([Xi], [Coercion], CoercionN)
flatten_args_fast :: [TyCoBinder]
-> TcType
-> [Role]
-> [TcType]
-> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args_fast [TyCoBinder]
orig_binders TcType
orig_inner_ki [Role]
orig_roles [TcType]
orig_tys
= (([TcType], [CoercionN], [TyCoBinder])
-> ([TcType], [CoercionN], CoercionN))
-> FlatM ([TcType], [CoercionN], [TyCoBinder])
-> FlatM ([TcType], [CoercionN], CoercionN)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([TcType], [CoercionN], [TyCoBinder])
-> ([TcType], [CoercionN], CoercionN)
finish ([TcType]
-> [Role]
-> [TyCoBinder]
-> FlatM ([TcType], [CoercionN], [TyCoBinder])
iterate [TcType]
orig_tys [Role]
orig_roles [TyCoBinder]
orig_binders)
where
iterate :: [Type]
-> [Role]
-> [TyCoBinder]
-> FlatM ([Xi], [Coercion], [TyCoBinder])
iterate :: [TcType]
-> [Role]
-> [TyCoBinder]
-> FlatM ([TcType], [CoercionN], [TyCoBinder])
iterate (TcType
ty:[TcType]
tys) (Role
role:[Role]
roles) (TyCoBinder
_:[TyCoBinder]
binders) = do
(TcType
xi, CoercionN
co) <- Role -> TcType -> FlatM (TcType, CoercionN)
go Role
role TcType
ty
([TcType]
xis, [CoercionN]
cos, [TyCoBinder]
binders) <- [TcType]
-> [Role]
-> [TyCoBinder]
-> FlatM ([TcType], [CoercionN], [TyCoBinder])
iterate [TcType]
tys [Role]
roles [TyCoBinder]
binders
([TcType], [CoercionN], [TyCoBinder])
-> FlatM ([TcType], [CoercionN], [TyCoBinder])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TcType
xi TcType -> [TcType] -> [TcType]
forall a. a -> [a] -> [a]
: [TcType]
xis, CoercionN
co CoercionN -> [CoercionN] -> [CoercionN]
forall a. a -> [a] -> [a]
: [CoercionN]
cos, [TyCoBinder]
binders)
iterate [] [Role]
_ [TyCoBinder]
binders = ([TcType], [CoercionN], [TyCoBinder])
-> FlatM ([TcType], [CoercionN], [TyCoBinder])
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], [], [TyCoBinder]
binders)
iterate [TcType]
_ [Role]
_ [TyCoBinder]
_ = String -> SDoc -> FlatM ([TcType], [CoercionN], [TyCoBinder])
forall a. HasCallStack => String -> SDoc -> a
pprPanic
String
"flatten_args wandered into deeper water than usual" ([SDoc] -> SDoc
vcat [])
{-# INLINE go #-}
go :: Role
-> Type
-> FlatM (Xi, Coercion)
go :: Role -> TcType -> FlatM (TcType, CoercionN)
go Role
role TcType
ty
= case Role
role of
Role
Nominal -> EqRel -> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
NomEq (FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN))
-> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a b. (a -> b) -> a -> b
$ TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
ty
Role
Representational -> EqRel -> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
ReprEq (FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN))
-> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a b. (a -> b) -> a -> b
$ TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
ty
Role
Phantom ->
do { TcType
ty <- TcS TcType -> FlatM TcType
forall a. TcS a -> FlatM a
liftTcS (TcS TcType -> FlatM TcType) -> TcS TcType -> FlatM TcType
forall a b. (a -> b) -> a -> b
$ TcType -> TcS TcType
zonkTcType TcType
ty
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
ty, Role -> TcType -> CoercionN
mkReflCo Role
Phantom TcType
ty) }
{-# INLINE finish #-}
finish :: ([Xi], [Coercion], [TyCoBinder]) -> ([Xi], [Coercion], CoercionN)
finish :: ([TcType], [CoercionN], [TyCoBinder])
-> ([TcType], [CoercionN], CoercionN)
finish ([TcType]
xis, [CoercionN]
cos, [TyCoBinder]
binders) = ([TcType]
xis, [CoercionN]
cos, CoercionN
kind_co)
where
final_kind :: TcType
final_kind = [TyCoBinder] -> TcType -> TcType
mkPiTys [TyCoBinder]
binders TcType
orig_inner_ki
kind_co :: CoercionN
kind_co = TcType -> CoercionN
mkNomReflCo TcType
final_kind
{-# INLINE flatten_args_slow #-}
flatten_args_slow :: [TyCoBinder] -> Kind -> TcTyCoVarSet
-> [Role] -> [Type]
-> FlatM ([Xi], [Coercion], CoercionN)
flatten_args_slow :: [TyCoBinder]
-> TcType
-> TcTyCoVarSet
-> [Role]
-> [TcType]
-> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args_slow [TyCoBinder]
binders TcType
inner_ki TcTyCoVarSet
fvs [Role]
roles [TcType]
tys
= do { [(TcType, CoercionN)]
flattened_args <- (Bool -> Role -> TcType -> FlatM (TcType, CoercionN))
-> [Bool] -> [Role] -> [TcType] -> FlatM [(TcType, CoercionN)]
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Bool -> Role -> TcType -> FlatM (TcType, CoercionN)
fl ((TyCoBinder -> Bool) -> [TyCoBinder] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map TyCoBinder -> Bool
isNamedBinder [TyCoBinder]
binders [Bool] -> [Bool] -> [Bool]
forall {a}. [a] -> [a] -> [a]
++ Bool -> [Bool]
forall a. a -> [a]
repeat Bool
True)
[Role]
roles [TcType]
tys
; ([TcType], [CoercionN], CoercionN)
-> FlatM ([TcType], [CoercionN], CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return ([TyCoBinder]
-> TcType
-> TcTyCoVarSet
-> [Role]
-> [(TcType, CoercionN)]
-> ([TcType], [CoercionN], CoercionN)
simplifyArgsWorker [TyCoBinder]
binders TcType
inner_ki TcTyCoVarSet
fvs [Role]
roles [(TcType, CoercionN)]
flattened_args) }
where
{-# INLINE fl #-}
fl :: Bool
-> Role -> Type -> FlatM (Xi, Coercion)
fl :: Bool -> Role -> TcType -> FlatM (TcType, CoercionN)
fl Bool
True Role
r TcType
ty = FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a. FlatM a -> FlatM a
noBogusCoercions (FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN))
-> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a b. (a -> b) -> a -> b
$ Role -> TcType -> FlatM (TcType, CoercionN)
fl1 Role
r TcType
ty
fl Bool
False Role
r TcType
ty = Role -> TcType -> FlatM (TcType, CoercionN)
fl1 Role
r TcType
ty
{-# INLINE fl1 #-}
fl1 :: Role -> Type -> FlatM (Xi, Coercion)
fl1 :: Role -> TcType -> FlatM (TcType, CoercionN)
fl1 Role
Nominal TcType
ty
= EqRel -> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
NomEq (FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN))
-> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a b. (a -> b) -> a -> b
$
TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
ty
fl1 Role
Representational TcType
ty
= EqRel -> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
ReprEq (FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN))
-> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a b. (a -> b) -> a -> b
$
TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
ty
fl1 Role
Phantom TcType
ty
= do { TcType
ty <- TcS TcType -> FlatM TcType
forall a. TcS a -> FlatM a
liftTcS (TcS TcType -> FlatM TcType) -> TcS TcType -> FlatM TcType
forall a b. (a -> b) -> a -> b
$ TcType -> TcS TcType
zonkTcType TcType
ty
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
ty, Role -> TcType -> CoercionN
mkReflCo Role
Phantom TcType
ty) }
flatten_one :: TcType -> FlatM (Xi, Coercion)
flatten_one :: TcType -> FlatM (TcType, CoercionN)
flatten_one xi :: TcType
xi@(LitTy {})
= do { Role
role <- FlatM Role
getRole
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
xi, Role -> TcType -> CoercionN
mkReflCo Role
role TcType
xi) }
flatten_one (TyVarTy TcTyVar
tv)
= TcTyVar -> FlatM (TcType, CoercionN)
flattenTyVar TcTyVar
tv
flatten_one (AppTy TcType
ty1 TcType
ty2)
= TcType -> [TcType] -> FlatM (TcType, CoercionN)
flatten_app_tys TcType
ty1 [TcType
ty2]
flatten_one (TyConApp TyCon
tc [TcType]
tys)
| Just ([(TcTyVar, TcType)]
tenv, TcType
rhs, [TcType]
tys') <- TyCon -> [TcType] -> Maybe ([(TcTyVar, TcType)], TcType, [TcType])
forall tyco.
TyCon -> [tyco] -> Maybe ([(TcTyVar, tyco)], TcType, [tyco])
expandSynTyCon_maybe TyCon
tc [TcType]
tys
, let expanded_ty :: TcType
expanded_ty = TcType -> [TcType] -> TcType
mkAppTys (HasCallStack => TCvSubst -> TcType -> TcType
TCvSubst -> TcType -> TcType
substTy ([(TcTyVar, TcType)] -> TCvSubst
mkTvSubstPrs [(TcTyVar, TcType)]
tenv) TcType
rhs) [TcType]
tys'
= do { FlattenMode
mode <- FlatM FlattenMode
getMode
; case FlattenMode
mode of
FlattenMode
FM_FlattenAll | Bool -> Bool
not (TyCon -> Bool
isFamFreeTyCon TyCon
tc)
-> TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
expanded_ty
FlattenMode
_ -> TyCon -> [TcType] -> FlatM (TcType, CoercionN)
flatten_ty_con_app TyCon
tc [TcType]
tys }
| TyCon -> Bool
isTypeFamilyTyCon TyCon
tc
= TyCon -> [TcType] -> FlatM (TcType, CoercionN)
flatten_fam_app TyCon
tc [TcType]
tys
| Bool
otherwise
= TyCon -> [TcType] -> FlatM (TcType, CoercionN)
flatten_ty_con_app TyCon
tc [TcType]
tys
flatten_one ty :: TcType
ty@(FunTy { ft_mult :: TcType -> TcType
ft_mult = TcType
mult, ft_arg :: TcType -> TcType
ft_arg = TcType
ty1, ft_res :: TcType -> TcType
ft_res = TcType
ty2 })
= do { (TcType
xi1,CoercionN
co1) <- TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
ty1
; (TcType
xi2,CoercionN
co2) <- TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
ty2
; (TcType
xi3,CoercionN
co3) <- EqRel -> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
NomEq (FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN))
-> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a b. (a -> b) -> a -> b
$ TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
mult
; Role
role <- FlatM Role
getRole
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
ty { ft_mult :: TcType
ft_mult = TcType
xi3, ft_arg :: TcType
ft_arg = TcType
xi1, ft_res :: TcType
ft_res = TcType
xi2 }
, Role -> CoercionN -> CoercionN -> CoercionN -> CoercionN
mkFunCo Role
role CoercionN
co3 CoercionN
co1 CoercionN
co2) }
flatten_one ty :: TcType
ty@(ForAllTy {})
= do { let ([TyVarBinder]
bndrs, TcType
rho) = TcType -> ([TyVarBinder], TcType)
tcSplitForAllVarBndrs TcType
ty
tvs :: [TcTyVar]
tvs = [TyVarBinder] -> [TcTyVar]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [TyVarBinder]
bndrs
; (TcType
rho', CoercionN
co) <- FlattenMode
-> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a. FlattenMode -> FlatM a -> FlatM a
setMode FlattenMode
FM_SubstOnly (FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN))
-> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a b. (a -> b) -> a -> b
$ TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
rho
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return ([TyVarBinder] -> TcType -> TcType
mkForAllTys [TyVarBinder]
bndrs TcType
rho', [TcTyVar] -> CoercionN -> CoercionN
mkHomoForAllCos [TcTyVar]
tvs CoercionN
co) }
flatten_one (CastTy TcType
ty CoercionN
g)
= do { (TcType
xi, CoercionN
co) <- TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
ty
; (CoercionN
g', CoercionN
_) <- CoercionN -> FlatM (CoercionN, CoercionN)
flatten_co CoercionN
g
; Role
role <- FlatM Role
getRole
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType -> CoercionN -> TcType
mkCastTy TcType
xi CoercionN
g', CoercionN -> Role -> TcType -> TcType -> CoercionN -> CoercionN
castCoercionKind1 CoercionN
co Role
role TcType
xi TcType
ty CoercionN
g') }
flatten_one (CoercionTy CoercionN
co) = (CoercionN -> TcType)
-> (CoercionN, CoercionN) -> (TcType, CoercionN)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first CoercionN -> TcType
mkCoercionTy ((CoercionN, CoercionN) -> (TcType, CoercionN))
-> FlatM (CoercionN, CoercionN) -> FlatM (TcType, CoercionN)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CoercionN -> FlatM (CoercionN, CoercionN)
flatten_co CoercionN
co
flatten_co :: Coercion -> FlatM (Coercion, Coercion)
flatten_co :: CoercionN -> FlatM (CoercionN, CoercionN)
flatten_co CoercionN
co
= do { CoercionN
co <- TcS CoercionN -> FlatM CoercionN
forall a. TcS a -> FlatM a
liftTcS (TcS CoercionN -> FlatM CoercionN)
-> TcS CoercionN -> FlatM CoercionN
forall a b. (a -> b) -> a -> b
$ CoercionN -> TcS CoercionN
zonkCo CoercionN
co
; Role
env_role <- FlatM Role
getRole
; let co' :: CoercionN
co' = Role -> TcType -> CoercionN
mkTcReflCo Role
env_role (CoercionN -> TcType
mkCoercionTy CoercionN
co)
; (CoercionN, CoercionN) -> FlatM (CoercionN, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (CoercionN
co, CoercionN
co') }
flatten_app_tys :: Type -> [Type] -> FlatM (Xi, Coercion)
flatten_app_tys :: TcType -> [TcType] -> FlatM (TcType, CoercionN)
flatten_app_tys (AppTy TcType
ty1 TcType
ty2) [TcType]
tys = TcType -> [TcType] -> FlatM (TcType, CoercionN)
flatten_app_tys TcType
ty1 (TcType
ty2TcType -> [TcType] -> [TcType]
forall a. a -> [a] -> [a]
:[TcType]
tys)
flatten_app_tys TcType
fun_ty [TcType]
arg_tys
= do { (TcType
fun_xi, CoercionN
fun_co) <- TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
fun_ty
; TcType -> CoercionN -> [TcType] -> FlatM (TcType, CoercionN)
flatten_app_ty_args TcType
fun_xi CoercionN
fun_co [TcType]
arg_tys }
flatten_app_ty_args :: Xi -> Coercion -> [Type] -> FlatM (Xi, Coercion)
flatten_app_ty_args :: TcType -> CoercionN -> [TcType] -> FlatM (TcType, CoercionN)
flatten_app_ty_args TcType
fun_xi CoercionN
fun_co []
= (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
fun_xi, CoercionN
fun_co)
flatten_app_ty_args TcType
fun_xi CoercionN
fun_co [TcType]
arg_tys
= do { (TcType
xi, CoercionN
co, CoercionN
kind_co) <- case HasCallStack => TcType -> Maybe (TyCon, [TcType])
TcType -> Maybe (TyCon, [TcType])
tcSplitTyConApp_maybe TcType
fun_xi of
Just (TyCon
tc, [TcType]
xis) ->
do { let tc_roles :: [Role]
tc_roles = TyCon -> [Role]
tyConRolesRepresentational TyCon
tc
arg_roles :: [Role]
arg_roles = [TcType] -> [Role] -> [Role]
forall b a. [b] -> [a] -> [a]
dropList [TcType]
xis [Role]
tc_roles
; ([TcType]
arg_xis, [CoercionN]
arg_cos, CoercionN
kind_co)
<- TcType
-> [Role] -> [TcType] -> FlatM ([TcType], [CoercionN], CoercionN)
flatten_vector (HasDebugCallStack => TcType -> TcType
TcType -> TcType
tcTypeKind TcType
fun_xi) [Role]
arg_roles [TcType]
arg_tys
; EqRel
eq_rel <- FlatM EqRel
getEqRel
; let app_xi :: TcType
app_xi = TyCon -> [TcType] -> TcType
mkTyConApp TyCon
tc ([TcType]
xis [TcType] -> [TcType] -> [TcType]
forall {a}. [a] -> [a] -> [a]
++ [TcType]
arg_xis)
app_co :: CoercionN
app_co = case EqRel
eq_rel of
EqRel
NomEq -> CoercionN -> [CoercionN] -> CoercionN
mkAppCos CoercionN
fun_co [CoercionN]
arg_cos
EqRel
ReprEq -> Role -> TyCon -> [CoercionN] -> CoercionN
mkTcTyConAppCo Role
Representational TyCon
tc
((Role -> TcType -> CoercionN) -> [Role] -> [TcType] -> [CoercionN]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Role -> TcType -> CoercionN
mkReflCo [Role]
tc_roles [TcType]
xis [CoercionN] -> [CoercionN] -> [CoercionN]
forall {a}. [a] -> [a] -> [a]
++ [CoercionN]
arg_cos)
CoercionN -> CoercionN -> CoercionN
`mkTcTransCo`
CoercionN -> [CoercionN] -> CoercionN
mkAppCos CoercionN
fun_co ((TcType -> CoercionN) -> [TcType] -> [CoercionN]
forall a b. (a -> b) -> [a] -> [b]
map TcType -> CoercionN
mkNomReflCo [TcType]
arg_tys)
; (TcType, CoercionN, CoercionN)
-> FlatM (TcType, CoercionN, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
app_xi, CoercionN
app_co, CoercionN
kind_co) }
Maybe (TyCon, [TcType])
Nothing ->
do { ([TcType]
arg_xis, [CoercionN]
arg_cos, CoercionN
kind_co)
<- TcType
-> [Role] -> [TcType] -> FlatM ([TcType], [CoercionN], CoercionN)
flatten_vector (HasDebugCallStack => TcType -> TcType
TcType -> TcType
tcTypeKind TcType
fun_xi) (Role -> [Role]
forall a. a -> [a]
repeat Role
Nominal) [TcType]
arg_tys
; let arg_xi :: TcType
arg_xi = TcType -> [TcType] -> TcType
mkAppTys TcType
fun_xi [TcType]
arg_xis
arg_co :: CoercionN
arg_co = CoercionN -> [CoercionN] -> CoercionN
mkAppCos CoercionN
fun_co [CoercionN]
arg_cos
; (TcType, CoercionN, CoercionN)
-> FlatM (TcType, CoercionN, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
arg_xi, CoercionN
arg_co, CoercionN
kind_co) }
; Role
role <- FlatM Role
getRole
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType -> CoercionN -> Role -> CoercionN -> (TcType, CoercionN)
homogenise_result TcType
xi CoercionN
co Role
role CoercionN
kind_co) }
flatten_ty_con_app :: TyCon -> [TcType] -> FlatM (Xi, Coercion)
flatten_ty_con_app :: TyCon -> [TcType] -> FlatM (TcType, CoercionN)
flatten_ty_con_app TyCon
tc [TcType]
tys
= do { Role
role <- FlatM Role
getRole
; ([TcType]
xis, [CoercionN]
cos, CoercionN
kind_co) <- TyCon
-> [Role] -> [TcType] -> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args_tc TyCon
tc (Role -> TyCon -> [Role]
tyConRolesX Role
role TyCon
tc) [TcType]
tys
; let tyconapp_xi :: TcType
tyconapp_xi = TyCon -> [TcType] -> TcType
mkTyConApp TyCon
tc [TcType]
xis
tyconapp_co :: CoercionN
tyconapp_co = HasDebugCallStack => Role -> TyCon -> [CoercionN] -> CoercionN
Role -> TyCon -> [CoercionN] -> CoercionN
mkTyConAppCo Role
role TyCon
tc [CoercionN]
cos
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType -> CoercionN -> Role -> CoercionN -> (TcType, CoercionN)
homogenise_result TcType
tyconapp_xi CoercionN
tyconapp_co Role
role CoercionN
kind_co) }
homogenise_result :: Xi
-> Coercion
-> Role
-> CoercionN
-> (Xi, Coercion)
homogenise_result :: TcType -> CoercionN -> Role -> CoercionN -> (TcType, CoercionN)
homogenise_result TcType
xi CoercionN
co Role
r CoercionN
kind_co
| CoercionN -> Bool
isGReflCo CoercionN
kind_co = (TcType
xi TcType -> CoercionN -> TcType
`mkCastTy` CoercionN
kind_co, CoercionN
co)
| Bool
otherwise = (TcType
xi TcType -> CoercionN -> TcType
`mkCastTy` CoercionN
kind_co
, (CoercionN -> CoercionN
mkSymCo (CoercionN -> CoercionN) -> CoercionN -> CoercionN
forall a b. (a -> b) -> a -> b
$ Role -> TcType -> MCoercionN -> CoercionN
GRefl Role
r TcType
xi (CoercionN -> MCoercionN
MCo CoercionN
kind_co)) CoercionN -> CoercionN -> CoercionN
`mkTransCo` CoercionN
co)
{-# INLINE homogenise_result #-}
flatten_vector :: Kind
-> [Role]
-> [Type]
-> FlatM ([Xi], [Coercion], CoercionN)
flatten_vector :: TcType
-> [Role] -> [TcType] -> FlatM ([TcType], [CoercionN], CoercionN)
flatten_vector TcType
ki [Role]
roles [TcType]
tys
= do { EqRel
eq_rel <- FlatM EqRel
getEqRel
; case EqRel
eq_rel of
EqRel
NomEq -> [TyCoBinder]
-> Bool
-> TcType
-> TcTyCoVarSet
-> [Role]
-> [TcType]
-> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args [TyCoBinder]
bndrs
Bool
any_named_bndrs
TcType
inner_ki
TcTyCoVarSet
fvs
(Role -> [Role]
forall a. a -> [a]
repeat Role
Nominal)
[TcType]
tys
EqRel
ReprEq -> [TyCoBinder]
-> Bool
-> TcType
-> TcTyCoVarSet
-> [Role]
-> [TcType]
-> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args [TyCoBinder]
bndrs
Bool
any_named_bndrs
TcType
inner_ki
TcTyCoVarSet
fvs
[Role]
roles
[TcType]
tys
}
where
([TyCoBinder]
bndrs, TcType
inner_ki, Bool
any_named_bndrs) = TcType -> ([TyCoBinder], TcType, Bool)
split_pi_tys' TcType
ki
fvs :: TcTyCoVarSet
fvs = TcType -> TcTyCoVarSet
tyCoVarsOfType TcType
ki
{-# INLINE flatten_vector #-}
flatten_fam_app :: TyCon -> [TcType] -> FlatM (Xi, Coercion)
flatten_fam_app :: TyCon -> [TcType] -> FlatM (TcType, CoercionN)
flatten_fam_app TyCon
tc [TcType]
tys
= ASSERT2( tys `lengthAtLeast` tyConArity tc
, ppr tc $$ ppr (tyConArity tc) $$ ppr tys)
do { FlattenMode
mode <- FlatM FlattenMode
getMode
; case FlattenMode
mode of
{ FlattenMode
FM_SubstOnly -> TyCon -> [TcType] -> FlatM (TcType, CoercionN)
flatten_ty_con_app TyCon
tc [TcType]
tys
; FlattenMode
FM_FlattenAll ->
do { let ([TcType]
tys1, [TcType]
tys_rest) = Arity -> [TcType] -> ([TcType], [TcType])
forall a. Arity -> [a] -> ([a], [a])
splitAt (TyCon -> Arity
tyConArity TyCon
tc) [TcType]
tys
; (TcType
xi1, CoercionN
co1) <- TyCon -> [TcType] -> FlatM (TcType, CoercionN)
flatten_exact_fam_app_fully TyCon
tc [TcType]
tys1
; TcType -> CoercionN -> [TcType] -> FlatM (TcType, CoercionN)
flatten_app_ty_args TcType
xi1 CoercionN
co1 [TcType]
tys_rest } } }
flatten_exact_fam_app_fully :: TyCon -> [TcType] -> FlatM (Xi, Coercion)
flatten_exact_fam_app_fully :: TyCon -> [TcType] -> FlatM (TcType, CoercionN)
flatten_exact_fam_app_fully TyCon
tc [TcType]
tys
= do { Maybe (TcType, CoercionN)
mOut <- TyCon -> [TcType] -> FlatM (Maybe (TcType, CoercionN))
try_to_reduce_nocache TyCon
tc [TcType]
tys
; case Maybe (TcType, CoercionN)
mOut of
Just (TcType, CoercionN)
out -> (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TcType, CoercionN)
out
Maybe (TcType, CoercionN)
Nothing -> do
{
; ([TcType]
xis, [CoercionN]
cos, CoercionN
kind_co)
<- EqRel
-> FlatM ([TcType], [CoercionN], CoercionN)
-> FlatM ([TcType], [CoercionN], CoercionN)
forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
NomEq (FlatM ([TcType], [CoercionN], CoercionN)
-> FlatM ([TcType], [CoercionN], CoercionN))
-> FlatM ([TcType], [CoercionN], CoercionN)
-> FlatM ([TcType], [CoercionN], CoercionN)
forall a b. (a -> b) -> a -> b
$
TyCon
-> [Role] -> [TcType] -> FlatM ([TcType], [CoercionN], CoercionN)
flatten_args_tc TyCon
tc (Role -> [Role]
forall a. a -> [a]
repeat Role
Nominal) [TcType]
tys
; EqRel
eq_rel <- FlatM EqRel
getEqRel
; CtFlavour
cur_flav <- FlatM CtFlavour
getFlavour
; let role :: Role
role = EqRel -> Role
eqRelRole EqRel
eq_rel
ret_co :: CoercionN
ret_co = HasDebugCallStack => Role -> TyCon -> [CoercionN] -> CoercionN
Role -> TyCon -> [CoercionN] -> CoercionN
mkTyConAppCo Role
role TyCon
tc [CoercionN]
cos
; Maybe (CoercionN, TcType, CtFlavour)
mb_ct <- TcS (Maybe (CoercionN, TcType, CtFlavour))
-> FlatM (Maybe (CoercionN, TcType, CtFlavour))
forall a. TcS a -> FlatM a
liftTcS (TcS (Maybe (CoercionN, TcType, CtFlavour))
-> FlatM (Maybe (CoercionN, TcType, CtFlavour)))
-> TcS (Maybe (CoercionN, TcType, CtFlavour))
-> FlatM (Maybe (CoercionN, TcType, CtFlavour))
forall a b. (a -> b) -> a -> b
$ TyCon -> [TcType] -> TcS (Maybe (CoercionN, TcType, CtFlavour))
lookupFlatCache TyCon
tc [TcType]
xis
; case Maybe (CoercionN, TcType, CtFlavour)
mb_ct of
Just (CoercionN
co, TcType
rhs_ty, CtFlavour
flav)
| (SwapFlag
NotSwapped, Bool
_) <- CtFlavour
flav CtFlavour -> CtFlavour -> (SwapFlag, Bool)
`funEqCanDischargeF` CtFlavour
cur_flav
->
do { String -> SDoc -> FlatM ()
traceFlat String
"flatten/flat-cache hit" (SDoc -> FlatM ()) -> SDoc -> FlatM ()
forall a b. (a -> b) -> a -> b
$
(TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc SDoc -> SDoc -> SDoc
<+> [TcType] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcType]
xis SDoc -> SDoc -> SDoc
$$ TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcType
rhs_ty)
; (TcType
fsk_xi, CoercionN
fsk_co) <- TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
rhs_ty
; let xi :: TcType
xi = TcType
fsk_xi TcType -> CoercionN -> TcType
`mkCastTy` CoercionN
kind_co
co' :: CoercionN
co' = Role -> TcType -> CoercionN -> CoercionN -> CoercionN
mkTcCoherenceLeftCo Role
role TcType
fsk_xi CoercionN
kind_co CoercionN
fsk_co
CoercionN -> CoercionN -> CoercionN
`mkTransCo`
EqRel -> CoercionN -> CoercionN
maybeTcSubCo EqRel
eq_rel (CoercionN -> CoercionN
mkSymCo CoercionN
co)
CoercionN -> CoercionN -> CoercionN
`mkTransCo` CoercionN
ret_co
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
xi, CoercionN
co')
}
Maybe (CoercionN, TcType, CtFlavour)
_ -> do { Maybe (TcType, CoercionN)
mOut <- TyCon
-> [TcType]
-> CoercionN
-> (CoercionN -> CoercionN)
-> FlatM (Maybe (TcType, CoercionN))
try_to_reduce TyCon
tc
[TcType]
xis
CoercionN
kind_co
(CoercionN -> CoercionN -> CoercionN
`mkTransCo` CoercionN
ret_co)
; case Maybe (TcType, CoercionN)
mOut of
Just (TcType, CoercionN)
out -> (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TcType, CoercionN)
out
Maybe (TcType, CoercionN)
Nothing -> do
{ CtLoc
loc <- FlatM CtLoc
getLoc
; (CtEvidence
ev, CoercionN
co, TcTyVar
fsk) <- TcS (CtEvidence, CoercionN, TcTyVar)
-> FlatM (CtEvidence, CoercionN, TcTyVar)
forall a. TcS a -> FlatM a
liftTcS (TcS (CtEvidence, CoercionN, TcTyVar)
-> FlatM (CtEvidence, CoercionN, TcTyVar))
-> TcS (CtEvidence, CoercionN, TcTyVar)
-> FlatM (CtEvidence, CoercionN, TcTyVar)
forall a b. (a -> b) -> a -> b
$
CtFlavour
-> CtLoc
-> TyCon
-> [TcType]
-> TcS (CtEvidence, CoercionN, TcTyVar)
newFlattenSkolem CtFlavour
cur_flav CtLoc
loc TyCon
tc [TcType]
xis
; let ct :: Ct
ct = CFunEqCan :: CtEvidence -> TyCon -> [TcType] -> TcTyVar -> Ct
CFunEqCan { cc_ev :: CtEvidence
cc_ev = CtEvidence
ev
, cc_fun :: TyCon
cc_fun = TyCon
tc
, cc_tyargs :: [TcType]
cc_tyargs = [TcType]
xis
, cc_fsk :: TcTyVar
cc_fsk = TcTyVar
fsk }
; Ct -> FlatM ()
emitFlatWork Ct
ct
; String -> SDoc -> FlatM ()
traceFlat String
"flatten/flat-cache miss" (SDoc -> FlatM ()) -> SDoc -> FlatM ()
forall a b. (a -> b) -> a -> b
$
(TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc SDoc -> SDoc -> SDoc
<+> [TcType] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcType]
xis SDoc -> SDoc -> SDoc
$$ TcTyVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVar
fsk SDoc -> SDoc -> SDoc
$$ CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
ev)
; let fsk_ty :: TcType
fsk_ty = TcTyVar -> TcType
mkTyVarTy TcTyVar
fsk
xi :: TcType
xi = TcType
fsk_ty TcType -> CoercionN -> TcType
`mkCastTy` CoercionN
kind_co
co' :: CoercionN
co' = Role -> TcType -> CoercionN -> CoercionN -> CoercionN
mkTcCoherenceLeftCo Role
role TcType
fsk_ty CoercionN
kind_co (EqRel -> CoercionN -> CoercionN
maybeTcSubCo EqRel
eq_rel (CoercionN -> CoercionN
mkSymCo CoercionN
co))
CoercionN -> CoercionN -> CoercionN
`mkTransCo` CoercionN
ret_co
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
xi, CoercionN
co')
}
}
}
}
where
try_to_reduce :: TyCon
-> [Type]
-> CoercionN
-> ( Coercion
-> Coercion )
-> FlatM (Maybe (Xi, Coercion))
try_to_reduce :: TyCon
-> [TcType]
-> CoercionN
-> (CoercionN -> CoercionN)
-> FlatM (Maybe (TcType, CoercionN))
try_to_reduce TyCon
tc [TcType]
tys CoercionN
kind_co CoercionN -> CoercionN
update_co
= do { TcType -> FlatM ()
checkStackDepth (TyCon -> [TcType] -> TcType
mkTyConApp TyCon
tc [TcType]
tys)
; Maybe (CoercionN, TcType)
mb_match <- TcS (Maybe (CoercionN, TcType))
-> FlatM (Maybe (CoercionN, TcType))
forall a. TcS a -> FlatM a
liftTcS (TcS (Maybe (CoercionN, TcType))
-> FlatM (Maybe (CoercionN, TcType)))
-> TcS (Maybe (CoercionN, TcType))
-> FlatM (Maybe (CoercionN, TcType))
forall a b. (a -> b) -> a -> b
$ TyCon -> [TcType] -> TcS (Maybe (CoercionN, TcType))
matchFam TyCon
tc [TcType]
tys
; case Maybe (CoercionN, TcType)
mb_match of
Just (CoercionN
norm_co, TcType
norm_ty)
-> do { String -> SDoc -> FlatM ()
traceFlat String
"Eager T.F. reduction success" (SDoc -> FlatM ()) -> SDoc -> FlatM ()
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tc, [TcType] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcType]
tys, TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcType
norm_ty
, CoercionN -> SDoc
forall a. Outputable a => a -> SDoc
ppr CoercionN
norm_co SDoc -> SDoc -> SDoc
<+> SDoc
dcolon
SDoc -> SDoc -> SDoc
<+> Pair TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CoercionN -> Pair TcType
coercionKind CoercionN
norm_co)
]
; (TcType
xi, CoercionN
final_co) <- FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a. FlatM a -> FlatM a
bumpDepth (FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN))
-> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a b. (a -> b) -> a -> b
$ TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
norm_ty
; EqRel
eq_rel <- FlatM EqRel
getEqRel
; let co :: CoercionN
co = EqRel -> CoercionN -> CoercionN
maybeTcSubCo EqRel
eq_rel CoercionN
norm_co
CoercionN -> CoercionN -> CoercionN
`mkTransCo` CoercionN -> CoercionN
mkSymCo CoercionN
final_co
; CtFlavour
flavour <- FlatM CtFlavour
getFlavour
; Bool -> FlatM () -> FlatM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (EqRel
eq_rel EqRel -> EqRel -> Bool
forall a. Eq a => a -> a -> Bool
== EqRel
NomEq) (FlatM () -> FlatM ()) -> FlatM () -> FlatM ()
forall a b. (a -> b) -> a -> b
$
TcS () -> FlatM ()
forall a. TcS a -> FlatM a
liftTcS (TcS () -> FlatM ()) -> TcS () -> FlatM ()
forall a b. (a -> b) -> a -> b
$
TyCon -> [TcType] -> (CoercionN, TcType, CtFlavour) -> TcS ()
extendFlatCache TyCon
tc [TcType]
tys ( CoercionN
co, TcType
xi, CtFlavour
flavour )
; let role :: Role
role = EqRel -> Role
eqRelRole EqRel
eq_rel
xi' :: TcType
xi' = TcType
xi TcType -> CoercionN -> TcType
`mkCastTy` CoercionN
kind_co
co' :: CoercionN
co' = CoercionN -> CoercionN
update_co (CoercionN -> CoercionN) -> CoercionN -> CoercionN
forall a b. (a -> b) -> a -> b
$
Role -> TcType -> CoercionN -> CoercionN -> CoercionN
mkTcCoherenceLeftCo Role
role TcType
xi CoercionN
kind_co (CoercionN -> CoercionN
mkSymCo CoercionN
co)
; Maybe (TcType, CoercionN) -> FlatM (Maybe (TcType, CoercionN))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (TcType, CoercionN) -> FlatM (Maybe (TcType, CoercionN)))
-> Maybe (TcType, CoercionN) -> FlatM (Maybe (TcType, CoercionN))
forall a b. (a -> b) -> a -> b
$ (TcType, CoercionN) -> Maybe (TcType, CoercionN)
forall a. a -> Maybe a
Just (TcType
xi', CoercionN
co') }
Maybe (CoercionN, TcType)
Nothing -> Maybe (TcType, CoercionN) -> FlatM (Maybe (TcType, CoercionN))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (TcType, CoercionN)
forall a. Maybe a
Nothing }
try_to_reduce_nocache :: TyCon
-> [Type]
-> FlatM (Maybe (Xi, Coercion))
try_to_reduce_nocache :: TyCon -> [TcType] -> FlatM (Maybe (TcType, CoercionN))
try_to_reduce_nocache TyCon
tc [TcType]
tys
= do { TcType -> FlatM ()
checkStackDepth (TyCon -> [TcType] -> TcType
mkTyConApp TyCon
tc [TcType]
tys)
; Maybe (CoercionN, TcType)
mb_match <- TcS (Maybe (CoercionN, TcType))
-> FlatM (Maybe (CoercionN, TcType))
forall a. TcS a -> FlatM a
liftTcS (TcS (Maybe (CoercionN, TcType))
-> FlatM (Maybe (CoercionN, TcType)))
-> TcS (Maybe (CoercionN, TcType))
-> FlatM (Maybe (CoercionN, TcType))
forall a b. (a -> b) -> a -> b
$ TyCon -> [TcType] -> TcS (Maybe (CoercionN, TcType))
matchFam TyCon
tc [TcType]
tys
; case Maybe (CoercionN, TcType)
mb_match of
Just (CoercionN
norm_co, TcType
norm_ty)
-> do { (TcType
xi, CoercionN
final_co) <- FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a. FlatM a -> FlatM a
bumpDepth (FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN))
-> FlatM (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall a b. (a -> b) -> a -> b
$ TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
norm_ty
; EqRel
eq_rel <- FlatM EqRel
getEqRel
; let co :: CoercionN
co = CoercionN -> CoercionN
mkSymCo (EqRel -> CoercionN -> CoercionN
maybeTcSubCo EqRel
eq_rel CoercionN
norm_co
CoercionN -> CoercionN -> CoercionN
`mkTransCo` CoercionN -> CoercionN
mkSymCo CoercionN
final_co)
; Maybe (TcType, CoercionN) -> FlatM (Maybe (TcType, CoercionN))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (TcType, CoercionN) -> FlatM (Maybe (TcType, CoercionN)))
-> Maybe (TcType, CoercionN) -> FlatM (Maybe (TcType, CoercionN))
forall a b. (a -> b) -> a -> b
$ (TcType, CoercionN) -> Maybe (TcType, CoercionN)
forall a. a -> Maybe a
Just (TcType
xi, CoercionN
co) }
Maybe (CoercionN, TcType)
Nothing -> Maybe (TcType, CoercionN) -> FlatM (Maybe (TcType, CoercionN))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (TcType, CoercionN)
forall a. Maybe a
Nothing }
data FlattenTvResult
= FTRNotFollowed
| FTRFollowed TcType Coercion
flattenTyVar :: TyVar -> FlatM (Xi, Coercion)
flattenTyVar :: TcTyVar -> FlatM (TcType, CoercionN)
flattenTyVar TcTyVar
tv
= do { FlattenTvResult
mb_yes <- TcTyVar -> FlatM FlattenTvResult
flatten_tyvar1 TcTyVar
tv
; case FlattenTvResult
mb_yes of
FTRFollowed TcType
ty1 CoercionN
co1
-> do { (TcType
ty2, CoercionN
co2) <- TcType -> FlatM (TcType, CoercionN)
flatten_one TcType
ty1
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
ty2, CoercionN
co2 CoercionN -> CoercionN -> CoercionN
`mkTransCo` CoercionN
co1) }
FlattenTvResult
FTRNotFollowed
-> do { TcTyVar
tv' <- TcS TcTyVar -> FlatM TcTyVar
forall a. TcS a -> FlatM a
liftTcS (TcS TcTyVar -> FlatM TcTyVar) -> TcS TcTyVar -> FlatM TcTyVar
forall a b. (a -> b) -> a -> b
$ (TcType -> TcS TcType) -> TcTyVar -> TcS TcTyVar
forall (m :: * -> *).
Monad m =>
(TcType -> m TcType) -> TcTyVar -> m TcTyVar
updateTyVarKindM TcType -> TcS TcType
zonkTcType TcTyVar
tv
; Role
role <- FlatM Role
getRole
; let ty' :: TcType
ty' = TcTyVar -> TcType
mkTyVarTy TcTyVar
tv'
; (TcType, CoercionN) -> FlatM (TcType, CoercionN)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType
ty', Role -> TcType -> CoercionN
mkTcReflCo Role
role TcType
ty') } }
flatten_tyvar1 :: TcTyVar -> FlatM FlattenTvResult
flatten_tyvar1 :: TcTyVar -> FlatM FlattenTvResult
flatten_tyvar1 TcTyVar
tv
= do { Maybe TcType
mb_ty <- TcS (Maybe TcType) -> FlatM (Maybe TcType)
forall a. TcS a -> FlatM a
liftTcS (TcS (Maybe TcType) -> FlatM (Maybe TcType))
-> TcS (Maybe TcType) -> FlatM (Maybe TcType)
forall a b. (a -> b) -> a -> b
$ TcTyVar -> TcS (Maybe TcType)
isFilledMetaTyVar_maybe TcTyVar
tv
; case Maybe TcType
mb_ty of
Just TcType
ty -> do { String -> SDoc -> FlatM ()
traceFlat String
"Following filled tyvar"
(TcTyVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVar
tv SDoc -> SDoc -> SDoc
<+> SDoc
equals SDoc -> SDoc -> SDoc
<+> TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcType
ty)
; Role
role <- FlatM Role
getRole
; FlattenTvResult -> FlatM FlattenTvResult
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType -> CoercionN -> FlattenTvResult
FTRFollowed TcType
ty (Role -> TcType -> CoercionN
mkReflCo Role
role TcType
ty)) } ;
Maybe TcType
Nothing -> do { String -> SDoc -> FlatM ()
traceFlat String
"Unfilled tyvar" (TcTyVar -> SDoc
pprTyVar TcTyVar
tv)
; CtFlavourRole
fr <- FlatM CtFlavourRole
getFlavourRole
; TcTyVar -> CtFlavourRole -> FlatM FlattenTvResult
flatten_tyvar2 TcTyVar
tv CtFlavourRole
fr } }
flatten_tyvar2 :: TcTyVar -> CtFlavourRole -> FlatM FlattenTvResult
flatten_tyvar2 :: TcTyVar -> CtFlavourRole -> FlatM FlattenTvResult
flatten_tyvar2 TcTyVar
tv fr :: CtFlavourRole
fr@(CtFlavour
_, EqRel
eq_rel)
= do { DTyVarEnv [Ct]
ieqs <- TcS (DTyVarEnv [Ct]) -> FlatM (DTyVarEnv [Ct])
forall a. TcS a -> FlatM a
liftTcS (TcS (DTyVarEnv [Ct]) -> FlatM (DTyVarEnv [Ct]))
-> TcS (DTyVarEnv [Ct]) -> FlatM (DTyVarEnv [Ct])
forall a b. (a -> b) -> a -> b
$ TcS (DTyVarEnv [Ct])
getInertEqs
; FlattenMode
mode <- FlatM FlattenMode
getMode
; case DTyVarEnv [Ct] -> TcTyVar -> Maybe [Ct]
forall a. DVarEnv a -> TcTyVar -> Maybe a
lookupDVarEnv DTyVarEnv [Ct]
ieqs TcTyVar
tv of
Just (Ct
ct:[Ct]
_)
| CTyEqCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ctev, cc_tyvar :: Ct -> TcTyVar
cc_tyvar = TcTyVar
tv
, cc_rhs :: Ct -> TcType
cc_rhs = TcType
rhs_ty, cc_eq_rel :: Ct -> EqRel
cc_eq_rel = EqRel
ct_eq_rel } <- Ct
ct
, let ct_fr :: CtFlavourRole
ct_fr = (CtEvidence -> CtFlavour
ctEvFlavour CtEvidence
ctev, EqRel
ct_eq_rel)
, CtFlavourRole
ct_fr CtFlavourRole -> CtFlavourRole -> Bool
`eqCanRewriteFR` CtFlavourRole
fr
-> do { String -> SDoc -> FlatM ()
traceFlat String
"Following inert tyvar"
(FlattenMode -> SDoc
forall a. Outputable a => a -> SDoc
ppr FlattenMode
mode SDoc -> SDoc -> SDoc
<+>
TcTyVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVar
tv SDoc -> SDoc -> SDoc
<+>
SDoc
equals SDoc -> SDoc -> SDoc
<+>
TcType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcType
rhs_ty SDoc -> SDoc -> SDoc
$$ CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
ctev)
; let rewrite_co1 :: CoercionN
rewrite_co1 = CoercionN -> CoercionN
mkSymCo (HasDebugCallStack => CtEvidence -> CoercionN
CtEvidence -> CoercionN
ctEvCoercion CtEvidence
ctev)
rewrite_co :: CoercionN
rewrite_co = case (EqRel
ct_eq_rel, EqRel
eq_rel) of
(EqRel
ReprEq, EqRel
_rel) -> ASSERT( _rel == ReprEq )
CoercionN
rewrite_co1
(EqRel
NomEq, EqRel
NomEq) -> CoercionN
rewrite_co1
(EqRel
NomEq, EqRel
ReprEq) -> CoercionN -> CoercionN
mkSubCo CoercionN
rewrite_co1
; FlattenTvResult -> FlatM FlattenTvResult
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType -> CoercionN -> FlattenTvResult
FTRFollowed TcType
rhs_ty CoercionN
rewrite_co) }
Maybe [Ct]
_other -> FlattenTvResult -> FlatM FlattenTvResult
forall (m :: * -> *) a. Monad m => a -> m a
return FlattenTvResult
FTRNotFollowed }
unflattenWanteds :: Cts -> Cts -> TcS Cts
unflattenWanteds :: Cts -> Cts -> TcS Cts
unflattenWanteds Cts
tv_eqs Cts
funeqs
= do { TcLevel
tclvl <- TcS TcLevel
getTcLevel
; String -> SDoc -> TcS ()
traceTcS String
"Unflattening" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$ SDoc -> SDoc
braces (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
vcat [ String -> SDoc
text String
"Funeqs =" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
pprCts Cts
funeqs
, String -> SDoc
text String
"Tv eqs =" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
pprCts Cts
tv_eqs ]
; Cts
funeqs <- (Ct -> Cts -> TcS Cts) -> Cts -> Cts -> TcS Cts
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> b -> m b) -> b -> t a -> m b
foldrM Ct -> Cts -> TcS Cts
unflatten_funeq Cts
emptyCts Cts
funeqs
; String -> SDoc -> TcS ()
traceTcS String
"Unflattening 1" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$ SDoc -> SDoc
braces (Cts -> SDoc
pprCts Cts
funeqs)
; Cts
tv_eqs <- (Ct -> Cts -> TcS Cts) -> Cts -> Cts -> TcS Cts
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> b -> m b) -> b -> t a -> m b
foldrM (TcLevel -> Ct -> Cts -> TcS Cts
unflatten_eq TcLevel
tclvl) Cts
emptyCts Cts
tv_eqs
; String -> SDoc -> TcS ()
traceTcS String
"Unflattening 2" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$ SDoc -> SDoc
braces (Cts -> SDoc
pprCts Cts
tv_eqs)
; Cts
funeqs <- (Ct -> TcS Ct) -> Cts -> TcS Cts
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Bag a -> m (Bag b)
mapBagM Ct -> TcS Ct
finalise_funeq Cts
funeqs
; String -> SDoc -> TcS ()
traceTcS String
"Unflattening 3" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$ SDoc -> SDoc
braces (Cts -> SDoc
pprCts Cts
funeqs)
; Cts
tv_eqs <- (Ct -> Cts -> TcS Cts) -> Cts -> Cts -> TcS Cts
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> b -> m b) -> b -> t a -> m b
foldrM Ct -> Cts -> TcS Cts
finalise_eq Cts
emptyCts Cts
tv_eqs
; let all_flat :: Cts
all_flat = Cts
tv_eqs Cts -> Cts -> Cts
`andCts` Cts
funeqs
; String -> SDoc -> TcS ()
traceTcS String
"Unflattening done" (SDoc -> TcS ()) -> SDoc -> TcS ()
forall a b. (a -> b) -> a -> b
$ SDoc -> SDoc
braces (Cts -> SDoc
pprCts Cts
all_flat)
; Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return Cts
all_flat }
where
unflatten_funeq :: Ct -> Cts -> TcS Cts
unflatten_funeq :: Ct -> Cts -> TcS Cts
unflatten_funeq ct :: Ct
ct@(CFunEqCan { cc_fun :: Ct -> TyCon
cc_fun = TyCon
tc, cc_tyargs :: Ct -> [TcType]
cc_tyargs = [TcType]
xis
, cc_fsk :: Ct -> TcTyVar
cc_fsk = TcTyVar
fmv, cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev }) Cts
rest
= do {
TcType
rhs' <- TcType -> TcS TcType
zonkTcType (TyCon -> [TcType] -> TcType
mkTyConApp TyCon
tc [TcType]
xis)
; case [TcTyVar] -> TcType -> Maybe TcType
occCheckExpand [TcTyVar
fmv] TcType
rhs' of
Just TcType
rhs''
-> do { CtEvidence -> EqRel -> TcType -> TcS ()
setReflEvidence CtEvidence
ev EqRel
NomEq TcType
rhs''
; TcTyVar -> TcType -> TcS ()
unflattenFmv TcTyVar
fmv TcType
rhs''
; Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return Cts
rest }
Maybe TcType
Nothing ->
Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return (Ct
ct Ct -> Cts -> Cts
`consCts` Cts
rest) }
unflatten_funeq Ct
other_ct Cts
_
= String -> SDoc -> TcS Cts
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"unflatten_funeq" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
other_ct)
finalise_funeq :: Ct -> TcS Ct
finalise_funeq :: Ct -> TcS Ct
finalise_funeq (CFunEqCan { cc_fsk :: Ct -> TcTyVar
cc_fsk = TcTyVar
fmv, cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev })
= do { TcTyVar -> TcS ()
demoteUnfilledFmv TcTyVar
fmv
; Ct -> TcS Ct
forall (m :: * -> *) a. Monad m => a -> m a
return (CtEvidence -> Ct
mkNonCanonical CtEvidence
ev) }
finalise_funeq Ct
ct = String -> SDoc -> TcS Ct
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"finalise_funeq" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)
unflatten_eq :: TcLevel -> Ct -> Cts -> TcS Cts
unflatten_eq :: TcLevel -> Ct -> Cts -> TcS Cts
unflatten_eq TcLevel
tclvl ct :: Ct
ct@(CTyEqCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev, cc_tyvar :: Ct -> TcTyVar
cc_tyvar = TcTyVar
tv
, cc_rhs :: Ct -> TcType
cc_rhs = TcType
rhs, cc_eq_rel :: Ct -> EqRel
cc_eq_rel = EqRel
eq_rel }) Cts
rest
| EqRel
NomEq <- EqRel
eq_rel
, TcTyVar -> Bool
isFmvTyVar TcTyVar
tv
= ASSERT2( tyVarKind tv `eqType` tcTypeKind rhs, ppr ct )
do { Bool
is_filled <- TcTyVar -> TcS Bool
isFilledMetaTyVar TcTyVar
tv
; Bool
elim <- case Bool
is_filled of
Bool
False -> do { String -> SDoc -> TcS ()
traceTcS String
"unflatten_eq 2" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)
; CtEvidence -> TcTyVar -> TcType -> TcS Bool
tryFill CtEvidence
ev TcTyVar
tv TcType
rhs }
Bool
True -> do { String -> SDoc -> TcS ()
traceTcS String
"unflatten_eq 3" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)
; CtEvidence -> TcLevel -> TcTyVar -> TcType -> TcS Bool
try_fill_rhs CtEvidence
ev TcLevel
tclvl TcTyVar
tv TcType
rhs }
; if Bool
elim
then do { CtEvidence -> EqRel -> TcType -> TcS ()
setReflEvidence CtEvidence
ev EqRel
eq_rel (TcTyVar -> TcType
mkTyVarTy TcTyVar
tv)
; Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return Cts
rest }
else Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return (Ct
ct Ct -> Cts -> Cts
`consCts` Cts
rest) }
| Bool
otherwise
= Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return (Ct
ct Ct -> Cts -> Cts
`consCts` Cts
rest)
unflatten_eq TcLevel
_ Ct
ct Cts
_ = String -> SDoc -> TcS Cts
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"unflatten_irred" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)
try_fill_rhs :: CtEvidence -> TcLevel -> TcTyVar -> TcType -> TcS Bool
try_fill_rhs CtEvidence
ev TcLevel
tclvl TcTyVar
lhs_tv TcType
rhs
| Just (TcTyVar
rhs_tv, CoercionN
co) <- TcType -> Maybe (TcTyVar, CoercionN)
getCastedTyVar_maybe TcType
rhs
, TcTyVar -> Bool
isFmvTyVar TcTyVar
rhs_tv Bool -> Bool -> Bool
|| (TcLevel -> TcTyVar -> Bool
isTouchableMetaTyVar TcLevel
tclvl TcTyVar
rhs_tv
Bool -> Bool -> Bool
&& Bool -> Bool
not (TcTyVar -> Bool
isTyVarTyVar TcTyVar
rhs_tv))
= do { Bool
is_filled <- TcTyVar -> TcS Bool
isFilledMetaTyVar TcTyVar
rhs_tv
; if Bool
is_filled then Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else CtEvidence -> TcTyVar -> TcType -> TcS Bool
tryFill CtEvidence
ev TcTyVar
rhs_tv
(TcTyVar -> TcType
mkTyVarTy TcTyVar
lhs_tv TcType -> CoercionN -> TcType
`mkCastTy` CoercionN -> CoercionN
mkSymCo CoercionN
co) }
| Bool
otherwise
= Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
finalise_eq :: Ct -> Cts -> TcS Cts
finalise_eq :: Ct -> Cts -> TcS Cts
finalise_eq (CTyEqCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev, cc_tyvar :: Ct -> TcTyVar
cc_tyvar = TcTyVar
tv
, cc_rhs :: Ct -> TcType
cc_rhs = TcType
rhs, cc_eq_rel :: Ct -> EqRel
cc_eq_rel = EqRel
eq_rel }) Cts
rest
| TcTyVar -> Bool
isFmvTyVar TcTyVar
tv
= do { TcType
ty1 <- TcTyVar -> TcS TcType
zonkTcTyVar TcTyVar
tv
; TcType
rhs' <- TcType -> TcS TcType
zonkTcType TcType
rhs
; if TcType
ty1 HasDebugCallStack => TcType -> TcType -> Bool
TcType -> TcType -> Bool
`tcEqType` TcType
rhs'
then do { CtEvidence -> EqRel -> TcType -> TcS ()
setReflEvidence CtEvidence
ev EqRel
eq_rel TcType
rhs'
; Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return Cts
rest }
else Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return (CtEvidence -> Ct
mkNonCanonical CtEvidence
ev Ct -> Cts -> Cts
`consCts` Cts
rest) }
| Bool
otherwise
= Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return (CtEvidence -> Ct
mkNonCanonical CtEvidence
ev Ct -> Cts -> Cts
`consCts` Cts
rest)
finalise_eq Ct
ct Cts
_ = String -> SDoc -> TcS Cts
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"finalise_irred" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)
tryFill :: CtEvidence -> TcTyVar -> TcType -> TcS Bool
tryFill :: CtEvidence -> TcTyVar -> TcType -> TcS Bool
tryFill CtEvidence
ev TcTyVar
tv TcType
rhs
= ASSERT2( not (isGiven ev), ppr ev )
do { TcType
rhs' <- TcType -> TcS TcType
zonkTcType TcType
rhs
; case () of
()
_ | Just TcTyVar
tv' <- TcType -> Maybe TcTyVar
tcGetTyVar_maybe TcType
rhs'
, TcTyVar
tv TcTyVar -> TcTyVar -> Bool
forall a. Eq a => a -> a -> Bool
== TcTyVar
tv'
-> Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
()
_ | Just TcType
rhs'' <- [TcTyVar] -> TcType -> Maybe TcType
occCheckExpand [TcTyVar
tv] TcType
rhs'
-> do {
TcTyVar -> TcType -> TcS ()
unifyTyVar TcTyVar
tv TcType
rhs''
; 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
}
setReflEvidence :: CtEvidence -> EqRel -> TcType -> TcS ()
setReflEvidence :: CtEvidence -> EqRel -> TcType -> TcS ()
setReflEvidence CtEvidence
ev EqRel
eq_rel TcType
rhs
= CtEvidence -> EvTerm -> TcS ()
setEvBindIfWanted CtEvidence
ev (CoercionN -> EvTerm
evCoercion CoercionN
refl_co)
where
refl_co :: CoercionN
refl_co = Role -> TcType -> CoercionN
mkTcReflCo (EqRel -> Role
eqRelRole EqRel
eq_rel) TcType
rhs
split_pi_tys' :: Type -> ([TyCoBinder], Type, Bool)
split_pi_tys' :: TcType -> ([TyCoBinder], TcType, Bool)
split_pi_tys' TcType
ty = TcType -> TcType -> ([TyCoBinder], TcType, Bool)
split TcType
ty TcType
ty
where
split :: TcType -> TcType -> ([TyCoBinder], TcType, Bool)
split TcType
_ (ForAllTy TyVarBinder
b TcType
res) = let ([TyCoBinder]
bs, TcType
ty, Bool
_) = TcType -> TcType -> ([TyCoBinder], TcType, Bool)
split TcType
res TcType
res
in (TyVarBinder -> TyCoBinder
Named TyVarBinder
b TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bs, TcType
ty, Bool
True)
split TcType
_ (FunTy { ft_af :: TcType -> AnonArgFlag
ft_af = AnonArgFlag
af, ft_mult :: TcType -> TcType
ft_mult = TcType
w, ft_arg :: TcType -> TcType
ft_arg = TcType
arg, ft_res :: TcType -> TcType
ft_res = TcType
res })
= let ([TyCoBinder]
bs, TcType
ty, Bool
named) = TcType -> TcType -> ([TyCoBinder], TcType, Bool)
split TcType
res TcType
res
in (AnonArgFlag -> Scaled TcType -> TyCoBinder
Anon AnonArgFlag
af (TcType -> TcType -> Scaled TcType
forall a. TcType -> a -> Scaled a
mkScaled TcType
w TcType
arg) TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bs, TcType
ty, Bool
named)
split TcType
orig_ty TcType
ty | Just TcType
ty' <- TcType -> Maybe TcType
coreView TcType
ty = TcType -> TcType -> ([TyCoBinder], TcType, Bool)
split TcType
orig_ty TcType
ty'
split TcType
orig_ty TcType
_ = ([], TcType
orig_ty, Bool
False)
{-# INLINE split_pi_tys' #-}
ty_con_binders_ty_binders' :: [TyConBinder] -> ([TyCoBinder], Bool)
ty_con_binders_ty_binders' :: [TyConBinder] -> ([TyCoBinder], Bool)
ty_con_binders_ty_binders' = (TyConBinder -> ([TyCoBinder], Bool) -> ([TyCoBinder], Bool))
-> ([TyCoBinder], Bool) -> [TyConBinder] -> ([TyCoBinder], Bool)
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr TyConBinder -> ([TyCoBinder], Bool) -> ([TyCoBinder], Bool)
go ([], Bool
False)
where
go :: TyConBinder -> ([TyCoBinder], Bool) -> ([TyCoBinder], Bool)
go (Bndr TcTyVar
tv (NamedTCB ArgFlag
vis)) ([TyCoBinder]
bndrs, Bool
_)
= (TyVarBinder -> TyCoBinder
Named (TcTyVar -> ArgFlag -> TyVarBinder
forall var argf. var -> argf -> VarBndr var argf
Bndr TcTyVar
tv ArgFlag
vis) TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bndrs, Bool
True)
go (Bndr TcTyVar
tv (AnonTCB AnonArgFlag
af)) ([TyCoBinder]
bndrs, Bool
n)
= (AnonArgFlag -> Scaled TcType -> TyCoBinder
Anon AnonArgFlag
af (TcType -> Scaled TcType
forall a. a -> Scaled a
unrestricted (TcTyVar -> TcType
tyVarKind TcTyVar
tv)) TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bndrs, Bool
n)
{-# INLINE go #-}
{-# INLINE ty_con_binders_ty_binders' #-}