Skip to content

Commit

Permalink
Add Monoid and Semigroup instances (#276)
Browse files Browse the repository at this point in the history
* Add `Monoid` and `Semigroup` instances

Fixes #269.

* Add documentation

* Update src/Algebra/Graph/Labelled.hs

Co-authored-by: Andrey Mokhov <[email protected]>
  • Loading branch information
ocharles and snowleopard authored Sep 20, 2021
1 parent 8e1799d commit 778a4cc
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Algebra/Graph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,14 @@ instance MonadPlus Graph where
mzero = Empty
mplus = Overlay

-- | Defined via 'overlay'.
instance Semigroup (Graph a) where
(<>) = overlay

-- | Defined via 'overlay' and 'empty'.
instance Monoid (Graph a) where
mempty = empty

-- | Construct the /empty graph/. An alias for the constructor 'Empty'.
--
-- @
Expand Down
8 changes: 8 additions & 0 deletions src/Algebra/Graph/AdjacencyIntMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ instance Num AdjacencyIntMap where
instance NFData AdjacencyIntMap where
rnf (AM a) = rnf a

-- | Defined via 'overlay'.
instance Semigroup AdjacencyIntMap where
(<>) = overlay

-- | Defined via 'overlay' and 'empty'.
instance Monoid AdjacencyIntMap where
mempty = empty

-- | Construct an 'AdjacencyIntMap' from an 'AM.AdjacencyMap' with vertices of
-- type 'Int'.
-- Complexity: /O(n + m)/ time and memory.
Expand Down
8 changes: 8 additions & 0 deletions src/Algebra/Graph/AdjacencyMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ instance IsString a => IsString (AdjacencyMap a) where
instance NFData a => NFData (AdjacencyMap a) where
rnf (AM a) = rnf a

-- | Defined via 'overlay'.
instance Ord a => Semigroup (AdjacencyMap a) where
(<>) = overlay

-- | Defined via 'overlay' and 'empty'.
instance Ord a => Monoid (AdjacencyMap a) where
mempty = empty

-- | Construct the /empty graph/.
--
-- @
Expand Down
8 changes: 8 additions & 0 deletions src/Algebra/Graph/Bipartite/AdjacencyMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ instance (Ord a, Ord b, Show a, Show b) => Show (AdjacencyMap a b) where
eShow es = showString "edges " . showsPrec 11 es
veShow xs = vShow (lefts xs) (rights xs)

-- | Defined via 'overlay'.
instance (Ord a, Ord b) => Semigroup (AdjacencyMap a b) where
(<>) = overlay

-- | Defined via 'overlay' and 'empty'.
instance (Ord a, Ord b) => Monoid (AdjacencyMap a b) where
mempty = empty

-- | Construct the /empty graph/.
--
-- @
Expand Down
8 changes: 8 additions & 0 deletions src/Algebra/Graph/Labelled.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ instance (NFData e, NFData a) => NFData (Graph e a) where
rnf (Vertex x ) = rnf x
rnf (Connect e x y) = e `seq` rnf x `seq` rnf y

-- | Defined via 'overlay'.
instance Monoid e => Semigroup (Graph e a) where
(<>) = overlay

-- | Defined via 'overlay' and 'empty'.
instance Monoid e => Monoid (Graph e a) where
mempty = empty

-- TODO: This is a very inefficient implementation. Find a way to construct an
-- adjacency map directly, without building intermediate representations for all
-- subgraphs.
Expand Down
8 changes: 8 additions & 0 deletions src/Algebra/Graph/Labelled/AdjacencyMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ instance (Eq e, Dioid e, Num a, Ord a) => Num (AdjacencyMap e a) where
instance IsString a => IsString (AdjacencyMap e a) where
fromString = vertex . fromString

-- | Defined via 'overlay'.
instance (Ord a, Eq e, Monoid e) => Semigroup (AdjacencyMap e a) where
(<>) = overlay

-- | Defined via 'overlay' and 'empty'.
instance (Ord a, Eq e, Monoid e) => Monoid (AdjacencyMap e a) where
mempty = empty

-- | Construct the /empty graph/.
--
-- @
Expand Down
4 changes: 4 additions & 0 deletions src/Algebra/Graph/NonEmpty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ instance Ord a => Eq (Graph a) where
instance Ord a => Ord (Graph a) where
compare = ord

-- | Defined via 'overlay'.
instance Semigroup (Graph a) where
(<>) = overlay

-- TODO: Find a more efficient equality check.
-- | Check if two graphs are equal by converting them to their adjacency maps.
eq :: Ord a => Graph a -> Graph a -> Bool
Expand Down
4 changes: 4 additions & 0 deletions src/Algebra/Graph/NonEmpty/AdjacencyMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ instance (Ord a, Show a) => Show (AdjacencyMap a) where
eshow xs = showString "edges1 " . showsPrec 11 xs
used = Set.toAscList $ Set.fromList $ uncurry (++) $ unzip es

-- | Defined via 'overlay'.
instance Ord a => Semigroup (AdjacencyMap a) where
(<>) = overlay

-- Unsafe creation of a NonEmpty list.
unsafeNonEmpty :: [a] -> NonEmpty a
unsafeNonEmpty = fromMaybe (error msg) . nonEmpty
Expand Down
8 changes: 8 additions & 0 deletions src/Algebra/Graph/Relation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ instance (Ord a, Num a) => Num (Relation a) where
instance IsString a => IsString (Relation a) where
fromString = vertex . fromString

-- | Defined via 'overlay'.
instance Ord a => Semigroup (Relation a) where
(<>) = overlay

-- | Defined via 'overlay' and 'empty'.
instance Ord a => Monoid (Relation a) where
mempty = empty

-- | Construct the /empty graph/.
--
-- @
Expand Down
8 changes: 8 additions & 0 deletions src/Algebra/Graph/Relation/Symmetric.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ instance (Ord a, Num a) => Num (Relation a) where
abs = id
negate = id

-- | Defined via 'overlay'.
instance Ord a => Semigroup (Relation a) where
(<>) = overlay

-- | Defined via 'overlay' and 'empty'.
instance Ord a => Monoid (Relation a) where
mempty = empty

-- | Construct a symmetric relation from a given "Algebra.Graph.Relation".
-- Complexity: /O(m*log(m))/ time.
--
Expand Down
8 changes: 8 additions & 0 deletions src/Algebra/Graph/Undirected.hs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ instance Ord a => Eq (Graph a) where
instance Ord a => Ord (Graph a) where
compare = ordR

-- | Defined via 'overlay'.
instance Semigroup (Graph a) where
(<>) = overlay

-- | Defined via 'overlay' and 'empty'.
instance Monoid (Graph a) where
mempty = empty

-- TODO: Find a more efficient equality check.
-- Check if two graphs are equal by converting them to symmetric relations.
eqR :: Ord a => Graph a -> Graph a -> Bool
Expand Down

0 comments on commit 778a4cc

Please sign in to comment.