import OAI.Computability.UniqueGames.Machines.MachineCompositionLemmas import OAI.Computability.UniqueGames.Reduction.MachineTransfer namespace OAI section /-! Actual complete-stack cleanup, with an arbitrary continuation. Each symbol costs one transition and the empty-stack branch costs one final transition. -/ namespace UniqueGamesTheorem.Foundations.Complexity.MachineDrain open Turing variable {K Λ σ : Type} [DecidableEq K] abbrev Alphabet (_ : K) := Bool def drain (source : K) (again : Λ) (exit : Option Λ) : TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool) := .pop source (fun state head => (state.1, head)) (.branch (fun state => state.2.isSome) (.goto fun _ => again) (.load (fun state => (state.1, none)) (Reduction.MachineTransfer.exitAt source exit))) theorem drainTrace (source : K) (again : Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atDrain : program again = drain source again exit) (base : K → List Bool) (word : List Bool) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step program))^[word.length + 1] (some ⟨some again, (ambient, register), Function.update base source word⟩) = some ⟨exit, (ambient, none), Function.update base source []⟩ := by induction word generalizing register with | nil => change some (TM2.stepAux (program again) (ambient, register) (Function.update base source [])) = _ rw [atDrain] cases exit <;> simp [drain, TM2.stepAux, Reduction.MachineTransfer.exitAt] | cons symbol word ih => rw [List.length_cons, Function.iterate_succ_apply] change (MachineComposition.advance (TM2.step program))^[word.length + 1] (some (TM2.stepAux (program again) (ambient, register) (Function.update base source (symbol :: word)))) = _ rw [atDrain] simpa [drain, TM2.stepAux] using ih (some symbol) def drainInTime (source : K) (again : Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) (atDrain : program again = drain source again exit) (base : K → List Bool) (ambient : σ) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some again, (ambient, register), base⟩ (some ⟨exit, (ambient, none), Function.update base source []⟩) ((base source).length + 1) where steps := (base source).length + 1 evals_in_steps := by change (MachineComposition.advance (TM2.step program))^[(base source).length + 1] (some ⟨some again, (ambient, register), base⟩) = _ simpa only [Function.update_eq_self] using drainTrace source again exit program atDrain base (base source) ambient register steps_le_m := Nat.le_refl _ end UniqueGamesTheorem.Foundations.Complexity.MachineDrain end end OAI