Skip to content

Laws

A Law is a named statement about a carrier, or about the tensors an implementation returns. Every member of the algebra hierarchy is Lawful: its class lists the laws it introduces, and the member reports a LawClaim for each law it has. The claim's status is read from its evidence: Proved for a written argument, Decided for a decision procedure on a stated domain, Tested for finitely many cases of a larger domain, and Assumed for a declaration. Proved and decided claims are established. The arguments of the shipped classes are in Semirings, t-norms, and change of base §8.

laws

Laws, the evidence for them, and the structures that claim them.

A Law is a named statement about a carrier, or about the tensors an implementation returns. A LawClaim says that a law holds of a subject and carries its evidence; the claim's status is read from the kind of evidence, so the two cannot disagree:

  • Proved: a written argument, with the published results it uses;
  • Decided: a procedure that decides the law on the stated domain, such as every tuple of a finite carrier;
  • Tested: finitely many points of a larger domain, which can refute a law and cannot establish it;
  • Assumed: a declaration and nothing more.

Proved and decided claims are established. A structure of the algebra hierarchy is Lawful: its class lists the laws it introduces, and it reports a claim for each law it has, with evidence declared by its own class and never inherited, since a subclass may replace an operation. Requirement pairs a law with the subject it must hold of, which is what a plan asks for.

PLUS_ASSOCIATIVE module-attribute

PLUS_ASSOCIATIVE = Law(name='plus-associative', statement='(a + b) + c = a + (b + c) for all a, b, c')

Associativity of the sum.

PLUS_COMMUTATIVE module-attribute

PLUS_COMMUTATIVE = Law(name='plus-commutative', statement='a + b = b + a for all a, b')

Commutativity of the sum, which licenses reordering a finite sum.

PLUS_UNIT module-attribute

PLUS_UNIT = Law(name='plus-unit', statement='0 + a = a + 0 = a for all a')

Zero is the unit of the sum.

TIMES_ASSOCIATIVE module-attribute

TIMES_ASSOCIATIVE = Law(name='times-associative', statement='(a * b) * c = a * (b * c) for all a, b, c')

Associativity of the product.

TIMES_COMMUTATIVE module-attribute

TIMES_COMMUTATIVE = Law(name='times-commutative', statement='a * b = b * a for all a, b')

Commutativity of the product, which licenses elimination orders.

TIMES_UNIT module-attribute

TIMES_UNIT = Law(name='times-unit', statement='1 * a = a * 1 = a for all a')

One is the unit of the product.

DISTRIBUTIVE module-attribute

DISTRIBUTIVE = Law(name='distributive', statement='a * (b + c) = a * b + a * c and (a + b) * c = a * c + b * c')

Distributivity, which licenses merging pending contributions.

ZERO_ANNIHILATES module-attribute

ZERO_ANNIHILATES = Law(name='zero-annihilates', statement='0 * a = a * 0 = 0 for all a')

Zero annihilates the product.

PLUS_IDEMPOTENT module-attribute

PLUS_IDEMPOTENT = Law(name='plus-idempotent', statement='a + a = a for all a')

Idempotence, which licenses dropping a repeated contribution.

PLUS_SELECTIVE module-attribute

PLUS_SELECTIVE = Law(name='plus-selective', statement='a + b is a or b for all a, b')

Selectivity, which a best derivation and its witness need.

SUPERIOR module-attribute

SUPERIOR = Law(name='superior', statement='under the declared total order, a * b is no better than a and no better than b, and the product is monotone in each argument')

Superiority (Knuth 1977), which licenses stopping best-first at the goal.

STAR_LEAST_SOLUTION module-attribute

STAR_LEAST_SOLUTION = Law(name='star-least-solution', statement='star(A) is the least solution X of X = I + A X on the declared domain')

The star of a matrix solves its linear system, which linear cycles need.

DIVISION module-attribute

DIVISION = Law(name='division', statement='(a * b) / b = a for all a and every nonzero b')

Division by nonzero elements, which normalizing needs.

ADDITIVE_INVERSE module-attribute

ADDITIVE_INVERSE = Law(name='additive-inverse', statement='a + (-a) = 0 for all a')

An additive inverse, which subtraction needs.

ZERO_SUM_FREE module-attribute

ZERO_SUM_FREE = Law(name='zero-sum-free', statement='a + b = 0 implies a = b = 0')

No sum of nonzero elements is zero.

NO_ZERO_DIVISORS module-attribute

NO_ZERO_DIVISORS = Law(name='no-zero-divisors', statement='a * b = 0 implies a = 0 or b = 0')

No product of nonzero elements is zero.

SEMIMODULE_ACTION module-attribute

SEMIMODULE_ACTION = Law(name='semimodule-action', statement='the action of the weights on the statistics is associative with the product, unital, distributes over the sum of the weights and over the sum of the statistics, and either zero annihilates it')

The action that carries expected statistics.

OMEGA_CONTINUOUS module-attribute

OMEGA_CONTINUOUS = Law(name='omega-continuous', statement='the natural order is a complete partial order on increasing chains, and sum and product preserve their suprema')

Continuity, which gives a least fixed point of a monotone polynomial system.

REAL_EMBEDDING module-attribute

REAL_EMBEDDING = Law(name='real-embedding', statement='to_reals is an injective homomorphism of semirings into the nonnegative reals that preserves the natural order, and from_reals inverts it on its image')

An embedding into the nonnegative reals, whose completion [0, inf] gives a monotone polynomial system its least fixed point and its certificate.

FOLD_LINEAR module-attribute

FOLD_LINEAR = Law(name='fold-linear', statement='h(x + y) = h(x) + h(y) and h(k x) = k h(x) for weights k')

Linearity of a fold of the detailed meaning over the weights.

OPERATIONS_CLOSED module-attribute

OPERATIONS_CLOSED = Law(name='operations-closed', statement='a + b, and a * b and the action when the carrier has them, lie in the carrier whenever their operands do')

The operations do not leave the carrier.

JOIN_ITERATED module-attribute

JOIN_ITERATED = Law(name='join-iterated', statement='a sum over several axes equals the sums over one axis at a time, in any order')

A reduction over several axes is iterated, which a contraction plan needs.

COMPOSITION_ASSOCIATIVE module-attribute

COMPOSITION_ASSOCIATIVE = Law(name='composition-associative', statement='(f >> g) >> h = f >> (g >> h) for composable matrices')

Associativity of matrix composition.

ZERO_FORM module-attribute

ZERO_FORM = Law(name='zero-form', statement='zero_like(shape, like) is the zero with shape (*shape, *element_shape), the dtype and device of like, and no gradient, and a dtype that cannot hold it is refused', scope='implementation')

The zero is made in the shape, dtype and device a computation needs.

UNIT_FORM module-attribute

UNIT_FORM = Law(name='unit-form', statement='unit_like(shape, like) is the unit with shape (*shape, *element_shape), the dtype and device of like, and no gradient, and a dtype that cannot hold it is refused', scope='implementation')

The unit is made in the shape, dtype and device a computation needs.

EMPTY_JOIN module-attribute

EMPTY_JOIN = Law(name='empty-join', statement='a sum over an axis of size zero is the zero of the remaining shape, in the dtype and device of its input, and its derivative is zero', scope='implementation')

A reduction over no terms is the zero.

TIE_FIRST_MAXIMIZER module-attribute

TIE_FIRST_MAXIMIZER = Law(name='tie-first-maximizer', statement='a selective sum returns as its witness the first best operand in row-major order of the reduced axes, and that operand receives the whole derivative, in every operation', scope='implementation')

The one tie rule of a selective sum, as its implementation keeps it.

STAR_DECIDED module-attribute

STAR_DECIDED = Law(name='star-decided', statement='star returns a value exactly on its declared domain: for a matrix outside it raises DivergentClosureError, and when rounding cannot decide which side of the boundary a cycle lies on it raises UndecidedClosureError', scope='implementation')

The closure refuses where it has no value and where rounding cannot decide.

SEMIRING_LAWS module-attribute

The laws of a semiring whose product need not commute.

FIRST_MAXIMIZER module-attribute

FIRST_MAXIMIZER = TieRule(name='first-maximizer', statement='the first maximizer in declared order receives the whole subgradient, and the value carries it as the witness')

The one tie rule of every selective sum.

LAW_GRID module-attribute

LAW_GRID = 'tests/evidence/test_law_grid.py::test_cell'

The test that checks the laws of the shipped structures.

TEST_DOMAINS module-attribute

TEST_DOMAINS: Mapping[str, str] = MappingProxyType({ZERO_FORM.name: 'dtypes float64, float32, int64 and bool, and index shapes (), (0,), (3,), (2, 0) and (0, 2, 3), on the CPU', UNIT_FORM.name: 'dtypes float64, float32, int64 and bool, and index shapes (), (0,), (3,), (2, 0) and (0, 2, 3), on the CPU', EMPTY_JOIN.name: 'six index shapes with an axis of size zero, reduced over one or two axes, in float64 and float32', STAR_DECIDED.name: "every matrix of the row's closure cases inside and outside the domain, of orders zero to three, in float64 and float32", TIE_FIRST_MAXIMIZER.name: "every pair, triple and four-tuple of the row's samples, ties included, in float64 and float32"})

Where the tests of the laws about the implementation take their cases.

LawScope

LawScope = Literal['carrier', 'implementation']

What a law is about: the mathematical carrier, or the tensors of an implementation.

Citation didactic-model

Citation(*, reference: str, locator: str, doi: str | None = None)

Bases: Model

A published source and the place in it that is relied on.

PARAMETER DESCRIPTION
reference

Authors, year and title.

TYPE: str

locator

The section, theorem, equation, page, or the notion relied on.

TYPE: str

doi

The digital object identifier, when the source has one.

TYPE: str or None DEFAULT: None

Law didactic-model

Law(*, name: str, statement: str, scope: LawScope = 'carrier')

Bases: Model

An equation or property that an executor may rely on.

A law about the carrier is a statement over the mathematical carrier; a floating point implementation satisfies no algebraic law exactly, and its rounding is a separate matter. A law about the implementation states a property of the tensors the code returns, such as their shape, dtype, device and derivative.

PARAMETER DESCRIPTION
name

A short name, unique among laws.

TYPE: str

statement

The statement, quantified over the carrier.

TYPE: str

scope

"carrier" or "implementation".

TYPE: LawScope DEFAULT: 'carrier'

TieRule didactic-model

TieRule(*, name: str, statement: str)

Bases: Model

The element of the subdifferential that a selection returns at a tie.

PARAMETER DESCRIPTION
name

A short name.

TYPE: str

statement

Which operand receives the gradient.

TYPE: str

ClaimSubject didactic-model

ClaimSubject(*, kind: Literal['algebra', 'computation', 'interpretation', 'operation', 'presentation', 'estimator'], name: str)

Bases: Model

What a law claim is about.

PARAMETER DESCRIPTION
kind

"algebra", "computation", "interpretation", "operation", "presentation" or "estimator".

TYPE: str

name

The subject's qualified Python name or stable identifier text.

TYPE: str

LawEvidence didactic-model

LawEvidence()

Bases: TaggedUnion

Why a law is believed: an argument, a decision, a test, or a declaration.

Proved didactic-model

Proved(*, argument: str, sources: tuple[Citation, ...] = (), tag: Literal['proved'] = 'proved')

Bases: LawEvidence

Evidence that is a written argument.

PARAMETER DESCRIPTION
argument

Where the argument is written, as a documentation path and anchor.

TYPE: str

sources

The published results the argument uses.

TYPE: tuple of Citation DEFAULT: ()

tag

Always "proved".

TYPE: str DEFAULT: 'proved'

Decided didactic-model

Decided(*, procedure: Literal['exhaustive', 'polynomial-identity', 'real-closed-field'], domain: str, check: str, tag: Literal['decided'] = 'decided')

Bases: LawEvidence

Evidence from a procedure that decides the law on the carrier.

PARAMETER DESCRIPTION
procedure

"exhaustive" (every tuple of a finite carrier), "polynomial-identity" (a normal form of polynomial terms) or "real-closed-field" (quantifier elimination over the reals).

TYPE: str

domain

The carrier the decision covers.

TYPE: str

check

The test that runs the procedure again.

TYPE: str

tag

Always "decided".

TYPE: str DEFAULT: 'decided'

Tested didactic-model

Tested(*, check: str, cases: int, domain: str, tag: Literal['tested'] = 'tested')

Bases: LawEvidence

Evidence from tests on finitely many points of a larger domain.

A test can refute a law and cannot establish one on an infinite domain, so tested laws license no exact or certified result.

PARAMETER DESCRIPTION
check

The test.

TYPE: str

cases

How many points it evaluates, at least one.

TYPE: int

domain

Where the points come from.

TYPE: str

tag

Always "tested".

TYPE: str DEFAULT: 'tested'

Assumed didactic-model

Assumed(*, declared_by: str, reason: str, tag: Literal['assumed'] = 'assumed')

Bases: LawEvidence

A law that is declared and neither proved, decided nor tested.

PARAMETER DESCRIPTION
declared_by

Who or what declares it.

TYPE: str

reason

Why it is assumed.

TYPE: str

tag

Always "assumed".

TYPE: str DEFAULT: 'assumed'

LawStatus

Bases: str, Enum

The status a law claim has, fixed by its evidence.

LawClaim didactic-model

LawClaim(*, law: Law, subject: ClaimSubject, evidence: LawEvidence)

Bases: Model

A claim that a law holds of a subject, with its evidence.

The status is read from the evidence, so the two cannot disagree. QIEC equality evidence and registrations in a type class are not law evidence: they prove index equalities and record declarations.

PARAMETER DESCRIPTION
law

The law.

TYPE: Law

subject

What it holds of.

TYPE: ClaimSubject

evidence

Why it is believed.

TYPE: LawEvidence

status property

status: LawStatus

The status of the claim.

RETURNS DESCRIPTION
LawStatus

Proved, decided, tested or assumed, by the kind of evidence.

established property

established: bool

Whether the claim is proved or decided.

RETURNS DESCRIPTION
bool

True when an argument or a decision procedure stands behind the claim. Only established claims license exact, certified and probabilistic results.

Requirement didactic-model

Requirement(*, law: Law, subject: ClaimSubject)

Bases: Model

A law that a plan needs to hold of a subject.

PARAMETER DESCRIPTION
law

The law.

TYPE: Law

subject

What it must hold of.

TYPE: ClaimSubject

Lawful

Bases: ABC

A structure whose class membership claims laws.

A class lists in introduces the laws (from LAWS) that membership in it adds; laws collects them along the class hierarchy. The evidence of a claim comes from declarations on the member's own class, never from a parent class, since a subclass may replace an operation: proof is the argument for every claimed law about the carrier, and tested gives, for each law about the implementation that the law grid checks, the number of cases it evaluates. A law without either is assumed.

name abstractmethod property

name: str

The structure's name.

RETURNS DESCRIPTION
str

A human-readable name.

subject property

subject: ClaimSubject

What the structure's claims are about.

RETURNS DESCRIPTION
ClaimSubject

The qualified name of the structure's class.

laws classmethod

laws() -> tuple[Law, ...]

The laws that membership in this class claims.

RETURNS DESCRIPTION
tuple of Law

Each law once, in the order the hierarchy introduces them.

Source code in src/quivers/core/laws.py
@classmethod
def laws(cls) -> tuple[Law, ...]:
    """The laws that membership in this class claims.

    Returns
    -------
    tuple of Law
        Each law once, in the order the hierarchy introduces them.
    """
    found: list[Law] = []
    for klass in reversed(cls.__mro__):
        for law in vars(klass).get("introduces", ()):
            if law not in found:
                found.append(law)
    return tuple(found)

evidence

evidence(law: Law) -> LawEvidence

The evidence for one claimed law.

PARAMETER DESCRIPTION
law

A law of laws.

TYPE: Law

RETURNS DESCRIPTION
LawEvidence

The evidence the member's own class declares.

Source code in src/quivers/core/laws.py
def evidence(self, law: Law) -> LawEvidence:
    """The evidence for one claimed law.

    Parameters
    ----------
    law : Law
        A law of `laws`.

    Returns
    -------
    LawEvidence
        The evidence the member's own class declares.
    """
    own = vars(type(self))
    if law.scope == "carrier":
        proof = own.get("proof")
        if isinstance(proof, Proved):
            return proof
    else:
        tested = own.get("tested")
        if isinstance(tested, Mapping) and law.name in tested:
            return Tested(
                check=f"{LAW_GRID}[{self.name}, {law.name}]",
                cases=int(tested[law.name]),
                domain=TEST_DOMAINS[law.name],
            )
    return self.assumption("membership in the class is the only ground")

assumption

assumption(reason: str) -> Assumed

Evidence that a law is only declared.

PARAMETER DESCRIPTION
reason

Why it is assumed.

TYPE: str

RETURNS DESCRIPTION
Assumed

The declaration by the member's class.

Source code in src/quivers/core/laws.py
def assumption(self, reason: str) -> Assumed:
    """Evidence that a law is only declared.

    Parameters
    ----------
    reason : str
        Why it is assumed.

    Returns
    -------
    Assumed
        The declaration by the member's class.
    """
    cls = type(self)
    return Assumed(
        declared_by=f"{cls.__module__}.{cls.__qualname__}", reason=reason
    )

claim

claim(law: Law) -> LawClaim

The claim that one law holds, with its evidence.

PARAMETER DESCRIPTION
law

A law of laws.

TYPE: Law

RETURNS DESCRIPTION
LawClaim

The claim.

RAISES DESCRIPTION
ValueError

If the member does not claim the law.

Source code in src/quivers/core/laws.py
def claim(self, law: Law) -> LawClaim:
    """The claim that one law holds, with its evidence.

    Parameters
    ----------
    law : Law
        A law of `laws`.

    Returns
    -------
    LawClaim
        The claim.

    Raises
    ------
    ValueError
        If the member does not claim the law.
    """
    if law not in self.laws():
        raise ValueError(f"{self.name} does not claim {law.name!r}")
    return LawClaim(law=law, subject=self.subject, evidence=self.evidence(law))

claims

claims() -> tuple[LawClaim, ...]

The claims of every law of laws.

RETURNS DESCRIPTION
tuple of LawClaim

One claim per law.

Source code in src/quivers/core/laws.py
def claims(self) -> tuple[LawClaim, ...]:
    """The claims of every law of `laws`.

    Returns
    -------
    tuple of LawClaim
        One claim per law.
    """
    return tuple(self.claim(law) for law in self.laws())

establishes

establishes(*laws: Law) -> bool

Whether every given law is claimed and established.

PARAMETER DESCRIPTION
*laws

Laws of the catalogue.

TYPE: Law DEFAULT: ()

RETURNS DESCRIPTION
bool

True when each law is claimed with a proved or decided status, read from its evidence as LawClaim.status reads it. The evidence of a structure is fixed by its class and its parts, so the answer is kept on the structure for the laws' names.

Source code in src/quivers/core/laws.py
def establishes(self, *laws: Law) -> bool:
    """Whether every given law is claimed and established.

    Parameters
    ----------
    *laws : Law
        Laws of the catalogue.

    Returns
    -------
    bool
        True when each law is claimed with a proved or decided status,
        read from its evidence as `LawClaim.status` reads it. The
        evidence of a structure is fixed by its class and its parts, so
        the answer is kept on the structure for the laws' names.
    """
    memo: dict[tuple[str, ...], bool] = vars(self).setdefault("_established", {})
    key = tuple(law.name for law in laws)
    found = memo.get(key)
    if found is None:
        claimed = self.laws()
        found = all(
            law in claimed and isinstance(self.evidence(law), (Proved, Decided))
            for law in laws
        )
        memo[key] = found
    return found

requirements

requirements(laws: Iterable[Law], subject: ClaimSubject) -> tuple[Requirement, ...]

Pair laws with the subject they must hold of.

PARAMETER DESCRIPTION
laws

The laws.

TYPE: Iterable of Law

subject

The subject.

TYPE: ClaimSubject

RETURNS DESCRIPTION
tuple of Requirement

One requirement per law, in order.

Source code in src/quivers/core/laws.py
def requirements(laws: Iterable[Law], subject: ClaimSubject) -> tuple[Requirement, ...]:
    """Pair laws with the subject they must hold of.

    Parameters
    ----------
    laws : Iterable of Law
        The laws.
    subject : ClaimSubject
        The subject.

    Returns
    -------
    tuple of Requirement
        One requirement per law, in order.
    """
    return tuple(Requirement(law=law, subject=subject) for law in laws)

weakest

weakest(claims: Iterable[LawClaim]) -> LawStatus

The weakest status among claims.

PARAMETER DESCRIPTION
claims

The claims; none at all is proved.

TYPE: Iterable of LawClaim

RETURNS DESCRIPTION
LawStatus

The status no claim falls below, in the order assumed, tested, decided, proved.

Source code in src/quivers/core/laws.py
def weakest(claims: Iterable[LawClaim]) -> LawStatus:
    """The weakest status among claims.

    Parameters
    ----------
    claims : Iterable of LawClaim
        The claims; none at all is proved.

    Returns
    -------
    LawStatus
        The status no claim falls below, in the order assumed, tested,
        decided, proved.
    """
    status = LawStatus.PROVED
    for claim in claims:
        if _STRENGTH[claim.status] < _STRENGTH[status]:
            status = claim.status
    return status