A Tour of Temporal Typing Specifications & State · 4/6
Specifications & State · Page 4 of 6

Invariants & Counterexamples (DieHard)

A model checker explores reachable states breadth-first, so when an Invariant fails, it gives you a shortest counterexample trace from an initial state to the violation.

In the movie Die Hard with a Vengeance (and Leslie Lamport's famous TLA⁺ lecture), you have a 3-gallon jug and a 5-gallon jug and must measure out exactly 4 gallons.

How do we ask a model checker to solve the puzzle? Claim the opposite as an invariant: not_solved=Invariant(lambda s: s.big != 4). When the checker disproves not_solved, its counterexample trace is the shortest solution!

Try it: Click Check to watch the checker discover the 6-step solution to the DieHard puzzle. Then change s.big != 4 to s.big != 6 (impossible!) and verify that the model now passes.
Click Check (or press Shift+Enter) to run the front-end and check this specification. Switch to TLA⁺ to view its TLA⁺ meaning.
---- MODULE DieHard ----
EXTENDS Integers
VARIABLES small, big

Min(a, b) == IF a < b THEN a ELSE b
Init == small = 0 /\ big = 0
FillSmall == small' = 3 /\ UNCHANGED big
FillBig   == big' = 5 /\ UNCHANGED small
EmptySmall == small' = 0 /\ UNCHANGED big
EmptyBig   == big' = 0 /\ UNCHANGED small
SmallToBig == LET p == Min(small, 5 - big) IN small' = small - p /\ big' = big + p
BigToSmall == LET p == Min(big, 3 - small) IN big' = big - p /\ small' = small + p
Next == FillSmall \/ FillBig \/ EmptySmall \/ EmptyBig \/ SmallToBig \/ BigToSmall

TypeOK    == 0 <= small /\ small <= 3 /\ 0 <= big /\ big <= 5
NotSolved == big # 4
====