std.semiring¶
Semirings as theories, and the instances deductions run in.
A deduction's weights live in a commutative
semiring: derivations of an
item are summed and the weights along one derivation are multiplied.
Semiring states the laws that make that sum independent of evaluation
order. ClosedSemiring adds a closure star(x), the sum of all powers
of x, and OmegaContinuousSemiring the identities of the closure that
hold when every increasing chain has a least upper bound; a deduction
whose rules may derive an item from itself runs only in an instance of
one of the two, or under a declared stopping rule.
One sentence in six semirings¶
This grammar can analyze birds as a noun or as a lexical subject. Both
analyses combine with fly to derive the same sentence category. Each
deduction uses the same rules and lexicon and selects a different semiring
with its S parameter.
import std.semiring as SR
object Word : FinSet 2
deduction signature Fragment
atoms S, Noun, Subject, Verb, span, birds, fly
rule noun_subject : span(I, K, Noun), span(K, J, Verb) |- span(I, J, S)
rule lexical_subject : span(I, K, Subject), span(K, J, Verb) |- span(I, J, S)
lexicon Words for Fragment
"birds" : Noun = birds
"birds" : Subject = birds
"fly" : Verb = fly
deduction Count[S = SR.Counting] : Word -> Word [start=S, depth=4]
extends Fragment
lexicon Words
deduction Recognize[S = SR.Boolean] : Word -> Word [start=S, depth=4]
extends Fragment
lexicon Words
deduction Total[S = SR.LogProb] : Word -> Word [start=S, depth=4]
extends Fragment
lexicon Words
deduction Best[S = SR.Viterbi] : Word -> Word [start=S, depth=4]
extends Fragment
lexicon Words
deduction Cheapest[S = SR.Tropical] : Word -> Word [start=S, depth=4]
extends Fragment
lexicon Words
deduction Strongest[S = SR.Godel] : Word -> Word [start=S, depth=4]
extends Fragment
lexicon Words
Counting gives two derivations, and Boolean recognizes the sentence. Each derivation has log weight zero, so LogProb gives \(\log 2\) and Viterbi gives the best log weight, zero. The default cost is zero in Tropical, and the default strength is one in Godel.
from math import isclose, log
from quivers.dsl import load
from quivers.dsl.semirings import free_chart, polynomial
program = load("docs/examples/qiec/semiring-ambiguity.qvr")
sentence = ["birds", "fly"]
systems = program.deductions
scores = {name: system(sentence).goal_weight() for name, system in systems.items()}
assert scores["Count"].item() == 2
assert bool(scores["Recognize"])
assert isclose(scores["Total"].item(), log(2), rel_tol=1e-6)
assert scores["Best"].item() == 0
assert scores["Cheapest"].item() == 0
assert scores["Strongest"].item() == 1
chart = free_chart(program.deductions["Best"], sentence)
(goal,) = chart.goals
assert sorted(polynomial(chart, goal).values()) == [1, 1]
The free chart retains the two derivations as distinct monomials, each with coefficient one.
The examples on this page run in this module, which opens std.semiring:
Boolean (semiring)¶
semiring Boolean : OmegaContinuousSemiring = algebra Boolean on Bool
The Boolean semiring of recognition.
Carrier (theory)¶
theory Carrier
The carrier of a semiring.
ClosedSemiring (theory)¶
theory ClosedSemiring
A closed semiring,
one with a closure: star(x) solves w = 1 + x w.
Counting (semiring)¶
semiring Counting : Semiring = algebra Counting on Int
The counting semiring of derivation counts.
Godel (semiring)¶
semiring Godel : OmegaContinuousSemiring = algebra Godel on Real
The Goedel semiring of the unit interval under maximum and minimum.
LogProb (semiring)¶
semiring LogProb : Semiring = algebra LogProb on LogWeight
The log semiring: log-sum-exp and addition of log weights.
OmegaContinuousSemiring (theory)¶
theory OmegaContinuousSemiring
A closed semiring whose closure is the least upper bound of the powers of its argument; it satisfies the Conway identities.
Semiring (theory)¶
theory Semiring
A commutative semiring: the laws a chart's sums and products rely on.
Tropical (semiring)¶
semiring Tropical : OmegaContinuousSemiring = algebra Tropical on Real
The tropical semiring of least costs over the nonnegative reals with infinity.
Viterbi (semiring)¶
semiring Viterbi : Semiring = algebra MaxPlus on LogWeight
The max-plus semiring of best derivations over log weights.