{-# LANGUAGE CPP #-}
module CoreFVs (
exprFreeVars,
exprFreeVarsDSet,
exprFreeVarsList,
exprFreeIds,
exprFreeIdsDSet,
exprFreeIdsList,
exprsFreeIdsDSet,
exprsFreeIdsList,
exprsFreeVars,
exprsFreeVarsList,
bindFreeVars,
InterestingVarFun,
exprSomeFreeVars, exprsSomeFreeVars,
exprSomeFreeVarsList, exprsSomeFreeVarsList,
varTypeTyCoVars,
varTypeTyCoFVs,
idUnfoldingVars, idFreeVars, dIdFreeVars,
bndrRuleAndUnfoldingVarsDSet,
idFVs,
idRuleVars, idRuleRhsVars, stableUnfoldingVars,
ruleRhsFreeVars, ruleFreeVars, rulesFreeVars,
rulesFreeVarsDSet,
ruleLhsFreeIds, ruleLhsFreeIdsList,
expr_fvs,
orphNamesOfType, orphNamesOfCo, orphNamesOfAxiom,
orphNamesOfTypes, orphNamesOfCoCon,
exprsOrphNames, orphNamesOfFamInst,
FVAnn,
CoreExprWithFVs,
CoreExprWithFVs',
CoreBindWithFVs,
CoreAltWithFVs,
freeVars,
freeVarsBind,
freeVarsOf,
freeVarsOfAnn
) where
#include "HsVersions.h"
import GhcPrelude
import CoreSyn
import Id
import IdInfo
import NameSet
import UniqSet
import Unique (Uniquable (..))
import Name
import VarSet
import Var
import Type
import TyCoRep
import TyCon
import CoAxiom
import FamInstEnv
import TysPrim( funTyConName )
import Maybes( orElse )
import Util
import BasicTypes( Activation )
import Outputable
import FV
exprFreeVars :: CoreExpr -> VarSet
exprFreeVars :: CoreExpr -> VarSet
exprFreeVars = FV -> VarSet
fvVarSet (FV -> VarSet) -> (CoreExpr -> FV) -> CoreExpr -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> FV
exprFVs
exprFVs :: CoreExpr -> FV
exprFVs :: CoreExpr -> FV
exprFVs = InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
isLocalVar (FV -> FV) -> (CoreExpr -> FV) -> CoreExpr -> FV
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> FV
expr_fvs
exprFreeVarsDSet :: CoreExpr -> DVarSet
exprFreeVarsDSet :: CoreExpr -> DVarSet
exprFreeVarsDSet = FV -> DVarSet
fvDVarSet (FV -> DVarSet) -> (CoreExpr -> FV) -> CoreExpr -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> FV
exprFVs
exprFreeVarsList :: CoreExpr -> [Var]
exprFreeVarsList :: CoreExpr -> [Var]
exprFreeVarsList = FV -> [Var]
fvVarList (FV -> [Var]) -> (CoreExpr -> FV) -> CoreExpr -> [Var]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> FV
exprFVs
exprFreeIds :: CoreExpr -> IdSet
exprFreeIds :: CoreExpr -> VarSet
exprFreeIds = InterestingVarFun -> CoreExpr -> VarSet
exprSomeFreeVars InterestingVarFun
isLocalId
exprFreeIdsDSet :: CoreExpr -> DIdSet
exprFreeIdsDSet :: CoreExpr -> DVarSet
exprFreeIdsDSet = InterestingVarFun -> CoreExpr -> DVarSet
exprSomeFreeVarsDSet InterestingVarFun
isLocalId
exprFreeIdsList :: CoreExpr -> [Id]
exprFreeIdsList :: CoreExpr -> [Var]
exprFreeIdsList = InterestingVarFun -> CoreExpr -> [Var]
exprSomeFreeVarsList InterestingVarFun
isLocalId
exprsFreeIdsDSet :: [CoreExpr] -> DIdSet
exprsFreeIdsDSet :: [CoreExpr] -> DVarSet
exprsFreeIdsDSet = InterestingVarFun -> [CoreExpr] -> DVarSet
exprsSomeFreeVarsDSet InterestingVarFun
isLocalId
exprsFreeIdsList :: [CoreExpr] -> [Id]
exprsFreeIdsList :: [CoreExpr] -> [Var]
exprsFreeIdsList = InterestingVarFun -> [CoreExpr] -> [Var]
exprsSomeFreeVarsList InterestingVarFun
isLocalId
exprsFreeVars :: [CoreExpr] -> VarSet
exprsFreeVars :: [CoreExpr] -> VarSet
exprsFreeVars = FV -> VarSet
fvVarSet (FV -> VarSet) -> ([CoreExpr] -> FV) -> [CoreExpr] -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreExpr] -> FV
exprsFVs
exprsFVs :: [CoreExpr] -> FV
exprsFVs :: [CoreExpr] -> FV
exprsFVs exprs :: [CoreExpr]
exprs = (CoreExpr -> FV) -> [CoreExpr] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV CoreExpr -> FV
exprFVs [CoreExpr]
exprs
exprsFreeVarsList :: [CoreExpr] -> [Var]
exprsFreeVarsList :: [CoreExpr] -> [Var]
exprsFreeVarsList = FV -> [Var]
fvVarList (FV -> [Var]) -> ([CoreExpr] -> FV) -> [CoreExpr] -> [Var]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreExpr] -> FV
exprsFVs
bindFreeVars :: CoreBind -> VarSet
bindFreeVars :: CoreBind -> VarSet
bindFreeVars (NonRec b :: Var
b r :: CoreExpr
r) = FV -> VarSet
fvVarSet (FV -> VarSet) -> FV -> VarSet
forall a b. (a -> b) -> a -> b
$ InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
isLocalVar (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ (Var, CoreExpr) -> FV
rhs_fvs (Var
b,CoreExpr
r)
bindFreeVars (Rec prs :: [(Var, CoreExpr)]
prs) = FV -> VarSet
fvVarSet (FV -> VarSet) -> FV -> VarSet
forall a b. (a -> b) -> a -> b
$ InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
isLocalVar (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$
[Var] -> FV -> FV
addBndrs (((Var, CoreExpr) -> Var) -> [(Var, CoreExpr)] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst [(Var, CoreExpr)]
prs)
(((Var, CoreExpr) -> FV) -> [(Var, CoreExpr)] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV (Var, CoreExpr) -> FV
rhs_fvs [(Var, CoreExpr)]
prs)
exprSomeFreeVars :: InterestingVarFun
-> CoreExpr
-> VarSet
exprSomeFreeVars :: InterestingVarFun -> CoreExpr -> VarSet
exprSomeFreeVars fv_cand :: InterestingVarFun
fv_cand e :: CoreExpr
e = FV -> VarSet
fvVarSet (FV -> VarSet) -> FV -> VarSet
forall a b. (a -> b) -> a -> b
$ InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
fv_cand (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ CoreExpr -> FV
expr_fvs CoreExpr
e
exprSomeFreeVarsList :: InterestingVarFun
-> CoreExpr
-> [Var]
exprSomeFreeVarsList :: InterestingVarFun -> CoreExpr -> [Var]
exprSomeFreeVarsList fv_cand :: InterestingVarFun
fv_cand e :: CoreExpr
e = FV -> [Var]
fvVarList (FV -> [Var]) -> FV -> [Var]
forall a b. (a -> b) -> a -> b
$ InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
fv_cand (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ CoreExpr -> FV
expr_fvs CoreExpr
e
exprSomeFreeVarsDSet :: InterestingVarFun
-> CoreExpr
-> DVarSet
exprSomeFreeVarsDSet :: InterestingVarFun -> CoreExpr -> DVarSet
exprSomeFreeVarsDSet fv_cand :: InterestingVarFun
fv_cand e :: CoreExpr
e = FV -> DVarSet
fvDVarSet (FV -> DVarSet) -> FV -> DVarSet
forall a b. (a -> b) -> a -> b
$ InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
fv_cand (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ CoreExpr -> FV
expr_fvs CoreExpr
e
exprsSomeFreeVars :: InterestingVarFun
-> [CoreExpr]
-> VarSet
fv_cand :: InterestingVarFun
fv_cand es :: [CoreExpr]
es =
FV -> VarSet
fvVarSet (FV -> VarSet) -> FV -> VarSet
forall a b. (a -> b) -> a -> b
$ InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
fv_cand (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ (CoreExpr -> FV) -> [CoreExpr] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV CoreExpr -> FV
expr_fvs [CoreExpr]
es
exprsSomeFreeVarsList :: InterestingVarFun
-> [CoreExpr]
-> [Var]
fv_cand :: InterestingVarFun
fv_cand es :: [CoreExpr]
es =
FV -> [Var]
fvVarList (FV -> [Var]) -> FV -> [Var]
forall a b. (a -> b) -> a -> b
$ InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
fv_cand (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ (CoreExpr -> FV) -> [CoreExpr] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV CoreExpr -> FV
expr_fvs [CoreExpr]
es
exprsSomeFreeVarsDSet :: InterestingVarFun
-> [CoreExpr]
-> DVarSet
fv_cand :: InterestingVarFun
fv_cand e :: [CoreExpr]
e =
FV -> DVarSet
fvDVarSet (FV -> DVarSet) -> FV -> DVarSet
forall a b. (a -> b) -> a -> b
$ InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
fv_cand (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ (CoreExpr -> FV) -> [CoreExpr] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV CoreExpr -> FV
expr_fvs [CoreExpr]
e
addBndr :: CoreBndr -> FV -> FV
addBndr :: Var -> FV -> FV
addBndr bndr :: Var
bndr fv :: FV
fv fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc
= (Var -> FV
varTypeTyCoFVs Var
bndr FV -> FV -> FV
`unionFV`
Var -> FV -> FV
FV.delFV Var
bndr FV
fv) InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
addBndrs :: [CoreBndr] -> FV -> FV
addBndrs :: [Var] -> FV -> FV
addBndrs bndrs :: [Var]
bndrs fv :: FV
fv = (Var -> FV -> FV) -> FV -> [Var] -> FV
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Var -> FV -> FV
addBndr FV
fv [Var]
bndrs
expr_fvs :: CoreExpr -> FV
expr_fvs :: CoreExpr -> FV
expr_fvs (Type ty :: Type
ty) fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc =
Type -> FV
tyCoFVsOfType Type
ty InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
expr_fvs (Coercion co :: Coercion
co) fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc =
Coercion -> FV
tyCoFVsOfCo Coercion
co InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
expr_fvs (Var var :: Var
var) fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc = Var -> FV
FV.unitFV Var
var InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
expr_fvs (Lit _) fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc = FV
emptyFV InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
expr_fvs (Tick t :: Tickish Var
t expr :: CoreExpr
expr) fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc =
(Tickish Var -> FV
tickish_fvs Tickish Var
t FV -> FV -> FV
`unionFV` CoreExpr -> FV
expr_fvs CoreExpr
expr) InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
expr_fvs (App fun :: CoreExpr
fun arg :: CoreExpr
arg) fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc =
(CoreExpr -> FV
expr_fvs CoreExpr
fun FV -> FV -> FV
`unionFV` CoreExpr -> FV
expr_fvs CoreExpr
arg) InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
expr_fvs (Lam bndr :: Var
bndr body :: CoreExpr
body) fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc =
Var -> FV -> FV
addBndr Var
bndr (CoreExpr -> FV
expr_fvs CoreExpr
body) InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
expr_fvs (Cast expr :: CoreExpr
expr co :: Coercion
co) fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc =
(CoreExpr -> FV
expr_fvs CoreExpr
expr FV -> FV -> FV
`unionFV` Coercion -> FV
tyCoFVsOfCo Coercion
co) InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
expr_fvs (Case scrut :: CoreExpr
scrut bndr :: Var
bndr ty :: Type
ty alts :: [Alt Var]
alts) fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc
= (CoreExpr -> FV
expr_fvs CoreExpr
scrut FV -> FV -> FV
`unionFV` Type -> FV
tyCoFVsOfType Type
ty FV -> FV -> FV
`unionFV` Var -> FV -> FV
addBndr Var
bndr
((Alt Var -> FV) -> [Alt Var] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV Alt Var -> FV
forall a. (a, [Var], CoreExpr) -> FV
alt_fvs [Alt Var]
alts)) InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
where
alt_fvs :: (a, [Var], CoreExpr) -> FV
alt_fvs (_, bndrs :: [Var]
bndrs, rhs :: CoreExpr
rhs) = [Var] -> FV -> FV
addBndrs [Var]
bndrs (CoreExpr -> FV
expr_fvs CoreExpr
rhs)
expr_fvs (Let (NonRec bndr :: Var
bndr rhs :: CoreExpr
rhs) body :: CoreExpr
body) fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc
= ((Var, CoreExpr) -> FV
rhs_fvs (Var
bndr, CoreExpr
rhs) FV -> FV -> FV
`unionFV` Var -> FV -> FV
addBndr Var
bndr (CoreExpr -> FV
expr_fvs CoreExpr
body))
InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
expr_fvs (Let (Rec pairs :: [(Var, CoreExpr)]
pairs) body :: CoreExpr
body) fv_cand :: InterestingVarFun
fv_cand in_scope :: VarSet
in_scope acc :: ([Var], VarSet)
acc
= [Var] -> FV -> FV
addBndrs (((Var, CoreExpr) -> Var) -> [(Var, CoreExpr)] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst [(Var, CoreExpr)]
pairs)
(((Var, CoreExpr) -> FV) -> [(Var, CoreExpr)] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV (Var, CoreExpr) -> FV
rhs_fvs [(Var, CoreExpr)]
pairs FV -> FV -> FV
`unionFV` CoreExpr -> FV
expr_fvs CoreExpr
body)
InterestingVarFun
fv_cand VarSet
in_scope ([Var], VarSet)
acc
rhs_fvs :: (Id, CoreExpr) -> FV
rhs_fvs :: (Var, CoreExpr) -> FV
rhs_fvs (bndr :: Var
bndr, rhs :: CoreExpr
rhs) = CoreExpr -> FV
expr_fvs CoreExpr
rhs FV -> FV -> FV
`unionFV`
Var -> FV
bndrRuleAndUnfoldingFVs Var
bndr
exprs_fvs :: [CoreExpr] -> FV
exprs_fvs :: [CoreExpr] -> FV
exprs_fvs exprs :: [CoreExpr]
exprs = (CoreExpr -> FV) -> [CoreExpr] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV CoreExpr -> FV
expr_fvs [CoreExpr]
exprs
tickish_fvs :: Tickish Id -> FV
tickish_fvs :: Tickish Var -> FV
tickish_fvs (Breakpoint _ ids :: [Var]
ids) = [Var] -> FV
FV.mkFVs [Var]
ids
tickish_fvs _ = FV
emptyFV
exprOrphNames :: CoreExpr -> NameSet
exprOrphNames :: CoreExpr -> NameSet
exprOrphNames e :: CoreExpr
e
= CoreExpr -> NameSet
go CoreExpr
e
where
go :: CoreExpr -> NameSet
go (Var v :: Var
v)
| Name -> Bool
isExternalName Name
n = Name -> NameSet
unitNameSet Name
n
| Bool
otherwise = NameSet
emptyNameSet
where n :: Name
n = Var -> Name
idName Var
v
go (Lit _) = NameSet
emptyNameSet
go (Type ty :: Type
ty) = Type -> NameSet
orphNamesOfType Type
ty
go (Coercion co :: Coercion
co) = Coercion -> NameSet
orphNamesOfCo Coercion
co
go (App e1 :: CoreExpr
e1 e2 :: CoreExpr
e2) = CoreExpr -> NameSet
go CoreExpr
e1 NameSet -> NameSet -> NameSet
`unionNameSet` CoreExpr -> NameSet
go CoreExpr
e2
go (Lam v :: Var
v e :: CoreExpr
e) = CoreExpr -> NameSet
go CoreExpr
e NameSet -> Name -> NameSet
`delFromNameSet` Var -> Name
idName Var
v
go (Tick _ e :: CoreExpr
e) = CoreExpr -> NameSet
go CoreExpr
e
go (Cast e :: CoreExpr
e co :: Coercion
co) = CoreExpr -> NameSet
go CoreExpr
e NameSet -> NameSet -> NameSet
`unionNameSet` Coercion -> NameSet
orphNamesOfCo Coercion
co
go (Let (NonRec _ r :: CoreExpr
r) e :: CoreExpr
e) = CoreExpr -> NameSet
go CoreExpr
e NameSet -> NameSet -> NameSet
`unionNameSet` CoreExpr -> NameSet
go CoreExpr
r
go (Let (Rec prs :: [(Var, CoreExpr)]
prs) e :: CoreExpr
e) = [CoreExpr] -> NameSet
exprsOrphNames (((Var, CoreExpr) -> CoreExpr) -> [(Var, CoreExpr)] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map (Var, CoreExpr) -> CoreExpr
forall a b. (a, b) -> b
snd [(Var, CoreExpr)]
prs) NameSet -> NameSet -> NameSet
`unionNameSet` CoreExpr -> NameSet
go CoreExpr
e
go (Case e :: CoreExpr
e _ ty :: Type
ty as :: [Alt Var]
as) = CoreExpr -> NameSet
go CoreExpr
e NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
ty
NameSet -> NameSet -> NameSet
`unionNameSet` [NameSet] -> NameSet
unionNameSets ((Alt Var -> NameSet) -> [Alt Var] -> [NameSet]
forall a b. (a -> b) -> [a] -> [b]
map Alt Var -> NameSet
go_alt [Alt Var]
as)
go_alt :: Alt Var -> NameSet
go_alt (_,_,r :: CoreExpr
r) = CoreExpr -> NameSet
go CoreExpr
r
exprsOrphNames :: [CoreExpr] -> NameSet
exprsOrphNames :: [CoreExpr] -> NameSet
exprsOrphNames es :: [CoreExpr]
es = (CoreExpr -> NameSet -> NameSet)
-> NameSet -> [CoreExpr] -> NameSet
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (NameSet -> NameSet -> NameSet
unionNameSet (NameSet -> NameSet -> NameSet)
-> (CoreExpr -> NameSet) -> CoreExpr -> NameSet -> NameSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> NameSet
exprOrphNames) NameSet
emptyNameSet [CoreExpr]
es
orphNamesOfTyCon :: TyCon -> NameSet
orphNamesOfTyCon :: TyCon -> NameSet
orphNamesOfTyCon tycon :: TyCon
tycon = Name -> NameSet
unitNameSet (TyCon -> Name
forall a. NamedThing a => a -> Name
getName TyCon
tycon) NameSet -> NameSet -> NameSet
`unionNameSet` case TyCon -> Maybe Class
tyConClass_maybe TyCon
tycon of
Nothing -> NameSet
emptyNameSet
Just cls :: Class
cls -> Name -> NameSet
unitNameSet (Class -> Name
forall a. NamedThing a => a -> Name
getName Class
cls)
orphNamesOfType :: Type -> NameSet
orphNamesOfType :: Type -> NameSet
orphNamesOfType ty :: Type
ty | Just ty' :: Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> NameSet
orphNamesOfType Type
ty'
orphNamesOfType (TyVarTy _) = NameSet
emptyNameSet
orphNamesOfType (LitTy {}) = NameSet
emptyNameSet
orphNamesOfType (TyConApp tycon :: TyCon
tycon tys :: [Type]
tys) = TyCon -> NameSet
orphNamesOfTyCon TyCon
tycon
NameSet -> NameSet -> NameSet
`unionNameSet` [Type] -> NameSet
orphNamesOfTypes [Type]
tys
orphNamesOfType (ForAllTy bndr :: TyCoVarBinder
bndr res :: Type
res) = Type -> NameSet
orphNamesOfType (TyCoVarBinder -> Type
forall argf. VarBndr Var argf -> Type
binderType TyCoVarBinder
bndr)
NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
res
orphNamesOfType (FunTy arg :: Type
arg res :: Type
res) = Name -> NameSet
unitNameSet Name
funTyConName
NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
arg
NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
res
orphNamesOfType (AppTy fun :: Type
fun arg :: Type
arg) = Type -> NameSet
orphNamesOfType Type
fun NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
arg
orphNamesOfType (CastTy ty :: Type
ty co :: Coercion
co) = Type -> NameSet
orphNamesOfType Type
ty NameSet -> NameSet -> NameSet
`unionNameSet` Coercion -> NameSet
orphNamesOfCo Coercion
co
orphNamesOfType (CoercionTy co :: Coercion
co) = Coercion -> NameSet
orphNamesOfCo Coercion
co
orphNamesOfThings :: (a -> NameSet) -> [a] -> NameSet
orphNamesOfThings :: (a -> NameSet) -> [a] -> NameSet
orphNamesOfThings f :: a -> NameSet
f = (a -> NameSet -> NameSet) -> NameSet -> [a] -> NameSet
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (NameSet -> NameSet -> NameSet
unionNameSet (NameSet -> NameSet -> NameSet)
-> (a -> NameSet) -> a -> NameSet -> NameSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> NameSet
f) NameSet
emptyNameSet
orphNamesOfTypes :: [Type] -> NameSet
orphNamesOfTypes :: [Type] -> NameSet
orphNamesOfTypes = (Type -> NameSet) -> [Type] -> NameSet
forall a. (a -> NameSet) -> [a] -> NameSet
orphNamesOfThings Type -> NameSet
orphNamesOfType
orphNamesOfMCo :: MCoercion -> NameSet
orphNamesOfMCo :: MCoercion -> NameSet
orphNamesOfMCo MRefl = NameSet
emptyNameSet
orphNamesOfMCo (MCo co :: Coercion
co) = Coercion -> NameSet
orphNamesOfCo Coercion
co
orphNamesOfCo :: Coercion -> NameSet
orphNamesOfCo :: Coercion -> NameSet
orphNamesOfCo (Refl ty :: Type
ty) = Type -> NameSet
orphNamesOfType Type
ty
orphNamesOfCo (GRefl _ ty :: Type
ty mco :: MCoercion
mco) = Type -> NameSet
orphNamesOfType Type
ty NameSet -> NameSet -> NameSet
`unionNameSet` MCoercion -> NameSet
orphNamesOfMCo MCoercion
mco
orphNamesOfCo (TyConAppCo _ tc :: TyCon
tc cos :: [Coercion]
cos) = Name -> NameSet
unitNameSet (TyCon -> Name
forall a. NamedThing a => a -> Name
getName TyCon
tc) NameSet -> NameSet -> NameSet
`unionNameSet` [Coercion] -> NameSet
orphNamesOfCos [Coercion]
cos
orphNamesOfCo (AppCo co1 :: Coercion
co1 co2 :: Coercion
co2) = Coercion -> NameSet
orphNamesOfCo Coercion
co1 NameSet -> NameSet -> NameSet
`unionNameSet` Coercion -> NameSet
orphNamesOfCo Coercion
co2
orphNamesOfCo (ForAllCo _ kind_co :: Coercion
kind_co co :: Coercion
co)
= Coercion -> NameSet
orphNamesOfCo Coercion
kind_co NameSet -> NameSet -> NameSet
`unionNameSet` Coercion -> NameSet
orphNamesOfCo Coercion
co
orphNamesOfCo (FunCo _ co1 :: Coercion
co1 co2 :: Coercion
co2) = Coercion -> NameSet
orphNamesOfCo Coercion
co1 NameSet -> NameSet -> NameSet
`unionNameSet` Coercion -> NameSet
orphNamesOfCo Coercion
co2
orphNamesOfCo (CoVarCo _) = NameSet
emptyNameSet
orphNamesOfCo (AxiomInstCo con :: CoAxiom Branched
con _ cos :: [Coercion]
cos) = CoAxiom Branched -> NameSet
forall (br :: BranchFlag). CoAxiom br -> NameSet
orphNamesOfCoCon CoAxiom Branched
con NameSet -> NameSet -> NameSet
`unionNameSet` [Coercion] -> NameSet
orphNamesOfCos [Coercion]
cos
orphNamesOfCo (UnivCo p :: UnivCoProvenance
p _ t1 :: Type
t1 t2 :: Type
t2) = UnivCoProvenance -> NameSet
orphNamesOfProv UnivCoProvenance
p NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
t1 NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
t2
orphNamesOfCo (SymCo co :: Coercion
co) = Coercion -> NameSet
orphNamesOfCo Coercion
co
orphNamesOfCo (TransCo co1 :: Coercion
co1 co2 :: Coercion
co2) = Coercion -> NameSet
orphNamesOfCo Coercion
co1 NameSet -> NameSet -> NameSet
`unionNameSet` Coercion -> NameSet
orphNamesOfCo Coercion
co2
orphNamesOfCo (NthCo _ _ co :: Coercion
co) = Coercion -> NameSet
orphNamesOfCo Coercion
co
orphNamesOfCo (LRCo _ co :: Coercion
co) = Coercion -> NameSet
orphNamesOfCo Coercion
co
orphNamesOfCo (InstCo co :: Coercion
co arg :: Coercion
arg) = Coercion -> NameSet
orphNamesOfCo Coercion
co NameSet -> NameSet -> NameSet
`unionNameSet` Coercion -> NameSet
orphNamesOfCo Coercion
arg
orphNamesOfCo (KindCo co :: Coercion
co) = Coercion -> NameSet
orphNamesOfCo Coercion
co
orphNamesOfCo (SubCo co :: Coercion
co) = Coercion -> NameSet
orphNamesOfCo Coercion
co
orphNamesOfCo (AxiomRuleCo _ cs :: [Coercion]
cs) = [Coercion] -> NameSet
orphNamesOfCos [Coercion]
cs
orphNamesOfCo (HoleCo _) = NameSet
emptyNameSet
orphNamesOfProv :: UnivCoProvenance -> NameSet
orphNamesOfProv :: UnivCoProvenance -> NameSet
orphNamesOfProv UnsafeCoerceProv = NameSet
emptyNameSet
orphNamesOfProv (PhantomProv co :: Coercion
co) = Coercion -> NameSet
orphNamesOfCo Coercion
co
orphNamesOfProv (ProofIrrelProv co :: Coercion
co) = Coercion -> NameSet
orphNamesOfCo Coercion
co
orphNamesOfProv (PluginProv _) = NameSet
emptyNameSet
orphNamesOfCos :: [Coercion] -> NameSet
orphNamesOfCos :: [Coercion] -> NameSet
orphNamesOfCos = (Coercion -> NameSet) -> [Coercion] -> NameSet
forall a. (a -> NameSet) -> [a] -> NameSet
orphNamesOfThings Coercion -> NameSet
orphNamesOfCo
orphNamesOfCoCon :: CoAxiom br -> NameSet
orphNamesOfCoCon :: CoAxiom br -> NameSet
orphNamesOfCoCon (CoAxiom { co_ax_tc :: forall (br :: BranchFlag). CoAxiom br -> TyCon
co_ax_tc = TyCon
tc, co_ax_branches :: forall (br :: BranchFlag). CoAxiom br -> Branches br
co_ax_branches = Branches br
branches })
= TyCon -> NameSet
orphNamesOfTyCon TyCon
tc NameSet -> NameSet -> NameSet
`unionNameSet` Branches br -> NameSet
forall (br :: BranchFlag). Branches br -> NameSet
orphNamesOfCoAxBranches Branches br
branches
orphNamesOfAxiom :: CoAxiom br -> NameSet
orphNamesOfAxiom :: CoAxiom br -> NameSet
orphNamesOfAxiom axiom :: CoAxiom br
axiom
= [Type] -> NameSet
orphNamesOfTypes ((CoAxBranch -> [Type]) -> [CoAxBranch] -> [Type]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CoAxBranch -> [Type]
coAxBranchLHS ([CoAxBranch] -> [Type]) -> [CoAxBranch] -> [Type]
forall a b. (a -> b) -> a -> b
$ Branches br -> [CoAxBranch]
forall (br :: BranchFlag). Branches br -> [CoAxBranch]
fromBranches (Branches br -> [CoAxBranch]) -> Branches br -> [CoAxBranch]
forall a b. (a -> b) -> a -> b
$ CoAxiom br -> Branches br
forall (br :: BranchFlag). CoAxiom br -> Branches br
coAxiomBranches CoAxiom br
axiom)
NameSet -> Name -> NameSet
`extendNameSet` TyCon -> Name
forall a. NamedThing a => a -> Name
getName (CoAxiom br -> TyCon
forall (br :: BranchFlag). CoAxiom br -> TyCon
coAxiomTyCon CoAxiom br
axiom)
orphNamesOfCoAxBranches :: Branches br -> NameSet
orphNamesOfCoAxBranches :: Branches br -> NameSet
orphNamesOfCoAxBranches
= (CoAxBranch -> NameSet -> NameSet)
-> NameSet -> [CoAxBranch] -> NameSet
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (NameSet -> NameSet -> NameSet
unionNameSet (NameSet -> NameSet -> NameSet)
-> (CoAxBranch -> NameSet) -> CoAxBranch -> NameSet -> NameSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoAxBranch -> NameSet
orphNamesOfCoAxBranch) NameSet
emptyNameSet ([CoAxBranch] -> NameSet)
-> (Branches br -> [CoAxBranch]) -> Branches br -> NameSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Branches br -> [CoAxBranch]
forall (br :: BranchFlag). Branches br -> [CoAxBranch]
fromBranches
orphNamesOfCoAxBranch :: CoAxBranch -> NameSet
orphNamesOfCoAxBranch :: CoAxBranch -> NameSet
orphNamesOfCoAxBranch (CoAxBranch { cab_lhs :: CoAxBranch -> [Type]
cab_lhs = [Type]
lhs, cab_rhs :: CoAxBranch -> Type
cab_rhs = Type
rhs })
= [Type] -> NameSet
orphNamesOfTypes [Type]
lhs NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
rhs
orphNamesOfFamInst :: FamInst -> NameSet
orphNamesOfFamInst :: FamInst -> NameSet
orphNamesOfFamInst fam_inst :: FamInst
fam_inst = CoAxiom Unbranched -> NameSet
forall (br :: BranchFlag). CoAxiom br -> NameSet
orphNamesOfAxiom (FamInst -> CoAxiom Unbranched
famInstAxiom FamInst
fam_inst)
ruleRhsFreeVars :: CoreRule -> VarSet
ruleRhsFreeVars :: CoreRule -> VarSet
ruleRhsFreeVars (BuiltinRule {}) = VarSet
noFVs
ruleRhsFreeVars (Rule { ru_fn :: CoreRule -> Name
ru_fn = Name
_, ru_bndrs :: CoreRule -> [Var]
ru_bndrs = [Var]
bndrs, ru_rhs :: CoreRule -> CoreExpr
ru_rhs = CoreExpr
rhs })
= FV -> VarSet
fvVarSet (FV -> VarSet) -> FV -> VarSet
forall a b. (a -> b) -> a -> b
$ InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
isLocalVar (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ [Var] -> FV -> FV
addBndrs [Var]
bndrs (CoreExpr -> FV
expr_fvs CoreExpr
rhs)
ruleFreeVars :: CoreRule -> VarSet
ruleFreeVars :: CoreRule -> VarSet
ruleFreeVars = FV -> VarSet
fvVarSet (FV -> VarSet) -> (CoreRule -> FV) -> CoreRule -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreRule -> FV
ruleFVs
ruleFVs :: CoreRule -> FV
ruleFVs :: CoreRule -> FV
ruleFVs (BuiltinRule {}) = FV
emptyFV
ruleFVs (Rule { ru_fn :: CoreRule -> Name
ru_fn = Name
_do_not_include
, ru_bndrs :: CoreRule -> [Var]
ru_bndrs = [Var]
bndrs
, ru_rhs :: CoreRule -> CoreExpr
ru_rhs = CoreExpr
rhs, ru_args :: CoreRule -> [CoreExpr]
ru_args = [CoreExpr]
args })
= InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
isLocalVar (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ [Var] -> FV -> FV
addBndrs [Var]
bndrs ([CoreExpr] -> FV
exprs_fvs (CoreExpr
rhsCoreExpr -> [CoreExpr] -> [CoreExpr]
forall a. a -> [a] -> [a]
:[CoreExpr]
args))
rulesFVs :: [CoreRule] -> FV
rulesFVs :: [CoreRule] -> FV
rulesFVs = (CoreRule -> FV) -> [CoreRule] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV CoreRule -> FV
ruleFVs
rulesFreeVarsDSet :: [CoreRule] -> DVarSet
rulesFreeVarsDSet :: [CoreRule] -> DVarSet
rulesFreeVarsDSet rules :: [CoreRule]
rules = FV -> DVarSet
fvDVarSet (FV -> DVarSet) -> FV -> DVarSet
forall a b. (a -> b) -> a -> b
$ [CoreRule] -> FV
rulesFVs [CoreRule]
rules
idRuleRhsVars :: (Activation -> Bool) -> Id -> VarSet
idRuleRhsVars :: (Activation -> Bool) -> Var -> VarSet
idRuleRhsVars is_active :: Activation -> Bool
is_active id :: Var
id
= (CoreRule -> VarSet) -> [CoreRule] -> VarSet
forall a. (a -> VarSet) -> [a] -> VarSet
mapUnionVarSet CoreRule -> VarSet
get_fvs (Var -> [CoreRule]
idCoreRules Var
id)
where
get_fvs :: CoreRule -> VarSet
get_fvs (Rule { ru_fn :: CoreRule -> Name
ru_fn = Name
fn, ru_bndrs :: CoreRule -> [Var]
ru_bndrs = [Var]
bndrs
, ru_rhs :: CoreRule -> CoreExpr
ru_rhs = CoreExpr
rhs, ru_act :: CoreRule -> Activation
ru_act = Activation
act })
| Activation -> Bool
is_active Activation
act
= VarSet -> Unique -> VarSet
forall a. UniqSet a -> Unique -> UniqSet a
delOneFromUniqSet_Directly VarSet
fvs (Name -> Unique
forall a. Uniquable a => a -> Unique
getUnique Name
fn)
where
fvs :: VarSet
fvs = FV -> VarSet
fvVarSet (FV -> VarSet) -> FV -> VarSet
forall a b. (a -> b) -> a -> b
$ InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
isLocalVar (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ [Var] -> FV -> FV
addBndrs [Var]
bndrs (CoreExpr -> FV
expr_fvs CoreExpr
rhs)
get_fvs _ = VarSet
noFVs
rulesFreeVars :: [CoreRule] -> VarSet
rulesFreeVars :: [CoreRule] -> VarSet
rulesFreeVars rules :: [CoreRule]
rules = (CoreRule -> VarSet) -> [CoreRule] -> VarSet
forall a. (a -> VarSet) -> [a] -> VarSet
mapUnionVarSet CoreRule -> VarSet
ruleFreeVars [CoreRule]
rules
ruleLhsFreeIds :: CoreRule -> VarSet
ruleLhsFreeIds :: CoreRule -> VarSet
ruleLhsFreeIds = FV -> VarSet
fvVarSet (FV -> VarSet) -> (CoreRule -> FV) -> CoreRule -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreRule -> FV
ruleLhsFVIds
ruleLhsFreeIdsList :: CoreRule -> [Var]
ruleLhsFreeIdsList :: CoreRule -> [Var]
ruleLhsFreeIdsList = FV -> [Var]
fvVarList (FV -> [Var]) -> (CoreRule -> FV) -> CoreRule -> [Var]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreRule -> FV
ruleLhsFVIds
ruleLhsFVIds :: CoreRule -> FV
ruleLhsFVIds :: CoreRule -> FV
ruleLhsFVIds (BuiltinRule {}) = FV
emptyFV
ruleLhsFVIds (Rule { ru_bndrs :: CoreRule -> [Var]
ru_bndrs = [Var]
bndrs, ru_args :: CoreRule -> [CoreExpr]
ru_args = [CoreExpr]
args })
= InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
isLocalId (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ [Var] -> FV -> FV
addBndrs [Var]
bndrs ([CoreExpr] -> FV
exprs_fvs [CoreExpr]
args)
type FVAnn = DVarSet
type CoreBindWithFVs = AnnBind Id FVAnn
type CoreExprWithFVs = AnnExpr Id FVAnn
type CoreExprWithFVs' = AnnExpr' Id FVAnn
type CoreAltWithFVs = AnnAlt Id FVAnn
freeVarsOf :: CoreExprWithFVs -> DIdSet
freeVarsOf :: CoreExprWithFVs -> DVarSet
freeVarsOf (fvs :: DVarSet
fvs, _) = DVarSet
fvs
freeVarsOfAnn :: FVAnn -> DIdSet
freeVarsOfAnn :: DVarSet -> DVarSet
freeVarsOfAnn fvs :: DVarSet
fvs = DVarSet
fvs
noFVs :: VarSet
noFVs :: VarSet
noFVs = VarSet
emptyVarSet
aFreeVar :: Var -> DVarSet
aFreeVar :: Var -> DVarSet
aFreeVar = Var -> DVarSet
unitDVarSet
unionFVs :: DVarSet -> DVarSet -> DVarSet
unionFVs :: DVarSet -> DVarSet -> DVarSet
unionFVs = DVarSet -> DVarSet -> DVarSet
unionDVarSet
unionFVss :: [DVarSet] -> DVarSet
unionFVss :: [DVarSet] -> DVarSet
unionFVss = [DVarSet] -> DVarSet
unionDVarSets
delBindersFV :: [Var] -> DVarSet -> DVarSet
delBindersFV :: [Var] -> DVarSet -> DVarSet
delBindersFV bs :: [Var]
bs fvs :: DVarSet
fvs = (Var -> DVarSet -> DVarSet) -> DVarSet -> [Var] -> DVarSet
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Var -> DVarSet -> DVarSet
delBinderFV DVarSet
fvs [Var]
bs
delBinderFV :: Var -> DVarSet -> DVarSet
delBinderFV :: Var -> DVarSet -> DVarSet
delBinderFV b :: Var
b s :: DVarSet
s = (DVarSet
s DVarSet -> Var -> DVarSet
`delDVarSet` Var
b) DVarSet -> DVarSet -> DVarSet
`unionFVs` Var -> DVarSet
dVarTypeTyCoVars Var
b
varTypeTyCoVars :: Var -> TyCoVarSet
varTypeTyCoVars :: Var -> VarSet
varTypeTyCoVars var :: Var
var = FV -> VarSet
fvVarSet (FV -> VarSet) -> FV -> VarSet
forall a b. (a -> b) -> a -> b
$ Var -> FV
varTypeTyCoFVs Var
var
dVarTypeTyCoVars :: Var -> DTyCoVarSet
dVarTypeTyCoVars :: Var -> DVarSet
dVarTypeTyCoVars var :: Var
var = FV -> DVarSet
fvDVarSet (FV -> DVarSet) -> FV -> DVarSet
forall a b. (a -> b) -> a -> b
$ Var -> FV
varTypeTyCoFVs Var
var
varTypeTyCoFVs :: Var -> FV
varTypeTyCoFVs :: Var -> FV
varTypeTyCoFVs var :: Var
var = Type -> FV
tyCoFVsOfType (Var -> Type
varType Var
var)
idFreeVars :: Id -> VarSet
idFreeVars :: Var -> VarSet
idFreeVars id :: Var
id = ASSERT( isId id) fvVarSet $ idFVs id
dIdFreeVars :: Id -> DVarSet
dIdFreeVars :: Var -> DVarSet
dIdFreeVars id :: Var
id = FV -> DVarSet
fvDVarSet (FV -> DVarSet) -> FV -> DVarSet
forall a b. (a -> b) -> a -> b
$ Var -> FV
idFVs Var
id
idFVs :: Id -> FV
idFVs :: Var -> FV
idFVs id :: Var
id = ASSERT( isId id)
Var -> FV
varTypeTyCoFVs Var
id FV -> FV -> FV
`unionFV`
Var -> FV
bndrRuleAndUnfoldingFVs Var
id
bndrRuleAndUnfoldingVarsDSet :: Id -> DVarSet
bndrRuleAndUnfoldingVarsDSet :: Var -> DVarSet
bndrRuleAndUnfoldingVarsDSet id :: Var
id = FV -> DVarSet
fvDVarSet (FV -> DVarSet) -> FV -> DVarSet
forall a b. (a -> b) -> a -> b
$ Var -> FV
bndrRuleAndUnfoldingFVs Var
id
bndrRuleAndUnfoldingFVs :: Id -> FV
bndrRuleAndUnfoldingFVs :: Var -> FV
bndrRuleAndUnfoldingFVs id :: Var
id
| InterestingVarFun
isId Var
id = Var -> FV
idRuleFVs Var
id FV -> FV -> FV
`unionFV` Var -> FV
idUnfoldingFVs Var
id
| Bool
otherwise = FV
emptyFV
idRuleVars ::Id -> VarSet
idRuleVars :: Var -> VarSet
idRuleVars id :: Var
id = FV -> VarSet
fvVarSet (FV -> VarSet) -> FV -> VarSet
forall a b. (a -> b) -> a -> b
$ Var -> FV
idRuleFVs Var
id
idRuleFVs :: Id -> FV
idRuleFVs :: Var -> FV
idRuleFVs id :: Var
id = ASSERT( isId id)
[Var] -> FV
FV.mkFVs (DVarSet -> [Var]
dVarSetElems (DVarSet -> [Var]) -> DVarSet -> [Var]
forall a b. (a -> b) -> a -> b
$ RuleInfo -> DVarSet
ruleInfoFreeVars (Var -> RuleInfo
idSpecialisation Var
id))
idUnfoldingVars :: Id -> VarSet
idUnfoldingVars :: Var -> VarSet
idUnfoldingVars id :: Var
id = FV -> VarSet
fvVarSet (FV -> VarSet) -> FV -> VarSet
forall a b. (a -> b) -> a -> b
$ Var -> FV
idUnfoldingFVs Var
id
idUnfoldingFVs :: Id -> FV
idUnfoldingFVs :: Var -> FV
idUnfoldingFVs id :: Var
id = Unfolding -> Maybe FV
stableUnfoldingFVs (Var -> Unfolding
realIdUnfolding Var
id) Maybe FV -> FV -> FV
forall a. Maybe a -> a -> a
`orElse` FV
emptyFV
stableUnfoldingVars :: Unfolding -> Maybe VarSet
stableUnfoldingVars :: Unfolding -> Maybe VarSet
stableUnfoldingVars unf :: Unfolding
unf = FV -> VarSet
fvVarSet (FV -> VarSet) -> Maybe FV -> Maybe VarSet
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Unfolding -> Maybe FV
stableUnfoldingFVs Unfolding
unf
stableUnfoldingFVs :: Unfolding -> Maybe FV
stableUnfoldingFVs :: Unfolding -> Maybe FV
stableUnfoldingFVs unf :: Unfolding
unf
= case Unfolding
unf of
CoreUnfolding { uf_tmpl :: Unfolding -> CoreExpr
uf_tmpl = CoreExpr
rhs, uf_src :: Unfolding -> UnfoldingSource
uf_src = UnfoldingSource
src }
| UnfoldingSource -> Bool
isStableSource UnfoldingSource
src
-> FV -> Maybe FV
forall a. a -> Maybe a
Just (InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
isLocalVar (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ CoreExpr -> FV
expr_fvs CoreExpr
rhs)
DFunUnfolding { df_bndrs :: Unfolding -> [Var]
df_bndrs = [Var]
bndrs, df_args :: Unfolding -> [CoreExpr]
df_args = [CoreExpr]
args }
-> FV -> Maybe FV
forall a. a -> Maybe a
Just (InterestingVarFun -> FV -> FV
filterFV InterestingVarFun
isLocalVar (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ VarSet -> FV -> FV
FV.delFVs ([Var] -> VarSet
mkVarSet [Var]
bndrs) (FV -> FV) -> FV -> FV
forall a b. (a -> b) -> a -> b
$ [CoreExpr] -> FV
exprs_fvs [CoreExpr]
args)
_other :: Unfolding
_other -> Maybe FV
forall a. Maybe a
Nothing
freeVarsBind :: CoreBind
-> DVarSet
-> (CoreBindWithFVs, DVarSet)
freeVarsBind :: CoreBind -> DVarSet -> (CoreBindWithFVs, DVarSet)
freeVarsBind (NonRec binder :: Var
binder rhs :: CoreExpr
rhs) body_fvs :: DVarSet
body_fvs
= ( Var -> CoreExprWithFVs -> CoreBindWithFVs
forall bndr annot. bndr -> AnnExpr bndr annot -> AnnBind bndr annot
AnnNonRec Var
binder CoreExprWithFVs
rhs2
, CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
rhs2 DVarSet -> DVarSet -> DVarSet
`unionFVs` DVarSet
body_fvs2
DVarSet -> DVarSet -> DVarSet
`unionFVs` Var -> DVarSet
bndrRuleAndUnfoldingVarsDSet Var
binder )
where
rhs2 :: CoreExprWithFVs
rhs2 = CoreExpr -> CoreExprWithFVs
freeVars CoreExpr
rhs
body_fvs2 :: DVarSet
body_fvs2 = Var
binder Var -> DVarSet -> DVarSet
`delBinderFV` DVarSet
body_fvs
freeVarsBind (Rec binds :: [(Var, CoreExpr)]
binds) body_fvs :: DVarSet
body_fvs
= ( [(Var, CoreExprWithFVs)] -> CoreBindWithFVs
forall bndr annot.
[(bndr, AnnExpr bndr annot)] -> AnnBind bndr annot
AnnRec ([Var]
binders [Var] -> [CoreExprWithFVs] -> [(Var, CoreExprWithFVs)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [CoreExprWithFVs]
rhss2)
, [Var] -> DVarSet -> DVarSet
delBindersFV [Var]
binders DVarSet
all_fvs )
where
(binders :: [Var]
binders, rhss :: [CoreExpr]
rhss) = [(Var, CoreExpr)] -> ([Var], [CoreExpr])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Var, CoreExpr)]
binds
rhss2 :: [CoreExprWithFVs]
rhss2 = (CoreExpr -> CoreExprWithFVs) -> [CoreExpr] -> [CoreExprWithFVs]
forall a b. (a -> b) -> [a] -> [b]
map CoreExpr -> CoreExprWithFVs
freeVars [CoreExpr]
rhss
rhs_body_fvs :: DVarSet
rhs_body_fvs = (CoreExprWithFVs -> DVarSet -> DVarSet)
-> DVarSet -> [CoreExprWithFVs] -> DVarSet
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (DVarSet -> DVarSet -> DVarSet
unionFVs (DVarSet -> DVarSet -> DVarSet)
-> (CoreExprWithFVs -> DVarSet)
-> CoreExprWithFVs
-> DVarSet
-> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExprWithFVs -> DVarSet
freeVarsOf) DVarSet
body_fvs [CoreExprWithFVs]
rhss2
binders_fvs :: DVarSet
binders_fvs = FV -> DVarSet
fvDVarSet (FV -> DVarSet) -> FV -> DVarSet
forall a b. (a -> b) -> a -> b
$ (Var -> FV) -> [Var] -> FV
forall a. (a -> FV) -> [a] -> FV
mapUnionFV Var -> FV
bndrRuleAndUnfoldingFVs [Var]
binders
all_fvs :: DVarSet
all_fvs = DVarSet
rhs_body_fvs DVarSet -> DVarSet -> DVarSet
`unionFVs` DVarSet
binders_fvs
freeVars :: CoreExpr -> CoreExprWithFVs
freeVars :: CoreExpr -> CoreExprWithFVs
freeVars = CoreExpr -> CoreExprWithFVs
go
where
go :: CoreExpr -> CoreExprWithFVs
go :: CoreExpr -> CoreExprWithFVs
go (Var v :: Var
v)
| InterestingVarFun
isLocalVar Var
v = (Var -> DVarSet
aFreeVar Var
v DVarSet -> DVarSet -> DVarSet
`unionFVs` DVarSet
ty_fvs, Var -> AnnExpr' Var DVarSet
forall bndr annot. Var -> AnnExpr' bndr annot
AnnVar Var
v)
| Bool
otherwise = (DVarSet
emptyDVarSet, Var -> AnnExpr' Var DVarSet
forall bndr annot. Var -> AnnExpr' bndr annot
AnnVar Var
v)
where
ty_fvs :: DVarSet
ty_fvs = Var -> DVarSet
dVarTypeTyCoVars Var
v
go (Lit lit :: Literal
lit) = (DVarSet
emptyDVarSet, Literal -> AnnExpr' Var DVarSet
forall bndr annot. Literal -> AnnExpr' bndr annot
AnnLit Literal
lit)
go (Lam b :: Var
b body :: CoreExpr
body)
= ( DVarSet
b_fvs DVarSet -> DVarSet -> DVarSet
`unionFVs` (Var
b Var -> DVarSet -> DVarSet
`delBinderFV` DVarSet
body_fvs)
, Var -> CoreExprWithFVs -> AnnExpr' Var DVarSet
forall bndr annot.
bndr -> AnnExpr bndr annot -> AnnExpr' bndr annot
AnnLam Var
b CoreExprWithFVs
body' )
where
body' :: CoreExprWithFVs
body'@(body_fvs :: DVarSet
body_fvs, _) = CoreExpr -> CoreExprWithFVs
go CoreExpr
body
b_ty :: Type
b_ty = Var -> Type
idType Var
b
b_fvs :: DVarSet
b_fvs = Type -> DVarSet
tyCoVarsOfTypeDSet Type
b_ty
go (App fun :: CoreExpr
fun arg :: CoreExpr
arg)
= ( CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
fun' DVarSet -> DVarSet -> DVarSet
`unionFVs` CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
arg'
, CoreExprWithFVs -> CoreExprWithFVs -> AnnExpr' Var DVarSet
forall bndr annot.
AnnExpr bndr annot -> AnnExpr bndr annot -> AnnExpr' bndr annot
AnnApp CoreExprWithFVs
fun' CoreExprWithFVs
arg' )
where
fun' :: CoreExprWithFVs
fun' = CoreExpr -> CoreExprWithFVs
go CoreExpr
fun
arg' :: CoreExprWithFVs
arg' = CoreExpr -> CoreExprWithFVs
go CoreExpr
arg
go (Case scrut :: CoreExpr
scrut bndr :: Var
bndr ty :: Type
ty alts :: [Alt Var]
alts)
= ( (Var
bndr Var -> DVarSet -> DVarSet
`delBinderFV` DVarSet
alts_fvs)
DVarSet -> DVarSet -> DVarSet
`unionFVs` CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
scrut2
DVarSet -> DVarSet -> DVarSet
`unionFVs` Type -> DVarSet
tyCoVarsOfTypeDSet Type
ty
, CoreExprWithFVs
-> Var -> Type -> [AnnAlt Var DVarSet] -> AnnExpr' Var DVarSet
forall bndr annot.
AnnExpr bndr annot
-> bndr -> Type -> [AnnAlt bndr annot] -> AnnExpr' bndr annot
AnnCase CoreExprWithFVs
scrut2 Var
bndr Type
ty [AnnAlt Var DVarSet]
alts2 )
where
scrut2 :: CoreExprWithFVs
scrut2 = CoreExpr -> CoreExprWithFVs
go CoreExpr
scrut
(alts_fvs_s :: [DVarSet]
alts_fvs_s, alts2 :: [AnnAlt Var DVarSet]
alts2) = (Alt Var -> (DVarSet, AnnAlt Var DVarSet))
-> [Alt Var] -> ([DVarSet], [AnnAlt Var DVarSet])
forall a b c. (a -> (b, c)) -> [a] -> ([b], [c])
mapAndUnzip Alt Var -> (DVarSet, AnnAlt Var DVarSet)
forall a.
(a, [Var], CoreExpr) -> (DVarSet, (a, [Var], CoreExprWithFVs))
fv_alt [Alt Var]
alts
alts_fvs :: DVarSet
alts_fvs = [DVarSet] -> DVarSet
unionFVss [DVarSet]
alts_fvs_s
fv_alt :: (a, [Var], CoreExpr) -> (DVarSet, (a, [Var], CoreExprWithFVs))
fv_alt (con :: a
con,args :: [Var]
args,rhs :: CoreExpr
rhs) = ([Var] -> DVarSet -> DVarSet
delBindersFV [Var]
args (CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
rhs2),
(a
con, [Var]
args, CoreExprWithFVs
rhs2))
where
rhs2 :: CoreExprWithFVs
rhs2 = CoreExpr -> CoreExprWithFVs
go CoreExpr
rhs
go (Let bind :: CoreBind
bind body :: CoreExpr
body)
= (DVarSet
bind_fvs, CoreBindWithFVs -> CoreExprWithFVs -> AnnExpr' Var DVarSet
forall bndr annot.
AnnBind bndr annot -> AnnExpr bndr annot -> AnnExpr' bndr annot
AnnLet CoreBindWithFVs
bind2 CoreExprWithFVs
body2)
where
(bind2 :: CoreBindWithFVs
bind2, bind_fvs :: DVarSet
bind_fvs) = CoreBind -> DVarSet -> (CoreBindWithFVs, DVarSet)
freeVarsBind CoreBind
bind (CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
body2)
body2 :: CoreExprWithFVs
body2 = CoreExpr -> CoreExprWithFVs
go CoreExpr
body
go (Cast expr :: CoreExpr
expr co :: Coercion
co)
= ( CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
expr2 DVarSet -> DVarSet -> DVarSet
`unionFVs` DVarSet
cfvs
, CoreExprWithFVs -> (DVarSet, Coercion) -> AnnExpr' Var DVarSet
forall bndr annot.
AnnExpr bndr annot -> (annot, Coercion) -> AnnExpr' bndr annot
AnnCast CoreExprWithFVs
expr2 (DVarSet
cfvs, Coercion
co) )
where
expr2 :: CoreExprWithFVs
expr2 = CoreExpr -> CoreExprWithFVs
go CoreExpr
expr
cfvs :: DVarSet
cfvs = Coercion -> DVarSet
tyCoVarsOfCoDSet Coercion
co
go (Tick tickish :: Tickish Var
tickish expr :: CoreExpr
expr)
= ( Tickish Var -> DVarSet
tickishFVs Tickish Var
tickish DVarSet -> DVarSet -> DVarSet
`unionFVs` CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
expr2
, Tickish Var -> CoreExprWithFVs -> AnnExpr' Var DVarSet
forall bndr annot.
Tickish Var -> AnnExpr bndr annot -> AnnExpr' bndr annot
AnnTick Tickish Var
tickish CoreExprWithFVs
expr2 )
where
expr2 :: CoreExprWithFVs
expr2 = CoreExpr -> CoreExprWithFVs
go CoreExpr
expr
tickishFVs :: Tickish Var -> DVarSet
tickishFVs (Breakpoint _ ids :: [Var]
ids) = [Var] -> DVarSet
mkDVarSet [Var]
ids
tickishFVs _ = DVarSet
emptyDVarSet
go (Type ty :: Type
ty) = (Type -> DVarSet
tyCoVarsOfTypeDSet Type
ty, Type -> AnnExpr' Var DVarSet
forall bndr annot. Type -> AnnExpr' bndr annot
AnnType Type
ty)
go (Coercion co :: Coercion
co) = (Coercion -> DVarSet
tyCoVarsOfCoDSet Coercion
co, Coercion -> AnnExpr' Var DVarSet
forall bndr annot. Coercion -> AnnExpr' bndr annot
AnnCoercion Coercion
co)