Issue #2697: The bounds walk walks a loop’s rest again within one pass from an equal state

Open and answer this issue on GitHub.

blocked, solver · opened 2026-11-22 · updated 2026-11-22 · 1 comment

Part of #2474 Found while #2652 After #2711

Goal

Within one pass of the bounds walk, a walk past a loop whose complete entry state equals that of an earlier walk in the same pass is not walked again: its outcome and its side effects are replayed. The walk proves exactly what it proves today (owner rule).

Today (read in the code)

Measured (commit 15c267871, instrumented scratch build of bounding_past_loop logging a hash kst of the facts, alpha-renamed, plus every BoundsMark field and the written, poisoned and missed-goal sets, and ko of the outcome)

Memo key and scope, packages/logic/src/sv_strategy.nika Duplicate calls Calls skipped Inconsistent outcomes
kst, within a pass 1760 of 7989 2537 (32 %) 0
kst, across passes 6184 7117 390
facts only, across passes     3033
facts only, within a pass     25

In loopsN (below, N loops) the 2^N − 1 entry states within a pass are all distinct (each again starts from new invariant facts): 0 saved there. In sv 99.2 % of the walks lie at nesting ≥ 3. The invariant searches with the full key (entry facts plus walk state) repeat 1604 of 2106 times in sv (82 % of the proofs asked in searches), nearly all across passes, which the kept-proof issue (#2711) removes in part (measure again after it); within a pass this memo covers them.

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.

Why it is exact

The walk is deterministic: a walk with equal complete input gives equal output. Complete input needs all four: (1) the state outside BoundsMark listed above in the key, or replayed from the entry; (2) the key alpha-renamed over the entries / codes names and the stored outcome renamed back to the current counters; (3) a structural key of the terms, not their ids; (4) the round inputs (context.returns, the summaries of other functions, assumed, assumed_ends, collecting), which the memo held only within one pass keeps fixed. Without (4) the across-pass figure shows 390 inconsistent outcomes; without the walk state, 25 within a pass.

Cause

traced for the cost (the calls counted above; 81-84 % of the Ir is the invariant search inside these walks, callgrind on chained and loops6). Which repeat inside a pass gives the 1760 duplicates (the same loop reached by the leaves and again walks of an outer loop with equal state) is suspected; item 1 shows it.

To build

  1. A structural, alpha-renamed key of a walk’s complete entry state (facts, BoundsMark, the outside fields above, the position), behind NIKAIA_BOUNDS_MEMO=count: a stderr line per pass with the duplicates it would skip. Test: tests/compiler/tests/the_walk_memo_counts_repeated_walks.nika with .stdout, .stderr and .bounds (a body of two nested loops each followed by a third loop, so an inner state repeats; the builder writes it and checks with scripts/probe.nika --check). If no repeat inside a pass shows on it or on sv after the kept-proof issue (#2711) landed, stop: Result: blocked, Found: the counts.
  2. The memo: keep each walk’s outcome and its side effects on the outside fields and the counters, renamed to the key’s names; on a hit, replay them renamed back instead of walking. Test: the same .bounds rows as without the memo on the test of item 1, on loops3 above, and every *.bounds test unchanged.

Done when

nikaia check --bounds and lower-std --bounds give byte-identical reports before and after, bytes::the_corpus_needs_no_tether passes, and the calls of bounding_past_loop within a pass on sv_strategy.nika fall by the share item 1 measured (32 % at 15c267871); the commit of item 2 carries Fixes #N. The report gives a Found: line per finding.

Files and limits

Facts: Ledger and bounds proofs, Prover, Measuring

Ruled out


Generated by Claude Code

Comments

keywan-ghadami, 2026-11-22

Driver: not queued, this issue waits for #2695, which is still open (#1819). Set ready again when it is closed.


Generated by Claude Code

Back to Open Issues