module Algebra.Graph.Relation (
Relation, domain, relation,
empty, vertex, edge, overlay, connect, vertices, edges, overlays, connects,
isSubgraphOf,
isEmpty, hasVertex, hasEdge, vertexCount, edgeCount, vertexList, edgeList,
adjacencyList, vertexSet, edgeSet, preSet, postSet,
path, circuit, clique, biclique, star, stars, tree, forest,
removeVertex, removeEdge, replaceVertex, mergeVertices, transpose, gmap,
induce, induceJust,
compose, closure, reflexiveClosure, symmetricClosure, transitiveClosure,
consistent
) where
import Control.DeepSeq
import Data.Bifunctor
import Data.Set (Set, union)
import Data.String
import Data.Tree
import Data.Tuple
import qualified Data.IntSet as IntSet
import qualified Data.Maybe as Maybe
import qualified Data.Set as Set
import qualified Data.Tree as Tree
import Algebra.Graph.Internal
import qualified Algebra.Graph as G
import qualified Algebra.Graph.AdjacencyIntMap as AIM
import qualified Algebra.Graph.AdjacencyMap as AM
import qualified Algebra.Graph.ToGraph as T
data Relation a = Relation {
forall a. Relation a -> Set a
domain :: Set a,
forall a. Relation a -> Set (a, a)
relation :: Set (a, a)
} deriving Relation a -> Relation a -> Bool
forall a. Eq a => Relation a -> Relation a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Relation a -> Relation a -> Bool
$c/= :: forall a. Eq a => Relation a -> Relation a -> Bool
== :: Relation a -> Relation a -> Bool
$c== :: forall a. Eq a => Relation a -> Relation a -> Bool
Eq
instance (Ord a, Show a) => Show (Relation a) where
showsPrec :: Int -> Relation a -> ShowS
showsPrec Int
p (Relation Set a
d Set (a, a)
r)
| forall a. Set a -> Bool
Set.null Set a
d = String -> ShowS
showString String
"empty"
| forall a. Set a -> Bool
Set.null Set (a, a)
r = Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$ forall {a}. Show a => [a] -> ShowS
vshow (forall a. Set a -> [a]
Set.toAscList Set a
d)
| Set a
d forall a. Eq a => a -> a -> Bool
== Set a
used = Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$ forall {a} {a}. (Show a, Show a) => [(a, a)] -> ShowS
eshow (forall a. Set a -> [a]
Set.toAscList Set (a, a)
r)
| Bool
otherwise = Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"overlay (" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall {a}. Show a => [a] -> ShowS
vshow (forall a. Set a -> [a]
Set.toAscList forall a b. (a -> b) -> a -> b
$ forall a. Ord a => Set a -> Set a -> Set a
Set.difference Set a
d Set a
used) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> ShowS
showString String
") (" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a} {a}. (Show a, Show a) => [(a, a)] -> ShowS
eshow (forall a. Set a -> [a]
Set.toAscList Set (a, a)
r) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> ShowS
showString String
")"
where
vshow :: [a] -> ShowS
vshow [a
x] = String -> ShowS
showString String
"vertex " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 a
x
vshow [a]
xs = String -> ShowS
showString String
"vertices " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 [a]
xs
eshow :: [(a, a)] -> ShowS
eshow [(a
x, a
y)] = String -> ShowS
showString String
"edge " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> ShowS
showString String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 a
y
eshow [(a, a)]
xs = String -> ShowS
showString String
"edges " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 [(a, a)]
xs
used :: Set a
used = forall a. Ord a => Set (a, a) -> Set a
referredToVertexSet Set (a, a)
r
instance Ord a => Ord (Relation a) where
compare :: Relation a -> Relation a -> Ordering
compare Relation a
x Relation a
y = forall a. Monoid a => [a] -> a
mconcat
[ forall a. Ord a => a -> a -> Ordering
compare (forall a. Relation a -> Int
vertexCount Relation a
x) (forall a. Relation a -> Int
vertexCount Relation a
y)
, forall a. Ord a => a -> a -> Ordering
compare (forall a. Relation a -> Set a
vertexSet Relation a
x) (forall a. Relation a -> Set a
vertexSet Relation a
y)
, forall a. Ord a => a -> a -> Ordering
compare (forall a. Relation a -> Int
edgeCount Relation a
x) (forall a. Relation a -> Int
edgeCount Relation a
y)
, forall a. Ord a => a -> a -> Ordering
compare (forall a. Relation a -> Set (a, a)
edgeSet Relation a
x) (forall a. Relation a -> Set (a, a)
edgeSet Relation a
y) ]
instance NFData a => NFData (Relation a) where
rnf :: Relation a -> ()
rnf (Relation Set a
d Set (a, a)
r) = forall a. NFData a => a -> ()
rnf Set a
d seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf Set (a, a)
r
instance (Ord a, Num a) => Num (Relation a) where
fromInteger :: Integer -> Relation a
fromInteger = forall a. a -> Relation a
vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger
+ :: Relation a -> Relation a -> Relation a
(+) = forall a. Ord a => Relation a -> Relation a -> Relation a
overlay
* :: Relation a -> Relation a -> Relation a
(*) = forall a. Ord a => Relation a -> Relation a -> Relation a
connect
signum :: Relation a -> Relation a
signum = forall a b. a -> b -> a
const forall a. Relation a
empty
abs :: Relation a -> Relation a
abs = forall a. a -> a
id
negate :: Relation a -> Relation a
negate = forall a. a -> a
id
instance IsString a => IsString (Relation a) where
fromString :: String -> Relation a
fromString = forall a. a -> Relation a
vertex forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString
instance Ord a => Semigroup (Relation a) where
<> :: Relation a -> Relation a -> Relation a
(<>) = forall a. Ord a => Relation a -> Relation a -> Relation a
overlay
instance Ord a => Monoid (Relation a) where
mempty :: Relation a
mempty = forall a. Relation a
empty
instance Ord a => T.ToGraph (Relation a) where
type ToVertex (Relation a) = a
toGraph :: Relation a -> Graph (ToVertex (Relation a))
toGraph Relation a
r = forall a. [a] -> Graph a
G.vertices (forall a. Set a -> [a]
Set.toList forall a b. (a -> b) -> a -> b
$ forall a. Relation a -> Set a
domain Relation a
r) forall a. Graph a -> Graph a -> Graph a
`G.overlay`
forall a. [(a, a)] -> Graph a
G.edges (forall a. Set a -> [a]
Set.toList forall a b. (a -> b) -> a -> b
$ forall a. Relation a -> Set (a, a)
relation Relation a
r)
isEmpty :: Relation a -> Bool
isEmpty = forall a. Relation a -> Bool
isEmpty
hasVertex :: Eq (ToVertex (Relation a)) =>
ToVertex (Relation a) -> Relation a -> Bool
hasVertex = forall a. Ord a => a -> Relation a -> Bool
hasVertex
hasEdge :: Eq (ToVertex (Relation a)) =>
ToVertex (Relation a)
-> ToVertex (Relation a) -> Relation a -> Bool
hasEdge = forall a. Ord a => a -> a -> Relation a -> Bool
hasEdge
vertexCount :: Ord (ToVertex (Relation a)) => Relation a -> Int
vertexCount = forall a. Relation a -> Int
vertexCount
edgeCount :: Ord (ToVertex (Relation a)) => Relation a -> Int
edgeCount = forall a. Relation a -> Int
edgeCount
vertexList :: Ord (ToVertex (Relation a)) =>
Relation a -> [ToVertex (Relation a)]
vertexList = forall a. Relation a -> [a]
vertexList
vertexSet :: Ord (ToVertex (Relation a)) =>
Relation a -> Set (ToVertex (Relation a))
vertexSet = forall a. Relation a -> Set a
vertexSet
vertexIntSet :: (ToVertex (Relation a) ~ Int) => Relation a -> IntSet
vertexIntSet = [Int] -> IntSet
IntSet.fromAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Relation a -> [a]
vertexList
edgeList :: Ord (ToVertex (Relation a)) =>
Relation a -> [(ToVertex (Relation a), ToVertex (Relation a))]
edgeList = forall a. Relation a -> [(a, a)]
edgeList
edgeSet :: Ord (ToVertex (Relation a)) =>
Relation a -> Set (ToVertex (Relation a), ToVertex (Relation a))
edgeSet = forall a. Relation a -> Set (a, a)
edgeSet
adjacencyList :: Ord (ToVertex (Relation a)) =>
Relation a -> [(ToVertex (Relation a), [ToVertex (Relation a)])]
adjacencyList = forall a. Eq a => Relation a -> [(a, [a])]
adjacencyList
toAdjacencyMap :: Ord (ToVertex (Relation a)) =>
Relation a -> AdjacencyMap (ToVertex (Relation a))
toAdjacencyMap = forall a. Ord a => [(a, [a])] -> AdjacencyMap a
AM.stars forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => Relation a -> [(a, [a])]
adjacencyList
toAdjacencyIntMap :: (ToVertex (Relation a) ~ Int) => Relation a -> AdjacencyIntMap
toAdjacencyIntMap = [(Int, [Int])] -> AdjacencyIntMap
AIM.stars forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => Relation a -> [(a, [a])]
adjacencyList
toAdjacencyMapTranspose :: Ord (ToVertex (Relation a)) =>
Relation a -> AdjacencyMap (ToVertex (Relation a))
toAdjacencyMapTranspose = forall a. Ord a => AdjacencyMap a -> AdjacencyMap a
AM.transpose forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t.
(ToGraph t, Ord (ToVertex t)) =>
t -> AdjacencyMap (ToVertex t)
T.toAdjacencyMap
toAdjacencyIntMapTranspose :: (ToVertex (Relation a) ~ Int) => Relation a -> AdjacencyIntMap
toAdjacencyIntMapTranspose = AdjacencyIntMap -> AdjacencyIntMap
AIM.transpose forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. (ToGraph t, ToVertex t ~ Int) => t -> AdjacencyIntMap
T.toAdjacencyIntMap
empty :: Relation a
empty :: forall a. Relation a
empty = forall a. Set a -> Set (a, a) -> Relation a
Relation forall a. Set a
Set.empty forall a. Set a
Set.empty
vertex :: a -> Relation a
vertex :: forall a. a -> Relation a
vertex a
x = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall a. a -> Set a
Set.singleton a
x) forall a. Set a
Set.empty
edge :: Ord a => a -> a -> Relation a
edge :: forall a. Ord a => a -> a -> Relation a
edge a
x a
y = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall a. Ord a => [a] -> Set a
Set.fromList [a
x, a
y]) (forall a. a -> Set a
Set.singleton (a
x, a
y))
overlay :: Ord a => Relation a -> Relation a -> Relation a
overlay :: forall a. Ord a => Relation a -> Relation a -> Relation a
overlay Relation a
x Relation a
y = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall a. Relation a -> Set a
domain Relation a
x forall a. Ord a => Set a -> Set a -> Set a
`union` forall a. Relation a -> Set a
domain Relation a
y) (forall a. Relation a -> Set (a, a)
relation Relation a
x forall a. Ord a => Set a -> Set a -> Set a
`union` forall a. Relation a -> Set (a, a)
relation Relation a
y)
connect :: Ord a => Relation a -> Relation a -> Relation a
connect :: forall a. Ord a => Relation a -> Relation a -> Relation a
connect Relation a
x Relation a
y = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall a. Relation a -> Set a
domain Relation a
x forall a. Ord a => Set a -> Set a -> Set a
`union` forall a. Relation a -> Set a
domain Relation a
y)
(forall a. Relation a -> Set (a, a)
relation Relation a
x forall a. Ord a => Set a -> Set a -> Set a
`union` forall a. Relation a -> Set (a, a)
relation Relation a
y forall a. Ord a => Set a -> Set a -> Set a
`union` (forall a. Relation a -> Set a
domain Relation a
x forall a b. Set a -> Set b -> Set (a, b)
`Set.cartesianProduct` forall a. Relation a -> Set a
domain Relation a
y))
vertices :: Ord a => [a] -> Relation a
vertices :: forall a. Ord a => [a] -> Relation a
vertices [a]
xs = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall a. Ord a => [a] -> Set a
Set.fromList [a]
xs) forall a. Set a
Set.empty
edges :: Ord a => [(a, a)] -> Relation a
edges :: forall a. Ord a => [(a, a)] -> Relation a
edges [(a, a)]
es = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall a. Ord a => [a] -> Set a
Set.fromList forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. [a] -> [a] -> [a]
(++) forall a b. (a -> b) -> a -> b
$ forall a b. [(a, b)] -> ([a], [b])
unzip [(a, a)]
es) (forall a. Ord a => [a] -> Set a
Set.fromList [(a, a)]
es)
overlays :: Ord a => [Relation a] -> Relation a
overlays :: forall a. Ord a => [Relation a] -> Relation a
overlays [Relation a]
xs = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. Relation a -> Set a
domain [Relation a]
xs) (forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. Relation a -> Set (a, a)
relation [Relation a]
xs)
connects :: Ord a => [Relation a] -> Relation a
connects :: forall a. Ord a => [Relation a] -> Relation a
connects = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. Ord a => Relation a -> Relation a -> Relation a
connect forall a. Relation a
empty
isSubgraphOf :: Ord a => Relation a -> Relation a -> Bool
isSubgraphOf :: forall a. Ord a => Relation a -> Relation a -> Bool
isSubgraphOf Relation a
x Relation a
y = forall a. Relation a -> Set a
domain Relation a
x forall a. Ord a => Set a -> Set a -> Bool
`Set.isSubsetOf` forall a. Relation a -> Set a
domain Relation a
y
Bool -> Bool -> Bool
&& forall a. Relation a -> Set (a, a)
relation Relation a
x forall a. Ord a => Set a -> Set a -> Bool
`Set.isSubsetOf` forall a. Relation a -> Set (a, a)
relation Relation a
y
isEmpty :: Relation a -> Bool
isEmpty :: forall a. Relation a -> Bool
isEmpty = forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Relation a -> Set a
domain
hasVertex :: Ord a => a -> Relation a -> Bool
hasVertex :: forall a. Ord a => a -> Relation a -> Bool
hasVertex a
x = forall a. Ord a => a -> Set a -> Bool
Set.member a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Relation a -> Set a
domain
hasEdge :: Ord a => a -> a -> Relation a -> Bool
hasEdge :: forall a. Ord a => a -> a -> Relation a -> Bool
hasEdge a
x a
y = forall a. Ord a => a -> Set a -> Bool
Set.member (a
x, a
y) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Relation a -> Set (a, a)
relation
vertexCount :: Relation a -> Int
vertexCount :: forall a. Relation a -> Int
vertexCount = forall a. Set a -> Int
Set.size forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Relation a -> Set a
domain
edgeCount :: Relation a -> Int
edgeCount :: forall a. Relation a -> Int
edgeCount = forall a. Set a -> Int
Set.size forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Relation a -> Set (a, a)
relation
vertexList :: Relation a -> [a]
vertexList :: forall a. Relation a -> [a]
vertexList = forall a. Set a -> [a]
Set.toAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Relation a -> Set a
domain
edgeList :: Relation a -> [(a, a)]
edgeList :: forall a. Relation a -> [(a, a)]
edgeList = forall a. Set a -> [a]
Set.toAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Relation a -> Set (a, a)
relation
vertexSet :: Relation a -> Set.Set a
vertexSet :: forall a. Relation a -> Set a
vertexSet = forall a. Relation a -> Set a
domain
edgeSet :: Relation a -> Set.Set (a, a)
edgeSet :: forall a. Relation a -> Set (a, a)
edgeSet = forall a. Relation a -> Set (a, a)
relation
adjacencyList :: Eq a => Relation a -> [(a, [a])]
adjacencyList :: forall a. Eq a => Relation a -> [(a, [a])]
adjacencyList Relation a
r = forall {a} {a}. Eq a => [a] -> [(a, a)] -> [(a, [a])]
go (forall a. Set a -> [a]
Set.toAscList forall a b. (a -> b) -> a -> b
$ forall a. Relation a -> Set a
domain Relation a
r) (forall a. Set a -> [a]
Set.toAscList forall a b. (a -> b) -> a -> b
$ forall a. Relation a -> Set (a, a)
relation Relation a
r)
where
go :: [a] -> [(a, a)] -> [(a, [a])]
go [] [(a, a)]
_ = []
go [a]
vs [] = forall a b. (a -> b) -> [a] -> [b]
map (, []) [a]
vs
go (a
x:[a]
vs) [(a, a)]
es = let ([(a, a)]
ys, [(a, a)]
zs) = forall a. (a -> Bool) -> [a] -> ([a], [a])
span ((forall a. Eq a => a -> a -> Bool
==a
x) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(a, a)]
es in (a
x, forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd [(a, a)]
ys) forall a. a -> [a] -> [a]
: [a] -> [(a, a)] -> [(a, [a])]
go [a]
vs [(a, a)]
zs
preSet :: Ord a => a -> Relation a -> Set.Set a
preSet :: forall a. Ord a => a -> Relation a -> Set a
preSet a
x = forall a b. (a -> b) -> Set a -> Set b
Set.mapMonotonic forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> Set a -> Set a
Set.filter ((forall a. Eq a => a -> a -> Bool
== a
x) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Relation a -> Set (a, a)
relation
postSet :: Ord a => a -> Relation a -> Set.Set a
postSet :: forall a. Ord a => a -> Relation a -> Set a
postSet a
x = forall a b. (a -> b) -> Set a -> Set b
Set.mapMonotonic forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> Set a -> Set a
Set.filter ((forall a. Eq a => a -> a -> Bool
== a
x) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Relation a -> Set (a, a)
relation
path :: Ord a => [a] -> Relation a
path :: forall a. Ord a => [a] -> Relation a
path [a]
xs = case [a]
xs of [] -> forall a. Relation a
empty
[a
x] -> forall a. a -> Relation a
vertex a
x
(a
_:[a]
ys) -> forall a. Ord a => [(a, a)] -> Relation a
edges (forall a b. [a] -> [b] -> [(a, b)]
zip [a]
xs [a]
ys)
circuit :: Ord a => [a] -> Relation a
circuit :: forall a. Ord a => [a] -> Relation a
circuit [] = forall a. Relation a
empty
circuit (a
x:[a]
xs) = forall a. Ord a => [a] -> Relation a
path forall a b. (a -> b) -> a -> b
$ [a
x] forall a. [a] -> [a] -> [a]
++ [a]
xs forall a. [a] -> [a] -> [a]
++ [a
x]
clique :: Ord a => [a] -> Relation a
clique :: forall a. Ord a => [a] -> Relation a
clique [a]
xs = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall a. Ord a => [a] -> Set a
Set.fromList [a]
xs) (forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall {a}. Ord a => [a] -> (Set (a, a), Set a)
go [a]
xs)
where
go :: [a] -> (Set (a, a), Set a)
go [] = (forall a. Set a
Set.empty, forall a. Set a
Set.empty)
go (a
x:[a]
xs) = (forall a. Ord a => Set a -> Set a -> Set a
Set.union Set (a, a)
res (forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map (a
x,) Set a
set), forall a. Ord a => a -> Set a -> Set a
Set.insert a
x Set a
set)
where
(Set (a, a)
res, Set a
set) = [a] -> (Set (a, a), Set a)
go [a]
xs
biclique :: Ord a => [a] -> [a] -> Relation a
biclique :: forall a. Ord a => [a] -> [a] -> Relation a
biclique [a]
xs [a]
ys = forall a. Set a -> Set (a, a) -> Relation a
Relation (Set a
x forall a. Ord a => Set a -> Set a -> Set a
`Set.union` Set a
y) (Set a
x forall a b. Set a -> Set b -> Set (a, b)
`Set.cartesianProduct` Set a
y)
where
x :: Set a
x = forall a. Ord a => [a] -> Set a
Set.fromList [a]
xs
y :: Set a
y = forall a. Ord a => [a] -> Set a
Set.fromList [a]
ys
star :: Ord a => a -> [a] -> Relation a
star :: forall a. Ord a => a -> [a] -> Relation a
star a
x [] = forall a. a -> Relation a
vertex a
x
star a
x [a]
ys = forall a. Ord a => Relation a -> Relation a -> Relation a
connect (forall a. a -> Relation a
vertex a
x) (forall a. Ord a => [a] -> Relation a
vertices [a]
ys)
stars :: Ord a => [(a, [a])] -> Relation a
stars :: forall a. Ord a => [(a, [a])] -> Relation a
stars [(a, [a])]
as = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall a. Ord a => [a] -> Set a
Set.fromList [a]
vs) (forall a. Ord a => [a] -> Set a
Set.fromList [(a, a)]
es)
where
vs :: [a]
vs = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (:)) [(a, [a])]
as
es :: [(a, a)]
es = [ (a
x, a
y) | (a
x, [a]
ys) <- [(a, [a])]
as, a
y <- [a]
ys ]
tree :: Ord a => Tree.Tree a -> Relation a
tree :: forall a. Ord a => Tree a -> Relation a
tree (Node a
x []) = forall a. a -> Relation a
vertex a
x
tree (Node a
x [Tree a]
f ) = forall a. Ord a => a -> [a] -> Relation a
star a
x (forall a b. (a -> b) -> [a] -> [b]
map forall a. Tree a -> a
rootLabel [Tree a]
f)
forall a. Ord a => Relation a -> Relation a -> Relation a
`overlay` forall a. Ord a => Forest a -> Relation a
forest (forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Tree a -> [Tree a]
subForest) [Tree a]
f)
forest :: Ord a => Tree.Forest a -> Relation a
forest :: forall a. Ord a => Forest a -> Relation a
forest = forall a. Ord a => [Relation a] -> Relation a
overlaysforall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall a. Ord a => Tree a -> Relation a
tree
removeVertex :: Ord a => a -> Relation a -> Relation a
removeVertex :: forall a. Ord a => a -> Relation a -> Relation a
removeVertex a
x (Relation Set a
d Set (a, a)
r) = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall a. Ord a => a -> Set a -> Set a
Set.delete a
x Set a
d) (forall a. (a -> Bool) -> Set a -> Set a
Set.filter (a, a) -> Bool
notx Set (a, a)
r)
where
notx :: (a, a) -> Bool
notx (a
a, a
b) = a
a forall a. Eq a => a -> a -> Bool
/= a
x Bool -> Bool -> Bool
&& a
b forall a. Eq a => a -> a -> Bool
/= a
x
removeEdge :: Ord a => a -> a -> Relation a -> Relation a
removeEdge :: forall a. Ord a => a -> a -> Relation a -> Relation a
removeEdge a
x a
y (Relation Set a
d Set (a, a)
r) = forall a. Set a -> Set (a, a) -> Relation a
Relation Set a
d (forall a. Ord a => a -> Set a -> Set a
Set.delete (a
x, a
y) Set (a, a)
r)
replaceVertex :: Ord a => a -> a -> Relation a -> Relation a
replaceVertex :: forall a. Ord a => a -> a -> Relation a -> Relation a
replaceVertex a
u a
v = forall b a. Ord b => (a -> b) -> Relation a -> Relation b
gmap 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
mergeVertices :: Ord a => (a -> Bool) -> a -> Relation a -> Relation a
mergeVertices :: forall a. Ord a => (a -> Bool) -> a -> Relation a -> Relation a
mergeVertices a -> Bool
p a
v = forall b a. Ord b => (a -> b) -> Relation a -> Relation b
gmap forall a b. (a -> b) -> a -> b
$ \a
u -> if a -> Bool
p a
u then a
v else a
u
transpose :: Ord a => Relation a -> Relation a
transpose :: forall a. Ord a => Relation a -> Relation a
transpose (Relation Set a
d Set (a, a)
r) = forall a. Set a -> Set (a, a) -> Relation a
Relation Set a
d (forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map forall a b. (a, b) -> (b, a)
swap Set (a, a)
r)
gmap :: Ord b => (a -> b) -> Relation a -> Relation b
gmap :: forall b a. Ord b => (a -> b) -> Relation a -> Relation b
gmap a -> b
f (Relation Set a
d Set (a, a)
r) = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map a -> b
f Set a
d) (forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map (forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> b
f a -> b
f) Set (a, a)
r)
induce :: (a -> Bool) -> Relation a -> Relation a
induce :: forall a. (a -> Bool) -> Relation a -> Relation a
induce a -> Bool
p (Relation Set a
d Set (a, a)
r) = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall a. (a -> Bool) -> Set a -> Set a
Set.filter a -> Bool
p Set a
d) (forall a. (a -> Bool) -> Set a -> Set a
Set.filter (a, a) -> Bool
pp Set (a, a)
r)
where
pp :: (a, a) -> Bool
pp (a
x, a
y) = a -> Bool
p a
x Bool -> Bool -> Bool
&& a -> Bool
p a
y
induceJust :: Ord a => Relation (Maybe a) -> Relation a
induceJust :: forall a. Ord a => Relation (Maybe a) -> Relation a
induceJust (Relation Set (Maybe a)
d Set (Maybe a, Maybe a)
r) = forall a. Set a -> Set (a, a) -> Relation a
Relation (Set (Maybe a) -> Set a
catMaybesSet Set (Maybe a)
d) (forall {b} {d}. Set (Maybe b, Maybe d) -> Set (b, d)
catMaybesSet2 Set (Maybe a, Maybe a)
r)
where
catMaybesSet :: Set (Maybe a) -> Set a
catMaybesSet = forall a b. (a -> b) -> Set a -> Set b
Set.mapMonotonic forall a. HasCallStack => Maybe a -> a
Maybe.fromJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => a -> Set a -> Set a
Set.delete forall a. Maybe a
Nothing
catMaybesSet2 :: Set (Maybe b, Maybe d) -> Set (b, d)
catMaybesSet2 = forall a b. (a -> b) -> Set a -> Set b
Set.mapMonotonic (forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap forall a. HasCallStack => Maybe a -> a
Maybe.fromJust forall a. HasCallStack => Maybe a -> a
Maybe.fromJust)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> Set a -> Set a
Set.filter forall {a} {a}. (Maybe a, Maybe a) -> Bool
p
p :: (Maybe a, Maybe a) -> Bool
p (Maybe a
Nothing, Maybe a
_) = Bool
False
p (Maybe a
_, Maybe a
Nothing) = Bool
False
p (Maybe a
_, Maybe a
_) = Bool
True
compose :: Ord a => Relation a -> Relation a -> Relation a
compose :: forall a. Ord a => Relation a -> Relation a -> Relation a
compose Relation a
x Relation a
y = forall a. Set a -> Set (a, a) -> Relation a
Relation (forall a. Ord a => Set (a, a) -> Set a
referredToVertexSet Set (a, a)
r) Set (a, a)
r
where
vs :: [a]
vs = forall a. Set a -> [a]
Set.toAscList (forall a. Relation a -> Set a
domain Relation a
x forall a. Ord a => Set a -> Set a -> Set a
`Set.union` forall a. Relation a -> Set a
domain Relation a
y)
r :: Set (a, a)
r = forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions [ forall a. Ord a => a -> Relation a -> Set a
preSet a
v Relation a
x forall a b. Set a -> Set b -> Set (a, b)
`Set.cartesianProduct` forall a. Ord a => a -> Relation a -> Set a
postSet a
v Relation a
y | a
v <- [a]
vs ]
closure :: Ord a => Relation a -> Relation a
closure :: forall a. Ord a => Relation a -> Relation a
closure = forall a. Ord a => Relation a -> Relation a
reflexiveClosure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => Relation a -> Relation a
transitiveClosure
reflexiveClosure :: Ord a => Relation a -> Relation a
reflexiveClosure :: forall a. Ord a => Relation a -> Relation a
reflexiveClosure (Relation Set a
d Set (a, a)
r) =
forall a. Set a -> Set (a, a) -> Relation a
Relation Set a
d forall a b. (a -> b) -> a -> b
$ Set (a, a)
r forall a. Ord a => Set a -> Set a -> Set a
`Set.union` forall a. [a] -> Set a
Set.fromDistinctAscList [ (a
a, a
a) | a
a <- forall a. Set a -> [a]
Set.toAscList Set a
d ]
symmetricClosure :: Ord a => Relation a -> Relation a
symmetricClosure :: forall a. Ord a => Relation a -> Relation a
symmetricClosure (Relation Set a
d Set (a, a)
r) = forall a. Set a -> Set (a, a) -> Relation a
Relation Set a
d forall a b. (a -> b) -> a -> b
$ Set (a, a)
r forall a. Ord a => Set a -> Set a -> Set a
`Set.union` forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map forall a b. (a, b) -> (b, a)
swap Set (a, a)
r
transitiveClosure :: Ord a => Relation a -> Relation a
transitiveClosure :: forall a. Ord a => Relation a -> Relation a
transitiveClosure Relation a
old
| Relation a
old forall a. Eq a => a -> a -> Bool
== Relation a
new = Relation a
old
| Bool
otherwise = forall a. Ord a => Relation a -> Relation a
transitiveClosure Relation a
new
where
new :: Relation a
new = forall a. Ord a => Relation a -> Relation a -> Relation a
overlay Relation a
old (Relation a
old forall a. Ord a => Relation a -> Relation a -> Relation a
`compose` Relation a
old)
consistent :: Ord a => Relation a -> Bool
consistent :: forall a. Ord a => Relation a -> Bool
consistent (Relation Set a
d Set (a, a)
r) = forall a. Ord a => Set (a, a) -> Set a
referredToVertexSet Set (a, a)
r forall a. Ord a => Set a -> Set a -> Bool
`Set.isSubsetOf` Set a
d
referredToVertexSet :: Ord a => Set (a, a) -> Set a
referredToVertexSet :: forall a. Ord a => Set (a, a) -> Set a
referredToVertexSet = forall a. Ord a => [a] -> Set a
Set.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. [a] -> [a] -> [a]
(++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. [(a, b)] -> ([a], [b])
unzip forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Set a -> [a]
Set.toAscList