Documentation

CslibFrontier.DataStructure.Memory.Record

Records of named cells over the memory oracle #

A data structure with a fixed shape, such as a struct of arrays, stores its words in named cells: key v, par v, a counter. Record.spec ok w is the interface to such a record. Its state gives each cell of a type ι a value; an operation reads or writes one cell. The predicate ok says which cells exist (for instance v < n for an array of length n), and admissibility asks that an accessed cell exist and that a written value fit a w-bit word.

A data structure is then a program over its record (Record.Prog), and its proofs see named cells rather than addresses. Record.wp is the weakest precondition of such a program under a postcondition on its answer, its final record and its number of operations. It evaluates by simp one operation at a time (wp_read_bind, wp_write_bind), composes (wp_bind), and determines admissibility, answer, final state and cost of the run (wp_iff).

A Record.Layout places the cells in memory: an address for each cell, injective on the cells that exist. Record.refinement implements the record over Memory.spec w by one memory operation per record operation (Record.amortized). Record.framed packages it as a framed structure over memory, whose footprint holds the cells' addresses, and Record.allocator allocates a record in zeroed cells at the frontier. A structure written over a record then joins products of structures over one memory through Memory.Framed.comp and Memory.Allocator.comp. Address arithmetic, allocation and word-fit of addresses are therefore proved once here, for every record.

Main definitions #

Main results #

Operations on a record of cells indexed by ι.

  • read {ι : Type} (i : ι) : Op ι

    Read a cell.

  • write {ι : Type} (i : ι) (value : ℕ) : Op ι

    Write a value to a cell.

@[reducible, inline]

A read returns the cell's value.

def Cslib.DataStructure.Record.step {ι : Type} [DecidableEq ι] (g : ι → ℕ) (o : Op ι) :
(ι → ℕ) × o.Result

The transition of each record operation.

def Cslib.DataStructure.Record.valid {ι : Type} (ok : ι → Prop) (w : ℕ) :
(ι → ℕ) → Op ι → Prop

Admissibility: the accessed cell exists, and a written value fits a w-bit word.

@[reducible, inline]

The record interface with cells ok at word size w.

@[reducible, inline]

Programs over a record with cells indexed by ι.

@[reducible, inline]
abbrev Cslib.DataStructure.Record.read {ι : Type} (i : ι) :

Read a cell.

@[reducible, inline]
abbrev Cslib.DataStructure.Record.write {ι : Type} (i : ι) (v : ℕ) :

Write a value to a cell.

Weakest preconditions #

def Cslib.DataStructure.Record.wp {ι : Type} [DecidableEq ι] {α : Type} (ok : ι → Prop) (w : ℕ) :
Prog ι α → (α → (ι → ℕ) → ℕ → Prop) → (ι → ℕ) → Prop

The weakest precondition of p under Q: every operation of the run from g is admissible, and Q holds of the answer, the final record and the number of operations.

@[simp]
theorem Cslib.DataStructure.Record.wp_pure {ι : Type} [DecidableEq ι] {ok : ι → Prop} {w : ℕ} {α : Type} (a : α) (Q : α → (ι → ℕ) → ℕ → Prop) (g : ι → ℕ) :
wp ok w (pure a) Q g ↔ Q a g 0
@[simp]
theorem Cslib.DataStructure.Record.wp_read_bind {ι : Type} [DecidableEq ι] {ok : ι → Prop} {w : ℕ} {α : Type} (i : ι) (k : ℕ → Prog ι α) (Q : α → (ι → ℕ) → ℕ → Prop) (g : ι → ℕ) :
wp ok w (read i >>= k) Q g ↔ ok i ∧ wp ok w (k (g i)) (fun (a : α) (g' : ι → ℕ) (t : ℕ) => Q a g' (t + 1)) g
@[simp]
theorem Cslib.DataStructure.Record.wp_write_bind {ι : Type} [DecidableEq ι] {ok : ι → Prop} {w : ℕ} {α : Type} (i : ι) (v : ℕ) (k : Unit → Prog ι α) (Q : α → (ι → ℕ) → ℕ → Prop) (g : ι → ℕ) :
wp ok w (write i v >>= k) Q g ↔ (ok i ∧ WordRAM.Fits w v) ∧ wp ok w (k ()) (fun (a : α) (g' : ι → ℕ) (t : ℕ) => Q a g' (t + 1)) (Function.update g i v)
@[simp]
theorem Cslib.DataStructure.Record.wp_read {ι : Type} [DecidableEq ι] {ok : ι → Prop} {w : ℕ} (i : ι) (Q : ℕ → (ι → ℕ) → ℕ → Prop) (g : ι → ℕ) :
wp ok w (read i) Q g ↔ ok i ∧ Q (g i) g 1
@[simp]
theorem Cslib.DataStructure.Record.wp_write {ι : Type} [DecidableEq ι] {ok : ι → Prop} {w : ℕ} (i : ι) (v : ℕ) (Q : Unit → (ι → ℕ) → ℕ → Prop) (g : ι → ℕ) :
wp ok w (write i v) Q g ↔ (ok i ∧ WordRAM.Fits w v) ∧ Q () (Function.update g i v) 1
theorem Cslib.DataStructure.Record.wp_mono {ι : Type} [DecidableEq ι] {ok : ι → Prop} {w : ℕ} {α : Type} {p : Prog ι α} {Q Q' : α → (ι → ℕ) → ℕ → Prop} (h : ∀ (a : α) (g : ι → ℕ) (t : ℕ), Q a g t → Q' a g t) {g : ι → ℕ} (hp : wp ok w p Q g) :
wp ok w p Q' g

Consequence: a weaker postcondition.

theorem Cslib.DataStructure.Record.wp_bind {ι : Type} [DecidableEq ι] {ok : ι → Prop} {w : ℕ} {α β : Type} (p : Prog ι α) (k : α → Prog ι β) (Q : β → (ι → ℕ) → ℕ → Prop) (g : ι → ℕ) :
wp ok w (p >>= k) Q g ↔ wp ok w p (fun (a : α) (g' : ι → ℕ) (t : ℕ) => wp ok w (k a) (fun (b : β) (g'' : ι → ℕ) (t' : ℕ) => Q b g'' (t + t')) g') g

Sequencing: the cost of p >>= k is the cost of p plus the cost of k.

theorem Cslib.DataStructure.Record.wp_iff {ι : Type} [DecidableEq ι] {ok : ι → Prop} {w : ℕ} {α : Type} (p : Prog ι α) (Q : α → (ι → ℕ) → ℕ → Prop) (g : ι → ℕ) :
wp ok w p Q g ↔ (spec ok w).ValidHistory g ((spec ok w).clientHistory g p) ∧ Q ((spec ok w).clientResult g p) ((spec ok w).clientState g p) ((spec ok w).clientHistory g p).length

The weakest precondition holds exactly when the run is admissible and Q holds of its answer, final record and number of operations.

Placement in memory #

structure Cslib.DataStructure.Record.Layout {ι : Type} (ok : ι → Prop) :

A placement of the record's cells in memory: an address for each cell, injective on the cells that exist.

  • addr : ι → ℕ

    The address of each cell.

  • addr_injOn (i j : ι) : ok i → ok j → self.addr i = self.addr j → i = j

    Distinct existing cells have distinct addresses.

def Cslib.DataStructure.Record.Layout.methods {ι : Type} [DecidableEq ι] {ok : ι → Prop} (L : Layout ok) (w : ℕ) :

The record operations as memory operations at the cells' addresses.

def Cslib.DataStructure.Record.Layout.abs {ι : Type} {ok : ι → Prop} (L : Layout ok) [DecidablePred ok] (m : Memory.State) (i : ι) :

The record stored in memory; cells that do not exist read as 0.

def Cslib.DataStructure.Record.Layout.Inv {ι : Type} {ok : ι → Prop} (L : Layout ok) (m : Memory.State) :

Every existing cell is allocated.

theorem Cslib.DataStructure.Record.Layout.abs_write {ι : Type} [DecidableEq ι] {ok : ι → Prop} (L : Layout ok) [DecidablePred ok] {m : Memory.State} {i : ι} (hi : ok i) (v : ℕ) :
L.abs { mem := Function.update m.mem (L.addr i) v, frontier := m.frontier } = Function.update (L.abs m) i v

Writing an existing cell writes its address.

def Cslib.DataStructure.Record.refinement {ι : Type} [DecidableEq ι] {ok : ι → Prop} [DecidablePred ok] (L : Layout ok) (w : ℕ) :

The record over the memory oracle: each operation is the memory operation at the cell's address.

theorem Cslib.DataStructure.Record.amortized {ι : Type} [DecidableEq ι] {ok : ι → Prop} [DecidablePred ok] (L : Layout ok) (w : ℕ) :
(refinement L w).Amortized Memory.charge (fun (x : (spec ok w).State) (x_1 : (spec ok w).Op) => 1) fun (x : (Memory.spec w).State) => 0

Each record operation costs one memory unit; no potential is needed.

Records as framed structures #

def Cslib.DataStructure.Record.framed {ι : Type} [DecidableEq ι] {ok : ι → Prop} [DecidablePred ok] (L : Layout ok) (w : ℕ) (fp : Finset ℕ) (hfp : ∀ (i : ι), ok i → L.addr i ∈ fp) (hlt : ∀ {s : Memory.State} {a : ℕ}, L.Inv s → a ∈ fp → a < s.frontier) :

A record in memory as a framed structure. Its footprint fp holds the addresses of its cells, and is allocated once they are. Each record operation reads or writes the address of one cell and keeps the frontier.

def Cslib.DataStructure.Record.allocator {ι : Type} [DecidableEq ι] {ok : ι → Prop} [DecidablePred ok] (L : ℕ → Layout ok) (size w : ℕ) (hL : ∀ (base : ℕ) (i : ι), ok i → base ≤ (L base).addr i ∧ (L base).addr i < base + size) (hlast : ∀ (base : ℕ), ∃ (i : ι), ok i ∧ (L base).addr i + 1 = base + size) :

Allocating a record: size zeroed cells at the frontier, laid out by L from their base, where the last of them is a cell. It costs size + 1 memory units, and the record holds 0 in every cell.

theorem Cslib.DataStructure.Record.amortized_allocator {ι : Type} [DecidableEq ι] {ok : ι → Prop} [DecidablePred ok] {L : ℕ → Layout ok} {size w : ℕ} {hL : ∀ (base : ℕ) (i : ι), ok i → base ≤ (L base).addr i ∧ (L base).addr i < base + size} {hlast : ∀ (base : ℕ), ∃ (i : ι), ok i ∧ (L base).addr i + 1 = base + size} :
(allocator L size w hL hlast).Amortized (fun (x : (spec ok w).State) (x_1 : (spec ok w).Op) => 1) 0

Every operation on an allocated record costs one memory unit, with no potential.