23#ifndef GENERIC_CIRCUIT_H
24#define GENERIC_CIRCUIT_H
29#include <boost/archive/binary_oarchive.hpp>
30#include <boost/serialization/unordered_map.hpp>
31#include <boost/serialization/map.hpp>
32#include <boost/serialization/set.hpp>
33#include <boost/serialization/vector.hpp>
51std::map<gate_t, std::pair<unsigned,unsigned> >
infos;
52std::map<gate_t, std::string>
extra;
64 return "<GenericCircuit>";
75 infos[g]=std::make_pair(info1, info2);
85 auto it =
infos.find(g);
87 return std::make_pair(-1, -1);
108 auto it =
extra.find(g);
136 prob[
static_cast<std::underlying_type<gate_t>::type
>(g)]=p;
145 return prob[
static_cast<std::underlying_type<gate_t>::type
>(g)];
179 }
else if (p == 1.0) {
358 std::string u =
"dec-in-" + std::to_string(
static_cast<size_t>(
id));
379 unsigned value_index) {
385 std::string u =
"dec-mul-" + std::to_string(
static_cast<size_t>(
id));
404 std::vector<gate_t> wires_) {
407 setInfos(
id,
static_cast<int>(op), 0);
409 std::string u =
"dec-arith-" + std::to_string(
static_cast<size_t>(
id));
432 std::string u =
"dec-value-" + std::to_string(
static_cast<size_t>(
id));
455 std::vector<gate_t> w;
457 w.push_back(p_token);
458 w.push_back(x_token);
459 w.push_back(y_token);
476 unsigned value_index,
477 const std::string &value_text) {
520 if (w.size() < 2)
return false;
522 for (std::size_t i = 1; i < w.size(); ++i) {
533template<
class Archive>
534void serialize (Archive & ar,
const unsigned int version)
563template<
typename S, std::enable_if_t<std::is_base_of_v<semiring::Semiring<
typename S::value_type>, S>,
int> = 0>
564typename S::value_type
evaluate(
gate_t g, std::unordered_map<gate_t, typename S::value_type> &provenance_mapping, S
semiring)
const;
Generic directed-acyclic-graph circuit template and gate identifier.
gate_t
Strongly-typed gate identifier.
Abstract semiring interface for provenance evaluation.
Generic template base class for provenance circuits.
std::vector< gate_t > & getWires(gate_t g)
gate_type getGateType(gate_t g) const
std::unordered_map< gate_t, uuid > id2uuid
std::unordered_map< uuid, gate_t > uuid2id
void setGateType(gate_t g, gate_type t)
std::vector< gate_type > gates
std::vector< std::vector< gate_t > > wires
In-memory provenance circuit with semiring-generic evaluation.
void resolveToPlus(gate_t g, std::vector< gate_t > w)
Rewrite an arbitrary gate as a gate_plus over w.
void resolveToCategoricalMixture(gate_t g, std::vector< gate_t > wires_)
Rewrite g in place as a categorical-form gate_mixture over wires ([key, mul_1, ......
void resolveGateToZero(gate_t g)
Replace an arbitrary gate (typically gate_times) by gate_zero.
void setWires(gate_t g, std::vector< gate_t > w)
Replace the wires of g with w.
std::map< gate_t, std::pair< unsigned, unsigned > > infos
Per-gate (info1, info2) annotations.
S::value_type evaluate(gate_t g, std::unordered_map< gate_t, typename S::value_type > &provenance_mapping, S semiring) const
Evaluate the sub-circuit rooted at gate g over semiring semiring.
gate_t addAnonymousMulinputGateWithValue(gate_t key, double p, unsigned value_index, const std::string &value_text)
Allocate a fresh gate_mulinput labelled with a numeric outcome value carried in extra.
void resolveToRv(gate_t g, const std::string &s)
Rewrite an arbitrary gate as a gate_rv carrying the distribution-spec extra s.
void resolveToMixture(gate_t g, gate_t p_token, gate_t x_token, gate_t y_token)
Rewrite g in place as a gate_mixture over the wires [p_token, x_token, y_token].
std::map< gate_t, std::string > extra
Per-gate string extras.
gate_t addGate() override
Allocate a new gate with a default-initialised type.
std::set< gate_t > inputs
Set of input (leaf) gate IDs.
void foldSemiringIdentities()
Drop semiring identity wires and collapse single-wire gate_times / gate_plus to their lone non-identi...
gate_t addAnonymousArithGate(provsql_arith_op op, std::vector< gate_t > wires_)
Allocate a fresh gate_arith gate with operator tag op and the given wires.
gate_t addAnonymousValueGate(const std::string &text)
Allocate a fresh gate_value gate carrying the textual scalar text.
void serialize(Archive &ar, const unsigned int version)
Boost serialisation support.
friend class dDNNFTreeDecompositionBuilder
bool isCategoricalMixture(gate_t g) const
Test whether g is a categorical-form gate_mixture (the explicit provsql.categorical output).
std::vector< double > prob
Per-gate probability values.
void setInfos(gate_t g, unsigned info1, unsigned info2)
Set the integer annotation pair for gate g.
std::string getExtra(gate_t g) const
Return the string extra for gate g.
gate_t setGate(gate_type type) override
Allocate a new gate with type type and no UUID.
void markBooleanAssumed(gate_t g)
Mark gate g as Boolean-assumed (in-memory side band).
std::set< gate_t > boolean_assumed_gates
Side-band Boolean-assumption marker set by foldBooleanIdentities ; an evaluator visiting a gate in th...
double getProb(gate_t g) const
Return the probability for gate g.
void resolveCmpToBernoulli(gate_t g, double p)
Replace a gate_cmp by a constant Boolean leaf (gate_one for p == 1, gate_zero for p == 0) or by a Ber...
gate_t addAnonymousInputGate(double p)
Allocate a fresh gate_input gate carrying probability p, with a unique synthetic UUID so subsequent B...
friend class boost::serialization::access
const std::set< gate_t > & getInputs() const
Return the set of input (leaf) gates.
bool isBooleanAssumed(gate_t g) const
Report whether g carries the Boolean-assumption flag.
std::pair< unsigned, unsigned > getInfos(gate_t g) const
Return the integer annotation pair for gate g.
void setExtra(gate_t g, const std::string &ex)
Attach a string extra to gate g.
virtual std::string toString(gate_t g) const override
Return a placeholder debug string (not intended for display).
gate_t addAnonymousMulinputGate(gate_t key, double p, unsigned value_index)
Allocate a fresh gate_mulinput gate with key key, probability p, and value index value_index.
void resolveToValue(gate_t g, const std::string &s)
Rewrite an arbitrary gate as a gate_value carrying the textual extra s.
void foldBooleanIdentities()
Apply Boolean-only simplification rules to gate_plus and gate_times.
void setProb(gate_t g, double p)
Set the probability for gate g.
Core types, constants, and utilities shared across ProvSQL.
provsql_arith_op
Arithmetic operator tags used by gate_arith.
@ gate_rv
Continuous random-variable leaf (extra encodes distribution).
@ gate_mixture
Probabilistic mixture: three wires [p_token (gate_input Bernoulli), x_token, y_token]; samples x when...
@ gate_arith
n-ary arithmetic gate over scalar-valued children (info1 holds operator tag)