Issue #2698: Decision: may the bounds walk factorise its stand rule by loop cones under a guard, after a written proof? (ADR-361 D10)

Open and answer this issue on GitHub.

decision, solver · opened 2026-11-22 · updated 2026-11-22 · 0 comments

Part of #2474 Found while #2652

The question

The bounds walk is 2^n in the number of loops in a row (ADR-361 D10: two walks past each loop). Should it get a cone-factorised stand rule that is linear where a run-time guard holds and today’s walk where it does not, once a written proof shows the guard keeps every proof? Owner decision: it changes the walk’s decision rule, and the owner rules that no proof is traded for speed.

Today (read in the code)

bounding_past_loop in crates/nikaia-std/src/compiler/bounds_walk.nika walks the rest of the body once from the facts that leave the loop (leaves) and once from the invariant facts bounding_loop_invariant found (again), and keeps again only where it stands: where it proves at least as many of the open goals of the whole rest of the walk. Each walk does the same at every later loop, so a body of N loops in a row is walked 2^N − 1 times per pass, in 4 checks passes and 8 returns passes: exactly 12·(2^N − 1) calls.

Measured (commit 15c267871)

Probe Calls C / R check --bounds
loops1 4 / 8 0.14 s
loops3 28 / 56 1.13 s
loops6 252 / 504 27.5 s
packages/logic/src/sv_strategy.nika 4505 / 3484 47.7 s

Time grows 2.6-3.1 times per loop; the 2^n dominates from n = 3. Of 16 831 functions, 463 have at least 3 loops finished before some statement, 74 at least 6 (largest 16, in crates/nikaia-std/src/compiler). In sv, 99.2 % of the walks lie at nesting ≥ 3.

fn loops(a: ref Vec[i64], lo: i64, hi: i64) -> Vec[i64] {
	assert(0 <= lo)
	assert(lo <= hi)
	assert(hi <= a.len())
	let mut out: Vec[i64] = []
	let mut i1 = lo
	for j in lo..<hi { if a[j] < 1 { i1 = i1 + 1 } }
	out.push(a[i1])
	let mut i2 = lo
	for j in lo..<hi { if a[j] < 2 { i2 = i2 + 1 } }
	out.push(a[i2])
	let mut i3 = lo
	for j in lo..<hi { if a[j] < 3 { i3 = i3 + 1 } }
	out.push(a[i3])
	return out
}

fn main() {}

nikaia check --bounds loops3.nika (target/fast, built 2026-11-22, shared checkout; checked with scripts/probe.nika --check --file, exit 0): 6 indexes, 3 proved; 3 operations, 3 proved; the three a[iK] stay checked: a counter the loop on line 7 changes, with no bound tied to its turns or to another counter (ADR-361 D13, #1634), 1.2 s. loopsN below is this program with N loops.

The idea

The choice at loop L compares the proof counts of two subtrees. Split the open goals after L into cones: the goals reading names a loop changes, grouped so that cones share no loop. Suppose the guard holds:

Then a subtree’s count is a sum over cones, and the choice at L depends only on L’s cone. Each cone costs 2^(its loops) walks; in loopsN that is n cones of one loop: 2n walks instead of 2^n − 1. Where the guard fails, the walk is today’s walk.

Options

  1. Build it after a written proof. First the proof (an ADR text under ADR-361) that under the guard the factorised choice equals today’s at every loop, and that the guard is checked completely at run time; then the build, with --bounds reports byte-identical on the corpus and the guard’s fallback counted. Linear where the guard holds; today’s cost elsewhere.
  2. Keep today’s walk and take only the exact constant factors (the pass-skip (#2695), query-key (#2696) and walk-memo (#2697) issues of the solver lane): up to 4 times fewer walks, 10-25 % cheaper walks, still 2^n.
  3. A cheaper rule that may prove less (skip again, cut to the cone without the guard, the trial-plus-rerun hybrid as measured): ruled out by the owner rule; listed only so the choice is complete.

What is not shown yet

Recommendation

Option 2 now (its issues are ready), and option 1 as a research step: a builder measures, behind a counter, how often the guard holds on the corpus and sv; only if it holds for most 2^n bodies is the proof written and the build filed. Nothing that may prove less is built.

Cause

traced: the walk counts above (12·(2^N − 1), counted in an instrumented build) and callgrind (the invariant search inside these walks is 81-84 % of the Ir on chained and loops6).

To build (if option 1 is chosen)

  1. A counter behind NIKAIA_BOUNDS_CONES=count: per loop, whether the guard holds and the cones. Test: tests/compiler/tests/the_walk_counts_its_cones.nika with .stdout, .stderr, .bounds (loops3 above).
  2. The proof as ADR text under ADR-361 (supervisor; owner approval).
  3. The factorised rule under the guard, falling back to today’s walk. Test: the .bounds rows of item 1 unchanged and every *.bounds test unchanged.

Done when

The owner chose an option; for option 1, nikaia check --bounds and lower-std --bounds give byte-identical reports before and after item 3, bytes::the_corpus_needs_no_tether passes, and loops6 takes at most 2·6 walks per pass where the guard holds; the commit of item 3 carries Fixes #N. The report gives a Found: line per finding.

Files and limits

Facts: Ledger and bounds proofs, Prover, Measuring

Ruled out

Back to Open Issues