Rewriting¶
Oriented equations form a higher-order pattern rewrite system. This module normalizes terms under a strategy, computes critical pairs, searches for a recursive path order, and records termination and confluence as proved or assumed laws (Equations and normalization §2 to §4).
rewriting
¶
Equational theories on binding signatures: checked rewriting and normalizers.
An EquationalTheory declares second-order equations over a Signature
[@fiore2010second; @fiore2010algebraic]. An oriented equation is a
RewriteRule whose left side is in Miller's pattern fragment, so the rules
form a higher-order pattern rewrite system [@mayr1998higher]. Such a system
is confluent when it terminates and every critical pair joins, and its
normal forms then decide the equational theory of its rules
[@hamana2017decidable]. check_theory establishes both, soundly and
incompletely:
- Critical pairs. Every overlap of two left sides is computed by
higher-order pattern unification (
quivers.structural.patterns). Each peak is instantiated with a generic constant for every unsolved metavariable, both reducts are normalized, and a pair whose normal forms differ is reported with its peak. - Termination. A recursive path order is searched for: a precedence on
the constructors under which every left side is greater than its right
side [@dershowitz1982orderings; @baader1998term]. The search reads each
rule first-order, with binders as ordinary symbols, bound variables as
one least constant, and metavariables as variables. This reading is
sound when every right-side metavariable is applied to variables only,
because instantiating it then renames and never copies a term; a rule
that instantiates a metavariable with a term is not ordered, and its
theory needs the recorded assumption
assume terminating.
The result records each law's status with quivers.core.laws evidence:
termination is proved by the path order found, or assumed when declared;
confluence is proved from joinable critical pairs and proved termination,
or assumed when termination is.
RewriteSystem.normalize computes normal forms under a strategy:
innermost, outermost, or node-wise during construction (RewriteSystem.build).
Normal forms are memoized in a table keyed by the term, whose hash is
structural and cached, so a shared subterm is normalized once.
TERMINATION
module-attribute
¶
TERMINATION = Law(name='rewriting-terminates', statement="every sequence of rewrites by the theory's rules is finite")
The law that a theory's rules terminate.
CONFLUENCE
module-attribute
¶
CONFLUENCE = Law(name='rewriting-confluent', statement="any two rewrites of one term by the theory's rules have a common reduct")
The law that a theory's rules are confluent.
Precedence
¶
A strict partial order on symbols, as its transitively closed pairs.
RewriteError
¶
Bases: ValueError
A rewrite system or equational theory is ill-formed or fails a check.
RewriteLimitExceeded
¶
Bases: RewriteError
Normalization took more steps than its budget allows.
Strategy
¶
Bases: str, Enum
Where a normalizer looks for the next redex.
INNERMOST normalizes arguments before their parent, OUTERMOST
contracts the leftmost-outermost redex first, and NODEWISE
normalizes each node as it is built from normal arguments, so a term
built by RewriteSystem.build is normal at every stage.
Equation
didactic-model
¶
Equation(*, name: str, left: Pattern = field(opaque=True, extras=OPAQUE_BY_VALUE), right: Pattern = field(opaque=True, extras=OPAQUE_BY_VALUE), sort: str, metas: dict = field(default_factory=dict, opaque=True, extras=OPAQUE_BY_VALUE), oriented: bool = False, line: int = 0, col: int = 0)
Bases: ValueModel
A named second-order equation between two patterns.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Its name.
TYPE:
|
left
|
Its two sides.
TYPE:
|
right
|
Its two sides.
TYPE:
|
sort
|
The sort of both sides.
TYPE:
|
metas
|
The type of every metavariable it uses.
TYPE:
|
oriented
|
Whether it is a rule
TYPE:
|
line
|
Its source position, zero when built in Python.
TYPE:
|
col
|
Its source position, zero when built in Python.
TYPE:
|
text
¶
text(signature: Signature) -> str
The equation as source writes it.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
|
Source code in src/quivers/structural/rewriting.py
RewriteSystem
¶
A higher-order pattern rewrite system with memoized normal forms.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature of the terms.
TYPE:
|
rules
|
Oriented equations.
TYPE:
|
max_steps
|
The number of contractions one normalization may make.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
RewriteError
|
If a rule is not oriented. |
Source code in src/quivers/structural/rewriting.py
with_signature
¶
with_signature(signature: Signature) -> RewriteSystem
The same rules over an extended signature.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
A signature that declares every constructor of this one.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RewriteSystem
|
A system with a fresh memo table. |
Source code in src/quivers/structural/rewriting.py
contract
¶
Contract a redex at the root, by the first rule that matches.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of Equation and Term, or None
|
The rule and the contractum, or |
Source code in src/quivers/structural/rewriting.py
normalize
¶
The normal form of a term.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
strategy
|
Where to contract first. Every strategy reaches the same normal form when the system is confluent and terminating.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
Its normal form. |
| RAISES | DESCRIPTION |
|---|---|
RewriteLimitExceeded
|
If more than |
Source code in src/quivers/structural/rewriting.py
build
¶
Build a node from normal arguments and normalize it at once.
This is node-wise normalization: a term assembled bottom-up through
build is normal at every node, which is how a derivation that
constructs meanings item by item keeps them small.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor.
TYPE:
|
arguments
|
Its arguments, each already normal.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The normal form of the node. |
Source code in src/quivers/structural/rewriting.py
equal_modulo
¶
Whether two terms have the same normal form.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
The terms.
TYPE:
|
right
|
The terms.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Equality modulo the rules, decided when they are confluent and terminating. |
Source code in src/quivers/structural/rewriting.py
is_normal
¶
is_normal(term: Term) -> bool
Whether no subterm is a redex.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/quivers/structural/rewriting.py
CriticalPair
didactic-model
¶
CriticalPair(*, outer: str, inner: str, position: tuple[int, ...] = (), peak: Term = field(opaque=True, extras=OPAQUE_BY_VALUE), left: Term = field(opaque=True, extras=OPAQUE_BY_VALUE), right: Term = field(opaque=True, extras=OPAQUE_BY_VALUE), left_normal: Term | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE), right_normal: Term | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE), joinable: bool = False, extended: Signature = field(opaque=True, extras=OPAQUE_BY_VALUE))
Bases: ValueModel
An overlap of two rules and whether its two reducts join.
| PARAMETER | DESCRIPTION |
|---|---|
outer
|
The rule contracted at the root of the peak and the one contracted
at
TYPE:
|
inner
|
The rule contracted at the root of the peak and the one contracted
at
TYPE:
|
position
|
The argument path from the root of the peak to the inner redex.
TYPE:
|
peak
|
The most general term both rules rewrite, with a generic constant
TYPE:
|
left
|
The reducts by the outer and the inner rule.
TYPE:
|
right
|
The reducts by the outer and the inner rule.
TYPE:
|
left_normal
|
Their normal forms,
TYPE:
|
right_normal
|
Their normal forms,
TYPE:
|
joinable
|
Whether the normal forms are equal.
TYPE:
|
describe
¶
The pair as a diagnostic names it.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The rules, the peak, and the two normal forms. |
Source code in src/quivers/structural/rewriting.py
FirstOrder
didactic-model
¶
FirstOrder(*, symbol: str, args: tuple = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), variable: bool = False)
Bases: ValueModel
A first-order term: a variable, or a symbol applied to terms.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
The function symbol, or the variable's name when
TYPE:
|
args
|
The arguments.
TYPE:
|
variable
|
Whether it is a variable.
TYPE:
|
PathOrder
¶
A recursive path order with a searched precedence.
| PARAMETER | DESCRIPTION |
|---|---|
status
|
TYPE:
|
budget
|
The number of comparisons the search may make.
TYPE:
|
Source code in src/quivers/structural/rewriting.py
greater
¶
greater(left: FirstOrder, right: FirstOrder, order: Precedence) -> Iterator[Precedence]
Precedences extending order under which left > right.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
The terms.
TYPE:
|
right
|
The terms.
TYPE:
|
order
|
The precedence so far.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
Precedence
|
Each extension found. |
Source code in src/quivers/structural/rewriting.py
greater_equal
¶
greater_equal(left: FirstOrder, right: FirstOrder, order: Precedence) -> Iterator[Precedence]
Precedences under which left >= right.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
The terms.
TYPE:
|
right
|
The terms.
TYPE:
|
order
|
The precedence so far.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
Precedence
|
Each extension found. |
Source code in src/quivers/structural/rewriting.py
orient
¶
orient(pairs: Sequence[tuple[FirstOrder, FirstOrder]], order: Precedence) -> Precedence | None
A precedence under which every left term exceeds its right term.
| PARAMETER | DESCRIPTION |
|---|---|
pairs
|
The rules, left then right.
TYPE:
|
order
|
The precedence so far.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Precedence or None
|
The first precedence found, or |
Source code in src/quivers/structural/rewriting.py
Greater
didactic-model
¶
Bases: Model
One pair of a precedence: high > low.
| PARAMETER | DESCRIPTION |
|---|---|
high
|
The symbols.
TYPE:
|
low
|
The symbols.
TYPE:
|
TerminationProof
didactic-model
¶
TerminationProof(*, status: str, precedence: tuple[Greater, ...] = ())
Bases: ValueModel
A recursive path order that orients every rule.
| PARAMETER | DESCRIPTION |
|---|---|
status
|
TYPE:
|
precedence
|
The pairs of the precedence, sorted.
TYPE:
|
describe
¶
The order as a sentence.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The status and the precedence. |
Source code in src/quivers/structural/rewriting.py
TerminationFailure
didactic-model
¶
Bases: ValueModel
Why no path order was found.
| PARAMETER | DESCRIPTION |
|---|---|
rules
|
The rules that could not be oriented, or that are outside what the first-order reading can order.
TYPE:
|
reason
|
The explanation.
TYPE:
|
EquationalTheory
didactic-model
¶
EquationalTheory(*, signature: Signature = field(opaque=True, extras=OPAQUE_BY_VALUE), rules: tuple = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), laws: tuple = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), assume_terminating: bool = False, strategy: Strategy = INNERMOST)
Bases: ValueModel
The equations of a signature, with how each is to be decided.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature.
TYPE:
|
rules
|
The oriented rules.
TYPE:
|
laws
|
The equations that are not oriented.
TYPE:
|
assume_terminating
|
Whether termination is a recorded assumption.
TYPE:
|
strategy
|
The normalization strategy.
TYPE:
|
TheoryReport
didactic-model
¶
TheoryReport(*, termination: TerminationProof | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE), pairs: tuple = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), claims: tuple[LawClaim, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: ValueModel
What check_theory established about a theory.
| PARAMETER | DESCRIPTION |
|---|---|
termination
|
The path order, or
TYPE:
|
pairs
|
Every critical pair.
TYPE:
|
claims
|
The termination and confluence claims with their evidence.
TYPE:
|
NonConfluent
¶
Bases: RewriteError
A theory's rules have a critical pair that does not join.
NonTerminating
¶
Bases: RewriteError
No path order orients a theory's rules and no assumption is recorded.
make_equation
¶
make_equation(name: str, left: Pattern, right: Pattern, signature: Signature, *, oriented: bool, line: int = 0, col: int = 0) -> Equation
Check an equation's sorts and scopes and record its metavariables.
Both sides must be closed patterns of one sort. Every metavariable of the right side occurs on the left with the same arity. A rule's left side is in Miller's pattern fragment.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Its name.
TYPE:
|
left
|
Its sides.
TYPE:
|
right
|
Its sides.
TYPE:
|
signature
|
The signature.
TYPE:
|
oriented
|
Whether it is a rule.
TYPE:
|
line
|
Its source position.
TYPE:
|
col
|
Its source position.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Equation
|
The checked equation. |
| RAISES | DESCRIPTION |
|---|---|
RewriteError
|
If a side is ill-sorted, a right-side metavariable is unbound, or a rule's left side is outside the pattern fragment; the message names the equation. |
Source code in src/quivers/structural/rewriting.py
critical_pairs
¶
critical_pairs(signature: Signature, rules: Sequence[Equation], max_steps: int = 10000) -> tuple[CriticalPair, ...]
Every critical pair of a rule set, with joinability.
For each pair of rules and each constructor position of the first rule's left side, the second rule's left side is unified with the subpattern there. The second rule's metavariables are raised over the variables bound above the position, so they may depend on them, and pruning keeps the unifier most general [@miller1991logic].
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature.
TYPE:
|
rules
|
The oriented rules.
TYPE:
|
max_steps
|
The step budget for normalizing each reduct.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of CriticalPair
|
One pair per overlap, outer rule first, in rule and position order. |
Source code in src/quivers/structural/rewriting.py
first_order
¶
first_order(pattern: PatternArgument, *, right: bool, rule: str) -> FirstOrder
Read a pattern as a first-order term for the path order.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern.
TYPE:
|
right
|
Whether it is a right side, where a metavariable must be applied to variables only.
TYPE:
|
rule
|
The rule, for diagnostics.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FirstOrder
|
The first-order reading. |
| RAISES | DESCRIPTION |
|---|---|
RewriteError
|
If a right-side metavariable is instantiated with a term other than a variable, which no first-order path order bounds. |
Source code in src/quivers/structural/rewriting.py
prove_termination
¶
prove_termination(rules: Sequence[Equation], budget: int = 200000) -> TerminationProof | TerminationFailure
Search for a recursive path order that orients every rule.
| PARAMETER | DESCRIPTION |
|---|---|
rules
|
The oriented rules.
TYPE:
|
budget
|
The comparison budget of each search.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TerminationProof or TerminationFailure
|
The order, or the rules that defeat the search. |
Source code in src/quivers/structural/rewriting.py
check_theory
¶
check_theory(theory: EquationalTheory, max_steps: int = 10000) -> TheoryReport
Establish termination and confluence of a theory's rules.
| PARAMETER | DESCRIPTION |
|---|---|
theory
|
The theory.
TYPE:
|
max_steps
|
The budget for normalizing each critical pair's reducts.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TheoryReport
|
The proofs, the critical pairs, and the law claims. |
| RAISES | DESCRIPTION |
|---|---|
NonTerminating
|
If no path order orients the rules and termination is not assumed; the message names the rules. |
NonConfluent
|
If a critical pair does not join; the message reports every such pair with its peak and its two normal forms. |
Source code in src/quivers/structural/rewriting.py
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 | |
solutions_of
¶
Match a term against a pattern; a convenience over match.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern.
TYPE:
|
term
|
The term.
TYPE:
|
signature
|
The signature.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Mapping or None
|
The solutions, or |