File size: 16,700 Bytes
760f90e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | 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
|