Skip to content

Architecture & Invariants

This page summarizes the living architectural laws that govern src/algebrax/. For historical design rationale and the multi-phase roadmap, see the Enhancement Proposals and EP-0099.


1. Core Design Laws

The Unboxed Sparse Carrier Invariant

AlgebraX treats native Python dict (and Mapping) as the canonical sparse carrier — never wrapping vectors, matrices, polynomials, or chains inside heavy boxed container classes (EP-0001).

  • Sparse Vectors & Matrices: dict[K, V] and dict[K, dict[K, V]].
  • Multivariate Polynomials: dict[tuple[int, ...], V] (SparseMPoly[V]) — the exact same carrier powers classical Gröbner ideals (MultivariatePolynomialAlgebra) and piecewise-linear tropical hypersurfaces (TropicalHypersurface).
  • Tensors, Chains & Cochains: dict[tuple[K, ...], V].
  • Sparsity Principle: Missing keys represent the active algebraic structure's additive identity (zero), never hardcoded 0.0 (e.g., +inf for TropicalSemiring, -inf for ArcticSemiring).

Zero Heavy Core Dependencies

The core runtime depends solely on Python >=3.11 and mappingtools — zero C/C++ compilation required. Optional bridges (numpy, scipy, PIL) live strictly behind lazy imports in algebrax.converters and differential verification tests.

The Law of Flat Protocol Composition

All algebraic, categorical, and geometric protocols in algebrax.algebra, algebrax.category, and algebrax.variety are @runtime_checkable typing.Protocol definitions composed flatly from 41 orthogonal atomic operational mixins (28 algebraic + 9 categorical + 4 geometric) with zero diamond inheritance (EP-0005).

  • Structural over Nominal: Concrete structures never inherit from a monolithic Structure base class; conformance is checked structurally via isinstance(obj, Protocol).
  • Law Markers (Law*): Semantic axioms that cannot be expressed as method signatures (e.g., LawCommutative, LawIdempotent, LawAssociative) are declared via marker protocols and verified empirically by algebrax.verification.

2. Module Dependency Boundaries

AlgebraX enforces strict acyclic layer direction across src/algebrax/, automatically audited in CI by tests/algebrax/test_hygiene.py and tests/algebrax/test_registry_invariants.py:

flowchart TD
    Core["algebra / display\n(Zero-dependency Protocols & Mixins)"] --> Semiring["semiring / group / lattice"]
    Semiring --> Matrix["matrix / tensor / trie"]
    Matrix --> Domains["clifford / lie / rep / galois / variety / topology / category / automata / transforms / analysis / probability / metrics"]
    Domains --> Registry["registry\n(Pure-data 0-cell catalog, lazy verification)"]
    Domains --> Verification["verification\n(Axiom checkers & registry bridge)"]
    Registry --> CLI["cli / __main__\n(catalog, describe, inspect, verify)"]
    Verification --> CLI

Enforced Isolation Rules

Rule Enforced By Rationale
algebra and display never import registry test_ast_isolation_algebra_and_display_never_import_registry Keeps foundational protocols and rich display helpers cycle-free.
Importing algebrax.registry loads zero verification modules test_fresh_process_registry_import_loads_zero_verification_modules Preserves fast startup and keeps the catalog pure data.
algebrax.rep never imports algebrax.verification test_rep_has_zero_verification_imports Prevents production computation modules from depending on test/verification helpers.
Zero EP-XXXX references in src/algebrax/ or docs/guide/ test_no_ep_references_in_src, test_no_ep_references_in_user_guide Keeps production source and end-user guides decoupled from internal proposal numbering.

3. Unified Registry Invariants (I-1–I-5)

algebrax.registry exposes an immutable MappingProxyType catalog of all built-in concrete 0-cell algebraic structures (classes and canonical factory families), governed by five CI-enforced invariants (EP-0255):

  1. I-1 — Reified Protocol & Law Types: Every StructureEntry.protocols and StructureEntry.laws tuple holds live type objects, never raw strings.
  2. I-2 — Protocol & Law Honesty (Anti-Vacuity): Every registered structure must pass verify_structure(key) with zero vacuous checks, including explicit Popper negative controls confirming false law claims fail.
  3. I-3 — Full AMDS Signature Coverage: Every registered structure's docstring includes an Algebraic Signature: block conforming to EP-0002.
  4. I-4 — Completeness Audit: Every public exported 0-cell class across algebrax.* must appear in algebrax.registry or be explicitly classified in EXPLICIT_EXCLUSIONS (for carriers, 1-cell morphisms/functors, ideals, or solvers) in tests/algebrax/test_registry_invariants.py.
  5. I-5 — Bidirectional Zero-Arg Contract: zero_arg=True entries instantiate with zero arguments; parameterized (zero_arg=False) entries provide a canonical example= factory and produce descriptive TypeError diagnostics when invoked without parameters.

4. Verification Architecture

Mathematical correctness follows a 5-layer verification cascade (EP-0003, live traceability in Verification & Stamp of Trust):

  1. Runtime Axiom Checkers (algebrax.verification): Executable law verifiers (verify_semiring_laws, verify_field_laws, verify_hopf_algebra_laws, verify_groebner_basis_laws, verify_tropical_hypersurface_laws, etc.) plus verify_structure() bridge dispatch.
  2. Property-Based Fuzzing (hypothesis): Randomized algebraic law stress-testing across domains.
  3. Differential Oracles (numpy, scipy, sympy): Cross-checking exact solvers, Gröbner bases, SVD/QR, homology, and tropical lower/upper convex hulls against independent reference engines.
  4. Probabilistic & Formal Proofs: Schwartz–Zippel polynomial identity testing and z3-solver SMT proofs for semiring/lattice laws.
  5. Executable Recipes (nbmake): Every Golden Source script in recipes/ and its synced .ipynb notebook executes end-to-end as an integration test.

Every *.py::test_* reference in docs/verification.md is AST-verified in CI by test_verification_doc_traceability_paths_resolve.