A Tour of Temporal Typing Refinement & Models · 1/5
Refinement & Models · Page 1 of 5

Refinement (Refines[A])

A ring buffer is an efficient circular-array implementation of a bounded FIFO queue. How do we prove every behaviour of RingBuffer[T] is a legal behaviour of BoundedQueue[T]?

Write a refinement mapping method annotated -> Refines[BoundedQueue[T]] returning Refines(v=e, ...) for each abstract constant and variable:

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)),
    )

The checker verifies C!Spec => A!Spec under that mapping: abstract Init, stuttering-allowed □[A!Next], abstract Fairness, and every abstract Invariant and Property (including makes_space!). And because refines_queue is real Python, unit tests can call it directly too.

Try it: Click Check to verify all 45 states of RingBuffer refine BoundedQueue. Then introduce a wrap-around bug in pop — change (self.head + 1) % self.capacity to (self.head + 2) % self.capacity — and watch FAIL refinement catch it!
Click Check (or press Shift+Enter) to run the front-end and check this specification. Switch to TLA⁺ to view its TLA⁺ meaning.
---- MODULE RingBuffer ----
EXTENDS Integers, Sequences
CONSTANT capacity
VARIABLES buf, head, size

Items == [i \in 1..size |-> buf[((head + i - 1) % capacity) + 1]]
Queue == INSTANCE BoundedQueue WITH capacity <- capacity, items <- Items

THEOREM Spec => Queue!Spec
====