{-# language ScopedTypeVariables#-}
module Reactive.Banana.Prim.Graph where
import Control.Monad
import Data.Functor.Identity
import qualified Data.HashMap.Strict as Map
import qualified Data.HashSet as Set
import Data.Hashable
import Data.Maybe
data Graph a = Graph
{ children :: Map.HashMap a [a]
, parents :: Map.HashMap a [a]
, nodes :: Set.HashSet a
}
emptyGraph :: Graph a
emptyGraph = Graph Map.empty Map.empty Set.empty
insertEdge :: (Eq a, Hashable a) => (a,a) -> Graph a -> Graph a
insertEdge (x,y) gr = gr
{ children = Map.insertWith (flip (++)) x [y] (children gr)
, parents = Map.insertWith (flip (++)) y [x] (parents gr)
, nodes = Set.insert x $ Set.insert y $ nodes gr
}
getChildren :: (Eq a, Hashable a) => Graph a -> a -> [a]
getChildren gr x = maybe [] id . Map.lookup x . children $ gr
getParents :: (Eq a, Hashable a) => Graph a -> a -> [a]
getParents gr x = maybe [] id . Map.lookup x . parents $ gr
listParents :: forall a. (Eq a, Hashable a) => Graph a -> [a]
listParents gr = list
where
ancestors :: [a]
ancestors = [x | x <- Set.toList $ nodes gr, null (getParents gr x)]
list :: [a]
list = runIdentity $ dfs' ancestors (Identity . getChildren gr)
type GraphM m a = a -> m [a]
dfs :: (Eq a, Hashable a, Monad m) => a -> GraphM m a -> m [a]
dfs x = dfs' [x]
dfs' :: forall a m. (Eq a, Hashable a, Monad m) => [a] -> GraphM m a -> m [a]
dfs' xs succs = liftM fst $ go xs [] Set.empty
where
go :: [a] -> [a] -> Set.HashSet a -> m ([a], Set.HashSet a)
go [] ys seen = return (ys, seen)
go (x:xs) ys seen
| x `Set.member` seen = go xs ys seen
| otherwise = do
xs' <- succs x
(ys', seen') <- go xs' ys (Set.insert x seen)
go xs (x:ys') seen'