{-# LANGUAGE DeriveFunctor #-}
module GHC.Driver.Env.KnotVars( KnotVars(..)
, emptyKnotVars
, knotVarsFromModuleEnv
, knotVarElems
, lookupKnotVars
, knotVarsWithout
) where
import GHC.Prelude
import GHC.Unit.Types ( Module )
import GHC.Unit.Module.Env
import Data.Maybe
import GHC.Utils.Outputable
data KnotVars a = KnotVars { forall a. KnotVars a -> [Module]
kv_domain :: [Module]
, forall a. KnotVars a -> Module -> Maybe a
kv_lookup :: Module -> Maybe a
}
| NoKnotVars
deriving forall a b. a -> KnotVars b -> KnotVars a
forall a b. (a -> b) -> KnotVars a -> KnotVars b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> KnotVars b -> KnotVars a
$c<$ :: forall a b. a -> KnotVars b -> KnotVars a
fmap :: forall a b. (a -> b) -> KnotVars a -> KnotVars b
$cfmap :: forall a b. (a -> b) -> KnotVars a -> KnotVars b
Functor
instance Outputable (KnotVars a) where
ppr :: KnotVars a -> SDoc
ppr KnotVars a
NoKnotVars = forall doc. IsLine doc => String -> doc
text String
"NoKnot"
ppr (KnotVars [Module]
dom Module -> Maybe a
_lookup) = forall doc. IsLine doc => String -> doc
text String
"Knotty:" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr [Module]
dom
emptyKnotVars :: KnotVars a
emptyKnotVars :: forall a. KnotVars a
emptyKnotVars = forall a. KnotVars a
NoKnotVars
knotVarsFromModuleEnv :: ModuleEnv a -> KnotVars a
knotVarsFromModuleEnv :: forall a. ModuleEnv a -> KnotVars a
knotVarsFromModuleEnv ModuleEnv a
me | forall a. ModuleEnv a -> Bool
isEmptyModuleEnv ModuleEnv a
me = forall a. KnotVars a
NoKnotVars
knotVarsFromModuleEnv ModuleEnv a
me = forall a. [Module] -> (Module -> Maybe a) -> KnotVars a
KnotVars (forall a. ModuleEnv a -> [Module]
moduleEnvKeys ModuleEnv a
me) (forall a. ModuleEnv a -> Module -> Maybe a
lookupModuleEnv ModuleEnv a
me)
knotVarElems :: KnotVars a -> [a]
knotVarElems :: forall a. KnotVars a -> [a]
knotVarElems (KnotVars [Module]
keys Module -> Maybe a
lookup) = forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Module -> Maybe a
lookup [Module]
keys
knotVarElems KnotVars a
NoKnotVars = []
lookupKnotVars :: KnotVars a -> Module -> Maybe a
lookupKnotVars :: forall a. KnotVars a -> Module -> Maybe a
lookupKnotVars (KnotVars [Module]
_ Module -> Maybe a
lookup) Module
x = Module -> Maybe a
lookup Module
x
lookupKnotVars KnotVars a
NoKnotVars Module
_ = forall a. Maybe a
Nothing
knotVarsWithout :: Module -> KnotVars a -> KnotVars a
knotVarsWithout :: forall a. Module -> KnotVars a -> KnotVars a
knotVarsWithout Module
this_mod (KnotVars [Module]
loop_mods Module -> Maybe a
lkup) = forall a. [Module] -> (Module -> Maybe a) -> KnotVars a
KnotVars
(forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => a -> a -> Bool
/= Module
this_mod) [Module]
loop_mods)
(\Module
that_mod -> if Module
that_mod forall a. Eq a => a -> a -> Bool
== Module
this_mod then forall a. Maybe a
Nothing else Module -> Maybe a
lkup Module
that_mod)
knotVarsWithout Module
_ KnotVars a
NoKnotVars = forall a. KnotVars a
NoKnotVars