Download lean/OAI/Computability/DirectedFeedback/Machines/MachineEmbedding.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 40.1 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/DirectedFeedback/Machines/MachineEmbedding.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Computability/DirectedFeedback/Machines/MachineEmbedding.lean
-
curl -L -o MachineEmbedding.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/DirectedFeedback/Machines/MachineEmbedding.lean
40.1 kB
| import Mathlib | |
| namespace OAI | |
| namespace DFVSGames.Foundations.Complexity.MachineEmbedding | |
| open Turing.TM2 | |
| variable {K E Λ Λextra σ τ : Type} {Γ : K → Type} {Δ : E → Type} | |
| abbrev Alphabet (Γ : K → Type) (Δ : E → Type) : K ⊕ E → Type | |
| | .inl k => Γ k | |
| | .inr e => Δ e | |
| def tapes (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e)) : | |
| ∀ j, List (Alphabet Γ Δ j) | |
| | .inl k => source k | |
| | .inr e => extra e | |
| @[simp] theorem tapes_inl (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e)) | |
| (k : K) : tapes source extra (.inl k) = source k := rfl | |
| @[simp] theorem tapes_inr (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e)) | |
| (e : E) : tapes source extra (.inr e) = extra e := rfl | |
| def label (haltTarget : Option (Λ ⊕ Λextra)) : Option Λ → Option (Λ ⊕ Λextra) | |
| | none => haltTarget | |
| | some l => some (.inl l) | |
| def configuration (haltTarget : Option (Λ ⊕ Λextra)) (extraState : τ) | |
| (extraTapes : ∀ e, List (Δ e)) (c : Cfg Γ Λ σ) : | |
| Cfg (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ) where | |
| l := label haltTarget c.l | |
| var := (c.var, extraState) | |
| stk := tapes c.stk extraTapes | |
| def statement (haltTarget : Option (Λ ⊕ Λextra)) : | |
| Stmt Γ Λ σ → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ) | |
| | .push k f next => .push (.inl k) (fun st => f st.1) (statement haltTarget next) | |
| | .peek k f next => .peek (.inl k) (fun st v => (f st.1 v, st.2)) | |
| (statement haltTarget next) | |
| | .pop k f next => .pop (.inl k) (fun st v => (f st.1 v, st.2)) | |
| (statement haltTarget next) | |
| | .load f next => .load (fun st => (f st.1, st.2)) (statement haltTarget next) | |
| | .branch f yes no => .branch (fun st => f st.1) | |
| (statement haltTarget yes) (statement haltTarget no) | |
| | .goto f => .goto (fun st => .inl (f st.1)) | |
| | .halt => match haltTarget with | |
| | none => .halt | |
| | some l => .goto (fun _ => l) | |
| variable [DecidableEq K] [DecidableEq E] | |
| theorem tapes_update (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e)) | |
| (k : K) (value : List (Γ k)) : | |
| tapes (Function.update source k value) extra = | |
| Function.update (tapes source extra) (.inl k) value := by | |
| funext j | |
| cases j with | |
| | inl j => | |
| by_cases h : j = k | |
| · subst j | |
| simp [Function.update] | |
| · simp [Function.update, h] | |
| | inr e => simp [Function.update] | |
| theorem stepAux_simulation (haltTarget : Option (Λ ⊕ Λextra)) | |
| (extraState : τ) (extraTapes : ∀ e, List (Δ e)) | |
| (q : Stmt Γ Λ σ) (state : σ) (source : ∀ k, List (Γ k)) : | |
| stepAux (statement haltTarget q) (state, extraState) (tapes source extraTapes) = | |
| configuration haltTarget extraState extraTapes (stepAux q state source) := by | |
| induction q generalizing state source with | |
| | push k f next ih => | |
| simp only [statement, stepAux, tapes_inl] | |
| rw [← tapes_update] | |
| exact ih state (Function.update source k (f state :: source k)) | |
| | peek k f next ih => | |
| simpa only [statement, stepAux, tapes_inl] using | |
| ih (f state (source k).head?) source | |
| | pop k f next ih => | |
| simp only [statement, stepAux, tapes_inl] | |
| rw [← tapes_update] | |
| exact ih (f state (source k).head?) (Function.update source k (source k).tail) | |
| | load f next ih => | |
| simpa only [statement, stepAux] using ih (f state) source | |
| | branch f yes no ihYes ihNo => | |
| cases h : f state with | |
| | false => simpa only [statement, stepAux, h, Bool.cond_false] using ihNo state source | |
| | true => simpa only [statement, stepAux, h, Bool.cond_true] using ihYes state source | |
| | goto f => rfl | |
| | halt => cases haltTarget <;> rfl | |
| theorem stepAux_preserves_extra_state (haltTarget : Option (Λ ⊕ Λextra)) | |
| (extraState : τ) (extraTapes : ∀ e, List (Δ e)) | |
| (q : Stmt Γ Λ σ) (state : σ) (source : ∀ k, List (Γ k)) : | |
| (stepAux (statement haltTarget q) (state, extraState) | |
| (tapes source extraTapes)).var.2 = extraState := by | |
| rw [stepAux_simulation] | |
| rfl | |
| theorem stepAux_preserves_extra_tape (haltTarget : Option (Λ ⊕ Λextra)) | |
| (extraState : τ) (extraTapes : ∀ e, List (Δ e)) | |
| (q : Stmt Γ Λ σ) (state : σ) (source : ∀ k, List (Γ k)) (e : E) : | |
| (stepAux (statement haltTarget q) (state, extraState) | |
| (tapes source extraTapes)).stk (.inr e) = extraTapes e := by | |
| rw [stepAux_simulation] | |
| rfl | |
| def program (haltTarget : Option (Λ ⊕ Λextra)) (source : Λ → Stmt Γ Λ σ) | |
| (extra : Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ)) : | |
| Λ ⊕ Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ) | |
| | .inl l => statement haltTarget (source l) | |
| | .inr l => extra l | |
| theorem step_running (haltTarget : Option (Λ ⊕ Λextra)) | |
| (extraState : τ) (extraTapes : ∀ e, List (Δ e)) | |
| (source : Λ → Stmt Γ Λ σ) | |
| (extra : Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ)) | |
| (l : Λ) (state : σ) (sourceTapes : ∀ k, List (Γ k)) : | |
| step (program haltTarget source extra) | |
| (configuration haltTarget extraState extraTapes ⟨some l, state, sourceTapes⟩) = | |
| some (configuration haltTarget extraState extraTapes | |
| (stepAux (source l) state sourceTapes)) := by | |
| change some (stepAux (statement haltTarget (source l)) (state, extraState) | |
| (tapes sourceTapes extraTapes)) = _ | |
| rw [stepAux_simulation] | |
| theorem step_simulation (haltTarget : Option (Λ ⊕ Λextra)) | |
| (extraState : τ) (extraTapes : ∀ e, List (Δ e)) | |
| (source : Λ → Stmt Γ Λ σ) | |
| (extra : Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ)) | |
| (a b : Cfg Γ Λ σ) (h : step source a = some b) : | |
| step (program haltTarget source extra) | |
| (configuration haltTarget extraState extraTapes a) = | |
| some (configuration haltTarget extraState extraTapes b) := by | |
| cases a with | |
| | mk l state sourceTapes => | |
| cases l with | |
| | none => simp [step] at h | |
| | some l => | |
| have hb : stepAux (source l) state sourceTapes = b := Option.some.inj h | |
| rw [← hb] | |
| exact step_running haltTarget extraState extraTapes source extra l state sourceTapes | |
| end DFVSGames.Foundations.Complexity.MachineEmbedding | |
| namespace DFVSGames.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 DFVSGames.Foundations.Complexity.MachineComposition | |
| namespace DFVSGames.Reduction.MachineTransfer | |
| open Turing | |
| abbrev alphabet (α β : Type) : Bool → Type | |
| | false => α | |
| | true => β | |
| variable {α β : Type} [Fintype α] [Fintype β] | |
| def loop (f : α → β) (fallback : β) : | |
| TM2.Stmt (alphabet α β) Unit (Option β) := | |
| .pop false (fun _ head => head.map f) | |
| (.branch Option.isSome | |
| (.push true (fun state => state.getD fallback) (.goto fun _ => ())) | |
| .halt) | |
| def machine (f : α → β) (fallback : β) : FinTM2 where | |
| K := Bool | |
| k₀ := false | |
| k₁ := true | |
| Γ := alphabet α β | |
| Λ := Unit | |
| main := () | |
| σ := Option β | |
| initialState := none | |
| Γk₀Fin := inferInstanceAs (Fintype α) | |
| m _ := loop f fallback | |
| def tapeStacks (input : List α) (output : List β) : | |
| (side : Bool) → List (alphabet α β side) | |
| | false => input | |
| | true => output | |
| def running (f : α → β) (fallback : β) (input : List α) (output : List β) | |
| (state : Option β) : (machine f fallback).Cfg := | |
| ⟨some (), state, tapeStacks input output⟩ | |
| def halted (f : α → β) (fallback : β) (output : List β) : (machine f fallback).Cfg := | |
| ⟨none, none, tapeStacks (α := α) [] output⟩ | |
| omit [Fintype α] [Fintype β] in | |
| private theorem update_input_inline_MachineTransfer (input replacement : List α) (output : List β) : | |
| Function.update (tapeStacks input output) false replacement = tapeStacks replacement output := by | |
| funext side | |
| cases side <;> rfl | |
| omit [Fintype α] [Fintype β] in | |
| private theorem update_output_inline_MachineTransfer (input : List α) (output replacement : List β) : | |
| Function.update (tapeStacks input output) true replacement = tapeStacks input replacement := by | |
| funext side | |
| cases side <;> rfl | |
| theorem step_empty (f : α → β) (fallback : β) (output : List β) (state : Option β) : | |
| (machine f fallback).step (running f fallback [] output state) = | |
| some (halted f fallback output) := by | |
| change some (TM2.stepAux (loop f fallback) state (tapeStacks (α := α) [] output)) = _ | |
| simp [loop, TM2.stepAux, tapeStacks, halted, Function.update] | |
| rw [update_input_inline_MachineTransfer] | |
| rfl | |
| theorem step_cons (f : α → β) (fallback : β) (head : α) (input : List α) | |
| (output : List β) (state : Option β) : | |
| (machine f fallback).step (running f fallback (head :: input) output state) = | |
| some (running f fallback input (f head :: output) (some (f head))) := by | |
| change some (TM2.stepAux (loop f fallback) state (tapeStacks (head :: input) output)) = _ | |
| simp [loop, TM2.stepAux, tapeStacks, running, Function.update] | |
| rw [update_input_inline_MachineTransfer, update_output_inline_MachineTransfer] | |
| rfl | |
| def next (f : α → β) (fallback : β) (configuration : Option (machine f fallback).Cfg) : | |
| Option (machine f fallback).Cfg := configuration.bind (machine f fallback).step | |
| theorem transfer_steps (f : α → β) (fallback : β) (input : List α) (output : List β) | |
| (state : Option β) : | |
| (next f fallback)^[input.length + 1] (some (running f fallback input output state)) = | |
| some (halted f fallback (input.reverse.map f ++ output)) := by | |
| induction input generalizing output state with | |
| | nil => | |
| simpa only [List.length_nil, Nat.zero_add, Function.iterate_one, next, | |
| Option.bind_some, List.reverse_nil, List.map_nil, List.nil_append] | |
| using step_empty f fallback output state | |
| | cons head input ih => | |
| rw [List.length_cons, Function.iterate_succ_apply] | |
| change (next f fallback)^[input.length + 1] | |
| ((machine f fallback).step (running f fallback (head :: input) output state)) = _ | |
| rw [step_cons, ih] | |
| simp only [List.reverse_cons, List.map_append, List.map_singleton, | |
| List.append_assoc, List.singleton_append] | |
| theorem initList_eq (f : α → β) (fallback : β) (input : List α) : | |
| initList (machine f fallback) input = running f fallback input [] none := by | |
| unfold initList running | |
| congr 1 | |
| funext side | |
| cases side <;> rfl | |
| theorem haltList_eq (f : α → β) (fallback : β) (output : List β) : | |
| haltList (machine f fallback) output = halted f fallback output := by | |
| unfold haltList halted | |
| congr 1 | |
| funext side | |
| cases side <;> rfl | |
| theorem transfer_init_steps (f : α → β) (fallback : β) (input : List α) : | |
| (next f fallback)^[input.length + 1] (some (initList (machine f fallback) input)) = | |
| some (haltList (machine f fallback) (input.reverse.map f)) := by | |
| calc | |
| _ = (next f fallback)^[input.length + 1] | |
| (some (running f fallback input [] none)) := | |
| congrArg _ (congrArg some (initList_eq f fallback input)) | |
| _ = some (halted f fallback (input.reverse.map f)) := by | |
| simpa only [List.append_nil] using transfer_steps f fallback input [] none | |
| _ = _ := congrArg some (haltList_eq f fallback (input.reverse.map f)).symm | |
| def outputsInTime (f : α → β) (fallback : β) (input : List α) : | |
| TM2OutputsInTime (machine f fallback) input (some (input.reverse.map f)) | |
| (input.length + 1) where | |
| steps := input.length + 1 | |
| evals_in_steps := transfer_init_steps f fallback input | |
| steps_le_m := Nat.le_refl _ | |
| @[simp] theorem outputsInTime_steps (f : α → β) (fallback : β) (input : List α) : | |
| (outputsInTime f fallback input).steps = input.length + 1 := rfl | |
| noncomputable def computableInPolyTime (f : α → β) (fallback : β) : | |
| TM2ComputableInPolyTime (id : List α → List α) (id : List β → List β) | |
| (fun input => input.reverse.map f) where | |
| tm := machine f fallback | |
| inputAlphabet := Equiv.refl α | |
| outputAlphabet := Equiv.refl β | |
| time := Polynomial.X + 1 | |
| outputsFun input := by | |
| change TM2OutputsInTime (machine f fallback) (input.map id) | |
| (some ((input.reverse.map f).map id)) | |
| ((Polynomial.X + 1 : Polynomial Nat).eval input.length) | |
| simpa only [machine, List.map_id, Polynomial.eval_add, Polynomial.eval_X, Polynomial.eval_one] | |
| using outputsInTime f fallback input | |
| variable {K Λ σ : Type} {Γ : K → Type} [DecidableEq K] | |
| def exitAt (dst : K) (exit : Option Λ) : TM2.Stmt Γ Λ (σ × Option (Γ dst)) := | |
| match exit with | |
| | none => .halt | |
| | some label => .goto fun _ => label | |
| def loopAt (src dst : K) (f : Γ src → Γ dst) (fallback : Γ dst) | |
| (loopLabel : Λ) (exit : Option Λ) : TM2.Stmt Γ Λ (σ × Option (Γ dst)) := | |
| .pop src (fun state head => (state.1, head.map f)) | |
| (.branch (fun state => state.2.isSome) | |
| (.push dst (fun state => state.2.getD fallback) (.goto fun _ => loopLabel)) | |
| (exitAt dst exit)) | |
| def tapesAt (src dst : K) (base : (k : K) → List (Γ k)) | |
| (input : List (Γ src)) (output : List (Γ dst)) : (k : K) → List (Γ k) := | |
| Function.update (Function.update base src input) dst output | |
| @[simp] theorem tapesAt_src (src dst : K) (distinct : src ≠ dst) | |
| (base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst)) : | |
| tapesAt src dst base input output src = input := by | |
| simp [tapesAt, distinct] | |
| @[simp] theorem tapesAt_dst (src dst : K) | |
| (base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst)) : | |
| tapesAt src dst base input output dst = output := by | |
| simp [tapesAt] | |
| theorem tapesAt_other (src dst k : K) (notSrc : k ≠ src) (notDst : k ≠ dst) | |
| (base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst)) : | |
| tapesAt src dst base input output k = base k := by | |
| simp [tapesAt, notSrc, notDst] | |
| @[simp] theorem tapesAt_self (src dst : K) (base : (k : K) → List (Γ k)) : | |
| tapesAt src dst base (base src) (base dst) = base := by | |
| simp [tapesAt] | |
| private theorem update_tapesAt_src_inline_MachineTransfer (src dst : K) (distinct : src ≠ dst) | |
| (base : (k : K) → List (Γ k)) (input replacement : List (Γ src)) | |
| (output : List (Γ dst)) : | |
| Function.update (tapesAt src dst base input output) src replacement = | |
| tapesAt src dst base replacement output := by | |
| funext k | |
| by_cases hs : k = src | |
| · subst k | |
| simp [tapesAt, distinct] | |
| · by_cases hd : k = dst | |
| · subst k | |
| simp [tapesAt, Ne.symm distinct] | |
| · simp [tapesAt, hs, hd] | |
| private theorem update_tapesAt_dst_inline_MachineTransfer (src dst : K) | |
| (base : (k : K) → List (Γ k)) (input : List (Γ src)) | |
| (output replacement : List (Γ dst)) : | |
| Function.update (tapesAt src dst base input output) dst replacement = | |
| tapesAt src dst base input replacement := by | |
| funext k | |
| by_cases hd : k = dst | |
| · subst k | |
| simp [tapesAt] | |
| · simp [tapesAt, hd] | |
| def nextAt (dst : K) (program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst))) | |
| (configuration : Option (TM2.Cfg Γ Λ (σ × Option (Γ dst)))) : | |
| Option (TM2.Cfg Γ Λ (σ × Option (Γ dst))) := | |
| configuration.bind (TM2.step program) | |
| theorem stepAt_empty (src dst : K) (distinct : src ≠ dst) | |
| (f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst))) | |
| (atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit) | |
| (base : (k : K) → List (Γ k)) (output : List (Γ dst)) | |
| (ambient : σ) (register : Option (Γ dst)) : | |
| TM2.step program ⟨some loopLabel, (ambient, register), tapesAt src dst base [] output⟩ = | |
| some ⟨exit, (ambient, none), tapesAt src dst base [] output⟩ := by | |
| change some (TM2.stepAux (program loopLabel) (ambient, register) | |
| (tapesAt src dst base [] output)) = _ | |
| rw [atLoop] | |
| cases exit <;> | |
| simp [loopAt, exitAt, TM2.stepAux, tapesAt_src, distinct, update_tapesAt_src_inline_MachineTransfer] | |
| theorem stepAt_cons (src dst : K) (distinct : src ≠ dst) | |
| (f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst))) | |
| (atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit) | |
| (base : (k : K) → List (Γ k)) (head : Γ src) (input : List (Γ src)) | |
| (output : List (Γ dst)) (ambient : σ) (register : Option (Γ dst)) : | |
| TM2.step program | |
| ⟨some loopLabel, (ambient, register), tapesAt src dst base (head :: input) output⟩ = | |
| some ⟨some loopLabel, (ambient, some (f head)), | |
| tapesAt src dst base input (f head :: output)⟩ := by | |
| change some (TM2.stepAux (program loopLabel) (ambient, register) | |
| (tapesAt src dst base (head :: input) output)) = _ | |
| rw [atLoop] | |
| simp [loopAt, TM2.stepAux, tapesAt_src, tapesAt_dst, distinct, | |
| update_tapesAt_src_inline_MachineTransfer, update_tapesAt_dst_inline_MachineTransfer] | |
| theorem transferAt_steps (src dst : K) (distinct : src ≠ dst) | |
| (f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst))) | |
| (atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit) | |
| (base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst)) | |
| (ambient : σ) (register : Option (Γ dst)) : | |
| (nextAt dst program)^[input.length + 1] | |
| (some ⟨some loopLabel, (ambient, register), tapesAt src dst base input output⟩) = | |
| some ⟨exit, (ambient, none), tapesAt src dst base [] (input.reverse.map f ++ output)⟩ := by | |
| induction input generalizing output register with | |
| | nil => | |
| simpa only [List.length_nil, Nat.zero_add, Function.iterate_one, nextAt, | |
| Option.bind_some, List.reverse_nil, List.map_nil, List.nil_append] | |
| using stepAt_empty src dst distinct f fallback loopLabel exit program atLoop base output | |
| ambient register | |
| | cons head input ih => | |
| rw [List.length_cons, Function.iterate_succ_apply] | |
| change (nextAt dst program)^[input.length + 1] | |
| (TM2.step program | |
| ⟨some loopLabel, (ambient, register), tapesAt src dst base (head :: input) output⟩) = _ | |
| rw [stepAt_cons src dst distinct f fallback loopLabel exit program atLoop, ih] | |
| simp only [List.reverse_cons, List.map_append, List.map_singleton, | |
| List.append_assoc, List.singleton_append] | |
| theorem transferAt_fromTapes (src dst : K) (distinct : src ≠ dst) | |
| (f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst))) | |
| (atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit) | |
| (base : (k : K) → List (Γ k)) (ambient : σ) (register : Option (Γ dst)) : | |
| (nextAt dst program)^[(base src).length + 1] | |
| (some ⟨some loopLabel, (ambient, register), base⟩) = | |
| some ⟨exit, (ambient, none), | |
| tapesAt src dst base [] ((base src).reverse.map f ++ base dst)⟩ := by | |
| simpa only [tapesAt_self] using | |
| transferAt_steps src dst distinct f fallback loopLabel exit program atLoop base | |
| (base src) (base dst) ambient register | |
| def transferAtInTime (src dst : K) (distinct : src ≠ dst) | |
| (f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst))) | |
| (atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit) | |
| (base : (k : K) → List (Γ k)) (ambient : σ) (register : Option (Γ dst)) : | |
| StateTransition.EvalsToInTime (TM2.step program) | |
| ⟨some loopLabel, (ambient, register), base⟩ | |
| (some ⟨exit, (ambient, none), | |
| tapesAt src dst base [] ((base src).reverse.map f ++ base dst)⟩) | |
| ((base src).length + 1) where | |
| steps := (base src).length + 1 | |
| evals_in_steps := transferAt_fromTapes src dst distinct f fallback loopLabel exit | |
| program atLoop base ambient register | |
| steps_le_m := Nat.le_refl _ | |
| end DFVSGames.Reduction.MachineTransfer | |
| namespace DFVSGames.Foundations.Complexity.MachineCopy | |
| open Turing | |
| variable {K Λ σ β : Type} [DecidableEq K] | |
| abbrev Alphabet (β : Type) (_ : K) := β | |
| 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_inline_MachineCopy (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_inline_MachineCopy (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_inline_MachineCopy (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_inline_MachineCopy] | |
| 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_inline_MachineCopy, update_fork_left_inline_MachineCopy, update_fork_right_inline_MachineCopy] | |
| 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 | |
| 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 DFVSGames.Foundations.Complexity.MachineCopy | |
| end OAI | |