import OAI.Computability.UniqueGames.Machines.MachineLemmas import OAI.Computability.UniqueGames.PCP.RawInitialMachineLoop namespace OAI section /-! The actual finite machine computing the initial graph's full serialized table. The runtime bound counts every parse, row, cleanup and output reversal. -/ namespace UniqueGamesTheorem.Foundations.PCP.MachineRawInitialTable open Turing Target Complexity RawInitialMachineModel RawInitialMachineLoopData open RawInitialMachineBudget RawInitialMachineLoop 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] def prefixBits (F : Formula) : List Bool := encodeWords ([F.variables + F.clauses.length + 1, 6 * F.clauses.length + 1] ++ clauseOutput F.variables 0 F.clauses) theorem outputBits (F : Formula) : GraphTables.tableBits (RawInitialTables.table F) = prefixBits F ++ encodeWords (RawInitialRows.dummyWords F.variables F.clauses.length) := by change encodeWords (GraphTables.tableWords (RawInitialTables.table F)) = _ rw [tableWords_decomposition, encodeWords_append] rfl theorem machineTrace (F : Formula) : (MachineComposition.advance (TM2.step program))^[fullBudget F] (some (initList machine (formulaBits F))) = some (haltList machine (GraphTables.tableBits (RawInitialTables.table F))) := by have startup := RawInitialMachineStart.formulaStartTrace F rw [startTapes_eq] at startup have loop := loopTrace 0 F.clauses (encodeWords [F.variables + F.clauses.length + 1, 6 * F.clauses.length + 1]).reverse initialState have hprefix : (encodeWords (clauseOutput F.variables 0 F.clauses)).reverse ++ (encodeWords [F.variables + F.clauses.length + 1, 6 * F.clauses.length + 1]).reverse = (prefixBits F).reverse := by simp only [prefixBits, encodeWords_append, List.reverse_append] rw [hprefix, Nat.zero_add] at loop have finish := RawInitialMachineFinish.finishTrace F.variables F.clauses.length (prefixBits F).reverse (finalSigns initialState.1 F.clauses, none) rw [List.reverse_reverse, ← outputBits F] at finish have total := trace_trans _ (trace_trans _ startup loop) finish have time : (3 * F.variables + 5 * F.clauses.length + 13 + loopTime 0 F.clauses) + (3 * F.variables + 5 * F.clauses.length + 17 + (encodeWords (RawInitialRows.dummyWords F.variables F.clauses.length)).length + (prefixBits F).reverse.length) = fullBudget F := by rw [fullBudget, outputBits, List.length_append, List.length_reverse] omega simpa only [time] using total def outputsInTime (F : Formula) : TM2OutputsInTime machine (formulaBits F) (some (GraphTables.tableBits (RawInitialTables.table F))) (timePolynomial.eval (formulaBits F).length) where steps := fullBudget F evals_in_steps := machineTrace F steps_le_m := fullBudget_le F noncomputable def computableInPolyTime : TM2ComputableInPolyTime formulaEncoding.encode GraphTables.encoding.encode RawInitialTables.table where tm := machine inputAlphabet := Equiv.refl Bool outputAlphabet := Equiv.refl Bool time := timePolynomial outputsFun F := by change TM2OutputsInTime machine ((formulaBits F).map id) (some ((GraphTables.tableBits (RawInitialTables.table F)).map id)) (timePolynomial.eval (formulaBits F).length) have hi := @List.map_id (machine.Γ machine.k₀) (formulaBits F) have ho := @List.map_id (machine.Γ machine.k₁) (GraphTables.tableBits (RawInitialTables.table F)) rw [hi, ho] exact outputsInTime F end UniqueGamesTheorem.Foundations.PCP.MachineRawInitialTable end end OAI