Download lean/OAI/Computability/UniqueGames/Machines/MachineControlLemmas.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 16.7 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/UniqueGames/Machines/MachineControlLemmas.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Computability/UniqueGames/Machines/MachineControlLemmas.lean
-
curl -L -o MachineControlLemmas.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Computability/UniqueGames/Machines/MachineControlLemmas.lean
16.7 kB
| import Mathlib.Computability.TuringMachine.StackTuringMachine | |
| import Mathlib.Logic.Equiv.Basic | |
| namespace OAI | |
| section | |
| /-! Static renaming of labels and finite internal states. This does not execute | |
| a data-copying phase and preserves every actual transition exactly. -/ | |
| namespace UniqueGamesTheorem.Foundations.Complexity.MachineControl | |
| open Turing.TM2 | |
| variable {K Λ Λ' σ σ' : Type} {Γ : K → Type} | |
| def configuration (labels : Λ → Λ') (states : σ ≃ σ') (c : Cfg Γ Λ σ) : | |
| Cfg Γ Λ' σ' where | |
| l := c.l.map labels | |
| var := states c.var | |
| stk := c.stk | |
| def statement (labels : Λ → Λ') (states : σ ≃ σ') : Stmt Γ Λ σ → Stmt Γ Λ' σ' | |
| | .push k f next => .push k (fun st => f (states.symm st)) (statement labels states next) | |
| | .peek k f next => .peek k (fun st v => states (f (states.symm st) v)) | |
| (statement labels states next) | |
| | .pop k f next => .pop k (fun st v => states (f (states.symm st) v)) | |
| (statement labels states next) | |
| | .load f next => .load (fun st => states (f (states.symm st))) | |
| (statement labels states next) | |
| | .branch f yes no => .branch (fun st => f (states.symm st)) | |
| (statement labels states yes) (statement labels states no) | |
| | .goto f => .goto (fun st => labels (f (states.symm st))) | |
| | .halt => .halt | |
| variable [DecidableEq K] | |
| theorem stepAux_simulation (labels : Λ → Λ') (states : σ ≃ σ') | |
| (q : Stmt Γ Λ σ) (state : σ) (tapes : ∀ k, List (Γ k)) : | |
| stepAux (statement labels states q) (states state) tapes = | |
| configuration labels states (stepAux q state tapes) := by | |
| induction q generalizing state tapes with | |
| | push k f next ih => | |
| simpa only [statement, stepAux, Equiv.symm_apply_apply] using | |
| ih state (Function.update tapes k (f state :: tapes k)) | |
| | peek k f next ih => | |
| simpa only [statement, stepAux, Equiv.symm_apply_apply] using | |
| ih (f state (tapes k).head?) tapes | |
| | pop k f next ih => | |
| simpa only [statement, stepAux, Equiv.symm_apply_apply] using | |
| ih (f state (tapes k).head?) (Function.update tapes k (tapes k).tail) | |
| | load f next ih => | |
| simpa only [statement, stepAux, Equiv.symm_apply_apply] using ih (f state) tapes | |
| | branch f yes no ihYes ihNo => | |
| cases h : f state with | |
| | false => | |
| simpa only [statement, stepAux, Equiv.symm_apply_apply, h, Bool.cond_false] using | |
| ihNo state tapes | |
| | true => | |
| simpa only [statement, stepAux, Equiv.symm_apply_apply, h, Bool.cond_true] using | |
| ihYes state tapes | |
| | goto f => simp [statement, stepAux, configuration] | |
| | halt => rfl | |
| def program (labels : Λ ≃ Λ') (states : σ ≃ σ') (source : Λ → Stmt Γ Λ σ) : | |
| Λ' → Stmt Γ Λ' σ' := fun l => statement labels states (source (labels.symm l)) | |
| /-- The renamed program is conjugate to the original transition function, | |
| including the halted configuration. -/ | |
| theorem step_simulation (labels : Λ ≃ Λ') (states : σ ≃ σ') | |
| (source : Λ → Stmt Γ Λ σ) (c : Cfg Γ Λ σ) : | |
| step (program labels states source) (configuration labels states c) = | |
| (step source c).map (configuration labels states) := by | |
| cases c with | |
| | mk l state tapes => | |
| cases l with | |
| | none => rfl | |
| | some l => | |
| change some (stepAux (statement labels states (source (labels.symm (labels l)))) | |
| (states state) tapes) = _ | |
| rw [Equiv.symm_apply_apply, stepAux_simulation] | |
| rfl | |
| end UniqueGamesTheorem.Foundations.Complexity.MachineControl | |
| end | |
| section | |
| /-! Exact embedding of a finite-stack program into disjoint stacks and labels. | |
| An original halt can either halt or enter an explicit continuation label. | |
| The simulation preserves all extra stacks and the second state component. -/ | |
| namespace UniqueGamesTheorem.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] | |
| /-- One finite statement executes identically on the original stacks and state. | |
| The entire statement is simulated in exactly one TM2 transition. -/ | |
| 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 UniqueGamesTheorem.Foundations.Complexity.MachineEmbedding | |
| end | |
| section | |
| /-! Static exchange of the first two groups of stacks. Symbol types and stack | |
| contents are retained exactly; no machine transition is spent on this layout | |
| change. This aligns the two component machines in sequential composition. -/ | |
| namespace UniqueGamesTheorem.Foundations.Complexity.MachineStackSwap | |
| open Turing.TM2 | |
| variable {A B C Λ σ : Type} | |
| {ΓA : A → Type} {ΓB : B → Type} {ΓC : C → Type} | |
| abbrev Alphabet (ΓA : A → Type) (ΓB : B → Type) (ΓC : C → Type) := | |
| MachineEmbedding.Alphabet ΓA (MachineEmbedding.Alphabet ΓB ΓC) | |
| def tapes (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) : | |
| ∀ j, List (Alphabet ΓB ΓA ΓC j) | |
| | .inl b => source (.inr (.inl b)) | |
| | .inr (.inl a) => source (.inl a) | |
| | .inr (.inr c) => source (.inr (.inr c)) | |
| @[simp] theorem tapes_first (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) (a : A) : | |
| tapes source (.inr (.inl a)) = source (.inl a) := rfl | |
| @[simp] theorem tapes_second (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) (b : B) : | |
| tapes source (.inl b) = source (.inr (.inl b)) := rfl | |
| @[simp] theorem tapes_third (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) (c : C) : | |
| tapes source (.inr (.inr c)) = source (.inr (.inr c)) := rfl | |
| theorem tapes_involutive (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) : | |
| tapes (tapes source) = source := by | |
| funext k | |
| rcases k with a | b | c <;> rfl | |
| def configuration (c : Cfg (Alphabet ΓA ΓB ΓC) Λ σ) : | |
| Cfg (Alphabet ΓB ΓA ΓC) Λ σ where | |
| l := c.l | |
| var := c.var | |
| stk := tapes c.stk | |
| def statement : Stmt (Alphabet ΓA ΓB ΓC) Λ σ → Stmt (Alphabet ΓB ΓA ΓC) Λ σ | |
| | .push (.inl a) f next => .push (.inr (.inl a)) f (statement next) | |
| | .push (.inr (.inl b)) f next => .push (.inl b) f (statement next) | |
| | .push (.inr (.inr c)) f next => .push (.inr (.inr c)) f (statement next) | |
| | .peek (.inl a) f next => .peek (.inr (.inl a)) f (statement next) | |
| | .peek (.inr (.inl b)) f next => .peek (.inl b) f (statement next) | |
| | .peek (.inr (.inr c)) f next => .peek (.inr (.inr c)) f (statement next) | |
| | .pop (.inl a) f next => .pop (.inr (.inl a)) f (statement next) | |
| | .pop (.inr (.inl b)) f next => .pop (.inl b) f (statement next) | |
| | .pop (.inr (.inr c)) f next => .pop (.inr (.inr c)) f (statement next) | |
| | .load f next => .load f (statement next) | |
| | .branch f yes no => .branch f (statement yes) (statement no) | |
| | .goto f => .goto f | |
| | .halt => .halt | |
| variable [DecidableEq A] [DecidableEq B] [DecidableEq C] | |
| theorem tapes_update_first (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) | |
| (a : A) (value : List (ΓA a)) : | |
| tapes (Function.update source (.inl a) value) = | |
| Function.update (tapes source) (.inr (.inl a)) value := by | |
| funext j | |
| rcases j with b | a' | c | |
| · simp [Function.update] | |
| · by_cases h : a' = a | |
| · subst a'; simp [Function.update] | |
| · simp [Function.update, h] | |
| · simp [Function.update] | |
| theorem tapes_update_second (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) | |
| (b : B) (value : List (ΓB b)) : | |
| tapes (Function.update source (.inr (.inl b)) value) = | |
| Function.update (tapes source) (.inl b) value := by | |
| funext j | |
| rcases j with b' | a | c | |
| · by_cases h : b' = b | |
| · subst b'; simp [Function.update] | |
| · simp [Function.update, h] | |
| · simp [Function.update] | |
| · simp [Function.update] | |
| theorem tapes_update_third (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) | |
| (c : C) (value : List (ΓC c)) : | |
| tapes (Function.update source (.inr (.inr c)) value) = | |
| Function.update (tapes source) (.inr (.inr c)) value := by | |
| funext j | |
| rcases j with b | a | c' | |
| · simp [Function.update] | |
| · simp [Function.update] | |
| · by_cases h : c' = c | |
| · subst c'; simp [Function.update] | |
| · simp [Function.update, h] | |
| theorem stepAux_simulation (q : Stmt (Alphabet ΓA ΓB ΓC) Λ σ) | |
| (state : σ) (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) : | |
| stepAux (statement q) state (tapes source) = | |
| configuration (stepAux q state source) := by | |
| induction q generalizing state source with | |
| | push k f next ih => | |
| rcases k with a | b | c | |
| · simp only [statement, stepAux, tapes_first] | |
| rw [← tapes_update_first] | |
| exact ih state (Function.update source (.inl a) (f state :: source (.inl a))) | |
| · simp only [statement, stepAux, tapes_second] | |
| rw [← tapes_update_second] | |
| exact ih state (Function.update source (.inr (.inl b)) | |
| (f state :: source (.inr (.inl b)))) | |
| · simp only [statement, stepAux, tapes_third] | |
| rw [← tapes_update_third] | |
| exact ih state (Function.update source (.inr (.inr c)) | |
| (f state :: source (.inr (.inr c)))) | |
| | peek k f next ih => | |
| rcases k with a | b | c | |
| · simpa only [statement, stepAux, tapes_first] using | |
| ih (f state (source (.inl a)).head?) source | |
| · simpa only [statement, stepAux, tapes_second] using | |
| ih (f state (source (.inr (.inl b))).head?) source | |
| · simpa only [statement, stepAux, tapes_third] using | |
| ih (f state (source (.inr (.inr c))).head?) source | |
| | pop k f next ih => | |
| rcases k with a | b | c | |
| · simp only [statement, stepAux, tapes_first] | |
| rw [← tapes_update_first] | |
| exact ih (f state (source (.inl a)).head?) | |
| (Function.update source (.inl a) (source (.inl a)).tail) | |
| · simp only [statement, stepAux, tapes_second] | |
| rw [← tapes_update_second] | |
| exact ih (f state (source (.inr (.inl b))).head?) | |
| (Function.update source (.inr (.inl b)) (source (.inr (.inl b))).tail) | |
| · simp only [statement, stepAux, tapes_third] | |
| rw [← tapes_update_third] | |
| exact ih (f state (source (.inr (.inr c))).head?) | |
| (Function.update source (.inr (.inr c)) (source (.inr (.inr c))).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 => rfl | |
| def program (source : Λ → Stmt (Alphabet ΓA ΓB ΓC) Λ σ) : | |
| Λ → Stmt (Alphabet ΓB ΓA ΓC) Λ σ := fun l => statement (source l) | |
| theorem step_simulation (source : Λ → Stmt (Alphabet ΓA ΓB ΓC) Λ σ) | |
| (c : Cfg (Alphabet ΓA ΓB ΓC) Λ σ) : | |
| step (program source) (configuration c) = (step source c).map configuration := by | |
| cases c with | |
| | mk l state sourceTapes => | |
| cases l with | |
| | none => rfl | |
| | some l => | |
| change some (stepAux (statement (source l)) state (tapes sourceTapes)) = _ | |
| rw [stepAux_simulation] | |
| rfl | |
| end UniqueGamesTheorem.Foundations.Complexity.MachineStackSwap | |
| end | |
| end OAI | |