import Mathlib import OAI.Combinatorics.IndependentSets.Machines.MachineEmbedding namespace OAI namespace IndependentSetsGames.Foundations.Complexity.MachineComposition def advance {σ : Type} (step : σ → Option σ) (state : Option σ) : Option σ := state.bind step @[simp] theorem advance_none {σ : Type} (step : σ → Option σ) : advance step none = none := rfl @[simp] theorem advance_some {σ : Type} (step : σ → Option σ) (state : σ) : advance step (some state) = step state := rfl @[simp] theorem advance_iterate_none {σ : Type} (step : σ → Option σ) (n : Nat) : (advance step)^[n] none = none := by induction n with | zero => rfl | succ n ih => rw [Function.iterate_succ_apply', ih, advance_none] theorem liftSuccessfulTrace {σ τ : Type} (source : σ → Option σ) (target : τ → Option τ) (embed : σ → τ) (simulation : ∀ a b, source a = some b → target (embed a) = some (embed b)) (n : Nat) (start finish : σ) (trace : (advance source)^[n] (some start) = some finish) : (advance target)^[n] (some (embed start)) = some (embed finish) := by induction n generalizing start with | zero => have same := Option.some.inj trace cases same rfl | succ n ih => rw [Function.iterate_succ_apply] at trace ⊢ change (advance source)^[n] (source start) = some finish at trace change (advance target)^[n] (target (embed start)) = some (embed finish) cases firstStep : source start with | none => rw [firstStep, advance_iterate_none] at trace contradiction | some intermediate => rw [firstStep] at trace rw [simulation start intermediate firstStep] exact ih intermediate trace def liftExecutionInTime {σ τ : Type} (source : σ → Option σ) (target : τ → Option τ) (embed : σ → τ) (simulation : ∀ a b, source a = some b → target (embed a) = some (embed b)) {start finish : σ} {budget : Nat} (execution : StateTransition.EvalsToInTime source start (some finish) budget) : StateTransition.EvalsToInTime target (embed start) (some (embed finish)) budget where steps := execution.steps evals_in_steps := by have sourceTrace := execution.evals_in_steps change (advance source)^[execution.steps] (some start) = some finish at sourceTrace change (advance target)^[execution.steps] (some (embed start)) = some (embed finish) exact liftSuccessfulTrace source target embed simulation execution.steps start finish sourceTrace steps_le_m := execution.steps_le_m @[simp] theorem liftExecutionInTime_steps {σ τ : Type} (source : σ → Option σ) (target : τ → Option τ) (embed : σ → τ) (simulation : ∀ a b, source a = some b → target (embed a) = some (embed b)) {start finish : σ} {budget : Nat} (execution : StateTransition.EvalsToInTime source start (some finish) budget) : (liftExecutionInTime source target embed simulation execution).steps = execution.steps := rfl def embeddedExecution {K E Λ Λextra σ τ : Type} {Γ : K → Type} {Δ : E → Type} [DecidableEq K] [DecidableEq E] (haltTarget : Option (Λ ⊕ Λextra)) (extraState : τ) (extraTapes : ∀ e, List (Δ e)) (source : Λ → Turing.TM2.Stmt Γ Λ σ) (extra : Λextra → Turing.TM2.Stmt (MachineEmbedding.Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ)) {start finish : Turing.TM2.Cfg Γ Λ σ} {budget : Nat} (execution : StateTransition.EvalsToInTime (Turing.TM2.step source) start (some finish) budget) : StateTransition.EvalsToInTime (Turing.TM2.step (MachineEmbedding.program haltTarget source extra)) (MachineEmbedding.configuration haltTarget extraState extraTapes start) (some (MachineEmbedding.configuration haltTarget extraState extraTapes finish)) budget := liftExecutionInTime (Turing.TM2.step source) (Turing.TM2.step (MachineEmbedding.program haltTarget source extra)) (MachineEmbedding.configuration haltTarget extraState extraTapes) (MachineEmbedding.step_simulation haltTarget extraState extraTapes source extra) execution theorem natPolynomial_eval_mono (p : Polynomial Nat) {a b : Nat} (h : a ≤ b) : p.eval a ≤ p.eval b := by induction p using Polynomial.induction_on' with | add p q hp hq => simpa only [Polynomial.eval_add] using Nat.add_le_add hp hq | monomial degree coefficient => simp only [Polynomial.eval_monomial] exact Nat.mul_le_mul_left coefficient (Nat.pow_le_pow_left h degree) noncomputable def compositionPolynomial (first second intermediate : Polynomial Nat) : Polynomial Nat := first + Polynomial.C 2 * (intermediate + 1) + second.comp intermediate theorem compositionBudget_le (first second intermediate : Polynomial Nat) (inputLength intermediateLength : Nat) (intermediateBound : intermediateLength ≤ intermediate.eval inputLength) : first.eval inputLength + 2 * (intermediateLength + 1) + second.eval intermediateLength ≤ (compositionPolynomial first second intermediate).eval inputLength := by have secondBound := natPolynomial_eval_mono second intermediateBound have transferBound := Nat.mul_le_mul_left 2 (Nat.add_le_add_right intermediateBound 1) simp only [compositionPolynomial, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_one, Polynomial.eval_comp] omega def fourPhaseExecution {σ : Type} (step : σ → Option σ) (start afterFirst afterTransfer₁ afterTransfer₂ : σ) (finish : Option σ) (first second intermediate : Polynomial Nat) (inputLength intermediateLength : Nat) (intermediateBound : intermediateLength ≤ intermediate.eval inputLength) (runFirst : StateTransition.EvalsToInTime step start (some afterFirst) (first.eval inputLength)) (transfer₁ : StateTransition.EvalsToInTime step afterFirst (some afterTransfer₁) (intermediateLength + 1)) (transfer₂ : StateTransition.EvalsToInTime step afterTransfer₁ (some afterTransfer₂) (intermediateLength + 1)) (runSecond : StateTransition.EvalsToInTime step afterTransfer₂ finish (second.eval intermediateLength)) : StateTransition.EvalsToInTime step start finish ((compositionPolynomial first second intermediate).eval inputLength) := by let firstTwo := StateTransition.EvalsToInTime.trans step _ _ start afterFirst (some afterTransfer₁) runFirst transfer₁ let firstThree := StateTransition.EvalsToInTime.trans step _ _ start afterTransfer₁ (some afterTransfer₂) firstTwo transfer₂ let allFour := StateTransition.EvalsToInTime.trans step _ _ start afterTransfer₂ finish firstThree runSecond refine { toEvalsTo := allFour.toEvalsTo steps_le_m := Nat.le_trans allFour.steps_le_m ?_ } have bounded := compositionBudget_le first second intermediate inputLength intermediateLength intermediateBound omega end IndependentSetsGames.Foundations.Complexity.MachineComposition end OAI