pub function state its preconditions where its body needs a fact about a parameter? (the open package boundary of ADR-396 §4)Open 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: preconditions and postconditions are mandatory on public functions only, and optional inside, said to give Vera’s advantage (every function carries its contract) without its cost (writing one for every helper). The proof, with a run-time check where the proof fails, stays the mechanism. The same question is already left open in ADR-396 §4, Across a package’s boundary.
A precondition is what a function needs from its caller, for example “i is below xs.len()”. In Nikaia a precondition is an assert about a parameter (ADR-269 D4, D5). The compiler proves it at each call where it can, and checks it at run time where it cannot.
Inside one package, the compiler sees every caller. A private function’s preconditions are derived from its callers, so nobody writes them (ADR-391). A pub function is different, because it cannot see who calls it. Its contract is the set of asserts its author wrote. Those are published in the ledger as requires and ensures (ADR-269 D18). The function gets two entries, one checked and one proved (ADR-269 D20). A derived precondition is never published (ADR-391 D4).
So today a pub function may publish no contract at all. Take pub fn at(xs: ref Vec[int], i: int) -> int { return xs[i] }. Nothing tells the caller that i must be in range. The index is checked at run time, and the call stops the program when i is out of range. ADR-396 refuses an index that outside data reaches and the build does not prove (NK1302). It leaves open whether a pub function’s index on its own parameter counts as reached by outside data, and so needs a written precondition or a guard.
The question: should a pub function have to state, as a written precondition or as a guard, every fact about a parameter that its body needs and does not prove? Should the same go for postconditions?
asserts on a pub function are published and are checked at the boundary (ADR-269 D18, D20).NK1263 (ADR-359 D4).assert is refused unless the build says warn (ADR-380).pub boundary is not decided (§4).These facts are from memory, not checked.
| Language | Rule | Cost |
|---|---|---|
| Vera | every function carries requires and ensures |
the author writes a contract for every helper |
| Dafny | contracts are optional, but a call proves only what the callee’s contract says, so in practice every function called across a proof has one | contracts on helpers too |
| SPARK (Ada) | contracts are optional. At the “silver” level every run-time check is proved, so preconditions appear wherever a check needs one | written on any subprogram whose checks need them |
| Eiffel | contracts are optional and checked at run time; a library’s contracts are its documentation | none enforced |
| Rust | no contracts; a pub fn documents its panics in prose (# Panics) |
nothing is checked |
pub function’s contract stays whatever its author wrote. An unproved index or division inside it is checked at run time.
pub function writes pre- and postconditions (the suggestion, read literally).
pub functions have nothing to claim. Mandatory postconditions mostly restate the body. It goes against “whoever uses something pays for it”.pub function must state what its body needs (the narrow form). Take an index, a divisor or an overflow on a parameter that the body does not prove from its own guards. It needs a written precondition on that parameter or a guard in the body; otherwise it is refused, under unproved-claims as NK1302 is. Postconditions stay optional.
pub functions in std and packages/ will need preconditions or guards. The count is not known yet.pub function, so nobody writes it.
Option 3, with a count first. As ADR-396 D4 did, count the sites in crates/nikaia-std/src/tools, packages/ and examples/ that it would refuse, and switch it on once our own code builds under it. Postconditions stay optional: a caller who needs a fact about a result already gets the hint NK1263 (ADR-359 D4).
crates/nikaia-std/src/tools/bounds_*.nika, --asserts and --bounds.std.contracts and the pub functions of packages/, which may need preconditions or guards.NK1302, ADR-396 build), and the divisor/overflow half of ADR-396 §4 (filed alongside).A pub function must state a precondition, or guard the operation itself, for any index, divisor or overflow on a parameter that its body does not prove safe. Otherwise it gets NK1302 under unproved-claims. Postconditions stay optional.
This is a question of correctness. The rollout is done carefully, the same way as #1578:
crates/nikaia-std/src/tools, packages/ and examples/ are listed. This only decides the order of the work. It does not reopen the rule.Generated by Claude Code
pub functions in crates/nikaia-std/src/tools, packages/ and examples/ whose body has an index, a divisor or a plain + - * on a parameter that it does not prove safe. Report them grouped by cause: a missing precondition, a missing std fact, or a prover gap. Post the count here. It sets the order of work and does not reopen the rule.ready). No guard goes into our own code just so that the prover gets through (D8).unproved-claims, a pub function whose body needs a fact about a parameter that it neither proves, guards nor states as a precondition gets NK1302 (an index) or NK1303 (a divisor or overflow, once #1578 has built it). Turn it on only once our own code builds with it.tests/compiler/:
requires;pub function keeps the call-site rule (D1);unproved-claims = "warn" builds with a warning and a check.30-nikaia-tooling.md (NK1302) is already amended. Part I says it where pub and preconditions are described.--both-settings.Generated by Claude Code
Brief queued: issue-1576
agent: builder model: opus max_turns: 200 budget_usd: 30 effort: medium —
Your work order is issue #1576, 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/1576/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-1576.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-1576, Result: partial
Tokens: input 280, cache read 9000947, cache write 252001, output 80333 Cost: 11.51 USD, time 41 min 3 s, turns 140
Result: partial
Files: crates/nikaia-std/src/tools/prove_terms.nika crates/nikaia-std/src/tools/prover_claims.nika crates/nikaia-std/src/tools/prover_passes.nika crates/nikaia-std/src/tools/prover_scope.nika crates/nikaia-std/src/tools/prover_state.nika crates/nikaia-std/src/tools/prover_walk.nika crates/nikaia-std/src/tools/check_places.nika crates/nikaia-std/src/tools/emit_contracts.nika crates/nikaia-std/src/tools/sv_num.nika crates/nikaia-std/src/tools/sysroot_package.nika crates/nikaia-std/src/tools/lowered.rs crates/nikaia-std/src/tools/lowered/prove_terms.rs crates/nikaia-std/src/tools/lowered/prover_claims.rs crates/nikaia-std/src/tools/lowered/prover_passes.rs crates/nikaia-std/src/tools/lowered/prover_scope.rs crates/nikaia-std/src/tools/lowered/prover_state.rs crates/nikaia-std/src/tools/lowered/prover_walk.rs crates/nikaia-std/src/tools/lowered/check_places.rs crates/nikaia-std/src/tools/lowered/emit_contracts.rs crates/nikaia-std/src/tools/lowered/sv_num.rs crates/nikaia-std/src/tools/lowered/sysroot_package.rs tests/compiler/tests/a_guard_with_an_unreadable_part_still_bounds_a_read.nika tests/compiler/tests/a_guard_with_an_unreadable_part_still_bounds_a_read.stdout
Commit: Prover: a guard keeps the parts of its condition it can read, and reads is_empty; pub-function contracts in the tools
Body: The walk splits &&/|| conditions into the parts it can read (claim_parts), so if !f(k) || xs.len() == 0 { return } still bounds xs (before: no fact at all). It reads xs.is_empty() as xs.len() == 0. It records open checks whose path does not carry to the entry (state.unstated) and where each one is (UnwrittenCheck.site); that is the input the #1576 rule needs. Adds preconditions to pub tool functions that lacked one (check_places, emit_contracts, sysroot_package), and a guard in sv_dense_eliminate. The rule itself is not in this commit: it is off, and its draft is saved in docs/briefs/safety/issue-1576.rule.patch.
Part: #1576
Pushed: local 665dc448b372d2f1a7f0170387a22a4a93f74477
Tests: new a_guard_with_an_unreadable_part_still_bounds_a_read passes at –both-settings; the old binary refused it (2x NK1300). These pass: –filter prove_ (70), bounds_reason, outside (12), a_published. lower-std and its fixpoint are clean, clippy -D warnings is clean, fmt is clean.
Gate: green
Deviation: The refusal (NK1302 for pub functions) is built but not switched on, because our own code does not build with it yet: 4 relational prover gaps and 3 sites that need a caller’s postcondition remain (Found). NK1303 waits for #1578. The 5 rule tests and the Part I sentence are in the patch, not committed.
Found: (1) prover gap, a relation between two values: check_core_method:1023 args[last] (code_keys non-empty only when args.len()>0); check_core_stmt:881 names[0] (the bool code_place carries names.len()==1); check_crossings:683 args[0] (the elements of at are below args.len(), and at is non-empty); emit_sums:393 thrown[0] (named_error_of != 0 implies thrown is non-empty). (2) a natural precondition whose caller needs a postcondition: check_core_call_on:1516 config[witness] (needs check_core_method_witness’s ensures); project_settings:353 levels[0] (only Rust callers); check_values:135 given[0] (found.len()==given.len()). (3) existing tests the rule refuses, to adapt when it is switched on: src/over.nika over_at; src/slice_beside.nika slice_tail_first; tests/accepted/738_lent_element_handed_back published; a_published_function_hands_back_an_element_it_is_lent first; bounds_reason_names_where_an_element_comes_from ks[0]; prove_a_checked_read_bounds_its_position twice and keyed; prove_a_found_result_bounds_a_read find and same.
Needs: supervisor decision on the gaps in Found (1): file them as a ready prover issue, then a run applies issue-1576.rule.patch, adds the Found (2) preconditions, adapts the Found (3) tests and switches the rule on.
Changelog: none (no change of behaviour a user sees, apart from fewer false NK1300/NK1207 after guards with an unreadable part or with is_empty())
Issue: Listing for the #1576 comment: packages/ and examples/ have no sites. Tools: missing preconditions, now added: check_places:174, emit_contracts:83/84/219/224, sysroot_package:412, sv_num:157 (a guard). Prover gaps, fixed: unreadable parts of && / || (check_arith:93,134, check_patterns:171, emit_below:274,415, check_simd, check_reads, check_core_stmt:2106,2178) and is_empty() (project_tests:886,998, sysroot_cache:227). Still open: the 7 sites in Found (1)(2), and loop-counter indexes such as sv_pick_column x[j], which the rule does not cover. Issue meets I1-I13.
Owner: none
Disk freed: scratch ov/target; cache 7.4G, / 5.7G free (86%); new entries >100MB: rlib/df8b 288M, run-bundle/88f7 308M, build-time-bundle/ac07 515M, rust-library-splice/0422 692M
Attribution:
Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01UVxJDv4ha75BSeuJhmqFAL
--bounds report and the derived preconditions do not use the walk I changed.--bounds and the D1 derivation are a separate path from prover_walk. In user programs the walk only shows through NK1300 on a std precondition, or NK1207 at a call.unwritten and precondition_at live, and how to tell pub items apart (Item::Fn.is_public, and impls without a trait).nikaia check needs a file argument.prove_ (70), bounds_reason, outside (12), a_published. Wasted: three --bounds probes that could not show the change, and five rule tests I wrote before checking whether the rule could be switched on at all. They ended up in the patch file, not committed.broke I5 (a probe or measure of scope).pub tool functions have only Rust callers (project_settings). rules-gap.rules-gap.Fact: Core | A lower-std that refuses writes no lowered files, so new prover code that makes the tools refuse must be lowered with a copy of the old binary, then built, then lowered again for the fixpoint.Fact: lowering | Uncalled functions in a tools/*.nika give dead_code warnings in lowered/, which fail clippy -D warnings, so a rule kept "off" cannot stay in the tree uncalled.Fact: prover | The --bounds report and the ADR-391 D1 derived preconditions do not show prover_walk facts; to test the walk in a user program, use a std precondition (NK1300) or an NK1207 at a call.My report file has 18 lines, three over the brief’s limit of 15.
Generated by Claude Code