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 The identity map @c BoolFunc(X) →+* Bool (evaluating a free
72 * Boolean function at a valuation) is an m-semiring
73 * homomorphism, so the safe-query Boolean rewrite preserves
74 * evaluation results in this semiring.
75 *
76 * Lean: @c Provenance.Semirings.Bool.homomorphism_to_BoolFunc and
77 * @c Bool.homomorphism_from_BoolFunc
78 * (provenance-lean/Provenance/Semirings/Bool.lean).
79 */
80virtual bool compatibleWithBooleanRewrite() const override {
81 return true;
82}
83value_type parse_leaf(const char *v) const {
84 return *v != 'f' && *v != '0';
85}
86};
87}
88
89#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:80
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:83
virtual value_type monus(value_type x, value_type y) const override
Apply the monus (m-semiring difference) operation.
Definition Boolean.h:59
Abstract base class for (m-)semirings.
Definition Semiring.h:93