ADR-391: The Compiler Derives What It Can and Keeps It — a Private Function’s Preconditions From Its Callers, and What a Method Changes From Its Body

Status: Accepted Date: November 7, 2026 (supervisor’s decision, by the language principles, on the owner’s direction that the compiler derives as much as it can itself, in small pieces it stores and reuses) Answers: #1053 (the facts the walk lacks) Related: ADR-269 D5 (a precondition is an assert at the top), D18 (only what is written is a contract); ADR-270 D19-D22 (nikaia.proofs); ADR-306 (the walk of the checks); ADR-361 D3, D4, D13 (nobody has to write an invariant; every one used is checked); ADR-369 D4 (found rules of a type)

1. Question

Half the index and arithmetic checks in tools/*.nika stay at run time (#1053: 3049 of 6009 proved on November 7, 2026). The solver is not what stops them: a new engine added three sites. The facts are missing, and most of them are in the program already:

The owner’s direction (2026-11-07): the programmer writes as little as possible; the compiler derives what it can, in small pieces, stores what it derived and uses it again.

2. How others do it

3. Decisions

D1 — A function no other package can call takes, as facts, what all its callers in the package prove about its arguments.

For a function that is not pub (or whose package is a program), the walk gathers at each call site the facts it holds over the arguments, in the callee’s parameter names and their lengths (at <= arms.len(), 0 <= at), and proposes as the callee’s derived preconditions the claims every call site proves. A claim is proposed from the shapes the walk already reads: a comparison between an argument and a length or a constant at the call site, and the facts a call site’s guard gives. Each is then a fact at the callee’s entry, as a written assert at the top is (ADR-269 D5), and an obligation at every call site, asked like any other claim (ADR-361 D4). A function taken as a value (a lambda, a function pointer) has call sites the walk does not see and derives none. A derived precondition is never a refusal: a caller that does not prove it removes it, and the checks it would have removed stay.

Widened (supervisor, 2026-11-15, #1673): the length side of a proposed claim may be a linear term of one length and constants, c * xs.len() + d with c and d constants the walk knows (l < 2 * value.len(), at <= xs.len() - 1), when every call site proves that term. The callee then reads the claim like any written assert of the same shape, so xs[l >> 1] under l < 2 * xs.len() and 0 <= l is proved.

Widened (supervisor, 2026-11-16, by the language principles: the owner’s direction that the compiler derives what it can; 132 sites in private functions stayed checked because the proposals took too few shapes): a proposed claim may also be

Each is proposed only when every call site proves it, and is a fact and an obligation like any other derived precondition; nothing else in D1-D4 changes.

Changed by ADR-396 D1: a caller that fills a parameter with data from outside the program and does not prove a derived bound of an index on it is refused, NK1302.

D2 — What a function may change is derived from its body.

For every function the walk derives its frame: the paths it may write through its mut parameters and mut self (a field, a field’s length, an element), by its own assignments and by the frames of what it calls, over the call graph to a fixed point; a call the walk cannot resolve writes everything. At a call, the walk forgets only the facts about the paths in the callee’s frame. A frame changes no meaning and no check; it only keeps facts the walk now throws away.

D3 — A derived fact is kept, not searched again.

A derived precondition’s obligations and a frame’s uses are claims like any other: their answers are kept in nikaia.proofs (ADR-270 D19-D22), checked when read, written by a normal build and compared under --locked. The proposals themselves are cheap and read off the source in each build; what is kept is what costs, the proofs. An edit to a callee or a caller changes the claims it asks and so the entries it uses; nothing else is searched again.

D4 — Derived is not promised.

A derived precondition or frame is never published in the ledger, never checked when the program runs, and never part of a function’s contract (ADR-269 D18): a pub function’s preconditions are the ones its author writes. --asserts and --bounds show a derived precondition as such, with the call sites that prove it, so a reader sees what carried a proof.

4. Considered and rejected

Asking the author for an assert at the top of every private function. It is what ADR-269 D5 offers today and what the owner’s direction rules out: the facts are in the callers already.

Storing the derived claims themselves in nikaia.derived. That record is what a package’s contract was derived from (ADR-251 D1); a derived precondition is not a contract, and the proofs that matter are already kept in nikaia.proofs.

Publishing frames in the ledger for other packages. Useful later, beside touches, but it widens what a package promises; it waits for a need across a package boundary.

5. Consequences

This site is open source. Improve this page.