import Mathlib import OAI.Computability.MinUncut.Machines.MachineExpanderRowFrames import OAI.Computability.MinUncut.PCP.ExpanderTableEnumeration namespace OAI namespace MinUncutGames.Foundations.Complexity.MachineExpanderTable open Turing open PCP.ExpanderTables PCP.ExpanderRowControl PCP.ExpanderTableWords open PCP.ExpanderTableEnumeration private def singleStep {α : Type*} (step : α → Option α) (a b : α) (h : step a = some b) : StateTransition.EvalsToInTime step a (some b) 1 where steps := 1 evals_in_steps := by change step a = some b; exact h steps_le_m := Nat.le_refl _ private def sequence {α : Type*} {step : α → Option α} {a b c : α} {m n : Nat} (first : StateTransition.EvalsToInTime step a (some b) m) (second : StateTransition.EvalsToInTime step b (some c) n) : StateTransition.EvalsToInTime step a (some c) (m + n) := by simpa only [Nat.add_comm n m] using StateTransition.EvalsToInTime.trans step m n a b (some c) first second private def widen {α : Type*} {step : α → Option α} {a : α} {b : Option α} {m n : Nat} (run : StateTransition.EvalsToInTime step a b m) (bound : m ≤ n) : StateTransition.EvalsToInTime step a b n where toEvalsTo := run.toEvalsTo steps_le_m := run.steps_le_m.trans bound def tableRowData {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (position : Position d) : MachineExpanderRow.RowData v d := MachineExpanderRow.rowData G H vertex (positionPair position).1 (positionPair position).2 theorem tableRowData_finalValue {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (position : Position d) : (tableRowData G H vertex position).finalValue = rowValue G H vertex position := rfl theorem tableRowData_frame0 {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (position : Position d) (output : List Bool) : MachineExpanderRow.frame0 (tableRowData G H vertex position) output = rowTapes vertex.val (encodeWords (rotationWords G)) output := by funext tape cases tape <;> rfl theorem tableRowData_finalFrame {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (position : Position d) (output : List Bool) : MachineExpanderRow.emittedWord .output (MachineExpanderRow.frame0 (tableRowData G H vertex position) output) (tableRowData G H vertex position).finalValue = rowTapes vertex.val (encodeWords (rotationWords G)) ((encodeWord (rowValue G H vertex position)).reverse ++ output) := by rw [tableRowData_frame0, tableRowData_finalValue] funext tape cases tape <;> simp [MachineExpanderRow.emittedWord, rowTapes] variable {ρ : Type} [Fintype ρ] {v d : Nat} def rowAfterState (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (state : State ρ d) : State ρ d := (MachineExpanderRow.divisionState positive (caller state) (tableRowData G H vertex state.2).control2 none, state.2) omit [Fintype ρ] in @[simp] theorem rowAfterState_position (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (state : State ρ d) : (rowAfterState positive G H vertex state).2 = state.2 := rfl omit [Fintype ρ] in @[simp] theorem caller_rowAfterState (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (state : State ρ d) : caller (rowAfterState positive G H vertex state) = caller state := rfl def preparedRowInTime (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (state : State ρ d) (remaining : Nat) (output countSuffix result : List Bool) : StateTransition.EvalsToInTime (TM2.step (program positive H)) ⟨some (.inr .prepareRow), state, boundaryTapes vertex.val remaining (encodeWords (rotationWords G)) output countSuffix result⟩ (some ⟨some (.inr .afterRow), rowAfterState positive G H vertex state, boundaryTapes vertex.val remaining (encodeWords (rotationWords G)) ((encodeWord (rowValue G H vertex state.2)).reverse ++ output) countSuffix result⟩) (1 + 80 * ((encodeWords (rotationWords G)).length + 1)) := by have raw := rowExecution positive H state.2 (extraTapes remaining countSuffix result) (MachineExpanderRow.rowInTime positive (tableRowData G H vertex state.2) output (caller state)) have run : StateTransition.EvalsToInTime (TM2.step (program positive H)) ⟨some (.inl .initialize), prepareState positive H state, boundaryTapes vertex.val remaining (encodeWords (rotationWords G)) output countSuffix result⟩ (some ⟨some (.inr .afterRow), rowAfterState positive G H vertex state, boundaryTapes vertex.val remaining (encodeWords (rotationWords G)) ((encodeWord (rowValue G H vertex state.2)).reverse ++ output) countSuffix result⟩) (80 * ((encodeWords (rotationWords G)).length + 1)) := by rw [tableRowData_finalFrame, tableRowData_frame0] at raw simpa only [MachineEmbedding.configuration, MachineEmbedding.label, rowReturn, prepareState, boundaryState, rowAfterState, boundaryTapes, MachineExpanderRow.RowData.tableLength, MachineExpanderRow.RowData.control0, tableRowData, MachineExpanderRow.rowData] using raw exact sequence (singleStep _ _ _ (prepareRowStep positive H (boundaryTapes vertex.val remaining (encodeWords (rotationWords G)) output countSuffix result) state)) run structure VertexSuffixRun (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (state : State ρ d) (remaining : Nat) (output countSuffix result : List Bool) where finalState : State ρ d caller_preserved : caller finalState = caller state position_zero : finalState.2 = zeroPosition positive execution : StateTransition.EvalsToInTime (TM2.step (program positive H)) ⟨some (.inr .prepareRow), state, boundaryTapes vertex.val remaining (encodeWords (rotationWords G)) output countSuffix result⟩ (some ⟨some (.inr .vertexGuard), finalState, boundaryTapes (vertex.val + 1) remaining (encodeWords (rotationWords G)) (accumulate ((vertexWords G H vertex).drop state.2.val) output) countSuffix result⟩) ((rowFactor d - state.2.val) * (80 * ((encodeWords (rotationWords G)).length + 1) + 2)) private def suffixRun_aux (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (remaining : Nat) (countSuffix result : List Bool) (n : Nat) : (state : State ρ d) → state.2.val + n = rowFactor d → (output : List Bool) → VertexSuffixRun positive G H vertex state remaining output countSuffix result := by induction n with | zero => intro state count output have hp := state.2.isLt exact False.elim (by omega) | succ n ih => intro state count output let after := rowAfterState positive G H vertex state let nextOutput := (encodeWord (rowValue G H vertex state.2)).reverse ++ output have row := preparedRowInTime positive G H vertex state remaining output countSuffix result by_cases last : n = 0 · have hp : state.2.val + 1 = rowFactor d := by omega have finish := singleStep _ _ _ (afterRow_last_boundaryStep positive H vertex.val remaining (encodeWords (rotationWords G)) nextOutput countSuffix result after (by change ¬ state.2.val + 1 < rowFactor d omega)) refine ⟨resetPositionState positive after, ?_, rfl, ?_⟩ · rfl · have all := sequence row finish have words : accumulate ((vertexWords G H vertex).drop state.2.val) output = nextOutput := by rw [vertexWords_drop_last G H vertex state.2 hp] rfl have budget : rowFactor d - state.2.val = 1 := by omega rw [words, budget, Nat.one_mul] exact widen all (by omega) · have more : state.2.val + 1 < rowFactor d := by omega let next := advancePositionState positive after have nextPositionValue : next.2.val = state.2.val + 1 := nextPosition_val_of_lt positive state.2 more have nextCount : next.2.val + n = rowFactor d := by omega let tail := ih next nextCount nextOutput have advance := singleStep _ _ _ (afterRow_moreStep positive H (boundaryTapes vertex.val remaining (encodeWords (rotationWords G)) nextOutput countSuffix result) after more) have all := sequence (sequence row advance) tail.execution refine ⟨tail.finalState, ?_, tail.position_zero, ?_⟩ · exact tail.caller_preserved · have words : accumulate ((vertexWords G H vertex).drop state.2.val) output = accumulate ((vertexWords G H vertex).drop next.2.val) nextOutput := by rw [vertexWords_drop_succ G H vertex state.2, nextPositionValue] rfl rw [words] apply widen all have hcount : rowFactor d - state.2.val = n + 1 := by omega have hnext : rowFactor d - next.2.val = n := by omega rw [hcount, hnext, Nat.add_mul, Nat.one_mul] omega def suffixInTime (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (state : State ρ d) (remaining : Nat) (output countSuffix result : List Bool) : VertexSuffixRun positive G H vertex state remaining output countSuffix result := suffixRun_aux positive G H vertex remaining countSuffix result (rowFactor d - state.2.val) state (by have h := state.2.isLt; omega) output structure VertexRun (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (state : State ρ d) (remaining : Nat) (output countSuffix result : List Bool) where finalState : State ρ d caller_preserved : caller finalState = caller state position_zero : finalState.2 = zeroPosition positive execution : StateTransition.EvalsToInTime (TM2.step (program positive H)) ⟨some (.inr .prepareRow), state, boundaryTapes vertex.val remaining (encodeWords (rotationWords G)) output countSuffix result⟩ (some ⟨some (.inr .vertexGuard), finalState, boundaryTapes (vertex.val + 1) remaining (encodeWords (rotationWords G)) (accumulate (vertexWords G H vertex) output) countSuffix result⟩) (rowFactor d * (80 * ((encodeWords (rotationWords G)).length + 1) + 2)) def vertexInTime (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (vertex : Fin v) (state : State ρ d) (position : state.2 = zeroPosition positive) (remaining : Nat) (output countSuffix result : List Bool) : VertexRun positive G H vertex state remaining output countSuffix result := by let run := suffixInTime positive G H vertex state remaining output countSuffix result refine ⟨run.finalState, run.caller_preserved, run.position_zero, ?_⟩ have zero : state.2.val = 0 := congrArg Fin.val position simpa only [zero, List.drop_zero, Nat.sub_zero] using run.execution open Turing MachineComposition open PCP.ExpanderTables PCP.ExpanderRowControl PCP.ExpanderTableWords open PCP.ExpanderTableEnumeration def oldTableWord {v d : Nat} (G : Table v (degree d)) : List Bool := encodeWords (rotationWords G) def accumulatedVertices {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) (current : Nat) : List Bool := accumulate (priorVertices G H current) [] @[simp] theorem accumulatedVertices_zero {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) : accumulatedVertices G H 0 = [] := by simp [accumulatedVertices, accumulate] theorem accumulatedVertices_succ {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) (current : Nat) (valid : current < v) : accumulatedVertices G H (current + 1) = accumulate (vertexWords G H ⟨current, valid⟩) (accumulatedVertices G H current) := by simp only [accumulatedVertices, priorVertices_succ G H current valid, accumulate_append] theorem accumulatedVertices_full {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) : accumulatedVertices G H v = (encodeWords (rotationWords (step G H))).reverse := by simp [accumulatedVertices, accumulate_generatedWords] def vertexBodyBudget {v d : Nat} (G : Table v (degree d)) : Nat := rowFactor d * (80 * ((oldTableWord G).length + 1) + 2) def vertexLoopBudget {v d : Nat} (G : Table v (degree d)) (remaining : Nat) : Nat := remaining * (vertexBodyBudget G + 1) + 1 def vertexLoopTapes {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) (current remaining : Nat) (suffix : List Bool) : ∀ tape, List (Alphabet tape) := boundaryTapes current remaining (oldTableWord G) (accumulatedVertices G H current) suffix [] structure VertexLoopRun {v d : Nat} {ρ : Type} [Fintype ρ] (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (current remaining : Nat) (state : State ρ d) (suffix : List Bool) where finalState : State ρ d caller_preserved : caller finalState = caller state position_zero : finalState.2 = zeroPosition positive execution : StateTransition.EvalsToInTime (TM2.step (program positive H)) ⟨some (.inr .vertexGuard), state, vertexLoopTapes G H current remaining suffix⟩ (some ⟨some (.inr .reverseOutput), finalState, vertexLoopTapes G H v 0 suffix⟩) (vertexLoopBudget G remaining) private def boundarySingleStep {S : Type*} (f : S → Option S) (a b : S) (h : f a = some b) : StateTransition.EvalsToInTime f a (some b) 1 where steps := 1 evals_in_steps := by change f a = some b; exact h steps_le_m := Nat.le_refl _ def vertexLoopInTime {v d : Nat} {ρ : Type} [Fintype ρ] (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (current remaining : Nat) (state : State ρ d) (suffix : List Bool) (count : current + remaining = v) (position : state.2 = zeroPosition positive) : VertexLoopRun positive G H current remaining state suffix := by induction remaining generalizing current state with | zero => have hc : current = v := by omega subst current refine ⟨clearRegister state, caller_clearRegister state, ?_, ?_⟩ · simpa only [clearRegister] using position · simpa only [vertexLoopBudget, Nat.zero_mul, Nat.zero_add, vertexLoopTapes] using boundarySingleStep _ _ _ (vertexGuard_boundary_zeroStep positive H v (oldTableWord G) (accumulatedVertices G H v) suffix [] state) | succ remaining ih => have valid : current < v := by omega let vertex : Fin v := ⟨current, valid⟩ let body := vertexInTime positive G H vertex (clearRegister state) (by simpa only [clearRegister] using position) remaining (accumulatedVertices G H current) suffix [] have nextCount : current + 1 + remaining = v := by omega let rest := ih (current + 1) body.finalState nextCount body.position_zero have output_eq : accumulate (vertexWords G H vertex) (accumulatedVertices G H current) = accumulatedVertices G H (current + 1) := (accumulatedVertices_succ G H current valid).symm let guard := boundarySingleStep _ _ _ (vertexGuard_boundary_succStep positive H current remaining (oldTableWord G) (accumulatedVertices G H current) suffix [] state) have bodyRun : StateTransition.EvalsToInTime (TM2.step (program positive H)) ⟨some (.inr .prepareRow), clearRegister state, boundaryTapes current remaining (oldTableWord G) (accumulatedVertices G H current) suffix []⟩ (some ⟨some (.inr .vertexGuard), body.finalState, vertexLoopTapes G H (current + 1) remaining suffix⟩) (vertexBodyBudget G) := by simpa only [vertexLoopTapes, vertexBodyBudget, oldTableWord, output_eq] using body.execution let first := StateTransition.EvalsToInTime.trans (TM2.step (program positive H)) _ _ _ _ _ guard bodyRun let all := StateTransition.EvalsToInTime.trans (TM2.step (program positive H)) _ _ _ _ _ first rest.execution refine ⟨rest.finalState, ?_, rest.position_zero, ?_⟩ · exact rest.caller_preserved.trans (body.caller_preserved.trans (caller_clearRegister state)) · refine { toEvalsTo := all.toEvalsTo, steps_le_m := ?_ } have bound := all.steps_le_m simp only [vertexLoopBudget, Nat.succ_mul] at bound ⊢ omega open Turing open MachineComposition open PCP.ExpanderTables PCP.ExpanderRowControl variable {ρ : Type} {d : Nat} @[simp] theorem clearRegister_position (state : State ρ d) : (clearRegister state).2 = state.2 := rfl @[simp] theorem clearRegister_register (state : State ρ d) : (clearRegister state).1.2 = none := rfl @[simp] theorem clearRegister_idempotent (state : State ρ d) : clearRegister (clearRegister state) = clearRegister state := rfl theorem boundaryTapes_reverse_update (vertex remaining : Nat) (oldTable output countSuffix result nextOutput nextResult : List Bool) : Function.update (Function.update (boundaryTapes vertex remaining oldTable output countSuffix result) (.inl .output) nextOutput) (.inr .result) nextResult = boundaryTapes vertex remaining oldTable nextOutput countSuffix nextResult := by funext tape rcases tape with row | extra · cases row <;> simp [boundaryTapes, rowTapes, MachineEmbedding.tapes, Function.update] · cases extra <;> simp [boundaryTapes, extraTapes, MachineEmbedding.tapes, Function.update] variable [Fintype ρ] theorem reverseOutputTrace (positive : 0 < d) (H : Table (cloudSize d) d) (vertex remaining : Nat) (oldTable output countSuffix result : List Bool) (state : State ρ d) : (advance (TM2.step (program positive H)))^[output.length + 1] (some ⟨some (.inr .reverseOutput), state, boundaryTapes vertex remaining oldTable output countSuffix result⟩) = some ⟨some (.inr .done), clearRegister state, boundaryTapes vertex remaining oldTable [] countSuffix (output.reverse ++ result)⟩ := by induction output generalizing result state with | nil => simpa only [List.length_nil, Nat.zero_add, Function.iterate_one, advance_some, List.reverse_nil, List.nil_append] using reverseOutput_nilStep positive H (boundaryTapes vertex remaining oldTable [] countSuffix result) state rfl | cons symbol output ih => rw [List.length_cons, Function.iterate_succ_apply] simp only [advance_some] rw [reverseOutput_consStep positive H (boundaryTapes vertex remaining oldTable (symbol :: output) countSuffix result) state symbol output rfl] change (advance (TM2.step (program positive H)))^[output.length + 1] (some ⟨some (.inr .reverseOutput), ((state.1.1, some symbol), state.2), Function.update (Function.update (boundaryTapes vertex remaining oldTable (symbol :: output) countSuffix result) (.inl .output) output) (.inr .result) (symbol :: result)⟩) = _ rw [boundaryTapes_reverse_update] simpa only [clearRegister, List.reverse_cons, List.append_assoc, List.singleton_append] using ih (symbol :: result) ((state.1.1, some symbol), state.2) theorem reverseOutputHaltTrace (positive : 0 < d) (H : Table (cloudSize d) d) (vertex remaining : Nat) (oldTable output countSuffix result : List Bool) (state : State ρ d) : (advance (TM2.step (program positive H)))^[output.length + 2] (some ⟨some (.inr .reverseOutput), state, boundaryTapes vertex remaining oldTable output countSuffix result⟩) = some ⟨none, clearRegister state, boundaryTapes vertex remaining oldTable [] countSuffix (output.reverse ++ result)⟩ := by rw [show output.length + 2 = (output.length + 1) + 1 by omega, Function.iterate_succ_apply', reverseOutputTrace positive H vertex remaining oldTable output countSuffix result state] exact doneStep positive H (boundaryTapes vertex remaining oldTable [] countSuffix (output.reverse ++ result)) (clearRegister state) def reverseOutputInTime (positive : 0 < d) (H : Table (cloudSize d) d) (vertex remaining : Nat) (oldTable output countSuffix result : List Bool) (state : State ρ d) : StateTransition.EvalsToInTime (TM2.step (program positive H)) ⟨some (.inr .reverseOutput), state, boundaryTapes vertex remaining oldTable output countSuffix result⟩ (some ⟨some (.inr .done), clearRegister state, boundaryTapes vertex remaining oldTable [] countSuffix (output.reverse ++ result)⟩) (output.length + 1) where steps := output.length + 1 evals_in_steps := reverseOutputTrace positive H vertex remaining oldTable output countSuffix result state steps_le_m := Nat.le_refl _ def reverseOutputHaltInTime (positive : 0 < d) (H : Table (cloudSize d) d) (vertex remaining : Nat) (oldTable output countSuffix result : List Bool) (state : State ρ d) : StateTransition.EvalsToInTime (TM2.step (program positive H)) ⟨some (.inr .reverseOutput), state, boundaryTapes vertex remaining oldTable output countSuffix result⟩ (some ⟨none, clearRegister state, boundaryTapes vertex remaining oldTable [] countSuffix (output.reverse ++ result)⟩) (output.length + 2) where steps := output.length + 2 evals_in_steps := reverseOutputHaltTrace positive H vertex remaining oldTable output countSuffix result state steps_le_m := Nat.le_refl _ open Turing MachineComposition open PCP.ExpanderTables PCP.ExpanderRowControl PCP.ExpanderTableWords open PCP.ExpanderTableEnumeration def completeBudget {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) : Nat := 1 + vertexLoopBudget G v + ((encodeWords (rotationWords (step G H))).length + 2) def timeCoefficient (d : Nat) : Nat := rowFactor d * (rowFactor d + 1) + 82 * rowFactor d + 5 noncomputable def timePolynomial (d : Nat) : Polynomial Nat := Polynomial.C (timeCoefficient d) * (Polynomial.X + 1) ^ 2 theorem vertices_le_word_length {v d : Nat} (positive : 0 < d) (G : Table v (degree d)) : v ≤ (oldTableWord G).length := by have hq : 1 ≤ degree d := Nat.mul_pos positive positive have hv : v ≤ v * degree d := by simpa using Nat.mul_le_mul_left v hq have hr : v * degree d ≤ (oldTableWord G).length := by simp only [oldTableWord, encodeWords_length, rotationWords_length] omega exact hv.trans hr theorem completeBudget_le {v d : Nat} (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) : completeBudget G H ≤ (timePolynomial d).eval (oldTableWord G).length := by let L := (oldTableWord G).length let M := rowFactor d have vBound : v ≤ L + 1 := (vertices_le_word_length positive G).trans (Nat.le_succ L) have emitBound : 80 * (L + 1) + 2 ≤ 82 * (L + 1) := by omega have bodyBound : vertexBodyBudget G + 1 ≤ (82 * M + 1) * (L + 1) := by have mulBound := Nat.mul_le_mul_left M emitBound change M * (80 * (L + 1) + 2) + 1 ≤ _ nlinarith have iterations := Nat.mul_le_mul vBound bodyBound have loopBound : v * (vertexBodyBudget G + 1) ≤ (82 * M + 1) * (L + 1)^2 := by calc _ ≤ (L + 1) * ((82 * M + 1) * (L + 1)) := iterations _ = _ := by ring have rowBound : v * M ≤ M * (L + 1) := by simpa only [Nat.mul_comm v M] using Nat.mul_le_mul_left M vBound have rowSuccBound : v * M + 1 ≤ (M + 1) * (L + 1) := by nlinarith have productBound := Nat.mul_le_mul rowBound rowSuccBound have lengthBound : (encodeWords (rotationWords (step G H))).length ≤ M * (M + 1) * (L + 1)^2 := by calc _ ≤ (v * M) * (v * M + 1) := by simpa only [generatedWords_eq_rotationWords] using encode_generatedWords_length_le G H _ ≤ (M * (L + 1)) * ((M + 1) * (L + 1)) := productBound _ = _ := by ring have oneBound : 1 ≤ (L + 1)^2 := by nlinarith have combined := Nat.add_le_add loopBound lengthBound simp only [timePolynomial, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_pow, Polynomial.eval_add, Polynomial.eval_X, Polynomial.eval_one] change completeBudget G H ≤ (M * (M + 1) + 82 * M + 5) * (L + 1)^2 unfold completeBudget vertexLoopBudget nlinarith structure TableRun {v d : Nat} {ρ : Type} [Fintype ρ] (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (state : State ρ d) (suffix : List Bool) where finalState : State ρ d caller_preserved : caller finalState = caller state position_zero : finalState.2 = zeroPosition positive execution : StateTransition.EvalsToInTime (TM2.step (program positive H)) ⟨some (.inr .initialize), state, initialTapes v (oldTableWord G) suffix⟩ (some ⟨none, finalState, finalTapes v (oldTableWord G) (encodeWords (rotationWords (step G H))) suffix⟩) ((timePolynomial d).eval (oldTableWord G).length) private def initializeInTime {v d : Nat} {ρ : Type} [Fintype ρ] (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (state : State ρ d) (suffix : List Bool) : StateTransition.EvalsToInTime (TM2.step (program positive H)) ⟨some (.inr .initialize), state, initialTapes v (oldTableWord G) suffix⟩ (some ⟨some (.inr .vertexGuard), initialState positive H (caller state), vertexLoopTapes G H 0 v suffix⟩) 1 where steps := 1 evals_in_steps := by change TM2.step (program (ρ := ρ) positive H) ⟨some (.inr .initialize), state, initialTapes v (oldTableWord G) suffix⟩ = some ⟨some (.inr .vertexGuard), initialState positive H (caller state), vertexLoopTapes G H 0 v suffix⟩ simpa only [vertexLoopTapes, accumulatedVertices_zero] using initialize_boundaryStep positive H v (oldTableWord G) suffix state steps_le_m := Nat.le_refl _ def tableInTime {v d : Nat} {ρ : Type} [Fintype ρ] (positive : 0 < d) (G : Table v (degree d)) (H : Table (cloudSize d) d) (state : State ρ d) (suffix : List Bool) : TableRun positive G H state suffix := by let loop := vertexLoopInTime positive G H 0 v (initialState positive H (caller state)) suffix (by omega) rfl let initialRun := initializeInTime positive G H state suffix have finish : StateTransition.EvalsToInTime (TM2.step (program positive H)) ⟨some (.inr .reverseOutput), loop.finalState, vertexLoopTapes G H v 0 suffix⟩ (some ⟨none, clearRegister loop.finalState, finalTapes v (oldTableWord G) (encodeWords (rotationWords (step G H))) suffix⟩) ((encodeWords (rotationWords (step G H))).length + 2) := by simpa only [vertexLoopTapes, finalTapes, accumulatedVertices_full, List.reverse_reverse, List.append_nil, List.length_reverse] using reverseOutputHaltInTime positive H v 0 (oldTableWord G) (accumulatedVertices G H v) suffix [] loop.finalState let first := StateTransition.EvalsToInTime.trans (TM2.step (program positive H)) _ _ _ _ _ initialRun loop.execution let all := StateTransition.EvalsToInTime.trans (TM2.step (program positive H)) _ _ _ _ _ first finish refine ⟨clearRegister loop.finalState, ?_, ?_, ?_⟩ · exact (caller_clearRegister loop.finalState).trans loop.caller_preserved · simpa only [clearRegister] using loop.position_zero · refine { toEvalsTo := all.toEvalsTo, steps_le_m := ?_ } have bound := all.steps_le_m have polynomialBound := completeBudget_le positive G H unfold completeBudget at polynomialBound omega end MinUncutGames.Foundations.Complexity.MachineExpanderTable namespace MinUncutGames.Foundations.Complexity.MachineExpanderFamily open PCP.ExpanderTables PCP.ExpanderRowControl PCP.ExpanderTableWords def cycleWords (remaining : Nat) (word current vertex count result suffix : List Bool) : Tape → List Bool | .inl (.inl .table) => word | .inl (.inl .inputVertex) => vertex | .inl (.inr .vertexCount) => count | .inl (.inr .result) => result | .inr .remainingLevel => encodeWord remaining ++ suffix | .inr .currentSize => current | _ => [] def cycleTapes (remaining : Nat) (word current vertex count result suffix : List Bool) : (tape : Tape) → List (Alphabet tape) := fromBoolTapes (cycleWords remaining word current vertex count result suffix) @[simp] theorem toBool_cycleTapes (remaining : Nat) (word current vertex count result suffix : List Bool) : toBoolTapes (cycleTapes remaining word current vertex count result suffix) = cycleWords remaining word current vertex count result suffix := toBool_fromBool _ theorem boundaryTapes_eq_cycleTapes (remaining current : Nat) (word suffix : List Bool) : boundaryTapes remaining current word suffix = cycleTapes remaining word (encodeWord current) [] [] [] suffix := by funext tape rcases tape with tape | tape · rcases tape with row | extra · cases row <;> rfl · cases extra <;> rfl · cases tape <;> rfl def copyFrame (remaining current : Nat) (word suffix : List Bool) : (tape : Tape) → List (Alphabet tape) := MachineEmbedding.tapes (MachineExpanderTable.initialTapes current word []) (extraFrame remaining current suffix) def returnFrame (remaining current : Nat) (oldWord newWord suffix : List Bool) : (tape : Tape) → List (Alphabet tape) := MachineEmbedding.tapes (MachineExpanderTable.finalTapes current oldWord newWord []) (extraFrame remaining current suffix) def installedFrame (remaining current : Nat) (newWord suffix : List Bool) : (tape : Tape) → List (Alphabet tape) := cycleTapes remaining newWord [] (encodeWord current) (encodeWord 0) [] suffix def multipliedFrame (d remaining current : Nat) (newWord suffix : List Bool) : (tape : Tape) → List (Alphabet tape) := cycleTapes remaining newWord (encodeWord (cloudSize d * current)) (encodeWord current) (encodeWord 0) [] suffix theorem copyFrame_eq_cycleTapes (remaining current : Nat) (word suffix : List Bool) : copyFrame remaining current word suffix = cycleTapes remaining word (encodeWord current) [] (encodeWord current) [] suffix := by funext tape rcases tape with tape | tape · rcases tape with row | extra · cases row <;> rfl · cases extra <;> simp [copyFrame, MachineEmbedding.tapes, MachineExpanderTable.initialTapes, MachineExpanderTable.extraTapes, cycleTapes, fromBoolTapes, boolWord, cycleWords] · cases tape <;> rfl theorem returnFrame_eq_cycleTapes (remaining current : Nat) (oldWord newWord suffix : List Bool) : returnFrame remaining current oldWord newWord suffix = cycleTapes remaining oldWord (encodeWord current) (encodeWord current) (encodeWord 0) newWord suffix := by funext tape rcases tape with tape | tape · rcases tape with row | extra · cases row <;> rfl · cases extra <;> simp [returnFrame, MachineEmbedding.tapes, MachineExpanderTable.finalTapes, MachineExpanderTable.boundaryTapes, MachineExpanderTable.extraTapes, cycleTapes, fromBoolTapes, boolWord, cycleWords] · cases tape <;> rfl theorem copyFrame_eq_update (remaining current : Nat) (word suffix : List Bool) : Function.update (boundaryTapes remaining current word suffix) vertexCountTape (encodeWord current) = copyFrame remaining current word suffix := by rw [boundaryTapes_eq_cycleTapes, copyFrame_eq_cycleTapes] funext tape rcases tape with tape | tape · rcases tape with row | extra · cases row <;> rfl · cases extra <;> rfl · cases tape <;> rfl theorem multipliedFrame_eq_update (d remaining current : Nat) (word suffix : List Bool) : Function.update (installedFrame remaining current word suffix) (.inr .currentSize) (encodeWord (cloudSize d * current)) = multipliedFrame d remaining current word suffix := by funext tape rcases tape with tape | tape · rcases tape with row | extra · cases row <;> rfl · cases extra <;> rfl · cases tape <;> rfl end MinUncutGames.Foundations.Complexity.MachineExpanderFamily namespace MinUncutGames.Foundations.Complexity.MachineExpanderFamilyBounds open PCP.ExpanderTables PCP.ExpanderRowControl PCP.ExpanderTableWords noncomputable def resizeCost {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) : Nat := (MachineExpanderTable.timePolynomial d).eval (MachineExpanderTable.oldTableWord G).length + 6 * v + (MachineExpanderTable.oldTableWord G).length + 2 * (encodeWords (rotationWords (step G H))).length + 15 def resizeCoefficient (d : Nat) : Nat := MachineExpanderTable.timeCoefficient d * (degree d + 1) ^ 4 + 3 * (degree d + 1) ^ 2 + 21 theorem wordLength_add_one_le {v q N : Nat} (G : Table v q) (hv : v ≤ N) : (encodeWords (rotationWords G)).length + 1 ≤ (q + 1)^2 * (N + 1)^2 := by have hlength := encode_rotationWords_length_le G have hrow : v * q + 1 ≤ (q + 1) * (N + 1) := by have h := Nat.mul_le_mul_right q hv nlinarith calc _ ≤ (v * q + 1)^2 := by nlinarith _ ≤ ((q + 1) * (N + 1))^2 := Nat.pow_le_pow_left hrow 2 _ = _ := by ring theorem resizeCost_le {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d) (N : Nat) (hv : v ≤ N) (hnext : v * cloudSize d ≤ N) : resizeCost G H ≤ resizeCoefficient d * (N + 1)^4 := by let oldL := (MachineExpanderTable.oldTableWord G).length let newL := (encodeWords (rotationWords (step G H))).length have oldBound : oldL + 1 ≤ (degree d + 1)^2 * (N + 1)^2 := wordLength_add_one_le G hv have newBound : newL + 1 ≤ (degree d + 1)^2 * (N + 1)^2 := wordLength_add_one_le (step G H) hnext have timeBound : (MachineExpanderTable.timePolynomial d).eval oldL ≤ MachineExpanderTable.timeCoefficient d * (degree d + 1)^4 * (N + 1)^4 := by simp only [MachineExpanderTable.timePolynomial, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_pow, Polynomial.eval_add, Polynomial.eval_X, Polynomial.eval_one] calc _ ≤ MachineExpanderTable.timeCoefficient d * ((degree d + 1)^2 * (N + 1)^2)^2 := Nat.mul_le_mul_left _ (Nat.pow_le_pow_left oldBound 2) _ = _ := by ring have pow24 : (N + 1)^2 ≤ (N + 1)^4 := Nat.pow_le_pow_right (Nat.succ_pos N) (by decide) have overheadBound : oldL + 2 * newL ≤ 3 * (degree d + 1)^2 * (N + 1)^4 := by have h := Nat.mul_le_mul_left ((degree d + 1)^2) pow24 calc _ ≤ 3 * ((degree d + 1)^2 * (N + 1)^2) := by omega _ ≤ 3 * ((degree d + 1)^2 * (N + 1)^4) := Nat.mul_le_mul_left 3 h _ = _ := by ring have npow : N + 1 ≤ (N + 1)^4 := by simpa only [pow_one] using Nat.pow_le_pow_right (Nat.succ_pos N) (show 1 ≤ 4 by decide) have linearBound : 6 * v + 15 ≤ 21 * (N + 1)^4 := by omega change (MachineExpanderTable.timePolynomial d).eval oldL + 6 * v + oldL + 2 * newL + 15 ≤ _ unfold resizeCoefficient nlinarith only [timeBound, overheadBound, linearBound] theorem vertexCount_monotone {q : Nat} (positive : 0 < q) : Monotone (vertexCount q) := by intro m n hmn rw [vertexCount_eq, vertexCount_eq] exact Nat.pow_le_pow_right (Nat.mul_pos positive positive) hmn theorem level_le_vertexCount {q : Nat} (growth : 1 < q * q) (level : Nat) : level ≤ vertexCount q level := by have positive : 0 < q := by nlinarith induction level with | zero => exact Nat.zero_le _ | succ level ih => have hpos := vertexCount_positive positive level have hmul := Nat.mul_le_mul_left (vertexCount q level) (show 2 ≤ q * q by omega) change level + 1 ≤ vertexCount q level * (q * q) nlinarith noncomputable def resizeSum {d : Nat} (H : Table (cloudSize d) d) (level : Nat) : Nat := ∑ i ∈ Finset.range level, resizeCost (family H i) H @[simp] theorem resizeSum_zero {d : Nat} (H : Table (cloudSize d) d) : resizeSum H 0 = 0 := by simp [resizeSum] theorem resizeSum_succ {d : Nat} (H : Table (cloudSize d) d) (level : Nat) : resizeSum H (level + 1) = resizeSum H level + resizeCost (family H level) H := by simp only [resizeSum, Finset.sum_range_succ] theorem resizeSum_add {d : Nat} (H : Table (cloudSize d) d) (start count : Nat) : resizeSum H (start + count) = resizeSum H start + ∑ i ∈ Finset.range count, resizeCost (family H (start + i)) H := Finset.sum_range_add (fun i => resizeCost (family H i) H) start count noncomputable def familyBudget {d : Nat} (H : Table (cloudSize d) d) (level : Nat) : Nat := resizeSum H level + level + 3 @[simp] theorem familyBudget_zero {d : Nat} (H : Table (cloudSize d) d) : familyBudget H 0 = 3 := by simp [familyBudget] theorem familyBudget_succ {d : Nat} (H : Table (cloudSize d) d) (level : Nat) : familyBudget H (level + 1) = familyBudget H level + resizeCost (family H level) H + 1 := by simp only [familyBudget, resizeSum_succ] omega theorem resizeSum_le {d : Nat} (H : Table (cloudSize d) d) (growth : 1 < cloudSize d) (level : Nat) : resizeSum H level ≤ resizeCoefficient d * (vertexCount (degree d) level + 1)^5 := by let N := vertexCount (degree d) level have positive : 0 < degree d := by have h : 1 < degree d * degree d := growth nlinarith have hmono := vertexCount_monotone positive have hlevel : level ≤ N := level_le_vertexCount growth level have eachBound : ∀ i ∈ Finset.range level, resizeCost (family H i) H ≤ resizeCoefficient d * (N + 1)^4 := by intro i hi have hi' : i < level := Finset.mem_range.mp hi apply resizeCost_le (family H i) H N · exact hmono (by omega) · exact hmono (show i + 1 ≤ level by omega) have sumBound : resizeSum H level ≤ level * (resizeCoefficient d * (N + 1)^4) := by calc _ ≤ ∑ _i ∈ Finset.range level, resizeCoefficient d * (N + 1)^4 := Finset.sum_le_sum eachBound _ = _ := by simp calc _ ≤ level * (resizeCoefficient d * (N + 1)^4) := sumBound _ ≤ (N + 1) * (resizeCoefficient d * (N + 1)^4) := Nat.mul_le_mul_right _ (hlevel.trans (Nat.le_succ N)) _ = _ := by ring theorem familyBudget_le {d : Nat} (H : Table (cloudSize d) d) (growth : 1 < cloudSize d) (level : Nat) : familyBudget H level ≤ (resizeCoefficient d + 4) * (vertexCount (degree d) level + 1)^5 := by have hsum := resizeSum_le H growth level have hlevel := level_le_vertexCount growth level have hpow : vertexCount (degree d) level + 1 ≤ (vertexCount (degree d) level + 1)^5 := by simpa only [pow_one] using Nat.pow_le_pow_right (Nat.succ_pos (vertexCount (degree d) level)) (show 1 ≤ 5 by decide) have overhead : level + 3 ≤ 4 * (vertexCount (degree d) level + 1)^5 := by omega unfold familyBudget nlinarith only [hsum, overhead] theorem paddedSize_le_succ_input (k : Nat) : PCP.PreprocessingLevels.paddedSize k ≤ PCP.ExpanderFamily.growth * (k + 1) := by by_cases hk : k = 0 · subst k have h := PCP.ExpanderFamily.growth_gt_one simp only [PCP.PreprocessingLevels.paddedSize_zero, Nat.zero_add, Nat.mul_one] omega · exact (PCP.PreprocessingLevels.paddedSize_bounds (Nat.pos_of_ne_zero hk)).2.trans (Nat.mul_le_mul_left _ (Nat.le_succ k)) def inputCoefficient : Nat := (resizeCoefficient PCP.Expanders.baseDegree + 4) * (PCP.ExpanderFamily.growth + 1)^5 theorem familyBudget_at_boundedLevel_le (H : Table (cloudSize PCP.Expanders.baseDegree) PCP.Expanders.baseDegree) (k : Nat) : familyBudget H (PCP.PreprocessingLevels.boundedLevel k) ≤ inputCoefficient * (k + 1)^5 := by have hg : cloudSize PCP.Expanders.baseDegree = PCP.ExpanderFamily.growth := by unfold cloudSize degree PCP.ExpanderFamily.growth ring have growth : 1 < cloudSize PCP.Expanders.baseDegree := by rw [hg] exact PCP.ExpanderFamily.growth_gt_one have bound := familyBudget_le H growth (PCP.PreprocessingLevels.boundedLevel k) rw [PCP.PreprocessingLevels.table_vertexCount_eq_paddedSize] at bound have sizeBound : PCP.PreprocessingLevels.paddedSize k + 1 ≤ (PCP.ExpanderFamily.growth + 1) * (k + 1) := by have h := paddedSize_le_succ_input k nlinarith calc _ ≤ (resizeCoefficient PCP.Expanders.baseDegree + 4) * (PCP.PreprocessingLevels.paddedSize k + 1)^5 := bound _ ≤ (resizeCoefficient PCP.Expanders.baseDegree + 4) * ((PCP.ExpanderFamily.growth + 1) * (k + 1))^5 := Nat.mul_le_mul_left _ (Nat.pow_le_pow_left sizeBound 5) _ = _ := by unfold inputCoefficient; ring end MinUncutGames.Foundations.Complexity.MachineExpanderFamilyBounds end OAI