/ and % in --boundsOpen and answer this issue on GitHub.
ready · opened 2026-11-12 · updated 2026-11-15 · 4 comments
An outside consultancy made suggestions to the owner on 2026-11-12. This is one of them: safe arithmetic, with division by zero and overflow either proved impossible or handled explicitly, never silently wrapping and never x / 0 = 0. Most of this is already decided in Nikaia. This issue files only the part that is missing.
Integer arithmetic has two run-time failures: overflow, where the result does not fit the type, and division by zero, which covers / and %. Languages pick among wrapping silently (C for unsigned values, Go), defining x / 0 as 0 (Pony, some verification languages), stopping the program, or proving at build time that the failure cannot happen.
Nikaia already stops the program on both, and drops the check where it is proved. For indexes, ADR-396 goes one step further: an index that data from outside the program reaches, and that the build does not prove, is refused (NK1302, #1572). ADR-396 §4 leaves open whether a divisor and an overflow reached by outside data are refused the same way. It says this “waits on the count”.
A second, smaller gap is the report. nikaia build --bounds (#389) lists each index and each +, - and * as proved or checked. It does not list / or %, so an author cannot see which divisions still carry a check.
The question: should a divisor or an arithmetic operation that outside data reaches, and that the build does not prove safe, be refused like an index? And should --bounds list / and %?
&+, which ADR-285 D6 rejected (ADR-315).x / 0 = 0. Division by zero is unrecoverable at run time (Part I, 10-nikaia-light.md). A constant zero divisor is NK1118, and checked_div gives null.NK1207), and the divisor check is never dropped silently (ADR-395 D1-D3).NK1302, #1572).These facts are from memory, not checked.
| Language | Overflow | Division by zero |
|---|---|---|
| Rust | panics in debug builds, wraps in release builds unless configured | panics |
| Swift | traps; &+ wraps |
traps |
| Zig | undefined behaviour in release-fast builds, checked in safe builds | the same |
| SPARK | proved absent at the “silver” level, or refused | proved absent, or refused |
| Dafny | mathematical integers with no overflow; machine types need proofs | the divisor must be proved non-zero (a precondition of /) |
| Pony | wraps | x / 0 = 0 |
| Vera, AILANG, Boruna | not known to me |
--bounds keeps silent on / and %.
--bounds lists / and % as proved or checked, with what reaches the divisor, as it does for +, - and *. Nothing new is refused.
crates/nikaia-std/src/tools, packages/ and examples/, an unproved divisor or overflow that outside data reaches is refused with a code next to NK1302, under unproved-claims. The author fixes it with a guard, a checked_* call or a precondition.
Option 2 now. It is small: bounds_report_checked_op in crates/nikaia-std/src/tools/bounds_report.nika returns true only for Add, Sub and Mul, so it can take Div and Rem as well, and Part III’s --bounds text needs the matching change. Then decide between 3 and 4 on the count that option 2 gives, as ADR-396 D4 did for indexes.
crates/nikaia-std/src/tools/bounds_report.nika (bounds_report_checked_op), and the bounds walk.30-nikaia-tooling.md (--bounds), plus a new NK code next to NK1302 if option 3 or 4 is taken (to be reserved by the supervisor).NK1302), #1567, #682, #632, #1053, and the public-function half of ADR-396 §4 (#1576).This is a question of correctness, not of a count. The rule for indexes in ADR-396 applies the same way to /, % and overflow of +, - and *. An operation that outside data reaches, and that the build does not prove safe, is refused, with a code next to NK1302 under unproved-claims.
Overflow: the rule applies only to the plain operators. Where the author has chosen how overflow is handled, for example with wrapping_*, saturating_* or checked_*, that choice is the handling and nothing is refused. Only a plain +, - or * that is not proved safe on outside data is refused.
Rollout, done carefully, because the prover is not yet strong enough in many places:
--bounds lists / and % as proved or checked, as it already does for +, - and * (bounds_report_checked_op in crates/nikaia-std/src/tools/bounds_report.nika, and Part III 30-nikaia-tooling.md).crates/nikaia-std/src/tools, packages/, examples/) builds with it.--bounds report for / and %, with a test in tests/compiler/.unproved-claims, with tests: refused on outside data, accepted when proved, and accepted with wrapping_*, saturating_* or checked_*.Generated by Claude Code
Brief queued: issue-1578
agent: builder model: opus max_turns: 200 budget_usd: 30 effort: medium —
Your work order is issue #1578, inlined below with its comments. There is no
brief written around it: plan your own steps from its To build list, build
them in order, commit after each finished step, and end when its Done when
holds (or at the limits of builder.md, reporting what is left). The
facts in your prompt are Core of docs/builder-facts.md; more on a topic:
nikaia run scripts/syntax.nika -- KEYWORD, a probe: nikaia run scripts/probe.nika -- SNIPPET.
Measure the issue against docs/issues.md while you work: what it lacked,
what was wrong in it. That answer goes into the report’s Issue: field.
A finding, a changed plan or a finished item also goes into the issue as a
comment (gh api repos/Nikaia-Foundation/Nikaia/issues/1578/comments -F body=@file,
ending with the Claude Code footer), rule I13.
develop in the main checkout; other agents’ uncommitted files are not yours: never touch or stage them.target/fast/nikaia run scripts/commit.nika -- REPORT (it adds exactly the report’s Files:); the driver pushes.cargo build --profile fast -p nikaia, lower-std and its fixpoint when a tools/*.nika changed, cargo fmt, cargo clippy --all-targets -- -D warnings, nikaia run scripts/pre-push.nika, the tests the issue names and the ones your change touches.CHANGELOG.md, badges and NK codes are the supervisor’s. New tests in Nikaia only.Body:. Found: is only for what lies outside the area, cannot be fixed this run or needs a decision; the driver files it as an issue.std or the toolchain lacks stops the work, unless an ADR or Part I-III refuses it (name it): Result: blocked, Found: the gap with a probe, Needs: supervisor decision.NIKAIA_CACHE_LIMIT=2G; at the end delete your own clone’s target/ and target/release/nikaia-scratch.Result: partial with what you ruled out, the probe, and the suspected file and function./home/user/Nikaia/docs/briefs/safety/issue-1578.md.report.md)Fields: Result, Files, Commit, Body (optional), Fixes, Pushed, Tests, Gate, Deviation, Found, Needs, Changelog, Issue, Owner (a question for the owner), Disk freed, and last Attribution: with your commit trailer lines below it.
Result:, Gate: (green or red <check>), Deviation:, Found: and the other fields with nothing to say hold the bare word: done, none. Anything after it reads as content.Result: done only when the issue’s Done when holds; partial with the items left otherwise.Deviation: only for an item of the issue built otherwise than written, or not built. Found: only for a defect outside your area, or one this run cannot fix. Everything else goes into Body:; how you verified goes into Tests:.Issue: by docs/issues.md, How a run judges its issue: complete, or broke I<n>: ..., rules-gap: ..., wrong I<n>: ..., joined with ;.done <sha>, report <path> (or BLOCKED: ... / PUSH REJECTED ...).Generated by Claude Code
Run: issue-1578, Result: partial: step 2 built (–bounds lists / and % proved or checked); left: step 3 (the prover and fact gaps behind std’s checked divisions, each its own issue) and step 4 (NK1303 refusal under unproved-claims, switched on once our code builds with it)
Tokens: input 184, cache read 6223045, cache write 138487, output 38881 Cost: 6.59 USD, time 0 min 14 s, turns 92
Result: partial: step 2 built (–bounds lists / and % proved or checked); left: step 3 (the prover and fact gaps behind std’s checked divisions, each its own issue) and step 4 (NK1303 refusal under unproved-claims, switched on once our code builds with it)
Files: crates/nikaia-std/src/tools/check_facts.nika crates/nikaia-std/src/tools/check_core_ops.nika crates/nikaia-std/src/tools/check_core_stmt.nika crates/nikaia-std/src/tools/bounds_walk.nika crates/nikaia-std/src/tools/bounds_frame.nika crates/nikaia-std/src/tools/bounds_proven.nika crates/nikaia-std/src/tools/bounds_report.nika crates/nikaia-std/src/tools/lowered/check_facts.rs crates/nikaia-std/src/tools/lowered/check_core_ops.rs crates/nikaia-std/src/tools/lowered/check_core_stmt.rs crates/nikaia-std/src/tools/lowered/bounds_walk.rs crates/nikaia-std/src/tools/lowered/bounds_frame.rs crates/nikaia-std/src/tools/lowered/bounds_proven.rs crates/nikaia-std/src/tools/lowered/bounds_report.rs crates/nikaia/src/bounds_report.rs docs/specification/30-nikaia-tooling.md tests/compiler/tests/a_divisor_is_listed_proved_or_checked.nika tests/compiler/tests/a_divisor_is_listed_proved_or_checked.bounds tests/compiler/tests/a_divisor_is_listed_proved_or_checked.stdout
Commit: –bounds lists / and % as proved or checked (ADR-396 D7)
Body: The checker records each /, %, /=, %= on whole numbers with a non-literal divisor in a new fact CheckFacts.divisions (kept apart from arithmetic, which drives unchecked emission: a divisor’s check is never dropped, ADR-395 D3). The bounds walk proves it with goal d != 0 and, for signed types, d != -1 || dividend > MIN (fallback: the divisor’s range), recording the site in proven.arithmetic; every reader there already gates / and % apart, so nothing emitted changes. bounds_report.nika lists the sites with / and %. Rust added: one plumbing line in crates/nikaia/src/bounds_report.rs (divisions into BoundsReportFacts), for #557/#125. Spec 30 --bounds names the divisions.
Part: #1578
Pushed: local a9337462a066eb169bb764a063342b83d2ba95dd
Tests: new tests/compiler a_divisor_is_listed_proved_or_checked (–both-settings ok; FAILED on the unfixed compiler, which lists no division); .bounds tests with divisions (a_name_set_only_to_the_for_binding_lies_below_it, prove_an_early_return_on_a_comparison_bounds_what_follows) ok unchanged; it bounds_report:: facts_of_the_walk:: field_paths:: prove_through_loops:: fold_counters:: expression_functions:: 40 ok; lower-std fixpoint clean; clippy, fmt clean; std tools lower-std –bounds: 19 divisions proved, 43 checked (step 3 input), none refused
Gate: green
Deviation: none
Found: lowering defect (views lane): a let bound to an if choice of two owned String locals that are read afterwards, then returned, passes check and fails in rustc (E0515, NK1343 internal compiler error); probe: fn make(s: String) -> String { let mut g = String() g.push_str(s) return g } / fn pick(x: i64) -> String { let a = make("a") let b = make("b") let named = if a != "" { a } else { b } if x > 1 || a == b { return "" } return named } / fn main() throws { println(pick(0)) }; without the later a == b it runs
Needs: none
Changelog: –bounds lists every / and % whose divisor is not a literal, as proved (divisor nonzero and, signed, not -1 against MIN) or checked with the reason (ADR-396 D7, #1578); the division’s check stays either way (ADR-395 D3).
Issue: broke I8: no Done when; rules-gap: step 1 (ADR-396 §4 text) and the NK1303 code were already on develop (b7abcc78), the issue does not say so
Owner: none
Disk freed: none (cache 7.4G kept, 5.9G free on /, above the 3G floor); new cache entries over 100 MB: rlib/df8bcfe6 288M, run-bundle/88f74747 308M, build-time-bundle/ac07d071 515M, rust-library-splice/0422fba8 692M; scratch old binary deleted
Attribution:
Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01UVxJDv4ha75BSeuJhmqFAL
lower-std plus build. Any change to a bounds_*.nika re-proves std, so each cycle took 10+ minutes. Two of them were lost to slips: a ref String passed to insert (NK1102), and a let bound to an if choice of two locals that the lowering turned into an E0515 rustc error. The context ran out once along the way.proven.arithmetic gate / and % apart (emit_contracts, panics.nika, emit_order). I also had to learn that the arithmetic fact drives unchecked emission, so divisions needed their own fact. BoundsReportFacts is built in Rust (crates/nikaia/src/bounds_report.rs), and --bounds is in spec 30 at about line 476. The brief could have named all of these.--bless, --both-settings, and against the old binary; the two existing .bounds tests that have divisions; six it modules; fmt, clippy and pre-push; and std’s lower-std --bounds count.
cargo test --lib bounds_report:: run, which matched 0 tests. The std count was useful, but only as input for step 3.tools/*.nika, never bind let x = if c { a } else { b } over owned locals you read again; the lowering may borrow them”. Also: before the first cycle, run nikaia check on the edited tools file alone with the old binary. That catches NK1102-type slips in seconds instead of minutes.broke I8: no Done when.rules-gap: step 1 (the ADR-396 text) and NK1303 were already on develop (b7abcc78), and the issue doesn’t say so.broke I11: it names no files, though the walk, the checker fact, bounds_report.nika and spec 30 are the obvious set.rules-gap: no note that arithmetic drives unchecked emission, so / and % must not be added to it.| Fact: Lowering | let x = if c { a } else { b } over owned String locals that are read again later passes check but lowers to a borrow of a temporary (E0515, NK1343); the fix in the compiler is still open. |
| Fact: Core | CheckFacts.arithmetic is read by the emitter to drop checks, so a new kind of site the bounds walk should list (like divisions) needs its own fact, not an entry there. |
Generated by Claude Code
Supervisor, 2026-11-15: To build items 1 and 2 are done (the ADR-396 D7 text and NK1303 in b7abcc78; --bounds lists / and % in a9337462, pushed in 9a56560f). The rest is split:
ready, queued in the safety lane);NK1303): #1661 (blocked on #1660).This issue stays open until both are built.
Generated by Claude Code