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 #
stack,stackPair, andqueue: total specifications with operation-dependent result types.implementation: semantic queue implementation over the stack-pair interface.
Main results #
transfer_run: stack operations reverse the rear contents into the front stack.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 #
Instances For
A total stack specification; popping an empty stack returns none.
Instances For
Two independently addressable stacks.
Instances For
Instances For
A total FIFO queue specification.
Instances For
The same queue methods work for every realization of the stack interface.
Instances For
Representation and behavior are proved once, before selecting a backend or cost model.
Instances For
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.