module Algebra.Graph (
Graph (..),
empty, vertex, edge, overlay, connect, vertices, edges, overlays, connects,
foldg, buildg,
isSubgraphOf, (===),
isEmpty, size, hasVertex, hasEdge, vertexCount, edgeCount, vertexList,
edgeList, vertexSet, edgeSet, adjacencyList,
path, circuit, clique, biclique, star, stars, tree, forest, mesh, torus,
deBruijn,
removeVertex, removeEdge, replaceVertex, mergeVertices, splitVertex,
transpose, induce, induceJust, simplify, sparsify, sparsifyKL,
compose, box,
Context (..), context
) where
import Control.Applicative (Alternative)
import Control.DeepSeq
import Control.Monad (MonadPlus (..))
import Control.Monad.State (runState, get, put)
import Data.Foldable (toList)
import Data.Maybe (fromMaybe)
import Data.String
import Data.Tree
import GHC.Generics
import Algebra.Graph.Internal
import qualified Algebra.Graph.AdjacencyMap as AM
import qualified Algebra.Graph.AdjacencyIntMap as AIM
import qualified Control.Applicative as Ap
import qualified Data.Graph as KL
import qualified Data.IntSet as IntSet
import qualified Data.Set as Set
import qualified Data.Tree as Tree
import qualified GHC.Exts as Exts
data Graph a = Empty
| Vertex a
| Overlay (Graph a) (Graph a)
| Connect (Graph a) (Graph a)
deriving (Int -> Graph a -> ShowS
[Graph a] -> ShowS
Graph a -> String
(Int -> Graph a -> ShowS)
-> (Graph a -> String) -> ([Graph a] -> ShowS) -> Show (Graph a)
forall a. Show a => Int -> Graph a -> ShowS
forall a. Show a => [Graph a] -> ShowS
forall a. Show a => Graph a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Graph a] -> ShowS
$cshowList :: forall a. Show a => [Graph a] -> ShowS
show :: Graph a -> String
$cshow :: forall a. Show a => Graph a -> String
showsPrec :: Int -> Graph a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Graph a -> ShowS
Show, (forall x. Graph a -> Rep (Graph a) x)
-> (forall x. Rep (Graph a) x -> Graph a) -> Generic (Graph a)
forall x. Rep (Graph a) x -> Graph a
forall x. Graph a -> Rep (Graph a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Graph a) x -> Graph a
forall a x. Graph a -> Rep (Graph a) x
$cto :: forall a x. Rep (Graph a) x -> Graph a
$cfrom :: forall a x. Graph a -> Rep (Graph a) x
Generic)
instance Functor Graph where
fmap :: (a -> b) -> Graph a -> Graph b
fmap a -> b
f Graph a
g = Graph a
g Graph a -> (a -> Graph b) -> Graph b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (b -> Graph b
forall a. a -> Graph a
vertex (b -> Graph b) -> (a -> b) -> a -> Graph b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
{-# INLINE fmap #-}
instance NFData a => NFData (Graph a) where
rnf :: Graph a -> ()
rnf Graph a
Empty = ()
rnf (Vertex a
x ) = a -> ()
forall a. NFData a => a -> ()
rnf a
x
rnf (Overlay Graph a
x Graph a
y) = Graph a -> ()
forall a. NFData a => a -> ()
rnf Graph a
x () -> () -> ()
`seq` Graph a -> ()
forall a. NFData a => a -> ()
rnf Graph a
y
rnf (Connect Graph a
x Graph a
y) = Graph a -> ()
forall a. NFData a => a -> ()
rnf Graph a
x () -> () -> ()
`seq` Graph a -> ()
forall a. NFData a => a -> ()
rnf Graph a
y
instance Num a => Num (Graph a) where
fromInteger :: Integer -> Graph a
fromInteger = a -> Graph a
forall a. a -> Graph a
Vertex (a -> Graph a) -> (Integer -> a) -> Integer -> Graph a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a
forall a. Num a => Integer -> a
fromInteger
+ :: Graph a -> Graph a -> Graph a
(+) = Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
Overlay
* :: Graph a -> Graph a -> Graph a
(*) = Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
Connect
signum :: Graph a -> Graph a
signum = Graph a -> Graph a -> Graph a
forall a b. a -> b -> a
const Graph a
forall a. Graph a
Empty
abs :: Graph a -> Graph a
abs = Graph a -> Graph a
forall a. a -> a
id
negate :: Graph a -> Graph a
negate = Graph a -> Graph a
forall a. a -> a
id
instance IsString a => IsString (Graph a) where
fromString :: String -> Graph a
fromString = a -> Graph a
forall a. a -> Graph a
Vertex (a -> Graph a) -> (String -> a) -> String -> Graph a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> a
forall a. IsString a => String -> a
fromString
instance Ord a => Eq (Graph a) where
== :: Graph a -> Graph a -> Bool
(==) = Graph a -> Graph a -> Bool
forall a. Ord a => Graph a -> Graph a -> Bool
eqR
instance Ord a => Ord (Graph a) where
compare :: Graph a -> Graph a -> Ordering
compare = Graph a -> Graph a -> Ordering
forall a. Ord a => Graph a -> Graph a -> Ordering
ordR
eqR :: Ord a => Graph a -> Graph a -> Bool
eqR :: Graph a -> Graph a -> Bool
eqR Graph a
x Graph a
y = Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
x AdjacencyMap a -> AdjacencyMap a -> Bool
forall a. Eq a => a -> a -> Bool
== Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
y
{-# INLINE [2] eqR #-}
{-# RULES "eqR/Int" eqR = eqIntR #-}
eqIntR :: Graph Int -> Graph Int -> Bool
eqIntR :: Graph Int -> Graph Int -> Bool
eqIntR Graph Int
x Graph Int
y = Graph Int -> AdjacencyIntMap
toAdjacencyIntMap Graph Int
x AdjacencyIntMap -> AdjacencyIntMap -> Bool
forall a. Eq a => a -> a -> Bool
== Graph Int -> AdjacencyIntMap
toAdjacencyIntMap Graph Int
y
{-# INLINE eqIntR #-}
ordR :: Ord a => Graph a -> Graph a -> Ordering
ordR :: Graph a -> Graph a -> Ordering
ordR Graph a
x Graph a
y = AdjacencyMap a -> AdjacencyMap a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
x) (Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
y)
{-# INLINE [2] ordR #-}
{-# RULES "ordR/Int" ordR = ordIntR #-}
ordIntR :: Graph Int -> Graph Int -> Ordering
ordIntR :: Graph Int -> Graph Int -> Ordering
ordIntR Graph Int
x Graph Int
y = AdjacencyIntMap -> AdjacencyIntMap -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Graph Int -> AdjacencyIntMap
toAdjacencyIntMap Graph Int
x) (Graph Int -> AdjacencyIntMap
toAdjacencyIntMap Graph Int
y)
{-# INLINE ordIntR #-}
instance Applicative Graph where
pure :: a -> Graph a
pure = a -> Graph a
forall a. a -> Graph a
Vertex
Graph (a -> b)
f <*> :: Graph (a -> b) -> Graph a -> Graph b
<*> Graph a
x = (forall r. r -> (b -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph b
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (b -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph b)
-> (forall r. r -> (b -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph b
forall a b. (a -> b) -> a -> b
$ \r
e b -> r
v r -> r -> r
o r -> r -> r
c -> r
-> ((a -> b) -> r)
-> (r -> r -> r)
-> (r -> r -> r)
-> Graph (a -> b)
-> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e (\a -> b
w -> r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e (b -> r
v (b -> r) -> (a -> b) -> a -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
w) r -> r -> r
o r -> r -> r
c Graph a
x) r -> r -> r
o r -> r -> r
c Graph (a -> b)
f
{-# INLINE (<*>) #-}
instance Monad Graph where
return :: a -> Graph a
return = a -> Graph a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
Graph a
g >>= :: Graph a -> (a -> Graph b) -> Graph b
>>= a -> Graph b
f = (forall r. r -> (b -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph b
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (b -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph b)
-> (forall r. r -> (b -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph b
forall a b. (a -> b) -> a -> b
$ \r
e b -> r
v r -> r -> r
o r -> r -> r
c -> r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e ((Graph b -> r) -> (a -> Graph b) -> a -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
composeR (r -> (b -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph b -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e b -> r
v r -> r -> r
o r -> r -> r
c) a -> Graph b
f) r -> r -> r
o r -> r -> r
c Graph a
g
{-# INLINE (>>=) #-}
instance Alternative Graph where
empty :: Graph a
empty = Graph a
forall a. Graph a
Empty
<|> :: Graph a -> Graph a -> Graph a
(<|>) = Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
Overlay
instance MonadPlus Graph where
mzero :: Graph a
mzero = Graph a
forall a. Graph a
Empty
mplus :: Graph a -> Graph a -> Graph a
mplus = Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
Overlay
instance Semigroup (Graph a) where
<> :: Graph a -> Graph a -> Graph a
(<>) = Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
overlay
instance Monoid (Graph a) where
mempty :: Graph a
mempty = Graph a
forall a. Graph a
empty
empty :: Graph a
empty :: Graph a
empty = Graph a
forall a. Graph a
Empty
{-# INLINE empty #-}
vertex :: a -> Graph a
vertex :: a -> Graph a
vertex = a -> Graph a
forall a. a -> Graph a
Vertex
{-# INLINE vertex #-}
edge :: a -> a -> Graph a
edge :: a -> a -> Graph a
edge a
x a
y = Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
connect (a -> Graph a
forall a. a -> Graph a
vertex a
x) (a -> Graph a
forall a. a -> Graph a
vertex a
y)
{-# INLINE edge #-}
overlay :: Graph a -> Graph a -> Graph a
overlay :: Graph a -> Graph a -> Graph a
overlay = Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
Overlay
{-# INLINE overlay #-}
connect :: Graph a -> Graph a -> Graph a
connect :: Graph a -> Graph a -> Graph a
connect = Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
Connect
{-# INLINE connect #-}
vertices :: [a] -> Graph a
vertices :: [a] -> Graph a
vertices [a]
xs = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
_ -> r -> (r -> r -> r) -> (a -> r) -> [a] -> r
forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
o a -> r
v [a]
xs
{-# INLINE vertices #-}
edges :: [(a, a)] -> Graph a
edges :: [(a, a)] -> Graph a
edges [(a, a)]
xs = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> r -> (r -> r -> r) -> ((a, a) -> r) -> [(a, a)] -> r
forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
o (\(a
x, a
y) -> r -> r -> r
c (a -> r
v a
x) (a -> r
v a
y)) [(a, a)]
xs
{-# INLINE edges #-}
overlays :: [Graph a] -> Graph a
overlays :: [Graph a] -> Graph a
overlays [Graph a]
xs = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> r -> (r -> r -> r) -> (Graph a -> r) -> [Graph a] -> r
forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
o (r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c) [Graph a]
xs
{-# INLINE overlays #-}
connects :: [Graph a] -> Graph a
connects :: [Graph a] -> Graph a
connects [Graph a]
xs = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> r -> (r -> r -> r) -> (Graph a -> r) -> [Graph a] -> r
forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
c (r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c) [Graph a]
xs
{-# INLINE connects #-}
combineR :: c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR :: c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR c
e c -> c -> c
o a -> c
f = c -> Maybe c -> c
forall a. a -> Maybe a -> a
fromMaybe c
e (Maybe c -> c) -> ([a] -> Maybe c) -> [a] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> c -> c) -> [c] -> Maybe c
forall a. (a -> a -> a) -> [a] -> Maybe a
foldr1Safe c -> c -> c
o ([c] -> Maybe c) -> ([a] -> [c]) -> [a] -> Maybe c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> c) -> [a] -> [c]
forall a b. (a -> b) -> [a] -> [b]
map a -> c
f
{-# INLINE combineR #-}
foldg :: b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg :: b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg b
e a -> b
v b -> b -> b
o b -> b -> b
c = Graph a -> b
go
where
go :: Graph a -> b
go Graph a
Empty = b
e
go (Vertex a
x ) = a -> b
v a
x
go (Overlay Graph a
x Graph a
y) = b -> b -> b
o (Graph a -> b
go Graph a
x) (Graph a -> b
go Graph a
y)
go (Connect Graph a
x Graph a
y) = b -> b -> b
c (Graph a -> b
go Graph a
x) (Graph a -> b
go Graph a
y)
{-# INLINE [0] foldg #-}
{-# RULES
"foldg/Empty" forall e v o c.
foldg e v o c Empty = e
"foldg/Vertex" forall e v o c x.
foldg e v o c (Vertex x) = v x
"foldg/Overlay" forall e v o c x y.
foldg e v o c (Overlay x y) = o (foldg e v o c x) (foldg e v o c y)
"foldg/Connect" forall e v o c x y.
foldg e v o c (Connect x y) = c (foldg e v o c x) (foldg e v o c y)
#-}
buildg :: (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r) -> Graph a
buildg :: (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r
f = Graph a
-> (a -> Graph a)
-> (Graph a -> Graph a -> Graph a)
-> (Graph a -> Graph a -> Graph a)
-> Graph a
forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r
f Graph a
forall a. Graph a
Empty a -> Graph a
forall a. a -> Graph a
Vertex Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
Overlay Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
Connect
{-# INLINE [1] buildg #-}
isSubgraphOf :: Ord a => Graph a -> Graph a -> Bool
isSubgraphOf :: Graph a -> Graph a -> Bool
isSubgraphOf Graph a
x Graph a
y = AdjacencyMap a -> AdjacencyMap a -> Bool
forall a. Ord a => AdjacencyMap a -> AdjacencyMap a -> Bool
AM.isSubgraphOf (Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
x) (Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
y)
{-# INLINE [2] isSubgraphOf #-}
{-# RULES "isSubgraphOf/Int" isSubgraphOf = isSubgraphOfIntR #-}
isSubgraphOfIntR :: Graph Int -> Graph Int -> Bool
isSubgraphOfIntR :: Graph Int -> Graph Int -> Bool
isSubgraphOfIntR Graph Int
x Graph Int
y = AdjacencyIntMap -> AdjacencyIntMap -> Bool
AIM.isSubgraphOf (Graph Int -> AdjacencyIntMap
toAdjacencyIntMap Graph Int
x) (Graph Int -> AdjacencyIntMap
toAdjacencyIntMap Graph Int
y)
{-# INLINE isSubgraphOfIntR #-}
(===) :: Eq a => Graph a -> Graph a -> Bool
Graph a
Empty === :: Graph a -> Graph a -> Bool
=== Graph a
Empty = Bool
True
(Vertex a
x1 ) === (Vertex a
x2 ) = a
x1 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x2
(Overlay Graph a
x1 Graph a
y1) === (Overlay Graph a
x2 Graph a
y2) = Graph a
x1 Graph a -> Graph a -> Bool
forall a. Eq a => Graph a -> Graph a -> Bool
=== Graph a
x2 Bool -> Bool -> Bool
&& Graph a
y1 Graph a -> Graph a -> Bool
forall a. Eq a => Graph a -> Graph a -> Bool
=== Graph a
y2
(Connect Graph a
x1 Graph a
y1) === (Connect Graph a
x2 Graph a
y2) = Graph a
x1 Graph a -> Graph a -> Bool
forall a. Eq a => Graph a -> Graph a -> Bool
=== Graph a
x2 Bool -> Bool -> Bool
&& Graph a
y1 Graph a -> Graph a -> Bool
forall a. Eq a => Graph a -> Graph a -> Bool
=== Graph a
y2
Graph a
_ === Graph a
_ = Bool
False
{-# SPECIALISE (===) :: Graph Int -> Graph Int -> Bool #-}
infix 4 ===
isEmpty :: Graph a -> Bool
isEmpty :: Graph a -> Bool
isEmpty = Bool
-> (a -> Bool)
-> (Bool -> Bool -> Bool)
-> (Bool -> Bool -> Bool)
-> Graph a
-> Bool
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Bool
True (Bool -> a -> Bool
forall a b. a -> b -> a
const Bool
False) Bool -> Bool -> Bool
(&&) Bool -> Bool -> Bool
(&&)
{-# INLINE isEmpty #-}
size :: Graph a -> Int
size :: Graph a -> Int
size = Int
-> (a -> Int)
-> (Int -> Int -> Int)
-> (Int -> Int -> Int)
-> Graph a
-> Int
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Int
1 (Int -> a -> Int
forall a b. a -> b -> a
const Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) Int -> Int -> Int
forall a. Num a => a -> a -> a
(+)
{-# INLINE size #-}
hasVertex :: Eq a => a -> Graph a -> Bool
hasVertex :: a -> Graph a -> Bool
hasVertex a
x = Bool
-> (a -> Bool)
-> (Bool -> Bool -> Bool)
-> (Bool -> Bool -> Bool)
-> Graph a
-> Bool
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Bool
False (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
x) Bool -> Bool -> Bool
(||) Bool -> Bool -> Bool
(||)
{-# INLINE hasVertex #-}
{-# SPECIALISE hasVertex :: Int -> Graph Int -> Bool #-}
hasEdge :: Eq a => a -> a -> Graph a -> Bool
hasEdge :: a -> a -> Graph a -> Bool
hasEdge a
s a
t Graph a
g = (Int -> Int)
-> (a -> Int -> Int)
-> ((Int -> Int) -> (Int -> Int) -> Int -> Int)
-> ((Int -> Int) -> (Int -> Int) -> Int -> Int)
-> Graph a
-> Int
-> Int
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Int -> Int
forall a. a -> a
id a -> Int -> Int
v (Int -> Int) -> (Int -> Int) -> Int -> Int
forall a t. (Eq a, Num a) => (t -> a) -> (t -> Int) -> t -> Int
o (Int -> Int) -> (Int -> Int) -> Int -> Int
forall t p t.
(Eq t, Num t, Num p) =>
(t -> t) -> (t -> p) -> t -> p
c Graph a
g Int
0 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2
where
v :: a -> Int -> Int
v a
x Int
0 = if a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
s then Int
1 else Int
0
v a
x Int
_ = if a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
t then Int
2 else Int
1
o :: (t -> a) -> (t -> Int) -> t -> Int
o t -> a
x t -> Int
y t
a = case t -> a
x t
a of
a
0 -> t -> Int
y t
a
a
1 -> if t -> Int
y t
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2 then Int
2 else Int
1
a
_ -> Int
2 :: Int
c :: (t -> t) -> (t -> p) -> t -> p
c t -> t
x t -> p
y t
a = case t -> t
x t
a of { t
2 -> p
2; t
res -> t -> p
y t
res }
{-# INLINE hasEdge #-}
{-# SPECIALISE hasEdge :: Int -> Int -> Graph Int -> Bool #-}
vertexCount :: Ord a => Graph a -> Int
vertexCount :: Graph a -> Int
vertexCount = Set a -> Int
forall a. Set a -> Int
Set.size (Set a -> Int) -> (Graph a -> Set a) -> Graph a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph a -> Set a
forall a. Ord a => Graph a -> Set a
vertexSet
{-# INLINE [2] vertexCount #-}
{-# RULES "vertexCount/Int" vertexCount = vertexIntCountR #-}
vertexIntCountR :: Graph Int -> Int
vertexIntCountR :: Graph Int -> Int
vertexIntCountR = IntSet -> Int
IntSet.size (IntSet -> Int) -> (Graph Int -> IntSet) -> Graph Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> IntSet
vertexIntSetR
{-# INLINE vertexIntCountR #-}
edgeCount :: Ord a => Graph a -> Int
edgeCount :: Graph a -> Int
edgeCount = AdjacencyMap a -> Int
forall a. AdjacencyMap a -> Int
AM.edgeCount (AdjacencyMap a -> Int)
-> (Graph a -> AdjacencyMap a) -> Graph a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap
{-# INLINE [2] edgeCount #-}
{-# RULES "edgeCount/Int" edgeCount = edgeCountIntR #-}
edgeCountIntR :: Graph Int -> Int
edgeCountIntR :: Graph Int -> Int
edgeCountIntR = AdjacencyIntMap -> Int
AIM.edgeCount (AdjacencyIntMap -> Int)
-> (Graph Int -> AdjacencyIntMap) -> Graph Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> AdjacencyIntMap
toAdjacencyIntMap
{-# INLINE edgeCountIntR #-}
vertexList :: Ord a => Graph a -> [a]
vertexList :: Graph a -> [a]
vertexList = Set a -> [a]
forall a. Set a -> [a]
Set.toAscList (Set a -> [a]) -> (Graph a -> Set a) -> Graph a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph a -> Set a
forall a. Ord a => Graph a -> Set a
vertexSet
{-# INLINE [2] vertexList #-}
{-# RULES "vertexList/Int" vertexList = vertexIntListR #-}
vertexIntListR :: Graph Int -> [Int]
vertexIntListR :: Graph Int -> [Int]
vertexIntListR = IntSet -> [Int]
IntSet.toList (IntSet -> [Int]) -> (Graph Int -> IntSet) -> Graph Int -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> IntSet
vertexIntSetR
{-# INLINE vertexIntListR #-}
edgeList :: Ord a => Graph a -> [(a, a)]
edgeList :: Graph a -> [(a, a)]
edgeList = AdjacencyMap a -> [(a, a)]
forall a. AdjacencyMap a -> [(a, a)]
AM.edgeList (AdjacencyMap a -> [(a, a)])
-> (Graph a -> AdjacencyMap a) -> Graph a -> [(a, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap
{-# INLINE [2] edgeList #-}
{-# RULES "edgeList/Int" edgeList = edgeIntListR #-}
edgeIntListR :: Graph Int -> [(Int, Int)]
edgeIntListR :: Graph Int -> [(Int, Int)]
edgeIntListR = AdjacencyIntMap -> [(Int, Int)]
AIM.edgeList (AdjacencyIntMap -> [(Int, Int)])
-> (Graph Int -> AdjacencyIntMap) -> Graph Int -> [(Int, Int)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> AdjacencyIntMap
toAdjacencyIntMap
{-# INLINE edgeIntListR #-}
vertexSet :: Ord a => Graph a -> Set.Set a
vertexSet :: Graph a -> Set a
vertexSet = Set a
-> (a -> Set a)
-> (Set a -> Set a -> Set a)
-> (Set a -> Set a -> Set a)
-> Graph a
-> Set a
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph 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
forall a. Ord a => Set a -> Set a -> Set a
Set.union Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
Set.union
{-# INLINE vertexSet #-}
vertexIntSetR :: Graph Int -> IntSet.IntSet
vertexIntSetR :: Graph Int -> IntSet
vertexIntSetR = IntSet
-> (Int -> IntSet)
-> (IntSet -> IntSet -> IntSet)
-> (IntSet -> IntSet -> IntSet)
-> Graph Int
-> IntSet
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg IntSet
IntSet.empty Int -> IntSet
IntSet.singleton IntSet -> IntSet -> IntSet
IntSet.union IntSet -> IntSet -> IntSet
IntSet.union
{-# INLINE vertexIntSetR #-}
edgeSet :: Ord a => Graph a -> Set.Set (a, a)
edgeSet :: Graph a -> Set (a, a)
edgeSet = AdjacencyMap a -> Set (a, a)
forall a. Eq a => AdjacencyMap a -> Set (a, a)
AM.edgeSet (AdjacencyMap a -> Set (a, a))
-> (Graph a -> AdjacencyMap a) -> Graph a -> Set (a, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap
{-# INLINE [2] edgeSet #-}
{-# RULES "edgeSet/Int" edgeSet = edgeIntSetR #-}
edgeIntSetR :: Graph Int -> Set.Set (Int,Int)
edgeIntSetR :: Graph Int -> Set (Int, Int)
edgeIntSetR = AdjacencyIntMap -> Set (Int, Int)
AIM.edgeSet (AdjacencyIntMap -> Set (Int, Int))
-> (Graph Int -> AdjacencyIntMap) -> Graph Int -> Set (Int, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph Int -> AdjacencyIntMap
toAdjacencyIntMap
{-# INLINE edgeIntSetR #-}
adjacencyList :: Ord a => Graph a -> [(a, [a])]
adjacencyList :: Graph a -> [(a, [a])]
adjacencyList = AdjacencyMap a -> [(a, [a])]
forall a. AdjacencyMap a -> [(a, [a])]
AM.adjacencyList (AdjacencyMap a -> [(a, [a])])
-> (Graph a -> AdjacencyMap a) -> Graph a -> [(a, [a])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap
{-# INLINE adjacencyList #-}
{-# SPECIALISE adjacencyList :: Graph Int -> [(Int, [Int])] #-}
toAdjacencyMap :: Ord a => Graph a -> AM.AdjacencyMap a
toAdjacencyMap :: Graph a -> AdjacencyMap a
toAdjacencyMap = AdjacencyMap a
-> (a -> AdjacencyMap a)
-> (AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a)
-> (AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a)
-> Graph a
-> AdjacencyMap a
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg AdjacencyMap a
forall a. AdjacencyMap a
AM.empty a -> AdjacencyMap a
forall a. a -> AdjacencyMap a
AM.vertex AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a
forall a.
Ord a =>
AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a
AM.overlay AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a
forall a.
Ord a =>
AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a
AM.connect
{-# INLINE toAdjacencyMap #-}
toAdjacencyIntMap :: Graph Int -> AIM.AdjacencyIntMap
toAdjacencyIntMap :: Graph Int -> AdjacencyIntMap
toAdjacencyIntMap = AdjacencyIntMap
-> (Int -> AdjacencyIntMap)
-> (AdjacencyIntMap -> AdjacencyIntMap -> AdjacencyIntMap)
-> (AdjacencyIntMap -> AdjacencyIntMap -> AdjacencyIntMap)
-> Graph Int
-> AdjacencyIntMap
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg AdjacencyIntMap
AIM.empty Int -> AdjacencyIntMap
AIM.vertex AdjacencyIntMap -> AdjacencyIntMap -> AdjacencyIntMap
AIM.overlay AdjacencyIntMap -> AdjacencyIntMap -> AdjacencyIntMap
AIM.connect
{-# INLINE toAdjacencyIntMap #-}
path :: [a] -> Graph a
path :: [a] -> Graph a
path [a]
xs = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> case [a]
xs of
[] -> r
e
[a
x] -> a -> r
v a
x
(a
_ : [a]
ys) -> r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c (Graph a -> r) -> Graph a -> r
forall a b. (a -> b) -> a -> b
$ [(a, a)] -> Graph a
forall a. [(a, a)] -> Graph a
edges ([a] -> [a] -> [(a, a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [a]
xs [a]
ys)
{-# INLINE path #-}
circuit :: [a] -> Graph a
circuit :: [a] -> Graph a
circuit [a]
xs = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> case [a]
xs of
[] -> r
e
(a
x : [a]
xs) -> r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c (Graph a -> r) -> Graph a -> r
forall a b. (a -> b) -> a -> b
$ [a] -> Graph a
forall a. [a] -> Graph a
path ([a] -> Graph a) -> [a] -> Graph a
forall a b. (a -> b) -> a -> b
$ [a
x] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
xs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
x]
{-# INLINE circuit #-}
clique :: [a] -> Graph a
clique :: [a] -> Graph a
clique [a]
xs = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
_ r -> r -> r
c -> r -> (r -> r -> r) -> (a -> r) -> [a] -> r
forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
c a -> r
v [a]
xs
{-# INLINE clique #-}
biclique :: [a] -> [a] -> Graph a
biclique :: [a] -> [a] -> Graph a
biclique [a]
xs [a]
ys = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> case (r -> r -> r) -> [r] -> Maybe r
forall a. (a -> a -> a) -> [a] -> Maybe a
foldr1Safe r -> r -> r
o ((a -> r) -> [a] -> [r]
forall a b. (a -> b) -> [a] -> [b]
map a -> r
v [a]
xs) of
Maybe r
Nothing -> r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c (Graph a -> r) -> Graph a -> r
forall a b. (a -> b) -> a -> b
$ [a] -> Graph a
forall a. [a] -> Graph a
vertices [a]
ys
Just r
xs -> case (r -> r -> r) -> [r] -> Maybe r
forall a. (a -> a -> a) -> [a] -> Maybe a
foldr1Safe r -> r -> r
o ((a -> r) -> [a] -> [r]
forall a b. (a -> b) -> [a] -> [b]
map a -> r
v [a]
ys) of
Maybe r
Nothing -> r
xs
Just r
ys -> r -> r -> r
c r
xs r
ys
{-# INLINE biclique #-}
star :: a -> [a] -> Graph a
star :: a -> [a] -> Graph a
star a
x [a]
ys = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
_ a -> r
v r -> r -> r
o r -> r -> r
c -> case (r -> r -> r) -> [r] -> Maybe r
forall a. (a -> a -> a) -> [a] -> Maybe a
foldr1Safe r -> r -> r
o ((a -> r) -> [a] -> [r]
forall a b. (a -> b) -> [a] -> [b]
map a -> r
v [a]
ys) of
Maybe r
Nothing -> a -> r
v a
x
Just r
ys -> r -> r -> r
c (a -> r
v a
x) r
ys
{-# INLINE star #-}
stars :: [(a, [a])] -> Graph a
stars :: [(a, [a])] -> Graph a
stars [(a, [a])]
xs = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> r -> (r -> r -> r) -> ((a, [a]) -> r) -> [(a, [a])] -> r
forall c a. c -> (c -> c -> c) -> (a -> c) -> [a] -> c
combineR r
e r -> r -> r
o (r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c (Graph a -> r) -> ((a, [a]) -> Graph a) -> (a, [a]) -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> [a] -> Graph a) -> (a, [a]) -> Graph a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> [a] -> Graph a
forall a. a -> [a] -> Graph a
star) [(a, [a])]
xs
{-# INLINE stars #-}
tree :: Tree.Tree a -> Graph a
tree :: Tree a -> Graph a
tree (Node a
x []) = a -> Graph a
forall a. a -> Graph a
vertex a
x
tree (Node a
x [Tree a]
f ) = a -> [a] -> Graph a
forall a. a -> [a] -> Graph a
star a
x ((Tree a -> a) -> [Tree a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Tree a -> a
forall a. Tree a -> a
rootLabel [Tree a]
f)
Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
`overlay` [Tree a] -> Graph a
forall a. Forest a -> Graph a
forest ((Tree a -> Bool) -> [Tree a] -> [Tree a]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Tree a -> Bool) -> Tree a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Tree a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Tree a] -> Bool) -> (Tree a -> [Tree a]) -> Tree a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tree a -> [Tree a]
forall a. Tree a -> Forest a
subForest) [Tree a]
f)
forest :: Tree.Forest a -> Graph a
forest :: Forest a -> Graph a
forest = [Graph a] -> Graph a
forall a. [Graph a] -> Graph a
overlays ([Graph a] -> Graph a)
-> (Forest a -> [Graph a]) -> Forest a -> Graph a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Tree a -> Graph a) -> Forest a -> [Graph a]
forall a b. (a -> b) -> [a] -> [b]
map Tree a -> Graph a
forall a. Tree a -> Graph a
tree
mesh :: [a] -> [b] -> Graph (a, b)
mesh :: [a] -> [b] -> Graph (a, b)
mesh [] [b]
_ = Graph (a, b)
forall a. Graph a
empty
mesh [a]
_ [] = Graph (a, b)
forall a. Graph a
empty
mesh [a
x] [b
y] = (a, b) -> Graph (a, b)
forall a. a -> Graph a
vertex (a
x, b
y)
mesh [a]
xs [b]
ys = [((a, b), [(a, b)])] -> Graph (a, b)
forall a. [(a, [a])] -> Graph a
stars ([((a, b), [(a, b)])] -> Graph (a, b))
-> [((a, b), [(a, b)])] -> Graph (a, b)
forall a b. (a -> b) -> a -> b
$
[ ((a
a1, b
b1), [(a
a1, b
b2), (a
a2, b
b1)]) | (a
a1, a
a2) <- [(a, a)]
ix, (b
b1, b
b2) <- [(b, b)]
iy ]
[((a, b), [(a, b)])]
-> [((a, b), [(a, b)])] -> [((a, b), [(a, b)])]
forall a. [a] -> [a] -> [a]
++ [ ((a
lx, b
y1), [(a
lx, b
y2)]) | (b
y1, b
y2) <- [(b, b)]
iy ]
[((a, b), [(a, b)])]
-> [((a, b), [(a, b)])] -> [((a, b), [(a, b)])]
forall a. [a] -> [a] -> [a]
++ [ ((a
x1, b
ly), [(a
x2, b
ly)]) | (a
x1, a
x2) <- [(a, a)]
ix ]
where
lx :: a
lx = [a] -> a
forall a. [a] -> a
last [a]
xs
ly :: b
ly = [b] -> b
forall a. [a] -> a
last [b]
ys
ix :: [(a, a)]
ix = [(a, a)] -> [(a, a)]
forall a. [a] -> [a]
init ([a] -> [(a, a)]
forall a. [a] -> [(a, a)]
pairs [a]
xs)
iy :: [(b, b)]
iy = [(b, b)] -> [(b, b)]
forall a. [a] -> [a]
init ([b] -> [(b, b)]
forall a. [a] -> [(a, a)]
pairs [b]
ys)
torus :: [a] -> [b] -> Graph (a, b)
torus :: [a] -> [b] -> Graph (a, b)
torus [a]
xs [b]
ys = [((a, b), [(a, b)])] -> Graph (a, b)
forall a. [(a, [a])] -> Graph a
stars
[ ((a
a1, b
b1), [(a
a1, b
b2), (a
a2, b
b1)]) | (a
a1, a
a2) <- [a] -> [(a, a)]
forall a. [a] -> [(a, a)]
pairs [a]
xs, (b
b1, b
b2) <- [b] -> [(b, b)]
forall a. [a] -> [(a, a)]
pairs [b]
ys ]
pairs :: [a] -> [(a, a)]
pairs :: [a] -> [(a, a)]
pairs [] = []
pairs as :: [a]
as@(a
x:[a]
xs) = [a] -> [a] -> [(a, a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [a]
as ([a]
xs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
x])
deBruijn :: Int -> [a] -> Graph [a]
deBruijn :: Int -> [a] -> Graph [a]
deBruijn Int
0 [a]
_ = [a] -> [a] -> Graph [a]
forall a. a -> a -> Graph a
edge [] []
deBruijn Int
len [a]
alphabet = Graph (Either [a] [a])
skeleton Graph (Either [a] [a])
-> (Either [a] [a] -> Graph [a]) -> Graph [a]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either [a] [a] -> Graph [a]
expand
where
overlaps :: [[a]]
overlaps = (Int -> [a]) -> [Int] -> [[a]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([a] -> Int -> [a]
forall a b. a -> b -> a
const [a]
alphabet) [Int
2..Int
len]
skeleton :: Graph (Either [a] [a])
skeleton = [(Either [a] [a], Either [a] [a])] -> Graph (Either [a] [a])
forall a. [(a, a)] -> Graph a
edges [ ([a] -> Either [a] [a]
forall a b. a -> Either a b
Left [a]
s, [a] -> Either [a] [a]
forall a b. b -> Either a b
Right [a]
s) | [a]
s <- [[a]]
overlaps ]
expand :: Either [a] [a] -> Graph [a]
expand Either [a] [a]
v = [[a]] -> Graph [a]
forall a. [a] -> Graph a
vertices [ ([a] -> [a]) -> ([a] -> [a]) -> Either [a] [a] -> [a]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([a
a] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++) ([a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
a]) Either [a] [a]
v | a
a <- [a]
alphabet ]
removeVertex :: Eq a => a -> Graph a -> Graph a
removeVertex :: a -> Graph a -> Graph a
removeVertex a
v = (a -> Bool) -> Graph a -> Graph a
forall a. (a -> Bool) -> Graph a -> Graph a
induce (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
v)
{-# SPECIALISE removeVertex :: Int -> Graph Int -> Graph Int #-}
removeEdge :: Eq a => a -> a -> Graph a -> Graph a
removeEdge :: a -> a -> Graph a -> Graph a
removeEdge a
s a
t = a -> (a -> Bool) -> (a -> Bool) -> Graph a -> Graph a
forall a.
Eq a =>
a -> (a -> Bool) -> (a -> Bool) -> Graph a -> Graph 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)
{-# SPECIALISE removeEdge :: Int -> Int -> Graph Int -> Graph Int #-}
filterContext :: Eq a => a -> (a -> Bool) -> (a -> Bool) -> Graph a -> Graph a
filterContext :: a -> (a -> Bool) -> (a -> Bool) -> Graph a -> Graph a
filterContext a
s a -> Bool
i a -> Bool
o Graph a
g = Graph a -> (Context a -> Graph a) -> Maybe (Context a) -> Graph a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Graph a
g Context a -> Graph a
go (Maybe (Context a) -> Graph a) -> Maybe (Context a) -> Graph a
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> Graph a -> Maybe (Context a)
forall a. (a -> Bool) -> Graph a -> Maybe (Context a)
context (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
s) Graph a
g
where
go :: Context a -> Graph a
go (Context [a]
is [a]
os) = (a -> Bool) -> Graph a -> Graph a
forall a. (a -> Bool) -> Graph a -> Graph a
induce (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/=a
s) Graph a
g Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
`overlay` Graph a -> Graph a
forall a. Graph a -> Graph a
transpose (a -> [a] -> Graph a
forall a. a -> [a] -> Graph a
star a
s ((a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
filter a -> Bool
i [a]
is))
Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
`overlay` a -> [a] -> Graph a
forall a. a -> [a] -> Graph a
star a
s ((a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
filter a -> Bool
o [a]
os)
{-# SPECIALISE filterContext :: Int -> (Int -> Bool) -> (Int -> Bool) -> Graph Int -> Graph Int #-}
replaceVertex :: Eq a => a -> a -> Graph a -> Graph a
replaceVertex :: a -> a -> Graph a -> Graph a
replaceVertex a
u a
v = (a -> a) -> Graph a -> Graph a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> a) -> Graph a -> Graph a) -> (a -> a) -> Graph a -> Graph 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
{-# INLINE replaceVertex #-}
{-# SPECIALISE replaceVertex :: Int -> Int -> Graph Int -> Graph Int #-}
mergeVertices :: (a -> Bool) -> a -> Graph a -> Graph a
mergeVertices :: (a -> Bool) -> a -> Graph a -> Graph a
mergeVertices a -> Bool
p a
v = (a -> a) -> Graph a -> Graph a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> a) -> Graph a -> Graph a) -> (a -> a) -> Graph a -> Graph a
forall a b. (a -> b) -> a -> b
$ \a
w -> if a -> Bool
p a
w then a
v else a
w
{-# INLINE mergeVertices #-}
splitVertex :: Eq a => a -> [a] -> Graph a -> Graph a
splitVertex :: a -> [a] -> Graph a -> Graph a
splitVertex a
x [a]
us Graph a
g = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c ->
let split :: a -> r
split a
y = if a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y then r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c ([a] -> Graph a
forall a. [a] -> Graph a
vertices [a]
us) else a -> r
v a
y in
r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
split r -> r -> r
o r -> r -> r
c Graph a
g
{-# INLINE splitVertex #-}
{-# SPECIALISE splitVertex :: Int -> [Int] -> Graph Int -> Graph Int #-}
transpose :: Graph a -> Graph a
transpose :: Graph a -> Graph a
transpose Graph a
g = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o ((r -> r -> r) -> r -> r -> r
forall a b c. (a -> b -> c) -> b -> a -> c
flip r -> r -> r
c) Graph a
g
{-# INLINE transpose #-}
induce :: (a -> Bool) -> Graph a -> Graph a
induce :: (a -> Bool) -> Graph a -> Graph a
induce a -> Bool
p = Graph (Maybe a) -> Graph a
forall a. Graph (Maybe a) -> Graph a
induceJust (Graph (Maybe a) -> Graph a)
-> (Graph a -> Graph (Maybe a)) -> Graph a -> Graph a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Maybe a) -> Graph a -> Graph (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)
{-# INLINE induce #-}
induceJust :: Graph (Maybe a) -> Graph a
induceJust :: Graph (Maybe a) -> Graph a
induceJust Graph (Maybe a)
g = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> r -> Maybe r -> r
forall a. a -> Maybe a -> a
fromMaybe r
e (Maybe r -> r) -> Maybe r -> r
forall a b. (a -> b) -> a -> b
$
Maybe r
-> (Maybe a -> Maybe r)
-> (Maybe r -> Maybe r -> Maybe r)
-> (Maybe r -> Maybe r -> Maybe r)
-> Graph (Maybe a)
-> Maybe r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Maybe r
forall a. Maybe a
Nothing ((a -> r) -> Maybe a -> Maybe r
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> r
v) ((r -> r -> r) -> Maybe r -> Maybe r -> Maybe r
forall t. (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
k r -> r -> r
o) ((r -> r -> r) -> Maybe r -> Maybe r -> Maybe r
forall t. (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
k r -> r -> r
c) Graph (Maybe a)
g
where
k :: (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
k t -> t -> t
_ Maybe t
x Maybe t
Nothing = Maybe t
x
k t -> t -> t
_ Maybe t
Nothing Maybe t
y = Maybe t
y
k t -> t -> t
f (Just t
x) (Just t
y) = t -> Maybe t
forall a. a -> Maybe a
Just (t -> t -> t
f t
x t
y)
{-# INLINE induceJust #-}
simplify :: Ord a => Graph a -> Graph a
simplify :: Graph a -> Graph a
simplify = Graph a
-> (a -> Graph a)
-> (Graph a -> Graph a -> Graph a)
-> (Graph a -> Graph a -> Graph a)
-> Graph a
-> Graph a
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Graph a
forall a. Graph a
Empty a -> Graph a
forall a. a -> Graph a
Vertex ((Graph a -> Graph a -> Graph a) -> Graph a -> Graph a -> Graph a
forall g. Eq g => (g -> g -> g) -> g -> g -> g
simple Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
Overlay) ((Graph a -> Graph a -> Graph a) -> Graph a -> Graph a -> Graph a
forall g. Eq g => (g -> g -> g) -> g -> g -> g
simple Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
Connect)
{-# INLINE simplify #-}
{-# SPECIALISE simplify :: Graph Int -> Graph Int #-}
simple :: Eq g => (g -> g -> g) -> g -> g -> g
simple :: (g -> g -> g) -> g -> g -> g
simple g -> g -> g
op g
x g
y
| g
x g -> g -> Bool
forall a. Eq a => a -> a -> Bool
== g
z = g
x
| g
y g -> g -> Bool
forall a. Eq a => a -> a -> Bool
== g
z = g
y
| Bool
otherwise = g
z
where
z :: g
z = g -> g -> g
op g
x g
y
{-# SPECIALISE simple :: (Int -> Int -> Int) -> Int -> Int -> Int #-}
compose :: Ord a => Graph a -> Graph a -> Graph a
compose :: Graph a -> Graph a -> Graph a
compose Graph a
x Graph a
y = (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a.
(forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
buildg ((forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a)
-> (forall r. r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> r)
-> Graph a
forall a b. (a -> b) -> a -> b
$ \r
e a -> r
v r -> r -> r
o r -> r -> r
c -> r -> Maybe r -> r
forall a. a -> Maybe a -> a
fromMaybe r
e (Maybe r -> r) -> Maybe r -> r
forall a b. (a -> b) -> a -> b
$
(r -> r -> r) -> [r] -> Maybe r
forall a. (a -> a -> a) -> [a] -> Maybe a
foldr1Safe r -> r -> r
o
[ r -> (a -> r) -> (r -> r -> r) -> (r -> r -> r) -> Graph a -> r
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg r
e a -> r
v r -> r -> r
o r -> r -> r
c ([a] -> [a] -> Graph a
forall a. [a] -> [a] -> Graph a
biclique [a]
xs [a]
ys)
| a
ve <- Set a -> [a]
forall a. Set a -> [a]
Set.toList (AdjacencyMap a -> Set a
forall a. AdjacencyMap a -> Set a
AM.vertexSet AdjacencyMap a
mx Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
`Set.union` AdjacencyMap a -> Set a
forall a. AdjacencyMap a -> Set a
AM.vertexSet AdjacencyMap a
my)
, let xs :: [a]
xs = Set a -> [a]
forall a. Set a -> [a]
Set.toList (a -> AdjacencyMap a -> Set a
forall a. Ord a => a -> AdjacencyMap a -> Set a
AM.postSet a
ve AdjacencyMap a
mx), Bool -> Bool
not ([a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs)
, let ys :: [a]
ys = Set a -> [a]
forall a. Set a -> [a]
Set.toList (a -> AdjacencyMap a -> Set a
forall a. Ord a => a -> AdjacencyMap a -> Set a
AM.postSet a
ve AdjacencyMap a
my), Bool -> Bool
not ([a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
ys) ]
where
mx :: AdjacencyMap a
mx = Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap (Graph a -> Graph a
forall a. Graph a -> Graph a
transpose Graph a
x)
my :: AdjacencyMap a
my = Graph a -> AdjacencyMap a
forall a. Ord a => Graph a -> AdjacencyMap a
toAdjacencyMap Graph a
y
{-# INLINE compose #-}
box :: Graph a -> Graph b -> Graph (a, b)
box :: Graph a -> Graph b -> Graph (a, b)
box Graph a
x Graph b
y = Graph (a, b) -> Graph (a, b) -> Graph (a, b)
forall a. Graph a -> Graph a -> Graph a
overlay (Graph (b -> (a, b))
fx Graph (b -> (a, b)) -> Graph b -> Graph (a, b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Graph b
y) (Graph (a -> (a, b))
fy Graph (a -> (a, b)) -> Graph a -> Graph (a, b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Graph a
x)
where
fx :: Graph (b -> (a, b))
fx = Graph (b -> (a, b))
-> (a -> Graph (b -> (a, b)))
-> (Graph (b -> (a, b))
-> Graph (b -> (a, b)) -> Graph (b -> (a, b)))
-> (Graph (b -> (a, b))
-> Graph (b -> (a, b)) -> Graph (b -> (a, b)))
-> Graph a
-> Graph (b -> (a, b))
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Graph (b -> (a, b))
forall a. Graph a
empty ((b -> (a, b)) -> Graph (b -> (a, b))
forall a. a -> Graph a
vertex ((b -> (a, b)) -> Graph (b -> (a, b)))
-> (a -> b -> (a, b)) -> a -> Graph (b -> (a, b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,)) Graph (b -> (a, b)) -> Graph (b -> (a, b)) -> Graph (b -> (a, b))
forall a. Graph a -> Graph a -> Graph a
overlay Graph (b -> (a, b)) -> Graph (b -> (a, b)) -> Graph (b -> (a, b))
forall a. Graph a -> Graph a -> Graph a
overlay Graph a
x
fy :: Graph (a -> (a, b))
fy = Graph (a -> (a, b))
-> (b -> Graph (a -> (a, b)))
-> (Graph (a -> (a, b))
-> Graph (a -> (a, b)) -> Graph (a -> (a, b)))
-> (Graph (a -> (a, b))
-> Graph (a -> (a, b)) -> Graph (a -> (a, b)))
-> Graph b
-> Graph (a -> (a, b))
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Graph (a -> (a, b))
forall a. Graph a
empty ((a -> (a, b)) -> Graph (a -> (a, b))
forall a. a -> Graph a
vertex ((a -> (a, b)) -> Graph (a -> (a, b)))
-> (b -> a -> (a, b)) -> b -> Graph (a -> (a, b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> (a, b)) -> b -> a -> (a, b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (,)) Graph (a -> (a, b)) -> Graph (a -> (a, b)) -> Graph (a -> (a, b))
forall a. Graph a -> Graph a -> Graph a
overlay Graph (a -> (a, b)) -> Graph (a -> (a, b)) -> Graph (a -> (a, b))
forall a. Graph a -> Graph a -> Graph a
overlay Graph b
y
sparsify :: Graph a -> Graph (Either Int a)
sparsify :: Graph a -> Graph (Either Int a)
sparsify Graph a
graph = Graph (Either Int a)
res
where
(Graph (Either Int a)
res, Int
end) = State Int (Graph (Either Int a))
-> Int -> (Graph (Either Int a), Int)
forall s a. State s a -> s -> (a, s)
runState ((Int -> Int -> State Int (Graph (Either Int a)))
-> (a -> Int -> Int -> State Int (Graph (Either Int a)))
-> ((Int -> Int -> State Int (Graph (Either Int a)))
-> (Int -> Int -> State Int (Graph (Either Int a)))
-> Int
-> Int
-> State Int (Graph (Either Int a)))
-> ((Int -> Int -> State Int (Graph (Either Int a)))
-> (Int -> Int -> State Int (Graph (Either Int a)))
-> Int
-> Int
-> State Int (Graph (Either Int a)))
-> Graph a
-> Int
-> Int
-> State Int (Graph (Either Int a))
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Int -> Int -> State Int (Graph (Either Int a))
forall (m :: * -> *) a b.
Monad m =>
a -> a -> m (Graph (Either a b))
e a -> Int -> Int -> State Int (Graph (Either Int a))
forall (m :: * -> *) b a.
Monad m =>
b -> a -> a -> m (Graph (Either a b))
v (Int -> Int -> State Int (Graph (Either Int a)))
-> (Int -> Int -> State Int (Graph (Either Int a)))
-> Int
-> Int
-> State Int (Graph (Either Int a))
forall (f :: * -> *) t t a.
Applicative f =>
(t -> t -> f (Graph a))
-> (t -> t -> f (Graph a)) -> t -> t -> f (Graph a)
o (Int -> Int -> State Int (Graph (Either Int a)))
-> (Int -> Int -> State Int (Graph (Either Int a)))
-> Int
-> Int
-> State Int (Graph (Either Int a))
forall (m :: * -> *) t t a t.
(MonadState t m, Num t) =>
(t -> t -> m (Graph a))
-> (t -> t -> m (Graph a)) -> t -> t -> m (Graph a)
c Graph a
graph Int
0 Int
end) Int
1
e :: a -> a -> m (Graph (Either a b))
e a
s a
t = Graph (Either a b) -> m (Graph (Either a b))
forall (m :: * -> *) a. Monad m => a -> m a
return (Graph (Either a b) -> m (Graph (Either a b)))
-> Graph (Either a b) -> m (Graph (Either a b))
forall a b. (a -> b) -> a -> b
$ [Either a b] -> Graph (Either a b)
forall a. [a] -> Graph a
path [a -> Either a b
forall a b. a -> Either a b
Left a
s, a -> Either a b
forall a b. a -> Either a b
Left a
t]
v :: b -> a -> a -> m (Graph (Either a b))
v b
x a
s a
t = Graph (Either a b) -> m (Graph (Either a b))
forall (m :: * -> *) a. Monad m => a -> m a
return (Graph (Either a b) -> m (Graph (Either a b)))
-> Graph (Either a b) -> m (Graph (Either a b))
forall a b. (a -> b) -> a -> b
$ [Either a b] -> Graph (Either a b)
forall a. [a] -> Graph a
clique [a -> Either a b
forall a b. a -> Either a b
Left a
s, b -> Either a b
forall a b. b -> Either a b
Right b
x, a -> Either a b
forall a b. a -> Either a b
Left a
t]
o :: (t -> t -> f (Graph a))
-> (t -> t -> f (Graph a)) -> t -> t -> f (Graph a)
o t -> t -> f (Graph a)
x t -> t -> f (Graph a)
y t
s t
t = Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
overlay (Graph a -> Graph a -> Graph a)
-> f (Graph a) -> f (Graph a -> Graph a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t
s t -> t -> f (Graph a)
`x` t
t f (Graph a -> Graph a) -> f (Graph a) -> f (Graph a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t
s t -> t -> f (Graph a)
`y` t
t
c :: (t -> t -> m (Graph a))
-> (t -> t -> m (Graph a)) -> t -> t -> m (Graph a)
c t -> t -> m (Graph a)
x t -> t -> m (Graph a)
y t
s t
t = do
t
m <- m t
forall s (m :: * -> *). MonadState s m => m s
get
t -> m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put (t
m t -> t -> t
forall a. Num a => a -> a -> a
+ t
1)
Graph a -> Graph a -> Graph a
forall a. Graph a -> Graph a -> Graph a
overlay (Graph a -> Graph a -> Graph a)
-> m (Graph a) -> m (Graph a -> Graph a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t
s t -> t -> m (Graph a)
`x` t
m m (Graph a -> Graph a) -> m (Graph a) -> m (Graph a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t
m t -> t -> m (Graph a)
`y` t
t
sparsifyKL :: Int -> Graph Int -> KL.Graph
sparsifyKL :: Int -> Graph Int -> Graph
sparsifyKL Int
n Graph Int
graph = (Int, Int) -> [(Int, Int)] -> Graph
KL.buildG (Int
1, Int
next Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ((Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) (Int, Int) -> [(Int, Int)] -> [(Int, Int)]
forall a. a -> [a] -> [a]
: List (Int, Int) -> [Item (List (Int, Int))]
forall l. IsList l => l -> [Item l]
Exts.toList (List (Int, Int)
res :: List KL.Edge))
where
(List (Int, Int)
res, Int
next) = State Int (List (Int, Int)) -> Int -> (List (Int, Int), Int)
forall s a. State s a -> s -> (a, s)
runState ((Int -> Int -> State Int (List (Int, Int)))
-> (Int -> Int -> Int -> State Int (List (Int, Int)))
-> ((Int -> Int -> State Int (List (Int, Int)))
-> (Int -> Int -> State Int (List (Int, Int)))
-> Int
-> Int
-> State Int (List (Int, Int)))
-> ((Int -> Int -> State Int (List (Int, Int)))
-> (Int -> Int -> State Int (List (Int, Int)))
-> Int
-> Int
-> State Int (List (Int, Int)))
-> Graph Int
-> Int
-> Int
-> State Int (List (Int, Int))
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Int -> Int -> State Int (List (Int, Int))
forall (m :: * -> *) a p p. (Monad m, IsList a) => p -> p -> m a
e Int -> Int -> Int -> State Int (List (Int, Int))
forall (m :: * -> *) a b.
(Monad m, IsList a, Item a ~ (b, b)) =>
b -> b -> b -> m a
v (Int -> Int -> State Int (List (Int, Int)))
-> (Int -> Int -> State Int (List (Int, Int)))
-> Int
-> Int
-> State Int (List (Int, Int))
forall (f :: * -> *) b t t.
(Applicative f, Semigroup b) =>
(t -> t -> f b) -> (t -> t -> f b) -> t -> t -> f b
o (Int -> Int -> State Int (List (Int, Int)))
-> (Int -> Int -> State Int (List (Int, Int)))
-> Int
-> Int
-> State Int (List (Int, Int))
forall (m :: * -> *) t b.
(MonadState t m, Num t, Semigroup b, IsList b, Item b ~ (t, t)) =>
(t -> t -> m b) -> (t -> t -> m b) -> t -> t -> m b
c Graph Int
graph (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)) (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3)
e :: p -> p -> m a
e p
_ p
_ = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$ [Item a] -> a
forall l. IsList l => [Item l] -> l
Exts.fromList []
v :: b -> b -> b -> m a
v b
x b
s b
t = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$ [Item a] -> a
forall l. IsList l => [Item l] -> l
Exts.fromList [(b
s,b
x), (b
x,b
t)]
o :: (t -> t -> f b) -> (t -> t -> f b) -> t -> t -> f b
o t -> t -> f b
x t -> t -> f b
y t
s t
t = b -> b -> b
forall a. Semigroup a => a -> a -> a
(<>) (b -> b -> b) -> f b -> f (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t
s t -> t -> f b
`x` t
t f (b -> b) -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t
s t -> t -> f b
`y` t
t
c :: (t -> t -> m b) -> (t -> t -> m b) -> t -> t -> m b
c t -> t -> m b
x t -> t -> m b
y t
s t
t = do
t
m <- m t
forall s (m :: * -> *). MonadState s m => m s
get
t -> m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put (t
m t -> t -> t
forall a. Num a => a -> a -> a
+ t
1)
(\b
xs b
ys -> [Item b] -> b
forall l. IsList l => [Item l] -> l
Exts.fromList [(t
s,t
m), (t
m,t
t)] b -> b -> b
forall a. Semigroup a => a -> a -> a
<> b
xs b -> b -> b
forall a. Semigroup a => a -> a -> a
<> b
ys) (b -> b -> b) -> m b -> m (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t
s t -> t -> m b
`x` t
m m (b -> b) -> m b -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t
m t -> t -> m b
`y` t
t
composeR :: (b -> c) -> (a -> b) -> a -> c
composeR :: (b -> c) -> (a -> b) -> a -> c
composeR = (b -> c) -> (a -> b) -> a -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.)
{-# INLINE [1] composeR #-}
{-# RULES
-- Fuse a 'foldg' followed by a 'buildg':
"foldg/buildg" forall e v o c (g :: forall b. b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> b).
foldg e v o c (buildg g) = g e v o c
-- Fuse 'composeR' chains (see the definition of the bind operator).
"composeR/composeR" forall c f g.
composeR (composeR c f) g = composeR c (f . g)
-- Rewrite identity (which can appear in the inlining of 'buildg') to a more
-- efficient one.
"foldg/id"
foldg Empty Vertex Overlay Connect = id
#-}
focus :: (a -> Bool) -> Graph a -> Focus a
focus :: (a -> Bool) -> Graph a -> Focus a
focus a -> Bool
f = Focus a
-> (a -> Focus a)
-> (Focus a -> Focus a -> Focus a)
-> (Focus a -> Focus a -> Focus a)
-> Graph a
-> Focus a
forall b a.
b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Graph a -> b
foldg Focus a
forall a. Focus a
emptyFocus ((a -> Bool) -> a -> Focus a
forall a. (a -> Bool) -> a -> Focus a
vertexFocus a -> Bool
f) Focus a -> Focus a -> Focus a
forall a. Focus a -> Focus a -> Focus a
overlayFoci Focus a -> Focus a -> Focus a
forall a. Focus a -> Focus a -> Focus a
connectFoci
{-# INLINE focus #-}
data Context a = Context { Context a -> [a]
inputs :: [a], Context a -> [a]
outputs :: [a] }
deriving (Context a -> Context a -> Bool
(Context a -> Context a -> Bool)
-> (Context a -> Context a -> Bool) -> Eq (Context a)
forall a. Eq a => Context a -> Context a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Context a -> Context a -> Bool
$c/= :: forall a. Eq a => Context a -> Context a -> Bool
== :: Context a -> Context a -> Bool
$c== :: forall a. Eq a => Context a -> Context a -> Bool
Eq, Int -> Context a -> ShowS
[Context a] -> ShowS
Context a -> String
(Int -> Context a -> ShowS)
-> (Context a -> String)
-> ([Context a] -> ShowS)
-> Show (Context a)
forall a. Show a => Int -> Context a -> ShowS
forall a. Show a => [Context a] -> ShowS
forall a. Show a => Context a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Context a] -> ShowS
$cshowList :: forall a. Show a => [Context a] -> ShowS
show :: Context a -> String
$cshow :: forall a. Show a => Context a -> String
showsPrec :: Int -> Context a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Context a -> ShowS
Show)
context :: (a -> Bool) -> Graph a -> Maybe (Context a)
context :: (a -> Bool) -> Graph a -> Maybe (Context a)
context a -> Bool
p Graph a
g | Focus a -> Bool
forall a. Focus a -> Bool
ok Focus a
f = Context a -> Maybe (Context a)
forall a. a -> Maybe a
Just (Context a -> Maybe (Context a)) -> Context a -> Maybe (Context a)
forall a b. (a -> b) -> a -> b
$ [a] -> [a] -> Context a
forall a. [a] -> [a] -> Context a
Context (List a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (List a -> [a]) -> List a -> [a]
forall a b. (a -> b) -> a -> b
$ Focus a -> List a
forall a. Focus a -> List a
is Focus a
f) (List a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (List a -> [a]) -> List a -> [a]
forall a b. (a -> b) -> a -> b
$ Focus a -> List a
forall a. Focus a -> List a
os Focus a
f)
| Bool
otherwise = Maybe (Context a)
forall a. Maybe a
Nothing
where
f :: Focus a
f = (a -> Bool) -> Graph a -> Focus a
forall a. (a -> Bool) -> Graph a -> Focus a
focus a -> Bool
p Graph a
g
{-# INLINE context #-}