Foreign handler semantics¶
A foreign handler's clauses run in a runtime provider. Its declaration states what they compute in
its foreign field, and the core provider builds the handler
from that statement and from nothing else, in particular never from the handler's name.
SemiringSum: a search, resuming once per alternative and answering the semiring sum of its completed shots' weights with their number. The number is the counting semiring's sum of ones, a structural count that no weight changes.SemiringProduct: a collector, multiplying the weights a scope adds, in order.Enumeration: an enumeration of a finite draw.Settlement: the stop of an iteration, answered with its value only when the stop certifies it.DeductionLinearGoalcomputes the full-spanLogWeightof a checked lexical recurrence by span evaluation with certified unary closure. The program bindingparse_linearselects this operation.DeductionOutcomesfolds a finite packed lexical chart inLogProband groups full-span goal masses by a structural key.DeductionProofsuses the same chart and returns the supported full-span proofs after an exact budget check. Each row binds its proof's indices and carries itsLogProbweight.DeductionOutcomeGroupinggroups weighted rows whose checked keys admit exact structural equality. Ordinary QIEC calls compute the keys before this handler runs.DeductionOutcomeLawturns nominal weighted rows into a normalizedSampleableover their key type. The checkedoutcome_lawbinding uses this adapter.
A KernelSemiring names a member of the
algebra hierarchy by the subject of its law claims
and gives the kernel type of its elements. A search requires its semiring to establish
SUM_LAWS and a collector
PRODUCT_LAWS, and the provider raises
MissingLaw for a member that does not. The operations convert
kernel values to float64 tensors, apply the member's own join and tensor_op, and convert
back. The conversion is exact or refused: a count is exact below
EXACT_COUNT, the bound at which the counting algebra also
refuses its closure.
Finite log expectations¶
Enumeration(reduction="log_expectation") serves an ungrouped
Random handler with signature LogWeight -> LogWeight. Each
request resumes its continuation once for each positive-weight value
of the unbatched finite draw. If the draw has mass \(p(z)\) and the
continuation returns \(\ell(z)\) for the value \(z\), its answer is
A later request inside the continuation returns the same answer type. Induction on the finite tree of draws therefore gives the log of the sum over complete assignments, with each conditional draw mass appearing once along its branch. Work scales with that tree. An execution budget can cap it through the usual reference-machine fuel. The tensor backend preserves score and parameter derivatives on fixed positive support. A subtree whose scores all have zero weight returns \(-\infty\) through the masked LogProb sum.
Settled stops¶
A deduction whose derivations may repeat an item iterates. A stop is certified when the iteration
found no further derivation, so the value sums every derivation, or when the arithmetic is exact and
two iterates agree, since a fixed point reached from below is the least one. Any other stop is by a
control: the declared tolerance, or two iterates equal in rounded arithmetic while derivations
keep growing. Its report names a
ControlReached stop, guarantees nothing, and so fails the
guarantee of the contract. The settlement refuses it
with ContractNotMet, or, when the caller asks for
RETURN_UNESTABLISHED, records the report and
run_deduction returns the value as
Unestablished. A program that calls a deduction can hold
no unestablished value, so there the stop is refused.
foreign
¶
What a foreign handler computes, stated as data on its declaration.
A foreign handler's clauses run in a runtime provider, so its
declaration says what they compute, and the provider builds them from
that statement and from nothing else; in particular never from the
handler's name. A search that sums its shots and a collector that
multiplies the weights of one derivation carry the semiring they work
in as a KernelSemiring: a member of the algebra hierarchy of
quivers.core.algebras, named by the subject of its law claims, with
the kernel type that holds its elements. The provider asks the member
for the laws the handler relies on and refuses a member that does not
establish them.
The elements of a kernel semiring are numbers of the machine, so the
operations apply the member's own operations to them: a real or log weight
in its own dtype, keeping its gradient, and a truth value or a count in
float64 and back. The conversion is exact or refused: a natural number
is exact in float64 below 2 / eps, the bound beyond which the
counting algebra refuses its own closure, and a truth value is 0 or
1.
KERNEL_ALGEBRAS
module-attribute
¶
KERNEL_ALGEBRAS: Mapping[str, Semiring] = MappingProxyType({member.subject.name: member for member in (LOG_PROB, MAX_PLUS, TROPICAL, BOOLEAN, GODEL, COUNTING, REAL, MARKOV)})
The shipped semirings with a scalar carrier, by the subject of their claims.
KERNEL_CARRIERS
module-attribute
¶
KERNEL_CARRIERS: tuple[TypeExpr, ...] = (LOG_WEIGHT, REAL_TYPE, BOOL, INT)
The kernel types that hold the element of a kernel semiring.
EXACT_COUNT
module-attribute
¶
The bound below which every natural number is exact in float64.
SUM_LAWS
module-attribute
¶
SUM_LAWS: tuple[Law, ...] = (PLUS_ASSOCIATIVE, PLUS_COMMUTATIVE, PLUS_UNIT)
What a search relies on: the sum over a set of derivations is defined by commutativity and associativity, whatever the enumeration order and the nesting of choices, and an empty choice contributes the unit of the sum.
PRODUCT_LAWS
module-attribute
¶
PRODUCT_LAWS: tuple[Law, ...] = (TIMES_ASSOCIATIVE, TIMES_UNIT)
What a collector relies on: the product of a derivation's weights is taken in premise order from the unit, so it needs associativity and the unit and not commutativity.
KernelValue
¶
A literal element of a kernel scalar type, as elaboration writes it.
EnumerationReduction
¶
How an enumeration aggregates its shots.
ForeignSemantics
¶
ForeignSemantics = SemiringSum | SemiringProduct | Enumeration | Settlement | DeductionLinearGoal | DeductionOutcomes | DeductionProofs | DeductionBooleanOutcomes | DeductionOutcomeGrouping | DeductionOutcomeLaw
What a foreign handler computes.
MissingLaw
¶
Bases: ValueError
A handler relies on a law its semiring does not establish.
InexactKernelValue
¶
Bases: ValueError
A kernel value has no exact image in the semiring's tensors.
KernelSemiring
didactic-model
¶
KernelSemiring(*, algebra: ClaimSubject, carrier: TypeExpr = field(opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
A semiring of the algebra hierarchy acting on one kernel type.
| PARAMETER | DESCRIPTION |
|---|---|
algebra
|
The semiring, named by the subject its law claims are about; a
key of
TYPE:
|
carrier
|
The kernel type of its elements, one of
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValidationError
|
If the subject names no shipped semiring with a scalar carrier, the carrier is not a kernel scalar type, or the semiring's zero or unit has no exact element of the carrier. |
member
property
¶
member: Semiring
The semiring.
| RETURNS | DESCRIPTION |
|---|---|
Semiring
|
The shipped member the record names. |
exact
property
¶
Whether the carrier's arithmetic is exact.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True for truth values and natural numbers, whose operations
are exact within |
log_weights
property
¶
Whether the elements are log weights, so a learned log weight is one.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True when the carrier is |
of
classmethod
¶
of(member: Semiring, carrier: TypeExpr) -> KernelSemiring
The kernel semiring of a shipped member on a carrier.
| PARAMETER | DESCRIPTION |
|---|---|
member
|
A value of
TYPE:
|
carrier
|
The kernel type of its elements.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
KernelSemiring
|
The record. |
Source code in src/quivers/qiec/foreign.py
require
¶
require(laws: Sequence[Law], what: str) -> None
Require the semiring to establish the laws a handler relies on.
| PARAMETER | DESCRIPTION |
|---|---|
laws
|
The laws.
TYPE:
|
what
|
The handler, for the diagnostic.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
MissingLaw
|
If a law is not claimed with a proved or decided status. |
Source code in src/quivers/qiec/foreign.py
tensor
¶
An element as the member's operations take it.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
An element of the carrier, or a tensor of them, as the machine holds it.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The same numbers: a real or log weight in its own dtype, so
its gradient is kept; a truth value or a count in |
| RAISES | DESCRIPTION |
|---|---|
InexactKernelValue
|
If the value is not a tensor of the carrier's kind, a count
reaches |
Source code in src/quivers/qiec/foreign.py
value
¶
value(found: Tensor, numbers: NumberDomain) -> Tensor
The element of the carrier a tensor of the member holds.
| PARAMETER | DESCRIPTION |
|---|---|
found
|
Elements of the semiring.
TYPE:
|
numbers
|
The run's numbers.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The elements in the carrier's dtype: |
| RAISES | DESCRIPTION |
|---|---|
InexactKernelValue
|
If an entry has no exact element of the carrier: a truth value
other than |
Source code in src/quivers/qiec/foreign.py
zero
¶
zero() -> KernelValue
The additive identity, as a literal of the carrier.
| RETURNS | DESCRIPTION |
|---|---|
KernelValue
|
The semiring's zero. |
unit
¶
unit() -> KernelValue
The multiplicative identity, as a literal of the carrier.
| RETURNS | DESCRIPTION |
|---|---|
KernelValue
|
The semiring's unit. |
plus
¶
plus(values: Sequence[Tensor], numbers: NumberDomain) -> Tensor
The sum of elements in their order.
| PARAMETER | DESCRIPTION |
|---|---|
values
|
Elements of one shape.
TYPE:
|
numbers
|
The run's numbers.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Their sum, by the member's |
Source code in src/quivers/qiec/foreign.py
times
¶
times(left: Tensor, right: Tensor, numbers: NumberDomain) -> Tensor
The product of two elements, entrywise over tensors of them.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
The left factor; a scalar broadcasts over a tensor.
TYPE:
|
right
|
The right factor.
TYPE:
|
numbers
|
The run's numbers.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
|
Source code in src/quivers/qiec/foreign.py
SemiringSum
didactic-model
¶
SemiringSum(*, semiring: KernelSemiring = field(opaque=True, extras=OPAQUE_BY_VALUE), tag: Literal['semiring-sum'] = 'semiring-sum')
Bases: QiecModel
A search: resume once per alternative and sum what the shots answer.
The handled computation answers its value with the product a collector inside it accumulated; a completed shot answers that product with the count one, an empty choice answers the zero with the count zero, and a choice answers the sum of its shots' weights with the sum of their counts. The answer is thus the semiring sum over the completed derivations and their number, the second being the counting semiring's sum of ones: a structural count that no weight can change.
| PARAMETER | DESCRIPTION |
|---|---|
semiring
|
The semiring of the sum.
TYPE:
|
tag
|
Always
TYPE:
|
SemiringProduct
didactic-model
¶
SemiringProduct(*, semiring: KernelSemiring = field(opaque=True, extras=OPAQUE_BY_VALUE), forkable: bool = False, tag: Literal['semiring-product'] = 'semiring-product')
Bases: QiecModel
A collector: multiply every weight added in the scope, in order.
| PARAMETER | DESCRIPTION |
|---|---|
semiring
|
The semiring of the product, applied entrywise to tensors of its elements.
TYPE:
|
forkable
|
Whether an unrestricted resumption may capture an installation, each shot continuing from its own copy of the product so far; a search inside the scope needs it.
TYPE:
|
tag
|
Always
TYPE:
|
Enumeration
didactic-model
¶
Enumeration(*, grouped: bool = False, reduction: EnumerationReduction = 'logsumexp', tag: Literal['enumeration'] = 'enumeration')
Bases: QiecModel
An enumeration of a finite draw: resume once per support value.
| PARAMETER | DESCRIPTION |
|---|---|
grouped
|
Whether the handler answers one weight per group position.
TYPE:
|
reduction
|
TYPE:
|
tag
|
Always
TYPE:
|
Settlement
didactic-model
¶
Settlement(*, semiring: KernelSemiring = field(opaque=True, extras=OPAQUE_BY_VALUE), tag: Literal['settlement'] = 'settlement')
Bases: QiecModel
A stop of an iteration: answer its value only when the stop certifies it.
The handled operation carries the value, whether the stop certifies it, and the residual of the last two iterates. A certified stop is answered with the value; any other is a stop by a control, which the provider refuses or reports as unestablished, as the caller's failure policy says.
| PARAMETER | DESCRIPTION |
|---|---|
semiring
|
The semiring of the value, which fixes its metric.
TYPE:
|
tag
|
Always
TYPE:
|
DeductionLinearGoal
didactic-model
¶
DeductionLinearGoal(*, presentation: QiecModel = field(opaque=True, extras=OPAQUE_BY_VALUE), lexicon: tuple[tuple[str, str], ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), parameters: tuple[ParameterId, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), start: str | None, tag: Literal['deduction-linear-goal'] = 'deduction-linear-goal')
Bases: QiecModel
Solve the full-span weight of one checked lexical linear system.
| PARAMETER | DESCRIPTION |
|---|---|
presentation
|
The nominal span rules. The native adapter admits unary cycles and binary rules whose two spans concatenate. Lexical spans have width one and the presentation has no other axiom widths.
TYPE:
|
lexicon
|
The ordered pairs of token and atomic category. Their live log weights occupy the first slots of the operation's weight vector.
TYPE:
|
parameters
|
The distinct nominal rule-weight slots, in their vector order.
TYPE:
|
start
|
The requested final category, or None to sum all full-span items.
TYPE:
|
DeductionOutcomes
didactic-model
¶
DeductionOutcomes(*, presentation: QiecModel = field(opaque=True, extras=OPAQUE_BY_VALUE), words: tuple[str, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), parameters: tuple[ParameterId, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), span: str, start: str | None = None, item_type: TypeApplication = field(opaque=True, extras=OPAQUE_BY_VALUE), item_constructors: tuple[tuple[str, ConstructorId], ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), empty: ConstructorId = field(opaque=True, extras=OPAQUE_BY_VALUE), entry: ConstructorId = field(opaque=True, extras=OPAQUE_BY_VALUE), tag: Literal['deduction-outcomes'] = 'deduction-outcomes')
Bases: QiecModel
A finite lexical LogProb chart returning typed weighted outcomes.
The operation receives a token tensor, the corresponding lexical entries' category and optional form values in lexicon order, the log weights of those entries and the listed rule parameters, and the structural key and normalization choices. Lexical occurrences have positive width one. Ground rules may supply fixed ordered spans, including empty spans. Other rules concatenate contiguous spans, and any edge preserving width advances in an acyclic graph on category keys. The pair of span width and category rank increases strictly along every edge.
| PARAMETER | DESCRIPTION |
|---|---|
presentation
|
The checked
TYPE:
|
words
|
One word per lexical row, preserving distinct occurrences.
TYPE:
|
parameters
|
Rule parameters supplied after the lexical weights, in order.
TYPE:
|
span
|
The name of the single lexical judgment and corresponding item constructor, whose fields are the endpoints, category, and any form.
TYPE:
|
start
|
The start category, or
TYPE:
|
item_type
|
The runtime constructor family for the span and its data fields.
TYPE:
|
item_constructors
|
Source names and constructor identities of that family.
TYPE:
|
empty
|
The constructors of the result family. An entry has a key of
TYPE:
|
entry
|
The constructors of the result family. An entry has a key of
TYPE:
|
span_order
cached
property
¶
Certify finite grounding of this presentation.
| RETURNS | DESCRIPTION |
|---|---|
SpanOrder
|
The strict endpoint-width and category-rank order. |
unary_height
property
¶
Measure chains using only unary transitions.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The longest chain in the certified unary category graph. |
DeductionProofs
didactic-model
¶
DeductionProofs(*, chart: DeductionOutcomes = field(opaque=True, extras=OPAQUE_BY_VALUE), tag: Literal['deduction-proofs'] = 'deduction-proofs')
Bases: QiecModel
Return supported full-span proofs under one exact proof budget.
Each result entry binds its proof's indexed family arguments. The entry holds that proof, its log weight, and the next entry. The operation accepts a structural proof limit and a normalization choice. Different derivations keep their own weights.
| PARAMETER | DESCRIPTION |
|---|---|
chart
|
The existing lexical chart descriptor and its result constructors. Its lexical span syntax and grammar parameters remain unchanged.
TYPE:
|
proof_presentation
cached
property
¶
proof_presentation: RulePresentation
Rename the chart's proof declarations into the calling module.
| RETURNS | DESCRIPTION |
|---|---|
RulePresentation
|
The same rule, axiom and judgment identities at renamed sorts, with proof constructors named by the deduction. |
DeductionBooleanOutcomes
didactic-model
¶
DeductionBooleanOutcomes(*, chart: DeductionOutcomes = field(opaque=True, extras=OPAQUE_BY_VALUE), tag: Literal['deduction-boolean-outcomes'] = 'deduction-boolean-outcomes')
Bases: QiecModel
Push finite lexical proof mass to a Boolean interpretation.
An input truth table contains one axiom result per distinct lexical word in first occurrence order, followed by each rule's truth table in presentation order. Each rule table enumerates premise outcomes left to right, with false before true. An arity bound of two keeps every rule table at most four entries.
| PARAMETER | DESCRIPTION |
|---|---|
chart
|
The shared weighted span presentation and its Boolean result row constructors.
TYPE:
|
lexical_words
cached
property
¶
Count one axiom evaluation for every distinct lexical word.
| RETURNS | DESCRIPTION |
|---|---|
tuple of str
|
The words in first lexical occurrence order. |
table_size
cached
property
¶
Count entries of the axiom and ordered rule tables.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The length of the input truth vector. |
DeductionOutcomeGrouping
didactic-model
¶
DeductionOutcomeGrouping(*, key_type: TypeExpr = field(opaque=True, extras=OPAQUE_BY_VALUE), empty: ConstructorId = field(opaque=True, extras=OPAQUE_BY_VALUE), entry: ConstructorId = field(opaque=True, extras=OPAQUE_BY_VALUE), tag: Literal['deduction-outcome-grouping'] = 'deduction-outcome-grouping')
Bases: QiecModel
Group a typed list of finite log masses by exact structural equality.
| PARAMETER | DESCRIPTION |
|---|---|
key_type
|
The checked type of each key. The module verifies that the type admits exact structural equality before the reducer can run.
TYPE:
|
empty
|
The constructors of the typed row list. An entry contains a key, its log mass, and the rest of the list.
TYPE:
|
entry
|
The constructors of the typed row list. An entry contains a key, its log mass, and the rest of the list.
TYPE:
|
DeductionOutcomeLaw
didactic-model
¶
DeductionOutcomeLaw(*, layout: DeductionOutcomeGrouping = field(opaque=True, extras=OPAQUE_BY_VALUE), tag: Literal['deduction-outcome-law'] = 'deduction-outcome-law')
Bases: QiecModel
Normalize checked deduction outcome rows as one finite sampleable.
| PARAMETER | DESCRIPTION |
|---|---|
layout
|
The original key type and the two constructors of the weighted rows.
TYPE:
|