/-! proofs/arena_plan.lean Formal contract for `memory.vplan` arena safety (docs/ARCHITECTURE.md §4/§8), validated statically before the first GPU launch: 1. Declared buffers in the same arena never overlap (checked offsets). 2. Every op read/write index is within the declaration table. 3. No use-before-write: a read is covered by an input or by the writes of strictly earlier ops. Mirrors `vetta_infer_abi::arena`. Zero `sorryAx`; checked with `lean proofs/arena_plan.lean`. -/ namespace Vetta.Proofs.Arena /-- A declared buffer: arena id + byte interval `[off, off + len)`. -/ structure Decl where arena : Nat off : Nat len : Nat deriving DecidableEq, Repr /-- One micro-op: reads/writes are indices into the declaration table. -/ structure Op where reads : List Nat writes : List Nat deriving DecidableEq, Repr /-- A memory plan: declarations, the op sequence, and pre-initialized inputs. -/ structure Plan where decls : List Decl ops : List Op inputs : List Nat deriving DecidableEq, Repr /-- Two declarations are disjoint if in different arenas or non-overlapping intervals (touching boundaries are allowed). -/ def intervalsDisjoint (a b : Decl) : Bool := decide (a.arena ≠ b.arena) || decide (a.off + a.len ≤ b.off) || decide (b.off + b.len ≤ a.off) theorem intervalsDisjoint_true {a b : Decl} (h : intervalsDisjoint a b = true) : a.arena ≠ b.arena ∨ a.off + a.len ≤ b.off ∨ b.off + b.len ≤ a.off := by simp [intervalsDisjoint, Bool.or_eq_true, decide_eq_true_eq] at h rcases h with (h1 | h2) | h3 · exact Or.inl h1 · exact Or.inr (Or.inl h2) · exact Or.inr (Or.inr h3) /-- Pairwise disjointness check over the declaration table. -/ def allPairsDisjoint : List Decl → Bool | [] => true | d :: ds => ds.all (intervalsDisjoint d) && allPairsDisjoint ds /-- Theorem 1 (no overlap): a passing check means any two DISTINCT declarations are in different arenas or have non-overlapping intervals. -/ theorem allPairsDisjoint_sound {ds : List Decl} (h : allPairsDisjoint ds = true) : ∀ a ∈ ds, ∀ b ∈ ds, a ≠ b → a.arena ≠ b.arena ∨ a.off + a.len ≤ b.off ∨ b.off + b.len ≤ a.off := by induction ds with | nil => intro a ha; simp at ha | cons d ds ih => simp [allPairsDisjoint, Bool.and_eq_true, List.all_eq_true] at h obtain ⟨hhead, htail⟩ := h intro a ha b hb hne simp at ha hb rcases ha with rfl | ha <;> rcases hb with rfl | hb · exact absurd rfl hne · exact intervalsDisjoint_true (hhead b hb) · have := intervalsDisjoint_true (hhead a ha) rcases this with h1 | h2 | h3 · exact Or.inl (Ne.symm h1) · exact Or.inr (Or.inr h2) · exact Or.inr (Or.inl h3) · exact ih htail a ha b hb hne /-- Index-bounds check for every op against `n` declarations. -/ def indicesOk (n : Nat) (ops : List Op) : Bool := ops.all (fun op => op.reads.all (fun r => decide (r < n)) && op.writes.all (fun w => decide (w < n))) /-- Theorem 2 (checked bounds): a passing check bounds every read/write index. -/ theorem indicesOk_sound {n : Nat} {ops : List Op} (h : indicesOk n ops = true) : ∀ op ∈ ops, (∀ r ∈ op.reads, r < n) ∧ (∀ w ∈ op.writes, w < n) := by simp [indicesOk, List.all_eq_true, Bool.and_eq_true, decide_eq_true_eq] at h intro op hop exact h op hop /-- Sequential use-before-write check: `written` accumulates prior writes. -/ def checkOps (inputs written : List Nat) : List Op → Bool | [] => true | op :: rest => op.reads.all (fun r => decide (r ∈ inputs ++ written)) && checkOps inputs (written ++ op.writes) rest /-- Theorem 3 (no use-before-write): if the check passes, every read of an op at ANY position is covered by the inputs, the initial written set, or the writes of the strictly preceding ops (`pre`). -/ theorem checkOps_sound (inputs written : List Nat) : ∀ (ops : List Op), checkOps inputs written ops = true → ∀ (pre : List Op) (op : Op) (post : List Op), ops = pre ++ op :: post → ∀ r ∈ op.reads, r ∈ inputs ++ written ++ pre.flatMap Op.writes := by intro ops induction ops generalizing written with | nil => intro h pre op post heq; cases pre <;> simp at heq | cons o os ih => intro h pre op post heq r hr simp [checkOps, Bool.and_eq_true, List.all_eq_true, decide_eq_true_eq] at h obtain ⟨hreads, hrest⟩ := h cases pre with | nil => simp at heq obtain ⟨rfl, rfl⟩ := heq simpa using hreads r hr | cons p pre' => simp at heq obtain ⟨rfl, heq'⟩ := heq have := ih (written ++ o.writes) hrest pre' op post heq' r hr simpa [List.flatMap, List.append_assoc] using this /-- The full `memory.vplan` validator (mirrors `abi::arena::validate`). -/ def validate (p : Plan) : Bool := allPairsDisjoint p.decls && indicesOk p.decls.length p.ops && p.inputs.all (fun i => decide (i < p.decls.length)) && checkOps p.inputs [] p.ops /-- Theorem 4: validation implies pairwise no-overlap. -/ theorem validate_sound_disjoint {p : Plan} (h : validate p = true) : ∀ a ∈ p.decls, ∀ b ∈ p.decls, a ≠ b → a.arena ≠ b.arena ∨ a.off + a.len ≤ b.off ∨ b.off + b.len ≤ a.off := by simp only [validate, Bool.and_eq_true] at h obtain ⟨⟨⟨hap, _⟩, _⟩, _⟩ := h exact allPairsDisjoint_sound hap /-- Theorem 5: validation implies all op indices and inputs are in bounds. -/ theorem validate_sound_bounds {p : Plan} (h : validate p = true) : (∀ op ∈ p.ops, (∀ r ∈ op.reads, r < p.decls.length) ∧ (∀ w ∈ op.writes, w < p.decls.length)) ∧ (∀ i ∈ p.inputs, i < p.decls.length) := by simp only [validate, Bool.and_eq_true] at h obtain ⟨⟨⟨_, hidx⟩, hin⟩, _⟩ := h have hin' : ∀ i ∈ p.inputs, i < p.decls.length := by simpa [List.all_eq_true, decide_eq_true_eq] using hin exact ⟨indicesOk_sound hidx, hin'⟩ /-- Theorem 6: validation implies no use-before-write anywhere in the plan. -/ theorem validate_sound_use_before_write {p : Plan} (h : validate p = true) : ∀ (pre : List Op) (op : Op) (post : List Op), p.ops = pre ++ op :: post → ∀ r ∈ op.reads, r ∈ p.inputs ++ pre.flatMap Op.writes := by simp only [validate, Bool.and_eq_true] at h obtain ⟨⟨⟨_, _⟩, _⟩, hcheck⟩ := h intro pre op post heq r hr have := checkOps_sound p.inputs [] p.ops hcheck pre op post heq r hr simpa using this /-- Theorem 7 (rejection completeness, overlap): two distinct same-arena declarations with overlapping intervals force validation to fail. -/ theorem validate_rejects_overlap {p : Plan} {a b : Decl} (ha : a ∈ p.decls) (hb : b ∈ p.decls) (hne : a ≠ b) (hsame : a.arena = b.arena) (hov : ¬ (a.off + a.len ≤ b.off) ∧ ¬ (b.off + b.len ≤ a.off)) : validate p = false := by by_cases hv : validate p = true · have := validate_sound_disjoint hv a ha b hb hne rcases this with h1 | h2 | h3 · exact absurd hsame h1 · exact absurd h2 hov.1 · exact absurd h3 hov.2 · simpa using hv /-- Theorem 8: the empty plan is valid. -/ theorem empty_plan_valid : validate ⟨[], [], []⟩ = true := by decide -- Concrete acceptance/rejection examples (computational checks) example : validate ⟨[⟨0, 0, 64⟩, ⟨0, 64, 32⟩, ⟨1, 0, 128⟩], [⟨[0], [1]⟩, ⟨[1, 0], [2]⟩], [0]⟩ = true := by decide example : validate ⟨[⟨0, 0, 64⟩, ⟨0, 32, 32⟩], [], []⟩ = false := by decide example : validate ⟨[⟨0, 0, 8⟩, ⟨0, 8, 8⟩], [⟨[0], [1]⟩], []⟩ = false := by decide example : validate ⟨[⟨0, 0, 8⟩], [⟨[5], []⟩], []⟩ = false := by decide -- Axiom audit (standard core axioms only, 0 sorryAx) #print axioms allPairsDisjoint_sound #print axioms indicesOk_sound #print axioms checkOps_sound #print axioms validate_sound_disjoint #print axioms validate_sound_bounds #print axioms validate_sound_use_before_write #print axioms validate_rejects_overlap #print axioms empty_plan_valid end Vetta.Proofs.Arena