Replay a bank account’s history, one event at a time, to get today’s balance.
state-transitions.pl · output · proof · check · try it in the playground
An account opens with 100. Then three things happen, in order: a deposit of 25, a withdrawal of 40, a withdrawal of 20.
What is the balance after event 3? And can we see how each event changed it?
An event log is a numbered list of what happened:
opening_balance(100).
event(1, deposit, 25).
event(2, withdraw, 40).
event(3, withdraw, 20).
Each balance is a state, and each event is a transition: a step from one state to the next.
balance(0, Amount) :- opening_balance(Amount).
balance(N, Amount) :- N > 0, event(N, deposit, Value), Before is N-1, balance(Before, Previous), Amount is Previous+Value.
balance(N, Amount) :- N > 0, event(N, withdraw, Value), Before is N-1, balance(Before, Previous), Amount is Previous-Value.
true :+ balance(3, Amount).
balance(3, 65).
After the three events, the balance is 65.
Read from the start of the log:
The proof also records each small calculation, such as “3 − 1 = 2” for finding the previous event.
The proof has 17 steps. The checker found:
Verdict: checked. All 17 steps verified, nothing taken on trust.
node bin/eyedia.js examples/state-transitions.pl
node bin/eyedia.js --proof examples/state-transitions.pl
Or open it in the playground.
Add event(4, deposit, 10). and change the last line to ask for
balance(4, Amount): the answer becomes balance(4, 75).
An audit trail you can trust: the final number comes with every intermediate balance and every calculation that led to it, each one rechecked.