Documentation

CslibFrontier.DataStructure.Stack.Memory

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 #

Main results #

References #

The storage of an array stack: a one-cell field holding the number of items, and the item array, bottom first.

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 cell belongs to the footprint.

Every item cell belongs to the footprint.

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.

Evaluation #

A push reads the count, writes the item and writes the new count.

A pop from an empty stack reads the count only.

A pop from a nonempty stack reads the count and the top item and writes the new count.

The list view #

@[simp]

The stack has as many elements as the count.

theorem Cslib.DataStructure.Stack.Memory.abs_update_count {L : Layout} {w C : ℕ} (hL : L.Valid w C) (m : ℕ → ℕ) (f : ℕ) {n : ℕ} (hn : n ≤ C) :
abs L { mem := Function.update m L.count.base n, frontier := f } = (L.items.entries n m).reverse

Writing the count leaves the items in place.

theorem Cslib.DataStructure.Stack.Memory.abs_congr {L : Layout} {w C : ℕ} {s s' : Memory.State} (hL : L.Valid w C) (hs : Inv L s) (h : ∀ a ∈ L.footprint, s'.mem a = s.mem a) :
abs L s' = abs L s

The abstraction and the invariant read only the layout's cells and the frontier.

theorem Cslib.DataStructure.Stack.Memory.Inv.congr {L : Layout} {w C : ℕ} {s s' : Memory.State} (hL : L.Valid w C) (hs : Inv L s) (h : ∀ a ∈ L.footprint, s'.mem a = s.mem a) (hf : s.frontier ≤ s'.frontier) :
Inv L s'

The invariant reads only the layout's cells and survives a growing frontier.

The refinement #

theorem Cslib.DataStructure.Stack.Memory.Inv.count_lt {L : Layout} {w C : ℕ} (hL : L.Valid w C) {s : Memory.State} (hs : Inv L s) :

The count cell is an admissible address.

theorem Cslib.DataStructure.Stack.Memory.Inv.item_lt {L : Layout} {s : Memory.State} (hs : Inv L s) {i : ℕ} (hi : i < L.items.length) :

Item i below the capacity is an admissible address.

The array stack implements the bounded stack of capacity C.

The frame of a stack #

theorem Cslib.DataStructure.Stack.Memory.modifies_methods {L : Layout} {w C : ℕ} (hL : L.Valid w C) {s : Memory.State} (o : Queue.StackOp ℕ) (hv : (spec w C).valid (abs L s) o) :
(∀ a ∉ L.footprint, ((Memory.spec w).clientState s (methods L w C o)).mem a = s.mem a) ∧ ((Memory.spec w).clientState s (methods L w C o)).frontier = s.frontier

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).

theorem Cslib.DataStructure.Stack.Memory.amortized {L : Layout} {w C : ℕ} (hL : L.Valid w C) :
(framed hL).Amortized Memory.charge (fun (x : (spec w C).State) (x_1 : (spec w C).Op) => 3) fun (x : (Memory.spec w).State) => 0

Every operation costs at most three memory units, so the stack needs no potential.

@[simp]

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.

theorem Cslib.DataStructure.Stack.Memory.runClient_allocLayout_bind {w : ℕ} {α : Type} (C : ℕ) (next : Layout → Memory.Prog α) (s : Memory.State) :
(Memory.spec w).runClient s (allocLayout C >>= next) = match (Memory.spec w).runClient { mem := WordRAM.clear s.mem s.frontier 1, frontier := s.frontier + 1 + C } (next { count := { base := s.frontier, length := 1 }, items := { base := s.frontier + 1, length := C } }) with | (v, s', os) => (v, s', Memory.Op.alloc 1 :: Memory.Op.allocDirty C :: os)

Allocation places the count at the frontier and the items just above it.

theorem Cslib.DataStructure.Stack.Memory.valid_allocLayout {w C f : ℕ} (hf : WordRAM.Fits w (f + 1 + C)) :
{ count := { base := f, length := 1 }, items := { base := f + 1, length := C } }.Valid w C

The allocated layout is well formed when its end fits a word.

theorem Cslib.DataStructure.Stack.Memory.inv_allocLayout {C : ℕ} (s : Memory.State) :
Inv { count := { base := s.frontier, length := 1 }, items := { base := s.frontier + 1, length := C } } { mem := WordRAM.clear s.mem s.frontier 1, frontier := s.frontier + 1 + C } ∧ abs { count := { base := s.frontier, length := 1 }, items := { base := s.frontier + 1, length := C } } { mem := WordRAM.clear s.mem s.frontier 1, frontier := s.frontier + 1 + C } = []

The allocated stack satisfies the invariant and is empty.

theorem Cslib.DataStructure.Stack.Memory.runClient_allocLayout {w : ℕ} (C : ℕ) (s : Memory.State) :
(Memory.spec w).runClient s (allocLayout C) = ({ count := { base := s.frontier, length := 1 }, items := { base := s.frontier + 1, length := C } }, { mem := WordRAM.clear s.mem s.frontier 1, frontier := s.frontier + 1 + C }, [Memory.Op.alloc 1, Memory.Op.allocDirty C])

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.

@[simp]

The allocated stack is empty.

theorem Cslib.DataStructure.Stack.Memory.amortized_allocator {w C : ℕ} :
(allocator w C).Amortized (fun (x : (spec w C).State) (x_1 : (spec w C).Op) => 3) 0

Every stack operation costs at most three memory units, and allocation leaves no potential.