![]() |
ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
|
Abstract base class for (m-)semirings. More...
#include "Semiring.h"
Public Types | |
| typedef V | value_type |
| The carrier type of this semiring. | |
Public Member Functions | |
| virtual value_type | zero () const =0 |
| Return the additive identity \(\mathbb{0}\). | |
| virtual value_type | one () const =0 |
| Return the multiplicative identity \(\mathbb{1}\). | |
| virtual value_type | plus (const std::vector< value_type > &v) const =0 |
| Apply the additive operation to a list of values. | |
| virtual value_type | times (const std::vector< value_type > &v) const =0 |
| Apply the multiplicative operation to a list of values. | |
| virtual value_type | monus (value_type x, value_type y) const =0 |
| Apply the monus (m-semiring difference) operation. | |
| virtual value_type | delta (value_type x) const =0 |
| Apply the \(\delta\) operator. | |
| virtual value_type | cmp (value_type s1, ComparisonOperator op, value_type s2) const |
| Evaluate a comparison gate. | |
| virtual value_type | semimod (value_type x, value_type s) const |
| Apply a semimodule scalar multiplication. | |
| virtual value_type | agg (AggregationOperator op, const std::vector< value_type > &s) |
| Evaluate an aggregation gate. | |
| virtual value_type | value (const std::string &s) const |
| Interpret a literal string as a semiring value. | |
| virtual value_type | unmapped_input (const std::string &uuid) const |
Value of a variable leaf (gate_input / gate_mulinput) that the provenance mapping does not name. | |
| virtual value_type | rv (const std::string &spec, const std::vector< value_type > ¶ms) const |
Evaluate a continuous random-variable leaf (gate_rv). | |
| virtual value_type | arith (ArithmeticOperator op, const std::vector< value_type > &children, const std::string &extra) const |
Evaluate an arithmetic gate over scalar children (gate_arith). | |
| virtual value_type | mixture (value_type p, value_type x, value_type y) const |
Evaluate a Bernoulli mixture (gate_mixture, three wires). | |
| virtual value_type | categorical (value_type key, const std::vector< double > &probs, const std::vector< std::string > &outcomes) const |
Evaluate a categorical mixture (gate_mixture over gate_mulinput outcomes). | |
| virtual value_type | guarded_case (const std::vector< value_type > &children) const |
Evaluate a guarded selection (gate_case). | |
| virtual value_type | observe (value_type child, const std::string &datum) const |
Evaluate a latent-variable observation (gate_observe). | |
| virtual value_type | conditioned (const std::vector< value_type > &children) const |
Evaluate a conditioning marker (gate_conditioned). | |
| virtual | ~Semiring ()=default |
| virtual bool | absorptive () const |
Return true if this semiring is absorptive ( \(\mathbb{1} \oplus a = \mathbb{1}\) for all \(a\)). | |
| virtual bool | compatibleWithBooleanRewrite () const |
Return true if a semiring homomorphism BoolFunc(X) →+* S exists, so the safe-query (Boolean-rewrite) optimisation produces circuits that are semantically faithful when evaluated under this semiring. | |
| virtual bool | certifying () const |
| Whether this semiring builds certified exclusive enumerations (see the three hooks below). | |
| virtual bool | independent_literal (const value_type &) const |
Whether v is an independent literal for certification purposes: a base Bernoulli variable (or a constant), so that distinct literals have disjoint supports and an AND over them is decomposable. | |
| virtual value_type | certified_world_term (const std::vector< value_type > &, const std::vector< value_type > &) const |
Build one complete world term: the conjunction of the present literals and the negations of the missing literals, certified decomposable. | |
| virtual value_type | certified_exclusive_plus (const std::vector< value_type > &) const |
Build the disjunction of pairwise-exclusive disjuncts, certified deterministic. | |
Abstract base class for (m-)semirings.
| V | The carrier type (e.g. bool, unsigned, std::string). |
All pure-virtual methods must be implemented by concrete subclasses.
cmp(), semimod(), agg(), and value() have default implementations that throw SemiringException. Override them in subclasses that support these circuit gate types.
A semiring is absorptive (i.e., \(\mathbb{1} \oplus a = \mathbb{1}\) for all \(a\)) iff absorptive() returns true. Absorptivity implies idempotency ( \(a \oplus a = a\)), which lets the circuit evaluator and the HAVING-semantics machinery deduplicate operands and short-circuit over the multiplicative identity.
Definition at line 96 of file Semiring.h.
| typedef V semiring::Semiring< V >::value_type |
The carrier type of this semiring.
Definition at line 100 of file Semiring.h.
|
virtualdefault |
|
inlinevirtual |
Return true if this semiring is absorptive ( \(\mathbb{1} \oplus a = \mathbb{1}\) for all \(a\)).
When true, the circuit evaluator and HAVING-semantics machinery may exploit the resulting idempotency ( \(a \oplus a = a\), implied by absorptivity) to deduplicate children of plus gates and to short-circuit over the multiplicative identity.
false by default; override to return true. Reimplemented in semiring::Boolean, semiring::BoolExpr, semiring::IntervalUnion, semiring::Lukasiewicz, semiring::MinMax, semiring::TropicalNonneg, and semiring::Viterbi.
Definition at line 360 of file Semiring.h.
|
inlinevirtual |
Evaluate an aggregation gate.
| op | The aggregation function (COUNT, SUM, MIN…). |
| s | List of child semiring values to aggregate. |
| SemiringException | if not overridden. |
Reimplemented in semiring::Formula.
Definition at line 180 of file Semiring.h.
|
inlinevirtual |
Evaluate an arithmetic gate over scalar children (gate_arith).
| op | The arithmetic operation. |
| children | The values of the gate's wires (for PERCENTILE, interleaved [indicator, value] pairs). |
| extra | The gate's payload (the fraction for PERCENTILE, empty otherwise). |
| SemiringException | if not overridden. |
Reimplemented in semiring::Formula.
Definition at line 248 of file Semiring.h.
|
inlinevirtual |
Evaluate a categorical mixture (gate_mixture over gate_mulinput outcomes).
| key | The value of the mixture's key (its gate_input wire). |
| probs | Probability of each outcome. |
| outcomes | Textual payload of each outcome, parallel to probs. |
| SemiringException | if not overridden. |
Reimplemented in semiring::Formula.
Definition at line 285 of file Semiring.h.
|
inlinevirtual |
Build the disjunction of pairwise-exclusive disjuncts, certified deterministic.
Only called when certifying() is true; the default throws.
Reimplemented in semiring::BoolExpr.
Definition at line 441 of file Semiring.h.
|
inlinevirtual |
Build one complete world term: the conjunction of the present literals and the negations of the missing literals, certified decomposable.
Only called when certifying() is true; the default throws.
Reimplemented in semiring::BoolExpr.
Definition at line 429 of file Semiring.h.
|
inlinevirtual |
Whether this semiring builds certified exclusive enumerations (see the three hooks below).
The HAVING possible-worlds machinery (provsql_having) enumerates, for a comparison over a group aggregate, the complete valuations of the group's contributors that satisfy the predicate. Those disjuncts partition the worlds – the OR is deterministic and each world term an AND of literals over distinct contributors, decomposable – i.e., the enumeration is a d-DNNF by construction. A circuit-building semiring can persist that knowledge as the d-DNNF certificate (the same mark the bounded-treewidth reachability route emits), letting the linear certificate-aware evaluators handle the result; scalar semirings have nothing to mark.
false by default (the hooks below then go unused and the default constructions are used). Reimplemented in semiring::BoolExpr.
Definition at line 404 of file Semiring.h.
|
inlinevirtual |
Evaluate a comparison gate.
| s1 | Left operand. |
| op | Comparison operator. |
| s2 | Right operand. |
| SemiringException | if not overridden. |
Reimplemented in semiring::Formula.
Definition at line 156 of file Semiring.h.
|
inlinevirtual |
Return true if a semiring homomorphism BoolFunc(X) →+* S exists, so the safe-query (Boolean-rewrite) optimisation produces circuits that are semantically faithful when evaluated under this semiring.
The compiled-semiring dispatcher consults this predicate before evaluating a circuit whose root gate carries PROVSQL_ROOT_TAG_BOOLEAN_REWRITE. Returning false on a tagged circuit raises CircuitException.
Defaults to false: a new semiring whose author has not yet verified the homomorphism is fail-closed by construction. Subclasses with a verified homomorphism (currently Boolean, BoolExpr, Formula, and IntervalUnion) override to return true. The justification (Lean-proof reference) belongs in a comment next to each override; see the src/semiring/ headers.
Reimplemented in semiring::Boolean, semiring::BoolExpr, semiring::Counting, semiring::Formula, semiring::How, semiring::IntervalUnion, semiring::Lukasiewicz, semiring::MinMax, semiring::Tropical, semiring::Viterbi, semiring::Which, and semiring::Why.
Definition at line 382 of file Semiring.h.
|
inlinevirtual |
Evaluate a conditioning marker (gate_conditioned).
| children | The gate's wires: [target, evidence] for the value-level (random-variable) form, [target, evidence, joint] for the Boolean-event one, where joint is the materialised times(target, evidence). |
| SemiringException | if not overridden. |
Reimplemented in semiring::Formula.
Definition at line 337 of file Semiring.h.
|
pure virtual |
Apply the \(\delta\) operator.
| x | Input value. |
Implemented in semiring::Boolean, semiring::BoolExpr, semiring::Counting, semiring::Formula, semiring::How, semiring::IntervalUnion, semiring::Lukasiewicz, semiring::MinMax, semiring::Tropical, semiring::Viterbi, semiring::Which, and semiring::Why.
|
inlinevirtual |
Evaluate a guarded selection (gate_case).
| children | The gate's wires, [guard_1, value_1, …, guard_k, value_k, default] (odd length), with first-match semantics. |
| SemiringException | if not overridden. |
Reimplemented in semiring::Formula.
Definition at line 304 of file Semiring.h.
|
inlinevirtual |
Whether v is an independent literal for certification purposes: a base Bernoulli variable (or a constant), so that distinct literals have disjoint supports and an AND over them is decomposable.
Only consulted when certifying() is true.
false by default. Reimplemented in semiring::BoolExpr.
Definition at line 418 of file Semiring.h.
|
inlinevirtual |
Evaluate a Bernoulli mixture (gate_mixture, three wires).
| p | The Bernoulli event's value. |
| x | The value taken when the event holds. |
| y | The value taken otherwise. |
| SemiringException | if not overridden. |
Reimplemented in semiring::Formula.
Definition at line 267 of file Semiring.h.
|
pure virtual |
Apply the monus (m-semiring difference) operation.
| x | Minuend. |
| y | Subtrahend. |
Implemented in semiring::Boolean, semiring::BoolExpr, semiring::Counting, semiring::Formula, semiring::How, semiring::IntervalUnion, semiring::Lukasiewicz, semiring::MinMax, semiring::Tropical, semiring::Viterbi, semiring::Which, and semiring::Why.
|
inlinevirtual |
Evaluate a latent-variable observation (gate_observe).
| child | The observed leaf's value. |
| datum | The observed value, as text. |
| SemiringException | if not overridden. |
Reimplemented in semiring::Formula.
Definition at line 319 of file Semiring.h.
|
pure virtual |
Return the multiplicative identity \(\mathbb{1}\).
Implemented in semiring::Boolean, semiring::BoolExpr, semiring::Counting, semiring::Formula, semiring::How, semiring::IntervalUnion, semiring::Lukasiewicz, semiring::MinMax, semiring::Tropical, semiring::Viterbi, semiring::Which, and semiring::Why.

|
pure virtual |
Apply the additive operation to a list of values.
| v | Ordered list of operands (empty list should return zero()). |
Implemented in semiring::Boolean, semiring::BoolExpr, semiring::Counting, semiring::Formula, semiring::How, semiring::IntervalUnion, semiring::Lukasiewicz, semiring::MinMax, semiring::Tropical, semiring::Viterbi, semiring::Which, and semiring::Why.
|
inlinevirtual |
Evaluate a continuous random-variable leaf (gate_rv).
| spec | The gate's distribution encoding ("normal:2.5,0.5"), where a parameter written "$i" is wired rather than literal (a latent / compound leaf). |
| params | The values of the gate's wires, indexed as the "$i" references; empty for an all-literal leaf. |
| SemiringException | if not overridden. |
Reimplemented in semiring::Formula.
Definition at line 227 of file Semiring.h.
|
inlinevirtual |
Apply a semimodule scalar multiplication.
| x | Provenance value. |
| s | Scalar value. |
| SemiringException | if not overridden. |
Reimplemented in semiring::Formula.
Definition at line 168 of file Semiring.h.
|
pure virtual |
Apply the multiplicative operation to a list of values.
| v | Ordered list of operands (empty list should return one()). |
Implemented in semiring::Boolean, semiring::BoolExpr, semiring::Counting, semiring::Formula, semiring::How, semiring::IntervalUnion, semiring::Lukasiewicz, semiring::MinMax, semiring::Tropical, semiring::Viterbi, semiring::Which, and semiring::Why.
|
inlinevirtual |
Value of a variable leaf (gate_input / gate_mulinput) that the provenance mapping does not name.
The absent-mapping convention: such a leaf contributes no provenance, i.e. the multiplicative identity, which is what every proper semiring wants and what this default returns. A semiring that renders the circuit rather than evaluating it overrides this to identify the leaf instead – \(\mathbb{1}\) would not merely be anonymous there, it would be absorbed by the enclosing times and take the structure with it. BooleanCircuit::toString does the same for the Boolean-expression rendering (its x<id> fallback).
| uuid | The leaf gate's UUID, in canonical text form. |
Reimplemented in semiring::Formula.
Definition at line 212 of file Semiring.h.

|
inlinevirtual |
Interpret a literal string as a semiring value.
Used for gate_value gates whose payload is a string.
| s | Literal string. |
| SemiringException | if not overridden. |
Reimplemented in semiring::Formula.
Definition at line 193 of file Semiring.h.
|
pure virtual |
Return the additive identity \(\mathbb{0}\).
Implemented in semiring::Boolean, semiring::BoolExpr, semiring::Counting, semiring::Formula, semiring::How, semiring::IntervalUnion, semiring::Lukasiewicz, semiring::MinMax, semiring::Tropical, semiring::Viterbi, semiring::Which, and semiring::Why.