import Mathlib import OAI.Combinatorics.IndependentSets.Machines.MachineFiniteAlphabet import OAI.Combinatorics.IndependentSets.Machines.GraphCounterFinish namespace OAI namespace LargeIndependentSets.BoundedIteration open Turing open IndependentSetsGames.Foundations.Complexity variable {α β : Type} def counted (count : α → ℕ) (enc : β → List Bool) (start : α → β) (a : α) : List Bool := encodeWord (count a) ++ enc (start a) lemma count_le (count : α → ℕ) (enc : β → List Bool) (start : α → β) (a : α) : count a ≤ (counted count enc start a).length := by simp [counted,encodeWord] noncomputable def runtime (body length : Polynomial ℕ) : Polynomial ℕ := Polynomial.X*(body.comp length + 2*length + 4) + 2*length + 4 lemma runtime_eval (body length : Polynomial ℕ) (n : ℕ) : (runtime body length).eval n = n*(body.eval (length.eval n)+2*length.eval n+4)+2*length.eval n+4 := by simp [runtime] noncomputable def certificate (enc : β → List Bool) (step : β → β) (start : α → β) (count : α → ℕ) (body : TM2ComputableInPolyTime enc enc step) (length : Polynomial ℕ) (growth : ∀ a i, i≤count a → (enc (step^[i] (start a))).length ≤ length.eval (counted count enc start a).length) : TM2ComputableInPolyTime (counted count enc start) enc (fun a => step^[count a] (start a)) where tm := MachineRepeat.machine body.tm body.inputAlphabet body.outputAlphabet inputAlphabet := body.inputAlphabet outputAlphabet := body.inputAlphabet time := runtime body.time length outputsFun a := by let words : ℕ → List Bool := fun i => enc (step^[i] (start a)) let budgets : ℕ → ℕ := fun i => body.time.eval (words i).length have runs (i : ℕ) (_hi : i