import Mathlib import OAI.Computability.DeterministicSum.NewtonCommands import OAI.Computability.DeterministicSum.MatrixDown namespace OAI /-! Return-count stacks and uniform upward matrix recursion. -/ namespace DeterministicThreeSum.Structured.Indexed.MatrixLayout open Command DeterministicThreeSum.Rectangular Axis Finset open scoped BigOperators theorem combine_level_correct {w T a b k r d t P S C : ℕ} (state : Data) (f : FixedFormula (ZMod T) a b k r) (z : OutputBatch (ZMod T) a d (batchCount t k*r)) (data co : ℕ → ℕ) (ha : 0 rfl | succ d ih => exact ih (batchCount t k*r) theorem command_correct {w k r M d t : ℕ} (s : Data) (hk : 0batchCount (finalCount k r i t) k) d ∧ (∀ j, j≠0 → j≠2 → j≠8 → j≠9 → out.registers j=s.registers j) := by let Inv : ℕ → Data → Prop := fun i u => u.registers 0=i ∧ u.registers 2=finalCount k r i t ∧ u.registers 1=d ∧ u.registers 3=M ∧ u.memory=written s M (fun j=>batchCount (finalCount k r j t) k) i have hy : ∀ i u, i simp [u,prepared,put,environment,h2,h3,h4,h5,h6, show (2:ℕ)≠K by omega,show (3:ℕ)≠K by omega,show (4:ℕ)≠K by omega, show (5:ℕ)≠K by omega,show (6:ℕ)≠K by omega] obtain ⟨c,v,ev,hcv,hframe,hzero,hm,houtside⟩ := pass_exact u x co hr (Nat.mul_pos hk hQ) hB hT hadd hmul hN hP hS hC hdisS hdisC hregsu hx hc have hp (a : Fin 8) (ha : a.val≠0) : v.registers a.val= environment T (A*(k*Q)*B) P S C B A 0 a := (hframe a.val a.isLt ha).trans (hregsu a) have h3v : v.registers 3=P := by simpa [environment] using hp ⟨3,by omega⟩ (by norm_num) have h4v : v.registers 4=S := by simpa [environment] using hp ⟨4,by omega⟩ (by norm_num) have h6v : v.registers 6=B := by simpa [environment] using hp ⟨6,by omega⟩ (by norm_num) have ew:=update_correct v hQW (by omega) (by omega) hBW hBQ h3v h4v h6v let out:=updated v Q P S B refine ⟨5+(c+4),out,Eval.seq eu (Eval.seq ev ew),by omega,?_,?_,?_,hm,houtside⟩ · intro a have h1v : v.registers 1=A*(k*Q)*B := by simpa [environment] using hp ⟨1,by omega⟩ (by norm_num) have h2v : v.registers 2=T := by simpa [environment] using hp ⟨2,by omega⟩ (by norm_num) have h5v : v.registers 5=C := by simpa [environment] using hp ⟨5,by omega⟩ (by norm_num) have h7v : v.registers 7=A := by simpa [environment] using hp ⟨7,by omega⟩ (by norm_num) fin_cases a <;> simp [out,updated,put,environment,hzero,h1v,h2v,h5v,h7v] · have hv:=protected_frame ev hKw 0 simpa [out,updated,put,u,prepared,show K≠0 by omega,show K≠1 by omega, show K≠3 by omega,show K≠4 by omega,show K≠6 by omega,show K≠7 by omega, show K≠8 by omega] using hv · have hv:=protected_frame ev hKw 1 simpa [out,updated,put,u,prepared,Function.update_apply,show K≠0 by omega,show K+1≠0 by omega,show K+1≠1 by omega, show K+1≠3 by omega,show K+1≠4 by omega,show K+1≠6 by omega, show K+1≠7 by omega,show K+1≠8 by omega,show K+1≠K by omega] using hv end DeterministicThreeSum.Structured.Indexed.MatrixUp namespace DeterministicThreeSum.Structured.Indexed.MatrixUp open Command DeterministicThreeSum.Rectangular Axis def Bounds (Q k r L : ℕ) (A : ℕ → ℕ) : ℕ → ℕ → ℕ → Prop | 0,_,N => N≤L | d+1,B,N => N≤L ∧ 0 N | d+1,B,_ => outputSize Q k A d (B*Q) (A d*(k*Q)*B) def value (T Q k r : ℕ) (co : ℕ → ℕ) : ℕ → ℕ → (ℕ → ℕ) → (ℕ → ℕ) | 0,_,x => x | d+1,B,x => value T Q k r co d (B*Q) (mapValue T r (k*Q) B x co) theorem command_correct {w T Q k r K L n B N P S C M : ℕ} (s : Data) (x co A : ℕ → ℕ) (hK : 10≤K) (hKw : (pass r (k*Q)).writes.sup id have ht : test w s .lt (.register (K+1)) (.register K)=false := by simp [test,operand_register,evalTest,hptr,hbase] exact ⟨1,s,Eval.loopFalse ht,by simp,h2,by simpa [banks_zero] using h3, by simpa [banks_zero] using h4,h5,by simpa using h6,by simpa using hptr,hbase, by simpa [outputSize,value,banks_zero] using hx,by simp⟩ | succ n ih => obtain ⟨hNL,hA,hinput,houtput,hnext⟩:=hbounds have hR : 0.trans houtput have hAW : A nhx i (hi.trans_le hinput)) hc have h2u : u.registers 2=T := by simpa [environment] using hu ⟨2,by omega⟩ have h3u : u.registers 3=S := by simpa [environment] using hu ⟨3,by omega⟩ have h4u : u.registers 4=P := by simpa [environment] using hu ⟨4,by omega⟩ have h5u : u.registers 5=C := by simpa [environment] using hu ⟨5,by omega⟩ have h6u : u.registers 6=B*Q := by simpa [environment] using hu ⟨6,by omega⟩ have hx' : ∀ i, i⟨hm i hi,mapValue_lt hT _ _ _ _ _ _⟩ have hc' : ∀ i, i<(k*Q)*r → u.memory (C+i)=some (co i) ∧ co i