{-# LANGUAGE CPP, ViewPatterns, BangPatterns #-}
module TcFlatten(
FlattenMode(..),
flatten, flattenKind, flattenArgsNom,
unflattenWanteds
) where
#include "HsVersions.h"
import GhcPrelude
import TcRnTypes
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 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 FM_FlattenAll = String -> SDoc
text "FM_FlattenAll"
ppr FM_SubstOnly = String -> SDoc
text "FM_SubstOnly"
eqFlattenMode :: FlattenMode -> FlattenMode -> Bool
eqFlattenMode :: FlattenMode -> FlattenMode -> Bool
eqFlattenMode FM_FlattenAll FM_FlattenAll = Bool
True
eqFlattenMode FM_SubstOnly FM_SubstOnly = Bool
True
eqFlattenMode _ _ = Bool
False
newtype FlatM a
= FlatM { FlatM a -> FlattenEnv -> TcS a
runFlatM :: FlattenEnv -> TcS a }
instance Monad FlatM where
m :: FlatM a
m >>= :: FlatM a -> (a -> FlatM b) -> FlatM b
>>= k :: 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
$ \env :: 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 Functor FlatM where
fmap :: (a -> b) -> FlatM a -> FlatM b
fmap = (a -> b) -> FlatM a -> FlatM b
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM
instance Applicative FlatM where
pure :: a -> FlatM a
pure x :: 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 thing_inside :: 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
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
$ \env :: 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 mode :: FlattenMode
mode ev :: 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 mode :: FlattenMode
mode loc :: CtLoc
loc flav :: CtFlavour
flav eq_rel :: EqRel
eq_rel thing_inside :: FlatM a
thing_inside
= do { FlatWorkListRef
flat_ref <- [Ct] -> TcS FlatWorkListRef
forall a. a -> TcS (TcRef a)
newTcRef []
; let fmode :: FlattenEnv
fmode = $WFE :: FlattenMode
-> CtLoc -> CtFlavour -> EqRel -> FlatWorkListRef -> FlattenEnv
FE { fe_mode :: FlattenMode
fe_mode = FlattenMode
mode
, fe_loc :: CtLoc
fe_loc = 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 new_flats :: [Ct]
new_flats wl :: 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 [] wl :: [a]
wl = [a]
wl
add_funeqs (f :: a
f:fs :: [a]
fs) wl :: [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 herald :: String
herald doc :: 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 accessor :: 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
$ \env :: 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 ty :: 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 new_eq_rel :: EqRel
new_eq_rel thing_inside :: 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
$ \env :: 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 new_mode :: FlattenMode
new_mode thing_inside :: 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
$ \env :: 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 thing_inside :: 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
$ \env :: FlattenEnv
env ->
let !env' :: FlattenEnv
env' = case FlattenEnv -> CtFlavour
fe_flavour FlattenEnv
env of
Derived -> FlattenEnv
env { fe_flavour :: CtFlavour
fe_flavour = ShadowInfo -> CtFlavour
Wanted ShadowInfo
WDeriv }
_ -> 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 thing_inside :: 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
$ \env :: 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 mode :: FlattenMode
mode ev :: CtEvidence
ev ty :: Type
ty
= do { String -> SDoc -> TcS ()
traceTcS "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)
; (ty' :: Type
ty', co :: 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 "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) }
flattenKind :: CtLoc -> CtFlavour -> TcType -> TcS (Xi, TcCoercionN)
flattenKind :: CtLoc -> CtFlavour -> Type -> TcS (Type, TcCoercion)
flattenKind loc :: CtLoc
loc flav :: CtFlavour
flav ty :: Type
ty
= do { String -> SDoc -> TcS ()
traceTcS "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
Derived -> ShadowInfo -> CtFlavour
Wanted ShadowInfo
WDeriv
_ -> CtFlavour
flav
; (ty' :: Type
ty', co :: 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 "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 ev :: CtEvidence
ev tc :: TyCon
tc tys :: [Type]
tys
= do { String -> SDoc -> TcS ()
traceTcS "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))
; (tys' :: [Type]
tys', cos :: [TcCoercion]
cos, kind_co :: 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 "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 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
(bndrs :: [TyCoBinder]
bndrs, named :: Bool
named)
= [TyConBinder] -> ([TyCoBinder], Bool)
ty_con_binders_ty_binders' (TyCon -> [TyConBinder]
tyConBinders TyCon
tc)
(inner_bndrs :: [TyCoBinder]
inner_bndrs, inner_ki :: Type
inner_ki, inner_named :: 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 orig_binders :: [TyCoBinder]
orig_binders
any_named_bndrs :: Bool
any_named_bndrs
orig_inner_ki :: Type
orig_inner_ki
orig_fvs :: TcTyCoVarSet
orig_fvs
orig_roles :: [Role]
orig_roles
orig_tys :: [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 orig_binders :: [TyCoBinder]
orig_binders orig_inner_ki :: Type
orig_inner_ki orig_roles :: [Role]
orig_roles orig_tys :: [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 (ty :: Type
ty:tys :: [Type]
tys) (role :: Role
role:roles :: [Role]
roles) (_:binders :: [TyCoBinder]
binders) = do
(xi :: Type
xi, co :: TcCoercion
co) <- Role -> Type -> FlatM (Type, TcCoercion)
go Role
role Type
ty
(xis :: [Type]
xis, cos :: [TcCoercion]
cos, binders :: [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 [] _ binders :: [TyCoBinder]
binders = ([Type], [TcCoercion], [TyCoBinder])
-> FlatM ([Type], [TcCoercion], [TyCoBinder])
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], [], [TyCoBinder]
binders)
iterate _ _ _ = String -> SDoc -> FlatM ([Type], [TcCoercion], [TyCoBinder])
forall a. HasCallStack => String -> SDoc -> a
pprPanic
"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
role ty :: Type
ty
= case Role
role of
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
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
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 (xis :: [Type]
xis, cos :: [TcCoercion]
cos, binders :: [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 binders :: [TyCoBinder]
binders inner_ki :: Type
inner_ki fvs :: TcTyCoVarSet
fvs roles :: [Role]
roles tys :: [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 True r :: Role
r ty :: 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 False r :: Role
r ty :: 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 Nominal ty :: 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 Representational ty :: 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 Phantom ty :: 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 tv :: Var
tv)
= Var -> FlatM (Type, TcCoercion)
flattenTyVar Var
tv
flatten_one (AppTy ty1 :: Type
ty1 ty2 :: Type
ty2)
= Type -> [Type] -> FlatM (Type, TcCoercion)
flatten_app_tys Type
ty1 [Type
ty2]
flatten_one (TyConApp tc :: TyCon
tc tys :: [Type]
tys)
| Just (tenv :: [(Var, Type)]
tenv, rhs :: Type
rhs, tys' :: [Type]
tys') <- TyCon -> [Type] -> Maybe ([(Var, Type)], Type, [Type])
forall tyco. TyCon -> [tyco] -> Maybe ([(Var, 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 ([(Var, Type)] -> TCvSubst
mkTvSubstPrs [(Var, Type)]
tenv) Type
rhs) [Type]
tys'
= do { FlattenMode
mode <- FlatM FlattenMode
getMode
; case FlattenMode
mode of
FM_FlattenAll | Bool -> Bool
not (TyCon -> Bool
isFamFreeTyCon TyCon
tc)
-> Type -> FlatM (Type, TcCoercion)
flatten_one Type
expanded_ty
_ -> 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 (FunTy ty1 :: Type
ty1 ty2 :: Type
ty2)
= do { (xi1 :: Type
xi1,co1 :: TcCoercion
co1) <- Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty1
; (xi2 :: Type
xi2,co2 :: 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 -> Type -> Type
mkFunTy Type
xi1 Type
xi2, Role -> TcCoercion -> TcCoercion -> TcCoercion
mkFunCo Role
role TcCoercion
co1 TcCoercion
co2) }
flatten_one ty :: Type
ty@(ForAllTy {})
= do { let (bndrs :: [TyVarBinder]
bndrs, rho :: Type
rho) = Type -> ([TyVarBinder], Type)
tcSplitForAllVarBndrs Type
ty
tvs :: [Var]
tvs = [TyVarBinder] -> [Var]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [TyVarBinder]
bndrs
; (rho' :: Type
rho', co :: 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', [Var] -> TcCoercion -> TcCoercion
mkHomoForAllCos [Var]
tvs TcCoercion
co) }
flatten_one (CastTy ty :: Type
ty g :: TcCoercion
g)
= do { (xi :: Type
xi, co :: TcCoercion
co) <- Type -> FlatM (Type, TcCoercion)
flatten_one Type
ty
; (g' :: TcCoercion
g', _) <- 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 co :: 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 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 ty1 :: Type
ty1 ty2 :: Type
ty2) tys :: [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 fun_ty :: Type
fun_ty arg_tys :: [Type]
arg_tys
= do { (fun_xi :: Type
fun_xi, fun_co :: 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 fun_xi :: Type
fun_xi fun_co :: 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 fun_xi :: Type
fun_xi fun_co :: TcCoercion
fun_co arg_tys :: [Type]
arg_tys
= do { (xi :: Type
xi, co :: TcCoercion
co, kind_co :: TcCoercion
kind_co) <- case HasCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
tcSplitTyConApp_maybe Type
fun_xi of
Just (tc :: TyCon
tc, xis :: [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
; (arg_xis :: [Type]
arg_xis, arg_cos :: [TcCoercion]
arg_cos, kind_co :: 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
NomEq -> TcCoercion -> [TcCoercion] -> TcCoercion
mkAppCos TcCoercion
fun_co [TcCoercion]
arg_cos
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) }
Nothing ->
do { (arg_xis :: [Type]
arg_xis, arg_cos :: [TcCoercion]
arg_cos, kind_co :: 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 tc :: TyCon
tc tys :: [Type]
tys
= do { Role
role <- FlatM Role
getRole
; (xis :: [Type]
xis, cos :: [TcCoercion]
cos, kind_co :: 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 xi :: Type
xi co :: TcCoercion
co r :: Role
r kind_co :: 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 ki :: Type
ki roles :: [Role]
roles tys :: [Type]
tys
= do { EqRel
eq_rel <- FlatM EqRel
getEqRel
; case EqRel
eq_rel of
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
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
(bndrs :: [TyCoBinder]
bndrs, inner_ki :: Type
inner_ki, any_named_bndrs :: 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 tc :: TyCon
tc tys :: [Type]
tys
= ASSERT2( tys `lengthAtLeast` tyConArity tc
, ppr tc $$ ppr (tyConArity tc) $$ ppr tys)
do { FlattenMode
mode <- FlatM FlattenMode
getMode
; case FlattenMode
mode of
{ FM_SubstOnly -> TyCon -> [Type] -> FlatM (Type, TcCoercion)
flatten_ty_con_app TyCon
tc [Type]
tys
; FM_FlattenAll ->
do { let (tys1 :: [Type]
tys1, tys_rest :: [Type]
tys_rest) = Int -> [Type] -> ([Type], [Type])
forall a. Int -> [a] -> ([a], [a])
splitAt (TyCon -> Int
tyConArity TyCon
tc) [Type]
tys
; (xi1 :: Type
xi1, co1 :: 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 tc :: TyCon
tc tys :: [Type]
tys
= do { let reduce_co :: TcCoercion
reduce_co = Type -> TcCoercion
mkNomReflCo (HasDebugCallStack => Type -> Type
Type -> Type
tcTypeKind (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
tys))
; Maybe (Type, TcCoercion)
mOut <- TyCon
-> [Type]
-> TcCoercion
-> (TcCoercion -> TcCoercion)
-> FlatM (Maybe (Type, TcCoercion))
try_to_reduce_nocache TyCon
tc [Type]
tys TcCoercion
reduce_co TcCoercion -> TcCoercion
forall a. a -> a
id
; case Maybe (Type, TcCoercion)
mOut of
Just out :: (Type, TcCoercion)
out -> (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Type, TcCoercion)
out
Nothing -> do
{
; (xis :: [Type]
xis, cos :: [TcCoercion]
cos, kind_co :: 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 (co :: TcCoercion
co, rhs_ty :: Type
rhs_ty, flav :: CtFlavour
flav)
| (NotSwapped, _) <- CtFlavour
flav CtFlavour -> CtFlavour -> (SwapFlag, Bool)
`funEqCanDischargeF` CtFlavour
cur_flav
->
do { String -> SDoc -> FlatM ()
traceFlat "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)
; (fsk_xi :: Type
fsk_xi, fsk_co :: 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
maybeSubCo 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')
}
_ -> 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 out :: (Type, TcCoercion)
out -> (Type, TcCoercion) -> FlatM (Type, TcCoercion)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Type, TcCoercion)
out
Nothing -> do
{ CtLoc
loc <- FlatM CtLoc
getLoc
; (ev :: CtEvidence
ev, co :: TcCoercion
co, fsk :: Var
fsk) <- TcS (CtEvidence, TcCoercion, Var)
-> FlatM (CtEvidence, TcCoercion, Var)
forall a. TcS a -> FlatM a
liftTcS (TcS (CtEvidence, TcCoercion, Var)
-> FlatM (CtEvidence, TcCoercion, Var))
-> TcS (CtEvidence, TcCoercion, Var)
-> FlatM (CtEvidence, TcCoercion, Var)
forall a b. (a -> b) -> a -> b
$
CtFlavour
-> CtLoc -> TyCon -> [Type] -> TcS (CtEvidence, TcCoercion, Var)
newFlattenSkolem CtFlavour
cur_flav CtLoc
loc TyCon
tc [Type]
xis
; let ct :: Ct
ct = CFunEqCan :: CtEvidence -> TyCon -> [Type] -> Var -> 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 :: Var
cc_fsk = Var
fsk }
; Ct -> FlatM ()
emitFlatWork Ct
ct
; String -> SDoc -> FlatM ()
traceFlat "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
$$ Var -> SDoc
forall a. Outputable a => a -> SDoc
ppr Var
fsk SDoc -> SDoc -> SDoc
$$ CtEvidence -> SDoc
forall a. Outputable a => a -> SDoc
ppr CtEvidence
ev)
; let fsk_ty :: Type
fsk_ty = Var -> Type
mkTyVarTy Var
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
maybeSubCo 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 tc :: TyCon
tc tys :: [Type]
tys kind_co :: TcCoercion
kind_co update_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 (norm_co :: TcCoercion
norm_co, norm_ty :: Type
norm_ty)
-> do { String -> SDoc -> FlatM ()
traceFlat "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)
]
; (xi :: Type
xi, final_co :: 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
maybeSubCo 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') }
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]
-> CoercionN
-> ( Coercion
-> Coercion )
-> FlatM (Maybe (Xi, Coercion))
try_to_reduce_nocache :: TyCon
-> [Type]
-> TcCoercion
-> (TcCoercion -> TcCoercion)
-> FlatM (Maybe (Type, TcCoercion))
try_to_reduce_nocache tc :: TyCon
tc tys :: [Type]
tys kind_co :: TcCoercion
kind_co update_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 (norm_co :: TcCoercion
norm_co, norm_ty :: Type
norm_ty)
-> do { (xi :: Type
xi, final_co :: 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
maybeSubCo EqRel
eq_rel TcCoercion
norm_co
TcCoercion -> TcCoercion -> TcCoercion
`mkTransCo` TcCoercion -> TcCoercion
mkSymCo TcCoercion
final_co
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') }
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 :: Var -> FlatM (Type, TcCoercion)
flattenTyVar tv :: Var
tv
= do { FlattenTvResult
mb_yes <- Var -> FlatM FlattenTvResult
flatten_tyvar1 Var
tv
; case FlattenTvResult
mb_yes of
FTRFollowed ty1 :: Type
ty1 co1 :: TcCoercion
co1
-> do { (ty2 :: Type
ty2, co2 :: 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) }
FTRNotFollowed
-> do { Var
tv' <- TcS Var -> FlatM Var
forall a. TcS a -> FlatM a
liftTcS (TcS Var -> FlatM Var) -> TcS Var -> FlatM Var
forall a b. (a -> b) -> a -> b
$ (Type -> TcS Type) -> Var -> TcS Var
forall (m :: * -> *). Monad m => (Type -> m Type) -> Var -> m Var
updateTyVarKindM Type -> TcS Type
zonkTcType Var
tv
; Role
role <- FlatM Role
getRole
; let ty' :: Type
ty' = Var -> Type
mkTyVarTy Var
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 :: Var -> FlatM FlattenTvResult
flatten_tyvar1 tv :: Var
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
$ Var -> TcS (Maybe Type)
isFilledMetaTyVar_maybe Var
tv
; case Maybe Type
mb_ty of
Just ty :: Type
ty -> do { String -> SDoc -> FlatM ()
traceFlat "Following filled tyvar"
(Var -> SDoc
forall a. Outputable a => a -> SDoc
ppr Var
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)) } ;
Nothing -> do { String -> SDoc -> FlatM ()
traceFlat "Unfilled tyvar" (Var -> SDoc
forall a. Outputable a => a -> SDoc
ppr Var
tv)
; CtFlavourRole
fr <- FlatM CtFlavourRole
getFlavourRole
; Var -> CtFlavourRole -> FlatM FlattenTvResult
flatten_tyvar2 Var
tv CtFlavourRole
fr } }
flatten_tyvar2 :: TcTyVar -> CtFlavourRole -> FlatM FlattenTvResult
flatten_tyvar2 :: Var -> CtFlavourRole -> FlatM FlattenTvResult
flatten_tyvar2 tv :: Var
tv fr :: CtFlavourRole
fr@(_, eq_rel :: 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] -> Var -> Maybe [Ct]
forall a. DVarEnv a -> Var -> Maybe a
lookupDVarEnv DTyVarEnv [Ct]
ieqs Var
tv of
Just (ct :: Ct
ct:_)
| CTyEqCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ctev, cc_tyvar :: Ct -> Var
cc_tyvar = Var
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 "Following inert tyvar"
(FlattenMode -> SDoc
forall a. Outputable a => a -> SDoc
ppr FlattenMode
mode SDoc -> SDoc -> SDoc
<+>
Var -> SDoc
forall a. Outputable a => a -> SDoc
ppr Var
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
(ReprEq, _rel :: EqRel
_rel) -> ASSERT( _rel == ReprEq )
TcCoercion
rewrite_co1
(NomEq, NomEq) -> TcCoercion
rewrite_co1
(NomEq, 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) }
_other :: 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 tv_eqs :: Cts
tv_eqs funeqs :: Cts
funeqs
= do { TcLevel
tclvl <- TcS TcLevel
getTcLevel
; String -> SDoc -> TcS ()
traceTcS "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 "Funeqs =" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
pprCts Cts
funeqs
, String -> SDoc
text "Tv eqs =" SDoc -> SDoc -> SDoc
<+> Cts -> SDoc
pprCts Cts
tv_eqs ]
; Cts
funeqs <- (Ct -> Cts -> TcS Cts) -> Cts -> Cts -> TcS Cts
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> b -> Bag a -> m b
foldrBagM Ct -> Cts -> TcS Cts
unflatten_funeq Cts
emptyCts Cts
funeqs
; String -> SDoc -> TcS ()
traceTcS "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 (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> b -> Bag a -> m b
foldrBagM (TcLevel -> Ct -> Cts -> TcS Cts
unflatten_eq TcLevel
tclvl) Cts
emptyCts Cts
tv_eqs
; String -> SDoc -> TcS ()
traceTcS "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 "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 (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> b -> Bag a -> m b
foldrBagM 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 "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 -> Var
cc_fsk = Var
fmv, cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev }) rest :: Cts
rest
= do {
Type
rhs' <- Type -> TcS Type
zonkTcType (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
xis)
; case [Var] -> Type -> Maybe Type
occCheckExpand [Var
fmv] Type
rhs' of
Just rhs'' :: Type
rhs''
-> do { CtEvidence -> EqRel -> Type -> TcS ()
setReflEvidence CtEvidence
ev EqRel
NomEq Type
rhs''
; Var -> Type -> TcS ()
unflattenFmv Var
fmv Type
rhs''
; Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return Cts
rest }
Nothing ->
Cts -> TcS Cts
forall (m :: * -> *) a. Monad m => a -> m a
return (Ct
ct Ct -> Cts -> Cts
`consCts` Cts
rest) }
unflatten_funeq other_ct :: Ct
other_ct _
= String -> SDoc -> TcS Cts
forall a. HasCallStack => String -> SDoc -> a
pprPanic "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 -> Var
cc_fsk = Var
fmv, cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev })
= do { Var -> TcS ()
demoteUnfilledFmv Var
fmv
; Ct -> TcS Ct
forall (m :: * -> *) a. Monad m => a -> m a
return (CtEvidence -> Ct
mkNonCanonical CtEvidence
ev) }
finalise_funeq ct :: Ct
ct = String -> SDoc -> TcS Ct
forall a. HasCallStack => String -> SDoc -> a
pprPanic "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 tclvl :: TcLevel
tclvl ct :: Ct
ct@(CTyEqCan { cc_ev :: Ct -> CtEvidence
cc_ev = CtEvidence
ev, cc_tyvar :: Ct -> Var
cc_tyvar = Var
tv
, cc_rhs :: Ct -> Type
cc_rhs = Type
rhs, cc_eq_rel :: Ct -> EqRel
cc_eq_rel = EqRel
eq_rel }) rest :: Cts
rest
| EqRel
NomEq <- EqRel
eq_rel
, Var -> Bool
isFmvTyVar Var
tv
= ASSERT2( tyVarKind tv `eqType` tcTypeKind rhs, ppr ct )
do { Bool
is_filled <- Var -> TcS Bool
isFilledMetaTyVar Var
tv
; Bool
elim <- case Bool
is_filled of
False -> do { String -> SDoc -> TcS ()
traceTcS "unflatten_eq 2" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)
; CtEvidence -> Var -> Type -> TcS Bool
tryFill CtEvidence
ev Var
tv Type
rhs }
True -> do { String -> SDoc -> TcS ()
traceTcS "unflatten_eq 3" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)
; CtEvidence -> TcLevel -> Var -> Type -> TcS Bool
try_fill_rhs CtEvidence
ev TcLevel
tclvl Var
tv Type
rhs }
; if Bool
elim
then do { CtEvidence -> EqRel -> Type -> TcS ()
setReflEvidence CtEvidence
ev EqRel
eq_rel (Var -> Type
mkTyVarTy Var
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 _ ct :: Ct
ct _ = String -> SDoc -> TcS Cts
forall a. HasCallStack => String -> SDoc -> a
pprPanic "unflatten_irred" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)
try_fill_rhs :: CtEvidence -> TcLevel -> Var -> Type -> TcS Bool
try_fill_rhs ev :: CtEvidence
ev tclvl :: TcLevel
tclvl lhs_tv :: Var
lhs_tv rhs :: Type
rhs
| Just (rhs_tv :: Var
rhs_tv, co :: TcCoercion
co) <- Type -> Maybe (Var, TcCoercion)
getCastedTyVar_maybe Type
rhs
, Var -> Bool
isFmvTyVar Var
rhs_tv Bool -> Bool -> Bool
|| (TcLevel -> Var -> Bool
isTouchableMetaTyVar TcLevel
tclvl Var
rhs_tv
Bool -> Bool -> Bool
&& Bool -> Bool
not (Var -> Bool
isTyVarTyVar Var
rhs_tv))
= do { Bool
is_filled <- Var -> TcS Bool
isFilledMetaTyVar Var
rhs_tv
; if Bool
is_filled then Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else CtEvidence -> Var -> Type -> TcS Bool
tryFill CtEvidence
ev Var
rhs_tv
(Var -> Type
mkTyVarTy Var
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 -> Var
cc_tyvar = Var
tv
, cc_rhs :: Ct -> Type
cc_rhs = Type
rhs, cc_eq_rel :: Ct -> EqRel
cc_eq_rel = EqRel
eq_rel }) rest :: Cts
rest
| Var -> Bool
isFmvTyVar Var
tv
= do { Type
ty1 <- Var -> TcS Type
zonkTcTyVar Var
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
ct _ = String -> SDoc -> TcS Cts
forall a. HasCallStack => String -> SDoc -> a
pprPanic "finalise_irred" (Ct -> SDoc
forall a. Outputable a => a -> SDoc
ppr Ct
ct)
tryFill :: CtEvidence -> TcTyVar -> TcType -> TcS Bool
tryFill :: CtEvidence -> Var -> Type -> TcS Bool
tryFill ev :: CtEvidence
ev tv :: Var
tv rhs :: Type
rhs
= ASSERT2( not (isGiven ev), ppr ev )
do { Type
rhs' <- Type -> TcS Type
zonkTcType Type
rhs
; case () of
_ | Just tv' :: Var
tv' <- Type -> Maybe Var
tcGetTyVar_maybe Type
rhs'
, Var
tv Var -> Var -> Bool
forall a. Eq a => a -> a -> Bool
== Var
tv'
-> Bool -> TcS Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
_ | Just rhs'' :: Type
rhs'' <- [Var] -> Type -> Maybe Type
occCheckExpand [Var
tv] Type
rhs'
-> do {
Var -> Type -> TcS ()
unifyTyVar Var
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 ev :: CtEvidence
ev eq_rel :: EqRel
eq_rel rhs :: 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' ty :: Type
ty = Type -> Type -> ([TyCoBinder], Type, Bool)
split Type
ty Type
ty
where
split :: Type -> Type -> ([TyCoBinder], Type, Bool)
split orig_ty :: Type
orig_ty ty :: Type
ty | Just ty' :: Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Type -> ([TyCoBinder], Type, Bool)
split Type
orig_ty Type
ty'
split _ (ForAllTy b :: TyVarBinder
b res :: Type
res) = let (bs :: [TyCoBinder]
bs, ty :: Type
ty, _) = 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 _ (FunTy arg :: Type
arg res :: Type
res) = let (bs :: [TyCoBinder]
bs, ty :: Type
ty, named :: Bool
named) = Type -> Type -> ([TyCoBinder], Type, Bool)
split Type
res Type
res
in (Type -> TyCoBinder
Anon Type
arg TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bs, Type
ty, Bool
named)
split orig_ty :: Type
orig_ty _ = ([], 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 tv :: Var
tv (NamedTCB vis :: ArgFlag
vis)) (bndrs :: [TyCoBinder]
bndrs, _)
= (TyVarBinder -> TyCoBinder
Named (Var -> ArgFlag -> TyVarBinder
forall var argf. var -> argf -> VarBndr var argf
Bndr Var
tv ArgFlag
vis) TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bndrs, Bool
True)
go (Bndr tv :: Var
tv AnonTCB) (bndrs :: [TyCoBinder]
bndrs, n :: Bool
n)
= (Type -> TyCoBinder
Anon (Var -> Type
tyVarKind Var
tv) TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bndrs, Bool
n)
{-# INLINE go #-}
{-# INLINE ty_con_binders_ty_binders' #-}