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#ifndef SEMIRING_H
28#define SEMIRING_H
29
30#include <vector>
31#include <string>
32
33#include "../Aggregation.h"
34
35namespace semiring {
36
37/**
38 * @brief Exception thrown when a semiring operation is not supported.
39 *
40 * Raised by the default implementations of optional operations
41 * (@c cmp, @c semimod, @c agg, @c value) when a subclass does not
42 * override them.
43 */
44class SemiringException : public std::exception
45{
46std::string message; ///< Human-readable description of the error
47
48public:
49/**
50 * @brief Construct with a descriptive error message.
51 * @param m Error message.
52 */
53SemiringException(const std::string &m) : message(m) {
54}
55/**
56 * @brief Return the error message as a C-string.
57 * @return Null-terminated error description.
58 */
59virtual char const * what() const noexcept {
60 return message.c_str();
61}
62};
63
64/**
65 * @brief Abstract base class for (m-)semirings.
66 *
67 * @tparam V The carrier type (e.g. @c bool, @c unsigned, @c std::string).
68 *
69 * ### Required operations
70 * All pure-virtual methods must be implemented by concrete subclasses.
71 *
72 * ### Optional operations
73 * @c cmp(), @c semimod(), @c agg(), and @c value() have default
74 * implementations that throw @c SemiringException. Override them in
75 * subclasses that support these circuit gate types.
76 *
77 * ### Absorptive semirings
78 * A semiring is *absorptive* (i.e., @f$a \oplus a = a@f$ for all @f$a@f$)
79 * iff @c absorptive() returns @c true. This allows the circuit evaluator
80 * to deduplicate children of @c plus gates for efficiency.
81 */
82template<typename V>
84{
85public:
86/** @brief The carrier type of this semiring. */
87typedef V value_type;
88
89/**
90 * @brief Return the additive identity @f$\mathbb{0}@f$.
91 * @return The zero element of the semiring.
92 */
93virtual value_type zero() const = 0;
94
95/**
96 * @brief Return the multiplicative identity @f$\mathbb{1}@f$.
97 * @return The one element of the semiring.
98 */
99virtual value_type one() const = 0;
100
101/**
102 * @brief Apply the additive operation to a list of values.
103 *
104 * @param v Ordered list of operands (empty list should return @c zero()).
105 * @return @f$v_0 \oplus v_1 \oplus \cdots@f$.
106 */
107virtual value_type plus(const std::vector<value_type> &v) const = 0;
108
109/**
110 * @brief Apply the multiplicative operation to a list of values.
111 *
112 * @param v Ordered list of operands (empty list should return @c one()).
113 * @return @f$v_0 \otimes v_1 \otimes \cdots@f$.
114 */
115virtual value_type times(const std::vector<value_type> &v) const = 0;
116
117/**
118 * @brief Apply the monus (m-semiring difference) operation.
119 *
120 * @param x Minuend.
121 * @param y Subtrahend.
122 * @return @f$x \ominus y@f$.
123 */
124virtual value_type monus(value_type x, value_type y) const = 0;
125
126/**
127 * @brief Apply the @f$\delta@f$ operator.
128 *
129 * @param x Input value.
130 * @return @f$\delta(x)@f$.
131 */
132virtual value_type delta(value_type x) const = 0;
133
134/**
135 * @brief Evaluate a comparison gate.
136 *
137 * @param s1 Left operand.
138 * @param op Comparison operator.
139 * @param s2 Right operand.
140 * @return Result of the comparison in this semiring.
141 * @throws SemiringException if not overridden.
142 */
144 throw SemiringException("This semiring does not support cmp gates.");
145}
146
147/**
148 * @brief Apply a semimodule scalar multiplication.
149 *
150 * @param x Provenance value.
151 * @param s Scalar value.
152 * @return @f$x * s@f$ in the semimodule.
153 * @throws SemiringException if not overridden.
154 */
156 throw SemiringException("This semiring does not support semimod gates.");
157}
158
159/**
160 * @brief Evaluate an aggregation gate.
161 *
162 * @param op The aggregation function (COUNT, SUM, MIN, …).
163 * @param s List of child semiring values to aggregate.
164 * @return The aggregated value.
165 * @throws SemiringException if not overridden.
166 */
167virtual value_type agg(AggregationOperator op, const std::vector<value_type> &s) {
168 throw SemiringException("This semiring does not support agg gates.");
169}
170
171/**
172 * @brief Interpret a literal string as a semiring value.
173 *
174 * Used for @c gate_value gates whose payload is a string.
175 *
176 * @param s Literal string.
177 * @return The corresponding semiring value.
178 * @throws SemiringException if not overridden.
179 */
180virtual value_type value(const std::string &s) const {
181 throw SemiringException("This semiring does not support value gates.");
182}
183
184virtual ~Semiring() = default;
185
186/**
187 * @brief Return @c true if this semiring is absorptive (@f$a \oplus a = a@f$).
188 *
189 * When @c true, the circuit evaluator may deduplicate the children of
190 * @c plus gates, which can improve performance significantly for
191 * semirings such as Boolean and Why-provenance.
192 *
193 * @return @c false by default; override to return @c true.
194 */
195virtual bool absorptive() const {
196 return false;
197}
198};
199
200
201}
202
203#endif /* SEMIRING_H */
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:45
std::string message
Human-readable description of the error.
Definition Semiring.h:46
virtual char const * what() const noexcept
Return the error message as a C-string.
Definition Semiring.h:59
SemiringException(const std::string &m)
Construct with a descriptive error message.
Definition Semiring.h:53
Abstract base class for (m-)semirings.
Definition Semiring.h:84
V value_type
The carrier type of this semiring.
Definition Semiring.h:87
virtual value_type semimod(value_type x, value_type s) const
Apply a semimodule scalar multiplication.
Definition Semiring.h:155
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 ( ).
Definition Semiring.h:195
virtual ~Semiring()=default
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:143
virtual value_type agg(AggregationOperator op, const std::vector< value_type > &s)
Evaluate an aggregation gate.
Definition Semiring.h:167
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:180