module GraphRewriting.Rule.Internal where
import GraphRewriting.Graph.Internal
import GraphRewriting.Graph.Write
import qualified Data.IntSet as Set
type MergeEdges = [Edge]
newtype Replace n a = Replace (Rewrite n (a, [MergeEdges]))
mergeEs :: View [Port] n ⇒ MergeEdges -> Rewrite n ()
mergeEs (e:es) = mapM_ (mergeEdges e) es
type Set = Set.IntSet
joinEdges ∷ [[Edge]] → [[Edge]]
joinEdges = map (map Edge . Set.elems) . join . map (Set.fromList . map eKey)
join ∷ [Set] → [Set]
join = foldr join1 []
join1 ∷ Set → [Set] → [Set]
join1 x [ ] = [x]
join1 x (y:ys) = if Set.null $ Set.intersection x y
then y : join1 x ys
else join1 (Set.union x y) ys