import OAI.Computability.BinPacking.Computation.PoweringMachineRowBody import OAI.Computability.BinPacking.PCP.PoweringTableLayout namespace OAI namespace BinPackingGames.Foundations.Complexity.PoweringMachineVertex open Turing MachineComposition PCP variable {K Λ : Type} [DecidableEq K] {vertices d n : Nat} abbrev Command (d n : Nat) := Fin (PoweringTableLayout.blockSize d n) def ports {d n : Nat} (j : Command d n) : Fin (n + 1) → Fin d := ((PoweringEnumeration.dartBlockEquiv d n).symm j).1 def direction {d n : Nat} (j : Command d n) : Bool := ((PoweringEnumeration.dartBlockEquiv d n).symm j).2 def commands (d n : Nat) : List (Command d n) := (List.ofFn id).reverse @[simp] theorem commands_length (d n : Nat) : (commands d n).length = PoweringTableLayout.blockSize d n := by simp only [commands, List.length_reverse, List.length_ofFn] abbrev LocalLabel (j : Command d n) := PoweringMachineRowBody.Label n (ports j) (direction j) abbrev SequenceLabel (ops : List (Command d n)) := MachineFiniteSequence.Label LocalLabel ops abbrev Label (d n : Nat) := SequenceLabel (commands d n) abbrev State (d n : Nat) := PoweringMachineRowBody.State d n def sequenceEntry (ops : List (Command d n)) (labels : SequenceLabel ops → Λ) (exit : Option Λ) : Option Λ := MachineFiniteSequence.entry LocalLabel (fun j => PoweringMachineRowBody.entry n (ports j) (direction j)) ops labels exit def entry (d n : Nat) (labels : Label d n → Λ) (exit : Option Λ) : Option Λ := sequenceEntry (commands d n) labels exit def sequenceInstruction (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (output : K) (ops : List (Command d n)) (labels : SequenceLabel ops → Λ) (exit : Option Λ) : SequenceLabel ops → TM2.Stmt (fun _ : K => Bool) Λ (State d n) := MachineFiniteSequence.instruction LocalLabel (fun j => PoweringMachineRowBody.entry n (ports j) (direction j)) (fun j => PoweringMachineRowBody.instruction n placement output (ports j) (direction j)) ops labels exit def instruction (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (output : K) (labels : Label d n → Λ) (exit : Option Λ) : Label d n → TM2.Stmt (fun _ : K => Bool) Λ (State d n) := sequenceInstruction n placement output (commands d n) labels exit def result (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (output : K) (vertex : Fin vertices) (j : Command d n) (base : K → List Bool) : K → List Bool := PoweringMachineRowBody.finalTapes graph n placement output vertex (ports j) (direction j) base def sequenceTapes (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (output : K) (vertex : Fin vertices) (ops : List (Command d n)) (base : K → List Bool) : K → List Bool := MachineFiniteSequence.resultOf (result graph n placement output vertex) ops base def finalTapes (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (output : K) (vertex : Fin vertices) (base : K → List Bool) : K → List Bool := sequenceTapes graph n placement output vertex (commands d n) base def sequenceSteps (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (output : K) (vertex : Fin vertices) (ops : List (Command d n)) (base : K → List Bool) : Nat := MachineFiniteSequence.steps (result graph n placement output vertex) (fun j _ => PoweringMachineRowBody.steps graph vertex n (ports j) (direction j)) ops base def costSum (graph : PortTables.Table vertices d) (vertex : Fin vertices) (n : Nat) : List (Command d n) → Nat | [] => 0 | j :: ops => PoweringMachineRowBody.steps graph vertex n (ports j) (direction j) + costSum graph vertex n ops theorem sequenceSteps_eq (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (output : K) (vertex : Fin vertices) (ops : List (Command d n)) (base : K → List Bool) : sequenceSteps graph n placement output vertex ops base = costSum graph vertex n ops := by induction ops generalizing base with | nil => rfl | cons j ops ih => change PoweringMachineRowBody.steps graph vertex n (ports j) (direction j) + sequenceSteps graph n placement output vertex ops (result graph n placement output vertex j base) = _ rw [ih] rfl def steps (graph : PortTables.Table vertices d) (vertex : Fin vertices) (n : Nat) : Nat := costSum graph vertex n (commands d n) def budget (d n inputLength : Nat) : Nat := PoweringTableLayout.blockSize d n * PoweringMachineRowBody.budget d n inputLength theorem costSum_le (graph : PortTables.Table vertices d) (vertex : Fin vertices) (n : Nat) (ops : List (Command d n)) : costSum graph vertex n ops ≤ ops.length * PoweringMachineRowBody.budget d n (PortTables.tableBits graph).length := by induction ops with | nil => simp only [costSum, List.length_nil, Nat.zero_mul, Nat.le_refl] | cons j ops ih => have hfirst := PoweringMachineRowBody.steps_le graph vertex n (ports j) (direction j) change PoweringMachineRowBody.steps graph vertex n (ports j) (direction j) + costSum graph vertex n ops ≤ _ simpa only [List.length_cons, Nat.add_mul, Nat.one_mul, Nat.add_comm] using Nat.add_le_add hfirst ih theorem steps_le (graph : PortTables.Table vertices d) (vertex : Fin vertices) (n : Nat) : steps graph vertex n ≤ budget d n (PortTables.tableBits graph).length := by simpa only [steps, budget, commands_length] using costSum_le graph vertex n (commands d n) theorem sequenceTrace (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (distinct : Function.Injective placement) (output : K) (outside : ∀ i, output ≠ placement i) (vertex : Fin vertices) (ops : List (Command d n)) (labels : SequenceLabel ops → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State d n)) (atLabels : ∀ l, program (labels l) = sequenceInstruction n placement output ops labels exit l) (base : K → List Bool) (suffix : List Bool) (ready : PoweringMachineRowBody.Ready graph n placement vertex suffix base) : (advance (TM2.step program))^[sequenceSteps graph n placement output vertex ops base] (some ⟨sequenceEntry ops labels exit, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), base⟩) = some ⟨exit, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), sequenceTapes graph n placement output vertex ops base⟩ := by apply MachineFiniteSequence.trace LocalLabel (fun j => PoweringMachineRowBody.entry n (ports j) (direction j)) (fun j => PoweringMachineRowBody.instruction n placement output (ports j) (direction j)) (result graph n placement output vertex) (fun j _ => PoweringMachineRowBody.steps graph vertex n (ports j) (direction j)) program (PoweringMachineRowBody.Ready graph n placement vertex suffix) (fun _ => PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n)) id ops · intro j _ tapes good exact PoweringMachineRowBody.finalTapes_ready graph n placement distinct output outside vertex (ports j) (direction j) tapes suffix good · intro j _ localLabels localExit atLocal tapes good exact PoweringMachineRowBody.rowTrace graph n placement distinct output outside vertex (ports j) (direction j) localLabels localExit program atLocal tapes suffix good · exact atLabels · exact ready theorem vertexTrace (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (distinct : Function.Injective placement) (output : K) (outside : ∀ i, output ≠ placement i) (vertex : Fin vertices) (labels : Label d n → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State d n)) (atLabels : ∀ l, program (labels l) = instruction n placement output labels exit l) (base : K → List Bool) (suffix : List Bool) (ready : PoweringMachineRowBody.Ready graph n placement vertex suffix base) : (advance (TM2.step program))^[steps graph vertex n] (some ⟨entry d n labels exit, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), base⟩) = some ⟨exit, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), finalTapes graph n placement output vertex base⟩ := by have h := sequenceTrace graph n placement distinct output outside vertex (commands d n) labels exit program atLabels base suffix ready rw [sequenceSteps_eq] at h exact h theorem sequenceTapes_ready (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (distinct : Function.Injective placement) (output : K) (outside : ∀ i, output ≠ placement i) (vertex : Fin vertices) (ops : List (Command d n)) (base : K → List Bool) (suffix : List Bool) (ready : PoweringMachineRowBody.Ready graph n placement vertex suffix base) : PoweringMachineRowBody.Ready graph n placement vertex suffix (sequenceTapes graph n placement output vertex ops base) := by induction ops generalizing base with | nil => exact ready | cons j ops ih => apply ih exact PoweringMachineRowBody.finalTapes_ready graph n placement distinct output outside vertex (ports j) (direction j) base suffix ready theorem finalTapes_ready (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (distinct : Function.Injective placement) (output : K) (outside : ∀ i, output ≠ placement i) (vertex : Fin vertices) (base : K → List Bool) (suffix : List Bool) (ready : PoweringMachineRowBody.Ready graph n placement vertex suffix base) : PoweringMachineRowBody.Ready graph n placement vertex suffix (finalTapes graph n placement output vertex base) := sequenceTapes_ready graph n placement distinct output outside vertex (commands d n) base suffix ready theorem sequenceTapes_other (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (output : K) (vertex : Fin vertices) (ops : List (Command d n)) (base : K → List Bool) (k : K) (hout : k ≠ output) (outside : ∀ i, k ≠ placement i) : sequenceTapes graph n placement output vertex ops base k = base k := by induction ops generalizing base with | nil => rfl | cons j ops ih => change sequenceTapes graph n placement output vertex ops (result graph n placement output vertex j base) k = _ rw [ih] exact PoweringMachineRowBody.finalTapes_other graph n placement output vertex (ports j) (direction j) base k hout outside theorem finalTapes_other (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (output : K) (vertex : Fin vertices) (base : K → List Bool) (k : K) (hout : k ≠ output) (outside : ∀ i, k ≠ placement i) : finalTapes graph n placement output vertex base k = base k := sequenceTapes_other graph n placement output vertex (commands d n) base k hout outside def rowBits (graph : PortTables.Table vertices d) (n : Nat) (vertex : Fin vertices) (j : Command d n) : List Bool := encodeWords (PoweringMachineRowBody.rowWords graph n vertex (ports j) (direction j)) theorem blockIndex_eq_encodeDart (n : Nat) (vertex : Fin vertices) (j : Command d n) : PoweringTableLayout.blockIndex d n vertex j = PoweringEnumeration.encodeDart vertices d n (direction j, vertex, ports j) := by let index : ((Fin (n + 1) → Fin d) × Bool) ≃ Fin (PoweringTableLayout.blockSize d n) := PoweringEnumeration.dartBlockEquiv d n have h := PoweringTableLayout.blockIndex_eq_encodeDart d n vertex (ports j) (direction j) simp only [ports, direction] at h change PoweringTableLayout.blockIndex d n vertex (index (index.symm j)) = _ at h rw [index.apply_symm_apply] at h exact h theorem rowBits_eq (graph : PortTables.Table vertices d) (n : Nat) (vertex : Fin vertices) (j : Command d n) : rowBits graph n vertex j = encodeWords (GenericGraphTables.rowWords (PoweringTables.table graph n).rows[PoweringTableLayout.blockIndex d n vertex j]) := by rw [blockIndex_eq_encodeDart] rfl private theorem encodeWords_flatMap {X : Type*} (xs : List X) (f : X → List Nat) : encodeWords (xs.flatMap f) = xs.flatMap (fun x => encodeWords (f x)) := by induction xs with | nil => rfl | cons x xs ih => simp only [List.flatMap_cons, encodeWords_append, ih] theorem rows_eq_vertexRowBits (graph : PortTables.Table vertices d) (n : Nat) (vertex : Fin vertices) : (List.ofFn id : List (Command d n)).flatMap (rowBits graph n vertex) = PoweringTableLayout.vertexRowBits graph n vertex := by have hrows : (List.ofFn id : List (Command d n)).map (fun j => (PoweringTables.table graph n).rows[PoweringTableLayout.blockIndex d n vertex j]) = List.ofFn (fun j : Command d n => (PoweringTables.table graph n).rows[PoweringTableLayout.blockIndex d n vertex j]) := by simp only [List.map_ofFn, Function.comp_id] calc _ = (List.ofFn id : List (Command d n)).flatMap (fun j => encodeWords (GenericGraphTables.rowWords (PoweringTables.table graph n).rows[PoweringTableLayout.blockIndex d n vertex j])) := by apply congrArg (List.flatMap · (List.ofFn id)) funext j exact rowBits_eq graph n vertex j _ = PoweringTableLayout.vertexRowBits graph n vertex := by unfold PoweringTableLayout.vertexRowBits PoweringTableLayout.vertexRowWords PoweringTableLayout.vertexRows rw [encodeWords_flatMap] erw [← hrows, List.flatMap_map] rfl theorem sequenceTapes_output (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (distinct : Function.Injective placement) (output : K) (outside : ∀ i, output ≠ placement i) (vertex : Fin vertices) (ops : List (Command d n)) (base : K → List Bool) (suffix : List Bool) (ready : PoweringMachineRowBody.Ready graph n placement vertex suffix base) : sequenceTapes graph n placement output vertex ops base output = ops.reverse.flatMap (rowBits graph n vertex) ++ base output := by induction ops generalizing base with | nil => rfl | cons j ops ih => have nextReady := PoweringMachineRowBody.finalTapes_ready graph n placement distinct output outside vertex (ports j) (direction j) base suffix ready change sequenceTapes graph n placement output vertex ops (result graph n placement output vertex j base) output = _ dsimp only [result] rw [ih _ nextReady, PoweringMachineRowBody.finalTapes_output graph n placement distinct output outside vertex (ports j) (direction j) base suffix ready] simp only [List.reverse_cons, List.flatMap_append, List.flatMap_cons, List.flatMap_nil, List.append_nil, List.append_assoc, rowBits] theorem finalTapes_output (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (distinct : Function.Injective placement) (output : K) (outside : ∀ i, output ≠ placement i) (vertex : Fin vertices) (base : K → List Bool) (suffix : List Bool) (ready : PoweringMachineRowBody.Ready graph n placement vertex suffix base) : finalTapes graph n placement output vertex base output = PoweringTableLayout.vertexRowBits graph n vertex ++ base output := by rw [finalTapes, sequenceTapes_output graph n placement distinct output outside vertex (commands d n) base suffix ready] simp only [commands, List.reverse_reverse, rows_eq_vertexRowBits] def vertexInTime (graph : PortTables.Table vertices d) (n : Nat) (placement : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K) (distinct : Function.Injective placement) (output : K) (outside : ∀ i, output ≠ placement i) (vertex : Fin vertices) (labels : Label d n → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State d n)) (atLabels : ∀ l, program (labels l) = instruction n placement output labels exit l) (base : K → List Bool) (suffix : List Bool) (ready : PoweringMachineRowBody.Ready graph n placement vertex suffix base) : StateTransition.EvalsToInTime (TM2.step program) ⟨entry d n labels exit, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), base⟩ (some ⟨exit, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), finalTapes graph n placement output vertex base⟩) (budget d n (PortTables.tableBits graph).length) where steps := steps graph vertex n evals_in_steps := vertexTrace graph n placement distinct output outside vertex labels exit program atLabels base suffix ready steps_le_m := steps_le graph vertex n end BinPackingGames.Foundations.Complexity.PoweringMachineVertex namespace BinPackingGames.Foundations.Complexity.PoweringMachineOuterLoop open Turing MachineComposition PCP variable {K Λ : Type} [DecidableEq K] {vertices d : Nat} abbrev Placement (K : Type) (n : Nat) := PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → K abbrev State := PoweringMachineVertex.State structure Ready (graph : PortTables.Table vertices d) (n : Nat) (placement : Placement K n) (base : K → List Bool) : Prop where table : base (placement (.inl 0)) = PortTables.tableBits graph scratch : base (placement (.inl 5)) = [] leftCopy : base (placement (.inl 8)) = [] rightCopy : base (placement (.inl 9)) = [] data : base (placement (.inl 10)) = [] def counter {length : Nat} (placement : Placement K length) (base : K → List Bool) (remaining : Nat) : K → List Bool := MachineUnaryCounter.counterTapes (placement (.inl 1)) base remaining [] theorem counter_other {length : Nat} (placement : Placement K length) (distinct : Function.Injective placement) (base : K → List Bool) (remaining : Nat) (j : Fin 11) (hj : j ≠ 1) : counter placement base remaining (placement (.inl j)) = base (placement (.inl j)) := by apply MachineUnaryCounter.counterTapes_other intro h exact hj (Sum.inl.inj (distinct h)) theorem ready_vertex (graph : PortTables.Table vertices d) (n : Nat) (placement : Placement K n) (distinct : Function.Injective placement) (base : K → List Bool) (ready : Ready graph n placement base) (vertex : Fin vertices) : PoweringMachineRowBody.Ready graph n placement vertex [] (counter placement base vertex.val) := by constructor · constructor · rw [counter_other placement distinct base vertex.val 0 (by decide)] exact ready.table · simp only [counter, MachineUnaryCounter.counterTapes_counter] · rw [counter_other placement distinct base vertex.val 5 (by decide)] exact ready.scratch · rw [counter_other placement distinct base vertex.val 8 (by decide)] exact ready.leftCopy · rw [counter_other placement distinct base vertex.val 9 (by decide)] exact ready.rightCopy · rw [counter_other placement distinct base vertex.val 10 (by decide)] exact ready.data omit [DecidableEq K] in theorem ready_of_vertex (graph : PortTables.Table vertices d) (n : Nat) (placement : Placement K n) (base : K → List Bool) (vertex : Fin vertices) (ready : PoweringMachineRowBody.Ready graph n placement vertex [] base) : Ready graph n placement base := ⟨ready.1.table, ready.1.scratch, ready.1.leftCopy, ready.1.rightCopy, ready.2⟩ def vertexStart (d n : Nat) (labels : PoweringMachineVertex.Label d n → Λ) (guardLabel : Λ) : Λ := (PoweringMachineVertex.entry d n labels (some guardLabel)).getD guardLabel theorem vertexEntry (d n : Nat) (labels : PoweringMachineVertex.Label d n → Λ) (guardLabel : Λ) : PoweringMachineVertex.entry d n labels (some guardLabel) = some (vertexStart d n labels guardLabel) := by unfold vertexStart PoweringMachineVertex.entry PoweringMachineVertex.sequenceEntry have h : ∀ (commands : List (PoweringMachineVertex.Command d n)) (labels : MachineFiniteSequence.Label PoweringMachineVertex.LocalLabel commands → Λ), MachineFiniteSequence.entry PoweringMachineVertex.LocalLabel (fun j => PoweringMachineRowBody.entry n (PoweringMachineVertex.ports j) (PoweringMachineVertex.direction j)) commands labels (some guardLabel) = some ((MachineFiniteSequence.entry PoweringMachineVertex.LocalLabel (fun j => PoweringMachineRowBody.entry n (PoweringMachineVertex.ports j) (PoweringMachineVertex.direction j)) commands labels (some guardLabel)).getD guardLabel) := by intro commands labels cases commands <;> rfl exact h _ labels def bridge (d n : Nat) (labels : PoweringMachineVertex.Label d n → Λ) (guardLabel : Λ) : TM2.Stmt (fun _ : K => Bool) Λ (State d n) := .goto (fun _ => vertexStart d n labels guardLabel) def finalTapes (graph : PortTables.Table vertices d) (n : Nat) (placement : Placement K n) (output : K) : (remaining : Nat) → remaining ≤ vertices → (K → List Bool) → K → List Bool | 0, _, base => counter placement base 0 | r + 1, bounded, base => finalTapes graph n placement output r (by omega) (PoweringMachineVertex.finalTapes graph n placement output ⟨r, by omega⟩ (counter placement base r)) def steps (graph : PortTables.Table vertices d) (n : Nat) : (remaining : Nat) → remaining ≤ vertices → Nat | 0, _ => 1 | r + 1, bounded => 2 + PoweringMachineVertex.steps graph ⟨r, by omega⟩ n + steps graph n r (by omega) theorem steps_le (graph : PortTables.Table vertices d) (n remaining : Nat) (bounded : remaining ≤ vertices) : steps graph n remaining bounded ≤ remaining * (PoweringMachineVertex.budget d n (PortTables.tableBits graph).length + 2) + 1 := by induction remaining with | zero => simp [steps] | succ r ih => have hv := PoweringMachineVertex.steps_le graph (⟨r, by omega⟩ : Fin vertices) n have hi := ih (by omega) simp only [steps, Nat.add_mul, Nat.one_mul] omega theorem finalTapes_ready (graph : PortTables.Table vertices d) (n : Nat) (placement : Placement K n) (distinct : Function.Injective placement) (output : K) (outside : ∀ i, output ≠ placement i) (remaining : Nat) (bounded : remaining ≤ vertices) (base : K → List Bool) (ready : Ready graph n placement base) : Ready graph n placement (finalTapes graph n placement output remaining bounded base) := by induction remaining generalizing base with | zero => constructor · rw [finalTapes, counter_other placement distinct base 0 0 (by decide)] exact ready.table · rw [finalTapes, counter_other placement distinct base 0 5 (by decide)] exact ready.scratch · rw [finalTapes, counter_other placement distinct base 0 8 (by decide)] exact ready.leftCopy · rw [finalTapes, counter_other placement distinct base 0 9 (by decide)] exact ready.rightCopy · rw [finalTapes, counter_other placement distinct base 0 10 (by decide)] exact ready.data | succ r ih => apply ih apply ready_of_vertex graph n placement _ (⟨r, by omega⟩ : Fin vertices) exact PoweringMachineVertex.finalTapes_ready graph n placement distinct output outside ⟨r, by omega⟩ _ [] (ready_vertex graph n placement distinct base ready _) def prefixRows (graph : PortTables.Table vertices d) (n : Nat) : Nat → List Bool | 0 => [] | r + 1 => prefixRows graph n r ++ if h : r < vertices then PoweringTableLayout.vertexRowBits graph n ⟨r, h⟩ else [] theorem prefixRows_ofFn (graph : PortTables.Table vertices d) (n remaining : Nat) (bounded : remaining ≤ vertices) : prefixRows graph n remaining = (List.ofFn (fun i : Fin remaining => PoweringTableLayout.vertexRowBits graph n (Fin.castLE bounded i))).flatten := by induction remaining with | zero => rfl | succ r ih => have hr : r < vertices := by omega rw [prefixRows, dite_eq_left hr, List.ofFn_succ_last, List.flatten_append] simp only [List.flatten_cons, List.flatten_nil, List.append_nil] change prefixRows graph n r ++ PoweringTableLayout.vertexRowBits graph n ⟨r, hr⟩ = (List.ofFn (fun i : Fin r => PoweringTableLayout.vertexRowBits graph n (Fin.castLE (show r ≤ vertices by omega) i))).flatten ++ PoweringTableLayout.vertexRowBits graph n ⟨r, hr⟩ rw [ih] theorem prefixRows_all (graph : PortTables.Table vertices d) (n : Nat) : prefixRows graph n vertices = (List.finRange vertices).flatMap (PoweringTableLayout.vertexRowBits graph n) := by rw [prefixRows_ofFn graph n vertices (Nat.le_refl _)] simp only [List.finRange, List.flatMap_def, List.map_ofFn] rfl theorem finalTapes_output (graph : PortTables.Table vertices d) (n : Nat) (placement : Placement K n) (distinct : Function.Injective placement) (output : K) (outside : ∀ i, output ≠ placement i) (remaining : Nat) (bounded : remaining ≤ vertices) (base : K → List Bool) (ready : Ready graph n placement base) : finalTapes graph n placement output remaining bounded base output = prefixRows graph n remaining ++ base output := by induction remaining generalizing base with | zero => exact MachineUnaryCounter.counterTapes_other (placement (.inl 1)) output (outside _) base 0 [] | succ r ih => let vertex : Fin vertices := ⟨r, by omega⟩ let current := counter placement base r have vertexReady := ready_vertex graph n placement distinct base ready vertex have nextReady := PoweringMachineVertex.finalTapes_ready graph n placement distinct output outside vertex current [] vertexReady change finalTapes graph n placement output r (by omega) (PoweringMachineVertex.finalTapes graph n placement output vertex current) output = _ rw [ih (by omega) _ (ready_of_vertex graph n placement _ vertex nextReady), PoweringMachineVertex.finalTapes_output graph n placement distinct output outside vertex current [] vertexReady] have hc : current output = base output := MachineUnaryCounter.counterTapes_other (placement (.inl 1)) output (outside _) base r [] rw [hc, prefixRows, dite_eq_left vertex.isLt, List.append_assoc] theorem finalTapes_other (graph : PortTables.Table vertices d) (n : Nat) (placement : Placement K n) (output : K) (remaining : Nat) (bounded : remaining ≤ vertices) (base : K → List Bool) (k : K) (hout : k ≠ output) (outside : ∀ i, k ≠ placement i) : finalTapes graph n placement output remaining bounded base k = base k := by induction remaining generalizing base with | zero => exact MachineUnaryCounter.counterTapes_other _ _ (outside _) base 0 [] | succ r ih => rw [finalTapes, ih, PoweringMachineVertex.finalTapes_other graph n placement output ⟨r, by omega⟩ _ k hout outside] exact MachineUnaryCounter.counterTapes_other _ _ (outside _) base r [] theorem loopTrace (graph : PortTables.Table vertices d) (n : Nat) (placement : Placement K n) (distinct : Function.Injective placement) (output : K) (outside : ∀ i, output ≠ placement i) (guardLabel bridgeLabel exitLabel : Λ) (labels : PoweringMachineVertex.Label d n → Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State d n)) (atGuard : program guardLabel = MachineUnaryCounter.guard (placement (.inl 1)) bridgeLabel exitLabel) (atBridge : program bridgeLabel = bridge d n labels guardLabel) (atVertex : ∀ l, program (labels l) = PoweringMachineVertex.instruction n placement output labels (some guardLabel) l) (remaining : Nat) (bounded : remaining ≤ vertices) (base : K → List Bool) (ready : Ready graph n placement base) : (advance (TM2.step program))^[steps graph n remaining bounded] (some ⟨some guardLabel, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), counter placement base remaining⟩) = some ⟨some exitLabel, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), finalTapes graph n placement output remaining bounded base⟩ := by let initial := PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n) induction remaining generalizing base with | zero => simpa only [steps, finalTapes, counter, initial, PoweringMasterState.clean, PoweringMasterState.withBuffer] using MachineUnaryCounter.guardTrace_zero (placement (.inl 1)) guardLabel bridgeLabel exitLabel program atGuard base [] initial.1 none | succ r ih => let vertex : Fin vertices := ⟨r, by omega⟩ let current := counter placement base r let next := PoweringMachineVertex.finalTapes graph n placement output vertex current have vertexReady : PoweringMachineRowBody.Ready graph n placement vertex [] current := ready_vertex graph n placement distinct base ready vertex have nextVertexReady := PoweringMachineVertex.finalTapes_ready graph n placement distinct output outside vertex current [] vertexReady have nextReady : Ready graph n placement next := ready_of_vertex graph n placement next vertex nextVertexReady have sameCounter : counter placement next r = next := by have hs := nextVertexReady.1.source change next (placement (.inl 1)) = encodeWord r ++ [] at hs unfold counter MachineUnaryCounter.counterTapes rw [← hs] exact Function.update_eq_self _ _ have tailRun := ih (by omega) next nextReady rw [sameCounter] at tailRun have guardRun : advance (TM2.step program) (some ⟨some guardLabel, initial, counter placement base (r + 1)⟩) = some ⟨some bridgeLabel, initial, current⟩ := by simpa only [advance_some, current, counter, initial, PoweringMasterState.clean, PoweringMasterState.withBuffer] using MachineUnaryCounter.guardStep_succ (placement (.inl 1)) guardLabel bridgeLabel exitLabel program atGuard base r [] initial.1 none have bridgeRun : advance (TM2.step program) (some ⟨some bridgeLabel, initial, current⟩) = some ⟨PoweringMachineVertex.entry d n labels (some guardLabel), initial, current⟩ := by change some (TM2.stepAux (program bridgeLabel) initial current) = _ rw [atBridge, vertexEntry] rfl have vertexRun := PoweringMachineVertex.vertexTrace graph n placement distinct output outside vertex labels (some guardLabel) program atVertex current [] vertexReady change (advance (TM2.step program))^[2 + PoweringMachineVertex.steps graph vertex n + steps graph n r (by omega)] (some ⟨some guardLabel, initial, counter placement base (r + 1)⟩) = _ rw [show 2 + PoweringMachineVertex.steps graph vertex n + steps graph n r (by omega) = (steps graph n r (by omega) + PoweringMachineVertex.steps graph vertex n) + 1 + 1 by omega, Function.iterate_succ_apply, guardRun, Function.iterate_succ_apply, bridgeRun, Function.iterate_add_apply, vertexRun] exact tailRun def loopInTime (graph : PortTables.Table vertices d) (n : Nat) (placement : Placement K n) (distinct : Function.Injective placement) (output : K) (outside : ∀ i, output ≠ placement i) (guardLabel bridgeLabel exitLabel : Λ) (labels : PoweringMachineVertex.Label d n → Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State d n)) (atGuard : program guardLabel = MachineUnaryCounter.guard (placement (.inl 1)) bridgeLabel exitLabel) (atBridge : program bridgeLabel = bridge d n labels guardLabel) (atVertex : ∀ l, program (labels l) = PoweringMachineVertex.instruction n placement output labels (some guardLabel) l) (remaining : Nat) (bounded : remaining ≤ vertices) (base : K → List Bool) (ready : Ready graph n placement base) : StateTransition.EvalsToInTime (TM2.step program) ⟨some guardLabel, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), counter placement base remaining⟩ (some ⟨some exitLabel, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), finalTapes graph n placement output remaining bounded base⟩) (remaining * (PoweringMachineVertex.budget d n (PortTables.tableBits graph).length + 2) + 1) where steps := steps graph n remaining bounded evals_in_steps := loopTrace graph n placement distinct output outside guardLabel bridgeLabel exitLabel labels program atGuard atBridge atVertex remaining bounded base ready steps_le_m := steps_le graph n remaining bounded end BinPackingGames.Foundations.Complexity.PoweringMachineOuterLoop namespace BinPackingGames.Foundations.Complexity.PoweringPolynomialBudget open PCP def preBudget (d n vertices inputLength : Nat) : Nat := (3 * vertices + 5) + (vertices * (PoweringMachineVertex.budget d n inputLength + 2) + 1) def finishBudget (d n pushBound tapeCount vertices inputLength prior : Nat) : Nat := 2 * (vertices + PoweringTableLayout.blockSize d n * vertices) + 6 + tapeCount * (inputLength + prior * pushBound + vertices + PoweringTableLayout.blockSize d n * vertices + 3) def totalBudget (d n pushBound tapeCount vertices inputLength : Nat) : Nat := preBudget d n vertices inputLength + finishBudget d n pushBound tapeCount vertices inputLength (preBudget d n vertices inputLength) theorem preBudget_mono_vertices (d n inputLength : Nat) {v w : Nat} (h : v ≤ w) : preBudget d n v inputLength ≤ preBudget d n w inputLength := by have hthree := Nat.mul_le_mul_left 3 h have hloop := Nat.mul_le_mul_right (PoweringMachineVertex.budget d n inputLength + 2) h unfold preBudget omega theorem finishBudget_mono (d n pushBound tapeCount inputLength : Nat) {v w prior later : Nat} (hvertices : v ≤ w) (hprior : prior ≤ later) : finishBudget d n pushBound tapeCount v inputLength prior ≤ finishBudget d n pushBound tapeCount w inputLength later := by have hdarts := Nat.mul_le_mul_left (PoweringTableLayout.blockSize d n) hvertices have hheader := Nat.mul_le_mul_left 2 (Nat.add_le_add hvertices hdarts) have hgrowth := Nat.mul_le_mul_right pushBound hprior have hinside : inputLength + prior * pushBound + v + PoweringTableLayout.blockSize d n * v + 3 ≤ inputLength + later * pushBound + w + PoweringTableLayout.blockSize d n * w + 3 := by omega have hclear := Nat.mul_le_mul_left tapeCount hinside unfold finishBudget omega theorem totalBudget_mono_vertices (d n pushBound tapeCount inputLength : Nat) {v w : Nat} (hvertices : v ≤ w) : totalBudget d n pushBound tapeCount v inputLength ≤ totalBudget d n pushBound tapeCount w inputLength := by have hpre := preBudget_mono_vertices d n inputLength hvertices unfold totalBudget exact Nat.add_le_add hpre (finishBudget_mono d n pushBound tapeCount inputLength hvertices hpre) noncomputable def rowTime (d n : Nat) : Polynomial Nat := Polynomial.C (PoweringMachineRowBody.bufferSize d n) * (Polynomial.C (28 * (2 * (n + 1)) + 10) * Polynomial.X + Polynomial.C (24 * (2 * (n + 1)) + 15)) + Polynomial.C 1 + (Polynomial.C (14 * (n + 1) + 4) * Polynomial.X + Polynomial.C (12 * (n + 1)) + Polynomial.C 6) @[simp] theorem rowTime_eval (d n inputLength : Nat) : (rowTime d n).eval inputLength = PoweringMachineRowBody.budget d n inputLength := by simp only [rowTime, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_X, PoweringMachineRowBody.budget, PoweringPlanBudget.rowBudget, PoweringPlanBudget.fieldBudget, PoweringMachineRowBody.bufferSize, Nat.add_assoc] noncomputable def vertexTime (d n : Nat) : Polynomial Nat := Polynomial.C (PoweringTableLayout.blockSize d n) * rowTime d n @[simp] theorem vertexTime_eval (d n inputLength : Nat) : (vertexTime d n).eval inputLength = PoweringMachineVertex.budget d n inputLength := by simp only [vertexTime, Polynomial.eval_mul, Polynomial.eval_C, rowTime_eval, PoweringMachineVertex.budget] noncomputable def preTime (d n : Nat) : Polynomial Nat := (Polynomial.C 3 * Polynomial.X + Polynomial.C 5) + (Polynomial.X * (vertexTime d n + Polynomial.C 2) + Polynomial.C 1) @[simp] theorem preTime_eval (d n inputLength : Nat) : (preTime d n).eval inputLength = preBudget d n inputLength inputLength := by simp only [preTime, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_X, vertexTime_eval, preBudget] noncomputable def time (d n pushBound tapeCount : Nat) : Polynomial Nat := preTime d n + (Polynomial.C 2 * (Polynomial.X + Polynomial.C (PoweringTableLayout.blockSize d n) * Polynomial.X) + Polynomial.C 6 + Polynomial.C tapeCount * (Polynomial.X + preTime d n * Polynomial.C pushBound + Polynomial.X + Polynomial.C (PoweringTableLayout.blockSize d n) * Polynomial.X + Polynomial.C 3)) @[simp] theorem time_eval (d n pushBound tapeCount inputLength : Nat) : (time d n pushBound tapeCount).eval inputLength = totalBudget d n pushBound tapeCount inputLength inputLength := by simp only [time, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_X, preTime_eval, totalBudget, finishBudget] theorem totalBudget_le_time (d n pushBound tapeCount vertices inputLength : Nat) (hvertices : vertices ≤ inputLength) : totalBudget d n pushBound tapeCount vertices inputLength ≤ (time d n pushBound tapeCount).eval inputLength := by rw [time_eval] exact totalBudget_mono_vertices d n pushBound tapeCount inputLength hvertices theorem setup_loop_finish_le_time (d n pushBound tapeCount vertices inputLength : Nat) (hvertices : vertices ≤ inputLength) : ((3 * vertices + 5) + (vertices * (PoweringMachineVertex.budget d n inputLength + 2) + 1)) + (2 * (vertices + (2 * d ^ (n + 1)) * vertices) + 6 + tapeCount * (inputLength + ((3 * vertices + 5) + (vertices * (PoweringMachineVertex.budget d n inputLength + 2) + 1)) * pushBound + vertices + (2 * d ^ (n + 1)) * vertices + 3)) ≤ (time d n pushBound tapeCount).eval inputLength := by simpa only [totalBudget, preBudget, finishBudget, PoweringTableLayout.blockSize] using totalBudget_le_time d n pushBound tapeCount vertices inputLength hvertices theorem execution_budget_le_time (d n pushBound tapeCount vertices inputLength : Nat) (hvertices : vertices ≤ inputLength) {preSteps finishSteps : Nat} (hpre : preSteps ≤ preBudget d n vertices inputLength) (hfinish : finishSteps ≤ finishBudget d n pushBound tapeCount vertices inputLength (preBudget d n vertices inputLength)) : preSteps + finishSteps ≤ (time d n pushBound tapeCount).eval inputLength := by calc preSteps + finishSteps ≤ totalBudget d n pushBound tapeCount vertices inputLength := by unfold totalBudget exact Nat.add_le_add hpre hfinish _ ≤ (time d n pushBound tapeCount).eval inputLength := totalBudget_le_time d n pushBound tapeCount vertices inputLength hvertices theorem graph_totalBudget_le_time {vertices d : Nat} (graph : PortTables.Table vertices d) (n pushBound tapeCount : Nat) : totalBudget d n pushBound tapeCount vertices (PortTables.tableBits graph).length ≤ (time d n pushBound tapeCount).eval (PortTables.tableBits graph).length := totalBudget_le_time d n pushBound tapeCount vertices (PortTables.tableBits graph).length (PortTables.vertices_le_tableBits_length graph) end BinPackingGames.Foundations.Complexity.PoweringPolynomialBudget namespace BinPackingGames.Foundations.Complexity.PoweringMachineGlobal open Turing abbrev Tape (n : Nat) := PoweringMachineInitialize.GlobalTape (PoweringMachineRowBody.capacity n) abbrev State (d n : Nat) := PoweringMasterState.Master (PoweringMachineRowBody.bufferSize d n) def placement (n : Nat) : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n) → Tape n := PoweringMachineInitialize.commonPlacement (PoweringMachineRowBody.capacity n) def inputTape (n : Nat) : Tape n := placement n (PoweringMachineTapes.table (PoweringMachineRowBody.capacity n)) def outputTape (n : Nat) : Tape n := PoweringMachineInitialize.finalOutput (PoweringMachineRowBody.capacity n) def headerVertices (n : Nat) : Tape n := .inl PoweringMachineLoop.HeaderTape.vertices def headerDarts (n : Nat) : Tape n := .inl PoweringMachineLoop.HeaderTape.darts def enumeration (n : Nat) : Fin (4 + (11 + PoweringMachineRowBody.capacity n + 1)) ≃ Tape n := PoweringGlobalEnumeration.enumeration (PoweringMachineRowBody.capacity n) abbrev FinishLabel (n : Nat) := PoweringMachineFinish.Label (enumeration n) (outputTape n) abbrev Label (d n : Nat) := PoweringMachineInitialize.Label ⊕ (Bool ⊕ (PoweringMachineVertex.Label d n ⊕ FinishLabel n)) instance labelFintype (d n : Nat) : Fintype (Label d n) := inferInstanceAs (Fintype (PoweringMachineInitialize.Label ⊕ (Bool ⊕ (PoweringMachineVertex.Label d n ⊕ FinishLabel n)))) def initLabels (d n : Nat) : PoweringMachineInitialize.Label → Label d n := Sum.inl def guardLabel (d n : Nat) : Label d n := .inr (.inl false) def bridgeLabel (d n : Nat) : Label d n := .inr (.inl true) def vertexLabels (d n : Nat) (label : PoweringMachineVertex.Label d n) : Label d n := .inr (.inr (.inl label)) def finishLabels (d n : Nat) (label : FinishLabel n) : Label d n := .inr (.inr (.inr label)) def finishEntry (d n : Nat) : Label d n := finishLabels d n (.inl .seed) def main (d n : Nat) : Label d n := initLabels d n (.inr .seed) def program (d n : Nat) : Label d n → TM2.Stmt (fun _ : Tape n => Bool) (Label d n) (State d n) | .inl label => PoweringMachineInitialize.instruction (PoweringMachineRowBody.capacity n) (PoweringMachineRowBody.bufferSize d n) (PCP.PoweringTableLayout.blockSize d n) (initLabels d n) (some (guardLabel d n)) label | .inr (.inl false) => MachineUnaryCounter.guard (placement n (PoweringMachineTapes.start (PoweringMachineRowBody.capacity n))) (bridgeLabel d n) (finishEntry d n) | .inr (.inl true) => PoweringMachineOuterLoop.bridge d n (vertexLabels d n) (guardLabel d n) | .inr (.inr (.inl label)) => PoweringMachineVertex.instruction n (placement n) (outputTape n) (vertexLabels d n) (some (guardLabel d n)) label | .inr (.inr (.inr label)) => PoweringMachineFinish.instruction (enumeration n) (headerVertices n) (headerDarts n) (outputTape n) (placement n (PoweringMachineTapes.scratch (PoweringMachineRowBody.capacity n))) (finishLabels d n) label @[simp] theorem atInit (d n : Nat) (label : PoweringMachineInitialize.Label) : program d n (initLabels d n label) = PoweringMachineInitialize.instruction (PoweringMachineRowBody.capacity n) (PoweringMachineRowBody.bufferSize d n) (PCP.PoweringTableLayout.blockSize d n) (initLabels d n) (some (guardLabel d n)) label := rfl @[simp] theorem atGuard (d n : Nat) : program d n (guardLabel d n) = MachineUnaryCounter.guard (placement n (PoweringMachineTapes.start (PoweringMachineRowBody.capacity n))) (bridgeLabel d n) (finishEntry d n) := rfl @[simp] theorem atBridge (d n : Nat) : program d n (bridgeLabel d n) = PoweringMachineOuterLoop.bridge d n (vertexLabels d n) (guardLabel d n) := rfl @[simp] theorem atVertex (d n : Nat) (label : PoweringMachineVertex.Label d n) : program d n (vertexLabels d n label) = PoweringMachineVertex.instruction n (placement n) (outputTape n) (vertexLabels d n) (some (guardLabel d n)) label := rfl @[simp] theorem atFinish (d n : Nat) (label : FinishLabel n) : program d n (finishLabels d n label) = PoweringMachineFinish.instruction (enumeration n) (headerVertices n) (headerDarts n) (outputTape n) (placement n (PoweringMachineTapes.scratch (PoweringMachineRowBody.capacity n))) (finishLabels d n) label := rfl def machine (d n : Nat) : FinTM2 where K := Tape n k₀ := inputTape n k₁ := outputTape n Γ _ := Bool Λ := Label d n main := main d n σ := State d n initialState := PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n) m := program d n @[simp] theorem machine_code (d n : Nat) (label : Label d n) : (machine d n).m label = program d n label := rfl @[simp] theorem machine_main (d n : Nat) : (machine d n).main = main d n := rfl @[simp] theorem machine_initialState (d n : Nat) : (machine d n).initialState = PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n) := rfl theorem input_ne_output (n : Nat) : inputTape n ≠ outputTape n := PoweringMachineInitialize.commonPlacement_ne_finalOutput (PoweringMachineRowBody.capacity n) (PoweringMachineTapes.table (PoweringMachineRowBody.capacity n)) end BinPackingGames.Foundations.Complexity.PoweringMachineGlobal namespace BinPackingGames.Foundations.Complexity.PoweringInitializeFrame open Turing PCP variable {vertices d : Nat} def initialExtra (graph : PortTables.Table vertices d) (n : Nat) : PoweringMachineInitialize.ExtraTape (PoweringMachineRowBody.capacity n) → List Bool := fun tape => if tape = .inl (PoweringMachineTapes.table (PoweringMachineRowBody.capacity n)) then PortTables.tableBits graph else [] @[simp] theorem initialExtra_table (graph : PortTables.Table vertices d) (n : Nat) : initialExtra graph n (.inl (PoweringMachineTapes.table (PoweringMachineRowBody.capacity n))) = PortTables.tableBits graph := by simp [initialExtra] theorem initialExtra_other (graph : PortTables.Table vertices d) (n : Nat) (tape : PoweringMachineInitialize.ExtraTape (PoweringMachineRowBody.capacity n)) (hne : tape ≠ .inl (PoweringMachineTapes.table (PoweringMachineRowBody.capacity n))) : initialExtra graph n tape = [] := by simp [initialExtra, hne] @[simp] theorem initialExtra_scratch (graph : PortTables.Table vertices d) (n : Nat) : initialExtra graph n (.inl (PoweringMachineTapes.scratch (PoweringMachineRowBody.capacity n))) = [] := by simp [initialExtra, PoweringMachineTapes.table, PoweringMachineTapes.scratch] def preparedTapes (graph : PortTables.Table vertices d) (n : Nat) : PoweringMachineGlobal.Tape n → List Bool := PoweringMachineInitialize.mergeTapes (PoweringMachineLoop.finalTapes (PoweringTableLayout.blockSize d n) vertices []) (initialExtra graph n) theorem prepared_commonRole (graph : PortTables.Table vertices d) (n : Nat) (j : Fin 11) (hne : j ≠ 1) : preparedTapes graph n (PoweringMachineGlobal.placement n (.inl j)) = if j = 0 then PortTables.tableBits graph else [] := by simp [preparedTapes, PoweringMachineGlobal.placement, PoweringMachineInitialize.commonPlacement, PoweringMachineTapes.start, PoweringMachineInitialize.mergeTapes, initialExtra, PoweringMachineTapes.table, hne] theorem prepared_ready (graph : PortTables.Table vertices d) (n : Nat) : PoweringMachineOuterLoop.Ready graph n (PoweringMachineGlobal.placement n) (preparedTapes graph n) := by constructor · simpa using prepared_commonRole graph n 0 (by decide) · simpa using prepared_commonRole graph n 5 (by decide) · simpa using prepared_commonRole graph n 8 (by decide) · simpa using prepared_commonRole graph n 9 (by decide) · simpa using prepared_commonRole graph n 10 (by decide) @[simp] theorem prepared_start (graph : PortTables.Table vertices d) (n : Nat) : preparedTapes graph n (PoweringMachineGlobal.placement n (.inl 1)) = encodeWord vertices := by simpa only [preparedTapes, PoweringMachineGlobal.placement, PoweringMachineTapes.start] using PoweringMachineInitialize.final_counter (PoweringMachineRowBody.capacity n) (PoweringTableLayout.blockSize d n) vertices (initialExtra graph n) theorem prepared_counter (graph : PortTables.Table vertices d) (n : Nat) : PoweringMachineOuterLoop.counter (PoweringMachineGlobal.placement n) (preparedTapes graph n) vertices = preparedTapes graph n := by unfold PoweringMachineOuterLoop.counter MachineUnaryCounter.counterTapes rw [List.append_nil, ← prepared_start graph n] simp only [Function.update_eq_self] @[simp] theorem prepared_output (graph : PortTables.Table vertices d) (n : Nat) : preparedTapes graph n (PoweringMachineGlobal.outputTape n) = [] := by simp [preparedTapes, PoweringMachineGlobal.outputTape, PoweringMachineInitialize.finalOutput, PoweringMachineInitialize.mergeTapes, initialExtra] @[simp] theorem prepared_vertices (graph : PortTables.Table vertices d) (n : Nat) : preparedTapes graph n (PoweringMachineGlobal.headerVertices n) = encodeWord vertices := rfl @[simp] theorem prepared_darts (graph : PortTables.Table vertices d) (n : Nat) : preparedTapes graph n (PoweringMachineGlobal.headerDarts n) = encodeWord (PoweringTableLayout.blockSize d n * vertices) := rfl @[simp] theorem prepared_source (graph : PortTables.Table vertices d) (n : Nat) : preparedTapes graph n (.inl PoweringMachineLoop.HeaderTape.source) = [] := rfl def preparedCfg (graph : PortTables.Table vertices d) (n : Nat) : (PoweringMachineGlobal.machine d n).Cfg := ⟨some (PoweringMachineGlobal.guardLabel d n), PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), preparedTapes graph n⟩ theorem initialGlobalTapes_eq (graph : PortTables.Table vertices d) (n : Nat) : PoweringMachineInitialize.initialGlobalTapes (initialExtra graph n) = fun tape => if tape = PoweringMachineGlobal.inputTape n then PortTables.tableBits graph else [] := by funext tape cases tape with | inl field => simp [PoweringMachineInitialize.initialGlobalTapes, PoweringMachineInitialize.mergeTapes, PoweringMachineGlobal.inputTape, PoweringMachineGlobal.placement, PoweringMachineInitialize.commonPlacement_table] | inr field => simp [PoweringMachineInitialize.initialGlobalTapes, PoweringMachineInitialize.mergeTapes, PoweringMachineGlobal.inputTape, PoweringMachineGlobal.placement, PoweringMachineInitialize.commonPlacement_table, initialExtra] private theorem machine_initialStacks (d n : Nat) (input : List Bool) : (initList (PoweringMachineGlobal.machine d n) input).stk = fun tape => if tape = PoweringMachineGlobal.inputTape n then input else [] := by simp only [PoweringMachineGlobal.machine] rfl theorem initList_eq (graph : PortTables.Table vertices d) (n : Nat) : initList (PoweringMachineGlobal.machine d n) (PortTables.tableBits graph) = ⟨some (PoweringMachineGlobal.main d n), PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), PoweringMachineInitialize.initialGlobalTapes (initialExtra graph n)⟩ := by have ht := (machine_initialStacks d n (PortTables.tableBits graph)).trans (initialGlobalTapes_eq graph n).symm exact congrArg (TM2.Cfg.mk _ _) ht theorem tableBits_head (graph : PortTables.Table vertices d) : PortTables.tableBits graph = encodeWord vertices ++ encodeWords (PortTables.tableWords graph).tail := rfl def initializeInTime (graph : PortTables.Table vertices d) (n : Nat) : StateTransition.EvalsToInTime (PoweringMachineGlobal.machine d n).step (initList (PoweringMachineGlobal.machine d n) (PortTables.tableBits graph)) (some (preparedCfg graph n)) (3 * vertices + 5) := by rw [initList_eq] exact PoweringMachineInitialize.initializeAtInTime (PoweringMachineRowBody.capacity n) (PoweringMachineRowBody.bufferSize d n) (PoweringTableLayout.blockSize d n) vertices (PoweringMachineGlobal.initLabels d n) (some (PoweringMachineGlobal.guardLabel d n)) (PoweringMachineGlobal.program d n) (PoweringMachineGlobal.atInit d n) (initialExtra graph n) (encodeWords (PortTables.tableWords graph).tail) (by rw [initialExtra_table, tableBits_head]) (initialExtra_scratch graph n) (PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n)) end BinPackingGames.Foundations.Complexity.PoweringInitializeFrame namespace BinPackingGames.Foundations.Complexity.PoweringGlobalConfiguration open Turing PoweringMachineGlobal theorem placement_injective (n : Nat) : Function.Injective (placement n) := PoweringMachineInitialize.commonPlacement_injective (PoweringMachineRowBody.capacity n) theorem placement_ne_output (n : Nat) (tape : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n)) : placement n tape ≠ outputTape n := PoweringMachineInitialize.commonPlacement_ne_finalOutput (PoweringMachineRowBody.capacity n) tape theorem output_ne_placement (n : Nat) (tape : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n)) : outputTape n ≠ placement n tape := Ne.symm (placement_ne_output n tape) theorem headerVertices_ne_placement (n : Nat) (tape : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n)) : headerVertices n ≠ placement n tape := Ne.symm (PoweringMachineInitialize.commonPlacement_ne_header (PoweringMachineRowBody.capacity n) tape .vertices (by decide)) theorem headerDarts_ne_placement (n : Nat) (tape : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n)) : headerDarts n ≠ placement n tape := Ne.symm (PoweringMachineInitialize.commonPlacement_ne_header (PoweringMachineRowBody.capacity n) tape .darts (by decide)) theorem placement_ne_headerVertices (n : Nat) (tape : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n)) : placement n tape ≠ headerVertices n := Ne.symm (headerVertices_ne_placement n tape) theorem placement_ne_headerDarts (n : Nat) (tape : PoweringMachineTapes.Tape (PoweringMachineRowBody.capacity n)) : placement n tape ≠ headerDarts n := Ne.symm (headerDarts_ne_placement n tape) theorem headerVertices_ne_headerDarts (n : Nat) : headerVertices n ≠ headerDarts n := by simp [headerVertices, headerDarts] theorem headerVertices_ne_output (n : Nat) : headerVertices n ≠ outputTape n := by simp [headerVertices, outputTape, PoweringMachineInitialize.finalOutput] theorem headerDarts_ne_output (n : Nat) : headerDarts n ≠ outputTape n := by simp [headerDarts, outputTape, PoweringMachineInitialize.finalOutput] theorem headerVertices_ne_scratch (n : Nat) : headerVertices n ≠ placement n (PoweringMachineTapes.scratch (PoweringMachineRowBody.capacity n)) := headerVertices_ne_placement n _ theorem headerDarts_ne_scratch (n : Nat) : headerDarts n ≠ placement n (PoweringMachineTapes.scratch (PoweringMachineRowBody.capacity n)) := headerDarts_ne_placement n _ theorem scratch_ne_output (n : Nat) : placement n (PoweringMachineTapes.scratch (PoweringMachineRowBody.capacity n)) ≠ outputTape n := placement_ne_output n _ @[simp] theorem boolList_mpr_eq (h : List Bool = List Bool) (bits : List Bool) : h.mpr bits = bits := rfl theorem haltList_tapes (d n : Nat) (output : List Bool) : (Turing.haltList (machine d n) output).stk = MachineDrainMany.haltTapes (outputTape n) output := by rfl theorem haltList_eq (d n : Nat) (output : List Bool) : Turing.haltList (machine d n) output = (⟨none, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), MachineDrainMany.haltTapes (outputTape n) output⟩ : (machine d n).Cfg) := by change (⟨none, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), (Turing.haltList (machine d n) output).stk⟩ : (machine d n).Cfg) = _ exact congrArg (fun tapes => (⟨none, PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), tapes⟩ : (machine d n).Cfg)) (haltList_tapes d n output) end BinPackingGames.Foundations.Complexity.PoweringGlobalConfiguration namespace BinPackingGames.Foundations.Complexity.PoweringRuntimeBudget open Turing theorem stackLength_le_of_execution (tm : FinTM2) (start finish : tm.Cfg) (initialBound priorBudget : Nat) (initial : ∀ k, (start.stk k).length ≤ initialBound) (prior : StateTransition.EvalsToInTime tm.step start (some finish) priorBudget) (k : tm.K) : (finish.stk k).length ≤ initialBound + priorBudget * Runtime.programPushBound tm := by have grown := Runtime.executionSizeBound tm.step (fun cfg => (cfg.stk k).length) (Runtime.programPushBound tm) (Runtime.stepStackLength tm k) prior exact grown.trans (Nat.add_le_add_right (initial k) _) theorem stackLength_le_from_input (tm : FinTM2) (input : List (tm.Γ tm.k₀)) (finish : tm.Cfg) (priorBudget : Nat) (prior : StateTransition.EvalsToInTime tm.step (initList tm input) (some finish) priorBudget) (k : tm.K) : (finish.stk k).length ≤ input.length + priorBudget * Runtime.programPushBound tm := stackLength_le_of_execution tm (initList tm input) finish input.length priorBudget (Runtime.initialStackLength tm input) prior k abbrev tapeCount (radius : Nat) : Nat := 4 + (11 + PoweringMachineRowBody.capacity radius + 1) theorem finish_steps_le_of_execution (d radius : Nat) (input : List Bool) (before : (PoweringMachineGlobal.machine d radius).Cfg) (priorBudget vertices darts : Nat) (prior : StateTransition.EvalsToInTime (PoweringMachineGlobal.machine d radius).step (initList (PoweringMachineGlobal.machine d radius) input) (some before) priorBudget) : PoweringMachineFinish.steps (PoweringMachineGlobal.enumeration radius) (PoweringMachineGlobal.outputTape radius) before.stk vertices darts ≤ 2 * (vertices + darts) + 6 + tapeCount radius * (input.length + priorBudget * Runtime.programPushBound (PoweringMachineGlobal.machine d radius) + vertices + darts + 3) := by exact PoweringMachineFinish.steps_le_uniform (PoweringMachineGlobal.enumeration radius) (PoweringMachineGlobal.outputTape radius) before.stk vertices darts (input.length + priorBudget * Runtime.programPushBound (PoweringMachineGlobal.machine d radius)) (stackLength_le_from_input (PoweringMachineGlobal.machine d radius) input before priorBudget prior) theorem total_steps_le_of_execution (d radius : Nat) (input : List Bool) (before : (PoweringMachineGlobal.machine d radius).Cfg) (priorBudget vertices darts : Nat) (prior : StateTransition.EvalsToInTime (PoweringMachineGlobal.machine d radius).step (initList (PoweringMachineGlobal.machine d radius) input) (some before) priorBudget) : prior.steps + PoweringMachineFinish.steps (PoweringMachineGlobal.enumeration radius) (PoweringMachineGlobal.outputTape radius) before.stk vertices darts ≤ priorBudget + (2 * (vertices + darts) + 6 + tapeCount radius * (input.length + priorBudget * Runtime.programPushBound (PoweringMachineGlobal.machine d radius) + vertices + darts + 3)) := Nat.add_le_add prior.steps_le_m (finish_steps_le_of_execution d radius input before priorBudget vertices darts prior) end BinPackingGames.Foundations.Complexity.PoweringRuntimeBudget namespace BinPackingGames.Foundations.Complexity.PoweringMachineRuntime open Turing MachineComposition PCP PoweringMachineGlobal variable {vertices d : Nat} def beforeFinish (graph : PortTables.Table vertices d) (n : Nat) : Tape n → List Bool := PoweringMachineOuterLoop.finalTapes graph n (placement n) (outputTape n) vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n) def finishConfiguration (graph : PortTables.Table vertices d) (n : Nat) : (machine d n).Cfg := ⟨some (finishEntry d n), PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n), beforeFinish graph n⟩ def beforeFinishBudget (graph : PortTables.Table vertices d) (n : Nat) : Nat := (3 * vertices + 5) + (vertices * (PoweringMachineVertex.budget d n (PortTables.tableBits graph).length + 2) + 1) theorem beforeFinish_ready (graph : PortTables.Table vertices d) (n : Nat) : PoweringMachineOuterLoop.Ready graph n (placement n) (beforeFinish graph n) := PoweringMachineOuterLoop.finalTapes_ready graph n (placement n) (PoweringMachineInitialize.commonPlacement_injective _) (outputTape n) (fun i => Ne.symm (PoweringMachineInitialize.commonPlacement_ne_finalOutput _ i)) vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n) (PoweringInitializeFrame.prepared_ready graph n) theorem beforeFinish_output (graph : PortTables.Table vertices d) (n : Nat) : beforeFinish graph n (outputTape n) = (List.finRange vertices).flatMap (PoweringTableLayout.vertexRowBits graph n) := by rw [beforeFinish, PoweringMachineOuterLoop.finalTapes_output graph n (placement n) (PoweringMachineInitialize.commonPlacement_injective _) (outputTape n) (fun i => Ne.symm (PoweringMachineInitialize.commonPlacement_ne_finalOutput _ i)) vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n) (PoweringInitializeFrame.prepared_ready graph n), PoweringMachineOuterLoop.prefixRows_all, PoweringInitializeFrame.prepared_output, List.append_nil] theorem beforeFinish_vertices (graph : PortTables.Table vertices d) (n : Nat) : beforeFinish graph n (headerVertices n) = encodeWord vertices := by rw [beforeFinish, PoweringMachineOuterLoop.finalTapes_other graph n (placement n) (outputTape n) vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n) (headerVertices n) (PoweringGlobalConfiguration.headerVertices_ne_output n) (fun i => Ne.symm (PoweringMachineInitialize.commonPlacement_ne_header _ i .vertices (by decide)))] exact PoweringInitializeFrame.prepared_vertices graph n theorem beforeFinish_darts (graph : PortTables.Table vertices d) (n : Nat) : beforeFinish graph n (headerDarts n) = encodeWord (PoweringTableLayout.blockSize d n * vertices) := by rw [beforeFinish, PoweringMachineOuterLoop.finalTapes_other graph n (placement n) (outputTape n) vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n) (headerDarts n) (PoweringGlobalConfiguration.headerDarts_ne_output n) (fun i => Ne.symm (PoweringMachineInitialize.commonPlacement_ne_header _ i .darts (by decide)))] exact PoweringInitializeFrame.prepared_darts graph n theorem serialized_eq (graph : PortTables.Table vertices d) (n : Nat) : encodeWords [vertices, PoweringTableLayout.blockSize d n * vertices] ++ beforeFinish graph n (outputTape n) = PoweringTables.outputBits graph n := by rw [beforeFinish_output, PoweringTableLayout.outputBits_vertexRows] congr 2 rw [PoweringTableLayout.dartCount_eq_vertex_blocks, Nat.mul_comm] def beforeFinishExecution (graph : PortTables.Table vertices d) (n : Nat) : StateTransition.EvalsToInTime (machine d n).step (initList (machine d n) (PortTables.tableBits graph)) (some (finishConfiguration graph n)) (beforeFinishBudget graph n) := by have initialRun := PoweringInitializeFrame.initializeInTime graph n have loop := PoweringMachineOuterLoop.loopInTime graph n (placement n) (PoweringMachineInitialize.commonPlacement_injective _) (outputTape n) (fun i => Ne.symm (PoweringMachineInitialize.commonPlacement_ne_finalOutput _ i)) (guardLabel d n) (bridgeLabel d n) (finishEntry d n) (vertexLabels d n) (program d n) (atGuard d n) (atBridge d n) (atVertex d n) vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n) (PoweringInitializeFrame.prepared_ready graph n) rw [PoweringInitializeFrame.prepared_counter] at loop simpa only [beforeFinishBudget, Nat.add_comm] using (StateTransition.EvalsToInTime.trans (machine d n).step (3 * vertices + 5) (vertices * (PoweringMachineVertex.budget d n (PortTables.tableBits graph).length + 2) + 1) (initList (machine d n) (PortTables.tableBits graph)) (PoweringInitializeFrame.preparedCfg graph n) (some (finishConfiguration graph n)) initialRun loop) def finishSteps (graph : PortTables.Table vertices d) (n : Nat) : Nat := PoweringMachineFinish.steps (enumeration n) (outputTape n) (beforeFinish graph n) vertices (PoweringTableLayout.blockSize d n * vertices) def finishExecution (graph : PortTables.Table vertices d) (n : Nat) : StateTransition.EvalsToInTime (machine d n).step (finishConfiguration graph n) (some (haltList (machine d n) (PoweringTables.outputBits graph n))) (finishSteps graph n) := by have run := PoweringMachineFinish.traceAt (enumeration n) (headerVertices n) (headerDarts n) (outputTape n) (placement n (PoweringMachineTapes.scratch (PoweringMachineRowBody.capacity n))) (PoweringGlobalConfiguration.headerVertices_ne_scratch n) (PoweringGlobalConfiguration.headerVertices_ne_output n) (PoweringGlobalConfiguration.headerDarts_ne_scratch n) (PoweringGlobalConfiguration.headerDarts_ne_output n) (PoweringMachineInitialize.commonPlacement_ne_finalOutput _ _) (finishLabels d n) (program d n) (atFinish d n) (beforeFinish graph n) vertices (PoweringTableLayout.blockSize d n * vertices) [] [] (by simpa only [List.append_nil] using beforeFinish_vertices graph n) (by simpa only [List.append_nil] using beforeFinish_darts graph n) (beforeFinish_ready graph n).scratch (PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n)).1 none rw [serialized_eq] at run refine { steps := finishSteps graph n, evals_in_steps := ?_, steps_le_m := Nat.le_refl _ } change (advance (TM2.step (program d n)))^[finishSteps graph n] (some (finishConfiguration graph n)) = _ rw [PoweringGlobalConfiguration.haltList_eq] simpa only [finishConfiguration, finishEntry, finishSteps, PoweringMasterState.clean, PoweringMasterState.withBuffer] using! run def execution (graph : PortTables.Table vertices d) (n : Nat) : TM2OutputsInTime (machine d n) (PortTables.tableBits graph) (some (PoweringTables.outputBits graph n)) (beforeFinishBudget graph n + finishSteps graph n) := by simpa only [TM2OutputsInTime, Option.map_some, Nat.add_comm] using! (StateTransition.EvalsToInTime.trans (machine d n).step (beforeFinishBudget graph n) (finishSteps graph n) (initList (machine d n) (PortTables.tableBits graph)) (finishConfiguration graph n) (some (haltList (machine d n) (PoweringTables.outputBits graph n))) (beforeFinishExecution graph n) (finishExecution graph n)) theorem finishSteps_le (graph : PortTables.Table vertices d) (n : Nat) : finishSteps graph n ≤ 2 * (vertices + PoweringTableLayout.blockSize d n * vertices) + 6 + PoweringRuntimeBudget.tapeCount n * ((PortTables.tableBits graph).length + beforeFinishBudget graph n * Runtime.programPushBound (machine d n) + vertices + PoweringTableLayout.blockSize d n * vertices + 3) := PoweringRuntimeBudget.finish_steps_le_of_execution d n (PortTables.tableBits graph) (finishConfiguration graph n) (beforeFinishBudget graph n) vertices (PoweringTableLayout.blockSize d n * vertices) (beforeFinishExecution graph n) theorem execution_budget_le (graph : PortTables.Table vertices d) (n : Nat) : beforeFinishBudget graph n + finishSteps graph n ≤ (PoweringPolynomialBudget.time d n (Runtime.programPushBound (machine d n)) (PoweringRuntimeBudget.tapeCount n)).eval (PortTables.tableBits graph).length := by exact PoweringPolynomialBudget.execution_budget_le_time d n (Runtime.programPushBound (machine d n)) (PoweringRuntimeBudget.tapeCount n) vertices (PortTables.tableBits graph).length (PortTables.vertices_le_tableBits_length graph) (Nat.le_refl _) (finishSteps_le graph n) noncomputable def computableInPolyTime (d n : Nat) : TM2ComputableInPolyTime (@PortTables.inputBits d) GenericGraphTables.tableBits (PoweringTables.transform d n) where tm := machine d n inputAlphabet := Equiv.refl Bool outputAlphabet := Equiv.refl Bool time := PoweringPolynomialBudget.time d n (Runtime.programPushBound (machine d n)) (PoweringRuntimeBudget.tapeCount n) outputsFun input := by rcases input with ⟨vertices, graph⟩ have run := execution graph n let bounded : TM2OutputsInTime (machine d n) (PortTables.tableBits graph) (some (PoweringTables.outputBits graph n)) ((PoweringPolynomialBudget.time d n (Runtime.programPushBound (machine d n)) (PoweringRuntimeBudget.tapeCount n)).eval (PortTables.tableBits graph).length) := ⟨run.toEvalsTo, run.steps_le_m.trans (execution_budget_le graph n)⟩ change TM2OutputsInTime (machine d n) ((PortTables.tableBits graph).map id) (some ((PoweringTables.outputBits graph n).map id)) _ have input_eq : @List.map ((machine d n).Γ (machine d n).k₀) ((machine d n).Γ (machine d n).k₀) id (PortTables.tableBits graph) = PortTables.tableBits graph := List.map_id _ have output_eq : @List.map ((machine d n).Γ (machine d n).k₁) ((machine d n).Γ (machine d n).k₁) id (PoweringTables.outputBits graph n) = PoweringTables.outputBits graph n := List.map_id _ simpa only [input_eq, output_eq, PortTables.inputBits] using bounded theorem computableInPolyTime_finite_alphabet (d n : Nat) : ∀ k, Finite ((computableInPolyTime d n).tm.Γ k) := by intro k change Finite Bool infer_instance end BinPackingGames.Foundations.Complexity.PoweringMachineRuntime end OAI