Documentation

CslibFrontier.DataStructure.Stack.Doubling

A stack in a doubling array over the memory oracle #

The bounded stack Stack.spec w C is implemented by a growing array: a dynamic array used as a stack. A header of three cells at hdr holds the number of items, the capacity, and the base address of the item array. A push into a full array first allocates an array of twice the capacity, copies the items into it and switches the header to it (grow). The old array is abandoned, since memory is never reused.

So the structure allocates inside its operations. It is a Memory.Framed refinement whose footprint, the header and the current array, moves into fresh cells as it grows, and whose room, 4 C - 2 · capacity, is what its future growth may still allocate: doubling from capacity c allocates 2 c cells and lowers the room by exactly 2 c. Its invariant asks that the frontier plus the room fit a word, which is what makes each allocation admissible. In a product with other structures that allocate (Memory.Framed.prod), the shared room budget keeps this true.

Every operation costs at most 13 memory units, amortized, with the potential 2 (2 · count - capacity): a push into a full array of n items costs 9 + 2 n and releases the potential 2 n. The allocator (allocator) sets up an empty stack of capacity one in four cells, at a cost of seven units, and reserves 4 C + 4 cells in all.

The word RAM's counterpart is DynamicArray.WordRAM, whose append routine is stepped instruction by instruction and which assumes that it is the only allocator.

Main definitions #

Main results #

References #

The representation #

The number of items, in the header's first cell.

The capacity of the item array, in the header's second cell.

The base address of the item array, in the header's third cell.

The stored stack, top first: the first count items, reversed.

The header's three cells and the current item array.

The cells that growing to capacity 2 C may still allocate.

Four credits per item beyond half the capacity: 2 n on a full array of n items, which pays for copying them.

The implementation invariant.

The operations #

Double the capacity of a full array of n items: allocate 2 n cells, copy the items, and switch the header to the new array.

Grow the array if the n items fill its capacity c.

Write the item v after the n items, and count it.

Push: grow a full array, then append.

Pop: an empty stack returns none; otherwise read the top item and decrement the count.

The stack operations as memory programs.

Evaluation #

The memory after growing a full array of n items.

theorem Cslib.DataStructure.Stack.Doubling.clientState_grow (hdr : ℕ) {w : ℕ} (n : ℕ) (s : Memory.State) (h : base hdr s + n ≤ s.frontier) :
(Memory.spec w).clientState s (grow hdr n) = grown hdr n s

Growing moves the items to a fresh array of twice the size.

Growing costs 4 + 2 n memory units.

theorem Cslib.DataStructure.Stack.Doubling.validHistory_grow (hdr : ℕ) {w : ℕ} (n : ℕ) (s : Memory.State) (hh : hdr + 3 ≤ s.frontier) (h : base hdr s + n ≤ s.frontier) (hfit : WordRAM.Fits w (s.frontier + 2 * n)) (hitems : ∀ i < n, WordRAM.Fits w (s.mem (base hdr s + i))) :

Growing is admissible when the old array is allocated and holds words, and the new frontier fits a word.

theorem Cslib.DataStructure.Stack.Doubling.runClient_append (hdr : ℕ) {w : ℕ} (n v : ℕ) (s : Memory.State) :
(Memory.spec w).runClient s (append hdr n v) = ((), { mem := Function.update (Function.update s.mem (base hdr s + n) v) hdr (n + 1), frontier := s.frontier }, [Memory.Op.read (hdr + 2), Memory.Op.write (base hdr s + n) v, Memory.Op.write hdr (n + 1)])

Appending reads the base, writes the item and the new count.

theorem Cslib.DataStructure.Stack.Doubling.runClient_push_not_full (hdr : ℕ) {w : ℕ} (v : ℕ) (s : Memory.State) (h : count hdr s ≠ capacity hdr s) :
(Memory.spec w).runClient s (push hdr v) = ((), { mem := Function.update (Function.update s.mem (base hdr s + count hdr s) v) hdr (count hdr s + 1), frontier := s.frontier }, [Memory.Op.read hdr, Memory.Op.read (hdr + 1), Memory.Op.read (hdr + 2), Memory.Op.write (base hdr s + count hdr s) v, Memory.Op.write hdr (count hdr s + 1)])

A push into an array with room reads the count and capacity, then appends.

theorem Cslib.DataStructure.Stack.Doubling.push_full_eq (hdr : ℕ) {w : ℕ} (v : ℕ) (s : Memory.State) (h : count hdr s = capacity hdr s) :
((Memory.spec w).clientState s (push hdr v) = (Memory.spec w).clientState s do grow hdr (count hdr s) append hdr (count hdr s) v) ∧ (Memory.spec w).clientHistory s (push hdr v) = Memory.Op.read hdr :: Memory.Op.read (hdr + 1) :: (Memory.spec w).clientHistory s do grow hdr (count hdr s) append hdr (count hdr s) v

A push into a full array grows it, then appends.

A pop from an empty stack reads the count only.

theorem Cslib.DataStructure.Stack.Doubling.runClient_pop_of_ne_zero (hdr : ℕ) {w : ℕ} (s : Memory.State) (h : count hdr s ≠ 0) :
(Memory.spec w).runClient s (pop hdr) = (some (s.mem (base hdr s + (count hdr s - 1))), { mem := Function.update s.mem hdr (count hdr s - 1), frontier := s.frontier }, [Memory.Op.read hdr, Memory.Op.read (hdr + 2), Memory.Op.read (base hdr s + (count hdr s - 1)), Memory.Op.write hdr (count hdr s - 1)])

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

A size query reads the count.

The operations' effects #

theorem Cslib.DataStructure.Stack.Doubling.abs_eq {hdr : ℕ} (s : Memory.State) :
abs hdr s = (List.map (fun (i : ℕ) => s.mem (base hdr s + i)) (List.range (count hdr s))).reverse

The stored stack lists the first count items of the array, top first.

@[simp]

The stack has as many elements as the count.

theorem Cslib.DataStructure.Stack.Doubling.abs_congr {hdr : ℕ} {s s' : Memory.State} (hn : count hdr s' = count hdr s) (h : ∀ i < count hdr s, s'.mem (base hdr s' + i) = s.mem (base hdr s + i)) :
abs hdr s' = abs hdr s

Two states whose counts agree and whose arrays agree up to the count store the same stack.

theorem Cslib.DataStructure.Stack.Doubling.abs_push {hdr : ℕ} {s s' : Memory.State} (hn : count hdr s' = count hdr s + 1) (h : ∀ i < count hdr s, s'.mem (base hdr s' + i) = s.mem (base hdr s + i)) {v : ℕ} (hv : s'.mem (base hdr s' + count hdr s) = v) :
abs hdr s' = v :: abs hdr s

A state with one more item v on top stores v on top of the stack.

theorem Cslib.DataStructure.Stack.Doubling.push_not_full {hdr w C : ℕ} {s : Memory.State} (hs : Inv hdr w C s) (h : count hdr s ≠ capacity hdr s) (v : ℕ) :
count hdr ((Memory.spec w).clientState s (push hdr v)) = count hdr s + 1 ∧ capacity hdr ((Memory.spec w).clientState s (push hdr v)) = capacity hdr s ∧ base hdr ((Memory.spec w).clientState s (push hdr v)) = base hdr s ∧ ((Memory.spec w).clientState s (push hdr v)).frontier = s.frontier ∧ ((Memory.spec w).clientState s (push hdr v)).mem (base hdr s + count hdr s) = v ∧ ∀ (a : ℕ), a ≠ hdr → a ≠ base hdr s + count hdr s → ((Memory.spec w).clientState s (push hdr v)).mem a = s.mem a

A push into an array with room writes the item and the count, and keeps the rest.

After growing, the array starts at the old frontier.

theorem Cslib.DataStructure.Stack.Doubling.push_full {hdr w C : ℕ} {s : Memory.State} (hs : Inv hdr w C s) (h : count hdr s = capacity hdr s) (v : ℕ) :
count hdr ((Memory.spec w).clientState s (push hdr v)) = count hdr s + 1 ∧ capacity hdr ((Memory.spec w).clientState s (push hdr v)) = 2 * count hdr s ∧ base hdr ((Memory.spec w).clientState s (push hdr v)) = s.frontier ∧ ((Memory.spec w).clientState s (push hdr v)).frontier = s.frontier + 2 * count hdr s ∧ ((Memory.spec w).clientState s (push hdr v)).mem (s.frontier + count hdr s) = v ∧ (∀ i < count hdr s, ((Memory.spec w).clientState s (push hdr v)).mem (s.frontier + i) = s.mem (base hdr s + i)) ∧ ∀ a < s.frontier, a ≠ hdr → a ≠ hdr + 1 → a ≠ hdr + 2 → ((Memory.spec w).clientState s (push hdr v)).mem a = s.mem a

A push into a full array moves the items to a fresh array of twice the capacity at the frontier, writes the item and the count, and keeps every other allocated cell.

A push into a full array of n items costs 9 + 2 n memory units.

theorem Cslib.DataStructure.Stack.Doubling.validHistory_push_full {hdr w C : ℕ} {s : Memory.State} (hs : Inv hdr w C s) (h : count hdr s = capacity hdr s) {v : ℕ} (hv : WordRAM.Fits w v) (hroom : 2 * count hdr s ≤ room hdr C s) :

A push of a word into a full array is admissible when twice its capacity fits the room.

theorem Cslib.DataStructure.Stack.Doubling.pop_of_ne_zero {hdr w : ℕ} {s : Memory.State} (h : count hdr s ≠ 0) :
count hdr ((Memory.spec w).clientState s (pop hdr)) = count hdr s - 1 ∧ capacity hdr ((Memory.spec w).clientState s (pop hdr)) = capacity hdr s ∧ base hdr ((Memory.spec w).clientState s (pop hdr)) = base hdr s ∧ ((Memory.spec w).clientState s (pop hdr)).frontier = s.frontier ∧ ∀ (a : ℕ), a ≠ hdr → ((Memory.spec w).clientState s (pop hdr)).mem a = s.mem a

A pop from a nonempty stack decrements the count and keeps the rest.

The refinement #

theorem Cslib.DataStructure.Stack.Doubling.Inv.header_lt {hdr w C : ℕ} {s : Memory.State} (hs : Inv hdr w C s) :
hdr + 3 ≤ s.frontier

The header is allocated.

The frontier fits a word.

theorem Cslib.DataStructure.Stack.Doubling.abs_of_ne_zero {hdr w C : ℕ} {s : Memory.State} (hs : Inv hdr w C s) (h : count hdr s ≠ 0) :
abs hdr s = s.mem (base hdr s + (count hdr s - 1)) :: abs hdr ((Memory.spec w).clientState s (pop hdr))

The stack before a pop is the popped item on top of the stack after it.

The doubling array implements the bounded stack.

The frame: footprint and room #

theorem Cslib.DataStructure.Stack.Doubling.mem_footprint {hdr : ℕ} {s : Memory.State} {a : ℕ} :
a ∈ footprint hdr s ↔ hdr ≤ a ∧ a < hdr + 3 ∨ base hdr s ≤ a ∧ a < base hdr s + capacity hdr s

The footprint is the header and the item array.

theorem Cslib.DataStructure.Stack.Doubling.congr {hdr w C : ℕ} {s s' : Memory.State} (hs : Inv hdr w C s) (h : ∀ a ∈ footprint hdr s, s'.mem a = s.mem a) (hf : s.frontier ≤ s'.frontier) (hroom : WordRAM.Fits w (s'.frontier + room hdr C s)) :
Inv hdr w C s' ∧ abs hdr s' = abs hdr s ∧ footprint hdr s' = footprint hdr s ∧ room hdr C s' = room hdr C s

The invariant, the abstraction, the footprint and the room read only the footprint, and survive a growing frontier that leaves the room fitting a word.

theorem Cslib.DataStructure.Stack.Doubling.modifies_methods {hdr w C : ℕ} {s : Memory.State} (hs : Inv hdr w C s) (o : Queue.StackOp ℕ) (hv : (spec w C).valid (abs hdr s) o) :
(∀ b < s.frontier, b ∉ footprint hdr s → ((Memory.spec w).clientState s (methods hdr w C o)).mem b = s.mem b) ∧ (∀ b ∈ footprint hdr ((Memory.spec w).clientState s (methods hdr w C o)), b ∈ footprint hdr s ∨ s.frontier ≤ b) ∧ ((Memory.spec w).clientState s (methods hdr w C o)).frontier + room hdr C ((Memory.spec w).clientState s (methods hdr w C o)) ≤ s.frontier + room hdr C s

Each admissible operation changes no allocated cell outside the footprint, grows the footprint only into fresh cells, and advances the frontier by at most the room it gives up.

The doubling stack, framed by its header and current array, with the room its growth may still allocate.

The potential reads only the header.

theorem Cslib.DataStructure.Stack.Doubling.amortized {hdr w C : ℕ} :
(framed hdr w C).Amortized Memory.charge (fun (x : (spec w C).State) (x_1 : (spec w C).Op) => 13) (potential hdr)

Every operation costs at most thirteen memory units, amortized: a push into a full array of n items costs 9 + 2 n and releases the potential 2 n that the last n / 2 pushes saved.

Allocation #

Allocate an empty doubling stack: a zeroed header of three cells, then an item array of one cell, recorded in the header. Returns the header's address.

Creation allocates the header at the frontier and the array of one cell above it.

The header of a created stack: no items, capacity one, and the array just above.

The doubling stack as an allocator: an empty stack in four cells at a memory cost of seven units, reserving 4 C + 4 cells for itself and its growth to capacity 2 C.

@[simp]

The allocated stack is empty.

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

The allocator's cost certificate: thirteen memory units per operation, amortized, and no potential once allocated.