ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Tropical.h
Go to the documentation of this file.
1/**
2 * @file semiring/Tropical.h
3 * @brief Tropical (min-plus) m-semiring over @f$\mathbb{R} \cup \{+\infty\}@f$.
4 *
5 * The tropical m-semiring (@f$\mathbb{R} \cup \{+\infty\}@f$,
6 * @f$\min@f$, @f$+@f$, @f$+\infty@f$, 0) is used to model
7 * shortest-path/least-cost provenance: input gates carry edge weights
8 * (or costs), @f$\oplus = \min@f$ selects the cheapest derivation,
9 * and @f$\otimes = +@f$ accumulates cost along a derivation.
10 *
11 * Operations:
12 * - @c zero() → @f$+\infty@f$
13 * - @c one() → 0
14 * - @c plus() → minimum of all operands (empty list → @f$+\infty@f$)
15 * - @c times() → sum of all operands (empty list → 0)
16 * - @c monus() → @f$+\infty@f$ if @f$x \ge y@f$ in the usual order,
17 * @f$x@f$ otherwise (note this is the *reverse* of the
18 * natural semiring order; see Lean reference)
19 * - @c delta() → @f$+\infty@f$ if @c x is @f$+\infty@f$, else 0
20 *
21 * Absorptivity: `absorptive()` returns `false`. The Lean formalisation
22 * proves absorptivity only for canonically-ordered carriers (e.g.
23 * @f$\mathbb{N}@f$); over arbitrary @c double values (including
24 * negatives) @f$\mathbb{1} \oplus a = \min(0, a)@f$ is not always 0.
25 *
26 * @see https://provsql.org/lean-docs/Provenance/Semirings/Tropical.html
27 * Lean 4 verified instance: @c instSemiringWithMonusTropicalWithTop,
28 * with proofs of @c Tropical.absorptive (under
29 * @c CanonicallyOrderedAdd) and
30 * @c Tropical.mul_sub_left_distributive.
31 */
32#ifndef TROPICAL_H
33#define TROPICAL_H
34
35#include <algorithm>
36#include <limits>
37#include <numeric>
38#include <vector>
39
40#include "Semiring.h"
41
42namespace semiring {
43/**
44 * @brief Tropical (min-plus) m-semiring over @c double.
45 *
46 * Each gate evaluates to a real-valued cost (with @f$+\infty@f$ as
47 * the additive identity). Inputs are read from the mapping table as
48 * %float8 values; pass <tt>'Infinity'::%float8</tt> to encode the
49 * additive zero.
50 */
51class Tropical : public semiring::Semiring<double>
52{
53public:
54virtual value_type zero() const override {
55 return std::numeric_limits<double>::infinity();
56}
57virtual value_type one() const override {
58 return 0.0;
59}
60virtual value_type plus(const std::vector<value_type> &v) const override {
61 if(v.empty()) return zero();
62 return *std::min_element(v.begin(), v.end());
63}
64virtual value_type times(const std::vector<value_type> &v) const override {
65 return std::accumulate(v.begin(), v.end(), 0.0);
66}
67virtual value_type monus(value_type x, value_type y) const override
68{
69 return x>=y ? zero() : x;
70}
71virtual value_type delta(value_type x) const override
72{
73 return x==zero() ? zero() : one();
74}
75/**
76 * @brief No semiring homomorphism @c BoolFunc(Y) →+* Tropical exists
77 * (the min-plus structure cannot be made Boolean-functions
78 * compatible), so the safe-query Boolean rewrite is unsound
79 * under the tropical semiring. Inherits the @c false default
80 * from @c Semiring; this override exists for documentation.
81 *
82 * Lean: @c Provenance.Semirings.TropicalN.no_hom_from_BoolFunc
83 * (provenance-lean/Provenance/Semirings/Tropical.lean).
84 */
85virtual bool compatibleWithBooleanRewrite() const override {
86 return false;
87}
88value_type parse_leaf(const char *v) const {
89 return atof(v);
90}
91
92};
93}
94
95#endif /* TROPICAL_H */
Abstract semiring interface for provenance evaluation.
Abstract base class for (m-)semirings.
Definition Semiring.h:93
Tropical (min-plus) m-semiring over double.
Definition Tropical.h:52
virtual value_type plus(const std::vector< value_type > &v) const override
Apply the additive operation to a list of values.
Definition Tropical.h:60
value_type parse_leaf(const char *v) const
Definition Tropical.h:88
virtual bool compatibleWithBooleanRewrite() const override
No semiring homomorphism BoolFunc(Y) →+* Tropical exists (the min-plus structure cannot be made Boole...
Definition Tropical.h:85
virtual value_type delta(value_type x) const override
Apply the operator.
Definition Tropical.h:71
virtual value_type monus(value_type x, value_type y) const override
Apply the monus (m-semiring difference) operation.
Definition Tropical.h:67
virtual value_type one() const override
Return the multiplicative identity .
Definition Tropical.h:57
virtual value_type zero() const override
Return the additive identity .
Definition Tropical.h:54
virtual value_type times(const std::vector< value_type > &v) const override
Apply the multiplicative operation to a list of values.
Definition Tropical.h:64