Documentation

CslibFrontier.Computability.WordRAM.Record

Fixed-width flat memory records on the word RAM #

Algorithms on the word RAM serialize fixed-width headers, task descriptors, and explicit stack frames as k consecutive words at an address addr. This module provides:

Main definitions #

Main results #

Multi-word consecutive memory updates #

def Cslib.WordRAM.writeWords (mem : ℕ → ℕ) (addr : ℕ) {k : ℕ} :
(Fin k → ℕ) → ℕ → ℕ

Write k consecutive words starting at addr, in increasing offset order.

@[simp]
theorem Cslib.WordRAM.writeWords_zero (mem : ℕ → ℕ) (addr : ℕ) (words : Fin 0 → ℕ) :
writeWords mem addr words = mem
@[simp]
theorem Cslib.WordRAM.writeWords_succ (mem : ℕ → ℕ) (addr : ℕ) {k : ℕ} (words : Fin (k + 1) → ℕ) :
writeWords mem addr words = Function.update (writeWords mem addr fun (i : Fin k) => words i.castSucc) (addr + k) (words (Fin.last k))
theorem Cslib.WordRAM.writeWords_cons (mem : ℕ → ℕ) (addr : ℕ) {k : ℕ} (words : Fin (k + 1) → ℕ) :
(writeWords (Function.update mem addr (words 0)) (addr + 1) fun (i : Fin k) => words i.succ) = writeWords mem addr words

Writing words 0 at addr and the remaining k words at addr + 1 equals writing all k + 1 words from addr.

theorem Cslib.WordRAM.Block.realizes_writeWords (mem : ℕ → ℕ) (addr : ℕ) {k : ℕ} (words : Fin k → ℕ) :
{ base := addr, length := k }.Realizes (writeWords mem addr words) words

Writing k words at addr realizes words on Block.mk addr k.

@[simp]
theorem Cslib.WordRAM.writeWords_add_fin (mem : ℕ → ℕ) (addr : ℕ) {k : ℕ} (words : Fin k → ℕ) (i : Fin k) :
writeWords mem addr words (addr + ↑i) = words i
theorem Cslib.WordRAM.writeWords_of_not_owns {mem : ℕ → ℕ} {addr k a : ℕ} {words : Fin k → ℕ} (ha : ¬{ base := addr, length := k }.Owns a) :
writeWords mem addr words a = mem a

Addresses outside Block.mk addr k are unchanged by writeWords.

theorem Cslib.WordRAM.modifies_writeWords (mem : ℕ → ℕ) (addr : ℕ) {k : ℕ} (words : Fin k → ℕ) :
Modifies { base := addr, length := k }.footprint mem (writeWords mem addr words)

writeWords mem addr words modifies at most (Block.mk addr k).footprint.

Typed fixed-width record specifications #

structure Cslib.WordRAM.RecordSpec (α : Type u_1) (k : ℕ) :
Type u_1

Specification of a fixed-width flat k-word memory layout for values of type α.

  • toWords : α → Fin k → ℕ

    The k consecutive words representing a record value.

@[reducible, inline]
abbrev Cslib.WordRAM.RecordSpec.HoldsAt {α : Type u_1} {k : ℕ} (R : RecordSpec α k) (addr : ℕ) (v : α) (mem : ℕ → ℕ) :

Memory mem holds the k-word representation of v starting at addr. Defined as an abbreviation of Block.Realizes so ram_keep applies directly.

@[reducible, inline]
abbrev Cslib.WordRAM.RecordSpec.write {α : Type u_1} {k : ℕ} (R : RecordSpec α k) (addr : ℕ) (v : α) (mem : ℕ → ℕ) :
ℕ → ℕ

Write the k-word representation of v into mem starting at addr.

theorem Cslib.WordRAM.RecordSpec.holdsAt_iff {α : Type u_1} {k : ℕ} (R : RecordSpec α k) (addr : ℕ) (v : α) (mem : ℕ → ℕ) :
R.HoldsAt addr v mem ↔ ∀ (i : Fin k), mem (addr + ↑i) = R.toWords v i
theorem Cslib.WordRAM.RecordSpec.HoldsAt.unique {α : Type u_1} {k : ℕ} {R : RecordSpec α k} (hinj : Function.Injective R.toWords) {addr : ℕ} {u v : α} {mem : ℕ → ℕ} (hu : R.HoldsAt addr u mem) (hv : R.HoldsAt addr v mem) :
u = v

At most one value of α can hold at addr in a given memory when R.toWords is injective.

theorem Cslib.WordRAM.RecordSpec.holdsAt_write {α : Type u_1} {k : ℕ} (R : RecordSpec α k) (addr : ℕ) (v : α) (mem : ℕ → ℕ) :
R.HoldsAt addr v (R.write addr v mem)

Writing v at addr establishes R.HoldsAt addr v.

theorem Cslib.WordRAM.RecordSpec.holdsAt_write_iff {α : Type u_1} {k : ℕ} (R : RecordSpec α k) (hinj : Function.Injective R.toWords) (addr : ℕ) (u v : α) (mem : ℕ → ℕ) :
R.HoldsAt addr u (R.write addr v mem) ↔ u = v

After writing v at addr, R.HoldsAt addr u holds if and only if u = v when R.toWords is injective.

Right-associated composition rules for appendCode #

theorem Cslib.WordRAM.exec_append {B : Basis} {r w T₁ T₂ : ℕ} {c d : Code B r} {x y z : Data r} (h₁ : Exec w c x y T₁) (h₂ : Exec w d y z T₂) :
Exec w (appendCode c d) x z (T₁ + T₂)

Exact instruction-count execution rule for appendCode.

theorem Cslib.WordRAM.Code.preserves_appendCode {B : Basis} {r : ℕ} {c d : Code B r} {i : Fin r} (hc : Preserves i c) (hd : Preserves i d) :

appendCode preserves register i when both parts preserve i.

appendCode avoids width% when both parts do.

theorem Cslib.WordRAM.WP.of_exec_spec {B : Basis} {r w T : ℕ} {c : Code B r} {x : Data r} {P : Data r → Prop} {Q : Data r → ℕ → Prop} (h : ∃ (y : Data r), Exec w c x y T ∧ P y) (hQ : ∀ (y : Data r), P y → Q y T) :
WP w c x Q

Continue symbolic WP execution from an exact-count routine contract.

theorem Cslib.WordRAM.WP.of_append_exec {B : Basis} {r w T₁ : ℕ} {c d : Code B r} {x : Data r} {P : Data r → Prop} {Q : Data r → ℕ → Prop} (h : ∃ (y : Data r), Exec w c x y T₁ ∧ P y) (hcont : ∀ (y : Data r), P y → WP w d y fun (z : Data r) (T₂ : ℕ) => Q z (T₂ + T₁)) :
WP w (appendCode c d) x Q

Continue symbolic WP execution across an exact-count prefix c of a right-nested ram[ … ] block.

theorem Cslib.WordRAM.WP.exists_execWithin_preserves {B : Basis} {r w T : ℕ} {c : Code B r} {x : Data r} {P : Fin r → Prop} {Q : Data r → Prop} (hpres : ∀ (i : Fin r), P i → Code.Preserves i c) (hwp : WP w c x fun (y : Data r) (t : ℕ) => t ≤ T ∧ ((∀ (i : Fin r), P i → y.regs i = x.regs i) → Q y)) :
∃ (y : Data r), ExecWithin w c x y T ∧ Q y

Discharge an ∃ y, ExecWithin w c x y T ∧ Q y goal from a register-preservation lemma ∀ i, P i → c.Preserves i and a WP proof that receives ∀ i, P i → y.regs i = x.regs i.

Certified sequential cursor store and load routines #

def Cslib.WordRAM.storeCursor {r : ℕ} (cursor one : Fin r) {k : ℕ} [NeZero k] :
(Fin k → Fin r) → Code Basis.standard r

Store k ≥ 1 field registers at consecutive addresses starting from the address already in cursor, stepping cursor by one between stores (2 * k - 1 instructions).

def Cslib.WordRAM.storeRecord {r k : ℕ} [NeZero k] (addr cursor one : Fin r) (fields : Fin k → Fin r) :

Copy addr into cursor and store k ≥ 1 field registers at addr, …, addr + (k - 1) (2 * k instructions). Matches ram[ cursor ← addr; *cursor ← f₀; cursor ← cursor + one; … ] definitionally.

def Cslib.WordRAM.loadCursor {r : ℕ} (cursor one : Fin r) {k : ℕ} [NeZero k] :
(Fin k → Fin r) → Code Basis.standard r

Load k ≥ 1 consecutive memory words starting from the address already in cursor into fields, stepping cursor by one between loads (2 * k - 1 instructions).

def Cslib.WordRAM.loadRecord {r k : ℕ} [NeZero k] (addr cursor one : Fin r) (fields : Fin k → Fin r) :

Copy addr into cursor and load k ≥ 1 consecutive words from addr, …, addr + (k - 1) into fields (2 * k instructions).

def Cslib.WordRAM.LoadRecordLocal {r k : ℕ} (cursor : Fin r) (fields : Fin k → Fin r) (j : Fin r) :

Registers written by loadCursor and loadRecord: the address cursor and the target field registers.

Instances For
    @[instance_reducible]
    instance Cslib.WordRAM.instDecidableLoadRecordLocal {r k : ℕ} (cursor : Fin r) (fields : Fin k → Fin r) (j : Fin r) :
    Decidable (LoadRecordLocal cursor fields j)
    theorem Cslib.WordRAM.storeCursor_preserves {r k : ℕ} [inst : NeZero k] (cursor one : Fin r) (fields : Fin k → Fin r) {j : Fin r} (hj : j ≠ cursor) :
    Code.Preserves j (storeCursor cursor one fields)

    storeCursor preserves every register except cursor.

    theorem Cslib.WordRAM.storeRecord_preserves {r k : ℕ} [NeZero k] (addr cursor one : Fin r) (fields : Fin k → Fin r) {j : Fin r} (hj : j ≠ cursor) :
    Code.Preserves j (storeRecord addr cursor one fields)

    storeRecord preserves every register except cursor.

    theorem Cslib.WordRAM.loadCursor_preserves {r k : ℕ} [inst : NeZero k] (cursor one : Fin r) (fields : Fin k → Fin r) {j : Fin r} (hj : ¬LoadRecordLocal cursor fields j) :
    Code.Preserves j (loadCursor cursor one fields)

    loadCursor preserves every register outside LoadRecordLocal cursor fields.

    theorem Cslib.WordRAM.loadRecord_preserves {r k : ℕ} [NeZero k] (addr cursor one : Fin r) (fields : Fin k → Fin r) {j : Fin r} (hj : ¬LoadRecordLocal cursor fields j) :
    Code.Preserves j (loadRecord addr cursor one fields)

    loadRecord preserves every register outside LoadRecordLocal cursor fields.

    theorem Cslib.WordRAM.storeCursor_not_readsWidth {r k : ℕ} [inst : NeZero k] (cursor one : Fin r) (fields : Fin k → Fin r) :
    ¬(storeCursor cursor one fields).ReadsWidth

    storeCursor never inspects the physical word width.

    theorem Cslib.WordRAM.storeRecord_not_readsWidth {r k : ℕ} [NeZero k] (addr cursor one : Fin r) (fields : Fin k → Fin r) :
    ¬(storeRecord addr cursor one fields).ReadsWidth

    storeRecord never inspects the physical word width.

    theorem Cslib.WordRAM.loadCursor_not_readsWidth {r k : ℕ} [inst : NeZero k] (cursor one : Fin r) (fields : Fin k → Fin r) :
    ¬(loadCursor cursor one fields).ReadsWidth

    loadCursor never inspects the physical word width.

    theorem Cslib.WordRAM.loadRecord_not_readsWidth {r k : ℕ} [NeZero k] (addr cursor one : Fin r) (fields : Fin k → Fin r) :
    ¬(loadRecord addr cursor one fields).ReadsWidth

    loadRecord never inspects the physical word width.

    theorem Cslib.WordRAM.storeCursor_exec {r k w : ℕ} [inst : NeZero k] (cursor one : Fin r) (fields : Fin k → Fin r) (x : Data r) (hone_ne : one ≠ cursor) (hfields_ne : ∀ (i : Fin k), fields i ≠ cursor) (hone : x.regs one = 1) (hfit : Fits w (x.regs cursor + (k - 1))) :
    ∃ (y : Data r), Exec w (storeCursor cursor one fields) x y (2 * k - 1) ∧ (y.mem = writeWords x.mem (x.regs cursor) fun (i : Fin k) => x.regs (fields i)) ∧ y.regs cursor = x.regs cursor + (k - 1) ∧ ∀ (j : Fin r), j ≠ cursor → y.regs j = x.regs j

    Execute storeCursor in 2 * k - 1 instructions from an initialized cursor register.

    theorem Cslib.WordRAM.storeRecord_exec {r k w : ℕ} [NeZero k] (addr cursor one : Fin r) (fields : Fin k → Fin r) (x : Data r) (hone_ne : one ≠ cursor) (hfields_ne : ∀ (i : Fin k), fields i ≠ cursor) (hone : x.regs one = 1) (hfit : Fits w (x.regs addr + (k - 1))) :
    ∃ (y : Data r), Exec w (storeRecord addr cursor one fields) x y (2 * k) ∧ (y.mem = writeWords x.mem (x.regs addr) fun (i : Fin k) => x.regs (fields i)) ∧ Modifies { base := x.regs addr, length := k }.footprint x.mem y.mem ∧ y.regs cursor = x.regs addr + (k - 1) ∧ ∀ (j : Fin r), j ≠ cursor → y.regs j = x.regs j

    Execute storeRecord in 2 * k exact instructions, writing x.regs ∘ fields at x.regs addr and preserving all registers other than cursor.

    theorem Cslib.WordRAM.RecordSpec.store_exec {r : ℕ} {α : Type u_1} {k w : ℕ} [NeZero k] (R : RecordSpec α k) (addr cursor one : Fin r) (fields : Fin k → Fin r) (v : α) (x : Data r) (hone_ne : one ≠ cursor) (hfields_ne : ∀ (i : Fin k), fields i ≠ cursor) (hone : x.regs one = 1) (hfields : ∀ (i : Fin k), x.regs (fields i) = R.toWords v i) (hfit : Fits w (x.regs addr + (k - 1))) :
    ∃ (y : Data r), Exec w (storeRecord addr cursor one fields) x y (2 * k) ∧ R.HoldsAt (x.regs addr) v y.mem ∧ y.mem = R.write (x.regs addr) v x.mem ∧ Modifies { base := x.regs addr, length := k }.footprint x.mem y.mem ∧ ∀ (j : Fin r), j ≠ cursor → y.regs j = x.regs j

    Execute storeRecord for a typed RecordSpec α k value held across fields.

    theorem Cslib.WordRAM.loadCursor_exec {r k w : ℕ} [inst : NeZero k] (cursor one : Fin r) (fields : Fin k → Fin r) (x : Data r) (hone_ne : one ≠ cursor) (hfields_cur : ∀ (i : Fin k), fields i ≠ cursor) (hfields_one : ∀ (i : Fin k), fields i ≠ one) (hinj : Function.Injective fields) (hone : x.regs one = 1) (hfit : Fits w (x.regs cursor + (k - 1))) :
    ∃ (y : Data r), Exec w (loadCursor cursor one fields) x y (2 * k - 1) ∧ y.mem = x.mem ∧ (∀ (i : Fin k), y.regs (fields i) = x.mem (x.regs cursor + ↑i)) ∧ y.regs cursor = x.regs cursor + (k - 1) ∧ ∀ (j : Fin r), ¬LoadRecordLocal cursor fields j → y.regs j = x.regs j

    Execute loadCursor in 2 * k - 1 instructions from an initialized cursor register.

    theorem Cslib.WordRAM.loadRecord_exec {r k w : ℕ} [NeZero k] (addr cursor one : Fin r) (fields : Fin k → Fin r) (x : Data r) (hone_ne : one ≠ cursor) (hfields_cur : ∀ (i : Fin k), fields i ≠ cursor) (hfields_one : ∀ (i : Fin k), fields i ≠ one) (hinj : Function.Injective fields) (hone : x.regs one = 1) (hfit : Fits w (x.regs addr + (k - 1))) :
    ∃ (y : Data r), Exec w (loadRecord addr cursor one fields) x y (2 * k) ∧ y.mem = x.mem ∧ (∀ (i : Fin k), y.regs (fields i) = x.mem (x.regs addr + ↑i)) ∧ y.regs cursor = x.regs addr + (k - 1) ∧ ∀ (j : Fin r), ¬LoadRecordLocal cursor fields j → y.regs j = x.regs j

    Execute loadRecord in 2 * k exact instructions, loading x.mem (x.regs addr + i) into fields i and preserving memory and all registers outside LoadRecordLocal cursor fields.

    theorem Cslib.WordRAM.RecordSpec.load_exec {r : ℕ} {α : Type u_1} {k w : ℕ} [NeZero k] (R : RecordSpec α k) (addr cursor one : Fin r) (fields : Fin k → Fin r) (v : α) (x : Data r) (hone_ne : one ≠ cursor) (hfields_cur : ∀ (i : Fin k), fields i ≠ cursor) (hfields_one : ∀ (i : Fin k), fields i ≠ one) (hinj : Function.Injective fields) (hone : x.regs one = 1) (hmem : R.HoldsAt (x.regs addr) v x.mem) (hfit : Fits w (x.regs addr + (k - 1))) :
    ∃ (y : Data r), Exec w (loadRecord addr cursor one fields) x y (2 * k) ∧ y.mem = x.mem ∧ (∀ (i : Fin k), y.regs (fields i) = R.toWords v i) ∧ ∀ (j : Fin r), ¬LoadRecordLocal cursor fields j → y.regs j = x.regs j

    Execute loadRecord when R.HoldsAt (x.regs addr) v x.mem, placing R.toWords v i in each fields i.

    theorem Cslib.WordRAM.loadRecord_exec_self {r k w : ℕ} [NeZero k] (addr cursor one : Fin r) (fields : Fin k → Fin r) (x : Data r) (hone_ne : one ≠ cursor) (hfields_cur : ∀ (i : Fin k), fields i ≠ cursor) (hfields_one : ∀ (i : Fin k), fields i ≠ one) (hinj : Function.Injective fields) (hone : x.regs one = 1) (hmem : ∀ (i : Fin k), x.mem (x.regs addr + ↑i) = x.regs (fields i)) (hfit : Fits w (x.regs addr + (k - 1))) :
    ∃ (y : Data r), Exec w (loadRecord addr cursor one fields) x y (2 * k) ∧ y.mem = x.mem ∧ ∀ (j : Fin r), j ≠ cursor → y.regs j = x.regs j

    Reloading a k-word record whose memory cells at x.regs addr already match x.regs ∘ fields leaves every register except cursor unchanged.