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 Algebra.Graph.ToGraph as T
import qualified Data.IntSet as IntSet
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 (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
<$ :: forall a b. a -> Graph e b -> Graph e a
$c<$ :: forall e a b. a -> Graph e b -> Graph e a
fmap :: forall a b. (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
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 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 = forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap Graph e a
x forall a. Eq a => a -> a -> Bool
== 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 = forall a. Ord a => a -> a -> Ordering
compare (forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap Graph e a
x) (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 = forall a e. a -> Graph e a
vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger
+ :: 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
(*) = forall e a. e -> Graph e a -> Graph e a -> Graph e a
connect forall a. Semiring a => a
one
signum :: Graph e a -> Graph e a
signum = forall a b. a -> b -> a
const forall e a. Graph e a
empty
abs :: Graph e a -> Graph e a
abs = forall a. a -> a
id
negate :: Graph e a -> Graph e a
negate = forall a. a -> a
id
instance IsString a => IsString (Graph e a) where
fromString :: String -> Graph e a
fromString = forall e a. a -> Graph e a
Vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString
instance Bifunctor Graph where
bimap :: forall a b c d. (a -> b) -> (c -> d) -> Graph a c -> Graph b d
bimap a -> b
f c -> d
g = forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg forall e a. Graph e a
Empty (forall e a. a -> Graph e a
Vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> d
g) (forall e a. e -> Graph e a -> Graph e a -> Graph e a
Connect 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 ) = forall a. NFData a => a -> ()
rnf a
x
rnf (Connect e
e Graph e a
x Graph e a
y) = e
e seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf Graph e a
x seq :: forall a b. a -> b -> b
`seq` 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
(<>) = 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 = forall e a. Graph e a
empty
instance (Eq e, Monoid e, Ord a) => T.ToGraph (Graph e a) where
type ToVertex (Graph e a) = a
foldg :: forall r.
r
-> (ToVertex (Graph e a) -> r)
-> (r -> r -> r)
-> (r -> r -> r)
-> Graph e a
-> r
foldg r
e ToVertex (Graph e a) -> r
v r -> r -> r
o r -> r -> r
c = forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg r
e ToVertex (Graph e a) -> r
v (\e
e -> if e
e forall a. Eq a => a -> a -> Bool
== forall a. Monoid a => a
mempty then r -> r -> r
o else r -> r -> r
c)
vertexList :: Ord (ToVertex (Graph e a)) => Graph e a -> [ToVertex (Graph e a)]
vertexList = forall a e. Ord a => Graph e a -> [a]
vertexList
vertexSet :: Ord (ToVertex (Graph e a)) =>
Graph e a -> Set (ToVertex (Graph e a))
vertexSet = forall a e. Ord a => Graph e a -> Set a
vertexSet
toAdjacencyMap :: Ord (ToVertex (Graph e a)) =>
Graph e a -> AdjacencyMap (ToVertex (Graph e a))
toAdjacencyMap = forall a e. Ord a => AdjacencyMap e a -> AdjacencyMap a
AM.skeleton forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap
toAdjacencyMapTranspose :: Ord (ToVertex (Graph e a)) =>
Graph e a -> AdjacencyMap (ToVertex (Graph e a))
toAdjacencyMapTranspose = forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> AdjacencyMap (ToVertex t)
T.toAdjacencyMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Graph e a -> Graph e a
transpose
toAdjacencyIntMap :: (ToVertex (Graph e a) ~ Int) => Graph e a -> AdjacencyIntMap
toAdjacencyIntMap = forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap
toAdjacencyIntMapTranspose :: (ToVertex (Graph e a) ~ Int) => Graph e a -> AdjacencyIntMap
toAdjacencyIntMapTranspose = forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> AdjacencyMap (ToVertex t)
T.toAdjacencyMapTranspose
toAdjacencyMap :: (Eq e, Monoid e, Ord a) => Graph e a -> AM.AdjacencyMap e a
toAdjacencyMap :: forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> AdjacencyMap e a
toAdjacencyMap = forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg forall e a. AdjacencyMap e a
AM.empty forall a e. a -> AdjacencyMap e a
AM.vertex 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 :: forall e a. Monoid e => AdjacencyMap e a -> Graph e a
fromAdjacencyMap = forall e a. Monoid e => [Graph e a] -> Graph e a
overlays forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall {e} {a}. Monoid e => (a, Map a e) -> Graph e a
go forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [(k, a)]
Map.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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) = forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay (forall a e. a -> Graph e a
vertex a
u) (forall e a. Monoid e => [(e, a, a)] -> Graph e a
edges [ (e
e, a
u, a
v) | (a
v, e
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 :: forall b a e. 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 a e.
(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 = forall r. r -> (a -> r) -> (e -> r -> r -> r) -> r
f forall e a. Graph e a
Empty forall e a. a -> Graph e a
Vertex 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 :: forall e a.
(Eq e, Monoid e, Ord a) =>
Graph e a -> Graph e a -> Bool
isSubgraphOf Graph e a
x Graph e a
y = forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay Graph e a
x Graph e a
y forall a. Eq a => a -> a -> Bool
== Graph e a
y
empty :: Graph e a
empty :: forall e a. Graph e a
empty = forall e a. Graph e a
Empty
vertex :: a -> Graph e a
vertex :: forall a e. a -> Graph e a
vertex = forall e a. a -> Graph e a
Vertex
edge :: e -> a -> a -> Graph e a
edge :: forall e a. e -> a -> a -> Graph e a
edge e
e a
x a
y = forall e a. e -> Graph e a -> Graph e a -> Graph e a
connect e
e (forall a e. a -> Graph e a
vertex a
x) (forall a e. a -> Graph e a
vertex a
y)
(-<) :: a -> e -> (a, e)
a
g -< :: forall a e. a -> e -> (a, e)
-< e
e = (a
g, e
e)
(>-) :: (a, e) -> a -> Graph e a
(a
x, e
e) >- :: forall a e. (a, e) -> a -> Graph e a
>- a
y = 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 :: forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay = forall e a. e -> Graph e a -> Graph e a -> Graph e a
connect forall a. Monoid a => a
zero
connect :: e -> Graph e a -> Graph e a -> Graph e a
connect :: forall e a. e -> Graph e a -> Graph e a -> Graph e a
connect = forall e a. e -> Graph e a -> Graph e a -> Graph e a
Connect
vertices :: Monoid e => [a] -> Graph e a
vertices :: forall e a. Monoid e => [a] -> Graph e a
vertices = forall e a. Monoid e => [Graph e a] -> Graph e a
overlays forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall a e. a -> Graph e a
vertex
edges :: Monoid e => [(e, a, a)] -> Graph e a
edges :: forall e a. Monoid e => [(e, a, a)] -> Graph e a
edges = forall e a. Monoid e => [Graph e a] -> Graph e a
overlays forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (\(e
e, a
x, a
y) -> 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 :: forall e a. Monoid e => [Graph e a] -> Graph e a
overlays = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay forall e a. Graph e a
empty
isEmpty :: Graph e a -> Bool
isEmpty :: forall e a. Graph e a -> Bool
isEmpty = forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Bool
True (forall a b. a -> b -> a
const Bool
False) (forall a b. a -> b -> a
const Bool -> Bool -> Bool
(&&))
size :: Graph e a -> Int
size :: forall e a. Graph e a -> Int
size = forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Int
1 (forall a b. a -> b -> a
const Int
1) (forall a b. a -> b -> a
const forall a. Num a => a -> a -> a
(+))
hasVertex :: Eq a => a -> Graph e a -> Bool
hasVertex :: forall a e. Eq a => a -> Graph e a -> Bool
hasVertex a
x = forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg Bool
False (forall a. Eq a => a -> a -> Bool
==a
x) (forall a b. a -> b -> a
const Bool -> Bool -> Bool
(||))
hasEdge :: (Eq e, Monoid e, Ord a) => a -> a -> Graph e a -> Bool
hasEdge :: forall e a. (Eq e, Monoid e, Ord a) => a -> a -> Graph e a -> Bool
hasEdge a
x a
y = (forall a. Eq a => a -> a -> Bool
/= forall a. Monoid a => a
zero) forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall a e. (Eq a, Monoid e) => a -> a -> Graph e a -> e
edgeLabel a
s a
t Graph e a
g = let (e
res, 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 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 = (forall a. Monoid a => a
zero , Bool
False , Bool
False )
v :: a -> (e, Bool, Bool)
v a
x = (forall a. Monoid a => a
zero , a
x forall a. Eq a => a -> a -> Bool
== a
s , a
x 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 = (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 = (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 :: forall a e. Ord a => Graph e a -> [a]
vertexList = forall a. Set a -> [a]
Set.toAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall e a. (Eq e, Monoid e, Ord a) => Graph e a -> [(e, a, a)]
edgeList = forall e a. AdjacencyMap e a -> [(e, a, a)]
AM.edgeList forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall a e. Ord a => Graph e a -> Set a
vertexSet = forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg forall a. Set a
Set.empty forall a. a -> Set a
Set.singleton (forall a b. a -> b -> a
const 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 :: forall e a. (Eq e, Monoid e, Ord a) => Graph e a -> Set (e, a, a)
edgeSet = forall a. Eq a => [a] -> Set a
Set.fromAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall a e. Eq a => a -> Graph e a -> Graph e a
removeVertex a
x = forall a e. (a -> Bool) -> Graph e a -> Graph e a
induce (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 :: forall a e.
(Eq a, Eq e, Monoid e) =>
a -> a -> Graph e a -> Graph e a
removeEdge a
s a
t = forall a e.
(Eq a, Eq e, Monoid e) =>
a -> (a -> Bool) -> (a -> Bool) -> Graph e a -> Graph e a
filterContext a
s (forall a. Eq a => a -> a -> Bool
/=a
s) (forall a. Eq a => a -> a -> Bool
/=a
t)
replaceVertex :: Eq a => a -> a -> Graph e a -> Graph e a
replaceVertex :: forall a e. Eq a => a -> a -> Graph e a -> Graph e a
replaceVertex a
u a
v = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a -> b) -> a -> b
$ \a
w -> if a
w 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 :: forall e a.
(Eq e, Monoid e, Ord a) =>
e -> a -> a -> Graph e a -> Graph e a
replaceEdge e
e a
x a
y = forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay (forall e a. e -> a -> a -> Graph e a
edge e
e a
x a
y) forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall e a. Graph e a -> Graph e a
transpose = forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg forall e a. Graph e a
empty forall a e. a -> Graph e a
vertex (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b c. (a -> b -> c) -> b -> a -> c
flip forall e a. e -> Graph e a -> Graph e a -> Graph e a
connect)
emap :: (e -> f) -> Graph e a -> Graph f a
emap :: forall a b c. (a -> b) -> Graph a c -> Graph b c
emap e -> f
f = forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg forall e a. Graph e a
Empty forall e a. a -> Graph e a
Vertex (forall e a. e -> Graph e a -> Graph e a -> Graph e a
Connect forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> f
f)
induce :: (a -> Bool) -> Graph e a -> Graph e a
induce :: forall a e. (a -> Bool) -> Graph e a -> Graph e a
induce a -> Bool
p = forall e a. Graph e (Maybe a) -> Graph e a
induceJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
a -> if a -> Bool
p a
a then forall a. a -> Maybe a
Just a
a else forall a. Maybe a
Nothing)
induceJust :: Graph e (Maybe a) -> Graph e a
induceJust :: forall e a. Graph e (Maybe a) -> Graph e a
induceJust = forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg forall e a. Graph e a
Empty (forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall e a. Graph e a
Empty forall e a. a -> Graph e a
Vertex) 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 = 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 :: forall e a. (Eq e, Ord a, StarSemiring e) => Graph e a -> Graph e a
closure = forall e a. Monoid e => AdjacencyMap e a -> Graph e a
fromAdjacencyMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a.
(Eq e, Ord a, StarSemiring e) =>
AdjacencyMap e a -> AdjacencyMap e a
AM.closure forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall a e. (Ord a, Semiring e) => Graph e a -> Graph e a
reflexiveClosure Graph e a
x = forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay Graph e a
x forall a b. (a -> b) -> a -> b
$ forall e a. Monoid e => [(e, a, a)] -> Graph e a
edges [ (forall a. Semiring a => a
one, a
v, a
v) | a
v <- forall a e. Ord a => Graph e a -> [a]
vertexList Graph e a
x ]
symmetricClosure :: Monoid e => Graph e a -> Graph e a
symmetricClosure :: forall e a. Monoid e => Graph e a -> Graph e a
symmetricClosure Graph e a
m = forall e a. Monoid e => Graph e a -> Graph e a -> Graph e a
overlay Graph e a
m (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 :: forall e a. (Eq e, Ord a, StarSemiring e) => Graph e a -> Graph e a
transitiveClosure = forall e a. Monoid e => AdjacencyMap e a -> Graph e a
fromAdjacencyMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a.
(Eq e, Ord a, StarSemiring e) =>
AdjacencyMap e a -> AdjacencyMap e a
AM.transitiveClosure forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall a e.
(Eq a, Eq e, Monoid e) =>
a -> (a -> Bool) -> (a -> Bool) -> Graph e a -> Graph e a
filterContext a
s a -> Bool
i a -> Bool
o Graph e a
g = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Graph e a
g Context e a -> Graph e a
go forall a b. (a -> b) -> a -> b
$ forall e a.
(Eq e, Monoid e) =>
(a -> Bool) -> Graph e a -> Maybe (Context e a)
context (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) = forall e a. Monoid e => [Graph e a] -> Graph e a
overlays [ forall a e. a -> Graph e a
vertex a
s
, forall a e. (a -> Bool) -> Graph e a -> Graph e a
induce (forall a. Eq a => a -> a -> Bool
/=a
s) Graph e a
g
, 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 ]
, 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
{ forall e a. Focus e a -> Bool
ok :: Bool
, forall e a. Focus e a -> List (e, a)
is :: List (e, a)
, forall e a. Focus e a -> List (e, a)
os :: List (e, a)
, forall e a. Focus e a -> List a
vs :: List a }
emptyFocus :: Focus e a
emptyFocus :: forall e a. Focus e a
emptyFocus = forall e a.
Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
Focus Bool
False forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty
vertexFocus :: (a -> Bool) -> a -> Focus e a
vertexFocus :: forall a e. (a -> Bool) -> a -> Focus e a
vertexFocus a -> Bool
f a
x = forall e a.
Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
Focus (a -> Bool
f a
x) forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty (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 :: forall e a.
(Eq e, Monoid e) =>
e -> Focus e a -> Focus e a -> Focus e a
connectFoci e
e Focus e a
x Focus e a
y
| e
e forall a. Eq a => a -> a -> Bool
== forall a. Monoid a => a
mempty = forall e a.
Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
Focus (forall e a. Focus e a -> Bool
ok Focus e a
x Bool -> Bool -> Bool
|| forall e a. Focus e a -> Bool
ok Focus e a
y) (forall e a. Focus e a -> List (e, a)
is Focus e a
x forall a. Semigroup a => a -> a -> a
<> forall e a. Focus e a -> List (e, a)
is Focus e a
y) (forall e a. Focus e a -> List (e, a)
os Focus e a
x forall a. Semigroup a => a -> a -> a
<> forall e a. Focus e a -> List (e, a)
os Focus e a
y) (forall e a. Focus e a -> List a
vs Focus e a
x forall a. Semigroup a => a -> a -> a
<> forall e a. Focus e a -> List a
vs Focus e a
y)
| Bool
otherwise = forall e a.
Bool -> List (e, a) -> List (e, a) -> List a -> Focus e a
Focus (forall e a. Focus e a -> Bool
ok Focus e a
x Bool -> Bool -> Bool
|| forall e a. Focus e a -> Bool
ok Focus e a
y) (List (e, a)
xs forall a. Semigroup a => a -> a -> a
<> forall e a. Focus e a -> List (e, a)
is Focus e a
y) (forall e a. Focus e a -> List (e, a)
os Focus e a
x forall a. Semigroup a => a -> a -> a
<> List (e, a)
ys ) (forall e a. Focus e a -> List a
vs Focus e a
x forall a. Semigroup a => a -> a -> a
<> forall e a. Focus e a -> List a
vs Focus e a
y)
where
xs :: List (e, a)
xs = if forall e a. Focus e a -> Bool
ok Focus e a
y then forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (e
e,) (forall e a. Focus e a -> List a
vs Focus e a
x) else forall e a. Focus e a -> List (e, a)
is Focus e a
x
ys :: List (e, a)
ys = if forall e a. Focus e a -> Bool
ok Focus e a
x then forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (e
e,) (forall e a. Focus e a -> List a
vs Focus e a
y) else 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 :: forall e a.
(Eq e, Monoid e) =>
(a -> Bool) -> Graph e a -> Focus e a
focus a -> Bool
f = forall b a e. b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
foldg forall e a. Focus e a
emptyFocus (forall a e. (a -> Bool) -> a -> Focus e a
vertexFocus a -> Bool
f) forall e a.
(Eq e, Monoid e) =>
e -> Focus e a -> Focus e a -> Focus e a
connectFoci
data Context e a = Context { forall e a. Context e a -> [(e, a)]
inputs :: [(e, a)], forall e a. Context e a -> [(e, a)]
outputs :: [(e, a)] }
deriving (Context e a -> Context e a -> Bool
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
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 :: forall e a.
(Eq e, Monoid e) =>
(a -> Bool) -> Graph e a -> Maybe (Context e a)
context a -> Bool
p Graph e a
g | forall e a. Focus e a -> Bool
ok Focus e a
f = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall e a. [(e, a)] -> [(e, a)] -> Context e a
Context (forall l. IsList l => l -> [Item l]
Exts.toList forall a b. (a -> b) -> a -> b
$ forall e a. Focus e a -> List (e, a)
is Focus e a
f) (forall l. IsList l => l -> [Item l]
Exts.toList forall a b. (a -> b) -> a -> b
$ forall e a. Focus e a -> List (e, a)
os Focus e a
f)
| Bool
otherwise = forall a. Maybe a
Nothing
where
f :: Focus e a
f = forall e a.
(Eq e, Monoid e) =>
(a -> Bool) -> Graph e a -> Focus e a
focus a -> Bool
p Graph e a
g