A queue over two array stacks over the memory oracle #
The bounded FIFO queue Queue.WordRAM.spec w C is implemented in two refinement steps, each
proved once:
refinement: the two-stack queueQueue.methodsover a pair of bounded stacks. It reuses the queue's existing methods and their behavior (Queue.stepFn_methods) and potential argument (Queue.potential_step); only the capacity and word-fit admissibility of the bounded stacks is new. Each queue operation costs at most three stack calls, amortized, with two credits per rear element.- Two array stacks allocated one after the other, as the generic product
Memory.Allocator.prodof two stack allocators, each stack call costing at most three memory units.
allocator puts the first step on top of the second (Memory.Allocator.comp), and
amortized_allocator stacks the potentials across the composition: every queue operation costs
at most nine memory units, amortized, with six credits per rear element. run allocates the two
stacks and then runs a queue client. From any memory with room for both stacks, run_correct
gives its answer and the admissibility of its memory operations, run_cost bounds its memory
cost by 6 + 9 m for m queue operations, and run_cost_isBigO exports that bound as O(m)
over clients returning Unit, with the bounding function chosen for each word size, capacity
and start memory. All three are instances of the generic theorems of Memory.Allocator, so this
file has no setup proof of its own.
The machine-level counterpart is Queue.WordRAM, whose dispatcher is hand-stepped instruction by
instruction. Here the proofs mention neither registers nor instructions. Executing the memory
programs on the word RAM is a separate, generic realization step, which this file does not
provide.
Main definitions #
boundedStacks: the pair of bounded stacks.Inv: a FIFO list within capacity holding words.refinement: the two-stack queue over the pair of bounded stacks.allocator: the queue over two array stacks, allocated in memory.run: allocate the stacks, then run a queue client.
Main results #
runClient_boundedStacks: bounded stacks run clients exactly as unbounded ones.amortized: three stack calls per queue operation, with two credits per rear element.amortized_allocator: nine memory units per queue operation, with six credits per rear element.run_correct,run_cost: the allocated queue answers like the specification and costs at most6 + 9 mmemory units formqueue operations.run_cost_glue: with glue charged per memory operation, at most(2 K + 1) (6 + 9 m).run_cost_isBigO: the same cost asO(m).
References #
Two bounded stacks of capacity C at word size w.
Moving the rear into the front is admissible for bounded stacks when both fit together and the rear holds words.
The two-stack queue over a pair of bounded stacks: the stacks represent front ++ rear⁻¹.
Each queue operation costs at most three stack calls, amortized, with two credits per rear element.
Over memory #
The queue over two array stacks of capacity C: allocate the stacks one after the other,
and run the two-stack queue on their product.
The two stacks are allocated from scratch, so the allocation needs nothing of the memory besides room.
Stacked potentials. Every queue operation costs at most nine memory units, amortized, with six credits per element of the rear stack: the queue's potential, at three memory units per stack call, read through the stacks' abstraction. Allocation leaves no potential.
Allocate two empty stacks of capacity C, then run a queue client on them.
Correctness. From any memory with room for both stacks, an admissible queue client run over two freshly allocated array stacks issues only admissible memory operations and returns the specified answer.
Cost. The same run costs at most 6 + 9 m memory units for a client of m queue
operations: six for the two allocations and nine per operation, amortized.
Cost with glue. A realization also charges the glue that runs after each memory operation.
If that glue costs at most K times the operation's charge plus K, the run costs at most
(2 K + 1) (6 + 9 m) in memory charges and glue together. The queue's and the stacks' methods
are straight-line code between memory operations, so their glue is of this form.
The exported form. For each word size w, capacity C and memory s with room for both
stacks, the memory cost of a run from s is O(m) in the number m of queue operations, over
admissible clients returning Unit. The bounding function is chosen after w, C and s;
run_cost gives the explicit bound 6 + 9 m, which is independent of them.