An array stack over the memory oracle #
The bounded stack Stack.spec w C is implemented by three short programs over
Memory.spec w. The storage is a Layout with two named fields: a one-cell count and an
items array of C cells, bottom first. The abstraction function reads the first count items
and reverses them, so the top comes first. Each operation is at most three memory operations and
one branch. Its evaluation is definitional (runClient_push, runClient_pop_of_pos, …), and the
refinement obligations follow from those equations and the list view of the array.
Every operation costs at most three memory units, with no potential (amortized). The methods
write only the layout's cells and never allocate (modifies_methods), and the abstraction and
invariant depend only on those cells (abs_congr, Inv.congr). So the stack is framed by its
layout (framed), and stacks with disjoint layouts share one memory through the generic product
Memory.Framed.prod. allocator packages the allocation of an empty stack (allocLayout) with
the framed stack it sets up, so that clients allocate stacks through Memory.Allocator.
No register or instruction count appears here. Word fit enters only through the admissibility
of Memory.spec w and one static fact of the layout, that the capacity fits. Stack.Native runs
these programs on the word RAM.
Main definitions #
Layout,Layout.footprint,Layout.Valid: the two fields, the cells they own, and their static well-formedness.push,pop,size,methods: the operations as memory programs.abs,Inv: the abstraction function and the implementation invariant.refinement: the array stack as aRefinementof the bounded stack.framed: the same refinement, framed by the layout's cells.allocLayout: allocate an empty stack.allocator: allocation and the framed stack, as aMemory.Allocator.
Main results #
amortized: every operation costs at most three memory units.runClient_push,runClient_pop_of_eq_zero,runClient_pop_of_pos,runClient_size: the operations evaluate definitionally.modifies_methods,abs_congr,Inv.congr,Inv.lt_frontier: the frame of a stack.runClient_allocLayout_bind,runClient_allocLayout,valid_allocLayout,inv_allocLayout: allocation yields a well-formed empty stack in three memory units.amortized_allocator: the allocator's cost certificate, three units per operation.
References #
The storage of an array stack: a one-cell field holding the number of items, and the item array, bottom first.
- count : WordRAM.Block
One cell holding the number of stored items.
- items : WordRAM.Block
The items, bottom first.
The cells a stack owns.
Static well-formedness for capacity C at word size w: the count field is one cell outside
the item array, the array has C cells, and C fits a word.
The count field is one cell.
The count cell is not an item.
The item array has the capacity.
- capacity_fits : WordRAM.Fits w C
The capacity fits a word, so every count does.
Push: write the item above the top, then increment the count.
Pop: an empty stack returns none; otherwise read the top item and decrement the count.
The stack operations as memory programs.
The stored stack, top first: the first count items, reversed.
The implementation invariant: both fields are allocated and the count is within capacity.
The count cell is allocated.
The item array is allocated.
The count is at most the capacity.
Evaluation #
A push reads the count, writes the item and writes the new count.
A pop from a nonempty stack reads the count and the top item and writes the new count.
The list view #
The stack has as many elements as the count.
The refinement #
The array stack implements the bounded stack of capacity C.
The frame of a stack #
Each admissible operation writes only the layout's cells and never allocates.
The layout's cells are allocated.
The array stack, with its layout's cells as a constant footprint. Stacks with disjoint
layouts therefore share one memory as a product of stacks (Memory.Framed.prod).
Every operation costs at most three memory units, so the stack needs no potential.
The footprint of a stack is its layout's cells.
Allocation #
Allocate an empty stack of capacity C: a zeroed count cell and an item array that is not
cleared.
Allocation places the count at the frontier and the items just above it.
The allocated stack satisfies the invariant and is empty.
Allocation returns the layout at the frontier, clears the count cell, and advances the frontier past the items.
The array stack as an allocator: allocate an empty stack of capacity C in C + 1
cells, at a memory cost of three units.