Systems Specification Reference

TLA+ ↔ Lean 4 Rosetta Stone

Already think in Lamport's state machines, invariants, TLC model checking, and refinement mappings? Filter every TLA+ construct alongside its idiomatic zero-dependency Lean 4 counterpart.

State Variables (`VARIABLES`)

TLA+ declares untyped global variables; Lean groups state cleanly into a typed `structure`.

VARIABLES leaderEpoch, storageEpoch, committedVal
structure SysState where
  leaderEpoch  : Nat
  storageEpoch : Nat
  committedVal : Option String

Initial State Predicate (`Init`)

Express deterministic initialization as a value `init : S` or nondeterministic initial sets as `Init : S → Prop`.

Init ==
  /\ leaderEpoch = 1
  /\ storageEpoch = 1
  /\ committedVal = "none"
def Init (s : SysState) : Prop :=
  s.leaderEpoch = 1 ∧
  s.storageEpoch = 1 ∧
  s.committedVal = none

Next-State Relation (`Next`) & `UNCHANGED`

Record update syntax `{ s with field := v }` automatically keeps all unmentioned fields `UNCHANGED` without forgetting any!

BumpEpoch ==
  /\ leaderEpoch' = leaderEpoch + 1
  /\ UNCHANGED <<storageEpoch, committedVal>>

Next == BumpEpoch \/ CommitWrite
inductive Next : SysState → SysState → Prop
  | bumpEpoch (s : SysState) :
      Next s { s with leaderEpoch := s.leaderEpoch + 1 }
  | commitWrite (s : SysState) (v : String)
      (h : s.leaderEpoch ≥ s.storageEpoch) :
      Next s { s with storageEpoch := s.leaderEpoch, committedVal := some v }

Gear 1: TLC Bounded Model Checking (`Spec => []TypeOK /\ []Safe`)

Executable `step : S → List S` explored via pure BFS catches shallow bugs in milliseconds via `#guard`.

-- In MCMini.cfg:
SPECIFICATION Spec
INVARIANT Safe
-- Pure Lean Gear 1 BFS (Ch 04):
#guard bfsExplore 6 [initSys] == .allSafe

Gear 2: Inductive Invariant (`Init => Inv` & `Inv /\ Next => Inv'`)

Equivalent to TLAPS `THEOREM Spec => []Safe` via strengthened invariant `Inv`.

Inv ==
  /\ Safe
  /\ committedVal /= "none" => storageEpoch >= 1

THEOREM Init => Inv
THEOREM Inv /\ [Next]_vars => Inv'
theorem inv_init (s : SysState) : Init s → Inv s := ...
theorem inv_next (s s' : SysState) : Inv s → Next s s' → Inv s' := ...
theorem reachable_safe (s : SysState) : Reachable s → Safe s := ...

Refinement Mapping & Stuttering (`INSTANCE Abs WITH ...`)

Concrete implementation steps map either to an abstract `SpecNext` step or an abstract stuttering step `abs s' = abs s`.

Abs == INSTANCE SpecKV
  WITH kv <- [k \in Keys |-> LookupLog(log, k)]

THEOREM ImplSpec => Abs!Spec
def abs (impl : ImplKV) : SpecKV := lookupLog impl.log

theorem compact_stutters (impl : ImplKV) :
    abs (compact impl) = abs impl := ...

Bounded Progress / Termination (`WF_vars` / Variant Function)

Lean's `termination_by` proves a well-founded ranking measure strictly decreases, guaranteeing zero infinite internal loops.

Variant == totalBytes - offset
THEOREM ParseStep => Variant' < Variant
def parseAll (buf : ByteArray) (offset : Nat) : ...
  ...
termination_by buf.size - offset
decreasing_by omega