Describe the next state using the current one.

A small distinction that changes how you program.

Nilakan: programming made simpler by modelling differences.

Which update order gets them right?

Start with 0 and 1; add the previous two to get the next: 0, 1, 1, 2, 3, 5, …
Do we get the next pair via the first assignment order or the reverse order?

Python / First order

a = b
b = a + b

Move the second. Then add.

Python / Reverse order

b = a + b
a = b

Add first. Then move the second.

The answer

Neither.

In Python, you instead need simultaneous assignment, or temporaries.

a, b = b, a + b

In Nilakan, the order is unnecessary. Both orders work.

Nilakan / First order

a' = b
b' = a + b

a and b describe now.
a' and b' describe next.

Nilakan / Reverse order

b' = a + b
a' = b

Both orders / The same numbers

  1. 0
  2. 1
  3. 1
  4. 2
  5. 3
  6. 5
  7. 8
  8. 13
  9. 21
  10. 34
  11. 55

Business rules can state dependencies clearly.

Intentionally, not invisibly by statement order.

Nilakan / Explicit dependencies

subtotal' = subtotal + price
total' = subtotal' + tax'
tax' = rate * subtotal'

New tax uses the new subtotal.
New total uses both new values.

Accidental reorder
with normal assignment

subtotal = subtotal + price
total = subtotal + tax
tax = rate * subtotal

The total picks up the old tax.
Every variable still has a value.

Same inputs / Different totals

Nilakan

90 + 20 = 110

10% × 110 = 11

121 (correct total)

Reordered

90 + 20 = 110

Old tax: 9

119 (uses old tax)

The later tax update does not repair the already calculated total.

Physics models, too, update state precisely.

Choose which velocity leads to the next position.

Use new velocities

x'  = x + h * vx'
y'  = y + h * vy'
vx' = vx + h * ax
vy' = vy + h * ay

Move using the velocity
you have just calculated.

Use current velocities

x'  = x + h * vx
y'  = y + h * vy
vx' = vx + h * ax
vy' = vy + h * ay

Move using the velocity
from the current state.

Symplectic Euler
(new velocities)

Forward Euler
(current velocities)

Plan the trace, then write the code.

Find the relation that turns each row into the next.

1 / Plan the trace

100 at 10% interest, for three years.

Write the rows you expect before writing the program.

StateWithin round
Roundyear <= yearsyearprincipalinterestamount
1T110010110
2T211011121
3T312112.1133.1
ExitF4133.1——

The exit row is the final state. The loop body no longer runs.

2 / Write how each row becomes the next

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, 3))

Each part of the table becomes one part of the loop.

From the trace to the program

In the trace In the program
The first row The init line
The stopping condition The while guard
The change from one row to the next Primed equations, like year' = year + 1
Values worked out within a row Temporaries, like interest

Choose the progress step, then “diff” the invariants.

A novel method for deriving programs formally.

To compute s =∑nj=1j^2 for a natural number n, let the bound be k and choose k' = k + 1 as the progress step. Compare each invariant I with its next-state form I' to find the update it needs. Repeat until closure, when only simple operations remain. The invariants at k = 0 give the initial values.

The method / Repeat the “diff”

I I' Diff(I, I') Update
s =∑kj=1j^2 s' =∑k'j=1j^2 d = (k + 1)^2 s' = s + d
d = (k + 1)^2 d' = (k' + 1)^2 e = 2 * k + 3 d' = d + e
e = 2 * k + 3 e' = 2 * k' + 3 2 e' = e + 2

The code / Sum of squares

def sum_squares(n):
    init: k = 0; s = 0; d = 1; e = 3
    while k < n:
        k' = k + 1
        s' = s + d
        d' = d + e
        e' = e + 2
    return s

Progress and update equations derive the body.

Proof by school induction, since code is formula.

No back-substitution. No untangling assignment order.

Add the invariant and variant. Prove preservation and termination.

The summation program

def sum_to_n(n):
    init: s = 0; t = 0
    while t < n:
        s' = s + t + 1
        t' = t + 1
    return s

Let n, s, t ∈ ℕ. Choose the invariant:

P(s, t) ≡ 2s = t(t + 1)
          ∧ 0 ≤ t ≤ n

And the remaining distance:

ρ(t) = n − t

The init establishes P(0, 0). At exit, t = n, so the returned sum is n(n + 1)/2.

Preservation

Combine the invariant, the while guard, and the two body equations:

P(s, t) ∧ t < n
  ∧ s' = s + t + 1 ∧ t' = t + 1
⇒ P(s', t')

The current state satisfies P. The guard and body must make P hold at the next state.

Termination

The invariant keeps ρ nonnegative. The guard and counter equation give:

P(s, t) ∧ t < n ∧ t' = t + 1
⇒ 0 ≤ ρ(t') = ρ(t) − 1 < ρ(t)

The natural-number distance n − t decreases by one each round, so the loop must stop.

Nilakantha Series for π.

Nilakan is named after Nilakantha Somayaji, the Kerala School mathematician and astronomer.

His series builds an approximation to π one term at a time.

Nilakan / Add one term each round

def nilakantha_pi(terms: num) -> num:
    init: k = 1; pi = 3; sign = 1
    while k <= terms:
        n = 2 * k
        term = 4 / (n * (n + 1) * (n + 2))
        pi' = pi + sign * term
        sign' = -sign
        k' = k + 1
    return pi

print(nilakantha_pi(5))

The sign alternates; each round updates the approximation.

The trace / Five terms

StateWithin round
Roundk <= termskpisignnterm
1T13120.16667
2T23.16667-140.03333
3T33.13333160.0119
4T43.14524-180.00556
5T53.139681100.00303
ExitF63.14271-1——

Each row starts with the current approximation. The exit row shows the result after five terms.

Papers.

The language, its foundations, and trace-first program design.

  1. ICER · Vol. 2 2025

  2. CompEd 2023

    Teaching Programmers to Think of Program Dynamics

    Aamod Sane and Jayaraman Valadi.