// Shared data model for the C++ translation of `Statement.closedForm`. // // Keys are byte strings ordered lexicographically. The model separates // ownership on the two sides of the API: // // * input keys (the selectors in `RangeRead` and the reported keys in // `Result`) are `std::string_view`s, so callers can point them straight // at their own storage without copying; // * result keys (`ConflictRange`) are owned `std::string`s, because the // decision procedure can synthesise new keys (`keyAfter`, `afterFirst`, // clamp bounds) that outlive the inputs. // // `OptKey`/`OptKeyView` represent Lean's `WithTop Key`: `std::nullopt` is `⊤`, // and an empty key is the bottom key. `NatInf` represents Lean's `ℕ∞` // (`ENat`): `inf` is `⊤`, otherwise `n` is the finite value. #pragma once #include #include #include #include #include #include #include #include #include namespace cr { using Byte = std::uint8_t; // Owned byte string. `std::string`'s small-string optimisation keeps the // short keys used by this model allocation-free, and its comparisons go // through `char_traits`, which orders bytes as `unsigned char`. using Key = std::string; // Borrowed byte string, used for every input key. using KeyView = std::string_view; using OptKey = std::optional; using OptKeyView = std::optional; using Int = std::int64_t; struct Selector { OptKeyView key; Int offset = 0; }; struct RangeRead { Selector begin; Selector end_; bool reverse = false; }; struct Result { // A non-owning view of the reported keys. The caller keeps the underlying // keys alive for the duration of the call. std::span keys; bool more = false; }; struct ConflictRange { OptKey begin; OptKey end_; }; inline bool operator==(const ConflictRange& a, const ConflictRange& b) { return a.begin == b.begin && a.end_ == b.end_; } inline bool operator!=(const ConflictRange& a, const ConflictRange& b) { return !(a == b); } // --------------------------------------------------------------------------- // Borrowed/owned conversions // --------------------------------------------------------------------------- inline KeyView keyView(const Key& k) { return KeyView(k); } inline OptKeyView optView(const OptKey& k) { return k ? OptKeyView{KeyView(*k)} : std::nullopt; } inline OptKey ownKey(OptKeyView k) { if (!k) return std::nullopt; // The bottom key is the empty string; avoid the string_view constructor. if (k->empty()) return OptKey{Key{}}; return OptKey{Key(*k)}; } inline OptKey ownKey(const OptKey& k) { return k; } inline OptKey ownKey(OptKey&& k) { return std::move(k); } // --------------------------------------------------------------------------- // Keys and `WithTop Key` // --------------------------------------------------------------------------- inline bool keyLt(KeyView a, KeyView b) { return a.compare(b) < 0; } inline bool keyEq(KeyView a, KeyView b) { return a == b; } template inline bool optLt(const std::optional& a, const std::optional& b) { if (!a) return false; // ⊤ < x is false if (!b) return true; // ↑k < ⊤ return keyLt(*a, *b); } template inline bool optLe(const std::optional& a, const std::optional& b) { return !optLt(b, a); } template inline bool optGt(const std::optional& a, const std::optional& b) { return optLt(b, a); } template inline bool optGe(const std::optional& a, const std::optional& b) { return !optLt(a, b); } template inline OptKey ownAny(const std::optional& k) { return k ? OptKey{Key(*k)} : std::nullopt; } template inline OptKey optMin(const std::optional& a, const std::optional& b) { return optLt(a, b) ? ownAny(a) : ownAny(b); } template inline OptKey optMax(const std::optional& a, const std::optional& b) { return optLt(a, b) ? ownAny(b) : ownAny(a); } inline OptKey topKey() { return std::nullopt; } inline OptKey bottomKey() { return OptKey{Key{}}; } // A view of a static empty string, so empty views always have a valid pointer. inline KeyView emptyKeyView() { static const Key empty; return KeyView(empty); } inline OptKeyView topKeyView() { return std::nullopt; } inline OptKeyView bottomKeyView() { return OptKeyView{emptyKeyView()}; } inline bool isTop(OptKeyView k) { return !k.has_value(); } inline bool isBottom(OptKeyView k) { return k.has_value() && k->empty(); } inline OptKey asKey(Key k) { return OptKey{std::move(k)}; } inline OptKeyView asKey(KeyView k) { return OptKeyView{k}; } inline Key keyAfter(KeyView k) { Key out(k); out.push_back(0); return out; } inline Key keyAfterN(std::uint64_t n, KeyView k) { Key out(k); out.reserve(out.size() + static_cast(n)); out.insert(out.end(), static_cast(n), 0); return out; } inline bool isPrefix(KeyView prefix, KeyView whole) { return prefix.size() <= whole.size() && std::equal(prefix.begin(), prefix.end(), whole.begin()); } inline bool allZeroFrom(KeyView k, std::size_t from) { for (std::size_t i = from; i < k.size(); ++i) if (k[i] != 0) return false; return true; } // --------------------------------------------------------------------------- // `ℕ∞` (ENat) // --------------------------------------------------------------------------- struct NatInf { bool inf = false; std::uint64_t n = 0; static NatInf top() { return NatInf{true, 0}; } static NatInf of(std::uint64_t v) { return NatInf{false, v}; } }; inline bool niLe(NatInf a, NatInf b) { if (a.inf) return b.inf; if (b.inf) return true; return a.n <= b.n; } inline bool niLt(NatInf a, NatInf b) { if (a.inf) return false; if (b.inf) return true; return a.n < b.n; } inline bool niEq(NatInf a, NatInf b) { if (a.inf != b.inf) return false; return a.inf || a.n == b.n; } inline NatInf niAdd(NatInf a, NatInf b) { if (a.inf || b.inf) return NatInf::top(); return NatInf::of(a.n + b.n); } // Lean's `ENat` subtraction: `a - ⊤ = 0`, `⊤ - b = ⊤` (b finite), and // finite values subtract with truncation at zero. inline NatInf niSub(NatInf a, NatInf b) { if (b.inf) return NatInf::of(0); if (a.inf) return NatInf::top(); return NatInf::of(a.n >= b.n ? a.n - b.n : 0); } // `Int.toNat`. inline std::uint64_t toNat(__int128 x) { return x <= 0 ? 0 : static_cast(x); } inline std::uint64_t toNat(Int x) { return toNat(static_cast<__int128>(x)); } // Natural-number truncated subtraction. inline std::uint64_t natSub(std::uint64_t a, std::uint64_t b) { return a >= b ? a - b : 0; } // --------------------------------------------------------------------------- // Result projections // --------------------------------------------------------------------------- inline KeyView firstKey(const Result& res) { return res.keys.empty() ? emptyKeyView() : res.keys.front(); } inline KeyView lastKey(const Result& res) { return res.keys.empty() ? emptyKeyView() : res.keys.back(); } inline OptKeyView first(const Result& res) { return asKey(firstKey(res)); } inline OptKeyView last(const Result& res) { return asKey(lastKey(res)); } inline OptKey afterFirst(const Result& res) { return asKey(keyAfter(firstKey(res))); } inline OptKey afterLast(const Result& res) { return asKey(keyAfter(lastKey(res))); } inline KeyView leastReported(const RangeRead& r, const Result& res) { return r.reverse ? lastKey(res) : firstKey(res); } inline OptKeyView leastReportedOpt(const RangeRead& r, const Result& res) { return asKey(leastReported(r, res)); } inline RangeRead forwardRead(const RangeRead& r) { RangeRead out = r; out.reverse = false; return out; } // A reversed view of the reported keys. The caller owns the returned vector, // whose elements borrow from `res.keys`. inline std::vector reversedKeys(const Result& res) { return {res.keys.rbegin(), res.keys.rend()}; } // --------------------------------------------------------------------------- // Cardinality of `[b, e)` // --------------------------------------------------------------------------- inline NatInf intervalCard(OptKeyView b, OptKeyView e) { if (!b) return NatInf::of(0); // b = ⊤ if (!e) return NatInf::top(); // e = ⊤ const KeyView bb = *b; const KeyView ee = *e; if (!keyLt(bb, ee)) return NatInf::of(0); // e <= b if (isPrefix(bb, ee) && allZeroFrom(ee, bb.size())) return NatInf::of(static_cast(ee.size() - bb.size())); return NatInf::top(); } // --------------------------------------------------------------------------- // Result-based counters, in the exact form used by the statement // --------------------------------------------------------------------------- inline std::uint64_t residualT(const RangeRead& r, const Result& res) { if (isTop(r.end_.key)) return 0; std::uint64_t count = 0; for (KeyView k : res.keys) if (!keyLt(k, *r.end_.key)) ++count; // r.end_.key <= k return count; } inline std::uint64_t reportedBelowBegin(const RangeRead& r, const Result& res) { if (isTop(r.begin.key)) return static_cast(res.keys.size()); std::uint64_t count = 0; for (KeyView k : res.keys) if (keyLt(k, *r.begin.key)) ++count; return count; } inline std::uint64_t residualB(const RangeRead& r) { return toNat(static_cast<__int128>(r.begin.offset) - 1); } inline std::uint64_t residualE(const RangeRead& r) { return toNat(static_cast<__int128>(r.end_.offset) - 1); } inline std::uint64_t beginSlack(const RangeRead& r) { return toNat(-static_cast<__int128>(r.begin.offset)); } inline std::uint64_t clampNonposD(const RangeRead& r) { return toNat(-static_cast<__int128>(r.end_.offset)); } inline std::uint64_t clampCap_(const RangeRead& r) { NatInf c = intervalCard(r.end_.key, r.begin.key); return niEq(c, NatInf::top()) ? 0 : c.n; // caller must check finiteness } } // namespace cr