import Mathlib import OAI.Combinatorics.IndependentSets.PCP.BoundedIteration import OAI.Combinatorics.IndependentSets.Reduction.StoredFloyd namespace OAI namespace LargeIndependentSets.StoredFloyd open ShortestPaths open IndependentSetsCut.CounterMachine open IndependentSetsGames.Foundations.Complexity open Turing structure Input (B : ℕ) where n : ℕ matrix : Capped.Matrix B n def Input.initial {B : ℕ} (a : Input B) : State B := ⟨a.n,0,a.matrix⟩ def Input.bits {B : ℕ} (a : Input B) : List Bool := BoundedIteration.counted Input.n State.bits Input.initial a def Input.output {B : ℕ} (a : Input B) : State B := ⟨a.n,a.n,a.matrix.allPairs⟩ lemma run_append {B n : ℕ} (a : Capped.Matrix B n) (xs ys : List (Fin n)) : a.run (xs++ys) = (a.run xs).run ys := by induction xs generalizing a with | nil => rfl | cons x xs ih => exact ih (a.pivot x) lemma iterate_initial {B : ℕ} (a : Input B) (i : ℕ) (hi : i≤a.n) : State.step^[i] a.initial = ⟨a.n,i,a.matrix.run ((List.finRange a.n).take i)⟩ := by induction i with | zero => rfl | succ i ih => have hn : i State.step^[a.n] a.initial) := BoundedIteration.certificate State.bits State.step Input.initial Input.n (body B) (lengthPolynomial B) intermediate_bound noncomputable def certificate (B : ℕ) : TM2ComputableInPolyTime Input.bits State.bits (@Input.output B) where tm := (rawCertificate B).tm inputAlphabet := (rawCertificate B).inputAlphabet outputAlphabet := (rawCertificate B).outputAlphabet time := (rawCertificate B).time outputsFun a := by simpa only [iterate_output] using (rawCertificate B).outputsFun a lemma certificate_finiteAlphabet (B : ℕ) (k : (certificate B).tm.K) : Finite ((certificate B).tm.Γ k) := BoundedIteration.certificate_finiteAlphabet State.bits State.step Input.initial Input.n (body B) (lengthPolynomial B) intermediate_bound (body_finiteAlphabet B) k lemma output_threshold (B : ℕ) {n : ℕ} (a : ShortestPaths.Matrix n) (i j : Fin n) (t : ℕ) (ht : t≤B) : ((Input.output (⟨n,Capped.Matrix.ofInteger B a⟩ : Input B)).matrix.get i j).val≤t ↔ a.allPairs.get i j ≤ (t : ℕ∞) := Capped.Matrix.threshold_exact B a i j ht end LargeIndependentSets.StoredFloyd namespace LargeIndependentSets.FloydCarry open IndependentSetsCut.CounterMachine open ShortestPaths open Turing open scoped BigOperators open IndependentSetsGames.Foundations.Complexity structure State (B : ℕ) where core : StoredFloyd.State B payload : List ℕ def State.value {B : ℕ} (s : State B) (w : ℕ) : ℕ := if w<2+s.core.n*s.core.n then s.core.value w else if w=2+s.core.n*s.core.n then s.payload.length else s.payload[w-(3+s.core.n*s.core.n)]?.getD 0 def State.count {B : ℕ} (s : State B) := 3+s.core.n*s.core.n+s.payload.length def State.bits {B : ℕ} (s : State B) := UnaryTables.words s.value s.count def State.step {B : ℕ} (s : State B) : State B := ⟨s.core.step,s.payload⟩ lemma read_core {B : ℕ} (s : State B) (w : ℕ) (hw : w<2+s.core.n*s.core.n) : Expr.wordValue s.bits w=s.core.value w := by rw [State.bits,UnaryTables.wordValue_words _ _ _ (by unfold State.count; omega)] simp [State.value,hw] @[simp] lemma read_n {B : ℕ} (s : State B) : Expr.wordValue s.bits 0=s.core.n := by rw [read_core _ _ (by omega)]; simp [StoredFloyd.State.value] @[simp] lemma read_k {B : ℕ} (s : State B) : Expr.wordValue s.bits 1=s.core.k := by rw [read_core _ _ (by omega)]; simp [StoredFloyd.State.value] @[simp] lemma read_count {B : ℕ} (s : State B) : Expr.wordValue s.bits (2+s.core.n*s.core.n)=s.payload.length := by rw [State.bits,UnaryTables.wordValue_words _ _ _ (by unfold State.count; omega)] simp [State.value] lemma read_payload {B : ℕ} (s : State B) (w : ℕ) (hw : w w)=s.core.step.value w := by by_cases hw0 : w=0 · subst w; simp [StoredFloyd.valueExpr,Expr.eval,StoredFloyd.State.value,StoredFloyd.State.step] by_cases hw1 : w=1 · subst w; simp [StoredFloyd.valueExpr,Expr.eval,StoredFloyd.State.value,StoredFloyd.State.step] have hw2 : 2 ≤ w := by omega have ht : w-2 w)=i.val := by simp [StoredFloyd.rowExpr,Expr.eval,i] have hc : StoredFloyd.colExpr.eval s.bits (fun _ => w)=j.val := by simp [StoredFloyd.colExpr,Expr.eval,j] simp only [StoredFloyd.valueExpr,Expr.cond_eval,Expr.equal_eval,Expr.eval,hw0,hw1,ite_false, ne_eq,not_true_eq_false, sizeExpr_eval,pivotExpr_eval,Expr.lt_eval,StoredFloyd.minExpr_eval,StoredFloyd.cellExpr_eval,hr,hc] by_cases hk : s.core.k w)=s.step.value w := by simp only [valueExpr,Expr.cond_eval,Expr.lt_eval,Expr.eval,coreCount_eval] by_cases hc : w<2+s.core.n*s.core.n · simp [hc,coreValue_eval s w hc,State.value,State.step,StoredFloyd.State.step] · simp only [hc,ite_false,ne_eq,not_true_eq_false,Expr.word_eval,Expr.eval] rw [State.bits,UnaryTables.wordValue_words _ _ _ hw] simp only [State.value,State.step,StoredFloyd.State.step,hc,ite_false] rfl lemma body_spec {B : ℕ} (s : State B) : UnaryTables.words (fun w => (valueExpr B).eval s.bits (fun _ => w)) (countExpr.eval s.bits (fun _ => 0))=s.step.bits := by rw [countExpr_eval] exact UnaryTables.words_congr (valueExpr_eval s) noncomputable def body (B : ℕ) : TM2ComputableInPolyTime State.bits State.bits (@State.step B) := UnaryTables.encodedComputer _ _ _ countExpr (valueExpr B) body_spec lemma body_finiteAlphabet (B : ℕ) : MachineFiniteAlphabet.FiniteAlphabet (body B).tm := UnaryTables.encodedComputer_finiteAlphabet _ _ _ _ _ _ structure Input (B : ℕ) where core : StoredFloyd.Input B payload : List ℕ def Input.initial {B : ℕ} (a : Input B) : State B := ⟨a.core.initial,a.payload⟩ def Input.count {B : ℕ} (a : Input B) := a.core.n def Input.bits {B : ℕ} (a : Input B) : List Bool := BoundedIteration.counted Input.count State.bits Input.initial a def Input.output {B : ℕ} (a : Input B) : State B := ⟨a.core.output,a.payload⟩ lemma iterate_pair {B : ℕ} (s : State B) (i : ℕ) : State.step^[i] s=⟨StoredFloyd.State.step^[i] s.core,s.payload⟩ := by induction i with | zero => rfl | succ i ih => rw [Function.iterate_succ_apply',ih,Function.iterate_succ_apply']; rfl lemma iterate_output {B : ℕ} (a : Input B) : State.step^[a.count] a.initial=a.output := by rw [iterate_pair] dsimp only [Input.initial,Input.count,Input.output] rw [StoredFloyd.iterate_output] lemma wordValue_le_length (s : List Bool) (w : ℕ) : Expr.wordValue s w ≤ s.length := by unfold Expr.wordValue calc _ ≤ ∑ _ ∈ Finset.range s.length, (1:ℕ) := by apply Finset.sum_le_sum intro i _hi unfold Expr.bitValue split <;> split <;> simp _ = _ := by simp lemma initial_length_le {B : ℕ} (a : Input B) : a.initial.bits.length≤a.bits.length := by simp [Input.bits,BoundedIteration.counted] lemma payload_length_le {B : ℕ} (a : Input B) : a.payload.length≤a.bits.length := by change a.initial.payload.length ≤ a.bits.length rw [← read_count a.initial] exact (wordValue_le_length _ _).trans (initial_length_le a) lemma payload_value_le {B : ℕ} (a : Input B) (w : ℕ) : a.payload[w]?.getD 0≤a.bits.length := by by_cases hw : w State.step^[a.count] a.initial) := BoundedIteration.certificate State.bits State.step Input.initial Input.count (body B) (lengthPolynomial B) intermediate_bound noncomputable def certificate (B : ℕ) : TM2ComputableInPolyTime Input.bits State.bits (@Input.output B) where tm := (rawCertificate B).tm inputAlphabet := (rawCertificate B).inputAlphabet outputAlphabet := (rawCertificate B).outputAlphabet time := (rawCertificate B).time outputsFun a := by simpa only [iterate_output] using (rawCertificate B).outputsFun a lemma certificate_finiteAlphabet (B : ℕ) (k : (certificate B).tm.K) : Finite ((certificate B).tm.Γ k) := BoundedIteration.certificate_finiteAlphabet State.bits State.step Input.initial Input.count (body B) (lengthPolynomial B) intermediate_bound (body_finiteAlphabet B) k end LargeIndependentSets.FloydCarry end OAI