Section 3.32

Refinement

Definition

Refinement is a correctness relation between two descriptions of one system at different levels of detail: an implementation refines a specification when every behavior the implementation can show is one the specification allows. A behavior is the sequence of observable events in one run, such as outputs, external calls and faults; internal steps are hidden. The implementation may settle the specification’s choices and so do less than it permits, but never what it forbids. Refinement is usually proved with a simulation relation between implementation states and specification states.

The idea

A specification says what a system may do and ignores how. An implementation decides how, with registers, temporary variables and extra steps that the specification never mentions. Comparing the two state by state fails at once. Refinement compares them where it matters: at the boundary an observer can see. The specification of a virtual machine §2.3 instruction “add register 1 to register 0” is one step. A compiled implementation may take four machine instructions and two temporary registers to do it. The program running on the virtual machine sees only that register 0 changed from 2 to 5.

Refinement also organizes large proofs. Prove a property once about a small abstract specification, then prove that each layer of detail refines the layer above it. Refinement is transitive: if CC refines BB and BB refines AA, then CC refines AA. A safety property §1.11 stated over observable events, one saying that some bad sequence of events never happens, carries down through every layer, because each layer’s behaviors are among those of the layer above. A building plan and the finished building are a fair picture. The plan shows a door between two rooms; the builder chooses hinges and screws that the plan never mentions. The building refines the plan if every way a visitor can walk through it is on the plan. The picture stops at choice: a plan rarely leaves the builder a choice, but a specification often does, as in “return any free buffer”, and the implementation may settle it.

How it works

A refinement claim is set up and proved in five steps.

  1. Choose the observable events: the events through which the guarantee is stated, or through which anything outside the system could tell the two descriptions apart. Typical observable events are returned values, outputs, writes to memory that another component reads or that the guarantee protects, calls to external functions with their arguments, and faults. All other steps are internal, also called silent.

  2. Define behaviors. The trace of a run is its sequence of observable events, with the silent steps left out.

  3. State the relation. The implementation refines the specification when every trace of the implementation is a trace of the specification.

  4. Prove it with a simulation relation, which pairs implementation states with specification states. Two obligations suffice. First, every initial implementation state is paired with some initial specification state. Second, whenever a paired implementation state takes one step, the specification can follow from its partner and reach a state paired with the new implementation state. If the implementation step was silent, the specification follows with zero or more silent steps; if it showed an event, the specification follows with silent steps around exactly one step that shows the same event. Following an implementation run step by step then builds a specification run with the same trace.

  5. Often the simulation relation is given by an abstraction function, which maps each implementation state to the specification state it represents, together with a representation invariant: a condition on implementation states under which the mapping makes sense, such as “the array §14.5 is sorted” for an implementation of a set by a sorted array.

Worked example

A correct implementation. A virtual machine runs a program, here called the guest. The specification has guest registers r0 and r1, and the instruction add r0, r1 sets r0 to the sum of r0 and r1. The implementation keeps the guest registers in memory cells regs[0], regs[1] and so on, and carries out the instruction with four machine instructions:

  1. load regs[0] into machine register t1;

  2. load regs[1] into machine register t2;

  3. add t2 to t1;

  4. store t1 into regs[0].

The abstraction function reads r0 from regs[0] and r1 from regs[1], and ignores t1 and t2. Start with regs[0] holding 2 and regs[1] holding 3, so the specification state is 𝚛𝟶=2,𝚛𝟷=3\texttt{r0} = 2, \texttt{r1} = 3. Machine instructions 1 to 3 change only t1 and t2, so the abstract state, the pair of values of r0 and r1, stays (2,3)(2, 3): they are silent steps, matched by no specification step. Machine instruction 4 makes regs[0] equal to 2+3=52 + 3 = 5, so the abstract state becomes (5,3)(5, 3), exactly the result of the specification’s one step.

An extra effect. A second implementation also stores t2 into regs[7], the cell that holds guest register r7. It still produces 𝚛𝟶=5\texttt{r0} = 5. The specification says that add r0, r1 leaves r7 unchanged, and the next guest instruction can read r7, so the change is observable and the second implementation does not refine the specification. A comparison of r0 alone would have missed the difference.

Settling a choice. The specification of an allocator says: return the number of any free slot. With slots 2 and 5 free, it allows the trace consisting of the single event “return 2” and the trace consisting of “return 5”. An implementation that always returns the lowest free slot produces “return 2” and refines the specification. An implementation that returns 3, a slot in use, does not.

A run that never ends. An implementation that loops forever without any observable event has only the empty trace. The empty trace is a trace of every specification, because every run of the specification starts with no events yet. Under trace inclusion, this implementation therefore refines the allocator specification although it never allocates anything. The precise statement below repairs this.

Stated precisely

Model each description as a labeled transition system: a set of states, a set of initial states, and steps, each labeled either with an observable event from a set EE or with the silent label τ\tau (the Greek letter tau). The trace of a finite run is its sequence of labels with every τ\tau removed, and Tr(X)\mathrm{Tr}(X) is the set of traces of all finite runs of system XX that start in an initial state. Implementation CC refines specification AA, written C⊑AC \sqsubseteq A, when Tr(C)⊆Tr(A)\mathrm{Tr}(C) \subseteq \mathrm{Tr}(A).

A forward simulation §19.2.9 is a relation ℛ\mathcal{R} between states of CC and states of AA with two properties. (1) Every initial state of CC is related to some initial state of AA. (2) Whenever cℛac \mathrel{\mathcal{R}} a and CC steps from cc to c′c' with label ℓ\ell, system AA can go from aa to some state a′a' with c′ℛa′c' \mathrel{\mathcal{R}} a': by τ\tau steps only, possibly none, when ℓ=τ\ell = \tau, and by τ\tau steps, one step labeled ℓ\ell and τ\tau steps when ℓ∈E\ell \in E. If a forward simulation exists, then C⊑AC \sqsubseteq A. The converse fails: when the specification makes a choice earlier than the implementation does, trace inclusion can hold although no forward simulation exists. Backward simulations, or the prophecy variables of Martin Abadi and Leslie Lamport (1991), cover that case.

Trace inclusion over finite runs preserves every safety property stated over EE, but it does not preserve liveness properties §1.12, as the implementation that loops forever shows. To preserve termination, compare complete behaviors instead: for each run, its trace together with how it ends, which is normal termination with a result, a fault, or running forever. Then require every complete behavior of CC to be a complete behavior of AA. The looping implementation’s complete behavior, “runs forever with no events”, is not one of the allocator’s, so the stronger relation rejects it. Compiler correctness §3.40 uses this stronger relation with one convention: where the source program has undefined behavior §4.12, every behavior counts as allowed.

⊑\sqsubseteq is reflexive and transitive. Refinement in both directions, C⊑AC \sqsubseteq A and A⊑CA \sqsubseteq C, is trace equivalence, which is weaker than bisimulation §19.2.10.

Limits and common mistakes

The word has several technical meanings. Stepwise refinement is a way of developing a program by adding detail in stages. Data refinement replaces an abstract data type by a concrete one. In counterexample-guided abstraction refinement §15.13, refining means making an abstraction more precise. A refinement claim must name its definition: the direction, the observable events, and the treatment of choice and of progress.

The choice of observable events decides what the proof covers, and an event left out is not checked. A refinement over results and memory says nothing about timing, so it preserves neither constant-time behavior nor deadlines. For those, the observable events must include the timing, or the sequence of memory addresses and branch decisions on which timing depends.

Refinement removes possibilities. A property of the specification of the form “a reset is always still possible” can fail in an implementation that settled the choices differently.

Running the implementation and the specification on the same inputs and comparing the results is evidence about those runs, not a proof of refinement for all runs.

A refinement proof is relative to its model of the environment: what interrupts §6.11, other threads and external functions may do. It proves nothing about an environment that behaves differently.

A wrong specification is refined faithfully. Refinement carries the specification’s properties down to the implementation; it does not check that they are the right properties.

Questions answered

Which observable events must correspond across abstraction levels?

Every event through which the guarantee is stated, or through which anything outside the system could tell the implementation from the specification, must correspond across the abstraction levels. Refinement then means that each sequence of such events the implementation can produce is one that the specification allows. In practice the corresponding events are:

Internal steps, such as the use of temporary registers, cache contents or the number of machine instructions spent on one specification step, may be hidden only when no observer covered by the claim can see them. Matching only the final result is too weak: an implementation can return the right value after an extra, forbidden write.

Related entries: Interpreter semantic fidelity (§2.38), Translation validation (§3.33), Compiler correctness (§3.40), Model checking (§3.30), Safety property (§1.11), Liveness property (§1.12). Further reading: [229].