diff options
Diffstat (limited to 'formal/tla')
| -rw-r--r-- | formal/tla/README.md | 62 | ||||
| -rw-r--r-- | formal/tla/SleepSort.cfg | 13 | ||||
| -rw-r--r-- | formal/tla/SleepSort.tla | 108 |
3 files changed, 183 insertions, 0 deletions
diff --git a/formal/tla/README.md b/formal/tla/README.md new file mode 100644 index 0000000..a80c687 --- /dev/null +++ b/formal/tla/README.md @@ -0,0 +1,62 @@ +# TLA+ model check of sleep sort + +`SleepSort.tla` is a [TLA+](https://lamport.azurewebsites.net/tla/tla.html) +model of the concurrent sleep sort in [`sort/sleep.go`](../../sort/sleep.go), +checked exhaustively by the TLC model checker. + +## What this does and does NOT prove + +Model checking verifies a **hand-written model**, not the Go source. TLC +explores *every* reachable state of the model and confirms: + +- **`OutputSorted`** (safety) — the collected output is always non-decreasing. +- **`PermutationWhenDone`** (safety) — the output is a multiset permutation of + the input; nothing is dropped, duplicated, or invented. +- **`Terminates`** (liveness) — the collector eventually receives every value: + no deadlock and no value left sleeping forever. + +The gap you keep responsibility for: that the model faithfully abstracts +`sleep.go`. The model represents the sleeps as a discrete clock and the +unbuffered channel + `WaitGroup` as an urgent single-value rendezvous — see the +header comment in `SleepSort.tla`. + +Sortedness is **not** assumed. It emerges from one rule: time cannot pass while +a fired-but-unreceived timer waits (`Tick`'s urgency guard). Delete that guard +and TLC finds an unsorted counterexample — proof the check has teeth. + +## Running + +Needs Java and `tla2tools.jar` (download once): + +```sh +mkdir -p ~/tools/tlaplus +curl -fsSL -o ~/tools/tlaplus/tla2tools.jar \ + https://github.com/tlaplus/tlaplus/releases/latest/download/tla2tools.jar +``` + +Then, from the repository root: + +```sh +make verify-model +``` + +or directly: + +```sh +java -cp ~/tools/tlaplus/tla2tools.jar tlc2.TLC \ + -config formal/tla/SleepSort.cfg formal/tla/SleepSort.tla +``` + +Expected output ends with: + +``` +Model checking completed. No error has been found. +``` + +## The input + +`SleepSort.cfg` checks the input `<<3, 1, 4, 1, 2>>` (defined as `InputValue` +in the module, because a `.cfg` cannot hold a sequence literal). It has a +duplicate and is out of order — enough to exercise ordering, ties, and +termination. Edit `InputValue` in `SleepSort.tla` to try others; the state space +stays small because the clock never exceeds the largest input value. diff --git a/formal/tla/SleepSort.cfg b/formal/tla/SleepSort.cfg new file mode 100644 index 0000000..6c86d67 --- /dev/null +++ b/formal/tla/SleepSort.cfg @@ -0,0 +1,13 @@ +\* TLC configuration for the SleepSort model. +\* Input is a small multiset with a duplicate (the two 1s) and out-of-order +\* values -- enough to exercise ordering, ties, and termination exhaustively. +\* It is defined as InputValue in the module (a .cfg cannot hold a <<..>>). +CONSTANT Input <- InputValue + +SPECIFICATION Spec + +INVARIANT TypeOK +INVARIANT OutputSorted +INVARIANT PermutationWhenDone + +PROPERTY Terminates diff --git a/formal/tla/SleepSort.tla b/formal/tla/SleepSort.tla new file mode 100644 index 0000000..45cc0e0 --- /dev/null +++ b/formal/tla/SleepSort.tla @@ -0,0 +1,108 @@ +------------------------------ MODULE SleepSort ------------------------------ +(***************************************************************************) +(* A TLA+ model of the sleep sort in sort/sleep.go. *) +(* *) +(* This verifies a *model*, not the Go source. It abstracts the goroutines *) +(* + WaitGroup + unbuffered channel of sleep.go into: *) +(* *) +(* - a global discrete clock (stand-in for wall-clock time / sleeps), *) +(* - one "timer" per input element that fires when the clock reaches *) +(* that element's value (a value-v element sleeps v seconds), and *) +(* - a single collector that receives one fired value at a time *) +(* (an unbuffered channel with one receiver hands off exactly one *) +(* value per rendezvous, with no buffering or reordering). *) +(* *) +(* Crucially, sortedness of the output is NOT baked in: it must *emerge* *) +(* from the timing mechanism. The only thing that enforces order is Tick's *) +(* urgency guard (time cannot pass while a fired-but-unreceived timer is *) +(* waiting). Remove that guard and TLC finds an unsorted counterexample -- *) +(* which is exactly what makes this a real check rather than a tautology. *) +(***************************************************************************) +EXTENDS Naturals, Sequences, FiniteSets + +CONSTANT Input \* the values to sort, as a sequence, e.g. <<3, 1, 2>> + +\* Concrete value for the model. TLC config files cannot parse a sequence +\* literal in a "CONSTANT Input = ..." assignment, so SleepSort.cfg overrides the +\* constant with this operator via "CONSTANT Input <- InputValue". +InputValue == <<3, 1, 4, 1, 2>> + +Idx == DOMAIN Input +Values == { Input[i] : i \in Idx } + +VARIABLES + clock, \* global time; the sleeps are measured against it + sent, \* sent[i] = TRUE once element i has been received + output \* the values collected so far, in arrival order + +vars == <<clock, sent, output>> + +\* Multiplicity of value v in a sequence s (compares the two as multisets). +Count(v, s) == Cardinality({ k \in DOMAIN s : s[k] = v }) + +TypeOK == + /\ clock \in Nat + /\ sent \in [Idx -> BOOLEAN] + /\ \A k \in DOMAIN output : output[k] \in Values + /\ Len(output) =< Cardinality(Idx) + +Init == + /\ clock = 0 + /\ sent = [i \in Idx |-> FALSE] + /\ output = << >> + +\* Element i's timer has fired (its sleep of Input[i] seconds has elapsed) and +\* it has not yet been received. +Ready(i) == /\ ~sent[i] + /\ clock >= Input[i] + +AllDone == \A i \in Idx : sent[i] + +\* The collector receives one ready value (channel rendezvous: exactly one +\* value transfers, chosen nondeterministically among those currently ready). +Receive == + /\ \E i \in Idx : + /\ Ready(i) + /\ sent' = [sent EXCEPT ![i] = TRUE] + /\ output' = Append(output, Input[i]) + /\ clock' = clock + +\* Time advances only when no fired timer is waiting to be received: every +\* not-yet-received element still has its deadline strictly in the future. +\* This urgency is what guarantees shorter sleeps deliver before longer ones. +Tick == + /\ ~AllDone + /\ \A i \in Idx : ~sent[i] => clock < Input[i] + /\ clock' = clock + 1 + /\ UNCHANGED <<sent, output>> + +\* Once everything has been collected, stutter (models the closed channel / +\* finished range loop) so termination is not mistaken for a deadlock. +Done == + /\ AllDone + /\ UNCHANGED vars + +Next == Receive \/ Tick \/ Done + +Spec == Init /\ [][Next]_vars /\ WF_vars(Receive) /\ WF_vars(Tick) + +----------------------------------------------------------------------------- +\* Properties checked by TLC (see SleepSort.cfg). + +\* SAFETY: whatever has been collected so far is always non-decreasing. +IsSorted(s) == \A a, b \in DOMAIN s : a < b => s[a] <= s[b] +OutputSorted == IsSorted(output) + +\* SAFETY: the output never invents or duplicates values, and once finished it +\* is exactly a permutation (multiset) of the input. +PermutationWhenDone == + AllDone => \A v \in Values : Count(v, output) = Count(v, Input) + +\* LIVENESS: the collector eventually receives every value (no deadlock, no +\* value left sleeping forever) -- i.e. the algorithm terminates. +Terminates == <>AllDone + +\* Note: the state space is already finite without a constraint -- Tick is +\* disabled once clock reaches the largest input value, so clock never grows +\* unbounded and TLC explores every reachable state. +============================================================================= |
