Skip to content

Equational theories

A SignatureTheory holds the checked equations of a syntax declaration and decides them with rewriting, normalization by evaluation, and equality saturation (Equations and normalization §7). Program.theories maps each syntax with equations to its theory.

theory

The equational theory of a signature, decided by three backends.

A signature's equations block declares oriented rules l ~> r and laws l = r. SignatureTheory sends each to the backend that decides it:

  • oriented rules to the checked rewrite system of quivers.structural.rewriting, whose termination and confluence are established or recorded as assumed;
  • laws that make up a type-directed structure (functions with beta and eta, products with surjective pairing, a monad) to normalization by evaluation over the typing judgment, quivers.structural.theories.nbe;
  • every other law to equality saturation with the declared extraction cost, quivers.structural.egraph.

SignatureTheory.normalize applies the backends in that order until the term no longer changes, each step an equality of the theory, and SignatureTheory.equal_modulo compares normal forms; a theory with saturation laws compares by saturating both terms together.

SignatureTheory

SignatureTheory(signature: Signature, equations: Sequence[Equation], *, assume_terminating: bool = False, strategy: Strategy = INNERMOST, cost: CostModel | None = None, typing: str | None = None)

The checked equations of a signature with their decision procedures.

PARAMETER DESCRIPTION
signature

The signature.

TYPE: Signature

equations

Its rules and laws, checked by make_equation.

TYPE: Sequence of Equation

assume_terminating

Whether termination of the rules is a recorded assumption.

TYPE: bool DEFAULT: False

strategy

The rewriting strategy.

TYPE: Strategy DEFAULT: INNERMOST

cost

The extraction cost of the saturation backend; required when some law goes to it.

TYPE: CostModel or None DEFAULT: None

typing

The typing judgment normalization by evaluation reads.

TYPE: str or None DEFAULT: None

RAISES DESCRIPTION
RewriteError

If the rules fail their termination or confluence check, a law needs the saturation backend and no cost is declared, or a type-directed structure has no typing judgment named.

Source code in src/quivers/structural/theories/theory.py
def __init__(
    self,
    signature: Signature,
    equations: Sequence[Equation],
    *,
    assume_terminating: bool = False,
    strategy: Strategy = Strategy.INNERMOST,
    cost: CostModel | None = None,
    typing: str | None = None,
) -> None:
    self.signature = signature
    self.strategy = strategy
    self.typing = typing
    rules = tuple(e for e in equations if e.oriented)
    laws = tuple(e for e in equations if not e.oriented)
    self.report: TheoryReport = check_theory(
        EquationalTheory(
            signature=signature,
            rules=rules,
            laws=laws,
            assume_terminating=assume_terminating,
            strategy=strategy,
        )
    )
    self.rules = RewriteSystem(signature, rules)
    self.structure: TypeDirectedStructure = recognize(laws)
    typed = set(self.structure.laws)
    self.saturated_laws = tuple(law for law in laws if law.name not in typed)
    if not self.structure.empty and typing is None:
        raise RewriteError(
            f"syntax {signature.name!r}: laws {sorted(typed)} are type "
            "directed and need the typing judgment; declare it with "
            "`equations [typing=J]`"
        )
    self.saturation: Saturation | None = None
    if self.saturated_laws:
        if cost is None:
            names = sorted(law.name for law in self.saturated_laws)
            raise RewriteError(
                f"syntax {signature.name!r}: laws {names} cannot be "
                "oriented and are decided by equality saturation, which "
                "extracts by a declared cost; declare it with "
                "`equations [cost=size]` or `[cost=depth]`"
            )
        self.saturation = Saturation(signature, self.saturated_laws, cost)
    self._normalizer: Normalizer | None = None

normalizer property

normalizer: Normalizer | None

The normalizer by evaluation, once the judgment is attached.

RETURNS DESCRIPTION
Normalizer or None

It, or None when the theory has no type-directed laws.

RAISES DESCRIPTION
RewriteError

If the theory has type-directed laws and no judgment is bound.

attach_judgments

attach_judgments(judgments: Mapping[str, JudgmentSystem]) -> None

Bind the typing judgment of the type-directed structures.

PARAMETER DESCRIPTION
judgments

The module's judgments.

TYPE: Mapping of str to JudgmentSystem

RAISES DESCRIPTION
RewriteError

If the named judgment is not declared.

Source code in src/quivers/structural/theories/theory.py
def attach_judgments(self, judgments: Mapping[str, JudgmentSystem]) -> None:
    """Bind the typing judgment of the type-directed structures.

    Parameters
    ----------
    judgments : Mapping of str to JudgmentSystem
        The module's judgments.

    Raises
    ------
    RewriteError
        If the named judgment is not declared.
    """
    if self.structure.empty or self.typing is None:
        return
    system = judgments.get(self.typing)
    if system is None:
        raise RewriteError(
            f"syntax {self.signature.name!r}: typing judgment "
            f"{self.typing!r} is not declared"
        )
    structure = attach_types(self.structure, system, self.typing)
    self._normalizer = Normalizer(self.signature, structure, system, self.typing)

normalize

normalize(term: Term, context: Sequence[Term] = (), max_rounds: int = 32) -> Term

A normal form of a term in the theory.

PARAMETER DESCRIPTION
term

The term.

TYPE: Term

context

The types of its free variables, for normalization by evaluation.

TYPE: Sequence of Term DEFAULT: ()

max_rounds

The rounds of the three backends allowed.

TYPE: int DEFAULT: 32

RETURNS DESCRIPTION
Term

The normal form.

RAISES DESCRIPTION
RewriteError

If the backends do not reach a common fixed point in time.

Source code in src/quivers/structural/theories/theory.py
def normalize(
    self, term: Term, context: Sequence[Term] = (), max_rounds: int = 32
) -> Term:
    """A normal form of a term in the theory.

    Parameters
    ----------
    term : Term
        The term.
    context : Sequence of Term
        The types of its free variables, for normalization by
        evaluation.
    max_rounds : int
        The rounds of the three backends allowed.

    Returns
    -------
    Term
        The normal form.

    Raises
    ------
    RewriteError
        If the backends do not reach a common fixed point in time.
    """
    current = term
    for _ in range(max_rounds):
        following = self.rules.normalize(current, self.strategy)
        normalizer = self.normalizer
        if normalizer is not None:
            following = normalizer.normalize(following, context)
        if self.saturation is not None:
            following, _ = self.saturation.normalize(following)
        if following == current:
            return current
        current = following
    raise RewriteError(
        f"syntax {self.signature.name!r}: normalization did not settle in "
        f"{max_rounds} rounds"
    )

equal_modulo

equal_modulo(left: Term, right: Term, context: Sequence[Term] = ()) -> bool

Whether two terms are equal in the theory.

PARAMETER DESCRIPTION
left

The terms.

TYPE: Term

right

The terms.

TYPE: Term

context

The types of their free variables.

TYPE: Sequence of Term DEFAULT: ()

RETURNS DESCRIPTION
bool

Equality of their normal forms; with saturation laws, of their classes after saturating both.

Source code in src/quivers/structural/theories/theory.py
def equal_modulo(
    self, left: Term, right: Term, context: Sequence[Term] = ()
) -> bool:
    """Whether two terms are equal in the theory.

    Parameters
    ----------
    left, right : Term
        The terms.
    context : Sequence of Term
        The types of their free variables.

    Returns
    -------
    bool
        Equality of their normal forms; with saturation laws, of their
        classes after saturating both.
    """
    one = self.normalize(left, context)
    two = self.normalize(right, context)
    if one == two:
        return True
    if self.saturation is not None:
        return self.saturation.equal_modulo(one, two)
    return False