import OAI.Computability.UniqueGames.PCP.CleanupLemmas import OAI.Computability.UniqueGames.PCP.CompareLoop import OAI.Computability.UniqueGames.PCP.EmitRows import OAI.Computability.UniqueGames.PCP.InputLemmas import OAI.Computability.UniqueGames.PCP.RuntimeModel import OAI.Computability.UniqueGames.PCP.Setup namespace OAI namespace UniqueGamesTheorem.Foundations.PCP.AlphabetTable.Driver open Turing open UniqueGamesTheorem.Foundations.Complexity open UniqueGamesTheorem.Foundations.Hastad open RuntimeModel def headerPlan (q : Nat) := EmitRows.headerCommands q (Ambient q) def rowPlan (q : Nat) := EmitRows.blockCommandsIn q (@context q) inductive Label (q : Nat) | setup (label : Setup.Label) | header (label : Emitter.Label (headerPlan q).length (Addresses.fieldBound q)) | guard | tailStart | tailLoop | reverseStart | reverseLoop | readPredicate | headCopyFirst | headCopySecond | indexCopyFirst | indexCopySecond | headVertices | headDarts | headLookup (label : Lookup.Label) | compare (label : CompareLoop.Label) | emit (label : Emitter.Label (rowPlan q).length (Addresses.fieldBound q)) | clearField (slot : Fin 3) | increment | reverseOutput | cleanup (slot : Fin 16) deriving DecidableEq, Fintype def headerEntry (q : Nat) : Label q := .header (Emitter.labelAt (headerPlan q).length (Addresses.fieldBound q) 0 .entry) def rowEntry (q : Nat) : Label q := .emit (Emitter.labelAt (rowPlan q).length (Addresses.fieldBound q) 0 .entry) def clearNext {q : Nat} (slot : Fin 3) : Label q := if h : slot.val < 2 then .clearField ⟨slot.val + 1, by omega⟩ else .increment def program (q : Nat) : Label q → TM2.Stmt Alphabet (Label q) (State q) | .setup label => Setup.statement setupPorts Label.setup (some (headerEntry q)) label | .header label => emitterStatement (Emitter.statement (Emitter.listCommands (headerPlan q)) headerSources .scratch .reversed Label.header (some .guard) label) | .guard => MachineUnaryCounter.guard .counter .tailStart .reverseOutput | .tailStart => SourceMachine.fieldStart .tail .tailLoop | .tailLoop => SourceMachine.fieldLoop .cursor .tail .tailLoop (some .reverseStart) | .reverseStart => SourceMachine.fieldStart .reverseIndex .reverseLoop | .reverseLoop => SourceMachine.fieldLoop .cursor .reverseIndex .reverseLoop (some .readPredicate) | .readPredicate => ReadRelation.parser .cursor .headCopyFirst | .headCopyFirst => Reduction.MachineTransfer.loopAt .original .scratch id false .headCopyFirst (some .headCopySecond) | .headCopySecond => MachineCopy.forkLoop .scratch .original .scan false .headCopySecond (some .indexCopyFirst) | .indexCopyFirst => Reduction.MachineTransfer.loopAt .reverseIndex .scratch id false .indexCopyFirst (some .indexCopySecond) | .indexCopySecond => MachineCopy.forkLoop .scratch .reverseIndex .indexScan false .indexCopySecond (some .headVertices) | .headVertices => MachineLookup.discard .scan .headVertices .headDarts | .headDarts => MachineLookup.discard .scan .headDarts (.headLookup .guard) | .headLookup label => Lookup.statement q .indexScan .scan .head Label.headLookup (some (.compare .leftFirst)) label | .compare label => CompareLoop.statement Label.compare (rowEntry q) label | .emit label => emitterStatement (Emitter.statement (Emitter.listCommands (rowPlan q)) rowSources .scratch .reversed Label.emit (some (.clearField 0)) label) | .clearField slot => MachineDrain.drain (fieldPorts slot) (.clearField slot) (some (clearNext slot)) | .increment => nextRow .guard | .reverseOutput => Reduction.MachineTransfer.loopAt .reversed .output id false .reverseOutput (some (.cleanup 0)) | .cleanup slot => Cleanup.instruction cleanupPorts Label.cleanup (initial q).1 none slot /-- Every state component is finite; the q-dependent relation register is enumerated by the explicit vector/function equivalence of the parser. -/ def machine (q : Nat) : FinTM2 := by letI := ReadRelation.stateFintype Flags q exact { K := Tape k₀ := .original k₁ := .output Γ := Alphabet Λ := Label q main := .setup .copyFirst σ := State q initialState := initial q m := program q } @[simp] theorem program_setup (q : Nat) (label : Setup.Label) : program q (.setup label) = Setup.statement setupPorts Label.setup (some (headerEntry q)) label := rfl @[simp] theorem program_guard (q : Nat) : program q .guard = MachineUnaryCounter.guard .counter .tailStart .reverseOutput := rfl @[simp] theorem program_headLookup (q : Nat) (label : Lookup.Label) : program q (.headLookup label) = Lookup.statement q .indexScan .scan .head Label.headLookup (some (.compare .leftFirst)) label := rfl @[simp] theorem program_compare (q : Nat) (label : CompareLoop.Label) : program q (.compare label) = CompareLoop.statement Label.compare (rowEntry q) label := rfl @[simp] theorem program_increment (q : Nat) : program q .increment = nextRow .guard := rfl end UniqueGamesTheorem.Foundations.PCP.AlphabetTable.Driver /-! The actual final driver phases: reverse the accumulated output, drain all fifteen nonoutput stacks, reset the finite state, and reach `haltList`. Polynomial resource bounds below require an actual preceding execution. -/ namespace UniqueGamesTheorem.Foundations.PCP.AlphabetTable.Finish open Turing open UniqueGamesTheorem.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 /-- The actual driver's reversal loop enters its first cleanup label. -/ 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 /-- All remaining driver stacks are actually popped, and the exact initial finite state is restored before the halting configuration is reached. -/ 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] /-- Every stack bound here is derived from the actual preceding driver trace. -/ 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 /-- A polynomial time allowance for the whole prefixRun followed by actual reversal and cleanup. The fixed push bound belongs to the concrete driver. -/ 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 UniqueGamesTheorem.Foundations.PCP.AlphabetTable.Finish end OAI