ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Boolean.h
Go to the documentation of this file.
1/**
2 * @file semiring/Boolean.h
3 * @brief Boolean semiring ({false, true}, ∨, ∧, false, true).
4 *
5 * The Boolean semiring is the simplest semiring supported by ProvSQL.
6 * Provenance evaluates to @c true if at least one derivation of the
7 * tuple exists in the current database instance (i.e., the query answer
8 * is "certain"), and @c false otherwise.
9 *
10 * The semiring is absorptive (∨ is idempotent), so the circuit evaluator
11 * can safely deduplicate children of OR gates.
12 *
13 * Operations:
14 * - @c zero() → @c false
15 * - @c one() → @c true
16 * - @c plus() → logical OR (any_of)
17 * - @c times() → logical AND (all_of)
18 * - @c monus() → @f$x \;\&\; \lnot y@f$
19 * - @c delta() → identity
20 *
21 * @see https://provsql.org/lean-docs/Provenance/Semirings/Bool.html
22 * Lean 4 verified instance: @c instSemiringWithMonusBool, with
23 * proofs of @c Bool.absorptive, @c Bool.idempotent, and
24 * @c Bool.mul_sub_left_distributive.
25 */
26#ifndef BOOLEAN_H
27#define BOOLEAN_H
28
29#include <algorithm>
30#include <vector>
31
32#include "Semiring.h"
33
34namespace semiring {
35/**
36 * @brief The Boolean semiring over @c bool.
37 *
38 * Provides the standard Boolean interpretation of provenance circuits.
39 */
40class Boolean : public semiring::Semiring<bool>
41{
42public:
43virtual value_type zero() const override {
44 return false;
45}
46virtual value_type one() const override {
47 return true;
48}
49virtual value_type plus(const std::vector<value_type> &v) const override {
50 return std::any_of(v.begin(), v.end(), [](bool x) {
51 return x;
52 });
53}
54virtual value_type times(const std::vector<value_type> &v) const override {
55 return std::all_of(v.begin(), v.end(), [](bool x) {
56 return x;
57 });
58}
59virtual value_type monus(value_type x, value_type y) const override
60{
61 return x & !y;
62}
63virtual value_type delta(value_type x) const override
64{
65 return x;
66}
67virtual bool absorptive() const override {
68 return true;
69}
70/**
71 * @brief @f$\otimes@f$ distributes over @f$\ominus@f$, so the HAVING
72 * @c MIN / @c MAX comparisons take the single-scan closed form.
73 *
74 * Lean: @c Provenance.Semirings.Bool.mul_sub_left_distributive.
75 */
76virtual bool mul_sub_left_distributive() const override {
77 return true;
78}
79/**
80 * @brief The identity map @c BoolFunc(X) →+* Bool (evaluating a free
81 * Boolean function at a valuation) is an m-semiring
82 * homomorphism, so the safe-query Boolean rewrite preserves
83 * evaluation results in this semiring.
84 *
85 * Lean: @c Provenance.Semirings.Bool.homomorphism_to_BoolFunc and
86 * @c Bool.homomorphism_from_BoolFunc
87 * (provenance-lean/Provenance/Semirings/Bool.lean).
88 */
89virtual bool compatibleWithBooleanRewrite() const override {
90 return true;
91}
92value_type parse_leaf(const char *v) const {
93 return *v != 'f' && *v != '0';
94}
95};
96}
97
98#endif /* BOOLEAN_H */
Abstract semiring interface for provenance evaluation.
The Boolean semiring over bool.
Definition Boolean.h:41
virtual value_type zero() const override
Return the additive identity .
Definition Boolean.h:43
virtual value_type times(const std::vector< value_type > &v) const override
Apply the multiplicative operation to a list of values.
Definition Boolean.h:54
virtual value_type plus(const std::vector< value_type > &v) const override
Apply the additive operation to a list of values.
Definition Boolean.h:49
virtual bool compatibleWithBooleanRewrite() const override
The identity map BoolFunc(X) →+* Bool (evaluating a free Boolean function at a valuation) is an m-sem...
Definition Boolean.h:89
virtual value_type one() const override
Return the multiplicative identity .
Definition Boolean.h:46
virtual bool absorptive() const override
Return true if this semiring is absorptive ( for all ).
Definition Boolean.h:67
virtual value_type delta(value_type x) const override
Apply the operator.
Definition Boolean.h:63
value_type parse_leaf(const char *v) const
Definition Boolean.h:92
virtual value_type monus(value_type x, value_type y) const override
Apply the monus (m-semiring difference) operation.
Definition Boolean.h:59
virtual bool mul_sub_left_distributive() const override
distributes over , so the HAVING MIN / MAX comparisons take the single-scan closed form.
Definition Boolean.h:76
Abstract base class for (m-)semirings.
Definition Semiring.h:119