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#ifndef COUNTING_H
20#define COUNTING_H
21
22#include <numeric>
23#include <vector>
24#include <stdexcept>
25
26#include "Semiring.h"
27
28namespace semiring {
29/**
30 * @brief The counting semiring over @c unsigned.
31 *
32 * Each gate evaluates to the number of distinct derivations of the
33 * corresponding sub-formula.
34 */
35class Counting : public semiring::Semiring<unsigned>
36{
37public:
38virtual value_type zero() const override {
39 return 0;
40}
41virtual value_type one() const override {
42 return 1;
43}
44virtual value_type plus(const std::vector<value_type> &v) const override {
45 return std::accumulate(v.begin(), v.end(), 0);
46}
47virtual value_type times(const std::vector<value_type> &v) const override {
48 return std::accumulate(v.begin(), v.end(), 1, std::multiplies<value_type>());
49}
50virtual value_type monus(value_type x, value_type y) const override
51{
52 return x<=y ? 0 : x-y;
53}
54virtual value_type delta(value_type x) const override
55{
56 return x!=0 ? 1 : 0;
57}
58
59};
60}
61
62#endif /* COUNTING_H */
Abstract semiring interface for provenance evaluation.
The counting semiring over unsigned.
Definition Counting.h:36
virtual value_type zero() const override
Return the additive identity .
Definition Counting.h:38
virtual value_type plus(const std::vector< value_type > &v) const override
Apply the additive operation to a list of values.
Definition Counting.h:44
virtual value_type times(const std::vector< value_type > &v) const override
Apply the multiplicative operation to a list of values.
Definition Counting.h:47
virtual value_type one() const override
Return the multiplicative identity .
Definition Counting.h:41
virtual value_type monus(value_type x, value_type y) const override
Apply the monus (m-semiring difference) operation.
Definition Counting.h:50
virtual value_type delta(value_type x) const override
Apply the operator.
Definition Counting.h:54
Abstract base class for (m-)semirings.
Definition Semiring.h:84
unsigned value_type
The carrier type of this semiring.
Definition Semiring.h:87