{-# LANGUAGE CPP, FlexibleContexts #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Type (
TyThing(..), Type, ArgFlag(..), KindOrType, PredType, ThetaType,
Var, TyVar, isTyVar, TyCoVar, TyCoBinder, TyCoVarBinder, TyVarBinder,
KnotTied,
mkTyVarTy, mkTyVarTys, getTyVar, getTyVar_maybe, repGetTyVar_maybe,
getCastedTyVar_maybe, tyVarKind, varType,
mkAppTy, mkAppTys, splitAppTy, splitAppTys, repSplitAppTys,
splitAppTy_maybe, repSplitAppTy_maybe, tcRepSplitAppTy_maybe,
mkFunTy, mkFunTys, splitFunTy, splitFunTy_maybe,
splitFunTys, funResultTy, funArgTy,
mkTyConApp, mkTyConTy,
tyConAppTyCon_maybe, tyConAppTyConPicky_maybe,
tyConAppArgs_maybe, tyConAppTyCon, tyConAppArgs,
splitTyConApp_maybe, splitTyConApp, tyConAppArgN, nextRole,
tcRepSplitTyConApp_maybe, tcRepSplitTyConApp, tcSplitTyConApp_maybe,
splitListTyConApp_maybe,
repSplitTyConApp_maybe,
mkForAllTy, mkForAllTys, mkTyCoInvForAllTys, mkSpecForAllTys,
mkVisForAllTys, mkTyCoInvForAllTy,
mkInvForAllTy, mkInvForAllTys,
splitForAllTys, splitForAllVarBndrs,
splitForAllTy_maybe, splitForAllTy,
splitForAllTy_ty_maybe, splitForAllTy_co_maybe,
splitPiTy_maybe, splitPiTy, splitPiTys,
mkTyCoPiTy, mkTyCoPiTys, mkTyConBindersPreferAnon,
mkPiTys,
mkLamType, mkLamTypes,
piResultTy, piResultTys,
applyTysX, dropForAlls,
mkNumLitTy, isNumLitTy,
mkStrLitTy, isStrLitTy,
isLitTy,
getRuntimeRep_maybe, kindRep_maybe, kindRep,
mkCastTy, mkCoercionTy, splitCastTy_maybe,
userTypeError_maybe, pprUserTypeErrorTy,
coAxNthLHS,
stripCoercionTy, splitCoercionType_maybe,
splitPiTysInvisible, splitPiTysInvisibleN,
invisibleTyBndrCount,
filterOutInvisibleTypes, filterOutInferredTypes,
partitionInvisibleTypes, partitionInvisibles,
tyConArgFlags, appTyArgFlags,
synTyConResKind,
modifyJoinResTy, setJoinResTy,
TyCoMapper(..), mapType, mapCoercion,
newTyConInstRhs,
mkFamilyTyConApp,
isDictLikeTy,
mkPrimEqPred, mkReprPrimEqPred, mkPrimEqPredRole,
equalityTyCon,
mkHeteroPrimEqPred, mkHeteroReprPrimEqPred,
mkClassPred,
isClassPred, isEqPred, isNomEqPred,
isIPPred, isIPPred_maybe, isIPTyCon, isIPClass,
isCTupleClass,
PredTree(..), EqRel(..), eqRelRole, classifyPredType,
getClassPredTys, getClassPredTys_maybe,
getEqPredTys, getEqPredTys_maybe, getEqPredRole,
predTypeEqRel,
sameVis,
mkTyCoVarBinder, mkTyCoVarBinders,
mkTyVarBinders,
mkAnonBinder,
isAnonTyCoBinder,
binderVar, binderVars, binderType, binderArgFlag,
tyCoBinderType, tyCoBinderVar_maybe,
tyBinderType,
binderRelevantType_maybe, caseBinder,
isVisibleArgFlag, isInvisibleArgFlag, isVisibleBinder,
isInvisibleBinder, isNamedBinder,
tyConBindersTyCoBinders,
funTyCon,
isTyVarTy, isFunTy, isDictTy, isPredTy, isCoercionTy,
isCoercionTy_maybe, isForAllTy,
isForAllTy_ty, isForAllTy_co,
isPiTy, isTauTy, isFamFreeTy,
isCoVarType, isEvVarType,
isValidJoinPointType,
isLiftedType_maybe, isUnliftedType, isUnboxedTupleType, isUnboxedSumType,
isAlgType, isDataFamilyAppType,
isPrimitiveType, isStrictType,
isRuntimeRepTy, isRuntimeRepVar, isRuntimeRepKindedTy,
dropRuntimeRepArgs,
getRuntimeRep,
Kind,
typeKind, tcTypeKind, isTypeLevPoly, resultIsLevPoly,
tcIsLiftedTypeKind, tcIsConstraintKind, tcReturnsConstraintKind,
liftedTypeKind,
tyCoFVsOfType, tyCoFVsBndr, tyCoFVsVarBndr, tyCoFVsVarBndrs,
tyCoVarsOfType, tyCoVarsOfTypes,
tyCoVarsOfTypeDSet,
coVarsOfType,
coVarsOfTypes,
closeOverKindsDSet, closeOverKindsFV, closeOverKindsList,
closeOverKinds,
noFreeVarsOfType,
splitVisVarsOfType, splitVisVarsOfTypes,
expandTypeSynonyms,
typeSize, occCheckExpand,
dVarSetElemsWellScoped, scopedSort, tyCoVarsOfTypeWellScoped,
tyCoVarsOfTypesWellScoped, tyCoVarsOfBindersWellScoped,
eqType, eqTypeX, eqTypes, nonDetCmpType, nonDetCmpTypes, nonDetCmpTypeX,
nonDetCmpTypesX, nonDetCmpTc,
eqVarBndrs,
seqType, seqTypes,
coreView, tcView,
tyConsOfType,
TvSubstEnv,
TCvSubst(..),
emptyTvSubstEnv, emptyTCvSubst, mkEmptyTCvSubst,
mkTCvSubst, zipTvSubst, mkTvSubstPrs,
zipTCvSubst,
notElemTCvSubst,
getTvSubstEnv, setTvSubstEnv,
zapTCvSubst, getTCvInScope, getTCvSubstRangeFVs,
extendTCvInScope, extendTCvInScopeList, extendTCvInScopeSet,
extendTCvSubst, extendCvSubst,
extendTvSubst, extendTvSubstBinderAndInScope,
extendTvSubstList, extendTvSubstAndInScope,
extendTCvSubstList,
extendTvSubstWithClone,
extendTCvSubstWithClone,
isInScope, composeTCvSubstEnv, composeTCvSubst, zipTyEnv, zipCoEnv,
isEmptyTCvSubst, unionTCvSubst,
substTy, substTys, substTyWith, substTysWith, substTheta,
substTyAddInScope,
substTyUnchecked, substTysUnchecked, substThetaUnchecked,
substTyWithUnchecked,
substCoUnchecked, substCoWithUnchecked,
substTyVarBndr, substTyVarBndrs, substTyVar, substTyVars,
substVarBndr, substVarBndrs,
cloneTyVarBndr, cloneTyVarBndrs, lookupTyVar,
pprType, pprParendType, pprPrecType,
pprTypeApp, pprTyThingCategory, pprShortTyThing,
pprTCvBndr, pprTCvBndrs, pprForAll, pprUserForAll,
pprSigmaType, pprWithExplicitKindsWhen,
pprTheta, pprThetaArrowTy, pprClassPred,
pprKind, pprParendKind, pprSourceTyCon,
PprPrec(..), topPrec, sigPrec, opPrec, funPrec, appPrec, maybeParen,
pprTyVar, pprTyVars,
pprWithTYPE,
tidyType, tidyTypes,
tidyOpenType, tidyOpenTypes,
tidyOpenKind,
tidyVarBndr, tidyVarBndrs, tidyFreeTyCoVars,
tidyOpenTyCoVar, tidyOpenTyCoVars,
tidyTyCoVarOcc,
tidyTopType,
tidyKind,
tidyTyCoVarBinder, tidyTyCoVarBinders
) where
#include "GhclibHsVersions.h"
import GhcPrelude
import BasicTypes
import Kind
import TyCoRep
import Var
import VarEnv
import VarSet
import UniqSet
import Class
import TyCon
import TysPrim
import {-# SOURCE #-} TysWiredIn ( listTyCon, typeNatKind, unitTy
, typeSymbolKind, liftedTypeKind
, constraintKind )
import PrelNames
import CoAxiom
import {-# SOURCE #-} Coercion( mkNomReflCo, mkGReflCo, mkReflCo
, mkTyConAppCo, mkAppCo, mkCoVarCo, mkAxiomRuleCo
, mkForAllCo, mkFunCo, mkAxiomInstCo, mkUnivCo
, mkSymCo, mkTransCo, mkNthCo, mkLRCo, mkInstCo
, mkKindCo, mkSubCo, mkFunCo, mkAxiomInstCo
, decomposePiCos, coercionKind, coercionType
, isReflexiveCo, seqCo )
import Util
import FV
import Outputable
import FastString
import Pair
import DynFlags ( gopt_set, GeneralFlag(Opt_PrintExplicitRuntimeReps) )
import ListSetOps
import Unique ( nonDetCmpUnique )
import Maybes ( orElse )
import Data.Maybe ( isJust )
import Control.Monad ( guard )
{-# INLINE tcView #-}
tcView :: Type -> Maybe Type
tcView :: Type -> Maybe Type
tcView (TyConApp TyCon
tc [Type]
tys) | Just ([(TyVar, Type)]
tenv, Type
rhs, [Type]
tys') <- TyCon -> [Type] -> Maybe ([(TyVar, Type)], Type, [Type])
forall tyco.
TyCon -> [tyco] -> Maybe ([(TyVar, tyco)], Type, [tyco])
expandSynTyCon_maybe TyCon
tc [Type]
tys
= Type -> Maybe Type
forall a. a -> Maybe a
Just (Type -> [Type] -> Type
mkAppTys (HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy ([(TyVar, Type)] -> TCvSubst
mkTvSubstPrs [(TyVar, Type)]
tenv) Type
rhs) [Type]
tys')
tcView Type
_ = Maybe Type
forall a. Maybe a
Nothing
{-# INLINE coreView #-}
coreView :: Type -> Maybe Type
coreView :: Type -> Maybe Type
coreView ty :: Type
ty@(TyConApp TyCon
tc [Type]
tys)
| Just ([(TyVar, Type)]
tenv, Type
rhs, [Type]
tys') <- TyCon -> [Type] -> Maybe ([(TyVar, Type)], Type, [Type])
forall tyco.
TyCon -> [tyco] -> Maybe ([(TyVar, tyco)], Type, [tyco])
expandSynTyCon_maybe TyCon
tc [Type]
tys
= Type -> Maybe Type
forall a. a -> Maybe a
Just (Type -> [Type] -> Type
mkAppTys (HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy ([(TyVar, Type)] -> TCvSubst
mkTvSubstPrs [(TyVar, Type)]
tenv) Type
rhs) [Type]
tys')
| TyCon -> Bool
isConstraintKindCon TyCon
tc
= ASSERT2( null tys, ppr ty )
Type -> Maybe Type
forall a. a -> Maybe a
Just Type
liftedTypeKind
coreView Type
_ = Maybe Type
forall a. Maybe a
Nothing
expandTypeSynonyms :: Type -> Type
expandTypeSynonyms :: Type -> Type
expandTypeSynonyms Type
ty
= TCvSubst -> Type -> Type
go (InScopeSet -> TCvSubst
mkEmptyTCvSubst InScopeSet
in_scope) Type
ty
where
in_scope :: InScopeSet
in_scope = VarSet -> InScopeSet
mkInScopeSet (Type -> VarSet
tyCoVarsOfType Type
ty)
go :: TCvSubst -> Type -> Type
go TCvSubst
subst (TyConApp TyCon
tc [Type]
tys)
| Just ([(TyVar, Type)]
tenv, Type
rhs, [Type]
tys') <- TyCon -> [Type] -> Maybe ([(TyVar, Type)], Type, [Type])
forall tyco.
TyCon -> [tyco] -> Maybe ([(TyVar, tyco)], Type, [tyco])
expandSynTyCon_maybe TyCon
tc [Type]
expanded_tys
= let subst' :: TCvSubst
subst' = InScopeSet -> TvSubstEnv -> TCvSubst
mkTvSubst InScopeSet
in_scope ([(TyVar, Type)] -> TvSubstEnv
forall a. [(TyVar, a)] -> VarEnv a
mkVarEnv [(TyVar, Type)]
tenv)
in Type -> [Type] -> Type
mkAppTys (TCvSubst -> Type -> Type
go TCvSubst
subst' Type
rhs) [Type]
tys'
| Bool
otherwise
= TyCon -> [Type] -> Type
TyConApp TyCon
tc [Type]
expanded_tys
where
expanded_tys :: [Type]
expanded_tys = ((Type -> Type) -> [Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map (TCvSubst -> Type -> Type
go TCvSubst
subst) [Type]
tys)
go TCvSubst
_ (LitTy TyLit
l) = TyLit -> Type
LitTy TyLit
l
go TCvSubst
subst (TyVarTy TyVar
tv) = TCvSubst -> TyVar -> Type
substTyVar TCvSubst
subst TyVar
tv
go TCvSubst
subst (AppTy Type
t1 Type
t2) = Type -> Type -> Type
mkAppTy (TCvSubst -> Type -> Type
go TCvSubst
subst Type
t1) (TCvSubst -> Type -> Type
go TCvSubst
subst Type
t2)
go TCvSubst
subst (FunTy Type
arg Type
res)
= Type -> Type -> Type
mkFunTy (TCvSubst -> Type -> Type
go TCvSubst
subst Type
arg) (TCvSubst -> Type -> Type
go TCvSubst
subst Type
res)
go TCvSubst
subst (ForAllTy (Bndr TyVar
tv ArgFlag
vis) Type
t)
= let (TCvSubst
subst', TyVar
tv') = (TCvSubst -> Type -> Type)
-> TCvSubst -> TyVar -> (TCvSubst, TyVar)
substVarBndrUsing TCvSubst -> Type -> Type
go TCvSubst
subst TyVar
tv in
VarBndr TyVar ArgFlag -> Type -> Type
ForAllTy (TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv' ArgFlag
vis) (TCvSubst -> Type -> Type
go TCvSubst
subst' Type
t)
go TCvSubst
subst (CastTy Type
ty KindCoercion
co) = Type -> KindCoercion -> Type
mkCastTy (TCvSubst -> Type -> Type
go TCvSubst
subst Type
ty) (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co)
go TCvSubst
subst (CoercionTy KindCoercion
co) = KindCoercion -> Type
mkCoercionTy (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co)
go_mco :: TCvSubst -> MCoercionN -> MCoercionN
go_mco TCvSubst
_ MCoercionN
MRefl = MCoercionN
MRefl
go_mco TCvSubst
subst (MCo KindCoercion
co) = KindCoercion -> MCoercionN
MCo (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co)
go_co :: TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst (Refl Type
ty)
= Type -> KindCoercion
mkNomReflCo (TCvSubst -> Type -> Type
go TCvSubst
subst Type
ty)
go_co TCvSubst
subst (GRefl Role
r Type
ty MCoercionN
mco)
= Role -> Type -> MCoercionN -> KindCoercion
mkGReflCo Role
r (TCvSubst -> Type -> Type
go TCvSubst
subst Type
ty) (TCvSubst -> MCoercionN -> MCoercionN
go_mco TCvSubst
subst MCoercionN
mco)
go_co TCvSubst
subst (TyConAppCo Role
r TyCon
tc [KindCoercion]
args)
= HasDebugCallStack =>
Role -> TyCon -> [KindCoercion] -> KindCoercion
Role -> TyCon -> [KindCoercion] -> KindCoercion
mkTyConAppCo Role
r TyCon
tc ((KindCoercion -> KindCoercion) -> [KindCoercion] -> [KindCoercion]
forall a b. (a -> b) -> [a] -> [b]
map (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst) [KindCoercion]
args)
go_co TCvSubst
subst (AppCo KindCoercion
co KindCoercion
arg)
= KindCoercion -> KindCoercion -> KindCoercion
mkAppCo (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co) (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
arg)
go_co TCvSubst
subst (ForAllCo TyVar
tv KindCoercion
kind_co KindCoercion
co)
= let (TCvSubst
subst', TyVar
tv', KindCoercion
kind_co') = TCvSubst
-> TyVar -> KindCoercion -> (TCvSubst, TyVar, KindCoercion)
go_cobndr TCvSubst
subst TyVar
tv KindCoercion
kind_co in
TyVar -> KindCoercion -> KindCoercion -> KindCoercion
mkForAllCo TyVar
tv' KindCoercion
kind_co' (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst' KindCoercion
co)
go_co TCvSubst
subst (FunCo Role
r KindCoercion
co1 KindCoercion
co2)
= Role -> KindCoercion -> KindCoercion -> KindCoercion
mkFunCo Role
r (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co1) (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co2)
go_co TCvSubst
subst (CoVarCo TyVar
cv)
= TCvSubst -> TyVar -> KindCoercion
substCoVar TCvSubst
subst TyVar
cv
go_co TCvSubst
subst (AxiomInstCo CoAxiom Branched
ax Int
ind [KindCoercion]
args)
= CoAxiom Branched -> Int -> [KindCoercion] -> KindCoercion
mkAxiomInstCo CoAxiom Branched
ax Int
ind ((KindCoercion -> KindCoercion) -> [KindCoercion] -> [KindCoercion]
forall a b. (a -> b) -> [a] -> [b]
map (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst) [KindCoercion]
args)
go_co TCvSubst
subst (UnivCo UnivCoProvenance
p Role
r Type
t1 Type
t2)
= UnivCoProvenance -> Role -> Type -> Type -> KindCoercion
mkUnivCo (TCvSubst -> UnivCoProvenance -> UnivCoProvenance
go_prov TCvSubst
subst UnivCoProvenance
p) Role
r (TCvSubst -> Type -> Type
go TCvSubst
subst Type
t1) (TCvSubst -> Type -> Type
go TCvSubst
subst Type
t2)
go_co TCvSubst
subst (SymCo KindCoercion
co)
= KindCoercion -> KindCoercion
mkSymCo (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co)
go_co TCvSubst
subst (TransCo KindCoercion
co1 KindCoercion
co2)
= KindCoercion -> KindCoercion -> KindCoercion
mkTransCo (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co1) (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co2)
go_co TCvSubst
subst (NthCo Role
r Int
n KindCoercion
co)
= HasDebugCallStack => Role -> Int -> KindCoercion -> KindCoercion
Role -> Int -> KindCoercion -> KindCoercion
mkNthCo Role
r Int
n (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co)
go_co TCvSubst
subst (LRCo LeftOrRight
lr KindCoercion
co)
= LeftOrRight -> KindCoercion -> KindCoercion
mkLRCo LeftOrRight
lr (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co)
go_co TCvSubst
subst (InstCo KindCoercion
co KindCoercion
arg)
= KindCoercion -> KindCoercion -> KindCoercion
mkInstCo (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co) (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
arg)
go_co TCvSubst
subst (KindCo KindCoercion
co)
= KindCoercion -> KindCoercion
mkKindCo (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co)
go_co TCvSubst
subst (SubCo KindCoercion
co)
= KindCoercion -> KindCoercion
mkSubCo (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co)
go_co TCvSubst
subst (AxiomRuleCo CoAxiomRule
ax [KindCoercion]
cs)
= CoAxiomRule -> [KindCoercion] -> KindCoercion
AxiomRuleCo CoAxiomRule
ax ((KindCoercion -> KindCoercion) -> [KindCoercion] -> [KindCoercion]
forall a b. (a -> b) -> [a] -> [b]
map (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst) [KindCoercion]
cs)
go_co TCvSubst
_ (HoleCo CoercionHole
h)
= String -> SDoc -> KindCoercion
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"expandTypeSynonyms hit a hole" (CoercionHole -> SDoc
forall a. Outputable a => a -> SDoc
ppr CoercionHole
h)
go_prov :: TCvSubst -> UnivCoProvenance -> UnivCoProvenance
go_prov TCvSubst
_ UnivCoProvenance
UnsafeCoerceProv = UnivCoProvenance
UnsafeCoerceProv
go_prov TCvSubst
subst (PhantomProv KindCoercion
co) = KindCoercion -> UnivCoProvenance
PhantomProv (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co)
go_prov TCvSubst
subst (ProofIrrelProv KindCoercion
co) = KindCoercion -> UnivCoProvenance
ProofIrrelProv (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst KindCoercion
co)
go_prov TCvSubst
_ p :: UnivCoProvenance
p@(PluginProv String
_) = UnivCoProvenance
p
go_cobndr :: TCvSubst
-> TyVar -> KindCoercion -> (TCvSubst, TyVar, KindCoercion)
go_cobndr TCvSubst
subst = Bool
-> (KindCoercion -> KindCoercion)
-> TCvSubst
-> TyVar
-> KindCoercion
-> (TCvSubst, TyVar, KindCoercion)
substForAllCoBndrUsing Bool
False (TCvSubst -> KindCoercion -> KindCoercion
go_co TCvSubst
subst) TCvSubst
subst
data TyCoMapper env m
= TyCoMapper
{ TyCoMapper env m -> Bool
tcm_smart :: Bool
, TyCoMapper env m -> env -> TyVar -> m Type
tcm_tyvar :: env -> TyVar -> m Type
, TyCoMapper env m -> env -> TyVar -> m KindCoercion
tcm_covar :: env -> CoVar -> m Coercion
, TyCoMapper env m -> env -> CoercionHole -> m KindCoercion
tcm_hole :: env -> CoercionHole -> m Coercion
, TyCoMapper env m -> env -> TyVar -> ArgFlag -> m (env, TyVar)
tcm_tycobinder :: env -> TyCoVar -> ArgFlag -> m (env, TyCoVar)
, TyCoMapper env m -> TyCon -> m TyCon
tcm_tycon :: TyCon -> m TyCon
}
{-# INLINABLE mapType #-}
mapType :: Monad m => TyCoMapper env m -> env -> Type -> m Type
mapType :: TyCoMapper env m -> env -> Type -> m Type
mapType mapper :: TyCoMapper env m
mapper@(TyCoMapper { tcm_smart :: forall env (m :: * -> *). TyCoMapper env m -> Bool
tcm_smart = Bool
smart, tcm_tyvar :: forall env (m :: * -> *).
TyCoMapper env m -> env -> TyVar -> m Type
tcm_tyvar = env -> TyVar -> m Type
tyvar
, tcm_tycobinder :: forall env (m :: * -> *).
TyCoMapper env m -> env -> TyVar -> ArgFlag -> m (env, TyVar)
tcm_tycobinder = env -> TyVar -> ArgFlag -> m (env, TyVar)
tycobinder, tcm_tycon :: forall env (m :: * -> *). TyCoMapper env m -> TyCon -> m TyCon
tcm_tycon = TyCon -> m TyCon
tycon })
env
env Type
ty
= Type -> m Type
go Type
ty
where
go :: Type -> m Type
go (TyVarTy TyVar
tv) = env -> TyVar -> m Type
tyvar env
env TyVar
tv
go (AppTy Type
t1 Type
t2) = Type -> Type -> Type
mkappty (Type -> Type -> Type) -> m Type -> m (Type -> Type)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Type -> m Type
go Type
t1 m (Type -> Type) -> m Type -> m Type
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Type -> m Type
go Type
t2
go t :: Type
t@(TyConApp TyCon
tc []) | Bool -> Bool
not (TyCon -> Bool
isTcTyCon TyCon
tc)
= Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
t
go (TyConApp TyCon
tc [Type]
tys)
= do { TyCon
tc' <- TyCon -> m TyCon
tycon TyCon
tc
; TyCon -> [Type] -> Type
mktyconapp TyCon
tc' ([Type] -> Type) -> m [Type] -> m Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Type -> m Type) -> [Type] -> m [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> m Type
go [Type]
tys }
go (FunTy Type
arg Type
res) = Type -> Type -> Type
FunTy (Type -> Type -> Type) -> m Type -> m (Type -> Type)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Type -> m Type
go Type
arg m (Type -> Type) -> m Type -> m Type
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Type -> m Type
go Type
res
go (ForAllTy (Bndr TyVar
tv ArgFlag
vis) Type
inner)
= do { (env
env', TyVar
tv') <- env -> TyVar -> ArgFlag -> m (env, TyVar)
tycobinder env
env TyVar
tv ArgFlag
vis
; Type
inner' <- TyCoMapper env m -> env -> Type -> m Type
forall (m :: * -> *) env.
Monad m =>
TyCoMapper env m -> env -> Type -> m Type
mapType TyCoMapper env m
mapper env
env' Type
inner
; Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> m Type) -> Type -> m Type
forall a b. (a -> b) -> a -> b
$ VarBndr TyVar ArgFlag -> Type -> Type
ForAllTy (TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv' ArgFlag
vis) Type
inner' }
go ty :: Type
ty@(LitTy {}) = Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
ty
go (CastTy Type
ty KindCoercion
co) = Type -> KindCoercion -> Type
mkcastty (Type -> KindCoercion -> Type)
-> m Type -> m (KindCoercion -> Type)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Type -> m Type
go Type
ty m (KindCoercion -> Type) -> m KindCoercion -> m Type
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TyCoMapper env m -> env -> KindCoercion -> m KindCoercion
forall (m :: * -> *) env.
Monad m =>
TyCoMapper env m -> env -> KindCoercion -> m KindCoercion
mapCoercion TyCoMapper env m
mapper env
env KindCoercion
co
go (CoercionTy KindCoercion
co) = KindCoercion -> Type
CoercionTy (KindCoercion -> Type) -> m KindCoercion -> m Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TyCoMapper env m -> env -> KindCoercion -> m KindCoercion
forall (m :: * -> *) env.
Monad m =>
TyCoMapper env m -> env -> KindCoercion -> m KindCoercion
mapCoercion TyCoMapper env m
mapper env
env KindCoercion
co
(TyCon -> [Type] -> Type
mktyconapp, Type -> Type -> Type
mkappty, Type -> KindCoercion -> Type
mkcastty)
| Bool
smart = (TyCon -> [Type] -> Type
mkTyConApp, Type -> Type -> Type
mkAppTy, Type -> KindCoercion -> Type
mkCastTy)
| Bool
otherwise = (TyCon -> [Type] -> Type
TyConApp, Type -> Type -> Type
AppTy, Type -> KindCoercion -> Type
CastTy)
{-# INLINABLE mapCoercion #-}
mapCoercion :: Monad m
=> TyCoMapper env m -> env -> Coercion -> m Coercion
mapCoercion :: TyCoMapper env m -> env -> KindCoercion -> m KindCoercion
mapCoercion mapper :: TyCoMapper env m
mapper@(TyCoMapper { tcm_smart :: forall env (m :: * -> *). TyCoMapper env m -> Bool
tcm_smart = Bool
smart, tcm_covar :: forall env (m :: * -> *).
TyCoMapper env m -> env -> TyVar -> m KindCoercion
tcm_covar = env -> TyVar -> m KindCoercion
covar
, tcm_hole :: forall env (m :: * -> *).
TyCoMapper env m -> env -> CoercionHole -> m KindCoercion
tcm_hole = env -> CoercionHole -> m KindCoercion
cohole, tcm_tycobinder :: forall env (m :: * -> *).
TyCoMapper env m -> env -> TyVar -> ArgFlag -> m (env, TyVar)
tcm_tycobinder = env -> TyVar -> ArgFlag -> m (env, TyVar)
tycobinder
, tcm_tycon :: forall env (m :: * -> *). TyCoMapper env m -> TyCon -> m TyCon
tcm_tycon = TyCon -> m TyCon
tycon })
env
env KindCoercion
co
= KindCoercion -> m KindCoercion
go KindCoercion
co
where
go_mco :: MCoercionN -> m MCoercionN
go_mco MCoercionN
MRefl = MCoercionN -> m MCoercionN
forall (m :: * -> *) a. Monad m => a -> m a
return MCoercionN
MRefl
go_mco (MCo KindCoercion
co) = KindCoercion -> MCoercionN
MCo (KindCoercion -> MCoercionN) -> m KindCoercion -> m MCoercionN
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (KindCoercion -> m KindCoercion
go KindCoercion
co)
go :: KindCoercion -> m KindCoercion
go (Refl Type
ty) = Type -> KindCoercion
Refl (Type -> KindCoercion) -> m Type -> m KindCoercion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TyCoMapper env m -> env -> Type -> m Type
forall (m :: * -> *) env.
Monad m =>
TyCoMapper env m -> env -> Type -> m Type
mapType TyCoMapper env m
mapper env
env Type
ty
go (GRefl Role
r Type
ty MCoercionN
mco) = Role -> Type -> MCoercionN -> KindCoercion
mkgreflco Role
r (Type -> MCoercionN -> KindCoercion)
-> m Type -> m (MCoercionN -> KindCoercion)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TyCoMapper env m -> env -> Type -> m Type
forall (m :: * -> *) env.
Monad m =>
TyCoMapper env m -> env -> Type -> m Type
mapType TyCoMapper env m
mapper env
env Type
ty m (MCoercionN -> KindCoercion) -> m MCoercionN -> m KindCoercion
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (MCoercionN -> m MCoercionN
go_mco MCoercionN
mco)
go (TyConAppCo Role
r TyCon
tc [KindCoercion]
args)
= do { TyCon
tc' <- TyCon -> m TyCon
tycon TyCon
tc
; Role -> TyCon -> [KindCoercion] -> KindCoercion
mktyconappco Role
r TyCon
tc' ([KindCoercion] -> KindCoercion)
-> m [KindCoercion] -> m KindCoercion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (KindCoercion -> m KindCoercion)
-> [KindCoercion] -> m [KindCoercion]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM KindCoercion -> m KindCoercion
go [KindCoercion]
args }
go (AppCo KindCoercion
c1 KindCoercion
c2) = KindCoercion -> KindCoercion -> KindCoercion
mkappco (KindCoercion -> KindCoercion -> KindCoercion)
-> m KindCoercion -> m (KindCoercion -> KindCoercion)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KindCoercion -> m KindCoercion
go KindCoercion
c1 m (KindCoercion -> KindCoercion)
-> m KindCoercion -> m KindCoercion
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KindCoercion -> m KindCoercion
go KindCoercion
c2
go (ForAllCo TyVar
tv KindCoercion
kind_co KindCoercion
co)
= do { KindCoercion
kind_co' <- KindCoercion -> m KindCoercion
go KindCoercion
kind_co
; (env
env', TyVar
tv') <- env -> TyVar -> ArgFlag -> m (env, TyVar)
tycobinder env
env TyVar
tv ArgFlag
Inferred
; KindCoercion
co' <- TyCoMapper env m -> env -> KindCoercion -> m KindCoercion
forall (m :: * -> *) env.
Monad m =>
TyCoMapper env m -> env -> KindCoercion -> m KindCoercion
mapCoercion TyCoMapper env m
mapper env
env' KindCoercion
co
; KindCoercion -> m KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (KindCoercion -> m KindCoercion) -> KindCoercion -> m KindCoercion
forall a b. (a -> b) -> a -> b
$ TyVar -> KindCoercion -> KindCoercion -> KindCoercion
mkforallco TyVar
tv' KindCoercion
kind_co' KindCoercion
co' }
go (FunCo Role
r KindCoercion
c1 KindCoercion
c2) = Role -> KindCoercion -> KindCoercion -> KindCoercion
mkFunCo Role
r (KindCoercion -> KindCoercion -> KindCoercion)
-> m KindCoercion -> m (KindCoercion -> KindCoercion)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KindCoercion -> m KindCoercion
go KindCoercion
c1 m (KindCoercion -> KindCoercion)
-> m KindCoercion -> m KindCoercion
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KindCoercion -> m KindCoercion
go KindCoercion
c2
go (CoVarCo TyVar
cv) = env -> TyVar -> m KindCoercion
covar env
env TyVar
cv
go (AxiomInstCo CoAxiom Branched
ax Int
i [KindCoercion]
args)
= CoAxiom Branched -> Int -> [KindCoercion] -> KindCoercion
mkaxiominstco CoAxiom Branched
ax Int
i ([KindCoercion] -> KindCoercion)
-> m [KindCoercion] -> m KindCoercion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (KindCoercion -> m KindCoercion)
-> [KindCoercion] -> m [KindCoercion]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM KindCoercion -> m KindCoercion
go [KindCoercion]
args
go (HoleCo CoercionHole
hole) = env -> CoercionHole -> m KindCoercion
cohole env
env CoercionHole
hole
go (UnivCo UnivCoProvenance
p Role
r Type
t1 Type
t2)
= UnivCoProvenance -> Role -> Type -> Type -> KindCoercion
mkunivco (UnivCoProvenance -> Role -> Type -> Type -> KindCoercion)
-> m UnivCoProvenance -> m (Role -> Type -> Type -> KindCoercion)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UnivCoProvenance -> m UnivCoProvenance
go_prov UnivCoProvenance
p m (Role -> Type -> Type -> KindCoercion)
-> m Role -> m (Type -> Type -> KindCoercion)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Role -> m Role
forall (f :: * -> *) a. Applicative f => a -> f a
pure Role
r
m (Type -> Type -> KindCoercion)
-> m Type -> m (Type -> KindCoercion)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TyCoMapper env m -> env -> Type -> m Type
forall (m :: * -> *) env.
Monad m =>
TyCoMapper env m -> env -> Type -> m Type
mapType TyCoMapper env m
mapper env
env Type
t1 m (Type -> KindCoercion) -> m Type -> m KindCoercion
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TyCoMapper env m -> env -> Type -> m Type
forall (m :: * -> *) env.
Monad m =>
TyCoMapper env m -> env -> Type -> m Type
mapType TyCoMapper env m
mapper env
env Type
t2
go (SymCo KindCoercion
co) = KindCoercion -> KindCoercion
mksymco (KindCoercion -> KindCoercion) -> m KindCoercion -> m KindCoercion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KindCoercion -> m KindCoercion
go KindCoercion
co
go (TransCo KindCoercion
c1 KindCoercion
c2) = KindCoercion -> KindCoercion -> KindCoercion
mktransco (KindCoercion -> KindCoercion -> KindCoercion)
-> m KindCoercion -> m (KindCoercion -> KindCoercion)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KindCoercion -> m KindCoercion
go KindCoercion
c1 m (KindCoercion -> KindCoercion)
-> m KindCoercion -> m KindCoercion
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KindCoercion -> m KindCoercion
go KindCoercion
c2
go (AxiomRuleCo CoAxiomRule
r [KindCoercion]
cos) = CoAxiomRule -> [KindCoercion] -> KindCoercion
AxiomRuleCo CoAxiomRule
r ([KindCoercion] -> KindCoercion)
-> m [KindCoercion] -> m KindCoercion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (KindCoercion -> m KindCoercion)
-> [KindCoercion] -> m [KindCoercion]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM KindCoercion -> m KindCoercion
go [KindCoercion]
cos
go (NthCo Role
r Int
i KindCoercion
co) = Role -> Int -> KindCoercion -> KindCoercion
mknthco Role
r Int
i (KindCoercion -> KindCoercion) -> m KindCoercion -> m KindCoercion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KindCoercion -> m KindCoercion
go KindCoercion
co
go (LRCo LeftOrRight
lr KindCoercion
co) = LeftOrRight -> KindCoercion -> KindCoercion
mklrco LeftOrRight
lr (KindCoercion -> KindCoercion) -> m KindCoercion -> m KindCoercion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KindCoercion -> m KindCoercion
go KindCoercion
co
go (InstCo KindCoercion
co KindCoercion
arg) = KindCoercion -> KindCoercion -> KindCoercion
mkinstco (KindCoercion -> KindCoercion -> KindCoercion)
-> m KindCoercion -> m (KindCoercion -> KindCoercion)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KindCoercion -> m KindCoercion
go KindCoercion
co m (KindCoercion -> KindCoercion)
-> m KindCoercion -> m KindCoercion
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> KindCoercion -> m KindCoercion
go KindCoercion
arg
go (KindCo KindCoercion
co) = KindCoercion -> KindCoercion
mkkindco (KindCoercion -> KindCoercion) -> m KindCoercion -> m KindCoercion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KindCoercion -> m KindCoercion
go KindCoercion
co
go (SubCo KindCoercion
co) = KindCoercion -> KindCoercion
mksubco (KindCoercion -> KindCoercion) -> m KindCoercion -> m KindCoercion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KindCoercion -> m KindCoercion
go KindCoercion
co
go_prov :: UnivCoProvenance -> m UnivCoProvenance
go_prov UnivCoProvenance
UnsafeCoerceProv = UnivCoProvenance -> m UnivCoProvenance
forall (m :: * -> *) a. Monad m => a -> m a
return UnivCoProvenance
UnsafeCoerceProv
go_prov (PhantomProv KindCoercion
co) = KindCoercion -> UnivCoProvenance
PhantomProv (KindCoercion -> UnivCoProvenance)
-> m KindCoercion -> m UnivCoProvenance
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KindCoercion -> m KindCoercion
go KindCoercion
co
go_prov (ProofIrrelProv KindCoercion
co) = KindCoercion -> UnivCoProvenance
ProofIrrelProv (KindCoercion -> UnivCoProvenance)
-> m KindCoercion -> m UnivCoProvenance
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KindCoercion -> m KindCoercion
go KindCoercion
co
go_prov p :: UnivCoProvenance
p@(PluginProv String
_) = UnivCoProvenance -> m UnivCoProvenance
forall (m :: * -> *) a. Monad m => a -> m a
return UnivCoProvenance
p
( Role -> TyCon -> [KindCoercion] -> KindCoercion
mktyconappco, KindCoercion -> KindCoercion -> KindCoercion
mkappco, CoAxiom Branched -> Int -> [KindCoercion] -> KindCoercion
mkaxiominstco, UnivCoProvenance -> Role -> Type -> Type -> KindCoercion
mkunivco
, KindCoercion -> KindCoercion
mksymco, KindCoercion -> KindCoercion -> KindCoercion
mktransco, Role -> Int -> KindCoercion -> KindCoercion
mknthco, LeftOrRight -> KindCoercion -> KindCoercion
mklrco, KindCoercion -> KindCoercion -> KindCoercion
mkinstco
, KindCoercion -> KindCoercion
mkkindco, KindCoercion -> KindCoercion
mksubco, TyVar -> KindCoercion -> KindCoercion -> KindCoercion
mkforallco, Role -> Type -> MCoercionN -> KindCoercion
mkgreflco)
| Bool
smart
= ( HasDebugCallStack =>
Role -> TyCon -> [KindCoercion] -> KindCoercion
Role -> TyCon -> [KindCoercion] -> KindCoercion
mkTyConAppCo, KindCoercion -> KindCoercion -> KindCoercion
mkAppCo, CoAxiom Branched -> Int -> [KindCoercion] -> KindCoercion
mkAxiomInstCo, UnivCoProvenance -> Role -> Type -> Type -> KindCoercion
mkUnivCo
, KindCoercion -> KindCoercion
mkSymCo, KindCoercion -> KindCoercion -> KindCoercion
mkTransCo, HasDebugCallStack => Role -> Int -> KindCoercion -> KindCoercion
Role -> Int -> KindCoercion -> KindCoercion
mkNthCo, LeftOrRight -> KindCoercion -> KindCoercion
mkLRCo, KindCoercion -> KindCoercion -> KindCoercion
mkInstCo
, KindCoercion -> KindCoercion
mkKindCo, KindCoercion -> KindCoercion
mkSubCo, TyVar -> KindCoercion -> KindCoercion -> KindCoercion
mkForAllCo, Role -> Type -> MCoercionN -> KindCoercion
mkGReflCo )
| Bool
otherwise
= ( Role -> TyCon -> [KindCoercion] -> KindCoercion
TyConAppCo, KindCoercion -> KindCoercion -> KindCoercion
AppCo, CoAxiom Branched -> Int -> [KindCoercion] -> KindCoercion
AxiomInstCo, UnivCoProvenance -> Role -> Type -> Type -> KindCoercion
UnivCo
, KindCoercion -> KindCoercion
SymCo, KindCoercion -> KindCoercion -> KindCoercion
TransCo, Role -> Int -> KindCoercion -> KindCoercion
NthCo, LeftOrRight -> KindCoercion -> KindCoercion
LRCo, KindCoercion -> KindCoercion -> KindCoercion
InstCo
, KindCoercion -> KindCoercion
KindCo, KindCoercion -> KindCoercion
SubCo, TyVar -> KindCoercion -> KindCoercion -> KindCoercion
ForAllCo, Role -> Type -> MCoercionN -> KindCoercion
GRefl )
getTyVar :: String -> Type -> TyVar
getTyVar :: String -> Type -> TyVar
getTyVar String
msg Type
ty = case Type -> Maybe TyVar
getTyVar_maybe Type
ty of
Just TyVar
tv -> TyVar
tv
Maybe TyVar
Nothing -> String -> TyVar
forall a. String -> a
panic (String
"getTyVar: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
msg)
isTyVarTy :: Type -> Bool
isTyVarTy :: Type -> Bool
isTyVarTy Type
ty = Maybe TyVar -> Bool
forall a. Maybe a -> Bool
isJust (Type -> Maybe TyVar
getTyVar_maybe Type
ty)
getTyVar_maybe :: Type -> Maybe TyVar
getTyVar_maybe :: Type -> Maybe TyVar
getTyVar_maybe Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe TyVar
getTyVar_maybe Type
ty'
| Bool
otherwise = Type -> Maybe TyVar
repGetTyVar_maybe Type
ty
getCastedTyVar_maybe :: Type -> Maybe (TyVar, CoercionN)
getCastedTyVar_maybe :: Type -> Maybe (TyVar, KindCoercion)
getCastedTyVar_maybe Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe (TyVar, KindCoercion)
getCastedTyVar_maybe Type
ty'
getCastedTyVar_maybe (CastTy (TyVarTy TyVar
tv) KindCoercion
co) = (TyVar, KindCoercion) -> Maybe (TyVar, KindCoercion)
forall a. a -> Maybe a
Just (TyVar
tv, KindCoercion
co)
getCastedTyVar_maybe (TyVarTy TyVar
tv)
= (TyVar, KindCoercion) -> Maybe (TyVar, KindCoercion)
forall a. a -> Maybe a
Just (TyVar
tv, Role -> Type -> KindCoercion
mkReflCo Role
Nominal (TyVar -> Type
tyVarKind TyVar
tv))
getCastedTyVar_maybe Type
_ = Maybe (TyVar, KindCoercion)
forall a. Maybe a
Nothing
repGetTyVar_maybe :: Type -> Maybe TyVar
repGetTyVar_maybe :: Type -> Maybe TyVar
repGetTyVar_maybe (TyVarTy TyVar
tv) = TyVar -> Maybe TyVar
forall a. a -> Maybe a
Just TyVar
tv
repGetTyVar_maybe Type
_ = Maybe TyVar
forall a. Maybe a
Nothing
mkAppTy :: Type -> Type -> Type
mkAppTy :: Type -> Type -> Type
mkAppTy (CastTy Type
fun_ty KindCoercion
co) Type
arg_ty
| ([KindCoercion
arg_co], KindCoercion
res_co) <- HasDebugCallStack =>
KindCoercion
-> Pair Type -> [Type] -> ([KindCoercion], KindCoercion)
KindCoercion
-> Pair Type -> [Type] -> ([KindCoercion], KindCoercion)
decomposePiCos KindCoercion
co (KindCoercion -> Pair Type
coercionKind KindCoercion
co) [Type
arg_ty]
= (Type
fun_ty Type -> Type -> Type
`mkAppTy` (Type
arg_ty Type -> KindCoercion -> Type
`mkCastTy` KindCoercion
arg_co)) Type -> KindCoercion -> Type
`mkCastTy` KindCoercion
res_co
mkAppTy (TyConApp TyCon
tc [Type]
tys) Type
ty2 = TyCon -> [Type] -> Type
mkTyConApp TyCon
tc ([Type]
tys [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type
ty2])
mkAppTy Type
ty1 Type
ty2 = Type -> Type -> Type
AppTy Type
ty1 Type
ty2
mkAppTys :: Type -> [Type] -> Type
mkAppTys :: Type -> [Type] -> Type
mkAppTys Type
ty1 [] = Type
ty1
mkAppTys (CastTy Type
fun_ty KindCoercion
co) [Type]
arg_tys
= (Type -> Type -> Type) -> Type -> [Type] -> Type
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Type -> Type -> Type
AppTy ((Type -> [Type] -> Type
mkAppTys Type
fun_ty [Type]
casted_arg_tys) Type -> KindCoercion -> Type
`mkCastTy` KindCoercion
res_co) [Type]
leftovers
where
([KindCoercion]
arg_cos, KindCoercion
res_co) = HasDebugCallStack =>
KindCoercion
-> Pair Type -> [Type] -> ([KindCoercion], KindCoercion)
KindCoercion
-> Pair Type -> [Type] -> ([KindCoercion], KindCoercion)
decomposePiCos KindCoercion
co (KindCoercion -> Pair Type
coercionKind KindCoercion
co) [Type]
arg_tys
([Type]
args_to_cast, [Type]
leftovers) = [KindCoercion] -> [Type] -> ([Type], [Type])
forall b a. [b] -> [a] -> ([a], [a])
splitAtList [KindCoercion]
arg_cos [Type]
arg_tys
casted_arg_tys :: [Type]
casted_arg_tys = (Type -> KindCoercion -> Type)
-> [Type] -> [KindCoercion] -> [Type]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Type -> KindCoercion -> Type
mkCastTy [Type]
args_to_cast [KindCoercion]
arg_cos
mkAppTys (TyConApp TyCon
tc [Type]
tys1) [Type]
tys2 = TyCon -> [Type] -> Type
mkTyConApp TyCon
tc ([Type]
tys1 [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
tys2)
mkAppTys Type
ty1 [Type]
tys2 = (Type -> Type -> Type) -> Type -> [Type] -> Type
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Type -> Type -> Type
AppTy Type
ty1 [Type]
tys2
splitAppTy_maybe :: Type -> Maybe (Type, Type)
splitAppTy_maybe :: Type -> Maybe (Type, Type)
splitAppTy_maybe Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty
= Type -> Maybe (Type, Type)
splitAppTy_maybe Type
ty'
splitAppTy_maybe Type
ty = HasDebugCallStack => Type -> Maybe (Type, Type)
Type -> Maybe (Type, Type)
repSplitAppTy_maybe Type
ty
repSplitAppTy_maybe :: HasDebugCallStack => Type -> Maybe (Type,Type)
repSplitAppTy_maybe :: Type -> Maybe (Type, Type)
repSplitAppTy_maybe (FunTy Type
ty1 Type
ty2)
= (Type, Type) -> Maybe (Type, Type)
forall a. a -> Maybe a
Just (TyCon -> [Type] -> Type
TyConApp TyCon
funTyCon [Type
rep1, Type
rep2, Type
ty1], Type
ty2)
where
rep1 :: Type
rep1 = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
ty1
rep2 :: Type
rep2 = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
ty2
repSplitAppTy_maybe (AppTy Type
ty1 Type
ty2)
= (Type, Type) -> Maybe (Type, Type)
forall a. a -> Maybe a
Just (Type
ty1, Type
ty2)
repSplitAppTy_maybe (TyConApp TyCon
tc [Type]
tys)
| TyCon -> Bool
mightBeUnsaturatedTyCon TyCon
tc Bool -> Bool -> Bool
|| [Type]
tys [Type] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthExceeds` TyCon -> Int
tyConArity TyCon
tc
, Just ([Type]
tys', Type
ty') <- [Type] -> Maybe ([Type], Type)
forall a. [a] -> Maybe ([a], a)
snocView [Type]
tys
= (Type, Type) -> Maybe (Type, Type)
forall a. a -> Maybe a
Just (TyCon -> [Type] -> Type
TyConApp TyCon
tc [Type]
tys', Type
ty')
repSplitAppTy_maybe Type
_other = Maybe (Type, Type)
forall a. Maybe a
Nothing
tcRepSplitAppTy_maybe :: Type -> Maybe (Type,Type)
tcRepSplitAppTy_maybe :: Type -> Maybe (Type, Type)
tcRepSplitAppTy_maybe (FunTy Type
ty1 Type
ty2)
| Type -> Bool
isPredTy Type
ty1
= Maybe (Type, Type)
forall a. Maybe a
Nothing
| Bool
otherwise
= (Type, Type) -> Maybe (Type, Type)
forall a. a -> Maybe a
Just (TyCon -> [Type] -> Type
TyConApp TyCon
funTyCon [Type
rep1, Type
rep2, Type
ty1], Type
ty2)
where
rep1 :: Type
rep1 = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
ty1
rep2 :: Type
rep2 = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
ty2
tcRepSplitAppTy_maybe (AppTy Type
ty1 Type
ty2) = (Type, Type) -> Maybe (Type, Type)
forall a. a -> Maybe a
Just (Type
ty1, Type
ty2)
tcRepSplitAppTy_maybe (TyConApp TyCon
tc [Type]
tys)
| TyCon -> Bool
mightBeUnsaturatedTyCon TyCon
tc Bool -> Bool -> Bool
|| [Type]
tys [Type] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthExceeds` TyCon -> Int
tyConArity TyCon
tc
, Just ([Type]
tys', Type
ty') <- [Type] -> Maybe ([Type], Type)
forall a. [a] -> Maybe ([a], a)
snocView [Type]
tys
= (Type, Type) -> Maybe (Type, Type)
forall a. a -> Maybe a
Just (TyCon -> [Type] -> Type
TyConApp TyCon
tc [Type]
tys', Type
ty')
tcRepSplitAppTy_maybe Type
_other = Maybe (Type, Type)
forall a. Maybe a
Nothing
tcRepSplitTyConApp_maybe :: HasCallStack => Type -> Maybe (TyCon, [Type])
tcRepSplitTyConApp_maybe :: Type -> Maybe (TyCon, [Type])
tcRepSplitTyConApp_maybe (TyConApp TyCon
tc [Type]
tys)
= (TyCon, [Type]) -> Maybe (TyCon, [Type])
forall a. a -> Maybe a
Just (TyCon
tc, [Type]
tys)
tcRepSplitTyConApp_maybe (FunTy Type
arg Type
res)
= (TyCon, [Type]) -> Maybe (TyCon, [Type])
forall a. a -> Maybe a
Just (TyCon
funTyCon, [Type
arg_rep, Type
res_rep, Type
arg, Type
res])
where
arg_rep :: Type
arg_rep = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
arg
res_rep :: Type
res_rep = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
res
tcRepSplitTyConApp_maybe Type
_
= Maybe (TyCon, [Type])
forall a. Maybe a
Nothing
tcRepSplitTyConApp :: HasCallStack => Type -> (TyCon, [Type])
tcRepSplitTyConApp :: Type -> (TyCon, [Type])
tcRepSplitTyConApp Type
ty =
case HasCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
tcRepSplitTyConApp_maybe Type
ty of
Just (TyCon, [Type])
stuff -> (TyCon, [Type])
stuff
Maybe (TyCon, [Type])
Nothing -> String -> SDoc -> (TyCon, [Type])
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tcRepSplitTyConApp" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
splitAppTy :: Type -> (Type, Type)
splitAppTy :: Type -> (Type, Type)
splitAppTy Type
ty = case Type -> Maybe (Type, Type)
splitAppTy_maybe Type
ty of
Just (Type, Type)
pr -> (Type, Type)
pr
Maybe (Type, Type)
Nothing -> String -> (Type, Type)
forall a. String -> a
panic String
"splitAppTy"
splitAppTys :: Type -> (Type, [Type])
splitAppTys :: Type -> (Type, [Type])
splitAppTys Type
ty = Type -> Type -> [Type] -> (Type, [Type])
split Type
ty Type
ty []
where
split :: Type -> Type -> [Type] -> (Type, [Type])
split Type
orig_ty Type
ty [Type]
args | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Type -> [Type] -> (Type, [Type])
split Type
orig_ty Type
ty' [Type]
args
split Type
_ (AppTy Type
ty Type
arg) [Type]
args = Type -> Type -> [Type] -> (Type, [Type])
split Type
ty Type
ty (Type
argType -> [Type] -> [Type]
forall a. a -> [a] -> [a]
:[Type]
args)
split Type
_ (TyConApp TyCon
tc [Type]
tc_args) [Type]
args
= let
n :: Int
n | TyCon -> Bool
mightBeUnsaturatedTyCon TyCon
tc = Int
0
| Bool
otherwise = TyCon -> Int
tyConArity TyCon
tc
([Type]
tc_args1, [Type]
tc_args2) = Int -> [Type] -> ([Type], [Type])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
n [Type]
tc_args
in
(TyCon -> [Type] -> Type
TyConApp TyCon
tc [Type]
tc_args1, [Type]
tc_args2 [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
args)
split Type
_ (FunTy Type
ty1 Type
ty2) [Type]
args
= ASSERT( null args )
(TyCon -> [Type] -> Type
TyConApp TyCon
funTyCon [], [Type
rep1, Type
rep2, Type
ty1, Type
ty2])
where
rep1 :: Type
rep1 = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
ty1
rep2 :: Type
rep2 = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
ty2
split Type
orig_ty Type
_ [Type]
args = (Type
orig_ty, [Type]
args)
repSplitAppTys :: HasDebugCallStack => Type -> (Type, [Type])
repSplitAppTys :: Type -> (Type, [Type])
repSplitAppTys Type
ty = Type -> [Type] -> (Type, [Type])
split Type
ty []
where
split :: Type -> [Type] -> (Type, [Type])
split (AppTy Type
ty Type
arg) [Type]
args = Type -> [Type] -> (Type, [Type])
split Type
ty (Type
argType -> [Type] -> [Type]
forall a. a -> [a] -> [a]
:[Type]
args)
split (TyConApp TyCon
tc [Type]
tc_args) [Type]
args
= let n :: Int
n | TyCon -> Bool
mightBeUnsaturatedTyCon TyCon
tc = Int
0
| Bool
otherwise = TyCon -> Int
tyConArity TyCon
tc
([Type]
tc_args1, [Type]
tc_args2) = Int -> [Type] -> ([Type], [Type])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
n [Type]
tc_args
in
(TyCon -> [Type] -> Type
TyConApp TyCon
tc [Type]
tc_args1, [Type]
tc_args2 [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
args)
split (FunTy Type
ty1 Type
ty2) [Type]
args
= ASSERT( null args )
(TyCon -> [Type] -> Type
TyConApp TyCon
funTyCon [], [Type
rep1, Type
rep2, Type
ty1, Type
ty2])
where
rep1 :: Type
rep1 = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
ty1
rep2 :: Type
rep2 = HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
ty2
split Type
ty [Type]
args = (Type
ty, [Type]
args)
mkNumLitTy :: Integer -> Type
mkNumLitTy :: Integer -> Type
mkNumLitTy Integer
n = TyLit -> Type
LitTy (Integer -> TyLit
NumTyLit Integer
n)
isNumLitTy :: Type -> Maybe Integer
isNumLitTy :: Type -> Maybe Integer
isNumLitTy Type
ty | Just Type
ty1 <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe Integer
isNumLitTy Type
ty1
isNumLitTy (LitTy (NumTyLit Integer
n)) = Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
n
isNumLitTy Type
_ = Maybe Integer
forall a. Maybe a
Nothing
mkStrLitTy :: FastString -> Type
mkStrLitTy :: FastString -> Type
mkStrLitTy FastString
s = TyLit -> Type
LitTy (FastString -> TyLit
StrTyLit FastString
s)
isStrLitTy :: Type -> Maybe FastString
isStrLitTy :: Type -> Maybe FastString
isStrLitTy Type
ty | Just Type
ty1 <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe FastString
isStrLitTy Type
ty1
isStrLitTy (LitTy (StrTyLit FastString
s)) = FastString -> Maybe FastString
forall a. a -> Maybe a
Just FastString
s
isStrLitTy Type
_ = Maybe FastString
forall a. Maybe a
Nothing
isLitTy :: Type -> Maybe TyLit
isLitTy :: Type -> Maybe TyLit
isLitTy Type
ty | Just Type
ty1 <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe TyLit
isLitTy Type
ty1
isLitTy (LitTy TyLit
l) = TyLit -> Maybe TyLit
forall a. a -> Maybe a
Just TyLit
l
isLitTy Type
_ = Maybe TyLit
forall a. Maybe a
Nothing
userTypeError_maybe :: Type -> Maybe Type
userTypeError_maybe :: Type -> Maybe Type
userTypeError_maybe Type
t
= do { (TyCon
tc, Type
_kind : Type
msg : [Type]
_) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
t
; Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (TyCon -> Name
tyConName TyCon
tc Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
errorMessageTypeErrorFamName)
; Type -> Maybe Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
msg }
pprUserTypeErrorTy :: Type -> SDoc
pprUserTypeErrorTy :: Type -> SDoc
pprUserTypeErrorTy Type
ty =
case HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty of
Just (TyCon
tc,[Type
txt])
| TyCon -> Name
tyConName TyCon
tc Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
typeErrorTextDataConName
, Just FastString
str <- Type -> Maybe FastString
isStrLitTy Type
txt -> FastString -> SDoc
ftext FastString
str
Just (TyCon
tc,[Type
_k,Type
t])
| TyCon -> Name
tyConName TyCon
tc Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
typeErrorShowTypeDataConName -> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
t
Just (TyCon
tc,[Type
t1,Type
t2])
| TyCon -> Name
tyConName TyCon
tc Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
typeErrorAppendDataConName ->
Type -> SDoc
pprUserTypeErrorTy Type
t1 SDoc -> SDoc -> SDoc
<> Type -> SDoc
pprUserTypeErrorTy Type
t2
Just (TyCon
tc,[Type
t1,Type
t2])
| TyCon -> Name
tyConName TyCon
tc Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
typeErrorVAppendDataConName ->
Type -> SDoc
pprUserTypeErrorTy Type
t1 SDoc -> SDoc -> SDoc
$$ Type -> SDoc
pprUserTypeErrorTy Type
t2
Maybe (TyCon, [Type])
_ -> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty
isFunTy :: Type -> Bool
isFunTy :: Type -> Bool
isFunTy Type
ty = Maybe (Type, Type) -> Bool
forall a. Maybe a -> Bool
isJust (Type -> Maybe (Type, Type)
splitFunTy_maybe Type
ty)
splitFunTy :: Type -> (Type, Type)
splitFunTy :: Type -> (Type, Type)
splitFunTy Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> (Type, Type)
splitFunTy Type
ty'
splitFunTy (FunTy Type
arg Type
res) = (Type
arg, Type
res)
splitFunTy Type
other = String -> SDoc -> (Type, Type)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"splitFunTy" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
other)
splitFunTy_maybe :: Type -> Maybe (Type, Type)
splitFunTy_maybe :: Type -> Maybe (Type, Type)
splitFunTy_maybe Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe (Type, Type)
splitFunTy_maybe Type
ty'
splitFunTy_maybe (FunTy Type
arg Type
res) = (Type, Type) -> Maybe (Type, Type)
forall a. a -> Maybe a
Just (Type
arg, Type
res)
splitFunTy_maybe Type
_ = Maybe (Type, Type)
forall a. Maybe a
Nothing
splitFunTys :: Type -> ([Type], Type)
splitFunTys :: Type -> ([Type], Type)
splitFunTys Type
ty = [Type] -> Type -> Type -> ([Type], Type)
split [] Type
ty Type
ty
where
split :: [Type] -> Type -> Type -> ([Type], Type)
split [Type]
args Type
orig_ty Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = [Type] -> Type -> Type -> ([Type], Type)
split [Type]
args Type
orig_ty Type
ty'
split [Type]
args Type
_ (FunTy Type
arg Type
res) = [Type] -> Type -> Type -> ([Type], Type)
split (Type
argType -> [Type] -> [Type]
forall a. a -> [a] -> [a]
:[Type]
args) Type
res Type
res
split [Type]
args Type
orig_ty Type
_ = ([Type] -> [Type]
forall a. [a] -> [a]
reverse [Type]
args, Type
orig_ty)
funResultTy :: Type -> Type
funResultTy :: Type -> Type
funResultTy Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Type
funResultTy Type
ty'
funResultTy (FunTy Type
_ Type
res) = Type
res
funResultTy Type
ty = String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"funResultTy" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
funArgTy :: Type -> Type
funArgTy :: Type -> Type
funArgTy Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Type
funArgTy Type
ty'
funArgTy (FunTy Type
arg Type
_res) = Type
arg
funArgTy Type
ty = String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"funArgTy" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
piResultTy :: HasDebugCallStack => Type -> Type -> Type
piResultTy :: Type -> Type -> Type
piResultTy Type
ty Type
arg = case Type -> Type -> Maybe Type
piResultTy_maybe Type
ty Type
arg of
Just Type
res -> Type
res
Maybe Type
Nothing -> String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"piResultTy" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty SDoc -> SDoc -> SDoc
$$ Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
arg)
piResultTy_maybe :: Type -> Type -> Maybe Type
piResultTy_maybe :: Type -> Type -> Maybe Type
piResultTy_maybe Type
ty Type
arg
| Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Type -> Maybe Type
piResultTy_maybe Type
ty' Type
arg
| FunTy Type
_ Type
res <- Type
ty
= Type -> Maybe Type
forall a. a -> Maybe a
Just Type
res
| ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
res <- Type
ty
= let empty_subst :: TCvSubst
empty_subst = InScopeSet -> TCvSubst
mkEmptyTCvSubst (InScopeSet -> TCvSubst) -> InScopeSet -> TCvSubst
forall a b. (a -> b) -> a -> b
$ VarSet -> InScopeSet
mkInScopeSet (VarSet -> InScopeSet) -> VarSet -> InScopeSet
forall a b. (a -> b) -> a -> b
$
[Type] -> VarSet
tyCoVarsOfTypes [Type
arg,Type
res]
in Type -> Maybe Type
forall a. a -> Maybe a
Just (HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy (TCvSubst -> TyVar -> Type -> TCvSubst
extendTCvSubst TCvSubst
empty_subst TyVar
tv Type
arg) Type
res)
| Bool
otherwise
= Maybe Type
forall a. Maybe a
Nothing
piResultTys :: HasDebugCallStack => Type -> [Type] -> Type
piResultTys :: Type -> [Type] -> Type
piResultTys Type
ty [] = Type
ty
piResultTys Type
ty orig_args :: [Type]
orig_args@(Type
arg:[Type]
args)
| Just Type
ty' <- Type -> Maybe Type
coreView Type
ty
= HasDebugCallStack => Type -> [Type] -> Type
Type -> [Type] -> Type
piResultTys Type
ty' [Type]
orig_args
| FunTy Type
_ Type
res <- Type
ty
= HasDebugCallStack => Type -> [Type] -> Type
Type -> [Type] -> Type
piResultTys Type
res [Type]
args
| ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
res <- Type
ty
= TCvSubst -> Type -> [Type] -> Type
go (TCvSubst -> TyVar -> Type -> TCvSubst
extendTCvSubst TCvSubst
init_subst TyVar
tv Type
arg) Type
res [Type]
args
| Bool
otherwise
= String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"piResultTys1" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty SDoc -> SDoc -> SDoc
$$ [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
orig_args)
where
init_subst :: TCvSubst
init_subst = InScopeSet -> TCvSubst
mkEmptyTCvSubst (InScopeSet -> TCvSubst) -> InScopeSet -> TCvSubst
forall a b. (a -> b) -> a -> b
$ VarSet -> InScopeSet
mkInScopeSet ([Type] -> VarSet
tyCoVarsOfTypes (Type
tyType -> [Type] -> [Type]
forall a. a -> [a] -> [a]
:[Type]
orig_args))
go :: TCvSubst -> Type -> [Type] -> Type
go :: TCvSubst -> Type -> [Type] -> Type
go TCvSubst
subst Type
ty [] = HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy TCvSubst
subst Type
ty
go TCvSubst
subst Type
ty all_args :: [Type]
all_args@(Type
arg:[Type]
args)
| Just Type
ty' <- Type -> Maybe Type
coreView Type
ty
= TCvSubst -> Type -> [Type] -> Type
go TCvSubst
subst Type
ty' [Type]
all_args
| FunTy Type
_ Type
res <- Type
ty
= TCvSubst -> Type -> [Type] -> Type
go TCvSubst
subst Type
res [Type]
args
| ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
res <- Type
ty
= TCvSubst -> Type -> [Type] -> Type
go (TCvSubst -> TyVar -> Type -> TCvSubst
extendTCvSubst TCvSubst
subst TyVar
tv Type
arg) Type
res [Type]
args
| Bool -> Bool
not (TCvSubst -> Bool
isEmptyTCvSubst TCvSubst
subst)
= TCvSubst -> Type -> [Type] -> Type
go TCvSubst
init_subst
(HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy TCvSubst
subst Type
ty)
[Type]
all_args
| Bool
otherwise
=
String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"piResultTys2" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty SDoc -> SDoc -> SDoc
$$ [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
orig_args SDoc -> SDoc -> SDoc
$$ [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
all_args)
applyTysX :: [TyVar] -> Type -> [Type] -> Type
applyTysX :: [TyVar] -> Type -> [Type] -> Type
applyTysX [TyVar]
tvs Type
body_ty [Type]
arg_tys
= ASSERT2( arg_tys `lengthAtLeast` n_tvs, pp_stuff )
ASSERT2( tyCoVarsOfType body_ty `subVarSet` mkVarSet tvs, pp_stuff )
Type -> [Type] -> Type
mkAppTys (HasCallStack => [TyVar] -> [Type] -> Type -> Type
[TyVar] -> [Type] -> Type -> Type
substTyWith [TyVar]
tvs (Int -> [Type] -> [Type]
forall a. Int -> [a] -> [a]
take Int
n_tvs [Type]
arg_tys) Type
body_ty)
(Int -> [Type] -> [Type]
forall a. Int -> [a] -> [a]
drop Int
n_tvs [Type]
arg_tys)
where
pp_stuff :: SDoc
pp_stuff = [SDoc] -> SDoc
vcat [[TyVar] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TyVar]
tvs, Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
body_ty, [Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type]
arg_tys]
n_tvs :: Int
n_tvs = [TyVar] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TyVar]
tvs
mkTyConApp :: TyCon -> [Type] -> Type
mkTyConApp :: TyCon -> [Type] -> Type
mkTyConApp TyCon
tycon [Type]
tys
| TyCon -> Bool
isFunTyCon TyCon
tycon
, [Type
_rep1,Type
_rep2,Type
ty1,Type
ty2] <- [Type]
tys
= Type -> Type -> Type
FunTy Type
ty1 Type
ty2
| Bool
otherwise
= TyCon -> [Type] -> Type
TyConApp TyCon
tycon [Type]
tys
tyConAppTyConPicky_maybe :: Type -> Maybe TyCon
tyConAppTyConPicky_maybe :: Type -> Maybe TyCon
tyConAppTyConPicky_maybe (TyConApp TyCon
tc [Type]
_) = TyCon -> Maybe TyCon
forall a. a -> Maybe a
Just TyCon
tc
tyConAppTyConPicky_maybe (FunTy {}) = TyCon -> Maybe TyCon
forall a. a -> Maybe a
Just TyCon
funTyCon
tyConAppTyConPicky_maybe Type
_ = Maybe TyCon
forall a. Maybe a
Nothing
tyConAppTyCon_maybe :: Type -> Maybe TyCon
tyConAppTyCon_maybe :: Type -> Maybe TyCon
tyConAppTyCon_maybe Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe TyCon
tyConAppTyCon_maybe Type
ty'
tyConAppTyCon_maybe (TyConApp TyCon
tc [Type]
_) = TyCon -> Maybe TyCon
forall a. a -> Maybe a
Just TyCon
tc
tyConAppTyCon_maybe (FunTy {}) = TyCon -> Maybe TyCon
forall a. a -> Maybe a
Just TyCon
funTyCon
tyConAppTyCon_maybe Type
_ = Maybe TyCon
forall a. Maybe a
Nothing
tyConAppTyCon :: Type -> TyCon
tyConAppTyCon :: Type -> TyCon
tyConAppTyCon Type
ty = Type -> Maybe TyCon
tyConAppTyCon_maybe Type
ty Maybe TyCon -> TyCon -> TyCon
forall a. Maybe a -> a -> a
`orElse` String -> SDoc -> TyCon
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tyConAppTyCon" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
tyConAppArgs_maybe :: Type -> Maybe [Type]
tyConAppArgs_maybe :: Type -> Maybe [Type]
tyConAppArgs_maybe Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe [Type]
tyConAppArgs_maybe Type
ty'
tyConAppArgs_maybe (TyConApp TyCon
_ [Type]
tys) = [Type] -> Maybe [Type]
forall a. a -> Maybe a
Just [Type]
tys
tyConAppArgs_maybe (FunTy Type
arg Type
res)
| Just Type
rep1 <- HasDebugCallStack => Type -> Maybe Type
Type -> Maybe Type
getRuntimeRep_maybe Type
arg
, Just Type
rep2 <- HasDebugCallStack => Type -> Maybe Type
Type -> Maybe Type
getRuntimeRep_maybe Type
res
= [Type] -> Maybe [Type]
forall a. a -> Maybe a
Just [Type
rep1, Type
rep2, Type
arg, Type
res]
tyConAppArgs_maybe Type
_ = Maybe [Type]
forall a. Maybe a
Nothing
tyConAppArgs :: Type -> [Type]
tyConAppArgs :: Type -> [Type]
tyConAppArgs Type
ty = Type -> Maybe [Type]
tyConAppArgs_maybe Type
ty Maybe [Type] -> [Type] -> [Type]
forall a. Maybe a -> a -> a
`orElse` String -> SDoc -> [Type]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tyConAppArgs" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
tyConAppArgN :: Int -> Type -> Type
tyConAppArgN :: Int -> Type -> Type
tyConAppArgN Int
n Type
ty
= case Type -> Maybe [Type]
tyConAppArgs_maybe Type
ty of
Just [Type]
tys -> ASSERT2( tys `lengthExceeds` n, ppr n <+> ppr tys ) tys `getNth` n
Maybe [Type]
Nothing -> String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tyConAppArgN" (Int -> SDoc
forall a. Outputable a => a -> SDoc
ppr Int
n SDoc -> SDoc -> SDoc
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
splitTyConApp :: Type -> (TyCon, [Type])
splitTyConApp :: Type -> (TyCon, [Type])
splitTyConApp Type
ty = case HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty of
Just (TyCon, [Type])
stuff -> (TyCon, [Type])
stuff
Maybe (TyCon, [Type])
Nothing -> String -> SDoc -> (TyCon, [Type])
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"splitTyConApp" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
splitTyConApp_maybe :: HasDebugCallStack => Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe :: Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty'
splitTyConApp_maybe Type
ty = HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
repSplitTyConApp_maybe Type
ty
repSplitTyConApp_maybe :: HasDebugCallStack => Type -> Maybe (TyCon, [Type])
repSplitTyConApp_maybe :: Type -> Maybe (TyCon, [Type])
repSplitTyConApp_maybe (TyConApp TyCon
tc [Type]
tys) = (TyCon, [Type]) -> Maybe (TyCon, [Type])
forall a. a -> Maybe a
Just (TyCon
tc, [Type]
tys)
repSplitTyConApp_maybe (FunTy Type
arg Type
res)
| Just Type
arg_rep <- HasDebugCallStack => Type -> Maybe Type
Type -> Maybe Type
getRuntimeRep_maybe Type
arg
, Just Type
res_rep <- HasDebugCallStack => Type -> Maybe Type
Type -> Maybe Type
getRuntimeRep_maybe Type
res
= (TyCon, [Type]) -> Maybe (TyCon, [Type])
forall a. a -> Maybe a
Just (TyCon
funTyCon, [Type
arg_rep, Type
res_rep, Type
arg, Type
res])
repSplitTyConApp_maybe Type
_ = Maybe (TyCon, [Type])
forall a. Maybe a
Nothing
splitListTyConApp_maybe :: Type -> Maybe Type
splitListTyConApp_maybe :: Type -> Maybe Type
splitListTyConApp_maybe Type
ty = case HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty of
Just (TyCon
tc,[Type
e]) | TyCon
tc TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon
listTyCon -> Type -> Maybe Type
forall a. a -> Maybe a
Just Type
e
Maybe (TyCon, [Type])
_other -> Maybe Type
forall a. Maybe a
Nothing
nextRole :: Type -> Role
nextRole :: Type -> Role
nextRole Type
ty
| Just (TyCon
tc, [Type]
tys) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty
, let num_tys :: Int
num_tys = [Type] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Type]
tys
, Int
num_tys Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< TyCon -> Int
tyConArity TyCon
tc
= TyCon -> [Role]
tyConRoles TyCon
tc [Role] -> Int -> Role
forall a. Outputable a => [a] -> Int -> a
`getNth` Int
num_tys
| Bool
otherwise
= Role
Nominal
newTyConInstRhs :: TyCon -> [Type] -> Type
newTyConInstRhs :: TyCon -> [Type] -> Type
newTyConInstRhs TyCon
tycon [Type]
tys
= ASSERT2( tvs `leLength` tys, ppr tycon $$ ppr tys $$ ppr tvs )
[TyVar] -> Type -> [Type] -> Type
applyTysX [TyVar]
tvs Type
rhs [Type]
tys
where
([TyVar]
tvs, Type
rhs) = TyCon -> ([TyVar], Type)
newTyConEtadRhs TyCon
tycon
splitCastTy_maybe :: Type -> Maybe (Type, Coercion)
splitCastTy_maybe :: Type -> Maybe (Type, KindCoercion)
splitCastTy_maybe Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe (Type, KindCoercion)
splitCastTy_maybe Type
ty'
splitCastTy_maybe (CastTy Type
ty KindCoercion
co) = (Type, KindCoercion) -> Maybe (Type, KindCoercion)
forall a. a -> Maybe a
Just (Type
ty, KindCoercion
co)
splitCastTy_maybe Type
_ = Maybe (Type, KindCoercion)
forall a. Maybe a
Nothing
mkCastTy :: Type -> Coercion -> Type
mkCastTy :: Type -> KindCoercion -> Type
mkCastTy Type
ty KindCoercion
co | KindCoercion -> Bool
isReflexiveCo KindCoercion
co = Type
ty
mkCastTy (CastTy Type
ty KindCoercion
co1) KindCoercion
co2
= Type -> KindCoercion -> Type
mkCastTy Type
ty (KindCoercion
co1 KindCoercion -> KindCoercion -> KindCoercion
`mkTransCo` KindCoercion
co2)
mkCastTy (ForAllTy (Bndr TyVar
tv ArgFlag
vis) Type
inner_ty) KindCoercion
co
| TyVar -> Bool
isTyVar TyVar
tv
, let fvs :: VarSet
fvs = KindCoercion -> VarSet
tyCoVarsOfCo KindCoercion
co
=
if TyVar
tv TyVar -> VarSet -> Bool
`elemVarSet` VarSet
fvs
then let empty_subst :: TCvSubst
empty_subst = InScopeSet -> TCvSubst
mkEmptyTCvSubst (VarSet -> InScopeSet
mkInScopeSet VarSet
fvs)
(TCvSubst
subst, TyVar
tv') = HasCallStack => TCvSubst -> TyVar -> (TCvSubst, TyVar)
TCvSubst -> TyVar -> (TCvSubst, TyVar)
substVarBndr TCvSubst
empty_subst TyVar
tv
in VarBndr TyVar ArgFlag -> Type -> Type
ForAllTy (TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv' ArgFlag
vis) (HasCallStack => TCvSubst -> Type -> Type
TCvSubst -> Type -> Type
substTy TCvSubst
subst Type
inner_ty Type -> KindCoercion -> Type
`mkCastTy` KindCoercion
co)
else VarBndr TyVar ArgFlag -> Type -> Type
ForAllTy (TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv ArgFlag
vis) (Type
inner_ty Type -> KindCoercion -> Type
`mkCastTy` KindCoercion
co)
mkCastTy Type
ty KindCoercion
co = Type -> KindCoercion -> Type
CastTy Type
ty KindCoercion
co
tyConBindersTyCoBinders :: [TyConBinder] -> [TyCoBinder]
tyConBindersTyCoBinders :: [TyConBinder] -> [TyCoBinder]
tyConBindersTyCoBinders = (TyConBinder -> TyCoBinder) -> [TyConBinder] -> [TyCoBinder]
forall a b. (a -> b) -> [a] -> [b]
map TyConBinder -> TyCoBinder
to_tyb
where
to_tyb :: TyConBinder -> TyCoBinder
to_tyb (Bndr TyVar
tv (NamedTCB ArgFlag
vis)) = VarBndr TyVar ArgFlag -> TyCoBinder
Named (TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv ArgFlag
vis)
to_tyb (Bndr TyVar
tv TyConBndrVis
AnonTCB) = Type -> TyCoBinder
Anon (TyVar -> Type
varType TyVar
tv)
mkCoercionTy :: Coercion -> Type
mkCoercionTy :: KindCoercion -> Type
mkCoercionTy = KindCoercion -> Type
CoercionTy
isCoercionTy :: Type -> Bool
isCoercionTy :: Type -> Bool
isCoercionTy (CoercionTy KindCoercion
_) = Bool
True
isCoercionTy Type
_ = Bool
False
isCoercionTy_maybe :: Type -> Maybe Coercion
isCoercionTy_maybe :: Type -> Maybe KindCoercion
isCoercionTy_maybe (CoercionTy KindCoercion
co) = KindCoercion -> Maybe KindCoercion
forall a. a -> Maybe a
Just KindCoercion
co
isCoercionTy_maybe Type
_ = Maybe KindCoercion
forall a. Maybe a
Nothing
stripCoercionTy :: Type -> Coercion
stripCoercionTy :: Type -> KindCoercion
stripCoercionTy (CoercionTy KindCoercion
co) = KindCoercion
co
stripCoercionTy Type
ty = String -> SDoc -> KindCoercion
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"stripCoercionTy" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
mkTyCoInvForAllTy :: TyCoVar -> Type -> Type
mkTyCoInvForAllTy :: TyVar -> Type -> Type
mkTyCoInvForAllTy TyVar
tv Type
ty
| TyVar -> Bool
isCoVar TyVar
tv
, Bool -> Bool
not (TyVar
tv TyVar -> VarSet -> Bool
`elemVarSet` Type -> VarSet
tyCoVarsOfType Type
ty)
= Type -> Type -> Type
mkFunTy (TyVar -> Type
varType TyVar
tv) Type
ty
| Bool
otherwise
= VarBndr TyVar ArgFlag -> Type -> Type
ForAllTy (TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv ArgFlag
Inferred) Type
ty
mkInvForAllTy :: TyVar -> Type -> Type
mkInvForAllTy :: TyVar -> Type -> Type
mkInvForAllTy TyVar
tv Type
ty = ASSERT( isTyVar tv )
VarBndr TyVar ArgFlag -> Type -> Type
ForAllTy (TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv ArgFlag
Inferred) Type
ty
mkTyCoInvForAllTys :: [TyCoVar] -> Type -> Type
mkTyCoInvForAllTys :: [TyVar] -> Type -> Type
mkTyCoInvForAllTys [TyVar]
tvs Type
ty = (TyVar -> Type -> Type) -> Type -> [TyVar] -> Type
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr TyVar -> Type -> Type
mkTyCoInvForAllTy Type
ty [TyVar]
tvs
mkInvForAllTys :: [TyVar] -> Type -> Type
mkInvForAllTys :: [TyVar] -> Type -> Type
mkInvForAllTys [TyVar]
tvs Type
ty = (TyVar -> Type -> Type) -> Type -> [TyVar] -> Type
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr TyVar -> Type -> Type
mkInvForAllTy Type
ty [TyVar]
tvs
mkSpecForAllTys :: [TyVar] -> Type -> Type
mkSpecForAllTys :: [TyVar] -> Type -> Type
mkSpecForAllTys [TyVar]
tvs = ASSERT( all isTyVar tvs )
[VarBndr TyVar ArgFlag] -> Type -> Type
mkForAllTys [ TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv ArgFlag
Specified | TyVar
tv <- [TyVar]
tvs ]
mkVisForAllTys :: [TyVar] -> Type -> Type
mkVisForAllTys :: [TyVar] -> Type -> Type
mkVisForAllTys [TyVar]
tvs = ASSERT( all isTyVar tvs )
[VarBndr TyVar ArgFlag] -> Type -> Type
mkForAllTys [ TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv ArgFlag
Required | TyVar
tv <- [TyVar]
tvs ]
mkLamType :: Var -> Type -> Type
mkLamTypes :: [Var] -> Type -> Type
mkLamType :: TyVar -> Type -> Type
mkLamType TyVar
v Type
ty
| TyVar -> Bool
isCoVar TyVar
v
, TyVar
v TyVar -> VarSet -> Bool
`elemVarSet` Type -> VarSet
tyCoVarsOfType Type
ty
= VarBndr TyVar ArgFlag -> Type -> Type
ForAllTy (TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
v ArgFlag
Inferred) Type
ty
| TyVar -> Bool
isTyVar TyVar
v
= VarBndr TyVar ArgFlag -> Type -> Type
ForAllTy (TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
v ArgFlag
Inferred) Type
ty
| Bool
otherwise
= Type -> Type -> Type
FunTy (TyVar -> Type
varType TyVar
v) Type
ty
mkLamTypes :: [TyVar] -> Type -> Type
mkLamTypes [TyVar]
vs Type
ty = (TyVar -> Type -> Type) -> Type -> [TyVar] -> Type
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr TyVar -> Type -> Type
mkLamType Type
ty [TyVar]
vs
mkTyConBindersPreferAnon :: [TyVar]
-> TyCoVarSet
-> [TyConBinder]
mkTyConBindersPreferAnon :: [TyVar] -> VarSet -> [TyConBinder]
mkTyConBindersPreferAnon [TyVar]
vars VarSet
inner_tkvs = ASSERT( all isTyVar vars)
([TyConBinder], VarSet) -> [TyConBinder]
forall a b. (a, b) -> a
fst ([TyVar] -> ([TyConBinder], VarSet)
go [TyVar]
vars)
where
go :: [TyVar] -> ([TyConBinder], VarSet)
go :: [TyVar] -> ([TyConBinder], VarSet)
go [] = ([], VarSet
inner_tkvs)
go (TyVar
v:[TyVar]
vs) | TyVar
v TyVar -> VarSet -> Bool
`elemVarSet` VarSet
fvs
= ( TyVar -> TyConBndrVis -> TyConBinder
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
v (ArgFlag -> TyConBndrVis
NamedTCB ArgFlag
Required) TyConBinder -> [TyConBinder] -> [TyConBinder]
forall a. a -> [a] -> [a]
: [TyConBinder]
binders
, VarSet
fvs VarSet -> TyVar -> VarSet
`delVarSet` TyVar
v VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
kind_vars )
| Bool
otherwise
= ( TyVar -> TyConBndrVis -> TyConBinder
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
v TyConBndrVis
AnonTCB TyConBinder -> [TyConBinder] -> [TyConBinder]
forall a. a -> [a] -> [a]
: [TyConBinder]
binders
, VarSet
fvs VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
kind_vars )
where
([TyConBinder]
binders, VarSet
fvs) = [TyVar] -> ([TyConBinder], VarSet)
go [TyVar]
vs
kind_vars :: VarSet
kind_vars = Type -> VarSet
tyCoVarsOfType (Type -> VarSet) -> Type -> VarSet
forall a b. (a -> b) -> a -> b
$ TyVar -> Type
tyVarKind TyVar
v
splitForAllTys :: Type -> ([TyCoVar], Type)
splitForAllTys :: Type -> ([TyVar], Type)
splitForAllTys Type
ty = Type -> Type -> [TyVar] -> ([TyVar], Type)
split Type
ty Type
ty []
where
split :: Type -> Type -> [TyVar] -> ([TyVar], Type)
split Type
orig_ty Type
ty [TyVar]
tvs | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Type -> [TyVar] -> ([TyVar], Type)
split Type
orig_ty Type
ty' [TyVar]
tvs
split Type
_ (ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
ty) [TyVar]
tvs = Type -> Type -> [TyVar] -> ([TyVar], Type)
split Type
ty Type
ty (TyVar
tvTyVar -> [TyVar] -> [TyVar]
forall a. a -> [a] -> [a]
:[TyVar]
tvs)
split Type
orig_ty Type
_ [TyVar]
tvs = ([TyVar] -> [TyVar]
forall a. [a] -> [a]
reverse [TyVar]
tvs, Type
orig_ty)
splitTyVarForAllTys :: Type -> ([TyVar], Type)
splitTyVarForAllTys :: Type -> ([TyVar], Type)
splitTyVarForAllTys Type
ty = Type -> Type -> [TyVar] -> ([TyVar], Type)
split Type
ty Type
ty []
where
split :: Type -> Type -> [TyVar] -> ([TyVar], Type)
split Type
orig_ty Type
ty [TyVar]
tvs | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Type -> [TyVar] -> ([TyVar], Type)
split Type
orig_ty Type
ty' [TyVar]
tvs
split Type
_ (ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
ty) [TyVar]
tvs | TyVar -> Bool
isTyVar TyVar
tv = Type -> Type -> [TyVar] -> ([TyVar], Type)
split Type
ty Type
ty (TyVar
tvTyVar -> [TyVar] -> [TyVar]
forall a. a -> [a] -> [a]
:[TyVar]
tvs)
split Type
orig_ty Type
_ [TyVar]
tvs = ([TyVar] -> [TyVar]
forall a. [a] -> [a]
reverse [TyVar]
tvs, Type
orig_ty)
isForAllTy :: Type -> Bool
isForAllTy :: Type -> Bool
isForAllTy Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Bool
isForAllTy Type
ty'
isForAllTy (ForAllTy {}) = Bool
True
isForAllTy Type
_ = Bool
False
isForAllTy_ty :: Type -> Bool
isForAllTy_ty :: Type -> Bool
isForAllTy_ty Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Bool
isForAllTy_ty Type
ty'
isForAllTy_ty (ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
_) | TyVar -> Bool
isTyVar TyVar
tv = Bool
True
isForAllTy_ty Type
_ = Bool
False
isForAllTy_co :: Type -> Bool
isForAllTy_co :: Type -> Bool
isForAllTy_co Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Bool
isForAllTy_co Type
ty'
isForAllTy_co (ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
_) | TyVar -> Bool
isCoVar TyVar
tv = Bool
True
isForAllTy_co Type
_ = Bool
False
isPiTy :: Type -> Bool
isPiTy :: Type -> Bool
isPiTy Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Bool
isForAllTy Type
ty'
isPiTy (ForAllTy {}) = Bool
True
isPiTy (FunTy {}) = Bool
True
isPiTy Type
_ = Bool
False
splitForAllTy :: Type -> (TyCoVar, Type)
splitForAllTy :: Type -> (TyVar, Type)
splitForAllTy Type
ty
| Just (TyVar, Type)
answer <- Type -> Maybe (TyVar, Type)
splitForAllTy_maybe Type
ty = (TyVar, Type)
answer
| Bool
otherwise = String -> SDoc -> (TyVar, Type)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"splitForAllTy" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
dropForAlls :: Type -> Type
dropForAlls :: Type -> Type
dropForAlls Type
ty = Type -> Type
go Type
ty
where
go :: Type -> Type
go Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Type
go Type
ty'
go (ForAllTy VarBndr TyVar ArgFlag
_ Type
res) = Type -> Type
go Type
res
go Type
res = Type
res
splitForAllTy_maybe :: Type -> Maybe (TyCoVar, Type)
splitForAllTy_maybe :: Type -> Maybe (TyVar, Type)
splitForAllTy_maybe Type
ty = Type -> Maybe (TyVar, Type)
go Type
ty
where
go :: Type -> Maybe (TyVar, Type)
go Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe (TyVar, Type)
go Type
ty'
go (ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
ty) = (TyVar, Type) -> Maybe (TyVar, Type)
forall a. a -> Maybe a
Just (TyVar
tv, Type
ty)
go Type
_ = Maybe (TyVar, Type)
forall a. Maybe a
Nothing
splitForAllTy_ty_maybe :: Type -> Maybe (TyCoVar, Type)
splitForAllTy_ty_maybe :: Type -> Maybe (TyVar, Type)
splitForAllTy_ty_maybe Type
ty = Type -> Maybe (TyVar, Type)
go Type
ty
where
go :: Type -> Maybe (TyVar, Type)
go Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe (TyVar, Type)
go Type
ty'
go (ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
ty) | TyVar -> Bool
isTyVar TyVar
tv = (TyVar, Type) -> Maybe (TyVar, Type)
forall a. a -> Maybe a
Just (TyVar
tv, Type
ty)
go Type
_ = Maybe (TyVar, Type)
forall a. Maybe a
Nothing
splitForAllTy_co_maybe :: Type -> Maybe (TyCoVar, Type)
splitForAllTy_co_maybe :: Type -> Maybe (TyVar, Type)
splitForAllTy_co_maybe Type
ty = Type -> Maybe (TyVar, Type)
go Type
ty
where
go :: Type -> Maybe (TyVar, Type)
go Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe (TyVar, Type)
go Type
ty'
go (ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
ty) | TyVar -> Bool
isCoVar TyVar
tv = (TyVar, Type) -> Maybe (TyVar, Type)
forall a. a -> Maybe a
Just (TyVar
tv, Type
ty)
go Type
_ = Maybe (TyVar, Type)
forall a. Maybe a
Nothing
splitPiTy_maybe :: Type -> Maybe (TyCoBinder, Type)
splitPiTy_maybe :: Type -> Maybe (TyCoBinder, Type)
splitPiTy_maybe Type
ty = Type -> Maybe (TyCoBinder, Type)
go Type
ty
where
go :: Type -> Maybe (TyCoBinder, Type)
go Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Maybe (TyCoBinder, Type)
go Type
ty'
go (ForAllTy VarBndr TyVar ArgFlag
bndr Type
ty) = (TyCoBinder, Type) -> Maybe (TyCoBinder, Type)
forall a. a -> Maybe a
Just (VarBndr TyVar ArgFlag -> TyCoBinder
Named VarBndr TyVar ArgFlag
bndr, Type
ty)
go (FunTy Type
arg Type
res) = (TyCoBinder, Type) -> Maybe (TyCoBinder, Type)
forall a. a -> Maybe a
Just (Type -> TyCoBinder
Anon Type
arg, Type
res)
go Type
_ = Maybe (TyCoBinder, Type)
forall a. Maybe a
Nothing
splitPiTy :: Type -> (TyCoBinder, Type)
splitPiTy :: Type -> (TyCoBinder, Type)
splitPiTy Type
ty
| Just (TyCoBinder, Type)
answer <- Type -> Maybe (TyCoBinder, Type)
splitPiTy_maybe Type
ty = (TyCoBinder, Type)
answer
| Bool
otherwise = String -> SDoc -> (TyCoBinder, Type)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"splitPiTy" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
splitPiTys :: Type -> ([TyCoBinder], Type)
splitPiTys :: Type -> ([TyCoBinder], Type)
splitPiTys Type
ty = Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split Type
ty Type
ty []
where
split :: Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split Type
orig_ty Type
ty [TyCoBinder]
bs | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split Type
orig_ty Type
ty' [TyCoBinder]
bs
split Type
_ (ForAllTy VarBndr TyVar ArgFlag
b Type
res) [TyCoBinder]
bs = Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split Type
res Type
res (VarBndr TyVar ArgFlag -> TyCoBinder
Named VarBndr TyVar ArgFlag
b TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bs)
split Type
_ (FunTy Type
arg Type
res) [TyCoBinder]
bs = Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split Type
res Type
res (Type -> TyCoBinder
Anon Type
arg TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bs)
split Type
orig_ty Type
_ [TyCoBinder]
bs = ([TyCoBinder] -> [TyCoBinder]
forall a. [a] -> [a]
reverse [TyCoBinder]
bs, Type
orig_ty)
splitForAllVarBndrs :: Type -> ([TyCoVarBinder], Type)
splitForAllVarBndrs :: Type -> ([VarBndr TyVar ArgFlag], Type)
splitForAllVarBndrs Type
ty = Type
-> Type
-> [VarBndr TyVar ArgFlag]
-> ([VarBndr TyVar ArgFlag], Type)
split Type
ty Type
ty []
where
split :: Type
-> Type
-> [VarBndr TyVar ArgFlag]
-> ([VarBndr TyVar ArgFlag], Type)
split Type
orig_ty Type
ty [VarBndr TyVar ArgFlag]
bs | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type
-> Type
-> [VarBndr TyVar ArgFlag]
-> ([VarBndr TyVar ArgFlag], Type)
split Type
orig_ty Type
ty' [VarBndr TyVar ArgFlag]
bs
split Type
_ (ForAllTy VarBndr TyVar ArgFlag
b Type
res) [VarBndr TyVar ArgFlag]
bs = Type
-> Type
-> [VarBndr TyVar ArgFlag]
-> ([VarBndr TyVar ArgFlag], Type)
split Type
res Type
res (VarBndr TyVar ArgFlag
bVarBndr TyVar ArgFlag
-> [VarBndr TyVar ArgFlag] -> [VarBndr TyVar ArgFlag]
forall a. a -> [a] -> [a]
:[VarBndr TyVar ArgFlag]
bs)
split Type
orig_ty Type
_ [VarBndr TyVar ArgFlag]
bs = ([VarBndr TyVar ArgFlag] -> [VarBndr TyVar ArgFlag]
forall a. [a] -> [a]
reverse [VarBndr TyVar ArgFlag]
bs, Type
orig_ty)
{-# INLINE splitForAllVarBndrs #-}
invisibleTyBndrCount :: Type -> Int
invisibleTyBndrCount :: Type -> Int
invisibleTyBndrCount Type
ty = [TyCoBinder] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (([TyCoBinder], Type) -> [TyCoBinder]
forall a b. (a, b) -> a
fst (Type -> ([TyCoBinder], Type)
splitPiTysInvisible Type
ty))
splitPiTysInvisible :: Type -> ([TyCoBinder], Type)
splitPiTysInvisible :: Type -> ([TyCoBinder], Type)
splitPiTysInvisible Type
ty = Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split Type
ty Type
ty []
where
split :: Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split Type
orig_ty Type
ty [TyCoBinder]
bs
| Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split Type
orig_ty Type
ty' [TyCoBinder]
bs
split Type
_ (ForAllTy VarBndr TyVar ArgFlag
b Type
res) [TyCoBinder]
bs
| Bndr TyVar
_ ArgFlag
vis <- VarBndr TyVar ArgFlag
b
, ArgFlag -> Bool
isInvisibleArgFlag ArgFlag
vis = Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split Type
res Type
res (VarBndr TyVar ArgFlag -> TyCoBinder
Named VarBndr TyVar ArgFlag
b TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bs)
split Type
_ (FunTy Type
arg Type
res) [TyCoBinder]
bs
| Type -> Bool
isPredTy Type
arg = Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split Type
res Type
res (Type -> TyCoBinder
Anon Type
arg TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bs)
split Type
orig_ty Type
_ [TyCoBinder]
bs = ([TyCoBinder] -> [TyCoBinder]
forall a. [a] -> [a]
reverse [TyCoBinder]
bs, Type
orig_ty)
splitPiTysInvisibleN :: Int -> Type -> ([TyCoBinder], Type)
splitPiTysInvisibleN :: Int -> Type -> ([TyCoBinder], Type)
splitPiTysInvisibleN Int
n Type
ty = Int -> Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
forall a.
(Eq a, Num a) =>
a -> Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split Int
n Type
ty Type
ty []
where
split :: a -> Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split a
n Type
orig_ty Type
ty [TyCoBinder]
bs
| a
n a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = ([TyCoBinder] -> [TyCoBinder]
forall a. [a] -> [a]
reverse [TyCoBinder]
bs, Type
orig_ty)
| Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = a -> Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split a
n Type
orig_ty Type
ty' [TyCoBinder]
bs
| ForAllTy VarBndr TyVar ArgFlag
b Type
res <- Type
ty
, Bndr TyVar
_ ArgFlag
vis <- VarBndr TyVar ArgFlag
b
, ArgFlag -> Bool
isInvisibleArgFlag ArgFlag
vis = a -> Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split (a
na -> a -> a
forall a. Num a => a -> a -> a
-a
1) Type
res Type
res (VarBndr TyVar ArgFlag -> TyCoBinder
Named VarBndr TyVar ArgFlag
b TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bs)
| FunTy Type
arg Type
res <- Type
ty
, Type -> Bool
isPredTy Type
arg = a -> Type -> Type -> [TyCoBinder] -> ([TyCoBinder], Type)
split (a
na -> a -> a
forall a. Num a => a -> a -> a
-a
1) Type
res Type
res (Type -> TyCoBinder
Anon Type
arg TyCoBinder -> [TyCoBinder] -> [TyCoBinder]
forall a. a -> [a] -> [a]
: [TyCoBinder]
bs)
| Bool
otherwise = ([TyCoBinder] -> [TyCoBinder]
forall a. [a] -> [a]
reverse [TyCoBinder]
bs, Type
orig_ty)
filterOutInvisibleTypes :: TyCon -> [Type] -> [Type]
filterOutInvisibleTypes :: TyCon -> [Type] -> [Type]
filterOutInvisibleTypes TyCon
tc [Type]
tys = ([Type], [Type]) -> [Type]
forall a b. (a, b) -> b
snd (([Type], [Type]) -> [Type]) -> ([Type], [Type]) -> [Type]
forall a b. (a -> b) -> a -> b
$ TyCon -> [Type] -> ([Type], [Type])
partitionInvisibleTypes TyCon
tc [Type]
tys
filterOutInferredTypes :: TyCon -> [Type] -> [Type]
filterOutInferredTypes :: TyCon -> [Type] -> [Type]
filterOutInferredTypes TyCon
tc [Type]
tys =
[Bool] -> [Type] -> [Type]
forall a. [Bool] -> [a] -> [a]
filterByList ((ArgFlag -> Bool) -> [ArgFlag] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (ArgFlag -> ArgFlag -> Bool
forall a. Eq a => a -> a -> Bool
/= ArgFlag
Inferred) ([ArgFlag] -> [Bool]) -> [ArgFlag] -> [Bool]
forall a b. (a -> b) -> a -> b
$ TyCon -> [Type] -> [ArgFlag]
tyConArgFlags TyCon
tc [Type]
tys) [Type]
tys
partitionInvisibleTypes :: TyCon -> [Type] -> ([Type], [Type])
partitionInvisibleTypes :: TyCon -> [Type] -> ([Type], [Type])
partitionInvisibleTypes TyCon
tc [Type]
tys =
[Bool] -> [Type] -> ([Type], [Type])
forall a. [Bool] -> [a] -> ([a], [a])
partitionByList ((ArgFlag -> Bool) -> [ArgFlag] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map ArgFlag -> Bool
isInvisibleArgFlag ([ArgFlag] -> [Bool]) -> [ArgFlag] -> [Bool]
forall a b. (a -> b) -> a -> b
$ TyCon -> [Type] -> [ArgFlag]
tyConArgFlags TyCon
tc [Type]
tys) [Type]
tys
partitionInvisibles :: [(a, ArgFlag)] -> ([a], [a])
partitionInvisibles :: [(a, ArgFlag)] -> ([a], [a])
partitionInvisibles = ((a, ArgFlag) -> Either a a) -> [(a, ArgFlag)] -> ([a], [a])
forall a b c. (a -> Either b c) -> [a] -> ([b], [c])
partitionWith (a, ArgFlag) -> Either a a
forall a. (a, ArgFlag) -> Either a a
pick_invis
where
pick_invis :: (a, ArgFlag) -> Either a a
pick_invis :: (a, ArgFlag) -> Either a a
pick_invis (a
thing, ArgFlag
vis) | ArgFlag -> Bool
isInvisibleArgFlag ArgFlag
vis = a -> Either a a
forall a b. a -> Either a b
Left a
thing
| Bool
otherwise = a -> Either a a
forall a b. b -> Either a b
Right a
thing
tyConArgFlags :: TyCon -> [Type] -> [ArgFlag]
tyConArgFlags :: TyCon -> [Type] -> [ArgFlag]
tyConArgFlags TyCon
tc = Type -> [Type] -> [ArgFlag]
fun_kind_arg_flags (TyCon -> Type
tyConKind TyCon
tc)
appTyArgFlags :: Type -> [Type] -> [ArgFlag]
appTyArgFlags :: Type -> [Type] -> [ArgFlag]
appTyArgFlags Type
ty = Type -> [Type] -> [ArgFlag]
fun_kind_arg_flags (HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
ty)
fun_kind_arg_flags :: Kind -> [Type] -> [ArgFlag]
fun_kind_arg_flags :: Type -> [Type] -> [ArgFlag]
fun_kind_arg_flags = TCvSubst -> Type -> [Type] -> [ArgFlag]
go TCvSubst
emptyTCvSubst
where
go :: TCvSubst -> Type -> [Type] -> [ArgFlag]
go TCvSubst
subst Type
ki [Type]
arg_tys
| Just Type
ki' <- Type -> Maybe Type
coreView Type
ki = TCvSubst -> Type -> [Type] -> [ArgFlag]
go TCvSubst
subst Type
ki' [Type]
arg_tys
go TCvSubst
_ Type
_ [] = []
go TCvSubst
subst (ForAllTy (Bndr TyVar
tv ArgFlag
argf) Type
res_ki) (Type
arg_ty:[Type]
arg_tys)
= ArgFlag
argf ArgFlag -> [ArgFlag] -> [ArgFlag]
forall a. a -> [a] -> [a]
: TCvSubst -> Type -> [Type] -> [ArgFlag]
go TCvSubst
subst' Type
res_ki [Type]
arg_tys
where
subst' :: TCvSubst
subst' = TCvSubst -> TyVar -> Type -> TCvSubst
extendTvSubst TCvSubst
subst TyVar
tv Type
arg_ty
go TCvSubst
subst (TyVarTy TyVar
tv) [Type]
arg_tys
| Just Type
ki <- TCvSubst -> TyVar -> Maybe Type
lookupTyVar TCvSubst
subst TyVar
tv = TCvSubst -> Type -> [Type] -> [ArgFlag]
go TCvSubst
subst Type
ki [Type]
arg_tys
go TCvSubst
_ Type
_ [Type]
arg_tys = (Type -> ArgFlag) -> [Type] -> [ArgFlag]
forall a b. (a -> b) -> [a] -> [b]
map (ArgFlag -> Type -> ArgFlag
forall a b. a -> b -> a
const ArgFlag
Required) [Type]
arg_tys
isTauTy :: Type -> Bool
isTauTy :: Type -> Bool
isTauTy Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Bool
isTauTy Type
ty'
isTauTy (TyVarTy TyVar
_) = Bool
True
isTauTy (LitTy {}) = Bool
True
isTauTy (TyConApp TyCon
tc [Type]
tys) = (Type -> Bool) -> [Type] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Type -> Bool
isTauTy [Type]
tys Bool -> Bool -> Bool
&& TyCon -> Bool
isTauTyCon TyCon
tc
isTauTy (AppTy Type
a Type
b) = Type -> Bool
isTauTy Type
a Bool -> Bool -> Bool
&& Type -> Bool
isTauTy Type
b
isTauTy (FunTy Type
a Type
b) = Type -> Bool
isTauTy Type
a Bool -> Bool -> Bool
&& Type -> Bool
isTauTy Type
b
isTauTy (ForAllTy {}) = Bool
False
isTauTy (CastTy Type
ty KindCoercion
_) = Type -> Bool
isTauTy Type
ty
isTauTy (CoercionTy KindCoercion
_) = Bool
False
mkAnonBinder :: Type -> TyCoBinder
mkAnonBinder :: Type -> TyCoBinder
mkAnonBinder = Type -> TyCoBinder
Anon
isAnonTyCoBinder :: TyCoBinder -> Bool
isAnonTyCoBinder :: TyCoBinder -> Bool
isAnonTyCoBinder (Named {}) = Bool
False
isAnonTyCoBinder (Anon {}) = Bool
True
tyCoBinderVar_maybe :: TyCoBinder -> Maybe TyCoVar
tyCoBinderVar_maybe :: TyCoBinder -> Maybe TyVar
tyCoBinderVar_maybe (Named VarBndr TyVar ArgFlag
tv) = TyVar -> Maybe TyVar
forall a. a -> Maybe a
Just (TyVar -> Maybe TyVar) -> TyVar -> Maybe TyVar
forall a b. (a -> b) -> a -> b
$ VarBndr TyVar ArgFlag -> TyVar
forall tv argf. VarBndr tv argf -> tv
binderVar VarBndr TyVar ArgFlag
tv
tyCoBinderVar_maybe TyCoBinder
_ = Maybe TyVar
forall a. Maybe a
Nothing
tyCoBinderType :: TyCoBinder -> Type
tyCoBinderType :: TyCoBinder -> Type
tyCoBinderType (Named VarBndr TyVar ArgFlag
tvb) = VarBndr TyVar ArgFlag -> Type
forall argf. VarBndr TyVar argf -> Type
binderType VarBndr TyVar ArgFlag
tvb
tyCoBinderType (Anon Type
ty) = Type
ty
tyBinderType :: TyBinder -> Type
tyBinderType :: TyCoBinder -> Type
tyBinderType (Named (Bndr TyVar
tv ArgFlag
_))
= ASSERT( isTyVar tv )
TyVar -> Type
tyVarKind TyVar
tv
tyBinderType (Anon Type
ty) = Type
ty
binderRelevantType_maybe :: TyCoBinder -> Maybe Type
binderRelevantType_maybe :: TyCoBinder -> Maybe Type
binderRelevantType_maybe (Named {}) = Maybe Type
forall a. Maybe a
Nothing
binderRelevantType_maybe (Anon Type
ty) = Type -> Maybe Type
forall a. a -> Maybe a
Just Type
ty
caseBinder :: TyCoBinder
-> (TyCoVarBinder -> a)
-> (Type -> a)
-> a
caseBinder :: TyCoBinder -> (VarBndr TyVar ArgFlag -> a) -> (Type -> a) -> a
caseBinder (Named VarBndr TyVar ArgFlag
v) VarBndr TyVar ArgFlag -> a
f Type -> a
_ = VarBndr TyVar ArgFlag -> a
f VarBndr TyVar ArgFlag
v
caseBinder (Anon Type
t) VarBndr TyVar ArgFlag -> a
_ Type -> a
d = Type -> a
d Type
t
tcSplitTyConApp_maybe :: HasCallStack => Type -> Maybe (TyCon, [Type])
tcSplitTyConApp_maybe :: Type -> Maybe (TyCon, [Type])
tcSplitTyConApp_maybe Type
ty | Just Type
ty' <- Type -> Maybe Type
tcView Type
ty = HasCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
tcSplitTyConApp_maybe Type
ty'
tcSplitTyConApp_maybe Type
ty = HasCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
tcRepSplitTyConApp_maybe Type
ty
tcIsConstraintKind :: Kind -> Bool
tcIsConstraintKind :: Type -> Bool
tcIsConstraintKind Type
ty
| Just (TyCon
tc, [Type]
args) <- HasCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
tcSplitTyConApp_maybe Type
ty
, TyCon -> Bool
isConstraintKindCon TyCon
tc
= ASSERT2( null args, ppr ty ) True
| Bool
otherwise
= Bool
False
tcIsLiftedTypeKind :: Kind -> Bool
tcIsLiftedTypeKind :: Type -> Bool
tcIsLiftedTypeKind Type
ty
| Just (TyCon
tc, [Type
arg]) <- HasCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
tcSplitTyConApp_maybe Type
ty
, TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
tYPETyConKey
= Type -> Bool
isLiftedRuntimeRep Type
arg
| Bool
otherwise
= Bool
False
tcReturnsConstraintKind :: Kind -> Bool
tcReturnsConstraintKind :: Type -> Bool
tcReturnsConstraintKind Type
kind
| Just Type
kind' <- Type -> Maybe Type
tcView Type
kind = Type -> Bool
tcReturnsConstraintKind Type
kind'
tcReturnsConstraintKind (ForAllTy VarBndr TyVar ArgFlag
_ Type
ty) = Type -> Bool
tcReturnsConstraintKind Type
ty
tcReturnsConstraintKind (FunTy Type
_ Type
ty) = Type -> Bool
tcReturnsConstraintKind Type
ty
tcReturnsConstraintKind (TyConApp TyCon
tc [Type]
_) = TyCon -> Bool
isConstraintKindCon TyCon
tc
tcReturnsConstraintKind Type
_ = Bool
False
isEvVarType :: Type -> Bool
isEvVarType :: Type -> Bool
isEvVarType Type
ty = Type -> Bool
isCoVarType Type
ty Bool -> Bool -> Bool
|| Type -> Bool
isPredTy Type
ty
isCoVarType :: Type -> Bool
isCoVarType :: Type -> Bool
isCoVarType Type
ty
| Just (TyCon
tc,[Type]
tys) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty
, (TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqPrimTyConKey) Bool -> Bool -> Bool
|| (TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqReprPrimTyConKey)
, [Type]
tys [Type] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthIs` Int
4
= Bool
True
isCoVarType Type
_ = Bool
False
isClassPred, isEqPred, isNomEqPred, isIPPred :: PredType -> Bool
isClassPred :: Type -> Bool
isClassPred Type
ty = case Type -> Maybe TyCon
tyConAppTyCon_maybe Type
ty of
Just TyCon
tyCon | TyCon -> Bool
isClassTyCon TyCon
tyCon -> Bool
True
Maybe TyCon
_ -> Bool
False
isEqPred :: Type -> Bool
isEqPred Type
ty = case Type -> Maybe TyCon
tyConAppTyCon_maybe Type
ty of
Just TyCon
tyCon -> TyCon
tyCon TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqPrimTyConKey
Bool -> Bool -> Bool
|| TyCon
tyCon TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqReprPrimTyConKey
Maybe TyCon
_ -> Bool
False
isNomEqPred :: Type -> Bool
isNomEqPred Type
ty = case Type -> Maybe TyCon
tyConAppTyCon_maybe Type
ty of
Just TyCon
tyCon -> TyCon
tyCon TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqPrimTyConKey
Maybe TyCon
_ -> Bool
False
isIPPred :: Type -> Bool
isIPPred Type
ty = case Type -> Maybe TyCon
tyConAppTyCon_maybe Type
ty of
Just TyCon
tc -> TyCon -> Bool
isIPTyCon TyCon
tc
Maybe TyCon
_ -> Bool
False
isIPTyCon :: TyCon -> Bool
isIPTyCon :: TyCon -> Bool
isIPTyCon TyCon
tc = TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
ipClassKey
isIPClass :: Class -> Bool
isIPClass :: Class -> Bool
isIPClass Class
cls = Class
cls Class -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
ipClassKey
isCTupleClass :: Class -> Bool
isCTupleClass :: Class -> Bool
isCTupleClass Class
cls = TyCon -> Bool
isTupleTyCon (Class -> TyCon
classTyCon Class
cls)
isIPPred_maybe :: Type -> Maybe (FastString, Type)
isIPPred_maybe :: Type -> Maybe (FastString, Type)
isIPPred_maybe Type
ty =
do (TyCon
tc,[Type
t1,Type
t2]) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (TyCon -> Bool
isIPTyCon TyCon
tc)
FastString
x <- Type -> Maybe FastString
isStrLitTy Type
t1
(FastString, Type) -> Maybe (FastString, Type)
forall (m :: * -> *) a. Monad m => a -> m a
return (FastString
x,Type
t2)
mkPrimEqPredRole :: Role -> Type -> Type -> PredType
mkPrimEqPredRole :: Role -> Type -> Type -> Type
mkPrimEqPredRole Role
Nominal = Type -> Type -> Type
mkPrimEqPred
mkPrimEqPredRole Role
Representational = Type -> Type -> Type
mkReprPrimEqPred
mkPrimEqPredRole Role
Phantom = String -> Type -> Type -> Type
forall a. String -> a
panic String
"mkPrimEqPredRole phantom"
mkPrimEqPred :: Type -> Type -> Type
mkPrimEqPred :: Type -> Type -> Type
mkPrimEqPred Type
ty1 Type
ty2
= TyCon -> [Type] -> Type
TyConApp TyCon
eqPrimTyCon [Type
k1, Type
k2, Type
ty1, Type
ty2]
where
k1 :: Type
k1 = HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
ty1
k2 :: Type
k2 = HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
ty2
mkHeteroPrimEqPred :: Kind -> Kind -> Type -> Type -> Type
mkHeteroPrimEqPred :: Type -> Type -> Type -> Type -> Type
mkHeteroPrimEqPred Type
k1 Type
k2 Type
ty1 Type
ty2 = TyCon -> [Type] -> Type
TyConApp TyCon
eqPrimTyCon [Type
k1, Type
k2, Type
ty1, Type
ty2]
mkHeteroReprPrimEqPred :: Kind -> Kind -> Type -> Type -> Type
mkHeteroReprPrimEqPred :: Type -> Type -> Type -> Type -> Type
mkHeteroReprPrimEqPred Type
k1 Type
k2 Type
ty1 Type
ty2
= TyCon -> [Type] -> Type
TyConApp TyCon
eqReprPrimTyCon [Type
k1, Type
k2, Type
ty1, Type
ty2]
splitCoercionType_maybe :: Type -> Maybe (Type, Type)
splitCoercionType_maybe :: Type -> Maybe (Type, Type)
splitCoercionType_maybe Type
ty
= do { (TyCon
tc, [Type
_, Type
_, Type
ty1, Type
ty2]) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty
; Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> Bool -> Maybe ()
forall a b. (a -> b) -> a -> b
$ TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqPrimTyConKey Bool -> Bool -> Bool
|| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqReprPrimTyConKey
; (Type, Type) -> Maybe (Type, Type)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
ty1, Type
ty2) }
mkReprPrimEqPred :: Type -> Type -> Type
mkReprPrimEqPred :: Type -> Type -> Type
mkReprPrimEqPred Type
ty1 Type
ty2
= TyCon -> [Type] -> Type
TyConApp TyCon
eqReprPrimTyCon [Type
k1, Type
k2, Type
ty1, Type
ty2]
where
k1 :: Type
k1 = HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
ty1
k2 :: Type
k2 = HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
ty2
equalityTyCon :: Role -> TyCon
equalityTyCon :: Role -> TyCon
equalityTyCon Role
Nominal = TyCon
eqPrimTyCon
equalityTyCon Role
Representational = TyCon
eqReprPrimTyCon
equalityTyCon Role
Phantom = TyCon
eqPhantPrimTyCon
mkClassPred :: Class -> [Type] -> PredType
mkClassPred :: Class -> [Type] -> Type
mkClassPred Class
clas [Type]
tys = TyCon -> [Type] -> Type
TyConApp (Class -> TyCon
classTyCon Class
clas) [Type]
tys
isDictTy :: Type -> Bool
isDictTy :: Type -> Bool
isDictTy = Type -> Bool
isClassPred
isDictLikeTy :: Type -> Bool
isDictLikeTy :: Type -> Bool
isDictLikeTy Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Bool
isDictLikeTy Type
ty'
isDictLikeTy Type
ty = case HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty of
Just (TyCon
tc, [Type]
tys) | TyCon -> Bool
isClassTyCon TyCon
tc -> Bool
True
| TyCon -> Bool
isTupleTyCon TyCon
tc -> (Type -> Bool) -> [Type] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Type -> Bool
isDictLikeTy [Type]
tys
Maybe (TyCon, [Type])
_other -> Bool
False
data EqRel = NomEq | ReprEq
deriving (EqRel -> EqRel -> Bool
(EqRel -> EqRel -> Bool) -> (EqRel -> EqRel -> Bool) -> Eq EqRel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EqRel -> EqRel -> Bool
$c/= :: EqRel -> EqRel -> Bool
== :: EqRel -> EqRel -> Bool
$c== :: EqRel -> EqRel -> Bool
Eq, Eq EqRel
Eq EqRel
-> (EqRel -> EqRel -> Ordering)
-> (EqRel -> EqRel -> Bool)
-> (EqRel -> EqRel -> Bool)
-> (EqRel -> EqRel -> Bool)
-> (EqRel -> EqRel -> Bool)
-> (EqRel -> EqRel -> EqRel)
-> (EqRel -> EqRel -> EqRel)
-> Ord EqRel
EqRel -> EqRel -> Bool
EqRel -> EqRel -> Ordering
EqRel -> EqRel -> EqRel
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: EqRel -> EqRel -> EqRel
$cmin :: EqRel -> EqRel -> EqRel
max :: EqRel -> EqRel -> EqRel
$cmax :: EqRel -> EqRel -> EqRel
>= :: EqRel -> EqRel -> Bool
$c>= :: EqRel -> EqRel -> Bool
> :: EqRel -> EqRel -> Bool
$c> :: EqRel -> EqRel -> Bool
<= :: EqRel -> EqRel -> Bool
$c<= :: EqRel -> EqRel -> Bool
< :: EqRel -> EqRel -> Bool
$c< :: EqRel -> EqRel -> Bool
compare :: EqRel -> EqRel -> Ordering
$ccompare :: EqRel -> EqRel -> Ordering
$cp1Ord :: Eq EqRel
Ord)
instance Outputable EqRel where
ppr :: EqRel -> SDoc
ppr EqRel
NomEq = String -> SDoc
text String
"nominal equality"
ppr EqRel
ReprEq = String -> SDoc
text String
"representational equality"
eqRelRole :: EqRel -> Role
eqRelRole :: EqRel -> Role
eqRelRole EqRel
NomEq = Role
Nominal
eqRelRole EqRel
ReprEq = Role
Representational
data PredTree
= ClassPred Class [Type]
| EqPred EqRel Type Type
| IrredPred PredType
| ForAllPred [TyCoVarBinder] [PredType] PredType
classifyPredType :: PredType -> PredTree
classifyPredType :: Type -> PredTree
classifyPredType Type
ev_ty = case HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ev_ty of
Just (TyCon
tc, [Type
_, Type
_, Type
ty1, Type
ty2])
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqReprPrimTyConKey -> EqRel -> Type -> Type -> PredTree
EqPred EqRel
ReprEq Type
ty1 Type
ty2
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqPrimTyConKey -> EqRel -> Type -> Type -> PredTree
EqPred EqRel
NomEq Type
ty1 Type
ty2
Just (TyCon
tc, [Type]
tys)
| Just Class
clas <- TyCon -> Maybe Class
tyConClass_maybe TyCon
tc
-> Class -> [Type] -> PredTree
ClassPred Class
clas [Type]
tys
Maybe (TyCon, [Type])
_ | ([VarBndr TyVar ArgFlag]
tvs, Type
rho) <- Type -> ([VarBndr TyVar ArgFlag], Type)
splitForAllVarBndrs Type
ev_ty
, ([Type]
theta, Type
pred) <- Type -> ([Type], Type)
splitFunTys Type
rho
, Bool -> Bool
not ([VarBndr TyVar ArgFlag] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [VarBndr TyVar ArgFlag]
tvs Bool -> Bool -> Bool
&& [Type] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Type]
theta)
-> [VarBndr TyVar ArgFlag] -> [Type] -> Type -> PredTree
ForAllPred [VarBndr TyVar ArgFlag]
tvs [Type]
theta Type
pred
| Bool
otherwise
-> Type -> PredTree
IrredPred Type
ev_ty
getClassPredTys :: HasDebugCallStack => PredType -> (Class, [Type])
getClassPredTys :: Type -> (Class, [Type])
getClassPredTys Type
ty = case Type -> Maybe (Class, [Type])
getClassPredTys_maybe Type
ty of
Just (Class
clas, [Type]
tys) -> (Class
clas, [Type]
tys)
Maybe (Class, [Type])
Nothing -> String -> SDoc -> (Class, [Type])
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"getClassPredTys" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
getClassPredTys_maybe :: PredType -> Maybe (Class, [Type])
getClassPredTys_maybe :: Type -> Maybe (Class, [Type])
getClassPredTys_maybe Type
ty = case HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty of
Just (TyCon
tc, [Type]
tys) | Just Class
clas <- TyCon -> Maybe Class
tyConClass_maybe TyCon
tc -> (Class, [Type]) -> Maybe (Class, [Type])
forall a. a -> Maybe a
Just (Class
clas, [Type]
tys)
Maybe (TyCon, [Type])
_ -> Maybe (Class, [Type])
forall a. Maybe a
Nothing
getEqPredTys :: PredType -> (Type, Type)
getEqPredTys :: Type -> (Type, Type)
getEqPredTys Type
ty
= case HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty of
Just (TyCon
tc, [Type
_, Type
_, Type
ty1, Type
ty2])
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqPrimTyConKey
Bool -> Bool -> Bool
|| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqReprPrimTyConKey
-> (Type
ty1, Type
ty2)
Maybe (TyCon, [Type])
_ -> String -> SDoc -> (Type, Type)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"getEqPredTys" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
getEqPredTys_maybe :: PredType -> Maybe (Role, Type, Type)
getEqPredTys_maybe :: Type -> Maybe (Role, Type, Type)
getEqPredTys_maybe Type
ty
= case HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty of
Just (TyCon
tc, [Type
_, Type
_, Type
ty1, Type
ty2])
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqPrimTyConKey -> (Role, Type, Type) -> Maybe (Role, Type, Type)
forall a. a -> Maybe a
Just (Role
Nominal, Type
ty1, Type
ty2)
| TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqReprPrimTyConKey -> (Role, Type, Type) -> Maybe (Role, Type, Type)
forall a. a -> Maybe a
Just (Role
Representational, Type
ty1, Type
ty2)
Maybe (TyCon, [Type])
_ -> Maybe (Role, Type, Type)
forall a. Maybe a
Nothing
getEqPredRole :: PredType -> Role
getEqPredRole :: Type -> Role
getEqPredRole Type
ty = EqRel -> Role
eqRelRole (Type -> EqRel
predTypeEqRel Type
ty)
predTypeEqRel :: PredType -> EqRel
predTypeEqRel :: Type -> EqRel
predTypeEqRel Type
ty
| Just (TyCon
tc, [Type]
_) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty
, TyCon
tc TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqReprPrimTyConKey
= EqRel
ReprEq
| Bool
otherwise
= EqRel
NomEq
scopedSort :: [TyCoVar] -> [TyCoVar]
scopedSort :: [TyVar] -> [TyVar]
scopedSort = [TyVar] -> [VarSet] -> [TyVar] -> [TyVar]
go [] []
where
go :: [TyCoVar]
-> [TyCoVarSet]
-> [TyCoVar]
-> [TyCoVar]
go :: [TyVar] -> [VarSet] -> [TyVar] -> [TyVar]
go [TyVar]
acc [VarSet]
_fv_list [] = [TyVar] -> [TyVar]
forall a. [a] -> [a]
reverse [TyVar]
acc
go [TyVar]
acc [VarSet]
fv_list (TyVar
tv:[TyVar]
tvs)
= [TyVar] -> [VarSet] -> [TyVar] -> [TyVar]
go [TyVar]
acc' [VarSet]
fv_list' [TyVar]
tvs
where
([TyVar]
acc', [VarSet]
fv_list') = TyVar -> [TyVar] -> [VarSet] -> ([TyVar], [VarSet])
insert TyVar
tv [TyVar]
acc [VarSet]
fv_list
insert :: TyCoVar
-> [TyCoVar]
-> [TyCoVarSet]
-> ([TyCoVar], [TyCoVarSet])
insert :: TyVar -> [TyVar] -> [VarSet] -> ([TyVar], [VarSet])
insert TyVar
tv [] [] = ([TyVar
tv], [Type -> VarSet
tyCoVarsOfType (TyVar -> Type
tyVarKind TyVar
tv)])
insert TyVar
tv (TyVar
a:[TyVar]
as) (VarSet
fvs:[VarSet]
fvss)
| TyVar
tv TyVar -> VarSet -> Bool
`elemVarSet` VarSet
fvs
, ([TyVar]
as', [VarSet]
fvss') <- TyVar -> [TyVar] -> [VarSet] -> ([TyVar], [VarSet])
insert TyVar
tv [TyVar]
as [VarSet]
fvss
= (TyVar
aTyVar -> [TyVar] -> [TyVar]
forall a. a -> [a] -> [a]
:[TyVar]
as', VarSet
fvs VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
fv_tv VarSet -> [VarSet] -> [VarSet]
forall a. a -> [a] -> [a]
: [VarSet]
fvss')
| Bool
otherwise
= (TyVar
tvTyVar -> [TyVar] -> [TyVar]
forall a. a -> [a] -> [a]
:TyVar
aTyVar -> [TyVar] -> [TyVar]
forall a. a -> [a] -> [a]
:[TyVar]
as, VarSet
fvs VarSet -> VarSet -> VarSet
`unionVarSet` VarSet
fv_tv VarSet -> [VarSet] -> [VarSet]
forall a. a -> [a] -> [a]
: VarSet
fvs VarSet -> [VarSet] -> [VarSet]
forall a. a -> [a] -> [a]
: [VarSet]
fvss)
where
fv_tv :: VarSet
fv_tv = Type -> VarSet
tyCoVarsOfType (TyVar -> Type
tyVarKind TyVar
tv)
insert TyVar
_ [TyVar]
_ [VarSet]
_ = String -> ([TyVar], [VarSet])
forall a. String -> a
panic String
"scopedSort"
dVarSetElemsWellScoped :: DVarSet -> [Var]
dVarSetElemsWellScoped :: DVarSet -> [TyVar]
dVarSetElemsWellScoped = [TyVar] -> [TyVar]
scopedSort ([TyVar] -> [TyVar]) -> (DVarSet -> [TyVar]) -> DVarSet -> [TyVar]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DVarSet -> [TyVar]
dVarSetElems
tyCoVarsOfTypeWellScoped :: Type -> [TyVar]
tyCoVarsOfTypeWellScoped :: Type -> [TyVar]
tyCoVarsOfTypeWellScoped = [TyVar] -> [TyVar]
scopedSort ([TyVar] -> [TyVar]) -> (Type -> [TyVar]) -> Type -> [TyVar]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type -> [TyVar]
tyCoVarsOfTypeList
tyCoVarsOfTypesWellScoped :: [Type] -> [TyVar]
tyCoVarsOfTypesWellScoped :: [Type] -> [TyVar]
tyCoVarsOfTypesWellScoped = [TyVar] -> [TyVar]
scopedSort ([TyVar] -> [TyVar]) -> ([Type] -> [TyVar]) -> [Type] -> [TyVar]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Type] -> [TyVar]
tyCoVarsOfTypesList
tyCoVarsOfBindersWellScoped :: [TyVar] -> [TyVar]
tyCoVarsOfBindersWellScoped :: [TyVar] -> [TyVar]
tyCoVarsOfBindersWellScoped [TyVar]
tvs
= Type -> [TyVar]
tyCoVarsOfTypeWellScoped ([TyVar] -> Type -> Type
mkInvForAllTys [TyVar]
tvs Type
unitTy)
closeOverKinds :: TyVarSet -> TyVarSet
closeOverKinds :: VarSet -> VarSet
closeOverKinds = FV -> VarSet
fvVarSet (FV -> VarSet) -> (VarSet -> FV) -> VarSet -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TyVar] -> FV
closeOverKindsFV ([TyVar] -> FV) -> (VarSet -> [TyVar]) -> VarSet -> FV
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarSet -> [TyVar]
forall elt. UniqSet elt -> [elt]
nonDetEltsUniqSet
closeOverKindsFV :: [TyVar] -> FV
closeOverKindsFV :: [TyVar] -> FV
closeOverKindsFV [TyVar]
tvs =
(TyVar -> FV) -> [TyVar] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV (Type -> FV
tyCoFVsOfType (Type -> FV) -> (TyVar -> Type) -> TyVar -> FV
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyVar -> Type
tyVarKind) [TyVar]
tvs FV -> FV -> FV
`unionFV` [TyVar] -> FV
mkFVs [TyVar]
tvs
closeOverKindsList :: [TyVar] -> [TyVar]
closeOverKindsList :: [TyVar] -> [TyVar]
closeOverKindsList [TyVar]
tvs = FV -> [TyVar]
fvVarList (FV -> [TyVar]) -> FV -> [TyVar]
forall a b. (a -> b) -> a -> b
$ [TyVar] -> FV
closeOverKindsFV [TyVar]
tvs
closeOverKindsDSet :: DTyVarSet -> DTyVarSet
closeOverKindsDSet :: DVarSet -> DVarSet
closeOverKindsDSet = FV -> DVarSet
fvDVarSet (FV -> DVarSet) -> (DVarSet -> FV) -> DVarSet -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TyVar] -> FV
closeOverKindsFV ([TyVar] -> FV) -> (DVarSet -> [TyVar]) -> DVarSet -> FV
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DVarSet -> [TyVar]
dVarSetElems
mkFamilyTyConApp :: TyCon -> [Type] -> Type
mkFamilyTyConApp :: TyCon -> [Type] -> Type
mkFamilyTyConApp TyCon
tc [Type]
tys
| Just (TyCon
fam_tc, [Type]
fam_tys) <- TyCon -> Maybe (TyCon, [Type])
tyConFamInst_maybe TyCon
tc
, let tvs :: [TyVar]
tvs = TyCon -> [TyVar]
tyConTyVars TyCon
tc
fam_subst :: TCvSubst
fam_subst = ASSERT2( tvs `equalLength` tys, ppr tc <+> ppr tys )
[TyVar] -> [Type] -> TCvSubst
HasDebugCallStack => [TyVar] -> [Type] -> TCvSubst
zipTvSubst [TyVar]
tvs [Type]
tys
= TyCon -> [Type] -> Type
mkTyConApp TyCon
fam_tc (HasCallStack => TCvSubst -> [Type] -> [Type]
TCvSubst -> [Type] -> [Type]
substTys TCvSubst
fam_subst [Type]
fam_tys)
| Bool
otherwise
= TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
tys
coAxNthLHS :: CoAxiom br -> Int -> Type
coAxNthLHS :: CoAxiom br -> Int -> Type
coAxNthLHS CoAxiom br
ax Int
ind =
TyCon -> [Type] -> Type
mkTyConApp (CoAxiom br -> TyCon
forall (br :: BranchFlag). CoAxiom br -> TyCon
coAxiomTyCon CoAxiom br
ax) (CoAxBranch -> [Type]
coAxBranchLHS (CoAxiom br -> Int -> CoAxBranch
forall (br :: BranchFlag). CoAxiom br -> Int -> CoAxBranch
coAxiomNthBranch CoAxiom br
ax Int
ind))
pprSourceTyCon :: TyCon -> SDoc
pprSourceTyCon :: TyCon -> SDoc
pprSourceTyCon TyCon
tycon
| Just (TyCon
fam_tc, [Type]
tys) <- TyCon -> Maybe (TyCon, [Type])
tyConFamInst_maybe TyCon
tycon
= Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr (Type -> SDoc) -> Type -> SDoc
forall a b. (a -> b) -> a -> b
$ TyCon
fam_tc TyCon -> [Type] -> Type
`TyConApp` [Type]
tys
| Bool
otherwise
= TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
tycon
isFamFreeTy :: Type -> Bool
isFamFreeTy :: Type -> Bool
isFamFreeTy Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> Bool
isFamFreeTy Type
ty'
isFamFreeTy (TyVarTy TyVar
_) = Bool
True
isFamFreeTy (LitTy {}) = Bool
True
isFamFreeTy (TyConApp TyCon
tc [Type]
tys) = (Type -> Bool) -> [Type] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Type -> Bool
isFamFreeTy [Type]
tys Bool -> Bool -> Bool
&& TyCon -> Bool
isFamFreeTyCon TyCon
tc
isFamFreeTy (AppTy Type
a Type
b) = Type -> Bool
isFamFreeTy Type
a Bool -> Bool -> Bool
&& Type -> Bool
isFamFreeTy Type
b
isFamFreeTy (FunTy Type
a Type
b) = Type -> Bool
isFamFreeTy Type
a Bool -> Bool -> Bool
&& Type -> Bool
isFamFreeTy Type
b
isFamFreeTy (ForAllTy VarBndr TyVar ArgFlag
_ Type
ty) = Type -> Bool
isFamFreeTy Type
ty
isFamFreeTy (CastTy Type
ty KindCoercion
_) = Type -> Bool
isFamFreeTy Type
ty
isFamFreeTy (CoercionTy KindCoercion
_) = Bool
False
isLiftedType_maybe :: HasDebugCallStack => Type -> Maybe Bool
isLiftedType_maybe :: Type -> Maybe Bool
isLiftedType_maybe Type
ty = Type -> Maybe Bool
go (HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
ty)
where
go :: Type -> Maybe Bool
go Type
rr | Just Type
rr' <- Type -> Maybe Type
coreView Type
rr = Type -> Maybe Bool
go Type
rr'
| Type -> Bool
isLiftedRuntimeRep Type
rr = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
| TyConApp {} <- Type
rr = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
| Bool
otherwise = Maybe Bool
forall a. Maybe a
Nothing
isUnliftedType :: HasDebugCallStack => Type -> Bool
isUnliftedType :: Type -> Bool
isUnliftedType Type
ty
= Bool -> Bool
not (HasDebugCallStack => Type -> Maybe Bool
Type -> Maybe Bool
isLiftedType_maybe Type
ty Maybe Bool -> Bool -> Bool
forall a. Maybe a -> a -> a
`orElse`
String -> SDoc -> Bool
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"isUnliftedType" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty SDoc -> SDoc -> SDoc
<+> SDoc
dcolon SDoc -> SDoc -> SDoc
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr (HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
ty)))
isRuntimeRepKindedTy :: Type -> Bool
isRuntimeRepKindedTy :: Type -> Bool
isRuntimeRepKindedTy = Type -> Bool
isRuntimeRepTy (Type -> Bool) -> (Type -> Type) -> Type -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasDebugCallStack => Type -> Type
Type -> Type
typeKind
dropRuntimeRepArgs :: [Type] -> [Type]
dropRuntimeRepArgs :: [Type] -> [Type]
dropRuntimeRepArgs = (Type -> Bool) -> [Type] -> [Type]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Type -> Bool
isRuntimeRepKindedTy
getRuntimeRep_maybe :: HasDebugCallStack
=> Type -> Maybe Type
getRuntimeRep_maybe :: Type -> Maybe Type
getRuntimeRep_maybe = HasDebugCallStack => Type -> Maybe Type
Type -> Maybe Type
kindRep_maybe (Type -> Maybe Type) -> (Type -> Type) -> Type -> Maybe Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasDebugCallStack => Type -> Type
Type -> Type
typeKind
getRuntimeRep :: HasDebugCallStack => Type -> Type
getRuntimeRep :: Type -> Type
getRuntimeRep Type
ty
= case HasDebugCallStack => Type -> Maybe Type
Type -> Maybe Type
getRuntimeRep_maybe Type
ty of
Just Type
r -> Type
r
Maybe Type
Nothing -> String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"getRuntimeRep" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty SDoc -> SDoc -> SDoc
<+> SDoc
dcolon SDoc -> SDoc -> SDoc
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr (HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
ty))
isUnboxedTupleType :: Type -> Bool
isUnboxedTupleType :: Type -> Bool
isUnboxedTupleType Type
ty
= Type -> TyCon
tyConAppTyCon (HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
ty) TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
tupleRepDataConKey
isUnboxedSumType :: Type -> Bool
isUnboxedSumType :: Type -> Bool
isUnboxedSumType Type
ty
= Type -> TyCon
tyConAppTyCon (HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep Type
ty) TyCon -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
sumRepDataConKey
isAlgType :: Type -> Bool
isAlgType :: Type -> Bool
isAlgType Type
ty
= case HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty of
Just (TyCon
tc, [Type]
ty_args) -> ASSERT( ty_args `lengthIs` tyConArity tc )
TyCon -> Bool
isAlgTyCon TyCon
tc
Maybe (TyCon, [Type])
_other -> Bool
False
isDataFamilyAppType :: Type -> Bool
isDataFamilyAppType :: Type -> Bool
isDataFamilyAppType Type
ty = case Type -> Maybe TyCon
tyConAppTyCon_maybe Type
ty of
Just TyCon
tc -> TyCon -> Bool
isDataFamilyTyCon TyCon
tc
Maybe TyCon
_ -> Bool
False
isStrictType :: HasDebugCallStack => Type -> Bool
isStrictType :: Type -> Bool
isStrictType = HasDebugCallStack => Type -> Bool
Type -> Bool
isUnliftedType
isPrimitiveType :: Type -> Bool
isPrimitiveType :: Type -> Bool
isPrimitiveType Type
ty = case HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
ty of
Just (TyCon
tc, [Type]
ty_args) -> ASSERT( ty_args `lengthIs` tyConArity tc )
TyCon -> Bool
isPrimTyCon TyCon
tc
Maybe (TyCon, [Type])
_ -> Bool
False
isValidJoinPointType :: JoinArity -> Type -> Bool
isValidJoinPointType :: Int -> Type -> Bool
isValidJoinPointType Int
arity Type
ty
= VarSet -> Int -> Type -> Bool
forall a. (Eq a, Num a) => VarSet -> a -> Type -> Bool
valid_under VarSet
emptyVarSet Int
arity Type
ty
where
valid_under :: VarSet -> a -> Type -> Bool
valid_under VarSet
tvs a
arity Type
ty
| a
arity a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0
= VarSet -> Bool
isEmptyVarSet (VarSet
tvs VarSet -> VarSet -> VarSet
`intersectVarSet` Type -> VarSet
tyCoVarsOfType Type
ty)
| Just (TyVar
t, Type
ty') <- Type -> Maybe (TyVar, Type)
splitForAllTy_maybe Type
ty
= VarSet -> a -> Type -> Bool
valid_under (VarSet
tvs VarSet -> TyVar -> VarSet
`extendVarSet` TyVar
t) (a
aritya -> a -> a
forall a. Num a => a -> a -> a
-a
1) Type
ty'
| Just (Type
_, Type
res_ty) <- Type -> Maybe (Type, Type)
splitFunTy_maybe Type
ty
= VarSet -> a -> Type -> Bool
valid_under VarSet
tvs (a
aritya -> a -> a
forall a. Num a => a -> a -> a
-a
1) Type
res_ty
| Bool
otherwise
= Bool
False
seqType :: Type -> ()
seqType :: Type -> ()
seqType (LitTy TyLit
n) = TyLit
n TyLit -> () -> ()
`seq` ()
seqType (TyVarTy TyVar
tv) = TyVar
tv TyVar -> () -> ()
`seq` ()
seqType (AppTy Type
t1 Type
t2) = Type -> ()
seqType Type
t1 () -> () -> ()
`seq` Type -> ()
seqType Type
t2
seqType (FunTy Type
t1 Type
t2) = Type -> ()
seqType Type
t1 () -> () -> ()
`seq` Type -> ()
seqType Type
t2
seqType (TyConApp TyCon
tc [Type]
tys) = TyCon
tc TyCon -> () -> ()
`seq` [Type] -> ()
seqTypes [Type]
tys
seqType (ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
ty) = Type -> ()
seqType (TyVar -> Type
varType TyVar
tv) () -> () -> ()
`seq` Type -> ()
seqType Type
ty
seqType (CastTy Type
ty KindCoercion
co) = Type -> ()
seqType Type
ty () -> () -> ()
`seq` KindCoercion -> ()
seqCo KindCoercion
co
seqType (CoercionTy KindCoercion
co) = KindCoercion -> ()
seqCo KindCoercion
co
seqTypes :: [Type] -> ()
seqTypes :: [Type] -> ()
seqTypes [] = ()
seqTypes (Type
ty:[Type]
tys) = Type -> ()
seqType Type
ty () -> () -> ()
`seq` [Type] -> ()
seqTypes [Type]
tys
eqType :: Type -> Type -> Bool
eqType :: Type -> Type -> Bool
eqType Type
t1 Type
t2 = Ordering -> Bool
isEqual (Ordering -> Bool) -> Ordering -> Bool
forall a b. (a -> b) -> a -> b
$ Type -> Type -> Ordering
nonDetCmpType Type
t1 Type
t2
eqTypeX :: RnEnv2 -> Type -> Type -> Bool
eqTypeX :: RnEnv2 -> Type -> Type -> Bool
eqTypeX RnEnv2
env Type
t1 Type
t2 = Ordering -> Bool
isEqual (Ordering -> Bool) -> Ordering -> Bool
forall a b. (a -> b) -> a -> b
$ RnEnv2 -> Type -> Type -> Ordering
nonDetCmpTypeX RnEnv2
env Type
t1 Type
t2
eqTypes :: [Type] -> [Type] -> Bool
eqTypes :: [Type] -> [Type] -> Bool
eqTypes [Type]
tys1 [Type]
tys2 = Ordering -> Bool
isEqual (Ordering -> Bool) -> Ordering -> Bool
forall a b. (a -> b) -> a -> b
$ [Type] -> [Type] -> Ordering
nonDetCmpTypes [Type]
tys1 [Type]
tys2
eqVarBndrs :: RnEnv2 -> [Var] -> [Var] -> Maybe RnEnv2
eqVarBndrs :: RnEnv2 -> [TyVar] -> [TyVar] -> Maybe RnEnv2
eqVarBndrs RnEnv2
env [] []
= RnEnv2 -> Maybe RnEnv2
forall a. a -> Maybe a
Just RnEnv2
env
eqVarBndrs RnEnv2
env (TyVar
tv1:[TyVar]
tvs1) (TyVar
tv2:[TyVar]
tvs2)
| RnEnv2 -> Type -> Type -> Bool
eqTypeX RnEnv2
env (TyVar -> Type
varType TyVar
tv1) (TyVar -> Type
varType TyVar
tv2)
= RnEnv2 -> [TyVar] -> [TyVar] -> Maybe RnEnv2
eqVarBndrs (RnEnv2 -> TyVar -> TyVar -> RnEnv2
rnBndr2 RnEnv2
env TyVar
tv1 TyVar
tv2) [TyVar]
tvs1 [TyVar]
tvs2
eqVarBndrs RnEnv2
_ [TyVar]
_ [TyVar]
_= Maybe RnEnv2
forall a. Maybe a
Nothing
nonDetCmpType :: Type -> Type -> Ordering
nonDetCmpType :: Type -> Type -> Ordering
nonDetCmpType Type
t1 Type
t2
= RnEnv2 -> Type -> Type -> Ordering
nonDetCmpTypeX RnEnv2
rn_env Type
t1 Type
t2
where
rn_env :: RnEnv2
rn_env = InScopeSet -> RnEnv2
mkRnEnv2 (VarSet -> InScopeSet
mkInScopeSet ([Type] -> VarSet
tyCoVarsOfTypes [Type
t1, Type
t2]))
nonDetCmpTypes :: [Type] -> [Type] -> Ordering
nonDetCmpTypes :: [Type] -> [Type] -> Ordering
nonDetCmpTypes [Type]
ts1 [Type]
ts2 = RnEnv2 -> [Type] -> [Type] -> Ordering
nonDetCmpTypesX RnEnv2
rn_env [Type]
ts1 [Type]
ts2
where
rn_env :: RnEnv2
rn_env = InScopeSet -> RnEnv2
mkRnEnv2 (VarSet -> InScopeSet
mkInScopeSet ([Type] -> VarSet
tyCoVarsOfTypes ([Type]
ts1 [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type]
ts2)))
data TypeOrdering = TLT
| TEQ
| TEQX
| TGT
deriving (TypeOrdering -> TypeOrdering -> Bool
(TypeOrdering -> TypeOrdering -> Bool)
-> (TypeOrdering -> TypeOrdering -> Bool) -> Eq TypeOrdering
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TypeOrdering -> TypeOrdering -> Bool
$c/= :: TypeOrdering -> TypeOrdering -> Bool
== :: TypeOrdering -> TypeOrdering -> Bool
$c== :: TypeOrdering -> TypeOrdering -> Bool
Eq, Eq TypeOrdering
Eq TypeOrdering
-> (TypeOrdering -> TypeOrdering -> Ordering)
-> (TypeOrdering -> TypeOrdering -> Bool)
-> (TypeOrdering -> TypeOrdering -> Bool)
-> (TypeOrdering -> TypeOrdering -> Bool)
-> (TypeOrdering -> TypeOrdering -> Bool)
-> (TypeOrdering -> TypeOrdering -> TypeOrdering)
-> (TypeOrdering -> TypeOrdering -> TypeOrdering)
-> Ord TypeOrdering
TypeOrdering -> TypeOrdering -> Bool
TypeOrdering -> TypeOrdering -> Ordering
TypeOrdering -> TypeOrdering -> TypeOrdering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TypeOrdering -> TypeOrdering -> TypeOrdering
$cmin :: TypeOrdering -> TypeOrdering -> TypeOrdering
max :: TypeOrdering -> TypeOrdering -> TypeOrdering
$cmax :: TypeOrdering -> TypeOrdering -> TypeOrdering
>= :: TypeOrdering -> TypeOrdering -> Bool
$c>= :: TypeOrdering -> TypeOrdering -> Bool
> :: TypeOrdering -> TypeOrdering -> Bool
$c> :: TypeOrdering -> TypeOrdering -> Bool
<= :: TypeOrdering -> TypeOrdering -> Bool
$c<= :: TypeOrdering -> TypeOrdering -> Bool
< :: TypeOrdering -> TypeOrdering -> Bool
$c< :: TypeOrdering -> TypeOrdering -> Bool
compare :: TypeOrdering -> TypeOrdering -> Ordering
$ccompare :: TypeOrdering -> TypeOrdering -> Ordering
$cp1Ord :: Eq TypeOrdering
Ord, Int -> TypeOrdering
TypeOrdering -> Int
TypeOrdering -> [TypeOrdering]
TypeOrdering -> TypeOrdering
TypeOrdering -> TypeOrdering -> [TypeOrdering]
TypeOrdering -> TypeOrdering -> TypeOrdering -> [TypeOrdering]
(TypeOrdering -> TypeOrdering)
-> (TypeOrdering -> TypeOrdering)
-> (Int -> TypeOrdering)
-> (TypeOrdering -> Int)
-> (TypeOrdering -> [TypeOrdering])
-> (TypeOrdering -> TypeOrdering -> [TypeOrdering])
-> (TypeOrdering -> TypeOrdering -> [TypeOrdering])
-> (TypeOrdering -> TypeOrdering -> TypeOrdering -> [TypeOrdering])
-> Enum TypeOrdering
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: TypeOrdering -> TypeOrdering -> TypeOrdering -> [TypeOrdering]
$cenumFromThenTo :: TypeOrdering -> TypeOrdering -> TypeOrdering -> [TypeOrdering]
enumFromTo :: TypeOrdering -> TypeOrdering -> [TypeOrdering]
$cenumFromTo :: TypeOrdering -> TypeOrdering -> [TypeOrdering]
enumFromThen :: TypeOrdering -> TypeOrdering -> [TypeOrdering]
$cenumFromThen :: TypeOrdering -> TypeOrdering -> [TypeOrdering]
enumFrom :: TypeOrdering -> [TypeOrdering]
$cenumFrom :: TypeOrdering -> [TypeOrdering]
fromEnum :: TypeOrdering -> Int
$cfromEnum :: TypeOrdering -> Int
toEnum :: Int -> TypeOrdering
$ctoEnum :: Int -> TypeOrdering
pred :: TypeOrdering -> TypeOrdering
$cpred :: TypeOrdering -> TypeOrdering
succ :: TypeOrdering -> TypeOrdering
$csucc :: TypeOrdering -> TypeOrdering
Enum, TypeOrdering
TypeOrdering -> TypeOrdering -> Bounded TypeOrdering
forall a. a -> a -> Bounded a
maxBound :: TypeOrdering
$cmaxBound :: TypeOrdering
minBound :: TypeOrdering
$cminBound :: TypeOrdering
Bounded)
nonDetCmpTypeX :: RnEnv2 -> Type -> Type -> Ordering
nonDetCmpTypeX :: RnEnv2 -> Type -> Type -> Ordering
nonDetCmpTypeX RnEnv2
env Type
orig_t1 Type
orig_t2 =
case RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
orig_t1 Type
orig_t2 of
TypeOrdering
TEQX -> TypeOrdering -> Ordering
toOrdering (TypeOrdering -> Ordering) -> TypeOrdering -> Ordering
forall a b. (a -> b) -> a -> b
$ RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
k1 Type
k2
TypeOrdering
ty_ordering -> TypeOrdering -> Ordering
toOrdering TypeOrdering
ty_ordering
where
k1 :: Type
k1 = HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
orig_t1
k2 :: Type
k2 = HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
orig_t2
toOrdering :: TypeOrdering -> Ordering
toOrdering :: TypeOrdering -> Ordering
toOrdering TypeOrdering
TLT = Ordering
LT
toOrdering TypeOrdering
TEQ = Ordering
EQ
toOrdering TypeOrdering
TEQX = Ordering
EQ
toOrdering TypeOrdering
TGT = Ordering
GT
liftOrdering :: Ordering -> TypeOrdering
liftOrdering :: Ordering -> TypeOrdering
liftOrdering Ordering
LT = TypeOrdering
TLT
liftOrdering Ordering
EQ = TypeOrdering
TEQ
liftOrdering Ordering
GT = TypeOrdering
TGT
thenCmpTy :: TypeOrdering -> TypeOrdering -> TypeOrdering
thenCmpTy :: TypeOrdering -> TypeOrdering -> TypeOrdering
thenCmpTy TypeOrdering
TEQ TypeOrdering
rel = TypeOrdering
rel
thenCmpTy TypeOrdering
TEQX TypeOrdering
rel = TypeOrdering -> TypeOrdering
hasCast TypeOrdering
rel
thenCmpTy TypeOrdering
rel TypeOrdering
_ = TypeOrdering
rel
hasCast :: TypeOrdering -> TypeOrdering
hasCast :: TypeOrdering -> TypeOrdering
hasCast TypeOrdering
TEQ = TypeOrdering
TEQX
hasCast TypeOrdering
rel = TypeOrdering
rel
go :: RnEnv2 -> Type -> Type -> TypeOrdering
go :: RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
t1 Type
t2
| Just Type
t1' <- Type -> Maybe Type
coreView Type
t1 = RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
t1' Type
t2
| Just Type
t2' <- Type -> Maybe Type
coreView Type
t2 = RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
t1 Type
t2'
go RnEnv2
env (TyVarTy TyVar
tv1) (TyVarTy TyVar
tv2)
= Ordering -> TypeOrdering
liftOrdering (Ordering -> TypeOrdering) -> Ordering -> TypeOrdering
forall a b. (a -> b) -> a -> b
$ RnEnv2 -> TyVar -> TyVar
rnOccL RnEnv2
env TyVar
tv1 TyVar -> TyVar -> Ordering
`nonDetCmpVar` RnEnv2 -> TyVar -> TyVar
rnOccR RnEnv2
env TyVar
tv2
go RnEnv2
env (ForAllTy (Bndr TyVar
tv1 ArgFlag
_) Type
t1) (ForAllTy (Bndr TyVar
tv2 ArgFlag
_) Type
t2)
= RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env (TyVar -> Type
varType TyVar
tv1) (TyVar -> Type
varType TyVar
tv2)
TypeOrdering -> TypeOrdering -> TypeOrdering
`thenCmpTy` RnEnv2 -> Type -> Type -> TypeOrdering
go (RnEnv2 -> TyVar -> TyVar -> RnEnv2
rnBndr2 RnEnv2
env TyVar
tv1 TyVar
tv2) Type
t1 Type
t2
go RnEnv2
env (AppTy Type
s1 Type
t1) Type
ty2
| Just (Type
s2, Type
t2) <- HasDebugCallStack => Type -> Maybe (Type, Type)
Type -> Maybe (Type, Type)
repSplitAppTy_maybe Type
ty2
= RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
s1 Type
s2 TypeOrdering -> TypeOrdering -> TypeOrdering
`thenCmpTy` RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
t1 Type
t2
go RnEnv2
env Type
ty1 (AppTy Type
s2 Type
t2)
| Just (Type
s1, Type
t1) <- HasDebugCallStack => Type -> Maybe (Type, Type)
Type -> Maybe (Type, Type)
repSplitAppTy_maybe Type
ty1
= RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
s1 Type
s2 TypeOrdering -> TypeOrdering -> TypeOrdering
`thenCmpTy` RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
t1 Type
t2
go RnEnv2
env (FunTy Type
s1 Type
t1) (FunTy Type
s2 Type
t2)
= RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
s1 Type
s2 TypeOrdering -> TypeOrdering -> TypeOrdering
`thenCmpTy` RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
t1 Type
t2
go RnEnv2
env (TyConApp TyCon
tc1 [Type]
tys1) (TyConApp TyCon
tc2 [Type]
tys2)
= Ordering -> TypeOrdering
liftOrdering (TyCon
tc1 TyCon -> TyCon -> Ordering
`nonDetCmpTc` TyCon
tc2) TypeOrdering -> TypeOrdering -> TypeOrdering
`thenCmpTy` RnEnv2 -> [Type] -> [Type] -> TypeOrdering
gos RnEnv2
env [Type]
tys1 [Type]
tys2
go RnEnv2
_ (LitTy TyLit
l1) (LitTy TyLit
l2) = Ordering -> TypeOrdering
liftOrdering (TyLit -> TyLit -> Ordering
forall a. Ord a => a -> a -> Ordering
compare TyLit
l1 TyLit
l2)
go RnEnv2
env (CastTy Type
t1 KindCoercion
_) Type
t2 = TypeOrdering -> TypeOrdering
hasCast (TypeOrdering -> TypeOrdering) -> TypeOrdering -> TypeOrdering
forall a b. (a -> b) -> a -> b
$ RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
t1 Type
t2
go RnEnv2
env Type
t1 (CastTy Type
t2 KindCoercion
_) = TypeOrdering -> TypeOrdering
hasCast (TypeOrdering -> TypeOrdering) -> TypeOrdering -> TypeOrdering
forall a b. (a -> b) -> a -> b
$ RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
t1 Type
t2
go RnEnv2
_ (CoercionTy {}) (CoercionTy {}) = TypeOrdering
TEQ
go RnEnv2
_ Type
ty1 Type
ty2
= Ordering -> TypeOrdering
liftOrdering (Ordering -> TypeOrdering) -> Ordering -> TypeOrdering
forall a b. (a -> b) -> a -> b
$ (Type -> Int
get_rank Type
ty1) Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` (Type -> Int
get_rank Type
ty2)
where get_rank :: Type -> Int
get_rank :: Type -> Int
get_rank (CastTy {})
= String -> SDoc -> Int
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"nonDetCmpTypeX.get_rank" ([Type] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Type
ty1,Type
ty2])
get_rank (TyVarTy {}) = Int
0
get_rank (CoercionTy {}) = Int
1
get_rank (AppTy {}) = Int
3
get_rank (LitTy {}) = Int
4
get_rank (TyConApp {}) = Int
5
get_rank (FunTy {}) = Int
6
get_rank (ForAllTy {}) = Int
7
gos :: RnEnv2 -> [Type] -> [Type] -> TypeOrdering
gos :: RnEnv2 -> [Type] -> [Type] -> TypeOrdering
gos RnEnv2
_ [] [] = TypeOrdering
TEQ
gos RnEnv2
_ [] [Type]
_ = TypeOrdering
TLT
gos RnEnv2
_ [Type]
_ [] = TypeOrdering
TGT
gos RnEnv2
env (Type
ty1:[Type]
tys1) (Type
ty2:[Type]
tys2) = RnEnv2 -> Type -> Type -> TypeOrdering
go RnEnv2
env Type
ty1 Type
ty2 TypeOrdering -> TypeOrdering -> TypeOrdering
`thenCmpTy` RnEnv2 -> [Type] -> [Type] -> TypeOrdering
gos RnEnv2
env [Type]
tys1 [Type]
tys2
nonDetCmpTypesX :: RnEnv2 -> [Type] -> [Type] -> Ordering
nonDetCmpTypesX :: RnEnv2 -> [Type] -> [Type] -> Ordering
nonDetCmpTypesX RnEnv2
_ [] [] = Ordering
EQ
nonDetCmpTypesX RnEnv2
env (Type
t1:[Type]
tys1) (Type
t2:[Type]
tys2) = RnEnv2 -> Type -> Type -> Ordering
nonDetCmpTypeX RnEnv2
env Type
t1 Type
t2
Ordering -> Ordering -> Ordering
`thenCmp`
RnEnv2 -> [Type] -> [Type] -> Ordering
nonDetCmpTypesX RnEnv2
env [Type]
tys1 [Type]
tys2
nonDetCmpTypesX RnEnv2
_ [] [Type]
_ = Ordering
LT
nonDetCmpTypesX RnEnv2
_ [Type]
_ [] = Ordering
GT
nonDetCmpTc :: TyCon -> TyCon -> Ordering
nonDetCmpTc :: TyCon -> TyCon -> Ordering
nonDetCmpTc TyCon
tc1 TyCon
tc2
= ASSERT( not (isConstraintKindCon tc1) && not (isConstraintKindCon tc2) )
Unique
u1 Unique -> Unique -> Ordering
`nonDetCmpUnique` Unique
u2
where
u1 :: Unique
u1 = TyCon -> Unique
tyConUnique TyCon
tc1
u2 :: Unique
u2 = TyCon -> Unique
tyConUnique TyCon
tc2
typeKind :: HasDebugCallStack => Type -> Kind
typeKind :: Type -> Type
typeKind (TyConApp TyCon
tc [Type]
tys) = HasDebugCallStack => Type -> [Type] -> Type
Type -> [Type] -> Type
piResultTys (TyCon -> Type
tyConKind TyCon
tc) [Type]
tys
typeKind (LitTy TyLit
l) = TyLit -> Type
typeLiteralKind TyLit
l
typeKind (FunTy {}) = Type
liftedTypeKind
typeKind (TyVarTy TyVar
tyvar) = TyVar -> Type
tyVarKind TyVar
tyvar
typeKind (CastTy Type
_ty KindCoercion
co) = Pair Type -> Type
forall a. Pair a -> a
pSnd (Pair Type -> Type) -> Pair Type -> Type
forall a b. (a -> b) -> a -> b
$ KindCoercion -> Pair Type
coercionKind KindCoercion
co
typeKind (CoercionTy KindCoercion
co) = KindCoercion -> Type
coercionType KindCoercion
co
typeKind (AppTy Type
fun Type
arg)
= Type -> [Type] -> Type
go Type
fun [Type
arg]
where
go :: Type -> [Type] -> Type
go (AppTy Type
fun Type
arg) [Type]
args = Type -> [Type] -> Type
go Type
fun (Type
argType -> [Type] -> [Type]
forall a. a -> [a] -> [a]
:[Type]
args)
go Type
fun [Type]
args = HasDebugCallStack => Type -> [Type] -> Type
Type -> [Type] -> Type
piResultTys (HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
fun) [Type]
args
typeKind ty :: Type
ty@(ForAllTy {})
= case [TyVar] -> Type -> Maybe Type
occCheckExpand [TyVar]
tvs Type
body_kind of
Just Type
k' -> Type
k'
Maybe Type
Nothing -> String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"typeKind"
(Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty SDoc -> SDoc -> SDoc
$$ [TyVar] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TyVar]
tvs SDoc -> SDoc -> SDoc
$$ Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
body SDoc -> SDoc -> SDoc
<+> SDoc
dcolon SDoc -> SDoc -> SDoc
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
body_kind)
where
([TyVar]
tvs, Type
body) = Type -> ([TyVar], Type)
splitTyVarForAllTys Type
ty
body_kind :: Type
body_kind = HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
body
tcTypeKind :: HasDebugCallStack => Type -> Kind
tcTypeKind :: Type -> Type
tcTypeKind (TyConApp TyCon
tc [Type]
tys) = HasDebugCallStack => Type -> [Type] -> Type
Type -> [Type] -> Type
piResultTys (TyCon -> Type
tyConKind TyCon
tc) [Type]
tys
tcTypeKind (LitTy TyLit
l) = TyLit -> Type
typeLiteralKind TyLit
l
tcTypeKind (TyVarTy TyVar
tyvar) = TyVar -> Type
tyVarKind TyVar
tyvar
tcTypeKind (CastTy Type
_ty KindCoercion
co) = Pair Type -> Type
forall a. Pair a -> a
pSnd (Pair Type -> Type) -> Pair Type -> Type
forall a b. (a -> b) -> a -> b
$ KindCoercion -> Pair Type
coercionKind KindCoercion
co
tcTypeKind (CoercionTy KindCoercion
co) = KindCoercion -> Type
coercionType KindCoercion
co
tcTypeKind (FunTy Type
arg Type
res)
| Type -> Bool
isPredTy Type
arg Bool -> Bool -> Bool
&& Type -> Bool
isPredTy Type
res = Type
constraintKind
| Bool
otherwise = Type
liftedTypeKind
tcTypeKind (AppTy Type
fun Type
arg)
= Type -> [Type] -> Type
go Type
fun [Type
arg]
where
go :: Type -> [Type] -> Type
go (AppTy Type
fun Type
arg) [Type]
args = Type -> [Type] -> Type
go Type
fun (Type
argType -> [Type] -> [Type]
forall a. a -> [a] -> [a]
:[Type]
args)
go Type
fun [Type]
args = HasDebugCallStack => Type -> [Type] -> Type
Type -> [Type] -> Type
piResultTys (HasDebugCallStack => Type -> Type
Type -> Type
tcTypeKind Type
fun) [Type]
args
tcTypeKind ty :: Type
ty@(ForAllTy {})
| Type -> Bool
tcIsConstraintKind Type
body_kind
= Type
constraintKind
| Bool
otherwise
= case [TyVar] -> Type -> Maybe Type
occCheckExpand [TyVar]
tvs Type
body_kind of
Just Type
k' -> Type
k'
Maybe Type
Nothing -> String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tcTypeKind"
(Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty SDoc -> SDoc -> SDoc
$$ [TyVar] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TyVar]
tvs SDoc -> SDoc -> SDoc
$$ Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
body SDoc -> SDoc -> SDoc
<+> SDoc
dcolon SDoc -> SDoc -> SDoc
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
body_kind)
where
([TyVar]
tvs, Type
body) = Type -> ([TyVar], Type)
splitTyVarForAllTys Type
ty
body_kind :: Type
body_kind = HasDebugCallStack => Type -> Type
Type -> Type
tcTypeKind Type
body
isPredTy :: Type -> Bool
isPredTy :: Type -> Bool
isPredTy Type
ty = Type -> Bool
tcIsConstraintKind (HasDebugCallStack => Type -> Type
Type -> Type
tcTypeKind Type
ty)
typeLiteralKind :: TyLit -> Kind
typeLiteralKind :: TyLit -> Type
typeLiteralKind TyLit
l =
case TyLit
l of
NumTyLit Integer
_ -> Type
typeNatKind
StrTyLit FastString
_ -> Type
typeSymbolKind
isTypeLevPoly :: Type -> Bool
isTypeLevPoly :: Type -> Bool
isTypeLevPoly = Type -> Bool
go
where
go :: Type -> Bool
go ty :: Type
ty@(TyVarTy {}) = Type -> Bool
check_kind Type
ty
go ty :: Type
ty@(AppTy {}) = Type -> Bool
check_kind Type
ty
go ty :: Type
ty@(TyConApp TyCon
tc [Type]
_) | Bool -> Bool
not (TyCon -> Bool
isTcLevPoly TyCon
tc) = Bool
False
| Bool
otherwise = Type -> Bool
check_kind Type
ty
go (ForAllTy VarBndr TyVar ArgFlag
_ Type
ty) = Type -> Bool
go Type
ty
go (FunTy {}) = Bool
False
go (LitTy {}) = Bool
False
go ty :: Type
ty@(CastTy {}) = Type -> Bool
check_kind Type
ty
go ty :: Type
ty@(CoercionTy {}) = String -> SDoc -> Bool
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"isTypeLevPoly co" (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty)
check_kind :: Type -> Bool
check_kind = Type -> Bool
isKindLevPoly (Type -> Bool) -> (Type -> Type) -> Type -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasDebugCallStack => Type -> Type
Type -> Type
typeKind
resultIsLevPoly :: Type -> Bool
resultIsLevPoly :: Type -> Bool
resultIsLevPoly = Type -> Bool
isTypeLevPoly (Type -> Bool) -> (Type -> Type) -> Type -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([TyCoBinder], Type) -> Type
forall a b. (a, b) -> b
snd (([TyCoBinder], Type) -> Type)
-> (Type -> ([TyCoBinder], Type)) -> Type -> Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type -> ([TyCoBinder], Type)
splitPiTys
occCheckExpand :: [Var] -> Type -> Maybe Type
occCheckExpand :: [TyVar] -> Type -> Maybe Type
occCheckExpand [TyVar]
vs_to_avoid Type
ty
= (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go ([TyVar] -> VarSet
mkVarSet [TyVar]
vs_to_avoid, VarEnv TyVar
forall a. VarEnv a
emptyVarEnv) Type
ty
where
go :: (VarSet, VarEnv TyCoVar) -> Type -> Maybe Type
go :: (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go cxt :: (VarSet, VarEnv TyVar)
cxt@(VarSet
as, VarEnv TyVar
env) (TyVarTy TyVar
tv')
| TyVar
tv' TyVar -> VarSet -> Bool
`elemVarSet` VarSet
as = Maybe Type
forall a. Maybe a
Nothing
| Just TyVar
tv'' <- VarEnv TyVar -> TyVar -> Maybe TyVar
forall a. VarEnv a -> TyVar -> Maybe a
lookupVarEnv VarEnv TyVar
env TyVar
tv' = Type -> Maybe Type
forall (m :: * -> *) a. Monad m => a -> m a
return (TyVar -> Type
mkTyVarTy TyVar
tv'')
| Bool
otherwise = do { TyVar
tv'' <- (VarSet, VarEnv TyVar) -> TyVar -> Maybe TyVar
go_var (VarSet, VarEnv TyVar)
cxt TyVar
tv'
; Type -> Maybe Type
forall (m :: * -> *) a. Monad m => a -> m a
return (TyVar -> Type
mkTyVarTy TyVar
tv'') }
go (VarSet, VarEnv TyVar)
_ ty :: Type
ty@(LitTy {}) = Type -> Maybe Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
ty
go (VarSet, VarEnv TyVar)
cxt (AppTy Type
ty1 Type
ty2) = do { Type
ty1' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt Type
ty1
; Type
ty2' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt Type
ty2
; Type -> Maybe Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> Type -> Type
mkAppTy Type
ty1' Type
ty2') }
go (VarSet, VarEnv TyVar)
cxt (FunTy Type
ty1 Type
ty2) = do { Type
ty1' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt Type
ty1
; Type
ty2' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt Type
ty2
; Type -> Maybe Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> Type -> Type
mkFunTy Type
ty1' Type
ty2') }
go cxt :: (VarSet, VarEnv TyVar)
cxt@(VarSet
as, VarEnv TyVar
env) (ForAllTy (Bndr TyVar
tv ArgFlag
vis) Type
body_ty)
= do { Type
ki' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt (TyVar -> Type
varType TyVar
tv)
; let tv' :: TyVar
tv' = TyVar -> Type -> TyVar
setVarType TyVar
tv Type
ki'
env' :: VarEnv TyVar
env' = VarEnv TyVar -> TyVar -> TyVar -> VarEnv TyVar
forall a. VarEnv a -> TyVar -> a -> VarEnv a
extendVarEnv VarEnv TyVar
env TyVar
tv TyVar
tv'
as' :: VarSet
as' = VarSet
as VarSet -> TyVar -> VarSet
`delVarSet` TyVar
tv
; Type
body' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet
as', VarEnv TyVar
env') Type
body_ty
; Type -> Maybe Type
forall (m :: * -> *) a. Monad m => a -> m a
return (VarBndr TyVar ArgFlag -> Type -> Type
ForAllTy (TyVar -> ArgFlag -> VarBndr TyVar ArgFlag
forall var argf. var -> argf -> VarBndr var argf
Bndr TyVar
tv' ArgFlag
vis) Type
body') }
go (VarSet, VarEnv TyVar)
cxt ty :: Type
ty@(TyConApp TyCon
tc [Type]
tys)
= case (Type -> Maybe Type) -> [Type] -> Maybe [Type]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt) [Type]
tys of
Just [Type]
tys' -> Type -> Maybe Type
forall (m :: * -> *) a. Monad m => a -> m a
return (TyCon -> [Type] -> Type
mkTyConApp TyCon
tc [Type]
tys')
Maybe [Type]
Nothing | Just Type
ty' <- Type -> Maybe Type
tcView Type
ty -> (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt Type
ty'
| Bool
otherwise -> Maybe Type
forall a. Maybe a
Nothing
go (VarSet, VarEnv TyVar)
cxt (CastTy Type
ty KindCoercion
co) = do { Type
ty' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt Type
ty
; KindCoercion
co' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co
; Type -> Maybe Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> KindCoercion -> Type
mkCastTy Type
ty' KindCoercion
co') }
go (VarSet, VarEnv TyVar)
cxt (CoercionTy KindCoercion
co) = do { KindCoercion
co' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co
; Type -> Maybe Type
forall (m :: * -> *) a. Monad m => a -> m a
return (KindCoercion -> Type
mkCoercionTy KindCoercion
co') }
go_var :: (VarSet, VarEnv TyVar) -> TyVar -> Maybe TyVar
go_var (VarSet, VarEnv TyVar)
cxt TyVar
v = do { Type
k' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt (TyVar -> Type
varType TyVar
v)
; TyVar -> Maybe TyVar
forall (m :: * -> *) a. Monad m => a -> m a
return (TyVar -> Type -> TyVar
setVarType TyVar
v Type
k') }
go_mco :: (VarSet, VarEnv TyVar) -> MCoercionN -> Maybe MCoercionN
go_mco (VarSet, VarEnv TyVar)
_ MCoercionN
MRefl = MCoercionN -> Maybe MCoercionN
forall (m :: * -> *) a. Monad m => a -> m a
return MCoercionN
MRefl
go_mco (VarSet, VarEnv TyVar)
ctx (MCo KindCoercion
co) = KindCoercion -> MCoercionN
MCo (KindCoercion -> MCoercionN)
-> Maybe KindCoercion -> Maybe MCoercionN
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
ctx KindCoercion
co
go_co :: (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt (Refl Type
ty) = do { Type
ty' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt Type
ty
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> KindCoercion
mkNomReflCo Type
ty') }
go_co (VarSet, VarEnv TyVar)
cxt (GRefl Role
r Type
ty MCoercionN
mco) = do { MCoercionN
mco' <- (VarSet, VarEnv TyVar) -> MCoercionN -> Maybe MCoercionN
go_mco (VarSet, VarEnv TyVar)
cxt MCoercionN
mco
; Type
ty' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt Type
ty
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (Role -> Type -> MCoercionN -> KindCoercion
mkGReflCo Role
r Type
ty' MCoercionN
mco') }
go_co (VarSet, VarEnv TyVar)
cxt (TyConAppCo Role
r TyCon
tc [KindCoercion]
args) = do { [KindCoercion]
args' <- (KindCoercion -> Maybe KindCoercion)
-> [KindCoercion] -> Maybe [KindCoercion]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt) [KindCoercion]
args
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (HasDebugCallStack =>
Role -> TyCon -> [KindCoercion] -> KindCoercion
Role -> TyCon -> [KindCoercion] -> KindCoercion
mkTyConAppCo Role
r TyCon
tc [KindCoercion]
args') }
go_co (VarSet, VarEnv TyVar)
cxt (AppCo KindCoercion
co KindCoercion
arg) = do { KindCoercion
co' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co
; KindCoercion
arg' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
arg
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (KindCoercion -> KindCoercion -> KindCoercion
mkAppCo KindCoercion
co' KindCoercion
arg') }
go_co cxt :: (VarSet, VarEnv TyVar)
cxt@(VarSet
as, VarEnv TyVar
env) (ForAllCo TyVar
tv KindCoercion
kind_co KindCoercion
body_co)
= do { KindCoercion
kind_co' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
kind_co
; let tv' :: TyVar
tv' = TyVar -> Type -> TyVar
setVarType TyVar
tv (Type -> TyVar) -> Type -> TyVar
forall a b. (a -> b) -> a -> b
$
Pair Type -> Type
forall a. Pair a -> a
pFst (KindCoercion -> Pair Type
coercionKind KindCoercion
kind_co')
env' :: VarEnv TyVar
env' = VarEnv TyVar -> TyVar -> TyVar -> VarEnv TyVar
forall a. VarEnv a -> TyVar -> a -> VarEnv a
extendVarEnv VarEnv TyVar
env TyVar
tv TyVar
tv'
as' :: VarSet
as' = VarSet
as VarSet -> TyVar -> VarSet
`delVarSet` TyVar
tv
; KindCoercion
body' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet
as', VarEnv TyVar
env') KindCoercion
body_co
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (TyVar -> KindCoercion -> KindCoercion -> KindCoercion
ForAllCo TyVar
tv' KindCoercion
kind_co' KindCoercion
body') }
go_co (VarSet, VarEnv TyVar)
cxt (FunCo Role
r KindCoercion
co1 KindCoercion
co2) = do { KindCoercion
co1' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co1
; KindCoercion
co2' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co2
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (Role -> KindCoercion -> KindCoercion -> KindCoercion
mkFunCo Role
r KindCoercion
co1' KindCoercion
co2') }
go_co cxt :: (VarSet, VarEnv TyVar)
cxt@(VarSet
as,VarEnv TyVar
env) (CoVarCo TyVar
c)
| TyVar
c TyVar -> VarSet -> Bool
`elemVarSet` VarSet
as = Maybe KindCoercion
forall a. Maybe a
Nothing
| Just TyVar
c' <- VarEnv TyVar -> TyVar -> Maybe TyVar
forall a. VarEnv a -> TyVar -> Maybe a
lookupVarEnv VarEnv TyVar
env TyVar
c = KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (TyVar -> KindCoercion
mkCoVarCo TyVar
c')
| Bool
otherwise = do { TyVar
c' <- (VarSet, VarEnv TyVar) -> TyVar -> Maybe TyVar
go_var (VarSet, VarEnv TyVar)
cxt TyVar
c
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (TyVar -> KindCoercion
mkCoVarCo TyVar
c') }
go_co (VarSet, VarEnv TyVar)
cxt (HoleCo CoercionHole
h) = do { TyVar
c' <- (VarSet, VarEnv TyVar) -> TyVar -> Maybe TyVar
go_var (VarSet, VarEnv TyVar)
cxt (CoercionHole -> TyVar
ch_co_var CoercionHole
h)
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (CoercionHole -> KindCoercion
HoleCo (CoercionHole
h { ch_co_var :: TyVar
ch_co_var = TyVar
c' })) }
go_co (VarSet, VarEnv TyVar)
cxt (AxiomInstCo CoAxiom Branched
ax Int
ind [KindCoercion]
args) = do { [KindCoercion]
args' <- (KindCoercion -> Maybe KindCoercion)
-> [KindCoercion] -> Maybe [KindCoercion]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt) [KindCoercion]
args
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (CoAxiom Branched -> Int -> [KindCoercion] -> KindCoercion
mkAxiomInstCo CoAxiom Branched
ax Int
ind [KindCoercion]
args') }
go_co (VarSet, VarEnv TyVar)
cxt (UnivCo UnivCoProvenance
p Role
r Type
ty1 Type
ty2) = do { UnivCoProvenance
p' <- (VarSet, VarEnv TyVar)
-> UnivCoProvenance -> Maybe UnivCoProvenance
go_prov (VarSet, VarEnv TyVar)
cxt UnivCoProvenance
p
; Type
ty1' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt Type
ty1
; Type
ty2' <- (VarSet, VarEnv TyVar) -> Type -> Maybe Type
go (VarSet, VarEnv TyVar)
cxt Type
ty2
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (UnivCoProvenance -> Role -> Type -> Type -> KindCoercion
mkUnivCo UnivCoProvenance
p' Role
r Type
ty1' Type
ty2') }
go_co (VarSet, VarEnv TyVar)
cxt (SymCo KindCoercion
co) = do { KindCoercion
co' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (KindCoercion -> KindCoercion
mkSymCo KindCoercion
co') }
go_co (VarSet, VarEnv TyVar)
cxt (TransCo KindCoercion
co1 KindCoercion
co2) = do { KindCoercion
co1' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co1
; KindCoercion
co2' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co2
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (KindCoercion -> KindCoercion -> KindCoercion
mkTransCo KindCoercion
co1' KindCoercion
co2') }
go_co (VarSet, VarEnv TyVar)
cxt (NthCo Role
r Int
n KindCoercion
co) = do { KindCoercion
co' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (HasDebugCallStack => Role -> Int -> KindCoercion -> KindCoercion
Role -> Int -> KindCoercion -> KindCoercion
mkNthCo Role
r Int
n KindCoercion
co') }
go_co (VarSet, VarEnv TyVar)
cxt (LRCo LeftOrRight
lr KindCoercion
co) = do { KindCoercion
co' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (LeftOrRight -> KindCoercion -> KindCoercion
mkLRCo LeftOrRight
lr KindCoercion
co') }
go_co (VarSet, VarEnv TyVar)
cxt (InstCo KindCoercion
co KindCoercion
arg) = do { KindCoercion
co' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co
; KindCoercion
arg' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
arg
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (KindCoercion -> KindCoercion -> KindCoercion
mkInstCo KindCoercion
co' KindCoercion
arg') }
go_co (VarSet, VarEnv TyVar)
cxt (KindCo KindCoercion
co) = do { KindCoercion
co' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (KindCoercion -> KindCoercion
mkKindCo KindCoercion
co') }
go_co (VarSet, VarEnv TyVar)
cxt (SubCo KindCoercion
co) = do { KindCoercion
co' <- (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (KindCoercion -> KindCoercion
mkSubCo KindCoercion
co') }
go_co (VarSet, VarEnv TyVar)
cxt (AxiomRuleCo CoAxiomRule
ax [KindCoercion]
cs) = do { [KindCoercion]
cs' <- (KindCoercion -> Maybe KindCoercion)
-> [KindCoercion] -> Maybe [KindCoercion]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt) [KindCoercion]
cs
; KindCoercion -> Maybe KindCoercion
forall (m :: * -> *) a. Monad m => a -> m a
return (CoAxiomRule -> [KindCoercion] -> KindCoercion
mkAxiomRuleCo CoAxiomRule
ax [KindCoercion]
cs') }
go_prov :: (VarSet, VarEnv TyVar)
-> UnivCoProvenance -> Maybe UnivCoProvenance
go_prov (VarSet, VarEnv TyVar)
_ UnivCoProvenance
UnsafeCoerceProv = UnivCoProvenance -> Maybe UnivCoProvenance
forall (m :: * -> *) a. Monad m => a -> m a
return UnivCoProvenance
UnsafeCoerceProv
go_prov (VarSet, VarEnv TyVar)
cxt (PhantomProv KindCoercion
co) = KindCoercion -> UnivCoProvenance
PhantomProv (KindCoercion -> UnivCoProvenance)
-> Maybe KindCoercion -> Maybe UnivCoProvenance
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co
go_prov (VarSet, VarEnv TyVar)
cxt (ProofIrrelProv KindCoercion
co) = KindCoercion -> UnivCoProvenance
ProofIrrelProv (KindCoercion -> UnivCoProvenance)
-> Maybe KindCoercion -> Maybe UnivCoProvenance
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (VarSet, VarEnv TyVar) -> KindCoercion -> Maybe KindCoercion
go_co (VarSet, VarEnv TyVar)
cxt KindCoercion
co
go_prov (VarSet, VarEnv TyVar)
_ p :: UnivCoProvenance
p@(PluginProv String
_) = UnivCoProvenance -> Maybe UnivCoProvenance
forall (m :: * -> *) a. Monad m => a -> m a
return UnivCoProvenance
p
tyConsOfType :: Type -> UniqSet TyCon
tyConsOfType :: Type -> UniqSet TyCon
tyConsOfType Type
ty
= Type -> UniqSet TyCon
go Type
ty
where
go :: Type -> UniqSet TyCon
go :: Type -> UniqSet TyCon
go Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> UniqSet TyCon
go Type
ty'
go (TyVarTy {}) = UniqSet TyCon
forall a. UniqSet a
emptyUniqSet
go (LitTy {}) = UniqSet TyCon
forall a. UniqSet a
emptyUniqSet
go (TyConApp TyCon
tc [Type]
tys) = TyCon -> UniqSet TyCon
forall a. Uniquable a => a -> UniqSet a
go_tc TyCon
tc UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` [Type] -> UniqSet TyCon
forall (t :: * -> *). Foldable t => t Type -> UniqSet TyCon
go_s [Type]
tys
go (AppTy Type
a Type
b) = Type -> UniqSet TyCon
go Type
a UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` Type -> UniqSet TyCon
go Type
b
go (FunTy Type
a Type
b) = Type -> UniqSet TyCon
go Type
a UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` Type -> UniqSet TyCon
go Type
b UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` TyCon -> UniqSet TyCon
forall a. Uniquable a => a -> UniqSet a
go_tc TyCon
funTyCon
go (ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
ty) = Type -> UniqSet TyCon
go Type
ty UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` Type -> UniqSet TyCon
go (TyVar -> Type
varType TyVar
tv)
go (CastTy Type
ty KindCoercion
co) = Type -> UniqSet TyCon
go Type
ty UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` KindCoercion -> UniqSet TyCon
go_co KindCoercion
co
go (CoercionTy KindCoercion
co) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co
go_co :: KindCoercion -> UniqSet TyCon
go_co (Refl Type
ty) = Type -> UniqSet TyCon
go Type
ty
go_co (GRefl Role
_ Type
ty MCoercionN
mco) = Type -> UniqSet TyCon
go Type
ty UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` MCoercionN -> UniqSet TyCon
go_mco MCoercionN
mco
go_co (TyConAppCo Role
_ TyCon
tc [KindCoercion]
args) = TyCon -> UniqSet TyCon
forall a. Uniquable a => a -> UniqSet a
go_tc TyCon
tc UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` [KindCoercion] -> UniqSet TyCon
go_cos [KindCoercion]
args
go_co (AppCo KindCoercion
co KindCoercion
arg) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` KindCoercion -> UniqSet TyCon
go_co KindCoercion
arg
go_co (ForAllCo TyVar
_ KindCoercion
kind_co KindCoercion
co) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
kind_co UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` KindCoercion -> UniqSet TyCon
go_co KindCoercion
co
go_co (FunCo Role
_ KindCoercion
co1 KindCoercion
co2) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co1 UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` KindCoercion -> UniqSet TyCon
go_co KindCoercion
co2
go_co (AxiomInstCo CoAxiom Branched
ax Int
_ [KindCoercion]
args) = CoAxiom Branched -> UniqSet TyCon
forall (br :: BranchFlag). CoAxiom br -> UniqSet TyCon
go_ax CoAxiom Branched
ax UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` [KindCoercion] -> UniqSet TyCon
go_cos [KindCoercion]
args
go_co (UnivCo UnivCoProvenance
p Role
_ Type
t1 Type
t2) = UnivCoProvenance -> UniqSet TyCon
go_prov UnivCoProvenance
p UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` Type -> UniqSet TyCon
go Type
t1 UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` Type -> UniqSet TyCon
go Type
t2
go_co (CoVarCo {}) = UniqSet TyCon
forall a. UniqSet a
emptyUniqSet
go_co (HoleCo {}) = UniqSet TyCon
forall a. UniqSet a
emptyUniqSet
go_co (SymCo KindCoercion
co) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co
go_co (TransCo KindCoercion
co1 KindCoercion
co2) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co1 UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` KindCoercion -> UniqSet TyCon
go_co KindCoercion
co2
go_co (NthCo Role
_ Int
_ KindCoercion
co) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co
go_co (LRCo LeftOrRight
_ KindCoercion
co) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co
go_co (InstCo KindCoercion
co KindCoercion
arg) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
`unionUniqSets` KindCoercion -> UniqSet TyCon
go_co KindCoercion
arg
go_co (KindCo KindCoercion
co) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co
go_co (SubCo KindCoercion
co) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co
go_co (AxiomRuleCo CoAxiomRule
_ [KindCoercion]
cs) = [KindCoercion] -> UniqSet TyCon
go_cos [KindCoercion]
cs
go_mco :: MCoercionN -> UniqSet TyCon
go_mco MCoercionN
MRefl = UniqSet TyCon
forall a. UniqSet a
emptyUniqSet
go_mco (MCo KindCoercion
co) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co
go_prov :: UnivCoProvenance -> UniqSet TyCon
go_prov UnivCoProvenance
UnsafeCoerceProv = UniqSet TyCon
forall a. UniqSet a
emptyUniqSet
go_prov (PhantomProv KindCoercion
co) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co
go_prov (ProofIrrelProv KindCoercion
co) = KindCoercion -> UniqSet TyCon
go_co KindCoercion
co
go_prov (PluginProv String
_) = UniqSet TyCon
forall a. UniqSet a
emptyUniqSet
go_s :: t Type -> UniqSet TyCon
go_s t Type
tys = (Type -> UniqSet TyCon -> UniqSet TyCon)
-> UniqSet TyCon -> t Type -> UniqSet TyCon
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
unionUniqSets (UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon)
-> (Type -> UniqSet TyCon)
-> Type
-> UniqSet TyCon
-> UniqSet TyCon
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type -> UniqSet TyCon
go) UniqSet TyCon
forall a. UniqSet a
emptyUniqSet t Type
tys
go_cos :: [KindCoercion] -> UniqSet TyCon
go_cos [KindCoercion]
cos = (KindCoercion -> UniqSet TyCon -> UniqSet TyCon)
-> UniqSet TyCon -> [KindCoercion] -> UniqSet TyCon
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon
forall a. UniqSet a -> UniqSet a -> UniqSet a
unionUniqSets (UniqSet TyCon -> UniqSet TyCon -> UniqSet TyCon)
-> (KindCoercion -> UniqSet TyCon)
-> KindCoercion
-> UniqSet TyCon
-> UniqSet TyCon
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KindCoercion -> UniqSet TyCon
go_co) UniqSet TyCon
forall a. UniqSet a
emptyUniqSet [KindCoercion]
cos
go_tc :: a -> UniqSet a
go_tc a
tc = a -> UniqSet a
forall a. Uniquable a => a -> UniqSet a
unitUniqSet a
tc
go_ax :: CoAxiom br -> UniqSet TyCon
go_ax CoAxiom br
ax = TyCon -> UniqSet TyCon
forall a. Uniquable a => a -> UniqSet a
go_tc (TyCon -> UniqSet TyCon) -> TyCon -> UniqSet TyCon
forall a b. (a -> b) -> a -> b
$ CoAxiom br -> TyCon
forall (br :: BranchFlag). CoAxiom br -> TyCon
coAxiomTyCon CoAxiom br
ax
synTyConResKind :: TyCon -> Kind
synTyConResKind :: TyCon -> Type
synTyConResKind TyCon
tycon = HasDebugCallStack => Type -> [Type] -> Type
Type -> [Type] -> Type
piResultTys (TyCon -> Type
tyConKind TyCon
tycon) ([TyVar] -> [Type]
mkTyVarTys (TyCon -> [TyVar]
tyConTyVars TyCon
tycon))
splitVisVarsOfType :: Type -> Pair TyCoVarSet
splitVisVarsOfType :: Type -> Pair VarSet
splitVisVarsOfType Type
orig_ty = VarSet -> VarSet -> Pair VarSet
forall a. a -> a -> Pair a
Pair VarSet
invis_vars VarSet
vis_vars
where
Pair VarSet
invis_vars1 VarSet
vis_vars = Type -> Pair VarSet
go Type
orig_ty
invis_vars :: VarSet
invis_vars = VarSet
invis_vars1 VarSet -> VarSet -> VarSet
`minusVarSet` VarSet
vis_vars
go :: Type -> Pair VarSet
go (TyVarTy TyVar
tv) = VarSet -> VarSet -> Pair VarSet
forall a. a -> a -> Pair a
Pair (Type -> VarSet
tyCoVarsOfType (Type -> VarSet) -> Type -> VarSet
forall a b. (a -> b) -> a -> b
$ TyVar -> Type
tyVarKind TyVar
tv) (TyVar -> VarSet
unitVarSet TyVar
tv)
go (AppTy Type
t1 Type
t2) = Type -> Pair VarSet
go Type
t1 Pair VarSet -> Pair VarSet -> Pair VarSet
forall a. Monoid a => a -> a -> a
`mappend` Type -> Pair VarSet
go Type
t2
go (TyConApp TyCon
tc [Type]
tys) = TyCon -> [Type] -> Pair VarSet
go_tc TyCon
tc [Type]
tys
go (FunTy Type
t1 Type
t2) = Type -> Pair VarSet
go Type
t1 Pair VarSet -> Pair VarSet -> Pair VarSet
forall a. Monoid a => a -> a -> a
`mappend` Type -> Pair VarSet
go Type
t2
go (ForAllTy (Bndr TyVar
tv ArgFlag
_) Type
ty)
= ((VarSet -> TyVar -> VarSet
`delVarSet` TyVar
tv) (VarSet -> VarSet) -> Pair VarSet -> Pair VarSet
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Type -> Pair VarSet
go Type
ty) Pair VarSet -> Pair VarSet -> Pair VarSet
forall a. Monoid a => a -> a -> a
`mappend`
(VarSet -> Pair VarSet
invisible (Type -> VarSet
tyCoVarsOfType (Type -> VarSet) -> Type -> VarSet
forall a b. (a -> b) -> a -> b
$ TyVar -> Type
varType TyVar
tv))
go (LitTy {}) = Pair VarSet
forall a. Monoid a => a
mempty
go (CastTy Type
ty KindCoercion
co) = Type -> Pair VarSet
go Type
ty Pair VarSet -> Pair VarSet -> Pair VarSet
forall a. Monoid a => a -> a -> a
`mappend` VarSet -> Pair VarSet
invisible (KindCoercion -> VarSet
tyCoVarsOfCo KindCoercion
co)
go (CoercionTy KindCoercion
co) = VarSet -> Pair VarSet
invisible (VarSet -> Pair VarSet) -> VarSet -> Pair VarSet
forall a b. (a -> b) -> a -> b
$ KindCoercion -> VarSet
tyCoVarsOfCo KindCoercion
co
invisible :: VarSet -> Pair VarSet
invisible VarSet
vs = VarSet -> VarSet -> Pair VarSet
forall a. a -> a -> Pair a
Pair VarSet
vs VarSet
emptyVarSet
go_tc :: TyCon -> [Type] -> Pair VarSet
go_tc TyCon
tc [Type]
tys = let ([Type]
invis, [Type]
vis) = TyCon -> [Type] -> ([Type], [Type])
partitionInvisibleTypes TyCon
tc [Type]
tys in
VarSet -> Pair VarSet
invisible ([Type] -> VarSet
tyCoVarsOfTypes [Type]
invis) Pair VarSet -> Pair VarSet -> Pair VarSet
forall a. Monoid a => a -> a -> a
`mappend` (Type -> Pair VarSet) -> [Type] -> Pair VarSet
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Type -> Pair VarSet
go [Type]
vis
splitVisVarsOfTypes :: [Type] -> Pair TyCoVarSet
splitVisVarsOfTypes :: [Type] -> Pair VarSet
splitVisVarsOfTypes = (Type -> Pair VarSet) -> [Type] -> Pair VarSet
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Type -> Pair VarSet
splitVisVarsOfType
modifyJoinResTy :: Int
-> (Type -> Type)
-> Type
-> Type
modifyJoinResTy :: Int -> (Type -> Type) -> Type -> Type
modifyJoinResTy Int
orig_ar Type -> Type
f Type
orig_ty
= Int -> Type -> Type
forall t. (Eq t, Num t) => t -> Type -> Type
go Int
orig_ar Type
orig_ty
where
go :: t -> Type -> Type
go t
0 Type
ty = Type -> Type
f Type
ty
go t
n Type
ty | Just (TyCoBinder
arg_bndr, Type
res_ty) <- Type -> Maybe (TyCoBinder, Type)
splitPiTy_maybe Type
ty
= TyCoBinder -> Type -> Type
mkTyCoPiTy TyCoBinder
arg_bndr (t -> Type -> Type
go (t
nt -> t -> t
forall a. Num a => a -> a -> a
-t
1) Type
res_ty)
| Bool
otherwise
= String -> SDoc -> Type
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"modifyJoinResTy" (Int -> SDoc
forall a. Outputable a => a -> SDoc
ppr Int
orig_ar SDoc -> SDoc -> SDoc
<+> Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
orig_ty)
setJoinResTy :: Int
-> Type
-> Type
-> Type
setJoinResTy :: Int -> Type -> Type -> Type
setJoinResTy Int
ar Type
new_res_ty Type
ty
= Int -> (Type -> Type) -> Type -> Type
modifyJoinResTy Int
ar (Type -> Type -> Type
forall a b. a -> b -> a
const Type
new_res_ty) Type
ty
pprWithTYPE :: Type -> SDoc
pprWithTYPE :: Type -> SDoc
pprWithTYPE Type
ty = (DynFlags -> DynFlags) -> SDoc -> SDoc
updSDocDynFlags ((DynFlags -> GeneralFlag -> DynFlags)
-> GeneralFlag -> DynFlags -> DynFlags
forall a b c. (a -> b -> c) -> b -> a -> c
flip DynFlags -> GeneralFlag -> DynFlags
gopt_set GeneralFlag
Opt_PrintExplicitRuntimeReps) (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$
Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
ty