Indexed charts¶
An IndexedChart holds the items of a
RulePresentation and finds, when an item arrives, the
rule instances it completes. A rule whose patterns are relations of their own variables
(generic_joinable: no pattern is a sum of two
variables) is joined by Generic Join from each trigger; any other rule, or every rule
of a chart made with join="keyed", looks its premises up one at a time.
Generic Join from a trigger¶
For a rule and a trigger, the premise an arriving item fills, or the conclusion for the backward
pass, join_order puts the trigger's variables first and
the others in the order they first occur. Each other premise has a trie of the items of its form
that match its pattern, keyed by its variables in that order and kept up to date as items arrive.
Matching the trigger binds its variables; each other premise's trie is entered along the bound
prefix of its variables; the remaining variables are bound one at a time, each to the values in
the intersection of the tries that contain it, iterating the smallest and probing the others. A
complete binding determines every premise's item, since the item's arguments are its pattern
evaluated at the binding.
Lemma (each instance once). Admit items one at a time, and let each admitted item \(x\) trigger every premise position \(t\) whose form it has, enumerating the instances in which \(x\) fills \(t\), every premise is admitted, and no premise before \(t\) is \(x\). Then each instance is enumerated exactly once.
Proof. An instance is a rule with premise items \(p_1, \dots, p_n\); its binding determines the items, and the items determine the binding, since every variable of the rule occurs in a premise. Generic Join from \(t\) with \(x\) fixed at \(t\) reaches each binding of the other variables consistent with the admitted items once, by the values of its variables in order, so it yields each choice of other premise items once. Let \(x\) be the last of \(p_1, \dots, p_n\) to be admitted and \(t\) the least position with \(p_t = x\). Before \(x\) is admitted some premise is not, so the instance is not enumerated. When \(x\) is admitted, the join from \(t\) yields it, and the leaf test passes since no premise before \(t\) is \(x\); the join from a later position \(u\) with \(p_u = x\) yields it and the test rejects it, since \(p_t = x\) comes before \(u\). Items admitted later do not complete it. QED
Theorem (cost). Over a run, the lookups and candidates of one rule with \(m\) premises and \(n\) variables are \(O(m N + m^2 n\, \mathrm{AGM})\), where \(\mathrm{AGM}\) is the bound of a fractional edge cover of the body over the final relations and \(N\) the most items of a premise's form.
Proof. Fix a trigger position \(t\) and an arriving item \(x\). Its join binds the variables of \(t\) to \(x\)'s values and then runs the levels of Generic Join over the other premises, each relation a subset of its final one. Every set it intersects at a level is a subset of the set that Generic Join over the whole body, in the same order and on the final relations, intersects below the same prefix; and the prefixes it reaches are among those. So its work is at most that of the whole join below the prefix of \(x\)'s values, plus \(O(m)\) to enter the tries. Distinct items at \(t\) have distinct prefixes, so summing over them gives at most the whole join's work, \(O(m n\, \mathrm{AGM})\) (Joins), plus \(O(m N)\). Summing over the \(m\) positions gives the bound. QED
The plan records, in PlanRecord.joins, each rule's
algorithm, the weight \(\rho^*\) of a least fractional edge cover of its body, and this bound with
\(\mathrm{AGM} \le N^{\rho^*}\) (rule_joins). On the
triangle with a hub value and the skewed triangle, the chart stays within the bound, and a chart
that looks premises up one at a time does \(\Theta(N^2)\), which the same bound rejects; on chains,
diamonds and skip graphs, whose step rule has \(\rho^* = 1\), the work is linear in the edges.
Keyed lookups¶
join_plans fixes, for every rule and trigger, the order
in which the other premises are looked up and the positions each lookup is keyed on: next the
premise with the most arguments whose variables are already bound. It handles any pattern that
matching binds in premise order, including a sum whose other summand is already bound, and its
instances are enumerated once by the same lemma, with the test applied as each premise is chosen.
ChartWork counts the lookups, candidates, instances, side
conditions and index entries of either method.
indexed
¶
An indexed chart of a rule presentation, with bindings pushed into lookups.
Items of each judgment form are indexed on the argument positions a premise finds bound when it is matched: when a rule fires on one item, the variables that item binds turn the other premises' patterns into lookup keys, and only the items with those arguments are visited. The indexes are built on first use and kept up to date as items arrive, so a lookup costs its key and its answers, not a scan of the family.
A join plan fixes, for each rule and each premise that triggers it, the order in which the other premises are looked up and the positions each lookup is keyed on: next comes the premise with the most bound positions, ties in premise order.
TrieNode
¶
A level of a premise's trie; its leaves are items. A premise without
variables keeps its item under the key ().
JoinStep
didactic-model
¶
Bases: ValueModel
One lookup of a join plan.
| PARAMETER | DESCRIPTION |
|---|---|
premise
|
The premise looked up.
TYPE:
|
keyed
|
The argument positions whose values are known before the lookup and form its key.
TYPE:
|
RuleJoinPlan
didactic-model
¶
RuleJoinPlan(*, rule: ConstructorId = field(opaque=True, extras=OPAQUE_BY_VALUE), trigger: int, steps: tuple[JoinStep, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE))
Bases: ValueModel
The order of lookups when one premise of a rule triggers it.
| PARAMETER | DESCRIPTION |
|---|---|
rule
|
The rule.
TYPE:
|
trigger
|
The premise matched against the new item, or
TYPE:
|
steps
|
The other premises, in lookup order.
TYPE:
|
ChartWork
didactic-model
¶
ChartWork(*, lookups: int = 0, candidates: int = 0, instances: int = 0, conditions: int = 0, index_entries: int = 0)
Bases: ValueModel
Counters of the work of a chart.
| PARAMETER | DESCRIPTION |
|---|---|
lookups
|
Index lookups.
TYPE:
|
candidates
|
Items returned by lookups and matched against a pattern.
TYPE:
|
instances
|
Rule instances enumerated.
TYPE:
|
conditions
|
Side conditions evaluated.
TYPE:
|
index_entries
|
Entries added to indexes.
TYPE:
|
report
¶
report(phase: Phase) -> WorkReport
The counters as a work report.
| PARAMETER | DESCRIPTION |
|---|---|
phase
|
The phase they counted.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
WorkReport
|
Lookups as hash operations and candidates as key comparisons, the two measured. |
Source code in src/quivers/stochastic/indexed.py
IndexedChart
¶
IndexedChart(presentation: RulePresentation, *, join: Literal['generic', 'keyed'] = 'generic')
Items of a presentation indexed on the argument positions lookups use.
| PARAMETER | DESCRIPTION |
|---|---|
presentation
|
The presentation.
TYPE:
|
join
|
How a rule's other premises are found from a trigger.
TYPE:
|
Source code in src/quivers/stochastic/indexed.py
add
¶
add(item: Item) -> None
Index an item under every index of its family.
| PARAMETER | DESCRIPTION |
|---|---|
item
|
The item, added once.
TYPE:
|
Source code in src/quivers/stochastic/indexed.py
lookup
¶
The items of a family with given values at given positions.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The judgment form.
TYPE:
|
positions
|
The argument positions.
TYPE:
|
key
|
Their values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list of Item
|
The items, in the order they were added. |
Source code in src/quivers/stochastic/indexed.py
holds
¶
Evaluate a rule's side condition, counting the evaluation.
| PARAMETER | DESCRIPTION |
|---|---|
rule
|
The rule.
TYPE:
|
binding
|
Its variables.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether the condition holds; true without one. |
| RAISES | DESCRIPTION |
|---|---|
MatchError
|
If the condition reads a variable the binding lacks. |
Source code in src/quivers/stochastic/indexed.py
instances
¶
instances(rule: Rule, trigger: int, item: Item, admitted: Callable[[Item], bool]) -> Iterator[tuple[Binding, tuple[Item, ...]]]
The instances of a rule that item completes.
item fills premise trigger and no earlier premise; every
other premise is an admitted item. So when items are admitted one
at a time and each new item triggers every position it fits, each
instance is produced once, by the last of its premises to be
admitted, at the first position that item fills.
| PARAMETER | DESCRIPTION |
|---|---|
rule
|
The rule.
TYPE:
|
trigger
|
The premise
TYPE:
|
item
|
The newly admitted item.
TYPE:
|
admitted
|
Whether an item is admitted;
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
tuple of Binding and tuple of Item
|
The binding and the premise items, in premise order. |
Source code in src/quivers/stochastic/indexed.py
incoming
¶
incoming(rule: Rule, item: Item, admitted: Callable[[Item], bool]) -> Iterator[tuple[Binding, tuple[Item, ...]]]
The instances of a rule that conclude item from admitted premises.
| PARAMETER | DESCRIPTION |
|---|---|
rule
|
The rule.
TYPE:
|
item
|
The conclusion.
TYPE:
|
admitted
|
Whether a premise item is admitted.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
tuple of Binding and tuple of Item
|
The binding and the premise items. |
Source code in src/quivers/stochastic/indexed.py
method
¶
method(rule: Rule) -> Literal['generic', 'keyed']
How this chart joins a rule's premises.
| PARAMETER | DESCRIPTION |
|---|---|
rule
|
The rule.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
{'generic', 'keyed'}
|
|
Source code in src/quivers/stochastic/indexed.py
index_variables
¶
index_variables(term: IndexTerm) -> set[str]
The variable names of an index term.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
set of str
|
Its variables. |
Source code in src/quivers/stochastic/indexed.py
flat_pattern
¶
flat_pattern(term: IndexTerm) -> bool
Whether matching a term alone binds all its variables.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
False when a sum, here or nested, has two variables. |
Source code in src/quivers/stochastic/indexed.py
generic_joinable
¶
generic_joinable(rule: Rule) -> bool
Whether every premise, and the conclusion, is a relation of its own variables.
| PARAMETER | DESCRIPTION |
|---|---|
rule
|
The rule.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True when no pattern of the rule has a sum of two variables and no premise has hypotheses. |
Source code in src/quivers/stochastic/indexed.py
join_order
¶
join_order(rule: Rule, trigger: int) -> tuple[str, ...]
The variable order of a rule's join from one trigger.
The variables the trigger binds come first, in the order they occur
in its pattern (the conclusion's for trigger -1), then the others
in the order they first occur in the premises.
| PARAMETER | DESCRIPTION |
|---|---|
rule
|
The rule.
TYPE:
|
trigger
|
The triggering premise, or
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of str
|
The order. |
Source code in src/quivers/stochastic/indexed.py
index_variables_of
¶
index_variables_of(arguments: Sequence[IndexTerm]) -> set[str]
The variables of a pattern.
| PARAMETER | DESCRIPTION |
|---|---|
arguments
|
The pattern's arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
set of str
|
Its variables. |
Source code in src/quivers/stochastic/indexed.py
join_plans
¶
join_plans(presentation: RulePresentation) -> dict[tuple[ConstructorId, int], RuleJoinPlan]
The join plan of every rule for every trigger, and from its conclusion.
| PARAMETER | DESCRIPTION |
|---|---|
presentation
|
The presentation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict of tuple of ConstructorId and int to RuleJoinPlan
|
Plans by rule and trigger; trigger |
Source code in src/quivers/stochastic/indexed.py
side_condition_holds
¶
side_condition_holds(presentation: RulePresentation, rule: Rule, binding: Binding) -> bool
Evaluate a rule's side condition with the QIEC evaluator.
| PARAMETER | DESCRIPTION |
|---|---|
presentation
|
The presentation, which holds the condition's computation.
TYPE:
|
rule
|
The rule.
TYPE:
|
binding
|
Its variables.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether the condition holds; true without one. |
| RAISES | DESCRIPTION |
|---|---|
MatchError
|
If the condition reads a variable the binding lacks. |