Thinking In Traces
This page follows the trace-first method of Sane and Valadi (2023) and Sane and Prasad (2025): plan the trace of a loop first, then write what produces it. In Nilakan that last step is direct. As Sane (2026) puts it, designing a program from a trace “amounts to finding the relation that transforms each row into the next”, and those relations are the loop’s primed equations. The playground’s Trace section shows the table for every commit loop you run, so you can compare the trace you planned with the one your code made.
A loop is a table
Section titled “A loop is a table”To count by three up to 30, start with the values you want to see:
| Round | cond |
tot |
|---|---|---|
| 1 | T | 3 |
| 2 | T | 6 |
| … | … | … |
| 9 | T | 27 |
| Exit | F | 30 |
Two ideas carry the rest of this page. A variable is a column: a sequence of values, not a single value. The loop condition is a column too, checked on every row, including the first.
From table to code
Section titled “From table to code”- Each column except
condbecomes a state variable ininit, set to its first-row value. - The
condcolumn becomes thewhileguard. - A value computed from the previous row becomes a primed assignment.
tot' = tot + 3reads as “next row’stotis this row’stotplus three”.
def count_by_three() -> num: init: tot = 3 while tot < 30: tot' = tot + 3 return tot
print(count_by_three())init must sit directly before its while, so the first row and the loop that extends it are
always written as one unit. If the first row already fails the condition, the loop runs zero rounds
and its trace shows only the Exit row.
Within a row and across rows
Section titled “Within a row and across rows”Compound interest is a familiar example: one year’s amount is the next year’s principal. Some values depend on others in the same row, and some on the row above:
- within a row:
interest = principal × 10%andamount = principal + interest - across rows: next year’s
principalis this year’samount
Within-row values are temporaries, written as ordinary assignments in the loop body. Across-row values are primed assignments:
def compound(principal0: num, years: num) -> num: init: year = 1 principal = principal0 while year <= years: interest = principal * 0.1 amount = principal + interest principal' = amount year' = year + 1 return principal
print(compound(100, 10))The trace places temporaries under Within round, next to the state they were computed from:
| Round | cond |
year |
principal |
interest |
amount |
|---|---|---|---|---|---|
| 1 | T | 1 | 100 | 10 | 110 |
| 2 | T | 2 | 110 | 11 | 121 |
| 3 | T | 3 | 121 | 12.1 | 133.1 |
| … | … | … | … | … | … |
| 10 | T | 10 | 235.7947691 | 23.5794769 | 259.374246 |
| Exit | F | 11 | 259.374246 | — | — |
Every primed assignment reads the current row, so the order of the statements in the body does not change the result. Nilakan runs temporaries in the order their dependencies require.
Which row does a value come from?
Section titled “Which row does a value come from?”The paper asks whether a factorial loop multiplies by this round’s new n or the previous one. In
most languages the answer depends on the order of two statements. In Nilakan it depends on what you
write:
def previous_n() -> num: init: fac = 1 n = 1 steps = 1 while steps < 5: fac' = fac * n n' = n + 1 steps' = steps + 1 return fac
def fresh_n() -> num: init: fac = 1 n = 1 steps = 1 while steps < 5: next_n = n + 1 n' = next_n fac' = fac * next_n steps' = steps + 1 return fac
print(previous_n(), fresh_n())This prints 24 120. fac' = fac * n uses the previous row’s n. To use the value computed in
this round, give it a name as a temporary; it then gets its own column in the trace. You can also
write fac' = fac * n' directly: the dependency scheduler evaluates the definition of n' first.
Self-dependent accumulation uses
acc_prime: sequential.
Several variables, one step
Section titled “Several variables, one step”The paper’s Fibonacci trace needs three variables updated in a careful order, because a single variable cannot hold two earlier values. Primed assignments all read the same row, so two variables are enough and their order does not matter:
def fib(n: num) -> num: init: i = 0 a = 0 b = 1 while i < n: a' = b b' = a + b i' = i + 1 return a
print(fib(10))Invariants and variants live in the data
Section titled “Invariants and variants live in the data”Bisection finds √2 by keeping it between lower and upper. Two properties explain why it works,
and both are visible in the trace:
- an invariant holds on every row:
lower × lower ≤ 2 ≤ upper × upper - a variant shrinks on every row: the width
upper - lowerhalves, so the loop must end
Nilakan does not check invariants or variants itself. A temporary is still recorded in the trace even if nothing reads it, so you can add columns that let you watch them:
def root2() -> num: init: lower = 1 upper = 2 while upper - lower > 0.01: width = upper - lower brackets = lower * lower <= 2 and 2 <= upper * upper avg = (lower + upper) / 2 if avg * avg > 2: upper' = avg else: lower' = avg return lower
print(root2())Nested loops start from their own first row
Section titled “Nested loops start from their own first row”A common bug sets an inner loop’s counter once, before the outer loop, so the inner loop only runs
during the first outer round. Give the inner loop its own init; its first row is then set again on
every outer round:
def border_cells(size: num) -> num: init: r = 0 count = 0 while r < size: pragma acc_prime: sequential init: c = 0 while c < size: on_border = r' == 0 or r' == size - 1 or c == 0 or c == size - 1 count'' = count' + 1 if on_border else count' c' = c + 1 r' = r + 1 return count
print(border_cells(5))Inside the nested loop, the outer loop’s state carries one more prime: r' is the current value of
r there, and count'' is the next value of count'. The outer loop uses sequential mode so the
inner loop reads r' before the later assignment advances it. See
State Layers And Nested Loops.