Harden and fully cover the C++ translation

Add an exhaustive readable-instance sweep and a coverage gate requiring
100% line coverage of the summarised implementation. Delete branches that
are unreachable under `Readable`, centralise and assert the 32-bit offset
contract at the public entry points, and stop materialising `n` zero bytes
when deciding `keyAfterN n k < upper`.

Also add the extreme-offset regression test, harden the corpus parsers,
remove unused model helpers, document the literal-total / fast-readable
split, and refresh the validation and benchmark numbers.
This commit is contained in:
andrew committed 2026-11-17 18:46:24 -05:00
1 parent e32e42d933
commit 3bb5d062ff
15 files changed
+620 -196

No files matched your search

+34 -106
View File
@@ -5,11 +5,13 @@
// `res.keys` (and re-derives small arithmetic facts) on almost every branch.
// This version summarises the reported keys once (two binary searches on the
// sorted traversal order) and caches the handful of interval cardinalities the
// decision tree needs, then replays the same tree. Reversed results are handled
// through a `reversed` view instead of copying the key list.
// decision tree needs, then replays the same tree for readable inputs. Branches
// that the `Readable` invariant makes unreachable are omitted. Reversed results
// are handled through a `reversed` view instead of copying the key list.
#include "fast.h"
#include <algorithm>
#include <cassert>
#include <utility>
namespace cr::fast {
@@ -32,8 +34,14 @@ struct Obs {
bool ascending = true;
std::size_t n = 0;
const KeyView& firstK() const { return reversed ? keys.back() : keys.front(); }
const KeyView& lastK() const { return reversed ? keys.front() : keys.back(); }
const KeyView& firstK() const {
assert(!keys.empty());
return reversed ? keys.back() : keys.front();
}
const KeyView& lastK() const {
assert(!keys.empty());
return reversed ? keys.front() : keys.back();
}
};
// Facts that only depend on the read and on the *multiset* of reported keys.
@@ -206,12 +214,11 @@ bool clampsToBottom(const Ctx& c, const Obs& o) {
}
std::uint64_t cELo(Int oB, Int oE) {
return std::max(toNat(static_cast<__int128>(2) - oE),
toNat(static_cast<__int128>(oB) - oE + 1));
return std::max(toNat(2 - oE), toNat(oB - oE + 1));
}
std::uint64_t cEminP(Int oE) {
return toNat(static_cast<__int128>(2) - oE);
return toNat(2 - oE);
}
std::uint64_t noPinB(NatInf KBE, Int oB, Int oE) {
@@ -229,10 +236,6 @@ std::uint64_t closedFormDeficit(const Ctx& c) {
return residualD(rB(c), cardBE(c));
}
std::uint64_t gapOvershoot(const Ctx& c) {
return natSub(rE(c), static_cast<std::uint64_t>(c.t));
}
std::uint64_t crossedOvershootGeneral(const Ctx& c) {
return natSub(natSub(rE(c), static_cast<std::uint64_t>(c.t)), rB(c));
}
@@ -432,7 +435,7 @@ struct Impl {
NatInf lhs = NatInf::of(rE(c));
NatInf rhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
NatInf::of(static_cast<std::uint64_t>(c.n)));
__int128 nPlusBegin = static_cast<__int128>(c.n) + c.r->begin.offset;
Int nPlusBegin = static_cast<Int>(c.n) + c.r->begin.offset;
return niEq(lhs, rhs) && nPlusBegin == 1;
}
@@ -471,88 +474,21 @@ struct Impl {
if (isTop(c.r->end_.key)) return mk(std::move(L), topKey());
KeyView E = *c.r->end_.key;
std::uint64_t m = natSub(natSub(rE(c), c.t), 1);
if (keyLt(keyAfterN(m, E), o.lastK()))
// Lean's `keyAfterN m E < o.lastK()`, decided without building the `m`
// zero bytes.
if (keyAfterNLt(m, E, o.lastK()))
return mk(asKey(E), topKey());
return mk(std::move(L), topKey());
}
ConflictRange truncatedForwardClosed(const Ctx& c, const Obs& o) const {
NatInf cap = cardEB(c);
NatInf room = NatInf::of(natSub(rE(c), static_cast<std::uint64_t>(c.n)));
if (niLe(cap, room))
return mk(c.r->begin.key, after(o.lastK()));
return mk(c.r->end_.key, after(o.lastK()));
}
ConflictRange completeClosedWith(OptKey endKey, const Ctx& c, const Obs& o) const {
(void)o;
OptKeyView begin = c.r->begin.key;
if (optLt(c.r->end_.key, c.r->begin.key) &&
niLe(NatInf::of(gapOvershoot(c)), cardEB(c)))
begin = c.r->end_.key;
if (c.r->end_.offset <= 1)
return mk(c.r->begin.key, c.r->end_.key);
if (c.t == rE(c)) return mk(std::move(begin), std::move(endKey));
return mk(std::move(begin), topKey());
}
ConflictRange completeReversedClosed(const Ctx& c, const Obs& o) const {
return completeClosedWith(after(o.firstK()), c, o);
}
ConflictRange completeForwardClosed(const Ctx& c, const Obs& o) const {
return completeClosedWith(after(o.lastK()), c, o);
}
ConflictRange completeClosed(const Ctx& c, const Obs& o) const {
return c.r->reverse ? completeReversedClosed(c, o)
: completeForwardClosed(c, o);
}
ConflictRange closedFormOffsetOneTruncated(const Ctx& c, const Obs& o) const {
if (c.r->end_.offset <= 0)
return mk(c.r->begin.key, c.r->end_.key);
return mk(c.r->begin.key, after(o.lastK()));
}
ConflictRange topRange(const Ctx& c) const {
return mk(c.r->begin.key, topKey());
}
ConflictRange closedFormOffsetOneWith(OptKey endKey, const Ctx& c,
const Obs& o) const {
(void)o;
if (c.r->end_.offset <= 1)
return mk(c.r->begin.key, c.r->end_.key);
if (c.t == rE(c))
return mk(c.r->begin.key, std::move(endKey));
return topRange(c);
}
ConflictRange closedFormOffsetOneRev(const Ctx& c, const Obs& o) const {
return closedFormOffsetOneWith(after(o.firstK()), c, o);
}
ConflictRange closedFormOffsetOne(const Ctx& c, const Obs& o) const {
return closedFormOffsetOneWith(after(o.lastK()), c, o);
}
ConflictRange closedFormOffsetOneAny(const Ctx& c, const Obs& o) const {
return c.r->reverse ? closedFormOffsetOneRev(c, o)
: closedFormOffsetOne(c, o);
}
ConflictRange closedFormOffsetOneUniversal(const Ctx& c, const Obs& o) const {
if (o.more)
return c.r->reverse ? closedFormReverseTruncatedZero(c, o)
: closedFormOffsetOneTruncated(c, o);
return closedFormOffsetOneAny(c, o);
}
ConflictRange closedFormOffsetOneTotal(const Ctx& c, const Obs& o) const {
if (optLt(c.r->end_.key, c.r->begin.key)) {
if (o.more)
return c.r->reverse ? closedFormReverseTruncatedZero(c, o)
: truncatedForwardClosed(c, o);
return completeClosed(c, o);
}
return closedFormOffsetOneUniversal(c, o);
if (c.t == rE(c))
return mk(c.r->begin.key, after(o.lastK()));
return topRange(c);
}
// ----- zero crossed ------------------------------------------------------
@@ -562,8 +498,6 @@ struct Impl {
}
ConflictRange completeForwardCrossedGeneral(const Ctx& c, const Obs& o) const {
if (c.r->end_.offset <= 1)
return mk(c.r->begin.key, c.r->end_.key);
if (c.t + rB(c) == rE(c))
return mk(c.r->end_.key, after(o.lastK()));
return mk(crossedBegin(c), topKey());
@@ -592,7 +526,7 @@ struct Impl {
NatInf g = cardOK(c.r->end_.key, H);
bool useEnd = oLtK(c.r->end_.key, H) &&
niLt(niSub(NatInf::of(e), NatInf::of(t)), g);
return mk(useEnd ? c.r->end_.key : OptKey{H}, topKey());
return mk(useEnd ? c.r->end_.key : asKey(H), topKey());
}
ConflictRange a = zeroCrossLongAnchor(c, o);
return mk(std::move(a.begin), optMax(a.end_, c.r->begin.key));
@@ -601,7 +535,7 @@ struct Impl {
ConflictRange closedFormZeroCrossUnderEnd(const Ctx& c, const Obs& o) const {
RangeRead a = zeroBeginAnchor(c, o);
Ctx ca = makeCtx(a, o);
return closedFormOffsetOneTotal(ca, o);
return closedFormOffsetOne(ca, o);
}
ConflictRange closedFormZeroCrossComplete(const Ctx& c, const Obs& o) const {
@@ -671,8 +605,8 @@ struct Impl {
intervalCard(r.end_.key, r.begin.key)) &&
niLe(NatInf::of(natSub(e, b)),
intervalCard(r.begin.key, r.end_.key));
__int128 ePlusEnd = static_cast<__int128>(e) + r.end_.offset - 1;
__int128 bPlusBegin = static_cast<__int128>(b) + r.begin.offset - 1;
Int ePlusEnd = static_cast<Int>(e) + r.end_.offset - 1;
Int bPlusBegin = static_cast<Int>(b) + r.begin.offset - 1;
inner = firstFour && ePlusEnd > 0 && bPlusBegin < ePlusEnd;
}
return !inner;
@@ -694,8 +628,7 @@ struct Impl {
if (r.begin.offset <= 0) {
if (r.end_.offset >= 2) return mk(bottomKey(), topKey());
if (niLt(intervalCard(r.end_.key, r.begin.key),
NatInf::of(toNat(static_cast<__int128>(r.end_.offset) -
r.begin.offset))))
NatInf::of(toNat(r.end_.offset - r.begin.offset))))
return mk(bottomKey(), r.end_.key);
return mk(bottomKey(), optMax(r.begin.key, r.end_.key));
}
@@ -826,10 +759,7 @@ struct Impl {
return zeroCrossOvershootComplete(c, o) || negCrossOvershoot(c, o);
}
bool crossedCompleteEndLeOne(const Ctx& c, const Obs& o) const {
if (!(!c.r->reverse && !o.more && c.r->begin.offset <= 0 &&
kLtO(o.firstK(), c.r->begin.key)))
return false;
bool crossedCompleteEndLeOne(const Ctx& c) const {
if (c.r->end_.offset == 1 && bSlack(c) < c.below)
return true;
return c.r->end_.offset <= 0 &&
@@ -842,7 +772,7 @@ struct Impl {
}
bool zeroCrossLongFinite(const Ctx& c, const Obs& o) const {
return c.r->begin.offset == 0 && !c.r->reverse &&
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
c.r->end_.offset >= 2 && !isTop(c.r->begin.key) &&
optLt(c.r->end_.key, c.r->begin.key) &&
kLtO(leastReportedK(c, o), c.r->begin.key);
@@ -862,7 +792,7 @@ struct Impl {
ConflictRange closedFormCrossedComplete(const Ctx& c, const Obs& o) const {
if (crossedCompleteOvershoot(c, o))
return closedFormNegCrossTopUnified(c, o);
if (crossedCompleteEndLeOne(c, o))
if (crossedCompleteEndLeOne(c))
return closedFormNegCrossNonclampUnified(c, o);
if (crossedCompleteBeginZero(c, o))
return closedFormZeroCrossResidual(c, o);
@@ -891,8 +821,7 @@ struct Impl {
c.r->end_.offset <= 1 &&
niLe(niAdd(niAdd(cardEB(c),
NatInf::of(static_cast<std::uint64_t>(c.n))),
NatInf::of(toNat(static_cast<__int128>(1) -
c.r->end_.offset))),
NatInf::of(toNat(1 - c.r->end_.offset))),
NatInf::of(bSlack(c) + 1));
}
@@ -954,8 +883,7 @@ struct Impl {
bool negCrossTruncTopTopEq(const Ctx& c, const Obs& o) const {
if (!negCrossTruncTopTop(c, o)) return false;
std::uint64_t extra =
toNat(static_cast<__int128>(1) - c.r->end_.offset);
std::uint64_t extra = toNat(1 - c.r->end_.offset);
return static_cast<std::uint64_t>(c.n) + extra == bSlack(c) + 1;
}
@@ -1001,9 +929,8 @@ struct Impl {
if (c.r->end_.offset <= 0) {
if (optLt(c.r->end_.key, c.r->begin.key)) {
if (clampNonposBottom(c)) {
if (kLtO(o.lastK(), c.r->end_.key))
return closedFormNegCrossTruncNonposGapA2aBottom(c, o);
return mk(bottomKey(), clampLongUpper(c, o));
// Readability gives `lastK < resolve end <= end.key`.
return closedFormNegCrossTruncNonposGapA2aBottom(c, o);
}
return mk(OptKey{o.firstK()}, c.r->begin.key);
}
@@ -1071,6 +998,7 @@ struct Impl {
} // namespace
ConflictRange closedForm(const RangeRead& r, const Result& res) {
assertInputOffsetsFitInt32(r);
return Impl{}.closedForm(r, res);
}
+14 -12
View File
@@ -13,6 +13,7 @@ namespace {
using cr::afterFirst;
using cr::afterLast;
using cr::asKey;
using cr::assertInputOffsetsFitInt32;
using cr::beginSlack;
using cr::bottomKey;
using cr::clampNonposD;
@@ -22,7 +23,7 @@ using cr::intervalCard;
using cr::isBottom;
using cr::isTop;
using cr::keyAfter;
using cr::keyAfterN;
using cr::keyAfterNLt;
using cr::keyLt;
using cr::last;
using cr::lastKey;
@@ -113,12 +114,11 @@ bool clampsToBottom(const RangeRead& r, const Result& res) {
}
std::uint64_t cELo(Int oB, Int oE) {
return std::max(toNat(static_cast<__int128>(2) - oE),
toNat(static_cast<__int128>(oB) - oE + 1));
return std::max(toNat(2 - oE), toNat(oB - oE + 1));
}
std::uint64_t cEminP(Int oE) {
return toNat(static_cast<__int128>(2) - oE);
return toNat(2 - oE);
}
std::uint64_t noPinB(NatInf KBE, Int oB, Int oE) {
@@ -355,7 +355,7 @@ bool sentinelLargeTight(const RangeRead& r, const Result& res) {
NatInf lhs = NatInf::of(residualE(r));
NatInf rhs = niAdd(intervalCard(r.end_.key, last(res)),
NatInf::of(res.keys.size()));
__int128 nPlusBegin = static_cast<__int128>(res.keys.size()) + r.begin.offset;
Int nPlusBegin = static_cast<Int>(res.keys.size()) + r.begin.offset;
return niEq(lhs, rhs) && nPlusBegin == 1;
}
@@ -400,7 +400,9 @@ ConflictRange closedFormReverseTruncatedZero(const RangeRead& r, const Result& r
if (isTop(r.end_.key)) return mkRange(std::move(L), topKey());
KeyView E = *r.end_.key;
std::uint64_t n = natSub(natSub(residualE(r), residualT(r, res)), 1);
if (keyLt(keyAfterN(n, E), lastKey(res)))
// Lean's `keyAfterN n E < lastKey res`, decided without building the `n`
// zero bytes.
if (keyAfterNLt(n, E, lastKey(res)))
return mkRange(asKey(E), topKey());
return mkRange(std::move(L), topKey());
}
@@ -592,8 +594,8 @@ bool AlwaysEmpty(const RangeRead& r) {
niLe(NatInf::of(e), underEnd) &&
niLe(NatInf::of(natSub(b, e)), intervalCard(r.end_.key, r.begin.key)) &&
niLe(NatInf::of(natSub(e, b)), intervalCard(r.begin.key, r.end_.key));
__int128 ePlusEnd = static_cast<__int128>(e) + r.end_.offset - 1;
__int128 bPlusBegin = static_cast<__int128>(b) + r.begin.offset - 1;
Int ePlusEnd = static_cast<Int>(e) + r.end_.offset - 1;
Int bPlusBegin = static_cast<Int>(b) + r.begin.offset - 1;
inner = firstFour && ePlusEnd > 0 && bPlusBegin < ePlusEnd;
}
return !inner;
@@ -615,8 +617,7 @@ ConflictRange emptyValue(const RangeRead& r) {
if (r.begin.offset <= 0) {
if (r.end_.offset >= 2) return mkRange(bottomKey(), topKey());
if (niLt(intervalCard(r.end_.key, r.begin.key),
NatInf::of(toNat(static_cast<__int128>(r.end_.offset) -
r.begin.offset))))
NatInf::of(toNat(r.end_.offset - r.begin.offset))))
return mkRange(bottomKey(), r.end_.key);
return mkRange(bottomKey(), optMax(r.begin.key, r.end_.key));
}
@@ -799,7 +800,7 @@ bool negCrossTruncRevCrossedLow(const RangeRead& r, const Result& res) {
r.end_.offset <= 1 &&
niLe(niAdd(niAdd(intervalCard(r.end_.key, r.begin.key),
NatInf::of(res.keys.size())),
NatInf::of(toNat(static_cast<__int128>(1) - r.end_.offset))),
NatInf::of(toNat(1 - r.end_.offset))),
NatInf::of(beginSlack(r) + 1));
}
@@ -861,7 +862,7 @@ bool negCrossTruncTopTop(const RangeRead& r, const Result& res) {
bool negCrossTruncTopTopEq(const RangeRead& r, const Result& res) {
if (!negCrossTruncTopTop(r, res)) return false;
std::uint64_t extra = toNat(static_cast<__int128>(1) - r.end_.offset);
std::uint64_t extra = toNat(1 - r.end_.offset);
return res.keys.size() + extra == beginSlack(r) + 1;
}
@@ -951,6 +952,7 @@ ConflictRange closedFormCrossed(const RangeRead& r, const Result& res) {
} // namespace
ConflictRange closedForm(const RangeRead& r, const Result& res) {
assertInputOffsetsFitInt32(r);
if (res.keys.empty()) return emptyValue(r);
if (r.begin.offset >= 1 || clampedNonpos(r, res))
return closedFormPositive(r, res);
+50 -23
View File
@@ -16,8 +16,10 @@
#pragma once
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <optional>
#include <span>
#include <string>
@@ -36,10 +38,16 @@ using Key = std::string;
using KeyView = std::string_view;
using OptKey = std::optional<Key>;
using OptKeyView = std::optional<KeyView>;
// Lean's unbounded `Int`. Caller-supplied offsets are required to fit in
// `std::int32_t` (see `Selector`), so all intermediate arithmetic in `Int`
// stays far from overflow; the code below relies on that.
using Int = std::int64_t;
struct Selector {
OptKeyView key;
// Offsets supplied by callers must lie in `[INT32_MIN, INT32_MAX]`. They are
// stored as `Int` because the closed form also builds derived selectors whose
// offsets (for example `end.offset - residualT`) need not fit in 32 bits.
Int offset = 0;
};
@@ -49,6 +57,29 @@ struct RangeRead {
bool reverse = false;
};
// The public offset contract: caller-supplied offsets must fit in
// `std::int32_t`. This is checked at the public `closedForm` entry points
// (debug builds only) and by the corpus parsers. It is deliberately not
// checked in `Selector`, because the decision procedure derives selectors
// whose offsets may exceed this range.
inline constexpr Int kMinInputOffset =
std::numeric_limits<std::int32_t>::min();
inline constexpr Int kMaxInputOffset =
std::numeric_limits<std::int32_t>::max();
inline bool offsetsFitInt32(Int begin, Int end) {
return begin >= kMinInputOffset && begin <= kMaxInputOffset &&
end >= kMinInputOffset && end <= kMaxInputOffset;
}
inline bool inputOffsetsFitInt32(const RangeRead& r) {
return offsetsFitInt32(r.begin.offset, r.end_.offset);
}
inline void assertInputOffsetsFitInt32(const RangeRead& r) {
assert(inputOffsetsFitInt32(r));
}
struct Result {
// A non-owning view of the reported keys. The caller keeps the underlying
// keys alive for the duration of the call.
@@ -72,8 +103,6 @@ inline bool operator!=(const ConflictRange& a, const ConflictRange& 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;
}
@@ -93,8 +122,6 @@ inline OptKey ownKey(OptKey&& k) { return std::move(k); }
inline bool keyLt(KeyView a, KeyView b) { return a.compare(b) < 0; }
inline bool keyEq(KeyView a, KeyView b) { return a == b; }
template <class A, class B>
inline bool optLt(const std::optional<A>& a, const std::optional<B>& b) {
if (!a) return false; // ⊤ < x is false
@@ -136,7 +163,6 @@ 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(); }
@@ -151,13 +177,6 @@ inline Key keyAfter(KeyView k) {
return out;
}
inline Key keyAfterN(std::uint64_t n, KeyView k) {
Key out(k);
out.reserve(out.size() + static_cast<std::size_t>(n));
out.insert(out.end(), static_cast<std::size_t>(n), 0);
return out;
}
inline bool isPrefix(KeyView prefix, KeyView whole) {
return prefix.size() <= whole.size() &&
std::equal(prefix.begin(), prefix.end(), whole.begin());
@@ -169,6 +188,20 @@ inline bool allZeroFrom(KeyView k, std::size_t from) {
return true;
}
// Decide Lean's `keyAfterN n k < upper`, i.e. `k ++ 0^n < upper`, without
// materialising the `n` zero bytes. `n` comes from an offset, so with 32-bit
// input offsets it can still be about `2^31`; the comparison only needs the
// lengths and the first `upper.size() - k.size()` bytes of `upper`.
inline bool keyAfterNLt(std::uint64_t n, KeyView k, KeyView upper) {
const std::size_t m = std::min(k.size(), upper.size());
const int c = KeyView(k).substr(0, m).compare(KeyView(upper).substr(0, m));
if (c != 0) return c < 0;
if (k.size() >= upper.size()) return false; // `upper` is a prefix of `k ++ 0^n`
const std::uint64_t rem = static_cast<std::uint64_t>(upper.size() - k.size());
if (n < rem) return true; // proper prefix, or a nonzero byte decides `0 < b`
return !allZeroFrom(upper, k.size()); // equal iff `n == rem` and all zeros
}
// ---------------------------------------------------------------------------
// `ℕ∞` (ENat)
// ---------------------------------------------------------------------------
@@ -209,10 +242,9 @@ inline NatInf niSub(NatInf a, NatInf b) {
}
// `Int.toNat`.
inline std::uint64_t toNat(__int128 x) {
inline std::uint64_t toNat(Int x) {
return x <= 0 ? 0 : static_cast<std::uint64_t>(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) {
@@ -293,21 +325,16 @@ inline std::uint64_t reportedBelowBegin(const RangeRead& r, const Result& res) {
}
inline std::uint64_t residualB(const RangeRead& r) {
return toNat(static_cast<__int128>(r.begin.offset) - 1);
return toNat(r.begin.offset - 1);
}
inline std::uint64_t residualE(const RangeRead& r) {
return toNat(static_cast<__int128>(r.end_.offset) - 1);
return toNat(r.end_.offset - 1);
}
inline std::uint64_t beginSlack(const RangeRead& r) {
return toNat(-static_cast<__int128>(r.begin.offset));
return toNat(-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
return toNat(-r.end_.offset);
}
} // namespace cr