Issue #1577: Decision: should the compiler prove that functions and loops terminate (a guessed decreasing measure, decreases only where the guess fails, endless loops marked)?

Open and answer this issue on GitHub.

ready · opened 2026-11-12 · updated 2026-11-30 · 1 comment

Where this comes from

An outside consultancy made suggestions to the owner on 2026-11-12. This is one of them: Dafny’s termination proof, but with the measure guessed automatically, a decreases clause written only where the guess fails, and loops meant to run forever marked explicitly. No ADR and no issue covers termination yet.

The question, from zero

A program terminates when every call returns and every loop ends. Today the Nikaia compiler proves facts about values: indexes, divisors, overflow and asserts (ADR-269, ADR-306, ADR-395, ADR-396). It proves nothing about whether a loop or a recursion ends. A while whose condition never turns false, or a recursion that never reaches its base case, builds without complaint and hangs at run time.

The usual proof of termination is a measure: a number that is never negative and that every loop iteration or recursive call makes smaller, such as xs.len() - i, or n in f(n - 1). Such a number cannot shrink forever, so the loop ends. Dafny asks for the measure in a decreases clause, and guesses it for simple cases.

The question: should Nikaia prove termination? If so, how is the measure found? Where is a decreases written? How is a loop marked that is meant to run forever, such as a server loop, an event loop or a task that waits on a channel? And what happens to a function the prover cannot show to terminate: refused, a warning, or only reported?

What exists that it builds on

How others do it

These facts are from memory, not checked.

Language Rule
Dafny every loop and recursion must terminate. A decreases measure is guessed (the parameters in order, or the loop bound) and written where the guess fails. decreases * marks a method that may run forever, and it can only be called from other decreases * methods
Lean, Agda, Idris total by default. Structural recursion is accepted automatically, others need a well-founded measure. partial (Lean) and covering (Idris) mark the rest
F* Tot versus Div effects: a function that may diverge says so in its type
Vera contracts on every function. Whether it proves termination is unknown to me
AILANG, Boruna, Mojo no termination proof that I know of
Rust, Go none. loop {} marks the intended endless loop, but nothing proves the others end

Options

  1. Do nothing. Termination is not proved, and hangs are found by tests.
    • For: no work, and no false refusals.
    • Against: a hang in a library is invisible in its contract.
  2. Report only. The compiler tries a measure (guessed, or found as in ADR-361). --bounds / --asserts, or a new --termination, lists each loop and recursion as proved to end, meant to run forever (while true), or unknown. Nothing is refused.
    • For: cheap, it tells the author where the risk is, and it gives a count to decide on.
    • Against: nothing enforces it.
  3. Proved or marked, refused otherwise (Dafny’s rule with a guessed measure). Each loop and recursion is proved to end by a guessed measure, has a written decreases, or is marked as endless (while true, or a function-level mark that spreads to its callers like Dafny’s decreases *). Otherwise the build refuses it under unproved-claims.
    • For: termination becomes part of the contract, and an AI author gets one more fact checked.
    • Against: new syntax (decreases, a function mark). Some code the prover cannot handle needs a written measure, for example loops on data read from outside, or while loops over a linked structure. The count is unknown.
  4. Only for build-time code and for functions claimed deterministic or pure. Terminating is required where it matters most, for meta and comptime code and for #1435’s derived properties, and reported elsewhere.
    • For: smaller blast radius, and it replaces the step budget’s guesswork where a proof is found.
    • Against: two rules.

Recommendation

Option 2 first, then decide between 3 and 4 on the count. Build the measure search on ADR-361’s machinery: a ranking function is checked like an invariant. Treat while true as the existing endless mark, so no new keyword is needed for it. Take a decreases keyword and a function-level “may not terminate” mark only if the count shows the guess fails often enough to need them.

What it touches

Comments

keywan-ghadami, 2026-11-30

Owner’s answer, 2026-11-30: option 2 now, option 3 the aim.

Recorded in ADR-termination-v1 (D1-D5) and Part III 14.2 (--terminates), commit 234d5685.

To build

  1. The measure search as a strategy of ADR-361 D5: candidates from the loop’s condition, a walked length, a parameter a recursive call makes smaller, the program’s asserts and a proposer; two queries with certificates (bounded below where a turn or call begins, strictly smaller every turn, way back to the head and recursive call); a measure that passes is kept in nikaia.proofs with its origin (ADR-termination-v1 D1, D2).
  2. The --terminates report on nikaia build, check, lower and lower-std: every loop and recursion by line as ends (measure and origin), meant to run forever (while true) or unknown (the loop or cycle of calls and the names it changes), with the counts; the build is the same with or without the flag (D3, D4). Tests as .nika programs in tests/compiler/tests/ with their .stdout: a counted while, a for, a recursion on n - 1, a while true, and a loop over a linked structure that stays unknown.
  3. The count: lower-std --terminates over std and the compiler’s Nikaia code, the unknown loops and recursions by shape, as a comment here; it decides the next version of the record (D5).

Done when

--terminates reports the five test shapes as above, lower-std --terminates runs to the end on develop and its counts are a comment on this issue; the commit that adds the report carries Fixes #1577.


Generated by Claude Code

Back to Open Issues