Skip to content

Semirings Overview (algebrax.algebra.Semiring)

1. The Practical Problem: One Algorithm, 33 Domains

Consider classic matrix multiplication \(C_{ij} = \sum_k A_{ik} \cdot B_{kj}\). In standard linear algebra, \(\sum\) is addition (\(+\)) and \(\cdot\) is multiplication (\(\times\)). What if you replace \((+, \times)\) with: - \((\min, +)\) \(\longrightarrow\) Shortest Paths (Bellman–Ford / Floyd–Warshall) - \((\max, \times)\) \(\longrightarrow\) Most Probable Viterbi Path (HMM decoding) - \((\vee, \wedge)\) \(\longrightarrow\) Transitive Reachability - \((\max, \min)\) \(\longrightarrow\) Maximum Capacity Bottleneck Flow

All of these share the exact same algebraic laws: an Additive Commutative Monoid \(\langle V, \mathbf{0}, + \rangle\) and a Multiplicative Monoid \(\langle V, \mathbf{1}, \cdot \rangle\) linked by two-sided distributivity and zero annihilation—without requiring subtraction or division. That structure is a Semiring.


2. The 30-Second Executable Snippet

import algebrax as ax

graph = {0: {1: 2.0, 2: 10.0}, 1: {2: 3.0}}

# 1. Standard linear path weight: (2.0 * 3.0) = 6.0
lin = ax.matrix.dot(graph, graph, semiring='Standard')
assert lin[0][2] == 6.0

# 2. Tropical shortest 2-hop path: min(inf, 2.0 + 3.0) = 5.0
short = ax.matrix.dot(graph, graph, semiring='Tropical')
assert short[0][2] == 5.0

3. Atomic Mixins & Protocol Contract

In algebrax.algebra, the semiring family is structured as a clear four-tiered hierarchy of flat, diamond-free compositions:

  1. Semiring[V] (Pure Rig): The foundational two-operation system \(\langle V, \mathbf{0}, +, \mathbf{1}, \cdot \rangle\) combining an additive commutative monoid and a multiplicative monoid linked by distributivity and zero annihilation.
  2. StarSemiring[V] (Closed / Conway Semiring): Equips a Semiring with unary Kleene star closure \(a^* = \mathbf{1} \oplus a \cdot a^*\) (HasStar[V]) and strict positive iteration \(a^+ = a \cdot a^*\) (HasPlus[V]). Synonyms: ClosedSemiring, ConwaySemiring.
  3. KleeneAlgebra[V]: An idempotent star semiring (\(a \oplus a = a\)) witnessing the LawIdempotent marker.
  4. ActionAlgebra[V]: Augments a KleeneAlgebra with left and right residuals (\(a \backslash b\) and \(b / a\), HasResiduals[V]), providing purely equational axiomatization without Horn clauses.
from typing import Protocol, TypeVar
from algebrax.algebra import HasAdd, HasMul, HasOne, HasPlus, HasResiduals, HasStar, HasZero, LawIdempotent

V = TypeVar('V')


# 1. Pure Rig: (zero, one, add, mul)
class Semiring(HasZero[V], HasAdd[V], HasOne[V], HasMul[V], Protocol):
    ...


# 2. Closed / Conway Semiring: adds Kleene star and strict plus
class StarSemiring(HasZero[V], HasAdd[V], HasOne[V], HasMul[V], HasStar[V], HasPlus[V], Protocol):
    def plus(self, a: V) -> V:
        return self.mul(a, self.star(a))


# 3. Kleene Algebra: idempotent star semiring
class KleeneAlgebra(
    HasZero[V], HasAdd[V], HasOne[V], HasMul[V], HasStar[V], HasPlus[V], LawIdempotent, Protocol
):
    ...


# 4. Action Algebra: Kleene algebra with left and right residuals
class ActionAlgebra(
    HasZero[V],
    HasAdd[V],
    HasOne[V],
    HasMul[V],
    HasStar[V],
    HasPlus[V],
    HasResiduals[V],
    LawIdempotent,
    Protocol,
):
    ...
Protocol Mixin Composition Formal Signature Role & Capabilities
Semiring[V] (HasZero, HasAdd, HasOne, HasMul) \(\langle V, \mathbf{0}, +, \mathbf{1}, \cdot \rangle\) Standard linear algebra, polynomial algebras, and generic rigs.
StarSemiring[V] (..., HasStar, HasPlus) \(\langle V, \mathbf{0}, +, \mathbf{1}, \cdot, ^*, ^+ \rangle\) Graph reachability, all-pairs paths, and Taylor series closures (DualNumberAlgebra).
KleeneAlgebra[V] (..., HasStar, HasPlus, LawIdempotent) \(\langle V, \mathbf{0}, +, \mathbf{1}, \cdot, ^*, ^+ \rangle\) with \(a \oplus a = a\) Regular languages, finite automata, and extremal routing (TropicalSemiring, BooleanSemiring).
ActionAlgebra[V] (..., HasResiduals) \(\langle V, \mathbf{0}, +, \mathbf{1}, \cdot, ^*, ^+, \backslash, / \rangle\) Equational Kleene logic, Dijkstra weakest preconditions, and Brzozowski language quotients (RelationActionAlgebra).

[!NOTE] Subtyping Harmony: Every Ring is a Semiring Because Semiring requires only zero, one, add, mul, the classical mathematical inclusion \(\text{Semiring} \subset \text{Ring} \subset \text{CommRing} \subset \text{Field}\) holds strictly under structural subtyping:

from algebrax.algebra import ModularField, PolynomialAlgebra, Ring, Semiring, StandardField
from algebrax.semiring import StandardSemiring

assert isinstance(PolynomialAlgebra(StandardSemiring(int)), Ring)
assert isinstance(PolynomialAlgebra(StandardSemiring(int)), Semiring)  # Rings are Semirings!
assert isinstance(StandardField(float), Semiring)  # Fields are Semirings!
assert isinstance(ModularField(7), Semiring)  # Finite fields are Semirings!

Because Semiring includes HasZero, HasAdd, HasOne, and HasMul, every Semiring instance automatically satisfies AddCommMonoid[V], AddMonoid[V], Monoid[V], and Semigroup[V] via structural subtyping (isinstance).


4. Falsifiability: Law Verification & Counterexamples

Every semiring must satisfy 9 axioms (additive associativity/commutativity/identity, multiplicative associativity/identity, left/right distributivity, and left/right zero annihilation). Structures equipped with Kleene star (StarSemiring and KleeneAlgebra) satisfy additional closure laws:

from algebrax.semiring import TropicalSemiring
from algebrax.verification import (
    verify_kleene_algebra_laws,
    verify_semiring_laws,
    verify_star_semiring_laws,
)

samples = [float('inf'), 0.0, 1.5, 4.0]
trop = TropicalSemiring()

# 1. Standard 9 Semiring laws
res_rig = verify_semiring_laws(trop, samples)
assert all(res_rig.values())

# 2. Star Semiring fixed-point closure laws: a* = 1 + a · a* = 1 + a* · a
res_star = verify_star_semiring_laws(trop, samples)
assert all(res_star.values())

# 3. Kleene Algebra laws: Star closure + Additive Idempotence (a + a = a)
res_kleene = verify_kleene_algebra_laws(trop, samples)
assert all(res_kleene.values())

# Counterexample: Idempotent Semirings cannot possess additive inverses (no subtraction!)
# In TropicalSemiring, min(a, a) = a. If an additive inverse (-a) existed,
# adding it to both sides would force a = 0 (tropical infinity), collapsing the carrier.
assert trop.add(3.0, 3.0) == 3.0  # min(3.0, 3.0) == 3.0

5. Generalized Closures: Kleene Plus, Conway Matrix Star & Action Algebras

AlgebraX provides comprehensive algebraic closure primitives across both scalar carriers and sparse matrices:

Reflexive vs. Strict Closures (\(A^*\) vs. \(A^+\))

  • Kleene Star (\(A^*\)): Represents paths of length \(\ge 0\). Every node reaches itself reflexively via identity (\(d(u, u) = \mathbf{1}\)).
  • Kleene Plus (\(A^+\)): Represents paths of length \(\ge 1\) (\(A^+ = A \cdot A^*\)). On Directed Acyclic Graphs (DAGs), \(A^+\) avoids spurious self-loops, making it ideal for cycle detection and reachability analysis.
import algebrax as ax

# DAG: 0 -> 1 -> 2
dag = {0: {1: True}, 1: {2: True}}
b = ax.semiring.BooleanSemiring()

# 1. Reflexive-transitive star closure includes self-loops
star_m = ax.matrix.star(dag, b)
assert star_m[0][0] is True  # 0 reaches 0 in 0 steps

# 2. Strict transitive plus closure excludes self-loops on DAGs
plus_m = ax.matrix.plus(dag, b)
assert 0 not in plus_m.get(0, {})  # No cycle, no self-loop!
assert plus_m[0][2] is True  # 0 reaches 2 in 2 steps

Universal Conway Block Matrix Closure

Lifting scalar closure to matrices is achieved via Conway's recursive \(2 \times 2\) block decomposition over any StarSemiring:

# All-Pairs Shortest Paths via Tropical Matrix Star
graph = {
    0: {1: 2.0, 2: 10.0},
    1: {2: 3.0},
}
trop = ax.semiring.TropicalSemiring()
apsp = ax.matrix.star(graph, trop)
assert apsp[0][2] == 5.0  # min(10.0, 2.0 + 3.0) = 5.0

[!TIP] Explore the complete Semiring Taxonomy & Interactive Catalog to browse all 33 built-in semirings across Arithmetic, Optimization, Statistical Moments, Logic, and Formal Structures.