Documentation

CslibFrontier.Computability.WordRAM.WordStack

Register-top k-word stacks on the word RAM #

Stack.Memory and MemoryStack store one-word items alongside a count cell in memory. Iterative depth-first and worklist controllers on the word RAM instead keep the live record count in a register top and store k-word records directly at [base, base + cap * k) with no header cell, starting from possibly dirty memory.

This module provides:

Main definitions #

Main results #

Top-first k-word stack invariant #

def Cslib.WordRAM.WordStack.Holds {α : Type u_1} {k : ℕ} (R : RecordSpec α k) (base : ℕ) :
List α → (ℕ → ℕ) → Prop

Top-first LIFO stack of k-word records at base: items = v :: rest places v at base + rest.length * k above rest in [base, base + rest.length * k). Cells at or above base + items.length * k are unconstrained.

@[simp]
theorem Cslib.WordRAM.WordStack.holds_nil {α : Type u_1} {k : ℕ} (R : RecordSpec α k) (base : ℕ) (mem : ℕ → ℕ) :
Holds R base [] mem
@[simp]
theorem Cslib.WordRAM.WordStack.holds_cons {α : Type u_1} {k : ℕ} (R : RecordSpec α k) (base : ℕ) (v : α) (rest : List α) (mem : ℕ → ℕ) :
Holds R base (v :: rest) mem ↔ R.HoldsAt (base + rest.length * k) v mem ∧ Holds R base rest mem
theorem Cslib.WordRAM.WordStack.Holds.keep {α : Type u_1} {k : ℕ} {R : RecordSpec α k} {base : ℕ} {items : List α} {before after : ℕ → ℕ} {reads : Set ℕ} (h : Holds R base items before) (hf : Set.EqOn after before reads) (hspan : Set.Ico base (base + items.length * k) ⊆ reads) :
Holds R base items after

An equality frame covering [base, base + items.length * k) preserves Holds.

theorem Cslib.WordRAM.WordStack.Holds.frame {α : Type u_1} {k : ℕ} {R : RecordSpec α k} {base : ℕ} {items : List α} {mem mem' : ℕ → ℕ} (h : Holds R base items mem) (heq : ∀ (a : ℕ), base ≤ a → a < base + items.length * k → mem' a = mem a) :
Holds R base items mem'

Agreement on [base, base + items.length * k) transports Holds.

theorem Cslib.WordRAM.WordStack.Holds.push_write {α : Type u_1} {k : ℕ} {R : RecordSpec α k} {base : ℕ} {items : List α} {mem : ℕ → ℕ} (h : Holds R base items mem) (v : α) :
Holds R base (v :: items) (R.write (base + items.length * k) v mem)

Writing v immediately above a represented stack items represents v :: items.

theorem Cslib.WordRAM.WordStack.Holds.unique {α : Type u_1} {k : ℕ} {R : RecordSpec α k} (hinj : Function.Injective R.toWords) {base : ℕ} {items₁ items₂ : List α} {mem : ℕ → ℕ} (h₁ : Holds R base items₁ mem) (h₂ : Holds R base items₂ mem) (hlen : items₁.length = items₂.length) :
items₁ = items₂

At a fixed stack height, injective record encoding uniquely determines the logical stack list from memory.

theorem Cslib.WordRAM.WordStack.holds_iff_getElem_reverse {α : Type u_1} {k : ℕ} (R : RecordSpec α k) (base : ℕ) (items : List α) (mem : ℕ → ℕ) :
Holds R base items mem ↔ ∀ (idx : Fin items.length), R.HoldsAt (base + ↑idx * k) items.reverse[↑idx] mem

Holds R base items mem is equivalent to each slot idx < items.length holding the idx-th element from the bottom (items.reverse[idx]).

Inline register-top push and pop routines #

def Cslib.WordRAM.WordStack.push {r k : ℕ} [NeZero k] (base top cursor width one : Fin r) (fields : Fin k → Fin r) :

Push one k-word record from fields onto the stack at base + top * width and increment top (2 * k + 2 instructions).

def Cslib.WordRAM.WordStack.pop {r k : ℕ} [NeZero k] (base top cursor width one : Fin r) (fields : Fin k → Fin r) :

Decrement top and load the k-word record at base + top * width into fields (2 * k + 2 instructions).

def Cslib.WordRAM.WordStack.PushLocal {r : ℕ} (top cursor i : Fin r) :

Registers written by WordStack.push: cursor and top.

Instances For
    @[instance_reducible]
    instance Cslib.WordRAM.WordStack.instDecidablePushLocal {r : ℕ} (top cursor i : Fin r) :
    Decidable (PushLocal top cursor i)
    def Cslib.WordRAM.WordStack.PopLocal {r k : ℕ} (top cursor : Fin r) (fields : Fin k → Fin r) (i : Fin r) :

    Registers written by WordStack.pop: top, cursor, and the loaded fields.

    Instances For
      @[instance_reducible]
      instance Cslib.WordRAM.WordStack.instDecidablePopLocal {r k : ℕ} (top cursor : Fin r) (fields : Fin k → Fin r) (i : Fin r) :
      Decidable (PopLocal top cursor fields i)
      theorem Cslib.WordRAM.WordStack.push_preserves {r k : ℕ} [NeZero k] (base top cursor width one : Fin r) (fields : Fin k → Fin r) {i : Fin r} (hi : ¬PushLocal top cursor i) :
      Code.Preserves i (push base top cursor width one fields)

      WordStack.push preserves every register outside PushLocal top cursor.

      theorem Cslib.WordRAM.WordStack.pop_preserves {r k : ℕ} [NeZero k] (base top cursor width one : Fin r) (fields : Fin k → Fin r) {i : Fin r} (hi : ¬PopLocal top cursor fields i) :
      Code.Preserves i (pop base top cursor width one fields)

      WordStack.pop preserves every register outside PopLocal top cursor fields.

      theorem Cslib.WordRAM.WordStack.push_not_readsWidth {r k : ℕ} [NeZero k] (base top cursor width one : Fin r) (fields : Fin k → Fin r) :
      ¬(push base top cursor width one fields).ReadsWidth

      WordStack.push never inspects the physical word width.

      theorem Cslib.WordRAM.WordStack.pop_not_readsWidth {r k : ℕ} [NeZero k] (base top cursor width one : Fin r) (fields : Fin k → Fin r) :
      ¬(pop base top cursor width one fields).ReadsWidth

      WordStack.pop never inspects the physical word width.

      theorem Cslib.WordRAM.WordStack.pushWords_exec {r k w F n : ℕ} [NeZero k] (base top cursor width one : Fin r) (fields : Fin k → Fin r) (x : Data r) (hbase_cur : base ≠ cursor) (htop_cur : top ≠ cursor) (hone_cur : one ≠ cursor) (hfields_cur : ∀ (i : Fin k), fields i ≠ cursor) (hbase : x.regs base = F) (htop : x.regs top = n) (hwidth : x.regs width = k) (hone : x.regs one = 1) (hfit : Fits w (F + (n + 1) * k)) :
      ∃ (y : Data r), Exec w (push base top cursor width one fields) x y (2 * k + 2) ∧ y.regs top = n + 1 ∧ (y.mem = writeWords x.mem (F + n * k) fun (i : Fin k) => x.regs (fields i)) ∧ Modifies { base := F + n * k, length := k }.footprint x.mem y.mem ∧ ∀ (i : Fin r), ¬PushLocal top cursor i → y.regs i = x.regs i

      Single-slot execution of WordStack.push in exact 2 * k + 2 instructions.

      theorem Cslib.WordRAM.WordStack.push_exec {r : ℕ} {α : Type u_1} {k w baseAddr cap : ℕ} [NeZero k] (R : RecordSpec α k) (base top cursor width one : Fin r) (fields : Fin k → Fin r) (items : List α) (v : α) (x : Data r) (hbase_cur : base ≠ cursor) (htop_cur : top ≠ cursor) (hone_cur : one ≠ cursor) (hfields_cur : ∀ (i : Fin k), fields i ≠ cursor) (hbase : x.regs base = baseAddr) (htop : x.regs top = items.length) (hwidth : x.regs width = k) (hone : x.regs one = 1) (hfields : ∀ (i : Fin k), x.regs (fields i) = R.toWords v i) (hstack : Holds R baseAddr items x.mem) (hcap : items.length < cap) (hfit : Fits w (baseAddr + cap * k)) :
      ∃ (y : Data r), Exec w (push base top cursor width one fields) x y (2 * k + 2) ∧ y.regs top = items.length + 1 ∧ Holds R baseAddr (v :: items) y.mem ∧ y.mem = R.write (baseAddr + items.length * k) v x.mem ∧ Modifies { base := baseAddr, length := cap * k }.footprint x.mem y.mem ∧ ∀ (i : Fin r), ¬PushLocal top cursor i → y.regs i = x.regs i

      Push a typed k-word record v onto a stack of capacity cap in exact 2 * k + 2 instructions.

      theorem Cslib.WordRAM.WordStack.popWords_exec {r k w F n : ℕ} [NeZero k] (base top cursor width one : Fin r) (fields : Fin k → Fin r) (x : Data r) (hbase_top : base ≠ top) (hwidth_top : width ≠ top) (hone_top : one ≠ top) (hbase_cur : base ≠ cursor) (htop_cur : top ≠ cursor) (hone_cur : one ≠ cursor) (hfields_top : ∀ (i : Fin k), fields i ≠ top) (hfields_cur : ∀ (i : Fin k), fields i ≠ cursor) (hfields_one : ∀ (i : Fin k), fields i ≠ one) (hinj : Function.Injective fields) (hbase : x.regs base = F) (htop : x.regs top = n + 1) (hwidth : x.regs width = k) (hone : x.regs one = 1) (hfit : Fits w (F + n * k + (k - 1))) :
      ∃ (y : Data r), Exec w (pop base top cursor width one fields) x y (2 * k + 2) ∧ y.regs top = n ∧ y.mem = x.mem ∧ (∀ (i : Fin k), y.regs (fields i) = x.mem (F + n * k + ↑i)) ∧ ∀ (i : Fin r), ¬PopLocal top cursor fields i → y.regs i = x.regs i

      Single-slot execution of WordStack.pop in exact 2 * k + 2 instructions.

      theorem Cslib.WordRAM.WordStack.pop_exec {r : ℕ} {α : Type u_1} {k w baseAddr : ℕ} [NeZero k] (R : RecordSpec α k) (base top cursor width one : Fin r) (fields : Fin k → Fin r) (items : List α) (v : α) (x : Data r) (hbase_top : base ≠ top) (hwidth_top : width ≠ top) (hone_top : one ≠ top) (hbase_cur : base ≠ cursor) (htop_cur : top ≠ cursor) (hone_cur : one ≠ cursor) (hfields_top : ∀ (i : Fin k), fields i ≠ top) (hfields_cur : ∀ (i : Fin k), fields i ≠ cursor) (hfields_one : ∀ (i : Fin k), fields i ≠ one) (hinj : Function.Injective fields) (hbase : x.regs base = baseAddr) (htop : x.regs top = items.length + 1) (hwidth : x.regs width = k) (hone : x.regs one = 1) (hstack : Holds R baseAddr (v :: items) x.mem) (hfit : Fits w (baseAddr + items.length * k + (k - 1))) :
      ∃ (y : Data r), Exec w (pop base top cursor width one fields) x y (2 * k + 2) ∧ y.regs top = items.length ∧ Holds R baseAddr items y.mem ∧ (∀ (i : Fin k), y.regs (fields i) = R.toWords v i) ∧ y.mem = x.mem ∧ ∀ (i : Fin r), ¬PopLocal top cursor fields i → y.regs i = x.regs i

      Pop the top typed k-word record v from v :: items into fields in exact 2 * k + 2 instructions.