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 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.
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.
| 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 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:
most_facts() (48) or 256 held facts (no ranking dropped a fact);invariant.past keeps the facts of every name outside L’s cone unchanged;.len() fallback was used in the walk.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.
--bounds reports byte-identical on the corpus and the guard’s fallback counted. Linear where the guard holds; today’s cost elsewhere.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.context.returns) links two cones.sv has up to 108 facts at entry (mean 29), so the cap guard would often fail there; loopsN has 54-67 at entry, above 48, so even loopsN may fall back. Measuring the share of loops where the guard holds is the first step of option 1.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.
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).
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)..bounds rows of item 1 unchanged and every *.bounds test unchanged.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.
crates/nikaia-std/src/compiler/bounds_walk.nika and its lowered file (output); the ADR-361 text. No new Rust.--bounds row.most_facts() 48, 256 held. Time before: loops3 1.2 s (target/fast, shared checkout, develop 79ba87701), loops6 27.5 s, sv_strategy.nika 47.7 s (15c267871).Facts: Ledger and bounds proofs, Prover, Measuring
again: its entry facts are neither a subset nor a superset of leaves in 939 of 1050 cases in sv (NOT EXACT).invariant.past, the 48-fact ranking and the stand rule couple the cones (NOT EXACT).sv.