{-# LANGUAGE CPP, DeriveFunctor, ViewPatterns, BangPatterns #-}
module TcFlatten(
FlattenMode(..),
flatten, flattenKind, flattenArgsNom,
rewriteTyVar,
unflattenWanteds
) where
#include "HsVersions.h"
import GhcPrelude
import TcRnTypes
import TyCoPpr ( pprTyVar )
import Constraint
import Predicate
import TcType
import Type
import TcEvidence
import TyCon
import TyCoRep
import Coercion
import Var
import VarSet
import VarEnv
import Outputable
import TcSMonad as TcS
import BasicTypes( SwapFlag(..) )
import Util
import Bag
import Control.Monad
import MonadUtils ( 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 { FlatM a -> FlattenEnv -> TcS a
runFlatM :: FlattenEnv -> TcS a }
deriving (a -> FlatM b -> FlatM a
(a -> b) -> FlatM a -> FlatM b
(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
<$ :: a -> FlatM b -> FlatM a
$c<$ :: forall a b. a -> FlatM b -> FlatM a
fmap :: (a -> b) -> FlatM a -> FlatM b
$cfmap :: forall a b. (a -> b) -> FlatM a -> FlatM b
Functor)
instance Monad FlatM where
FlatM a
m >>= :: 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 :: 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)
<*> :: 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 :: 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 :: 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 :: 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
getFlatEnvField :: (FlattenEnv -> a) -> FlatM a
getFlatEnvField :: (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 :: Type -> FlatM ()
checkStackDepth Type
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 -> Type -> TcS ()
checkReductionDepth CtLoc
loc Type
ty }
setEqRel :: EqRel -> FlatM a -> FlatM a
setEqRel :: 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 :: 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 :: 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 :: 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 -> Type -> TcS (Type, TcCoercion)
flatten FlattenMode
mode CtEvidence
ev Type
ty
= do { String -> SDoc -> TcS ()
traceTcS String
"flatten {" (FlattenMode -> SDoc
forall a. Outputable a => a -> SDoc
ppr FlattenMode
mode SDoc -> SDoc -> SDoc
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
; (Type
ty', TcCoercion
co) <- FlattenMode
-> CtEvidence -> FlatM (Type, TcCoercion) -> TcS (Type, TcCoercion)
forall a. FlattenMode -> CtEvidence -> FlatM a -> TcS a
runFlattenCtEv FlattenMode
mode CtEvidence
ev (Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty)
; String -> SDoc -> TcS ()
traceTcS String
"flatten }" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty')
; (Type, TcCoercion) -> TcS (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
ty', TcCoercion
co) }
rewriteTyVar :: TcTyVar -> TcS TcType
rewriteTyVar :: TcTyVar -> TcS Type
rewriteTyVar TcTyVar
tv
= do { String -> SDoc -> TcS ()
traceTcS String
"rewriteTyVar {" (TcTyVar -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyVar
tv)
; (Type
ty, TcCoercion
_) <- FlattenMode
-> CtLoc
-> CtFlavour
-> EqRel
-> FlatM (Type, TcCoercion)
-> TcS (Type, TcCoercion)
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 (Type, TcCoercion) -> TcS (Type, TcCoercion))
-> FlatM (Type, TcCoercion) -> TcS (Type, TcCoercion)
forall a b. (a -> b) -> a -> b
$
TcTyVar -> FlatM (Type, TcCoercion)
flattenTyVar TcTyVar
tv
; String -> SDoc -> TcS ()
traceTcS String
"rewriteTyVar }" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
; Type -> TcS Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
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 -> Type -> TcS (Type, TcCoercion)
flattenKind CtLoc
loc CtFlavour
flav Type
ty
= do { String -> SDoc -> TcS ()
traceTcS String
"flattenKind {" (CtFlavour -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtFlavour
flav SDoc -> SDoc -> SDoc
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
; let flav' :: CtFlavour
flav' = case CtFlavour
flav of
CtFlavour
Derived -> ShadowInfo -> CtFlavour
Wanted ShadowInfo
WDeriv
CtFlavour
_ -> CtFlavour
flav
; (Type
ty', TcCoercion
co) <- FlattenMode
-> CtLoc
-> CtFlavour
-> EqRel
-> FlatM (Type, TcCoercion)
-> TcS (Type, TcCoercion)
forall a.
FlattenMode -> CtLoc -> CtFlavour -> EqRel -> FlatM a -> TcS a
runFlatten FlattenMode
FM_FlattenAll CtLoc
loc CtFlavour
flav' EqRel
NomEq (Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty)
; String -> SDoc -> TcS ()
traceTcS String
"flattenKind }" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty' SDoc -> SDoc -> SDoc
$$ TcCoercion -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcCoercion
co)
; (Type, TcCoercion) -> TcS (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
ty', TcCoercion
co) }
flattenArgsNom :: CtEvidence -> TyCon -> [TcType] -> TcS ([Xi], [TcCoercion], TcCoercionN)
flattenArgsNom :: CtEvidence
-> TyCon -> [Type] -> TcS ([Type], [TcCoercion], TcCoercion)
flattenArgsNom CtEvidence
ev TyCon
tc [Type]
tys
= do { String -> SDoc -> TcS ()
traceTcS String
"flatten_args {" ([SDoc] -> SDoc
vcat ((Type -> SDoc) -> [Type] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
tys))
; ([Type]
tys', [TcCoercion]
cos, TcCoercion
kind_co)
<- FlattenMode
-> CtEvidence
-> FlatM ([Type], [TcCoercion], TcCoercion)
-> TcS ([Type], [TcCoercion], TcCoercion)
forall a. FlattenMode -> CtEvidence -> FlatM a -> TcS a
runFlattenCtEv FlattenMode
FM_FlattenAll CtEvidence
ev (TyCon
-> [Role] -> [Type] -> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args_tc TyCon
tc (Role -> [Role]
forall a. a -> [a]
repeat Role
Nominal) [Type]
tys)
; String -> SDoc -> TcS ()
traceTcS String
"flatten }" ([SDoc] -> SDoc
vcat ((Type -> SDoc) -> [Type] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
tys'))
; ([Type], [TcCoercion], TcCoercion)
-> TcS ([Type], [TcCoercion], TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Type]
tys', [TcCoercion]
cos, TcCoercion
kind_co) }
{-# INLINE flatten_args_tc #-}
flatten_args_tc
:: TyCon
-> [Role]
-> [Type]
-> FlatM ( [Xi]
, [Coercion]
, CoercionN)
flatten_args_tc :: TyCon
-> [Role] -> [Type] -> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args_tc TyCon
tc = [TyCoBinder]
-> Bool
-> Type
-> TcTyCoVarSet
-> [Role]
-> [Type]
-> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args [TyCoBinder]
all_bndrs Bool
any_named_bndrs Type
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, Type
inner_ki, Bool
inner_named) = Type -> ([TyCoBinder], Type, Bool)
split_pi_tys' (TyCon -> Type
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
-> Type
-> TcTyCoVarSet
-> [Role]
-> [Type]
-> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args [TyCoBinder]
orig_binders
Bool
any_named_bndrs
Type
orig_inner_ki
TcTyCoVarSet
orig_fvs
[Role]
orig_roles
[Type]
orig_tys
= if Bool
any_named_bndrs
then [TyCoBinder]
-> Type
-> TcTyCoVarSet
-> [Role]
-> [Type]
-> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args_slow [TyCoBinder]
orig_binders
Type
orig_inner_ki
TcTyCoVarSet
orig_fvs
[Role]
orig_roles
[Type]
orig_tys
else [TyCoBinder]
-> Type
-> [Role]
-> [Type]
-> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args_fast [TyCoBinder]
orig_binders Type
orig_inner_ki [Role]
orig_roles [Type]
orig_tys
{-# INLINE flatten_args_fast #-}
flatten_args_fast :: [TyCoBinder]
-> Kind
-> [Role]
-> [Type]
-> FlatM ([Xi], [Coercion], CoercionN)
flatten_args_fast :: [TyCoBinder]
-> Type
-> [Role]
-> [Type]
-> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args_fast [TyCoBinder]
orig_binders Type
orig_inner_ki [Role]
orig_roles [Type]
orig_tys
= (([Type], [TcCoercion], [TyCoBinder])
-> ([Type], [TcCoercion], TcCoercion))
-> FlatM ([Type], [TcCoercion], [TyCoBinder])
-> FlatM ([Type], [TcCoercion], TcCoercion)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([Type], [TcCoercion], [TyCoBinder])
-> ([Type], [TcCoercion], TcCoercion)
finish ([Type]
-> [Role]
-> [TyCoBinder]
-> FlatM ([Type], [TcCoercion], [TyCoBinder])
iterate [Type]
orig_tys [Role]
orig_roles [TyCoBinder]
orig_binders)
where
iterate :: [Type]
-> [Role]
-> [TyCoBinder]
-> FlatM ([Xi], [Coercion], [TyCoBinder])
iterate :: [Type]
-> [Role]
-> [TyCoBinder]
-> FlatM ([Type], [TcCoercion], [TyCoBinder])
iterate (Type
ty:[Type]
tys) (Role
role:[Role]
roles) (TyCoBinder
_:[TyCoBinder]
binders) = do
(Type
xi, TcCoercion
co) <- Role -> Type -> FlatM (Type, TcCoercion)
go Role
role Type
ty
([Type]
xis, [TcCoercion]
cos, [TyCoBinder]
binders) <- [Type]
-> [Role]
-> [TyCoBinder]
-> FlatM ([Type], [TcCoercion], [TyCoBinder])
iterate [Type]
tys [Role]
roles [TyCoBinder]
binders
([Type], [TcCoercion], [TyCoBinder])
-> FlatM ([Type], [TcCoercion], [TyCoBinder])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Type
xi Type -> [Type] -> [Type]
forall a. a -> [a] -> [a]
: [Type]
xis, TcCoercion
co TcCoercion -> [TcCoercion] -> [TcCoercion]
forall a. a -> [a] -> [a]
: [TcCoercion]
cos, [TyCoBinder]
binders)
iterate [] [Role]
_ [TyCoBinder]
binders = ([Type], [TcCoercion], [TyCoBinder])
-> FlatM ([Type], [TcCoercion], [TyCoBinder])
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], [], [TyCoBinder]
binders)
iterate [Type]
_ [Role]
_ [TyCoBinder]
_ = String -> SDoc -> FlatM ([Type], [TcCoercion], [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 -> Type -> FlatM (Type, TcCoercion)
go Role
role Type
ty
= case Role
role of
Role
Nominal -> EqRel -> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
NomEq (FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion))
-> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a b. (a -> b) -> a -> b
$ Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty
Role
Representational -> EqRel -> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
ReprEq (FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion))
-> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a b. (a -> b) -> a -> b
$ Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty
Role
Phantom ->
do { Type
ty <- TcS Type -> FlatM Type
forall a. TcS a -> FlatM a
liftTcS (TcS Type -> FlatM Type) -> TcS Type -> FlatM Type
forall a b. (a -> b) -> a -> b
$ Type -> TcS Type
zonkTcType Type
ty
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
ty, Role -> Type -> TcCoercion
mkReflCo Role
Phantom Type
ty) }
{-# INLINE finish #-}
finish :: ([Xi], [Coercion], [TyCoBinder]) -> ([Xi], [Coercion], CoercionN)
finish :: ([Type], [TcCoercion], [TyCoBinder])
-> ([Type], [TcCoercion], TcCoercion)
finish ([Type]
xis, [TcCoercion]
cos, [TyCoBinder]
binders) = ([Type]
xis, [TcCoercion]
cos, TcCoercion
kind_co)
where
final_kind :: Type
final_kind = [TyCoBinder] -> Type -> Type
mkPiTys [TyCoBinder]
binders Type
orig_inner_ki
kind_co :: TcCoercion
kind_co = Type -> TcCoercion
mkNomReflCo Type
final_kind
{-# INLINE flatten_args_slow #-}
flatten_args_slow :: [TyCoBinder] -> Kind -> TcTyCoVarSet
-> [Role] -> [Type]
-> FlatM ([Xi], [Coercion], CoercionN)
flatten_args_slow :: [TyCoBinder]
-> Type
-> TcTyCoVarSet
-> [Role]
-> [Type]
-> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args_slow [TyCoBinder]
binders Type
inner_ki TcTyCoVarSet
fvs [Role]
roles [Type]
tys
= do { [(Type, TcCoercion)]
flattened_args <- (Bool -> Role -> Type -> FlatM (Type, TcCoercion))
-> [Bool] -> [Role] -> [Type] -> FlatM [(Type, TcCoercion)]
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Bool -> Role -> Type -> FlatM (Type, TcCoercion)
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 [Type]
tys
; ([Type], [TcCoercion], TcCoercion)
-> FlatM ([Type], [TcCoercion], TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return ([TyCoBinder]
-> Type
-> TcTyCoVarSet
-> [Role]
-> [(Type, TcCoercion)]
-> ([Type], [TcCoercion], TcCoercion)
simplifyArgsWorker [TyCoBinder]
binders Type
inner_ki TcTyCoVarSet
fvs [Role]
roles [(Type, TcCoercion)]
flattened_args) }
where
{-# INLINE fl #-}
fl :: Bool
-> Role -> Type -> FlatM (Xi, Coercion)
fl :: Bool -> Role -> Type -> FlatM (Type, TcCoercion)
fl Bool
True Role
r Type
ty = FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a. FlatM a -> FlatM a
noBogusCoercions (FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion))
-> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a b. (a -> b) -> a -> b
$ Role -> Type -> FlatM (Type, TcCoercion)
fl1 Role
r Type
ty
fl Bool
False Role
r Type
ty = Role -> Type -> FlatM (Type, TcCoercion)
fl1 Role
r Type
ty
{-# INLINE fl1 #-}
fl1 :: Role -> Type -> FlatM (Xi, Coercion)
fl1 :: Role -> Type -> FlatM (Type, TcCoercion)
fl1 Role
Nominal Type
ty
= EqRel -> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
NomEq (FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion))
-> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a b. (a -> b) -> a -> b
$
Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty
fl1 Role
Representational Type
ty
= EqRel -> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
ReprEq (FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion))
-> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a b. (a -> b) -> a -> b
$
Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty
fl1 Role
Phantom Type
ty
= do { Type
ty <- TcS Type -> FlatM Type
forall a. TcS a -> FlatM a
liftTcS (TcS Type -> FlatM Type) -> TcS Type -> FlatM Type
forall a b. (a -> b) -> a -> b
$ Type -> TcS Type
zonkTcType Type
ty
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
ty, Role -> Type -> TcCoercion
mkReflCo Role
Phantom Type
ty) }
flatten_one :: TcType -> FlatM (Xi, Coercion)
flatten_one :: Type -> FlatM (Type, TcCoercion)
flatten_one xi :: Type
xi@(LitTy {})
= do { Role
role <- FlatM Role
getRole
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
xi, Role -> Type -> TcCoercion
mkReflCo Role
role Type
xi) }
flatten_one (TyVarTy TcTyVar
tv)
= TcTyVar -> FlatM (Type, TcCoercion)
flattenTyVar TcTyVar
tv
flatten_one (AppTy Type
ty1 Type
ty2)
= Type -> [Type] -> FlatM (Type, TcCoercion)
flatten_app_tys Type
ty1 [Type
ty2]
flatten_one (TyConApp TyCon
tc [Type]
tys)
| Just ([(TcTyVar, Type)]
tenv, Type
rhs, [Type]
tys') <- TyCon -> [Type] -> Maybe ([(TcTyVar, Type)], Type, [Type])
forall tyco.
TyCon -> [tyco] -> Maybe ([(TcTyVar, tyco)], Type, [tyco])
expandSynTyCon_maybe TyCon
tc [Type]
tys
, let expanded_ty :: Type
expanded_ty = Type -> [Type] -> Type
mkAppTys (HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy ([(TcTyVar, Type)] -> TCvSubst
mkTvSubstPrs [(TcTyVar, Type)]
tenv) Type
rhs) [Type]
tys'
= do { FlattenMode
mode <- FlatM FlattenMode
getMode
; case FlattenMode
mode of
FlattenMode
FM_FlattenAll | Bool -> Bool
not (TyCon -> Bool
isFamFreeTyCon TyCon
tc)
-> Type -> FlatM (Type, TcCoercion)
flatten_one Type
expanded_ty
FlattenMode
_ -> TyCon -> [Type] -> FlatM (Type, TcCoercion)
flatten_ty_con_app TyCon
tc [Type]
tys }
| TyCon -> Bool
isTypeFamilyTyCon TyCon
tc
= TyCon -> [Type] -> FlatM (Type, TcCoercion)
flatten_fam_app TyCon
tc [Type]
tys
| Bool
otherwise
= TyCon -> [Type] -> FlatM (Type, TcCoercion)
flatten_ty_con_app TyCon
tc [Type]
tys
flatten_one ty :: Type
ty@(FunTy AnonArgFlag
_ Type
ty1 Type
ty2)
= do { (Type
xi1,TcCoercion
co1) <- Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty1
; (Type
xi2,TcCoercion
co2) <- Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty2
; Role
role <- FlatM Role
getRole
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
ty { ft_arg :: Type
ft_arg = Type
xi1, ft_res :: Type
ft_res = Type
xi2 }
, Role -> TcCoercion -> TcCoercion -> TcCoercion
mkFunCo Role
role TcCoercion
co1 TcCoercion
co2) }
flatten_one ty :: Type
ty@(ForAllTy {})
= do { let ([TyVarBinder]
bndrs, Type
rho) = Type -> ([TyVarBinder], Type)
tcSplitForAllVarBndrs Type
ty
tvs :: [TcTyVar]
tvs = [TyVarBinder] -> [TcTyVar]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [TyVarBinder]
bndrs
; (Type
rho', TcCoercion
co) <- FlattenMode -> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a. FlattenMode -> FlatM a -> FlatM a
setMode FlattenMode
FM_SubstOnly (FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion))
-> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a b. (a -> b) -> a -> b
$ Type -> FlatM (Type, TcCoercion)
flatten_one Type
rho
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return ([TyVarBinder] -> Type -> Type
mkForAllTys [TyVarBinder]
bndrs Type
rho', [TcTyVar] -> TcCoercion -> TcCoercion
mkHomoForAllCos [TcTyVar]
tvs TcCoercion
co) }
flatten_one (CastTy Type
ty TcCoercion
g)
= do { (Type
xi, TcCoercion
co) <- Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty
; (TcCoercion
g', TcCoercion
_) <- TcCoercion -> FlatM (TcCoercion, TcCoercion)
flatten_co TcCoercion
g
; Role
role <- FlatM Role
getRole
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> TcCoercion -> Type
mkCastTy Type
xi TcCoercion
g', TcCoercion
-> Role -> Type -> Type -> TcCoercion -> TcCoercion -> TcCoercion
castCoercionKind TcCoercion
co Role
role Type
xi Type
ty TcCoercion
g' TcCoercion
g) }
flatten_one (CoercionTy TcCoercion
co) = (TcCoercion -> Type)
-> (TcCoercion, TcCoercion) -> (Type, TcCoercion)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first TcCoercion -> Type
mkCoercionTy ((TcCoercion, TcCoercion) -> (Type, TcCoercion))
-> FlatM (TcCoercion, TcCoercion) -> FlatM (Type, TcCoercion)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcCoercion -> FlatM (TcCoercion, TcCoercion)
flatten_co TcCoercion
co
flatten_co :: Coercion -> FlatM (Coercion, Coercion)
flatten_co :: TcCoercion -> FlatM (TcCoercion, TcCoercion)
flatten_co TcCoercion
co
= do { TcCoercion
co <- TcS TcCoercion -> FlatM TcCoercion
forall a. TcS a -> FlatM a
liftTcS (TcS TcCoercion -> FlatM TcCoercion)
-> TcS TcCoercion -> FlatM TcCoercion
forall a b. (a -> b) -> a -> b
$ TcCoercion -> TcS TcCoercion
zonkCo TcCoercion
co
; Role
env_role <- FlatM Role
getRole
; let co' :: TcCoercion
co' = Role -> Type -> TcCoercion
mkTcReflCo Role
env_role (TcCoercion -> Type
mkCoercionTy TcCoercion
co)
; (TcCoercion, TcCoercion) -> FlatM (TcCoercion, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcCoercion
co, TcCoercion
co') }
flatten_app_tys :: Type -> [Type] -> FlatM (Xi, Coercion)
flatten_app_tys :: Type -> [Type] -> FlatM (Type, TcCoercion)
flatten_app_tys (AppTy Type
ty1 Type
ty2) [Type]
tys = Type -> [Type] -> FlatM (Type, TcCoercion)
flatten_app_tys Type
ty1 (Type
ty2Type -> [Type] -> [Type]
forall a. a -> [a] -> [a]
:[Type]
tys)
flatten_app_tys Type
fun_ty [Type]
arg_tys
= do { (Type
fun_xi, TcCoercion
fun_co) <- Type -> FlatM (Type, TcCoercion)
flatten_one Type
fun_ty
; Type -> TcCoercion -> [Type] -> FlatM (Type, TcCoercion)
flatten_app_ty_args Type
fun_xi TcCoercion
fun_co [Type]
arg_tys }
flatten_app_ty_args :: Xi -> Coercion -> [Type] -> FlatM (Xi, Coercion)
flatten_app_ty_args :: Type -> TcCoercion -> [Type] -> FlatM (Type, TcCoercion)
flatten_app_ty_args Type
fun_xi TcCoercion
fun_co []
= (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
fun_xi, TcCoercion
fun_co)
flatten_app_ty_args Type
fun_xi TcCoercion
fun_co [Type]
arg_tys
= do { (Type
xi, TcCoercion
co, TcCoercion
kind_co) <- case HasCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
tcSplitTyConApp_maybe Type
fun_xi of
Just (TyCon
tc, [Type]
xis) ->
do { let tc_roles :: [Role]
tc_roles = TyCon -> [Role]
tyConRolesRepresentational TyCon
tc
arg_roles :: [Role]
arg_roles = [Type] -> [Role] -> [Role]
forall b a. [b] -> [a] -> [a]
dropList [Type]
xis [Role]
tc_roles
; ([Type]
arg_xis, [TcCoercion]
arg_cos, TcCoercion
kind_co)
<- Type
-> [Role] -> [Type] -> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_vector (HasDebugCallStack => Type -> Type
Type -> Type
tcTypeKind Type
fun_xi) [Role]
arg_roles [Type]
arg_tys
; EqRel
eq_rel <- FlatM EqRel
getEqRel
; let app_xi :: Type
app_xi = TyCon -> [Type] -> Type
mkTyConApp TyCon
tc ([Type]
xis [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
arg_xis)
app_co :: TcCoercion
app_co = case EqRel
eq_rel of
EqRel
NomEq -> TcCoercion -> [TcCoercion] -> TcCoercion
mkAppCos TcCoercion
fun_co [TcCoercion]
arg_cos
EqRel
ReprEq -> Role -> TyCon -> [TcCoercion] -> TcCoercion
mkTcTyConAppCo Role
Representational TyCon
tc
((Role -> Type -> TcCoercion) -> [Role] -> [Type] -> [TcCoercion]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Role -> Type -> TcCoercion
mkReflCo [Role]
tc_roles [Type]
xis [TcCoercion] -> [TcCoercion] -> [TcCoercion]
forall a. [a] -> [a] -> [a]
++ [TcCoercion]
arg_cos)
TcCoercion -> TcCoercion -> TcCoercion
`mkTcTransCo`
TcCoercion -> [TcCoercion] -> TcCoercion
mkAppCos TcCoercion
fun_co ((Type -> TcCoercion) -> [Type] -> [TcCoercion]
forall a b. (a -> b) -> [a] -> [b]
map Type -> TcCoercion
mkNomReflCo [Type]
arg_tys)
; (Type, TcCoercion, TcCoercion)
-> FlatM (Type, TcCoercion, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
app_xi, TcCoercion
app_co, TcCoercion
kind_co) }
Maybe (TyCon, [Type])
Nothing ->
do { ([Type]
arg_xis, [TcCoercion]
arg_cos, TcCoercion
kind_co)
<- Type
-> [Role] -> [Type] -> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_vector (HasDebugCallStack => Type -> Type
Type -> Type
tcTypeKind Type
fun_xi) (Role -> [Role]
forall a. a -> [a]
repeat Role
Nominal) [Type]
arg_tys
; let arg_xi :: Type
arg_xi = Type -> [Type] -> Type
mkAppTys Type
fun_xi [Type]
arg_xis
arg_co :: TcCoercion
arg_co = TcCoercion -> [TcCoercion] -> TcCoercion
mkAppCos TcCoercion
fun_co [TcCoercion]
arg_cos
; (Type, TcCoercion, TcCoercion)
-> FlatM (Type, TcCoercion, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
arg_xi, TcCoercion
arg_co, TcCoercion
kind_co) }
; Role
role <- FlatM Role
getRole
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> TcCoercion -> Role -> TcCoercion -> (Type, TcCoercion)
homogenise_result Type
xi TcCoercion
co Role
role TcCoercion
kind_co) }
flatten_ty_con_app :: TyCon -> [TcType] -> FlatM (Xi, Coercion)
flatten_ty_con_app :: TyCon -> [Type] -> FlatM (Type, TcCoercion)
flatten_ty_con_app TyCon
tc [Type]
tys
= do { Role
role <- FlatM Role
getRole
; ([Type]
xis, [TcCoercion]
cos, TcCoercion
kind_co) <- TyCon
-> [Role] -> [Type] -> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args_tc TyCon
tc (Role -> TyCon -> [Role]
tyConRolesX Role
role TyCon
tc) [Type]
tys
; let tyconapp_xi :: Type
tyconapp_xi = TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
xis
tyconapp_co :: TcCoercion
tyconapp_co = HasDebugCallStack => Role -> TyCon -> [TcCoercion] -> TcCoercion
Role -> TyCon -> [TcCoercion] -> TcCoercion
mkTyConAppCo Role
role TyCon
tc [TcCoercion]
cos
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> TcCoercion -> Role -> TcCoercion -> (Type, TcCoercion)
homogenise_result Type
tyconapp_xi TcCoercion
tyconapp_co Role
role TcCoercion
kind_co) }
homogenise_result :: Xi
-> Coercion
-> Role
-> CoercionN
-> (Xi, Coercion)
homogenise_result :: Type -> TcCoercion -> Role -> TcCoercion -> (Type, TcCoercion)
homogenise_result Type
xi TcCoercion
co Role
r TcCoercion
kind_co
| TcCoercion -> Bool
isGReflCo TcCoercion
kind_co = (Type
xi Type -> TcCoercion -> Type
`mkCastTy` TcCoercion
kind_co, TcCoercion
co)
| Bool
otherwise = (Type
xi Type -> TcCoercion -> Type
`mkCastTy` TcCoercion
kind_co
, (TcCoercion -> TcCoercion
mkSymCo (TcCoercion -> TcCoercion) -> TcCoercion -> TcCoercion
forall a b. (a -> b) -> a -> b
$ Role -> Type -> MCoercionN -> TcCoercion
GRefl Role
r Type
xi (TcCoercion -> MCoercionN
MCo TcCoercion
kind_co)) TcCoercion -> TcCoercion -> TcCoercion
`mkTransCo` TcCoercion
co)
{-# INLINE homogenise_result #-}
flatten_vector :: Kind
-> [Role]
-> [Type]
-> FlatM ([Xi], [Coercion], CoercionN)
flatten_vector :: Type
-> [Role] -> [Type] -> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_vector Type
ki [Role]
roles [Type]
tys
= do { EqRel
eq_rel <- FlatM EqRel
getEqRel
; case EqRel
eq_rel of
EqRel
NomEq -> [TyCoBinder]
-> Bool
-> Type
-> TcTyCoVarSet
-> [Role]
-> [Type]
-> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args [TyCoBinder]
bndrs
Bool
any_named_bndrs
Type
inner_ki
TcTyCoVarSet
fvs
(Role -> [Role]
forall a. a -> [a]
repeat Role
Nominal)
[Type]
tys
EqRel
ReprEq -> [TyCoBinder]
-> Bool
-> Type
-> TcTyCoVarSet
-> [Role]
-> [Type]
-> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args [TyCoBinder]
bndrs
Bool
any_named_bndrs
Type
inner_ki
TcTyCoVarSet
fvs
[Role]
roles
[Type]
tys
}
where
([TyCoBinder]
bndrs, Type
inner_ki, Bool
any_named_bndrs) = Type -> ([TyCoBinder], Type, Bool)
split_pi_tys' Type
ki
fvs :: TcTyCoVarSet
fvs = Type -> TcTyCoVarSet
tyCoVarsOfType Type
ki
{-# INLINE flatten_vector #-}
flatten_fam_app :: TyCon -> [TcType] -> FlatM (Xi, Coercion)
flatten_fam_app :: TyCon -> [Type] -> FlatM (Type, TcCoercion)
flatten_fam_app TyCon
tc [Type]
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 -> [Type] -> FlatM (Type, TcCoercion)
flatten_ty_con_app TyCon
tc [Type]
tys
; FlattenMode
FM_FlattenAll ->
do { let ([Type]
tys1, [Type]
tys_rest) = Int -> [Type] -> ([Type], [Type])
forall a. Int -> [a] -> ([a], [a])
splitAt (TyCon -> Int
tyConArity TyCon
tc) [Type]
tys
; (Type
xi1, TcCoercion
co1) <- TyCon -> [Type] -> FlatM (Type, TcCoercion)
flatten_exact_fam_app_fully TyCon
tc [Type]
tys1
; Type -> TcCoercion -> [Type] -> FlatM (Type, TcCoercion)
flatten_app_ty_args Type
xi1 TcCoercion
co1 [Type]
tys_rest } } }
flatten_exact_fam_app_fully :: TyCon -> [TcType] -> FlatM (Xi, Coercion)
flatten_exact_fam_app_fully :: TyCon -> [Type] -> FlatM (Type, TcCoercion)
flatten_exact_fam_app_fully TyCon
tc [Type]
tys
= do { Maybe (Type, TcCoercion)
mOut <- TyCon -> [Type] -> FlatM (Maybe (Type, TcCoercion))
try_to_reduce_nocache TyCon
tc [Type]
tys
; case Maybe (Type, TcCoercion)
mOut of
Just (Type, TcCoercion)
out -> (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Type, TcCoercion)
out
Maybe (Type, TcCoercion)
Nothing -> do
{
; ([Type]
xis, [TcCoercion]
cos, TcCoercion
kind_co)
<- EqRel
-> FlatM ([Type], [TcCoercion], TcCoercion)
-> FlatM ([Type], [TcCoercion], TcCoercion)
forall a. EqRel -> FlatM a -> FlatM a
setEqRel EqRel
NomEq (FlatM ([Type], [TcCoercion], TcCoercion)
-> FlatM ([Type], [TcCoercion], TcCoercion))
-> FlatM ([Type], [TcCoercion], TcCoercion)
-> FlatM ([Type], [TcCoercion], TcCoercion)
forall a b. (a -> b) -> a -> b
$
TyCon
-> [Role] -> [Type] -> FlatM ([Type], [TcCoercion], TcCoercion)
flatten_args_tc TyCon
tc (Role -> [Role]
forall a. a -> [a]
repeat Role
Nominal) [Type]
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 :: TcCoercion
ret_co = HasDebugCallStack => Role -> TyCon -> [TcCoercion] -> TcCoercion
Role -> TyCon -> [TcCoercion] -> TcCoercion
mkTyConAppCo Role
role TyCon
tc [TcCoercion]
cos
; Maybe (TcCoercion, Type, CtFlavour)
mb_ct <- TcS (Maybe (TcCoercion, Type, CtFlavour))
-> FlatM (Maybe (TcCoercion, Type, CtFlavour))
forall a. TcS a -> FlatM a
liftTcS (TcS (Maybe (TcCoercion, Type, CtFlavour))
-> FlatM (Maybe (TcCoercion, Type, CtFlavour)))
-> TcS (Maybe (TcCoercion, Type, CtFlavour))
-> FlatM (Maybe (TcCoercion, Type, CtFlavour))
forall a b. (a -> b) -> a -> b
$ TyCon -> [Type] -> TcS (Maybe (TcCoercion, Type, CtFlavour))
lookupFlatCache TyCon
tc [Type]
xis
; case Maybe (TcCoercion, Type, CtFlavour)
mb_ct of
Just (TcCoercion
co, Type
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
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
xis SDoc -> SDoc -> SDoc
$$ Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
rhs_ty)
; (Type
fsk_xi, TcCoercion
fsk_co) <- Type -> FlatM (Type, TcCoercion)
flatten_one Type
rhs_ty
; let xi :: Type
xi = Type
fsk_xi Type -> TcCoercion -> Type
`mkCastTy` TcCoercion
kind_co
co' :: TcCoercion
co' = Role -> Type -> TcCoercion -> TcCoercion -> TcCoercion
mkTcCoherenceLeftCo Role
role Type
fsk_xi TcCoercion
kind_co TcCoercion
fsk_co
TcCoercion -> TcCoercion -> TcCoercion
`mkTransCo`
EqRel -> TcCoercion -> TcCoercion
maybeTcSubCo EqRel
eq_rel (TcCoercion -> TcCoercion
mkSymCo TcCoercion
co)
TcCoercion -> TcCoercion -> TcCoercion
`mkTransCo` TcCoercion
ret_co
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
xi, TcCoercion
co')
}
Maybe (TcCoercion, Type, CtFlavour)
_ -> do { Maybe (Type, TcCoercion)
mOut <- TyCon
-> [Type]
-> TcCoercion
-> (TcCoercion -> TcCoercion)
-> FlatM (Maybe (Type, TcCoercion))
try_to_reduce TyCon
tc
[Type]
xis
TcCoercion
kind_co
(TcCoercion -> TcCoercion -> TcCoercion
`mkTransCo` TcCoercion
ret_co)
; case Maybe (Type, TcCoercion)
mOut of
Just (Type, TcCoercion)
out -> (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Type, TcCoercion)
out
Maybe (Type, TcCoercion)
Nothing -> do
{ CtLoc
loc <- FlatM CtLoc
getLoc
; (CtEvidence
ev, TcCoercion
co, TcTyVar
fsk) <- TcS (CtEvidence, TcCoercion, TcTyVar)
-> FlatM (CtEvidence, TcCoercion, TcTyVar)
forall a. TcS a -> FlatM a
liftTcS (TcS (CtEvidence, TcCoercion, TcTyVar)
-> FlatM (CtEvidence, TcCoercion, TcTyVar))
-> TcS (CtEvidence, TcCoercion, TcTyVar)
-> FlatM (CtEvidence, TcCoercion, TcTyVar)
forall a b. (a -> b) -> a -> b
$
CtFlavour
-> CtLoc
-> TyCon
-> [Type]
-> TcS (CtEvidence, TcCoercion, TcTyVar)
newFlattenSkolem CtFlavour
cur_flav CtLoc
loc TyCon
tc [Type]
xis
; let ct :: Ct
ct = CFunEqCan :: CtEvidence -> TyCon -> [Type] -> TcTyVar -> Ct
CFunEqCan { cc_ev :: CtEvidence
cc_ev = CtEvidence
ev
, cc_fun :: TyCon
cc_fun = TyCon
tc
, cc_tyargs :: [Type]
cc_tyargs = [Type]
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
<+> [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
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 :: Type
fsk_ty = TcTyVar -> Type
mkTyVarTy TcTyVar
fsk
xi :: Type
xi = Type
fsk_ty Type -> TcCoercion -> Type
`mkCastTy` TcCoercion
kind_co
co' :: TcCoercion
co' = Role -> Type -> TcCoercion -> TcCoercion -> TcCoercion
mkTcCoherenceLeftCo Role
role Type
fsk_ty TcCoercion
kind_co (EqRel -> TcCoercion -> TcCoercion
maybeTcSubCo EqRel
eq_rel (TcCoercion -> TcCoercion
mkSymCo TcCoercion
co))
TcCoercion -> TcCoercion -> TcCoercion
`mkTransCo` TcCoercion
ret_co
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
xi, TcCoercion
co')
}
}
}
}
where
try_to_reduce :: TyCon
-> [Type]
-> CoercionN
-> ( Coercion
-> Coercion )
-> FlatM (Maybe (Xi, Coercion))
try_to_reduce :: TyCon
-> [Type]
-> TcCoercion
-> (TcCoercion -> TcCoercion)
-> FlatM (Maybe (Type, TcCoercion))
try_to_reduce TyCon
tc [Type]
tys TcCoercion
kind_co TcCoercion -> TcCoercion
update_co
= do { Type -> FlatM ()
checkStackDepth (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
tys)
; Maybe (TcCoercion, Type)
mb_match <- TcS (Maybe (TcCoercion, Type)) -> FlatM (Maybe (TcCoercion, Type))
forall a. TcS a -> FlatM a
liftTcS (TcS (Maybe (TcCoercion, Type))
-> FlatM (Maybe (TcCoercion, Type)))
-> TcS (Maybe (TcCoercion, Type))
-> FlatM (Maybe (TcCoercion, Type))
forall a b. (a -> b) -> a -> b
$ TyCon -> [Type] -> TcS (Maybe (TcCoercion, Type))
matchFam TyCon
tc [Type]
tys
; case Maybe (TcCoercion, Type)
mb_match of
Just (TcCoercion
norm_co, Type
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, [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
tys, Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
norm_ty
, TcCoercion -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcCoercion
norm_co SDoc -> SDoc -> SDoc
<+> SDoc
dcolon
SDoc -> SDoc -> SDoc
<+> Pair Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr (TcCoercion -> Pair Type
coercionKind TcCoercion
norm_co)
]
; (Type
xi, TcCoercion
final_co) <- FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a. FlatM a -> FlatM a
bumpDepth (FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion))
-> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a b. (a -> b) -> a -> b
$ Type -> FlatM (Type, TcCoercion)
flatten_one Type
norm_ty
; EqRel
eq_rel <- FlatM EqRel
getEqRel
; let co :: TcCoercion
co = EqRel -> TcCoercion -> TcCoercion
maybeTcSubCo EqRel
eq_rel TcCoercion
norm_co
TcCoercion -> TcCoercion -> TcCoercion
`mkTransCo` TcCoercion -> TcCoercion
mkSymCo TcCoercion
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 -> [Type] -> (TcCoercion, Type, CtFlavour) -> TcS ()
extendFlatCache TyCon
tc [Type]
tys ( TcCoercion
co, Type
xi, CtFlavour
flavour )
; let role :: Role
role = EqRel -> Role
eqRelRole EqRel
eq_rel
xi' :: Type
xi' = Type
xi Type -> TcCoercion -> Type
`mkCastTy` TcCoercion
kind_co
co' :: TcCoercion
co' = TcCoercion -> TcCoercion
update_co (TcCoercion -> TcCoercion) -> TcCoercion -> TcCoercion
forall a b. (a -> b) -> a -> b
$
Role -> Type -> TcCoercion -> TcCoercion -> TcCoercion
mkTcCoherenceLeftCo Role
role Type
xi TcCoercion
kind_co (TcCoercion -> TcCoercion
mkSymCo TcCoercion
co)
; Maybe (Type, TcCoercion) -> FlatM (Maybe (Type, TcCoercion))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Type, TcCoercion) -> FlatM (Maybe (Type, TcCoercion)))
-> Maybe (Type, TcCoercion) -> FlatM (Maybe (Type, TcCoercion))
forall a b. (a -> b) -> a -> b
$ (Type, TcCoercion) -> Maybe (Type, TcCoercion)
forall a. a -> Maybe a
Just (Type
xi', TcCoercion
co') }
Maybe (TcCoercion, Type)
Nothing -> Maybe (Type, TcCoercion) -> FlatM (Maybe (Type, TcCoercion))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Type, TcCoercion)
forall a. Maybe a
Nothing }
try_to_reduce_nocache :: TyCon
-> [Type]
-> FlatM (Maybe (Xi, Coercion))
try_to_reduce_nocache :: TyCon -> [Type] -> FlatM (Maybe (Type, TcCoercion))
try_to_reduce_nocache TyCon
tc [Type]
tys
= do { Type -> FlatM ()
checkStackDepth (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
tys)
; Maybe (TcCoercion, Type)
mb_match <- TcS (Maybe (TcCoercion, Type)) -> FlatM (Maybe (TcCoercion, Type))
forall a. TcS a -> FlatM a
liftTcS (TcS (Maybe (TcCoercion, Type))
-> FlatM (Maybe (TcCoercion, Type)))
-> TcS (Maybe (TcCoercion, Type))
-> FlatM (Maybe (TcCoercion, Type))
forall a b. (a -> b) -> a -> b
$ TyCon -> [Type] -> TcS (Maybe (TcCoercion, Type))
matchFam TyCon
tc [Type]
tys
; case Maybe (TcCoercion, Type)
mb_match of
Just (TcCoercion
norm_co, Type
norm_ty)
-> do { (Type
xi, TcCoercion
final_co) <- FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a. FlatM a -> FlatM a
bumpDepth (FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion))
-> FlatM (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall a b. (a -> b) -> a -> b
$ Type -> FlatM (Type, TcCoercion)
flatten_one Type
norm_ty
; EqRel
eq_rel <- FlatM EqRel
getEqRel
; let co :: TcCoercion
co = TcCoercion -> TcCoercion
mkSymCo (EqRel -> TcCoercion -> TcCoercion
maybeTcSubCo EqRel
eq_rel TcCoercion
norm_co
TcCoercion -> TcCoercion -> TcCoercion
`mkTransCo` TcCoercion -> TcCoercion
mkSymCo TcCoercion
final_co)
; Maybe (Type, TcCoercion) -> FlatM (Maybe (Type, TcCoercion))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Type, TcCoercion) -> FlatM (Maybe (Type, TcCoercion)))
-> Maybe (Type, TcCoercion) -> FlatM (Maybe (Type, TcCoercion))
forall a b. (a -> b) -> a -> b
$ (Type, TcCoercion) -> Maybe (Type, TcCoercion)
forall a. a -> Maybe a
Just (Type
xi, TcCoercion
co) }
Maybe (TcCoercion, Type)
Nothing -> Maybe (Type, TcCoercion) -> FlatM (Maybe (Type, TcCoercion))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Type, TcCoercion)
forall a. Maybe a
Nothing }
data FlattenTvResult
= FTRNotFollowed
| FTRFollowed TcType Coercion
flattenTyVar :: TyVar -> FlatM (Xi, Coercion)
flattenTyVar :: TcTyVar -> FlatM (Type, TcCoercion)
flattenTyVar TcTyVar
tv
= do { FlattenTvResult
mb_yes <- TcTyVar -> FlatM FlattenTvResult
flatten_tyvar1 TcTyVar
tv
; case FlattenTvResult
mb_yes of
FTRFollowed Type
ty1 TcCoercion
co1
-> do { (Type
ty2, TcCoercion
co2) <- Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty1
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
ty2, TcCoercion
co2 TcCoercion -> TcCoercion -> TcCoercion
`mkTransCo` TcCoercion
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
$ (Type -> TcS Type) -> TcTyVar -> TcS TcTyVar
forall (m :: * -> *).
Monad m =>
(Type -> m Type) -> TcTyVar -> m TcTyVar
updateTyVarKindM Type -> TcS Type
zonkTcType TcTyVar
tv
; Role
role <- FlatM Role
getRole
; let ty' :: Type
ty' = TcTyVar -> Type
mkTyVarTy TcTyVar
tv'
; (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
ty', Role -> Type -> TcCoercion
mkTcReflCo Role
role Type
ty') } }
flatten_tyvar1 :: TcTyVar -> FlatM FlattenTvResult
flatten_tyvar1 :: TcTyVar -> FlatM FlattenTvResult
flatten_tyvar1 TcTyVar
tv
= do { Maybe Type
mb_ty <- TcS (Maybe Type) -> FlatM (Maybe Type)
forall a. TcS a -> FlatM a
liftTcS (TcS (Maybe Type) -> FlatM (Maybe Type))
-> TcS (Maybe Type) -> FlatM (Maybe Type)
forall a b. (a -> b) -> a -> b
$ TcTyVar -> TcS (Maybe Type)
isFilledMetaTyVar_maybe TcTyVar
tv
; case Maybe Type
mb_ty of
Just Type
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
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
; Role
role <- FlatM Role
getRole
; FlattenTvResult -> FlatM FlattenTvResult
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> TcCoercion -> FlattenTvResult
FTRFollowed Type
ty (Role -> Type -> TcCoercion
mkReflCo Role
role Type
ty)) } ;
Maybe Type
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 -> Type
cc_rhs = Type
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
<+>
Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
rhs_ty SDoc -> SDoc -> SDoc
$$ CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
ctev)
; let rewrite_co1 :: TcCoercion
rewrite_co1 = TcCoercion -> TcCoercion
mkSymCo (HasDebugCallStack => CtEvidence -> TcCoercion
CtEvidence -> TcCoercion
ctEvCoercion CtEvidence
ctev)
rewrite_co :: TcCoercion
rewrite_co = case (EqRel
ct_eq_rel, EqRel
eq_rel) of
(EqRel
ReprEq, EqRel
_rel) -> ASSERT( _rel == ReprEq )
TcCoercion
rewrite_co1
(EqRel
NomEq, EqRel
NomEq) -> TcCoercion
rewrite_co1
(EqRel
NomEq, EqRel
ReprEq) -> TcCoercion -> TcCoercion
mkSubCo TcCoercion
rewrite_co1
; FlattenTvResult -> FlatM FlattenTvResult
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> TcCoercion -> FlattenTvResult
FTRFollowed Type
rhs_ty TcCoercion
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 -> [Type]
cc_tyargs = [Type]
xis
, cc_fsk :: Ct -> TcTyVar
cc_fsk = TcTyVar
fmv, cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev }) Cts
rest
= do {
Type
rhs' <- Type -> TcS Type
zonkTcType (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
xis)
; case [TcTyVar] -> Type -> Maybe Type
occCheckExpand [TcTyVar
fmv] Type
rhs' of
Just Type
rhs''
-> do { CtEvidence -> EqRel -> Type -> TcS ()
setReflEvidence CtEvidence
ev EqRel
NomEq Type
rhs''
; TcTyVar -> Type -> TcS ()
unflattenFmv TcTyVar
fmv Type
rhs''
; Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return Cts
rest }
Maybe Type
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 -> Type
cc_rhs = Type
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 -> Type -> TcS Bool
tryFill CtEvidence
ev TcTyVar
tv Type
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 -> Type -> TcS Bool
try_fill_rhs CtEvidence
ev TcLevel
tclvl TcTyVar
tv Type
rhs }
; if Bool
elim
then do { CtEvidence -> EqRel -> Type -> TcS ()
setReflEvidence CtEvidence
ev EqRel
eq_rel (TcTyVar -> Type
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 -> Type -> TcS Bool
try_fill_rhs CtEvidence
ev TcLevel
tclvl TcTyVar
lhs_tv Type
rhs
| Just (TcTyVar
rhs_tv, TcCoercion
co) <- Type -> Maybe (TcTyVar, TcCoercion)
getCastedTyVar_maybe Type
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 -> Type -> TcS Bool
tryFill CtEvidence
ev TcTyVar
rhs_tv
(TcTyVar -> Type
mkTyVarTy TcTyVar
lhs_tv Type -> TcCoercion -> Type
`mkCastTy` TcCoercion -> TcCoercion
mkSymCo TcCoercion
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 -> Type
cc_rhs = Type
rhs, cc_eq_rel :: Ct -> EqRel
cc_eq_rel = EqRel
eq_rel }) Cts
rest
| TcTyVar -> Bool
isFmvTyVar TcTyVar
tv
= do { Type
ty1 <- TcTyVar -> TcS Type
zonkTcTyVar TcTyVar
tv
; Type
rhs' <- Type -> TcS Type
zonkTcType Type
rhs
; if Type
ty1 HasDebugCallStack => Type -> Type -> Bool
Type -> Type -> Bool
`tcEqType` Type
rhs'
then do { CtEvidence -> EqRel -> Type -> TcS ()
setReflEvidence CtEvidence
ev EqRel
eq_rel Type
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 -> Type -> TcS Bool
tryFill CtEvidence
ev TcTyVar
tv Type
rhs
= ASSERT2( not (isGiven ev), ppr ev )
do { Type
rhs' <- Type -> TcS Type
zonkTcType Type
rhs
; case () of
()
_ | Just TcTyVar
tv' <- Type -> Maybe TcTyVar
tcGetTyVar_maybe Type
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 Type
rhs'' <- [TcTyVar] -> Type -> Maybe Type
occCheckExpand [TcTyVar
tv] Type
rhs'
-> do {
TcTyVar -> Type -> TcS ()
unifyTyVar TcTyVar
tv Type
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 -> Type -> TcS ()
setReflEvidence CtEvidence
ev EqRel
eq_rel Type
rhs
= CtEvidence -> EvTerm -> TcS ()
setEvBindIfWanted CtEvidence
ev (TcCoercion -> EvTerm
evCoercion TcCoercion
refl_co)
where
refl_co :: TcCoercion
refl_co = Role -> Type -> TcCoercion
mkTcReflCo (EqRel -> Role
eqRelRole EqRel
eq_rel) Type
rhs
split_pi_tys' :: Type -> ([TyCoBinder], Type, Bool)
split_pi_tys' :: Type -> ([TyCoBinder], Type, Bool)
split_pi_tys' Type
ty = Type -> Type -> ([TyCoBinder], Type, Bool)
split Type
ty Type
ty
where
split :: Type -> Type -> ([TyCoBinder], Type, Bool)
split Type
orig_ty Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Type -> ([TyCoBinder], Type, Bool)
split Type
orig_ty Type
ty'
split Type
_ (ForAllTy TyVarBinder
b Type
res) = let ([TyCoBinder]
bs, Type
ty, Bool
_) = Type -> Type -> ([TyCoBinder], Type, Bool)
split Type
res Type
res
in (TyVarBinder -> TyCoBinder
Named TyVarBinder
b TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bs, Type
ty, Bool
True)
split Type
_ (FunTy { ft_af :: Type -> AnonArgFlag
ft_af = AnonArgFlag
af, ft_arg :: Type -> Type
ft_arg = Type
arg, ft_res :: Type -> Type
ft_res = Type
res })
= let ([TyCoBinder]
bs, Type
ty, Bool
named) = Type -> Type -> ([TyCoBinder], Type, Bool)
split Type
res Type
res
in (AnonArgFlag -> Type -> TyCoBinder
Anon AnonArgFlag
af Type
arg TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bs, Type
ty, Bool
named)
split Type
orig_ty Type
_ = ([], Type
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 -> Type -> TyCoBinder
Anon AnonArgFlag
af (TcTyVar -> Type
tyVarKind TcTyVar
tv) TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bndrs, Bool
n)
{-# INLINE go #-}
{-# INLINE ty_con_binders_ty_binders' #-}