import OAI.Computability.UniqueGames.Machines.MachineExpanderFamilyBoundsLemmas import OAI.Computability.UniqueGames.Machines.MachineExpanderFamilyTransferLemmas namespace OAI /-! Actual counted repetition of complete table-expansion cycles. The input counter is physically decremented, and the invariant carries the exact table at the completed family level. -/ namespace UniqueGamesTheorem.Foundations.Complexity.MachineExpanderFamily open Turing open PCP.ExpanderTables PCP.ExpanderRowControl PCP.ExpanderTableWords open MachineExpanderFamilyBounds noncomputable def levelLoopCost {d : Nat} (H : Table (cloudSize d) d) : Nat → Nat → Nat | _, 0 => 1 | start, remaining + 1 => 1 + resizeCost (family H start) H + levelLoopCost H (start + 1) remaining theorem levelLoopCost_prefix {d : Nat} (H : Table (cloudSize d) d) (start remaining : Nat) : levelLoopCost H start remaining + resizeSum H start = resizeSum H (start + remaining) + remaining + 1 := by induction remaining generalizing start with | zero => simp [levelLoopCost, Nat.add_comm] | succ remaining ih => have h := ih (start + 1) rw [resizeSum_succ] at h have index : start + 1 + remaining = start + (remaining + 1) := by omega rw [index] at h simp only [levelLoopCost] omega theorem levelLoopCost_zero_start {d : Nat} (H : Table (cloudSize d) d) (remaining : Nat) : levelLoopCost H 0 remaining = resizeSum H remaining + remaining + 1 := by simpa only [resizeSum_zero, Nat.add_zero, Nat.zero_add] using levelLoopCost_prefix H 0 remaining def levelLoopTapes {d : Nat} (H : Table (cloudSize d) d) (completed remaining : Nat) (suffix : List Bool) : ∀ tape, List (Alphabet tape) := boundaryTapes remaining (vertexCount (degree d) completed) (encodeWords (rotationWords (family H completed))) suffix structure LevelLoopRun {d : Nat} {ρ : Type} [Fintype ρ] (positive : 0 < d) (H : Table (cloudSize d) d) (growth : 1 < cloudSize d) (completed remaining : Nat) (state : State ρ d) (suffix : List Bool) where finalState : State ρ d caller_preserved : caller finalState = caller state execution : StateTransition.EvalsToInTime (TM2.step (program positive H growth)) ⟨some (.inr .levelGuard), state, levelLoopTapes H completed remaining suffix⟩ (some ⟨some (.inr .done), finalState, familyTapes H (completed + remaining) suffix⟩) (levelLoopCost H completed remaining) private def familySingleStep {S : Type*} (f : S → Option S) (a b : S) (h : f a = some b) : StateTransition.EvalsToInTime f a (some b) 1 where steps := 1 evals_in_steps := by change f a = some b; exact h steps_le_m := Nat.le_refl _ /-- Every body is the already constructed actual resize run. There is no whole-loop, body-trace, or computational-correctness premise. -/ noncomputable def levelLoopInTime {d : Nat} {ρ : Type} [Fintype ρ] (positive : 0 < d) (H : Table (cloudSize d) d) (growth : 1 < cloudSize d) (completed remaining : Nat) (state : State ρ d) (suffix : List Bool) : LevelLoopRun positive H growth completed remaining state suffix := by induction remaining generalizing completed state with | zero => refine ⟨clearRegister state, caller_clearRegister state, ?_⟩ simpa only [levelLoopCost, levelLoopTapes, familyTapes, Nat.add_zero] using familySingleStep _ _ _ (levelGuard_boundary_zeroStep positive H growth (vertexCount (degree d) completed) (encodeWords (rotationWords (family H completed))) suffix state) | succ remaining ih => let cycle := resizeInTime positive H growth (family H completed) remaining (clearRegister state) suffix let rest := ih (completed + 1) cycle.finalState let guard := familySingleStep _ _ _ (levelGuard_boundary_succStep positive H growth remaining (vertexCount (degree d) completed) (encodeWords (rotationWords (family H completed))) suffix state) have cycleRun : StateTransition.EvalsToInTime (TM2.step (program positive H growth)) ⟨some (.inr (.affine .copyCount .seed)), clearRegister state, levelLoopTapes H completed remaining suffix⟩ (some ⟨some (.inr .levelGuard), cycle.finalState, levelLoopTapes H (completed + 1) remaining suffix⟩) (resizeCost (family H completed) H) := cycle.execution let first := StateTransition.EvalsToInTime.trans (TM2.step (program positive H growth)) _ _ _ _ _ guard cycleRun let all := StateTransition.EvalsToInTime.trans (TM2.step (program positive H growth)) _ _ _ _ _ first rest.execution have index : completed + 1 + remaining = completed + (remaining + 1) := by omega refine ⟨rest.finalState, ?_, ?_⟩ · exact rest.caller_preserved.trans (cycle.caller_preserved.trans (caller_clearRegister state)) · rw [index] at all refine { toEvalsTo := all.toEvalsTo, steps_le_m := ?_ } have bound := all.steps_le_m simp only [levelLoopCost] omega end UniqueGamesTheorem.Foundations.Complexity.MachineExpanderFamily end OAI