11-04
Chapter 11 · Refinement is implication
Break the mapping, read the trace
00Recall
One card from 11-03 on mappings as expressions, and two from chapter 04: what stuttering invariance is, and why SANY rejects []Next. Chapter 04 promised that the box's odd subscript would pay for itself. This is the lesson where it does.
11-03-a from Refinement mappings
The implementation has no variable that matches an abstract variable. Is refinement still possible?
Show answer
Yes. A refinement mapping gives each abstract variable an expression over the implementation's variables, such as "the current message while the receiver has not delivered it, else empty". It is substitution, not renaming.
04-04-a from The frame-rate test and the shadow
What is stuttering invariance, and which piece of syntax makes every TLA formula have it?
Show answer
Adding or removing steps that leave a formula's variables unchanged never changes whether the formula is true. The subscript in [A]_v, since the box always allows those steps; and TLA has no temporal "next" operator that could count them: a primed variable may appear only inside an action.
04-03-a from Why the subscript
Why does SANY reject Spec == Init /\ []Next?
Show answer
[] cannot be applied to a bare action. The only action form allowed under [] is [A]_v, which always permits steps that leave v unchanged. That keeps every TLA formula stuttering invariant.
Skip recall Recall is never required.
01Commit
A teammate argues that the backup is "the real store", because it is the copy that survives a primary failure. specs/BackupShadowKV.tla is 11-02's refinement check with one line changed:
KV is defined as INSTANCE AtomicKV WITH store <- backup \* <- the only changeCommit a
TLC checks KVStep with the abstract store read off the backup. Where does the first violation happen, if anywhere?
- At the backup's first sync: it installs the primary's whole map, not one key.
- At a crash of the backup.
- Nowhere: the backup holds only values the client wrote.
- At the client's first put, which leaves the shadow where it was.
Answer At a crash of the backup.
- If you picked A
- TLC never looks at what the code did. It compares the shadow before and after the step. After one put, how many keys does the sync change in the backup's map?
- If you picked C
- AtomicKV promises more than "only written values". It says how the store may change in one step, and one kind of change is missing from its only action.
- If you picked D
- A step that leaves the shadow alone is a stuttering step of AtomicKV, and [Next]_vars allows it. That was 11-02.
Now a different teammate, who likes clocks. specs/ClockedKV.tla is AtomicKV with a variable beat that swings between "tick" and "tock" on every write. specs/LockstepKV.tla gives ReplicatedKV a matching clock that swings on every step of the protocol, and maps beat <- clock, store <- primary.
Commit b
LockstepKV keeps 11-02's passing mapping for store and adds a clock that swings on every protocol step. What does TLC report for CKStep?
- No violation: 11-02 already showed that store <- primary works.
- A violation at the first PrimaryApply, when the shadow store changes.
- A violation on the very first step.
Answer A violation on the very first step.
- If you picked A
- The store part still works. But the shadow is now a pair, store and beat, and a step is a stutter only if neither part changes.
- If you picked B
- Look earlier. The client's put leaves the store alone and swings the clock. What does ClockedKV allow a step that moves beat to do with store?
02Lab
Run both checks, the clock first:
./tla check chapters/11-refinement/specs/LockstepKV.cfg
./tla check chapters/11-refinement/specs/BackupShadowKV.cfg
./tla trace --fullThe last command prints every variable of the most recent trace, the backup check's. Keep it open: in REASON you write the shadow next to it by hand.
11-refinement/specs/BackupShadowKV11-refinement/specs/BackupShadowKV BackupShadowKV.cfg (Keys={k1, k2}, Vals={v1, v2}, NoVal=NoVal)
parse ok
model check: action property KVStep violated; trace has 5 states (4 steps); 15 distinct states
TLC warning: The formula KVInit is a state-level formula, but it was used as a PROPERTY (or PROPERTIES), where a temporal formula is typically expected. State-level formulas used as PROPERTY or PROPERTIES are only checked in the initial state. To verify that the formula KVInit holds in all states of every behavior, use INVARIANT KVInit instead. Alternatively, applying the "always" temporal operator ([]) to the state-level formula KVInit changes it into a temporal formula, asserting that KVInit holds in all states of every behavior. See https://explain.tlapl.us/invariants-and-properties for additional details.
expected outcome met: property KVStep
trace (changed variables only; ./tla trace --full shows all):
1 Init backup = (k1 :> NoVal @@ k2 :> NoVal), primary = (k1 :> NoVal @@ k2 :> NoVal), net = {}, busy = FALSE
2 ClientPut net: {} -> {[type |-> "put", key |-> k1, val |-> v1]}, busy: FALSE -> TRUE
3 PrimaryApply primary: (k1 :> NoVal @@ k2 :> NoVal) -> (k1 :> v1 @@ k2 :> NoVal), net: {[type |-> "put", key |-> k1, val |-> v1]} -> {[type |-> "sync", map |-> (k1 :> v1 @@ k2 :> NoVal)]}
4 BackupApply backup: (k1 :> NoVal @@ k2 :> NoVal) -> (k1 :> v1 @@ k2 :> NoVal), net: {[type |-> "sync", map |-> (k1 :> v1 @@ k2 :> NoVal)]} -> {[type |-> "ack"]}
5 BackupCrash backup: (k1 :> v1 @@ k2 :> NoVal) -> (k1 :> NoVal @@ k2 :> NoVal)Reveal for commit a
KVStep is violated after a trace of 5 states. The sync passes; the crash does not. A crash empties the backup's map, and no AtomicKV step ever takes a key back to NoVal.
Reveal for commit b
CKStep is violated after a trace of 2 states: the initial state and the client's first put. The put swings the clock, so the shadow is not stuttering, and it leaves primary alone, so the shadow is not a write.
03Reason
If you picked "at the first sync", you judged the step by what the code did: the backup threw away its map and installed a new one. A refinement check never asks what the code did. It asks one question per step: read the shadow off the state before and the state after, and is that pair a step of the abstract spec? Work it state by state from the ./tla trace --full output.
Write something before openingwrite the shadow, the backup's map, next to each state of the trace. Which step is the first one AtomicKV cannot explain?
The put leaves the backup alone: stutter. The primary applies the put, and the backup is still alone: stutter. The sync installs a map that differs from the backup's in one key, which became a written value: that is Write(k, v). The crash takes the shadow from one written key back to Empty, every key at NoVal. AtomicKV's only action sets a key to a value in Vals, and NoVal is not in Vals. Nothing explains it.
Now check your column against the lab:
KVStep violated
This is a teaching view. Real checking happens with TLC on your machine.
Trace
- State 1 Initial state primary = (k1 :> NoVal @@ k2 :> NoVal) backup = (k1 :> NoVal @@ k2 :> NoVal) net = {} busy = FALSE shadow_store = (k1 :> NoVal @@ k2 :> NoVal)
- State 2 ClientPut(k1,v1) net {} becomes {[type |-> "put", key |-> k1, val |-> v1]} busy FALSE becomes TRUE
- State 3 PrimaryApply primary (k1 :> NoVal @@ k2 :> NoVal) becomes (k1 :> v1 @@ k2 :> NoVal) net {[type |-> "put", key |-> k1, val |-> v1]} becomes {[type |-> "sync", map |-> (k1 :> v1 @@ k2 :> NoVal)]}
- State 4 BackupApply backup (k1 :> NoVal @@ k2 :> NoVal) becomes (k1 :> v1 @@ k2 :> NoVal) net {[type |-> "sync", map |-> (k1 :> v1 @@ k2 :> NoVal)]} becomes {[type |-> "ack"]} shadow_store (k1 :> NoVal @@ k2 :> NoVal) becomes (k1 :> v1 @@ k2 :> NoVal)
- State 5 BackupCrash backup (k1 :> v1 @@ k2 :> NoVal) becomes (k1 :> NoVal @@ k2 :> NoVal) shadow_store (k1 :> v1 @@ k2 :> NoVal) becomes (k1 :> NoVal @@ k2 :> NoVal)
Text view: every state and step
| State | backup | primary | net | busy | shadow_store | Steps (action → state) |
|---|---|---|---|---|---|---|
| initial 1 | (k1 :> NoVal @@ k2 :> NoVal) | (k1 :> NoVal @@ k2 :> NoVal) | {} | FALSE | (k1 :> NoVal @@ k2 :> NoVal) | ClientPut(k1,v1) → 2; ClientPut(k2,v1) → 3; ClientPut(k1,v2) → 4; ClientPut(k2,v2) → 5 |
| 2 | (k1 :> NoVal @@ k2 :> NoVal) | (k1 :> NoVal @@ k2 :> NoVal) | {[type |-> "put", key |-> k1, val |-> v1]} | TRUE | (k1 :> NoVal @@ k2 :> NoVal) | PrimaryApply → 6 |
| 3 | (k1 :> NoVal @@ k2 :> NoVal) | (k1 :> NoVal @@ k2 :> NoVal) | {[type |-> "put", key |-> k2, val |-> v1]} | TRUE | (k1 :> NoVal @@ k2 :> NoVal) | PrimaryApply → 7 |
| 4 | (k1 :> NoVal @@ k2 :> NoVal) | (k1 :> NoVal @@ k2 :> NoVal) | {[type |-> "put", key |-> k1, val |-> v2]} | TRUE | (k1 :> NoVal @@ k2 :> NoVal) | PrimaryApply → 8 |
| 5 | (k1 :> NoVal @@ k2 :> NoVal) | (k1 :> NoVal @@ k2 :> NoVal) | {[type |-> "put", key |-> k2, val |-> v2]} | TRUE | (k1 :> NoVal @@ k2 :> NoVal) | PrimaryApply → 9 |
| 6 | (k1 :> NoVal @@ k2 :> NoVal) | (k1 :> v1 @@ k2 :> NoVal) | {[type |-> "sync", map |-> (k1 :> v1 @@ k2 :> NoVal)]} | TRUE | (k1 :> NoVal @@ k2 :> NoVal) | BackupApply → 10 |
| 7 | (k1 :> NoVal @@ k2 :> NoVal) | (k1 :> NoVal @@ k2 :> v1) | {[type |-> "sync", map |-> (k1 :> NoVal @@ k2 :> v1)]} | TRUE | (k1 :> NoVal @@ k2 :> NoVal) | BackupApply → 11 |
| 8 | (k1 :> NoVal @@ k2 :> NoVal) | (k1 :> v2 @@ k2 :> NoVal) | {[type |-> "sync", map |-> (k1 :> v2 @@ k2 :> NoVal)]} | TRUE | (k1 :> NoVal @@ k2 :> NoVal) | BackupApply → 12 |
| 9 | (k1 :> NoVal @@ k2 :> NoVal) | (k1 :> NoVal @@ k2 :> v2) | {[type |-> "sync", map |-> (k1 :> NoVal @@ k2 :> v2)]} | TRUE | (k1 :> NoVal @@ k2 :> NoVal) | BackupApply → 13 |
| 10 | (k1 :> v1 @@ k2 :> NoVal) | (k1 :> v1 @@ k2 :> NoVal) | {[type |-> "ack"]} | TRUE | (k1 :> v1 @@ k2 :> NoVal) | ClientAck → 14; BackupCrash → 15 |
| 11 | (k1 :> NoVal @@ k2 :> v1) | (k1 :> NoVal @@ k2 :> v1) | {[type |-> "ack"]} | TRUE | (k1 :> NoVal @@ k2 :> v1) | |
| 12 | (k1 :> v2 @@ k2 :> NoVal) | (k1 :> v2 @@ k2 :> NoVal) | {[type |-> "ack"]} | TRUE | (k1 :> v2 @@ k2 :> NoVal) | |
| 13 | (k1 :> NoVal @@ k2 :> v2) | (k1 :> NoVal @@ k2 :> v2) | {[type |-> "ack"]} | TRUE | (k1 :> NoVal @@ k2 :> v2) | |
| 14 | (k1 :> v1 @@ k2 :> NoVal) | (k1 :> v1 @@ k2 :> NoVal) | {} | FALSE | (k1 :> v1 @@ k2 :> NoVal) | |
| 15 | (k1 :> NoVal @@ k2 :> NoVal) | (k1 :> v1 @@ k2 :> NoVal) | {[type |-> "ack"]} | TRUE | (k1 :> NoVal @@ k2 :> NoVal) |
Write something before openingwhy does TLC accept a whole-map copy as a one-key write?
Because, seen through the mapping, it is one. Before the sync the backup was empty; after it, one key holds a value and the other still holds NoVal. The pair of shadows is exactly a Write. Copy a map that differs in two keys and the same code would fail. The check judges shadows, not mechanisms: an intent cannot know how a design does its work.
So the backup is the wrong variable, and one step says why: the backup can lose a written key, and the intent never takes a written key back to NoVal. You found that without reading the protocol: one abstract value per state, then the first jump the abstract spec forbids. That is how every refinement counterexample is read.
Write something before openingso store <- primary is right. Would it still be right in a store where the primary can fail too?
Only if a failure never loses what the primary held. When a primary fails and a backup takes over, the store a client sees is the new primary's map, so the shadow jumps from one map to another in one step. That jump is an AtomicKV step only if the two maps agree, or differ in one key by a value someone wrote. The mapping stays the same; the design has to earn it. The chapter review is a teammate's failover that does not.
Now the clock. Its failure is stranger: each half of the mapping is fine on its own, and together they fail on the first step.
ReplicatedKV takes four steps to do one write: put, apply, sync, ack. Exactly one of them, the apply, changes the shadow store. The other three have to be stuttering steps of the intent. ClockedKV allows stuttering steps; every TLA spec does, because its Next sits inside [...]_vars. But the mapping fixes beat to a clock that moves on every step, so in the abstract view nothing ever stutters. The put has to be a ClockedKV step that swings the beat and leaves the store alone, and since Write must change the key's value (store[k] # v), there is no such step.
Write something before openingcould any implementation with more steps per write than ClockedKV refine it under a clock like this one?
No. Under a clock that moves on every implementation step, the abstract spec has to take a step whenever the implementation does, and every ClockedKV step that is not a stutter changes the store. Each implementation step must then be a full abstract write. An implementation that does a write in four steps would need four writes. The only implementations that pass are those with exactly the intent's granularity: one step per write, with no internal steps. ClockedKV.tla's module comment says why its Write needs the store[k] # v guard for this to hold.
The clock was never the problem. Making it move on every step was: the Warm-up keeps ClockedKV and beat <- clock, and passes once the clock swings only on writes. The same mistake appears as mapping an abstract counter, round or event log to an implementation variable that also changes on internal steps. The intent may count its own steps. The mapping must make that count move only on steps the intent can explain, and stutter the rest of the time.
04In your editor
The two broken checks, and the clock module, are in chapters/11-refinement/specs/. Check this lesson's exercises with:
./tla check 11-04The clock failure, on the first step:
11-refinement/specs/LockstepKV11-refinement/specs/LockstepKV LockstepKV.cfg (Keys={k1, k2}, Vals={v1, v2}, NoVal=NoVal)
parse ok
model check: action property CKStep violated; trace has 2 states (1 steps); 2 distinct states
TLC warning: The formula CKInit is a state-level formula, but it was used as a PROPERTY (or PROPERTIES), where a temporal formula is typically expected. State-level formulas used as PROPERTY or PROPERTIES are only checked in the initial state. To verify that the formula CKInit holds in all states of every behavior, use INVARIANT CKInit instead. Alternatively, applying the "always" temporal operator ([]) to the state-level formula CKInit changes it into a temporal formula, asserting that CKInit holds in all states of every behavior. See https://explain.tlapl.us/invariants-and-properties for additional details.
expected outcome met: property CKStep
trace (changed variables only; ./tla trace --full shows all):
1 Init clock = "tick", primary = (k1 :> NoVal @@ k2 :> NoVal), backup = (k1 :> NoVal @@ k2 :> NoVal), net = {}, busy = FALSE
2 LockstepNext clock: "tick" -> "tock", net: {} -> {[type |-> "put", key |-> k1, val |-> v1]}, busy: FALSE -> TRUEWhen your own refinement check fails, resist reading the protocol first. Run ./tla trace --full, write the shadow next to each state, and find the first pair the abstract spec cannot explain. Then ask which side is wrong: the design, the mapping, or the intent. In this lesson it was the mapping both times. The review in this chapter is a case where it is the design.
05Practice
Warm-up. Keep LockstepKV's clock, but let it stutter. Decide on which steps it should swing.
Core. A key moves from one shard to another. Write the mapping, the move and a witness, so that a client reading the store sees nothing happen when the key moves.
11-04-ex1Warm-up5 min
11-04-ex1 · Tick on writes
Tier: warm-up. Time: about 5 minutes.
LockstepKV.tla is the lesson's step-locked clock: it swings on every step of the protocol, and CKStep fails on the first one. Keep the clock and keep the mapping beat <- clock. Change only when the clock swings.
- Run
./tla check 11-04-ex1and read the two-state trace. Which part of the shadow moved, and which did not? - Fill gap g1 in
LockstepKV.tla: the clock swings on a step that changes the primary's map, and stays where it is on every other step.primary'is the primary's map after the step. - Run the check until it passes with the shadow witness
ShadowFullreached.
Ask yourself afterwards: a put can write the value a key already holds, and then the primary's map does not change. If the clock swung on every PrimaryApply instead, would the check still pass? Predict from ClockedKV's Write, whose first conjunct asks for a new value, then try it.
Hints: HINTS.md, under "11-04-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/11-refinement/exercises/04-ex1-tick-on-writes/; open it after you have a passing answer of your own.
If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.
Done when: ./tla check 11-04-ex1 prints "goal met: pass".
./tla check 11-04-ex1
Repo chapters/11-refinement/exercises/04-ex1-tick-on-writes/
Hints
Hint 1
In the starter's trace, the shadow is the pair of the primary's map and the clock. In the second state, which of the two changed?
Hint 2
The clock may only swing on a step that ClockedKV can explain as a write. Every other step must leave the whole shadow alone.
Hint 3 (nearly the answer)
The clock's new value is either Swing(clock) or clock. Which comparison between the old and the new state says that the step was a write the intent can explain?
11-04-ex2Core8 min
11-04-ex2 · Move a key
Tier: core. Time: about 8 minutes.
ShardMoveKV.tla keeps a store on two shards. owner[k] says which shard owns key k; each shard has a map over all keys, but only the owner's entry counts. Every key starts on shard A. You write three things: the move, the mapping to AtomicKV, and a witness.
- Run
./tla check 11-04-ex2. Both cfgs reportWriteVisibleviolated after one write. That is the course's check of your mapping: a write must show in the store a client reads, and the starter'sStoreis a constant. - Fill gap g2:
Store, the map a client reads, with each key's value taken from the shard that owns it. - Fill gap g1:
Migrate(k)hands keykfrom shard A to shard B. A client reading the store must see no change, so in the abstract view this step must be a stutter. - Fill gap g3:
MovedWithValue, a state in which a key that has moved to shard B holds a written value there. - Run the check until both cfgs pass. Your
ShardMoveKV.cfgchecks the whole spec with your witness. The course'sMCShardMoveKV.cfglets clients write only to keys on shard A, so its witnessCarriedOver, a value on shard B, is reached only by a move that carries the value. LeaveMCShardMoveKV.tla, its cfg andWriteVisibleas they are. - Then break it on purpose: move the ownership but not the value. Read the trace as in the lesson. Write the shadow of the last two states next to each other: which
AtomicKVstep would you need, and why is there none?
Hints: HINTS.md, under "11-04-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/11-refinement/exercises/04-ex2-move-a-key/; open it after you have a passing answer of your own.
Both cfgs must meet their goal, "goal met: pass". If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.
Done when: ./tla check 11-04-ex2 prints "goal met: pass".
./tla check 11-04-ex2
Repo chapters/11-refinement/exercises/04-ex2-move-a-key/
Hints
Hint 1
Start with Store: for each key, which shard's map holds the value a client should read? Then the move: the mapped store reads key k from shard A before it and from shard B after it, and for the step to be a stutter those two must be equal.
Hint 2
Store is a function over Keys built with [k \in Keys |-> …], choosing between a[k] and b[k] by owner[k]. In Migrate, three things change in one step: the value of k arrives on shard B, owner[k] becomes "B", and shard A is left alone. Guard it so that only a key on shard A can move.
Hint 3 (nearly the answer)
Migrate keeps the guard owner[k] = "A". Which value must b'[k] take so that a client reading k sees no change? owner' changes at k only, and a does not change. MovedWithValue combines the owner of some key with what shard B holds for it.
06Explain it
Open NOTES.md and answer the three prompts under 11-04, about the Core exercise.
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:Compare your answers with "11-04 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "Migrate copies the value to shard B and changes the owner in one step, so the mapped store reads the same value before and after: a stutter of AtomicKV. It assumes the copy and the ownership change are atomic; if they were two steps, a read between them would see the wrong shard. When I moved only the ownership, TLC's trace ended with a written key going back to NoVal, which no abstract step does. TLC checked two keys, moves from A to B only, and no failures."
A weak answer: "Migrate copies the key, so nothing is lost." It does not mention the stutter, the atomicity it relies on, or what the broken version showed.
Done?