Chart Semirings¶
Semiring abstractions for parameterizing chart parsing (Goodman 1999). Selecting a semiring changes what the CKY skeleton computes: marginal log-probability, a Viterbi best parse, boolean recognition, or a derivation count.
semiring
¶
Semiring abstractions for parameterizing chart parsing.
A semiring (S, ⊕, ⊗, 0, 1) provides the algebraic structure for chart parsing (Goodman, 1999). Different semirings yield different parsing algorithms from the same CKY skeleton:
- LogProbSemiring: marginal log-probability (logsumexp / +)
- ViterbiSemiring: best-derivation log-probability (max / +)
- BooleanSemiring: recognition (or / and)
- CountingSemiring: derivation counting (+ / ×)
Each semiring defines:
times(a, b): multiplicative combination (⊗)plus(scores, dim): additive aggregation (⊕) over a dimensionzero: additive identity (0): score for impossible derivationsone: multiplicative identity (1): score for trivial derivations
Categorical perspective
Semirings are the decategorification of rig categories (bimonoidal
categories). The times operation corresponds to the tensor
product, plus to the coproduct. Chart parsing is a functor
from the free monoidal category (grammar) to the semiring.
The connection to algebras: an algebra is a complete lattice with an associative binary operation distributing over arbitrary joins. The log-probability semiring is the algebra ([-∞, 0], logsumexp, +).
ChartSemiring
¶
Bases: ABC
Abstract semiring for chart parsing.
Subclasses implement the four semiring operations. All operations must be differentiable (where applicable) to support gradient-based learning.
zero
abstractmethod
property
¶
Additive identity: the score of an impossible derivation.
one
abstractmethod
property
¶
Multiplicative identity: the score of a trivial derivation.
times
abstractmethod
¶
Multiplicative combination (⊗).
Combines scores from two chart cells being joined by a rule.
| PARAMETER | DESCRIPTION |
|---|---|
a
|
Left operand scores.
TYPE:
|
b
|
Right operand scores.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Combined scores. |
Source code in src/quivers/stochastic/semiring.py
plus
abstractmethod
¶
Additive aggregation (⊕) over a dimension.
Marginalizes over multiple derivations of the same span.
| PARAMETER | DESCRIPTION |
|---|---|
scores
|
Scores to aggregate.
TYPE:
|
dim
|
Dimension to aggregate over.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Aggregated scores. |
Source code in src/quivers/stochastic/semiring.py
plus_pair
abstractmethod
¶
Binary additive combination (⊕) of two tensors.
Used for combining a cell with unary rule contributions.
| PARAMETER | DESCRIPTION |
|---|---|
a
|
First operand.
TYPE:
|
b
|
Second operand.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Combined scores. |
Source code in src/quivers/stochastic/semiring.py
LogProbSemiring
¶
Bases: ChartSemiring
Log-probability semiring: (logsumexp, +, -∞, 0).
This is the default semiring for marginal parsing. times is
addition in log-space (= multiplication of probabilities) and
plus is logsumexp (= addition of probabilities in log-space).
times
¶
plus
¶
Sum probabilities over dim by logsumexp.
| PARAMETER | DESCRIPTION |
|---|---|
scores
|
Scores to aggregate.
TYPE:
|
dim
|
Dimension to aggregate over.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The aggregated scores, with |
Source code in src/quivers/stochastic/semiring.py
ViterbiSemiring
¶
Bases: ChartSemiring
Viterbi semiring: (max, +, -∞, 0).
Finds the single best derivation. plus takes the max over
derivations rather than summing. Useful for Viterbi parsing
and best-first decoding.
times
¶
plus
¶
Take the best score over dim.
| PARAMETER | DESCRIPTION |
|---|---|
scores
|
Scores to aggregate.
TYPE:
|
dim
|
Dimension to aggregate over.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The aggregated scores, with |
Source code in src/quivers/stochastic/semiring.py
BooleanSemiring
¶
Bases: ChartSemiring
Boolean semiring: (or, and, 0, 1).
For recognition only: determines whether a parse exists without computing scores. Values are 0.0 (false) and 1.0 (true).
times
¶
Conjoin truth values in {0, 1} by multiplication.
| PARAMETER | DESCRIPTION |
|---|---|
a
|
Left operand scores.
TYPE:
|
b
|
Right operand scores.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The combined scores. |
Source code in src/quivers/stochastic/semiring.py
plus
¶
Disjoin truth values in {0, 1} over dim by max.
| PARAMETER | DESCRIPTION |
|---|---|
scores
|
Scores to aggregate.
TYPE:
|
dim
|
Dimension to aggregate over.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The aggregated scores, with |
Source code in src/quivers/stochastic/semiring.py
CountingSemiring
¶
Bases: ChartSemiring
Counting semiring: (+, ×, 0, 1).
Counts the number of derivations. plus sums counts and
times multiplies them (product rule for independent choices).
Note: Not differentiable in a useful sense for gradient-based learning, but useful for analysis.
times
¶
plus
¶
Sum derivation counts over dim.
| PARAMETER | DESCRIPTION |
|---|---|
scores
|
Scores to aggregate.
TYPE:
|
dim
|
Dimension to aggregate over.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The aggregated scores, with |
Source code in src/quivers/stochastic/semiring.py
Semiring singletons¶
Stochastic morphisms: Markov kernels over finite sets.
This module provides the probabilistic layer for quivers, implementing the category FinStoch of finite sets and stochastic maps (Markov kernels). This is the Kleisli category of the Giry monad restricted to finite sets, where:
- Objects are finite sets (same as the base category).
- Morphisms A -> B are stochastic matrices: tensors of shape (|A|, |B|) whose rows sum to 1.
- Composition is standard matrix multiplication.
- Identity is the Kronecker delta.
The package also re-exports MarkovAlgebra and its singleton
MARKOV from quivers.core.algebras, the algebra these morphisms
compose under.
Submodules
morphisms : StochasticMorphism, CategoricalMorphism families : Discretized distribution families transforms : condition, mix, factor, normalize queries : prob, marginal_prob, expectation giry : GiryMonad, FinStoch categories : Category types including atoms, slashes, products, unit, modals semiring : Chart semiring abstractions (LogProb, Viterbi, Boolean, Counting) schema : Composable rule schemas (functors CategorySystem -> RuleSystem) rules : Rule systems and the CCG / Lambek presets span : Span-based CKY components (LexicalAxiom, BinarySpanDeduction, etc.) parsers, ccg, lambek : Chart parsers inside : The inside algorithm over a rule system agenda : The agenda-driven weighted-deduction engine effect_lifts : Effect-lifted rule schemas for deduction systems deduction : Abstract weighted deductive system framework and its operations stdlib : Pre-registered deduction systems
LOG_PROB
module-attribute
¶
LOG_PROB = LogProbSemiring()
The log-probability semiring singleton, the default for chart parsing.
VITERBI
module-attribute
¶
VITERBI = ViterbiSemiring()
The Viterbi (max-product in log space) semiring singleton.
BOOLEAN
module-attribute
¶
BOOLEAN = BooleanSemiring()
The Boolean (or-and) semiring singleton, for recognition.
COUNTING
module-attribute
¶
COUNTING = CountingSemiring()
The counting (sum-product over naturals) semiring singleton.