Packed forests¶
A plan whose retention is PackedForest keeps the derived
hypergraph of a run. build_forest discovers the items
from the input occurrences on an IndexedChart and
stores, for each item, one ForestEdge per grounded rule
instance concluding it and one per input occurrence of it. The result is a
DerivationForest.
Lemma (unfoldings are derivations). The tree unfoldings of an item in the forest correspond one to one to its derivations, occurrence identities included.
Proof. A derivation of \(x\) is an input occurrence of \(x\), or a rule instance concluding \(x\) together with a derivation of each premise. The forest has exactly one edge per occurrence of \(x\), since occurrences are added as their own edges and never merged even when they share a slot and a binding, and exactly one edge per instance concluding \(x\), by the lemma that each instance is produced once (Plans). Unfolding chooses an edge at each node and recurses into its premises, so the two definitions agree by induction on the derivation. QED
Without the occurrence identities two inputs of one word at one position would be one leaf, and every count and statistic above them would be halved; the checks compare the unfoldings with the derivation oracle on exactly such an input.
Linear folds¶
forest_fold computes
\(\bigoplus_d \bigotimes_{e \in d} \mathrm{value}(e)\), each derivation's product read as its factors
are, the premises' values in order and then the edge's own. By distributivity, as for two-pass
evaluation, the sum over the derivations of an item is the sum over its edges of the edge's
product with its premises' sums, so dynamic programming over the forest in order computes it in
time linear in the forest's size. A carrier such as the
ExpectationSemiring makes the fold a statistic.
forest_folds computes the folds of several goals
in one pass, sharing the values of any premises used by more than one goal.
Nonlinear folds¶
nonlinear_fold computes \(\bigoplus_d h(d, w(d))\) for
any \(h\). No aggregate summarizes it: for two derivations of weights \(-1\) and \(1\) and \(h\) the square,
it is \(2\), while the square of the aggregate is \(0\). So every derivation is built, by
fold_derivations, within a limit the caller
declares.
fold_derivations_many
lists the derivations of several goals under one limit. A repeated goal is
counted once. Derivations are counted on the common subforest and the
count includes zero-weight proofs. Each count saturates at one more than
the limit. The saturated sum and product give the same budget decision as
exact integers because the counts are nonnegative and multiplication by
zero returns zero. The preflight uses one count update per edge and
premise, with integers whose size is bounded by the declared limit.
If the count exceeds the limit, the forest raises
ForestRefusal.TOO_MANY before
building a derivation or calling a host interpretation. Otherwise it
builds each premise derivation once for use by all the requested proofs
above it. The single-goal call uses the same traversal.
Lemma (the limit bounds every list). Call a pair of an item and a height bound useful when it is the goal's, or a premise of an edge of a useful pair whose premises all have derivations within the bound. Every useful pair has at most as many derivations as the goal.
Proof. Let \((y, h)\) be a premise of a live edge of a useful pair \((x, h + 1)\). Fix a derivation within the bound for each other premise of the edge, which exists since the edge is live, and, by induction, an extension of the derivations of \(x\) to derivations of the goal. Substituting a derivation of \(y\) into that context gives a derivation of the goal, and distinct derivations of \(y\) give distinct ones. QED
The same argument applies with several roots by choosing any requested goal
that uses the pair. The number of constructed rows is thus at most the
limit times the number of useful pairs. The work also includes the rule
arities and the cost of the chosen interpretation. See
FOREST_WORK.
Heights and cycles¶
A forest with a cycle has items with infinitely many derivations. It is counted, folded and
unfolded only within a bound on height, under the recursion contract
Bounded with measure "height": an input or a rule without
premises has height one, and a rule instance one more than its highest premise. Asked without a
bound, the forest is refused with ForestRefusal.UNBOUNDED. A plan under bounded recursion
refuses the two-pass run, whose meaning counts every derivation, and folds on the forest within
Plan.height.
Expected proof embeddings¶
expected_embedding computes
\(\sum_d p(d)\, \phi(d) / \sum_d p(d)\) for a fold \(\phi\) of derivations into vectors given as a
HostInterpretation, with \(p(d)\) the derivation's
weight as a nonnegative real. A neural fold is not linear, so the derivations are enumerated
within the limit; each shared sub-derivation is folded once.
encoder_interpretation makes an
Encoder such a fold over the derivations of its
signature's presentation, one
Encoder.step per node, which equals the encoder
applied to each derivation's term.
forest
¶
Packed forests of derivations, with occurrence-sensitive provenance.
A packed forest is the derived hypergraph of a run: every item, and for each item every grounded rule instance concluding it and every input occurrence of it, as an edge to its premise items. Two occurrences of one item stay two edges, so the derivations a forest unfolds into are the derivations of the presentation, occurrence identities included, one to one.
Three kinds of observation are computed from a forest.
- A linear proof fold, the sum over derivations of the product of a value per edge, by dynamic programming over the forest in premise order; an expectation or other carrier makes it a statistic.
- A nonlinear fold, the sum over derivations of any function of the derivation and its weight, by enumerating the derivations. It needs them all, so a declared limit on their number is checked before any is built, and a forest with more is refused, never truncated.
- An expected proof embedding, the weighted mean of a fold of each derivation into vectors, by enumeration in which each shared sub-derivation is folded once.
A cyclic forest has infinitely many derivations; it is unfolded only under a declared bound on their height.
Key
¶
Key = tuple[Item, int | None]
An item and the bound on the height of its derivations, None for none.
Listed
¶
Listed = list[tuple[Derivation, Tensor, V]]
Derivations of one item, each with its weight and its fold.
ForestRefusal
¶
Bases: str, Enum
Why a forest is not unfolded.
ForestRefused
¶
ForestRefused(code: ForestRefusal, reason: str)
Bases: ValueError
A forest is not unfolded, with the reason.
| PARAMETER | DESCRIPTION |
|---|---|
code
|
The kind of reason.
TYPE:
|
reason
|
The reason.
TYPE:
|
Source code in src/quivers/stochastic/forest.py
ForestEdge
didactic-model
¶
ForestEdge(*, constructor: ConstructorId = field(opaque=True, extras=OPAQUE_BY_VALUE), binding: tuple[tuple[str, Ground], ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), premises: tuple[Item, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), occurrence: OccurrenceId | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE), values: tuple[str | int | float | bool, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
One way an item is derived: a rule instance or an input occurrence.
| PARAMETER | DESCRIPTION |
|---|---|
constructor
|
The rule, or the input's slot.
TYPE:
|
binding
|
The rule's or slot's variables.
TYPE:
|
premises
|
The premise items in premise order; none for an input.
TYPE:
|
occurrence
|
The input occurrence;
TYPE:
|
values
|
The occurrence's values; none for a rule instance.
TYPE:
|
DerivationForest
didactic-model
¶
DerivationForest(*, presentation: RulePresentation = field(opaque=True, extras=OPAQUE_BY_VALUE), items: tuple[Item, ...] = field(opaque=True, extras=OPAQUE_BY_IDENTITY), edges: dict[Item, tuple[ForestEdge, ...]] = field(opaque=True, extras=OPAQUE_BY_IDENTITY), order: tuple[Item, ...] | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY), work: ChartWork = field(default=ChartWork(), opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: ValueModel
The derived hypergraph of a run.
| PARAMETER | DESCRIPTION |
|---|---|
presentation
|
The presentation.
TYPE:
|
items
|
Every item, in the order discovered.
TYPE:
|
edges
|
The edges concluding each item: its input occurrences, then its rule instances, in the order found.
TYPE:
|
order
|
The items in an order in which every premise precedes its
conclusions, or
TYPE:
|
work
|
The counters of the chart that found the edges.
TYPE:
|
acyclic
property
¶
Whether the hypergraph has no cycle.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True when every item has finitely many derivations. |
size
property
¶
The number of edges and premise references, the forest's size.
| RETURNS | DESCRIPTION |
|---|---|
int
|
|
report
¶
report() -> WorkReport
The work of building the forest.
| RETURNS | DESCRIPTION |
|---|---|
WorkReport
|
The chart's lookups and candidates, and the forest's size as its provenance, each measured. |
Source code in src/quivers/stochastic/forest.py
count
¶
count(goal: Item, *, height: int | None = None) -> int
The number of derivations of an item, exactly.
| PARAMETER | DESCRIPTION |
|---|---|
goal
|
The item.
TYPE:
|
height
|
The greatest height of a derivation counted; required when the forest has a cycle. An input has height one.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The count, zero for an item not in the forest. |
| RAISES | DESCRIPTION |
|---|---|
ForestRefused
|
If the forest has a cycle and no height is given. |
Source code in src/quivers/stochastic/forest.py
below
¶
below(goal: Item) -> DerivationForest
The part of the forest that derivations of one item use.
| PARAMETER | DESCRIPTION |
|---|---|
goal
|
The item.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DerivationForest
|
The items reachable from |
Source code in src/quivers/stochastic/forest.py
below_all
¶
below_all(goals: Sequence[Item]) -> DerivationForest
The part of the forest used by derivations of any of the goals.
| PARAMETER | DESCRIPTION |
|---|---|
goals
|
The requested items.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DerivationForest
|
The items reachable from the goals through premises, with their edges, in the same orders. |
Source code in src/quivers/stochastic/forest.py
build_forest
¶
build_forest(presentation: RulePresentation, axioms: Sequence[AxiomOccurrence]) -> DerivationForest
The packed forest of a presentation over input occurrences.
Items are discovered from the inputs, and each instance is found once, by the last of its premises to be discovered, and stored as an edge of its conclusion. Rules without premises are edges of their ground conclusions. The forest is then ordered so premises precede conclusions, when it has no cycle.
| PARAMETER | DESCRIPTION |
|---|---|
presentation
|
The presentation; no premise may have hypotheses.
TYPE:
|
axioms
|
The input occurrences, each kept as its own edge.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DerivationForest
|
The forest. |
| RAISES | DESCRIPTION |
|---|---|
ForestRefused
|
If an occurrence names no slot of the presentation. |
Source code in src/quivers/stochastic/forest.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | |
edge_weight
¶
edge_weight(forest: DerivationForest, edge: ForestEdge, algebra: OrderPreservingSemiring, inputs: Mapping[InputId, Tensor], *, like: Tensor) -> Tensor
The weight of an edge, as a plan weighs it.
An input occurrence takes its own weight, else its slot's parameter; a rule instance takes its rule's parameter; a missing weight is the unit.
| PARAMETER | DESCRIPTION |
|---|---|
forest
|
The forest.
TYPE:
|
edge
|
The edge.
TYPE:
|
algebra
|
The algebra.
TYPE:
|
inputs
|
The weights.
TYPE:
|
like
|
The dtype and device of the unit.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The weight. |
| RAISES | DESCRIPTION |
|---|---|
ForestRefused
|
If the rule's weight needs an executable attachment. |
Source code in src/quivers/stochastic/forest.py
forest_fold
¶
forest_fold(forest: DerivationForest, goal: Item, algebra: OrderPreservingSemiring, value: Callable[[ForestEdge], Tensor], *, like: Tensor, height: int | None = None) -> Tensor
A linear proof fold: the sum over derivations of the product of edge values.
Each derivation's product is read as its factors are: the premises' values in order, then the edge's own. Dynamic programming over the forest computes it in time linear in the forest's size, times the height bound when one is given.
| PARAMETER | DESCRIPTION |
|---|---|
forest
|
The forest.
TYPE:
|
goal
|
The item.
TYPE:
|
algebra
|
The algebra of the values.
TYPE:
|
value
|
The value of each edge.
TYPE:
|
like
|
The dtype and device of the zero.
TYPE:
|
height
|
The bound on height, required for a cyclic forest.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The fold, zero when the goal has no derivation. |
| RAISES | DESCRIPTION |
|---|---|
ForestRefused
|
If the forest has a cycle and no height is given. |
Source code in src/quivers/stochastic/forest.py
forest_folds
¶
forest_folds(forest: DerivationForest, goals: Sequence[Item], algebra: OrderPreservingSemiring, value: Callable[[ForestEdge], Tensor], *, like: Tensor, height: int | None = None) -> dict[Item, Tensor]
Evaluate linear proof folds sharing one table for all requested items.
For each incoming edge, the premise occurrences independently choose derivations of their items. Distributivity factors this sum of products into the product of the already-computed premise sums and the edge's value. Repeated occurrences of an item each contribute a factor.
| PARAMETER | DESCRIPTION |
|---|---|
forest
|
The forest.
TYPE:
|
goals
|
The requested items.
TYPE:
|
algebra
|
The algebra of the values.
TYPE:
|
value
|
The value of each edge.
TYPE:
|
like
|
The dtype and device of the zero.
TYPE:
|
height
|
The bound on height, required for a cyclic forest.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict of Item to torch.Tensor
|
The requested folds, with zero for any goal with no derivation. |
| RAISES | DESCRIPTION |
|---|---|
ForestRefused
|
If the forest has a cycle and no height is given. |
Source code in src/quivers/stochastic/forest.py
fold_derivations
¶
fold_derivations(forest: DerivationForest, goal: Item, algebra: OrderPreservingSemiring, inputs: Mapping[InputId, Tensor], interpretation: HostInterpretation[V], *, limit: int, like: Tensor, height: int | None = None) -> Listed[V]
Every derivation of an item, with its weight and its fold.
Derivations are built bottom up and shared: a derivation of a premise is built and folded once and reused by every derivation above it. The exact budget decision happens before any derivation is built.
| PARAMETER | DESCRIPTION |
|---|---|
forest
|
The forest.
TYPE:
|
goal
|
The item.
TYPE:
|
algebra
|
The algebra of the weights.
TYPE:
|
inputs
|
The weights.
TYPE:
|
interpretation
|
The fold of a derivation; its operations receive each node and its premises' folds.
TYPE:
|
limit
|
The most derivations of the goal to build, declared by the caller.
TYPE:
|
like
|
The dtype and device of the unit.
TYPE:
|
height
|
The bound on height, required for a cyclic forest.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list of tuple of Derivation, torch.Tensor and V
|
Every derivation of the goal, its weight and its fold. |
| RAISES | DESCRIPTION |
|---|---|
ForestRefused
|
If the goal has more derivations than |
Source code in src/quivers/stochastic/forest.py
fold_derivations_many
¶
fold_derivations_many(forest: DerivationForest, goals: Sequence[Item], algebra: OrderPreservingSemiring, inputs: Mapping[InputId, Tensor], interpretation: HostInterpretation[V], *, limit: int, like: Tensor, height: int | None = None, node_limit: int | None = None) -> dict[Item, Listed[V]]
List several goals' derivations under one exact global proof budget.
Count structural proofs over the common subforest in one pass,
saturating integers at one above the larger of the active limits.
Every useful subtree extends to
a requested proof, so each list in a successful run has at most
limit entries. Build and fold each sub-derivation once across all
goals. This sharing applies to host interpretations whose operations
may be reused. The count includes zero-weight structural proofs.
| PARAMETER | DESCRIPTION |
|---|---|
forest
|
The common forest of the requested goals.
TYPE:
|
goals
|
The roots to list. A repeated goal is counted and returned once, at its first position.
TYPE:
|
algebra
|
The algebra of each derivation's weight.
TYPE:
|
inputs
|
The rule and occurrence weights.
TYPE:
|
interpretation
|
The fold performed on each listed derivation.
TYPE:
|
limit
|
The maximum total number of proofs of the distinct goals.
TYPE:
|
like
|
The dtype and device of the unit.
TYPE:
|
height
|
The bound on height, required for a cyclic forest.
TYPE:
|
node_limit
|
An optional global limit on the sum of the unfolded proof sizes of those goals. Repeated subtree occurrences each count, even when the listing stores their derivations once.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict of Item to Listed
|
Every requested goal's derivations, with weights and folds. |
| RAISES | DESCRIPTION |
|---|---|
ForestRefused
|
If there are more proofs or unfolded nodes than the limits allow, or the forest has an unbounded cycle. |
ValueError
|
If a proof budget is negative. |
Source code in src/quivers/stochastic/forest.py
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 | |
nonlinear_fold
¶
nonlinear_fold(forest: DerivationForest, goal: Item, algebra: OrderPreservingSemiring, inputs: Mapping[InputId, Tensor], observe: Callable[[Derivation, Tensor], Tensor], *, limit: int, like: Tensor, height: int | None = None) -> Tensor
The sum over derivations of any function of a derivation and its weight.
No aggregate summarizes such a fold: with two derivations of weights
-1 and 1, squaring gives 2 here and 0 on their sum. So
every derivation is built, which the declared limit bounds.
| PARAMETER | DESCRIPTION |
|---|---|
forest
|
The forest.
TYPE:
|
goal
|
The item.
TYPE:
|
algebra
|
The algebra of the weights and of the sum.
TYPE:
|
inputs
|
The weights.
TYPE:
|
observe
|
The function of a derivation and its weight.
TYPE:
|
limit
|
The most derivations to build.
TYPE:
|
like
|
The dtype and device of the zero.
TYPE:
|
height
|
The bound on height, required for a cyclic forest.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The sum, zero when the goal has no derivation. |
| RAISES | DESCRIPTION |
|---|---|
ForestRefused
|
If the goal has more derivations than |
Source code in src/quivers/stochastic/forest.py
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 | |
expected_embedding
¶
expected_embedding(forest: DerivationForest, goal: Item, algebra: OrderPreservingSemiring, inputs: Mapping[InputId, Tensor], interpretation: HostInterpretation[Tensor], *, probability: Callable[[Tensor], Tensor], limit: int, like: Tensor, height: int | None = None) -> Tensor
The mean of a fold of the derivations into vectors, weighted by their weights.
sum_d p(d) phi(d) / sum_d p(d) with p(d) the derivation's
weight read as a nonnegative real by probability (exp for log
weights) and phi the interpretation's fold. A fold that is not
linear, such as a neural encoder, has no aggregate over the forest, so
the derivations are enumerated within the declared limit; each shared
sub-derivation is folded once.
| PARAMETER | DESCRIPTION |
|---|---|
forest
|
The forest.
TYPE:
|
goal
|
The item.
TYPE:
|
algebra
|
The algebra of the weights.
TYPE:
|
inputs
|
The weights.
TYPE:
|
interpretation
|
The embedding of a derivation.
TYPE:
|
probability
|
A derivation's weight as a nonnegative real. |
limit
|
The most derivations to build.
TYPE:
|
like
|
The dtype and device of the unit.
TYPE:
|
height
|
The bound on height, required for a cyclic forest.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The expected embedding. |
| RAISES | DESCRIPTION |
|---|---|
ForestRefused
|
If the goal has no derivation or more than |
Source code in src/quivers/stochastic/forest.py
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 | |
encoder_interpretation
¶
encoder_interpretation(encoder: Encoder, presentation: RulePresentation) -> HostInterpretation[Tensor]
The fold of an encoder over the derivations of its signature's presentation.
Each rule of presentation_of the encoder's signature is a
constructor; its operation is the encoder's Encoder.step on the
premises' embeddings and the node's values, interleaved by the kinds
of the constructor's argument sorts. On every derivation it equals
the encoder applied to the derivation's term.
| PARAMETER | DESCRIPTION |
|---|---|
encoder
|
The encoder.
TYPE:
|
presentation
|
The presentation of the encoder's signature.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
HostInterpretation of torch.Tensor
|
The fold. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a rule or slot of the presentation is not a constructor of the signature. |