Appearance
Go condition-only loops that count
Status: accepted · Date: 2026-11-30
Go has no while; for cond { … } takes its place. A general condition loop has no termination measure, so the proved path refused all of them. This record accepts the common counting shape and proves it through the existing counted loop.
The decision
- The interpreter (
tolean/Go/Spec/Eval.lean) and the translator (tolean/Go/Translate/Pure.lean) acceptfor i < b { ss; i++ }when the body ends withi++,ssdoes not writei, the body has nocontinueat any depth, andbdoes not readi. Eval.whileFormrewrites such a loop asfor i' := i; i' < b; i'++ { i = i'; ss; i++ }, wherei'isEval.whileIndex i. The counteriis an outer local, so it keeps its last value after the loop, as in Go: the bound after a normal end, the value at thebreakafter a break. The trailingi++matters only on the last iteration, where it setsito the bound.- The interpreter runs this loop over
Go.upTo i b, withbevaluated once. That is exact, because the body cannot changeibefore its trailingi++nor whatbreads, so the condition at each test isi_0 + k < b. execStmt_while(tolean/Go/Proof/Stmt.lean) proves that the interpreter gives the same result for the loop and for its rewrite, and the statement proof reuses the counted-loop proof on the rewrite throughsim_congr.
Refused
continuein the body: it would skipi++, so the loop is not the counted one.- Other conditions (
<=,>,!=, compound tests), other updates, or a body that does not end withi++. - A body that writes
ielsewhere or changes whatbreads, and a bound that readsi. - A
returnin the body, and nested loops, as for counted loops. - Counters of other integer types, as for counted loops.
for { … }and every other condition loop: no measure.
The fresh name i' exists because the loop law forbids shadowing; a local named i' cannot exist in Go source, so it never collides.
This page is docs/decisions/2026-11-30-go-condition-loops.md at commit 5f1d031.
