HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Modulealgebraic-graphs-0.8Haskell2010

Algebra.Graph.Label

Alga is a library for algebraic construction and manipulation of graphs in Haskell. See this paper for the motivation behind the library, the underlying theory, and implementation details.

This module provides basic data types and type classes for representing edge labels in edge-labelled graphs, e.g. see Algebra.Graph.Labelled.

  • 14 types
  • 3 classes
  • 18 values

Semirings and dioids

5 declarations
classclass Monoid a => Semiring a where
#

A semiring extends a commutative Monoid with operation <.> that acts similarly to multiplication over the underlying (additive) monoid and has one as the identity. This module also provides two convenient aliases: zero for mempty, and <+> for <>, which makes the interface more uniform.

Instances of this type class must satisfy the following semiring laws:

  • Associativity of <+> and <.>:

    x <+> (y <+> z) == (x <+> y) <+> z
    x <.> (y <.> z) == (x <.> y) <.> z
  • Identities of <+> and <.>:

    zero <+> x == x == x <+> zero
     one <.> x == x == x <.> one
  • Commutativity of <+>:

    x <+> y == y <+> x
  • Annihilating zero:

    x <.> zero == zero
    zero <.> x == zero
  • Distributivity:

    x <.> (y <+> z) == x <.> y <+> x <.> z
    (x <+> y) <.> z == x <.> z <+> y <.> z

Methods

  • one :: a
  • (<.>) :: a -> a -> ainfixr 7
Instances8Semiring, …
classclass Semiring a => StarSemiring a where
#

A star semiring is a Semiring with an additional unary operator star satisfying the following two laws:

star a = one <+> a <.> star a
star a = one <+> star a <.> a

Methods

Instances6StarSemiring
classclass Semiring a => Dioid a
#

A dioid is an idempotent semiring, i.e. it satisfies the following idempotence law in addition to the Semiring laws:

x <+> x == x
Instances6Dioid

Data types for edge labels

25 declarations
newtypenewtype NonNegative a
#

A non-negative value that can be finite or infinite. Note: the current implementation of the Num instance raises an error on negative literals and on the negate method.

Instances8Monad, Functor, Applicative, Bounded, Eq, Num, …
valueunsafeFinite :: a -> NonNegative a
#

A non-negative finite value, created unsafely: the argument is not checked for being non-negative, so unsafeFinite (-1) compiles just fine.

newtypenewtype Distance a
#

A distance is a non-negative value that can be finite or infinite. Distances form a Dioid as follows:

zero  = distance infinite
one   = 0
(<+>) = min
(<.>) = (+)
Instances10Bounded, Eq, Num, Ord, Show, Semigroup, …
newtypenewtype Capacity a
#

A capacity is a non-negative value that can be finite or infinite. Capacities form a Dioid as follows:

zero  = 0
one   = capacity infinite
(<+>) = max
(<.>) = min
Instances10Bounded, Eq, Num, Ord, Show, Semigroup, …
newtypenewtype Count a
#

A count is a non-negative value that can be finite or infinite. Counts form a Semiring as follows:

zero  = 0
one   = 1
(<+>) = (+)
(<.>) = (*)
Instances9Bounded, Eq, Num, Ord, Show, Semigroup, …
  • Num a => Bounded (Count a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • Eq a => Eq (Count a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • (Num a, Ord a) => Num (Count a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • Ord a => Ord (Count a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • Show a => Show (Count a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • (Num a, Ord a) => Semigroup (Count a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • (Num a, Ord a) => Monoid (Count a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • (Num a, Ord a) => Semiring (Count a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • (Num a, Ord a) => StarSemiring (Count a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
newtypenewtype PowerSet a
#

The power set over the underlying set of elements a. If a is a monoid, then the power set forms a Dioid as follows:

zero    = PowerSet Set.empty
one     = PowerSet $ Set.singleton mempty
x <+> y = PowerSet $ Set.union (getPowerSet x) (getPowerSet y)
x <.> y = PowerSet $ cartesianProductWith mappend (getPowerSet x) (getPowerSet y)

Constructors

Instances7Eq, Ord, Show, Semigroup, Monoid, Dioid, …
newtypenewtype Minimum a
#

If a is a monoid, Minimum a forms the following Dioid:

zero  = noMinimum
one   = pure mempty
(<+>) = liftA2 min
(<.>) = liftA2 mappend

To create a singleton value of type Minimum a use the pure function. For example:

getMinimum (pure "Hello, " <+> pure "World!") == Just "Hello, "
getMinimum (pure "Hello, " <.> pure "World!") == Just "Hello, World!"
Instances12Monad, Functor, Applicative, IsList, Eq, Ord, …
valuenoMinimum :: Minimum a
#

The value corresponding to the lack of minimum, e.g. the minimum of the empty set.

typetype Path a = [(a, a)]
#

A path is a list of edges.

datadata Label a
#

The type of free labels over the underlying set of symbols a. Label values can be manipulated via its Semigroup, Monoid and StarSemiring class instances.

Instances8Functor, IsList, Show, Semigroup, Monoid, Semiring, …
  • Functor LabelDefined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • IsList (Label a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • Show a => Show (Label a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • Semigroup (Label a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • Monoid (Label a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • Semiring (Label a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • StarSemiring (Label a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Label
  • type Item (Label a) = aDefined in algebraic-graphs-0.8 · Algebra.Graph.Label
valuesymbol :: a -> Label a
#

Wrap a value into a Symbol constructor

valuesymbols :: Foldable t => t a -> Label a
#

Wrap a list of values into Symbol constructors terminated by Zero

Combining edge labels

5 declarations
datadata Optimum o a
#

An optimum semiring obtained by combining a semiring o that defines an optimisation criterion, and a semiring a that describes the arguments of an optimisation problem. For example, by choosing o = Distance Int and and a = Minimum (Path String), we obtain the shortest path semiring for computing the shortest path in an Int-labelled graph with String vertices.

We assume that the semiring o is selective i.e. for all x and y:

x <+> y == x || x <+> y == y

In words, the operation <+> always simply selects one of its arguments. For example, the Capacity and Distance semirings are selective, whereas the the Count semiring is not.

Constructors

Instances8Eq, Ord, Show, Semigroup, Monoid, Dioid, …