Status: Accepted Date: 2026-11-30 Answers: Should the compiler prove that functions and loops terminate, and how is the measure found, where is it written, how is an endless loop marked, and what happens where no proof is found? (#1577) Supersedes / Related:
nikaia.proofs with their origin, and may be
proposed by an outside program (D6). A measure is found and checked the same
way.while true is the one unconditional loop; it is the
visible mark of a loop meant to run forever.Should the compiler prove that every loop ends and every recursion reaches its base case, and what does it do with a loop or a recursion it cannot show to end?
Today a while whose condition never turns false, or a recursion that never
reaches its base case, builds without a word and hangs when it runs. Every
other property the compiler proves (an index, a divisor, an overflow, an
assert) falls back to a check at run time where the proof is not found;
termination has none. So it cannot default to “proved where possible, checked
otherwise”, and needs its own decision.
| Language / tool | Its answer | What it costs there | Source |
|---|---|---|---|
| Dafny | every loop and recursion must terminate; a decreases measure is guessed (the parameters in order, a loop’s bound) and written where the guess fails; decreases * marks a method that may run forever and may be called only from another such method |
a written measure where the guess fails; the mark spreads to callers | from memory, not checked |
| Lean, Agda, Idris | total by default: structural recursion is accepted, other recursion needs a well-founded measure; partial (Lean) and covering (Idris) mark the rest |
proofs of well-foundedness by hand in the hard cases | from memory, not checked |
| F* | Tot and Div effects: a function that may diverge says so in its type |
the effect is part of every signature | from memory, not checked |
| SPARK | Loop_Variant written by the programmer, proved by GNATprove |
written by hand | from memory, not checked |
| Rust, Go | nothing proved; loop {} (Rust) marks the intended endless loop |
hangs are found by tests | from memory, not checked |
What does not transfer: Dafny, Lean and SPARK ask the programmer for a measure where their guess fails; Nikaia’s rule for invariants is that nobody has to write one (ADR-361 D3), and a measure is the same kind of fact. Lean’s structural recursion relies on inductive types throughout the language; Nikaia’s data is lists, maps and structs, and its loops are mostly counted.
Termination is not proved; hangs are found by tests.
fn find(xs: Vec[i64], v: i64) -> i64 {
let mut i = 0
while i < xs.len() { // ends, but nothing says so
return i if xs[i] == v
i += 1
}
return -1
}
The compiler searches a measure for each loop and recursion, with ADR-361’s machinery, and reports each as proved to end, meant to run forever, or unknown. Nothing is refused and nothing is written.
fn find(xs: Vec[i64], v: i64) -> i64 {
let mut i = 0
while i < xs.len() { // reported: ends, measure `xs.len() - i`
return i if xs[i] == v
i += 1
}
return -1
}
As B, and a loop or recursion with no measure found, no written decreases
and no mark of its own is refused; a function that may run forever carries a
mark that spreads to its callers.
// a sketch of the option: `decreases` is not in the language
fn walk(n: Node?) -> i64 {
let mut at = n
let mut k = 0
while at != null decreases depth(at) { // written where the search fails
k += 1
at = at.next
}
return k
}
C for code run at build time and for functions claimed deterministic or pure; B elsewhere.
| D1 the special case pays, not the common one | D2 the compiler works, not the developer | D3 no cost for the common case | D4 consistent with itself | If it is wrong | |
|---|---|---|---|---|---|
| A | nobody pays; a hang pays at run time | nothing is done by either | none | the one property with neither proof nor check | a hang in a library is invisible in its contract; nobody notices until it hangs |
| B | nobody pays; the unknown loops are listed | the compiler searches; the developer writes nothing | search time at build, under the counted budget; nothing at run time | the same machinery, report and proof book as invariants | the report is read or not; it can be made stricter later without undoing anything |
| C | the loop the search cannot read pays a written measure | the compiler searches; the developer writes where it fails | as B, plus a written measure in the hard cases | new syntax (decreases, a mark) beside a rule that nobody writes an invariant |
a program that built yesterday is refused; how often is not known |
| D | as C, for a part of the code | as C | as C, for a part | two rules for one property | as C, for a part |
C and D can only be weighed on a number nobody has: how many loops and recursions in real code the search cannot show to end. B gives that number and decides nothing that C or D would have to undo.
Option B now, with C as the aim; D is not taken.
A loop is a while or a for. A recursion is a call that can come back to
the function it stands in, directly or through other functions whose bodies
the compiler reads. A call inside the body of a loop or a recursion is taken to
return: whether the callee ends is the callee’s own question, asked where it is
written. A for over a range or over a collection the body does not change
ends by construction and needs no search.
A measure is a whole-number term over the names a loop or a recursion
changes, or a tuple of such terms ordered lexicographically. It shows the loop
or recursion ends when two queries are proved with their certificates: it is
never below a bound where a turn or a call begins, and every turn, every way
back to the loop’s head, and every recursive call makes it strictly smaller.
It is searched as one more strategy of ADR-361 D5’s portfolio, under the
build’s counted budget: candidates come from the loop’s condition (n - i for
i < n), from a length the loop walks (xs.len() - i), from a parameter a
recursive call makes smaller, from the program’s asserts, and from a
proposer (ADR-361 D6). A measure that passes is kept in nikaia.proofs with
what found it, as an invariant is; where it came from never decides whether it
is believed. No syntax is added: there is no decreases.
while true is a loop meant to run forever.A while true is reported as meant to run forever, whether or not a break
or a return leaves it, and no measure is searched for it. It is the mark
ADR-276 already makes visible (D6: a while true nothing leaves needs no
return after it). There is no other mark.
--terminates, on nikaia build, check and lower and on lower-std,
lists every loop and recursion by line as ends, with its measure and what
found it; meant to run forever (while true); or unknown, with the
loop or the cycle of calls and the names it changes. It counts each. No answer
is an error, a warning or a code; the build is the same with or without the
flag, and termination is not published in the ledger.
The next version of this record decides C - a loop or recursion proved, marked
or refused, a written measure where the search fails, and a mark for a
function that may run forever - once lower-std --terminates has counted the
unknown loops and recursions over std and the compiler’s Nikaia code. C is
the aim; the count decides its form (whether a written measure and a function
mark are needed, and how often), not whether it comes.
D was not taken: two rules for one property, where B’s count can show whether one rule for all code is affordable.
std and the compiler.--terminates report (Part III 14.2), and the count with lower-std
(#1577).decreases or a function mark is taken, and
how a mark spreads to callers.std): D1 takes them to return, and
whether that is checked is asked with C.