Skip to content

Go range loops over local slices ​

Status: accepted · Date: 2026-11-30

The proved path had counted and condition-only loops, but refused every range statement. This record adds for _, x := range s over a local slice and generalizes the loop law that both forms use.

The decision ​

  • The interpreter (tolean/Go/Spec/Eval.lean) runs for _, x := range s { … } by evaluating s once, then running the body once for each element, with a new x bound to it (Go specification, "For statements with range clause"; since Go 1.22 each iteration has its own variable). It uses the shared execLoop, as the counted loop does.
  • The body must not change x or s and holds no other loop (countedBodySafe [x, s]). The elements are therefore those of s before the loop, whether or not Go reads them again on each iteration.
  • The translator writes the shared lowerLoop over the local itself: List.foldl or forIn over s. The IR's Expr.items takes a local whose value is a slice as its list of elements.
  • LoopLaw (tolean/Go/Proof/Stmt.lean) is now stated for any statement that runs its body over the items of the output's loop, with an element type of its own, rather than for the counted head alone. loop_law proves it once. The counted loop gets its premise from execStmt_counted, and the range loop gets its own from the value of s.
  • Owned.check counts range s as a read of s that does not share its array, so a function may append to a slice and then range over it.

The index form ​

A later change adds for i := range s over a local slice. Go evaluates s once and binds a new i to each index from 0 up to below its length. The interpreter runs the shared execLoop over upTo 0 (sliceLen vs), where vs is the value of s, under the same countedBodySafe [i, s] rule. The translator writes lowerLoop over the IR items Go.upTo 0 (Go.sliceLen s), which Expr.items already evaluates for counted loops. The proof gets the premise of LoopLaw from the value of s, as for the element form, so no new loop lemma is needed. A range over a string stays refused, because its indices step over runes.

The index and element form ​

A later change adds for i, x := range s over a local slice. Go binds a new i and a new x in each iteration, and x is a copy of s[i]. Since the body changes neither i nor s, s[i] is that element when the iteration starts. The interpreter therefore runs the index loop, and each iteration runs x := s[i] and then the body. The translator translates the same statements, so the proof reuses LoopLaw without a new loop lemma: the body of the loop is the list x := s[i] followed by the body, and stmtChanges of the range statement counts x, as the short declaration does. Eval.pairRunner_eq rewrites the interpreter's runner into exec of that list, and Unused proves its rules for the two parts separately, because the list is not smaller than the range statement.

The cost is a bounds test on s[i] in the output, which never fails, and which makes the function return Except. Writing the pair with a fold over List.zipIdx would avoid the test, but it needs a loop lemma with two binders, which this change does not add.

Refused ​

  • for i, x := range s with _ for i or x (for _, x has its own form), with one name twice or the name of the slice, or with an empty body.
  • Ranging over anything but a local slice: literals, calls, fields, strings (whose range gives runes), arrays, maps, channels, integers and function iterators.
  • = instead of := in the range clause.
  • A body that assigns the element or the slice, holds a return, or holds another loop, as for counted loops.

This page is docs/decisions/2026-11-30-go-range-slices.md at commit 5f1d031.