File size: 18,285 Bytes
e320e82 | 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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | import Mathlib
import OAI.Combinatorics.IndependentSets.Machines.MachineUnaryAffine
import OAI.Combinatorics.IndependentSets.Machines.MachineCloudPrefix
import OAI.Combinatorics.IndependentSets.Machines.MachinePreservingLookupClean
namespace OAI
namespace IndependentSetsGames.Foundations.Complexity.MachineRegularTable
open Turing MachineComposition
open IndependentSetsGames.Foundations.PCP
namespace Header
inductive Extra
| m | index | work | scratch | vertices | darts | emit | output
deriving DecidableEq
instance : Fintype Extra where
elems := {.m, .index, .work, .scratch, .vertices, .darts, .emit, .output}
complete value := by cases value <;> simp
abbrev Tape := MachineCloudPrefix.Tape ⊕ Extra
abbrev Alphabet (_ : Tape) := Bool
abbrev State (σ : Type) := MachineCloudPrefix.State σ
def tableTape : Tape := .inl (.inner .table)
def nTape : Tape := .inl .bound
def queryTape : Tape := .inl (.inner .query)
def fuelTape : Tape := .inl .remaining
def sumTape : Tape := .inl .total
def mTape : Tape := .inr .m
def indexTape : Tape := .inr .index
def workTape : Tape := .inr .work
def scratchTape : Tape := .inr .scratch
def verticesTape : Tape := .inr .vertices
def dartsTape : Tape := .inr .darts
def emitTape : Tape := .inr .emit
def outputTape : Tape := .inr .output
def memory (inner : MachineCloudPrefix.Tape → List Bool)
(m index work scratch vertices darts emit output : List Bool) : Tape → List Bool
| .inl k => inner k
| .inr .m => m
| .inr .index => index
| .inr .work => work
| .inr .scratch => scratch
| .inr .vertices => vertices
| .inr .darts => darts
| .inr .emit => emit
| .inr .output => output
def frame (t n v f s m i w a N R e o : List Bool) : Tape → List Bool :=
memory (MachineCloudPrefix.frame t v [] [] [] n f s) m i w a N R e o
@[simp] theorem frame_table (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o tableTape = t := rfl
@[simp] theorem frame_n (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o nTape = n := rfl
@[simp] theorem frame_query (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o queryTape = v := rfl
@[simp] theorem frame_fuel (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o fuelTape = f := rfl
@[simp] theorem frame_sum (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o sumTape = s := rfl
@[simp] theorem frame_m (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o mTape = m := rfl
@[simp] theorem frame_index (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o indexTape = i := rfl
@[simp] theorem frame_work (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o workTape = w := rfl
@[simp] theorem frame_scratch (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o scratchTape = a := rfl
@[simp] theorem frame_vertices (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o verticesTape = N := rfl
@[simp] theorem frame_darts (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o dartsTape = R := rfl
@[simp] theorem frame_emit (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o emitTape = e := rfl
@[simp] theorem frame_output (t n v f s m i w a N R e o : List Bool) :
frame t n v f s m i w a N R e o outputTape = o := rfl
@[simp] theorem update_frame_table (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) tableTape x =
frame x n v f s m i w a N R e o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_n (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) nTape x =
frame t x v f s m i w a N R e o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_query (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) queryTape x =
frame t n x f s m i w a N R e o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_fuel (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) fuelTape x =
frame t n v x s m i w a N R e o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_sum (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) sumTape x =
frame t n v f x m i w a N R e o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_m (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) mTape x =
frame t n v f s x i w a N R e o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_index (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) indexTape x =
frame t n v f s m x w a N R e o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_work (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) workTape x =
frame t n v f s m i x a N R e o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_scratch (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) scratchTape x =
frame t n v f s m i w x N R e o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_vertices (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) verticesTape x =
frame t n v f s m i w a x R e o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_darts (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) dartsTape x =
frame t n v f s m i w a N x e o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_emit (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) emitTape x =
frame t n v f s m i w a N R x o := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
@[simp] theorem update_frame_output (t n v f s m i w a N R e o x : List Bool) :
Function.update (frame t n v f s m i w a N R e o) outputTape x =
frame t n v f s m i w a N R e x := by
funext k
cases k with
| inl k =>
cases k with
| inner j => cases j <;> rfl
| bound => rfl
| remaining => rfl
| total => rfl
| inr j => cases j <;> rfl
inductive Field
| vertices | darts
deriving DecidableEq
instance : Fintype Field where
elems := {.vertices, .darts}
complete value := by cases value <;> simp
inductive CopyPhase
| count | rEmit | nEmit
deriving DecidableEq
instance : Fintype CopyPhase where
elems := {.count, .rEmit, .nEmit}
complete value := by cases value <;> simp
inductive Label
| init | seedM | clearIndex
| lookup (field : Field) (label : MachinePreservingLookupClean.Label)
| prefix (label : MachineCloudPrefix.Label)
| copyFirst (phase : CopyPhase) | copySecond (phase : CopyPhase)
| add | scaleSeed | scaleScan | scaleRestore
| appendFirst | appendSecond | appendThird
| cleanupZeros | clearQuery | done
deriving DecidableEq, Fintype
def fieldTape : Field → Tape
| .vertices => nTape
| .darts => mTape
def fieldExit : Field → Label
| .vertices => .seedM
| .darts => .clearIndex
def lookupSlots (field : Field) : Fin 5 → Tape :=
![tableTape, indexTape, workTape, fieldTape field, scratchTape]
theorem lookupSlots_injective (field : Field) : Function.Injective (lookupSlots field) := by
intro i j h
cases field <;> fin_cases i <;> fin_cases j <;>
simp_all [lookupSlots, fieldTape, tableTape, indexTape, workTape, nTape, mTape, scratchTape]
def copySource : CopyPhase → Tape
| .count => mTape
| .rEmit => dartsTape
| .nEmit => verticesTape
def copyDestination : CopyPhase → Tape
| .count => verticesTape
| .rEmit | .nEmit => emitTape
def copyExit : CopyPhase → Label
| .count => .add
| .rEmit => .copyFirst .nEmit
| .nEmit => .appendFirst
def prefixView : Tape → Option MachineCloudPrefix.Tape
| .inl k => some k
| .inr _ => none
theorem prefixView_left (k : MachineCloudPrefix.Tape) : prefixView (.inl k) = some k := rfl
theorem prefixView_right (j : Tape) (k : MachineCloudPrefix.Tape)
(h : prefixView j = some k) : Sum.inl k = j := by
cases j <;> simp_all [prefixView]
theorem prefixTapes (inner : MachineCloudPrefix.Tape → List Bool) (extra : Tape → List Bool) :
MachineCloudPadding.Placement.tapes prefixView inner extra =
memory inner (extra mTape) (extra indexTape) (extra workTape) (extra scratchTape)
(extra verticesTape) (extra dartsTape) (extra emitTape) (extra outputTape) := by
funext j
cases j with
| inl k => rfl
| inr k => cases k <;> rfl
variable {σ : Type}
def program (q : Nat) : Label → TM2.Stmt Alphabet Label (State σ)
| .init => .push indexTape (fun _ => false) (.goto fun _ => .lookup .vertices (.run .copyFirst))
| .seedM => .push indexTape (fun _ => true) (.goto fun _ => .lookup .darts (.run .copyFirst))
| .clearIndex => .pop indexTape (fun state _ => (state.1, none))
(.goto fun _ => .prefix .init)
| .lookup field l => MachinePreservingLookupClean.statement (lookupSlots field)
(Label.lookup field) (some (fieldExit field)) l
| .prefix l => MachineCloudPadding.Placement.statement Sum.inl Label.prefix
(some (.copyFirst .count)) (MachineCloudPrefix.program l)
| .copyFirst phase => Reduction.MachineTransfer.loopAt (copySource phase) scratchTape
id false (.copyFirst phase) (some (.copySecond phase))
| .copySecond phase => MachineCopy.forkLoop scratchTape (copySource phase) (copyDestination phase)
false (.copySecond phase) (some (copyExit phase))
| .add => MachineUnaryAddAt.loop sumTape verticesTape .add (some .scaleSeed)
| .scaleSeed => MachineUnaryAffineAt.seed dartsTape 0 .scaleScan
| .scaleScan => MachineUnaryAffineAt.scan verticesTape scratchTape dartsTape (q + 1)
.scaleScan .scaleRestore
| .scaleRestore => Reduction.MachineTransfer.loopAt scratchTape verticesTape id false
.scaleRestore (some (.copyFirst .rEmit))
| .appendFirst => Reduction.MachineTransfer.loopAt outputTape scratchTape id false
.appendFirst (some .appendSecond)
| .appendSecond => Reduction.MachineTransfer.loopAt emitTape scratchTape id false
.appendSecond (some .appendThird)
| .appendThird => Reduction.MachineTransfer.loopAt scratchTape outputTape id false
.appendThird (some .cleanupZeros)
| .cleanupZeros => .pop sumTape (fun state _ => (state.1, none))
(.pop fuelTape (fun state _ => (state.1, none)) (.goto fun _ => .clearQuery))
| .clearQuery => MachineLookup.discard queryTape .clearQuery .done
| .done => .halt
theorem appendTrace {α : Type} (f : α → α) {a b : Nat} {x y z : α}
(hs : f^[a] x = y) (ht : f^[b] y = z) : f^[a + b] x = z := by
rw [Nat.add_comm a b, Function.iterate_add_apply, hs, ht]
def paddingTotal (t : GraphTables.Table) : Nat :=
PreprocessingPaddingOffsets.offset (PreprocessingRegularTables.padding t) t.vertices
def vertexTotal (t : GraphTables.Table) : Nat :=
PreprocessingRegularTables.vertexCount t (PreprocessingRegularTables.padding t)
def dartTotal (q : Nat) (t : GraphTables.Table) : Nat := vertexTotal t * (q + 1)
theorem vertexTotal_eq (t : GraphTables.Table) :
vertexTotal t = t.darts + paddingTotal t := by
unfold vertexTotal PreprocessingRegularTables.vertexCount paddingTotal
rw [PreprocessingPaddingOffsets.offset_all]
theorem initStep (q : Nat) (table output : List Bool) (ambient : σ)
(register : Option Bool) :
TM2.step (program q)
⟨some .init, ((ambient, false), register),
frame table [] [] [] [] [] [] [] [] [] [] [] output⟩ =
some ⟨some (.lookup .vertices (.run .copyFirst)), ((ambient, false), register),
frame table [] [] [] [] [] (encodeWord 0) [] [] [] [] [] output⟩ := by
simp [TM2.step, program, TM2.stepAux, encodeWord]
theorem seedMStep (q : Nat) (table n output : List Bool) (ambient : σ)
(register : Option Bool) :
TM2.step (program q)
⟨some .seedM, ((ambient, false), register),
frame table n [] [] [] [] (encodeWord 0) [] [] [] [] [] output⟩ =
some ⟨some (.lookup .darts (.run .copyFirst)), ((ambient, false), register),
frame table n [] [] [] [] (encodeWord 1) [] [] [] [] [] output⟩ := by
simp [TM2.step, program, TM2.stepAux, encodeWord]
theorem clearIndexStep (q : Nat) (table n m output : List Bool) (ambient : σ)
(register : Option Bool) :
TM2.step (program q)
⟨some .clearIndex, ((ambient, false), register),
frame table n [] [] [] m (encodeWord 0) [] [] [] [] [] output⟩ =
some ⟨some (.prefix .init), ((ambient, false), none),
frame table n [] [] [] m [] [] [] [] [] [] output⟩ := by
simp [TM2.step, program, TM2.stepAux, encodeWord]
def readTime (t : GraphTables.Table) : Nat :=
1 + MachinePreservingLookupClean.steps (GraphTables.tableWords t) 0 + 1 +
MachinePreservingLookupClean.steps (GraphTables.tableWords t) 1 + 1
theorem readTrace (q : Nat) (t : GraphTables.Table) (output : List Bool)
(ambient : σ) (register : Option Bool) :
(advance (TM2.step (program q)))^[readTime t]
(some ⟨some .init, ((ambient, false), register),
frame (GraphTables.tableBits t) [] [] [] [] [] [] [] [] [] [] [] output⟩) =
some ⟨some (.prefix .init), ((ambient, false), none),
frame (GraphTables.tableBits t) (encodeWord t.vertices) [] [] []
(encodeWord t.darts) [] [] [] [] [] [] output⟩ := by
have hi : (advance (TM2.step (program q)))^[1]
(some ⟨some .init, ((ambient, false), register),
frame (GraphTables.tableBits t) [] [] [] [] [] [] [] [] [] [] [] output⟩) =
some ⟨some (.lookup .vertices (.run .copyFirst)), ((ambient, false), register),
frame (GraphTables.tableBits t) [] [] [] [] [] (encodeWord 0) [] [] [] [] [] output⟩ :=
initStep q _ _ ambient register
have hn := MachinePreservingLookupClean.traceAt_fromTapes (lookupSlots .vertices)
(lookupSlots_injective .vertices) (Label.lookup .vertices) (some Label.seedM)
(program q) (fun _ => rfl)
(frame (GraphTables.tableBits t) [] [] [] [] [] (encodeWord 0) [] [] [] [] [] output)
(GraphTables.tableWords t) rfl rfl 0 t.vertices (by simp [GraphTables.tableWords]) []
(by simp [lookupSlots]) (by simp [lookupSlots]) (ambient, false) register
simp [MachinePreservingLookup.initialTapes, MachineLookup.tapes, lookupSlots, fieldTape] at hn
have hs : (advance (TM2.step (program q)))^[1]
(some ⟨some .seedM, ((ambient, false), none),
frame (GraphTables.tableBits t) (encodeWord t.vertices) [] [] [] []
(encodeWord 0) [] [] [] [] [] output⟩) =
some ⟨some (.lookup .darts (.run .copyFirst)), ((ambient, false), none),
frame (GraphTables.tableBits t) (encodeWord t.vertices) [] [] [] []
(encodeWord 1) [] [] [] [] [] output⟩ :=
seedMStep q _ _ _ ambient none
have hm := MachinePreservingLookupClean.traceAt_fromTapes (lookupSlots .darts)
(lookupSlots_injective .darts) (Label.lookup .darts) (some Label.clearIndex)
(program q) (fun _ => rfl)
(frame (GraphTables.tableBits t) (encodeWord t.vertices) [] [] [] []
(encodeWord 1) [] [] [] [] [] output)
(GraphTables.tableWords t) rfl rfl 1 t.darts (by simp [GraphTables.tableWords]) []
(by simp [lookupSlots]) (by simp [lookupSlots]) (ambient, false) none
simp [MachinePreservingLookup.initialTapes, MachineLookup.tapes, lookupSlots, fieldTape] at hm
have hc : (advance (TM2.step (program q)))^[1]
(some ⟨some .clearIndex, ((ambient, false), none),
frame (GraphTables.tableBits t) (encodeWord t.vertices) [] [] []
(encodeWord t.darts) (encodeWord 0) [] [] [] [] [] output⟩) =
some ⟨some (.prefix .init), ((ambient, false), none),
frame (GraphTables.tableBits t) (encodeWord t.vertices) [] [] []
(encodeWord t.darts) [] [] [] [] [] [] output⟩ :=
clearIndexStep q _ _ _ _ ambient none
exact appendTrace _ (appendTrace _ (appendTrace _ (appendTrace _ hi hn) hs) hm) hc
end Header
end IndependentSetsGames.Foundations.Complexity.MachineRegularTable
end OAI
|