11-02
Chapter 11 · Refinement is implication
The one-line intent
00Recall
One card from 11-01 on what a coarse model can answer, one from chapter 04 on what a stuttering step leaves unchanged, and one from chapter 09 on what PROPERTY does with a formula that has no []. The last two do the heavy lifting today.
11-01-a from One system, three granularities
Why can AtomicKV not answer "was the client told the write is done?"
Show answer
The question is not a sentence about AtomicKV's variables. It has a store and nothing else: no client, no backup, no ack. A model answers only questions about what it contains.
04-02-a from Stuttering steps
A step of the minute clock takes min from 7 to 8 and leaves hr unchanged. Is it a stuttering step?
Show answer
For the hour clock's spec, subscript hr, yes: hr is unchanged. For the minute clock's spec, subscript << hr, min >>, no: min changed. Stuttering is always relative to a subscript.
09-03-a from Why TLC shows you a lasso
PROPERTY P, where P is a state predicate with no temporal operator. What does TLC check?
Show answer
Only that P holds in the initial states. TLC warns about it. INVARIANT P, or PROPERTY []P, checks every reachable state.
Skip recall Recall is never required.
01Commit
The intent of the store is AtomicKV, one action long. To check that ReplicatedKV keeps it, specs/MCReplicatedKV.tla reads the abstract store off the primary:
KV is defined as INSTANCE AtomicKV WITH store <- primaryINSTANCE ... WITH is read "the module AtomicKV, with primary put in place of store". Its cfg checks KV!Spec, AtomicKV's whole behavior spec seen through that substitution, against every behavior of ReplicatedKV.
Commit a
ReplicatedKV's ClientPut step changes net and busy but not primary. AtomicKV has no action for sending a message. What does TLC make of that step?
- A violation: no action of AtomicKV explains it.
- Nothing wrong: seen through the mapping, it is a step that leaves store unchanged.
- An error: ReplicatedKV has variables that AtomicKV never declares.
Answer Nothing wrong: seen through the mapping, it is a step that leaves store unchanged.
- If you picked A
- Only a step that changes the shadow needs an AtomicKV action to explain it. A step that leaves primary alone leaves the abstract store alone, and AtomicKV's Spec allows such steps: that is what the subscript in [][Next]_vars says.
- If you picked C
- The mapping must supply AtomicKV's variables, nothing more. ReplicatedKV may have as many other variables as it likes; AtomicKV does not mention them, so it says nothing about them.
Commit b
You are writing the intent spec of the store, the one every design is checked against. Which variables should it have?
- store, primary and backup, so it matches the real system.
- store only.
- store and busy, so it can say when the client is waiting.
Answer store only.
- If you picked A
- Then only a design with exactly a primary and a backup could satisfy it. A store with three replicas, or none, would fail the intent for reasons that have nothing to do with being a store.
- If you picked C
- Whether the client waits, and how, is a design choice. The intent says what a write does to the store. A spec about the client's waiting would be a second, different intent.
02Lab
./tla check chapters/11-refinement/specs/MCReplicatedKV.cfg11-refinement/specs/MCReplicatedKV11-refinement/specs/MCReplicatedKV MCReplicatedKV.cfg (Keys={k1, k2}, Vals={v1, v2}, NoVal=NoVal)
parse ok
model check: no violation found in 137 distinct states (265 generated, depth 11)
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: pass
coverage: all 5 actions fired
witness ShadowFull reached (trace of 7 states)Reveal for commit a
No violation of KVInit or KVStep in 137 distinct states, every action fired, and the witness ShadowFull was reached. ClientPut is one of the steps that leave the shadow unchanged, and AtomicKV allows those.
Reveal for commit b
store only. An intent spec states what any correct design must do and nothing about how. The primary, the backup and the client's waiting are how; they belong in the design, which is checked against the intent.
You can watch choice x fail a good design. specs/TwoCopyIntent.tla is the intent with a backup added, in which every write reaches the store and the backup in the same step, and specs/MCTwoCopyIntent.cfg checks ReplicatedKV against it:
./tla check chapters/11-refinement/specs/MCTwoCopyIntent.cfgTCStep is violated after a trace of 3 states: the primary applies a put while the backup is still behind. That is the same design that passes against AtomicKV. Nothing is wrong with it; the intent made a design decision for it.
03Reason
If you picked "a violation", you expected every step of ReplicatedKV to be one of AtomicKV's steps. Only the steps that change the shadow have to be. Here is the whole intent again:
Write(k, v) is defined as
and storeprime = [store EXCEPT ![k] = v]One key changes, to a value a client may write, in one step. It says nothing about servers, messages, clients or time, and that silence is the point. Every one of those details is a decision a design makes, and the intent must not make it for them.
Now the check. INSTANCE AtomicKV WITH store <- primary substitutes the expression primary for the variable store everywhere in AtomicKV. So KV!Spec is a formula about ReplicatedKV's variables: "primary starts as Empty, the map that sends every key to NoVal, and every step either writes one key of primary or leaves primary unchanged." Call primary, viewed this way, the shadow: the abstract state the mapping reads off each concrete state. Refinement is the claim that every behavior of the design, viewed through the mapping, is a behavior of the intent.
KV!Spec is KV!Init /\ [][KV!Next]_KV!vars. The cfg checks the two halves under their own names, KVInit and KVStep, so that when one fails, TLC's report says which. KVStep is the subscripted box from chapter 04: every step is an AtomicKV step or leaves KV!vars, which is << primary >>, unchanged. ClientPut, BackupApply, ClientAck and BackupCrash do not touch primary, so they pass as stuttering steps of the abstract store. Only PrimaryApply moves the shadow, and it moves it by exactly one write.
Write something before openingthe cfg also names a witness, ShadowFull, on the shadow. What failure is that witness for?
For a refinement that passes because the shadow never moves. If the mapping read the store off something constant, every step would be a stuttering step, KVStep would hold in every behavior, and the check would mean nothing. ShadowFull == KV!AllWritten must be reached, which forces the shadow to take real writes. A witness on the implementation, such as "the backup fills up", would not catch it: the protocol can do all its work while a bad mapping looks away.
Write something before openingwhat does TLC report for MCReplicatedKV.initinv.cfg, and why is the warning not a bug in the check?
KVInit is violated after a trace of 3 states: the first write reaches the primary, and the shadow no longer sends every key to NoVal. Of course it is not; AtomicKV's Init describes the first state of a behavior, not every state. Checking it only in the initial state is exactly right here. TLC's warning is generic advice for the common mistake from chapter 09, a PROPERTY that was meant to be an invariant. It does not know which one you meant.
Notice what the passing check does not say. It does not say that a client reading the store sees the latest value: AtomicKV has no reads. It does not say the backup is useful: the intent never mentions it. A refinement is exactly as strong as the intent, and the intent is exactly as strong as the sentence you wrote. That is why writing the one line is the hard part.
An intent can also be too strong. Suppose a teammate adds a second rule to AtomicKV: "a key, once written, is never written again". It sounds like extra safety. Under that intent ReplicatedKV fails: nothing stops a client from putting v2 to a key that holds v1, and when the primary applies it, the shadow takes a step the intent forbids. So would any design of this product, because the rule forbids the overwrite the store exists to do. A too-strong intent fails good designs. A too-weak one passes bad ones. Both look fine on the page.
Write something before openinghow would you tell, before writing any design, whether an intent is too weak or too strong?
Write down two behaviors you know are wrong, such as a write that changes two keys, and two you know are right, such as overwriting a value. Check that the intent rules out the first pair and allows the second. In this course that test is a grader cfg or a mutant; on a whiteboard it is a list. The Core exercise below does exactly this for a lock, with one check from each side.
04In your editor
The intent is chapters/11-refinement/specs/AtomicKV.tla and the check is MCReplicatedKV.tla next to it. Check this lesson's exercises with:
./tla check 11-02When you write an intent spec for your own system, write the sentence first: "a write replaces one key's value, all at once". Then give the spec only the variables that sentence mentions. If you find yourself adding a variable because the design has it, stop and ask whether a different correct design would need it too. If not, it belongs in the design.
Then run the refinement check early, before the design is finished. A failing refinement on a half-built design is cheap to read: few actions, short traces.
05Practice
Warm-up. Product adds delete. Add it to the intent, and see whether ReplicatedKV, which never deletes, still implements the new intent.
Core. Write the one-line intent of a lock, and check it from both sides: a message-based lock server must implement it, and it must not let the lock pass between clients without being free.
11-02-ex1Warm-up5 min
11-02-ex1 · Delete
Tier: warm-up. Time: about 5 minutes.
Product adds a delete to the store. The intent changes first; the design comes later.
- Run
./tla check 11-02-ex1.AtomicKV.cfgreports thatDeleteAvailableis violated after one write: a key holds a value, and the starter'sDeletecannot remove it.MCReplicatedKV.cfgis the refinement check from the lesson, against this new intent; leave its result for step 4. - Fill gap g1 in
AtomicKV.tla:Delete(k)returns keyktoNoVal, if it holds a value. No other key changes. - Run the check.
AtomicKV.cfgchecks three course properties at the bottom ofAtomicKV.tla: no step changes more than one key (OneKeyPerStep), a delete takes its key back toNoValand does nothing else (DeleteClears), and a key that holds a value can always be deleted (DeleteAvailable). Leave them and the cfg as they are. - Before you read the second result, predict:
ReplicatedKVhas no delete. Does it still implement the intent that has one?
Hints: HINTS.md, under "11-02-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/11-refinement/exercises/02-ex1-delete/; open it after you have a passing answer of your own.
Both cfgs must meet their goal, "goal met: pass", with every action firing and the witness reached. 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-02-ex1 prints "goal met: pass".
./tla check 11-02-ex1
Repo chapters/11-refinement/exercises/02-ex1-delete/
Hints
Hint 1
Delete looks like Write with one difference in the new value. Which value means "never written"?
Hint 2
The guard is "the key holds a value": store[k] is not NoVal. Without it a delete of an empty key changes nothing, which is a stutter, not a delete.
Hint 3 (nearly the answer)
Two conjuncts: the guard on store[k], and store' as store with key k set to NoVal, using EXCEPT. For the second result: does an implementation have to use every action its intent allows?
11-02-ex2Core8 min
11-02-ex2 · The lock's intent
Tier: core. Time: about 8 minutes.
LockServer.tla is a lock server and two clients that talk by messages. Write the one-line intent of a lock, AtomicLock, and check it from both sides.
- Run
./tla check 11-02-ex2. Both cfgs stop at the first state.AtomicLock.cfgreports that yourInitlets somebody hold the lock at the start;MCLockServer.cfgreports that the server's first state is not an initial state of your intent. Read both. - Fill gaps g1 to g3 in
AtomicLock.tla: nobody holds the lock at the start; a client may take it only when it is free; only the holder gives it back. One variable,holder. - Run the check until both cfgs pass.
The two cfgs pull in opposite directions, and that is the point:
AtomicLock.cfgchecksNoSteal, the course's property that the lock never passes straight from one client to another, andStartsFree, that nobody holds it at the start. An intent so loose that it allows either fails here.MCLockServer.cfgchecks that the real server, seen throughholder <- owner, is a behavior of your intent, with the witnessShadowHeldon the shadow. An intent so tight that the server cannot keep it fails here.
Leave MCLockServer.tla, MCLockServer.cfg, LockServer.tla, and NoSteal and StartsFree in AtomicLock.tla, as they are.
Hints: HINTS.md, under "11-02-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/11-refinement/exercises/02-ex2-lock-intent/; 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-02-ex2 prints "goal met: pass".
./tla check 11-02-ex2
Repo chapters/11-refinement/exercises/02-ex2-lock-intent/
Hints
Hint 1
One variable, holder. Read each gap's comment as a sentence about holder before and holder' after.
Hint 2
Init is an equality, not membership. Acquire(c) needs the lock free before the step and held by c after it. Release(c) needs c to hold it before.
Hint 3 (nearly the answer)
Each action is two conjuncts: a guard comparing holder with None or with c, and holder' set to c or to None. If NoSteal fails, the guard of Acquire is missing; if MCLockServer fails, read which server step your intent could not explain.
06Explain it
Open NOTES.md and answer the three prompts under 11-02, 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-02 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "AtomicLock has one variable, holder; a client may take a free lock and only the holder can release it. The lock server implements it under holder <- owner, because only Grant and GetRelease change owner, and each is one abstract step; every other server step is a stutter. The mapping assumes the server's view is the truth: the server still counts a client as owner after the client has sent its release, and after Grant a client owns the lock before it knows; the intent describes neither gap. TLC checked two clients with no message loss and no timeouts."
A weak answer: "My intent passes both checks, so it is correct." It does not say what each check rules out, or which assumption about the server the mapping makes.
Done?