Rule Systems¶
Composable structural rule systems for chart parsing. A RuleSystem
stores binary and unary inference rules as index tensors. The +
operator merges rule systems for hybrid grammars.
A rule system may carry learnable weights, one log weight per binary
rule and one per unary rule. Parsing adds these weights to the chart
combination scores. When passed to a ChartParser, the weights
initialize its nn.Parameter tensors, or fixed buffers when
learnable_rule_weights=False. Merging with + preserves each rule's
weight; duplicate rules keep the weight from the left operand.
The convenience functions ccg_rules and lambek_rules instantiate
rule schema presets over a given category system.
For custom grammars, compose the schema primitives directly via |.
_rule_system
¶
Core RuleSystem model for composable chart parsing rules.
This private module exists to break circular imports between
biclosed.py (primitive categorical operations) and rules.py
(grammar-specific rule combinations).
Each rule may carry an optional initial log-weight. When registered
in a ChartParser, these become learnable nn.Parameter values
that are added to rule scores during chart filling, implementing a
weighted deductive system.
RuleSystem
didactic-model
¶
RuleSystem(*, binary_rules: tuple[tuple[int, ...], ...] = (), unary_rules: tuple[tuple[int, ...], ...] = (), n_categories: int = 0, description: str = '', binary_weights: tuple[float, ...] | None = None, unary_weights: tuple[float, ...] | None = None)
Bases: Model
A composable set of structural rules for chart parsing.
| ATTRIBUTE | DESCRIPTION |
|---|---|
binary_rules |
Each inner tuple is
TYPE:
|
unary_rules |
Each inner tuple is
TYPE:
|
n_categories |
Total number of categories in the system.
TYPE:
|
description |
Human-readable label (e.g.
TYPE:
|
binary_weights |
Initial log-weights for binary rules.
TYPE:
|
unary_weights |
Initial log-weights for unary rules.
TYPE:
|
binary_tensors
¶
Return binary rules as (results, lefts, rights) index tensors.
Source code in src/quivers/stochastic/_rule_system.py
binary_weight_tensor
¶
Return initial binary rule weights as a float tensor.
Returns zeros when no explicit weights are set.
Source code in src/quivers/stochastic/_rule_system.py
unary_tensors
¶
Return unary rules as (results, inputs) index tensors, or None.
Source code in src/quivers/stochastic/_rule_system.py
unary_weight_tensor
¶
Return initial unary rule weights as a float tensor.
Returns zeros when no explicit weights are set.
Source code in src/quivers/stochastic/_rule_system.py
__add__
¶
__add__(other: RuleSystem) -> RuleSystem
Merge two rule systems (deduplicated union).
Source code in src/quivers/stochastic/_rule_system.py
rules
¶
Composable rule systems for chart parsing.
A RuleSystem encapsulates the structural inference rules that govern
a grammar -- binary combination rules and unary type-change rules -- as
index tensors suitable for CKY chart parsing. Rule systems are the
compositional unit: they can be merged (+), inspected, and passed
to any ChartParser.
The primitive rule schemas live in quivers.stochastic.schema,
which provides composable functors CategorySystem -> RuleSystem.
The convenience functions here (ccg_rules, lambek_rules)
instantiate schema presets over a given category system.
Categorical perspective
A rule system is a presentation of the arrows in a free category generated by a set of basic combinators. Merging two rule systems takes the coproduct of their generating sets.
ccg_rules
¶
ccg_rules(system: CategorySystem, *, enable_composition: bool = True, enable_crossed_composition: bool = True, enable_type_raising: bool = False, generalized_composition_depth: int = 1) -> RuleSystem
Build a CCG rule system from a category inventory.
| PARAMETER | DESCRIPTION |
|---|---|
system
|
The finite category inventory.
TYPE:
|
enable_composition
|
Enable harmonic composition (>B, <B).
TYPE:
|
enable_crossed_composition
|
Enable crossed composition (>Bx, <Bx).
TYPE:
|
enable_type_raising
|
Enable type raising (>T, <T).
TYPE:
|
generalized_composition_depth
|
Maximum depth for generalized composition (B^n).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RuleSystem
|
The CCG rule system. |
Source code in src/quivers/stochastic/rules.py
lambek_rules
¶
lambek_rules(system: CategorySystem, *, associative: bool = True, commutative: bool = False, enable_lifting: bool = True, enable_product: bool = True) -> RuleSystem
Build a Lambek calculus rule system from a category inventory.
| PARAMETER | DESCRIPTION |
|---|---|
system
|
The finite category inventory.
TYPE:
|
associative
|
Use associative Lambek calculus L (default True).
TYPE:
|
commutative
|
Use commutative product / LP calculus (default False).
TYPE:
|
enable_lifting
|
Enable Lambek lifting rules (adjunction units).
TYPE:
|
enable_product
|
Enable product introduction / projection.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RuleSystem
|
The Lambek calculus rule system. |
Source code in src/quivers/stochastic/rules.py
custom_rules
¶
custom_rules(binary: list[tuple[int, int, int]] | None = None, unary: list[tuple[int, int]] | None = None, n_categories: int = 0, description: str = 'custom') -> RuleSystem
Build a rule system from explicit rule triples.
| PARAMETER | DESCRIPTION |
|---|---|
binary
|
Binary rules as (result, left, right) triples.
TYPE:
|
unary
|
Unary rules as (result, input) pairs.
TYPE:
|
n_categories
|
Total number of categories.
TYPE:
|
description
|
Label for the rule system.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RuleSystem
|
The custom rule system. |