Liveness & Fairness
Safety (Invariant, ActionInvariant) states that nothing bad ever happens. Liveness (Property) states that something good eventually happens.
Temporal Formula Primitives#
| Primitive | TLA⁺ | Meaning |
|---|---|---|
always(p) | □p | p holds in every state of the behaviour. |
eventually(p) | ◇p | p holds in at least one state of the behaviour. |
leads_to(p, q) | p ~> q | Whenever p becomes true, q eventually becomes true (□(p ⇒ ◇q)). |
Why Stuttering Requires Fairness#
Every specification has semantics Init ∧ □[Next]vars ∧ Fairness. The bracketed [Next]vars always allows stuttering steps (leaving vars unchanged) so that refinement works cleanly. Without a Fairness clause, a behaviour can legally stop taking actions forever — violating any liveness Property.
Weak Fairness (weak_fairness)#
weak_fairness(s.release()) (TLA⁺ WFvars(release)) asserts: if release() stays continuously enabled, it must eventually happen.
@spec(
positive_capacity=Assume(lambda s: s.capacity > 0),
init=Init(lambda s: s.available == s.capacity),
bounded=Invariant(lambda s: 0 <= s.available <= s.capacity),
fair=Fairness(lambda s: weak_fairness(s.release())),
progress=Property(lambda s: leads_to(s.available == 0, s.available > 0)),
)Inside Fairness and enabled(...), write actions as method calls on s (e.g., s.release() or s.acquire(p)). Pass on=e to customize the subscript (WFe(A)); omitting on defaults to all state and ghost variables.
Strong Fairness (strong_fairness)#
Weak fairness is not enough when an action is repeatedly enabled and disabled by competing processes. In a 3-process Mutex, while p1 waits, p2 can repeatedly acquire and release the lock — disabling acquire(p1) each time p2 holds it, so acquire(p1) is never continuously enabled.
strong_fairness(s.acquire(p)) (TLA⁺ SFvars(acquire(p))) asserts: if acquire(p) is enabled infinitely often, it must eventually happen.
fair=Fairness(lambda s: all(strong_fairness(s.acquire(p)) and weak_fairness(s.release(p)) for p in s.procs)),
starvation_free=Property(lambda s: all(leads_to(p in s.waiting, s.holder == p) for p in s.procs)),Checking Action Enablement with enabled#
enabled(s.action(args)) is a Level-1 (state) expression, so you can even use it inside an Invariant — for instance, asserting deadlock freedom explicitly:
never_stuck=Invariant(
lambda s: any(enabled(s.request(p)) or enabled(s.acquire(p)) or enabled(s.release(p)) for p in s.procs)
)