ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Viterbi.h
Go to the documentation of this file.
1/**
2 * @file semiring/Viterbi.h
3 * @brief Viterbi (max-times) m-semiring over @f$[0,1]@f$.
4 *
5 * The Viterbi m-semiring (@f$[0,1]@f$, @f$\max@f$, @f$\times@f$, 0, 1)
6 * is used to model most-likely-derivation provenance: input gates
7 * carry probabilities, @f$\oplus = \max@f$ keeps the most likely
8 * derivation, and @f$\otimes = \times@f$ multiplies probabilities
9 * along a derivation.
10 *
11 * Operations:
12 * - @c zero() → 0
13 * - @c one() → 1
14 * - @c plus() → maximum of all operands (empty list → 0)
15 * - @c times() → product of all operands (empty list → 1)
16 * - @c monus() → 0 if @f$a \le b@f$, @f$a@f$ otherwise
17 * - @c delta() → 1 if @c x is non-zero, else 0
18 *
19 * Absorptivity: `absorptive()` returns `true`. With inputs in
20 * @f$[0,1]@f$, @f$\mathbb{1} \oplus a = \max(1, a) = 1@f$.
21 * The circuit evaluator may exploit the resulting idempotency to
22 * deduplicate operands.
23 *
24 * @note No bounds checking is performed: it is the caller's
25 * responsibility to supply input probabilities in @f$[0,1]@f$.
26 *
27 * @see https://provsql.org/lean-docs/Provenance/Semirings/Viterbi.html
28 * Lean 4 verified instance: @c instSemiringWithMonusViterbi,
29 * with proofs of @c Viterbi.absorptive,
30 * @c Viterbi.idempotent, and
31 * @c Viterbi.mul_sub_left_distributive.
32 */
33#ifndef VITERBI_H
34#define VITERBI_H
35
36#include <algorithm>
37#include <numeric>
38#include <vector>
39
40#include "Semiring.h"
41
42namespace semiring {
43/**
44 * @brief The Viterbi (max-times) m-semiring over @c double.
45 *
46 * Each gate evaluates to the probability of the most likely derivation
47 * of the corresponding sub-formula.
48 */
49class Viterbi : public semiring::Semiring<double>
50{
51public:
52virtual value_type zero() const override {
53 return 0.0;
54}
55virtual value_type one() const override {
56 return 1.0;
57}
58virtual value_type plus(const std::vector<value_type> &v) const override {
59 if(v.empty()) return zero();
60 return *std::max_element(v.begin(), v.end());
61}
62virtual value_type times(const std::vector<value_type> &v) const override {
63 return std::accumulate(v.begin(), v.end(), 1.0, std::multiplies<value_type>());
64}
65virtual value_type monus(value_type x, value_type y) const override
66{
67 return x<=y ? zero() : x;
68}
69virtual value_type delta(value_type x) const override
70{
71 return x!=zero() ? one() : zero();
72}
73virtual bool absorptive() const override {
74 return true;
75}
76/**
77 * @brief No semiring homomorphism @c BoolFunc(Y) →+* Viterbi exists
78 * (real multiplication is not idempotent), so the safe-query
79 * Boolean rewrite is unsound under the Viterbi semiring.
80 * Inherits the @c false default from @c Semiring; this
81 * override exists for documentation.
82 *
83 * Lean: @c Provenance.Semirings.Viterbi.no_hom_from_BoolFunc
84 * (provenance-lean/Provenance/Semirings/Viterbi.lean).
85 */
86virtual bool compatibleWithBooleanRewrite() const override {
87 return false;
88}
89value_type parse_leaf(const char *v) const {
90 return atof(v);
91}
92
93};
94}
95
96#endif /* VITERBI_H */
Abstract semiring interface for provenance evaluation.
Abstract base class for (m-)semirings.
Definition Semiring.h:93
The Viterbi (max-times) m-semiring over double.
Definition Viterbi.h:50
virtual value_type one() const override
Return the multiplicative identity .
Definition Viterbi.h:55
virtual value_type delta(value_type x) const override
Apply the operator.
Definition Viterbi.h:69
virtual bool absorptive() const override
Return true if this semiring is absorptive ( for all ).
Definition Viterbi.h:73
virtual value_type zero() const override
Return the additive identity .
Definition Viterbi.h:52
value_type parse_leaf(const char *v) const
Definition Viterbi.h:89
virtual value_type times(const std::vector< value_type > &v) const override
Apply the multiplicative operation to a list of values.
Definition Viterbi.h:62
virtual bool compatibleWithBooleanRewrite() const override
No semiring homomorphism BoolFunc(Y) →+* Viterbi exists (real multiplication is not idempotent),...
Definition Viterbi.h:86
virtual value_type plus(const std::vector< value_type > &v) const override
Apply the additive operation to a list of values.
Definition Viterbi.h:58
virtual value_type monus(value_type x, value_type y) const override
Apply the monus (m-semiring difference) operation.
Definition Viterbi.h:65