The HourClock, end to end

A 12-hour clock is trivially simple — which makes it a perfect lens for the whole workflow: spec, invariant, liveness, and a deliberately introduced bug.

The spec

----------------------------- MODULE HourClock -----------------------------
EXTENDS Naturals, TLC

VARIABLE hr

Init     ==  hr \in 1..12
Next     ==  hr′ = IF hr # 12 THEN hr + 1 ELSE 1
TypeOK   ==  hr \in 1..12
Live     ==  hr = 12 ~> hr = 1

Spec     ==  Init /\ [][Next]_hr /\ WF_hr(Next)
=============================================================================

Safety TypeOK

hr is always between 1 and 12 in every reachable state.

Liveness Live

From hour 12, the clock eventually reaches 1 — it never stops ticking.

Fairness WF_hr(Next)

Weak fairness forbids stuttering forever while Next remains enabled.

Watch it run

The clock ticks around its 12 reachable states. Press Break Next to remove the wrap-around and watch TLC's invariant check catch the bug.

Error: Invariant TypeOK is violated. State 12: hr = 12 State 13: hr = 13 <-- 13 \notin 1..12 Cause: Next \equiv hr' = hr + 1 has no wrap-around.

What TLC reports, in each case

ScenarioProperties checkedTLC output
Correct specTypeOK (safety)No error — 12 distinct states, 12 reachable states found.
Correct specLive (liveness)No error — fairness ensures the clock keeps advancing.
Broken NextTypeOKError — counterexample of length 13 ending at hr = 13.
No fairness clauseLiveError — a stuttering behavior that stays at hr = 12 forever.
The lesson

Three ideas, one tiny module: safety (TypeOK), liveness (Live), and fairness (WF) — and each failure mode maps to a different, precise TLC diagnostic. This is the exact reasoning you scale up to distributed protocols like consensus or replication.

Made with Aii CloudReport