Add a C++ translation of the closed form
- Literal transliteration of `Statement.closedForm` plus an equivalent summarised implementation - Borrowed input keys (`std::string_view` / `std::span`) and owned result keys (`std::string`) - Golden-vector test, differential/safety fuzzers, and a nanobench comparison The literal and summarised implementations agree on 40,000 Lean golden vectors, 18,971 sorted corpus cases, and the readable fuzzer; the raw fuzzer runs both implementations under ASan/UBSan. `build/` and the generated `oracle/golden.txt` are ignored.
This commit is contained in:
1 parent
6559cc582b
commit
e32e42d933
17 files changed
+8871
No files matched your search
@@ -0,0 +1,104 @@
|
||||
// Compare the literal translation with the summarised implementation on the
|
||||
// Lean-generated corpus (readable or not).
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "../src/fast.h"
|
||||
#include "../src/literal.h"
|
||||
|
||||
namespace {
|
||||
using namespace cr;
|
||||
|
||||
bool parseKey(const std::string& tok, OptKey& out) {
|
||||
if (tok == "T") { out = std::nullopt; return true; }
|
||||
if (tok == "-") { out = Key{}; return true; }
|
||||
if (tok.size() % 2 != 0) return false;
|
||||
auto hex = [](char c) -> int {
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
||||
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||||
return -1;
|
||||
};
|
||||
Key k;
|
||||
for (std::size_t i = 0; i < tok.size(); i += 2) {
|
||||
int hi = hex(tok[i]), lo = hex(tok[i + 1]);
|
||||
if (hi < 0 || lo < 0) return false;
|
||||
k.push_back(static_cast<Byte>(hi * 16 + lo));
|
||||
}
|
||||
out = std::move(k);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string encKey(const OptKey& k) {
|
||||
if (!k) return "T";
|
||||
if (k->empty()) return "-";
|
||||
static const char* d = "0123456789abcdef";
|
||||
std::string s;
|
||||
for (Byte b : *k) { s.push_back(d[b >> 4]); s.push_back(d[b & 15]); }
|
||||
return s;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) return 2;
|
||||
std::ifstream in(argv[1]);
|
||||
std::string line;
|
||||
std::size_t cases = 0, bad = 0;
|
||||
while (std::getline(in, line)) {
|
||||
if (line.empty() || line[0] == '#') continue;
|
||||
std::istringstream ss(line);
|
||||
std::string bk, ek, arrow, ob, oe;
|
||||
long long bo = 0, eo = 0;
|
||||
int rev = 0, more = 0;
|
||||
std::size_t n = 0;
|
||||
OptKey beginKey, endKey;
|
||||
if (!(ss >> bk >> bo >> ek >> eo >> rev >> more >> n)) continue;
|
||||
if (!parseKey(bk, beginKey) || !parseKey(ek, endKey)) continue;
|
||||
// Owned reported keys plus the views borrowed by both implementations.
|
||||
std::vector<Key> keys;
|
||||
keys.reserve(n);
|
||||
bool ok = true;
|
||||
for (std::size_t i = 0; i < n; ++i) {
|
||||
std::string kt;
|
||||
if (!(ss >> kt)) { ok = false; break; }
|
||||
OptKey p;
|
||||
if (!parseKey(kt, p) || !p) { ok = false; break; }
|
||||
keys.push_back(std::move(*p));
|
||||
}
|
||||
if (!ok || !(ss >> arrow >> ob >> oe) || arrow != "=>") continue;
|
||||
// `fast` assumes the readable sorting invariant, so only compare cases that
|
||||
// satisfy it (this is a superset of the readable cases).
|
||||
const bool reverse = rev != 0;
|
||||
bool sorted = true;
|
||||
for (std::size_t i = 1; i < keys.size(); ++i) {
|
||||
if (reverse ? !keyLt(keys[i], keys[i - 1])
|
||||
: !keyLt(keys[i - 1], keys[i])) {
|
||||
sorted = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!sorted) continue;
|
||||
std::vector<KeyView> keyViews;
|
||||
keyViews.reserve(keys.size());
|
||||
for (const Key& k : keys) keyViews.push_back(k);
|
||||
RangeRead r{Selector{optView(beginKey), static_cast<Int>(bo)},
|
||||
Selector{optView(endKey), static_cast<Int>(eo)}, reverse};
|
||||
Result res{keyViews, more != 0};
|
||||
++cases;
|
||||
ConflictRange a = literal::closedForm(r, res);
|
||||
ConflictRange b = fast::closedForm(r, res);
|
||||
if (a.begin != b.begin || a.end_ != b.end_) {
|
||||
if (++bad <= 20) {
|
||||
std::fprintf(stderr, "MISMATCH: lit [%s,%s) fast [%s,%s)\n %s\n",
|
||||
encKey(a.begin).c_str(), encKey(a.end_).c_str(),
|
||||
encKey(b.begin).c_str(), encKey(b.end_).c_str(),
|
||||
line.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
std::printf("diff: %zu cases, %zu mismatches\n", cases, bad);
|
||||
return bad == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user