Algebras¶
Ordered algebraic structures for morphism composition and enriched category theory.
algebras
¶
Algebras: enrichment algebras for V-enriched categories.
A commutative algebra Q = (L, ⊗, ⋁, ⋀, ¬, I, ⊥) provides the algebraic structure that parameterizes composition in a V-enriched category:
(g ∘ f)(a, c) = ⋁_b f(a, b) ⊗ g(b, c)
Different algebras yield different categories of relations:
- BooleanAlgebra: {0,1} with ∧, ∨ → Rel (crisp relations)
- ProductFuzzyAlgebra: [0,1] with ×, noisy-OR → FuzzyRel (product t-norm)
The enrichment determines composition, identity, marginalization, and quantification, all derived from the algebra's operations.
CompositionRule
¶
Bases: ABC
An associative-or-loose binary tensor contraction.
Defines the V-enriched composition kernel
(f >> g)[i, k] = ⋁_j f[i, j] ⊗ g[j, k] from the two
primitive operations tensor_op (binary ⊗) and join
(reduction ⋁). No identity element is required at this level.
The hierarchy below this class is:
CompositionRule— the bare composition surface.Semigroupoid— adds the assumption that ⊗ is associative, so composition forms a semigroupoid (a category without identities).Algebra— adds identity (unit/zero), a meet ⋀, a negation, and the full algebra-axiom package. This is the level at which compact-closed operations (identity,cup,cap,dagger,trace) become well-defined.
Operations that need identity check at runtime that the
composition rule is at least a Algebra; a clear
error is raised if a non-algebra rule is fed in.
tensor_op
abstractmethod
¶
join
abstractmethod
¶
compose
¶
V-enriched composition.
Computes: result[d..., c...] = ⋁_{s...} m[d..., s...] ⊗ n[s..., c...]
Source code in src/quivers/core/algebras.py
is_compatible
¶
is_compatible(other: CompositionRule) -> bool
Two composition rules compose if they're the same type or carry the same name (the latter catches custom-built instances of the same rule constructed independently).
Source code in src/quivers/core/algebras.py
Semigroupoid
¶
Bases: CompositionRule
A composition rule whose tensor_op is associative.
Semantically a CompositionRule with the marker
promise of associativity. No identity, no compact-closed
structure, no negation — those need Algebra.
Material implication composition (a ⊗ b = 1 - a + a*b,
⋁ = product) is the canonical example: associative under
its ⊗, but no tensor satisfies f >> id == f for all f.
BilinearForm
¶
Bases: CompositionRule
A composition rule with no associativity guarantee.
(f >> g) is still well-defined as a binary tensor
contraction, but (f >> g) >> h may differ from
f >> (g >> h). Callers fix an association order
explicitly — the type system records that the operation
isn't associative so downstream optimizations can't reorder
composition chains.
Examples include signed-dot-product compositions (sign flipping breaks associativity), top-k truncating compositions (early truncation isn't commutative with later contractions), and attention-style softmax-then-multiply rules.
Sibling of Semigroupoid under
CompositionRule: BilinearForm opts out of the
associativity promise that Semigroupoid carries; a rule
that's actually associative should be declared as
Semigroupoid instead.
Algebra
¶
Bases: Semigroupoid
Abstract commutative algebra for V-enriched categories.
Subclasses must implement the six primitive operations. Composition and identity are derived but overridable.
tensor_op
abstractmethod
¶
join
abstractmethod
¶
Join ⋁ — reduction for composition and existential (∃).
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Input tensor with values in L.
TYPE:
|
dim
|
Dimension(s) to reduce.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reduced tensor. |
Source code in src/quivers/core/algebras.py
meet
abstractmethod
¶
Meet ⋀ — reduction for universal quantification (∀).
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Input tensor with values in L.
TYPE:
|
dim
|
Dimension(s) to reduce.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reduced tensor. |
Source code in src/quivers/core/algebras.py
negate
abstractmethod
¶
compose
¶
V-enriched composition.
Computes: result[d..., c...] = ⋁_{s...} m[d..., s...] ⊗ n[s..., c...]
Override for numerical stability in specific algebras.
| PARAMETER | DESCRIPTION |
|---|---|
m
|
Left tensor of shape (domain, shared).
TYPE:
|
n
|
Right tensor of shape (shared, codomain).
TYPE:
|
n_contract
|
Number of shared dimensions to contract.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Composed tensor of shape (domain, codomain). |
Source code in src/quivers/core/algebras.py
identity_tensor
¶
identity_tensor(obj_shape: tuple[int, ...]) -> Tensor
Identity morphism tensor for an object with given shape.
Returns a tensor of shape (obj_shape, obj_shape) with the unit value on the diagonal and zero elsewhere.
| PARAMETER | DESCRIPTION |
|---|---|
obj_shape
|
Shape of the object (e.g., (n,) for FinSet(n)).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Identity tensor. |
Source code in src/quivers/core/algebras.py
is_compatible
¶
is_compatible(other: Algebra) -> bool
Check if two algebras are compatible for composition.
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The other algebra.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if morphisms from these algebras can compose. |
Source code in src/quivers/core/algebras.py
dual
¶
dual() -> Algebra
The dual algebra under de Morgan negation.
For a commutative algebra with involution N (negate),
the dual carries
tensor_op^op(a, b) = N(N(a) ⋁ N(b))
join^op(a_i) = N(⋀_i N(a_i)) = N(⊗_i N(a_i)) (finite I)
Unit and zero swap:
1^op = 0, 0^op = 1.
For ProductFuzzyAlgebra this yields the role-swapped pair
(⊗ = noisy-OR, ⋁ = product reduction), which is the
canonical Reichenbach-flavour probabilistic-implication
composition rule.
The default implementation requires negate to be a
true involution; subclasses with non-involutive lattices
should override.
Source code in src/quivers/core/algebras.py
DualAlgebra
¶
DualAlgebra(base: Algebra)
Bases: Algebra
The de-Morgan dual of an involutive commutative algebra.
For a t-norm / t-conorm pair (T, S) related by the strong
negation N (S(a, b) = N(T(N(a), N(b)))), the dual
algebra carries the role-swapped pair:
tensor_op^op = base.join (reducing as a binary op)
join^op = base.tensor_op (reducing as a fold)
meet^op = base.join (along whatever axis)
unit^op = base.zero
zero^op = base.unit
negate^op = base.negate (involution self-dualizes)
Concretely for the built-in pairs:
ProductFuzzyAlgebra.dual: ⊗ = noisy-OR (a + b - ab), ⋁ = product (∏ a_i).Lukasiewicz.dual: ⊗ = bounded sum (min(1, a + b)), ⋁ = bounded difference / repeated Łukasiewicz t-norm.Godel.dual: ⊗ = max, ⋁ = min.Boolean.dual: ⊗ = OR, ⋁ = AND.
Returned by Algebra.dual. Subclasses with
non-involutive negation (CountingAlgebra, …) should override
Algebra.dual to raise rather than allow dual construction
that breaks the de-Morgan equations.
Source code in src/quivers/core/algebras.py
CustomAlgebra
¶
CustomAlgebra(name: str, tensor_op: Callable[[Tensor, Tensor], Tensor], join: Callable[[Tensor, int | tuple[int, ...]], Tensor], unit: float, zero: float, negate: Callable[[Tensor], Tensor] | None = None, meet: Callable[[Tensor, int | tuple[int, ...]], Tensor] | None = None, verify: bool = True)
Bases: Algebra
User-defined algebra built from callable operations.
Construct a fresh algebra by supplying the primitive operations
as Python functions, rather than subclassing Algebra
for each variant.
The constructor only stores the operations; the user is
responsible for ensuring they satisfy the algebra axioms
(associativity, identity, distributivity of ⊗ over ⋁,
de-Morgan duality between ⊗ and ⋁ via negate for
involutive lattices). Basic structural axioms are
sanity-checked at construction time against a handful of
fixed sample inputs; serious deployments should write their
own targeted unit tests.
The DSL surface algebra name { tensor_op(a, b) = …;
join(t) = …; unit = …; zero = …; } compiles to this class
under the hood, with verify=False (user expressions are
arithmetic and routinely violate one of the canned samples
used by _sanity_check).
Source code in src/quivers/core/algebras.py
CustomSemigroupoid
¶
CustomSemigroupoid(name: str, tensor_op: Callable[[Tensor, Tensor], Tensor], join: Callable[[Tensor, int | tuple[int, ...]], Tensor], verify_associative: bool = True)
Bases: Semigroupoid
User-defined Semigroupoid built from callable
operations.
Use for composition rules that are associative under their
tensor_op but lack an identity element — Reichenbach-style
material implication composition, weighted shortest-path on
a non-pointed lattice, etc. Callers who want a full algebra
(with identity, dagger, compact-closed structure) should use
CustomAlgebra instead.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Human-readable name.
TYPE:
|
tensor_op
|
Binary monoidal product.
TYPE:
|
join
|
Reduction along an axis.
TYPE:
|
verify_associative
|
When
TYPE:
|
Source code in src/quivers/core/algebras.py
CustomBilinearForm
¶
CustomBilinearForm(name: str, tensor_op: Callable[[Tensor, Tensor], Tensor], join: Callable[[Tensor, int | tuple[int, ...]], Tensor])
Bases: BilinearForm
User-defined BilinearForm built from callable
operations.
Use for composition rules whose tensor_op is not
associative — for instance signed-dot-product or top-k
truncating rules. Callers must pin an association order
explicitly when chaining; the runtime doesn't promise that
(f >> g) >> h == f >> (g >> h).
No associativity smoke test runs because the operation may be non-associative.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Human-readable name.
TYPE:
|
tensor_op
|
Binary product (need not be associative).
TYPE:
|
join
|
Reduction along an axis.
TYPE:
|
Source code in src/quivers/core/algebras.py
ProductFuzzyAlgebra
¶
Bases: Algebra
[0,1] with product t-norm and probabilistic sum (noisy-OR).
This is the enrichment for the Kleisli category of the fuzzy powerset monad with the product t-norm:
⊗ = product: a ⊗ b = a * b
⋁ = noisy-OR: ⋁_i x_i = 1 - ∏_i (1 - x_i)
⋀ = product: ⋀_i x_i = ∏_i x_i
¬ = complement: ¬a = 1 - a
I = 1.0
⊥ = 0.0
Composition uses log-space for numerical stability.
join
¶
Noisy-OR in log-space: 1 - exp(∑ log(1 - t)).
Source code in src/quivers/core/algebras.py
meet
¶
Product (fuzzy AND): ∏_i t_i.
Source code in src/quivers/core/algebras.py
compose
¶
Override for log-space numerical stability.
Computes noisy-OR contraction matching the existing noisy_or_contract implementation exactly.
Source code in src/quivers/core/algebras.py
BooleanAlgebra
¶
Bases: Algebra
{0, 1} with logical AND and OR.
The enrichment for the category Rel of crisp binary relations:
⊗ = AND: a ⊗ b = a ∧ b
⋁ = OR: ⋁_i x_i = max_i x_i
⋀ = AND: ⋀_i x_i = min_i x_i
¬ = NOT: ¬a = 1 - a
I = 1.0
⊥ = 0.0
Works on float tensors with values in {0.0, 1.0}. Intermediate fuzzy values are rounded.
LukasiewiczAlgebra
¶
Bases: Algebra
[0,1] with Łukasiewicz t-norm and bounded sum.
The Łukasiewicz t-norm is the strongest continuous t-norm:
⊗ = Łukasiewicz: a ⊗ b = max(a + b - 1, 0)
⋁ = bounded sum: ⋁_i x_i = min(1, ∑_i x_i)
⋀ = min: ⋀_i x_i = min_i x_i
¬ = strong neg: ¬a = 1 - a
I = 1.0
⊥ = 0.0
GodelAlgebra
¶
Bases: Algebra
[0,1] with Gödel (min) t-norm.
⊗ = min, ⋁ = max, ⋀ = min, ¬ = Gödel neg (1 if a == 0 else 0), I = 1.0, ⊥ = 0.0.
TropicalAlgebra
¶
Bases: Algebra
[0, ∞] with (+, min) — Lawvere metric spaces.
⊗ = addition (distances compose),
⋁ = infimum (shortest path),
⋀ = supremum (longest path),
I = 0.0, ⊥ = ∞.
Composition is the tropical / (min, +) matrix product. Negation is undefined.
MaxPlusAlgebra
¶
Bases: Algebra
Max-plus (Viterbi) semiring on (-∞, ∞].
Distinct from TropicalAlgebra (which is min-plus,
suited to shortest-path aggregations): the join here is max
and the tensor is +. The canonical algebra for MAP decoding
in HMMs, CRFs, and weighted automata.
⊗ = +, ⋁ = max, ⋀ = min,
I = 0, ⊥ = -∞.
LogProbAlgebra
¶
Bases: Algebra
Log-space sum-product semiring on (-∞, 0].
Tensor is real addition (probability multiplication in log-
space) and join is torch.logsumexp (probability
summation in log-space). It supports float32 numerics for
hierarchical-Bayes log-likelihood pipelines.
⊗ = +, ⋁ = logsumexp, ⋀ = min,
I = 0 (log 1), ⊥ = -∞ (log 0).
RealAlgebra
¶
Bases: Algebra
Sum-product semiring on the real numbers (ℝ, +, ·).
The canonical numeric semiring: addition is the lattice join, multiplication the monoidal tensor. Use when entries are unbounded real weights with no probability interpretation.
ProbabilityAlgebra
¶
Bases: Algebra
Sum-product semiring on [0, 1] with explicit clamp.
Same operations as RealAlgebra but restricted to the
unit interval.
CountingAlgebra
¶
Bases: Algebra
Sum-product semiring on the non-negative integers (ℕ, +, ·).
Counting algebra: composition counts the number of distinct paths through a structure. The underlying tensor is float-typed (PyTorch's autograd requires it) but operations are integer-respecting.
MarkovAlgebra
¶
Bases: Algebra
Sum-product composition for stochastic matrices.
Implements the composition rule of FinStoch:
(g ∘ f)(a, c) = Σ_b f(a, b) · g(b, c)
standard matrix multiplication on row-stochastic matrices. Formally:
⊗ = product, ⋁ = sum, ⋀ = product,
¬ = complement (1 - p),
I = 1.0, ⊥ = 0.0.
Not a true algebra in the lattice-theoretic sense (Σ is not idempotent), but the composition formula matches the algebra interface and composition of row-stochastic matrices yields row-stochastic matrices.
semigroupoid
¶
semigroupoid(name: str, tensor_op: Callable[[Tensor, Tensor], Tensor], join: Callable[[Tensor, int | tuple[int, ...]], Tensor], *, verify_associative: bool = True) -> CustomSemigroupoid
Convenience constructor for CustomSemigroupoid.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Human-readable name.
TYPE:
|
tensor_op
|
Binary monoidal product.
TYPE:
|
join
|
Reduction along an axis.
TYPE:
|
verify_associative
|
When
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CustomSemigroupoid
|
The composition rule built from the callables. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/core/algebras.py
bilinear_form
¶
bilinear_form(name: str, tensor_op: Callable[[Tensor, Tensor], Tensor], join: Callable[[Tensor, int | tuple[int, ...]], Tensor]) -> CustomBilinearForm
Convenience constructor for CustomBilinearForm.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Human-readable name.
TYPE:
|
tensor_op
|
Binary product, which need not be associative.
TYPE:
|
join
|
Reduction along an axis.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CustomBilinearForm
|
The composition rule built from the callables. |
Source code in src/quivers/core/algebras.py
material_implication
¶
material_implication() -> CustomSemigroupoid
Reichenbach material implication composition as a Semigroupoid.
Tensor product is the probabilistic implication
a → b = 1 - a + a*b; join is the product reduction.
Associative but lacks an identity, so it's a semigroupoid,
not an algebra.
| RETURNS | DESCRIPTION |
|---|---|
CustomSemigroupoid
|
The material-implication composition rule. |
Source code in src/quivers/core/algebras.py
The algebra singletons, such as PRODUCT_FUZZY, are documented on the
core overview.