Documentation

CslibFrontier.Computability.WordRAM.Realization.Backend

Native implementations of refinements, one realized body per operation constructor #

MemoryBackend runs each memory operation as the static body of a primitive realization, chosen by the operation's constructor. Backend does the same for any specification D refined to the memory oracle by a Refinement D (Memory.spec w). One supplied realization witness per constructor tag supplies the body; the refinement supplies correctness and memory charges.

A Certificate bundles a Realizes.Witness with its program, encodings, domain and local work. Resource and code projections retain those supplied data. Certificate.ofRealizes supports existing existential clients using classical choice; it does not identify the chosen resources with a particular realization derivation. A family C : ι → Certificate B over a finite type of tags fixes one register file, one body per tag and one exchange factor (Backend.registers, Backend.code, Backend.factor), before every width, refinement, runtime layout and argument. A Backend D C says which tag each operation of D selects (tag), which arguments the tag's program receives (arg, which may include the structure's runtime layout), and how the program's result gives the specified answer (answer). The result encoding belongs to the tag, so an operation's arguments are never encoded in its result; the native code never dispatches on the operation.

Backend.Implements connects a backend to a refinement: each method runs the selected program and maps its result by answer, and from an admissible state the arguments are words and the program's domain holds. Then Backend.implementation is a StateTransitionImpl D with a functional representation: the memory satisfies the refinement's invariant and abstracts to the state, and every register and cell holds a word. Instruction counts follow from the refinement's amortized memory charges (Backend.amortized), and are constant per operation when those charges are constant and need no potential (Backend.bound). Backends of two specifications run their product side by side (Backend.sum), with tags in a sum type: the operation's Sum constructor selects the family, and no native code dispatches on it.

As in MemoryBackend, the selected tag's arguments are installed in registers before its body runs, uncharged, as a calling convention; decoding the answer executes no instruction. Bounds count the body's instructions only. The fields arg and answer are unrestricted: arg is any function of the operation and answer any function of the operation and the program's result. Nothing requires them to be computable or charges for evaluating them, and the answer need not be held in a register: Backend.exec requires it to be answer applied to a result whose encoding the result registers hold.

Memory charges do not depend on the word width (totalCost_width, memoryCost_eq_totalCost), so memoryCost, which is taken at width zero, is the charge that refinements state at width w.

Main definitions #

Main results #

Memory charges and the word width #

The memory history of a client does not depend on the word width.

theorem Cslib.WordRAM.Realization.fits_appendWords {δ α : Type} {k l w : ℕ} (input : δ ↪ Fin k → ℕ) (output : α ↪ Fin l → ℕ) {d : δ} {a : α} (h₁ : ∀ (i : Fin k), Fits w (input d i)) (h₂ : ∀ (i : Fin l), Fits w (output a i)) (i : Fin (k + l)) :
Fits w ((appendWords input output) (d, a) i)

Appended argument words fit when both parts do.

Certificates and backends #

A uniform realization certificate, bundled with the program it realizes, its argument and result encodings, its entry domain and its local work.

@[reducible, inline]
noncomputable abbrev Cslib.WordRAM.Realization.Certificate.ofRealizes {B : Basis} {δ α : Type} {k l : ℕ} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} (h : Realizes B p input output safe work) :

Package an existential realization using its classically selected witness. This fallback retains correctness, but supplies no concrete bounds on its selected scratch or factor.

How the operations of D run on the bodies of C: each operation selects a tag, passes arguments to the tag's program and reads its answer from that program's result. Only the tag selects code. The maps arg and answer are unrestricted: they need not be computable, and evaluating them is not charged.

  • tag : D.Op → ι

    The tag of each operation, typically its constructor.

  • arg (o : D.Op) : (C (self.tag o)).Arg

    The arguments of the selected program, including any runtime layout.

  • answer (o : D.Op) : (C (self.tag o)).Res → D.Result o

    The specified answer, read off the selected program's result.

noncomputable def Cslib.WordRAM.Realization.Backend.argWords {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) :

Registers for the longest argument vector.

noncomputable def Cslib.WordRAM.Realization.Backend.scratchWords {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) :

Registers for the largest result and scratch bank.

noncomputable def Cslib.WordRAM.Realization.Backend.registers {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) :

One register count, chosen before every width, refinement, layout and argument.

Register zero holds the allocation frontier.

def Cslib.WordRAM.Realization.Backend.src {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i : ι) (j : Fin (C i).k) :

The argument registers of tag i, from register one.

noncomputable def Cslib.WordRAM.Realization.Backend.tmp {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i : ι) (j : Fin ((C i).l + (C i).realizes.scratchSize + 1)) :

The result and scratch registers of tag i, after all argument registers.

Distinct argument coordinates use distinct source registers.

Distinct result/scratch coordinates use distinct registers.

theorem Cslib.WordRAM.Realization.Backend.src_eq_of_val_eq {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i i' : ι) {a : Fin (C i).k} {b : Fin (C i').k} (h : ↑a = ↑b) :
src C i a = src C i' b

Source coordinates at the same position agree across tags.

theorem Cslib.WordRAM.Realization.Backend.tmp_eq_of_val_eq {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i i' : ι) {a : Fin ((C i).l + (C i).realizes.scratchSize + 1)} {b : Fin ((C i').l + (C i').realizes.scratchSize + 1)} (h : ↑a = ↑b) :
tmp C i a = tmp C i' b

Result/scratch coordinates at the same position agree across tags.

theorem Cslib.WordRAM.Realization.Backend.src_ne_tmp {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i i' : ι) (a : Fin (C i).k) (b : Fin ((C i').l + (C i').realizes.scratchSize + 1)) :
src C i a ≠ tmp C i' b

No tag's argument register is used by any tag's result/scratch bank.

theorem Cslib.WordRAM.Realization.Backend.src_ne_frontier {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i : ι) (a : Fin (C i).k) :
src C i a ≠ frontier C

Argument installation never uses the frontier register.

theorem Cslib.WordRAM.Realization.Backend.tmp_ne_frontier {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i : ι) (a : Fin ((C i).l + (C i).realizes.scratchSize + 1)) :
tmp C i a ≠ frontier C

Native result/scratch writes never use the frontier register.

noncomputable def Cslib.WordRAM.Realization.Backend.code {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i : ι) :

The static body of tag i.

noncomputable def Cslib.WordRAM.Realization.Backend.factor {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) :

One exchange factor for every tag.

theorem Cslib.WordRAM.Realization.Backend.factor_le {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i : ι) :

Each tag's exchange factor is at most the common one.

The native data holding the memory state s: its cells, the frontier in its register, and zero in every other register. A calling-convention state, not an allocation.

@[simp]

The data holding s abstracts to s.

The data holding a well-formed memory state is word-bounded.

def Cslib.WordRAM.Realization.Backend.install {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i : ι) (d : (C i).Arg) (x : Data (registers C)) :

Install arguments d of tag i in its argument registers. Installation is uncharged and leaves memory and the frontier unchanged.

@[simp]
theorem Cslib.WordRAM.Realization.Backend.memoryState_install {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i : ι) (d : (C i).Arg) (x : Data (registers C)) :

Installation leaves the memory and the frontier.

theorem Cslib.WordRAM.Realization.Backend.install_src {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i : ι) (d : (C i).Arg) (x : Data (registers C)) :
(install C i d x).regs ∘ src C i = (C i).input d

Installed arguments occupy the selected tag's actual source registers.

theorem Cslib.WordRAM.Realization.Backend.install_eq_self {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) (i : ι) (d : (C i).Arg) (x : Data (registers C)) (hin : x.regs ∘ src C i = (C i).input d) :
install C i d x = x

Installing arguments already present in their source registers changes nothing. This identity requires actual live words; it supplies no native argument-staging code.

theorem Cslib.WordRAM.Realization.Backend.install_bounded {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) {w : ℕ} (i : ι) (d : (C i).Arg) {x : Data (registers C)} (hx : Data.Bounded w x) (hin : ∀ (j : Fin (C i).k), Fits w ((C i).input d j)) :
Data.Bounded w (install C i d x)

Installing fitted argument words preserves bounded data.

theorem Cslib.WordRAM.Realization.Backend.exists_exec_frame {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) {w : ℕ} (i : ι) (d : (C i).Arg) {x : Data (registers C)} (hx : Data.Bounded w x) (hin : ∀ (j : Fin (C i).k), Fits w ((C i).input d j)) (hsafe : (C i).safe w d (memoryState (frontier C) x)) (hv : (DataStructure.Memory.spec w).ValidHistory (memoryState (frontier C) x) ((DataStructure.Memory.spec w).clientHistory (memoryState (frontier C) x) ((C i).prog d))) :
∃ (y : Data (registers C)) (n : ℕ), Exec w (code C i) (install C i d x) y n ∧ n ≤ factor C * (memoryCost ((C i).prog d) (memoryState (frontier C) x) + (C i).work d (memoryState (frontier C) x) + 1) ∧ memoryState (frontier C) y = (DataStructure.Memory.spec w).clientState (memoryState (frontier C) x) ((C i).prog d) ∧ (∀ (j : Fin (C i).l), y.regs (tmp C i (Fin.castAdd ((C i).realizes.scratchSize + 1) j)) = (C i).output ((DataStructure.Memory.spec w).clientResult (memoryState (frontier C) x) ((C i).prog d)) j) ∧ Data.Bounded w y ∧ ∀ (j : Fin (registers C)), j ≠ frontier C → (∀ (a : Fin ((C i).l + (C i).realizes.scratchSize + 1)), tmp C i a ≠ j) → y.regs j = (install C i d x).regs j

The selected body's complete execution guarantee, including its register frame. Only the frontier and its result/scratch bank may change relative to installed arguments.

theorem Cslib.WordRAM.Realization.Backend.exists_exec {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) {w : ℕ} (i : ι) (d : (C i).Arg) {x : Data (registers C)} (hx : Data.Bounded w x) (hin : ∀ (j : Fin (C i).k), Fits w ((C i).input d j)) (hsafe : (C i).safe w d (memoryState (frontier C) x)) (hv : (DataStructure.Memory.spec w).ValidHistory (memoryState (frontier C) x) ((DataStructure.Memory.spec w).clientHistory (memoryState (frontier C) x) ((C i).prog d))) :
∃ (y : Data (registers C)) (n : ℕ), Exec w (code C i) (install C i d x) y n ∧ n ≤ factor C * (memoryCost ((C i).prog d) (memoryState (frontier C) x) + (C i).work d (memoryState (frontier C) x) + 1) ∧ memoryState (frontier C) y = (DataStructure.Memory.spec w).clientState (memoryState (frontier C) x) ((C i).prog d) ∧ (∀ (j : Fin (C i).l), y.regs (tmp C i (Fin.castAdd ((C i).realizes.scratchSize + 1) j)) = (C i).output ((DataStructure.Memory.spec w).clientResult (memoryState (frontier C) x) ((C i).prog d)) j) ∧ Data.Bounded w y

The body of one tag. From bounded data, with arguments that fit words, inside the tag's domain and with an admissible memory history, the body of tag i runs its program on the installed arguments within the certified bound at the common exchange factor. It leaves the program's final memory and encoded result, and every register and cell a word.

theorem Cslib.WordRAM.Realization.Backend.exists_exec_live {B : Basis} {ι : Type} [Fintype ι] (C : ι → Certificate B) {w : ℕ} (i : ι) (d : (C i).Arg) {x : Data (registers C)} (hx : Data.Bounded w x) (hin : x.regs ∘ src C i = (C i).input d) (hsafe : (C i).safe w d (memoryState (frontier C) x)) (hv : (DataStructure.Memory.spec w).ValidHistory (memoryState (frontier C) x) ((DataStructure.Memory.spec w).clientHistory (memoryState (frontier C) x) ((C i).prog d))) :
∃ (y : Data (registers C)) (n : ℕ), Exec w (code C i) x y n ∧ n ≤ factor C * (memoryCost ((C i).prog d) (memoryState (frontier C) x) + (C i).work d (memoryState (frontier C) x) + 1) ∧ memoryState (frontier C) y = (DataStructure.Memory.spec w).clientState (memoryState (frontier C) x) ((C i).prog d) ∧ (∀ (j : Fin (C i).l), y.regs (tmp C i (Fin.castAdd ((C i).realizes.scratchSize + 1) j)) = (C i).output ((DataStructure.Memory.spec w).clientResult (memoryState (frontier C) x) ((C i).prog d)) j) ∧ Data.Bounded w y ∧ ∀ (j : Fin (registers C)), j ≠ frontier C → (∀ (a : Fin ((C i).l + (C i).realizes.scratchSize + 1)), tmp C i a ≠ j) → y.regs j = x.regs j

Run the same fixed body directly from bounded data whose input words are already live. No installation or loader is executed. Safety and valid history remain explicit, and the register frame is relative to the actual initial data.

A backend runs the methods of R: each method is the selected program with its result mapped by answer, and from an admissible state the arguments are words and the program's domain holds.

Products #

Two backends side by side run the product specification: a left operation selects a left tag and a right operation a right tag.

theorem Cslib.WordRAM.Realization.Backend.Implements.prod {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} {ι' : Type} {C' : ι' → Certificate B} {D' : DataStructure.StateTransitionSpec} {w : ℕ} {S : Backend D C} {S' : Backend D' C'} {R : DataStructure.Refinement D (DataStructure.Memory.spec w)} {R' : DataStructure.Refinement D' (DataStructure.Memory.spec w)} {J : DataStructure.Memory.State → Prop} {frame₁ : ∀ {e : (DataStructure.Memory.spec w).State} (o : D.Op) {a : D.Result o} {e' : (DataStructure.Memory.spec w).State} {tr : (DataStructure.Memory.spec w).Transcript}, R.inv e → R'.inv e → J e → D.valid (R.abs e) o → (DataStructure.Memory.spec w).ClientRun e (R.methods o) a e' tr → R'.abs e' = R'.abs e ∧ R'.inv e' ∧ J e'} {frame₂ : ∀ {e : (DataStructure.Memory.spec w).State} (o : D'.Op) {a : D'.Result o} {e' : (DataStructure.Memory.spec w).State} {tr : (DataStructure.Memory.spec w).Transcript}, R.inv e → R'.inv e → J e → D'.valid (R'.abs e) o → (DataStructure.Memory.spec w).ClientRun e (R'.methods o) a e' tr → R.abs e' = R.abs e ∧ R.inv e' ∧ J e'} (hS : S.Implements R) (hS' : S'.Implements R') :
(S.sum S').Implements (R.prod R' J ⋯ ⋯)

Side-by-side backends run the product of two non-interfering refinements, whatever their joint invariant J.

def Cslib.WordRAM.Realization.Backend.extend {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} (S : Backend D C) {ι' : Type} (C' : ι' → Certificate B) :
Backend D (Sum.elim C C')

The backend with the tags of a second family added, such as a realized allocation's (Backend.Allocation). Every operation selects the body it selects in S.

An extended backend runs the methods its original runs.

Native execution #

Install the selected program's arguments in its argument registers. Installation is uncharged and leaves memory and the frontier unchanged.

An operation runs the body of its tag from the prepared data, in the counted number of instructions, and its answer is read off the encoded result.

theorem Cslib.WordRAM.Realization.Backend.exec_iff {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} (S : Backend D C) [Fintype ι] (w : ℕ) (x y : Data (registers C)) (o : D.Op) (a : D.Result o) (n : ℕ) :
S.exec w x o a y n ↔ Exec w (code C (S.tag o)) (S.prepare o x) y n ∧ ∃ (b : (C (S.tag o)).Res), a = S.answer o b ∧ ∀ (j : Fin (C (S.tag o)).l), y.regs (tmp C (S.tag o) (Fin.castAdd ((C (S.tag o)).realizes.scratchSize + 1) j)) = (C (S.tag o)).output b j

The execution relation, unfolded.

The native implementation: represented data holds a memory that satisfies the refinement's invariant and abstracts to the state, and every register and cell holds a word.

The representation, unfolded.

The data holding a well-formed memory state that satisfies the invariant represents its abstraction.

theorem Cslib.WordRAM.Realization.Backend.exec_memoryState {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} {S : Backend D C} [Fintype ι] {w : ℕ} {R : DataStructure.Refinement D (DataStructure.Memory.spec w)} (hS : S.Implements R) {s : D.State} {x y : Data (registers C)} {o : D.Op} {a : D.Result o} {n : ℕ} (hr : (implementation hS).rep s x) (hv : D.valid s o) (he : S.exec w x o a y n) :

An operation leaves the memory and frontier of its method.

theorem Cslib.WordRAM.Realization.Backend.amortized {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} {S : Backend D C} [Fintype ι] {w : ℕ} {R : DataStructure.Refinement D (DataStructure.Memory.spec w)} (hS : S.Implements R) {upper : D.State → D.Op → ℕ} {Φ : DataStructure.Memory.State → ℕ} (A : R.Amortized DataStructure.Memory.charge upper Φ) (c : ℕ) (hwork : ∀ {e : (DataStructure.Memory.spec w).State} (o : D.Op), R.inv e → D.valid (R.abs e) o → (C (S.tag o)).work (S.arg o) e ≤ c) :
DataStructure.Amortized (implementation hS) id (fun (x : D.State) (x_1 : Data (registers C)) => factor C * Φ (memoryState (frontier C) x_1)) fun (s : D.State) (o : D.Op) => factor C * (upper s o + c + 1)

Amortized instructions. Memory charges with a potential, plus local work at most c, give native instruction counts with the potential scaled by the exchange factor.

theorem Cslib.WordRAM.Realization.Backend.bound {B : Basis} {ι : Type} {C : ι → Certificate B} {D : DataStructure.StateTransitionSpec} {S : Backend D C} [Fintype ι] {w : ℕ} {R : DataStructure.Refinement D (DataStructure.Memory.spec w)} (hS : S.Implements R) {upper : D.State → D.Op → ℕ} (A : R.Amortized DataStructure.Memory.charge upper fun (x : (DataStructure.Memory.spec w).State) => 0) (c : ℕ) (hwork : ∀ {e : (DataStructure.Memory.spec w).State} (o : D.Op), R.inv e → D.valid (R.abs e) o → (C (S.tag o)).work (S.arg o) e ≤ c) :
DataStructure.Bound (implementation hS) id fun (s : D.State) (o : D.Op) => factor C * (upper s o + c + 1)

Worst-case instructions. Memory charges at most upper without a potential, plus local work at most c, bound every operation's instructions by factor C * (upper + c + 1). With a constant upper this is one constant for every width and refinement.