ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Counting.h
Go to the documentation of this file.
1/**
2 * @file semiring/Counting.h
3 * @brief Counting semiring (ℕ, +, ×, 0, 1).
4 *
5 * The counting semiring (@f$\mathbb{N}@f$, @f$+@f$, @f$\times@f$, 0, 1)
6 * counts the number of distinct derivations (proof witnesses) of each
7 * query result tuple.
8 *
9 * Operations:
10 * - @c zero() → 0
11 * - @c one() → 1
12 * - @c plus() → sum of all operands
13 * - @c times() → product of all operands
14 * - @c monus() → truncated subtraction: max(0, x − y)
15 * - @c delta() → 1 if x ≠ 0, else 0
16 *
17 * This semiring is **not** absorptive.
18 *
19 * @see https://provsql.org/lean-docs/Provenance/Semirings/Nat.html
20 * Lean 4 verified instance: @c instSemiringWithMonusNat, with
21 * proofs of @c Nat.mul_sub_left_distributive, @c Nat.not_idempotent,
22 * and @c Nat.not_absorptive.
23 */
24#ifndef COUNTING_H
25#define COUNTING_H
26
27#include <cstdlib>
28#include <numeric>
29#include <vector>
30#include <stdexcept>
31
32#include "Semiring.h"
33
34namespace semiring {
35/**
36 * @brief The counting semiring over @c unsigned.
37 *
38 * Each gate evaluates to the number of distinct derivations of the
39 * corresponding sub-formula.
40 */
41class Counting : public semiring::Semiring<unsigned>
42{
43public:
44virtual value_type zero() const override {
45 return 0;
46}
47virtual value_type one() const override {
48 return 1;
49}
50virtual value_type plus(const std::vector<value_type> &v) const override {
51 return std::accumulate(v.begin(), v.end(), 0);
52}
53virtual value_type times(const std::vector<value_type> &v) const override {
54 return std::accumulate(v.begin(), v.end(), 1, std::multiplies<value_type>());
55}
56virtual value_type monus(value_type x, value_type y) const override
57{
58 return x<=y ? 0 : x-y;
59}
60virtual value_type delta(value_type x) const override
61{
62 return x!=0 ? 1 : 0;
63}
64/**
65 * @brief No semiring homomorphism @c BoolFunc(X) →+* ℕ exists: any
66 * such map would force @c 1 + a = 1 in ℕ (from @c var + 1 = 1
67 * in @c BoolFunc), contradicting the non-absorptivity of ℕ.
68 * The safe-query Boolean rewrite is therefore unsound under
69 * the counting semiring (read-once factoring loses derivation
70 * multiplicities). Inherits the @c false default from
71 * @c Semiring; this override exists for documentation.
72 *
73 * Lean: @c Provenance.Semirings.Nat.no_hom_from_BoolFunc
74 * (provenance-lean/Provenance/Semirings/Nat.lean).
75 */
76virtual bool compatibleWithBooleanRewrite() const override {
77 return false;
78}
79value_type parse_leaf(const char *v) const {
80 return atoi(v);
81}
82
83};
84}
85
86#endif /* COUNTING_H */
Abstract semiring interface for provenance evaluation.
The counting semiring over unsigned.
Definition Counting.h:42
virtual value_type zero() const override
Return the additive identity .
Definition Counting.h:44
virtual value_type plus(const std::vector< value_type > &v) const override
Apply the additive operation to a list of values.
Definition Counting.h:50
value_type parse_leaf(const char *v) const
Definition Counting.h:79
virtual value_type times(const std::vector< value_type > &v) const override
Apply the multiplicative operation to a list of values.
Definition Counting.h:53
virtual bool compatibleWithBooleanRewrite() const override
No semiring homomorphism BoolFunc(X) →+* ℕ exists: any such map would force 1 + a = 1 in ℕ (from var ...
Definition Counting.h:76
virtual value_type one() const override
Return the multiplicative identity .
Definition Counting.h:47
virtual value_type monus(value_type x, value_type y) const override
Apply the monus (m-semiring difference) operation.
Definition Counting.h:56
virtual value_type delta(value_type x) const override
Apply the operator.
Definition Counting.h:60
Abstract base class for (m-)semirings.
Definition Semiring.h:93