Correctness and memory-cost specifications for basic subroutines (internal) #
Proves admissibility, frame preservation, O(n²)/O(n) memory cost, and functional
correctness for copyWordsProg, zeroWordsProg, argMinArrayProg, selectParentsProg,
findCycleProg, and cycleMaskProg.
Correctness, admissibility, and O(n³) memory-cost specifications #
theorem Cslib.Algorithms.WordRAM.Arborescence.Internal.copyWordsProg_spec {w : ℕ} (a : CopyArgs) (s₀ : DataStructure.Memory.State) (hsrc_f : a.src + a.len ≤ s₀.frontier) (hdst_f : a.dst + a.len ≤ s₀.frontier) (hdisj : a.src + a.len ≤ a.dst ∨ a.dst + a.len ≤ a.src) (hfit_src : WordRAM.Fits w (a.src + a.len)) (hfit_dst : WordRAM.Fits w (a.dst + a.len)) (hfit_len : WordRAM.Fits w a.len) (hval : ∀ i < a.len, WordRAM.Fits w (s₀.mem (a.src + i))) :
have s' := (DataStructure.Memory.spec 0).clientState s₀ (copyWordsProg a);
WordRAM.Realization.iterationSafe copyStep CopyStepSafe w a a.len 0 s₀ ∧ (DataStructure.Memory.spec w).ValidHistory s₀ ((DataStructure.Memory.spec w).clientHistory s₀ (copyWordsProg a)) ∧ WordRAM.Realization.memoryCost (copyWordsProg a) s₀ ≤ 2 * a.len ∧ s'.frontier = s₀.frontier ∧ (∀ (x : ℕ), ¬(a.dst ≤ x ∧ x < a.dst + a.len) → s'.mem x = s₀.mem x) ∧ ∀ i < a.len, s'.mem (a.dst + i) = s₀.mem (a.src + i)
theorem Cslib.Algorithms.WordRAM.Arborescence.Internal.argMinArrayProg_spec {w n : ℕ} (predBase costBase : ℕ) (pred : Fin n → Bool) (val : Fin n → ℤ) (s₀ : DataStructure.Memory.State) (h2 : WordRAM.Fits w 2) (hfit_p : WordRAM.Fits w (predBase + n)) (hfit_c : WordRAM.Fits w (costBase + n)) (hfit_n : WordRAM.Fits w n) (hp_f : predBase + n ≤ s₀.frontier) (hc_f : costBase + n ≤ s₀.frontier) (hp_mem : ∀ (i : Fin n), s₀.mem (predBase + ↑i) = if pred i = true then 1 else 0) (hc_mem : ∀ (i : Fin n), s₀.mem (costBase + ↑i) = WordRAM.Signed.encode (val i)) :
have a := { n := n, predBase := predBase, costBase := costBase };
WordRAM.Realization.iterationSafe argMinStep ArgMinStepSafe w a n { idx := 0, bestIdx := n, bestCost := 0 } s₀ ∧ (DataStructure.Memory.spec w).ValidHistory s₀ ((DataStructure.Memory.spec w).clientHistory s₀ (argMinArrayProg a)) ∧ WordRAM.Realization.memoryCost (argMinArrayProg a) s₀ ≤ 2 * n ∧ (DataStructure.Memory.spec 0).clientState s₀ (argMinArrayProg a) = s₀ ∧ (DataStructure.Memory.spec 0).clientResult s₀ (argMinArrayProg a) = optFinIdx (Lean.Arborescence.argMinFin n pred val)
theorem Cslib.Algorithms.WordRAM.Arborescence.Internal.selectParentsProg_spec {w n ws k : ℕ} (r : Fin n) (a : Lean.Arborescence.AdjTable n) (c : Lean.Arborescence.CostTable ℤ n) (s₀ : DataStructure.Memory.State) (hk : k < n) (hws : WsFits w ws n) (hf : ws + wsSize n ≤ s₀.frontier) (ha : ∀ (u col : Fin n), s₀.mem (curAdjBase ws n + ↑u * n + ↑col) = if a[↑u][↑col] = true then 1 else 0) (hc : ∀ (u col : Fin n), s₀.mem (curCostBase ws n + ↑u * n + ↑col) = WordRAM.Signed.encode c[↑u][↑col]) (hfit_c : ∀ (u col : Fin n), WordRAM.Fits w (WordRAM.Signed.encode c[↑u][↑col])) :
have sa := { n := n, r := ↑r, ws := ws, k := k };
have s' := (DataStructure.Memory.spec 0).clientState s₀ (selectParentsProg sa);
have sel := Lean.Arborescence.selectParents a c r;
(WordRAM.Fits w 1 ∧ WordRAM.Realization.iterationSafe selectOuterStep SelectOuterStepSafe w sa n { v := 0, feas := 1 } s₀) ∧ (DataStructure.Memory.spec w).ValidHistory s₀
((DataStructure.Memory.spec w).clientHistory s₀ (selectParentsProg sa)) ∧ WordRAM.Realization.memoryCost (selectParentsProg sa) s₀ ≤ (6 * n + 2) * n ∧ s'.frontier = s₀.frontier ∧ (∀ (x : ℕ),
¬(candPredBase ws n ≤ x ∧ x < candCostBase ws n + n ∨ frameParentBase ws n k ≤ x ∧ x < frameParentBase ws n k + n) →
s'.mem x = s₀.mem x) ∧ ((DataStructure.Memory.spec 0).clientResult s₀ (selectParentsProg sa) = if sel.feasible = true then 1 else 0) ∧ ∀ (v : Fin n), s'.mem (frameParentBase ws n k + ↑v) = ↑sel.parents[↑v]
theorem Cslib.Algorithms.WordRAM.Arborescence.Internal.findCycleProg_spec {w n parentBase : ℕ} (r : Fin n) (f : Lean.Arborescence.ParentTable n) (s₀ : DataStructure.Memory.State) (h1 : WordRAM.Fits w 1) (hfit_p : WordRAM.Fits w (parentBase + n)) (hfit_n : WordRAM.Fits w n) (hf0 : 0 < s₀.frontier) (hp_f : parentBase + n ≤ s₀.frontier) (hf_mem : ∀ (v : Fin n), s₀.mem (parentBase + ↑v) = ↑f[↑v]) :
WordRAM.Realization.iterationSafe findCycleOuterStep FindCycleOuterStepSafe w
{ n := n, r := ↑r, parentBase := parentBase } n { v := 0, found := n } s₀ ∧ (DataStructure.Memory.spec w).ValidHistory s₀
((DataStructure.Memory.spec w).clientHistory s₀ (findCycleProg { n := n, r := ↑r, parentBase := parentBase })) ∧ WordRAM.Realization.memoryCost (findCycleProg { n := n, r := ↑r, parentBase := parentBase }) s₀ ≤ (n + 1) * n ∧ (DataStructure.Memory.spec 0).clientState s₀ (findCycleProg { n := n, r := ↑r, parentBase := parentBase }) = s₀ ∧ (DataStructure.Memory.spec 0).clientResult s₀ (findCycleProg { n := n, r := ↑r, parentBase := parentBase }) = optFinIdx (Lean.Arborescence.findCycleTable r f)
theorem Cslib.Algorithms.WordRAM.Arborescence.Internal.cycleMaskProg_spec {w n parentBase maskBase : ℕ} (f : Lean.Arborescence.ParentTable n) (x0 : Fin n) (s₀ : DataStructure.Memory.State) (h1 : WordRAM.Fits w 1) (hfit_p : WordRAM.Fits w (parentBase + n)) (hfit_m : WordRAM.Fits w (maskBase + n)) (hfit_n : WordRAM.Fits w n) (hp_f : parentBase + n ≤ s₀.frontier) (hm_f : maskBase + n ≤ s₀.frontier) (hdisj : parentBase + n ≤ maskBase ∨ maskBase + n ≤ parentBase) (hf_mem : ∀ (v : Fin n), s₀.mem (parentBase + ↑v) = ↑f[↑v]) :
have cm := { n := n, parentBase := parentBase, maskBase := maskBase, x0 := ↑x0 };
have s0z := (DataStructure.Memory.spec 0).clientState s₀ (zeroWordsProg { dst := maskBase, len := n });
have s' := (DataStructure.Memory.spec 0).clientState s₀ (cycleMaskProg cm);
(WordRAM.Realization.iterationSafe zeroStep ZeroStepSafe w { dst := maskBase, len := n } n 0 s₀ ∧ WordRAM.Realization.iterationSafe cycleMaskStep CycleMaskStepSafe w cm n { step := 0, cur := ↑x0 } s0z) ∧ (DataStructure.Memory.spec w).ValidHistory s₀ ((DataStructure.Memory.spec w).clientHistory s₀ (cycleMaskProg cm)) ∧ WordRAM.Realization.memoryCost (cycleMaskProg cm) s₀ ≤ 3 * n ∧ s'.frontier = s₀.frontier ∧ (∀ (addr : ℕ), ¬(maskBase ≤ addr ∧ addr < maskBase + n) → s'.mem addr = s₀.mem addr) ∧ ∀ (y : Fin n), s'.mem (maskBase + ↑y) = if (Lean.Arborescence.cycleMask f x0)[↑y] = true then 1 else 0