{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE PatternGuards #-}
module Distribution.Backpack.ModSubst (
ModSubst(..),
) where
import Prelude ()
import Distribution.Compat.Prelude hiding (mod)
import Distribution.ModuleName
import Distribution.Backpack
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
class ModSubst a where
modSubst :: OpenModuleSubst -> a -> a
instance ModSubst OpenModule where
modSubst :: OpenModuleSubst -> OpenModule -> OpenModule
modSubst subst :: OpenModuleSubst
subst (OpenModule cid :: OpenUnitId
cid mod_name :: ModuleName
mod_name) = OpenUnitId -> ModuleName -> OpenModule
OpenModule (OpenModuleSubst -> OpenUnitId -> OpenUnitId
forall a. ModSubst a => OpenModuleSubst -> a -> a
modSubst OpenModuleSubst
subst OpenUnitId
cid) ModuleName
mod_name
modSubst subst :: OpenModuleSubst
subst mod :: OpenModule
mod@(OpenModuleVar mod_name :: ModuleName
mod_name)
| Just mod' :: OpenModule
mod' <- ModuleName -> OpenModuleSubst -> Maybe OpenModule
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ModuleName
mod_name OpenModuleSubst
subst = OpenModule
mod'
| Bool
otherwise = OpenModule
mod
instance ModSubst OpenUnitId where
modSubst :: OpenModuleSubst -> OpenUnitId -> OpenUnitId
modSubst subst :: OpenModuleSubst
subst (IndefFullUnitId cid :: ComponentId
cid insts :: OpenModuleSubst
insts) = ComponentId -> OpenModuleSubst -> OpenUnitId
IndefFullUnitId ComponentId
cid (OpenModuleSubst -> OpenModuleSubst -> OpenModuleSubst
forall a. ModSubst a => OpenModuleSubst -> a -> a
modSubst OpenModuleSubst
subst OpenModuleSubst
insts)
modSubst _subst :: OpenModuleSubst
_subst uid :: OpenUnitId
uid = OpenUnitId
uid
instance ModSubst (Set ModuleName) where
modSubst :: OpenModuleSubst -> Set ModuleName -> Set ModuleName
modSubst subst :: OpenModuleSubst
subst reqs :: Set ModuleName
reqs
= Set ModuleName -> Set ModuleName -> Set ModuleName
forall a. Ord a => Set a -> Set a -> Set a
Set.union (Set ModuleName -> Set ModuleName -> Set ModuleName
forall a. Ord a => Set a -> Set a -> Set a
Set.difference Set ModuleName
reqs (OpenModuleSubst -> Set ModuleName
forall k a. Map k a -> Set k
Map.keysSet OpenModuleSubst
subst))
(OpenModuleSubst -> Set ModuleName
openModuleSubstFreeHoles OpenModuleSubst
subst)
instance ModSubst a => ModSubst (Map k a) where
modSubst :: OpenModuleSubst -> Map k a -> Map k a
modSubst subst :: OpenModuleSubst
subst = (a -> a) -> Map k a -> Map k a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (OpenModuleSubst -> a -> a
forall a. ModSubst a => OpenModuleSubst -> a -> a
modSubst OpenModuleSubst
subst)
instance ModSubst a => ModSubst [a] where
modSubst :: OpenModuleSubst -> [a] -> [a]
modSubst subst :: OpenModuleSubst
subst = (a -> a) -> [a] -> [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (OpenModuleSubst -> a -> a
forall a. ModSubst a => OpenModuleSubst -> a -> a
modSubst OpenModuleSubst
subst)
instance ModSubst a => ModSubst (k, a) where
modSubst :: OpenModuleSubst -> (k, a) -> (k, a)
modSubst subst :: OpenModuleSubst
subst (x :: k
x,y :: a
y) = (k
x, OpenModuleSubst -> a -> a
forall a. ModSubst a => OpenModuleSubst -> a -> a
modSubst OpenModuleSubst
subst a
y)