import Mathlib.Tactic.Ring import OAI.Computability.BinPacking.Computation.MachineLazyTableRuntime import OAI.Computability.BinPacking.Computation.MachinePaddedOverlayRuntime import OAI.Computability.BinPacking.Computation.MachineRegularTableRuntime import OAI.Computability.BinPacking.Computation.PoweringMachineRuntime import OAI.Computability.BinPacking.Computation.TableIterationFiniteAlphabet import OAI.Computability.BinPacking.CookLevin.Completeness import OAI.Computability.BinPacking.PCP.Initialization import OAI.Computability.BinPacking.PCP.NormalizationMachine import OAI.Computability.BinPacking.Reductions.BinaryInputReduction namespace OAI noncomputable section namespace BinPackingGames.Foundations.PCP.AlphabetTable.LoopState open BinPackingGames.Foundations.Complexity open RuntimeModel variable {q : Nat} def inputBits (input : GenericGraphTables.Table q) : List Bool := GenericGraphTables.tableBits input def prefixWords (input : GenericGraphTables.Table q) (e : Nat) : List Nat := [Enumeration.vertexCount input.vertices input.darts q, Enumeration.dartCount input.darts q] ++ ((List.finRange input.darts).take e).flatMap (EmitRows.blockWords input) def prefixBits (input : GenericGraphTables.Table q) (e : Nat) : List Bool := encodeWords (prefixWords input e) def cursorWords (input : GenericGraphTables.Table q) (e : Nat) : List Nat := ((GenericGraphTables.rowList input).drop e).flatMap GenericGraphTables.rowWords def cursorBits (input : GenericGraphTables.Table q) (e : Nat) : List Bool := encodeWords (cursorWords input e) @[simp] theorem prefixWords_zero (input : GenericGraphTables.Table q) : prefixWords input 0 = [Enumeration.vertexCount input.vertices input.darts q, Enumeration.dartCount input.darts q] := by simp [prefixWords] @[simp] theorem prefixBits_zero (input : GenericGraphTables.Table q) : prefixBits input 0 = encodeWords [Enumeration.vertexCount input.vertices input.darts q, Enumeration.dartCount input.darts q] := by rw [prefixBits, prefixWords_zero] theorem prefixWords_succ (input : GenericGraphTables.Table q) (e : Nat) (he : e < input.darts) : prefixWords input (e + 1) = prefixWords input e ++ EmitRows.blockWords input ⟨e, he⟩ := by have bound : e < (List.finRange input.darts).length := by simpa using he have take := List.take_succ_eq_append_getElem bound have atIndex : (List.finRange input.darts)[e]'bound = (⟨e, he⟩ : Fin input.darts) := by simp rw [atIndex] at take unfold prefixWords rw [take, List.flatMap_append] simp only [List.flatMap_cons, List.flatMap_nil, List.append_nil, List.append_assoc] theorem prefixBits_succ (input : GenericGraphTables.Table q) (e : Nat) (he : e < input.darts) : prefixBits input (e + 1) = prefixBits input e ++ encodeWords (EmitRows.blockWords input ⟨e, he⟩) := by rw [prefixBits, prefixWords_succ input e he, encodeWords_append] rfl theorem prefixBits_reverse_succ (input : GenericGraphTables.Table q) (e : Nat) (he : e < input.darts) : (prefixBits input (e + 1)).reverse = (encodeWords (EmitRows.blockWords input ⟨e, he⟩)).reverse ++ (prefixBits input e).reverse := by rw [prefixBits_succ input e he, List.reverse_append] theorem prefixWords_full (input : GenericGraphTables.Table q) : prefixWords input input.darts = GraphTables.tableWords (Table.build input) := by have allRows : (List.finRange input.darts).take input.darts = List.finRange input.darts := by apply List.take_of_length_le simp unfold prefixWords rw [allRows] exact (EmitRows.tableWords_eq_blocks input).symm theorem prefixBits_full (input : GenericGraphTables.Table q) : prefixBits input input.darts = GraphTables.tableBits (Table.build input) := by rw [prefixBits, prefixWords_full] rfl @[simp] theorem cursorBits_zero (input : GenericGraphTables.Table q) : cursorBits input 0 = encodeWords ((GenericGraphTables.rowList input).flatMap GenericGraphTables.rowWords) := by simp [cursorBits, cursorWords] theorem cursorWords_succ (input : GenericGraphTables.Table q) (e : Nat) (he : e < input.darts) : cursorWords input e = GenericGraphTables.rowWords input.rows[(⟨e, he⟩ : Fin input.darts)] ++ cursorWords input (e + 1) := by have bound : e < (GenericGraphTables.rowList input).length := by simpa only [GenericGraphTables.rowList_length] using he unfold cursorWords rw [List.drop_eq_getElem_cons bound, List.flatMap_cons] simp only [GenericGraphTables.rowList, Vector.getElem_toList, Fin.getElem_fin] theorem cursorBits_succ (input : GenericGraphTables.Table q) (e : Nat) (he : e < input.darts) : cursorBits input e = encodeWords (GenericGraphTables.rowWords input.rows[(⟨e, he⟩ : Fin input.darts)]) ++ cursorBits input (e + 1) := by rw [cursorBits, cursorWords_succ input e he, encodeWords_append] rfl @[simp] theorem cursorBits_full (input : GenericGraphTables.Table q) : cursorBits input input.darts = [] := by have finished := List.drop_length (l := GenericGraphTables.rowList input) rw [GenericGraphTables.rowList_length] at finished unfold cursorBits cursorWords rw [finished] rfl structure Ready (input : GenericGraphTables.Table q) (e : Nat) (base : Tape → List Bool) : Prop where le_darts : e ≤ input.darts original : base .original = inputBits input cursor : base .cursor = cursorBits input e vertices : base .vertices = encodeWord input.vertices darts : base .darts = encodeWord input.darts counter : base .counter = encodeWord (input.darts - e) edge : base .edge = encodeWord e tail : base .tail = [] reverseIndex : base .reverseIndex = [] head : base .head = [] scratch : base .scratch = [] compareLeft : base .compareLeft = [] compareRight : base .compareRight = [] output : base .output = [] reversed : base .reversed = (prefixBits input e).reverse theorem Ready.of_agree {input : GenericGraphTables.Table q} {e : Nat} {base next : Tape → List Bool} (ready : Ready input e base) (agree : ∀ tape, tape ≠ .scan → tape ≠ .indexScan → next tape = base tape) : Ready input e next where le_darts := ready.le_darts original := (agree .original (by decide) (by decide)).trans ready.original cursor := (agree .cursor (by decide) (by decide)).trans ready.cursor vertices := (agree .vertices (by decide) (by decide)).trans ready.vertices darts := (agree .darts (by decide) (by decide)).trans ready.darts counter := (agree .counter (by decide) (by decide)).trans ready.counter edge := (agree .edge (by decide) (by decide)).trans ready.edge tail := (agree .tail (by decide) (by decide)).trans ready.tail reverseIndex := (agree .reverseIndex (by decide) (by decide)).trans ready.reverseIndex head := (agree .head (by decide) (by decide)).trans ready.head scratch := (agree .scratch (by decide) (by decide)).trans ready.scratch compareLeft := (agree .compareLeft (by decide) (by decide)).trans ready.compareLeft compareRight := (agree .compareRight (by decide) (by decide)).trans ready.compareRight output := (agree .output (by decide) (by decide)).trans ready.output reversed := (agree .reversed (by decide) (by decide)).trans ready.reversed theorem Ready.update_scan {input : GenericGraphTables.Table q} {e : Nat} {base : Tape → List Bool} (ready : Ready input e base) (word : List Bool) : Ready input e (Function.update base .scan word) := by apply ready.of_agree intro tape notScan _ simp [notScan] theorem Ready.update_indexScan {input : GenericGraphTables.Table q} {e : Nat} {base : Tape → List Bool} (ready : Ready input e base) (word : List Bool) : Ready input e (Function.update base .indexScan word) := by apply ready.of_agree intro tape _ notIndexScan simp [notIndexScan] theorem Ready.counter_succ {input : GenericGraphTables.Table q} {e : Nat} {base : Tape → List Bool} (ready : Ready input e base) (he : e < input.darts) : base .counter = encodeWord (input.darts - (e + 1) + 1) := by rw [ready.counter] congr 1 omega theorem Ready.counter_at_end {input : GenericGraphTables.Table q} {base : Tape → List Bool} (ready : Ready input input.darts base) : base .counter = encodeWord 0 := by simpa only [Nat.sub_self] using ready.counter theorem Ready.cursor_at_end {input : GenericGraphTables.Table q} {base : Tape → List Bool} (ready : Ready input input.darts base) : base .cursor = [] := ready.cursor.trans (cursorBits_full input) theorem Ready.reversed_at_end {input : GenericGraphTables.Table q} {base : Tape → List Bool} (ready : Ready input input.darts base) : base .reversed = (GraphTables.tableBits (Table.build input)).reverse := by rw [ready.reversed, prefixBits_full] end BinPackingGames.Foundations.PCP.AlphabetTable.LoopState namespace BinPackingGames.Foundations.PCP.AlphabetTable.Finish open Turing open BinPackingGames.Foundations.Complexity open RuntimeModel open scoped BigOperators def afterReversal (base : Tape → List Bool) (word : List Bool) : Tape → List Bool := Reduction.MachineTransfer.tapesAt .reversed .output base [] word @[simp] theorem afterReversal_output (base : Tape → List Bool) (word : List Bool) : afterReversal base word .output = word := by simp [afterReversal, Reduction.MachineTransfer.tapesAt] @[simp] theorem afterReversal_reversed (base : Tape → List Bool) (word : List Bool) : afterReversal base word .reversed = [] := by simp [afterReversal, Reduction.MachineTransfer.tapesAt] def finishBudget (base : Tape → List Bool) (word : List Bool) : Nat := word.length + 1 + (∑ i : Fin 15, (afterReversal base word (cleanupPorts i)).length) + 16 def reverseInTime (q : Nat) (base : Tape → List Bool) (word : List Bool) (state : State q) (hreversed : base .reversed = word.reverse) (houtput : base .output = []) : StateTransition.EvalsToInTime (Driver.machine q).step ⟨some .reverseOutput, state, base⟩ (some ⟨some (.cleanup 0), (state.1, none), afterReversal base word⟩) (word.length + 1) := by have run := Reduction.MachineTransfer.transferAtInTime (σ := Ambient q) Tape.reversed Tape.output (by decide) id false Driver.Label.reverseOutput (some (.cleanup 0)) (Driver.program q) rfl base state.1 state.2 simpa only [hreversed, houtput, List.length_reverse, List.reverse_reverse, List.map_id, List.append_nil, afterReversal, Driver.machine, FinTM2.step] using! run theorem cleared_eq_haltList (q : Nat) (base : Tape → List Bool) : (⟨none, initial q, Cleanup.clearTapes cleanupPorts base⟩ : (Driver.machine q).Cfg) = haltList (Driver.machine q) (base .output) := by have tapes := Cleanup.clearTapes_outputOnly cleanupPorts Tape.output base cleanupPorts_ne_output cleanupPorts_cover congr 1 def cleanupInTime (q : Nat) (base : Tape → List Bool) (state : State q) : StateTransition.EvalsToInTime (Driver.machine q).step ⟨some (.cleanup 0), state, base⟩ (some (haltList (Driver.machine q) (base .output))) ((∑ i : Fin 15, (base (cleanupPorts i)).length) + 16) := by have run := Cleanup.cleanupInTime cleanupPorts Driver.Label.cleanup (initial q).1 none (Driver.program q) (fun i => by simp only [Driver.program, Cleanup.instruction_drain]) (by simp only [Driver.program, Cleanup.instruction_finish]) base state.1 state.2 change StateTransition.EvalsToInTime (Driver.machine q).step ⟨some (.cleanup 0), state, base⟩ (some ⟨none, initial q, Cleanup.clearTapes cleanupPorts base⟩) ((∑ i : Fin 15, (base (cleanupPorts i)).length) + 15 + 1) at run rw [cleared_eq_haltList] at run simpa only [Nat.add_assoc] using run def finishInTime (q : Nat) (base : Tape → List Bool) (word : List Bool) (state : State q) (hreversed : base .reversed = word.reverse) (houtput : base .output = []) : StateTransition.EvalsToInTime (Driver.machine q).step ⟨some .reverseOutput, state, base⟩ (some (haltList (Driver.machine q) word)) (finishBudget base word) := by have first := reverseInTime q base word state hreversed houtput have last := cleanupInTime q (afterReversal base word) (state.1, none) rw [afterReversal_output] at last have run := StateTransition.EvalsToInTime.trans (Driver.machine q).step _ _ ⟨some .reverseOutput, state, base⟩ ⟨some (.cleanup 0), (state.1, none), afterReversal base word⟩ (some (haltList (Driver.machine q) word)) first last refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ } have budgetEq : ((∑ i : Fin 15, (afterReversal base word (cleanupPorts i)).length) + 16) + (word.length + 1) = finishBudget base word := by dsimp only [finishBudget] omega exact run.steps_le_m.trans (Nat.le_of_eq budgetEq) theorem afterReversal_selected_length_le (base : Tape → List Bool) (word : List Bool) (i : Fin 15) : (afterReversal base word (cleanupPorts i)).length ≤ (base (cleanupPorts i)).length := by have ho := cleanupPorts_ne_output i by_cases hr : cleanupPorts i = .reversed <;> simp [afterReversal, Reduction.MachineTransfer.tapesAt, ho, hr] theorem stackLength_after_prefix (q : Nat) (input : List Bool) (base : Tape → List Bool) (state : State q) (budget : Nat) (prefixRun : StateTransition.EvalsToInTime (Driver.machine q).step (initList (Driver.machine q) input) (some ⟨some .reverseOutput, state, base⟩) budget) (k : Tape) : (base k).length ≤ input.length + budget * Runtime.programPushBound (Driver.machine q) := by have bound := Runtime.executionSizeBound (Driver.machine q).step (fun cfg => (cfg.stk k).length) (Runtime.programPushBound (Driver.machine q)) (Runtime.stepStackLength (Driver.machine q) k) prefixRun exact bound.trans (Nat.add_le_add_right (Runtime.initialStackLength (Driver.machine q) input k) _) theorem finishBudget_le_of_prefix (q : Nat) (input : List Bool) (base : Tape → List Bool) (word : List Bool) (state : State q) (budget : Nat) (prefixRun : StateTransition.EvalsToInTime (Driver.machine q).step (initList (Driver.machine q) input) (some ⟨some .reverseOutput, state, base⟩) budget) (hreversed : base .reversed = word.reverse) : finishBudget base word ≤ 16 * (input.length + budget * Runtime.programPushBound (Driver.machine q)) + 17 := by let B := input.length + budget * Runtime.programPushBound (Driver.machine q) have wordBound : word.length ≤ B := by simpa only [hreversed, List.length_reverse] using stackLength_after_prefix q input base state budget prefixRun .reversed have sumBound : (∑ i : Fin 15, (afterReversal base word (cleanupPorts i)).length) ≤ 15 * B := by calc _ ≤ ∑ _i : Fin 15, B := by apply Finset.sum_le_sum intro i _ exact (afterReversal_selected_length_le base word i).trans (stackLength_after_prefix q input base state budget prefixRun (cleanupPorts i)) _ = 15 * B := by simp dsimp only [finishBudget] omega noncomputable def completedPolynomial (q : Nat) (prefixTime : Polynomial Nat) : Polynomial Nat := prefixTime + Polynomial.C 16 * (Polynomial.X + Polynomial.C (Runtime.programPushBound (Driver.machine q)) * prefixTime) + Polynomial.C 17 def finishAfterPrefix (q : Nat) (input : List Bool) (base : Tape → List Bool) (word : List Bool) (state : State q) (budget : Nat) (prefixRun : StateTransition.EvalsToInTime (Driver.machine q).step (initList (Driver.machine q) input) (some ⟨some .reverseOutput, state, base⟩) budget) (hreversed : base .reversed = word.reverse) (houtput : base .output = []) : TM2OutputsInTime (Driver.machine q) input (some word) (budget + 16 * (input.length + budget * Runtime.programPushBound (Driver.machine q)) + 17) := by have last := finishInTime q base word state hreversed houtput let run := StateTransition.EvalsToInTime.trans (Driver.machine q).step _ _ (initList (Driver.machine q) input) ⟨some .reverseOutput, state, base⟩ (some (haltList (Driver.machine q) word)) prefixRun last refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ } have finishBound := finishBudget_le_of_prefix q input base word state budget prefixRun hreversed have allBound := run.steps_le_m omega def finishAfterPolynomialPrefix (q : Nat) (input : List Bool) (base : Tape → List Bool) (word : List Bool) (state : State q) (prefixTime : Polynomial Nat) (prefixRun : StateTransition.EvalsToInTime (Driver.machine q).step (initList (Driver.machine q) input) (some ⟨some .reverseOutput, state, base⟩) (prefixTime.eval input.length)) (hreversed : base .reversed = word.reverse) (houtput : base .output = []) : TM2OutputsInTime (Driver.machine q) input (some word) ((completedPolynomial q prefixTime).eval input.length) := by have run := finishAfterPrefix q input base word state (prefixTime.eval input.length) prefixRun hreversed houtput refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ } simpa only [completedPolynomial, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_X, Nat.mul_comm] using run.steps_le_m end BinPackingGames.Foundations.PCP.AlphabetTable.Finish namespace BinPackingGames.Foundations.PCP.AlphabetTable.Body open Turing open BinPackingGames.Foundations.Complexity open RuntimeModel variable {q : Nat} def bodyTime (q N : Nat) : Nat := (32 + 8 * (Enumeration.localCount q * (6 * (2 * 4104)))) * (N + 1) structure RowStepResult (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) (oldRelation : GenericGraphTables.RelationTable q) (oldFlag : Bool) where tapes : Tape → List Bool ready : LoopState.Ready input (e.val + 1) tapes run : StateTransition.EvalsToInTime (TM2.step (Driver.program q)) ⟨some .guard, normal oldRelation oldFlag, base⟩ (some ⟨some .guard, CompareLoop.rowState input e, tapes⟩) (bodyTime q (LoopState.inputBits input).length) def guardTapes (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) : Tape → List Bool := Function.update base .counter (encodeWord (input.darts - (e.val + 1))) def parsedTapes (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) : Tape → List Bool := Lookup.readRowTapes .cursor .tail .reverseIndex (guardTapes input e base) input.rows[e] (LoopState.cursorBits input (e.val + 1)) def workingTapes (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) : Tape → List Bool := Lookup.headLookupTapes headPorts (parsedTapes input e base) input e def emittedTapes (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) : Tape → List Bool := Emitter.resultTapes .reversed (workingTapes input e base) (encodeWords (EmitRows.blockWords input e)) def clearFieldsTapes (base : Tape → List Bool) : Tape → List Bool := Function.update (Function.update (Function.update base .tail []) .reverseIndex []) .head [] def resultTapes (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) : Tape → List Bool := Function.update (clearFieldsTapes (emittedTapes input e base)) .edge (encodeWord (e.val + 1)) private theorem workingTapes_other (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) (other : Tape) (hcounter : other ≠ .counter) (hcursor : other ≠ .cursor) (htail : other ≠ .tail) (hreverse : other ≠ .reverseIndex) (hscan : other ≠ .scan) (hindex : other ≠ .indexScan) (hhead : other ≠ .head) : workingTapes input e base other = base other := by simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts, parsedTapes, Lookup.readRowTapes, guardTapes, hcounter, hcursor, htail, hreverse, hscan, hindex, hhead] private theorem workingTapes_counter (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) : workingTapes input e base .counter = encodeWord (input.darts - (e.val + 1)) := by simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts, parsedTapes, Lookup.readRowTapes, guardTapes] private theorem workingTapes_cursor (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) : workingTapes input e base .cursor = LoopState.cursorBits input (e.val + 1) := by simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts, parsedTapes, Lookup.readRowTapes] private theorem workingTapes_tail (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) : workingTapes input e base .tail = encodeWord input.rows[e].tail.val := by simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts, parsedTapes, Lookup.readRowTapes] private theorem workingTapes_reverse (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) : workingTapes input e base .reverseIndex = encodeWord input.rows[e].reverseIndex.val := by simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts, parsedTapes, Lookup.readRowTapes] private theorem workingTapes_head (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) : workingTapes input e base .head = encodeWord input.rows[input.rows[e].reverseIndex].tail.val := by simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts, Lookup.headValue, Lookup.headIndex] def clearFieldsInTime (base : Tape → List Bool) (ambient : Ambient q) : StateTransition.EvalsToInTime (TM2.step (Driver.program q)) ⟨some (.clearField 0), (ambient, none), base⟩ (some ⟨some .increment, (ambient, none), clearFieldsTapes base⟩) ((base .tail).length + (base .reverseIndex).length + (base .head).length + 3) := by let first := MachineDrain.drainInTime Tape.tail (Driver.Label.clearField 0) (some (.clearField 1)) (Driver.program q) (by rfl) base ambient none let second := MachineDrain.drainInTime Tape.reverseIndex (Driver.Label.clearField 1) (some (.clearField 2)) (Driver.program q) (by rfl) (Function.update base .tail []) ambient none let third := MachineDrain.drainInTime Tape.head (Driver.Label.clearField 2) (some .increment) (Driver.program q) (by rfl) (Function.update (Function.update base .tail []) .reverseIndex []) ambient none let firstTwo := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ first second let run := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ firstTwo third refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ } have h := run.steps_le_m simp only [Function.update_of_ne (by decide : Tape.reverseIndex ≠ .tail), Function.update_of_ne (by decide : Tape.head ≠ .reverseIndex), Function.update_of_ne (by decide : Tape.head ≠ .tail)] at h omega def incrementInTime (base : Tape → List Bool) (ambient : Ambient q) (e : Nat) (he : base .edge = encodeWord e) : StateTransition.EvalsToInTime (TM2.step (Driver.program q)) ⟨some .increment, (ambient, none), base⟩ (some ⟨some .guard, (ambient, none), Function.update base .edge (encodeWord (e + 1))⟩) 1 where steps := 1 evals_in_steps := by change some (TM2.stepAux (nextRow (Driver.Label.guard : Driver.Label q)) (ambient, none) base) = _ simpa only [List.append_nil] using congrArg some (stepAux_nextRow (Driver.Label.guard : Driver.Label q) (ambient, none) base e [] (by simpa only [List.append_nil] using he)) steps_le_m := Nat.le_refl _ private theorem emitterView_at (label : Emitter.Label (Driver.rowPlan q).length (Addresses.fieldBound q)) : (MachineControl.program (Equiv.refl (Driver.Label q)) (emitterEquiv q).symm (Driver.program q)) (.emit label) = Emitter.statement (Emitter.listCommands (Driver.rowPlan q)) rowSources Tape.scratch Tape.reversed Driver.Label.emit (some (.clearField 0)) label := by change MachineControl.statement id (emitterEquiv q).symm (MachineControl.statement id (emitterEquiv q) _) = _ exact MachineFieldProfile.statement_roundtrip (emitterEquiv q).symm _ private theorem context_rowState (input : GenericGraphTables.Table q) (e : Fin input.darts) : context (CompareLoop.rowState input e).1 = EmitRows.rowContext input e := by change (input.rows[e].relation, decide (input.rows[e].tail.val = input.rows[input.rows[e].reverseIndex].tail.val)) = (input.rows[e].relation, decide (input.rows[e].tail = input.rows[input.rows[e].reverseIndex].tail)) simp only [Fin.val_inj] def emitInTime (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) (operands : ∀ i, base (rowSources i) = encodeWord (EmitRows.rowValues input e i)) (scratchEmpty : base .scratch = []) : StateTransition.EvalsToInTime (TM2.step (Driver.program q)) ⟨some (Driver.rowEntry q), CompareLoop.rowState input e, base⟩ (some ⟨some (.clearField 0), CompareLoop.rowState input e, Emitter.resultTapes .reversed base (encodeWords (EmitRows.blockWords input e))⟩) (Enumeration.localCount q * (6 * (2 * 4104)) * (3 * ((LoopState.inputBits input).length + 1) + 3) + 1) := by let view := MachineControl.program (Equiv.refl (Driver.Label q)) (emitterEquiv q).symm (Driver.program q) have sourceScratch : ∀ i : Fin 5, rowSources i ≠ Tape.scratch := by intro i; fin_cases i <;> decide have sourceOutput : ∀ i : Fin 5, rowSources i ≠ Tape.reversed := by intro i; fin_cases i <;> decide have bounded : ∀ i, EmitRows.rowValues input e i ≤ (LoopState.inputBits input).length := by have hn := GenericGraphTables.vertices_le_tableBits_length input have hm := GenericGraphTables.darts_le_tableBits_length input have he := e.isLt have ht := input.rows[e].tail.isLt have hh := input.rows[input.rows[e].reverseIndex].tail.isLt intro i fin_cases i <;> simp [EmitRows.rowValues, Addresses.values, LoopState.inputBits] <;> omega have run := EmitRows.blockInTimeAmbient input e (@context q) (CompareLoop.rowState input e).1 (context_rowState input e) rowSources Tape.scratch Tape.reversed sourceScratch sourceOutput (by decide) Driver.Label.emit (some (.clearField 0)) view emitterView_at base operands scratchEmpty (LoopState.inputBits input).length bounded have restored : MachineControl.program (Equiv.refl (Driver.Label q)) (emitterEquiv q) view = Driver.program q := by funext label exact MachineFieldProfile.statement_roundtrip (emitterEquiv q) (Driver.program q label) have transported := transportInTime (emitterEquiv q) view run rw [restored] at transported simpa [MachineControl.configuration, emitterEquiv, Option.map_some, id_eq, Driver.rowEntry, Driver.rowPlan, CompareLoop.rowState, normal] using transported private theorem result_ready (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) (ready : LoopState.Ready input e.val base) : LoopState.Ready input (e.val + 1) (resultTapes input e base) := by constructor · exact Nat.succ_le_of_lt e.isLt all_goals simp [resultTapes, clearFieldsTapes, emittedTapes, Emitter.resultTapes, workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts, parsedTapes, Lookup.readRowTapes, guardTapes, ready.original, ready.vertices, ready.darts, ready.scratch, ready.compareLeft, ready.compareRight, ready.output, ready.reversed, LoopState.prefixBits_reverse_succ input e.val e.isLt] private theorem workingOperands (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) (ready : LoopState.Ready input e.val base) : ∀ i, workingTapes input e base (rowSources i) = encodeWord (EmitRows.rowValues input e i) := by intro i fin_cases i <;> simp [rowSources, EmitRows.rowValues, Addresses.values, workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts, parsedTapes, Lookup.readRowTapes, guardTapes, Lookup.headValue, Lookup.headIndex, ready.vertices, ready.darts, ready.edge] def rowStep (input : GenericGraphTables.Table q) (e : Fin input.darts) (base : Tape → List Bool) (ready : LoopState.Ready input e.val base) (oldRelation : GenericGraphTables.RelationTable q) (oldFlag : Bool) : RowStepResult input e base oldRelation oldFlag := by let N := (LoopState.inputBits input).length have guarded : StateTransition.EvalsToInTime (TM2.step (Driver.program q)) ⟨some .guard, normal oldRelation oldFlag, base⟩ (some ⟨some .tailStart, normal oldRelation oldFlag, guardTapes input e base⟩) 1 := by have hbase : MachineUnaryCounter.counterTapes .counter base (input.darts - (e.val + 1) + 1) [] = base := by simp only [MachineUnaryCounter.counterTapes, List.append_nil, ← ready.counter_succ e.isLt, Function.update_eq_self] have run := MachineUnaryCounter.guardInTime_succ Tape.counter Driver.Label.guard .tailStart .reverseOutput (Driver.program q) (by rfl) base (input.darts - (e.val + 1)) [] (normal oldRelation oldFlag).1 none rw [hbase] at run simpa only [MachineUnaryCounter.counterTapes, List.append_nil, guardTapes, normal] using run have parsed : StateTransition.EvalsToInTime (TM2.step (Driver.program q)) ⟨some .tailStart, normal oldRelation oldFlag, guardTapes input e base⟩ (some ⟨some .headCopyFirst, normal input.rows[e].relation oldFlag, parsedTapes input e base⟩) (Lookup.readRowCost input.rows[e]) := by have hcursor : guardTapes input e base .cursor = Lookup.rowBits input.rows[e] ++ LoopState.cursorBits input (e.val + 1) := by simpa only [guardTapes, Function.update_of_ne (by decide : Tape.cursor ≠ .counter), Lookup.rowBits] using ready.cursor.trans (LoopState.cursorBits_succ input e.val e.isLt) have htail : guardTapes input e base .tail = [] := by simp [guardTapes, ready.tail] have hreverse : guardTapes input e base .reverseIndex = [] := by simp [guardTapes, ready.reverseIndex] simpa only [normal, parsedTapes] using Lookup.readRowInTime Tape.cursor Tape.tail Tape.reverseIndex (by decide) (by decide) (by decide) Driver.Label.tailStart .tailLoop .reverseStart .reverseLoop .readPredicate .headCopyFirst (Driver.program q) (by rfl) (by rfl) (by rfl) (by rfl) (by rfl) (guardTapes input e base) input.rows[e] (LoopState.cursorBits input (e.val + 1)) hcursor htail hreverse (oldFlag, false, none) oldRelation none have looked : StateTransition.EvalsToInTime (TM2.step (Driver.program q)) ⟨some .headCopyFirst, normal input.rows[e].relation oldFlag, parsedTapes input e base⟩ (some ⟨some (.compare .leftFirst), normal input.rows[e].relation oldFlag, workingTapes input e base⟩) (7 * N + 5) := by have htable : parsedTapes input e base (headPorts 0) = GenericGraphTables.tableBits input := by simpa [headPorts, parsedTapes, Lookup.readRowTapes, guardTapes, LoopState.inputBits] using ready.original have hreverse : parsedTapes input e base (headPorts 1) = encodeWord (Lookup.headIndex input e).val := by simp [headPorts, parsedTapes, Lookup.readRowTapes, Lookup.headIndex] have hhead : parsedTapes input e base (headPorts 4) = [] := by simp [headPorts, parsedTapes, Lookup.readRowTapes, guardTapes, ready.head] have hscratch : parsedTapes input e base (headPorts 5) = [] := by simp [headPorts, parsedTapes, Lookup.readRowTapes, guardTapes, ready.scratch] have run := Lookup.headLookupInTime headPorts headPorts_injective Driver.Label.headCopyFirst .headCopySecond .indexCopyFirst .indexCopySecond .headVertices .headDarts Driver.Label.headLookup (some (.compare .leftFirst)) (Driver.program q) (by rfl) (by rfl) (by rfl) (by rfl) (by rfl) (by rfl) (fun _ => rfl) (parsedTapes input e base) input e htable hreverse hhead hscratch (normal input.rows[e].relation oldFlag).1 none simpa only [workingTapes, normal, N, LoopState.inputBits, Lookup.headTimePolynomial, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_X] using run have hleft : workingTapes input e base .compareLeft = [] := (workingTapes_other input e base .compareLeft (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)).trans ready.compareLeft have hright : workingTapes input e base .compareRight = [] := (workingTapes_other input e base .compareRight (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)).trans ready.compareRight have hscratch : workingTapes input e base .scratch = [] := (workingTapes_other input e base .scratch (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)).trans ready.scratch let compared := CompareLoop.compareRowInTime Driver.Label.compare (Driver.rowEntry q) (Driver.program q) (fun _ => rfl) input e (workingTapes input e base) (workingTapes_tail input e base) (workingTapes_head input e base) hleft hright hscratch oldFlag let emitted := emitInTime input e (workingTapes input e base) (workingOperands input e base ready) hscratch let cleared := clearFieldsInTime (emittedTapes input e base) (CompareLoop.rowState input e).1 have hedge : clearFieldsTapes (emittedTapes input e base) .edge = encodeWord e.val := by simp [clearFieldsTapes, emittedTapes, Emitter.resultTapes, workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts, parsedTapes, Lookup.readRowTapes, guardTapes, ready.edge] let incremented := incrementInTime (clearFieldsTapes (emittedTapes input e base)) (CompareLoop.rowState input e).1 e.val hedge let run₁ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ guarded parsed let run₂ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₁ looked let run₃ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₂ compared let run₄ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₃ emitted let run₅ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₄ cleared let run := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₅ incremented refine { tapes := resultTapes input e base ready := result_ready input e base ready run := { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ } } have hn : input.vertices ≤ N := GenericGraphTables.vertices_le_tableBits_length input have hm : input.darts ≤ N := GenericGraphTables.darts_le_tableBits_length input have hr : Lookup.readRowCost input.rows[e] ≤ 2 * N + 5 := Lookup.readRowCost_table_le input e have ht : (emittedTapes input e base .tail).length ≤ N := by simp only [emittedTapes, Emitter.resultTapes, Function.update_of_ne (by decide : Tape.tail ≠ .reversed), workingTapes_tail, encodeWord_length] exact (Nat.succ_le_of_lt input.rows[e].tail.isLt).trans hn have hv : (emittedTapes input e base .reverseIndex).length ≤ N := by simp only [emittedTapes, Emitter.resultTapes, Function.update_of_ne (by decide : Tape.reverseIndex ≠ .reversed), workingTapes_reverse, encodeWord_length] exact (Nat.succ_le_of_lt input.rows[e].reverseIndex.isLt).trans hm have hh : (emittedTapes input e base .head).length ≤ N := by simp only [emittedTapes, Emitter.resultTapes, Function.update_of_ne (by decide : Tape.head ≠ .reversed), workingTapes_head, encodeWord_length] exact (Nat.succ_le_of_lt input.rows[input.rows[e].reverseIndex].tail.isLt).trans hn have budget := run.steps_le_m change run.steps ≤ (32 + 8 * (Enumeration.localCount q * (6 * (2 * 4104)))) * (N + 1) nlinarith end BinPackingGames.Foundations.PCP.AlphabetTable.Body namespace BinPackingGames.Foundations.PCP.AlphabetTable.LoopInitialization open BinPackingGames.Foundations.Complexity open RuntimeModel LoopState theorem initialization_ready {q : Nat} (input : GenericGraphTables.Table q) : Ready input 0 (Initialization.loopTapes input) where le_darts := Nat.zero_le _ original := rfl cursor := by change Initialization.rowsBits input = cursorBits input 0 rw [cursorBits_zero] rfl vertices := rfl darts := rfl counter := by simp [Initialization.loopTapes] edge := rfl tail := rfl reverseIndex := rfl head := rfl scratch := rfl compareLeft := rfl compareRight := rfl output := rfl reversed := by change (Initialization.headerBits input).reverse = (prefixBits input 0).reverse rw [prefixBits_zero] rfl end BinPackingGames.Foundations.PCP.AlphabetTable.LoopInitialization namespace BinPackingGames.Foundations.PCP.AlphabetTable.Loop open Turing open BinPackingGames.Foundations.Complexity open RuntimeModel variable {q : Nat} def zeroGuardInTime (input : GenericGraphTables.Table q) (base : Tape → List Bool) (ready : LoopState.Ready input input.darts base) (relation : GenericGraphTables.RelationTable q) (flag : Bool) : StateTransition.EvalsToInTime (TM2.step (Driver.program q)) ⟨some .guard, normal relation flag, base⟩ (some ⟨some .reverseOutput, normal relation flag, base⟩) 1 := by have counterSelf : MachineUnaryCounter.counterTapes Tape.counter base 0 [] = base := by simp only [MachineUnaryCounter.counterTapes, List.append_nil, ← ready.counter_at_end, Function.update_eq_self] have run := MachineUnaryCounter.guardInTime_zero Tape.counter (Driver.Label.guard : Driver.Label q) .tailStart .reverseOutput (Driver.program q) (Driver.program_guard q) base [] (normal relation flag).1 none simpa only [counterSelf, normal] using run structure LoopResult (input : GenericGraphTables.Table q) (e remaining : Nat) (base : Tape → List Bool) (oldRelation : GenericGraphTables.RelationTable q) (oldFlag : Bool) where tapes : Tape → List Bool relation : GenericGraphTables.RelationTable q flag : Bool ready : LoopState.Ready input input.darts tapes run : StateTransition.EvalsToInTime (TM2.step (Driver.program q)) ⟨some .guard, normal oldRelation oldFlag, base⟩ (some ⟨some .reverseOutput, normal relation flag, tapes⟩) (remaining * Body.bodyTime q (LoopState.inputBits input).length + 1) def runRemaining (input : GenericGraphTables.Table q) : (remaining e : Nat) → e + remaining = input.darts → (base : Tape → List Bool) → LoopState.Ready input e base → (oldRelation : GenericGraphTables.RelationTable q) → (oldFlag : Bool) → LoopResult input e remaining base oldRelation oldFlag | 0, e, finished, base, ready, oldRelation, oldFlag => by have atEnd : e = input.darts := by omega subst e exact { tapes := base relation := oldRelation flag := oldFlag ready := ready run := by simpa only [Nat.zero_mul, Nat.zero_add] using zeroGuardInTime input base ready oldRelation oldFlag } | remaining + 1, e, finished, base, ready, oldRelation, oldFlag => by have beforeEnd : e < input.darts := by omega let edge : Fin input.darts := ⟨e, beforeEnd⟩ let one := Body.rowStep input edge base ready oldRelation oldFlag let rest := runRemaining input remaining (e + 1) (by omega) one.tapes one.ready input.rows[edge].relation (CompareLoop.rowLoop input edge) let composed := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ one.run rest.run refine { tapes := rest.tapes relation := rest.relation flag := rest.flag ready := rest.ready run := { toEvalsTo := composed.toEvalsTo, steps_le_m := ?_ } } have budget := composed.steps_le_m simpa only [Nat.succ_mul, Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] using budget def prefixTime (q N : Nat) : Nat := Initialization.time N + N * Body.bodyTime q N + 1 private local instance prefixRunSizeOf (input : GenericGraphTables.Table q) (tapes : Tape → List Bool) (relation : GenericGraphTables.RelationTable q) (flag : Bool) : SizeOf (StateTransition.EvalsToInTime (TM2.step (Driver.program q)) (initList (Driver.machine q) (GenericGraphTables.tableBits input)) (some ⟨some .reverseOutput, normal relation flag, tapes⟩) (prefixTime q (LoopState.inputBits input).length)) := @StateTransition.EvalsToInTime._sizeOf_inst (TM2.Cfg Alphabet (Driver.Label q) (State q)) (TM2.step (Driver.program q)) (initList (Driver.machine q) (GenericGraphTables.tableBits input)) (some ⟨some .reverseOutput, normal relation flag, tapes⟩) (prefixTime q (LoopState.inputBits input).length) inferInstance structure PrefixResult (input : GenericGraphTables.Table q) where tapes : Tape → List Bool relation : GenericGraphTables.RelationTable q flag : Bool ready : LoopState.Ready input input.darts tapes run : StateTransition.EvalsToInTime (TM2.step (Driver.program q)) (initList (Driver.machine q) (GenericGraphTables.tableBits input)) (some ⟨some .reverseOutput, normal relation flag, tapes⟩) (prefixTime q (LoopState.inputBits input).length) def runPrefix (input : GenericGraphTables.Table q) : PrefixResult input := by let rows := runRemaining input input.darts 0 (by simp) (Initialization.loopTapes input) (LoopInitialization.initialization_ready input) (Vector.replicate (q * q) false) false let composed := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ (Initialization.initializeInTime input) rows.run refine { tapes := rows.tapes relation := rows.relation flag := rows.flag ready := rows.ready run := { toEvalsTo := composed.toEvalsTo, steps_le_m := ?_ } } have budget := composed.steps_le_m change composed.steps ≤ input.darts * Body.bodyTime q (LoopState.inputBits input).length + 1 + Initialization.time (LoopState.inputBits input).length at budget have rowCount : input.darts ≤ (LoopState.inputBits input).length := GenericGraphTables.darts_le_tableBits_length input have rowBudget := Nat.mul_le_mul_right (Body.bodyTime q (LoopState.inputBits input).length) rowCount unfold prefixTime omega theorem PrefixResult.reversed {input : GenericGraphTables.Table q} (result : PrefixResult input) : result.tapes .reversed = (GraphTables.tableBits (Table.build input)).reverse := result.ready.reversed_at_end theorem PrefixResult.output {input : GenericGraphTables.Table q} (result : PrefixResult input) : result.tapes .output = [] := result.ready.output end BinPackingGames.Foundations.PCP.AlphabetTable.Loop namespace BinPackingGames.Foundations.PCP.AlphabetTable.Runtime open Turing open BinPackingGames.Foundations.Complexity open RuntimeModel noncomputable def prefixPolynomial (q : Nat) : Polynomial Nat := Polynomial.C 27 * Polynomial.X + Polynomial.C 54 + Polynomial.X * (Polynomial.C (32 + 8 * (Enumeration.localCount q * (6 * (2 * 4104)))) * (Polynomial.X + Polynomial.C 1)) + Polynomial.C 1 theorem prefixPolynomial_eval (q N : Nat) : (prefixPolynomial q).eval N = Loop.prefixTime q N := by simp only [prefixPolynomial, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_X, Loop.prefixTime, Initialization.time, Body.bodyTime] ring noncomputable def time (q : Nat) : Polynomial Nat := Finish.completedPolynomial q (prefixPolynomial q) noncomputable def tableOutputsInTime {q : Nat} (input : GenericGraphTables.Table q) : TM2OutputsInTime (Driver.machine q) (GenericGraphTables.tableBits input) (some (GraphTables.tableBits (Table.build input))) ((time q).eval (GenericGraphTables.tableBits input).length) := by let prefixRun := Loop.runPrefix input have hrun : StateTransition.EvalsToInTime (Driver.machine q).step (initList (Driver.machine q) (GenericGraphTables.tableBits input)) (some ⟨some .reverseOutput, normal prefixRun.relation prefixRun.flag, prefixRun.tapes⟩) ((prefixPolynomial q).eval (GenericGraphTables.tableBits input).length) := by rw [prefixPolynomial_eval] exact prefixRun.run exact Finish.finishAfterPolynomialPrefix q (GenericGraphTables.tableBits input) prefixRun.tapes (GraphTables.tableBits (Table.build input)) (normal prefixRun.relation prefixRun.flag) (prefixPolynomial q) hrun prefixRun.ready.reversed_at_end prefixRun.ready.output noncomputable def tablePolynomialTime (q : Nat) : TM2ComputableInPolyTime (GenericGraphTables.tableBits (q := q)) GraphTables.tableBits (Table.build (q := q)) where tm := Driver.machine q inputAlphabet := Equiv.refl Bool outputAlphabet := Equiv.refl Bool time := time q outputsFun input := by change TM2OutputsInTime (Driver.machine q) ((GenericGraphTables.tableBits input).map id) (some ((GraphTables.tableBits (Table.build input)).map id)) _ have input_eq : @List.map ((Driver.machine q).Γ (Driver.machine q).k₀) ((Driver.machine q).Γ (Driver.machine q).k₀) id (GenericGraphTables.tableBits input) = GenericGraphTables.tableBits input := List.map_id _ have output_eq : @List.map ((Driver.machine q).Γ (Driver.machine q).k₁) ((Driver.machine q).Γ (Driver.machine q).k₁) id (GraphTables.tableBits (Table.build input)) = GraphTables.tableBits (Table.build input) := List.map_id _ simpa only [Equiv.refl, input_eq, output_eq] using tableOutputsInTime input end BinPackingGames.Foundations.PCP.AlphabetTable.Runtime namespace BinPackingGames.Foundations.PCP.AlphabetTable open BinPackingGames.Foundations.Complexity theorem Driver.finiteAlphabet (q : Nat) : MachineFiniteAlphabet.FiniteAlphabet (Driver.machine q) := by intro k change Finite Bool infer_instance theorem Runtime.finiteAlphabet (q : Nat) : MachineFiniteAlphabet.FiniteAlphabet (Runtime.tablePolynomialTime q).tm := Driver.finiteAlphabet q end BinPackingGames.Foundations.PCP.AlphabetTable namespace BinPackingGames.Foundations.PCP.RoundComputation open Turing open BinPackingGames.Foundations.Complexity abbrev PreprocessingCertificate (H : RoundTables.BaseTable) := TM2ComputableInPolyTime GraphTables.tableBits (PortTables.inputBits (ports := PreprocessingTables.degree)) (PreprocessingTables.output H) abbrev PoweringCertificate := TM2ComputableInPolyTime (PortTables.inputBits (ports := PreprocessingTables.degree)) (GenericGraphTables.tableBits (q := RoundTables.alphabet)) (PoweringTables.transform PreprocessingTables.degree RoundTables.walkParameter) theorem powered_eq_transform (H : RoundTables.BaseTable) (table : GraphTables.Table) : RoundTables.powered H table = PoweringTables.transform PreprocessingTables.degree RoundTables.walkParameter (PreprocessingTables.output H table) := rfl noncomputable def poweredPolynomialTimeOfCertificates (H : RoundTables.BaseTable) (preprocessing : PreprocessingCertificate H) (powering : PoweringCertificate) : TM2ComputableInPolyTime GraphTables.tableBits (GenericGraphTables.tableBits (q := RoundTables.alphabet)) (RoundTables.powered H) := by change TM2ComputableInPolyTime GraphTables.tableBits _ (fun table => PoweringTables.transform PreprocessingTables.degree RoundTables.walkParameter (PreprocessingTables.output H table)) exact MachineSequential.composeBits preprocessing powering noncomputable def tablePolynomialTimeOfCertificates (H : RoundTables.BaseTable) (preprocessing : PreprocessingCertificate H) (powering : PoweringCertificate) : TM2ComputableInPolyTime GraphTables.tableBits GraphTables.tableBits (RoundTables.build H) := by change TM2ComputableInPolyTime GraphTables.tableBits GraphTables.tableBits (fun table => AlphabetTable.Table.build (RoundTables.powered H table)) exact MachineSequential.composeBits (poweredPolynomialTimeOfCertificates H preprocessing powering) (AlphabetTable.Runtime.tablePolynomialTime RoundTables.alphabet) theorem powered_finiteAlphabet (H : RoundTables.BaseTable) (preprocessing : PreprocessingCertificate H) (powering : PoweringCertificate) (hpreprocessing : MachineFiniteAlphabet.FiniteAlphabet preprocessing.tm) (hpowering : MachineFiniteAlphabet.FiniteAlphabet powering.tm) : MachineFiniteAlphabet.FiniteAlphabet (poweredPolynomialTimeOfCertificates H preprocessing powering).tm := MachineFiniteAlphabet.composeBits preprocessing powering hpreprocessing hpowering theorem finiteAlphabet (H : RoundTables.BaseTable) (preprocessing : PreprocessingCertificate H) (powering : PoweringCertificate) (hpreprocessing : MachineFiniteAlphabet.FiniteAlphabet preprocessing.tm) (hpowering : MachineFiniteAlphabet.FiniteAlphabet powering.tm) : MachineFiniteAlphabet.FiniteAlphabet (tablePolynomialTimeOfCertificates H preprocessing powering).tm := MachineFiniteAlphabet.composeBits (poweredPolynomialTimeOfCertificates H preprocessing powering) (AlphabetTable.Runtime.tablePolynomialTime RoundTables.alphabet) (powered_finiteAlphabet H preprocessing powering hpreprocessing hpowering) (AlphabetTable.Runtime.finiteAlphabet RoundTables.alphabet) noncomputable def tablePolynomialTime (H : RoundTables.BaseTable) (preprocessing : PreprocessingCertificate H) : TM2ComputableInPolyTime GraphTables.tableBits GraphTables.tableBits (RoundTables.build H) := tablePolynomialTimeOfCertificates H preprocessing (PoweringMachineRuntime.computableInPolyTime PreprocessingTables.degree RoundTables.walkParameter) theorem tablePolynomialTime_finite_alphabet (H : RoundTables.BaseTable) (preprocessing : PreprocessingCertificate H) (hpreprocessing : MachineFiniteAlphabet.FiniteAlphabet preprocessing.tm) : MachineFiniteAlphabet.FiniteAlphabet (tablePolynomialTime H preprocessing).tm := finiteAlphabet H preprocessing (PoweringMachineRuntime.computableInPolyTime PreprocessingTables.degree RoundTables.walkParameter) hpreprocessing (PoweringMachineRuntime.computableInPolyTime_finite_alphabet PreprocessingTables.degree RoundTables.walkParameter) end BinPackingGames.Foundations.PCP.RoundComputation namespace BinPackingGames.Foundations.PCP.PreprocessingFinishRuntime open Turing BinPackingGames.Foundations.Complexity open PreprocessingRegularTables def finish (H : PreprocessingTables.BaseTable) (d : Nat) (input : PortTables.Input d) : PortTables.Input (2 * (d + internalDegree)) := PreprocessingStageMaps.lazy (d + internalDegree) (PreprocessingStageMaps.paddedOverlay H d input) theorem output_eq_finish (H : PreprocessingTables.BaseTable) (t : GraphTables.Table) : PreprocessingTables.output H t = finish H (internalDegree + 1) (PreprocessingStageMaps.regular H t) := rfl theorem finish_degree_positive (d : Nat) (hd : 0 < d) : 0 < d + internalDegree := hd.trans_le (Nat.le_add_right d internalDegree) noncomputable def computableInPolyTime (H : PreprocessingTables.BaseTable) (d : Nat) (hd : 0 < d) : TM2ComputableInPolyTime (PortTables.inputBits (ports := d)) (PortTables.inputBits (ports := 2 * (d + internalDegree))) (finish H d) := by change TM2ComputableInPolyTime _ _ (fun input => PreprocessingStageMaps.lazy (d + internalDegree) (PreprocessingStageMaps.paddedOverlay H d input)) exact MachineSequential.composeBits (MachinePaddedOverlayRuntime.computableInPolyTime H d hd) (MachineLazyTableRuntime.computableInPolyTime (d + internalDegree) (finish_degree_positive d hd)) theorem finite_alphabet (H : PreprocessingTables.BaseTable) (d : Nat) (hd : 0 < d) : MachineFiniteAlphabet.FiniteAlphabet (computableInPolyTime H d hd).tm := MachineFiniteAlphabet.composeBits (MachinePaddedOverlayRuntime.computableInPolyTime H d hd) (MachineLazyTableRuntime.computableInPolyTime (d + internalDegree) (finish_degree_positive d hd)) (MachinePaddedOverlayRuntime.finite_alphabet H d hd) (MachineLazyTableRuntime.finite_alphabet (d + internalDegree) (finish_degree_positive d hd)) end BinPackingGames.Foundations.PCP.PreprocessingFinishRuntime namespace BinPackingGames.Foundations.PCP.PreprocessingRuntime open Turing BinPackingGames.Foundations.Complexity open PreprocessingRegularTables noncomputable def tablePolynomialTime (H : PreprocessingTables.BaseTable) : TM2ComputableInPolyTime GraphTables.tableBits (PortTables.inputBits (ports := PreprocessingTables.degree)) (PreprocessingTables.output H) := by change TM2ComputableInPolyTime GraphTables.tableBits (PortTables.inputBits (ports := 2 * ((internalDegree + 1) + internalDegree))) (fun t => PreprocessingFinishRuntime.finish H (internalDegree + 1) (PreprocessingStageMaps.regular H t)) exact MachineSequential.composeBits (MachineRegularTableRuntime.computableInPolyTime H) (PreprocessingFinishRuntime.computableInPolyTime H (internalDegree + 1) (Nat.succ_pos internalDegree)) theorem finiteAlphabet (H : PreprocessingTables.BaseTable) : MachineFiniteAlphabet.FiniteAlphabet (tablePolynomialTime H).tm := MachineFiniteAlphabet.composeBits (MachineRegularTableRuntime.computableInPolyTime H) (PreprocessingFinishRuntime.computableInPolyTime H (internalDegree + 1) (Nat.succ_pos internalDegree)) (MachineRegularTableRuntime.finite_alphabet H) (PreprocessingFinishRuntime.finite_alphabet H (internalDegree + 1) (Nat.succ_pos internalDegree)) end BinPackingGames.Foundations.PCP.PreprocessingRuntime namespace BinPackingCompleteness.PreprocessingMachine open Turing BinPackingGames.Foundations open Complexity PCP Target def preprocessing : RoundComputation.PreprocessingCertificate PCPSource.baseTable := PreprocessingRuntime.tablePolynomialTime PCPSource.baseTable theorem preprocessing_finite_alphabet : MachineFiniteAlphabet.FiniteAlphabet preprocessing.tm := PreprocessingRuntime.finiteAlphabet PCPSource.baseTable def round : TM2ComputableInPolyTime GraphTables.tableBits GraphTables.tableBits (RoundTables.build PCPSource.baseTable) := RoundComputation.tablePolynomialTime PCPSource.baseTable preprocessing theorem round_finite_alphabet : MachineFiniteAlphabet.FiniteAlphabet round.tm := RoundComputation.tablePolynomialTime_finite_alphabet PCPSource.baseTable preprocessing preprocessing_finite_alphabet def computableInPolyTime : TM2ComputableInPolyTime formulaBits formulaBits PCPSource.rawFormula := MachineTableIteration.gapMapCertificate PCPSource.baseTable round theorem finite_alphabet : MachineFiniteAlphabet.FiniteAlphabet computableInPolyTime.tm := TableIterationFiniteAlphabet.gapMap PCPSource.baseTable round round_finite_alphabet end BinPackingCompleteness.PreprocessingMachine namespace BinPackingCompleteness.PCPSourceMachine open Turing BinPackingGames.Foundations Complexity Target def normalizedComputation : TM2ComputableInPolyTime formulaBits formulaBits PCPSource.normalizedFormula := by change TM2ComputableInPolyTime formulaBits formulaBits (fun formula => Normalization.normalize (PCPSource.rawFormula formula)) exact MachineSequential.composeBits PreprocessingMachine.computableInPolyTime NormalizationMachine.computableInPolyTime theorem normalized_finiteAlphabet : MachineFiniteAlphabet.FiniteAlphabet normalizedComputation.tm := MachineFiniteAlphabet.composeBits PreprocessingMachine.computableInPolyTime NormalizationMachine.computableInPolyTime PreprocessingMachine.finite_alphabet NormalizationMachine.computation_finiteAlphabet def rawSource (input : List Bool) : Formula := PCPSource.normalizedFormula (BinaryLanguage.totalRename input) def rawComputation : TM2ComputableInPolyTime (id : List Bool → List Bool) formulaBits rawSource := by change TM2ComputableInPolyTime (id : List Bool → List Bool) formulaBits (fun input => PCPSource.normalizedFormula (BinaryLanguage.totalRename input)) exact MachineSequential.composeBits BinaryInputReduction.computation normalizedComputation theorem raw_finiteAlphabet : MachineFiniteAlphabet.FiniteAlphabet rawComputation.tm := MachineFiniteAlphabet.composeBits BinaryInputReduction.computation normalizedComputation BinaryInputReduction.computation_finiteAlphabet normalized_finiteAlphabet theorem rawSource_nonempty (input : List Bool) : (rawSource input).clauses ≠ [] := PCPSource.normalizedFormula_nonempty _ theorem rawSource_satisfiable_iff (input : List Bool) : (rawSource input).Satisfiable ↔ BinaryLanguage.language input := (PCPSource.normalizedFormula_satisfiable_iff (BinaryLanguage.totalRename input)).trans (BinaryLanguage.totalRename_satisfiable_iff input) theorem rawSource_distinct (input : List Bool) (clause : Clause (rawSource input).variables) (mem : clause ∈ (rawSource input).clauses) (i j : Fin 3) (same : clause[i].variableIndex = clause[j].variableIndex) : i = j := PCPSource.normalizedFormula_distinct (BinaryLanguage.totalRename input) clause mem i j same theorem rawSource_clauseGap (input : List Bool) (unsat : ¬BinaryLanguage.language input) : SourceAmplification.ClauseGap (PCPSource.clauseFamily (BinaryLanguage.totalRename input)) PCPSource.sourceGap := PCPSource.clauseGap (BinaryLanguage.totalRename input) (fun sat => unsat ((BinaryLanguage.totalRename_satisfiable_iff input).mp sat)) theorem amplifiedSource_complete (input : List Bool) (sat : BinaryLanguage.language input) (σ : ℚ) (hσ : 0 < σ) : (PCPSource.amplifiedSource (BinaryLanguage.totalRename input) σ hσ).value = 1 := PCPSource.amplifiedSource_complete (BinaryLanguage.totalRename input) ((BinaryLanguage.totalRename_satisfiable_iff input).mpr sat) σ hσ theorem amplifiedSource_sound (input : List Bool) (unsat : ¬BinaryLanguage.language input) (σ : ℚ) (hσ : 0 < σ) : (PCPSource.amplifiedSource (BinaryLanguage.totalRename input) σ hσ).value < (σ : ℝ) := PCPSource.amplifiedSource_sound (BinaryLanguage.totalRename input) (fun sat => unsat ((BinaryLanguage.totalRename_satisfiable_iff input).mp sat)) σ hσ end BinPackingCompleteness.PCPSourceMachine namespace BinPackingCompleteness.PCPSourceHardness open Turing BinPackingGames.Foundations Complexity Target def formula {language : List Bool → Prop} (source : CookLevin.PolynomialThreeSATReduction language) (input : List Bool) : Formula := PCPSource.normalizedFormula (source.reduce input) def computation {language : List Bool → Prop} (source : CookLevin.PolynomialThreeSATReduction language) : TM2ComputableInPolyTime (id : List Bool → List Bool) formulaBits (formula source) := by change TM2ComputableInPolyTime (id : List Bool → List Bool) formulaBits (fun input => PCPSource.normalizedFormula (source.reduce input)) exact MachineSequential.composeBits source.computation PCPSourceMachine.normalizedComputation theorem computation_finiteAlphabet {language : List Bool → Prop} (source : CookLevin.PolynomialThreeSATReduction language) (finiteSource : MachineFiniteAlphabet.FiniteAlphabet source.computation.tm) : MachineFiniteAlphabet.FiniteAlphabet (computation source).tm := MachineFiniteAlphabet.composeBits source.computation PCPSourceMachine.normalizedComputation finiteSource PCPSourceMachine.normalized_finiteAlphabet theorem normalizedSource (language : List Bool → Prop) (membership : CookLevin.InNP language) : ∃ source : CookLevin.PolynomialThreeSATReduction language, ∃ runtime : TM2ComputableInPolyTime (id : List Bool → List Bool) formulaBits (formula source), MachineFiniteAlphabet.FiniteAlphabet runtime.tm ∧ ∀ input : List Bool, ((formula source input).Satisfiable ↔ language input) ∧ (formula source input).clauses ≠ [] ∧ (∀ (clause : Clause (formula source input).variables), clause ∈ (formula source input).clauses → ∀ i j : Fin 3, clause[i].variableIndex = clause[j].variableIndex → i = j) ∧ (¬language input → SourceAmplification.ClauseGap (PCPSource.clauseFamily (source.reduce input)) PCPSource.sourceGap) := by obtain ⟨source, finiteSource⟩ := CookLevin.Completeness.threeSATReduction language membership refine ⟨source, computation source, computation_finiteAlphabet source finiteSource, ?_⟩ intro input refine ⟨?_, PCPSource.normalizedFormula_nonempty (source.reduce input), ?_, ?_⟩ · exact (PCPSource.normalizedFormula_satisfiable_iff (source.reduce input)).trans (source.correct input).symm · intro clause mem i j same exact PCPSource.normalizedFormula_distinct (source.reduce input) clause mem i j same · intro rejected exact PCPSource.clauseGap (source.reduce input) (fun sat => rejected ((source.correct input).mpr sat)) end BinPackingCompleteness.PCPSourceHardness end end OAI