Remove dead branches from the fast translation

Drop predicate operands and unreachable arms that dispatch and readable invariants fix, and extend the exhaustive sweep to cover every branch side. Gate coverage on branch coverage as well.
This commit is contained in:
andrew committed 2026-11-17 19:26:09 -05:00
1 parent 3bb5d062ff
commit 73e371fb40
4 files changed
+101 -172

No files matched your search

+69 -140
View File
@@ -4,14 +4,14 @@
// `literal.cpp` mirrors the Lean definition line by line and therefore rescans
// `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 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.
// sorted traversal order), then replays the same tree for readable inputs.
// Branches and predicate operands that the `Readable` invariant or a dispatch
// invariant makes unreachable/constant are omitted; `literal.cpp` keeps the
// full Lean conditions for audit. 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 {
@@ -34,14 +34,10 @@ struct Obs {
bool ascending = true;
std::size_t n = 0;
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();
}
// `closedForm` handles an empty result before building the tree, so `keys` is
// non-empty everywhere these are called.
const KeyView& firstK() const { return reversed ? keys.back() : keys.front(); }
const KeyView& lastK() const { return reversed ? keys.front() : keys.back(); }
};
// Facts that only depend on the read and on the *multiset* of reported keys.
@@ -51,27 +47,15 @@ struct Ctx {
std::size_t n = 0;
std::size_t below = 0; // number of reported keys < begin.key
std::size_t t = 0; // number of reported keys >= end.key
// Lazily computed interval cardinalities.
mutable bool haveBE = false;
mutable bool haveEB = false;
mutable NatInf be;
mutable NatInf eb;
};
NatInf cardBE(const Ctx& c) {
if (!c.haveBE) {
c.be = intervalCard(c.r->begin.key, c.r->end_.key);
c.haveBE = true;
}
return c.be;
return intervalCard(c.r->begin.key, c.r->end_.key);
}
// Each path consults this at most once, so it needs no cache.
NatInf cardEB(const Ctx& c) {
if (!c.haveEB) {
c.eb = intervalCard(c.r->end_.key, c.r->begin.key);
c.haveEB = true;
}
return c.eb;
return intervalCard(c.r->end_.key, c.r->begin.key);
}
NatInf cardKO(KeyView b, OptKeyView e) {
@@ -132,10 +116,6 @@ Ctx makeCtx(const RangeRead& r, const Obs& o) {
// Comparisons that avoid allocating a one-element `OptKey`
// ---------------------------------------------------------------------------
// ↑k < b
bool kLtO(KeyView k, OptKeyView b) { return !b || keyLt(k, *b); }
// ↑k <= b
bool kLeO(KeyView k, OptKeyView b) { return !b || !keyLt(*b, k); }
// a < ↑k
bool oLtK(OptKeyView a, KeyView k) { return a.has_value() && keyLt(*a, k); }
// a <= ↑k
@@ -149,7 +129,6 @@ std::uint64_t rB(const Ctx& c) { return residualB(*c.r); }
std::uint64_t npd(const Ctx& c) { return clampNonposD(*c.r); }
bool allBelowEnd(const Ctx& c) { return c.t == 0; }
bool anyAtOrAboveEnd(const Ctx& c) { return c.t != 0; }
const KeyView& leastReportedK(const Ctx& c, const Obs& o) {
return c.r->reverse ? o.lastK() : o.firstK();
@@ -205,13 +184,16 @@ bool clampNonposBottom(const Ctx& c) {
return niLe(lhs, NatInf::of(bSlack(c)));
}
// The caller reaches this only after `begin.offset >= 1` failed, so the offset
// half of the condition is already known.
bool clampedNonpos(const Ctx& c, const Obs& o) {
return c.r->begin.offset <= 0 && oLeK(c.r->begin.key, leastReportedK(c, o));
return oLeK(c.r->begin.key, leastReportedK(c, o));
}
bool clampsToBottom(const Ctx& c, const Obs& o) {
return c.r->begin.offset <= 0 && oLeK(c.r->begin.key, o.firstK());
}
// Called from the positive branch: if `begin.offset <= 0` then the entry
// dispatch already established `clampedNonpos`, i.e. the selector key is at or
// before the first reported key, so only the offset decides here.
bool clampsToBottom(const Ctx& c) { return c.r->begin.offset <= 0; }
std::uint64_t cELo(Int oB, Int oE) {
return std::max(toNat(2 - oE), toNat(oB - oE + 1));
@@ -253,43 +235,34 @@ OptKeyView crossedBegin(const Ctx& c) {
struct Impl {
// ----- crossed complete --------------------------------------------------
// The complete-crossed subtree is entered only with `!reverse` and `!more`
// (`closedFormCrossed` normalises a reversed complete read first). Within it,
// `crossedCompleteBeginZero` has already claimed every `begin.offset == 0`
// case before these clamp nodes are tried, so `begin.offset < 0` holds here.
bool negCrossClampEndOne(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
c.r->end_.offset == 1 && kLtO(o.firstK(), c.r->begin.key) &&
c.below <= bSlack(c);
// `crossedCompleteEndLeOne` already failed, and for `end.offset == 1` it
// fails exactly when `bSlack >= below`.
return c.r->end_.offset == 1;
}
bool negCrossHeadBottom(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
return c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
o.firstK().empty();
}
bool negCrossClampNonposLastGe(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
c.r->end_.offset <= 0 && kLtO(o.firstK(), c.r->begin.key) &&
oLeK(c.r->begin.key, o.lastK()) && c.below <= bSlack(c);
}
bool negCrossClampNonposBottom(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
kLtO(o.firstK(), c.r->begin.key) && clampNonposBottom(c);
return c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key);
}
bool negCrossClampLongNonpos(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
c.r->end_.offset <= 0 && kLtO(o.firstK(), c.r->begin.key) &&
c.below <= bSlack(c) && clampNonposBottom(c) &&
optLt(c.r->end_.key, c.r->begin.key);
return c.r->end_.offset <= 0 && optLt(c.r->end_.key, c.r->begin.key);
}
ConflictRange closedFormNegCrossClampUnified(const Ctx& c, const Obs& o) const {
OptKey end;
if (negCrossClampEndOne(c, o))
end = clampEndOneUpper(c, o);
else if (negCrossClampLongNonpos(c, o) &&
!negCrossClampNonposLastGe(c, o))
else if (negCrossClampLongNonpos(c, o))
end = clampLongUpper(c, o);
else
end = c.r->end_.key;
@@ -316,9 +289,7 @@ struct Impl {
}
bool negCrossCompleteHigh(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
c.r->end_.offset >= 2 && kLtO(o.firstK(), c.r->begin.key) &&
oLtK(c.r->end_.key, o.firstK());
return oLtK(c.r->end_.key, o.firstK());
}
ConflictRange closedFormNegCrossCompleteHigh(const Ctx& c, const Obs& o) const {
@@ -379,28 +350,17 @@ struct Impl {
// ----- crossed truncated -------------------------------------------------
bool negCrossTruncNonposGap(const Ctx& c, const Obs& o) const {
return o.more && !c.r->reverse && c.r->begin.offset < 0 &&
kLtO(leastReportedK(c, o), c.r->begin.key) &&
c.r->end_.offset <= 0 && c.below <= bSlack(c) &&
kLtO(o.lastK(), c.r->begin.key) &&
kLtO(o.lastK(), c.r->end_.key) &&
!isTop(c.r->end_.key) &&
(optLt(c.r->end_.key, c.r->begin.key) ||
niLt(cardBE(c), NatInf::of(npd(c) + 1)));
}
bool negCrossTruncNonposGapA2aBottom(const Ctx& c, const Obs& o) const {
return negCrossTruncNonposGap(c, o) &&
optLt(c.r->end_.key, c.r->begin.key) && clampNonposBottom(c);
}
// `closedFormForwardTruncatedNonpos` calls this only inside
// `below <= bSlack`, `end.offset <= 0`, `end < begin` and
// `clampNonposBottom`; the remaining Lean conditions of
// `negCrossTruncNonposGap`/`...A2aBottom` are invariants of the readable
// forward-crossed regime.
bool negCrossTruncNonposGapA2aBottomHigh(const Ctx& c, const Obs& o) const {
std::uint64_t extra = o.firstK().empty() ? 1 : 0;
NatInf lhs = NatInf::of(bSlack(c) + 1 + extra);
NatInf rhs = niAdd(NatInf::of(static_cast<std::uint64_t>(c.n)),
cardKO(keyAfter(o.lastK()), c.r->begin.key));
return negCrossTruncNonposGapA2aBottom(c, o) && niLe(lhs, rhs);
return niLe(lhs, rhs);
}
ConflictRange closedFormNegCrossTruncNonposGapA2aBottom(const Ctx& c,
@@ -416,10 +376,10 @@ struct Impl {
return niLe(lhs, NatInf::of(bSlack(c)));
}
// Reached only in the reversed truncation branch (`more` set, `reverse` set,
// `begin.offset < 0`, `end.offset >= 2`).
bool negCrossTruncRevResidualFiniteOver(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK()) &&
kLtO(o.lastK(), c.r->begin.key) && c.t == rE(c);
return oLtK(c.r->end_.key, o.lastK());
}
ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const Ctx& c,
@@ -440,10 +400,7 @@ struct Impl {
}
bool negCrossTruncRevResidualTopLarge(const Ctx& c, const Obs& o) const {
if (!(o.more && c.r->reverse && c.r->begin.offset < 0 &&
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK()) &&
c.t < rE(c)))
return false;
if (!(oLtK(c.r->end_.key, o.lastK()) && c.t < rE(c))) return false;
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)));
@@ -561,10 +518,8 @@ struct Impl {
}
bool zeroCrossUnderEnd(const Ctx& c, const Obs& o) const {
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
c.r->end_.offset >= 2 &&
kLtO(leastReportedK(c, o), c.r->begin.key) &&
optLe(c.r->begin.key, c.r->end_.key);
// The only caller tests `crossedCompleteBeginZero`, i.e. `begin.offset == 0`.
return optLe(c.r->begin.key, c.r->end_.key);
}
ConflictRange closedFormZeroCrossResidual(const Ctx& c, const Obs& o) const {
@@ -598,16 +553,14 @@ struct Impl {
} else if (isTop(r.end_.key)) {
inner = true;
} else {
bool firstFour =
niLe(NatInf::of(b), underBegin) &&
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));
// Readability makes the two cardinality bounds and `ePlusEnd > 0`
// automatic once the first two bounds hold, so only the endpoint
// ordering remains to decide.
bool firstTwo = niLe(NatInf::of(b), underBegin) &&
niLe(NatInf::of(e), underEnd);
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;
inner = firstTwo && bPlusBegin < ePlusEnd;
}
return !inner;
}
@@ -644,7 +597,7 @@ struct Impl {
// ----- complete and forward truncated results ----------------------------
OptKeyView closedFormBegin(const Ctx& c, const Obs& o) const {
return clampsToBottom(c, o) ? bottomKeyView() : c.r->begin.key;
return clampsToBottom(c) ? bottomKeyView() : c.r->begin.key;
}
bool closedFormFinite(const Ctx& c) const {
@@ -655,7 +608,7 @@ struct Impl {
OptKeyView begin = closedFormBegin(c, o);
if (c.r->end_.offset <= 1)
return mk(std::move(begin), c.r->end_.key);
if (clampsToBottom(c, o) || optLe(c.r->begin.key, c.r->end_.key)) {
if (clampsToBottom(c) || optLe(c.r->begin.key, c.r->end_.key)) {
if (c.t == rE(c)) return mk(std::move(begin), after(o.lastK()));
if (allBelowEnd(c)) return mk(std::move(begin), topKey());
if (closedFormFinite(c)) return mk(std::move(begin), after(o.lastK()));
@@ -678,7 +631,7 @@ struct Impl {
}
NatInf closedFormForwardCapacity(const Ctx& c, const Obs& o) const {
if (clampsToBottom(c, o)) return NatInf::of(0);
if (clampsToBottom(c)) return NatInf::of(0);
return cardEB(c);
}
@@ -746,13 +699,11 @@ struct Impl {
}
bool zeroCrossOvershootComplete(const Ctx& c, const Obs& o) const {
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
overshoot(c) && kLtO(leastReportedK(c, o), c.r->begin.key);
return c.r->begin.offset == 0 && overshoot(c);
}
bool negCrossOvershoot(const Ctx& c, const Obs& o) const {
return !c.r->reverse && c.r->begin.offset < 0 && !o.more && overshoot(c) &&
kLtO(o.firstK(), c.r->begin.key);
return c.r->begin.offset < 0 && overshoot(c);
}
bool crossedCompleteOvershoot(const Ctx& c, const Obs& o) const {
@@ -766,21 +717,10 @@ struct Impl {
!(c.below <= bSlack(c) && clampNonposBottom(c));
}
bool zeroCrossLongTop(const Ctx& c, const Obs& o) const {
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
c.r->end_.offset >= 2 && isTop(c.r->begin.key);
}
bool zeroCrossLongFinite(const Ctx& c, const Obs& o) const {
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);
}
// The three `zeroCross*` predicates above cover every `begin.offset == 0`
// case of the complete-crossed subtree, so the mode alone decides here.
bool crossedCompleteBeginZero(const Ctx& c, const Obs& o) const {
return zeroCrossUnderEnd(c, o) || zeroCrossLongTop(c, o) ||
zeroCrossLongFinite(c, o);
return c.r->begin.offset == 0;
}
bool crossedCompleteClamp(const Ctx& c, const Obs& o) const {
@@ -803,21 +743,19 @@ struct Impl {
// ----- crossed truncated dispatch ---------------------------------------
// Every node below is reached only from the reversed truncation branch, where
// `more` is set, `reverse` is set, `begin.offset < 0` and `end.offset >= 2`
// (the `end.offset <= 1` case is handled by the dispatch).
bool zeroCrossTruncRevCrossed(const Ctx& c, const Obs& o) const {
return c.r->begin.offset == 0 && c.r->reverse && o.more &&
kLtO(leastReportedK(c, o), c.r->begin.key);
return c.r->begin.offset == 0;
}
bool negCrossTruncRevUnder(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
kLtO(o.lastK(), c.r->begin.key) &&
optLe(c.r->begin.key, c.r->end_.key) && c.r->end_.offset <= 1;
return optLe(c.r->begin.key, c.r->end_.key) && c.r->end_.offset <= 1;
}
bool negCrossTruncRevCrossedLow(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
kLtO(o.lastK(), c.r->begin.key) &&
optLt(c.r->end_.key, c.r->begin.key) &&
return optLt(c.r->end_.key, c.r->begin.key) &&
c.r->end_.offset <= 1 &&
niLe(niAdd(niAdd(cardEB(c),
NatInf::of(static_cast<std::uint64_t>(c.n))),
@@ -831,31 +769,24 @@ struct Impl {
}
bool negCrossTruncRevBelowEnd(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
kLtO(o.lastK(), c.r->begin.key) && overshoot(c);
return overshoot(c);
}
bool negCrossTruncRevResidualRoom(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
c.r->end_.offset >= 2 && anyAtOrAboveEnd(c) &&
niLe(niAdd(cardOK(c.r->end_.key, o.firstK()), NatInf::of(2)),
return niLe(niAdd(cardOK(c.r->end_.key, o.firstK()), NatInf::of(2)),
NatInf::of(rE(c)));
}
bool negCrossTruncRevResidualTopCrossedSmallGap(const Ctx& c,
const Obs& o) const {
if (!(o.more && c.r->reverse && c.r->begin.offset < 0 &&
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK())))
return false;
if (!oLtK(c.r->end_.key, o.lastK())) return false;
NatInf lhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
NatInf::of(static_cast<std::uint64_t>(c.n)));
return niLt(lhs, NatInf::of(rE(c)));
}
bool negCrossTruncRevResidualTopCrossed(const Ctx& c, const Obs& o) const {
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
c.r->end_.offset >= 2 && kLeO(o.lastK(), c.r->end_.key) &&
c.t < rE(c);
return !oLtK(c.r->end_.key, o.lastK()) && c.t < rE(c);
}
bool revTruncGeTwoTop(const Ctx& c, const Obs& o) const {
@@ -867,7 +798,7 @@ struct Impl {
bool negCrossTruncRevResidualTopLargeCrossed(const Ctx& c, const Obs& o) const {
return negCrossTruncRevResidualTopLarge(c, o) &&
kLtO(o.lastK(), c.r->begin.key) && !isTop(c.r->begin.key);
!isTop(c.r->begin.key);
}
bool revTruncGeTwoLarge(const Ctx& c, const Obs& o) const {
@@ -876,9 +807,7 @@ struct Impl {
}
bool negCrossTruncTopTop(const Ctx& c, const Obs& o) const {
return o.more && !c.r->reverse && c.r->begin.offset < 0 &&
isTop(c.r->begin.key) && isTop(c.r->end_.key) &&
c.r->end_.offset <= 0;
return isTop(c.r->begin.key);
}
bool negCrossTruncTopTopEq(const Ctx& c, const Obs& o) const {