import OAI.Computability.BinPacking.Computation.PoweringMachineRow namespace OAI namespace BinPackingGames.Reduction.MachineSubstitution open Turing open BinPackingGames.Foundations.Complexity section PushWord variable {K Λ σ : Type} {Γ : K → Type} [DecidableEq K] def pushWord (dst : K) : List (Γ dst) → TM2.Stmt Γ Λ σ → TM2.Stmt Γ Λ σ | [], continuation => continuation | symbol :: word, continuation => .push dst (fun _ => symbol) (pushWord dst word continuation) theorem stepAux_pushWord (dst : K) (word : List (Γ dst)) (continuation : TM2.Stmt Γ Λ σ) (state : σ) (tapes : ∀ k, List (Γ k)) : TM2.stepAux (pushWord dst word continuation) state tapes = TM2.stepAux continuation state (Function.update tapes dst (word.reverse ++ tapes dst)) := by induction word generalizing tapes with | nil => simp [pushWord] | cons symbol word ih => simp only [pushWord, TM2.stepAux] rw [ih] simp only [Function.update_self, Function.update_idem, List.reverse_cons, List.append_assoc, List.singleton_append] omit [DecidableEq K] in theorem statementPushBound_pushWord (dst : K) (word : List (Γ dst)) (continuation : TM2.Stmt Γ Λ σ) : Runtime.statementPushBound (pushWord dst word continuation) = word.length + Runtime.statementPushBound continuation := by induction word with | nil => simp [pushWord] | cons symbol word ih => simp only [pushWord, Runtime.statementPushBound, ih, List.length_cons] omega end PushWord def loop : TM2.Stmt (fun _ : Bool => Bool) (Option Bool) (Option Bool) := .pop false (fun _ head => head) (.branch Option.isSome (.goto fun state => some (state.getD false)) .halt) def program (emit : Bool → List Bool) : Option Bool → TM2.Stmt (fun _ : Bool => Bool) (Option Bool) (Option Bool) | none => loop | some symbol => pushWord true (emit symbol) (.goto fun _ => none) def machine (emit : Bool → List Bool) : FinTM2 where K := Bool k₀ := false k₁ := true Γ _ := Bool Λ := Option Bool main := none σ := Option Bool initialState := none m := program emit def maxEmission (emit : Bool → List Bool) : Nat := max (emit false).length (emit true).length theorem statementPushBound_le (emit : Bool → List Bool) (label : Option Bool) : Runtime.statementPushBound (program emit label) ≤ maxEmission emit := by cases label with | none => simp [program, loop, Runtime.statementPushBound] | some symbol => simp only [program, statementPushBound_pushWord, Runtime.statementPushBound, Nat.add_zero] cases symbol · exact Nat.le_max_left _ _ · exact Nat.le_max_right _ _ theorem programPushBound_le (emit : Bool → List Bool) : Runtime.programPushBound (machine emit) ≤ maxEmission emit := by have allLabels (labels : List (Option Bool)) : Runtime.maxLabelPushes (program emit) labels ≤ maxEmission emit := by induction labels with | nil => exact Nat.zero_le _ | cons label rest ih => exact max_le (statementPushBound_le emit label) ih exact allLabels (machine emit).ΛFin.elems.toList def tapeStacks (input output : List Bool) : Bool → List Bool := fun side => if side then output else input def running (emit : Bool → List Bool) (input output : List Bool) (state : Option Bool) : (machine emit).Cfg := ⟨some none, state, tapeStacks input output⟩ def emitting (emit : Bool → List Bool) (symbol : Bool) (input output : List Bool) (state : Option Bool) : (machine emit).Cfg := ⟨some (some symbol), state, tapeStacks input output⟩ def halted (emit : Bool → List Bool) (output : List Bool) : (machine emit).Cfg := ⟨none, none, tapeStacks [] output⟩ private theorem update_input (input output replacement : List Bool) : Function.update (tapeStacks input output) false replacement = tapeStacks replacement output := by funext side cases side <;> simp [tapeStacks] private theorem update_output (input output replacement : List Bool) : Function.update (tapeStacks input output) true replacement = tapeStacks input replacement := by funext side cases side <;> simp [tapeStacks] theorem step_empty (emit : Bool → List Bool) (output : List Bool) (state : Option Bool) : (machine emit).step (running emit [] output state) = some (halted emit output) := by change some (TM2.stepAux loop state (tapeStacks [] output)) = some (halted emit output) simp [loop, TM2.stepAux, tapeStacks, halted] rw [update_input] rfl theorem step_cons (emit : Bool → List Bool) (symbol : Bool) (input output : List Bool) (state : Option Bool) : (machine emit).step (running emit (symbol :: input) output state) = some (emitting emit symbol input output (some symbol)) := by change some (TM2.stepAux loop state (tapeStacks (symbol :: input) output)) = _ simp [loop, TM2.stepAux, tapeStacks, emitting] rw [update_input] rfl theorem step_emit (emit : Bool → List Bool) (symbol : Bool) (input output : List Bool) (state : Option Bool) : (machine emit).step (emitting emit symbol input output state) = some (running emit input ((emit symbol).reverse ++ output) state) := by change some (TM2.stepAux (pushWord true (emit symbol) (.goto fun _ => none)) state (tapeStacks input output)) = _ rw [stepAux_pushWord] simp only [TM2.stepAux] change some (⟨some none, state, Function.update (tapeStacks input output) true ((emit symbol).reverse ++ output)⟩ : (machine emit).Cfg) = _ exact congrArg (fun tapes => some (⟨some none, state, tapes⟩ : (machine emit).Cfg)) (update_output input output ((emit symbol).reverse ++ output)) def next (emit : Bool → List Bool) (configuration : Option (machine emit).Cfg) : Option (machine emit).Cfg := configuration.bind (machine emit).step theorem two_steps_cons (emit : Bool → List Bool) (symbol : Bool) (input output : List Bool) (state : Option Bool) : (next emit)^[2] (some (running emit (symbol :: input) output state)) = some (running emit input ((emit symbol).reverse ++ output) (some symbol)) := by change (machine emit).step (running emit (symbol :: input) output state) >>= (machine emit).step = _ rw [step_cons] exact step_emit emit symbol input output (some symbol) theorem substitution_steps (emit : Bool → List Bool) (input output : List Bool) (state : Option Bool) : (next emit)^[2 * input.length + 1] (some (running emit input output state)) = some (halted emit ((input.flatMap emit).reverse ++ output)) := by induction input generalizing output state with | nil => simpa only [List.length_nil, Nat.mul_zero, Nat.zero_add, Function.iterate_one, next, Option.bind_some, List.flatMap_nil, List.reverse_nil, List.nil_append] using step_empty emit output state | cons symbol input ih => rw [List.length_cons] rw [show 2 * (input.length + 1) + 1 = (2 * input.length + 1) + 2 by omega] rw [Function.iterate_add_apply, two_steps_cons, ih] simp only [List.flatMap_cons, List.reverse_append, List.append_assoc] theorem initList_eq (emit : Bool → List Bool) (input : List Bool) : initList (machine emit) input = running emit input [] none := by unfold initList running congr 1 funext side cases side <;> rfl theorem haltList_eq (emit : Bool → List Bool) (output : List Bool) : haltList (machine emit) output = halted emit output := by unfold haltList halted congr 1 theorem substitution_init_steps (emit : Bool → List Bool) (input : List Bool) : (next emit)^[2 * input.length + 1] (some (initList (machine emit) input)) = some (haltList (machine emit) (input.flatMap emit).reverse) := by rw [initList_eq, haltList_eq] simpa only [List.append_nil] using substitution_steps emit input [] none def outputsInTime (emit : Bool → List Bool) (input : List Bool) : TM2OutputsInTime (machine emit) input (some (input.flatMap emit).reverse) (2 * input.length + 1) where steps := 2 * input.length + 1 evals_in_steps := substitution_init_steps emit input steps_le_m := Nat.le_refl _ @[simp] theorem outputsInTime_steps (emit : Bool → List Bool) (input : List Bool) : (outputsInTime emit input).steps = 2 * input.length + 1 := rfl noncomputable def computableInPolyTime (emit : Bool → List Bool) : TM2ComputableInPolyTime (id : List Bool → List Bool) id (fun input => (input.flatMap emit).reverse) where tm := machine emit inputAlphabet := Equiv.refl Bool outputAlphabet := Equiv.refl Bool time := 2 * Polynomial.X + 1 outputsFun input := by change TM2OutputsInTime (machine emit) (input.map id) (some ((input.flatMap emit).reverse.map id)) ((2 * Polynomial.X + 1 : Polynomial Nat).eval input.length) have hi := @List.map_id ((machine emit).Γ (machine emit).k₀) input have ho := @List.map_id ((machine emit).Γ (machine emit).k₁) (input.flatMap emit).reverse rw [hi, ho] simpa only [Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_X, Polynomial.eval_ofNat, Polynomial.eval_one] using outputsInTime emit input end BinPackingGames.Reduction.MachineSubstitution end OAI