Documentation

CslibFrontier.DataStructure.Queue.Memory

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:

  1. refinement: the two-stack queue Queue.methods over 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.
  2. Two array stacks allocated one after the other, as the generic product Memory.Allocator.prod of 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 #

Main results #

References #

@[reducible, inline]

Two bounded stacks of capacity C at word size w.

A FIFO list within capacity C whose entries fit a w-bit word.

theorem Cslib.DataStructure.Queue.Memory.Inv.step {w C : ℕ} {q : List ℕ} (h : Inv w C q) {o : QueueOp ℕ} (hv : (WordRAM.spec w C).valid q o) :
Inv w C ((queue ℕ).stepFn q o).1

Admissible FIFO steps keep the list within capacity and made of words.

The bounded stacks step exactly as the unbounded ones; only admissibility differs.

The final state of a client of the bounded stacks.

The result of a client of the bounded stacks.

The history of a client of the bounded stacks.

theorem Cslib.DataStructure.Queue.Memory.validHistory_transferTrace {w C : ℕ} {front back : List ℕ} (hlen : front.length + back.length ≤ C) (hfit : ∀ x ∈ back, WordRAM.Fits w x) :

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⁻¹.

theorem Cslib.DataStructure.Queue.Memory.amortized {w C : ℕ} :
(refinement w C).Amortized (fun (x : (boundedStacks w C).State) (x_1 : (boundedStacks w C).Op) => 1) (fun (x : (WordRAM.spec w C).State) (x_1 : (WordRAM.spec w C).Op) => 3) fun (s : (boundedStacks w C).State) => 2 * List.length s.2

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.

@[simp]

The allocated queue is empty.

@[simp]

The two stacks take 2 (C + 1) cells.

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.

theorem Cslib.DataStructure.Queue.Memory.run_cost_glue {w C : ℕ} {β : Type} (p : (WordRAM.spec w C).Client β) {s : Memory.State} (hfit : WordRAM.Fits w (s.frontier + 2 * (C + 1))) (hv : (WordRAM.spec w C).ValidHistory [] ((WordRAM.spec w C).clientHistory [] p)) {glue : Memory.State → Memory.Op → ℕ} {K : ℕ} (hglue : ∀ (s : Memory.State) (o : Memory.Op), glue s o ≤ K * (Memory.charge s o + 1)) :
(Memory.spec w).totalCost (fun (s : (Memory.spec w).State) (o : (Memory.spec w).Op) => Memory.charge s o + glue s o) s ((Memory.spec w).clientHistory s (run w C p)) ≤ (2 * K + 1) * (6 + 9 * ((WordRAM.spec w C).clientHistory [] p).length)

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.