Skip to content

EP-0200: Categorical Abstractions & Enriched Matrix Mechanics

Field Value
EP 0200
Title Categorical Abstractions & Enriched Matrix Mechanics
Author Eran Rivlis eran@rivlis.info, Antigravity
Sponsor Core Maintainers
Delegate 🧩 Russell (Taxonomy & Consistency) & ⚡ Shannon (Zero Bloat & Efficiency)
Status Final
Type Standards Track
Created 2026-10-27
Updated 2026-11-11
Replaces —
Superseded-By —

Abstract

This proposal establishes a rigorous, Lean 4 Mathlib-aligned category-theoretic substrate within algebrax.category, upgrading the module from a pair of thin helper functions into a first-class mathematical suite. EP-0200 introduces: (1) core protocols for Category[Obj, Mor], Functor[Obj, Mor, Obj_co, Mor_co], and OppositeCategory; (2) the Matrix Category \(\mathbf{Mat}(S)\), where objects are finite index sets, morphisms are sparse dict-matrices, and composition is sparse matrix multiplication (dot), junto with TransposeFunctor and SemiringChangeOfBaseFunctor; (3) Lawvere Metric Spaces (LawvereMetricCategory), realizing metric spaces and graph shortest-path networks as enriched categories over TropicalSemiring \(\langle [0, \infty], \min, +, \infty, 0 \rangle\); and (4) automated property-based verification (verify_category_laws, verify_functor_laws) in algebrax.verification. All abstractions adhere strictly to AlgebraX's canonical carrier constraint: zero-overhead, unboxed sparse dictionary matrices.


Motivation

AlgebraX introduced algebrax.category in Phase 2 (EP-0113) to support Kleisli composition and Kan extensions. However, the implementation remained skeletal: - kleisli_compose(f, g, semiring) is a 1-line wrapper around dot(f, g, semiring). - kan_extension_left(p, f, semiring) is a 1-line wrapper around dot(f, p, semiring).

Neither function exposes or verifies actual categorical structures. There is no Category protocol, no Functor abstraction, and no way to inspect objects, domains, codomains, or identity morphisms.

Meanwhile, seminal computer science and mathematics literature demonstrates that Category Theory is the natural unifying language of linear algebra and graph algorithms: 1. Stephen Dolan (2013) demonstrated in Fun with Semirings that for any semiring \(S\), matrices form a category \(\mathbf{Mat}(S)\) whose composition is matrix multiplication and whose Kleene star \(\mathbf{A}^*\) unifies shortest paths, transitive closure, and regular languages. 2. F. William Lawvere (1973) demonstrated in Metric spaces, generalized logic, and closed categories that an extended metric space \((X, d)\) is strictly a category enriched over the tropical cost semiring \(\mathcal{V} = \langle [0, \infty], \ge, +, 0 \rangle\), where the triangle inequality \(d(x, z) \le d(x, y) + d(y, z)\) is nothing more than morphism composition \(g \circ f\). 3. Lean 4 Mathlib organizes modern computational algebra around CategoryTheory.Category and CategoryTheory.ConcreteCategory.

By elevating algebrax.category into a first-class protocol suite, AlgebraX bridges abstract category theory with high-performance sparse matrix computation without introducing heavy boxed wrappers.


Rationale & Council Alignment

  • 🧩 Russell (Taxonomy, Ontological Coherence & Deductive Order): Directly aligns the category hierarchy with Lean 4 Mathlib. Separates object classification from morphism composition, eliminating ad-hoc function signatures.
  • ⚡ Shannon (Zero Bloat & Unboxed Efficiency): Rejects heavy object-oriented wrapper hierarchies (e.g. class Morphism, class Object). Morphisms in \(\mathbf{Mat}(S)\) remain raw Python dictionaries dict[K, dict[V, S]]. MatrixCategory acts as a stateless, zero-cost operator.
  • 🔬 Popper (Falsifiability & Empirical Invariants): Specifies executable property-based testing in algebrax.verification: verify_category_laws (left/right identities, associativity) and verify_functor_laws (identity and composition preservation).
  • 💡 Feynman (Freshman Test & Intuitive Demystification): Demystifies category theory through concrete computational realities: showing that "a category is just a typed graph of compatible matrices where matrix multiplication is composition."
  • ⚖️ Noether (Structural Conservation): Guarantees that algebraic conservation laws (e.g. semiring distributivity and associativity) map directly to the functorial preservation of paths across matrix pipelines.

Specification

1. The Core Category Protocols (src/algebrax/category/core.py)

from typing import Generic, Protocol, TypeVar, runtime_checkable

Obj = TypeVar("Obj")
Mor = TypeVar("Mor")
Obj_co = TypeVar("Obj_co", covariant=True)
Mor_co = TypeVar("Mor_co", covariant=True)


@runtime_checkable
class Category(Protocol[Obj, Mor]):
    r"""A mathematical category $\mathcal{C} = \langle \mathrm{Obj}, \mathrm{Hom}, \mathrm{id}, \circ \rangle$."""

    def id(self, x: Obj) -> Mor:
        r"""Identity morphism $\mathrm{id}_x : x \to x$."""
        ...

    def compose(self, f: Mor, g: Mor) -> Mor:
        r"""Composition of morphisms: $g \circ f$ (f followed by g)."""
        ...

    def source(self, f: Mor) -> Obj:
        r"""Source object $\mathrm{dom}(f)$."""
        ...

    def target(self, f: Mor) -> Obj:
        r"""Target object $\mathrm{cod}(f)$."""
        ...


@runtime_checkable
class Functor(Protocol[Obj, Mor, Obj_co, Mor_co]):
    r"""A functor $F : \mathcal{C} \to \mathcal{D}$ between categories."""

    def map_obj(self, x: Obj) -> Obj_co:
        r"""Object mapping: $F(x)$."""
        ...

    def map_mor(self, f: Mor) -> Mor_co:
        r"""Morphism mapping: $F(f)$."""
        ...

2. The Matrix Category \(\mathbf{Mat}(S)\) (src/algebrax/category/matrix.py)

A concrete category whose morphisms are unboxed sparse dict-matrices over a semiring or ring \(S\), together with TransposeFunctor and SemiringChangeOfBaseFunctor:

from typing import Generic, TypeVar
from algebrax.algebra import Semiring
from algebrax.matrix import dot
from algebrax.typing import SparseMatrix

K = TypeVar("K")
V = TypeVar("V")


class MatrixCategory(Generic[K, V]):
    r"""The Category $\mathbf{Mat}(S)$ of sparse matrices over a Semiring $S$.

    - Objects: Frozensets of row/column index keys.
    - Morphisms: Sparse matrices $M \in S^{A \times B}$.
    - Composition: $g \circ f = \mathrm{dot}(f, g, \mathrm{semiring}=S)$.
    - Identity: $\mathrm{id}_A = \{k: \{k: 1_S\} \mid k \in A\}$.
    """

    def __init__(self, semiring: Semiring[V] | type[Semiring[V]] | str | None = None):
        self._semiring = Semiring.normalize(semiring)

    def id(self, x: frozenset[K]) -> SparseMatrix[K, V]:
        one = self._semiring.one
        return {k: {k: one} for k in x}

    def compose(self, f: SparseMatrix[K, V], g: SparseMatrix[K, V]) -> SparseMatrix[K, V]:
        return dot(f, g, semiring=self._semiring)

    def source(self, f: SparseMatrix[K, V]) -> frozenset[K]:
        return frozenset(r for r, row in f.items() if row)

    def target(self, f: SparseMatrix[K, V]) -> frozenset[K]:
        targets = set()
        for row in f.values():
            targets.update(row.keys())
        return frozenset(targets)

    def opposite(self) -> OppositeCategory[frozenset[K], SparseMatrix[K, V]]:
        return OppositeCategory(self)

3. Lawvere Enriched Metric Category (src/algebrax/category/enriched.py)

Realizes F. William Lawvere's theorem that generalized metric spaces are categories enriched over the tropical cost semiring (TropicalSemiring) \(\langle [0, \infty], \min, +, \infty, 0 \rangle\):

from algebrax.semiring.optimization import TropicalSemiring


class LawvereMetricCategory(MatrixCategory[K, float], Generic[K]):
    r"""A Lawvere metric space $(X, d)$ as a category enriched over `TropicalSemiring`.

    - Objects: Points $x \in X$.
    - Enriched Morphism: Distance $d(x, y) \in [0, \infty]$.
    - Enriched Composition: Triangle inequality $d(x, y) + d(y, z) \ge d(x, z)$.
    - Enriched Identity: $d(x, x) = 0$.
    """

    def __init__(self, distance_matrix: SparseMatrix[K, float] | None = None):
        super().__init__(TropicalSemiring())
        self._d = distance_matrix if distance_matrix is not None else {}

    def distance(self, x: K, y: K) -> float:
        ...

    def compose_paths(self, f: SparseMatrix[K, float], g: SparseMatrix[K, float]) -> SparseMatrix[K, float]:
        """Compute all-pairs shortest paths via min-plus matrix composition."""
        return dot(f, g, semiring=self._semiring)

    def metric_closure(self, max_steps: int | None = None) -> SparseMatrix[K, float]:
        """Compute Kleene star metric closure enforcing the triangle inequality."""
        ...

4. Falsifiable Category Axiom Verification (src/algebrax/verification.py)

def verify_category_laws(
    cat: Category[Obj, Mor],
    objects: Iterable[Obj],
    morphisms: Iterable[Mor],
    eq_fn: Callable[[Mor, Mor], bool] | None = None,
    tol: float = 1e-7,
) -> dict[str, bool]:
    r"""Test Category axioms:
    - Left Identity: compose(f, id(target(f))) == f
    - Right Identity: compose(id(source(f)), f) == f
    - Associativity: compose(compose(f, g), h) == compose(f, compose(g, h))
    """
    ...


def verify_functor_laws(
    functor: Functor[Obj, Mor, Obj2, Mor2],
    cat_src: Category[Obj, Mor],
    cat_dst: Category[Obj2, Mor2],
    test_objects: Iterable[Obj],
    test_morphisms: Iterable[Mor],
    eq_fn: Callable[[Mor2, Mor2], bool] | None = None,
    tol: float = 1e-7,
) -> dict[str, bool]:
    r"""Test Functor axioms:
    - Identity preservation: F(id_x) == id_{F(x)}
    - Composition preservation: F(compose(f, g)) == compose(F(f), F(g))
    """
    ...

Backwards Compatibility

  1. Existing Functions Unbroken: kleisli_compose and kan_extension_left in algebrax.category remain fully functional and backwards-compatible. They are re-exported from algebrax.category.
  2. Zero Overhead Carrier Invariant: Morphisms remain unboxed dictionaries dict[K, dict[V, S]]. No boxing into class Morphism is required.

How to Teach This / Documentation Plan

  • Updated docs/guide/discrete/category.md in Tier 6 of the User Guide, showcasing:
  • Composing matrices via MatrixCategory(StandardSemiring(Fraction)) and TransposeFunctor duality.
  • The Lawvere metric interpretation of shortest path networks and metric_closure().
  • Automated verification using verify_category_laws and verify_functor_laws.

Reference Implementation

Implemented across src/algebrax/category/ (core.py, matrix.py, enriched.py, __init__.py), src/algebrax/verification.py, and verified in tests/algebrax/test_category.py.


Rejected Ideas

  1. Boxing Matrices into Morphism Classes:
  2. Rejected: Violates Shannon efficiency and core carrier constraints. Morphisms must remain raw dictionaries.
  3. Infinite Category Formalization:
  4. Rejected: Out of scope for a computational sparse matrix library. Focus remains on finite, computable categories.

Open Questions

  • [x] Should MatrixCategory provide automatic opposite category duality (\(\mathcal{C}^{\mathrm{op}}\)) and transposition functor? (Resolved: Implemented OppositeCategory, cat.opposite(), and TransposeFunctor).

Change Log

  • 2026-10-27:
    • Initial Draft authored by Eran Rivlis & Antigravity derived from EXP-0005.
  • 2026-11-11:
    • Implemented Category, Functor, OppositeCategory, MatrixCategory, TransposeFunctor, SemiringChangeOfBaseFunctor, LawvereMetricCategory, verify_category_laws, and verify_functor_laws. Status updated to Final.