A modular queue implemented with two stacks #
The behavior proof mentions no concrete stack representation or resource model. The front stack stores the next elements to dequeue; the reversed rear stack stores newly enqueued elements. When the front is empty, the implementation queries the rear size and transfers those elements. This standard construction follows Cormen et al., Exercise 10.1-6; the potential method is described in Chapter 17.
Main definitions #
StackOp,stack,stackPair,QueueOp, andqueue: total specifications with operation-dependent result types.transfer,transferTrace,methods, andimplementation: semantic queue implementation over the stack-pair interface.
Main results #
transfer_run: stack operations reverse the rear contents into the front stack.runClient_dequeue_nil,stepFn_methods: the methods realize the FIFO step on the represented list.potential_step: at most three amortized stack calls, including the size query.realized_client: every adaptive client has a correct backend execution with its bound.realized_client_bound: every supplied linked execution satisfies correctness and the bound.
For a primitive weight bound K, a client performing m queue operations satisfies
weight + 2 K rearFinal.length ≤ 3 K m + 2 K rearInitial.length. This is a conditional bound on
backend traces, not a machine instruction-count theorem. Pure client computation is uncharged;
backend implementations must justify their primitive weights and state independence separately.
References #
A total stack specification; popping an empty stack returns none.
Two independently addressable stacks.
A total FIFO queue specification.
A dequeue from an empty front stack asks for the rear size, transfers the rear, and pops the front again.
Each method returns the FIFO answer, and the stacks then represent the FIFO successor of the represented list.
Each transferred element incurs one pop and one push.
The queue has a three-call amortized bound; rear-stack credit remains private to the module. This counts stack calls, not execution of pure continuations or machine instructions.
Uniform primitive bounds can be chosen after proving queue correctness. The same theorem applies to any additive measure on any backend's native traces. It also retains leftover credit.
Correctness and the same resource bound hold for every supplied flattened execution.