Documentation

CslibFrontier.DataStructure.Memory.Field

Named field arrays over the memory oracle #

A field array is a Cslib.WordRAM.Block: a base address and a length. A data structure lays out its storage as a structure whose fields are blocks (a struct of arrays) and accesses entry i of a field f with the programs f.get i and f.set i v over Memory.spec w. Addresses are thus named (par.get v) rather than computed by hand (base + n + v), while computed indices, pointer chasing (do let p ← next.get v; key.get p) and reuse of a block across operations remain ordinary programs.

The lemmas here evaluate these programs, discharge their admissibility from Block.Allocated, and describe their frame: a write to a field leaves every other address unchanged. Block.entries reads the first n entries of a field as a list, which is how an abstraction function usually sees an array. It realizes Block.HoldsList.

Main definitions #

Main results #

@[reducible, inline]

Read entry i of the field b through the memory oracle.

@[reducible, inline]

Write v to entry i of the field b through the memory oracle.

The block lies below the frontier, so all of its entries are allocated.

An entry of an allocated block is an allocated address.

A block stays allocated as the frontier grows.

theorem Cslib.WordRAM.Block.owns_add {b : Block} {i : ℕ} (hi : i < b.length) :
b.Owns (b.base + i)

A block owns the address of each of its entries.

@[simp]

Reading an entry returns the cell and leaves memory unchanged.

@[simp]
theorem Cslib.WordRAM.Block.runClient_set_bind {w : ℕ} {α : Type} (b : Block) (i v : ℕ) (k : Unit → DataStructure.Memory.Prog α) (s : DataStructure.Memory.State) :
(DataStructure.Memory.spec w).runClient s (b.set i v >>= k) = match (DataStructure.Memory.spec w).runClient { mem := Function.update s.mem (b.base + i) v, frontier := s.frontier } (k ()) with | (r, s', os) => (r, s', DataStructure.Memory.Op.write (b.base + i) v :: os)

Writing an entry updates its cell.

The list of entries #

def Cslib.WordRAM.Block.entries (b : Block) (n : ℕ) (m : ℕ → ℕ) :

The first n entries of the field b in memory m, in index order.

@[simp]
theorem Cslib.WordRAM.Block.length_entries (b : Block) (n : ℕ) (m : ℕ → ℕ) :
(b.entries n m).length = n
@[simp]
theorem Cslib.WordRAM.Block.entries_zero (b : Block) (m : ℕ → ℕ) :
b.entries 0 m = []
@[simp]
theorem Cslib.WordRAM.Block.getElem_entries (b : Block) (n : ℕ) (m : ℕ → ℕ) (i : ℕ) (hi : i < (b.entries n m).length) :
(b.entries n m)[i] = m (b.base + i)

Entry i of the list view is the cell at b.base + i.

theorem Cslib.WordRAM.Block.entries_succ (b : Block) (n : ℕ) (m : ℕ → ℕ) :
b.entries (n + 1) m = b.entries n m ++ [m (b.base + n)]

One more entry appends its cell.

theorem Cslib.WordRAM.Block.entries_congr (b : Block) (n : ℕ) {m m' : ℕ → ℕ} (h : ∀ i < n, m' (b.base + i) = m (b.base + i)) :
b.entries n m' = b.entries n m

The list view depends only on the cells of its entries.

theorem Cslib.WordRAM.Block.entries_update_of_le (b : Block) {n i : ℕ} (hi : n ≤ i) (m : ℕ → ℕ) (v : ℕ) :
b.entries n (Function.update m (b.base + i) v) = b.entries n m

Writing at or beyond entry n leaves the first n entries unchanged.

theorem Cslib.WordRAM.Block.entries_update_of_not_owns (b : Block) {n : ℕ} (hn : n ≤ b.length) {a : ℕ} (ha : ¬b.Owns a) (m : ℕ → ℕ) (v : ℕ) :
b.entries n (Function.update m a v) = b.entries n m

Writing outside the block leaves every entry unchanged.

theorem Cslib.WordRAM.Block.holdsList_entries (b : Block) (n : ℕ) (m : ℕ → ℕ) :
HoldsList b.base (b.entries n m) m

The list view of n entries realizes the list at the block's base.

Allocate a zeroed field of k cells.

Allocate a field of k cells without clearing it.

@[simp]
theorem Cslib.DataStructure.Memory.runClient_allocBlock_bind {w : ℕ} {α : Type} (k : ℕ) (next : WordRAM.Block → Prog α) (s : State) :
(spec w).runClient s (allocBlock k >>= next) = match (spec w).runClient { mem := WordRAM.clear s.mem s.frontier k, frontier := s.frontier + k } (next { base := s.frontier, length := k }) with | (v, s', os) => (v, s', Op.alloc k :: os)

A zeroed allocation hands out the block at the frontier, clears it, and advances the frontier past it.

@[simp]
theorem Cslib.DataStructure.Memory.runClient_allocBlockDirty_bind {w : ℕ} {α : Type} (k : ℕ) (next : WordRAM.Block → Prog α) (s : State) :
(spec w).runClient s (allocBlockDirty k >>= next) = match (spec w).runClient { mem := s.mem, frontier := s.frontier + k } (next { base := s.frontier, length := k }) with | (v, s', os) => (v, s', Op.allocDirty k :: os)

A dirty allocation hands out the block at the frontier without changing memory.

allocBlockDirty k returns the freshly allocated block starting at s.frontier.

theorem Cslib.DataStructure.Memory.clientState_allocBlockDirty (w : ℕ) (s : State) (k : ℕ) :
(spec w).clientState s (allocBlockDirty k) = { mem := s.mem, frontier := s.frontier + k }

allocBlockDirty k leaves memory unchanged and advances the frontier by k.

theorem Cslib.DataStructure.Memory.allocated_alloc (s : State) (k : ℕ) :
{ base := s.frontier, length := k }.Allocated { mem := WordRAM.clear s.mem s.frontier k, frontier := s.frontier + k }

A freshly allocated block is allocated in the new state.

theorem Cslib.DataStructure.Memory.entries_alloc (s : State) (k : ℕ) {n : ℕ} (hn : n ≤ k) :
{ base := s.frontier, length := k }.entries n (WordRAM.clear s.mem s.frontier k) = List.replicate n 0

A freshly allocated zeroed block holds zeros.