Semirings¶
A deduction sums the weights of the derivations
of an item and multiplies the weights along one derivation, so its weights live in a
semiring (Goodman, 1999). The standard library module
std.semiring states the semiring laws as theories and declares the instances a
deduction runs in. A deduction names its instance in its header, or ranges over the instances of a
theory, and its chart in the free instance specializes to every other. The constructions are in
the semantics of semirings.
The theories and instances of std.semiring¶
| Theory | Operations | Laws |
|---|---|---|
Semiring |
zero, one, plus, times over the sort W |
plus and times associative and commutative with units zero and one; times distributes over plus; zero annihilates |
ClosedSemiring |
Semiring and star : W -> W |
star_unfold : star(x) = plus(one, times(x, star(x))) |
OmegaContinuousSemiring |
ClosedSemiring |
the Conway identities sum_star and product_star |
| Instance | Theory | Algebra on carrier |
|---|---|---|
LogProb |
Semiring |
LogProb on LogWeight (log-sum-exp and addition) |
Viterbi |
Semiring |
MaxPlus on LogWeight (the best derivation) |
Boolean |
OmegaContinuousSemiring |
Boolean on Bool (recognition) |
Counting |
Semiring |
Counting on Int (the number of derivations) |
Tropical |
OmegaContinuousSemiring |
Tropical on Real (least cost) |
Godel |
OmegaContinuousSemiring |
Godel on Real (maximum and minimum on the unit interval) |
Semiring instances¶
import std.semiring as SR
theory Idempotent
include SR.Semiring
equations
plus_idem : plus(x, x) = x
semiring Best : Idempotent = algebra MaxPlus on LogWeight
assume plus_idem
semiring I : T = algebra A on C declares an instance of the theory T, which must include
std.semiring's Semiring. The instance is realized by the shipped algebra A, named as its law
claims name it (LogProb, MaxPlus, Tropical, Boolean, Godel, Counting, Real, Markov),
acting on the kernel carrier C (LogWeight, Real, Bool, Int). Each law of T is evaluated on
the algebra at every assignment of a set of elements: all of them for a finite carrier, and the
units with a fixed spread of elements otherwise. A law that fails at an assignment refutes the
instance, and the refusal gives the assignment and both sides. A law that holds is established
when the carrier is finite, or when it is a law of std.semiring whose counterpart the algebra
claims with a proof. Any other law must be listed in an assume line of the declaration, which
records it as assumed:
import std.semiring as SR
semiring MinPlus : SR.OmegaContinuousSemiring = algebra Tropical on Real
assume sum_star, product_star
Program.semirings maps each instance to a
CheckedSemiring with one
LawRecord per law: its status and its evidence.
A finite model of a semiring theory, interpreted by QIEC computations, is
checked against the same laws exhaustively. A model whose times does not distribute over its
plus is refused with the counterexample:
model 'Broken' of 'std.semiring:Semiring' violates the law 'std.semiring:Semiring.distrib' at x = true, y = true, z = false: the left side is false and the right side is true
A deduction's semiring¶
import std.semiring
object Term : FinSet 8
deduction Attach[S = std.semiring.Counting] : Term -> Term [start=S]
atoms S, NP, VP, PP, V, P, span, saw, man, telescope, with, a
rule s_np_vp : span(I, K, NP), span(K, J, VP) |- span(I, J, S)
rule vp_v_np : span(I, K, V), span(K, J, NP) |- span(I, J, VP)
rule vp_vp_pp : span(I, K, VP), span(K, J, PP) |- span(I, J, VP)
rule np_np_pp : span(I, K, NP), span(K, J, PP) |- span(I, J, NP)
rule pp_p_np : span(I, K, P), span(K, J, NP) |- span(I, J, PP)
lexicon
"I" : NP = a
"saw" : V = saw
"man" : NP = man
"telescope" : NP = telescope
"with" : P = with
| Header | Meaning |
|---|---|
deduction G[S = I] : A -> B |
G runs in the instance I: every handler of its search, collection, and settlement carries I's algebra on I's carrier, and its answer is an element of that carrier |
deduction G : A -> B |
G runs in the log semiring, std.semiring.LogProb |
deduction G[S : T] : A -> B |
G is generic over the instances of the theory T, which includes Semiring; its chart is computed in the free instance and specialized |
The name S is a label of the deduction and names nothing in its rules, so it never shadows an
atom. A learned or declared weight is a log weight, and enters an instance whose elements are log
weights as it is; an instance of other elements reads it as its unit. Per-left-hand-side production
weights (normalize=lhs) therefore need an instance whose elements are log weights. A name that is
no instance in scope is refused with the instances that are.
Cycles need a fixpoint¶
A deduction whose rules may derive an item from itself sums an infinite series of derivations. It
must run in an instance of ClosedSemiring or OmegaContinuousSemiring, range over such a theory,
or declare how its iteration stops with tolerance; otherwise it is refused, with the rules of the
cycle named (Abo Khamis et al., 2022):
import std.semiring
deduction Loop[S = std.semiring.Counting] : Term -> Term [start=B]
atoms A, B
rule ab : A |- B
rule ba : B |- A
deduction 'Loop' may derive an item from itself (rules 'ab', 'ba' form a cycle), and std.semiring:Counting (an instance of std.semiring:Semiring) guarantees no fixpoint of a cycle
The test reads the rules alone and is sound: rules whose every possible cycle strictly grows the input an item covers, or keeps it while strictly growing the item's size, pass. A lexical grammar with binary rules and unary category changes that cannot repeat passes without a stop.
The free chart and its specializations¶
free_chart computes a deduction's chart once, in the free
commutative semiring: its derivation hypergraph, each item with the rule instances and input
occurrences that derive it (Green et al., 2007). specialize
and goal_value evaluate it along a homomorphism:
from quivers.dsl import load
from quivers.dsl.semirings import free_chart, goal_value, homomorphism, polynomial
program = load("grammar.qvr")
chart = free_chart(program.deductions["Attach"], "I saw man with telescope".split())
for name in ("LogProb", "Viterbi", "Boolean", "Counting"):
instance = program.semirings[f"std.semiring:{name}"]
print(name, float(goal_value(chart, homomorphism(instance))))
(goal,) = chart.goals
print(polynomial(chart, goal)) # the provenance polynomial of the goal
| Function | Gives |
|---|---|
homomorphism(instance) |
the homomorphism that sends each generator to its weight in the deduction run in instance, so the specialization equals that run |
product(h1, h2) |
the pairing of two homomorphisms, into the product of their semirings |
ModelHomomorphism(model, images) |
the homomorphism into a finite model of a semiring theory |
polynomial(chart, item) |
the item's element of the free semiring: each monomial of generators with the number of derivations that have it |
joint(left, right, input) |
two deductions with the same rules, each in its own instance, computed in one pass over the product (Cohen et al., 2011) |
A cyclic chart specializes to an instance whose theory guarantees the fixpoint, by iteration, or
under a height bound on its derivations; any other specialization of it is refused.
Diagnostics¶
| Code or error | Refuses |
|---|---|
qiec-semiring |
a semiring= option, an unknown instance, an instance whose algebra or carrier is not shipped, a law refuted or neither established nor assumed, a cyclic deduction without a fixpoint guarantee |
CompileError |
the same at compile time, at the declaration or the deduction |
SemiringError |
a cyclic free chart specialized without a guarantee or height, a product of deductions without one derivation hypergraph |
Every transpile target accepts semiring declarations, which denote no run-time code, and refuses
a module whose instance breaks a law. A program that parses with a deduction in an instance is
treated as the same program in the log semiring, and a Boolean deduction's answer has no real value
to score.
Cited sources¶
Joshua Goodman. Semiring Parsing. Computational Linguistics, 25(4):573–606, 1999. URL: https://aclanthology.org/J99-4004/.
Mahmoud Abo Khamis, Hung Q. Ngo, Reinhard Pichler, Dan Suciu, and Yisu Remy Wang. Convergence of Datalog over (Pre-) Semirings. In Proceedings of the 41st ACM SIGMOD-SIGACT-SIGAI Symposium on Principles of Database Systems, 105–117. 2022. URL: https://doi.org/10.1145/3517804.3524140, doi:10.1145/3517804.3524140.
Todd J. Green, Grigoris Karvounarakis, and Val Tannen. Provenance Semirings. In Proceedings of the Twenty-Sixth ACM SIGMOD-SIGACT-SIGART Symposium on Principles of Database Systems, 31–40. 2007. URL: https://doi.org/10.1145/1265530.1265535, doi:10.1145/1265530.1265535.
Shay B. Cohen, Robert J. Simmons, and Noah A. Smith. Products of Weighted Logic Programs. Theory and Practice of Logic Programming, 11(2-3):263–296, 2011. URL: https://doi.org/10.1017/S1471068410000529, doi:10.1017/S1471068410000529.