Layer-transition RAM Hoare proof for deadline DP
DefinitionCountingLinExt33_DeadlineDPRAMRunalgorithmscombinatoricsposets
Proves the Hoare triple for processing a full DP layer over all reachable states in polynomial bit-cost time per state.
Definition code
import Mathlib
import Definitions.Def_CountingLinExt33_RAMLib
import Definitions.Def_CountingLinExt33_DeadlineDPAlgo
import Definitions.Def_CountingLinExt33_DeadlineDPRAM
import Definitions.Def_CountingLinExt33_DeadlineDPRAMCore
import Definitions.Def_CountingLinExt33_DeadlineDPRAMTrie
import Definitions.Def_CountingLinExt33_DeadlineDPRAMAug
import Definitions.Def_CountingLinExt33_DeadlineDPRAMSteps
import Definitions.Def_CountingLinExt33_DeadlineDPRAMCoreSteps
set_option autoImplicit false
set_option maxHeartbeats 2000000
/-!
# Inner DP Run Hoare Proofs (`DeadlineDPRAMRun`)
Proves `hoare_dpRun` for `progDPRun`: given `StaticInv`, `PatEnc g`, and `QMatEnc (boolClosure (augAdj R B g))`,
runs the `n`-layer Deadline DP and adds `dpPaths P B g hg n (dpTerminal B)` to `rAns`
in `O(n^4 * (dpStates P B g hg).card)` RAM steps.
-/
namespace DeadlineDPRAM
open MinrankTW CountingLinExt33 CountingLinExt33.Main RAMLib DeadlineDP
open scoped Classical BigOperators
/-! ## Section 1: `LayerListInv` and Pure DP Bounds -/
/-- Linked-list invariant for a completed DP layer at `rCurHead`, occupying `[heapBase n, heapEnd)`. -/
structure LayerListInv (n heapEnd : ℕ) (tbl : DPState n → ℕ)
(keys : List (DPState n)) (addrOf : DPState n → ℕ) (m : ℕ → ℕ) : Prop where
heap_ge : heapBase n ≤ heapEnd
heap_le : heapEnd ≤ m rHeap
keys_nodup : keys.Nodup
keys_iff : ∀ s, s ∈ keys ↔ 0 < tbl s
rec_bounds : ∀ s ∈ keys, heapBase n ≤ addrOf s ∧ addrOf s + (2 * n + 3) ≤ heapEnd
rec_disj : ∀ s₁ ∈ keys, ∀ s₂ ∈ keys, s₁ ≠ s₂ →
addrOf s₁ + (2 * n + 3) ≤ addrOf s₂ ∨ addrOf s₂ + (2 * n + 3) ≤ addrOf s₁
list_head : match keys with
| [] => m rCurHead = 0
| s :: _ => m rCurHead = addrOf s
list_links : ∀ (i : ℕ) (hi : i < keys.length),
m (addrOf keys[i]) = if h : i + 1 < keys.length then addrOf keys[i + 1] else 0
rec_weight : ∀ s ∈ keys, m (addrOf s + 1) = tbl s
rec_key : ∀ s ∈ keys, ∀ l < 2 * n + 1, m (addrOf s + 2 + l) = stateKey s l
lemma layerListInv_of_trieListInv {n heapStart : ℕ} {tbl : DPState n → ℕ}
{ks : List (DPState n)} {ao : DPState n → ℕ} {nd : Finset ℕ} {dep : ℕ → ℕ} {m : ℕ → ℕ}
(hTL : TrieListInv n heapStart 0 tbl ks ao nd dep m) :
LayerListInv n (m rHeap) tbl ks ao (Function.update m rCurHead (m rNxtHead)) := by
have hrCH : rCurHead = 22 := rfl
have hrH : rHeap = 17 := rfl
have hrNH : rNxtHead = 23 := rfl
have hhs64 : 64 ≤ heapBase n := heapBase_ge n
refine {
heap_ge := by have := hTL.heapStart_ge; have := hTL.heap_ge; omega
heap_le := by simp [hrCH, hrH]
keys_nodup := hTL.keys_nodup
keys_iff := hTL.keys_iff
rec_bounds := fun s hs => ⟨le_trans hTL.heapStart_ge (hTL.rec_bounds s hs).1, (hTL.rec_bounds s hs).2⟩
rec_disj := hTL.rec_disj
list_head := by
have hlh := hTL.list_head
cases ks with
| nil => simpa [hrCH, hrNH] using hlh
| cons s rest => simpa [hrCH, hrNH] using hlh
list_links := fun i hi => by
have hrb := (hTL.rec_bounds ks[i] (List.getElem_mem hi)).1
rw [Function.update_of_ne (by have := hTL.heapStart_ge; omega)]
exact hTL.list_links i hi
rec_weight := fun s hs => by
have hrb := (hTL.rec_bounds s hs).1
rw [Function.update_of_ne (by have := hTL.heapStart_ge; omega)]
exact hTL.rec_weight s hs
rec_key := fun s hs l hl => by
have hrb := (hTL.rec_bounds s hs).1
rw [Function.update_of_ne (by have := hTL.heapStart_ge; omega)]
exact hTL.rec_key s hs l hl
}
/-- Maximum heap cells allocated by one DP layer. -/
def layerHeapBudget (n card : ℕ) : ℕ :=
(n + 1) + (card + 1) * (2 * n * (n + 1) + (2 * n + 3))
/-- Total heap budget across all `n + 1` layers of `progDPRun` fits well inside `valBound n`. -/
lemma dpRun_heap_le_valBound {n : ℕ} {P : PartialOrder (Fin n)} {B : Finset (Fin n)}
{g : Fin n → Option (Fin n)} {hg : Compatible P B g} :
heapBase n + (2 * n + 3) + (n + 1) * layerHeapBudget n (dpStates P B g hg).card ≤ valBound n := by
have hcard : (dpStates P B g hg).card ≤ (n + 2) ^ (2 * n + 1) := dpStates_card_le_pow P B g hg
have hpow1 : 1 ≤ (n + 2) ^ (2 * n + 1) := Nat.one_le_pow _ _ (by omega)
have hcell : 2 * n * (n + 1) + (2 * n + 3) ≤ 3 * (n + 2) ^ 2 := by
have : 3 * (n + 2) ^ 2 = 3 * (n * n) + 12 * n + 12 := by ring
have : 2 * n * (n + 1) = 2 * (n * n) + 2 * n := by ring
omega
have hlb : layerHeapBudget n (dpStates P B g hg).card ≤ 7 * (n + 2) ^ (2 * n + 3) := by
unfold layerHeapBudget
have h1 : (dpStates P B g hg).card + 1 ≤ 2 * (n + 2) ^ (2 * n + 1) := by omega
have h2 : ((dpStates P B g hg).card + 1) * (2 * n * (n + 1) + (2 * n + 3)) ≤
(2 * (n + 2) ^ (2 * n + 1)) * (3 * (n + 2) ^ 2) := Nat.mul_le_mul h1 hcell
have h3 : (2 * (n + 2) ^ (2 * n + 1)) * (3 * (n + 2) ^ 2) = 6 * (n + 2) ^ (2 * n + 3) := by
rw [show 2 * n + 3 = (2 * n + 1) + 2 by omega, pow_add]; ring
have h4 : n + 1 ≤ (n + 2) ^ (2 * n + 3) :=
le_trans (by omega : n + 1 ≤ n + 2) (Nat.le_self_pow (by omega) (n + 2))
omega
have htot : (n + 1) * layerHeapBudget n (dpStates P B g hg).card ≤ 7 * (n + 2) ^ (2 * n + 4) := by
have h1 : (n + 1) * layerHeapBudget n (dpStates P B g hg).card ≤
(n + 2) * (7 * (n + 2) ^ (2 * n + 3)) := Nat.mul_le_mul (by omega) hlb
have h2 : (n + 2) * (7 * (n + 2) ^ (2 * n + 3)) = 7 * (n + 2) ^ (2 * n + 4) := by
rw [show 2 * n + 4 = (2 * n + 3) + 1 by omega, pow_succ]; ring
omega
have hpow_le : (n + 2) ^ (2 * n + 4) ≤ (n + 2) ^ (3 * n + 8) :=
Nat.pow_le_pow_right (by omega) (by omega)
have hhb : heapBase n + (2 * n + 3) ≤ 100 * (n + 2) ^ (3 * n + 8) := by
unfold heapBase
have h1 : (n + 2) ^ 2 ≤ (n + 2) ^ (3 * n + 8) := Nat.pow_le_pow_right (by omega) (by omega)
have h2 : (n + 2) ^ 2 = n * n + 4 * n + 4 := by ring
nlinarith
unfold valBound
omega
/-- Every DP path weight at layer `l ≤ n` is well within `valBound n`. -/
lemma dpPaths_le_valBound {n : ℕ} {P : PartialOrder (Fin n)} {B : Finset (Fin n)}
{g : Fin n → Option (Fin n)} {hg : Compatible P B g} {l : ℕ} (hl : l ≤ n) (s : DPState n) :
dpPaths P B g hg l s ≤ valBound n := by
have h1 := dpPaths_le_pow P B g hg hl s
have h2 : (n + 1) ^ (2 * n) ≤ (n + 2) ^ (2 * n) := Nat.pow_le_pow_left (by omega) _
have h3 : (n + 2) ^ (2 * n) ≤ (n + 2) ^ (3 * n + 8) :=
Nat.pow_le_pow_right (by omega) (by omega)
have h4 : (n + 2) ^ (3 * n + 8) ≤ valBound n := by unfold valBound; omega
exact le_trans h1 (le_trans h2 (le_trans h3 h4))
/-- Any `Nodup` list of states reachable at layer `l` has length `≤ (dpStates P B g hg).card`. -/
lemma nodup_le_dpStates_card {n : ℕ} {P : PartialOrder (Fin n)} {B : Finset (Fin n)}
{g : Fin n → Option (Fin n)} {hg : Compatible P B g} {l : ℕ}
{ks : List (DPState n)} (hnd : ks.Nodup)
(hsub : ∀ s ∈ ks, 0 < dpPaths P B g hg l s) :
ks.length ≤ (dpStates P B g hg).card := by
have h1 : ks.toFinset ⊆ dpStates P B g hg := by
intro s hs
simp only [List.mem_toFinset] at hs
simp only [dpStates, Finset.mem_filter, Finset.mem_univ, true_and]
exact ⟨l, hsub s hs⟩
rw [← List.toFinset_card_of_nodup hnd]
exact Finset.card_le_card h1
/-- Partial sum of transitions from the first `idx` states in `curKeys`. -/
noncomputable def layerPrefix {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) (l : ℕ)
(curKeys : List (DPState n)) (idx : ℕ) (t : DPState n) : ℕ :=
∑ i ∈ Finset.range (min idx curKeys.length),
if h : i < curKeys.length then
dpPaths P B g hg l curKeys[i] * dpEdge P B g hg curKeys[i] t
else 0
lemma layerPrefix_zero {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) (l : ℕ)
(curKeys : List (DPState n)) (t : DPState n) :
layerPrefix P B g hg l curKeys 0 t = 0 := by
simp [layerPrefix]
lemma layerPrefix_succ {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) (l : ℕ)
(curKeys : List (DPState n)) {idx : ℕ} (hidx : idx < curKeys.length) (t : DPState n) :
layerPrefix P B g hg l curKeys (idx + 1) t =
layerPrefix P B g hg l curKeys idx t +
dpPaths P B g hg l curKeys[idx] * dpEdge P B g hg curKeys[idx] t := by
unfold layerPrefix
rw [Nat.min_eq_left (by omega : idx + 1 ≤ curKeys.length),
Nat.min_eq_left (by omega : idx ≤ curKeys.length),
Finset.sum_range_succ, dif_pos hidx]
lemma layerPrefix_full {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) (l : ℕ)
{curKeys : List (DPState n)} (hnd : curKeys.Nodup)
(hiff : ∀ s, s ∈ curKeys ↔ 0 < dpPaths P B g hg l s) (t : DPState n) :
layerPrefix P B g hg l curKeys curKeys.length t = dpPaths P B g hg (l + 1) t := by
unfold layerPrefix
rw [Nat.min_self]
change (∑ i ∈ Finset.range curKeys.length,
if h : i < curKeys.length then dpPaths P B g hg l curKeys[i] * dpEdge P B g hg curKeys[i] t else 0) =
∑ s : DPState n, dpPaths P B g hg l s * dpEdge P B g hg s t
rw [← Finset.sum_subset (Finset.subset_univ curKeys.toFinset)]
· refine Finset.sum_bij (fun (i : ℕ) (hi : i ∈ Finset.range curKeys.length) => curKeys[i]'(Finset.mem_range.mp hi))
(fun i hi => List.mem_toFinset.mpr (List.getElem_mem (Finset.mem_range.mp hi)))
(fun i₁ hi₁ i₂ hi₂ heq => (List.Nodup.getElem_inj_iff hnd).mp heq)
(fun s hs => by
obtain ⟨i, hi, rfl⟩ := List.mem_iff_getElem.mp (List.mem_toFinset.mp hs)
exact ⟨i, Finset.mem_range.mpr hi, rfl⟩)
(fun i hi => by rw [dif_pos (Finset.mem_range.mp hi)])
· intro s _ hs
have hnot : ¬ 0 < dpPaths P B g hg l s := fun h => hs (List.mem_toFinset.mpr ((hiff s).mpr h))
have h0 : dpPaths P B g hg l s = 0 := by omega
simp [h0]
lemma layerPrefix_le_dpPaths {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) (l : ℕ)
{curKeys : List (DPState n)} (hnd : curKeys.Nodup)
(hiff : ∀ s, s ∈ curKeys ↔ 0 < dpPaths P B g hg l s) (idx : ℕ) (t : DPState n) :
layerPrefix P B g hg l curKeys idx t ≤ dpPaths P B g hg (l + 1) t := by
rw [← layerPrefix_full P B g hg l hnd hiff t]
unfold layerPrefix
apply Finset.sum_le_sum_of_subset
intro i hi
simp only [Finset.mem_range] at hi ⊢
omega
/-- `dpEdge = antichainSumPrefix (n+1) + coreSumPrefix n`. -/
lemma dpEdge_eq_antichain_add_core {n : ℕ} (P : PartialOrder (Fin n)) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (hg : Compatible P B g) (s t : DPState n) :
dpEdge P B g hg s t = antichainSumPrefix s (n + 1) t + coreSumPrefix P B g hg s n t := by
rw [antichainSumPrefix_full, coreSumPrefix_full]
rfl
/-- Frame preserved by `progDPRun` (everything needed by the outer DFS loop). -/
structure DPFrame (n : ℕ) (m0 m : ℕ → ℕ) : Prop where
reg_low : ∀ r, r ≤ 19 → r ≠ rAns → r ≠ rHeap → m r = m0 r
reg_2N : m r2N = m0 r2N
mem_low : ∀ a, 64 ≤ a → a < baseKey n → m a = m0 a
lemma dpFrame_refl {n : ℕ} {m : ℕ → ℕ} : DPFrame n m m :=
⟨fun _ _ _ _ => rfl, rfl, fun _ _ _ => rfl⟩
lemma dpFrame_trans {n : ℕ} {m0 m1 m2 : ℕ → ℕ}
(h1 : DPFrame n m0 m1) (h2 : DPFrame n m1 m2) : DPFrame n m0 m2 :=
⟨fun r hle ha hh => (h2.reg_low r hle ha hh).trans (h1.reg_low r hle ha hh),
h2.reg_2N.trans h1.reg_2N,
fun a h64 hbk => (h2.mem_low a h64 hbk).trans (h1.mem_low a h64 hbk)⟩
lemma dpFrame_of_transFrame {n heapStart : ℕ} {m0 m : ℕ → ℕ}
(hhs : heapBase n ≤ heapStart) (h : TransFrame n heapStart m0 m) : DPFrame n m0 m :=
⟨fun r hle ha hh => h.1 r (by omega) hh (by have : rNxtHead = 23 := rfl; omega),
h.2.1 r2N (by decide),
fun a h64 hbk => h.2.2 a h64 (by unfold baseKey heapBase at *; omega) (Or.inl hbk)⟩
/-! ## Section 2: `hoare_dpInit` (Parts 1-3 of `progDPRun`) -/
/-- Number of initially released elements of `B` with pattern value `c` and index `< k`. -/
private noncomputable def initRelPrefix {n : ℕ} (R : Fin n → Fin n → Bool) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (k : ℕ) (c : Option (Fin n)) : ℕ :=
(B.filter (fun v => v.val < k ∧ g v = c ∧ ∀ z : Fin n, z ∉ B → R z v = false)).card
private lemma initRelPrefix_zero {n : ℕ} (R : Fin n → Fin n → Bool) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (c : Option (Fin n)) :
initRelPrefix R B g 0 c = 0 := by simp [initRelPrefix]
private lemma initRelPrefix_succ {n : ℕ} (R : Fin n → Fin n → Bool) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) {k : ℕ} (hk : k < n) (c : Option (Fin n)) :
initRelPrefix R B g (k + 1) c =
initRelPrefix R B g k c +
if (⟨k, hk⟩ : Fin n) ∈ B ∧ g ⟨k, hk⟩ = c ∧ ∀ z : Fin n, z ∉ B → R z ⟨k, hk⟩ = false then 1 else 0 := by
unfold initRelPrefix
let vf : Fin n := ⟨k, hk⟩
let S := B.filter (fun v => v.val < k ∧ g v = c ∧ ∀ z : Fin n, z ∉ B → R z v = false)
let S' := B.filter (fun v => v.val < k + 1 ∧ g v = c ∧ ∀ z : Fin n, z ∉ B → R z v = false)
have hnot : vf ∉ S := by simp [S, vf]
by_cases hcond : vf ∈ B ∧ g vf = c ∧ ∀ z : Fin n, z ∉ B → R z vf = false
· have heq : S' = insert vf S := by
ext v; simp only [S', S, Finset.mem_filter, Finset.mem_insert]
constructor
· rintro ⟨hvB, hlt, hgv, hall⟩
by_cases hvk : v.val < k
· exact Or.inr ⟨hvB, hvk, hgv, hall⟩
· exact Or.inl (Fin.ext (by dsimp [vf]; omega))
· rintro (rfl | ⟨hvB, hlt, hgv, hall⟩)
· exact ⟨hcond.1, by dsimp [vf]; omega, hcond.2.1, hcond.2.2⟩
· exact ⟨hvB, by omega, hgv, hall⟩
rw [show B.filter _ = S' from rfl, heq, Finset.card_insert_of_notMem hnot, if_pos hcond]
· have heq : S' = S := by
ext v; simp only [S', S, Finset.mem_filter]
constructor
· rintro ⟨hvB, hlt, hgv, hall⟩
have hvk : v.val < k := by
by_contra hge; have hv_eq : v = vf := Fin.ext (by dsimp [vf]; omega)
subst hv_eq; exact hcond ⟨hvB, hgv, hall⟩
exact ⟨hvB, hvk, hgv, hall⟩
· rintro ⟨hvB, hlt, hgv, hall⟩; exact ⟨hvB, by omega, hgv, hall⟩
rw [show B.filter _ = S' from rfl, heq, if_neg hcond, Nat.add_zero]
private lemma initRelPrefix_full {n : ℕ} {P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
(hR : ∀ u v, R u v = true ↔ P.lt u v) (B : Finset (Fin n)) (g : Fin n → Option (Fin n))
(c : Option (Fin n)) :
initRelPrefix R B g n c = released P B g ∅ c := by
unfold initRelPrefix released
congr 1; ext v
simp only [Finset.mem_filter, v.isLt, true_and]
constructor <;> rintro ⟨hvB, hgv, hall⟩ <;> refine ⟨hvB, hgv, ?_⟩
· intro z hz
simp only [predecessors, core, Finset.mem_filter, Finset.mem_sdiff, Finset.mem_univ, true_and] at hz
have hfalse := hall z hz.1
have htrue := (hR z v).mpr hz.2
rw [hfalse] at htrue; cases htrue
· intro z hzB
by_contra hne
have htrue : R z v = true := by
by_cases h : R z v = true
· exact h
· exact absurd (Bool.eq_false_iff.2 h) hne
have hlt := (hR z v).mp htrue
have hmem : z ∈ predecessors P B v := by
simp [predecessors, core, hzB, hlt]
exact Finset.notMem_empty z (hall hmem)
private lemma initRelPrefix_le_n {n : ℕ} (R : Fin n → Fin n → Bool) (B : Finset (Fin n))
(g : Fin n → Option (Fin n)) (k : ℕ) (c : Option (Fin n)) :
initRelPrefix R B g k c ≤ n := by
unfold initRelPrefix
exact (Finset.card_filter_le _ _).trans (by simpa using Finset.card_le_univ B)
private lemma contains_seq3_assoc_run {P : List Instr} {base : ℕ} {a b c d : Stmt}
(h : Contains P base (a.seq (b.seq (c.seq d)))) : Contains P base ((a.seq (b.seq c)).seq d) := by
intro k hk
have h1 := h k (by simp [Stmt.len] at hk ⊢; omega)
simpa [Stmt.compile, Stmt.len, List.append_assoc, Nat.add_assoc] using h1
private def dpInitInnerZStmt : Stmt :=
.seqs [
.const rT0 1,
.mov rZ rT1 rBaseR,
.forRange rZ r1 rJ rJRem rN
(.seqs [
.arrayLoad rT2 rBaseB rJ rT3,
.ifZero rZ rT2
(.seqs [
.arrayLoad rT2 rT1 rI rT3,
.ifNz rZ rT2 (.const rT0 0) .nop
])
.nop,
.add rT1 rT1 rN
])
]
private lemma dpInitInnerZStmt_len : dpInitInnerZStmt.len = 18 := by decide
/-- Inner `z`-loop of `progDPRun` initialization: sets `rT0 := 1` iff `∀ z ∉ B, R z v = false`. -/
private lemma hoare_dpInitInnerZ {P : List Instr} {base n : ℕ}
{R : Fin n → Fin n → Bool} {B : Finset (Fin n)} {v : Fin n} {m0 : ℕ → ℕ}
(hbl : base + 25 ≤ valBound n) :
Hoare P base (valBound n) dpInitInnerZStmt
(fun m => StaticInv n R B m ∧ m rI = v.val ∧ ∀ a, m a = m0 a)
(fun m => StaticInv n R B m ∧
m rT0 = (if ∀ z : Fin n, z ∉ B → R z v = false then 1 else 0) ∧
(∀ a, a ≠ rJ → a ≠ rJRem → a ≠ rT0 → a ≠ rT1 → a ≠ rT2 → a ≠ rT3 → m a = m0 a))
(16 * n + 10) := by
have hB1000 := valBound_ge_1000 n
have hrN : rN = 0 := rfl; have hrZ : rZ = 1 := rfl; have hr1 : r1 = 2 := rfl
have hrBR : rBaseR = 8 := rfl; have hrBB : rBaseB = 9 := rfl; have hrI : rI = 30 := rfl
have hrJ : rJ = 32 := rfl; have hrJRem : rJRem = 33 := rfl
have hrT0 : rT0 = 36 := rfl; have hrT1 : rT1 = 37 := rfl
have hrT2 : rT2 = 38 := rfl; have hrT3 : rT3 = 39 := rfl; have hr2N : r2N = 54 := rfl
have hn_le : n ≤ valBound n := by have := valBound_ge_heapBase n; unfold heapBase at this; omega
have hbr_ge : 64 ≤ baseR n := by unfold baseR; omega
have hbb_ge : 64 ≤ baseB n := by unfold baseB; omega
intro hc m ⟨hSt, hmI, hm0⟩
simp only [dpInitInnerZStmt, Stmt.seqs] at hc
have e1 := Exec.const (P := P) (B := valBound n) (m := m) hc.seq_left (by omega) (by omega)
let m1 := Function.update m rT0 1
have hm1B : MemB n m1 := memB_update hSt.memB (by omega)
have e2 := Exec.mov (P := P) (B := valBound n) (m := m1) hc.seq_right.seq_left
(by omega) (by omega) (by omega) (by simp [m1, hSt.rZ_eq])
(by simp [m1, hSt.rBR_eq]; have := baseR_add_nn_le n; omega)
rw [show m1 rBaseR = baseR n by simp [m1, hSt.rBR_eq]] at e2
let m2 := Function.update m1 rT1 (baseR n)
have hm2B : MemB n m2 := memB_update hm1B (by have := baseR_add_nn_le n; omega)
have hfor := Hoare.forRange (P := P) (base := base + 2) (rZ := rZ) (r1 := r1)
(idx := rJ) (rem := rJRem) (lim := rN) (B := valBound n) (N := n) (stepCap := 12)
(body := .seqs [
.arrayLoad rT2 rBaseB rJ rT3,
.ifZero rZ rT2
(.seqs [
.arrayLoad rT2 rT1 rI rT3,
.ifNz rZ rT2 (.const rT0 0) .nop
])
.nop,
.add rT1 rT1 rN
])
(by omega) (by omega) (by omega) (by omega) (by omega) (by omega) hn_le
(by simp [Stmt.seqs, Stmt.len]; omega)
(by decide) (by decide) (by decide) (by decide) (by decide) (by decide)
(Inv := fun z mb => MemB n mb ∧ mb rT1 = baseR n + z * n ∧
mb rT0 = (if ∀ u : Fin n, u.val < z → u ∉ B → R u v = false then 1 else 0) ∧
(∀ a, a ≠ rJ → a ≠ rJRem → a ≠ rT0 → a ≠ rT1 → a ≠ rT2 → a ≠ rT3 → mb a = m0 a))
(hInv_init := fun mb ⟨hB, h1, h0, hfr⟩ _ _ _ =>
⟨memB_update (memB_update hB (by omega)) hn_le, by simp [h1], by simp [h0],
fun a ha1 ha2 ha3 ha4 ha5 ha6 => by rw [Function.update_of_ne ha2, Function.update_of_ne ha1, hfr a ha1 ha2 ha3 ha4 ha5 ha6]⟩)
(hInv_step := fun z hz mb ⟨hB, h1, h0, hfr⟩ _ _ _ _ =>
⟨memB_update (memB_update hB (by omega)) (by omega), by simp [h1], by simp [h0],
fun a ha1 ha2 ha3 ha4 ha5 ha6 => by rw [Function.update_of_ne ha2, Function.update_of_ne ha1, hfr a ha1 ha2 ha3 ha4 ha5 ha6]⟩)
(hbody := fun z hz hcb mb ⟨⟨hB, h1, h0, hfr⟩, hmJ, hmJR, hmZ, hm1⟩ => by
simp only [Stmt.seqs] at hcb
let zf : Fin n := ⟨z, hz⟩
have hmN : mb rN = n := by rw [hfr _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), ← hm0, hSt.rN_eq]
have hmBB : mb rBaseB = baseB n := by rw [hfr _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), ← hm0, hSt.rBB_eq]
have hmI' : mb rI = v.val := by rw [hfr _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), ← hm0, hmI]
have hbb_le : baseB n + z ≤ valBound n := by have := baseB_add_n_le n; omega
have hrzv_le : baseR n + z * n + v.val ≤ valBound n := by
have := baseR_add_nn_le n; have : z * n + v.val < n * n := by nlinarith [v.isLt]
omega
have hzn1_le : baseR n + (z + 1) * n ≤ valBound n := by
have := baseR_add_nn_le n; have : (z + 1) * n ≤ n * n := Nat.mul_le_mul_right n (by omega)
omega
have eb1 := Exec.arrayLoad (P := P) (B := valBound n) (m := mb) hcb.seq_left
(by omega) (by omega) (by omega) (by omega) (by rw [hmBB]; have := baseB_add_n_le n; omega)
(by rw [hmJ]; omega) (by rw [hmBB, hmJ]; exact hbb_le) (hB _) (by rw [hmBB, hmJ]; omega)
rw [hmBB, hmJ, hfr (baseB n + z) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), ← hm0, hSt.B_ind zf] at eb1
let mb1 := Function.update (Function.update mb rT3 (baseB n + z)) rT2 (if zf ∈ B then 1 else 0)
have hmb1B : MemB n mb1 := memB_update (memB_update hB hbb_le) (by split_ifs <;> omega)
have hmb1_eq : ∀ a, a ≠ rT2 → a ≠ rT3 → mb1 a = mb a := fun a h2 h3 => by
dsimp only [mb1]; rw [Function.update_of_ne h2, Function.update_of_ne h3]
have hc_if1 := hcb.seq_right.seq_left
have hbr : ∃ m_br t_br, t_br ≤ 9 ∧
Exec P (base + 2 + 3 + 2)
(Stmt.ifZero rZ rT2 (.seqs [.arrayLoad rT2 rT1 rI rT3, .ifNz rZ rT2 (.const rT0 0) .nop]) .nop).len
(valBound n) mb1 m_br t_br ∧
MemB n m_br ∧
m_br rT0 = (if ∀ u : Fin n, u.val < z + 1 → u ∉ B → R u v = false then 1 else 0) ∧
(∀ a, a ≠ rT0 → a ≠ rT2 → a ≠ rT3 → m_br a = mb a) := by
by_cases hzB : zf ∈ B
· have hif1 := Exec.ifZero_false (B := valBound n) (m := mb1) (m' := mb1) hc_if1
(by omega) (by omega) (by simp [Stmt.seqs, Stmt.len]; omega)
(by simp [mb1, hzB]) (hmb1B _) (by rw [hmb1_eq _ (by omega) (by omega), hmZ]) Exec.nop
have hiff : (∀ u : Fin n, u.val < z + 1 → u ∉ B → R u v = false) ↔
(∀ u : Fin n, u.val < z → u ∉ B → R u v = false) := by
constructor
· intro h u hu; exact h u (by omega)
· intro h u hu huB
by_cases hlt : u.val < z
· exact h u hlt huB
· exact absurd ((show u = zf from Fin.ext (by dsimp [zf]; omega)) ▸ hzB) huB
exact ⟨mb1, 1 + 0 + 1, by omega, hif1, hmb1B,
by rw [hmb1_eq _ (by omega) (by omega), h0, if_congr hiff rfl rfl],
fun a _ h2 h3 => hmb1_eq a h2 h3⟩
· have hc_th1 := hc_if1.ifZero_then
have eb2 := Exec.arrayLoad (P := P) (B := valBound n) (m := mb1) hc_th1.seq_left
(by omega) (by omega) (by omega) (by omega)
(by rw [hmb1_eq _ (by omega) (by omega), h1]; omega)
(by rw [hmb1_eq _ (by omega) (by omega), hmI']; omega)
(by rw [hmb1_eq _ (by omega) (by omega), h1, hmb1_eq _ (by omega) (by omega), hmI']; exact hrzv_le)
(hmb1B _) (by rw [hmb1_eq _ (by omega) (by omega), h1, hmb1_eq _ (by omega) (by omega), hmI']; omega)
rw [hmb1_eq rT1 (by omega) (by omega), h1, hmb1_eq rI (by omega) (by omega), hmI',
hmb1_eq (baseR n + z * n + v.val) (by omega) (by omega),
hfr (baseR n + z * n + v.val) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega),
← hm0, hSt.R_mat zf v] at eb2
let mb2 := Function.update (Function.update mb1 rT3 (baseR n + z * n + v.val)) rT2 (if R zf v then 1 else 0)
have hmb2B : MemB n mb2 := memB_update (memB_update hmb1B hrzv_le) (by split_ifs <;> omega)
have hmb2_eq : ∀ a, a ≠ rT2 → a ≠ rT3 → mb2 a = mb a := fun a h2 h3 => by
dsimp only [mb2]; rw [Function.update_of_ne h2, Function.update_of_ne h3, hmb1_eq a h2 h3]
have hc_if2 := hc_th1.seq_right
by_cases hRzv : R zf v = true
· have eb3 := Exec.const (P := P) (B := valBound n) (m := mb2) hc_if2.ifZero_else (by omega) (by omega)
let mb3 := Function.update mb2 rT0 0
have hmb3B : MemB n mb3 := memB_update hmb2B (by omega)
have hif2 := Exec.ifZero_false (B := valBound n) (m := mb2) (m' := mb3) hc_if2
(by omega) (by omega) (by simp [Stmt.seqs, Stmt.len]; omega) (by simp [mb2, hRzv]) (hmb2B _)
(by dsimp only [mb3]; rw [Function.update_of_ne (by omega), hmb2_eq _ (by omega) (by omega), hmZ]) eb3
have hif1 := Exec.ifZero_true (B := valBound n) (m := mb1) hc_if1
(by omega) (by simp [Stmt.seqs, Stmt.len]; omega) (by simp [mb1, hzB])
(by simpa only [Stmt.seqs, Stmt.len, arrayLoad_len, ifNz_len] using eb2.seq hif2)
have hnot : ¬ (∀ u : Fin n, u.val < z + 1 → u ∉ B → R u v = false) := fun h => by
have := h zf (by dsimp [zf]; omega) hzB; rw [hRzv] at this; cases this
exact ⟨mb3, 1 + (2 + (1 + 1 + 1)), by omega, hif1, hmb3B,
by rw [if_neg hnot]; simp [mb3],
fun a ha0 h2 h3 => by dsimp only [mb3]; rw [Function.update_of_ne ha0, hmb2_eq a h2 h3]⟩
· have hRzvf : R zf v = false := Bool.eq_false_iff.2 hRzv
have hif2 := Exec.ifZero_true (B := valBound n) (m := mb2) hc_if2
(by omega) (by simp [Stmt.seqs, Stmt.len]; omega) (by simp [mb2, hRzvf]) Exec.nop
have hif1 := Exec.ifZero_true (B := valBound n) (m := mb1) hc_if1
(by omega) (by simp [Stmt.seqs, Stmt.len]; omega) (by simp [mb1, hzB])
(by simpa only [Stmt.seqs, Stmt.len, arrayLoad_len, ifNz_len] using eb2.seq hif2)
have hiff : (∀ u : Fin n, u.val < z + 1 → u ∉ B → R u v = false) ↔
(∀ u : Fin n, u.val < z → u ∉ B → R u v = false) := by
constructor
· intro h u hu; exact h u (by omega)
· intro h u hu huB
by_cases hlt : u.val < z
· exact h u hlt huB
· rw [show u = zf from Fin.ext (by dsimp [zf]; omega)]; exact hRzvf
exact ⟨mb2, 1 + (2 + (1 + 0)), by omega, hif1, hmb2B,
by rw [hmb2_eq _ (by omega) (by omega), h0, if_congr hiff rfl rfl],
fun a _ h2 h3 => hmb2_eq a h2 h3⟩
rcases hbr with ⟨m_br, t_br, ht_br, e_br, hmbrB, hmbr0, hmbrFr⟩
have hmbr1 : m_br rT1 = baseR n + z * n := by rw [hmbrFr _ (by omega) (by omega) (by omega), h1]
have hmbrN : m_br rN = n := by rw [hmbrFr _ (by omega) (by omega) (by omega), hmN]
have e_add := Exec.add (P := P) (B := valBound n) (m := m_br) hcb.seq_right.seq_right
(by omega) (by omega) (by omega) (by rw [hmbr1]; omega) (by rw [hmbrN]; omega)
rw [hmbr1, hmbrN] at e_add
let m_end := Function.update m_br rT1 (baseR n + z * n + n)
have hmendB : MemB n m_end := memB_update hmbrB (by have : baseR n + z * n + n = baseR n + (z + 1) * n := (by ring); omega)
exact ⟨m_end, 2 + (t_br + 1), by omega,
by simpa only [Stmt.seqs, Stmt.len, arrayLoad_len] using eb1.seq (e_br.seq e_add),
⟨hmendB, by simp only [m_end, Function.update_self]; ring,
by dsimp only [m_end]; rw [Function.update_of_ne (by omega), hmbr0],
fun a ha1 ha2 ha3 ha4 ha5 ha6 => by dsimp only [m_end]; rw [Function.update_of_ne ha4, hmbrFr a ha3 ha5 ha6, hfr a ha1 ha2 ha3 ha4 ha5 ha6]⟩,
by dsimp only [m_end]; rw [Function.update_of_ne (by omega), hmbrFr _ (by omega) (by omega) (by omega), hmJ],
by dsimp only [m_end]; rw [Function.update_of_ne (by omega), hmbrFr _ (by omega) (by omega) (by omega), hmJR],
by dsimp only [m_end]; rw [Function.update_of_ne (by omega), hmbrFr _ (by omega) (by omega) (by omega), hmZ],
by dsimp only [m_end]; rw [Function.update_of_ne (by omega), hmbrFr _ (by omega) (by omega) (by omega), hm1]⟩)
rcases hfor (by simpa [Stmt.seqs, Stmt.len, Nat.add_assoc] using hc.seq_right.seq_right) m2
⟨⟨hm2B, by simp [m2], by simp [m2, m1],
fun a _ _ ha3 ha4 _ _ => by dsimp only [m2, m1]; rw [Function.update_of_ne ha4, Function.update_of_ne ha3, hm0 a]⟩,
by simp [m2, m1, hSt.rN_eq], by simp [m2, m1, hSt.rZ_eq], by simp [m2, m1, hSt.r1_eq]⟩ with
⟨m_end, t_for, ht_for, e_for, ⟨hmendB, _, hmend0, hmendFr⟩, _, _, _, _⟩
have ht_bound : n * (12 + 4) = 16 * n := by ring
exact ⟨m_end, 1 + (1 + t_for), by omega,
by simpa [dpInitInnerZStmt, Stmt.seqs, Stmt.len, Nat.add_assoc] using e1.seq (e2.seq e_for),
staticInv_frame hSt hmendB
(fun r hr => by rw [hmendFr r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega), ← hm0])
(fun a h1 _ => by rw [hmendFr a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega), ← hm0]),
by simpa [Fin.isLt] using hmend0, hmendFr⟩
private def dpInitLoopStmt : Stmt :=
.forRange rZ r1 rI rIRem rN
(.seqs [
.arrayLoad rT0 rBaseB rI rT1,
.ifNz rZ rT0
(.seqs [
.const rT0 1,
.mov rZ rT1 rBaseR,
.forRange rZ r1 rJ rJRem rN
(.seqs [
.arrayLoad rT2 rBaseB rJ rT3,
.ifZero rZ rT2
(.seqs [
.arrayLoad rT2 rT1 rI rT3,
.ifNz rZ rT2 (.const rT0 0) .nop
])
.nop,
.add rT1 rT1 rN
]),
.ifNz rZ rT0
(.seqs [
.arrayLoad rT1 rBaseG rI rT2,
.add rT2 rBaseKey rN,
.add rT2 rT2 rT1,
.load rT3 rT2,
.inc r1 rT3,
.store rT2 rT3
])
.nop
])
.nop
])
private lemma dpInitLoopStmt_len : dpInitLoopStmt.len = 37 := by decide
/-- Outer `v`-loop of `progDPRun` initialization: populates `tmpKey[n + c] = initRelPrefix R B g n (optOfNat n c)`. -/
private lemma hoare_dpInitLoop {P : List Instr} {base n : ℕ}
{R : Fin n → Fin n → Bool} {B : Finset (Fin n)} {g : Fin n → Option (Fin n)} {m0 : ℕ → ℕ}
(hbl : base + 45 ≤ valBound n) :
Hoare P base (valBound n) dpInitLoopStmt
(fun m => StaticInv n R B m ∧ PatEnc g m ∧
(∀ l < 2 * n + 1, m (baseKey n + l) = 0) ∧ ∀ a, m a = m0 a)
(fun m => StaticInv n R B m ∧ PatEnc g m ∧
(∀ l < n, m (baseKey n + l) = 0) ∧
(∀ c < n + 1, m (baseKey n + n + c) = initRelPrefix R B g n (optOfNat n c)) ∧
m rHeap = m0 rHeap ∧ m rAns = m0 rAns ∧ DPFrame n m0 m)
(16 * n * n + 32 * n + 10) := by
have hB1000 := valBound_ge_1000 n
have hrN : rN = 0 := rfl; have hrZ : rZ = 1 := rfl; have hr1 : r1 = 2 := rfl
have hrAns : rAns = 7 := rfl; have hrH : rHeap = 17 := rfl
have hrBR : rBaseR = 8 := rfl; have hrBB : rBaseB = 9 := rfl; have hrBG : rBaseG = 12 := rfl
have hrBK : rBaseKey = 15 := rfl; have hrI : rI = 30 := rfl; have hrIRem : rIRem = 31 := rfl
have hrJ : rJ = 32 := rfl; have hrJRem : rJRem = 33 := rfl
have hrT0 : rT0 = 36 := rfl; have hrT1 : rT1 = 37 := rfl
have hrT2 : rT2 = 38 := rfl; have hrT3 : rT3 = 39 := rfl; have hr2N : r2N = 54 := rfl
have hn_le : n ≤ valBound n := by have := valBound_ge_heapBase n; unfold heapBase at this; omega
have hbb_ge : 64 ≤ baseB n := by unfold baseB; omega
have hbg_ge : 64 ≤ baseG n := by unfold baseG; omega
have hbk_ge : 64 ≤ baseKey n := by unfold baseKey; omega
have hbk_vb : baseKey n + (2 * n + 1) ≤ valBound n := by
have := valBound_ge_heapBase n; unfold baseKey heapBase at *; omega
have hfor := Hoare.forRange (P := P) (base := base) (rZ := rZ) (r1 := r1)
(idx := rI) (rem := rIRem) (lim := rN) (B := valBound n) (N := n)
(stepCap := 16 * n + 28)
(body := .seqs [
.arrayLoad rT0 rBaseB rI rT1,
.ifNz rZ rT0
(.seqs [
.const rT0 1,
.mov rZ rT1 rBaseR,
.forRange rZ r1 rJ rJRem rN
(.seqs [
.arrayLoad rT2 rBaseB rJ rT3,
.ifZero rZ rT2
(.seqs [
.arrayLoad rT2 rT1 rI rT3,
.ifNz rZ rT2 (.const rT0 0) .nop
])
.nop,
.add rT1 rT1 rN
]),
.ifNz rZ rT0
(.seqs [
.arrayLoad rT1 rBaseG rI rT2,
.add rT2 rBaseKey rN,
.add rT2 rT2 rT1,
.load rT3 rT2,
.inc r1 rT3,
.store rT2 rT3
])
.nop
])
.nop
])
(by omega) (by omega) (by omega) (by omega) (by omega) (by omega) hn_le
(by simp [Stmt.inc, Stmt.seqs, Stmt.len]; omega)
(by decide) (by decide) (by decide) (by decide) (by decide) (by decide)
(Inv := fun v m => StaticInv n R B m ∧ PatEnc g m ∧
(∀ l < n, m (baseKey n + l) = 0) ∧
(∀ c < n + 1, m (baseKey n + n + c) = initRelPrefix R B g v (optOfNat n c)) ∧
m rHeap = m0 rHeap ∧ m rAns = m0 rAns ∧ DPFrame n m0 m)
(hInv_init := fun m ⟨hSt, hPat, hI0, hq, hmH, hmA, hfr⟩ _ _ _ => by
have hmB' : MemB n (Function.update (Function.update m rI 0) rIRem n) :=
memB_update (memB_update hSt.memB (by omega)) hn_le
have hheq : ∀ a, a ≠ rI → a ≠ rIRem → Function.update (Function.update m rI 0) rIRem n a = m a :=
fun a h1 h2 => by rw [Function.update_of_ne h2, Function.update_of_ne h1]
exact ⟨staticInv_frame hSt hmB' (fun r hr => hheq r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 _ => hheq a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega)),
fun u => by rw [hheq _ (by omega) (by omega)]; exact hPat u,
fun l hl => by rw [hheq _ (by omega) (by omega)]; exact hI0 l hl,
fun c hc => by rw [hheq _ (by omega) (by omega)]; exact hq c hc,
by rw [hheq _ (by omega) (by omega), hmH], by rw [hheq _ (by omega) (by omega), hmA],
dpFrame_trans hfr ⟨fun r hle _ _ => hheq r (by omega) (by omega), hheq _ (by omega) (by omega),
fun a h64 _ => hheq a (by omega) (by omega)⟩⟩)
(hInv_step := fun v hv m ⟨hSt, hPat, hI0, hq, hmH, hmA, hfr⟩ _ _ _ _ => by
have hmB' : MemB n (Function.update (Function.update m rI (v + 1)) rIRem (n - v - 1)) :=
memB_update (memB_update hSt.memB (by omega)) (by omega)
have hheq : ∀ a, a ≠ rI → a ≠ rIRem → Function.update (Function.update m rI (v + 1)) rIRem (n - v - 1) a = m a :=
fun a h1 h2 => by rw [Function.update_of_ne h2, Function.update_of_ne h1]
exact ⟨staticInv_frame hSt hmB' (fun r hr => hheq r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 _ => hheq a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega)),
fun u => by rw [hheq _ (by omega) (by omega)]; exact hPat u,
fun l hl => by rw [hheq _ (by omega) (by omega)]; exact hI0 l hl,
fun c hc => by rw [hheq _ (by omega) (by omega)]; exact hq c hc,
by rw [hheq _ (by omega) (by omega), hmH], by rw [hheq _ (by omega) (by omega), hmA],
dpFrame_trans hfr ⟨fun r hle _ _ => hheq r (by omega) (by omega), hheq _ (by omega) (by omega),
fun a h64 _ => hheq a (by omega) (by omega)⟩⟩)
(hbody := fun v hv hc_body m ⟨⟨hSt, hPat, hI0, hq, hmH, hmA, hfr⟩, hmI, hmIR, hmZ, hm1⟩ => by
simp only [Stmt.seqs] at hc_body
let vf : Fin n := ⟨v, hv⟩
have hbb_le : baseB n + v ≤ valBound n := by have := baseB_add_n_le n; omega
have eb1 := Exec.arrayLoad (P := P) (B := valBound n) (m := m) hc_body.seq_left
(by omega) (by omega) (by omega) (by omega) (by rw [hSt.rBB_eq]; have := baseB_add_n_le n; omega)
(by rw [hmI]; omega) (by rw [hSt.rBB_eq, hmI]; exact hbb_le) (hSt.memB _) (by rw [hSt.rBB_eq, hmI]; omega)
rw [hSt.rBB_eq, hmI, hSt.B_ind vf] at eb1
let mb1 := Function.update (Function.update m rT1 (baseB n + v)) rT0 (if vf ∈ B then 1 else 0)
have hmb1B : MemB n mb1 := memB_update (memB_update hSt.memB hbb_le) (by split_ifs <;> omega)
have hmb1_eq : ∀ a, a ≠ rT0 → a ≠ rT1 → mb1 a = m a := fun a h0 h1 => by
dsimp only [mb1]; rw [Function.update_of_ne h0, Function.update_of_ne h1]
have hc_if1 := hc_body.seq_right
by_cases hvB : vf ∈ B
· have hc_th1 := hc_if1.ifZero_else
have hc_reassoc := contains_seq3_assoc_run hc_th1
have hSt1 : StaticInv n R B mb1 := staticInv_frame hSt hmb1B
(fun r hr => hmb1_eq r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 _ => hmb1_eq a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega))
have hiz := hoare_dpInitInnerZ (P := P) (base := base + 3 + 2 + 1) (n := n) (R := R) (B := B) (v := vf) (m0 := mb1) (by omega)
rcases hiz (by simpa [dpInitInnerZStmt, Stmt.seqs, Stmt.len, Nat.add_assoc] using hc_reassoc.seq_left) mb1
⟨hSt1, by rw [hmb1_eq _ (by omega) (by omega), hmI], fun _ => rfl⟩ with
⟨mb2, t_iz, ht_iz, e_iz, hSt2, hmb2_0, hmb2Fr⟩
have hmb2_eq : ∀ a, a ≠ rJ → a ≠ rJRem → a ≠ rT0 → a ≠ rT1 → a ≠ rT2 → a ≠ rT3 → mb2 a = m a :=
fun a h1 h2 h3 h4 h5 h6 => by rw [hmb2Fr a h1 h2 h3 h4 h5 h6, hmb1_eq a h3 h4]
have hc_if2 := hc_reassoc.seq_right
by_cases hallz : ∀ z : Fin n, z ∉ B → R z vf = false
· have hc_th2 := hc_if2.ifZero_else
let cg := match g vf with | none => n | some u => u.val
have hcg_lt : cg < n + 1 := by
change (match g vf with | none => n | some u => u.val) < n + 1
cases g vf with | none => exact Nat.lt_succ_self n | some u => exact Nat.lt_succ_of_lt u.isLt
have hopt_cg : optOfNat n cg = g vf := by
cases hgv : g vf with | none => simp [cg, hgv, optOfNat] | some u => simp [cg, hgv, optOfNat, u.isLt]
have hbg_le : baseG n + v ≤ valBound n := by have := baseG_add_n_le n; omega
have hmb2_I : mb2 rI = v := by rw [hmb2_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmI]
have ec1 := Exec.arrayLoad (P := P) (B := valBound n) (m := mb2) hc_th2.seq_left
(by omega) (by omega) (by omega) (by omega) (by rw [hSt2.rBG_eq]; have := baseG_add_n_le n; omega)
(by rw [hmb2_I]; omega) (by rw [hSt2.rBG_eq, hmb2_I]; exact hbg_le) (hSt2.memB _) (by rw [hSt2.rBG_eq, hmb2_I]; omega)
rw [hSt2.rBG_eq, hmb2_I, hmb2_eq (baseG n + v) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hPat vf] at ec1
let mc1 := Function.update (Function.update mb2 rT2 (baseG n + v)) rT1 cg
have hmc1B : MemB n mc1 := memB_update (memB_update hSt2.memB hbg_le) (by omega)
have ec2 := Exec.add (P := P) (B := valBound n) (m := mc1) hc_th2.seq_right.seq_left
(by omega) (by omega) (by omega) (by simp [mc1, hSt2.rBK_eq]; omega) (by simp [mc1, hSt2.rN_eq]; omega)
rw [show mc1 rBaseKey = baseKey n by simp [mc1, hSt2.rBK_eq], show mc1 rN = n by simp [mc1, hSt2.rN_eq]] at ec2
let mc2 := Function.update mc1 rT2 (baseKey n + n)
have hmc2B : MemB n mc2 := memB_update hmc1B (by omega)
have ec3 := Exec.add (P := P) (B := valBound n) (m := mc2) hc_th2.seq_right.seq_right.seq_left
(by omega) (by omega) (by omega) (by simp [mc2]; omega) (by simp [mc2, mc1]; omega)
rw [show mc2 rT2 = baseKey n + n by simp [mc2], show mc2 rT1 = cg by simp [mc2, mc1]] at ec3
let mc3 := Function.update mc2 rT2 (baseKey n + n + cg)
have hmc3B : MemB n mc3 := memB_update hmc2B (by omega)
have hcur_val : mc3 (baseKey n + n + cg) = initRelPrefix R B g v (g vf) := by
dsimp only [mc3, mc2, mc1]
rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_of_ne (by omega),
hmb2_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hq cg hcg_lt, hopt_cg]
have hval_le : initRelPrefix R B g v (g vf) + 1 ≤ n := by
have hsucc := initRelPrefix_succ R B g hv (g vf)
have hcond : (⟨v, hv⟩ : Fin n) ∈ B ∧ g ⟨v, hv⟩ = g vf ∧ ∀ z : Fin n, z ∉ B → R z ⟨v, hv⟩ = false := ⟨hvB, rfl, hallz⟩
rw [if_pos hcond] at hsucc
have := initRelPrefix_le_n R B g (v + 1) (g vf)
omega
have ec4 := Exec.load (P := P) (B := valBound n) (m := mc3) hc_th2.seq_right.seq_right.seq_right.seq_left
(by omega) (by omega) (by simp [mc3]; omega) (by rw [show mc3 rT2 = baseKey n + n + cg by simp [mc3], hcur_val]; omega)
rw [show mc3 rT2 = baseKey n + n + cg by simp [mc3], hcur_val] at ec4
let mc4 := Function.update mc3 rT3 (initRelPrefix R B g v (g vf))
have hmc4B : MemB n mc4 := memB_update hmc3B (by omega)
have hc_c5 := hc_th2.seq_right.seq_right.seq_right.seq_right
have ec5 := Exec.add (P := P) (B := valBound n) (m := mc4) (by simpa [Stmt.inc] using hc_c5.seq_left)
(by omega) (by omega) (by omega) (by simp [mc4]; omega) (by simp [mc4, mc3, mc2, mc1, hSt2.r1_eq]; omega)
rw [show mc4 rT3 = initRelPrefix R B g v (g vf) by simp [mc4],
show mc4 r1 = 1 by simp [mc4, mc3, mc2, mc1, hSt2.r1_eq]] at ec5
let mc5 := Function.update mc4 rT3 (initRelPrefix R B g v (g vf) + 1)
have hmc5B : MemB n mc5 := memB_update hmc4B (by omega)
have ec6 := Exec.store (P := P) (B := valBound n) (m := mc5) (by simpa [Stmt.inc] using hc_c5.seq_right)
(by omega) (by omega) (by simp [mc5, mc4, mc3]; omega) (by simp [mc5]; omega)
rw [show mc5 rT2 = baseKey n + n + cg by simp [mc5, mc4, mc3],
show mc5 rT3 = initRelPrefix R B g v (g vf) + 1 by simp [mc5]] at ec6
let m_end := Function.update mc5 (baseKey n + n + cg) (initRelPrefix R B g v (g vf) + 1)
have hmendB : MemB n m_end := memB_update hmc5B (by omega)
have hpres : ∀ a, a ≠ rJ → a ≠ rJRem → a ≠ rT0 → a ≠ rT1 → a ≠ rT2 → a ≠ rT3 →
a ≠ baseKey n + n + cg → m_end a = m a := fun a h1 h2 h3 h4 h5 h6 h7 => by
dsimp only [m_end, mc5, mc4, mc3, mc2, mc1]
rw [Function.update_of_ne h7, Function.update_of_ne h6, Function.update_of_ne h6, Function.update_of_ne h5,
Function.update_of_ne h5, Function.update_of_ne h4, Function.update_of_ne h5, hmb2_eq a h1 h2 h3 h4 h5 h6]
have hif2 := Exec.ifZero_false (B := valBound n) (m := mb2) (m' := m_end) hc_if2
(by omega) (by omega) (by simp [dpInitInnerZStmt, dpInitInnerZStmt_len, Stmt.inc, Stmt.len, arrayLoad_len]; omega)
(by rw [hmb2_0, if_pos hallz]; omega) (hSt2.memB _)
(by rw [hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmZ])
(by simpa only [Stmt.seqs, Stmt.len, arrayLoad_len, inc_len] using ec1.seq (ec2.seq (ec3.seq (ec4.seq (ec5.seq ec6)))))
have hif1 := Exec.ifZero_false (B := valBound n) (m := mb1) (m' := m_end) hc_if1
(by omega) (by omega) (by simp [dpInitInnerZStmt, dpInitInnerZStmt_len, Stmt.inc, Stmt.len, arrayLoad_len]; omega)
(by simp [mb1, hvB]) (hmb1B _)
(by rw [hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmZ])
(by simpa only [dpInitInnerZStmt, dpInitInnerZStmt_len, Stmt.seqs, Stmt.len, arrayLoad_len, ifNz_len, Nat.add_assoc] using e_iz.seq hif2)
refine ⟨m_end, 2 + (1 + (t_iz + (1 + (2 + (1 + (1 + (1 + (1 + 1))))) + 1)) + 1), by omega,
by simpa only [Stmt.seqs, Stmt.len, arrayLoad_len, ifNz_len] using eb1.seq hif1,
⟨staticInv_frame hSt hmendB
(fun r hr => hpres r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 h2 => hpres a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseAct at h2; unfold baseKey; have : 2 * n * n = n * n + n * n := (by ring); omega)),
fun u => by rw [hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by unfold baseG baseKey; have := u.isLt; have : 2 * n * n = n * n + n * n := (by ring); omega)]; exact hPat u,
fun l hl => by rw [hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)]; exact hI0 l hl,
fun c hc => by
rw [initRelPrefix_succ R B g hv (optOfNat n c)]
by_cases hceq : c = cg
· subst hceq
have hcond : (⟨v, hv⟩ : Fin n) ∈ B ∧ g ⟨v, hv⟩ = optOfNat n cg ∧ ∀ z : Fin n, z ∉ B → R z ⟨v, hv⟩ = false :=
⟨hvB, hopt_cg.symm, hallz⟩
rw [if_pos hcond]; simp only [m_end, Function.update_self, hopt_cg]
· have hne_opt : g ⟨v, hv⟩ ≠ optOfNat n c := fun h => hceq (optOfNat_inj hc hcg_lt (h.symm.trans hopt_cg.symm))
rw [if_neg (fun h => hne_opt h.2.1), hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hq c hc]; omega,
by rw [hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmH],
by rw [hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmA],
dpFrame_trans hfr ⟨fun r hle _ _ => hpres r (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega),
hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega),
fun a h64 hbk => hpres a (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)⟩⟩,
by rw [hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmI],
by rw [hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmIR],
by rw [hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmZ],
by rw [hpres _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hm1]⟩
· have hif2 := Exec.ifZero_true (B := valBound n) (m := mb2) hc_if2
(by omega) (by simp [dpInitInnerZStmt, dpInitInnerZStmt_len, Stmt.inc, Stmt.len, arrayLoad_len]; omega)
(by rw [hmb2_0, if_neg hallz]) Exec.nop
have hif1 := Exec.ifZero_false (B := valBound n) (m := mb1) (m' := mb2) hc_if1
(by omega) (by omega) (by simp [dpInitInnerZStmt, dpInitInnerZStmt_len, Stmt.inc, Stmt.len, arrayLoad_len]; omega)
(by simp [mb1, hvB]) (hmb1B _) hSt2.rZ_eq
(by simpa only [dpInitInnerZStmt, dpInitInnerZStmt_len, Stmt.seqs, Stmt.len, arrayLoad_len, ifNz_len, Nat.add_assoc] using e_iz.seq hif2)
refine ⟨mb2, 2 + (1 + (t_iz + (1 + 0)) + 1), by omega,
by simpa only [Stmt.seqs, Stmt.len, arrayLoad_len, ifNz_len] using eb1.seq hif1,
⟨hSt2, fun u => by rw [hmb2_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)]; exact hPat u,
fun l hl => by rw [hmb2_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)]; exact hI0 l hl,
fun c hc => by rw [initRelPrefix_succ R B g hv (optOfNat n c), if_neg (fun h => hallz h.2.2), hmb2_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hq c hc]; omega,
by rw [hmb2_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmH],
by rw [hmb2_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmA],
dpFrame_trans hfr ⟨fun r hle _ _ => hmb2_eq r (by omega) (by omega) (by omega) (by omega) (by omega) (by omega),
hmb2_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega),
fun a h64 _ => hmb2_eq a (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)⟩⟩,
by rw [hmb2_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmI],
by rw [hmb2_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmIR],
hSt2.rZ_eq, hSt2.r1_eq⟩
· have hif1 := Exec.ifZero_true (B := valBound n) (m := mb1) hc_if1
(by omega) (by simp [dpInitInnerZStmt, dpInitInnerZStmt_len, Stmt.inc, Stmt.len, arrayLoad_len]; omega)
(by simp [mb1, hvB]) Exec.nop
refine ⟨mb1, 2 + (1 + 0), by omega,
by simpa only [Stmt.seqs, Stmt.len, arrayLoad_len, ifNz_len] using eb1.seq hif1,
⟨staticInv_frame hSt hmb1B (fun r hr => hmb1_eq r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 _ => hmb1_eq a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega)),
fun u => by rw [hmb1_eq _ (by omega) (by omega)]; exact hPat u,
fun l hl => by rw [hmb1_eq _ (by omega) (by omega)]; exact hI0 l hl,
fun c hc => by rw [initRelPrefix_succ R B g hv (optOfNat n c), if_neg (fun h => hvB h.1), hmb1_eq _ (by omega) (by omega), hq c hc]; omega,
by rw [hmb1_eq _ (by omega) (by omega), hmH],
by rw [hmb1_eq _ (by omega) (by omega), hmA],
dpFrame_trans hfr ⟨fun r hle _ _ => hmb1_eq r (by omega) (by omega), hmb1_eq _ (by omega) (by omega),
fun a h64 _ => hmb1_eq a (by omega) (by omega)⟩⟩,
by rw [hmb1_eq _ (by omega) (by omega), hmI], by rw [hmb1_eq _ (by omega) (by omega), hmIR],
by rw [hmb1_eq _ (by omega) (by omega), hmZ], by rw [hmb1_eq _ (by omega) (by omega), hm1]⟩)
exact hfor.weaken
(fun m ⟨hSt, hPat, h0, hm0⟩ =>
⟨⟨hSt, hPat, fun l hl => h0 l (by omega),
fun c hc => by rw [initRelPrefix_zero, show baseKey n + n + c = baseKey n + (n + c) by omega]; exact h0 (n + c) (by omega),
hm0 rHeap, hm0 rAns, ⟨fun r _ _ _ => hm0 r, hm0 r2N, fun a _ _ => hm0 a⟩⟩,
hSt.rN_eq, hSt.rZ_eq, hSt.r1_eq⟩)
(fun m ⟨⟨hSt, hPat, hI0, hq, hmH, hmA, hfr⟩, _, _, _, _⟩ => ⟨hSt, hPat, hI0, hq, hmH, hmA, hfr⟩)
(by have : n * (16 * n + 28 + 4) = 16 * n * n + 32 * n := by ring
omega)
/-! ## Section 3: `hoare_dpInit` (Full Initialization of Layer 0) -/
private def dpInitStmt : Stmt :=
.seqs [
.mov rZ rHeap rHeapBase,
stmtZeroBlock rBaseKey rKeyLen rI rIRem rT0,
dpInitLoopStmt,
.mov rZ rCurHead rHeap,
.store rCurHead rZ,
.add rT0 rCurHead r1,
.store rT0 r1,
.add rT0 rT0 r1,
stmtCopyBlock rBaseKey rT0 rKeyLen rI rIRem rT1 rT2,
.add rHeap rHeap rRecW
]
private lemma dpInitStmt_len : dpInitStmt.len = 62 := by decide
private lemma stateKey_dpInitial {n : ℕ} {P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
(hR : ∀ u v, R u v = true ↔ P.lt u v) (B : Finset (Fin n)) (g : Fin n → Option (Fin n)) :
(∀ l < n, stateKey (dpInitial P B g) l = 0) ∧
(∀ c < n + 1, stateKey (dpInitial P B g) (n + c) = initRelPrefix R B g n (optOfNat n c)) := by
constructor
· intro l hl; unfold stateKey dpInitial; simp [hl]
· intro c hc; rw [stateKey_n_add _ c hc, initRelPrefix_full hR]; rfl
/-- Hoare triple for `dpInitStmt`: initializes layer 0 with `[dpInitial P B g]` at `heapBase n`. -/
private lemma hoare_dpInit {P_prog : List Instr} {base n : ℕ}
{P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
{B : Finset (Fin n)} {g : Fin n → Option (Fin n)} {hg : Compatible P B g} {m0 : ℕ → ℕ}
(hR : ∀ u v, R u v = true ↔ P.lt u v)
(hbl : base + 70 ≤ valBound n) :
Hoare P_prog base (valBound n) dpInitStmt
(fun m => StaticInv n R B m ∧ PatEnc g m ∧ QMatEnc (boolClosure (augAdj R B g)) m ∧
∀ a, m a = m0 a)
(fun m => StaticInv n R B m ∧ PatEnc g m ∧ QMatEnc (boolClosure (augAdj R B g)) m ∧
m rHeap = heapBase n + (2 * n + 3) ∧
LayerListInv n (heapBase n + (2 * n + 3)) (dpPaths P B g hg 0)
[dpInitial P B g] (fun _ => heapBase n) m ∧
m rAns = m0 rAns ∧ DPFrame n m0 m)
(16 * n * n + 70 * n + 40) := by
have hB1000 := valBound_ge_1000 n
have hrN : rN = 0 := rfl; have hrZ : rZ = 1 := rfl; have hr1 : r1 = 2 := rfl
have hrKL : rKeyLen = 5 := rfl; have hrRW : rRecW = 6 := rfl; have hrAns : rAns = 7 := rfl
have hrBK : rBaseKey = 15 := rfl; have hrHB : rHeapBase = 16 := rfl; have hrH : rHeap = 17 := rfl
have hrCH : rCurHead = 22 := rfl; have hrI : rI = 30 := rfl; have hrIRem : rIRem = 31 := rfl
have hrT0 : rT0 = 36 := rfl; have hrT1 : rT1 = 37 := rfl; have hrT2 : rT2 = 38 := rfl
have hr2N : r2N = 54 := rfl
have hhb_ge : 64 ≤ heapBase n := heapBase_ge n
have hhb_vb : heapBase n + (2 * n + 3) ≤ valBound n := by
have := valBound_ge_heapBase n; have := dpRun_heap_le_valBound (P := P) (B := B) (g := g) (hg := hg); omega
have hbk_ge : 64 ≤ baseKey n := by unfold baseKey; omega
have hbk_hb : baseKey n + (2 * n + 1) ≤ heapBase n := by unfold baseKey heapBase; omega
have hbg_ge : 64 ≤ baseG n := by unfold baseG; omega
have hbq_ge : 64 ≤ baseQ n := by unfold baseQ; omega
have hbg_bk : ∀ u : Fin n, baseG n + u.val < baseKey n := fun u => by
unfold baseG baseKey; have := u.isLt; have : 2 * n * n = n * n + n * n := (by ring); omega
have hbq_bk : ∀ u v : Fin n, baseQ n + u.val * n + v.val < baseKey n := fun u v => by
unfold baseQ baseKey; have : u.val * n + v.val < n * n := by nlinarith [u.isLt, v.isLt]
have : 2 * n * n = n * n + n * n := (by ring); omega
intro hc m ⟨hSt, hPat, hQ, hm0⟩
simp only [dpInitStmt, Stmt.seqs] at hc
-- 1. mov rZ rHeap rHeapBase
have e1 := Exec.mov (P := P_prog) (B := valBound n) (m := m) hc.seq_left
(by omega) (by omega) (by omega) hSt.rZ_eq (by rw [hSt.rHB_eq]; omega)
rw [hSt.rHB_eq] at e1
let m1 := Function.update m rHeap (heapBase n)
have hm1B : MemB n m1 := memB_update hSt.memB (by omega)
have hm1_eq : ∀ a, a ≠ rHeap → m1 a = m a := fun a h => Function.update_of_ne h _ _
-- 2. stmtZeroBlock rBaseKey rKeyLen rI rIRem rT0
have hzb := hoare_zeroBlock (P := P_prog) (base := base + 1) (n := n)
(baseReg := rBaseKey) (lenReg := rKeyLen) (idx := rI) (rem := rIRem) (tmp := rT0)
(N := 2 * n + 1) (baseAddr := baseKey n) (m0 := m1)
(by omega) (by omega) (by omega) (by omega) (by omega) (by omega)
(by decide) (by decide) (by decide) (by decide) (by decide)
(by decide) (by decide) (by decide) (by decide) (by decide)
(by decide) (by decide) (by decide) (by decide) (by decide)
hbk_ge (by omega)
rcases hzb (by simpa [Stmt.len, Nat.add_assoc] using hc.seq_right.seq_left) m1
⟨hm1B, by rw [hm1_eq _ (by omega), hSt.rZ_eq], by rw [hm1_eq _ (by omega), hSt.r1_eq],
by rw [hm1_eq _ (by omega), hSt.rBK_eq], by rw [hm1_eq _ (by omega), hSt.rKL_eq], fun _ => rfl⟩ with
⟨m2, t_zb, ht_zb, e_zb, hm2B, _, _, _, _, hm2_zero, hm2Fr⟩
have hm2_eq : ∀ a, a ≠ rHeap → a ≠ rI → a ≠ rIRem → a ≠ rT0 →
¬(baseKey n ≤ a ∧ a < baseKey n + (2 * n + 1)) → m2 a = m a := fun a h1 h2 h3 h4 hout => by
rw [hm2Fr a h2 h3 h4 (by omega), hm1_eq a h1]
have hSt2 : StaticInv n R B m2 := staticInv_frame hSt hm2B
(fun r hr => hm2_eq r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 h2 => hm2_eq a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseAct at h2; unfold baseKey; have : 2 * n * n = n * n + n * n := (by ring); omega))
-- 3. dpInitLoopStmt
have hil := hoare_dpInitLoop (P := P_prog) (base := base + 1 + 8) (n := n) (R := R) (B := B) (g := g) (m0 := m2) (by omega)
rcases hil (by simpa [stmtZeroBlock, Stmt.len, Nat.add_assoc] using hc.seq_right.seq_right.seq_left) m2
⟨hSt2, fun u => by rw [hm2_eq _ (by omega) (by omega) (by omega) (by omega) (by have := hbg_bk u; omega)]; exact hPat u,
hm2_zero, fun _ => rfl⟩ with
⟨m3, t_il, ht_il, e_il, hSt3, hPat3, hm3_I, hm3_q, hm3_H, hm3_A, hfr3⟩
have hm3_Heq : m3 rHeap = heapBase n := by
rw [hm3_H, hm2Fr rHeap (by omega) (by omega) (by omega) (by omega)]; simp [m1]
have hm3_Aeq : m3 rAns = m0 rAns := by
rw [hm3_A, hm2_eq _ (by omega) (by omega) (by omega) (by omega) (by omega), hm0]
have hfr03 : DPFrame n m0 m3 := dpFrame_trans
⟨fun r hle ha hh => by rw [hm2_eq r hh (by omega) (by omega) (by omega) (by omega), hm0],
by rw [hm2_eq r2N (by omega) (by omega) (by omega) (by omega) (by omega), hm0],
fun a h64 hbk => by rw [hm2_eq a (by omega) (by omega) (by omega) (by omega) (by omega), hm0]⟩ hfr3
-- 4. Allocate initial linked-list node at heapBase n
have hc4 := hc.seq_right.seq_right.seq_right
have e4 := Exec.mov (P := P_prog) (B := valBound n) (m := m3) hc4.seq_left
(by omega) (by omega) (by omega) hSt3.rZ_eq (by rw [hm3_Heq]; omega)
rw [hm3_Heq] at e4
let m4 := Function.update m3 rCurHead (heapBase n)
have hm4B : MemB n m4 := memB_update hSt3.memB (by omega)
have e5 := Exec.store (P := P_prog) (B := valBound n) (m := m4) hc4.seq_right.seq_left
(by omega) (by omega) (by simp [m4]; omega) (by simp [m4, hSt3.rZ_eq])
rw [show m4 rCurHead = heapBase n by simp [m4], show m4 rZ = 0 by simp [m4, hSt3.rZ_eq]] at e5
let m5 := Function.update m4 (heapBase n) 0
have hm5B : MemB n m5 := memB_update hm4B (by omega)
have e6 := Exec.add (P := P_prog) (B := valBound n) (m := m5) hc4.seq_right.seq_right.seq_left
(by omega) (by omega) (by omega)
(by dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_self]; omega)
(by dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), hSt3.r1_eq]; omega)
rw [show m5 rCurHead = heapBase n by dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_self],
show m5 r1 = 1 by dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), hSt3.r1_eq]] at e6
let m6 := Function.update m5 rT0 (heapBase n + 1)
have hm6B : MemB n m6 := memB_update hm5B (by omega)
have e7 := Exec.store (P := P_prog) (B := valBound n) (m := m6) hc4.seq_right.seq_right.seq_right.seq_left
(by omega) (by omega) (by simp [m6]; omega)
(by dsimp only [m6, m5, m4]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_of_ne (by omega), hSt3.r1_eq]; omega)
rw [show m6 rT0 = heapBase n + 1 by simp [m6],
show m6 r1 = 1 by dsimp only [m6, m5, m4]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_of_ne (by omega), hSt3.r1_eq]] at e7
let m7 := Function.update m6 (heapBase n + 1) 1
have hm7B : MemB n m7 := memB_update hm6B (by omega)
have hc8 := hc4.seq_right.seq_right.seq_right.seq_right
have e8 := Exec.add (P := P_prog) (B := valBound n) (m := m7) hc8.seq_left
(by omega) (by omega) (by omega)
(by dsimp only [m7, m6]; rw [Function.update_of_ne (by omega), Function.update_self]; omega)
(by dsimp only [m7, m6, m5, m4]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_of_ne (by omega), hSt3.r1_eq]; omega)
rw [show m7 rT0 = heapBase n + 1 by dsimp only [m7, m6]; rw [Function.update_of_ne (by omega), Function.update_self],
show m7 r1 = 1 by dsimp only [m7, m6, m5, m4]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_of_ne (by omega), hSt3.r1_eq]] at e8
let m8 := Function.update m7 rT0 (heapBase n + 1 + 1)
have hm8B : MemB n m8 := memB_update hm7B (by omega)
have hm8_eq : ∀ a, a ≠ rCurHead → a ≠ rT0 → a ≠ heapBase n → a ≠ heapBase n + 1 → m8 a = m3 a :=
fun a h1 h2 h3 h4 => by
dsimp only [m8, m7, m6, m5, m4]
rw [Function.update_of_ne h2, Function.update_of_ne h4, Function.update_of_ne h2, Function.update_of_ne h3, Function.update_of_ne h1]
-- 5. stmtCopyBlock rBaseKey rT0 rKeyLen rI rIRem rT1 rT2
have hcb := hoare_copyBlock (P := P_prog) (base := base + 1 + 8 + 37 + 5) (n := n)
(srcReg := rBaseKey) (dstReg := rT0) (lenReg := rKeyLen) (idx := rI) (rem := rIRem) (tVal := rT1) (tAddr := rT2)
(N := 2 * n + 1) (srcAddr := baseKey n) (dstAddr := heapBase n + 2) (m0 := m8)
(by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)
(by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)
(by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)
(by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)
(by decide) (by decide) (by decide) (by decide) (by decide)
hbk_ge (by omega) (by omega) (by omega) (Or.inl (by omega))
rcases hcb (by simpa [stmtZeroBlock, dpInitLoopStmt_len, Stmt.mov, Stmt.seqs, Stmt.len, Nat.add_assoc] using hc8.seq_right.seq_left) m8
⟨hm8B, by rw [hm8_eq _ (by omega) (by omega) (by omega) (by omega), hSt3.rZ_eq],
by rw [hm8_eq _ (by omega) (by omega) (by omega) (by omega), hSt3.r1_eq],
by rw [hm8_eq _ (by omega) (by omega) (by omega) (by omega), hSt3.rBK_eq],
by dsimp only [m8]; rw [Function.update_self],
by rw [hm8_eq _ (by omega) (by omega) (by omega) (by omega), hSt3.rKL_eq], fun _ => rfl⟩ with
⟨m9, t_cb, ht_cb, e_cb, hm9B, _, _, _, _, _, hm9_dst, hm9Fr⟩
-- 6. add rHeap rHeap rRecW
have hm9_H : m9 rHeap = heapBase n := by
rw [hm9Fr _ (by omega) (by omega) (by omega) (by omega) (Or.inl (by omega)),
hm8_eq _ (by omega) (by omega) (by omega) (by omega), hm3_Heq]
have hm9_RW : m9 rRecW = 2 * n + 3 := by
rw [hm9Fr _ (by omega) (by omega) (by omega) (by omega) (Or.inl (by omega)),
hm8_eq _ (by omega) (by omega) (by omega) (by omega), hSt3.rRW_eq]
have e10 := Exec.add (P := P_prog) (B := valBound n) (m := m9) hc8.seq_right.seq_right
(by omega) (by omega) (by omega) (by rw [hm9_H]; omega) (by rw [hm9_RW]; omega)
rw [hm9_H, hm9_RW] at e10
let m10 := Function.update m9 rHeap (heapBase n + (2 * n + 3))
have hm10B : MemB n m10 := memB_update hm9B hhb_vb
have hm10_eq : ∀ a, a ≠ rHeap → a ≠ rCurHead → a ≠ rI → a ≠ rIRem → a ≠ rT0 → a ≠ rT1 → a ≠ rT2 →
a < heapBase n → m10 a = m3 a := fun a h1 h2 h3 h4 h5 h6 h7 hlt => by
dsimp only [m10]; rw [Function.update_of_ne h1, hm9Fr a h3 h4 h6 h7 (Or.inl (by omega)),
hm8_eq a h2 h5 (by omega) (by omega)]
have hfr310 : DPFrame n m3 m10 :=
⟨fun r hle ha hh => hm10_eq r hh (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega),
hm10_eq r2N (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega),
fun a h64 hbk => hm10_eq a (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)⟩
have hfr010 : DPFrame n m0 m10 := dpFrame_trans hfr03 hfr310
have hLL : LayerListInv n (heapBase n + (2 * n + 3)) (dpPaths P B g hg 0)
[dpInitial P B g] (fun _ => heapBase n) m10 := {
heap_ge := by omega
heap_le := by simp [m10]
keys_nodup := List.nodup_singleton _
keys_iff := fun s => by
simp only [List.mem_singleton, dpPaths]
constructor
· intro h; rw [h]; simp
· intro h; by_contra hne; rw [if_neg hne] at h; omega
rec_bounds := fun s _ => ⟨le_rfl, le_rfl⟩
rec_disj := fun s₁ h1 s₂ h2 hne => absurd ((List.mem_singleton.mp h1).trans (List.mem_singleton.mp h2).symm) hne
list_head := by
dsimp only [m10]; rw [Function.update_of_ne (by omega),
hm9Fr _ (by omega) (by omega) (by omega) (by omega) (Or.inl (by omega))]
dsimp only [m8, m7, m6, m5, m4]
rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_of_ne (by omega),
Function.update_of_ne (by omega), Function.update_self]
list_links := fun i hi => by
have hi0 : i = 0 := by simp at hi; omega
subst hi0; simp only [List.length_singleton, Nat.lt_irrefl, ↓reduceDIte]
dsimp only [m10]; rw [Function.update_of_ne (by omega),
hm9Fr _ (by omega) (by omega) (by omega) (by omega) (Or.inl (by omega))]
dsimp only [m8, m7, m6, m5]
rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_of_ne (by omega),
Function.update_self]
rec_weight := fun s hs => by
have hs_eq : s = dpInitial P B g := List.mem_singleton.mp hs
subst hs_eq; simp only [dpPaths, ↓reduceIte]
dsimp only [m10]; rw [Function.update_of_ne (by omega),
hm9Fr _ (by omega) (by omega) (by omega) (by omega) (Or.inl (by omega))]
dsimp only [m8, m7]
rw [Function.update_of_ne (by omega), Function.update_self]
rec_key := fun s hs l hl => by
have hs_eq : s = dpInitial P B g := List.mem_singleton.mp hs
subst hs_eq
dsimp only [m10]; rw [Function.update_of_ne (by omega), hm9_dst l hl,
hm8_eq _ (by omega) (by omega) (by omega) (by omega)]
have ⟨hI, hQ'⟩ := stateKey_dpInitial hR B g
by_cases hln : l < n
· rw [hI l hln, hm3_I l hln]
· have hcn : l - n < n + 1 := by omega
have hl_eq : l = n + (l - n) := by omega
rw [hl_eq, hQ' (l - n) hcn, show baseKey n + (n + (l - n)) = baseKey n + n + (l - n) by omega, hm3_q (l - n) hcn]
}
have e_all : Exec P_prog base dpInitStmt.len (valBound n) m m10
(1 + (t_zb + (t_il + (1 + (1 + (1 + (1 + (1 + (t_cb + 1))))))))) := by
have hm9_1 : m9 r1 = 1 := by
rw [hm9Fr _ (by omega) (by omega) (by omega) (by omega) (Or.inl (by omega)),
hm8_eq _ (by omega) (by omega) (by omega) (by omega), hSt3.r1_eq]
have e910 := e_cb.seq (by convert e10 using 1; simp [dpInitLoopStmt_len, stmtZeroBlock, stmtCopyBlock, Stmt.forRange, Stmt.mov, Stmt.len])
have e810 := e8.seq (by convert e910 using 1; simp [dpInitLoopStmt_len, stmtZeroBlock, Stmt.forRange, Stmt.mov, Stmt.len])
have e710 := e7.seq (by convert e810 using 1; simp [dpInitLoopStmt_len, stmtZeroBlock, Stmt.forRange, Stmt.mov, Stmt.len])
have e610 := e6.seq (by convert e710 using 1; simp [dpInitLoopStmt_len, stmtZeroBlock, Stmt.forRange, Stmt.mov, Stmt.len])
have e510 := e5.seq (by convert e610 using 1; simp [dpInitLoopStmt_len, stmtZeroBlock, Stmt.forRange, Stmt.mov, Stmt.len])
have e410 := e4.seq (by convert e510 using 1; simp [dpInitLoopStmt_len, stmtZeroBlock, Stmt.forRange, Stmt.mov, Stmt.len])
have e310 := e_il.seq (by convert e410 using 1; simp [dpInitLoopStmt_len, stmtZeroBlock, Stmt.forRange, Stmt.mov, Stmt.len])
have e210 := e_zb.seq (by convert e310 using 1; simp [stmtZeroBlock, Stmt.forRange, Stmt.mov, Stmt.len])
have e110 := e1.seq (by convert e210 using 1)
convert e110 using 1
· rw [dpInitStmt_len, hm9_1]
simp [dpInitLoopStmt_len, stmtZeroBlock, stmtCopyBlock, Stmt.forRange, Stmt.mov, Stmt.seqs, Stmt.len, hrZ]
· simp [hm9_1]
refine ⟨m10, _, by nlinarith, e_all,
staticInv_frame hSt3 hm10B
(fun r hr => hm10_eq r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 h2 => hm10_eq a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseAct at h2; unfold heapBase; have : 2 * n * n = n * n + n * n := (by ring); omega)),
fun u => by rw [hfr310.mem_low _ (by omega) (hbg_bk u)]; exact hPat3 u,
fun u v => by rw [hfr010.mem_low _ (by omega) (hbq_bk u v), ← hm0]; exact hQ u v,
by simp [m10], hLL,
by rw [hm10_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hm3_Aeq],
hfr010⟩
/-! ## Section 4: `hoare_dpLayer` (One Layer Transition `l → l + 1`) -/
private def dpLayerBodyStmt : Stmt :=
.seqs [
.const rNxtHead 0,
.mov rZ rTrieRoot rHeap,
stmtZeroBlock rHeap rNp1 rI rIRem rT0,
.add rHeap rHeap rNp1,
.mov rZ rNode rCurHead,
.whileNz rZ rNode
(.seqs [
.add rT0 rNode r1,
.load rWeight rT0,
.add rStateKey rT0 r1,
progAntichainSteps,
progCoreSteps,
.load rNode rNode
]),
.mov rZ rCurHead rNxtHead
]
private lemma progAntichainSteps_len_run : progAntichainSteps.len = 89 := by decide
private lemma progCoreSteps_len_run : progCoreSteps.len = 194 := by decide
private lemma dpLayerBodyStmt_len : dpLayerBodyStmt.len = 302 := by decide
private lemma layerListInv_of_mem_eq {n heapEnd : ℕ} {tbl : DPState n → ℕ}
{ks : List (DPState n)} {ao : DPState n → ℕ} {m m' : ℕ → ℕ}
(hLL : LayerListInv n heapEnd tbl ks ao m)
(hHeap : heapEnd ≤ m' rHeap) (hCH : m' rCurHead = m rCurHead)
(hmem : ∀ a, heapBase n ≤ a → a < heapEnd → m' a = m a) :
LayerListInv n heapEnd tbl ks ao m' := {
heap_ge := hLL.heap_ge
heap_le := hHeap
keys_nodup := hLL.keys_nodup
keys_iff := hLL.keys_iff
rec_bounds := hLL.rec_bounds
rec_disj := hLL.rec_disj
list_head := by have hlh := hLL.list_head; cases ks <;> simpa [hCH] using hlh
list_links := fun i hi => by
have hrb := hLL.rec_bounds ks[i] (List.getElem_mem hi)
rw [hmem _ hrb.1 (by omega)]; exact hLL.list_links i hi
rec_weight := fun s hs => by
have hrb := hLL.rec_bounds s hs
rw [hmem _ (by omega) (by omega)]; exact hLL.rec_weight s hs
rec_key := fun s hs l hl => by
have hrb := hLL.rec_bounds s hs
rw [hmem _ (by omega) (by omega)]; exact hLL.rec_key s hs l hl
}
private lemma layerListInv_rec_inj {n heapEnd : ℕ} {tbl : DPState n → ℕ}
{ks : List (DPState n)} {ao : DPState n → ℕ} {m : ℕ → ℕ}
(hLL : LayerListInv n heapEnd tbl ks ao m)
{i j : ℕ} (hi : i < ks.length) (hj : j < ks.length)
(heq : ao ks[i] = ao ks[j]) : i = j := by
by_contra hne
have hkne : ks[i] ≠ ks[j] := fun h => hne ((List.Nodup.getElem_inj_iff hLL.keys_nodup).mp h)
rcases hLL.rec_disj ks[i] (List.getElem_mem hi) ks[j] (List.getElem_mem hj) hkne with h | h <;> omega
private lemma trieListInv_of_mem_eq_run {n heapStart nextKey tbl ks ao nd dep m m'}
(hTL : TrieListInv n heapStart nextKey tbl ks ao nd dep m)
(hHeap : m' rHeap = m rHeap) (hRoot : m' rTrieRoot = m rTrieRoot)
(hHead : m' rNxtHead = m rNxtHead)
(hmem : ∀ a, heapStart ≤ a → m' a = m a) :
TrieListInv n heapStart nextKey tbl ks ao nd dep m' := {
heapStart_ge := hTL.heapStart_ge
heap_ge := by rw [hHeap]; exact hTL.heap_ge
heap_le := by rw [hHeap]; exact hTL.heap_le
keys_nodup := hTL.keys_nodup
keys_iff := hTL.keys_iff
root_mem := by rw [hRoot]; exact hTL.root_mem
root_depth := by rw [hRoot]; exact hTL.root_depth
nodes_bounds := fun p hp => by rw [hHeap]; exact hTL.nodes_bounds p hp
nodes_disj := hTL.nodes_disj
rec_bounds := fun u hu => by rw [hHeap]; exact hTL.rec_bounds u hu
rec_disj := hTL.rec_disj
rec_node_disj := hTL.rec_node_disj
list_head := by have hlh := hTL.list_head; cases ks <;> simpa [hHead] using hlh
list_links := fun idx hidx => by
have := (hTL.rec_bounds ks[idx] (List.getElem_mem hidx)).1
rw [hmem _ (by omega)]; exact hTL.list_links idx hidx
rec_weight := fun u hu => by
have := (hTL.rec_bounds u hu).1; rw [hmem _ (by omega)]; exact hTL.rec_weight u hu
rec_key := fun u hu l hl => by
have := (hTL.rec_bounds u hu).1; rw [hmem _ (by omega)]; exact hTL.rec_key u hu l hl
trie_child := fun p hp hdp c hc => by
have := (hTL.nodes_bounds p hp).1; rw [hmem _ (by omega)]; exact hTL.trie_child p hp hdp c hc
trie_inj := fun p₁ hp₁ p₂ hp₂ hd₁ hd₂ c₁ hc₁ c₂ hc₂ => by
have := (hTL.nodes_bounds p₁ hp₁).1; have := (hTL.nodes_bounds p₂ hp₂).1
rw [hmem _ (by omega), hmem _ (by omega)]
exact hTL.trie_inj p₁ hp₁ p₂ hp₂ hd₁ hd₂ c₁ hc₁ c₂ hc₂
trie_leaf := fun u => by
rw [hRoot, ← hTL.trie_leaf u]
exact trieWalk_congr _ fun j hj hnz => by
rcases trieWalk_step_mem hTL (stateKey u) (fun l _ => stateKey_le u l) j (by omega) with h0 | ⟨hm, _⟩
· exact absurd h0 hnz
· have := (hTL.nodes_bounds _ hm).1; exact hmem _ (by omega)
}
set_option maxHeartbeats 2000000 in
/-- Hoare triple for one layer `l → l + 1` of `progDPRun`. -/
private lemma hoare_dpLayer {P_prog : List Instr} {base n l heapStart : ℕ}
{P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
{B : Finset (Fin n)} {g : Fin n → Option (Fin n)} {hg : Compatible P B g}
{curKeys : List (DPState n)} {curAo : DPState n → ℕ} {m0 : ℕ → ℕ}
(hR : ∀ u v, R u v = true ↔ P.lt u v)
(hl : l < n)
(hbl : base + 320 ≤ valBound n)
(hheap_cap : heapStart + layerHeapBudget n (dpStates P B g hg).card ≤ valBound n) :
Hoare P_prog base (valBound n) dpLayerBodyStmt
(fun m => StaticInv n R B m ∧ PatEnc g m ∧ QMatEnc (boolClosure (augAdj R B g)) m ∧
m rHeap = heapStart ∧
LayerListInv n heapStart (dpPaths P B g hg l) curKeys curAo m ∧
m rAns = m0 rAns ∧ ∀ a, m a = m0 a)
(fun m => StaticInv n R B m ∧ PatEnc g m ∧ QMatEnc (boolClosure (augAdj R B g)) m ∧
m rHeap ≤ heapStart + layerHeapBudget n (dpStates P B g hg).card ∧
(∃ nxtKeys nxtAo, LayerListInv n (m rHeap) (dpPaths P B g hg (l + 1)) nxtKeys nxtAo m) ∧
m rAns = m0 rAns ∧ m rLayer = m0 rLayer ∧ m rLayerRem = m0 rLayerRem ∧ DPFrame n m0 m)
((dpStates P B g hg).card * (56 * n * n * n + 330 * n * n + 380 * n + 280) + 10 * n + 30) := by
have hB1000 := valBound_ge_1000 n
have hrN : rN = 0 := rfl; have hrZ : rZ = 1 := rfl; have hr1 : r1 = 2 := rfl
have hrNp1 : rNp1 = 4 := rfl; have hrAns : rAns = 7 := rfl; have hrH : rHeap = 17 := rfl
have hrLayer : rLayer = 20 := rfl; have hrLayerRem : rLayerRem = 21 := rfl
have hrCH : rCurHead = 22 := rfl; have hrNH : rNxtHead = 23 := rfl
have hrTR : rTrieRoot = 24 := rfl; have hrNd : rNode = 25 := rfl
have hrW : rWeight = 26 := rfl; have hrSK : rStateKey = 27 := rfl
have hrI : rI = 30 := rfl; have hrIRem : rIRem = 31 := rfl; have hrT0 : rT0 = 36 := rfl
have hr2N : r2N = 54 := rfl
have hbg_ge : 64 ≤ baseG n := by unfold baseG; omega
have hbq_ge : 64 ≤ baseQ n := by unfold baseQ; omega
have hbk_ge : 64 ≤ baseKey n := by unfold baseKey; omega
have hbk_hb : baseKey n + (2 * n + 1) ≤ heapBase n := by unfold baseKey heapBase; omega
have hbg_bk : ∀ u : Fin n, baseG n + u.val < baseKey n := fun u => by
unfold baseG baseKey; have := u.isLt; have : 2 * n * n = n * n + n * n := (by ring); omega
have hbq_bk : ∀ u v : Fin n, baseQ n + u.val * n + v.val < baseKey n := fun u v => by
have huv : u.val * n + v.val < n * n := by
have h1 : (u.val + 1) * n ≤ n * n := Nat.mul_le_mul_right n u.isLt
have h2 : (u.val + 1) * n = u.val * n + n := by ring
have := v.isLt; omega
unfold baseQ baseKey; have : 2 * n * n = n * n + n * n := (by ring); omega
intro hc m ⟨hSt, hPat, hQ, hmH, hLL, hmA, hm0⟩
simp only [dpLayerBodyStmt, Stmt.seqs] at hc
have hhs_ge : heapBase n ≤ heapStart := hLL.heap_ge
have hhs64 : 64 ≤ heapStart := le_trans (heapBase_ge n) hhs_ge
have hhs_np1 : heapStart + (n + 1) ≤ valBound n := by unfold layerHeapBudget at hheap_cap; omega
have hck_card : curKeys.length ≤ (dpStates P B g hg).card :=
nodup_le_dpStates_card hLL.keys_nodup (fun s hs => (hLL.keys_iff s).mp hs)
-- 1. const rNxtHead 0
have e1 := Exec.const (P := P_prog) (B := valBound n) (m := m) hc.seq_left (by omega) (by omega)
let m1 := Function.update m rNxtHead 0
have hm1B : MemB n m1 := memB_update hSt.memB (by omega)
-- 2. mov rZ rTrieRoot rHeap
have e2 := Exec.mov (P := P_prog) (B := valBound n) (m := m1) hc.seq_right.seq_left
(by omega) (by omega) (by omega) (by simp [m1, hSt.rZ_eq]) (by simp [m1, hmH]; omega)
rw [show m1 rHeap = heapStart by simp [m1, hmH]] at e2
let m2 := Function.update m1 rTrieRoot heapStart
have hm2B : MemB n m2 := memB_update hm1B (by omega)
-- 3. stmtZeroBlock rHeap rNp1 rI rIRem rT0
have hzb := hoare_zeroBlock (P := P_prog) (base := base + 2) (n := n)
(baseReg := rHeap) (lenReg := rNp1) (idx := rI) (rem := rIRem) (tmp := rT0)
(N := n + 1) (baseAddr := heapStart) (m0 := m2)
(by omega) (by omega) (by omega) (by omega) (by omega) (by omega)
(by decide) (by decide) (by decide) (by decide) (by decide)
(by decide) (by decide) (by decide) (by decide) (by decide)
(by decide) (by decide) (by decide) (by decide) (by decide)
hhs64 hhs_np1
rcases hzb (by simpa [Stmt.mov, Stmt.len, Nat.add_assoc] using hc.seq_right.seq_right.seq_left) m2
⟨hm2B, by simp [m2, m1, hSt.rZ_eq], by simp [m2, m1, hSt.r1_eq],
by simp [m2, m1, hmH], by simp [m2, m1, hSt.rNp1_eq], fun _ => rfl⟩ with
⟨m3, t_zb, ht_zb, e_zb, hm3B, _, _, _, _, hm3_zero, hm3Fr⟩
-- 4. add rHeap rHeap rNp1
have hc4 := hc.seq_right.seq_right.seq_right
have hm3_H : m3 rHeap = heapStart := by rw [hm3Fr _ (by omega) (by omega) (by omega) (Or.inl (by omega))]; simp [m2, m1, hmH]
have hm3_Np1 : m3 rNp1 = n + 1 := by rw [hm3Fr _ (by omega) (by omega) (by omega) (Or.inl (by omega))]; simp [m2, m1, hSt.rNp1_eq]
have e4 := Exec.add (P := P_prog) (B := valBound n) (m := m3) hc4.seq_left
(by omega) (by omega) (by omega) (by rw [hm3_H]; omega) (by rw [hm3_Np1]; omega)
rw [hm3_H, hm3_Np1] at e4
let m4 := Function.update m3 rHeap (heapStart + (n + 1))
have hm4B : MemB n m4 := memB_update hm3B hhs_np1
-- 5. mov rZ rNode rCurHead
have hm4_CH : m4 rCurHead = m rCurHead := by
dsimp only [m4]; rw [Function.update_of_ne (by omega), hm3Fr _ (by omega) (by omega) (by omega) (Or.inl (by omega))]
simp [m2, m1]
have e5 := Exec.mov (P := P_prog) (B := valBound n) (m := m4) hc4.seq_right.seq_left
(by omega) (by omega) (by omega)
(by dsimp only [m4]; rw [Function.update_of_ne (by omega), hm3Fr _ (by omega) (by omega) (by omega) (Or.inl (by omega))]; simp [m2, m1, hSt.rZ_eq])
(by rw [hm4_CH]; exact hSt.memB rCurHead)
rw [hm4_CH] at e5
let m5 := Function.update m4 rNode (m rCurHead)
have hm5B : MemB n m5 := memB_update hm4B (hSt.memB rCurHead)
have hm5_eq : ∀ a, a ≠ rHeap → a ≠ rNxtHead → a ≠ rTrieRoot → a ≠ rNode → a ≠ rI → a ≠ rIRem → a ≠ rT0 →
a < heapStart → m5 a = m a := fun a h1 h2 h3 h4 h5 h6 h7 hlt => by
dsimp only [m5, m4]
rw [Function.update_of_ne h4, Function.update_of_ne h1, hm3Fr a h5 h6 h7 (Or.inl hlt)]
dsimp only [m2, m1]; rw [Function.update_of_ne h3, Function.update_of_ne h2]
have hTL_init : TrieListInv n heapStart 0 (layerPrefix P B g hg l curKeys 0)
[] (fun _ => 0) {heapStart} (fun _ => 0) m5 := {
heapStart_ge := hhs_ge
heap_ge := by dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_self]
heap_le := by dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_self]; simp
keys_nodup := List.nodup_nil
keys_iff := fun t => by simp [layerPrefix_zero]
root_mem := by
have : m5 rTrieRoot = heapStart := by
dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega),
hm3Fr _ (by omega) (by omega) (by omega) (Or.inl (by omega))]; simp [m2]
rw [this]; exact Finset.mem_singleton_self _
root_depth := rfl
nodes_bounds := fun p hp => by
have hp_eq : p = heapStart := Finset.mem_singleton.mp hp
subst hp_eq; dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_self]
exact ⟨le_rfl, le_rfl, Nat.zero_le _⟩
nodes_disj := fun p₁ h1 p₂ h2 hne => absurd ((Finset.mem_singleton.mp h1).trans (Finset.mem_singleton.mp h2).symm) hne
rec_bounds := fun _ h => by simp at h
rec_disj := fun _ h => by simp at h
rec_node_disj := fun _ h => by simp at h
list_head := by
dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega),
hm3Fr _ (by omega) (by omega) (by omega) (Or.inl (by omega))]; simp [m2, m1]
list_links := fun _ hi => absurd hi (Nat.not_lt_zero _)
rec_weight := fun _ h => by simp at h
rec_key := fun _ h => by simp at h
trie_child := fun p hp _ c hc => by
have hp_eq : p = heapStart := Finset.mem_singleton.mp hp
subst hp_eq
dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), hm3_zero c (by omega)]
exact Or.inl rfl
trie_inj := fun p₁ h1 p₂ h2 _ _ c₁ hc₁ c₂ hc₂ hnz => by
have hp1 : p₁ = heapStart := Finset.mem_singleton.mp h1
subst hp1
dsimp only [m5, m4] at hnz; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), hm3_zero c₁ (by omega)] at hnz
exact absurd rfl hnz
trie_leaf := fun u => by
have hrt : m5 rTrieRoot = heapStart := by
dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega),
hm3Fr _ (by omega) (by omega) (by omega) (Or.inl (by omega))]; simp [m2]
rw [hrt]
have h0 : m5 (heapStart + stateKey u 0) = 0 := by
dsimp only [m5, m4]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega),
hm3_zero (stateKey u 0) (by have := stateKey_le u 0; omega)]
have hwalk : ∀ j, trieWalk m5 0 (stateKey u) j = 0 := fun j => by
induction j with | zero => rfl | succ j ih => simp [trieWalk, ih]
have h1 : ∀ j, trieWalk m5 heapStart (stateKey u) (j + 1) = 0 := fun j => by
induction j with
| zero => simp [trieWalk, h0]
| succ j ih =>
change (if trieWalk m5 heapStart (stateKey u) (j + 1) = 0 then 0
else m5 (trieWalk m5 heapStart (stateKey u) (j + 1) + stateKey u (j + 1))) = 0
rw [ih]; rfl
exact h1 (2 * n)
}
-- 6. whileNz rZ rNode (...)
have hc_wh := hc4.seq_right.seq_right.seq_left
let LoopInv : (ℕ → ℕ) → Prop := fun mw =>
∃ idx ≤ curKeys.length,
StaticInv n R B mw ∧ PatEnc g mw ∧ QMatEnc (boolClosure (augAdj R B g)) mw ∧
LayerListInv n heapStart (dpPaths P B g hg l) curKeys curAo mw ∧
(∃ ks ao nd dep, TrieListInv n heapStart 0 (layerPrefix P B g hg l curKeys idx) ks ao nd dep mw) ∧
(mw rNode = if h : idx < curKeys.length then curAo curKeys[idx] else 0) ∧
mw rAns = m0 rAns ∧ mw rLayer = m0 rLayer ∧ mw rLayerRem = m0 rLayerRem ∧ DPFrame n m0 mw
let meas : (ℕ → ℕ) → ℕ := fun mw =>
if h : ∃ (i : ℕ) (hi : i < curKeys.length), mw rNode = curAo curKeys[i] then
curKeys.length - Classical.choose h
else 0
have hmeas_at : ∀ mw (idx : ℕ) (hidx : idx < curKeys.length),
mw rNode = curAo curKeys[idx] → meas mw = curKeys.length - idx := fun mw idx hidx hnd => by
have hex : ∃ (i : ℕ) (hi : i < curKeys.length), mw rNode = curAo curKeys[i] := ⟨idx, hidx, hnd⟩
simp only [meas, dif_pos hex]
rcases Classical.choose_spec hex with ⟨hch1, hch2⟩
have heq : Classical.choose hex = idx := layerListInv_rec_inj hLL hch1 hidx (hch2.symm.trans hnd)
rw [heq]
have hmeas_zero : ∀ mw, mw rNode = 0 → meas mw = 0 := fun mw h0 => by
by_cases hex : ∃ (i : ℕ) (hi : i < curKeys.length), mw rNode = curAo curKeys[i]
· rcases hex with ⟨i, hi, heq⟩
have := (hLL.rec_bounds curKeys[i] (List.getElem_mem hi)).1
have := heapBase_ge n; omega
· simp [meas, hex]
have hwh := Exec.whileNz_ind (P := P_prog) (base := base + 12) (rZ := rZ) (r := rNode) (B := valBound n)
(b := .seqs [
.add rT0 rNode r1,
.load rWeight rT0,
.add rStateKey rT0 r1,
progAntichainSteps,
progCoreSteps,
.load rNode rNode
])
(by simpa [stmtZeroBlock, Stmt.mov, Stmt.seqs, Stmt.len, Nat.add_assoc] using hc_wh)
(by omega) (by omega)
(by simp [progAntichainSteps_len_run, progCoreSteps_len_run, Stmt.seqs, Stmt.len]; omega)
LoopInv meas (56 * n * n * n + 330 * n * n + 380 * n + 278)
(hbody := fun mw ⟨idx, hidx_le, hStw, hPatw, hQw, hLLw, ⟨ks, ao, nd, dep, hTLw⟩, hNd_eq, hmAw, hmLw, hmLRw, hfrw⟩ hnz => by
have hidx : idx < curKeys.length := by
by_contra hge; rw [dif_neg hge] at hNd_eq; exact hnz hNd_eq
rw [dif_pos hidx] at hNd_eq
let s := curKeys[idx]
let sa := curAo s
change mw rNode = sa at hNd_eq
have hs_mem : s ∈ curKeys := List.getElem_mem hidx
have hhb64 : 64 ≤ heapBase n := heapBase_ge n
have hrb : heapBase n ≤ sa ∧ sa + (2 * n + 3) ≤ heapStart := hLLw.rec_bounds s hs_mem
have hw_pos : 0 < dpPaths P B g hg l s := (hLLw.keys_iff s).mp hs_mem
have hs_ideal : IdealOn (patternOrder P B g hg) (core B) s.1 := dpPaths_ideal l s hw_pos
have hs_q : ∀ c, (s.2 c).val ≤ released P B g s.1 c := dpPaths_q_le_released l s hw_pos
have hhc_body : Contains P_prog (base + 13) (.seqs [
.add rT0 rNode r1, .load rWeight rT0, .add rStateKey rT0 r1,
progAntichainSteps, progCoreSteps, .load rNode rNode]) := by
simpa [stmtZeroBlock, Stmt.mov, Stmt.seqs, Stmt.len, Nat.add_assoc] using hc_wh.whileNz_body
refine ⟨by rw [hNd_eq]; omega, ?_⟩
-- 6a. add rT0 rNode r1
have ea1 := Exec.add (P := P_prog) (B := valBound n) (m := mw) hhc_body.seq_left
(by omega) (by omega) (by omega) (by rw [hNd_eq]; omega) (by rw [hStw.r1_eq]; omega)
rw [hNd_eq, hStw.r1_eq] at ea1
let ma1 := Function.update mw rT0 (sa + 1)
have hma1B : MemB n ma1 := memB_update hStw.memB (by omega)
-- 6b. load rWeight rT0
have hma1_w : ma1 (sa + 1) = dpPaths P B g hg l s := by
dsimp only [ma1]; rw [Function.update_of_ne (by omega)]; exact hLLw.rec_weight s hs_mem
have ea2 := Exec.load (P := P_prog) (B := valBound n) (m := ma1) hhc_body.seq_right.seq_left
(by omega) (by omega) (by simp [ma1]; omega) (by rw [show ma1 rT0 = sa + 1 by simp [ma1], hma1_w]; exact dpPaths_le_valBound (by omega) s)
rw [show ma1 rT0 = sa + 1 by simp [ma1], hma1_w] at ea2
let ma2 := Function.update ma1 rWeight (dpPaths P B g hg l s)
have hma2B : MemB n ma2 := memB_update hma1B (dpPaths_le_valBound (by omega) s)
-- 6c. add rStateKey rT0 r1
have ea3 := Exec.add (P := P_prog) (B := valBound n) (m := ma2) hhc_body.seq_right.seq_right.seq_left
(by omega) (by omega) (by omega) (by dsimp only [ma2, ma1]; rw [Function.update_of_ne (by omega), Function.update_self]; omega)
(by dsimp only [ma2, ma1]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), hStw.r1_eq]; omega)
rw [show ma2 rT0 = sa + 1 by dsimp only [ma2, ma1]; rw [Function.update_of_ne (by omega), Function.update_self],
show ma2 r1 = 1 by dsimp only [ma2, ma1]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), hStw.r1_eq]] at ea3
let ma3 := Function.update ma2 rStateKey (sa + 1 + 1)
have hma3B : MemB n ma3 := memB_update hma2B (by omega)
have hma3_eq : ∀ a, a ≠ rT0 → a ≠ rWeight → a ≠ rStateKey → ma3 a = mw a := fun a h1 h2 h3 => by
dsimp only [ma3, ma2, ma1]; rw [Function.update_of_ne h3, Function.update_of_ne h2, Function.update_of_ne h1]
have hSt_a3 : StaticInv n R B ma3 := staticInv_frame hStw hma3B
(fun r hr => hma3_eq r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 _ => hma3_eq a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega))
have hTL_a3 : TrieListInv n heapStart 0 (layerPrefix P B g hg l curKeys idx) ks ao nd dep ma3 :=
trieListInv_of_mem_eq_run hTLw (hma3_eq _ (by omega) (by omega) (by omega))
(hma3_eq _ (by omega) (by omega) (by omega)) (hma3_eq _ (by omega) (by omega) (by omega))
(fun a ha => hma3_eq a (by omega) (by omega) (by omega))
-- Helper bounds for antichain & core steps
have hanti_le_dp : ∀ k ≤ n + 1, ∀ t,
layerPrefix P B g hg l curKeys idx t + dpPaths P B g hg l s * antichainSumPrefix s k t ≤
dpPaths P B g hg (l + 1) t := fun k hk t => by
have h1 : antichainSumPrefix s k t ≤ antichainSumPrefix s (n + 1) t := by
unfold antichainSumPrefix; apply Finset.sum_le_sum_of_subset
intro c hc; simp only [Finset.mem_range] at hc ⊢; omega
have h2 : antichainSumPrefix s (n + 1) t ≤ dpEdge P B g hg s t := by
rw [dpEdge_eq_antichain_add_core]; omega
have h3 := Nat.mul_le_mul_left (dpPaths P B g hg l s) (le_trans h1 h2)
have h4 := layerPrefix_succ P B g hg l curKeys hidx t
have h5 := layerPrefix_le_dpPaths P B g hg l hLLw.keys_nodup hLLw.keys_iff (idx + 1) t
dsimp only [s] at h3 ⊢; omega
have hcore_le_dp : ∀ k ≤ n, ∀ t,
(layerPrefix P B g hg l curKeys idx t + dpPaths P B g hg l s * antichainSumPrefix s (n + 1) t) +
dpPaths P B g hg l s * coreSumPrefix P B g hg s k t ≤ dpPaths P B g hg (l + 1) t := fun k hk t => by
have h1 : coreSumPrefix P B g hg s k t ≤ coreSumPrefix P B g hg s n t := by
unfold coreSumPrefix; apply Finset.sum_le_sum; intro x _
by_cases hxk : x.val < k <;> by_cases hcs : CoreStep P B g hg x s t <;> simp [hxk, hcs, x.isLt]
have h2 : dpPaths P B g hg l s * antichainSumPrefix s (n + 1) t +
dpPaths P B g hg l s * coreSumPrefix P B g hg s k t ≤
dpPaths P B g hg l s * dpEdge P B g hg s t := by
rw [← Nat.mul_add, dpEdge_eq_antichain_add_core]; exact Nat.mul_le_mul_left _ (by omega)
have h3 := layerPrefix_succ P B g hg l curKeys hidx t
have h4 := layerPrefix_le_dpPaths P B g hg l hLLw.keys_nodup hLLw.keys_iff (idx + 1) t
dsimp only [s] at h2 ⊢; omega
-- 6d. progAntichainSteps
have hanti := hoare_antichainSteps (P := P_prog) (base := base + 16) (n := n)
(R := R) (B := B) (s := s) (w := dpPaths P B g hg l s) (stateAddr := sa + 2)
(heapStart := heapStart) (maxKeys := (dpStates P B g hg).card)
(tbl := layerPrefix P B g hg l curKeys idx)
(keys := ks) (addrOf := ao) (nodes := nd) (nodeDepth := dep) (m0 := ma3)
(by omega) hw_pos (by omega) (by omega) (Or.inr (by omega))
(fun t => le_trans (hanti_le_dp (n + 1) le_rfl t) (dpPaths_le_valBound (by omega) t))
(fun k hk ks' hnd' hiff' => nodup_le_dpStates_card hnd' fun t ht =>
lt_of_lt_of_le ((hiff' t).mp ht) (hanti_le_dp k hk t))
(by unfold layerHeapBudget at hheap_cap; omega)
have hc_rest3 := hhc_body.seq_right.seq_right.seq_right
rcases hanti (by simpa [Stmt.seqs, Stmt.len, Nat.add_assoc] using hc_rest3.seq_left) ma3
⟨hSt_a3, hTL_a3, by dsimp only [ma3, ma2]; rw [Function.update_of_ne (by omega), Function.update_self],
by dsimp only [ma3]; rw [Function.update_self],
fun j hj => by rw [hma3_eq _ (by omega) (by omega) (by omega)]; exact hLLw.rec_key s hs_mem j hj,
fun _ => rfl⟩ with
⟨ma4, t_anti, ht_anti, e_anti, hSt_a4, ks4, ao4, nd4, dep4, hTL_a4, hfr_a4⟩
-- 6e. progCoreSteps
have hcore := hoare_coreSteps (P_prog := P_prog) (base := base + 16 + 89) (n := n)
(P := P) (R := R) (B := B) (g := g) (hg := hg) (s := s) (w := dpPaths P B g hg l s)
(stateAddr := sa + 2) (heapStart := heapStart) (maxKeys := (dpStates P B g hg).card)
(tbl₀ := fun t => layerPrefix P B g hg l curKeys idx t + dpPaths P B g hg l s * antichainSumPrefix s (n + 1) t)
(m0 := ma4) hR (by omega) hs_ideal hs_q hw_pos (by omega) (by omega) (Or.inr (by omega)) (by omega)
(fun k hk ks' hnd' hiff' => nodup_le_dpStates_card hnd' fun t ht =>
lt_of_lt_of_le ((hiff' t).mp ht) (hcore_le_dp k hk t))
(fun k hk t => le_trans (hcore_le_dp k hk t) (dpPaths_le_valBound (by omega) t))
(by unfold layerHeapBudget at hheap_cap; omega)
rcases hcore (by simpa [progAntichainSteps_len_run, Stmt.seqs, Stmt.len, Nat.add_assoc] using hc_rest3.seq_right.seq_left) ma4
⟨hSt_a4,
fun u => by rw [hfr_a4.2.2 _ (by omega) (by have := hbg_bk u; omega) (Or.inl (hbg_bk u)), hma3_eq _ (by omega) (by omega) (by omega)]; exact hPatw u,
fun u v => by rw [hfr_a4.2.2 _ (by omega) (by have := hbq_bk u v; omega) (Or.inl (hbq_bk u v)), hma3_eq _ (by omega) (by omega) (by omega)]; exact hQw u v,
⟨ks4, ao4, nd4, dep4, hTL_a4⟩,
by rw [hfr_a4.1 _ (by omega) (by omega) (by omega)]; dsimp only [ma3, ma2]; rw [Function.update_of_ne (by omega), Function.update_self],
by rw [hfr_a4.1 _ (by omega) (by omega) (by omega)]; dsimp only [ma3]; rw [Function.update_self],
fun j hj => by rw [hfr_a4.2.2 _ (by omega) (by omega) (Or.inr (by omega)), hma3_eq _ (by omega) (by omega) (by omega)]; exact hLLw.rec_key s hs_mem j hj,
fun _ => rfl⟩ with
⟨ma5, t_core, ht_core, e_core, hSt_a5, hPat_a5, hQ_a5, ⟨ks', ao', nd', dep', hTL_a5⟩, _, _, _, hfr_a5⟩
have hfr_a35 : TransFrame n heapStart ma3 ma5 := transFrame_trans hfr_a4 hfr_a5
have hma5_Nd : ma5 rNode = sa := by
rw [hfr_a35.1 _ (by omega) (by omega) (by omega), hma3_eq _ (by omega) (by omega) (by omega), hNd_eq]
have hma5_link : ma5 sa = if h : idx + 1 < curKeys.length then curAo curKeys[idx + 1] else 0 := by
rw [hfr_a35.2.2 _ (by omega) (by omega) (Or.inr (by omega)), hma3_eq _ (by omega) (by omega) (by omega)]
exact hLLw.list_links idx hidx
-- 6f. load rNode rNode
have ea6 := Exec.load (P := P_prog) (B := valBound n) (m := ma5) hc_rest3.seq_right.seq_right
(by omega) (by omega) (by rw [hma5_Nd]; omega)
(by rw [hma5_Nd, hma5_link]; split_ifs with hnext
· have := (hLLw.rec_bounds curKeys[idx + 1] (List.getElem_mem hnext)).2; omega
· omega)
rw [hma5_Nd, hma5_link] at ea6
let ma6 := Function.update ma5 rNode (if h : idx + 1 < curKeys.length then curAo curKeys[idx + 1] else 0)
have hma6B : MemB n ma6 := memB_update hSt_a5.memB (by
split_ifs with hnext
· have := (hLLw.rec_bounds curKeys[idx + 1] (List.getElem_mem hnext)).2; omega
· omega)
have htbl_next : (fun t => (layerPrefix P B g hg l curKeys idx t +
dpPaths P B g hg l s * antichainSumPrefix s (n + 1) t) +
dpPaths P B g hg l s * coreSumPrefix P B g hg s n t) =
layerPrefix P B g hg l curKeys (idx + 1) := by
funext t; rw [layerPrefix_succ P B g hg l curKeys hidx t, dpEdge_eq_antichain_add_core]; ring
have hTL_a6 : TrieListInv n heapStart 0 (layerPrefix P B g hg l curKeys (idx + 1)) ks' ao' nd' dep' ma6 :=
htbl_next ▸ trieListInv_of_mem_eq_run hTL_a5 (Function.update_of_ne (by omega) _ _)
(Function.update_of_ne (by omega) _ _) (Function.update_of_ne (by omega) _ _)
(fun a ha => Function.update_of_ne (by omega) _ _)
have hfr_wa6 : DPFrame n mw ma6 :=
dpFrame_trans
⟨fun r hle _ _ => hma3_eq r (by omega) (by omega) (by omega), hma3_eq _ (by omega) (by omega) (by omega),
fun a h64 _ => hma3_eq a (by omega) (by omega) (by omega)⟩
(dpFrame_trans (dpFrame_of_transFrame hhs_ge hfr_a35)
⟨fun r hle _ _ => Function.update_of_ne (by omega) _ _, Function.update_of_ne (by omega) _ _,
fun a h64 _ => Function.update_of_ne (by omega) _ _⟩)
have hmeas_lt : meas ma6 < meas mw := by
rw [hmeas_at mw idx hidx hNd_eq]
by_cases hnext : idx + 1 < curKeys.length
· rw [hmeas_at ma6 (idx + 1) hnext (by simp [ma6, hnext])]; omega
· rw [hmeas_zero ma6 (by simp [ma6, hnext])]; omega
have ht_step : 1 + (1 + (1 + (t_anti + (t_core + 1)))) ≤ 56 * n * n * n + 330 * n * n + 380 * n + 278 := by
have h1 : (n + 1) * (12 * n * n + 110 * n + 90) + 10 = 12 * n * n * n + 122 * n * n + 200 * n + 100 := by ring
have h2 : n * (44 * n * n + 200 * n + 160) + 10 = 44 * n * n * n + 200 * n * n + 160 * n + 10 := by ring
nlinarith
have e_seq6 : Exec P_prog (base + 12 + 1)
(Stmt.seqs [.add rT0 rNode r1, .load rWeight rT0, .add rStateKey rT0 r1,
progAntichainSteps, progCoreSteps, .load rNode rNode]).len
(valBound n) mw ma6 (1 + (1 + (1 + (t_anti + (t_core + 1))))) := by
have ea6' : Exec P_prog (base + 16 + 89 + progCoreSteps.len) 1 (valBound n) ma5 ma6 1 := by
rw [show base + 16 + 89 + progCoreSteps.len =
base + 13 + (Stmt.add rT0 rNode r1).len + (Stmt.load rWeight rT0).len +
(Stmt.add rStateKey rT0 r1).len + progAntichainSteps.len + progCoreSteps.len by
simp [progAntichainSteps_len_run, progCoreSteps_len_run, Stmt.len]]
exact ea6
have e56 := e_core.seq ea6'
have e46 := e_anti.seq (by rw [show base + 16 + progAntichainSteps.len = base + 16 + 89 by rw [progAntichainSteps_len_run]]; exact e56)
have e36 := ea3.seq (by rw [show base + 13 + (Stmt.add rT0 rNode r1).len + (Stmt.load rWeight rT0).len + 1 = base + 16 by simp [Stmt.len]]; exact e46)
have e26 := ea2.seq (by rw [show base + 13 + (Stmt.add rT0 rNode r1).len + 1 = base + 13 + (Stmt.add rT0 rNode r1).len + (Stmt.load rWeight rT0).len by simp [Stmt.len]]; exact e36)
have e16 := ea1.seq (by rw [show base + 13 + 1 = base + 13 + (Stmt.add rT0 rNode r1).len by simp [Stmt.len]]; exact e26)
rw [show base + 12 + 1 = base + 13 by omega,
show (Stmt.seqs [.add rT0 rNode r1, .load rWeight rT0, .add rStateKey rT0 r1,
progAntichainSteps, progCoreSteps, .load rNode rNode]).len =
1 + (1 + (1 + (progAntichainSteps.len + (progCoreSteps.len + 1)))) by
simp [Stmt.seqs, Stmt.len]]
exact e16
refine ⟨ma6, _, ht_step, e_seq6,
by dsimp only [ma6]; rw [Function.update_of_ne (by omega), hSt_a5.rZ_eq],
⟨idx + 1, by omega,
staticInv_frame hSt_a5 hma6B (fun r hr => Function.update_of_ne (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) _ _)
(fun a h1 _ => Function.update_of_ne (by unfold baseR at h1; omega) _ _),
fun u => by dsimp only [ma6]; rw [Function.update_of_ne (by omega)]; exact hPat_a5 u,
fun u v => by dsimp only [ma6]; rw [Function.update_of_ne (by omega)]; exact hQ_a5 u v,
layerListInv_of_mem_eq hLLw (le_trans (by omega) hTL_a6.heap_ge)
(by dsimp only [ma6]; rw [Function.update_of_ne (by omega), hfr_a35.1 _ (by omega) (by omega) (by omega), hma3_eq _ (by omega) (by omega) (by omega)])
(fun a h1 h2 => by dsimp only [ma6]; rw [Function.update_of_ne (by omega), hfr_a35.2.2 a (by omega) h2 (Or.inr (by omega)), hma3_eq a (by omega) (by omega) (by omega)]),
⟨ks', ao', nd', dep', hTL_a6⟩, by simp [ma6],
by dsimp only [ma6]; rw [Function.update_of_ne (by omega), hfr_a35.1 _ (by omega) (by omega) (by omega), hma3_eq _ (by omega) (by omega) (by omega), hmAw],
by dsimp only [ma6]; rw [Function.update_of_ne (by omega), hfr_a35.1 _ (by omega) (by omega) (by omega), hma3_eq _ (by omega) (by omega) (by omega), hmLw],
by dsimp only [ma6]; rw [Function.update_of_ne (by omega), hfr_a35.1 _ (by omega) (by omega) (by omega), hma3_eq _ (by omega) (by omega) (by omega), hmLRw],
dpFrame_trans hfrw hfr_wa6⟩,
hmeas_lt⟩)
-- Run the while loop from m5 with k = curKeys.length
have hLoopInit : LoopInv m5 := ⟨0, Nat.zero_le _,
staticInv_frame hSt hm5B (fun r hr => hm5_eq r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 h2 => hm5_eq a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseAct at h2; unfold heapBase at hhs_ge; have : 2 * n * n = n * n + n * n := (by ring); omega)),
fun u => by rw [hm5_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by have := hbg_bk u; omega)]; exact hPat u,
fun u v => by rw [hm5_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by have := hbq_bk u v; omega)]; exact hQ u v,
layerListInv_of_mem_eq hLL (le_trans (by omega) hTL_init.heap_ge)
(hm5_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega))
(fun a h1 h2 => hm5_eq a (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) h2),
⟨[], fun _ => 0, {heapStart}, fun _ => 0, hTL_init⟩,
by cases curKeys with
| nil => simpa [m5] using hLL.list_head
| cons s rest => simpa [m5] using hLL.list_head,
by rw [hm5_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmA],
by rw [hm5_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hm0],
by rw [hm5_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hm0],
⟨fun r hle ha hh => by rw [hm5_eq r hh (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hm0],
by rw [hm5_eq r2N (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hm0],
fun a h64 hbk => by rw [hm5_eq a (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hm0]⟩⟩
have hmeas_init : meas m5 ≤ curKeys.length := by
by_cases hex : ∃ (i : ℕ) (hi : i < curKeys.length), m5 rNode = curAo curKeys[i]
· simp only [meas, dif_pos hex]; omega
· simp [meas, hex]
rcases hwh curKeys.length m5 hLoopInit hmeas_init with
⟨m6, t_wh, ht_wh, e_wh, ⟨idx_end, hidx_end_le, hSt6, hPat6, hQ6, _, ⟨ks_end, ao_end, nd_end, dep_end, hTL6⟩, hNd6_eq, hmA6, hmL6, hmLR6, hfr6⟩, hNd6_0⟩
have hidx_full : idx_end = curKeys.length := by
by_contra hne
have hlt : idx_end < curKeys.length := by omega
rw [dif_pos hlt] at hNd6_eq
have := (hLL.rec_bounds curKeys[idx_end] (List.getElem_mem hlt)).1
have := heapBase_ge n; omega
subst hidx_full
have htbl_full : layerPrefix P B g hg l curKeys curKeys.length = dpPaths P B g hg (l + 1) :=
funext (layerPrefix_full P B g hg l hLL.keys_nodup hLL.keys_iff)
have hTL6' : TrieListInv n heapStart 0 (dpPaths P B g hg (l + 1)) ks_end ao_end nd_end dep_end m6 :=
htbl_full ▸ hTL6
-- 7. mov rZ rCurHead rNxtHead
have e7 := Exec.mov (P := P_prog) (B := valBound n) (m := m6) hc4.seq_right.seq_right.seq_right
(by omega) (by omega) (by omega) hSt6.rZ_eq (hSt6.memB rNxtHead)
let m7 := Function.update m6 rCurHead (m6 rNxtHead)
have hm7B : MemB n m7 := memB_update hSt6.memB (hSt6.memB rNxtHead)
have hks_end_le : ks_end.length ≤ (dpStates P B g hg).card :=
nodup_le_dpStates_card hTL6'.keys_nodup (fun s hs => (hTL6'.keys_iff s).mp hs)
have hm7_heap_le : m7 rHeap ≤ heapStart + layerHeapBudget n (dpStates P B g hg).card := by
dsimp only [m7]; rw [Function.update_of_ne (by omega)]
have h1 := hTL6'.heap_le
unfold layerHeapBudget
have h2 : ks_end.length * (2 * n * (n + 1) + (2 * n + 3)) ≤
((dpStates P B g hg).card + 1) * (2 * n * (n + 1) + (2 * n + 3)) :=
Nat.mul_le_mul_right _ (by omega)
omega
have ht_all : 1 + (1 + (t_zb + (1 + (1 + (t_wh + 1))))) ≤
(dpStates P B g hg).card * (56 * n * n * n + 330 * n * n + 380 * n + 280) + 10 * n + 30 := by
have h1 : curKeys.length * (56 * n * n * n + 330 * n * n + 380 * n + 278 + 2) ≤
(dpStates P B g hg).card * (56 * n * n * n + 330 * n * n + 380 * n + 280) :=
Nat.mul_le_mul_right _ hck_card
have h2 : 6 * (n + 1) + 3 = 6 * n + 9 := by ring
omega
refine ⟨m7, _, ht_all,
by simpa only [dpLayerBodyStmt, Stmt.seqs, Stmt.len, mov_len] using e1.seq (e2.seq (e_zb.seq (e4.seq (e5.seq (e_wh.seq e7))))),
staticInv_frame hSt6 hm7B (fun r hr => Function.update_of_ne (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) _ _)
(fun a h1 _ => Function.update_of_ne (by unfold baseR at h1; omega) _ _),
fun u => by dsimp only [m7]; rw [Function.update_of_ne (by omega)]; exact hPat6 u,
fun u v => by dsimp only [m7]; rw [Function.update_of_ne (by omega)]; exact hQ6 u v,
hm7_heap_le,
⟨ks_end, ao_end, by simpa [m7, Function.update_of_ne (by omega : rHeap ≠ rCurHead)] using layerListInv_of_trieListInv hTL6'⟩,
by dsimp only [m7]; rw [Function.update_of_ne (by omega), hmA6],
by dsimp only [m7]; rw [Function.update_of_ne (by omega), hmL6],
by dsimp only [m7]; rw [Function.update_of_ne (by omega), hmLR6],
dpFrame_trans hfr6 ⟨fun r hle _ _ => Function.update_of_ne (by omega) _ _,
Function.update_of_ne (by omega) _ _, fun a h64 _ => Function.update_of_ne (by omega) _ _⟩⟩
/-! ## Section 5: `hoare_dpScan` (Final Layer Scan for `dpTerminal B`) -/
private def dpScanCheckIStmt : Stmt :=
.forRange rZ r1 rI rIRem rN
(.seqs [
.arrayLoad rT2 rBaseB rI rT3,
.sub rT2 r1 rT2,
.arrayLoad rT3 rStateKey rI rT4,
stmtEq rT4 rT2 rT3 rT5 rT6,
.ifZero rZ rT4 (.const rT1 0) .nop
])
private lemma dpScanCheckIStmt_len : dpScanCheckIStmt.len = 22 := by decide
private def dpScanCheckQStmt : Stmt :=
.forRange rZ r1 rI rIRem rNp1
(.seqs [
.arrayLoad rT3 rT2 rI rT4,
.ifNz rZ rT3 (.const rT1 0) .nop
])
private lemma dpScanCheckQStmt_len : dpScanCheckQStmt.len = 11 := by decide
private def dpScanStmt : Stmt :=
.seqs [
.mov rZ rNode rCurHead,
.whileNz rZ rNode
(.seqs [
.add rT0 rNode r1,
.load rWeight rT0,
.add rStateKey rT0 r1,
.const rT1 1,
dpScanCheckIStmt,
.add rT2 rStateKey rN,
dpScanCheckQStmt,
.ifNz rZ rT1 (.add rAns rAns rWeight) .nop,
.load rNode rNode
])
]
private lemma dpScanStmt_len : dpScanStmt.len = 45 := by decide
private lemma state_eq_dpTerminal_iff {n : ℕ} (B : Finset (Fin n)) (s : DPState n) :
s = dpTerminal B ↔
(∀ x : Fin n, stateKey s x.val = 1 - (if x ∈ B then 1 else 0)) ∧
(∀ c < n + 1, stateKey s (n + c) = 0) := by
constructor
· rintro rfl
refine ⟨fun x => ?_, fun c hc => ?_⟩
· unfold stateKey dpTerminal core; simp [x.isLt]; split_ifs <;> simp
· rw [stateKey_n_add _ c hc]; rfl
· rintro ⟨hI, hQ⟩
apply Prod.ext
· ext x
have hx := hI x
unfold stateKey at hx; simp only [x.isLt, ↓reduceDIte] at hx
unfold dpTerminal core; simp only [Finset.mem_sdiff, Finset.mem_univ, true_and]
by_cases hxB : x ∈ B <;> by_cases hxI : x ∈ s.1 <;> simp_all
· funext c
obtain ⟨k, hk, rfl⟩ := optOfNat_surj c
have hqk := hQ k hk
rw [stateKey_n_add s k hk] at hqk
exact Fin.ext hqk
private lemma hoare_dpScanCheckI {P : List Instr} {base n : ℕ}
{R : Fin n → Fin n → Bool} {B : Finset (Fin n)} {s : DPState n} {stateAddr : ℕ} {m0 : ℕ → ℕ}
(hbl : base + 30 ≤ valBound n)
(hsa_ge : 64 ≤ stateAddr) (hsa_le : stateAddr + (2 * n + 1) ≤ valBound n) :
Hoare P base (valBound n) dpScanCheckIStmt
(fun m => StaticInv n R B m ∧ m rStateKey = stateAddr ∧ m rT1 = 1 ∧
(∀ l < 2 * n + 1, m (stateAddr + l) = stateKey s l) ∧ ∀ a, m a = m0 a)
(fun m => StaticInv n R B m ∧
m rT1 = (if ∀ x : Fin n, stateKey s x.val = 1 - (if x ∈ B then 1 else 0) then 1 else 0) ∧
(∀ a, a ≠ rI → a ≠ rIRem → a ≠ rT1 → a ≠ rT2 → a ≠ rT3 → a ≠ rT4 → a ≠ rT5 → a ≠ rT6 → m a = m0 a))
(20 * n + 10) := by
have hB1000 := valBound_ge_1000 n
have hrN : rN = 0 := rfl; have hrZ : rZ = 1 := rfl; have hr1 : r1 = 2 := rfl
have hrBB : rBaseB = 9 := rfl; have hrSK : rStateKey = 27 := rfl
have hrI : rI = 30 := rfl; have hrIRem : rIRem = 31 := rfl
have hrT1 : rT1 = 37 := rfl; have hrT2 : rT2 = 38 := rfl; have hrT3 : rT3 = 39 := rfl
have hrT4 : rT4 = 40 := rfl; have hrT5 : rT5 = 41 := rfl; have hrT6 : rT6 = 42 := rfl
have hr2N : r2N = 54 := rfl
have hn_le : n ≤ valBound n := by have := valBound_ge_heapBase n; unfold heapBase at this; omega
have hbb_ge : 64 ≤ baseB n := by unfold baseB; omega
intro hc m ⟨hSt, hmSK, hmT1, hsk, hm0⟩
have hfor := Hoare.forRange (P := P) (base := base) (rZ := rZ) (r1 := r1)
(idx := rI) (rem := rIRem) (lim := rN) (B := valBound n) (N := n) (stepCap := 16)
(body := .seqs [
.arrayLoad rT2 rBaseB rI rT3,
.sub rT2 r1 rT2,
.arrayLoad rT3 rStateKey rI rT4,
stmtEq rT4 rT2 rT3 rT5 rT6,
.ifZero rZ rT4 (.const rT1 0) .nop
])
(by omega) (by omega) (by omega) (by omega) (by omega) (by omega) hn_le
(by simp [stmtEq, Stmt.seqs, Stmt.len]; omega)
(by decide) (by decide) (by decide) (by decide) (by decide) (by decide)
(Inv := fun k mb => MemB n mb ∧
mb rT1 = (if ∀ x : Fin n, x.val < k → stateKey s x.val = 1 - (if x ∈ B then 1 else 0) then 1 else 0) ∧
(∀ a, a ≠ rI → a ≠ rIRem → a ≠ rT1 → a ≠ rT2 → a ≠ rT3 → a ≠ rT4 → a ≠ rT5 → a ≠ rT6 → mb a = m0 a))
(hInv_init := fun mb ⟨hB, h1, hfr⟩ _ _ _ =>
⟨memB_update (memB_update hB (by omega)) hn_le, by simp [h1],
fun a ha1 ha2 ha3 ha4 ha5 ha6 ha7 ha8 => by rw [Function.update_of_ne ha2, Function.update_of_ne ha1, hfr a ha1 ha2 ha3 ha4 ha5 ha6 ha7 ha8]⟩)
(hInv_step := fun k hk mb ⟨hB, h1, hfr⟩ _ _ _ _ =>
⟨memB_update (memB_update hB (by omega)) (by omega), by simp [h1],
fun a ha1 ha2 ha3 ha4 ha5 ha6 ha7 ha8 => by rw [Function.update_of_ne ha2, Function.update_of_ne ha1, hfr a ha1 ha2 ha3 ha4 ha5 ha6 ha7 ha8]⟩)
(hbody := fun k hk hcb mb ⟨⟨hB, h1, hfr⟩, hmI, hmIR, hmZ, hm1⟩ => by
simp only [Stmt.seqs] at hcb
let kf : Fin n := ⟨k, hk⟩
have hmBB : mb rBaseB = baseB n := by rw [hfr _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), ← hm0, hSt.rBB_eq]
have hmSK' : mb rStateKey = stateAddr := by rw [hfr _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), ← hm0, hmSK]
have hbb_le : baseB n + k ≤ valBound n := by have := baseB_add_n_le n; omega
have eb1 := Exec.arrayLoad (P := P) (B := valBound n) (m := mb) hcb.seq_left
(by omega) (by omega) (by omega) (by omega) (by rw [hmBB]; have := baseB_add_n_le n; omega)
(by rw [hmI]; omega) (by rw [hmBB, hmI]; exact hbb_le) (hB _) (by rw [hmBB, hmI]; omega)
rw [hmBB, hmI, hfr (baseB n + k) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), ← hm0, hSt.B_ind kf] at eb1
let mb1 := Function.update (Function.update mb rT3 (baseB n + k)) rT2 (if kf ∈ B then 1 else 0)
have hmb1B : MemB n mb1 := memB_update (memB_update hB hbb_le) (by split_ifs <;> omega)
have eb2 := Exec.sub (P := P) (B := valBound n) (m := mb1) hcb.seq_right.seq_left
(by omega) (by omega) (by omega) (by simp [mb1, hm1]; omega) (by simp [mb1]; split_ifs <;> omega)
rw [show mb1 r1 = 1 by simp [mb1, hm1], show mb1 rT2 = if kf ∈ B then 1 else 0 by simp [mb1]] at eb2
let expv := 1 - (if kf ∈ B then 1 else 0)
let mb2 := Function.update mb1 rT2 expv
have hmb2B : MemB n mb2 := memB_update hmb1B (by dsimp [expv]; split_ifs <;> omega)
have hmb2_sk : mb2 (stateAddr + k) = stateKey s k := by
dsimp only [mb2, mb1]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_of_ne (by omega),
hfr _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), ← hm0, hsk k (by omega)]
have hsk_le : stateKey s k ≤ valBound n := by have := stateKey_le s k; omega
have eb3 := Exec.arrayLoad (P := P) (B := valBound n) (m := mb2) hcb.seq_right.seq_right.seq_left
(by omega) (by omega) (by omega) (by omega) (by simp [mb2, mb1, hmSK']; omega)
(by simp [mb2, mb1, hmI]; omega) (by simp [mb2, mb1, hmSK', hmI]; omega)
(hmb2B _) (by simp [mb2, mb1, hmSK', hmI]; omega)
rw [show mb2 rStateKey = stateAddr by simp [mb2, mb1, hmSK'], show mb2 rI = k by simp [mb2, mb1, hmI], hmb2_sk] at eb3
let mb3 := Function.update (Function.update mb2 rT4 (stateAddr + k)) rT3 (stateKey s k)
have hmb3B : MemB n mb3 := memB_update (memB_update hmb2B (by omega)) hsk_le
have heq := hoare_eq (P := P) (base := base + 3 + 5) (n := n)
(dst := rT4) (rA := rT2) (rB := rT3) (t1 := rT5) (t2 := rT6) (A := expv) (Bv := stateKey s k) (m0 := mb3)
(by omega) (by omega) (by omega) (by omega) (by omega) (by omega)
(by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)
rcases heq (by simpa [Stmt.seqs, Stmt.len, arrayLoad_len, Nat.add_assoc] using hcb.seq_right.seq_right.seq_right.seq_left) mb3
⟨hmb3B, by simp [mb3, mb2, mb1, hmZ], by simp [mb3, mb2], by simp [mb3], fun _ => rfl⟩ with
⟨mb4, t_eq, ht_eq, e_eq, hmb4B, _, hmb4_4, hmb4Fr⟩
have hmb4_eq : ∀ a, a ≠ rT2 → a ≠ rT3 → a ≠ rT4 → a ≠ rT5 → a ≠ rT6 → mb4 a = mb a := fun a h2 h3 h4 h5 h6 => by
rw [hmb4Fr a h4 h5 h6]; dsimp only [mb3, mb2, mb1]
rw [Function.update_of_ne h3, Function.update_of_ne h4, Function.update_of_ne h2, Function.update_of_ne h2, Function.update_of_ne h3]
have hc_if := hcb.seq_right.seq_right.seq_right.seq_right
by_cases hmatch : stateKey s k = expv
· have hif := Exec.ifZero_false (B := valBound n) (m := mb4) (m' := mb4) hc_if
(by omega) (by omega) (by simp [stmtEq, Stmt.seqs, Stmt.len]; omega)
(by rw [hmb4_4, if_pos hmatch.symm]; omega) (hmb4B _) (by rw [hmb4_eq _ (by omega) (by omega) (by omega) (by omega) (by omega), hmZ]) Exec.nop
have hiff : (∀ x : Fin n, x.val < k + 1 → stateKey s x.val = 1 - (if x ∈ B then 1 else 0)) ↔
(∀ x : Fin n, x.val < k → stateKey s x.val = 1 - (if x ∈ B then 1 else 0)) := by
constructor
· intro h x hx; exact h x (by omega)
· intro h x hx
by_cases hlt : x.val < k
· exact h x hlt
· rw [show x = kf from Fin.ext (by dsimp [kf]; omega)]; exact hmatch
exact ⟨mb4, 2 + (1 + (2 + (t_eq + (1 + 0 + 1)))), by omega,
by simpa only [Stmt.seqs, Stmt.len, arrayLoad_len] using eb1.seq (eb2.seq (eb3.seq (e_eq.seq hif))),
⟨hmb4B, by rw [hmb4_eq _ (by omega) (by omega) (by omega) (by omega) (by omega), h1, if_congr hiff rfl rfl],
fun a ha1 ha2 ha3 ha4 ha5 ha6 ha7 ha8 => by rw [hmb4_eq a ha4 ha5 ha6 ha7 ha8, hfr a ha1 ha2 ha3 ha4 ha5 ha6 ha7 ha8]⟩,
by rw [hmb4_eq _ (by omega) (by omega) (by omega) (by omega) (by omega), hmI],
by rw [hmb4_eq _ (by omega) (by omega) (by omega) (by omega) (by omega), hmIR],
by rw [hmb4_eq _ (by omega) (by omega) (by omega) (by omega) (by omega), hmZ],
by rw [hmb4_eq _ (by omega) (by omega) (by omega) (by omega) (by omega), hm1]⟩
· have e_c := Exec.const (P := P) (B := valBound n) (m := mb4) hc_if.ifZero_then (by omega) (by omega)
let mb5 := Function.update mb4 rT1 0
have hmb5B : MemB n mb5 := memB_update hmb4B (by omega)
have hif := Exec.ifZero_true (B := valBound n) (m := mb4) hc_if
(by omega) (by simp [stmtEq, Stmt.seqs, Stmt.len]; omega)
(by rw [hmb4_4, if_neg (Ne.symm hmatch)]) e_c
have hnot : ¬ (∀ x : Fin n, x.val < k + 1 → stateKey s x.val = 1 - (if x ∈ B then 1 else 0)) := fun h =>
hmatch (h kf (by dsimp [kf]; omega))
exact ⟨mb5, 2 + (1 + (2 + (t_eq + (1 + 1)))), by omega,
by simpa only [Stmt.seqs, Stmt.len, arrayLoad_len] using eb1.seq (eb2.seq (eb3.seq (e_eq.seq hif))),
⟨hmb5B, by rw [if_neg hnot]; simp [mb5],
fun a ha1 ha2 ha3 ha4 ha5 ha6 ha7 ha8 => by dsimp only [mb5]; rw [Function.update_of_ne ha3, hmb4_eq a ha4 ha5 ha6 ha7 ha8, hfr a ha1 ha2 ha3 ha4 ha5 ha6 ha7 ha8]⟩,
by dsimp only [mb5]; rw [Function.update_of_ne (by omega), hmb4_eq _ (by omega) (by omega) (by omega) (by omega) (by omega), hmI],
by dsimp only [mb5]; rw [Function.update_of_ne (by omega), hmb4_eq _ (by omega) (by omega) (by omega) (by omega) (by omega), hmIR],
by dsimp only [mb5]; rw [Function.update_of_ne (by omega), hmb4_eq _ (by omega) (by omega) (by omega) (by omega) (by omega), hmZ],
by dsimp only [mb5]; rw [Function.update_of_ne (by omega), hmb4_eq _ (by omega) (by omega) (by omega) (by omega) (by omega), hm1]⟩)
rcases hfor hc m ⟨⟨hSt.memB, by simp [hmT1], fun a _ _ _ _ _ _ _ _ => hm0 a⟩, hSt.rN_eq, hSt.rZ_eq, hSt.r1_eq⟩ with
⟨m_end, t_for, ht_for, e_for, ⟨hmendB, hmend1, hmendFr⟩, _, _, _, _⟩
exact ⟨m_end, t_for, by omega, e_for,
staticInv_frame hSt hmendB
(fun r hr => by rw [hmendFr r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega), ← hm0])
(fun a h1 _ => by rw [hmendFr a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega), ← hm0]),
by simpa [Fin.isLt] using hmend1, hmendFr⟩
private lemma hoare_dpScanCheckQ {P : List Instr} {base n : ℕ}
{R : Fin n → Fin n → Bool} {B : Finset (Fin n)} {s : DPState n} {stateAddr : ℕ} {ok : Bool} {m0 : ℕ → ℕ}
(hbl : base + 20 ≤ valBound n)
(hsa_ge : 64 ≤ stateAddr) (hsa_le : stateAddr + (2 * n + 1) ≤ valBound n) :
Hoare P base (valBound n) dpScanCheckQStmt
(fun m => StaticInv n R B m ∧ m rT2 = stateAddr + n ∧ m rT1 = (if ok then 1 else 0) ∧
(∀ l < 2 * n + 1, m (stateAddr + l) = stateKey s l) ∧ ∀ a, m a = m0 a)
(fun m => StaticInv n R B m ∧
m rT1 = (if ok = true ∧ ∀ c < n + 1, stateKey s (n + c) = 0 then 1 else 0) ∧
(∀ a, a ≠ rI → a ≠ rIRem → a ≠ rT1 → a ≠ rT3 → a ≠ rT4 → m a = m0 a))
(12 * n + 20) := by
have hB1000 := valBound_ge_1000 n
have hrN : rN = 0 := rfl; have hrZ : rZ = 1 := rfl; have hr1 : r1 = 2 := rfl; have hrNp1 : rNp1 = 4 := rfl
have hrI : rI = 30 := rfl; have hrIRem : rIRem = 31 := rfl
have hrT1 : rT1 = 37 := rfl; have hrT2 : rT2 = 38 := rfl; have hrT3 : rT3 = 39 := rfl; have hrT4 : rT4 = 40 := rfl
have hr2N : r2N = 54 := rfl
have hnp1_le : n + 1 ≤ valBound n := by have := valBound_ge_heapBase n; unfold heapBase at this; omega
intro hc m ⟨hSt, hmT2, hmT1, hsk, hm0⟩
have hfor := Hoare.forRange (P := P) (base := base) (rZ := rZ) (r1 := r1)
(idx := rI) (rem := rIRem) (lim := rNp1) (B := valBound n) (N := n + 1) (stepCap := 8)
(body := .seqs [.arrayLoad rT3 rT2 rI rT4, .ifNz rZ rT3 (.const rT1 0) .nop])
(by omega) (by omega) (by omega) (by omega) (by omega) (by omega) hnp1_le
(by simp [Stmt.seqs, Stmt.len]; omega)
(by decide) (by decide) (by decide) (by decide) (by decide) (by decide)
(Inv := fun k mb => MemB n mb ∧
mb rT1 = (if ok = true ∧ ∀ c < k, stateKey s (n + c) = 0 then 1 else 0) ∧
(∀ a, a ≠ rI → a ≠ rIRem → a ≠ rT1 → a ≠ rT3 → a ≠ rT4 → mb a = m0 a))
(hInv_init := fun mb ⟨hB, h1, hfr⟩ _ _ _ =>
⟨memB_update (memB_update hB (by omega)) hnp1_le, by simp [h1],
fun a ha1 ha2 ha3 ha4 ha5 => by rw [Function.update_of_ne ha2, Function.update_of_ne ha1, hfr a ha1 ha2 ha3 ha4 ha5]⟩)
(hInv_step := fun k hk mb ⟨hB, h1, hfr⟩ _ _ _ _ =>
⟨memB_update (memB_update hB (by omega)) (by omega), by simp [h1],
fun a ha1 ha2 ha3 ha4 ha5 => by rw [Function.update_of_ne ha2, Function.update_of_ne ha1, hfr a ha1 ha2 ha3 ha4 ha5]⟩)
(hbody := fun k hk hcb mb ⟨⟨hB, h1, hfr⟩, hmI, hmIR, hmZ, hm1⟩ => by
simp only [Stmt.seqs] at hcb
have hmT2' : mb rT2 = stateAddr + n := by rw [hfr _ (by omega) (by omega) (by omega) (by omega) (by omega), ← hm0, hmT2]
have hsk_k : mb (stateAddr + n + k) = stateKey s (n + k) := by
rw [hfr _ (by omega) (by omega) (by omega) (by omega) (by omega), ← hm0,
show stateAddr + n + k = stateAddr + (n + k) by omega, hsk (n + k) (by omega)]
have hsk_le : stateKey s (n + k) ≤ valBound n := by have := stateKey_le s (n + k); omega
have eb1 := Exec.arrayLoad (P := P) (B := valBound n) (m := mb) hcb.seq_left
(by omega) (by omega) (by omega) (by omega) (by rw [hmT2']; omega)
(by rw [hmI]; omega) (by rw [hmT2', hmI]; omega) (hB _) (by rw [hmT2', hmI]; omega)
rw [hmT2', hmI, hsk_k] at eb1
let mb1 := Function.update (Function.update mb rT4 (stateAddr + n + k)) rT3 (stateKey s (n + k))
have hmb1B : MemB n mb1 := memB_update (memB_update hB (by omega)) hsk_le
have hc_if := hcb.seq_right
by_cases h0 : stateKey s (n + k) = 0
· have hif := Exec.ifZero_true (B := valBound n) (m := mb1) hc_if
(by omega) (by simp [Stmt.seqs, Stmt.len]; omega) (by simp [mb1, h0]) Exec.nop
have hiff : (ok = true ∧ ∀ c < k + 1, stateKey s (n + c) = 0) ↔ (ok = true ∧ ∀ c < k, stateKey s (n + c) = 0) := by
constructor
· rintro ⟨ho, h⟩; exact ⟨ho, fun c hc => h c (by omega)⟩
· rintro ⟨ho, h⟩; refine ⟨ho, fun c hc => ?_⟩
by_cases hlt : c < k
· exact h c hlt
· rw [show c = k by omega]; exact h0
exact ⟨mb1, 2 + (1 + 0), by omega,
by simpa only [Stmt.seqs, Stmt.len, arrayLoad_len, ifNz_len] using eb1.seq hif,
⟨hmb1B, by dsimp only [mb1]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), h1, if_congr hiff rfl rfl],
fun a ha1 ha2 ha3 ha4 ha5 => by dsimp only [mb1]; rw [Function.update_of_ne ha4, Function.update_of_ne ha5, hfr a ha1 ha2 ha3 ha4 ha5]⟩,
by simp [mb1, hmI], by simp [mb1, hmIR], by simp [mb1, hmZ], by simp [mb1, hm1]⟩
· have ec := Exec.const (P := P) (B := valBound n) (m := mb1) hc_if.ifZero_else (by omega) (by omega)
let mb2 := Function.update mb1 rT1 0
have hmb2B : MemB n mb2 := memB_update hmb1B (by omega)
have hif := Exec.ifZero_false (B := valBound n) (m := mb1) (m' := mb2) hc_if
(by omega) (by omega) (by simp [Stmt.seqs, Stmt.len]; omega) (by simp [mb1, h0]) (hmb1B _) (by simp [mb2, mb1, hmZ]) ec
have hnot : ¬ (ok = true ∧ ∀ c < k + 1, stateKey s (n + c) = 0) := fun h => h0 (h.2 k (by omega))
exact ⟨mb2, 2 + (1 + 1 + 1), by omega,
by simpa only [Stmt.seqs, Stmt.len, arrayLoad_len, ifNz_len] using eb1.seq hif,
⟨hmb2B, by rw [if_neg hnot]; simp [mb2],
fun a ha1 ha2 ha3 ha4 ha5 => by dsimp only [mb2, mb1]; rw [Function.update_of_ne ha3, Function.update_of_ne ha4, Function.update_of_ne ha5, hfr a ha1 ha2 ha3 ha4 ha5]⟩,
by simp [mb2, mb1, hmI], by simp [mb2, mb1, hmIR], by simp [mb2, mb1, hmZ], by simp [mb2, mb1, hm1]⟩)
rcases hfor hc m ⟨⟨hSt.memB, by simp [hmT1], fun a _ _ _ _ _ => hm0 a⟩, hSt.rNp1_eq, hSt.rZ_eq, hSt.r1_eq⟩ with
⟨m_end, t_for, ht_for, e_for, ⟨hmendB, hmend1, hmendFr⟩, _, _, _, _⟩
exact ⟨m_end, t_for, by omega, e_for,
staticInv_frame hSt hmendB
(fun r hr => by rw [hmendFr r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega), ← hm0])
(fun a h1 _ => by rw [hmendFr a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega), ← hm0]),
hmend1, hmendFr⟩
/-- Hoare triple for `dpScanStmt`: scans the final layer and adds `dpPaths P B g hg n (dpTerminal B)` to `rAns`. -/
private lemma hoare_dpScan {P_prog : List Instr} {base n heapEnd : ℕ}
{P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
{B : Finset (Fin n)} {g : Fin n → Option (Fin n)} {hg : Compatible P B g}
{finalKeys : List (DPState n)} {finalAo : DPState n → ℕ} {m0 : ℕ → ℕ}
(hbl : base + 55 ≤ valBound n)
(hhe_vb : heapEnd ≤ valBound n)
(hans_cap : m0 rAns + dpPaths P B g hg n (dpTerminal B) ≤ valBound n) :
Hoare P_prog base (valBound n) dpScanStmt
(fun m => StaticInv n R B m ∧
LayerListInv n heapEnd (dpPaths P B g hg n) finalKeys finalAo m ∧
∀ a, m a = m0 a)
(fun m => StaticInv n R B m ∧
m rAns = m0 rAns + dpPaths P B g hg n (dpTerminal B) ∧
DPFrame n m0 m)
((dpStates P B g hg).card * (32 * n + 50) + 10) := by
have hB1000 := valBound_ge_1000 n
have hrN : rN = 0 := rfl; have hrZ : rZ = 1 := rfl; have hr1 : r1 = 2 := rfl
have hrAns : rAns = 7 := rfl; have hrH : rHeap = 17 := rfl; have hrCH : rCurHead = 22 := rfl
have hrNd : rNode = 25 := rfl; have hrW : rWeight = 26 := rfl; have hrSK : rStateKey = 27 := rfl
have hrI : rI = 30 := rfl; have hrIRem : rIRem = 31 := rfl
have hrT0 : rT0 = 36 := rfl; have hrT1 : rT1 = 37 := rfl; have hrT2 : rT2 = 38 := rfl
have hrT3 : rT3 = 39 := rfl; have hrT4 : rT4 = 40 := rfl; have hrT5 : rT5 = 41 := rfl
have hrT6 : rT6 = 42 := rfl; have hr2N : r2N = 54 := rfl
intro hc m ⟨hSt, hLL, hm0⟩
simp only [dpScanStmt, Stmt.seqs] at hc
have hfk_card : finalKeys.length ≤ (dpStates P B g hg).card :=
nodup_le_dpStates_card hLL.keys_nodup (fun s hs => (hLL.keys_iff s).mp hs)
-- 1. mov rZ rNode rCurHead
have e1 := Exec.mov (P := P_prog) (B := valBound n) (m := m) hc.seq_left
(by omega) (by omega) (by omega) hSt.rZ_eq (hSt.memB rCurHead)
let m1 := Function.update m rNode (m rCurHead)
have hm1B : MemB n m1 := memB_update hSt.memB (hSt.memB rCurHead)
-- Partial sum of dpTerminal matches in first `idx` nodes
let scanSum : ℕ → ℕ := fun idx =>
∑ i ∈ Finset.range (min idx finalKeys.length),
if h : i < finalKeys.length then
if finalKeys[i] = dpTerminal B then dpPaths P B g hg n finalKeys[i] else 0
else 0
have hscan_zero : scanSum 0 = 0 := by simp [scanSum]
have hscan_succ : ∀ idx (hidx : idx < finalKeys.length),
scanSum (idx + 1) = scanSum idx + if finalKeys[idx] = dpTerminal B then dpPaths P B g hg n finalKeys[idx] else 0 :=
fun idx hidx => by
simp only [scanSum, Nat.min_eq_left (by omega : idx + 1 ≤ finalKeys.length),
Nat.min_eq_left (by omega : idx ≤ finalKeys.length), Finset.sum_range_succ, dif_pos hidx]
have hscan_full : scanSum finalKeys.length = dpPaths P B g hg n (dpTerminal B) := by
simp only [scanSum, Nat.min_self]
by_cases hpos : 0 < dpPaths P B g hg n (dpTerminal B)
· obtain ⟨i₀, hi₀, heq₀⟩ := List.mem_iff_getElem.mp ((hLL.keys_iff _).mpr hpos)
rw [Finset.sum_eq_single i₀]
· simp [hi₀, heq₀]
· intro i hi hne
have hlt : i < finalKeys.length := Finset.mem_range.mp hi
have hne_st : finalKeys[i] ≠ dpTerminal B := fun h => hne ((List.Nodup.getElem_inj_iff hLL.keys_nodup).mp (h.trans heq₀.symm))
simp [hlt, hne_st]
· intro h; exact absurd (Finset.mem_range.mpr hi₀) h
· have h0 : dpPaths P B g hg n (dpTerminal B) = 0 := by omega
rw [h0]; apply Finset.sum_eq_zero; intro i hi
have hlt : i < finalKeys.length := Finset.mem_range.mp hi
rw [dif_pos hlt]; split_ifs with h2
· rw [h2]; exact h0
· rfl
have hscan_le : ∀ idx, scanSum idx ≤ dpPaths P B g hg n (dpTerminal B) := fun idx => by
rw [← hscan_full]; simp only [scanSum]
apply Finset.sum_le_sum_of_subset; intro i hi; simp only [Finset.mem_range] at hi ⊢; omega
-- 2. whileNz rZ rNode
let LoopInv : (ℕ → ℕ) → Prop := fun mw =>
∃ idx ≤ finalKeys.length,
StaticInv n R B mw ∧
LayerListInv n heapEnd (dpPaths P B g hg n) finalKeys finalAo mw ∧
(mw rNode = if h : idx < finalKeys.length then finalAo finalKeys[idx] else 0) ∧
mw rAns = m0 rAns + scanSum idx ∧ DPFrame n m0 mw
let meas : (ℕ → ℕ) → ℕ := fun mw =>
if h : ∃ (i : ℕ) (hi : i < finalKeys.length), mw rNode = finalAo finalKeys[i] then
finalKeys.length - Classical.choose h
else 0
have hmeas_at : ∀ mw (idx : ℕ) (hidx : idx < finalKeys.length),
mw rNode = finalAo finalKeys[idx] → meas mw = finalKeys.length - idx := fun mw idx hidx hnd => by
have hex : ∃ (i : ℕ) (hi : i < finalKeys.length), mw rNode = finalAo finalKeys[i] := ⟨idx, hidx, hnd⟩
simp only [meas, dif_pos hex]
rcases Classical.choose_spec hex with ⟨hch1, hch2⟩
rw [layerListInv_rec_inj hLL hch1 hidx (hch2.symm.trans hnd)]
have hmeas_zero : ∀ mw, mw rNode = 0 → meas mw = 0 := fun mw h0 => by
by_cases hex : ∃ (i : ℕ) (hi : i < finalKeys.length), mw rNode = finalAo finalKeys[i]
· rcases hex with ⟨i, hi, heq⟩
have := (hLL.rec_bounds finalKeys[i] (List.getElem_mem hi)).1; have := heapBase_ge n; omega
· simp [meas, hex]
have hc_wh : Contains P_prog (base + 1) (.whileNz rZ rNode (.seqs [
.add rT0 rNode r1, .load rWeight rT0, .add rStateKey rT0 r1, .const rT1 1,
dpScanCheckIStmt, .add rT2 rStateKey rN, dpScanCheckQStmt,
.ifNz rZ rT1 (.add rAns rAns rWeight) .nop, .load rNode rNode
])) := by simpa [Stmt.mov, Stmt.seqs, Stmt.len] using hc.seq_right
have hwh := Exec.whileNz_ind (P := P_prog) (base := base + 1) (rZ := rZ) (r := rNode) (B := valBound n)
(b := .seqs [
.add rT0 rNode r1, .load rWeight rT0, .add rStateKey rT0 r1, .const rT1 1,
dpScanCheckIStmt, .add rT2 rStateKey rN, dpScanCheckQStmt,
.ifNz rZ rT1 (.add rAns rAns rWeight) .nop, .load rNode rNode
])
hc_wh
(by omega) (by omega)
(by simp [dpScanCheckIStmt_len, dpScanCheckQStmt_len, Stmt.seqs, Stmt.len]; omega)
LoopInv meas (32 * n + 48)
(hbody := fun mw ⟨idx, hidx_le, hStw, hLLw, hNd_eq, hmAw, hfrw⟩ hnz => by
have hidx : idx < finalKeys.length := by by_contra hge; rw [dif_neg hge] at hNd_eq; exact hnz hNd_eq
rw [dif_pos hidx] at hNd_eq
let s := finalKeys[idx]; let sa := finalAo s
change mw rNode = sa at hNd_eq
have hs_mem : s ∈ finalKeys := List.getElem_mem hidx
have hrb : heapBase n ≤ sa ∧ sa + (2 * n + 3) ≤ heapEnd := hLLw.rec_bounds s hs_mem
have hhb64 : 64 ≤ heapBase n := heapBase_ge n
have hhc_b : Contains P_prog (base + 2) (.seqs [
.add rT0 rNode r1, .load rWeight rT0, .add rStateKey rT0 r1, .const rT1 1,
dpScanCheckIStmt, .add rT2 rStateKey rN, dpScanCheckQStmt,
.ifNz rZ rT1 (.add rAns rAns rWeight) .nop, .load rNode rNode
]) := by simpa [Nat.add_assoc] using hc_wh.whileNz_body
refine ⟨by rw [hNd_eq]; omega, ?_⟩
have ea1 := Exec.add (P := P_prog) (B := valBound n) (m := mw) hhc_b.seq_left
(by omega) (by omega) (by omega) (by rw [hNd_eq]; omega) (by rw [hStw.r1_eq]; omega)
rw [hNd_eq, hStw.r1_eq] at ea1
let ma1 := Function.update mw rT0 (sa + 1)
have hma1B : MemB n ma1 := memB_update hStw.memB (by omega)
have hma1_w : ma1 (sa + 1) = dpPaths P B g hg n s := by
dsimp only [ma1]; rw [Function.update_of_ne (by omega)]; exact hLLw.rec_weight s hs_mem
have ea2 := Exec.load (P := P_prog) (B := valBound n) (m := ma1) hhc_b.seq_right.seq_left
(by omega) (by omega) (by simp [ma1]; omega) (by rw [show ma1 rT0 = sa + 1 by simp [ma1], hma1_w]; exact dpPaths_le_valBound le_rfl s)
rw [show ma1 rT0 = sa + 1 by simp [ma1], hma1_w] at ea2
let ma2 := Function.update ma1 rWeight (dpPaths P B g hg n s)
have hma2B : MemB n ma2 := memB_update hma1B (dpPaths_le_valBound le_rfl s)
have ea3 := Exec.add (P := P_prog) (B := valBound n) (m := ma2) hhc_b.seq_right.seq_right.seq_left
(by omega) (by omega) (by omega)
(by dsimp only [ma2, ma1]; rw [Function.update_of_ne (by omega), Function.update_self]; omega)
(by dsimp only [ma2, ma1]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), hStw.r1_eq]; omega)
rw [show ma2 rT0 = sa + 1 by dsimp only [ma2, ma1]; rw [Function.update_of_ne (by omega), Function.update_self],
show ma2 r1 = 1 by dsimp only [ma2, ma1]; rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), hStw.r1_eq]] at ea3
let ma3 := Function.update ma2 rStateKey (sa + 1 + 1)
have hma3B : MemB n ma3 := memB_update hma2B (by omega)
have ea4 := Exec.const (P := P_prog) (B := valBound n) (m := ma3) hhc_b.seq_right.seq_right.seq_right.seq_left (by omega) (by omega)
let ma4 := Function.update ma3 rT1 1
have hma4B : MemB n ma4 := memB_update hma3B (by omega)
have hma4_eq : ∀ a, a ≠ rT0 → a ≠ rWeight → a ≠ rStateKey → a ≠ rT1 → ma4 a = mw a := fun a h1 h2 h3 h4 => by
dsimp only [ma4, ma3, ma2, ma1]; rw [Function.update_of_ne h4, Function.update_of_ne h3, Function.update_of_ne h2, Function.update_of_ne h1]
have hSt_a4 : StaticInv n R B ma4 := staticInv_frame hStw hma4B
(fun r hr => hma4_eq r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 _ => hma4_eq a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega) (by unfold baseR at h1; omega))
have hc_r4 := hhc_b.seq_right.seq_right.seq_right.seq_right
have hci := hoare_dpScanCheckI (P := P_prog) (base := base + 6) (n := n) (R := R) (B := B)
(s := s) (stateAddr := sa + 2) (m0 := ma4) (by omega) (by omega) (by omega)
rcases hci (by simpa [Stmt.seqs, Stmt.len, Nat.add_assoc] using hc_r4.seq_left) ma4
⟨hSt_a4, by dsimp only [ma4, ma3]; rw [Function.update_of_ne (by omega), Function.update_self], by simp [ma4],
fun l hl => by rw [hma4_eq _ (by omega) (by omega) (by omega) (by omega)]; exact hLLw.rec_key s hs_mem l hl,
fun _ => rfl⟩ with
⟨ma5, t_ci, ht_ci, e_ci, hSt_a5, hma5_1, hma5Fr⟩
have hma5_SK : ma5 rStateKey = sa + 2 := by
rw [hma5Fr _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)]
dsimp only [ma4, ma3]; rw [Function.update_of_ne (by omega), Function.update_self]
have ea6 := Exec.add (P := P_prog) (B := valBound n) (m := ma5) hc_r4.seq_right.seq_left
(by omega) (by omega) (by omega) (by rw [hma5_SK]; omega) (by rw [hSt_a5.rN_eq]; omega)
rw [hma5_SK, hSt_a5.rN_eq] at ea6
let ma6 := Function.update ma5 rT2 (sa + 2 + n)
have hma6B : MemB n ma6 := memB_update hSt_a5.memB (by omega)
have hSt_a6 : StaticInv n R B ma6 := staticInv_frame hSt_a5 hma6B
(fun r hr => Function.update_of_ne (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) _ _)
(fun a h1 _ => Function.update_of_ne (by unfold baseR at h1; omega) _ _)
let okI : Bool := decide (∀ x : Fin n, stateKey s x.val = 1 - (if x ∈ B then 1 else 0))
have hcq := hoare_dpScanCheckQ (P := P_prog) (base := base + 6 + 22 + 1) (n := n) (R := R) (B := B)
(s := s) (stateAddr := sa + 2) (ok := okI) (m0 := ma6) (by omega) (by omega) (by omega)
rcases hcq (by simpa [dpScanCheckIStmt_len, Stmt.seqs, Stmt.len, Nat.add_assoc] using hc_r4.seq_right.seq_right.seq_left) ma6
⟨hSt_a6, by simp [ma6], by dsimp only [ma6]; rw [Function.update_of_ne (by omega), hma5_1]; simp [okI],
fun l hl => by dsimp only [ma6]; rw [Function.update_of_ne (by omega), hma5Fr _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hma4_eq _ (by omega) (by omega) (by omega) (by omega)]; exact hLLw.rec_key s hs_mem l hl,
fun _ => rfl⟩ with
⟨ma7, t_cq, ht_cq, e_cq, hSt_a7, hma7_1, hma7Fr⟩
have hma7_eq : ∀ a, a ≠ rWeight → a ≠ rStateKey → a ≠ rI → a ≠ rIRem →
a ≠ rT0 → a ≠ rT1 → a ≠ rT2 → a ≠ rT3 → a ≠ rT4 → a ≠ rT5 → a ≠ rT6 → ma7 a = mw a :=
fun a h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 h11 => by
rw [hma7Fr a h3 h4 h6 h8 h9]; dsimp only [ma6]; rw [Function.update_of_ne h7, hma5Fr a h3 h4 h6 h7 h8 h9 h10 h11, hma4_eq a h5 h1 h2 h6]
have hma7_W : ma7 rWeight = dpPaths P B g hg n s := by
rw [hma7Fr _ (by omega) (by omega) (by omega) (by omega) (by omega)]; dsimp only [ma6]
rw [Function.update_of_ne (by omega), hma5Fr _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)]
dsimp only [ma4, ma3, ma2]
rw [Function.update_of_ne (by omega), Function.update_of_ne (by omega), Function.update_self]
have hma7_Ans : ma7 rAns = m0 rAns + scanSum idx := by
rw [hma7_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hmAw]
have hterm_iff : (okI = true ∧ ∀ c < n + 1, stateKey s (n + c) = 0) ↔ s = dpTerminal B := by
rw [state_eq_dpTerminal_iff B s]; simp [okI]
have hma7_1' : ma7 rT1 = if s = dpTerminal B then 1 else 0 := by rw [hma7_1, if_congr hterm_iff rfl rfl]
have hc_if := hc_r4.seq_right.seq_right.seq_right.seq_left
have hbr : ∃ ma8 t_if, t_if ≤ 3 ∧
Exec P_prog (base + 6 + 22 + 1 + 11) (Stmt.ifNz rZ rT1 (.add rAns rAns rWeight) .nop).len (valBound n) ma7 ma8 t_if ∧
StaticInv n R B ma8 ∧ ma8 rAns = m0 rAns + scanSum (idx + 1) ∧
(∀ a, a ≠ rAns → ma8 a = ma7 a) := by
by_cases hst : s = dpTerminal B
· have hsum_le : m0 rAns + scanSum idx + dpPaths P B g hg n s ≤ valBound n := by
have h1 := hscan_succ idx hidx; rw [if_pos hst] at h1
have h2 := hscan_le (idx + 1)
dsimp only [s] at h1 ⊢; omega
have e_add := Exec.add (P := P_prog) (B := valBound n) (m := ma7) hc_if.ifZero_else
(by omega) (by omega) (by omega) (by rw [hma7_Ans]; omega) (by rw [hma7_W]; exact dpPaths_le_valBound le_rfl s)
rw [hma7_Ans, hma7_W] at e_add
let ma8 := Function.update ma7 rAns (m0 rAns + scanSum idx + dpPaths P B g hg n s)
have hma8B : MemB n ma8 := memB_update hSt_a7.memB hsum_le
have hif := Exec.ifZero_false (B := valBound n) (m := ma7) (m' := ma8) hc_if
(by omega) (by omega) (by simp [dpScanCheckIStmt_len, dpScanCheckQStmt_len, Stmt.len]; omega)
(by rw [hma7_1', if_pos hst]; omega) (hSt_a7.memB _) (by dsimp only [ma8]; rw [Function.update_of_ne (by omega), hSt_a7.rZ_eq]) e_add
exact ⟨ma8, 1 + 1 + 1, by omega,
by convert hif using 1
· simp [dpScanCheckIStmt_len, dpScanCheckQStmt_len, Stmt.len]
· rfl,
staticInv_frame hSt_a7 hma8B (fun r hr => Function.update_of_ne (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) _ _)
(fun a h1 _ => Function.update_of_ne (by unfold baseR at h1; omega) _ _),
by dsimp only [ma8]; rw [Function.update_self, hscan_succ idx hidx, if_pos hst]; dsimp only [s]; omega,
fun a ha => Function.update_of_ne ha _ _⟩
· have hif := Exec.ifZero_true (B := valBound n) (m := ma7) hc_if
(by omega) (by simp [dpScanCheckIStmt_len, dpScanCheckQStmt_len, Stmt.len]; omega)
(by rw [hma7_1', if_neg hst]) Exec.nop
exact ⟨ma7, 1 + 0, by omega,
by convert hif using 1
· simp [dpScanCheckIStmt_len, dpScanCheckQStmt_len, Stmt.len]
· rfl,
hSt_a7, by rw [hma7_Ans, hscan_succ idx hidx, if_neg hst]; omega, fun _ _ => rfl⟩
rcases hbr with ⟨ma8, t_if, ht_if, e_if, hSt_a8, hma8_Ans, hma8Fr⟩
have hma8_Nd : ma8 rNode = sa := by
rw [hma8Fr _ (by omega), hma7_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega), hNd_eq]
have hma8_link : ma8 sa = if h : idx + 1 < finalKeys.length then finalAo finalKeys[idx + 1] else 0 := by
rw [hma8Fr _ (by omega), hma7_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)]
exact hLLw.list_links idx hidx
have ea9 := Exec.load (P := P_prog) (B := valBound n) (m := ma8) hc_r4.seq_right.seq_right.seq_right.seq_right
(by omega) (by omega) (by rw [hma8_Nd]; omega)
(by rw [hma8_Nd, hma8_link]; split_ifs with hnext
· have := (hLLw.rec_bounds finalKeys[idx + 1] (List.getElem_mem hnext)).2; omega
· omega)
rw [hma8_Nd, hma8_link] at ea9
let ma9 := Function.update ma8 rNode (if h : idx + 1 < finalKeys.length then finalAo finalKeys[idx + 1] else 0)
have hma9B : MemB n ma9 := memB_update hSt_a8.memB (by
split_ifs with hnext
· have := (hLLw.rec_bounds finalKeys[idx + 1] (List.getElem_mem hnext)).2; omega
· omega)
have hma9_mem : ∀ a, 64 ≤ a → ma9 a = mw a := fun a ha => by
dsimp only [ma9]; rw [Function.update_of_ne (by omega), hma8Fr a (by omega),
hma7_eq a (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)]
have hmeas_lt : meas ma9 < meas mw := by
rw [hmeas_at mw idx hidx hNd_eq]
by_cases hnext : idx + 1 < finalKeys.length
· rw [hmeas_at ma9 (idx + 1) hnext (by simp [ma9, hnext])]; omega
· rw [hmeas_zero ma9 (by simp [ma9, hnext])]; omega
have e_seq9 : Exec P_prog (base + 2)
(Stmt.seqs [.add rT0 rNode r1, .load rWeight rT0, .add rStateKey rT0 r1, .const rT1 1,
dpScanCheckIStmt, .add rT2 rStateKey rN, dpScanCheckQStmt,
.ifNz rZ rT1 (.add rAns rAns rWeight) .nop, .load rNode rNode]).len
(valBound n) mw ma9 (1 + (1 + (1 + (1 + (t_ci + (1 + (t_cq + (t_if + 1)))))))) := by
have e89 := e_if.seq (by convert ea9 using 1; simp [dpScanCheckIStmt_len, dpScanCheckQStmt_len, Stmt.len, ifNz_len])
have e79 := e_cq.seq (by convert e89 using 1; simp [dpScanCheckQStmt_len])
have e69 := ea6.seq (by convert e79 using 1; simp [dpScanCheckIStmt_len, Stmt.len])
have e59 := e_ci.seq (by convert e69 using 1; simp [dpScanCheckIStmt_len, Stmt.len])
have e49 := ea4.seq (by convert e59 using 1; simp [Stmt.len])
have e39 := ea3.seq (by convert e49 using 1; simp [Stmt.len])
have e29 := ea2.seq (by convert e39 using 1; simp [Stmt.len])
have e19 := ea1.seq (by convert e29 using 1; simp [Stmt.len])
convert e19 using 1
simp [dpScanCheckIStmt_len, dpScanCheckQStmt_len, Stmt.seqs, Stmt.len, ifNz_len, hrZ]
refine ⟨ma9, 1 + (1 + (1 + (1 + (t_ci + (1 + (t_cq + (t_if + 1))))))), by omega,
e_seq9,
by dsimp only [ma9]; rw [Function.update_of_ne (by omega), hSt_a8.rZ_eq],
⟨idx + 1, by omega,
staticInv_frame hSt_a8 hma9B (fun r hr => Function.update_of_ne (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) _ _)
(fun a h1 _ => Function.update_of_ne (by unfold baseR at h1; omega) _ _),
layerListInv_of_mem_eq hLLw
(by dsimp only [ma9]; rw [Function.update_of_ne (by omega), hma8Fr _ (by omega), hma7_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)]; exact hLLw.heap_le)
(by dsimp only [ma9]; rw [Function.update_of_ne (by omega), hma8Fr _ (by omega), hma7_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)])
(fun a h1 _ => hma9_mem a (by omega)),
by simp [ma9], by dsimp only [ma9]; rw [Function.update_of_ne (by omega), hma8_Ans],
dpFrame_trans hfrw ⟨fun r hle ha hh => by dsimp only [ma9]; rw [Function.update_of_ne (by omega), hma8Fr r ha, hma7_eq r (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)],
by dsimp only [ma9]; rw [Function.update_of_ne (by omega), hma8Fr _ (by omega), hma7_eq _ (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)],
fun a h64 _ => hma9_mem a h64⟩⟩,
hmeas_lt⟩)
have hLoopInit : LoopInv m1 := ⟨0, Nat.zero_le _,
staticInv_frame hSt hm1B (fun r hr => Function.update_of_ne (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) _ _)
(fun a h1 _ => Function.update_of_ne (by unfold baseR at h1; omega) _ _),
layerListInv_of_mem_eq hLL (by dsimp only [m1]; rw [Function.update_of_ne (by omega)]; exact hLL.heap_le)
(Function.update_of_ne (by omega) _ _) (fun a h1 _ => Function.update_of_ne (by have := heapBase_ge n; omega) _ _),
by have hlh := hLL.list_head
cases finalKeys with
| nil => simpa [m1] using hlh
| cons s rest => simpa [m1] using hlh,
by dsimp only [m1]; rw [Function.update_of_ne (by omega), hm0, hscan_zero]; omega,
⟨fun r hle _ _ => by dsimp only [m1]; rw [Function.update_of_ne (by omega), hm0],
by dsimp only [m1]; rw [Function.update_of_ne (by omega), hm0],
fun a h64 _ => by dsimp only [m1]; rw [Function.update_of_ne (by omega), hm0]⟩⟩
have hmeas_init : meas m1 ≤ finalKeys.length := by
by_cases hex : ∃ (i : ℕ) (hi : i < finalKeys.length), m1 rNode = finalAo finalKeys[i]
· simp only [meas, dif_pos hex]; omega
· simp [meas, hex]
rcases hwh finalKeys.length m1 hLoopInit hmeas_init with
⟨m_end, t_wh, ht_wh, e_wh, ⟨idx_end, hidx_end_le, hSt_end, _, hNd_end, hmA_end, hfr_end⟩, hNd_0⟩
have hidx_full : idx_end = finalKeys.length := by
by_contra hne
have hlt : idx_end < finalKeys.length := by omega
rw [dif_pos hlt] at hNd_end
have := (hLL.rec_bounds finalKeys[idx_end] (List.getElem_mem hlt)).1; have := heapBase_ge n; omega
subst hidx_full
rw [hscan_full] at hmA_end
have ht_all : 1 + t_wh ≤ (dpStates P B g hg).card * (32 * n + 50) + 10 := by
have h1 := Nat.mul_le_mul_right (32 * n + 50) hfk_card
have h2 : 32 * n + 48 + 2 = 32 * n + 50 := by omega
rw [h2] at ht_wh
omega
exact ⟨m_end, 1 + t_wh, ht_all,
by convert e1.seq e_wh using 1; simp [dpScanStmt, Stmt.seqs, Stmt.len, mov_len],
hSt_end, hmA_end, hfr_end⟩
/-! ## Section 6: Top-Level `hoare_dpRun` -/
private def dpRunGroupedStmt : Stmt :=
dpInitStmt.seq ((Stmt.forRange rZ r1 rLayer rLayerRem rN dpLayerBodyStmt).seq dpScanStmt)
private lemma progDPRun_len_eq : progDPRun.len = dpRunGroupedStmt.len := by decide
private lemma progDPRun_compile_eq (base : ℕ) :
progDPRun.compile base = dpRunGroupedStmt.compile base := by
simp only [progDPRun, dpRunGroupedStmt, dpInitStmt, dpInitLoopStmt, dpLayerBodyStmt,
dpScanStmt, dpScanCheckIStmt, dpScanCheckQStmt,
Stmt.forRange, Stmt.ifNz, Stmt.mov, Stmt.inc, Stmt.dec, Stmt.seqs, Stmt.compile, Stmt.len,
List.append_assoc, Nat.add_assoc]
/-- Top-level Hoare triple for `progDPRun`: computes `dpPaths P B g hg n (dpTerminal B)`
and adds it to `rAns` in `O(n^4 * (dpStates P B g hg).card)` RAM steps. -/
theorem hoare_dpRun {P_prog : List Instr} {base n : ℕ}
{P : PartialOrder (Fin n)} {R : Fin n → Fin n → Bool}
{B : Finset (Fin n)} {g : Fin n → Option (Fin n)} {hg : Compatible P B g} {m0 : ℕ → ℕ}
(hR : ∀ u v, R u v = true ↔ P.lt u v)
(hbl : base + 440 ≤ valBound n)
(hans_cap : m0 rAns + dpPaths P B g hg n (dpTerminal B) ≤ valBound n) :
Hoare P_prog base (valBound n) progDPRun
(fun m => StaticInv n R B m ∧ PatEnc g m ∧ QMatEnc (boolClosure (augAdj R B g)) m ∧
∀ a, m a = m0 a)
(fun m => StaticInv n R B m ∧ PatEnc g m ∧ QMatEnc (boolClosure (augAdj R B g)) m ∧
m rAns = m0 rAns + dpPaths P B g hg n (dpTerminal B) ∧
DPFrame n m0 m)
((n + 1) * ((dpStates P B g hg).card * (56 * n * n * n + 330 * n * n + 420 * n + 340) + 10 * n + 40) + 16 * n * n + 70 * n + 60) := by
have hB1000 := valBound_ge_1000 n
have hrN : rN = 0 := rfl; have hrZ : rZ = 1 := rfl; have hr1 : r1 = 2 := rfl
have hrAns : rAns = 7 := rfl; have hrH : rHeap = 17 := rfl
have hrLayer : rLayer = 20 := rfl; have hrLayerRem : rLayerRem = 21 := rfl
have hrCH : rCurHead = 22 := rfl; have hr2N : r2N = 54 := rfl
have hn_le : n ≤ valBound n := by have := valBound_ge_heapBase n; unfold heapBase at this; omega
have hbg_ge : 64 ≤ baseG n := by unfold baseG; omega
have hbq_ge : 64 ≤ baseQ n := by unfold baseQ; omega
have hbg_bk : ∀ u : Fin n, baseG n + u.val < baseKey n := fun u => by
unfold baseG baseKey; have := u.isLt; have : 2 * n * n = n * n + n * n := (by ring); omega
have hbq_bk : ∀ u v : Fin n, baseQ n + u.val * n + v.val < baseKey n := fun u v => by
have huv : u.val * n + v.val < n * n := by
have h1 : (u.val + 1) * n ≤ n * n := Nat.mul_le_mul_right n u.isLt
have h2 : (u.val + 1) * n = u.val * n + n := by ring
have := v.isLt; omega
unfold baseQ baseKey; have : 2 * n * n = n * n + n * n := (by ring); omega
intro hc_raw m ⟨hSt, hPat, hQ, hm0⟩
have hc : Contains P_prog base dpRunGroupedStmt := fun k hk => by
have := hc_raw k (by rw [progDPRun_len_eq]; exact hk)
rwa [progDPRun_compile_eq] at this
-- 1. dpInitStmt
have hi := hoare_dpInit (P_prog := P_prog) (base := base) (n := n) (P := P) (R := R) (B := B) (g := g) (hg := hg) (m0 := m0) hR (by omega)
rcases hi hc.seq_left m ⟨hSt, hPat, hQ, hm0⟩ with
⟨m1, t_i, ht_i, e_i, hSt1, hPat1, hQ1, hm1H, hLL1, hm1A, hfr1⟩
-- 2. forRange rLayer = 0..n-1
let layerStepCap := (dpStates P B g hg).card * (56 * n * n * n + 330 * n * n + 380 * n + 280) + 10 * n + 30
have hfor := Hoare.forRange (P := P_prog) (base := base + 62) (rZ := rZ) (r1 := r1)
(idx := rLayer) (rem := rLayerRem) (lim := rN) (B := valBound n) (N := n) (stepCap := layerStepCap)
(body := dpLayerBodyStmt)
(by omega) (by omega) (by omega) (by omega) (by omega) (by omega) hn_le
(by rw [dpLayerBodyStmt_len]; omega)
(by decide) (by decide) (by decide) (by decide) (by decide) (by decide)
(Inv := fun l ml => StaticInv n R B ml ∧ PatEnc g ml ∧ QMatEnc (boolClosure (augAdj R B g)) ml ∧
ml rHeap ≤ heapBase n + (2 * n + 3) + l * layerHeapBudget n (dpStates P B g hg).card ∧
(∃ ks ao, LayerListInv n (ml rHeap) (dpPaths P B g hg l) ks ao ml) ∧
ml rAns = m0 rAns ∧ DPFrame n m0 ml)
(hInv_init := fun ml ⟨hStl, hPatl, hQl, hHl, ⟨ks, ao, hLLl⟩, hAl, hfrl⟩ _ _ _ => by
have hmB' : MemB n (Function.update (Function.update ml rLayer 0) rLayerRem n) :=
memB_update (memB_update hStl.memB (by omega)) hn_le
have hheq : ∀ a, a ≠ rLayer → a ≠ rLayerRem → Function.update (Function.update ml rLayer 0) rLayerRem n a = ml a :=
fun a h1 h2 => by rw [Function.update_of_ne h2, Function.update_of_ne h1]
exact ⟨staticInv_frame hStl hmB' (fun r hr => hheq r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 _ => hheq a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega)),
fun u => by rw [hheq _ (by omega) (by omega)]; exact hPatl u,
fun u v => by rw [hheq _ (by omega) (by omega)]; exact hQl u v,
by rw [hheq _ (by omega) (by omega)]; exact hHl,
⟨ks, ao, by rw [hheq _ (by omega) (by omega)]; exact layerListInv_of_mem_eq hLLl (by rw [hheq _ (by omega) (by omega)]) (hheq _ (by omega) (by omega)) (fun a h1 _ => hheq a (by have := heapBase_ge n; omega) (by have := heapBase_ge n; omega))⟩,
by rw [hheq _ (by omega) (by omega), hAl],
dpFrame_trans hfrl ⟨fun r hle _ _ => hheq r (by omega) (by omega), hheq _ (by omega) (by omega), fun a h64 _ => hheq a (by omega) (by omega)⟩⟩)
(hInv_step := fun l hl ml ⟨hStl, hPatl, hQl, hHl, ⟨ks, ao, hLLl⟩, hAl, hfrl⟩ _ _ _ _ => by
have hmB' : MemB n (Function.update (Function.update ml rLayer (l + 1)) rLayerRem (n - l - 1)) :=
memB_update (memB_update hStl.memB (by omega)) (by omega)
have hheq : ∀ a, a ≠ rLayer → a ≠ rLayerRem → Function.update (Function.update ml rLayer (l + 1)) rLayerRem (n - l - 1) a = ml a :=
fun a h1 h2 => by rw [Function.update_of_ne h2, Function.update_of_ne h1]
exact ⟨staticInv_frame hStl hmB' (fun r hr => hheq r (by rcases hr with ⟨_, _, _⟩ | _ <;> omega) (by rcases hr with ⟨_, _, _⟩ | _ <;> omega))
(fun a h1 _ => hheq a (by unfold baseR at h1; omega) (by unfold baseR at h1; omega)),
fun u => by rw [hheq _ (by omega) (by omega)]; exact hPatl u,
fun u v => by rw [hheq _ (by omega) (by omega)]; exact hQl u v,
by rw [hheq _ (by omega) (by omega)]; exact hHl,
⟨ks, ao, by rw [hheq _ (by omega) (by omega)]; exact layerListInv_of_mem_eq hLLl (by rw [hheq _ (by omega) (by omega)]) (hheq _ (by omega) (by omega)) (fun a h1 _ => hheq a (by have := heapBase_ge n; omega) (by have := heapBase_ge n; omega))⟩,
by rw [hheq _ (by omega) (by omega), hAl],
dpFrame_trans hfrl ⟨fun r hle _ _ => hheq r (by omega) (by omega), hheq _ (by omega) (by omega), fun a h64 _ => hheq a (by omega) (by omega)⟩⟩)
(hbody := fun l hl hcb ml ⟨⟨hStl, hPatl, hQl, hHl, ⟨ks, ao, hLLl⟩, hAl, hfrl⟩, hmL, hmLR, hmZ, hm1⟩ => by
have hcap_l : ml rHeap + layerHeapBudget n (dpStates P B g hg).card ≤ valBound n := by
have htot := dpRun_heap_le_valBound (P := P) (B := B) (g := g) (hg := hg)
have h1 : (l + 1) * layerHeapBudget n (dpStates P B g hg).card ≤ (n + 1) * layerHeapBudget n (dpStates P B g hg).card :=
Nat.mul_le_mul_right _ (by omega)
have h2 : (l + 1) * layerHeapBudget n (dpStates P B g hg).card = l * layerHeapBudget n (dpStates P B g hg).card + layerHeapBudget n (dpStates P B g hg).card := by ring
omega
have hlay := hoare_dpLayer (P_prog := P_prog) (base := base + 62 + 3) (n := n) (l := l)
(heapStart := ml rHeap) (P := P) (R := R) (B := B) (g := g) (hg := hg)
(curKeys := ks) (curAo := ao) (m0 := ml) hR hl (by omega) hcap_l
rcases hlay hcb ml ⟨hStl, hPatl, hQl, rfl, hLLl, rfl, fun _ => rfl⟩ with
⟨ml', t_l, ht_l, e_l, hStl', hPatl', hQl', hHl', hLLl', hAl', hLl', hLRl', hfrl'⟩
have hHl'_bound : ml' rHeap ≤ heapBase n + (2 * n + 3) + (l + 1) * layerHeapBudget n (dpStates P B g hg).card := by
have : (l + 1) * layerHeapBudget n (dpStates P B g hg).card = l * layerHeapBudget n (dpStates P B g hg).card + layerHeapBudget n (dpStates P B g hg).card := by ring
omega
exact ⟨ml', t_l, ht_l, e_l,
⟨hStl', hPatl', hQl', hHl'_bound, hLLl', hAl'.trans hAl, dpFrame_trans hfrl hfrl'⟩,
hLl'.trans hmL, hLRl'.trans hmLR, hStl'.rZ_eq, hStl'.r1_eq⟩)
rcases hfor (by simpa [dpInitStmt_len, Stmt.len, Nat.add_assoc] using hc.seq_right.seq_left) m1
⟨⟨hSt1, hPat1, hQ1, by omega, ⟨[dpInitial P B g], fun _ => heapBase n, hm1H ▸ hLL1⟩, hm1A, hfr1⟩,
hSt1.rN_eq, hSt1.rZ_eq, hSt1.r1_eq⟩ with
⟨m2, t_for, ht_for, e_for, ⟨hSt2, hPat2, hQ2, hH2, ⟨fks, fao, hLL2⟩, hm2A, hfr2⟩, _, _, _, _⟩
-- 3. dpScanStmt
have hH2_vb : m2 rHeap ≤ valBound n := by
have htot := dpRun_heap_le_valBound (P := P) (B := B) (g := g) (hg := hg)
have h1 : n * layerHeapBudget n (dpStates P B g hg).card ≤ (n + 1) * layerHeapBudget n (dpStates P B g hg).card :=
Nat.mul_le_mul_right _ (by omega)
omega
have hsc := hoare_dpScan (P_prog := P_prog) (base := base + 62 + 308) (n := n) (heapEnd := m2 rHeap)
(P := P) (R := R) (B := B) (g := g) (hg := hg) (finalKeys := fks) (finalAo := fao) (m0 := m2)
(by omega) hH2_vb (by rw [hm2A]; exact hans_cap)
rcases hsc (by simpa [dpInitStmt_len, dpLayerBodyStmt_len, forRange_len, Stmt.len, Nat.add_assoc] using hc.seq_right.seq_right) m2
⟨hSt2, hLL2, fun _ => rfl⟩ with
⟨m3, t_sc, ht_sc, e_sc, hSt3, hm3A, hfr3⟩
have hfr03 : DPFrame n m0 m3 := dpFrame_trans hfr2 hfr3
have ht_all : t_i + (t_for + t_sc) ≤
(n + 1) * ((dpStates P B g hg).card * (56 * n * n * n + 330 * n * n + 420 * n + 340) + 10 * n + 40) + 16 * n * n + 70 * n + 60 := by
let C := (dpStates P B g hg).card
let K1 := 56 * n * n * n + 330 * n * n + 380 * n + 280
let K2 := 56 * n * n * n + 330 * n * n + 420 * n + 340
have hK12 : K1 ≤ K2 := by dsimp only [K1, K2]; omega
have hcard_le : C * (32 * n + 50) ≤ C * K2 :=
Nat.mul_le_mul_left C (by dsimp only [K2]; omega)
have hlay_le : layerStepCap + 4 ≤ C * K2 + 10 * n + 40 := by
have h1 : C * K1 ≤ C * K2 := Nat.mul_le_mul_left C hK12
have h2 : C * K1 + 10 * n + 30 + 4 ≤ C * K2 + 10 * n + 40 := by
have hlin : ∀ a b : ℕ, a ≤ b → a + 10 * n + 30 + 4 ≤ b + 10 * n + 40 := fun a b h => by omega
exact hlin _ _ h1
exact h2
have hmul_le : n * (layerStepCap + 4) ≤ n * (C * K2 + 10 * n + 40) :=
Nat.mul_le_mul_left n hlay_le
have hsplit : (n + 1) * (C * K2 + 10 * n + 40) =
n * (C * K2 + 10 * n + 40) + (C * K2 + 10 * n + 40) := by ring
change t_i + (t_for + t_sc) ≤ (n + 1) * (C * K2 + 10 * n + 40) + 16 * n * n + 70 * n + 60
have ht_sc' : t_sc ≤ C * (32 * n + 50) + 10 := ht_sc
calc t_i + (t_for + t_sc)
≤ (16 * n * n + 70 * n + 40) + ((n * (layerStepCap + 4) + 3) + (C * (32 * n + 50) + 10)) :=
Nat.add_le_add ht_i (Nat.add_le_add ht_for ht_sc')
_ ≤ (16 * n * n + 70 * n + 40) + ((n * (C * K2 + 10 * n + 40) + 3) + (C * K2 + 10)) :=
Nat.add_le_add_left (Nat.add_le_add (Nat.add_le_add_right hmul_le 3) (Nat.add_le_add_right hcard_le 10)) _
_ ≤ (n + 1) * (C * K2 + 10 * n + 40) + 16 * n * n + 70 * n + 60 := by
rw [hsplit]
have hlin : ∀ a b d : ℕ, (d + 70 * n + 40) + ((a + 3) + (b + 10)) ≤ a + (b + 10 * n + 40) + d + 70 * n + 60 := by
intro a b d; omega
exact hlin _ _ _
have e_fsc : Exec P_prog (base + 62)
((Stmt.forRange rZ r1 rLayer rLayerRem rN dpLayerBodyStmt).len + dpScanStmt.len)
(valBound n) m1 m3 (t_for + t_sc) :=
e_for.seq (by convert e_sc using 1; simp [dpLayerBodyStmt_len, forRange_len])
have e_all : Exec P_prog base dpRunGroupedStmt.len (valBound n) m m3 (t_i + (t_for + t_sc)) := by
convert e_i.seq (by convert e_fsc using 1; simp [dpInitStmt_len]) using 1
simp [dpRunGroupedStmt, Stmt.len]
exact ⟨m3, t_i + (t_for + t_sc), ht_all,
by rw [progDPRun_len_eq]; exact e_all,
hSt3,
fun u => by rw [hfr3.mem_low _ (by omega) (hbg_bk u)]; exact hPat2 u,
fun u v => by rw [hfr3.mem_low _ (by omega) (hbq_bk u v)]; exact hQ2 u v,
by rw [hm3A, hm2A], hfr03⟩
end DeadlineDPRAM
Source
STOC 2027 submission #725, Counting Linear Extensions Faster than 2^n, Sections 3-5