ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
BoolExpr.h
Go to the documentation of this file.
1/**
2 * @file semiring/BoolExpr.h
3 * @brief Boolean-expression (lineage formula) semiring.
4 *
5 * The @c BoolExpr semiring represents provenance as a Boolean circuit
6 * rather than a scalar value. Each semiring value is a @c gate_t
7 * identifier inside a shared @c BooleanCircuit. The semiring operations
8 * create new gates in that circuit:
9 *
10 * - @c zero() → a fresh OR gate with no children (always @c false)
11 * - @c one() → a fresh AND gate with no children (always @c true)
12 * - @c plus() → an OR gate whose children are the operands
13 * - @c times() → an AND gate whose children are the operands
14 * - @c monus() → an AND gate combining @f$x@f$ with a NOT of @f$y@f$
15 * - @c delta() → identity
16 *
17 * The result of evaluating a circuit over this semiring is the root gate
18 * of a new Boolean circuit that encodes the provenance formula. This
19 * circuit can then be compiled to a d-DNNF for probability computation.
20 *
21 * The semiring is absorptive: duplicate children of OR/AND gates are
22 * deduplicated during gate construction.
23 */
24#ifndef BOOLEXPR_H
25#define BOOLEXPR_H
26
27#include <set>
28#include <string>
29#include <vector>
30#include <algorithm>
31#include "Semiring.h"
32#include "../BooleanCircuit.h"
33
34namespace semiring {
35/**
36 * @brief Provenance-as-Boolean-circuit semiring.
37 *
38 * The carrier type is @c gate_t (a gate identifier in @c BooleanCircuit).
39 * Evaluating the provenance circuit over this semiring constructs a new
40 * Boolean circuit expressing the provenance formula.
41 */
42class BoolExpr : public Semiring<gate_t> {
43using value_t = gate_t; ///< Carrier type: a gate ID in the target BooleanCircuit
44
45BooleanCircuit &c; ///< The Boolean circuit being constructed
46const gate_t ZERO; ///< Pre-allocated zero gate (OR with no children)
47const gate_t ONE; ///< Pre-allocated one gate (AND with no children)
48
49public:
50/**
51 * @brief Construct a BoolExpr semiring over the given circuit.
52 * @param bc The Boolean circuit in which semiring operations create new gates.
53 */
54BoolExpr(BooleanCircuit &bc) : c(bc), ZERO(c.setGate(BooleanGate::OR)), ONE(c.setGate(BooleanGate::AND)) {
55}
56
57value_type zero() const override {
58 return ZERO;
59}
60
61value_type one() const override {
62 return ONE;
63}
64
65value_type plus(const std::vector<value_type> &vec) const override {
66 if(vec.empty())
67 return ZERO;
68
69 auto g = c.setGate(BooleanGate::OR);
70
71 std::set<gate_t> seen;
72 for (const auto &h : vec) {
73 if(seen.find(h)!=seen.end())
74 continue;
75 seen.insert(h);
76 c.addWire(g, h);
77 }
78 return g;
79}
80
81value_type times(const std::vector<value_type> &vec) const override {
82 if(vec.empty())
83 return ONE;
84
85 auto g = c.setGate(BooleanGate::AND);
86
87 std::set<gate_t> seen;
88 for (const auto &h : vec) {
89 if(seen.find(h)!=seen.end())
90 continue;
91 seen.insert(h);
92 c.addWire(g, h);
93 }
94 return g;
95}
96
97virtual value_type monus(value_type x, value_type y) const override {
98 auto g2 = c.setGate(BooleanGate::NOT);
99 c.addWire(g2,y);
100
101 if(x==ONE)
102 return g2;
103 else {
104 auto g1 = c.setGate(BooleanGate::AND);
105 c.addWire(g1,x);
106 c.addWire(g1,g2);
107 return g1;
108 }
109}
110
111value_type delta(value_type x) const override {
112 return x;
113}
114
115virtual bool absorptive() const override {
116 return true;
117}
118
119};
120}
121
122#endif // BOOLEXPR_H
@ OR
Boolean OR aggregate.
@ AND
Boolean AND aggregate.
BooleanGate
Gate types for a Boolean provenance circuit.
@ NOT
Logical negation of a single child gate.
@ OR
Logical disjunction of child gates.
@ AND
Logical conjunction of child gates.
gate_t
Strongly-typed gate identifier.
Definition Circuit.h:48
Abstract semiring interface for provenance evaluation.
Boolean circuit for provenance formula evaluation.
gate_t setGate(BooleanGate type) override
Allocate a new gate with type type and no UUID.
void addWire(gate_t f, gate_t t)
Add a directed wire from gate f (parent) to gate t (child).
Definition Circuit.hpp:81
Provenance-as-Boolean-circuit semiring.
Definition BoolExpr.h:42
BoolExpr(BooleanCircuit &bc)
Construct a BoolExpr semiring over the given circuit.
Definition BoolExpr.h:54
value_type plus(const std::vector< value_type > &vec) const override
Apply the additive operation to a list of values.
Definition BoolExpr.h:65
const gate_t ZERO
Pre-allocated zero gate (OR with no children)
Definition BoolExpr.h:46
value_type delta(value_type x) const override
Apply the operator.
Definition BoolExpr.h:111
value_type one() const override
Return the multiplicative identity .
Definition BoolExpr.h:61
virtual value_type monus(value_type x, value_type y) const override
Apply the monus (m-semiring difference) operation.
Definition BoolExpr.h:97
BooleanCircuit & c
The Boolean circuit being constructed.
Definition BoolExpr.h:45
value_type zero() const override
Return the additive identity .
Definition BoolExpr.h:57
const gate_t ONE
Pre-allocated one gate (AND with no children)
Definition BoolExpr.h:47
virtual bool absorptive() const override
Return true if this semiring is absorptive ( ).
Definition BoolExpr.h:115
value_type times(const std::vector< value_type > &vec) const override
Apply the multiplicative operation to a list of values.
Definition BoolExpr.h:81
Abstract base class for (m-)semirings.
Definition Semiring.h:84