Documentation

CslibFrontier.Computability.WordRAM.PreservesMask

Reflected register-preservation and width-read checks for word-RAM code #

Proving ∀ i : Fin r, P i → c.Preserves i by decide runs Nat.decidableForallFin r times across c, taking O(r · |c|) reductions and exceeding the default maxRecDepth once r reaches a few hundred registers. Likewise, synthesizing Decidable c.ReadsWidth walks the full syntax tree of c in the elaborator before kernel reduction can run.

This module provides single-pass O(|c|) structural reflections:

Main definitions #

Main results #

def Cslib.WordRAM.Op.preservesAll {B : Basis} {r : ℕ} (keep : Fin r → Bool) (o : Op B r) :

Returns true iff o does not write a register satisfying keep.

def Cslib.WordRAM.Op.writeStart {B : Basis} {r : ℕ} (o : Op B r) :

Minimum written register index of o, or r for store.

def Cslib.WordRAM.Op.writeEnd {B : Basis} {r : ℕ} (o : Op B r) :

Strict upper bound (d.val + 1) on the register index written by o, or 0 for store.

Returns true iff o observes the physical word size w.

o.readsWidthBool = true holds iff o.ReadsWidth.

Single-pass Bool preservation predicate (preservesAll) #

def Cslib.WordRAM.Code.preservesAll {B : Basis} {r : ℕ} (keep : Fin r → Bool) :
Code B r → Bool

Single-pass Bool check over Fin r: returns true iff no instruction of c writes a register d with keep d = true. Unlike Nat.decidableForallFin, this visits c once and evaluates keep only on written destinations (O(|c|) instead of O(r · |c|)).

theorem Cslib.WordRAM.Code.preservesAll_eq_true_iff {B : Basis} {r : ℕ} (keep : Fin r → Bool) (c : Code B r) :
preservesAll keep c = true ↔ ∀ (i : Fin r), keep i = true → Preserves i c

c.preservesAll keep = true matches preserving every register with keep i = true.

theorem Cslib.WordRAM.Code.preserves_of_preservesAllPred {B : Basis} {r : ℕ} {P : Fin r → Prop} [DecidablePred P] {c : Code B r} (h : preservesAll (fun (i : Fin r) => decide (P i)) c = true) (i : Fin r) :
P i → Preserves i c

Drop-in replacement for ∀ i : Fin r, P i → c.Preserves i := by decide: evaluates P only on the destination registers written inside c, never iterating over all r elements of Fin r.

@[simp]
theorem Cslib.WordRAM.Code.preservesAll_rename {B : Basis} {r r' : ℕ} (keep : Fin r' → Bool) (f : Fin r → Fin r') (c : Code B r) :
preservesAll keep (rename f c) = preservesAll (keep ∘ f) c

Register renaming pulls keep back along f without unfolding c.

theorem Cslib.WordRAM.Code.preservesAll_rename_of_false {B : Basis} {r r' : ℕ} {keep : Fin r' → Bool} {f : Fin r → Fin r'} (hf : ∀ (j : Fin r), keep (f j) = false) (c : Code B r) :

preservesAll on a renamed subprogram is true without unfolding the callee whenever the register bank f is disjoint from keep.

Interval bounds (writeStart / writeEnd) #

Minimum written register index (or r when c writes no registers).

def Cslib.WordRAM.Code.writeEnd {B : Basis} {r : ℕ} :
Code B r → ℕ

Strict upper bound max d + 1 on written register indices (or 0 when c writes none).

theorem Cslib.WordRAM.Code.preserves_of_writeStart {B : Basis} {r : ℕ} {c : Code B r} {lo : ℕ} (h : lo ≤ c.writeStart) {i : Fin r} (hi : ↑i < lo) :

Every register strictly below lo is preserved when lo ≤ c.writeStart.

theorem Cslib.WordRAM.Code.preserves_of_writeEnd {B : Basis} {r : ℕ} {c : Code B r} {hi : ℕ} (h : c.writeEnd ≤ hi) {i : Fin r} (hge : hi ≤ ↑i) :

Every register at or above hi is preserved when c.writeEnd ≤ hi.

Reflected readsWidthBool and compositional rules #

Structural Bool check determining whether c reads the physical word width w.

c.readsWidthBool = true holds iff c.ReadsWidth.

Soundness for the reflected width check.

@[simp]
theorem Cslib.WordRAM.Code.readsWidthBool_rename {B : Basis} {r r' : ℕ} (f : Fin r → Fin r') (c : Code B r) :

Register renaming preserves readsWidthBool without unfolding c.