import Mathlib.Algebra.BigOperators.Group.Finset.Basic import Mathlib.Data.List.Range import OAI.Computability.BinPacking.Computation.MachineUnaryEqualityBit import OAI.Computability.BinPacking.Information.SourceFieldArray namespace OAI namespace BinPackingGames.Foundations.Complexity.MachineUnaryCounter open Turing variable {K Λ σ : Type} [DecidableEq K] abbrev Alphabet (_ : K) := Bool def guard (counter : K) (bodyLabel exitLabel : Λ) : TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool) := .peek counter (fun state head => (state.1, head)) (.branch (fun state => state.2.getD false) (.pop counter (fun state _ => (state.1, none)) (.goto fun _ => bodyLabel)) (.load (fun state => (state.1, none)) (.goto fun _ => exitLabel))) def counterTapes (counter : K) (base : K → List Bool) (n : Nat) (suffix : List Bool) : K → List Bool := Function.update base counter (encodeWord n ++ suffix) @[simp] theorem counterTapes_counter (counter : K) (base : K → List Bool) (n : Nat) (suffix : List Bool) : counterTapes counter base n suffix counter = encodeWord n ++ suffix := by simp [counterTapes] theorem counterTapes_other (counter other : K) (hne : other ≠ counter) (base : K → List Bool) (n : Nat) (suffix : List Bool) : counterTapes counter base n suffix other = base other := by simp [counterTapes, hne] omit [DecidableEq K] in theorem guardPushBound (counter : K) (bodyLabel exitLabel : Λ) : Runtime.statementPushBound (guard (σ := σ) counter bodyLabel exitLabel) = 0 := by rfl theorem stepAux_succ (counter : K) (bodyLabel exitLabel : Λ) (base : K → List Bool) (n : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : TM2.stepAux (guard counter bodyLabel exitLabel) (ambient, register) (counterTapes counter base (n + 1) suffix) = ⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩ := by simp [guard, TM2.stepAux, counterTapes, encodeWord, List.replicate_succ] theorem stepAux_zero (counter : K) (bodyLabel exitLabel : Λ) (base : K → List Bool) (suffix : List Bool) (ambient : σ) (register : Option Bool) : TM2.stepAux (guard counter bodyLabel exitLabel) (ambient, register) (counterTapes counter base 0 suffix) = ⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩ := by simp [guard, TM2.stepAux, counterTapes, encodeWord] theorem guardStep_succ (counter : K) (guardLabel bodyLabel exitLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atGuard : program guardLabel = guard counter bodyLabel exitLabel) (base : K → List Bool) (n : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : TM2.step program ⟨some guardLabel, (ambient, register), counterTapes counter base (n + 1) suffix⟩ = some ⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩ := by change some (TM2.stepAux (program guardLabel) (ambient, register) (counterTapes counter base (n + 1) suffix)) = _ rw [atGuard, stepAux_succ] theorem guardStep_zero (counter : K) (guardLabel bodyLabel exitLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atGuard : program guardLabel = guard counter bodyLabel exitLabel) (base : K → List Bool) (suffix : List Bool) (ambient : σ) (register : Option Bool) : TM2.step program ⟨some guardLabel, (ambient, register), counterTapes counter base 0 suffix⟩ = some ⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩ := by change some (TM2.stepAux (program guardLabel) (ambient, register) (counterTapes counter base 0 suffix)) = _ rw [atGuard, stepAux_zero] theorem guardTrace_succ (counter : K) (guardLabel bodyLabel exitLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atGuard : program guardLabel = guard counter bodyLabel exitLabel) (base : K → List Bool) (n : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step program))^[1] (some ⟨some guardLabel, (ambient, register), counterTapes counter base (n + 1) suffix⟩) = some ⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩ := by simpa only [Function.iterate_one, MachineComposition.advance_some] using guardStep_succ counter guardLabel bodyLabel exitLabel program atGuard base n suffix ambient register theorem guardTrace_zero (counter : K) (guardLabel bodyLabel exitLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atGuard : program guardLabel = guard counter bodyLabel exitLabel) (base : K → List Bool) (suffix : List Bool) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step program))^[1] (some ⟨some guardLabel, (ambient, register), counterTapes counter base 0 suffix⟩) = some ⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩ := by simpa only [Function.iterate_one, MachineComposition.advance_some] using guardStep_zero counter guardLabel bodyLabel exitLabel program atGuard base suffix ambient register def guardInTime_succ (counter : K) (guardLabel bodyLabel exitLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atGuard : program guardLabel = guard counter bodyLabel exitLabel) (base : K → List Bool) (n : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some guardLabel, (ambient, register), counterTapes counter base (n + 1) suffix⟩ (some ⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩) 1 where steps := 1 evals_in_steps := by change (MachineComposition.advance (TM2.step program))^[1] _ = _ exact guardTrace_succ counter guardLabel bodyLabel exitLabel program atGuard base n suffix ambient register steps_le_m := Nat.le_refl _ def guardInTime_zero (counter : K) (guardLabel bodyLabel exitLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atGuard : program guardLabel = guard counter bodyLabel exitLabel) (base : K → List Bool) (suffix : List Bool) (ambient : σ) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some guardLabel, (ambient, register), counterTapes counter base 0 suffix⟩ (some ⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩) 1 where steps := 1 evals_in_steps := by change (MachineComposition.advance (TM2.step program))^[1] _ = _ exact guardTrace_zero counter guardLabel bodyLabel exitLabel program atGuard base suffix ambient register steps_le_m := Nat.le_refl _ end BinPackingGames.Foundations.Complexity.MachineUnaryCounter namespace BinPackingGames.Foundations.Complexity.MachineCountedLoop open Turing open scoped BigOperators open MachineUnaryCounter variable {K Λ σ : Type} [DecidableEq K] def guardConfiguration (counter : K) (guardLabel : Λ) (suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool) (base : Nat → K → List Bool) (n : Nat) : TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) := ⟨some guardLabel, (ambient n, register n), counterTapes counter (base n) n suffix⟩ def bodyConfiguration (counter : K) (bodyLabel : Λ) (suffix : List Bool) (ambient : Nat → σ) (base : Nat → K → List Bool) (n : Nat) : TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) := ⟨some bodyLabel, (ambient (n + 1), none), counterTapes counter (base (n + 1)) n suffix⟩ def exitConfiguration (counter : K) (exitLabel : Λ) (suffix : List Bool) (ambient : Nat → σ) (base : Nat → K → List Bool) : TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) := ⟨some exitLabel, (ambient 0, none), counterTapes counter (base 0) 0 suffix⟩ def BodyTraces (counter : K) (guardLabel bodyLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool) (base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat) : Prop := ∀ r, r < n → (MachineComposition.advance (TM2.step program))^[cost r] (some (bodyConfiguration counter bodyLabel suffix ambient base r)) = some (guardConfiguration counter guardLabel suffix ambient register base r) def totalSteps (cost : Nat → Nat) (n : Nat) : Nat := (∑ r ∈ Finset.range n, cost r) + n + 1 @[simp] theorem totalSteps_zero (cost : Nat → Nat) : totalSteps cost 0 = 1 := by simp [totalSteps] theorem totalSteps_succ (cost : Nat → Nat) (n : Nat) : totalSteps cost (n + 1) = (totalSteps cost n + cost n) + 1 := by simp only [totalSteps, Finset.sum_range_succ] omega theorem loopTrace (counter : K) (guardLabel bodyLabel exitLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atGuard : program guardLabel = guard counter bodyLabel exitLabel) (suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool) (base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat) (bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix ambient register base cost n) : (MachineComposition.advance (TM2.step program))^[totalSteps cost n] (some (guardConfiguration counter guardLabel suffix ambient register base n)) = some (exitConfiguration counter exitLabel suffix ambient base) := by revert bodyTraces induction n with | zero => intro _ simpa only [totalSteps_zero, guardConfiguration, exitConfiguration] using guardTrace_zero counter guardLabel bodyLabel exitLabel program atGuard (base 0) suffix (ambient 0) (register 0) | succ n ih => intro bodyTraces rw [totalSteps_succ, Function.iterate_succ_apply] change (MachineComposition.advance (TM2.step program))^[totalSteps cost n + cost n] (TM2.step program ⟨some guardLabel, (ambient (n + 1), register (n + 1)), counterTapes counter (base (n + 1)) (n + 1) suffix⟩) = _ rw [guardStep_succ counter guardLabel bodyLabel exitLabel program atGuard (base (n + 1)) n suffix (ambient (n + 1)) (register (n + 1)), Function.iterate_add_apply] change (MachineComposition.advance (TM2.step program))^[totalSteps cost n] ((MachineComposition.advance (TM2.step program))^[cost n] (some (bodyConfiguration counter bodyLabel suffix ambient base n))) = _ rw [bodyTraces n (Nat.lt_succ_self n)] exact ih (fun r hr => bodyTraces r (Nat.lt_trans hr (Nat.lt_succ_self n))) theorem totalSteps_le (cost : Nat → Nat) (n B : Nat) (bodyBound : ∀ r, r < n → cost r ≤ B) : totalSteps cost n ≤ n * (B + 1) + 1 := by have hsum : (∑ r ∈ Finset.range n, cost r) ≤ n * B := by calc (∑ r ∈ Finset.range n, cost r) ≤ ∑ _r ∈ Finset.range n, B := Finset.sum_le_sum (fun r hr => bodyBound r (Finset.mem_range.mp hr)) _ = n * B := by simp simpa only [totalSteps, Nat.mul_add, Nat.mul_one] using Nat.add_le_add_right (Nat.add_le_add_right hsum n) 1 def loopInTime (counter : K) (guardLabel bodyLabel exitLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atGuard : program guardLabel = guard counter bodyLabel exitLabel) (suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool) (base : Nat → K → List Bool) (cost : Nat → Nat) (n B : Nat) (bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix ambient register base cost n) (bodyBound : ∀ r, r < n → cost r ≤ B) : StateTransition.EvalsToInTime (TM2.step program) (guardConfiguration counter guardLabel suffix ambient register base n) (some (exitConfiguration counter exitLabel suffix ambient base)) (n * (B + 1) + 1) where steps := totalSteps cost n evals_in_steps := by change (MachineComposition.advance (TM2.step program))^[totalSteps cost n] _ = _ exact loopTrace counter guardLabel bodyLabel exitLabel program atGuard suffix ambient register base cost n bodyTraces steps_le_m := totalSteps_le cost n B bodyBound omit [DecidableEq K] in theorem baseFrame (base : Nat → K → List Bool) (frame : K → Prop) (n : Nat) (bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) : ∀ k, frame k → base n k = base 0 k := by revert bodyFrame induction n with | zero => intro _ k _; rfl | succ n ih => intro bodyFrame k hk exact (bodyFrame n (Nat.lt_succ_self n) k hk).trans (ih (fun r hr => bodyFrame r (Nat.lt_trans hr (Nat.lt_succ_self n))) k hk) theorem counterFrame (counter : K) (suffix : List Bool) (base : Nat → K → List Bool) (frame : K → Prop) (n : Nat) (counterOutside : ∀ k, frame k → k ≠ counter) (bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) : ∀ k, frame k → counterTapes counter (base n) n suffix k = counterTapes counter (base 0) 0 suffix k := by intro k hk rw [counterTapes_other counter k (counterOutside k hk), counterTapes_other counter k (counterOutside k hk)] exact baseFrame base frame n bodyFrame k hk theorem loopTrace_framed (counter : K) (guardLabel bodyLabel exitLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atGuard : program guardLabel = guard counter bodyLabel exitLabel) (suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool) (base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat) (bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix ambient register base cost n) (frame : K → Prop) (counterOutside : ∀ k, frame k → k ≠ counter) (bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) : ((MachineComposition.advance (TM2.step program))^[totalSteps cost n] (some (guardConfiguration counter guardLabel suffix ambient register base n)) = some (exitConfiguration counter exitLabel suffix ambient base)) ∧ (∀ k, frame k → (guardConfiguration counter guardLabel suffix ambient register base n).stk k = (exitConfiguration counter exitLabel suffix ambient base).stk k) := ⟨loopTrace counter guardLabel bodyLabel exitLabel program atGuard suffix ambient register base cost n bodyTraces, counterFrame counter suffix base frame n counterOutside bodyFrame⟩ end BinPackingGames.Foundations.Complexity.MachineCountedLoop namespace BinPackingGames.Foundations.Complexity.MachineLookup open Turing variable {K Λ σ : Type} [DecidableEq K] abbrev Alphabet (_ : K) := Bool def discard (source : K) (loopLabel returnLabel : Λ) : TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool) := .pop source (fun state head => (state.1, head)) (.branch (fun state => state.2.getD false) (.goto fun _ => loopLabel) (.load (fun state => (state.1, none)) (.goto fun _ => returnLabel))) def select (source destination : K) (copyLabel rejected : Λ) : TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool) := .peek source (fun state head => (state.1, head)) (.branch (fun state => state.2.isSome) (Hastad.SourceMachine.fieldStart destination copyLabel) (.load (fun state => (state.1, none)) (.goto fun _ => rejected))) inductive Label | guard | skip | select | copy | accepted | rejected deriving DecidableEq protected abbrev Label.enumList : List Label := [.guard, .skip, .select, .copy, .accepted, .rejected] protected theorem Label.enumList_getElem?_ctorIdx_eq (x : Label) : Label.enumList[x.ctorIdx]? = some x := by cases x <;> rfl protected theorem Label.enumList_nodup : Label.enumList.Nodup := by decide instance : Fintype Label where elems := ⟨Label.enumList, Label.enumList_nodup⟩ complete x := by cases x <;> decide def program (index source destination : K) : Label → TM2.Stmt (Alphabet (K := K)) Label (σ × Option Bool) | .guard => MachineUnaryCounter.guard index .skip .select | .skip => discard source .skip .guard | .select => select source destination .copy .rejected | .copy => Hastad.SourceMachine.fieldLoop source destination .copy (some .accepted) | .accepted => .halt | .rejected => .halt def tapes (index source destination : K) (base : K → List Bool) (counter input output : List Bool) : K → List Bool := Function.update (Function.update (Function.update base index counter) source input) destination output @[simp] theorem tapes_index (index source destination : K) (his : index ≠ source) (hid : index ≠ destination) (base : K → List Bool) (counter input output : List Bool) : tapes index source destination base counter input output index = counter := by simp [tapes, his, hid] @[simp] theorem tapes_source (index source destination : K) (hsd : source ≠ destination) (base : K → List Bool) (counter input output : List Bool) : tapes index source destination base counter input output source = input := by simp [tapes, hsd] @[simp] theorem tapes_destination (index source destination : K) (base : K → List Bool) (counter input output : List Bool) : tapes index source destination base counter input output destination = output := by simp [tapes] theorem tapes_other (index source destination p : K) (hi : p ≠ index) (hs : p ≠ source) (hd : p ≠ destination) (base : K → List Bool) (counter input output : List Bool) : tapes index source destination base counter input output p = base p := by simp [tapes, hi, hs, hd] theorem update_index (index source destination : K) (his : index ≠ source) (hid : index ≠ destination) (base : K → List Bool) (counter input output replacement : List Bool) : Function.update (tapes index source destination base counter input output) index replacement = tapes index source destination base replacement input output := by funext p by_cases hi : p = index · subst p; simp [tapes, his, hid] · by_cases hs : p = source · subst p; by_cases hs : source = destination <;> simp [tapes, hi, hs, Ne.symm hid] · by_cases hd : p = destination · subst p; simp [tapes, hi] · simp [tapes, hi, hs, hd] theorem update_source (index source destination : K) (hsd : source ≠ destination) (base : K → List Bool) (counter input output replacement : List Bool) : Function.update (tapes index source destination base counter input output) source replacement = tapes index source destination base counter replacement output := by funext p by_cases hs : p = source · subst p; simp [tapes, hsd] · by_cases hd : p = destination · subst p; simp [tapes, hs] · simp [tapes, hs, hd] theorem update_destination (index source destination : K) (base : K → List Bool) (counter input output replacement : List Bool) : Function.update (tapes index source destination base counter input output) destination replacement = tapes index source destination base counter input replacement := by simp [tapes] theorem fieldTapes_eq (index source destination : K) (hsd : source ≠ destination) (base : K → List Bool) (counter input output input' output' : List Bool) : Hastad.SourceMachine.fieldTapes source destination (tapes index source destination base counter input output) input' output' = tapes index source destination base counter input' output' := by rw [Hastad.SourceMachine.fieldTapes, update_source _ _ _ hsd, update_destination] def machine : FinTM2 where K := Fin 3 k₀ := 1 k₁ := 2 Γ _ := Bool Λ := Label main := .guard σ := Unit × Option Bool initialState := ((), none) m := program 0 1 2 end BinPackingGames.Foundations.Complexity.MachineLookup namespace BinPackingGames.Foundations.Complexity.PoweringMachineLoop open Turing inductive HeaderTape | source | counter | vertices | darts deriving DecidableEq protected abbrev HeaderTape.enumList : List HeaderTape := [.source, .counter, .vertices, .darts] protected theorem HeaderTape.enumList_getElem?_ctorIdx_eq (x : HeaderTape) : HeaderTape.enumList[x.ctorIdx]? = some x := by cases x <;> rfl protected theorem HeaderTape.enumList_nodup : HeaderTape.enumList.Nodup := by decide instance : Fintype HeaderTape where elems := ⟨HeaderTape.enumList, HeaderTape.enumList_nodup⟩ complete x := by cases x <;> decide inductive HeaderLabel | initialize | scan deriving DecidableEq protected abbrev HeaderLabel.enumList : List HeaderLabel := [.initialize, .scan] protected theorem HeaderLabel.enumList_getElem?_ctorIdx_eq (x : HeaderLabel) : HeaderLabel.enumList[x.ctorIdx]? = some x := by cases x <;> rfl protected theorem HeaderLabel.enumList_nodup : HeaderLabel.enumList.Nodup := by decide instance : Fintype HeaderLabel where elems := ⟨HeaderLabel.enumList, HeaderLabel.enumList_nodup⟩ complete x := by cases x <;> decide abbrev HeaderAlphabet (_ : HeaderTape) := Bool abbrev HeaderState (σ : Type) := σ × Option Bool def pushTrue {K Λ σ : Type} (destination : K) : Nat → TM2.Stmt (fun _ : K => Bool) Λ σ → TM2.Stmt (fun _ : K => Bool) Λ σ | 0, next => next | count + 1, next => pushTrue destination count (.push destination (fun _ => true) next) theorem stepAux_pushTrue {K Λ σ : Type} [DecidableEq K] (destination : K) (count : Nat) (next : TM2.Stmt (fun _ : K => Bool) Λ σ) (state : σ) (tapes : K → List Bool) : TM2.stepAux (pushTrue destination count next) state tapes = TM2.stepAux next state (Function.update tapes destination (List.replicate count true ++ tapes destination)) := by induction count generalizing next with | zero => simp [pushTrue] | succ count ih => rw [pushTrue, ih] simp [TM2.stepAux, List.replicate_succ, Function.update_idem] theorem pushTrue_pushBound {K Λ σ : Type} (destination : K) (count : Nat) (next : TM2.Stmt (fun _ : K => Bool) Λ σ) : Runtime.statementPushBound (pushTrue destination count next) = count + Runtime.statementPushBound next := by induction count generalizing next with | zero => simp [pushTrue] | succ count ih => simp only [pushTrue, ih, Runtime.statementPushBound]; omega def headerProgram {σ : Type} (B : Nat) : HeaderLabel → TM2.Stmt HeaderAlphabet HeaderLabel (HeaderState σ) | .initialize => .push .counter (fun _ => false) (.push .vertices (fun _ => false) (.push .darts (fun _ => false) (.load (fun state => (state.1, none)) (.goto fun _ => .scan)))) | .scan => .pop .source (fun state head => (state.1, head)) (.branch (fun state => state.2.getD false) (.push .counter (fun _ => true) (.push .vertices (fun _ => true) (pushTrue .darts B (.load (fun state => (state.1, none)) (.goto fun _ => .scan))))) (.load (fun state => (state.1, none)) .halt)) def headerMachine (B : Nat) : FinTM2 where K := HeaderTape k₀ := .source k₁ := .darts Γ := HeaderAlphabet Λ := HeaderLabel main := .initialize σ := HeaderState Unit initialState := ((), none) m := headerProgram B def initialTapes (n : Nat) (suffix : List Bool) : HeaderTape → List Bool | .source => encodeWord n ++ suffix | _ => [] def scanTapes (B remaining processed : Nat) (suffix : List Bool) : HeaderTape → List Bool | .source => encodeWord remaining ++ suffix | .counter => encodeWord processed | .vertices => encodeWord processed | .darts => encodeWord (B * processed) def finalTapes (B n : Nat) (suffix : List Bool) : HeaderTape → List Bool | .source => suffix | .counter => encodeWord n | .vertices => encodeWord n | .darts => encodeWord (B * n) def scanConfiguration {σ : Type} (B remaining processed : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : TM2.Cfg HeaderAlphabet HeaderLabel (HeaderState σ) := ⟨some .scan, (ambient, register), scanTapes B remaining processed suffix⟩ def finalConfiguration {σ : Type} (B n : Nat) (suffix : List Bool) (ambient : σ) : TM2.Cfg HeaderAlphabet HeaderLabel (HeaderState σ) := ⟨none, (ambient, none), finalTapes B n suffix⟩ theorem initializeStep {σ : Type} (B n : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : TM2.step (headerProgram B) ⟨some .initialize, (ambient, register), initialTapes n suffix⟩ = some (scanConfiguration B n 0 suffix ambient none) := by simp only [TM2.step, headerProgram, TM2.stepAux] congr 2 funext tape cases tape <;> simp [initialTapes, scanTapes, encodeWord] theorem scanStep_zero {σ : Type} (B processed : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : TM2.step (headerProgram B) (scanConfiguration B 0 processed suffix ambient register) = some (finalConfiguration B processed suffix ambient) := by simp only [TM2.step, scanConfiguration, headerProgram, TM2.stepAux, scanTapes, encodeWord, List.replicate_zero, List.nil_append, List.singleton_append, List.head?_cons, List.tail_cons, Option.getD_some, Bool.cond_false] congr 2 funext tape cases tape <;> simp [finalTapes, scanTapes] theorem scanStep_succ {σ : Type} (B remaining processed : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : TM2.step (headerProgram B) (scanConfiguration B (remaining + 1) processed suffix ambient register) = some (scanConfiguration B remaining (processed + 1) suffix ambient none) := by simp only [TM2.step, scanConfiguration, headerProgram, TM2.stepAux, scanTapes, encodeWord, List.replicate_succ, List.cons_append, List.head?_cons, List.tail_cons, Option.getD_some, Bool.cond_true] rw [stepAux_pushTrue] simp only [TM2.stepAux] congr 2 funext tape cases tape <;> simp [scanTapes, encodeWord, Nat.mul_add, List.replicate_add, List.replicate_succ, List.append_assoc, Nat.add_comm, -List.replicate_append_replicate] theorem scanTrace {σ : Type} (B remaining processed : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step (headerProgram B)))^[remaining + 1] (some (scanConfiguration B remaining processed suffix ambient register)) = some (finalConfiguration B (processed + remaining) suffix ambient) := by induction remaining generalizing processed register with | zero => simpa only [Nat.add_zero, Nat.zero_add, Function.iterate_one, MachineComposition.advance_some] using scanStep_zero B processed suffix ambient register | succ remaining ih => rw [Function.iterate_succ_apply] change (MachineComposition.advance (TM2.step (headerProgram B)))^[remaining + 1] (TM2.step (headerProgram B) (scanConfiguration B (remaining + 1) processed suffix ambient register)) = _ rw [scanStep_succ, ih] simp only [Nat.add_comm, Nat.add_left_comm] theorem headerTrace {σ : Type} (B n : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step (headerProgram B)))^[n + 2] (some ⟨some .initialize, (ambient, register), initialTapes n suffix⟩) = some (finalConfiguration B n suffix ambient) := by rw [show n + 2 = (n + 1) + 1 by omega, Function.iterate_succ_apply] change (MachineComposition.advance (TM2.step (headerProgram B)))^[n + 1] (TM2.step (headerProgram B) ⟨some .initialize, (ambient, register), initialTapes n suffix⟩) = _ rw [initializeStep] simpa only [Nat.zero_add] using scanTrace B n 0 suffix ambient none def headerInTime {σ : Type} (B n : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step (headerProgram B)) ⟨some .initialize, (ambient, register), initialTapes n suffix⟩ (some (finalConfiguration B n suffix ambient)) (n + 2) where steps := n + 2 evals_in_steps := headerTrace B n suffix ambient register steps_le_m := Nat.le_refl _ def remainingRows (rowBlock : Nat → List Bool) (n r : Nat) : List Bool := (List.range' r (n - r)).flatMap rowBlock @[simp] theorem remainingRows_top (rowBlock : Nat → List Bool) (n : Nat) : remainingRows rowBlock n n = [] := by simp [remainingRows] theorem remainingRows_step (rowBlock : Nat → List Bool) (n r : Nat) (hr : r < n) : remainingRows rowBlock n r = rowBlock r ++ remainingRows rowBlock n (r + 1) := by have h : n - r = (n - (r + 1)) + 1 := by omega simp only [remainingRows, h, List.range'_succ, List.flatMap_cons] variable {K Λ σ : Type} [DecidableEq K] def rowBase (output : K) (base : K → List Bool) (rowBlock : Nat → List Bool) (n r : Nat) : K → List Bool := Function.update base output (remainingRows rowBlock n r ++ base output) def VertexBodyTraces (counter output : K) (guardLabel bodyLabel : Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (σ × Option Bool)) (base : K → List Bool) (suffix : List Bool) (ambient : σ) (rowBlock : Nat → List Bool) (cost : Nat → Nat) (n : Nat) : Prop := ∀ r, r < n → ∀ accumulator : List Bool, (MachineComposition.advance (TM2.step program))^[cost r] (some ⟨some bodyLabel, (ambient, none), MachineUnaryCounter.counterTapes counter (Function.update base output accumulator) r suffix⟩) = some ⟨some guardLabel, (ambient, none), MachineUnaryCounter.counterTapes counter (Function.update base output (rowBlock r ++ accumulator)) r suffix⟩ theorem vertexBodyTraces_to_counted (counter output : K) (guardLabel bodyLabel : Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (σ × Option Bool)) (base : K → List Bool) (suffix : List Bool) (ambient : σ) (rowBlock : Nat → List Bool) (cost : Nat → Nat) (n : Nat) (bodies : VertexBodyTraces counter output guardLabel bodyLabel program base suffix ambient rowBlock cost n) : MachineCountedLoop.BodyTraces counter guardLabel bodyLabel program suffix (fun _ => ambient) (fun _ => none) (rowBase output base rowBlock n) cost n := by intro r hr have h := bodies r hr (remainingRows rowBlock n (r + 1) ++ base output) simpa only [MachineCountedLoop.bodyConfiguration, MachineCountedLoop.guardConfiguration, rowBase, remainingRows_step rowBlock n r hr, List.append_assoc] using h def vertexLoopInTime (counter output : K) (guardLabel bodyLabel exitLabel : Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (σ × Option Bool)) (atGuard : program guardLabel = MachineUnaryCounter.guard counter bodyLabel exitLabel) (base : K → List Bool) (suffix : List Bool) (ambient : σ) (rowBlock : Nat → List Bool) (cost : Nat → Nat) (n bodyBound : Nat) (bodies : VertexBodyTraces counter output guardLabel bodyLabel program base suffix ambient rowBlock cost n) (bounded : ∀ r, r < n → cost r ≤ bodyBound) : StateTransition.EvalsToInTime (TM2.step program) ⟨some guardLabel, (ambient, none), MachineUnaryCounter.counterTapes counter base n suffix⟩ (some ⟨some exitLabel, (ambient, none), MachineUnaryCounter.counterTapes counter (rowBase output base rowBlock n 0) 0 suffix⟩) (n * (bodyBound + 1) + 1) := by have h := MachineCountedLoop.loopInTime counter guardLabel bodyLabel exitLabel program atGuard suffix (fun _ => ambient) (fun _ => none) (rowBase output base rowBlock n) cost n bodyBound (vertexBodyTraces_to_counted counter output guardLabel bodyLabel program base suffix ambient rowBlock cost n bodies) bounded simpa [MachineCountedLoop.guardConfiguration, MachineCountedLoop.exitConfiguration, rowBase] using h theorem vertexLoop_output (counter output : K) (different : output ≠ counter) (base : K → List Bool) (suffix : List Bool) (rowBlock : Nat → List Bool) (n : Nat) : MachineUnaryCounter.counterTapes counter (rowBase output base rowBlock n 0) 0 suffix output = remainingRows rowBlock n 0 ++ base output := by simp [MachineUnaryCounter.counterTapes, rowBase, different] def varyingRowBase (output : K) (base : Nat → K → List Bool) (rowBlock : Nat → List Bool) (n r : Nat) : K → List Bool := Function.update (base r) output (remainingRows rowBlock n r ++ base n output) def VaryingVertexBodyTraces (counter output : K) (guardLabel bodyLabel : Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (σ × Option Bool)) (base : Nat → K → List Bool) (suffix : List Bool) (ambient : Nat → σ) (rowBlock : Nat → List Bool) (cost : Nat → Nat) (n : Nat) : Prop := ∀ r, r < n → ∀ accumulator : List Bool, (MachineComposition.advance (TM2.step program))^[cost r] (some ⟨some bodyLabel, (ambient (r + 1), none), MachineUnaryCounter.counterTapes counter (Function.update (base (r + 1)) output accumulator) r suffix⟩) = some ⟨some guardLabel, (ambient r, none), MachineUnaryCounter.counterTapes counter (Function.update (base r) output (rowBlock r ++ accumulator)) r suffix⟩ theorem varyingVertexBodyTraces_to_counted (counter output : K) (guardLabel bodyLabel : Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (σ × Option Bool)) (base : Nat → K → List Bool) (suffix : List Bool) (ambient : Nat → σ) (rowBlock : Nat → List Bool) (cost : Nat → Nat) (n : Nat) (bodies : VaryingVertexBodyTraces counter output guardLabel bodyLabel program base suffix ambient rowBlock cost n) : MachineCountedLoop.BodyTraces counter guardLabel bodyLabel program suffix ambient (fun _ => none) (varyingRowBase output base rowBlock n) cost n := by intro r hr have h := bodies r hr (remainingRows rowBlock n (r + 1) ++ base n output) simpa only [MachineCountedLoop.bodyConfiguration, MachineCountedLoop.guardConfiguration, varyingRowBase, remainingRows_step rowBlock n r hr, List.append_assoc] using h def vertexLoopInTime_varying (counter output : K) (guardLabel bodyLabel exitLabel : Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (σ × Option Bool)) (atGuard : program guardLabel = MachineUnaryCounter.guard counter bodyLabel exitLabel) (base : Nat → K → List Bool) (suffix : List Bool) (ambient : Nat → σ) (rowBlock : Nat → List Bool) (cost : Nat → Nat) (n bodyBound : Nat) (bodies : VaryingVertexBodyTraces counter output guardLabel bodyLabel program base suffix ambient rowBlock cost n) (bounded : ∀ r, r < n → cost r ≤ bodyBound) : StateTransition.EvalsToInTime (TM2.step program) ⟨some guardLabel, (ambient n, none), MachineUnaryCounter.counterTapes counter (base n) n suffix⟩ (some ⟨some exitLabel, (ambient 0, none), MachineUnaryCounter.counterTapes counter (varyingRowBase output base rowBlock n 0) 0 suffix⟩) (n * (bodyBound + 1) + 1) := by have h := MachineCountedLoop.loopInTime counter guardLabel bodyLabel exitLabel program atGuard suffix ambient (fun _ => none) (varyingRowBase output base rowBlock n) cost n bodyBound (varyingVertexBodyTraces_to_counted counter output guardLabel bodyLabel program base suffix ambient rowBlock cost n bodies) bounded simpa [MachineCountedLoop.guardConfiguration, MachineCountedLoop.exitConfiguration, varyingRowBase] using h theorem vertexLoop_varying_output (counter output : K) (different : output ≠ counter) (base : Nat → K → List Bool) (suffix : List Bool) (rowBlock : Nat → List Bool) (n : Nat) : MachineUnaryCounter.counterTapes counter (varyingRowBase output base rowBlock n 0) 0 suffix output = remainingRows rowBlock n 0 ++ base n output := by simp [MachineUnaryCounter.counterTapes, varyingRowBase, different] end BinPackingGames.Foundations.Complexity.PoweringMachineLoop namespace BinPackingGames.Foundations.Complexity.MachineLookup open Turing variable {K Λ σ : Type} [DecidableEq K] private theorem discard_update_twice (source : K) (base : K → List Bool) (input replacement : List Bool) : Function.update (Function.update base source input) source replacement = Function.update base source replacement := by funext p by_cases hp : p = source · subst p; simp · simp [hp] theorem discardStep_delimiter (source : K) (loopLabel returnLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atLoop : program loopLabel = discard source loopLabel returnLabel) (base : K → List Bool) (suffix : List Bool) (ambient : σ) (register : Option Bool) : TM2.step program ⟨some loopLabel, (ambient, register), Function.update base source (false :: suffix)⟩ = some ⟨some returnLabel, (ambient, none), Function.update base source suffix⟩ := by change some (TM2.stepAux (program loopLabel) (ambient, register) (Function.update base source (false :: suffix))) = _ rw [atLoop] simp [discard, TM2.stepAux] theorem discardStep_true (source : K) (loopLabel returnLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atLoop : program loopLabel = discard source loopLabel returnLabel) (base : K → List Bool) (input : List Bool) (ambient : σ) (register : Option Bool) : TM2.step program ⟨some loopLabel, (ambient, register), Function.update base source (true :: input)⟩ = some ⟨some loopLabel, (ambient, some true), Function.update base source input⟩ := by change some (TM2.stepAux (program loopLabel) (ambient, register) (Function.update base source (true :: input))) = _ rw [atLoop] simp [discard, TM2.stepAux] theorem discard_empty_step (source : K) (loopLabel returnLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atLoop : program loopLabel = discard source loopLabel returnLabel) (base : K → List Bool) (hinput : base source = []) (ambient : σ) (register : Option Bool) : TM2.step program ⟨some loopLabel, (ambient, register), base⟩ = some ⟨some returnLabel, (ambient, none), base⟩ := by have hupdate : Function.update base source [] = base := by funext p by_cases hp : p = source · subst p; simp [hinput] · simp [hp] change some (TM2.stepAux (program loopLabel) (ambient, register) base) = _ rw [atLoop] simp [discard, TM2.stepAux, hinput, hupdate] theorem discardTrace_update (source : K) (loopLabel returnLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atLoop : program loopLabel = discard source loopLabel returnLabel) (base : K → List Bool) (n : Nat) (suffix : List Bool) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step program))^[n + 1] (some ⟨some loopLabel, (ambient, register), Function.update base source (encodeWord n ++ suffix)⟩) = some ⟨some returnLabel, (ambient, none), Function.update base source suffix⟩ := by induction n generalizing register with | zero => simpa only [Nat.zero_add, Function.iterate_one, MachineComposition.advance_some, encodeWord, List.replicate_zero, List.nil_append, List.singleton_append] using discardStep_delimiter source loopLabel returnLabel program atLoop base suffix ambient register | succ n ih => rw [Function.iterate_succ_apply] simp only [encodeWord, List.replicate_succ, List.cons_append] change (MachineComposition.advance (TM2.step program))^[n + 1] (TM2.step program ⟨some loopLabel, (ambient, register), Function.update base source (true :: (encodeWord n ++ suffix))⟩) = _ rw [discardStep_true source loopLabel returnLabel program atLoop, ih] theorem discardTrace (source : K) (loopLabel returnLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atLoop : program loopLabel = discard source loopLabel returnLabel) (base : K → List Bool) (n : Nat) (suffix : List Bool) (hinput : base source = encodeWord n ++ suffix) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step program))^[n + 1] (some ⟨some loopLabel, (ambient, register), base⟩) = some ⟨some returnLabel, (ambient, none), Function.update base source suffix⟩ := by have hbase : Function.update base source (encodeWord n ++ suffix) = base := by funext p by_cases hp : p = source · subst p; simp [hinput] · simp [hp] have h := discardTrace_update source loopLabel returnLabel program atLoop base n suffix ambient register rw [hbase] at h exact h def discardInTime (source : K) (loopLabel returnLabel : Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atLoop : program loopLabel = discard source loopLabel returnLabel) (base : K → List Bool) (n : Nat) (suffix : List Bool) (hinput : base source = encodeWord n ++ suffix) (ambient : σ) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some loopLabel, (ambient, register), base⟩ (some ⟨some returnLabel, (ambient, none), Function.update base source suffix⟩) (n + 1) where steps := n + 1 evals_in_steps := discardTrace source loopLabel returnLabel program atLoop base n suffix hinput ambient register steps_le_m := Nat.le_refl _ end BinPackingGames.Foundations.Complexity.MachineLookup namespace BinPackingGames.Foundations.Complexity.PoweringMasterState open Turing open MachineFixedBlockMap abbrev OtherRegisters (N : Nat) := Buffer N × (Bool × Option Bool) abbrev Master (N : Nat) := OtherRegisters N × Option Bool abbrev CompareState (N : Nat) := Buffer N × (Bool × (Option Bool × Option Bool)) abbrev RowState (N : Nat) := (Bool × (Option Bool × Option Bool)) × Buffer N def compareEquiv (N : Nat) : Master N ≃ CompareState N := (MachineUnaryEqualityBit.compareStateEquiv (Buffer N)).symm def rowEquiv (N : Nat) : Master N ≃ RowState N where toFun state := ((state.1.2.1, (state.1.2.2, state.2)), state.1.1) invFun state := ((state.2, (state.1.1, state.1.2.1)), state.1.2.2) left_inv := by rintro ⟨⟨buffer, ⟨flag, left⟩⟩, right⟩; rfl right_inv := by rintro ⟨⟨flag, ⟨left, right⟩⟩, buffer⟩; rfl @[simp] theorem compareEquiv_apply (N : Nat) (buffer : Buffer N) (flag : Bool) (left right : Option Bool) : compareEquiv N ((buffer, (flag, left)), right) = (buffer, (flag, (left, right))) := rfl @[simp] theorem compareEquiv_symm_apply (N : Nat) (buffer : Buffer N) (flag : Bool) (left right : Option Bool) : (compareEquiv N).symm (buffer, (flag, (left, right))) = ((buffer, (flag, left)), right) := rfl @[simp] theorem rowEquiv_apply (N : Nat) (buffer : Buffer N) (flag : Bool) (left right : Option Bool) : rowEquiv N ((buffer, (flag, left)), right) = ((flag, (left, right)), buffer) := rfl @[simp] theorem rowEquiv_symm_apply (N : Nat) (buffer : Buffer N) (flag : Bool) (left right : Option Bool) : (rowEquiv N).symm ((flag, (left, right)), buffer) = ((buffer, (flag, left)), right) := rfl def withBuffer {N : Nat} (buffer : Buffer N) : Master N := ((buffer, (false, none)), none) def clean (N : Nat) : Master N := withBuffer (emptyBuffer N) @[simp] theorem equalityBit_clean {N : Nat} (buffer : Buffer N) : MachineUnaryEqualityBit.clean buffer = withBuffer buffer := rfl @[simp] theorem rowEquiv_withBuffer {N : Nat} (buffer : Buffer N) : rowEquiv N (withBuffer buffer) = ((false, (none, none)), buffer) := rfl @[simp] theorem compareEquiv_withBuffer {N : Nat} (buffer : Buffer N) : compareEquiv N (withBuffer buffer) = (buffer, (false, (none, none))) := rfl def clearBuffer {N : Nat} (state : Master N) : Master N := (rowEquiv N).symm ((rowEquiv N state).1, emptyBuffer N) @[simp] theorem clearBuffer_apply {N : Nat} (buffer : Buffer N) (flag : Bool) (left right : Option Bool) : clearBuffer ((buffer, (flag, left)), right) = ((emptyBuffer N, (flag, left)), right) := rfl @[simp] theorem clearBuffer_withBuffer {N : Nat} (buffer : Buffer N) : clearBuffer (withBuffer buffer) = clean N := rfl section Row variable {K Λ : Type} [DecidableEq K] {N M : Nat} def encodedBlockAt (src dst : K) (F : Buffer N → Buffer M) (exit : Option Λ) : TM2.Stmt (fun _ : K => Bool) Λ (Master N) := MachineStateEquiv.statement (rowEquiv N).symm (PoweringMachineRow.encodedBlockAt src dst F exit) theorem stepAux_encodedBlockAt (src dst : K) (F : Buffer N → Buffer M) (exit : Option Λ) (hne : src ≠ dst) (bits : Buffer N) (suffix : List Bool) (state : Master N) (tapes : K → List Bool) (hinput : tapes src = PoweringMachineRow.encodeBits (List.ofFn bits) ++ suffix) : TM2.stepAux (encodedBlockAt src dst F exit) state tapes = { l := exit, var := clearBuffer state, stk := Function.update (Function.update tapes src suffix) dst (PoweringMachineRow.encodeBits (List.ofFn (F bits)) ++ tapes dst) } := by rw [encodedBlockAt, MachineStateEquiv.stepAux_transport_symm, Equiv.symm_symm] rw [PoweringMachineRow.stepAux_encodedBlockAt src dst F exit hne bits suffix (rowEquiv N state) tapes hinput] rfl theorem step_encodedBlockAt (src dst : K) (F : Buffer N → Buffer M) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (Master N)) (label : Λ) (atLabel : program label = encodedBlockAt src dst F exit) (hne : src ≠ dst) (bits : Buffer N) (suffix : List Bool) (state : Master N) (tapes : K → List Bool) (hinput : tapes src = PoweringMachineRow.encodeBits (List.ofFn bits) ++ suffix) : TM2.step program ⟨some label, state, tapes⟩ = some ⟨exit, clearBuffer state, Function.update (Function.update tapes src suffix) dst (PoweringMachineRow.encodeBits (List.ofFn (F bits)) ++ tapes dst)⟩ := by simp only [TM2.step, atLabel, stepAux_encodedBlockAt src dst F exit hne bits suffix state tapes hinput] def rowAt {t S q : Nat} (src dst : K) (labelAt : Fin q → Fin S → PCP.GraphTables.Label) (exit : Option Λ) : TM2.Stmt (fun _ : K => Bool) Λ (Master (PoweringMachineRow.inputSize t S)) := encodedBlockAt src dst (PoweringMachineRow.rowBlock labelAt) exit theorem step_rowAt_clean {t S q : Nat} (src dst : K) (labelAt : Fin q → Fin S → PCP.GraphTables.Label) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (Master (PoweringMachineRow.inputSize t S))) (label : Λ) (atLabel : program label = rowAt (t := t) src dst labelAt exit) (hne : src ≠ dst) (bits register : Buffer (PoweringMachineRow.inputSize t S)) (suffix : List Bool) (tapes : K → List Bool) (hinput : tapes src = PoweringMachineRow.encodeBits (List.ofFn bits) ++ suffix) : TM2.step program ⟨some label, withBuffer register, tapes⟩ = some ⟨exit, clean (PoweringMachineRow.inputSize t S), Function.update (Function.update tapes src suffix) dst (PoweringMachineRow.encodeBits (List.ofFn (PoweringMachineRow.rowBlock labelAt bits)) ++ tapes dst)⟩ := by simpa only [rowAt, clearBuffer_withBuffer] using step_encodedBlockAt src dst (PoweringMachineRow.rowBlock labelAt) exit program label atLabel hne bits suffix (withBuffer register) tapes hinput def rowInTime {t S q : Nat} (src dst : K) (labelAt : Fin q → Fin S → PCP.GraphTables.Label) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (Master (PoweringMachineRow.inputSize t S))) (label : Λ) (atLabel : program label = rowAt (t := t) src dst labelAt exit) (hne : src ≠ dst) (bits register : Buffer (PoweringMachineRow.inputSize t S)) (suffix : List Bool) (tapes : K → List Bool) (hinput : tapes src = PoweringMachineRow.encodeBits (List.ofFn bits) ++ suffix) : StateTransition.EvalsToInTime (TM2.step program) ⟨some label, withBuffer register, tapes⟩ (some ⟨exit, clean (PoweringMachineRow.inputSize t S), Function.update (Function.update tapes src suffix) dst (PoweringMachineRow.encodeBits (List.ofFn (PoweringMachineRow.rowBlock labelAt bits)) ++ tapes dst)⟩) 1 where steps := 1 evals_in_steps := by simpa only [Function.iterate_one, flip, bind, Option.bind] using step_rowAt_clean src dst labelAt exit program label atLabel hne bits register suffix tapes hinput steps_le_m := le_rfl end Row def headerInTime (N B n : Nat) (suffix : List Bool) (state : Master N) : StateTransition.EvalsToInTime (TM2.step (PoweringMachineLoop.headerProgram (σ := OtherRegisters N) B)) ⟨some .initialize, state, PoweringMachineLoop.initialTapes n suffix⟩ (some (PoweringMachineLoop.finalConfiguration B n suffix state.1)) (n + 2) := PoweringMachineLoop.headerInTime B n suffix state.1 state.2 end BinPackingGames.Foundations.Complexity.PoweringMasterState end OAI