Skip to content

ADR 0077: The range of a list[int]'s elements

Status: Final Type: Feature Created: 2026-11-14 Amends: 0075 (step 1's range proofs, extended to a list[int]'s elements; part of step 5) Related-ADRs: 0053 (the loops proven in range before they run). Provenance: 0.3's task 4.

Context

With the growing int, an operation on a value that no proof bounds must test that neither operand is a reference, and must check for overflow (ADR 0075 §4). The range proofs of step 1 cover literals, constants, range() counters, len(), counters that move by a bounded step in range loops, and parameters bounded at all call sites. They never bound a list element: ranges.py reads the syntax before types exist, so an element read is unbounded.

That is most of what is left of the growing int's cost. life, at 4.1 times C's time on c with the growing int and 1.33 with the 64-bit one (Linux, 2026-11-14), sums eight cells of a list[int] per cell; the cells it stores are 0, 1 or (s >> 16) % 2, so each is in [0, 1] and the sum in [0, 8], and the checks could never fire. The lists are made in main, passed to seed and step, and swapped there, so the bound has to hold for all the program's list[int] values. The syntax cannot say which values those are: a field, a dict value, a function's result or a list from the FFI each produces one. A producer the proof did not see would drop a check that could fire: a silent wrap.

The owner chose on 2026-11-14 to build the proof where the type of each value is known. The owner also declined a run-length assumption (that no run executes 2**62 stores), so a list whose stores grow by a step each pass, as levenshtein's do, keeps its checks.

Decision

  1. One range for every list[int] of an executable. The elements of all of a program's list[int] values share one interval, the element range. Sharing it is what makes passing, returning, aliasing and swapping lists need no tracking: whichever list an element came from, it is in the range. A list[Int64] holds machine words and is outside this; so is a library, whose lists come from callers this compiler does not see.

  2. What the range is built from: the stores, seen by the typed lowering. The frontend, lowering with the element range unknown, records each site that puts an int into a list[int]: xs[i] = v, append, insert, a list display, a comprehension, [v] * n, list(range(...)), with the AST of the value stored. A site that moves elements between such lists (extend, +, a copy, a slice, sorted, reverse, a swap through an element read) adds nothing. It also records whether any list[int] value has a producer it does not read: a list from CPython, a container's member of another type converted to one, a map or a call to a builtin it has no rule for. One such producer makes the range unbounded.

  3. The fixed point is the syntax pass's. ranges.py computes the interval of each recorded value with element reads given the current range, starting from the empty interval, and the range is the hull of them; it repeats until the hull stops growing. A store that grows the range on each round (an element plus one) makes it unbounded after a fixed number of rounds, since no trip-count rule bounds how often it runs. Each round is the syntax pass alone, over the recorded sites.

  4. The proof is used by lowering again. Where the range is bounded and fits a word, the program is lowered a second time with an element read of a list[int] given that range, so the operations it feeds are proven as any other bounded operation is: no reference test and no overflow check where the result fits. The guard pass of ADR 0075 §4 takes the same fact: an element read is no reference, and a store into a list[int] neither retains the new element nor releases the old one. Where the range is unbounded, the first lowering is kept and the program stays as it is today. The second lowering records its own stores. Their range must be within the one it was given, or the first lowering is kept: the two lowerings of one source agree, so a disagreement costs the proof and leaves the checks in place.

  5. What it costs. A second lowering of every executable that grows ints and has a bounded range; hq2x, the largest census program, has none and pays nothing, and life builds in about 1.2 s either way (Apple M1, 2026-11-14).

Consequences

  • life's step loop loses its checks. levenshtein, whose stores grow, keeps them, as does a program that reads a list from CPython; each reason is recorded as distance.
  • A store site the frontend forgets to record cannot shrink the range: the scan of the lowered IR finds the call, which no record names, so there is no range. The scan cannot see a store a runtime helper makes of an int it creates itself. No helper does that today (each runtime helper that returns a new list returns a list[str]); a new one has to join the scan's list of helpers. A stored value the syntax pass bounds wrongly would wrap; that pass is ADR 0075 step 1's, guarded by the differential over the benchmark programs, the census and tests/b_integration/test_element_ranges.py.
  • On an Apple M1 under load (2026-11-14), life went from 4.53 times C's time on c to 1.54, and the default build's index from 1.97 to 1.86 on c and 2.17 to 1.98 on llvm; fannkuch, sieve, nqueens and levenshtein are unchanged, since their stores grow or their lists are of bool.