A Tour of Temporal Typing Code, Contracts & Types · 4/6
Code, Contracts & Types · Page 4 of 6

Ghost State, Pure & ActionInvariant

Real implementations rarely store full audit histories, yet specifications often need them. Three constructs make this effortless:

  • Ghost[T] declares a specification-only variable. Look at ledger: the Python bodies of deposit and withdraw never touch self.ledger! The checker updates ledger from the Action predicate while checking the body's real balance changes.
  • Pure[T] marks a side-effect-free operator or property (like available) that predicates may read (s.available).
  • ActionInvariant asserts □[P]vars: a step-level invariant over s.x and prime(s.x), such as ledger_append_only.
Try it: Click Check to explore all 884 states bounded by len(s.ledger) <= 4. Then change self.balance -= amount to self.balance -= amount + 1 and watch conformance catch the drift immediately!
Click Check (or press Shift+Enter) to run the front-end and check this specification. Switch to TLA⁺ to view its TLA⁺ meaning.
---- MODULE BankAccount ----
EXTENDS Integers, Sequences
CONSTANTS owner, credit_limit
VARIABLES balance, ledger

ASSUME owner # "" /\ credit_limit >= 0
Available == balance + credit_limit
Init == balance = 0 /\ ledger = <<>>

Deposit(a)  == balance' = balance + a /\ ledger' = Append(ledger, a)
Withdraw(a) == a <= Available /\ balance' = balance - a /\ ledger' = Append(ledger, -a)

WithinLimit      == balance >= -credit_limit
LedgerAppendOnly == [][SubSeq(ledger', 1, Len(ledger)) = ledger]_<<balance, ledger>>
====