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:

examples/bounded_queue.py
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:

  1. Init: Every concrete initial state maps to a valid abstract initial state of A.
  2. Step: Every concrete step maps either to a valid step of A!Next or to a stuttering step that leaves A!vars unchanged (□[A!Next]A!vars).
  3. Fairness: Concrete fairness implies every Fairness clause of A.
  4. Claims: Every Invariant, ActionInvariant, and Property of A holds 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.