Python / First order
a = b
b = a + bMove the second. Then add.
Describe the next state using the current one.
A small distinction that changes how you program.
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 + bMove the second. Then add.
Python / Reverse order
b = a + b
a = b
Add first. Then move the second.
The answer
In Python, you instead need simultaneous assignment, or temporaries.
a, b = b, a + bNilakan / First order
a' = b
b' = a + b
a and b describe now.
a' and b' describe next.
Nilakan / Reverse order
b' = a + b
a' = bBoth orders / The same numbers
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
90 + 20 = 110
10% × 110 = 11
121 (correct total)
90 + 20 = 110
Old tax: 9
119 (uses old tax)
The later tax update does not repair the already calculated total.
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 * ayMove 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 * ayMove using the velocity
from the current state.
Symplectic Euler
(new velocities)
Forward Euler
(current velocities)
Find the relation that turns each row into the next.
1 / Plan the trace
Write the rows you expect before writing the program.
| State | Within round | ||||
|---|---|---|---|---|---|
| Round | year <= years | year | principal | interest | amount |
| 1 | T | 1 | 100 | 10 | 110 |
| 2 | T | 2 | 110 | 11 | 121 |
| 3 | T | 3 | 121 | 12.1 | 133.1 |
| Exit | F | 4 | 133.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 |
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 sProgress and update equations derive the body.
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 sLet n, s, t ∈ ℕ. Choose the invariant:
P(s, t) ≡ 2s = t(t + 1)
∧ 0 ≤ t ≤ nAnd the remaining distance:
ρ(t) = n − tThe init establishes P(0, 0). At exit, t = n, so the returned sum is n(n + 1)/2.
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.
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.
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
| State | Within round | |||||
|---|---|---|---|---|---|---|
| Round | k <= terms | k | pi | sign | n | term |
| 1 | T | 1 | 3 | 1 | 2 | 0.16667 |
| 2 | T | 2 | 3.16667 | -1 | 4 | 0.03333 |
| 3 | T | 3 | 3.13333 | 1 | 6 | 0.0119 |
| 4 | T | 4 | 3.14524 | -1 | 8 | 0.00556 |
| 5 | T | 5 | 3.13968 | 1 | 10 | 0.00303 |
| Exit | F | 6 | 3.14271 | -1 | — | — |
Each row starts with the current approximation. The exit row shows the result after five terms.
The language, its foundations, and trace-first program design.