import Mathlib.Computability.TuringMachine.Computable import Mathlib.Data.Fintype.Sum /-! # Adding untouched stacks to a finite stack machine Extend the original stack index by a disjoint sum. Each primitive still reads and writes exactly the same symbol type. The additional stacks may have arbitrary initial contents; they are carried unchanged through a run, including its final transition to a halted configuration. -/ namespace Millennium.Machines.ExtraStacks open Turing StateTransition Function variable {K E : Type} {Γ : K → Type} (extra : E → Type) {Λ σ : Type} abbrev ExtendedAlphabet (Γ : K → Type) (extra : E → Type) : K ⊕ E → Type := Sum.elim Γ extra /-- Retain the register, labels, and all operations, changing only stack addresses. -/ def translate : TM2.Stmt Γ Λ σ → TM2.Stmt (ExtendedAlphabet Γ extra) Λ σ | .push k f q => .push (.inl k) f (translate q) | .peek k f q => .peek (.inl k) f (translate q) | .pop k f q => .pop (.inl k) f (translate q) | .load f q => .load f (translate q) | .branch f q r => .branch f (translate q) (translate r) | .goto f => .goto f | .halt => .halt def joined (st : ∀ k, List (Γ k)) (saved : ∀ e, List (extra e)) : ∀ k, List (ExtendedAlphabet Γ extra k) | .inl k => st k | .inr e => saved e def extendCfg (saved : ∀ e, List (extra e)) (c : TM2.Cfg Γ Λ σ) : TM2.Cfg (ExtendedAlphabet Γ extra) Λ σ := ⟨c.l, c.var, joined extra c.stk saved⟩ variable [DecidableEq K] [DecidableEq E] private theorem joined_update (st : ∀ k, List (Γ k)) (saved : ∀ e, List (extra e)) (k : K) (xs : List (Γ k)) : Function.update (joined extra st saved) (.inl k) xs = joined extra (Function.update st k xs) saved := by funext j cases j with | inl j => by_cases h : j = k · subst j; simp [joined] · have different : (Sum.inl j : K ⊕ E) ≠ .inl k := fun eq => h (Sum.inl.inj eq) simp [joined, Function.update_of_ne h, Function.update_of_ne different] | inr e => simp [joined, Function.update_of_ne] private theorem block_correct (q : TM2.Stmt Γ Λ σ) (v : σ) (st : ∀ k, List (Γ k)) (saved : ∀ e, List (extra e)) : TM2.stepAux (translate extra q) v (joined extra st saved) = extendCfg extra saved (TM2.stepAux q v st) := by induction q generalizing v st with | push k f q ih => change TM2.stepAux (translate extra q) v (Function.update (joined extra st saved) (.inl k) (f v :: st k)) = _ rw [joined_update] exact ih v (Function.update st k (f v :: st k)) | peek k f q ih => exact ih (f v (st k).head?) st | pop k f q ih => change TM2.stepAux (translate extra q) (f v (st k).head?) (Function.update (joined extra st saved) (.inl k) (st k).tail) = _ rw [joined_update] exact ih (f v (st k).head?) (Function.update st k (st k).tail) | load f q ih => exact ih (f v) st | branch f q r ih ir => cases hf : f v <;> simp only [translate, TM2.stepAux, hf, cond_false, cond_true, ih, ir] | goto f => rfl | halt => rfl private theorem step_correct (prog : Λ → TM2.Stmt Γ Λ σ) (saved : ∀ e, List (extra e)) (c : TM2.Cfg Γ Λ σ) : TM2.step (fun l => translate extra (prog l)) (extendCfg extra saved c) = (TM2.step prog c).map (extendCfg extra saved) := by rcases c with ⟨l, v, st⟩ cases l with | none => rfl | some l => exact congrArg some (block_correct extra (prog l) v st saved) /-- The enlarged machine has the same initial instruction, input and output roles. -/ def extendMachine (tm : FinTM2) (E : Type) [DecidableEq E] [Fintype E] (extra : E → Type) : FinTM2 where K := tm.K ⊕ E kFin := by letI := tm.kFin; infer_instance k₀ := .inl tm.k₀ k₁ := .inl tm.k₁ Γ := ExtendedAlphabet tm.Γ extra Λ := tm.Λ ΛFin := tm.ΛFin main := tm.main σ := tm.σ σFin := tm.σFin initialState := tm.initialState Γk₀Fin := tm.Γk₀Fin m l := translate extra (tm.m l) /-- Every timed computation lifts with the very same step count and untouched extra stacks, including when the optional result is empty. -/ def liftRun (tm : FinTM2) {E : Type} [DecidableEq E] [Fintype E] (extra : E → Type) (saved : ∀ e, List (extra e)) {a : tm.Cfg} {b : Option tm.Cfg} {bound : ℕ} (run : EvalsToInTime tm.step a b bound) : EvalsToInTime (extendMachine tm E extra).step (extendCfg extra saved a) (b.map (extendCfg extra saved)) bound := by let f := flip bind tm.step let g := flip bind (extendMachine tm E extra).step let send : Option tm.Cfg → Option (extendMachine tm E extra).Cfg := Option.map (extendCfg extra saved) have commutes : Function.Semiconj send f g := by intro o cases o with | none => rfl | some c => exact (step_correct extra tm.m saved c).symm refine ⟨⟨run.steps, ?_⟩, run.steps_le_m⟩ exact ((commutes.iterate_right run.steps) (some a)).symm.trans (congrArg send run.evals_in_steps) #print axioms liftRun end Millennium.Machines.ExtraStacks