64template <
typename Range>
65static std::string
join(Range
const& elements,
const char *
const delimiter) {
66 const std::size_t dl = std::char_traits<char>::length(delimiter);
67 std::size_t n = 0, total = 0;
68 for(
const auto &x : elements) {
73 out.reserve(total + (n ? (n - 1) * dl : 0));
75 for(
const auto &x : elements) {
77 out.append(delimiter, dl);
98static std::pair<std::size_t, std::size_t>
100 const std::pair<std::size_t, std::size_t> whole{0, s.size()};
101 if(s.size() < 2 || s.front() !=
'(' || s.back() !=
')')
107 for(
size_t i = 0; i < s.size() - 1; ++i) {
108 if(s[i] ==
'(') ++depth;
109 else if(s[i] ==
')') {
117 const std::size_t b = 1, e = s.size() - 1;
119 for(
size_t i = b; i + op.size() <= e; ) {
120 if(s[i] ==
'(') { ++depth; ++i; }
121 else if(s[i] ==
')') { --depth; ++i; }
122 else if(depth == 0 && s.compare(i, op.size(), op) == 0)
135 const std::string &op) {
136 const std::string sep =
" " + op +
" ";
137 std::vector<std::pair<std::size_t, std::size_t> > r;
139 std::size_t total = 2;
140 for(
const auto *x : v) {
142 total += r.back().second;
145 total += (v.size() - 1) * sep.size();
149 for(std::size_t k = 0; k < v.size(); ++k) {
152 out.append(*v[k], r[k].first, r[k].second);
159static std::vector<const std::string *>
161 std::vector<const std::string *> r;
163 for(
const auto &x : v)
176 std::ostringstream os;
177 os << std::setprecision(15) << v;
195 std::vector<std::string>
rhs;
197 std::vector<std::pair<std::size_t, std::size_t> >
order;
198 std::map<std::string, std::size_t>
index;
200std::shared_ptr<Equations>
equations = std::make_shared<Equations>();
204 static const char *digits[] = {
"โ",
"โ",
"โ",
"โ",
"โ",
205 "โ
",
"โ",
"โ",
"โ",
"โ"};
206 std::string d = std::to_string(n), r;
208 r += digits[ch -
'0'];
216 std::vector<std::size_t> r;
217 for(std::size_t k = 0; k <
equations->names.size(); ++k) {
218 const std::string &nm =
equations->names[k];
219 for(std::size_t p = s.find(nm); p != std::string::npos;
220 p = s.find(nm, p + 1)) {
221 const std::size_t e = p + nm.size();
222 const bool more = e + 2 < s.size()
223 && (
unsigned char) s[e] == 0xE2
224 && (((
unsigned char) s[e + 1] == 0x82
225 && ((
unsigned char) s[e + 2] & 0xF0) == 0x80)
226 || ((
unsigned char) s[e + 1] == 0x80
227 && (
unsigned char) s[e + 2] == 0xB2));
248 std::string nm =
"x" +
subscript(position);
249 std::size_t primes = 0;
257 equations->order.emplace_back(primes, position);
299 return "("+x+
" โ "+y+
")";
309 std::string result =
"["+s1+
" ";
330 return result+
" "+s2+
"]";
385 for(
size_t i = 1; i<s.size(); ++i) {
431 const std::vector<value_type> ¶ms)
const override {
432 const auto colon = spec.find(
':');
433 if(colon == std::string::npos)
436 std::vector<value_type> args;
437 size_t pos = colon + 1;
438 while(pos <= spec.size()) {
439 const auto comma = spec.find(
',', pos);
440 const auto end = (comma == std::string::npos ? spec.size() : comma);
441 std::string arg = spec.substr(pos, end - pos);
442 if(arg.size() > 1 && arg[0] ==
'$') {
444 const size_t slot = std::stoul(arg.substr(1));
445 if(slot < params.size())
447 }
catch(
const std::exception &) {
452 if(comma == std::string::npos)
457 return spec.substr(0, colon) +
"(" +
join(args,
", ") +
")";
469 const std::vector<value_type> &v,
470 const std::string &extra)
const override {
471 const auto infix = [&v](
const char *sep) {
472 return "(" +
join(v, sep) +
")";
474 const auto functional = [&v](
const char *name) {
475 return name + (
"(" +
join(v,
", ") +
")");
480 if(v.empty())
return "0";
481 return v.size() == 1 ? v[0] : infix(
" + ");
483 if(v.empty())
return "1";
484 return v.size() == 1 ? v[0] : infix(
" * ");
486 return v.size() == 2 ? infix(
" - ") : functional(
"minus");
488 return v.size() == 2 ? infix(
" / ") : functional(
"div");
490 return functional(
"intdiv");
492 return v.size() == 1 ?
"(-" + v[0] +
")" : functional(
"neg");
494 return functional(
"max");
496 return functional(
"min");
498 return v.size() == 2 ? infix(
" ^ ") : functional(
"pow");
500 return functional(
"ln");
502 return functional(
"exp");
504 return functional(
"round");
506 return functional(
"floor");
508 return functional(
"ceil");
510 return functional(
"abs");
512 return functional(
"float8");
514 return functional(
"float4");
517 if(v.empty() || v.size() % 2 != 0)
518 return functional(
"percentile");
520 std::vector<value_type> rows;
521 for(
size_t i = 0; i + 1 < v.size(); i += 2)
522 rows.push_back(
"[" + v[i] +
"] " + v[i+1]);
523 return "percentile(" + extra +
"; " +
join(rows,
", ") +
")";
526 return functional(
"arith");
531 return "(" + p +
" ? " + x +
" : " + y +
")";
539 const std::vector<double> &probs,
540 const std::vector<std::string> &outcomes)
542 std::vector<value_type> arms;
543 for(
size_t i = 0; i < probs.size() && i < outcomes.size(); ++i)
544 arms.push_back(
format_number(probs[i]) +
": " + outcomes[i]);
545 return "categorical(" + key +
"; " +
join(arms,
", ") +
")";
550 if(v.empty() || v.size() % 2 == 0)
551 return "case(" +
join(v,
", ") +
")";
553 std::vector<value_type> arms;
554 for(
size_t i = 0; i + 1 < v.size(); i += 2)
555 arms.push_back(v[i] +
" โ " + v[i+1]);
556 arms.push_back(
"else " + v.back());
557 return "case(" +
join(arms,
"; ") +
")";
562 return "observe(" + child +
" = " + datum +
")";
574 return "cond(" +
join(v,
", ") +
")";
575 return "(" + v[0] +
" | " + v[1] +
")";
592 return uuid.size() > 8 ? uuid.substr(0, 8) +
"โฆ" : uuid;
595 return std::string(v);
620 std::vector<bool> needed(
equations->names.size(),
false);
622 while(!todo.empty()) {
623 const std::size_t k = todo.back();
631 std::vector<std::size_t> listed;
632 for(std::size_t k = 0; k < needed.size(); ++k)
635 std::sort(listed.begin(), listed.end(),
636 [
this](std::size_t a, std::size_t b) {
637 return equations->order[a] < equations->order[b];
640 for(
const auto k : listed)
641 eqs += (eqs.empty() ?
" where " :
", ") +
equations->names[k] +
" = " +
651 if(s.size() < 2 || s.front() !=
'(' || s.back() !=
')')
654 for(
size_t i = 0; i < s.size() - 1; ++i) {
655 if(s[i] ==
'(') ++depth;
656 else if(s[i] ==
')') {
672 return wrapped(s) ? s.substr(1, s.size() - 2) : s;
AggregationOperator
SQL aggregation functions tracked by ProvSQL.
@ OR
Boolean OR aggregate.
@ COUNT
COUNT(*) or COUNT(expr) โ integer.
@ AND
Boolean AND aggregate.
@ SUM
SUM โ integer or float.
@ ARRAY_AGG
Array aggregation.
@ NONE
No aggregation (returns NULL).
@ CHOOSE
Arbitrary selection (pick one element).
ComparisonOperator
SQL comparison operators used in gate_cmp circuit gates.
@ LE
Less than or equal (<=).
@ GE
Greater than or equal (>=).
ArithmeticOperator
Arithmetic operations carried by gate_arith circuit gates.
@ MAX
n-ary maximum (order statistic)
@ ROUND
rounding, to a number of digits given by a second wire
@ PERCENTILE
continuous percentile over interleaved [indicator, value] wires
@ INTDIV
binary quotient truncated toward zero (integer division)
@ ABS
unary absolute value
@ AS_FLOAT8
unary: the value as double precision reads it
@ AS_FLOAT4
unary: the value as real reads it
@ MIN
n-ary minimum (order statistic)
@ LN
unary natural logarithm
Abstract semiring interface for provenance evaluation.
Abstract base class for (m-)semirings.