algebraic-graphs-0.6.1: A library for algebraic graph construction and transformation
Copyright (c) Andrey Mokhov 2016-2022
License MIT (see the file LICENSE)
Maintainer andrey.mokhov@gmail.com
Stability experimental
Safe Haskell None
Language Haskell2010

Algebra.Graph.Label

Description

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 .

Synopsis

Semirings and dioids

class ( Monoid a, Semigroup a) => Semiring a where Source #

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 Source #

(<.>) :: a -> a -> a infixr 7 Source #

(<+>) :: Semigroup a => a -> a -> a infixr 6 Source #

An alias for <> .

class Semiring a => StarSemiring a where Source #

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

star :: a -> a Source #

class Semiring a => Dioid a Source #

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

x <+> x == x

Instances

Instances details
Dioid Any Source #
Instance details

Defined in Algebra.Graph.Label

( Monoid a, Ord a) => Dioid ( PowerSet a) Source #
Instance details

Defined in Algebra.Graph.Label

( Monoid a, Ord a) => Dioid ( Minimum a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Dioid ( Distance a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Dioid ( Capacity a) Source #
Instance details

Defined in Algebra.Graph.Label

( Eq o, Dioid a, Dioid o) => Dioid ( Optimum o a) Source #
Instance details

Defined in Algebra.Graph.Label

Data types for edge labels

data NonNegative a Source #

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.

Instances

Instances details
Monad NonNegative Source #
Instance details

Defined in Algebra.Graph.Label

Functor NonNegative Source #
Instance details

Defined in Algebra.Graph.Label

Applicative NonNegative Source #
Instance details

Defined in Algebra.Graph.Label

Num a => Bounded ( NonNegative a) Source #
Instance details

Defined in Algebra.Graph.Label

Eq a => Eq ( NonNegative a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Num ( NonNegative a) Source #
Instance details

Defined in Algebra.Graph.Label

Ord a => Ord ( NonNegative a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Show a) => Show ( NonNegative a) Source #
Instance details

Defined in Algebra.Graph.Label

finite :: ( Num a, Ord a) => a -> Maybe ( NonNegative a) Source #

A finite non-negative value or Nothing if the argument is negative.

unsafeFinite :: a -> NonNegative a Source #

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

infinite :: NonNegative a Source #

The (non-negative) infinite value.

getFinite :: NonNegative a -> Maybe a Source #

Get a finite value or Nothing if the value is infinite.

data Distance a Source #

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
(<.>) = (+)

Instances

Instances details
Num a => Bounded ( Distance a) Source #
Instance details

Defined in Algebra.Graph.Label

Eq a => Eq ( Distance a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Num ( Distance a) Source #
Instance details

Defined in Algebra.Graph.Label

Ord a => Ord ( Distance a) Source #
Instance details

Defined in Algebra.Graph.Label

Show a => Show ( Distance a) Source #
Instance details

Defined in Algebra.Graph.Label

Ord a => Semigroup ( Distance a) Source #
Instance details

Defined in Algebra.Graph.Label

( Ord a, Num a) => Monoid ( Distance a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Dioid ( Distance a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => StarSemiring ( Distance a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Semiring ( Distance a) Source #
Instance details

Defined in Algebra.Graph.Label

distance :: NonNegative a -> Distance a Source #

A non-negative distance.

getDistance :: Distance a -> NonNegative a Source #

Get the value of a distance.

data Capacity a Source #

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

Instances

Instances details
Num a => Bounded ( Capacity a) Source #
Instance details

Defined in Algebra.Graph.Label

Eq a => Eq ( Capacity a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Num ( Capacity a) Source #
Instance details

Defined in Algebra.Graph.Label

Ord a => Ord ( Capacity a) Source #
Instance details

Defined in Algebra.Graph.Label

Show a => Show ( Capacity a) Source #
Instance details

Defined in Algebra.Graph.Label

Ord a => Semigroup ( Capacity a) Source #
Instance details

Defined in Algebra.Graph.Label

( Ord a, Num a) => Monoid ( Capacity a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Dioid ( Capacity a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => StarSemiring ( Capacity a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Semiring ( Capacity a) Source #
Instance details

Defined in Algebra.Graph.Label

capacity :: NonNegative a -> Capacity a Source #

A non-negative capacity.

getCapacity :: Capacity a -> NonNegative a Source #

Get the value of a capacity.

data Count a Source #

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

zero  = 0
one   = 1
(<+>) = (+)
(<.>) = (*)

Instances

Instances details
Num a => Bounded ( Count a) Source #
Instance details

Defined in Algebra.Graph.Label

Eq a => Eq ( Count a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Num ( Count a) Source #
Instance details

Defined in Algebra.Graph.Label

Ord a => Ord ( Count a) Source #
Instance details

Defined in Algebra.Graph.Label

Show a => Show ( Count a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Semigroup ( Count a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Monoid ( Count a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => StarSemiring ( Count a) Source #
Instance details

Defined in Algebra.Graph.Label

( Num a, Ord a) => Semiring ( Count a) Source #
Instance details

Defined in Algebra.Graph.Label

count :: NonNegative a -> Count a Source #

A non-negative count.

getCount :: Count a -> NonNegative a Source #

Get the value of a count.

newtype PowerSet a Source #

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

PowerSet

Instances

Instances details
Eq a => Eq ( PowerSet a) Source #
Instance details

Defined in Algebra.Graph.Label

Ord a => Ord ( PowerSet a) Source #
Instance details

Defined in Algebra.Graph.Label

Show a => Show ( PowerSet a) Source #
Instance details

Defined in Algebra.Graph.Label

Ord a => Semigroup ( PowerSet a) Source #
Instance details

Defined in Algebra.Graph.Label

Ord a => Monoid ( PowerSet a) Source #
Instance details

Defined in Algebra.Graph.Label

( Monoid a, Ord a) => Dioid ( PowerSet a) Source #
Instance details

Defined in Algebra.Graph.Label

( Monoid a, Ord a) => Semiring ( PowerSet a) Source #
Instance details

Defined in Algebra.Graph.Label

data Minimum a Source #

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!"

Instances

Instances details
Monad Minimum Source #
Instance details

Defined in Algebra.Graph.Label

Functor Minimum Source #
Instance details

Defined in Algebra.Graph.Label

Applicative Minimum Source #
Instance details

Defined in Algebra.Graph.Label

IsList a => IsList ( Minimum a) Source #
Instance details

Defined in Algebra.Graph.Label

Associated Types

type Item ( Minimum a) Source #

Eq a => Eq ( Minimum a) Source #
Instance details

Defined in Algebra.Graph.Label

Ord a => Ord ( Minimum a) Source #
Instance details

Defined in Algebra.Graph.Label

Show a => Show ( Minimum a) Source #
Instance details

Defined in Algebra.Graph.Label

Ord a => Semigroup ( Minimum a) Source #
Instance details

Defined in Algebra.Graph.Label

( Monoid a, Ord a) => Monoid ( Minimum a) Source #
Instance details

Defined in Algebra.Graph.Label

( Monoid a, Ord a) => Dioid ( Minimum a) Source #
Instance details

Defined in Algebra.Graph.Label

( Monoid a, Ord a) => Semiring ( Minimum a) Source #
Instance details

Defined in Algebra.Graph.Label

type Item ( Minimum a) Source #
Instance details

Defined in Algebra.Graph.Label

getMinimum :: Minimum a -> Maybe a Source #

Extract the minimum or Nothing if it does not exist.

noMinimum :: Minimum a Source #

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

type Path a = [(a, a)] Source #

A path is a list of edges.

data Label a Source #

The type of free labels over the underlying set of symbols a . This data type is an instance of classes StarSemiring and Dioid .

Instances

Instances details
Functor Label Source #
Instance details

Defined in Algebra.Graph.Label

IsList ( Label a) Source #
Instance details

Defined in Algebra.Graph.Label

Associated Types

type Item ( Label a) Source #

Show a => Show ( Label a) Source #
Instance details

Defined in Algebra.Graph.Label

Semigroup ( Label a) Source #
Instance details

Defined in Algebra.Graph.Label

Monoid ( Label a) Source #
Instance details

Defined in Algebra.Graph.Label

StarSemiring ( Label a) Source #
Instance details

Defined in Algebra.Graph.Label

Semiring ( Label a) Source #
Instance details

Defined in Algebra.Graph.Label

type Item ( Label a) Source #
Instance details

Defined in Algebra.Graph.Label

type Item ( Label a) = a

type RegularExpression a = Label a Source #

A type synonym for regular expressions , built on top of free labels .

Combining edge labels

data Optimum o a Source #

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

Optimum

Instances

Instances details
( Eq o, Eq a) => Eq ( Optimum o a) Source #
Instance details

Defined in Algebra.Graph.Label

( Ord o, Ord a) => Ord ( Optimum o a) Source #
Instance details

Defined in Algebra.Graph.Label

( Show o, Show a) => Show ( Optimum o a) Source #
Instance details

Defined in Algebra.Graph.Label

( Eq o, Monoid a, Monoid o) => Semigroup ( Optimum o a) Source #
Instance details

Defined in Algebra.Graph.Label

( Eq o, Monoid a, Monoid o) => Monoid ( Optimum o a) Source #
Instance details

Defined in Algebra.Graph.Label

( Eq o, Dioid a, Dioid o) => Dioid ( Optimum o a) Source #
Instance details

Defined in Algebra.Graph.Label

( Eq o, StarSemiring a, StarSemiring o) => StarSemiring ( Optimum o a) Source #
Instance details

Defined in Algebra.Graph.Label

( Eq o, Semiring a, Semiring o) => Semiring ( Optimum o a) Source #
Instance details

Defined in Algebra.Graph.Label

type ShortestPath e a = Optimum ( Distance e) ( Minimum ( Path a)) Source #

The Optimum semiring specialised to finding the lexicographically smallest shortest path .

type AllShortestPaths e a = Optimum ( Distance e) ( PowerSet ( Path a)) Source #

The Optimum semiring specialised to finding all shortest paths .

type CountShortestPaths e = Optimum ( Distance e) ( Count Integer ) Source #

The Optimum semiring specialised to counting all shortest paths .

type WidestPath e a = Optimum ( Capacity e) ( Minimum ( Path a)) Source #

The Optimum semiring specialised to finding the lexicographically smallest widest path .