import OAI.Computability.UniqueGames.Machines.MachineCompositionLemmas import OAI.Computability.UniqueGames.Reduction.MachineTransfer namespace OAI section namespace UniqueGamesTheorem.Foundations.Complexity.MachineCopy open Turing variable {K Λ σ β : Type} [DecidableEq K] abbrev Alphabet (β : Type) (_ : K) := β /-- Drain one tape into two reverse-order accumulators. -/ def forkLoop (source left right : K) (fallback : β) (loopLabel : Λ) (exit : Option Λ) : TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β) := .pop source (fun state head => (state.1, head)) (.branch (fun state => state.2.isSome) (.push left (fun state => state.2.getD fallback) (.push right (fun state => state.2.getD fallback) (.goto fun _ => loopLabel))) (Reduction.MachineTransfer.exitAt right exit)) def forkTapes (source left right : K) (base : K → List β) (input leftOutput rightOutput : List β) : K → List β := Function.update (Function.update (Function.update base source input) left leftOutput) right rightOutput @[simp] theorem forkTapes_source (source left right : K) (sourceLeft : source ≠ left) (sourceRight : source ≠ right) (base : K → List β) (input leftOutput rightOutput : List β) : forkTapes source left right base input leftOutput rightOutput source = input := by simp [forkTapes, sourceLeft, sourceRight] @[simp] theorem forkTapes_left (source left right : K) (leftRight : left ≠ right) (base : K → List β) (input leftOutput rightOutput : List β) : forkTapes source left right base input leftOutput rightOutput left = leftOutput := by simp [forkTapes, leftRight] @[simp] theorem forkTapes_right (source left right : K) (base : K → List β) (input leftOutput rightOutput : List β) : forkTapes source left right base input leftOutput rightOutput right = rightOutput := by simp [forkTapes] @[simp] theorem forkTapes_self (source left right : K) (base : K → List β) : forkTapes source left right base (base source) (base left) (base right) = base := by simp [forkTapes] private theorem update_fork_source (source left right : K) (sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right) (base : K → List β) (input leftOutput rightOutput replacement : List β) : Function.update (forkTapes source left right base input leftOutput rightOutput) source replacement = forkTapes source left right base replacement leftOutput rightOutput := by funext k by_cases hs : k = source · subst k; simp [forkTapes, sourceLeft, sourceRight] · by_cases hl : k = left · subst k; simp [forkTapes, Ne.symm sourceLeft, leftRight] · by_cases hr : k = right · subst k; simp [forkTapes, Ne.symm sourceRight] · simp [forkTapes, hs, hl, hr] private theorem update_fork_left (source left right : K) (leftRight : left ≠ right) (base : K → List β) (input leftOutput rightOutput replacement : List β) : Function.update (forkTapes source left right base input leftOutput rightOutput) left replacement = forkTapes source left right base input replacement rightOutput := by funext k by_cases hl : k = left · subst k; simp [forkTapes, leftRight] · by_cases hr : k = right · subst k; simp [forkTapes, Ne.symm leftRight] · simp [forkTapes, hl, hr] private theorem update_fork_right (source left right : K) (base : K → List β) (input leftOutput rightOutput replacement : List β) : Function.update (forkTapes source left right base input leftOutput rightOutput) right replacement = forkTapes source left right base input leftOutput replacement := by simp [forkTapes] theorem forkStep_empty (source left right : K) (sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right) (fallback : β) (loopLabel : Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β)) (atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit) (base : K → List β) (leftOutput rightOutput : List β) (ambient : σ) (register : Option β) : TM2.step program ⟨some loopLabel, (ambient, register), forkTapes source left right base [] leftOutput rightOutput⟩ = some ⟨exit, (ambient, none), forkTapes source left right base [] leftOutput rightOutput⟩ := by change some (TM2.stepAux (program loopLabel) (ambient, register) (forkTapes source left right base [] leftOutput rightOutput)) = _ rw [atLoop] cases exit <;> simp [forkLoop, Reduction.MachineTransfer.exitAt, TM2.stepAux, sourceLeft, sourceRight, leftRight, update_fork_source] theorem forkStep_cons (source left right : K) (sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right) (fallback : β) (loopLabel : Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β)) (atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit) (base : K → List β) (head : β) (input leftOutput rightOutput : List β) (ambient : σ) (register : Option β) : TM2.step program ⟨some loopLabel, (ambient, register), forkTapes source left right base (head :: input) leftOutput rightOutput⟩ = some ⟨some loopLabel, (ambient, some head), forkTapes source left right base input (head :: leftOutput) (head :: rightOutput)⟩ := by change some (TM2.stepAux (program loopLabel) (ambient, register) (forkTapes source left right base (head :: input) leftOutput rightOutput)) = _ rw [atLoop] simp [forkLoop, TM2.stepAux, sourceLeft, sourceRight, leftRight, update_fork_source, update_fork_left, update_fork_right] /-- Each symbol and the final empty test take one actual transition. -/ theorem forkTrace (source left right : K) (sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right) (fallback : β) (loopLabel : Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β)) (atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit) (base : K → List β) (input leftOutput rightOutput : List β) (ambient : σ) (register : Option β) : (MachineComposition.advance (TM2.step program))^[input.length + 1] (some ⟨some loopLabel, (ambient, register), forkTapes source left right base input leftOutput rightOutput⟩) = some ⟨exit, (ambient, none), forkTapes source left right base [] (input.reverse ++ leftOutput) (input.reverse ++ rightOutput)⟩ := by induction input generalizing leftOutput rightOutput register with | nil => simpa only [List.length_nil, Nat.zero_add, Function.iterate_one, MachineComposition.advance_some, List.reverse_nil, List.nil_append] using forkStep_empty source left right sourceLeft sourceRight leftRight fallback loopLabel exit program atLoop base leftOutput rightOutput ambient register | cons head input ih => rw [List.length_cons, Function.iterate_succ_apply] change (MachineComposition.advance (TM2.step program))^[input.length + 1] (TM2.step program ⟨some loopLabel, (ambient, register), forkTapes source left right base (head :: input) leftOutput rightOutput⟩) = _ rw [forkStep_cons source left right sourceLeft sourceRight leftRight fallback loopLabel exit program atLoop base head input leftOutput rightOutput ambient register, ih] simp only [List.reverse_cons, List.append_assoc, List.singleton_append] def forkInTime (source left right : K) (sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right) (fallback : β) (loopLabel : Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β)) (atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit) (base : K → List β) (ambient : σ) (register : Option β) : StateTransition.EvalsToInTime (TM2.step program) ⟨some loopLabel, (ambient, register), base⟩ (some ⟨exit, (ambient, none), forkTapes source left right base [] ((base source).reverse ++ base left) ((base source).reverse ++ base right)⟩) ((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 loopLabel, (ambient, register), base⟩) = _ simpa only [forkTapes_self] using forkTrace source left right sourceLeft sourceRight leftRight fallback loopLabel exit program atLoop base (base source) (base left) (base right) ambient register steps_le_m := Nat.le_refl _ def drainedTapes (source scratch : K) (base : K → List β) : K → List β := Reduction.MachineTransfer.tapesAt source scratch base [] (base source).reverse theorem restoredTapes (source destination scratch : K) (sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch) (destinationScratch : destination ≠ scratch) (base : K → List β) (scratchEmpty : base scratch = []) : forkTapes scratch source destination (drainedTapes source scratch base) [] (base source) (base source ++ base destination) = Function.update base destination (base source ++ base destination) := by funext k by_cases hs : k = source · subst k simp [forkTapes, sourceDestination] · by_cases hd : k = destination · subst k; simp [forkTapes] · by_cases ht : k = scratch · subst k simp [forkTapes, Ne.symm sourceScratch, Ne.symm destinationScratch, scratchEmpty] · simp [forkTapes, drainedTapes, Reduction.MachineTransfer.tapesAt, hs, hd, ht] theorem copyTrace (source destination scratch : K) (sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch) (destinationScratch : destination ≠ scratch) (fallback : β) (firstLabel secondLabel : Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β)) (atFirst : program firstLabel = Reduction.MachineTransfer.loopAt source scratch id fallback firstLabel (some secondLabel)) (atSecond : program secondLabel = forkLoop scratch source destination fallback secondLabel exit) (base : K → List β) (scratchEmpty : base scratch = []) (ambient : σ) (register : Option β) : (MachineComposition.advance (TM2.step program))^[2 * ((base source).length + 1)] (some ⟨some firstLabel, (ambient, register), base⟩) = some ⟨exit, (ambient, none), Function.update base destination (base source ++ base destination)⟩ := by have first := Reduction.MachineTransfer.transferAt_fromTapes source scratch sourceScratch id fallback firstLabel (some secondLabel) program atFirst base ambient register change (MachineComposition.advance (TM2.step program))^[(base source).length + 1] (some ⟨some firstLabel, (ambient, register), base⟩) = _ at first simp only [List.map_id, scratchEmpty, List.append_nil] at first have second := forkTrace scratch source destination (Ne.symm sourceScratch) (Ne.symm destinationScratch) sourceDestination fallback secondLabel exit program atSecond (drainedTapes source scratch base) (drainedTapes source scratch base scratch) (drainedTapes source scratch base source) (drainedTapes source scratch base destination) ambient none simp only [forkTapes_self] at second have scratchWord : drainedTapes source scratch base scratch = (base source).reverse := by simp [drainedTapes] have sourceWord : drainedTapes source scratch base source = [] := by simp [drainedTapes, sourceScratch] have destinationWord : drainedTapes source scratch base destination = base destination := by simp [drainedTapes, Reduction.MachineTransfer.tapesAt, Ne.symm sourceDestination, destinationScratch] rw [scratchWord, sourceWord, destinationWord] at second simp only [List.reverse_reverse, List.append_nil, List.length_reverse] at second rw [restoredTapes source destination scratch sourceDestination sourceScratch destinationScratch base scratchEmpty] at second rw [show 2 * ((base source).length + 1) = ((base source).length + 1) + ((base source).length + 1) by omega, Function.iterate_add_apply, first] exact second /-- A timed witness whose stored step count is the exact two-loop trace length. -/ def copyInTime (source destination scratch : K) (sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch) (destinationScratch : destination ≠ scratch) (fallback : β) (firstLabel secondLabel : Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β)) (atFirst : program firstLabel = Reduction.MachineTransfer.loopAt source scratch id fallback firstLabel (some secondLabel)) (atSecond : program secondLabel = forkLoop scratch source destination fallback secondLabel exit) (base : K → List β) (scratchEmpty : base scratch = []) (ambient : σ) (register : Option β) : StateTransition.EvalsToInTime (TM2.step program) ⟨some firstLabel, (ambient, register), base⟩ (some ⟨exit, (ambient, none), Function.update base destination (base source ++ base destination)⟩) (2 * ((base source).length + 1)) where steps := 2 * ((base source).length + 1) evals_in_steps := copyTrace source destination scratch sourceDestination sourceScratch destinationScratch fallback firstLabel secondLabel exit program atFirst atSecond base scratchEmpty ambient register steps_le_m := Nat.le_refl _ end UniqueGamesTheorem.Foundations.Complexity.MachineCopy end end OAI