import Mathlib.Algebra.Polynomial.Eval.Defs import Mathlib.Tactic.Ring import OAI.Computability.BinPacking.CookLevin.PolynomialMachine import OAI.Computability.BinPacking.Information.SourceHonest import OAI.Computability.BinPacking.Information.SourceLoopInit import OAI.Computability.BinPacking.PCP.FinalBooleanVerifier import OAI.Computability.BinPacking.PCP.SourceAmplification namespace OAI noncomputable section namespace BinPackingGames.Foundations.Hastad.SourceBounds open Target open BinPackingGames.Reduction theorem formulaBits_length (F : Formula) : (Complexity.formulaBits F).length = F.variables + F.clauses.length + 2 + (Complexity.encodeWords (F.clauses.flatMap Complexity.clauseWords)).length := by simp only [Complexity.formulaBits, Complexity.formulaWords, Complexity.encodeWords_append, List.length_append, Complexity.encodeWords, Complexity.encodeWord_length, List.length_nil] omega theorem formulaBits_length_ge_variables (F : Formula) : F.variables ≤ (Complexity.formulaBits F).length := by rw [formulaBits_length] omega theorem formulaBits_length_ge_clauses (F : Formula) : F.clauses.length ≤ (Complexity.formulaBits F).length := by rw [formulaBits_length] omega theorem formulaBits_length_ge_two (F : Formula) : 2 ≤ (Complexity.formulaBits F).length := by rw [formulaBits_length] omega def bitCountFormula (n m u : ℕ) : ℕ := n ^ u * 2 ^ (2 ^ u) + m ^ u * 2 ^ (8 ^ u) + 1 def bitCoefficient (u : ℕ) : ℕ := 2 ^ (2 ^ u) + 2 ^ (8 ^ u) + 1 def testCoefficient (u D : ℕ) : ℕ := 3 ^ u * 2 ^ (2 ^ u) * D ^ (8 ^ u) * 2 ^ (8 ^ u) def occurrenceCoefficient (u D : ℕ) : ℕ := testCoefficient u D + 1 theorem one_le_input_power (F : Formula) (u : ℕ) : 1 ≤ (Complexity.formulaBits F).length ^ u := by apply Nat.one_le_pow have h := formulaBits_length_ge_two F omega theorem bitCountFormula_le_input (F : Formula) (u : ℕ) : bitCountFormula F.variables F.clauses.length u ≤ bitCoefficient u * (Complexity.formulaBits F).length ^ u := by have hn := Nat.pow_le_pow_left (formulaBits_length_ge_variables F) u have hm := Nat.pow_le_pow_left (formulaBits_length_ge_clauses F) u have hpos := one_le_input_power F u unfold bitCountFormula bitCoefficient calc _ ≤ (Complexity.formulaBits F).length ^ u * 2 ^ (2 ^ u) + (Complexity.formulaBits F).length ^ u * 2 ^ (8 ^ u) + (Complexity.formulaBits F).length ^ u := Nat.add_le_add (Nat.add_le_add (Nat.mul_le_mul_right _ hn) (Nat.mul_le_mul_right _ hm)) hpos _ = _ := by ring theorem testCountFormula_le_input (F : Formula) (u D : ℕ) : (F.clauses.length * 3) ^ u * 2 ^ (2 ^ u) * D ^ (8 ^ u) * 2 ^ (8 ^ u) ≤ testCoefficient u D * (Complexity.formulaBits F).length ^ u := by have hm := Nat.pow_le_pow_left (formulaBits_length_ge_clauses F) u calc _ = testCoefficient u D * F.clauses.length ^ u := by simp only [testCoefficient, Nat.mul_pow] ring _ ≤ _ := Nat.mul_le_mul_left _ hm theorem testCountFormula_add_one_le_input (F : Formula) (u D : ℕ) : (F.clauses.length * 3) ^ u * 2 ^ (2 ^ u) * D ^ (8 ^ u) * 2 ^ (8 ^ u) + 1 ≤ occurrenceCoefficient u D * (Complexity.formulaBits F).length ^ u := by calc _ ≤ testCoefficient u D * (Complexity.formulaBits F).length ^ u + (Complexity.formulaBits F).length ^ u := Nat.add_le_add (testCountFormula_le_input F u D) (one_le_input_power F u) _ = _ := by simp only [occurrenceCoefficient, Nat.add_mul, Nat.one_mul] def sourceSizeBound (V E u L : ℕ) : ℕ := V * L ^ u + E * L ^ u + 2 + E * L ^ u * (3 * (V * L ^ u) + 2) theorem sourceBits_length_le (input : SourceEncoding.Input) (V E u L : ℕ) (hv : input.variables ≤ V * L ^ u) (he : input.equations.length ≤ E * L ^ u) : (SourceEncoding.inputBits input).length ≤ sourceSizeBound V E u L := by apply (SourceEncoding.inputBits_length_le input).trans unfold sourceSizeBound exact Nat.add_le_add (Nat.add_le_add_right (Nat.add_le_add hv he) 2) (Nat.mul_le_mul he (Nat.add_le_add_right (Nat.mul_le_mul_left 3 hv) 2)) noncomputable def sourcePolynomial (u D : ℕ) : Polynomial ℕ := let V := Polynomial.C (bitCoefficient u) * Polynomial.X ^ u let E := Polynomial.C (occurrenceCoefficient u D) * Polynomial.X ^ u V + E + Polynomial.C 2 + E * (Polynomial.C 3 * V + Polynomial.C 2) theorem sourcePolynomial_eval (u D L : ℕ) : (sourcePolynomial u D).eval L = sourceSizeBound (bitCoefficient u) (occurrenceCoefficient u D) u L := by simp [sourcePolynomial, sourceSizeBound] open SourceOccurrences SourceContexts theorem card_testTape (u D : ℕ) : Fintype.card (SourceTape.TestTape (I u) (J u) D) = 2 ^ (2 ^ u) * (D ^ (8 ^ u) * 2 ^ (8 ^ u)) := by rw [SourceTape.card_testTape, card_I, card_J] theorem testTapeEncoding_size (u D : ℕ) : (SourceOccurrences.testTapeEncoding u D).size = 2 ^ (2 ^ u) * (D ^ (8 ^ u) * 2 ^ (8 ^ u)) := rfl theorem card_sourceIndex (F : Formula) (u D : ℕ) : Fintype.card (SourceIndex F u D) = (F.clauses.length ^ u * 3 ^ u) * (2 ^ (2 ^ u) * (D ^ (8 ^ u) * 2 ^ (8 ^ u))) := by rw [(sourceIndexEncoding F u D).card_eq_size] rfl theorem nBits_le_input (F : Formula) (u : ℕ) : nBits F u ≤ bitCoefficient u * (Complexity.formulaBits F).length ^ u := by have h := bitCountFormula_le_input F u simpa only [nBits_eq, bitCountFormula, Nat.add_assoc] using h theorem rawSourceList_length_eq (F : Formula) (u D : ℕ) : (rawSourceList F u D).length = testCoefficient u D * F.clauses.length ^ u := by rw [rawSourceList_length] unfold testCoefficient ac_rfl theorem rawSourceList_length_le_input (F : Formula) (u D : ℕ) : (rawSourceList F u D).length ≤ testCoefficient u D * (Complexity.formulaBits F).length ^ u := by rw [rawSourceList_length_eq] exact Nat.mul_le_mul_left _ (Nat.pow_le_pow_left (formulaBits_length_ge_clauses F) u) theorem sourceList_length_le_input (F : Formula) (u D : ℕ) : (sourceList F u D).length ≤ occurrenceCoefficient u D * (Complexity.formulaBits F).length ^ u := by calc _ ≤ (rawSourceList F u D).length + 1 := sourceList_length_le_raw_add_one F u D _ ≤ testCoefficient u D * (Complexity.formulaBits F).length ^ u + (Complexity.formulaBits F).length ^ u := Nat.add_le_add (rawSourceList_length_le_input F u D) (one_le_input_power F u) _ = _ := by simp only [occurrenceCoefficient, Nat.add_mul, Nat.one_mul] theorem sourceInput_bits_polynomial (F : Formula) (u D : ℕ) (hD : 0 < D) : (SourceEncoding.inputBits (sourceInput F u D hD)).length ≤ (sourcePolynomial u D).eval (Complexity.formulaBits F).length := by rw [sourcePolynomial_eval] exact sourceBits_length_le (sourceInput F u D hD) (bitCoefficient u) (occurrenceCoefficient u D) u (Complexity.formulaBits F).length (nBits_le_input F u) (sourceList_length_le_input F u D) theorem fixed_parameters_source_size (u D : ℕ) (hD : 0 < D) : ∃ p : Polynomial ℕ, ∀ F : Formula, (SourceEncoding.inputBits (sourceInput F u D hD)).length ≤ p.eval (Complexity.formulaBits F).length := ⟨sourcePolynomial u D, fun F => sourceInput_bits_polynomial F u D hD⟩ end BinPackingGames.Foundations.Hastad.SourceBounds namespace BinPackingGames.Foundations.Hastad.SourceSoundness open scoped BigOperators open Target SourceContexts SourceOccurrences SourceGame def leftTable (F : Formula) (u : ℕ) (bits : Fin (nBits F u) → Bool) (v : VariableContext F u) : HalfCube (leftAnchor u) → Bool := fun h => bits ((proofEncoding F u).code (.inl (v, h.val))) def rightOracle (F : Formula) (u : ℕ) (bits : Fin (nBits F u) → Bool) (c : ClauseContext F u) : ConditionedOracle (validJ F c) := match h : rightAnchor F c with | none => .empty ((rightAnchor_none_iff F c).mp h) | some j₀ => .stored j₀ (fun h => bits ((proofEncoding F u).code (.inr (.inl (c, extendRestricted (validJ F c) h.val))))) @[simp] theorem rightOracle_answer (F : Formula) (u : ℕ) (bits : Fin (nBits F u) → Bool) (c : ClauseContext F u) : (rightOracle F u bits c).answer = rightResponse F u bits c := by unfold rightOracle split <;> simp_all [rightResponse, ConditionedOracle.answer] @[simp] theorem leftTable_answer (F : Formula) (u : ℕ) (bits : Fin (nBits F u) → Bool) (v : VariableContext F u) : foldedAnswer (leftAnchor u) (leftTable F u bits v) = leftResponse F u bits v := rfl theorem sourceList_sound (F : Formula) (hne : F.clauses ≠ []) (u D : ℕ) (hD : 0 < D) (hDtwo : 2 ≤ D) (δ : ℝ) (hδ : 0 ≤ δ) (hvalue : ((baseGame F hne).repetition u).value ≤ 4 * (D : ℝ)⁻¹ * δ ^ 2) (bits : Fin (nBits F u) → Bool) : ((sourceList F u D).countP (fun e => BinPackingGames.Reduction.CloneGap.satisfied e bits) : ℝ) / (sourceList F u D).length ≤ (1 + δ) / 2 := by rw [sourceList_nonempty F u D hne, rawSourceList_acceptance F u D hD] have hε : (0 : ℝ) < (D : ℝ)⁻¹ := inv_pos.mpr (Nat.cast_pos.mpr hD) have hε' : (D : ℝ)⁻¹ ≤ (1 : ℝ) / 2 := by have hd : (2 : ℝ) ≤ D := by exact_mod_cast hDtwo simpa only [one_div] using (inv_le_inv₀ (Nat.cast_pos.mpr hD) (by norm_num : (0 : ℝ) < 2)).mpr hd have h := source_acceptance_le_split F hne u ((D : ℝ)⁻¹) δ (fun _ => leftAnchor u) (leftTable F u bits) (rightOracle F u bits) hε hε' hδ hvalue simpa only [leftTable_answer, rightOracle_answer] using h theorem sourceList_sound_rat (F : Formula) (hne : F.clauses ≠ []) (u D : ℕ) (hD : 0 < D) (hDtwo : 2 ≤ D) (δ : ℚ) (hδ : 0 ≤ δ) (hvalue : ((baseGame F hne).repetition u).value ≤ 4 * (D : ℝ)⁻¹ * (δ : ℝ) ^ 2) (bits : Fin (nBits F u) → Bool) : ((sourceList F u D).countP (fun e => BinPackingGames.Reduction.CloneGap.satisfied e bits) : ℚ) / (sourceList F u D).length ≤ (1 + δ) / 2 := by have h := sourceList_sound F hne u D hD hDtwo (δ : ℝ) (by exact_mod_cast hδ) hvalue bits apply (Rat.cast_le (K := ℝ)).mp push_cast exact h end BinPackingGames.Foundations.Hastad.SourceSoundness namespace BinPackingGames.Foundations.Hastad.SourceHeaderCounts open Turing Complexity open MachineComposition namespace Eval open CookLevin.PolynomialMachine variable {K Λ σ : Type} [DecidableEq K] abbrev Label (p : Polynomial Nat) := Unit ⊕ (MachineHorner.Label (width p) ⊕ Unit) def start (p : Polynomial Nat) : Label p := .inl () def inner (p : Polynomial Nat) (l : MachineHorner.Label (width p)) : Label p := .inr (.inl l) def finish (p : Polynomial Nat) : Label p := .inr (.inr ()) def word (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) : K → List Bool := Function.extend slots (fieldWord p) (fun _ => []) def indices (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) : List K := (fieldIndices p).map slots omit [DecidableEq K] in theorem indices_nodup (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) : (indices p slots).Nodup := (fieldIndices_nodup p).map slots.injective omit [DecidableEq K] in @[simp] theorem word_slot (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) (i : MachineHorner.Layout (width p)) : word p slots (slots i) = fieldWord p i := slots.injective.extend_apply _ _ i omit [DecidableEq K] in @[simp] theorem control_not_mem (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) (i : Fin 6) : slots (.inl i) ∉ indices p slots := by simp [indices, slots.injective.eq_iff] omit [DecidableEq K] in @[simp] theorem digit_mem (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) (i : Fin (width p)) : slots (.inr i) ∈ indices p slots := by simp [indices, slots.injective.eq_iff] def prepared (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) (base : K → List Bool) : K → List Bool := prependFieldsTapes (word p slots) (indices p slots) base @[simp] theorem prepared_control (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) (base : K → List Bool) (i : Fin 6) : prepared p slots base (slots (.inl i)) = base (slots (.inl i)) := by rw [prepared, prependFieldsTapes_apply _ _ (indices_nodup p slots)] simp @[simp] theorem prepared_digit (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) (base : K → List Bool) (i : Fin (width p)) (hi : base (slots (.inr i)) = []) : prepared p slots base (slots (.inr i)) = encodeWord (digit p i.val) := by rw [prepared, prependFieldsTapes_apply _ _ (indices_nodup p slots)] simp [fieldWord, hi] theorem clear_result (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) (base : K → List Bool) (result : Nat) (hcoeff : ∀ i : Fin (width p), base (slots (.inr i)) = []) : clearFieldsTapes (fun k => (word p slots k).length) (indices p slots) (MachineHorner.resultTapes slots (prepared p slots base) result) = MachineHorner.resultTapes slots base result := by funext k rw [clearFieldsTapes_apply _ _ (indices_nodup p slots)] by_cases h : k ∈ indices p slots · obtain ⟨q, hq, rfl⟩ := List.mem_map.mp h obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hq simp [MachineHorner.resultTapes, slots.injective.eq_iff, prepared_digit, fieldWord, hcoeff] · by_cases hd : k = slots (.inl 3) · subst k simp [MachineHorner.resultTapes] · simp [h, MachineHorner.resultTapes, hd, prepared, prependFieldsTapes_apply _ _ (indices_nodup p slots)] def statement (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) (labels : Label p → Λ) (exit : Option Λ) : Label p → TM2.Stmt (fun _ : K => Bool) Λ (MachineHorner.State σ) | .inl _ => prependFields (word p slots) (indices p slots) (.goto fun _ => labels (inner p .start)) | .inr (.inl l) => MachineHorner.statement slots (fun l => labels (inner p l)) (some (labels (finish p))) l | .inr (.inr _) => clearFields (fun k => (word p slots k).length) (indices p slots) (Reduction.MachineTransfer.exitAt (slots (.inl 3)) exit) def steps (p : Polynomial Nat) (n : Nat) : Nat := MachineHorner.steps n (digit p) (width p) + 2 theorem trace (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) (labels : Label p → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (MachineHorner.State σ)) (atLabels : ∀ l, program (labels l) = statement p slots labels exit l) (base : K → List Bool) (n : Nat) (hinput : base (slots (.inl 0)) = encodeWord n) (hcoeff : ∀ i : Fin (width p), base (slots (.inr i)) = []) (clean : MachineHorner.Clean slots base) (ambient : σ) (register : Option Bool) : (advance (TM2.step program))^[steps p n] (some ⟨some (labels (start p)), ((ambient, ()), register), base⟩) = some ⟨exit, ((ambient, ()), none), MachineHorner.resultTapes slots base (p.eval n)⟩ := by have hp : (advance (TM2.step program))^[1] (some ⟨some (labels (start p)), ((ambient, ()), register), base⟩) = some ⟨some (labels (inner p .start)), ((ambient, ()), register), prepared p slots base⟩ := by change some (TM2.stepAux (program (labels (start p))) _ _) = _ rw [atLabels] change some (TM2.stepAux (prependFields _ _ _) _ _) = _ rw [stepAux_prependFields] rfl have hh := MachineHorner.hornerTrace slots (fun l => labels (inner p l)) (some (labels (finish p))) program (fun l => atLabels (inner p l)) (prepared p slots base) n (digit p) (by simpa using hinput) (fun i => prepared_digit p slots base i (hcoeff i)) (by constructor · simpa using clean.accA · simpa using clean.accB · simpa using clean.counter · simpa using clean.scratch) ambient register rw [hornerValue_eq_eval] at hh have hc : (advance (TM2.step program))^[1] (some ⟨some (labels (finish p)), ((ambient, ()), none), MachineHorner.resultTapes slots (prepared p slots base) (p.eval n)⟩) = some ⟨exit, ((ambient, ()), none), MachineHorner.resultTapes slots base (p.eval n)⟩ := by change some (TM2.stepAux (program (labels (finish p))) _ _) = _ rw [atLabels] change some (TM2.stepAux (clearFields _ _ _) _ _) = _ rw [stepAux_clearFields, clear_result p slots base _ hcoeff] cases exit <;> rfl rw [show steps p n = 1 + (MachineHorner.steps n (digit p) (width p) + 1) by unfold steps; omega, Function.iterate_add_apply, Function.iterate_add_apply, hp, hh, hc] def inTime (p : Polynomial Nat) (slots : MachineHorner.Layout (width p) ↪ K) (labels : Label p → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (MachineHorner.State σ)) (atLabels : ∀ l, program (labels l) = statement p slots labels exit l) (base : K → List Bool) (n : Nat) (hinput : base (slots (.inl 0)) = encodeWord n) (hcoeff : ∀ i : Fin (width p), base (slots (.inr i)) = []) (clean : MachineHorner.Clean slots base) (ambient : σ) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels (start p)), ((ambient, ()), register), base⟩ (some ⟨exit, ((ambient, ()), none), MachineHorner.resultTapes slots base (p.eval n)⟩) ((timePolynomial p).eval n) where steps := steps p n evals_in_steps := trace p slots labels exit program atLabels base n hinput hcoeff clean ambient register steps_le_m := by rw [timePolynomial_eval]; unfold steps; omega end Eval def leftValue (u n : Nat) : Nat := 2 ^ (2 ^ u) * n ^ u def rightValue (u m : Nat) : Nat := 2 ^ (8 ^ u) * m ^ u def dummyValue (u n m : Nat) : Nat := leftValue u n + rightValue u m def bitValue (u n m : Nat) : Nat := dummyValue u n m + 1 def occurrenceValue (u D m : Nat) : Nat := 3 ^ u * (SourceOccurrences.testTapeEncoding u D).size * m ^ u inductive Kind | left | right | occurrences deriving DecidableEq protected abbrev Kind.enumList : List Kind := [.left, .right, .occurrences] protected theorem Kind.enumList_getElem?_ctorIdx_eq (x : Kind) : Kind.enumList[x.ctorIdx]? = some x := by cases x <;> rfl protected theorem Kind.enumList_nodup : Kind.enumList.Nodup := by decide instance : Fintype Kind where elems := ⟨Kind.enumList, Kind.enumList_nodup⟩ complete x := by cases x <;> decide def polynomial (u D : Nat) : Kind → Polynomial Nat | .left => Polynomial.C (2 ^ (2 ^ u)) * Polynomial.X ^ u | .right => Polynomial.C (2 ^ (8 ^ u)) * Polynomial.X ^ u | .occurrences => Polynomial.C (3 ^ u * (SourceOccurrences.testTapeEncoding u D).size) * Polynomial.X ^ u @[simp] theorem polynomial_left_eval (u D n : Nat) : (polynomial u D .left).eval n = leftValue u n := by simp [polynomial, leftValue] @[simp] theorem polynomial_right_eval (u D m : Nat) : (polynomial u D .right).eval m = rightValue u m := by simp [polynomial, rightValue] @[simp] theorem polynomial_occurrences_eval (u D m : Nat) : (polynomial u D .occurrences).eval m = occurrenceValue u D m := by simp [polynomial, occurrenceValue] theorem bitValue_eq_nBits (F : Target.Formula) (u : Nat) : bitValue u F.variables F.clauses.length = SourceOccurrences.nBits F u := by rw [SourceOccurrences.nBits_eq] simp [bitValue, dummyValue, leftValue, rightValue, Nat.mul_comm, Nat.add_assoc] theorem occurrenceValue_eq_length (F : Target.Formula) (u D : Nat) (hm : 0 < F.clauses.length) : occurrenceValue u D F.clauses.length = (SourceOccurrences.sourceList F u D).length := by have hne : F.clauses ≠ [] := by intro h; simp [h] at hm have hfalse : ¬ F.clauses.isEmpty = true := by simpa using hne simp only [SourceOccurrences.sourceList, ite_eq_right hfalse] rw [SourceOccurrences.rawSourceList_length] unfold occurrenceValue rw [SourceBounds.testTapeEncoding_size] ac_rfl inductive Control | variableCount | clauseCount | leftBlockSize | rightTemporary | dummyIndex | bitCount | occurrenceCount | accA | accB | counter | scratch deriving DecidableEq protected abbrev Control.enumList : List Control := [.variableCount, .clauseCount, .leftBlockSize, .rightTemporary, .dummyIndex, .bitCount, .occurrenceCount, .accA, .accB, .counter, .scratch] protected theorem Control.enumList_getElem?_ctorIdx_eq (x : Control) : Control.enumList[x.ctorIdx]? = some x := by cases x <;> rfl protected theorem Control.enumList_nodup : Control.enumList.Nodup := by decide instance : Fintype Control where elems := ⟨Control.enumList, Control.enumList_nodup⟩ complete x := by cases x <;> decide abbrev Coefficients (u D : Nat) := Fin (CookLevin.PolynomialMachine.width (polynomial u D .left)) ⊕ (Fin (CookLevin.PolynomialMachine.width (polynomial u D .right)) ⊕ Fin (CookLevin.PolynomialMachine.width (polynomial u D .occurrences))) abbrev Layout (u D : Nat) := Control ⊕ Coefficients u D def coefficientBlock (u D : Nat) (kind : Kind) : Fin (CookLevin.PolynomialMachine.width (polynomial u D kind)) ↪ Coefficients u D := match kind with | .left => ⟨Sum.inl, Sum.inl_injective⟩ | .right => ⟨fun i => .inr (.inl i), Sum.inr_injective.comp Sum.inl_injective⟩ | .occurrences => ⟨fun i => .inr (.inr i), Sum.inr_injective.comp Sum.inr_injective⟩ def inputControl : Kind → Control | .left => .variableCount | _ => .clauseCount def outputControl : Kind → Control | .left => .leftBlockSize | .right => .rightTemporary | .occurrences => .occurrenceCount def controlSlots (kind : Kind) : Fin 6 ↪ Control where toFun i := match i.val with | 0 => inputControl kind | 1 => .accA | 2 => .accB | 3 => outputControl kind | 4 => .counter | _ => .scratch inj' := by intro i j h cases kind <;> fin_cases i <;> fin_cases j <;> cases h <;> rfl def localSlots (u D : Nat) (kind : Kind) : MachineHorner.Layout (CookLevin.PolynomialMachine.width (polynomial u D kind)) ↪ Layout u D where toFun | .inl i => .inl (controlSlots kind i) | .inr i => .inr (coefficientBlock u D kind i) inj' := by intro a b h cases a with | inl a => cases b with | inl b => exact congrArg Sum.inl ((controlSlots kind).injective (Sum.inl.inj h)) | inr b => cases h | inr a => cases b with | inl b => cases h | inr b => exact congrArg Sum.inr ((coefficientBlock u D kind).injective (Sum.inr.inj h)) variable {u D : Nat} {K Λ σ : Type} def control (slots : Layout u D ↪ K) (c : Control) : K := slots (.inl c) def evalSlots (slots : Layout u D ↪ K) (kind : Kind) : MachineHorner.Layout (CookLevin.PolynomialMachine.width (polynomial u D kind)) ↪ K := (localSlots u D kind).trans slots @[simp] theorem evalSlots_radix (slots : Layout u D ↪ K) (kind : Kind) : evalSlots slots kind (.inl 0) = control slots (inputControl kind) := rfl @[simp] theorem evalSlots_accA (slots : Layout u D ↪ K) (kind : Kind) : evalSlots slots kind (.inl 1) = control slots .accA := rfl @[simp] theorem evalSlots_accB (slots : Layout u D ↪ K) (kind : Kind) : evalSlots slots kind (.inl 2) = control slots .accB := rfl @[simp] theorem evalSlots_destination (slots : Layout u D ↪ K) (kind : Kind) : evalSlots slots kind (.inl 3) = control slots (outputControl kind) := rfl @[simp] theorem evalSlots_counter (slots : Layout u D ↪ K) (kind : Kind) : evalSlots slots kind (.inl 4) = control slots .counter := rfl @[simp] theorem evalSlots_scratch (slots : Layout u D ↪ K) (kind : Kind) : evalSlots slots kind (.inl 5) = control slots .scratch := rfl @[simp] theorem evalSlots_digit (slots : Layout u D ↪ K) (kind : Kind) (i : Fin (CookLevin.PolynomialMachine.width (polynomial u D kind))) : evalSlots slots kind (.inr i) = slots (.inr (coefficientBlock u D kind i)) := rfl @[simp] theorem control_eq_iff (slots : Layout u D ↪ K) (a b : Control) : control slots a = control slots b ↔ a = b := by simp [control, slots.injective.eq_iff] structure Clean (slots : Layout u D ↪ K) (base : K → List Bool) : Prop where left : base (control slots .leftBlockSize) = [] right : base (control slots .rightTemporary) = [] dummy : base (control slots .dummyIndex) = [] bits : base (control slots .bitCount) = [] occurrences : base (control slots .occurrenceCount) = [] accA : base (control slots .accA) = [] accB : base (control slots .accB) = [] counter : base (control slots .counter) = [] scratch : base (control slots .scratch) = [] coefficients : ∀ i : Coefficients u D, base (slots (.inr i)) = [] inductive Label (u D : Nat) | evaluate (kind : Kind) (localLabel : Eval.Label (polynomial u D kind)) | copyLeftOut | copyLeftBack | addRight | clearTemporary | copyDummyOut | copyDummyBack | incrementBits deriving DecidableEq, Fintype def evalStart (kind : Kind) : Label u D := .evaluate kind (Eval.start (polynomial u D kind)) def afterEval : Kind → Label u D | .left => evalStart .right | .right => evalStart .occurrences | .occurrences => .copyLeftOut variable [DecidableEq K] def statement (slots : Layout u D ↪ K) (labels : Label u D → Λ) (exit : Option Λ) : Label u D → TM2.Stmt (fun _ : K => Bool) Λ (MachineHorner.State σ) | .evaluate kind l => Eval.statement (polynomial u D kind) (evalSlots slots kind) (fun l => labels (.evaluate kind l)) (some (labels (afterEval kind))) l | .copyLeftOut => Reduction.MachineTransfer.loopAt (control slots .leftBlockSize) (control slots .scratch) id false (labels .copyLeftOut) (some (labels .copyLeftBack)) | .copyLeftBack => MachineCopy.forkLoop (control slots .scratch) (control slots .leftBlockSize) (control slots .dummyIndex) false (labels .copyLeftBack) (some (labels .addRight)) | .addRight => MachineUnaryAddAt.loop (control slots .rightTemporary) (control slots .dummyIndex) (labels .addRight) (some (labels .clearTemporary)) | .clearTemporary => .pop (control slots .rightTemporary) (fun state _ => state) (.goto fun _ => labels .copyDummyOut) | .copyDummyOut => Reduction.MachineTransfer.loopAt (control slots .dummyIndex) (control slots .scratch) id false (labels .copyDummyOut) (some (labels .copyDummyBack)) | .copyDummyBack => MachineCopy.forkLoop (control slots .scratch) (control slots .dummyIndex) (control slots .bitCount) false (labels .copyDummyBack) (some (labels .incrementBits)) | .incrementBits => .push (control slots .bitCount) (fun _ => true) (Reduction.MachineTransfer.exitAt (control slots .bitCount) exit) def resultTapes (slots : Layout u D ↪ K) (base : K → List Bool) (n m : Nat) : K → List Bool := Function.update (Function.update (Function.update (Function.update base (control slots .leftBlockSize) (encodeWord (leftValue u n))) (control slots .occurrenceCount) (encodeWord (occurrenceValue u D m))) (control slots .dummyIndex) (encodeWord (dummyValue u n m))) (control slots .bitCount) (encodeWord (bitValue u n m)) def steps (u D n m : Nat) : Nat := Eval.steps (polynomial u D .left) n + Eval.steps (polynomial u D .right) m + Eval.steps (polynomial u D .occurrences) m + 2 * leftValue u n + rightValue u m + 2 * dummyValue u n m + 11 structure WorkClean (slots : Layout u D ↪ K) (base : K → List Bool) : Prop where accA : base (control slots .accA) = [] accB : base (control slots .accB) = [] counter : base (control slots .counter) = [] scratch : base (control slots .scratch) = [] coefficients : ∀ i : Coefficients u D, base (slots (.inr i)) = [] omit [DecidableEq K] in theorem Clean.work {slots : Layout u D ↪ K} {base : K → List Bool} (clean : Clean slots base) : WorkClean slots base := ⟨clean.accA, clean.accB, clean.counter, clean.scratch, clean.coefficients⟩ theorem WorkClean.update_output {slots : Layout u D ↪ K} {base : K → List Bool} (clean : WorkClean slots base) (kind : Kind) (value : List Bool) : WorkClean slots (Function.update base (control slots (outputControl kind)) value) := by cases kind <;> constructor all_goals try { simpa [outputControl] using clean.accA } all_goals try { simpa [outputControl] using clean.accB } all_goals try { simpa [outputControl] using clean.counter } all_goals try { simpa [outputControl] using clean.scratch } all_goals intro i; simpa [control, slots.injective.eq_iff] using clean.coefficients i theorem evalPhaseTrace (slots : Layout u D ↪ K) (labels : Label u D → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (MachineHorner.State σ)) (atLabels : ∀ l, program (labels l) = statement slots labels exit l) (kind : Kind) (base : K → List Bool) (n : Nat) (hinput : base (control slots (inputControl kind)) = encodeWord n) (hdest : base (control slots (outputControl kind)) = []) (clean : WorkClean slots base) (ambient : σ) (register : Option Bool) : (advance (TM2.step program))^[Eval.steps (polynomial u D kind) n] (some ⟨some (labels (evalStart kind)), ((ambient, ()), register), base⟩) = some ⟨some (labels (afterEval kind)), ((ambient, ()), none), Function.update base (control slots (outputControl kind)) (encodeWord ((polynomial u D kind).eval n))⟩ := by have h := Eval.trace (polynomial u D kind) (evalSlots slots kind) (fun l => labels (.evaluate kind l)) (some (labels (afterEval kind))) program (fun l => atLabels (.evaluate kind l)) base n (by simpa using hinput) (fun i => by simpa using clean.coefficients (coefficientBlock u D kind i)) ⟨by simpa using clean.accA, by simpa using clean.accB, by simpa using clean.counter, by simpa using clean.scratch⟩ ambient register simpa only [evalStart, MachineHorner.resultTapes, evalSlots_destination, hdest, List.append_nil] using h private theorem trace_trans {α : Type*} (f : α → α) {a b : Nat} {x y z : α} (first : f^[a] x = y) (second : f^[b] y = z) : f^[a + b] x = z := by rw [Nat.add_comm, Function.iterate_add_apply, first, second] theorem trace (slots : Layout u D ↪ K) (labels : Label u D → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (MachineHorner.State σ)) (atLabels : ∀ l, program (labels l) = statement slots labels exit l) (base : K → List Bool) (n m : Nat) (hn : base (control slots .variableCount) = encodeWord n) (hm : base (control slots .clauseCount) = encodeWord m) (clean : Clean slots base) (ambient : σ) (register : Option Bool) : (advance (TM2.step program))^[steps u D n m] (some ⟨some (labels (evalStart .left)), ((ambient, ()), register), base⟩) = some ⟨exit, ((ambient, ()), none), resultTapes slots base n m⟩ := by let t1 := Function.update base (control slots .leftBlockSize) (encodeWord (leftValue u n)) let t2 := Function.update t1 (control slots .rightTemporary) (encodeWord (rightValue u m)) let t3 := Function.update t2 (control slots .occurrenceCount) (encodeWord (occurrenceValue u D m)) let t4 := Function.update t3 (control slots .dummyIndex) (encodeWord (leftValue u n)) let t5 := Function.update (Function.update t4 (control slots .rightTemporary) [false]) (control slots .dummyIndex) (encodeWord (dummyValue u n m)) let t6 := Function.update t5 (control slots .rightTemporary) [] let t7 := Function.update t6 (control slots .bitCount) (encodeWord (dummyValue u n m)) have h1 := evalPhaseTrace slots labels exit program atLabels .left base n hn clean.left clean.work ambient register change (advance (TM2.step program))^[Eval.steps (polynomial u D .left) n] (some ⟨some (labels (evalStart .left)), ((ambient, ()), register), base⟩) = some ⟨some (labels (evalStart .right)), ((ambient, ()), none), _⟩ at h1 simp only [polynomial_left_eval] at h1 have cw1 : WorkClean slots t1 := clean.work.update_output .left _ have h2 := evalPhaseTrace slots labels exit program atLabels .right t1 m (by simpa [t1, inputControl] using hm) (by simpa [t1, outputControl] using clean.right) cw1 ambient none change (advance (TM2.step program))^[Eval.steps (polynomial u D .right) m] (some ⟨some (labels (evalStart .right)), ((ambient, ()), none), t1⟩) = some ⟨some (labels (evalStart .occurrences)), ((ambient, ()), none), _⟩ at h2 simp only [polynomial_right_eval] at h2 have cw2 : WorkClean slots t2 := cw1.update_output .right _ have h3 := evalPhaseTrace slots labels exit program atLabels .occurrences t2 m (by simpa [t2, t1, inputControl] using hm) (by simpa [t2, t1, outputControl] using clean.occurrences) cw2 ambient none change (advance (TM2.step program))^[Eval.steps (polynomial u D .occurrences) m] (some ⟨some (labels (evalStart .occurrences)), ((ambient, ()), none), t2⟩) = some ⟨some (labels .copyLeftOut), ((ambient, ()), none), _⟩ at h3 simp only [polynomial_occurrences_eval] at h3 have cw3 : WorkClean slots t3 := cw2.update_output .occurrences _ have copyingLeft := MachineCopy.copyTrace (control slots .leftBlockSize) (control slots .dummyIndex) (control slots .scratch) (by simp) (by simp) (by simp) false (labels .copyLeftOut) (labels .copyLeftBack) (some (labels .addRight)) program (atLabels .copyLeftOut) (atLabels .copyLeftBack) t3 cw3.scratch (ambient, ()) none have hleft : t3 (control slots .leftBlockSize) = encodeWord (leftValue u n) := by simp [t3, t2, t1] have hdummy : t3 (control slots .dummyIndex) = [] := by simpa [t3, t2, t1] using clean.dummy rw [hleft, hdummy, List.append_nil, encodeWord_length] at copyingLeft have adding := MachineUnaryAddAt.addFromTapes (control slots .rightTemporary) (control slots .dummyIndex) (by simp) (labels .addRight) (some (labels .clearTemporary)) program (atLabels .addRight) t4 (rightValue u m) (leftValue u n) [] [] (by simp [t4, t3, t2]) (by simp [t4]) (ambient, ()) none have ha : MachineUnaryAddAt.unaryTapes (control slots .rightTemporary) (control slots .dummyIndex) t4 0 (rightValue u m + leftValue u n) [] [] = t5 := by simp [MachineUnaryAddAt.unaryTapes, Reduction.MachineTransfer.tapesAt, t5, encodeWord, dummyValue, Nat.add_comm] rw [ha] at adding have clearing : (advance (TM2.step program))^[1] (some ⟨some (labels .clearTemporary), ((ambient, ()), none), t5⟩) = some ⟨some (labels .copyDummyOut), ((ambient, ()), none), t6⟩ := by change some (TM2.stepAux (program (labels .clearTemporary)) _ _) = _ rw [atLabels] simp [statement, TM2.stepAux, t6, t5] have copyingDummy := MachineCopy.copyTrace (control slots .dummyIndex) (control slots .bitCount) (control slots .scratch) (by simp) (by simp) (by simp) false (labels .copyDummyOut) (labels .copyDummyBack) (some (labels .incrementBits)) program (atLabels .copyDummyOut) (atLabels .copyDummyBack) t6 (by simpa [t6, t5, t4] using cw3.scratch) (ambient, ()) none have hdi : t6 (control slots .dummyIndex) = encodeWord (dummyValue u n m) := by simp [t6, t5] have hbi : t6 (control slots .bitCount) = [] := by simpa [t6, t5, t4, t3, t2, t1] using clean.bits rw [hdi, hbi, List.append_nil, encodeWord_length] at copyingDummy have increment : (advance (TM2.step program))^[1] (some ⟨some (labels .incrementBits), ((ambient, ()), none), t7⟩) = some ⟨exit, ((ambient, ()), none), Function.update t7 (control slots .bitCount) (encodeWord (bitValue u n m))⟩ := by change some (TM2.stepAux (program (labels .incrementBits)) _ _) = _ rw [atLabels] cases exit <;> simp [statement, TM2.stepAux, Reduction.MachineTransfer.exitAt, t7, bitValue, encodeWord, List.replicate_succ] have total := trace_trans _ (trace_trans _ (trace_trans _ (trace_trans _ (trace_trans _ (trace_trans _ (trace_trans _ h1 h2) h3) copyingLeft) adding) clearing) copyingDummy) increment have htime : Eval.steps (polynomial u D .left) n + Eval.steps (polynomial u D .right) m + Eval.steps (polynomial u D .occurrences) m + 2 * (leftValue u n + 1 + 1) + (rightValue u m + 1) + 1 + 2 * (dummyValue u n m + 1 + 1) + 1 = steps u D n m := by unfold steps; omega rw [htime] at total have frame : Function.update t7 (control slots .bitCount) (encodeWord (bitValue u n m)) = resultTapes slots base n m := by funext k by_cases hr : k = control slots .rightTemporary · subst k simp [t7, t6, t5, t4, t3, t2, t1, resultTapes, clean.right] · simp [t7, t6, t5, t4, t3, t2, t1, resultTapes, Function.update_apply, hr] split_ifs <;> rfl rw [frame] at total exact total def timePolynomial (u D : Nat) : Polynomial Nat := CookLevin.PolynomialMachine.timePolynomial (polynomial u D .left) + CookLevin.PolynomialMachine.timePolynomial (polynomial u D .right) + CookLevin.PolynomialMachine.timePolynomial (polynomial u D .occurrences) + Polynomial.C 2 * polynomial u D .left + polynomial u D .right + Polynomial.C 2 * (polynomial u D .left + polynomial u D .right) + Polynomial.C 11 theorem steps_le_timePolynomial (u D n m L : Nat) (hn : n ≤ L) (hm : m ≤ L) : steps u D n m ≤ (timePolynomial u D).eval L := by have hEval (p : Polynomial Nat) (a : Nat) (ha : a ≤ L) : Eval.steps p a ≤ (CookLevin.PolynomialMachine.timePolynomial p).eval L := by apply Nat.le_trans (m := (CookLevin.PolynomialMachine.timePolynomial p).eval a) · rw [CookLevin.PolynomialMachine.timePolynomial_eval] unfold Eval.steps omega · exact natPolynomial_eval_mono _ ha have h1 := hEval (polynomial u D .left) n hn have h2 := hEval (polynomial u D .right) m hm have h3 := hEval (polynomial u D .occurrences) m hm have hl : leftValue u n ≤ leftValue u L := Nat.mul_le_mul_left _ (Nat.pow_le_pow_left hn u) have hr : rightValue u m ≤ rightValue u L := Nat.mul_le_mul_left _ (Nat.pow_le_pow_left hm u) simp only [timePolynomial, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, polynomial_left_eval, polynomial_right_eval] unfold steps dummyValue omega def inTime (slots : Layout u D ↪ K) (labels : Label u D → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (MachineHorner.State σ)) (atLabels : ∀ l, program (labels l) = statement slots labels exit l) (base : K → List Bool) (n m : Nat) (hn : base (control slots .variableCount) = encodeWord n) (hm : base (control slots .clauseCount) = encodeWord m) (clean : Clean slots base) (ambient : σ) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels (evalStart .left)), ((ambient, ()), register), base⟩ (some ⟨exit, ((ambient, ()), none), resultTapes slots base n m⟩) (steps u D n m) where steps := steps u D n m evals_in_steps := trace slots labels exit program atLabels base n m hn hm clean ambient register steps_le_m := Nat.le_refl _ def inPolynomialTime (slots : Layout u D ↪ K) (labels : Label u D → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (MachineHorner.State σ)) (atLabels : ∀ l, program (labels l) = statement slots labels exit l) (base : K → List Bool) (n m L : Nat) (hn : base (control slots .variableCount) = encodeWord n) (hm : base (control slots .clauseCount) = encodeWord m) (clean : Clean slots base) (hnL : n ≤ L) (hmL : m ≤ L) (ambient : σ) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels (evalStart .left)), ((ambient, ()), register), base⟩ (some ⟨exit, ((ambient, ()), none), resultTapes slots base n m⟩) ((timePolynomial u D).eval L) where steps := steps u D n m evals_in_steps := trace slots labels exit program atLabels base n m hn hm clean ambient register steps_le_m := steps_le_timePolynomial u D n m L hnL hmL def formulaInTime (slots : Layout u D ↪ K) (labels : Label u D → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (MachineHorner.State σ)) (atLabels : ∀ l, program (labels l) = statement slots labels exit l) (base : K → List Bool) (F : Target.Formula) (hn : base (control slots .variableCount) = encodeWord F.variables) (hm : base (control slots .clauseCount) = encodeWord F.clauses.length) (clean : Clean slots base) (ambient : σ) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels (evalStart .left)), ((ambient, ()), register), base⟩ (some ⟨exit, ((ambient, ()), none), resultTapes slots base F.variables F.clauses.length⟩) ((timePolynomial u D).eval (formulaBits F).length) := inPolynomialTime slots labels exit program atLabels base F.variables F.clauses.length (formulaBits F).length hn hm clean (SourceBounds.formulaBits_length_ge_variables F) (SourceBounds.formulaBits_length_ge_clauses F) ambient register theorem result_frame (slots : Layout u D ↪ K) (base : K → List Bool) (n m : Nat) (k : K) (hl : k ≠ control slots .leftBlockSize) (ho : k ≠ control slots .occurrenceCount) (hd : k ≠ control slots .dummyIndex) (hb : k ≠ control slots .bitCount) : resultTapes slots base n m k = base k := by simp [resultTapes, hl, ho, hd, hb] @[simp] theorem result_left (slots : Layout u D ↪ K) (base : K → List Bool) (n m : Nat) : resultTapes slots base n m (control slots .leftBlockSize) = encodeWord (leftValue u n) := by simp [resultTapes] @[simp] theorem result_occurrences (slots : Layout u D ↪ K) (base : K → List Bool) (n m : Nat) : resultTapes slots base n m (control slots .occurrenceCount) = encodeWord (occurrenceValue u D m) := by simp [resultTapes] @[simp] theorem result_dummy (slots : Layout u D ↪ K) (base : K → List Bool) (n m : Nat) : resultTapes slots base n m (control slots .dummyIndex) = encodeWord (dummyValue u n m) := by simp [resultTapes] @[simp] theorem result_bits (slots : Layout u D ↪ K) (base : K → List Bool) (n m : Nat) : resultTapes slots base n m (control slots .bitCount) = encodeWord (bitValue u n m) := by simp [resultTapes] @[simp] theorem result_variableCount (slots : Layout u D ↪ K) (base : K → List Bool) (n m : Nat) : resultTapes slots base n m (control slots .variableCount) = base (control slots .variableCount) := by simp [resultTapes] @[simp] theorem result_clauseCount (slots : Layout u D ↪ K) (base : K → List Bool) (n m : Nat) : resultTapes slots base n m (control slots .clauseCount) = base (control slots .clauseCount) := by simp [resultTapes] theorem result_source_headers (slots : Layout u D ↪ K) (base : K → List Bool) (F : Target.Formula) (hm : 0 < F.clauses.length) : resultTapes slots base F.variables F.clauses.length (control slots .bitCount) = encodeWord (SourceOccurrences.nBits F u) ∧ resultTapes slots base F.variables F.clauses.length (control slots .occurrenceCount) = encodeWord (SourceOccurrences.sourceList F u D).length := by simp only [result_bits, result_occurrences, bitValue_eq_nBits, occurrenceValue_eq_length F u D hm, and_self] def program (slots : Layout u D ↪ K) : Label u D → TM2.Stmt (fun _ : K => Bool) (Label u D) (MachineHorner.State σ) := statement slots id none def machine (u D : Nat) : FinTM2 where K := Layout u D k₀ := .inl .variableCount k₁ := .inl .bitCount Γ _ := Bool Λ := Label u D main := evalStart .left σ := MachineHorner.State Unit initialState := (((), ()), none) m := program (Function.Embedding.refl _) def afterInitializationInTime {Extra : Type} [DecidableEq Extra] (slots : Layout u D ↪ SourceLoopInit.Tape u Extra) (hnslot : control slots .variableCount = SourceLoopInit.variableHeader) (hmslot : control slots .clauseCount = SourceLoopInit.clauseHeader) (labels : Label u D → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : SourceLoopInit.Tape u Extra => Bool) Λ (MachineHorner.State σ)) (atLabels : ∀ l, program (labels l) = statement slots labels exit l) (base : SourceLoopInit.Tape u Extra → List Bool) (F : Target.Formula) (hn : base SourceLoopInit.variableHeader = []) (hm : base SourceLoopInit.clauseHeader = []) (clean : Clean slots (SourceLoopInit.outputTapes F base)) (ambient : σ) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels (evalStart .left)), ((ambient, ()), register), SourceLoopInit.outputTapes F base⟩ (some ⟨exit, ((ambient, ()), none), resultTapes slots (SourceLoopInit.outputTapes F base) F.variables F.clauses.length⟩) ((timePolynomial u D).eval (formulaBits F).length) := formulaInTime slots labels exit program atLabels (SourceLoopInit.outputTapes F base) F (by rw [hnslot, SourceLoopInit.output_variableHeader, hn, List.append_nil]) (by rw [hmslot, SourceLoopInit.output_clauseHeader, hm, List.append_nil]) clean ambient register end BinPackingGames.Foundations.Hastad.SourceHeaderCounts namespace BinPackingGames.Foundations.Hastad.SourceGap open Target SourceContexts SourceOccurrences SourceGame open BinPackingGames.Integration.SourceParameters open BinPackingGames.Reduction def ClauseGap (F : Formula) (η : ℚ) : Prop := F.clauses ≠ [] ∧ ∀ assignment : Fin F.variables → Bool, (η : ℝ) * F.clauses.length ≤ (PCP.failureCount F assignment (PCP.allIndices F) : ℝ) theorem third_pos {η : ℚ} (hη : 0 < η) : 0 < η / 3 := by positivity theorem third_le_one {η : ℚ} (hη : η ≤ 1) : η / 3 ≤ 1 := by linarith def repetitionCount (η ξ : ℚ) (hη : 0 < η) (hη1 : η ≤ 1) (hξ : 0 < ξ) (D : ℕ) (hD : 0 < D) : ℕ := repetitionLength (η / 3) ξ (third_pos hη) (third_le_one hη1) hξ D hD theorem repetitionCount_pos (η ξ : ℚ) (hη : 0 < η) (hη1 : η ≤ 1) (hξ : 0 < ξ) (D : ℕ) (hD : 0 < D) : 0 < repetitionCount η ξ hη hη1 hξ D hD := (repetitionLength_spec (η / 3) ξ (third_pos hη) (third_le_one hη1) hξ D hD).1 theorem repeated_game_bound (F : Formula) (η ξ : ℚ) (hη : 0 < η) (hη1 : η ≤ 1) (hξ : 0 < ξ) (D : ℕ) (hD : 0 < D) (hgap : ClauseGap F η) : ((baseGame F hgap.1).repetition (repetitionCount η ξ hη hη1 hξ D hD)).value ≤ 4 * (D : ℝ)⁻¹ * (ξ : ℝ)^2 := by have hb : (baseGame F hgap.1).value ≤ 1 - ((η / 3 : ℚ) : ℝ) := by simpa only [Rat.cast_div, Rat.cast_ofNat] using base_value_le_of_clause_gap F hgap.1 (η : ℝ) hgap.2 have hc : Fintype.card Bool * Fintype.card PCP.ClauseAnswer = 16 := by simp exact (game_repetition_rate (baseGame F hgap.1) (third_pos hη) (third_le_one hη1) hc hb _).trans (repetitionLength_real_bound (η / 3) ξ (third_pos hη) (third_le_one hη1) hξ D hD) def source (η ξ : ℚ) (hη : 0 < η) (hη1 : η ≤ 1) (hξ : 0 < ξ) (D : ℕ) (hD : 0 < D) (F : Formula) : SourceEncoding.Input := sourceInput F (repetitionCount η ξ hη hη1 hξ D hD) D hD theorem source_complete (η ξ : ℚ) (hη : 0 < η) (hη1 : η ≤ 1) (hξ : 0 < ξ) (D : ℕ) (hD : 0 < D) (hnoise : (D : ℚ)⁻¹ ≤ ξ) (F : Formula) (hF : F.Satisfiable) : ∃ bits : Fin (source η ξ hη hη1 hξ D hD F).variables → Bool, 1 - ξ ≤ (((source η ξ hη hη1 hξ D hD F).equations.countP (fun e => CloneGap.satisfied e bits) : ℚ) / (source η ξ hη hη1 hξ D hD F).equations.length) := by obtain ⟨assignment, hs⟩ := hF refine ⟨SourceHonest.honestBits F _ assignment, ?_⟩ have h := SourceHonest.sourceList_honest_acceptance_rat F (repetitionCount η ξ hη hη1 hξ D hD) D hD assignment hs exact (sub_le_sub_left hnoise 1).trans h theorem source_sound (η ξ : ℚ) (hη : 0 < η) (hη1 : η ≤ 1) (hξ : 0 < ξ) (D : ℕ) (hD : 0 < D) (hDtwo : 2 ≤ D) (F : Formula) (hgap : ClauseGap F η) (bits : Fin (source η ξ hη hη1 hξ D hD F).variables → Bool) : ((source η ξ hη hη1 hξ D hD F).equations.countP (fun e => CloneGap.satisfied e bits) : ℚ) / (source η ξ hη hη1 hξ D hD F).equations.length ≤ (1 + ξ) / 2 := SourceSoundness.sourceList_sound_rat F hgap.1 (repetitionCount η ξ hη hη1 hξ D hD) D hD hDtwo ξ hξ.le (repeated_game_bound F η ξ hη hη1 hξ D hD hgap) bits theorem source_size (η ξ : ℚ) (hη : 0 < η) (hη1 : η ≤ 1) (hξ : 0 < ξ) (D : ℕ) (hD : 0 < D) : ∃ p : Polynomial ℕ, ∀ F : Formula, (SourceEncoding.inputBits (source η ξ hη hη1 hξ D hD F)).length ≤ p.eval (Complexity.formulaBits F).length := SourceBounds.fixed_parameters_source_size (repetitionCount η ξ hη hη1 hξ D hD) D hD end BinPackingGames.Foundations.Hastad.SourceGap namespace BinPackingGames.Foundations.Hastad.SourceNonempty open Target PCP theorem real_clause_gap_of_count (F : Formula) (a b : ℕ) (hb : 0 < b) (hgap : ∀ assignment : Fin F.variables → Bool, a * F.clauses.length ≤ b * NameCompaction.failedCount F assignment) (assignment : Fin F.variables → Bool) : (a : ℝ) / (b : ℝ) * F.clauses.length ≤ (failureCount F assignment (allIndices F) : ℝ) := by rw [NameCompaction.verifier_failureCount] have hreal : (a : ℝ) * F.clauses.length ≤ (b : ℝ) * (NameCompaction.failedCount F assignment : ℝ) := by exact_mod_cast hgap assignment rw [div_mul_eq_mul_div] apply (div_le_iff₀ (Nat.cast_pos.mpr hb)).mpr simpa only [mul_comm] using hreal theorem clauseGap_of_count (F : Formula) (a b : ℕ) (hne : F.clauses ≠ []) (hb : 0 < b) (hgap : ∀ assignment : Fin F.variables → Bool, a * F.clauses.length ≤ b * NameCompaction.failedCount F assignment) : SourceGap.ClauseGap F ((a : ℚ) / (b : ℚ)) := by refine ⟨hne, fun assignment => ?_⟩ simpa only [Rat.cast_div, Rat.cast_natCast] using real_clause_gap_of_count F a b hb hgap assignment variable {V E : Type*} {n m : ℕ} theorem cnf_nonempty_of_nonempty_darts [Fintype E] [Nonempty E] (G : ConstraintGraph V E FinalBooleanVerifier.Label) (vertices : V ≃ Fin n) (edges : E ≃ Fin m) : (FinalBooleanVerifier.cnf G vertices edges).clauses ≠ [] := by have hcard : Fintype.card E = m := by simpa only [Fintype.card_fin] using Fintype.card_congr edges have hm : 0 < m := hcard ▸ Fintype.card_pos exact FinalBooleanVerifier.cnf_nonempty G vertices edges hm theorem cnf_clauseGap [Fintype E] [Nonempty E] (G : ConstraintGraph V E FinalBooleanVerifier.Label) (vertices : V ≃ Fin n) (edges : E ≃ Fin m) (a b : ℕ) (hb : 0 < b) (hgap : ∀ labeling : V → FinalBooleanVerifier.Label, a * Fintype.card E ≤ b * G.rejectionCount labeling) : SourceGap.ClauseGap (FinalBooleanVerifier.cnf G vertices edges) ((a : ℚ) / ((b : ℚ) * 40960)) := by have h := clauseGap_of_count (FinalBooleanVerifier.cnf G vertices edges) a (b * 40960) (cnf_nonempty_of_nonempty_darts G vertices edges) (Nat.mul_pos hb (by decide)) (FinalBooleanVerifier.cnf_gap G vertices edges a b hgap) simpa only [Nat.cast_mul, Nat.cast_ofNat] using h theorem cnf_clauseGap_unit [Fintype E] [Nonempty E] (G : ConstraintGraph V E FinalBooleanVerifier.Label) (vertices : V ≃ Fin n) (edges : E ≃ Fin m) (walkLength : ℕ) (hwalk : 0 < walkLength) (hgap : ∀ labeling : V → FinalBooleanVerifier.Label, Fintype.card E ≤ walkLength * G.rejectionCount labeling) : SourceGap.ClauseGap (FinalBooleanVerifier.cnf G vertices edges) (1 / (40960 * (walkLength : ℚ))) := by have h := cnf_clauseGap G vertices edges 1 walkLength hwalk (by simpa only [one_mul] using hgap) simpa only [Nat.cast_one, mul_comm] using h end BinPackingGames.Foundations.Hastad.SourceNonempty end end OAI