ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Semiring.h
Go to the documentation of this file.
1/**
2 * @file semiring/Semiring.h
3 * @brief Abstract semiring interface for provenance evaluation.
4 *
5 * ProvSQL evaluates provenance circuits over arbitrary (m-)semirings.
6 * This header defines the abstract base class @c semiring::Semiring<V>
7 * that every concrete semiring must implement.
8 *
9 * A **semiring** @f$(S, \oplus, \otimes, \mathbb{0}, \mathbb{1})@f$
10 * consists of:
11 * - A carrier set @f$S@f$ (the @c value_type).
12 * - An additive operation @f$\oplus@f$ with identity @f$\mathbb{0}@f$.
13 * - A multiplicative operation @f$\otimes@f$ with identity @f$\mathbb{1}@f$.
14 *
15 * An **m-semiring** additionally provides:
16 * - A monus operation @f$\ominus@f$ used for set-difference queries.
17 * - A @f$\delta@f$ operator.
18 *
19 * Optional operations (comparison, semimodule scalar multiplication,
20 * aggregation, and value literals) are provided by subclasses that
21 * support them; the base class throws @c SemiringException for all of
22 * these.
23 *
24 * Concrete implementations live in the same @c semiring/ directory:
25 * @c Boolean.h, @c Counting.h, @c Formula.h, @c Why.h, @c BoolExpr.h.
26 *
27 * @see https://provsql.org/lean-docs/Provenance/SemiringWithMonus.html
28 * Lean 4 formalization of the @c SemiringWithMonus typeclass and
29 * proofs of the key monus identities (@c monus_smallest,
30 * @c monus_self, @c zero_monus, @c monus_add, @c add_monus,
31 * @c idempotent_iff_add_monus).
32 */
33#ifndef SEMIRING_H
34#define SEMIRING_H
35
36#include <vector>
37#include <string>
38
39#include "../Aggregation.h"
40
41namespace semiring {
42
43/**
44 * @brief Exception thrown when a semiring operation is not supported.
45 *
46 * Raised by the default implementations of optional operations
47 * (@c cmp, @c semimod, @c agg, @c value) when a subclass does not
48 * override them.
49 */
50class SemiringException : public std::exception
51{
52std::string message; ///< Human-readable description of the error
53
54public:
55/**
56 * @brief Construct with a descriptive error message.
57 * @param m Error message.
58 */
59SemiringException(const std::string &m) : message(m) {
60}
61/**
62 * @brief Return the error message as a C-string.
63 * @return Null-terminated error description.
64 */
65virtual char const * what() const noexcept {
66 return message.c_str();
67}
68};
69
70/**
71 * @brief Abstract base class for (m-)semirings.
72 *
73 * @tparam V The carrier type (e.g. @c bool, @c unsigned, @c std::string).
74 *
75 * ### Required operations
76 * All pure-virtual methods must be implemented by concrete subclasses.
77 *
78 * ### Optional operations
79 * @c cmp(), @c semimod(), @c agg(), and @c value() have default
80 * implementations that throw @c SemiringException. Override them in
81 * subclasses that support these circuit gate types.
82 *
83 * ### Absorptive semirings
84 * A semiring is *absorptive* (i.e.,
85 * @f$\mathbb{1} \oplus a = \mathbb{1}@f$ for all @f$a@f$) iff
86 * @c absorptive() returns @c true. Absorptivity implies idempotency
87 * (@f$a \oplus a = a@f$), which lets the circuit evaluator and the
88 * HAVING-semantics machinery deduplicate operands and short-circuit
89 * over the multiplicative identity.
90 */
91template<typename V>
93{
94public:
95/** @brief The carrier type of this semiring. */
96typedef V value_type;
97
98/**
99 * @brief Return the additive identity @f$\mathbb{0}@f$.
100 * @return The zero element of the semiring.
101 */
102virtual value_type zero() const = 0;
103
104/**
105 * @brief Return the multiplicative identity @f$\mathbb{1}@f$.
106 * @return The one element of the semiring.
107 */
108virtual value_type one() const = 0;
109
110/**
111 * @brief Apply the additive operation to a list of values.
112 *
113 * @param v Ordered list of operands (empty list should return @c zero()).
114 * @return @f$v_0 \oplus v_1 \oplus \cdots@f$.
115 */
116virtual value_type plus(const std::vector<value_type> &v) const = 0;
117
118/**
119 * @brief Apply the multiplicative operation to a list of values.
120 *
121 * @param v Ordered list of operands (empty list should return @c one()).
122 * @return @f$v_0 \otimes v_1 \otimes \cdots@f$.
123 */
124virtual value_type times(const std::vector<value_type> &v) const = 0;
125
126/**
127 * @brief Apply the monus (m-semiring difference) operation.
128 *
129 * @param x Minuend.
130 * @param y Subtrahend.
131 * @return @f$x \ominus y@f$.
132 */
133virtual value_type monus(value_type x, value_type y) const = 0;
134
135/**
136 * @brief Apply the @f$\delta@f$ operator.
137 *
138 * @param x Input value.
139 * @return @f$\delta(x)@f$.
140 */
141virtual value_type delta(value_type x) const = 0;
142
143/**
144 * @brief Evaluate a comparison gate.
145 *
146 * @param s1 Left operand.
147 * @param op Comparison operator.
148 * @param s2 Right operand.
149 * @return Result of the comparison in this semiring.
150 * @throws SemiringException if not overridden.
151 */
153 throw SemiringException("This semiring does not support cmp gates.");
154}
155
156/**
157 * @brief Apply a semimodule scalar multiplication.
158 *
159 * @param x Provenance value.
160 * @param s Scalar value.
161 * @return @f$x * s@f$ in the semimodule.
162 * @throws SemiringException if not overridden.
163 */
165 throw SemiringException("This semiring does not support semimod gates.");
166}
167
168/**
169 * @brief Evaluate an aggregation gate.
170 *
171 * @param op The aggregation function (COUNT, SUM, MIN, …).
172 * @param s List of child semiring values to aggregate.
173 * @return The aggregated value.
174 * @throws SemiringException if not overridden.
175 */
176virtual value_type agg(AggregationOperator op, const std::vector<value_type> &s) {
177 throw SemiringException("This semiring does not support agg gates.");
178}
179
180/**
181 * @brief Interpret a literal string as a semiring value.
182 *
183 * Used for @c gate_value gates whose payload is a string.
184 *
185 * @param s Literal string.
186 * @return The corresponding semiring value.
187 * @throws SemiringException if not overridden.
188 */
189virtual value_type value(const std::string &s) const {
190 throw SemiringException("This semiring does not support value gates.");
191}
192
193virtual ~Semiring() = default;
194
195/**
196 * @brief Return @c true if this semiring is absorptive
197 * (@f$\mathbb{1} \oplus a = \mathbb{1}@f$ for all @f$a@f$).
198 *
199 * When @c true, the circuit evaluator and HAVING-semantics machinery
200 * may exploit the resulting idempotency (@f$a \oplus a = a@f$, implied
201 * by absorptivity) to deduplicate children of @c plus gates and to
202 * short-circuit over the multiplicative identity.
203 *
204 * @return @c false by default; override to return @c true.
205 */
206virtual bool absorptive() const {
207 return false;
208}
209
210/**
211 * @brief Return @c true if a semiring homomorphism @c BoolFunc(X)
212 * →+* @c S exists, so the safe-query (Boolean-rewrite)
213 * optimisation produces circuits that are semantically faithful
214 * when evaluated under this semiring.
215 *
216 * The compiled-semiring dispatcher consults this predicate before
217 * evaluating a circuit whose root gate carries
218 * @c PROVSQL_ROOT_TAG_BOOLEAN_REWRITE. Returning @c false on a tagged
219 * circuit raises @c CircuitException.
220 *
221 * Defaults to @c false: a new semiring whose author has not yet
222 * verified the homomorphism is fail-closed by construction.
223 * Subclasses with a verified homomorphism (currently @c Boolean,
224 * @c BoolExpr, @c Formula, and @c IntervalUnion) override to return
225 * @c true. The justification (Lean-proof reference) belongs in a
226 * comment next to each override; see the @c src/semiring/ headers.
227 */
228virtual bool compatibleWithBooleanRewrite() const {
229 return false;
230}
231};
232
233
234}
235
236#endif /* SEMIRING_H */
Typed aggregation value, operator, and aggregator abstractions.
AggregationOperator
SQL aggregation functions tracked by ProvSQL.
Definition Aggregation.h:50
ComparisonOperator
SQL comparison operators used in gate_cmp circuit gates.
Definition Aggregation.h:38
Exception thrown when a semiring operation is not supported.
Definition Semiring.h:51
std::string message
Human-readable description of the error.
Definition Semiring.h:52
virtual char const * what() const noexcept
Return the error message as a C-string.
Definition Semiring.h:65
SemiringException(const std::string &m)
Construct with a descriptive error message.
Definition Semiring.h:59
Abstract base class for (m-)semirings.
Definition Semiring.h:93
V value_type
The carrier type of this semiring.
Definition Semiring.h:96
virtual value_type semimod(value_type x, value_type s) const
Apply a semimodule scalar multiplication.
Definition Semiring.h:164
virtual value_type plus(const std::vector< value_type > &v) const =0
Apply the additive operation to a list of values.
virtual bool absorptive() const
Return true if this semiring is absorptive ( for all ).
Definition Semiring.h:206
virtual ~Semiring()=default
virtual bool compatibleWithBooleanRewrite() const
Return true if a semiring homomorphism BoolFunc(X) →+* S exists, so the safe-query (Boolean-rewrite) ...
Definition Semiring.h:228
virtual value_type zero() const =0
Return the additive identity .
virtual value_type cmp(value_type s1, ComparisonOperator op, value_type s2) const
Evaluate a comparison gate.
Definition Semiring.h:152
virtual value_type agg(AggregationOperator op, const std::vector< value_type > &s)
Evaluate an aggregation gate.
Definition Semiring.h:176
virtual value_type one() const =0
Return the multiplicative identity .
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 operator.
virtual value_type times(const std::vector< value_type > &v) const =0
Apply the multiplicative operation to a list of values.
virtual value_type value(const std::string &s) const
Interpret a literal string as a semiring value.
Definition Semiring.h:189