Refinement (Refines[A])
Often you want to specify a system cleanly at a high level (e.g., a BoundedQueue[T] represented as an immutable tuple[T, ...]) and then implement it efficiently with a different internal representation (e.g., a circular-array RingBuffer[T] with buf, head, and size).
Writing a Refinement Mapping#
Add a method annotated -> Refines[AbstractSpec] that returns Refines(v=expr, ...), mapping each constant and variable of the abstract specification onto an expression over the concrete class's state:
class RingBuffer(Generic[T]):
capacity: Constant[Positive]
buf: State[tuple[T | None, ...]]
head: State[int]
size: State[int]
...
def refines_queue(self) -> Refines[BoundedQueue[T]]:
return Refines(
capacity=self.capacity,
items=tuple(self.buf[(self.head + i) % self.capacity] for i in range(self.size)),
)What Refinement Verifies#
Under the mapping, Refines[A] claims C!Spec ⇒ Ā!Spec, checking all four obligations:
- Init: Every concrete initial state maps to a valid abstract initial state of
A. - Step: Every concrete step maps either to a valid step of
A!Nextor to a stuttering step that leavesA!varsunchanged (□[A!Next]A!vars). - Fairness: Concrete fairness implies every
Fairnessclause ofA. - Claims: Every
Invariant,ActionInvariant, andPropertyofAholds on every concrete behaviour.
Omitting any variable or constant of A in the returned Refines(...) is rejected statically as missing-mapping.
Real Python at Runtime#
Because refines_queue() is ordinary Python returning a Refines value (whose .mapping holds the mapped fields), unit tests and property tests can call it directly at runtime to compare a concrete instance against an abstract reference model.