ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Formula.h File Reference

Symbolic representation of provenance as a human-readable formula. More...

#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <map>
#include <memory>
#include "Semiring.h"
Include dependency graph for Formula.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  semiring::Formula
 Symbolic provenance representation over std::string. More...
struct  semiring::Formula::Equations
 The unknowns named and the equations recorded during one evaluation, shared by the copies of the semiring that evaluation makes. More...

Namespaces

namespace  semiring

Functions

template<typename Range>
static std::string join (Range const &elements, const char *const delimiter)
 Concatenate the strings of a range with a delimiter.
static std::pair< std::size_t, std::size_t > operand_range (const std::string &s, const std::string &op)
 The part of s to write as an operand of op: its inner content if it is wrapped in a single matched outer paren pair whose top-level operator (depth 1, inside that pair) is op, all of it otherwise.
static std::string join_operands (const std::vector< const std::string * > &v, const std::string &op)
 "(a op b op …)" over the operands v points to, flattened by operand_range, built in place.
static std::vector< const std::string * > pointers_to (const std::vector< std::string > &v)
 Pointers to the strings of v.
static std::string format_number (double v)
 Render a probability for display in a symbolic formula.

Detailed Description

Symbolic representation of provenance as a human-readable formula.

The Formula pseudo-semiring (std::string, \(\oplus\), \(\otimes\), "šŸ˜", "šŸ™") produces a symbolic representation of provenance using Unicode semiring symbols. It is primarily used for debugging and testing.

Each gate evaluates to a string:

  • zero() → "šŸ˜"
  • one() → "šŸ™"
  • plus() → "(a āŠ• b āŠ• …)" or just "a" for singletons
  • times() → "(a āŠ— b āŠ— …)" or just "a" for singletons
  • monus() → "(a āŠ– b)"
  • delta() → "Ī“(a)" or "Ī“a" if a starts with (
  • cmp() → "[s1 op s2]"
  • semimod()→ "x*s"
  • agg() → operator-specific notation (e.g., "min(a,b)")
  • value() → the literal string itself
  • unmapped_input() → the leaf's abbreviated UUID ("1361b50e…")

It also renders the measure-carrier gates that carry no algebraic meaning, and that every proper semiring therefore refuses – being a serialisation of the circuit rather than an evaluation of it, Formula has a faithful rendering for each and refuses nothing:

  • rv() → "normal(2.5, 0.5)" (wired parameters substituted)
  • arith() → ordinary arithmetic notation ("(a + b)", "ln(a)"…), kept visually distinct from the semiring's \(\oplus\) / \(\otimes\)
  • mixture() → "(p ? x : y)"
  • categorical() → "categorical(Īŗ; 0.3: a, 0.7: b)"
  • guarded_case()→ "case(g → v; else d)"
  • observe() → "observe(x = 2.5)"
  • conditioned() → "(x | c)"

Definition in file Formula.h.

Function Documentation

◆ format_number()

std::string format_number ( double v)
static

Render a probability for display in a symbolic formula.

Enough significant digits that the usual decimal probabilities print back as themselves (0.3, not 0.299999), without the full round-trip verbosity of setprecision(17).

Definition at line 175 of file Formula.h.

Here is the caller graph for this function:

◆ join()

template<typename Range>
std::string join ( Range const & elements,
const char *const delimiter )
static

Concatenate the strings of a range with a delimiter.

Used internally by Formula::agg() and the other n-ary renderings. Built in place, the size reserved first: a formula may be most of the memory in use, and a stream would hold it twice more.

Template Parameters
RangeAny range of std::string.
Parameters
elementsThe range to join.
delimiterString to insert between adjacent elements.
Returns
All elements concatenated with delimiter between them.

Definition at line 65 of file Formula.h.

Here is the caller graph for this function:

◆ join_operands()

std::string join_operands ( const std::vector< const std::string * > & v,
const std::string & op )
static

"(a op b op …)" over the operands v points to, flattened by operand_range, built in place.

Definition at line 134 of file Formula.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operand_range()

std::pair< std::size_t, std::size_t > operand_range ( const std::string & s,
const std::string & op )
static

The part of s to write as an operand of op: its inner content if it is wrapped in a single matched outer paren pair whose top-level operator (depth 1, inside that pair) is op, all of it otherwise.

Used by Formula::plus() and Formula::times() to flatten same-op nested gates by associativity: a child "(a āŠ• b)" feeding into a parent plus is unwrapped to "a āŠ• b" so the join produces "a āŠ• b āŠ• c" instead of "(a āŠ• b) āŠ• c". A different top-level op (e.g., a times child) keeps its parens.

Returns
The offset and length of that part in s: no copy is made.

Definition at line 99 of file Formula.h.

Here is the caller graph for this function:

◆ pointers_to()

std::vector< const std::string * > pointers_to ( const std::vector< std::string > & v)
static

Pointers to the strings of v.

Definition at line 160 of file Formula.h.

Here is the caller graph for this function: