module Algebra.Graph.Labelled (
Graph (..), empty, vertex, edge, (-<), (>-), overlay, connect, vertices,
edges, overlays,
foldg, buildg,
isSubgraphOf,
isEmpty, size, hasVertex, hasEdge, edgeLabel, vertexList, edgeList,
vertexSet, edgeSet,
removeVertex, removeEdge, replaceVertex, replaceEdge, transpose, emap,
induce, induceJust,
closure, reflexiveClosure, symmetricClosure, transitiveClosure,
UnlabelledGraph, Automaton, Network,
Context (..), context
) where
import Data.Bifunctor
import Data.Monoid
import Data.String
import Control.DeepSeq
import GHC.Generics
import Algebra.Graph.Internal (List)
import Algebra.Graph.Label
import qualified Algebra.Graph.Labelled.AdjacencyMap as AM
import qualified Data.Set as Set
import qualified Data.Map as Map
import qualified GHC.Exts as Exts
data Graph e a = Empty
| Vertex a
| Connect e (Graph e a) (Graph e a)
deriving (a -> Graph e b -> Graph e a
(a -> b) -> Graph e a -> Graph e b
(forall a b. (a -> b) -> Graph e a -> Graph e b)
-> (forall a b. a -> Graph e b -> Graph e a) -> Functor (Graph e)
forall a b. a -> Graph e b -> Graph e a
forall a b. (a -> b) -> Graph e a -> Graph e b
forall e a b. a -> Graph e b -> Graph e a
forall e a b. (a -> b) -> Graph e a -> Graph e b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Graph e b -> Graph e a
$c<$ :: forall e a b. a -> Graph e b -> Graph e a
fmap :: (a -> b) -> Graph e a -> Graph e b
$cfmap :: forall e a b. (a -> b) -> Graph e a -> Graph e b
Functor, Int -> Graph e a -> ShowS
[Graph e a] -> ShowS
Graph e a -> String
(Int -> Graph e a -> ShowS)
-> (Graph e a -> String)
-> ([Graph e a] -> ShowS)
-> Show (Graph e a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall e a. (Show a, Show e) => Int -> Graph e a -> ShowS
forall e a. (Show a, Show e) => [Graph e a] -> ShowS
forall e a. (Show a, Show e) => Graph e a -> String
showList :: [Graph e a] -> ShowS
$cshowList :: forall e a. (Show a, Show e) => [Graph e a] -> ShowS
show :: Graph e a -> String
$cshow :: forall e a. (Show a, Show e) => Graph e a -> String
showsPrec :: Int -> Graph e a -> ShowS
$cshowsPrec :: forall e a. (Show a, Show e) => Int -> Graph e a -> ShowS
Show, (forall x. Graph e a -> Rep (Graph e a) x)
-> (forall x. Rep (Graph e a) x -> Graph e a)
-> Generic (Graph e a)
forall x. Rep (Graph e a) x -> Graph e a
forall x. Graph e a -> Rep (Graph e a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall e a x. Rep (Graph e a) x -> Graph e a
forall e a x. Graph e a -> Rep (Graph e a) x
$cto :: forall e a x. Rep (Graph e a) x -> Graph e a
$cfrom :: forall e a x. Graph e a -> Rep (Graph e a) x
Generic)
instance (Eq e, Monoid e, Ord a) => Eq (Graph e a) where
Graph e a
x == :: Graph e a -> Graph e a -> Bool
== Graph e a
y = Graph e a -> AdjacencyMap e a
forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap Graph e a
x AdjacencyMap e a -> AdjacencyMap e a -> Bool
forall a. Eq a => a -> a -> Bool
== Graph e a -> AdjacencyMap e a
forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap Graph e a
y
instance (Eq e, Monoid e, Ord a, Ord e) => Ord (Graph e a) where
compare :: Graph e a -> Graph e a -> Ordering
compare Graph e a
x Graph e a
y = AdjacencyMap e a -> AdjacencyMap e a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Graph e a -> AdjacencyMap e a
forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap Graph e a
x) (Graph e a -> AdjacencyMap e a
forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap Graph e a
y)
instance (Ord a, Num a, Dioid e) => Num (Graph e a) where
fromInteger :: Integer -> Graph e a
fromInteger = a -> Graph e a
forall a e. a -> Graph e a
vertex (a -> Graph e a) -> (Integer -> a) -> Integer -> Graph e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a
forall a. Num a => Integer -> a
fromInteger
+ :: Graph e a -> Graph e a -> Graph e a
(+) = Graph e a -> Graph e a -> Graph e a
forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay
* :: Graph e a -> Graph e a -> Graph e a
(*) = e -> Graph e a -> Graph e a -> Graph e a
forall e a. e -> Graph e a -> Graph e a -> Graph e a
connect e
forall a. Semiring a => a
one
signum :: Graph e a -> Graph e a
signum = Graph e a -> Graph e a -> Graph e a
forall a b. a -> b -> a
const Graph e a
forall e a. Graph e a
empty
abs :: Graph e a -> Graph e a
abs = Graph e a -> Graph e a
forall a. a -> a
id
negate :: Graph e a -> Graph e a
negate = Graph e a -> Graph e a
forall a. a -> a
id
instance IsString a => IsString (Graph e a) where
fromString :: String -> Graph e a
fromString = a -> Graph e a
forall e a. a -> Graph e a
Vertex (a -> Graph e a) -> (String -> a) -> String -> Graph e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> a
forall a. IsString a => String -> a
fromString
instance Bifunctor Graph where
bimap :: (a -> b) -> (c -> d) -> Graph a c -> Graph b d
bimap a -> b
f c -> d
g = Graph b d
-> (c -> Graph b d)
-> (a -> Graph b d -> Graph b d -> Graph b d)
-> Graph a c
-> Graph b d
forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Graph b d
forall e a. Graph e a
Empty (d -> Graph b d
forall e a. a -> Graph e a
Vertex (d -> Graph b d) -> (c -> d) -> c -> Graph b d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> d
g) (b -> Graph b d -> Graph b d -> Graph b d
forall e a. e -> Graph e a -> Graph e a -> Graph e a
Connect (b -> Graph b d -> Graph b d -> Graph b d)
-> (a -> b) -> a -> Graph b d -> Graph b d -> Graph b d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
instance (NFData e, NFData a) => NFData (Graph e a) where
rnf :: Graph e a -> ()
rnf Graph e a
Empty = ()
rnf (Vertex a
x ) = a -> ()
forall a. NFData a => a -> ()
rnf a
x
rnf (Connect e
e Graph e a
x Graph e a
y) = e
e e -> () -> ()
`seq` Graph e a -> ()
forall a. NFData a => a -> ()
rnf Graph e a
x () -> () -> ()
`seq` Graph e a -> ()
forall a. NFData a => a -> ()
rnf Graph e a
y
instance Monoid e => Semigroup (Graph e a) where
<> :: Graph e a -> Graph e a -> Graph e a
(<>) = Graph e a -> Graph e a -> Graph e a
forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay
instance Monoid e => Monoid (Graph e a) where
mempty :: Graph e a
mempty = Graph e a
forall e a. Graph e a
empty
toAdjacencyMap :: (Eq e, Monoid e, Ord a) => Graph e a -> AM.AdjacencyMap e a
toAdjacencyMap :: Graph e a -> AdjacencyMap e a
toAdjacencyMap = AdjacencyMap e a
-> (a -> AdjacencyMap e a)
-> (e -> AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a)
-> Graph e a
-> AdjacencyMap e a
forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg AdjacencyMap e a
forall e a. AdjacencyMap e a
AM.empty a -> AdjacencyMap e a
forall a e. a -> AdjacencyMap e a
AM.vertex e -> AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
forall e a.
(Eq e, Monoid e, Ord a) =>
e -> AdjacencyMap e a -> AdjacencyMap e a -> AdjacencyMap e a
AM.connect
fromAdjacencyMap :: Monoid e => AM.AdjacencyMap e a -> Graph e a
fromAdjacencyMap :: AdjacencyMap e a -> Graph e a
fromAdjacencyMap = [Graph e a] -> Graph e a
forall e a. Monoid e => [Graph e a] -> Graph e a
overlays ([Graph e a] -> Graph e a)
-> (AdjacencyMap e a -> [Graph e a])
-> AdjacencyMap e a
-> Graph e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, Map a e) -> Graph e a) -> [(a, Map a e)] -> [Graph e a]
forall a b. (a -> b) -> [a] -> [b]
map (a, Map a e) -> Graph e a
forall e a. Monoid e => (a, Map a e) -> Graph e a
go ([(a, Map a e)] -> [Graph e a])
-> (AdjacencyMap e a -> [(a, Map a e)])
-> AdjacencyMap e a
-> [Graph e a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map a (Map a e) -> [(a, Map a e)]
forall k a. Map k a -> [(k, a)]
Map.toList (Map a (Map a e) -> [(a, Map a e)])
-> (AdjacencyMap e a -> Map a (Map a e))
-> AdjacencyMap e a
-> [(a, Map a e)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AdjacencyMap e a -> Map a (Map a e)
forall e a. AdjacencyMap e a -> Map a (Map a e)
AM.adjacencyMap
where
go :: (a, Map a e) -> Graph e a
go (a
u, Map a e
m) = Graph e a -> Graph e a -> Graph e a
forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay (a -> Graph e a
forall a e. a -> Graph e a
vertex a
u) ([(e, a, a)] -> Graph e a
forall e a. Monoid e => [(e, a, a)] -> Graph e a
edges [ (e
e, a
u, a
v) | (a
v, e
e) <- Map a e -> [(a, e)]
forall k a. Map k a -> [(k, a)]
Map.toList Map a e
m])
foldg :: b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg :: b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg b
e a -> b
v e -> b -> b -> b
c = Graph e a -> b
go
where
go :: Graph e a -> b
go Graph e a
Empty = b
e
go (Vertex a
x ) = a -> b
v a
x
go (Connect e
e Graph e a
x Graph e a
y) = e -> b -> b -> b
c e
e (Graph e a -> b
go Graph e a
x) (Graph e a -> b
go Graph e a
y)
buildg :: (forall r. r -> (a -> r) -> (e -> r -> r -> r) -> r) -> Graph e a
buildg :: (forall r. r -> (a -> r) -> (e -> r -> r -> r) -> r) -> Graph e a
buildg forall r. r -> (a -> r) -> (e -> r -> r -> r) -> r
f = Graph e a
-> (a -> Graph e a)
-> (e -> Graph e a -> Graph e a -> Graph e a)
-> Graph e a
forall r. r -> (a -> r) -> (e -> r -> r -> r) -> r
f Graph e a
forall e a. Graph e a
Empty a -> Graph e a
forall e a. a -> Graph e a
Vertex e -> Graph e a -> Graph e a -> Graph e a
forall e a. e -> Graph e a -> Graph e a -> Graph e a
Connect
isSubgraphOf :: (Eq e, Monoid e, Ord a) => Graph e a -> Graph e a -> Bool
isSubgraphOf :: Graph e a -> Graph e a -> Bool
isSubgraphOf Graph e a
x Graph e a
y = Graph e a -> Graph e a -> Graph e a
forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay Graph e a
x Graph e a
y Graph e a -> Graph e a -> Bool
forall a. Eq a => a -> a -> Bool
== Graph e a
y
empty :: Graph e a
empty :: Graph e a
empty = Graph e a
forall e a. Graph e a
Empty
vertex :: a -> Graph e a
vertex :: a -> Graph e a
vertex = a -> Graph e a
forall e a. a -> Graph e a
Vertex
edge :: e -> a -> a -> Graph e a
edge :: e -> a -> a -> Graph e a
edge e
e a
x a
y = e -> Graph e a -> Graph e a -> Graph e a
forall e a. e -> Graph e a -> Graph e a -> Graph e a
connect e
e (a -> Graph e a
forall a e. a -> Graph e a
vertex a
x) (a -> Graph e a
forall a e. a -> Graph e a
vertex a
y)
(-<) :: a -> e -> (a, e)
a
g -< :: a -> e -> (a, e)
-< e
e = (a
g, e
e)
(>-) :: (a, e) -> a -> Graph e a
(a
x, e
e) >- :: (a, e) -> a -> Graph e a
>- a
y = e -> a -> a -> Graph e a
forall e a. e -> a -> a -> Graph e a
edge e
e a
x a
y
infixl 5 -<
infixl 5 >-
overlay :: Monoid e => Graph e a -> Graph e a -> Graph e a
overlay :: Graph e a -> Graph e a -> Graph e a
overlay = e -> Graph e a -> Graph e a -> Graph e a
forall e a. e -> Graph e a -> Graph e a -> Graph e a
connect e
forall a. Monoid a => a
zero
connect :: e -> Graph e a -> Graph e a -> Graph e a
connect :: e -> Graph e a -> Graph e a -> Graph e a
connect = e -> Graph e a -> Graph e a -> Graph e a
forall e a. e -> Graph e a -> Graph e a -> Graph e a
Connect
vertices :: Monoid e => [a] -> Graph e a
vertices :: [a] -> Graph e a
vertices = [Graph e a] -> Graph e a
forall e a. Monoid e => [Graph e a] -> Graph e a
overlays ([Graph e a] -> Graph e a)
-> ([a] -> [Graph e a]) -> [a] -> Graph e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Graph e a) -> [a] -> [Graph e a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Graph e a
forall a e. a -> Graph e a
vertex
edges :: Monoid e => [(e, a, a)] -> Graph e a
edges :: [(e, a, a)] -> Graph e a
edges = [Graph e a] -> Graph e a
forall e a. Monoid e => [Graph e a] -> Graph e a
overlays ([Graph e a] -> Graph e a)
-> ([(e, a, a)] -> [Graph e a]) -> [(e, a, a)] -> Graph e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((e, a, a) -> Graph e a) -> [(e, a, a)] -> [Graph e a]
forall a b. (a -> b) -> [a] -> [b]
map (\(e
e, a
x, a
y) -> e -> a -> a -> Graph e a
forall e a. e -> a -> a -> Graph e a
edge e
e a
x a
y)
overlays :: Monoid e => [Graph e a] -> Graph e a
overlays :: [Graph e a] -> Graph e a
overlays = (Graph e a -> Graph e a -> Graph e a)
-> Graph e a -> [Graph e a] -> Graph e a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Graph e a -> Graph e a -> Graph e a
forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay Graph e a
forall e a. Graph e a
empty
isEmpty :: Graph e a -> Bool
isEmpty :: Graph e a -> Bool
isEmpty = Bool
-> (a -> Bool) -> (e -> Bool -> Bool -> Bool) -> Graph e a -> Bool
forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Bool
True (Bool -> a -> Bool
forall a b. a -> b -> a
const Bool
False) ((Bool -> Bool -> Bool) -> e -> Bool -> Bool -> Bool
forall a b. a -> b -> a
const Bool -> Bool -> Bool
(&&))
size :: Graph e a -> Int
size :: Graph e a -> Int
size = Int -> (a -> Int) -> (e -> Int -> Int -> Int) -> Graph e a -> Int
forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Int
1 (Int -> a -> Int
forall a b. a -> b -> a
const Int
1) ((Int -> Int -> Int) -> e -> Int -> Int -> Int
forall a b. a -> b -> a
const Int -> Int -> Int
forall a. Num a => a -> a -> a
(+))
hasVertex :: Eq a => a -> Graph e a -> Bool
hasVertex :: a -> Graph e a -> Bool
hasVertex a
x = Bool
-> (a -> Bool) -> (e -> Bool -> Bool -> Bool) -> Graph e a -> Bool
forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Bool
False (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
x) ((Bool -> Bool -> Bool) -> e -> Bool -> Bool -> Bool
forall a b. a -> b -> a
const Bool -> Bool -> Bool
(||))
hasEdge :: (Eq e, Monoid e, Ord a) => a -> a -> Graph e a -> Bool
hasEdge :: a -> a -> Graph e a -> Bool
hasEdge a
x a
y = (e -> e -> Bool
forall a. Eq a => a -> a -> Bool
/= e
forall a. Monoid a => a
zero) (e -> Bool) -> (Graph e a -> e) -> Graph e a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a -> Graph e a -> e
forall a e. (Eq a, Monoid e) => a -> a -> Graph e a -> e
edgeLabel a
x a
y
edgeLabel :: (Eq a, Monoid e) => a -> a -> Graph e a -> e
edgeLabel :: a -> a -> Graph e a -> e
edgeLabel a
s a
t Graph e a
g = let (e
res, Bool
_, Bool
_) = (e, Bool, Bool)
-> (a -> (e, Bool, Bool))
-> (e -> (e, Bool, Bool) -> (e, Bool, Bool) -> (e, Bool, Bool))
-> Graph e a
-> (e, Bool, Bool)
forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg (e, Bool, Bool)
e a -> (e, Bool, Bool)
v e -> (e, Bool, Bool) -> (e, Bool, Bool) -> (e, Bool, Bool)
forall a.
Monoid a =>
a -> (a, Bool, Bool) -> (a, Bool, Bool) -> (a, Bool, Bool)
c Graph e a
g in e
res
where
e :: (e, Bool, Bool)
e = (e
forall a. Monoid a => a
zero , Bool
False , Bool
False )
v :: a -> (e, Bool, Bool)
v a
x = (e
forall a. Monoid a => a
zero , a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
s , a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
t )
c :: a -> (a, Bool, Bool) -> (a, Bool, Bool) -> (a, Bool, Bool)
c a
l (a
l1, Bool
s1, Bool
t1) (a
l2, Bool
s2, Bool
t2) | Bool
s1 Bool -> Bool -> Bool
&& Bool
t2 = ([a] -> a
forall a. Monoid a => [a] -> a
mconcat [a
l1, a
l, a
l2], Bool
s1 Bool -> Bool -> Bool
|| Bool
s2, Bool
t1 Bool -> Bool -> Bool
|| Bool
t2)
| Bool
otherwise = ([a] -> a
forall a. Monoid a => [a] -> a
mconcat [a
l1, a
l2], Bool
s1 Bool -> Bool -> Bool
|| Bool
s2, Bool
t1 Bool -> Bool -> Bool
|| Bool
t2)
vertexList :: Ord a => Graph e a -> [a]
vertexList :: Graph e a -> [a]
vertexList = Set a -> [a]
forall a. Set a -> [a]
Set.toAscList (Set a -> [a]) -> (Graph e a -> Set a) -> Graph e a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph e a -> Set a
forall a e. Ord a => Graph e a -> Set a
vertexSet
edgeList :: (Eq e, Monoid e, Ord a) => Graph e a -> [(e, a, a)]
edgeList :: Graph e a -> [(e, a, a)]
edgeList = AdjacencyMap e a -> [(e, a, a)]
forall e a. AdjacencyMap e a -> [(e, a, a)]
AM.edgeList (AdjacencyMap e a -> [(e, a, a)])
-> (Graph e a -> AdjacencyMap e a) -> Graph e a -> [(e, a, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph e a -> AdjacencyMap e a
forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap
vertexSet :: Ord a => Graph e a -> Set.Set a
vertexSet :: Graph e a -> Set a
vertexSet = Set a
-> (a -> Set a)
-> (e -> Set a -> Set a -> Set a)
-> Graph e a
-> Set a
forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Set a
forall a. Set a
Set.empty a -> Set a
forall a. a -> Set a
Set.singleton ((Set a -> Set a -> Set a) -> e -> Set a -> Set a -> Set a
forall a b. a -> b -> a
const Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
Set.union)
edgeSet :: (Eq e, Monoid e, Ord a) => Graph e a -> Set.Set (e, a, a)
edgeSet :: Graph e a -> Set (e, a, a)
edgeSet = [(e, a, a)] -> Set (e, a, a)
forall a. Eq a => [a] -> Set a
Set.fromAscList ([(e, a, a)] -> Set (e, a, a))
-> (Graph e a -> [(e, a, a)]) -> Graph e a -> Set (e, a, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph e a -> [(e, a, a)]
forall e a. (Eq e, Monoid e, Ord a) => Graph e a -> [(e, a, a)]
edgeList
removeVertex :: Eq a => a -> Graph e a -> Graph e a
removeVertex :: a -> Graph e a -> Graph e a
removeVertex a
x = (a -> Bool) -> Graph e a -> Graph e a
forall a e. (a -> Bool) -> Graph e a -> Graph e a
induce (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
x)
removeEdge :: (Eq a, Eq e, Monoid e) => a -> a -> Graph e a -> Graph e a
removeEdge :: a -> a -> Graph e a -> Graph e a
removeEdge a
s a
t = a -> (a -> Bool) -> (a -> Bool) -> Graph e a -> Graph e a
forall a e.
(Eq a, Eq e, Monoid e) =>
a -> (a -> Bool) -> (a -> Bool) -> Graph e a -> Graph e a
filterContext a
s (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/=a
s) (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/=a
t)
replaceVertex :: Eq a => a -> a -> Graph e a -> Graph e a
replaceVertex :: a -> a -> Graph e a -> Graph e a
replaceVertex a
u a
v = (a -> a) -> Graph e a -> Graph e a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> a) -> Graph e a -> Graph e a)
-> (a -> a) -> Graph e a -> Graph e a
forall a b. (a -> b) -> a -> b
$ \a
w -> if a
w a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
u then a
v else a
w
replaceEdge :: (Eq e, Monoid e, Ord a) => e -> a -> a -> Graph e a -> Graph e a
replaceEdge :: e -> a -> a -> Graph e a -> Graph e a
replaceEdge e
e a
x a
y = Graph e a -> Graph e a -> Graph e a
forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay (e -> a -> a -> Graph e a
forall e a. e -> a -> a -> Graph e a
edge e
e a
x a
y) (Graph e a -> Graph e a)
-> (Graph e a -> Graph e a) -> Graph e a -> Graph e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a -> Graph e a -> Graph e a
forall a e.
(Eq a, Eq e, Monoid e) =>
a -> a -> Graph e a -> Graph e a
removeEdge a
x a
y
transpose :: Graph e a -> Graph e a
transpose :: Graph e a -> Graph e a
transpose = Graph e a
-> (a -> Graph e a)
-> (e -> Graph e a -> Graph e a -> Graph e a)
-> Graph e a
-> Graph e a
forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Graph e a
forall e a. Graph e a
empty a -> Graph e a
forall a e. a -> Graph e a
vertex (((Graph e a -> Graph e a -> Graph e a)
-> Graph e a -> Graph e a -> Graph e a)
-> (e -> Graph e a -> Graph e a -> Graph e a)
-> e
-> Graph e a
-> Graph e a
-> Graph e a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Graph e a -> Graph e a -> Graph e a)
-> Graph e a -> Graph e a -> Graph e a
forall a b c. (a -> b -> c) -> b -> a -> c
flip e -> Graph e a -> Graph e a -> Graph e a
forall e a. e -> Graph e a -> Graph e a -> Graph e a
connect)
emap :: (e -> f) -> Graph e a -> Graph f a
emap :: (e -> f) -> Graph e a -> Graph f a
emap e -> f
f = Graph f a
-> (a -> Graph f a)
-> (e -> Graph f a -> Graph f a -> Graph f a)
-> Graph e a
-> Graph f a
forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Graph f a
forall e a. Graph e a
Empty a -> Graph f a
forall e a. a -> Graph e a
Vertex (f -> Graph f a -> Graph f a -> Graph f a
forall e a. e -> Graph e a -> Graph e a -> Graph e a
Connect (f -> Graph f a -> Graph f a -> Graph f a)
-> (e -> f) -> e -> Graph f a -> Graph f a -> Graph f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> f
f)
induce :: (a -> Bool) -> Graph e a -> Graph e a
induce :: (a -> Bool) -> Graph e a -> Graph e a
induce a -> Bool
p = Graph e (Maybe a) -> Graph e a
forall e a. Graph e (Maybe a) -> Graph e a
induceJust (Graph e (Maybe a) -> Graph e a)
-> (Graph e a -> Graph e (Maybe a)) -> Graph e a -> Graph e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Maybe a) -> Graph e a -> Graph e (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
a -> if a -> Bool
p a
a then a -> Maybe a
forall a. a -> Maybe a
Just a
a else Maybe a
forall a. Maybe a
Nothing)
induceJust :: Graph e (Maybe a) -> Graph e a
induceJust :: Graph e (Maybe a) -> Graph e a
induceJust = Graph e a
-> (Maybe a -> Graph e a)
-> (e -> Graph e a -> Graph e a -> Graph e a)
-> Graph e (Maybe a)
-> Graph e a
forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Graph e a
forall e a. Graph e a
Empty (Graph e a -> (a -> Graph e a) -> Maybe a -> Graph e a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Graph e a
forall e a. Graph e a
Empty a -> Graph e a
forall e a. a -> Graph e a
Vertex) e -> Graph e a -> Graph e a -> Graph e a
forall e a. e -> Graph e a -> Graph e a -> Graph e a
c
where
c :: e -> Graph e a -> Graph e a -> Graph e a
c e
_ Graph e a
x Graph e a
Empty = Graph e a
x
c e
_ Graph e a
Empty Graph e a
y = Graph e a
y
c e
e Graph e a
x Graph e a
y = e -> Graph e a -> Graph e a -> Graph e a
forall e a. e -> Graph e a -> Graph e a -> Graph e a
Connect e
e Graph e a
x Graph e a
y
closure :: (Eq e, Ord a, StarSemiring e) => Graph e a -> Graph e a
closure :: Graph e a -> Graph e a
closure = AdjacencyMap e a -> Graph e a
forall e a. Monoid e => AdjacencyMap e a -> Graph e a
fromAdjacencyMap (AdjacencyMap e a -> Graph e a)
-> (Graph e a -> AdjacencyMap e a) -> Graph e a -> Graph e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AdjacencyMap e a -> AdjacencyMap e a
forall e a.
(Eq e, Ord a, StarSemiring e) =>
AdjacencyMap e a -> AdjacencyMap e a
AM.closure (AdjacencyMap e a -> AdjacencyMap e a)
-> (Graph e a -> AdjacencyMap e a) -> Graph e a -> AdjacencyMap e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph e a -> AdjacencyMap e a
forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap
reflexiveClosure :: (Ord a, Semiring e) => Graph e a -> Graph e a
reflexiveClosure :: Graph e a -> Graph e a
reflexiveClosure Graph e a
x = Graph e a -> Graph e a -> Graph e a
forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay Graph e a
x (Graph e a -> Graph e a) -> Graph e a -> Graph e a
forall a b. (a -> b) -> a -> b
$ [(e, a, a)] -> Graph e a
forall e a. Monoid e => [(e, a, a)] -> Graph e a
edges [ (e
forall a. Semiring a => a
one, a
v, a
v) | a
v <- Graph e a -> [a]
forall a e. Ord a => Graph e a -> [a]
vertexList Graph e a
x ]
symmetricClosure :: Monoid e => Graph e a -> Graph e a
symmetricClosure :: Graph e a -> Graph e a
symmetricClosure Graph e a
m = Graph e a -> Graph e a -> Graph e a
forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay Graph e a
m (Graph e a -> Graph e a
forall e a. Graph e a -> Graph e a
transpose Graph e a
m)
transitiveClosure :: (Eq e, Ord a, StarSemiring e) => Graph e a -> Graph e a
transitiveClosure :: Graph e a -> Graph e a
transitiveClosure = AdjacencyMap e a -> Graph e a
forall e a. Monoid e => AdjacencyMap e a -> Graph e a
fromAdjacencyMap (AdjacencyMap e a -> Graph e a)
-> (Graph e a -> AdjacencyMap e a) -> Graph e a -> Graph e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AdjacencyMap e a -> AdjacencyMap e a
forall e a.
(Eq e, Ord a, StarSemiring e) =>
AdjacencyMap e a -> AdjacencyMap e a
AM.transitiveClosure (AdjacencyMap e a -> AdjacencyMap e a)
-> (Graph e a -> AdjacencyMap e a) -> Graph e a -> AdjacencyMap e a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph e a -> AdjacencyMap e a
forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap
type UnlabelledGraph a = Graph Any a
type Automaton a s = Graph (RegularExpression a) s
type Network e a = Graph (Distance e) a
filterContext :: (Eq a, Eq e, Monoid e) => a -> (a -> Bool) -> (a -> Bool) -> Graph e a -> Graph e a
filterContext :: a -> (a -> Bool) -> (a -> Bool) -> Graph e a -> Graph e a
filterContext a
s a -> Bool
i a -> Bool
o Graph e a
g = Graph e a
-> (Context e a -> Graph e a) -> Maybe (Context e a) -> Graph e a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Graph e a
g Context e a -> Graph e a
go (Maybe (Context e a) -> Graph e a)
-> Maybe (Context e a) -> Graph e a
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> Graph e a -> Maybe (Context e a)
forall e a.
(Eq e, Monoid e) =>
(a -> Bool) -> Graph e a -> Maybe (Context e a)
context (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
s) Graph e a
g
where
go :: Context e a -> Graph e a
go (Context [(e, a)]
is [(e, a)]
os) = [Graph e a] -> Graph e a
forall e a. Monoid e => [Graph e a] -> Graph e a
overlays [ a -> Graph e a
forall a e. a -> Graph e a
vertex a
s
, (a -> Bool) -> Graph e a -> Graph e a
forall a e. (a -> Bool) -> Graph e a -> Graph e a
induce (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/=a
s) Graph e a
g
, [(e, a, a)] -> Graph e a
forall e a. Monoid e => [(e, a, a)] -> Graph e a
edges [ (e
e, a
v, a
s) | (e
e, a
v) <- [(e, a)]
is, a -> Bool
i a
v ]
, [(e, a, a)] -> Graph e a
forall e a. Monoid e => [(e, a, a)] -> Graph e a
edges [ (e
e, a
s, a
v) | (e
e, a
v) <- [(e, a)]
os, a -> Bool
o a
v ] ]
data Focus e a = Focus
{ Focus e a -> Bool
ok :: Bool
, Focus e a -> List (e, a)
is :: List (e, a)
, Focus e a -> List (e, a)
os :: List (e, a)
, Focus e a -> List a
vs :: List a }
emptyFocus :: Focus e a
emptyFocus :: Focus e a
emptyFocus = Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
forall e a.
Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
Focus Bool
False List (e, a)
forall a. Monoid a => a
mempty List (e, a)
forall a. Monoid a => a
mempty List a
forall a. Monoid a => a
mempty
vertexFocus :: (a -> Bool) -> a -> Focus e a
vertexFocus :: (a -> Bool) -> a -> Focus e a
vertexFocus a -> Bool
f a
x = Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
forall e a.
Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
Focus (a -> Bool
f a
x) List (e, a)
forall a. Monoid a => a
mempty List (e, a)
forall a. Monoid a => a
mempty (a -> List a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
connectFoci :: (Eq e, Monoid e) => e -> Focus e a -> Focus e a -> Focus e a
connectFoci :: e -> Focus e a -> Focus e a -> Focus e a
connectFoci e
e Focus e a
x Focus e a
y
| e
e e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== e
forall a. Monoid a => a
mempty = Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
forall e a.
Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
Focus (Focus e a -> Bool
forall e a. Focus e a -> Bool
ok Focus e a
x Bool -> Bool -> Bool
|| Focus e a -> Bool
forall e a. Focus e a -> Bool
ok Focus e a
y) (Focus e a -> List (e, a)
forall e a. Focus e a -> List (e, a)
is Focus e a
x List (e, a) -> List (e, a) -> List (e, a)
forall a. Semigroup a => a -> a -> a
<> Focus e a -> List (e, a)
forall e a. Focus e a -> List (e, a)
is Focus e a
y) (Focus e a -> List (e, a)
forall e a. Focus e a -> List (e, a)
os Focus e a
x List (e, a) -> List (e, a) -> List (e, a)
forall a. Semigroup a => a -> a -> a
<> Focus e a -> List (e, a)
forall e a. Focus e a -> List (e, a)
os Focus e a
y) (Focus e a -> List a
forall e a. Focus e a -> List a
vs Focus e a
x List a -> List a -> List a
forall a. Semigroup a => a -> a -> a
<> Focus e a -> List a
forall e a. Focus e a -> List a
vs Focus e a
y)
| Bool
otherwise = Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
forall e a.
Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
Focus (Focus e a -> Bool
forall e a. Focus e a -> Bool
ok Focus e a
x Bool -> Bool -> Bool
|| Focus e a -> Bool
forall e a. Focus e a -> Bool
ok Focus e a
y) (List (e, a)
xs List (e, a) -> List (e, a) -> List (e, a)
forall a. Semigroup a => a -> a -> a
<> Focus e a -> List (e, a)
forall e a. Focus e a -> List (e, a)
is Focus e a
y) (Focus e a -> List (e, a)
forall e a. Focus e a -> List (e, a)
os Focus e a
x List (e, a) -> List (e, a) -> List (e, a)
forall a. Semigroup a => a -> a -> a
<> List (e, a)
ys ) (Focus e a -> List a
forall e a. Focus e a -> List a
vs Focus e a
x List a -> List a -> List a
forall a. Semigroup a => a -> a -> a
<> Focus e a -> List a
forall e a. Focus e a -> List a
vs Focus e a
y)
where
xs :: List (e, a)
xs = if Focus e a -> Bool
forall e a. Focus e a -> Bool
ok Focus e a
y then (a -> (e, a)) -> List a -> List (e, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (e
e,) (Focus e a -> List a
forall e a. Focus e a -> List a
vs Focus e a
x) else Focus e a -> List (e, a)
forall e a. Focus e a -> List (e, a)
is Focus e a
x
ys :: List (e, a)
ys = if Focus e a -> Bool
forall e a. Focus e a -> Bool
ok Focus e a
x then (a -> (e, a)) -> List a -> List (e, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (e
e,) (Focus e a -> List a
forall e a. Focus e a -> List a
vs Focus e a
y) else Focus e a -> List (e, a)
forall e a. Focus e a -> List (e, a)
os Focus e a
y
focus :: (Eq e, Monoid e) => (a -> Bool) -> Graph e a -> Focus e a
focus :: (a -> Bool) -> Graph e a -> Focus e a
focus a -> Bool
f = Focus e a
-> (a -> Focus e a)
-> (e -> Focus e a -> Focus e a -> Focus e a)
-> Graph e a
-> Focus e a
forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Focus e a
forall e a. Focus e a
emptyFocus ((a -> Bool) -> a -> Focus e a
forall a e. (a -> Bool) -> a -> Focus e a
vertexFocus a -> Bool
f) e -> Focus e a -> Focus e a -> Focus e a
forall e a.
(Eq e, Monoid e) =>
e -> Focus e a -> Focus e a -> Focus e a
connectFoci
data Context e a = Context { Context e a -> [(e, a)]
inputs :: [(e, a)], Context e a -> [(e, a)]
outputs :: [(e, a)] }
deriving (Context e a -> Context e a -> Bool
(Context e a -> Context e a -> Bool)
-> (Context e a -> Context e a -> Bool) -> Eq (Context e a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall e a. (Eq e, Eq a) => Context e a -> Context e a -> Bool
/= :: Context e a -> Context e a -> Bool
$c/= :: forall e a. (Eq e, Eq a) => Context e a -> Context e a -> Bool
== :: Context e a -> Context e a -> Bool
$c== :: forall e a. (Eq e, Eq a) => Context e a -> Context e a -> Bool
Eq, Int -> Context e a -> ShowS
[Context e a] -> ShowS
Context e a -> String
(Int -> Context e a -> ShowS)
-> (Context e a -> String)
-> ([Context e a] -> ShowS)
-> Show (Context e a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall e a. (Show e, Show a) => Int -> Context e a -> ShowS
forall e a. (Show e, Show a) => [Context e a] -> ShowS
forall e a. (Show e, Show a) => Context e a -> String
showList :: [Context e a] -> ShowS
$cshowList :: forall e a. (Show e, Show a) => [Context e a] -> ShowS
show :: Context e a -> String
$cshow :: forall e a. (Show e, Show a) => Context e a -> String
showsPrec :: Int -> Context e a -> ShowS
$cshowsPrec :: forall e a. (Show e, Show a) => Int -> Context e a -> ShowS
Show)
context :: (Eq e, Monoid e) => (a -> Bool) -> Graph e a -> Maybe (Context e a)
context :: (a -> Bool) -> Graph e a -> Maybe (Context e a)
context a -> Bool
p Graph e a
g | Focus e a -> Bool
forall e a. Focus e a -> Bool
ok Focus e a
f = Context e a -> Maybe (Context e a)
forall a. a -> Maybe a
Just (Context e a -> Maybe (Context e a))
-> Context e a -> Maybe (Context e a)
forall a b. (a -> b) -> a -> b
$ [(e, a)] -> [(e, a)] -> Context e a
forall e a. [(e, a)] -> [(e, a)] -> Context e a
Context (List (e, a) -> [Item (List (e, a))]
forall l. IsList l => l -> [Item l]
Exts.toList (List (e, a) -> [Item (List (e, a))])
-> List (e, a) -> [Item (List (e, a))]
forall a b. (a -> b) -> a -> b
$ Focus e a -> List (e, a)
forall e a. Focus e a -> List (e, a)
is Focus e a
f) (List (e, a) -> [Item (List (e, a))]
forall l. IsList l => l -> [Item l]
Exts.toList (List (e, a) -> [Item (List (e, a))])
-> List (e, a) -> [Item (List (e, a))]
forall a b. (a -> b) -> a -> b
$ Focus e a -> List (e, a)
forall e a. Focus e a -> List (e, a)
os Focus e a
f)
| Bool
otherwise = Maybe (Context e a)
forall a. Maybe a
Nothing
where
f :: Focus e a
f = (a -> Bool) -> Graph e a -> Focus e a
forall e a.
(Eq e, Monoid e) =>
(a -> Bool) -> Graph e a -> Focus e a
focus a -> Bool
p Graph e a
g