ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Why.h
Go to the documentation of this file.
1/**
2 * @file semiring/Why.h
3 * @brief Why-provenance semiring (set of witness sets).
4 *
5 * The **why-provenance** semiring represents provenance as a set of
6 * *witness sets*. Each witness set is a set of base-tuple labels that
7 * together "witness" one derivation of the query result. The full
8 * provenance is the collection of all such witness sets.
9 *
10 * Formally, the carrier type is @f$\mathcal{P}(\mathcal{P}(\text{Labels}))@f$,
11 * implemented as @c why_provenance_t = @c std::set<std::set<std::string>>.
12 *
13 * Operations:
14 * - @c zero() → ∅ (no derivations)
15 * - @c one() → {∅} (one derivation requiring no witnesses)
16 * - @c plus() → union of input sets
17 * - @c times() → pairwise concatenation (Cartesian product of witnesses)
18 * - @c monus() → remove elements of @f$y@f$ from @f$x@f$
19 * - @c delta() → identity (returns @f$x@f$ unchanged if non-empty)
20 *
21 * This semiring is absorptive (set union is idempotent).
22 */
23#ifndef WHY_H
24#define WHY_H
25
26#include <set>
27#include <string>
28#include <vector>
29#include <algorithm>
30
31#include "Semiring.h"
32
33namespace semiring {
34
35/** @brief A single label identifying a base tuple. */
36using label_t = std::string;
37/** @brief A witness: a set of labels that collectively justify one derivation. */
38using label_set = std::set<label_t>;
39/** @brief Why-provenance value: the full set of all witnesses. */
40using why_provenance_t = std::set<label_set>;
41
42/**
43 * @brief Why-provenance semiring.
44 *
45 * Each gate evaluates to a @c why_provenance_t (set of witness sets).
46 */
47class Why : public Semiring<why_provenance_t> {
48public:
49// Additive identity
50value_type zero() const override {
51 return {};
52}
53
54// Multiplicative identity: empty set (⊗(x,{{}}) means "don't change")
55value_type one() const override {
56 return { {} };
57}
58
59// Union of all input sets
60value_type plus(const std::vector<value_type> &vec) const override {
61 value_type result;
62 for (const auto &v : vec) {
63 result.insert(v.begin(), v.end());
64 }
65 return result;
66}
67
68// Cartesian product: union each inner set with each other
69value_type times(const std::vector<value_type> &vec) const override {
70 if (vec.empty()) return one();
71
72 value_type result = vec[0];
73 for (size_t i = 1; i < vec.size(); ++i) {
74 value_type temp;
75 for (const auto &s1 : result) {
76 for (const auto &s2 : vec[i]) {
77 label_set combined = s1;
78 combined.insert(s2.begin(), s2.end());
79 temp.insert(std::move(combined));
80 }
81 }
82 result = std::move(temp);
83 }
84 return result;
85}
86
87
88virtual value_type monus(value_type x, value_type y) const override {
89 for (auto const &s : y) {
90 x.erase(s);
91 }
92 return x;
93}
94
95value_type delta(value_type x) const override {
96 return x.empty() ? zero() : x;
97}
98
99};
100
101}
102
103#endif // WHY_H
Abstract semiring interface for provenance evaluation.
Abstract base class for (m-)semirings.
Definition Semiring.h:84
why_provenance_t value_type
The carrier type of this semiring.
Definition Semiring.h:87
Why-provenance semiring.
Definition Why.h:47
value_type one() const override
Return the multiplicative identity .
Definition Why.h:55
value_type times(const std::vector< value_type > &vec) const override
Apply the multiplicative operation to a list of values.
Definition Why.h:69
value_type plus(const std::vector< value_type > &vec) const override
Apply the additive operation to a list of values.
Definition Why.h:60
virtual value_type monus(value_type x, value_type y) const override
Apply the monus (m-semiring difference) operation.
Definition Why.h:88
value_type zero() const override
Return the additive identity .
Definition Why.h:50
value_type delta(value_type x) const override
Apply the operator.
Definition Why.h:95
std::set< label_set > why_provenance_t
Why-provenance value: the full set of all witnesses.
Definition Why.h:40
std::string label_t
A single label identifying a base tuple.
Definition Why.h:36
std::set< label_t > label_set
A witness: a set of labels that collectively justify one derivation.
Definition Why.h:38