ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Formula.h
Go to the documentation of this file.
1/**
2 * @file semiring/Formula.h
3 * @brief Symbolic formula semiring producing readable provenance expressions.
4 *
5 * The @c Formula semiring (@c std::string, @f$\oplus@f$, @f$\otimes@f$,
6 * "𝟘", "𝟙") represents provenance as a human-readable symbolic
7 * expression using Unicode semiring symbols. It is primarily used for
8 * debugging and testing.
9 *
10 * Each gate evaluates to a string:
11 * - @c zero() → "𝟘"
12 * - @c one() → "𝟙"
13 * - @c plus() → "(a ⊕ b ⊕ 
)" or just "a" for singletons
14 * - @c times() → "(a ⊗ b ⊗ 
)" or just "a" for singletons
15 * - @c monus() → "(a ⊖ b)"
16 * - @c delta() → "ή(a)" or "ήa" if @c a starts with @c (
17 * - @c cmp() → "[s1 op s2]"
18 * - @c semimod()→ "x*s"
19 * - @c agg() → operator-specific notation (e.g., "min(a,b)")
20 * - @c value() → the literal string itself
21 */
22#ifndef FORMULA_H
23#define FORMULA_H
24
25#include <numeric>
26#include <vector>
27#include <string>
28#include <sstream>
29#include <iterator>
30
31#include "Semiring.h"
32
33/**
34 * @brief Concatenate elements of a range with a delimiter.
35 *
36 * Used internally by @c Formula::plus(), @c Formula::times(), and
37 * @c Formula::agg() to build operator-separated strings.
38 *
39 * @tparam Range Any range type with a @c value_type typedef.
40 * @tparam Value Element type (defaults to @c Range::value_type).
41 * @param elements The range to join.
42 * @param delimiter String to insert between adjacent elements.
43 * @return All elements concatenated with @p delimiter between them.
44 */
45template <typename Range, typename Value = typename Range::value_type>
46static std::string join(Range const& elements, const char *const delimiter) {
47 std::ostringstream os;
48 auto b = begin(elements), e = end(elements);
49
50 if (b != e) {
51 std::copy(b, prev(e), std::ostream_iterator<Value>(os, delimiter));
52 b = prev(e);
53 }
54 if (b != e) {
55 os << *b;
56 }
57
58 return os.str();
59}
60
61namespace semiring {
62/**
63 * @brief Symbolic provenance formula semiring over @c std::string.
64 *
65 * Evaluates circuits to human-readable Unicode provenance formulas.
66 * Supports all optional operations (@c cmp, @c semimod, @c agg,
67 * @c value) in addition to the mandatory ones.
68 */
69class Formula : public semiring::Semiring<std::string>
70{
71public:
72virtual value_type zero() const override {
73 return "𝟘";
74}
75virtual value_type one() const override {
76 return "𝟙";
77}
78virtual value_type plus(const std::vector<value_type> &v) const override {
79 if(v.size()==0)
80 return zero();
81 else if(v.size()==1)
82 return v[0];
83 else
84 return "("+join(v, " ⊕ ")+")";
85}
86virtual value_type times(const std::vector<value_type> &v) const override {
87 if(v.size()==0)
88 return one();
89 else if(v.size()==1)
90 return v[0];
91 else
92 return "("+join(v, " ⊗ ")+")";
93}
94virtual value_type monus(value_type x, value_type y) const override
95{
96 return "("+x+" ⊖ "+y+")";
97}
98virtual value_type delta(value_type x) const override
99{
100 if(x[0]=='(')
101 return "ÎŽ"+x;
102 else
103 return "ÎŽ("+x+")";
104}
105virtual value_type cmp(value_type s1, ComparisonOperator op, value_type s2) const override {
106 std::string result = "["+s1+" ";
107 switch(op) {
109 result+="=";
110 break;
112 result+="≠";
113 break;
115 result+="≀";
116 break;
118 result+="<";
119 break;
121 result+="≥";
122 break;
124 result+=">";
125 break;
126 }
127 return result+" "+s2+"]";
128}
129virtual value_type semimod(value_type x, value_type s) const override {
130 return x + "*" + s;
131}
132virtual value_type agg(AggregationOperator op, const std::vector<std::string> &s) override {
134 return "<>";
135
136 if(s.empty()) {
137 switch(op) {
140 return "0";
142 return "+∞";
144 return "-∞";
147 return "<>";
149 return "⊀";
151 return "⊥";
153 return "[]";
155 assert(false);
156 }
157 }
158
159 std::string result;
160 switch(op) {
162 result+="[";
163 break;
165 result+="min(";
166 break;
168 result+="max(";
169 break;
171 result+="avg(";
172 break;
174 result+="choose(";
175 break;
176 default:
177 ;
178 }
179
180 result += s[0];
181
182 for(size_t i = 1; i<s.size(); ++i) {
183 switch(op) {
186 result+="+";
187 break;
193 result+=",";
194 break;
196 result+="√";
197 break;
199 result+="∧";
200 break;
202 assert(false);
203 }
204 result+=s[i];
205 }
207 result+="]";
208 else if(op==AggregationOperator::MIN ||
212 result+=")";
213 return result;
214}
215virtual value_type value(const std::string &s) const override {
216 return s;
217}
218};
219}
220
221#endif /* FORMULA_H */
AggregationOperator
SQL aggregation functions tracked by ProvSQL.
Definition Aggregation.h:50
@ OR
Boolean OR aggregate.
@ MAX
MAX → input type.
@ COUNT
COUNT(*) or COUNT(expr) → integer.
@ AND
Boolean AND aggregate.
@ SUM
SUM → integer or float.
@ ARRAY_AGG
Array aggregation.
@ NONE
No aggregation (returns NULL)
@ MIN
MIN → input type.
@ CHOOSE
Arbitrary selection (pick one element)
@ AVG
AVG → float.
ComparisonOperator
SQL comparison operators used in gate_cmp circuit gates.
Definition Aggregation.h:38
@ LT
Less than (<)
@ GT
Greater than (>)
@ LE
Less than or equal (<=)
@ NE
Not equal (<>)
@ GE
Greater than or equal (>=)
static std::string join(Range const &elements, const char *const delimiter)
Concatenate elements of a range with a delimiter.
Definition Formula.h:46
Abstract semiring interface for provenance evaluation.
Symbolic provenance formula semiring over std::string.
Definition Formula.h:70
virtual value_type zero() const override
Return the additive identity .
Definition Formula.h:72
virtual value_type cmp(value_type s1, ComparisonOperator op, value_type s2) const override
Evaluate a comparison gate.
Definition Formula.h:105
virtual value_type semimod(value_type x, value_type s) const override
Apply a semimodule scalar multiplication.
Definition Formula.h:129
virtual value_type agg(AggregationOperator op, const std::vector< std::string > &s) override
Evaluate an aggregation gate.
Definition Formula.h:132
virtual value_type times(const std::vector< value_type > &v) const override
Apply the multiplicative operation to a list of values.
Definition Formula.h:86
virtual value_type monus(value_type x, value_type y) const override
Apply the monus (m-semiring difference) operation.
Definition Formula.h:94
virtual value_type one() const override
Return the multiplicative identity .
Definition Formula.h:75
virtual value_type delta(value_type x) const override
Apply the operator.
Definition Formula.h:98
virtual value_type value(const std::string &s) const override
Interpret a literal string as a semiring value.
Definition Formula.h:215
virtual value_type plus(const std::vector< value_type > &v) const override
Apply the additive operation to a list of values.
Definition Formula.h:78
Abstract base class for (m-)semirings.
Definition Semiring.h:84
std::string value_type
The carrier type of this semiring.
Definition Semiring.h:87