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 * @see https://provsql.org/lean-docs/Provenance/Semirings/BoolFunc.html
25 * Lean 4 verified instance (@c BoolFunc): the algebraic counterpart
26 * of Boolean circuits, with proofs of @c BoolFunc.absorptive,
27 * @c BoolFunc.idempotent, and @c BoolFunc.mul_sub_left_distributive.
28 */
29#ifndef BOOLEXPR_H
30#define BOOLEXPR_H
31
32#include <set>
33#include <string>
34#include <vector>
35#include <algorithm>
36#include "Semiring.h"
37#include "../BooleanCircuit.h"
38
39namespace semiring {
40/**
41 * @brief Provenance-as-Boolean-circuit semiring.
42 *
43 * The carrier type is @c gate_t (a gate identifier in @c BooleanCircuit).
44 * Evaluating the provenance circuit over this semiring constructs a new
45 * Boolean circuit expressing the provenance formula.
46 */
47class BoolExpr : public Semiring<gate_t> {
48using value_t = gate_t; ///< Carrier type: a gate ID in the target BooleanCircuit
49
50BooleanCircuit &c; ///< The Boolean circuit being constructed
51const gate_t ZERO; ///< Pre-allocated zero gate (OR with no children)
52const gate_t ONE; ///< Pre-allocated one gate (AND with no children)
53
54public:
55/**
56 * @brief Construct a BoolExpr semiring over the given circuit.
57 * @param bc The Boolean circuit in which semiring operations create new gates.
58 */
59BoolExpr(BooleanCircuit &bc) : c(bc), ZERO(c.setGate(BooleanGate::OR)), ONE(c.setGate(BooleanGate::AND)) {
60}
61
62value_type zero() const override {
63 return ZERO;
64}
65
66value_type one() const override {
67 return ONE;
68}
69
70value_type plus(const std::vector<value_type> &vec) const override {
71 if(vec.empty())
72 return ZERO;
73
74 auto g = c.setGate(BooleanGate::OR);
75
76 std::set<gate_t> seen;
77 for (const auto &h : vec) {
78 if(seen.find(h)!=seen.end())
79 continue;
80 seen.insert(h);
81 c.addWire(g, h);
82 }
83 return g;
84}
85
86value_type times(const std::vector<value_type> &vec) const override {
87 if(vec.empty())
88 return ONE;
89
90 auto g = c.setGate(BooleanGate::AND);
91
92 std::set<gate_t> seen;
93 for (const auto &h : vec) {
94 if(seen.find(h)!=seen.end())
95 continue;
96 seen.insert(h);
97 c.addWire(g, h);
98 }
99 return g;
100}
101
102virtual value_type monus(value_type x, value_type y) const override {
103 auto g2 = c.setGate(BooleanGate::NOT);
104 c.addWire(g2,y);
105
106 if(x==ONE)
107 return g2;
108 else {
109 auto g1 = c.setGate(BooleanGate::AND);
110 c.addWire(g1,x);
111 c.addWire(g1,g2);
112 return g1;
113 }
114}
115
116value_type delta(value_type x) const override {
117 return x;
118}
119
120virtual bool absorptive() const override {
121 return true;
122}
123/**
124 * @brief @c BoolExpr is the free Boolean-circuit construction; the
125 * evaluation map to @c Bool at any valuation is an m-semiring
126 * homomorphism, so the safe-query Boolean rewrite preserves
127 * semantics. Lean: @c BoolFunc is the algebraic counterpart;
128 * the relevant theorems live in
129 * @c Provenance.Semirings.BoolFunc and
130 * @c Provenance.Semirings.Bool.homomorphism_to_BoolFunc.
131 */
132virtual bool compatibleWithBooleanRewrite() const override {
133 return true;
134}
135
136};
137}
138
139#endif // BOOLEXPR_H
@ OR
Boolean OR aggregate.
Definition Aggregation.h:57
@ AND
Boolean AND aggregate.
Definition Aggregation.h:56
Boolean provenance circuit with support for knowledge compilation.
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:49
Abstract semiring interface for provenance evaluation.
Boolean circuit for provenance formula evaluation.
BoolExpr(BooleanCircuit &bc)
Construct a BoolExpr semiring over the given circuit.
Definition BoolExpr.h:59
value_type plus(const std::vector< value_type > &vec) const override
Apply the additive operation to a list of values.
Definition BoolExpr.h:70
const gate_t ZERO
Pre-allocated zero gate (OR with no children).
Definition BoolExpr.h:51
value_type delta(value_type x) const override
Apply the operator.
Definition BoolExpr.h:116
value_type one() const override
Return the multiplicative identity .
Definition BoolExpr.h:66
virtual value_type monus(value_type x, value_type y) const override
Apply the monus (m-semiring difference) operation.
Definition BoolExpr.h:102
BooleanCircuit & c
The Boolean circuit being constructed.
Definition BoolExpr.h:50
value_type zero() const override
Return the additive identity .
Definition BoolExpr.h:62
virtual bool compatibleWithBooleanRewrite() const override
BoolExpr is the free Boolean-circuit construction; the evaluation map to Bool at any valuation is an ...
Definition BoolExpr.h:132
gate_t value_t
Carrier type: a gate ID in the target BooleanCircuit.
Definition BoolExpr.h:48
const gate_t ONE
Pre-allocated one gate (AND with no children).
Definition BoolExpr.h:52
virtual bool absorptive() const override
Return true if this semiring is absorptive ( for all ).
Definition BoolExpr.h:120
value_type times(const std::vector< value_type > &vec) const override
Apply the multiplicative operation to a list of values.
Definition BoolExpr.h:86
Abstract base class for (m-)semirings.
Definition Semiring.h:93