Documentation

CslibFrontier.DataStructure.Dictionary.Memory

A direct-address table over the memory oracle #

The dictionary Dictionary.spec U V n_max on the keys Fin U is implemented as a direct-address table: a field T of U cells, one per key, holding 0 for an absent key and v + 1 for the value v (decode). A lookup reads the key's cell and an insertion writes it, one memory unit each (amortized). The table has a cell for every key, so the capacity n_max never binds. Values below V are stored, so V must fit a word.

The table is framed by its field (framed), so it shares memory with other structures. Allocating its U cells without clearing (Memory.allocDirty U) costs one memory unit (Memory.charge_allocDirty); in memory that is zero above the frontier, the cells are then an empty table (inv_alloc).

Main definitions #

Main results #

References #

The value stored in a cell: 0 is an absent key, and v + 1 the value v. This is the word encoding of Option ℕ that the word-RAM dictionaries use.

A nonzero cell decodes to its predecessor.

Look up a key: read its cell.

Store the value v for a key: write v + 1 to its cell.

The dictionary operations as memory programs.

@[reducible, inline]

The operations as methods of Dictionary.spec U V n_max over Memory.spec w.

The represented dictionary: the decoded cell of each key.

@[reducible, inline]

The implementation invariant: the field is allocated.

Static well-formedness for U keys and values below V at word size w: one cell per key, and V fits a word, so every stored v + 1 does.

  • length_eq : T.length = U

    One cell per key.

  • value_fits : WordRAM.Fits w V

    The value bound fits a word.

theorem Cslib.DataStructure.Dictionary.Memory.cell_lt {U : ℕ} {T : WordRAM.Block} {w V : ℕ} (hT : Valid T w U V) {s : Memory.State} (hs : Inv T s) (u : Fin U) :
T.base + ↑u < s.frontier

The cell of a key is allocated.

theorem Cslib.DataStructure.Dictionary.Memory.mem_footprint {U : ℕ} {T : WordRAM.Block} {w V : ℕ} (hT : Valid T w U V) (u : Fin U) :
T.base + ↑u ∈ T.footprint

The cell of a key belongs to the field.

An owned cell of an allocated field is allocated.

What a run of the operation o from s establishes: the specified step, the invariant, and no change to the frontier or to any cell outside the field.

theorem Cslib.DataStructure.Dictionary.Memory.ops_triple {U : ℕ} {T : WordRAM.Block} {w V n_max : ℕ} (hT : Valid T w U V) {s : Memory.State} (hs : Inv T s) (o : Op U) (hv : (spec U V n_max).valid (abs T s) o) :
Memory.Triple w s (ops T o) 1 (Post T s o)

Each operation realizes the specified step with one memory operation.

theorem Cslib.DataStructure.Dictionary.Memory.ops_run {U : ℕ} {T : WordRAM.Block} {w V n_max : ℕ} (hT : Valid T w U V) {s : Memory.State} (hs : Inv T s) (o : Op U) (hv : (spec U V n_max).valid (abs T s) o) :

The run of an operation, by ops_triple.

def Cslib.DataStructure.Dictionary.Memory.refinement {U : ℕ} {T : WordRAM.Block} {w V n_max : ℕ} (hT : Valid T w U V) :
Refinement (spec U V n_max) (Memory.spec w)

The direct-address table implements the dictionary.

theorem Cslib.DataStructure.Dictionary.Memory.abs_congr {U : ℕ} {T : WordRAM.Block} {w V : ℕ} (hT : Valid T w U V) {s s' : Memory.State} (h : ∀ a ∈ T.footprint, s'.mem a = s.mem a) :
abs T s' = abs T s

The abstraction reads only the field.

def Cslib.DataStructure.Dictionary.Memory.framed {U : ℕ} {T : WordRAM.Block} {w V n_max : ℕ} (hT : Valid T w U V) :
Memory.Framed (spec U V n_max) w

The table, framed by its field.

@[simp]

The footprint of a table is its field.

theorem Cslib.DataStructure.Dictionary.Memory.amortized {U : ℕ} {T : WordRAM.Block} {w V n_max : ℕ} (hT : Valid T w U V) :
(framed hT).Amortized Memory.charge (fun (x : (spec U V n_max).State) (x_1 : (spec U V n_max).Op) => 1) fun (x : (Memory.spec w).State) => 0

Every operation costs one memory unit.

theorem Cslib.DataStructure.Dictionary.Memory.inv_alloc {U : ℕ} {s : Memory.State} (hz : Memory.ZeroAbove s) :
Inv { base := s.frontier, length := U } { mem := s.mem, frontier := s.frontier + U } ∧ abs { base := s.frontier, length := U } { mem := s.mem, frontier := s.frontier + U } = empty U

A field allocated without clearing in memory that is zero above the frontier is an empty table.