11-05
Chapter 11 · Refinement is implication
When no mapping exists
00Recall
One card from 11-04 on reading a refinement trace, one from chapter 10 on the difference between weak and strong fairness, and one from chapter 04 on what the hour clock's check really covered. Fairness comes back in the second half of this lesson.
11-04-a from Break the mapping, read the trace
How do you read a refinement counterexample?
Show answer
Compute the shadow, the abstract state the mapping reads off, in every state of the trace. The violating step is the first pair of shadows that is neither an abstract step nor a stutter. TLC prints only the implementation; the shadow is your job.
10-03-a from When WF is not enough
What is the one difference between the definitions of WF_v(A) and SF_v(A)?
Show answer
The hypothesis. WF needs ENABLED <<A>>_v continuously (<>[]); SF needs it only infinitely often ([]<>). Both then demand <<A>>_v steps infinitely often.
04-04-b from The frame-rate test and the shadow
The minute clock satisfies the hour clock's spec. What exactly was checked?
Show answer
That every behavior of the minute clock, with only hr kept (its shadow), is a behavior the hour clock's spec allows, stuttering included: Spec => HourSpec. Nothing about how hr lines up with min.
Skip recall Recall is never required.
01Commit
A store keeps a primary and a lazily synced replica, specs/LazyKV.tla. A read may go to either copy, so it may return an old value. The product promise is specs/LooseKV.tla: "a read returns some value that was written to that key at some point, or NoVal if it was never written". To state "at some point", LooseKV keeps a variable seen: for each key, every value it has ever held. LazyKV keeps only current values.
specs/MCLazyKV.tla tries the obvious mapping: seen is whatever the two copies hold now, plus NoVal.
Commit a
MCLazyKV's mapping fails. Could a cleverer expression over primary, replica and got serve as seen, so that the check passes?
- Yes: add the replica's old values to the set as well.
- Yes: map seen to every value, so any read is allowed.
- No: after two writes to one key with no sync and no read between them, the first value is in no variable at all.
- Yes: add got to every key's set, so a value that was read is kept.
Answer No: after two writes to one key with no sync and no read between them, the first value is in no variable at all.
- If you picked A
- The replica holds one value per key, its current one. Once a key is written twice with no sync and no read between, the first value was never copied anywhere.
- If you picked B
- Then LooseKV's Init fails: before any write, seen must hold NoVal alone. And a write must add exactly the written value, so seen cannot jump.
- If you picked D
- got holds one value, from the last read, and only a read sets it. Write v1 and then v2 to one key with nobody reading, and v1 was never in got. A read records what it saw, not what was written.
The alternating bit protocol passed its safety refinement in 11-03. ReliableChannel also has a liveness half, Liveness == WF_vars(Deliver): weak fairness on Deliver, which together with the safety part means that every message handed over is eventually delivered. specs/MCAlternatingBit.live.cfg checks Chan!Liveness on the protocol's Spec, with no fairness of its own.
Commit b
The protocol refines ReliableChannel's safety part. What does TLC report for Chan!Liveness on the protocol's Spec, which has no fairness?
- No violation: the refinement carries the abstract fairness over.
- A violation: the protocol may stop after taking a message and never deliver it.
- An error: fairness cannot be checked through a mapping.
Answer A violation: the protocol may stop after taking a message and never deliver it.
- If you picked A
- WF_vars(Deliver) is part of ReliableChannel's FairSpec, a separate conjunct. The protocol's Spec says what steps may happen, never that any must. Nothing makes the receiver act.
- If you picked C
- Chan!Liveness is an ordinary temporal formula over the protocol's variables, with Pending substituted for q, and TLC checks it like any other PROPERTY. Inside WF, ENABLED is computed over the protocol's variables after the substitution; for Deliver it comes to Pending # << >>.
02Lab
./tla check chapters/11-refinement/specs/MCLazyKV.cfg
./tla check chapters/11-refinement/specs/HistoryLazyKV.cfg
./tla check chapters/11-refinement/specs/MCAlternatingBit.live.cfg
./tla check chapters/11-refinement/specs/MCAlternatingBit.fair.cfg11-refinement/specs/MCLazyKV11-refinement/specs/MCLazyKV MCLazyKV.cfg (Keys={k1, k2}, Vals={v1, v2}, NoVal=NoVal)
parse ok
model check: action property LooseStep violated; trace has 3 states (2 steps); 6 distinct states
TLC warning: The formula LooseInit 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 LooseInit holds in all states of every behavior, use INVARIANT LooseInit instead. Alternatively, applying the "always" temporal operator ([]) to the state-level formula LooseInit changes it into a temporal formula, asserting that LooseInit holds in all states of every behavior. See https://explain.tlapl.us/invariants-and-properties for additional details.
expected outcome met: property LooseStep
trace (changed variables only; ./tla trace --full shows all):
1 Init primary = (k1 :> NoVal @@ k2 :> NoVal), replica = (k1 :> NoVal @@ k2 :> NoVal), got = NoVal
2 Write primary: (k1 :> NoVal @@ k2 :> NoVal) -> (k1 :> v1 @@ k2 :> NoVal)
3 Write primary: (k1 :> v1 @@ k2 :> NoVal) -> (k1 :> v2 @@ k2 :> NoVal)Reveal for commit a
No expression works. MCLazyKV fails after a trace of 3 states: two writes to one key, with different values. After the second, the first is gone from primary, from replica and from got. HistoryLazyKV adds one variable that remembers, and passes in 345 distinct states with the shadow witness reached.
Reveal for commit b
ChanLiveness is violated: a trace of 2 states, then stuttering forever. The sender takes the first message and nothing else ever happens. MCAlternatingBit.fair.cfg checks the same property on FairSpec, which adds the protocol's own fairness, a first attempt with weak fairness on sending and on receiving. It is still violated, this time with a lasso: a loop that repeats forever, not a stop. The Warm-up asks why, and changes one conjunct; with that change, the same check reports pass.
03Reason
If you picked "yes", you assumed a mapping always exists if you are clever enough. Here is why none does. Take two runs of LazyKV. In the first, a client writes v2 to key k1. In the second, it writes v1 and then v2, with no sync between them. Both end in the same state: primary[k1] is v2, the replica is empty, and nothing was read. A mapping is a function of the state, so it must give seen the same value in both. But in the first run seen[k1] can only be {NoVal, v2}, and in the second it must also contain v1, because LooseKV's seen never forgets. One state, two required answers. No expression exists.
Write something before openingso is LazyKV wrong? Does it break the promise?
No. Every read returns primary[k] or replica[k], and both were written at some point. By that argument the promise holds in every behavior of LazyKV; the history-variable check below confirms it only for two keys, two values and one reader. What fails is the proof technique: the information needed to name the abstract state was thrown away by the implementation, because the implementation never needed it. LooseKV's seen is bookkeeping for the statement, not something any store keeps.
The fix is an auxiliary variable: a variable added to the implementation purely so the mapping has something to point at. The simplest kind is a history variable, which records the past. specs/HistoryLazyKV.tla adds written, the set of values ever written to each key, updated only by writes:
HWrite(k, v) is defined as
and Write(k, v)
and writtenprime = [written EXCEPT ![k] = @ union {v}]and maps seen <- written. Now the check passes. But you changed the implementation to make it pass, and this chapter has spent four lessons warning you about exactly that. Why is this allowed?
Write something before openingwhat rule must written obey so that a pass with it still tells you something about the original LazyKV?
It must not change what LazyKV can do. No guard reads it, no other variable's new value is computed from it, and every LazyKV step can still happen with some update to it. Then the behaviors of HistoryLazyKV, with written erased, are exactly the behaviors of LazyKV. A variable that blocked a step, or steered a choice, would be a design change smuggled in as bookkeeping.
Write something before openingwhy does a variable that changes no behavior add states?
A state is a value for every variable. Two LazyKV states that look alike now can have different pasts, and written tells those pasts apart, so they become different states. That is the same split that made the mapping impossible: the history variable stores exactly the information the abstract spec needed and LazyKV discarded. The behaviors did not change. The states got more detailed.
Sometimes the past is not enough. If the abstract spec makes a choice earlier than the implementation does, say an abstract read that fixes its answer when the request arrives while the real store decides later, no record of the past can say which choice the abstract spec made. Then you need a prophecy variable, which guesses the future and is checked when the future arrives. Lamport and Merz show how to write them in ordinary TLA+, but this course does not exercise them. When you meet a refinement that fails because the abstract side commits too early, you will know the name to look up.
The second COMMIT is about the other half of a spec. Spec => Chan!Spec covered safety because Chan!Spec has no fairness. Liveness does not come for free: the protocol's Spec permits stopping, so it cannot imply that anything is delivered. You check liveness through the mapping like any property, and you supply the implementation's own fairness, stated in its own terms: which of its actions must keep happening. The Warm-up asks which kind of fairness the receiver needs on a lossy channel.
04In your editor
The store, the intent and both checks are in chapters/11-refinement/specs/. Check this lesson's exercises with:
./tla check 11-05The history variable, passing:
11-refinement/specs/HistoryLazyKV11-refinement/specs/HistoryLazyKV HistoryLazyKV.cfg (Keys={k1, k2}, Vals={v1, v2}, NoVal=NoVal)
parse ok
model check: no violation found in 345 distinct states (3145 generated, depth 8)
TLC warning: The formula LooseInit 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 LooseInit holds in all states of every behavior, use INVARIANT LooseInit instead. Alternatively, applying the "always" temporal operator ([]) to the state-level formula LooseInit changes it into a temporal formula, asserting that LooseInit holds in all states of every behavior. See https://explain.tlapl.us/invariants-and-properties for additional details.
expected outcome met: pass
coverage: all 4 actions fired
witness ShadowOverwritten reached (trace of 3 states)When a refinement check of your own fails and the trace shows two different pasts reaching one state, stop looking for a cleverer mapping. Add a history variable, update it in the actions that create the information, and check that no guard or other update reads it. Then write in the module comment what it records and why the mapping needs it, so a reviewer can confirm it changes nothing.
05Practice
Warm-up. The protocol's fairness, written by someone who reached for weak fairness first. Make Chan!Liveness pass by changing only the receiver's fairness conjunct.
Core. A feature-flag client keeps only current values; the rollout promise talks about every value ever published. Add the history variable and the mapping.
11-05-ex1Warm-up5 min
11-05-ex1 · Strong enough
Tier: warm-up. Time: about 5 minutes.
AlternatingBit.tla here has a Fairness written by someone who reached for weak fairness on both sides. MCAlternatingBit.live.cfg checks ReliableChannel's liveness through the lesson's mapping, on FairSpec.
- Run
./tla check 11-05-ex1. InMCAlternatingBit.live.cfg,ChanLivenessis violated, with a lasso. Read the loop: which action keeps being possible, and why does weak fairness not force it? Recall what the network does to the data channel inside the loop. - Fill gap g1 in
AlternatingBit.tla: fairness that makesChan!Livenesshold on a network that loses messages, by changing only the receiver's conjunct. Keep the sender's conjunct as it is, and do not add fairness toLoseDataorLoseAck. - Run the check until
MCAlternatingBit.live.cfgpasses with every action firing andShadowDonereached. The course'sMCAlternatingBit.deaf.cfgruns yourFairnesson a receiver that takes messages and never delivers them, soChanLivenessmust fail there: it meets its goal from the start, and a conjunct that assumes delivery or an ack outright makes it pass instead. Neither cfg reads the sender's conjunct, so keep it as step 2 says. LeaveMCAlternatingBit.tlaand both cfgs as they are.
Fairness on the receiver says something about the real system. In NOTES.md, write two sentences. First: what in a real network and a real receiver has to be true for your fairness to hold? Second: the sender kept its weak fairness, and the check still passed. Why is weak fairness enough for SendData when it was not enough for RcvData?
Hints: HINTS.md, under "11-05-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/11-refinement/exercises/05-ex1-strong-enough/; 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. Both cfgs must meet their goals: "goal met: pass" for MCAlternatingBit.live.cfg and "goal met: property ChanLiveness" for MCAlternatingBit.deaf.cfg. You are done when every cfg prints its "goal met" line and none prints "goal not met" or "starter state". The run's last line, "2 of 2 checks met their expected outcome", is no sign of that: the untouched starter prints it too.
Done when: ./tla check 11-05-ex1 prints "goal met: pass".
./tla check 11-05-ex1
Repo chapters/11-refinement/exercises/05-ex1-strong-enough/
Hints
Hint 1
Read the loop in the lasso. Is RcvData enabled in every state of the loop, or only in some of them?
Hint 2
Weak fairness forces an action only if it is enabled continuously from some point on. The network can keep emptying the data channel, so a receive is enabled only now and then. Chapter 10 named the fairness that covers "now and then".
Hint 3 (nearly the answer)
The conjunct to change is the one on RcvData. Keep its subscript and its action, and change only the kind of fairness to the one from chapter 10 that still applies when an action is enabled only now and then.
11-05-ex2Core8 min
11-05-ex2 · Remember the flags
Tier: core. Time: about 8 minutes.
FlagClient.tla is a feature-flag server and one app with a cache. It keeps only current values. Rollout.tla is the promise: the app may run with any value that was ever published, however old. To say "ever", Rollout keeps published. Like the lesson's LazyKV, the client throws away exactly what the promise talks about.
- Run
./tla check 11-05-ex2.RStepfails within a few steps. Read the trace and find the value that disappears from the shadow. - In
HistoryFlagClient.tla, fill gaps g1 and g2: a history variableeverPublishedthat starts with whatRollout'spublishedstarts with, and that a publish adds to. No other action changes it. - Fill gap g3: map
publishedto your history variable. - Run the check until
HistoryFlagClient.cfgpasses with every action firing andShadowBothPublishedreached. The course'sMCHistoryFlagClient.keep.cfgmust pass too: it checks that your history changes nothing the client does, so that the pass is about the real client. A history that blocks a publish the client allows fails it. - A new app release runs with a built-in default while its cache holds nothing.
MCHistoryFlagClient.fallback.cfgchecks that client through your mapping, andRStepfails again. Before you read the trace, write down whether some other history variable could make it pass, and why.
Leave FlagClient.tla, Rollout.tla, MCHistoryFlagClient.tla and its two cfgs as they are.
Hints: HINTS.md, under "11-05-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/11-refinement/exercises/05-ex2-remember-the-flags/; 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. All three cfgs must meet their goals: "goal met: pass" for HistoryFlagClient.cfg and MCHistoryFlagClient.keep.cfg, and "goal met: property RStep" for MCHistoryFlagClient.fallback.cfg. You are done when every cfg prints its "goal met" line and none prints "goal not met" or "starter state". The run's last line, "3 of 3 checks met their expected outcome", is no sign of that: the untouched starter prints it too.
Done when: ./tla check 11-05-ex2 prints "goal met: pass".
./tla check 11-05-ex2
Repo chapters/11-refinement/exercises/05-ex2-remember-the-flags/
Hints
Hint 1
Look at Rollout's Init and Publish. Your history variable must start and grow exactly as published does.
Hint 2
published starts as the set holding Unset alone, and a publish adds the published value to it. Refresh and adopt never change it.
Hint 3 (nearly the answer)
Put HInit next to Rollout's Init, and HPublish(v) next to Rollout's Publish: in both, your history must do what published does. The mapping needs an expression for published and one for flag.
11-mxMixed15 min · mastery path
11-mx · Printed once
Tier: mixed. Time: about 15 minutes. Off the core clock: this is on the mastery path.
A till sends print jobs to a receipt printer over a network that can deliver a message more than once. The printer prints whatever job arrives. The shop's rule: no customer gets two receipts for the same order.
- Run
./tla check 11-mx. All four cfgs pass, because the starter'sPrintedOnceisTRUE. - Fill gap g1 in
ReceiptPrinter.tla:PrintedOncestates the shop's rule overcopies. - Run the check and read the trace. Which part of the model lets the job arrive twice? There is no action called "duplicate".
The check also runs three course tests of your claim, all in MCReceiptPrinter.tla. In MCReceiptPrinter.cfg the printer skips an order it has already printed. That printer keeps the rule, so your PrintedOnce must pass there; a claim that forbids printing at all fails, because every order must still get its receipt. In MCReceiptPrinter.half.cfg the printer skips duplicates of every order but one, and MCReceiptPrinter.other.cfg runs it with the other order unlucky. That printer breaks the rule for one customer, so your PrintedOnce must fail on both; a claim about one order passes on one of them. Leave the four MCReceiptPrinter files as they are.
Hints: HINTS.md, under "11-mx hint 1" to "hint 3". The solution is in solutions/chapters/11-refinement/exercises/mx-printed-once/; open it after you have an answer of your own.
All four cfgs must meet their goals: "goal met: violation PrintedOnce" for ReceiptPrinter.cfg, MCReceiptPrinter.half.cfg and MCReceiptPrinter.other.cfg, and "goal met: pass" for MCReceiptPrinter.cfg. You are done when every cfg prints its "goal met" line and none prints "goal not met" or "starter state". The run's last line, "4 of 4 checks met their expected outcome", is no sign of that: the untouched starter prints it too. 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-mx prints "goal met: violation PrintedOnce".
./tla check 11-mx
Repo chapters/11-refinement/exercises/mx-printed-once/
Hints
Hint 1
copies[o] counts the receipts printed for order o. The rule is about every order.
Hint 2
"Not two receipts" is "at most one". Quantify over Orders with \A.
Hint 3 (nearly the answer)
PrintedOnce quantifies over Orders. Which comparison of copies[o] with one says "at most one"? A claim that every count is zero fails the course's check: a correct printer still prints.
06Explain it
Open NOTES.md and answer the three prompts under 11-05, 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-05 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "everPublished starts as {Unset} and grows with every publish, exactly like Rollout's published, and the mapping points published at it. No guard or update reads it, so the client's behaviors are unchanged and the pass is about the real client. Without it no mapping exists: after two publishes and no refresh, the first value is in no variable. TLC checked two flag values and one app, with no liveness: it did not check that the app ever adopts the latest value."
A weak answer: "I added a variable and now the refinement passes." It does not say why adding a variable is allowed, which is the whole question.
07Your system
Milestone 5: the abstract spec your system should refine.
Keep this work in my-system/, in your private repository. Strip identifiers from anything you share, and do not paste proprietary designs, customer data, credentials or unreleased specs into a third-party AI tool without your organisation's approval. Prefer a local model for sensitive work.
- Write the promise your system makes to its users in one sentence, with no mechanism in it. "A job submitted is run at most once and is eventually run" is a promise. "The worker takes a lease" is a mechanism.
- Write that sentence as a module of its own, with only the variables the sentence mentions. Give it a witness that needs real work, such as "two jobs have run".
- In a wrapper
MC<YourModule>.tla, map each abstract variable to an expression over the model you built at milestones 2 to 4. Check the two halves of the abstractSpecas properties, with a witness on the shadow. - If the check fails, read the trace as in 11-04 and decide: is it the design, the mapping or the intent? If two pasts reach one state, add a history variable and say in a comment why it changes nothing.
Tick milestone 5 in my-system/MILESTONES.md when the refinement passes with the shadow witness reached, or when you have a trace that shows your design breaks its own promise. Then write in my-system/DECISIONS.md the constants, constraints and fairness the result depended on, the bounds column of the evidence table, so that nobody later quotes "it refines" without them.
Done?