Buckets:
| import Mathlib.Tactic.FinCases | |
| import OAI.Computability.UniqueGames.Machines.MachineCopy | |
| import OAI.Computability.UniqueGames.Machines.MachineLemmas | |
| import OAI.Computability.UniqueGames.Machines.MachineLookupCore | |
| import OAI.Computability.UniqueGames.Machines.MachineLookupDiscard | |
| import OAI.Computability.UniqueGames.Machines.MachineSubroutineLemmas | |
| import OAI.Computability.UniqueGames.Machines.MachineUnaryAffineAt | |
| namespace OAI | |
| /-! | |
| Actual traces for the fixed unary lookup machine. Successful lookup preserves | |
| an arbitrary unread table suffix. Rejection relative to a list's end is stated | |
| only for its complete, unframed encoding, so no hidden endpoint is assumed. | |
| -/ | |
| namespace UniqueGamesTheorem.Foundations.Complexity.MachineLookup | |
| open Turing | |
| open MachineComposition | |
| variable {K σ : Type} [DecidableEq K] | |
| theorem guard_step_succ (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) | |
| (base : K → List Bool) (i : Nat) (indexSuffix input output : List Bool) | |
| (ambient : σ) (register : Option Bool) : | |
| TM2.step (program index source destination) | |
| ⟨some .guard, (ambient, register), tapes index source destination base | |
| (encodeWord (i + 1) ++ indexSuffix) input output⟩ = | |
| some ⟨some .skip, (ambient, none), tapes index source destination base | |
| (encodeWord i ++ indexSuffix) input output⟩ := by | |
| change some (TM2.stepAux (program index source destination .guard) _ _) = _ | |
| simp [program, MachineUnaryCounter.guard, TM2.stepAux, | |
| tapes_index, his, hid, encodeWord, List.replicate_succ, update_index] | |
| theorem guard_step_zero (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) | |
| (base : K → List Bool) (indexSuffix input output : List Bool) | |
| (ambient : σ) (register : Option Bool) : | |
| TM2.step (program index source destination) | |
| ⟨some .guard, (ambient, register), tapes index source destination base | |
| (encodeWord 0 ++ indexSuffix) input output⟩ = | |
| some ⟨some .select, (ambient, none), tapes index source destination base | |
| (encodeWord 0 ++ indexSuffix) input output⟩ := by | |
| change some (TM2.stepAux (program index source destination .guard) _ _) = _ | |
| simp [program, MachineUnaryCounter.guard, TM2.stepAux, | |
| tapes_index, his, hid, encodeWord] | |
| theorem select_step_nonempty (index source destination : K) | |
| (hsd : source ≠ destination) (base : K → List Bool) | |
| (counter input output : List Bool) (hinput : input ≠ []) | |
| (ambient : σ) (register : Option Bool) : | |
| TM2.step (program index source destination) | |
| ⟨some .select, (ambient, register), | |
| tapes index source destination base counter input output⟩ = | |
| some ⟨some .copy, (ambient, input.head?), | |
| tapes index source destination base counter input (false :: output)⟩ := by | |
| change some (TM2.stepAux (program index source destination .select) _ _) = _ | |
| cases input with | |
| | nil => exact (hinput rfl).elim | |
| | cons head tail => | |
| simp [program, select, Hastad.SourceMachine.fieldStart, TM2.stepAux, | |
| tapes_source, hsd, update_destination] | |
| theorem select_step_empty (index source destination : K) | |
| (hsd : source ≠ destination) (base : K → List Bool) | |
| (counter output : List Bool) (ambient : σ) (register : Option Bool) : | |
| TM2.step (program index source destination) | |
| ⟨some .select, (ambient, register), | |
| tapes index source destination base counter [] output⟩ = | |
| some ⟨some .rejected, (ambient, none), | |
| tapes index source destination base counter [] output⟩ := by | |
| change some (TM2.stepAux (program index source destination .select) _ _) = _ | |
| simp [program, select, TM2.stepAux, tapes_source, hsd] | |
| theorem copyTrace (index source destination : K) | |
| (hsd : source ≠ destination) (base : K → List Bool) | |
| (counter : List Bool) (n : Nat) (suffix output : List Bool) | |
| (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (program index source destination)))^[n + 1] | |
| (some ⟨some .copy, (ambient, register), tapes index source destination base | |
| counter (encodeWord n ++ suffix) (false :: output)⟩) = | |
| some ⟨some .accepted, (ambient, none), tapes index source destination base | |
| counter suffix (encodeWord n ++ output)⟩ := by | |
| have h := Hastad.SourceMachine.fieldLoopTrace source destination hsd | |
| Label.copy (some Label.accepted) (program index source destination) rfl | |
| (Function.update base index counter) n suffix (false :: output) ambient register | |
| simpa only [Hastad.SourceMachine.fieldTapes, tapes, encodeWord, | |
| List.append_assoc, List.singleton_append] using h | |
| theorem selectTrace (index source destination : K) | |
| (hsd : source ≠ destination) (base : K → List Bool) | |
| (counter : List Bool) (n : Nat) (suffix output : List Bool) | |
| (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (program index source destination)))^[n + 2] | |
| (some ⟨some .select, (ambient, register), tapes index source destination base | |
| counter (encodeWord n ++ suffix) output⟩) = | |
| some ⟨some .accepted, (ambient, none), tapes index source destination base | |
| counter suffix (encodeWord n ++ output)⟩ := by | |
| rw [show n + 2 = (n + 1) + 1 by omega, Function.iterate_succ_apply] | |
| simp only [advance_some] | |
| rw [select_step_nonempty index source destination hsd base counter | |
| (encodeWord n ++ suffix) output (by simp [encodeWord]) ambient register] | |
| exact copyTrace index source destination hsd base counter n suffix output ambient _ | |
| theorem skipCycleTrace (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) (hsd : source ≠ destination) | |
| (base : K → List Bool) (i n : Nat) (indexSuffix suffix output : List Bool) | |
| (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (program index source destination)))^[n + 2] | |
| (some ⟨some .guard, (ambient, register), tapes index source destination base | |
| (encodeWord (i + 1) ++ indexSuffix) (encodeWord n ++ suffix) output⟩) = | |
| some ⟨some .guard, (ambient, none), tapes index source destination base | |
| (encodeWord i ++ indexSuffix) suffix output⟩ := by | |
| rw [show n + 2 = (n + 1) + 1 by omega, Function.iterate_succ_apply] | |
| simp only [advance_some] | |
| rw [guard_step_succ index source destination his hid] | |
| have h := discardTrace source Label.skip Label.guard | |
| (program index source destination) rfl | |
| (tapes index source destination base (encodeWord i ++ indexSuffix) | |
| (encodeWord n ++ suffix) output) n suffix (by simp [hsd]) ambient none | |
| simpa only [update_source index source destination hsd] using h | |
| theorem emptyCycleTrace (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) (hsd : source ≠ destination) | |
| (base : K → List Bool) (i : Nat) (indexSuffix output : List Bool) | |
| (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (program index source destination)))^[2] | |
| (some ⟨some .guard, (ambient, register), tapes index source destination base | |
| (encodeWord (i + 1) ++ indexSuffix) [] output⟩) = | |
| some ⟨some .guard, (ambient, none), tapes index source destination base | |
| (encodeWord i ++ indexSuffix) [] output⟩ := by | |
| rw [show 2 = 1 + 1 from rfl, Function.iterate_succ_apply] | |
| simp only [Function.iterate_one, advance_some] | |
| rw [guard_step_succ index source destination his hid] | |
| exact discard_empty_step source Label.skip Label.guard (program index source destination) | |
| rfl (tapes index source destination base (encodeWord i ++ indexSuffix) [] output) | |
| (by simp [hsd]) ambient none | |
| /-- A successful positional lookup scans precisely its prior and selected | |
| field. The suffix is arbitrary and is retained byte for byte. -/ | |
| theorem successPrefixTrace (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) (hsd : source ≠ destination) | |
| (base : K → List Bool) (prior : List Nat) (value : Nat) | |
| (indexSuffix suffix output : List Bool) (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (program index source destination)))^[ | |
| (encodeWords prior).length + prior.length + value + 3] | |
| (some ⟨some .guard, (ambient, register), tapes index source destination base | |
| (encodeWord prior.length ++ indexSuffix) | |
| (encodeWords prior ++ (encodeWord value ++ suffix)) output⟩) = | |
| some ⟨some .accepted, (ambient, none), tapes index source destination base | |
| (encodeWord 0 ++ indexSuffix) suffix (encodeWord value ++ output)⟩ := by | |
| induction prior generalizing register with | |
| | nil => | |
| simp only [encodeWords, List.length_nil, Nat.zero_add, List.nil_append] | |
| rw [show value + 3 = (value + 2) + 1 by omega, Function.iterate_succ_apply] | |
| simp only [advance_some] | |
| rw [guard_step_zero index source destination his hid] | |
| exact selectTrace index source destination hsd base _ value suffix output ambient none | |
| | cons n prior ih => | |
| simp only [encodeWords, List.length_cons, List.length_append, encodeWord_length] | |
| rw [show n + 1 + (encodeWords prior).length + (prior.length + 1) + value + 3 = | |
| ((encodeWords prior).length + prior.length + value + 3) + (n + 2) by omega, | |
| Function.iterate_add_apply] | |
| rw [List.append_assoc, | |
| skipCycleTrace index source destination his hid hsd base prior.length n] | |
| exact ih none | |
| /-- Successful array access, with explicit selected value and preserved framing. -/ | |
| theorem lookupTrace_some (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) (hsd : source ≠ destination) | |
| (base : K → List Bool) (values : List Nat) (i value : Nat) | |
| (selected : values[i]? = some value) | |
| (indexSuffix suffix output : List Bool) (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (program index source destination)))^[MachineLookupSpec.steps values i] | |
| (some ⟨some .guard, (ambient, register), tapes index source destination base | |
| (encodeWord i ++ indexSuffix) (encodeWords values ++ suffix) output⟩) = | |
| some ⟨some .accepted, (ambient, none), tapes index source destination base | |
| (encodeWord 0 ++ indexSuffix) (encodeWords (values.drop (i + 1)) ++ suffix) | |
| (encodeWord value ++ output)⟩ := by | |
| have hi := (List.getElem?_eq_some_iff.mp selected).1 | |
| have hlength : (values.take i).length = i := by | |
| simp [List.length_take, Nat.min_eq_left (Nat.le_of_lt hi)] | |
| have hsource : encodeWords (values.take i) ++ | |
| (encodeWord value ++ (encodeWords (values.drop (i + 1)) ++ suffix)) = | |
| encodeWords values ++ suffix := by | |
| rw [MachineLookupSpec.encoded_selected_split values i value selected] | |
| simp only [List.append_assoc] | |
| have htime : MachineLookupSpec.steps values i = | |
| (encodeWords (values.take i)).length + i + value + 3 := by | |
| rw [MachineLookupSpec.steps_eq_scannedBits_of_some values i value selected, | |
| MachineLookupSpec.scannedBits_of_some values i value selected] | |
| omega | |
| have h := successPrefixTrace index source destination his hid hsd base (values.take i) | |
| value indexSuffix (encodeWords (values.drop (i + 1)) ++ suffix) output ambient register | |
| rw [hlength, hsource] at h | |
| simpa only [htime] using h | |
| theorem emptyTrace (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) (hsd : source ≠ destination) | |
| (base : K → List Bool) (i : Nat) (indexSuffix output : List Bool) | |
| (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (program index source destination)))^[2 * i + 2] | |
| (some ⟨some .guard, (ambient, register), tapes index source destination base | |
| (encodeWord i ++ indexSuffix) [] output⟩) = | |
| some ⟨some .rejected, (ambient, none), tapes index source destination base | |
| (encodeWord 0 ++ indexSuffix) [] output⟩ := by | |
| induction i generalizing register with | |
| | zero => | |
| simp only [Nat.mul_zero, Nat.zero_add] | |
| rw [show 2 = 1 + 1 from rfl, Function.iterate_succ_apply] | |
| simp only [Function.iterate_one, advance_some] | |
| rw [guard_step_zero index source destination his hid] | |
| exact select_step_empty index source destination hsd base _ output ambient none | |
| | succ i ih => | |
| rw [show 2 * (i + 1) + 2 = (2 * i + 2) + 2 by omega, | |
| Function.iterate_add_apply, emptyCycleTrace index source destination his hid hsd] | |
| exact ih none | |
| /-- Invalid indices reject on a complete encoded table, after consuming that | |
| table and all positive index bits. The output and other tapes are unchanged. -/ | |
| theorem lookupTrace_invalid (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) (hsd : source ≠ destination) | |
| (base : K → List Bool) (values : List Nat) (i : Nat) (invalid : values.length ≤ i) | |
| (indexSuffix output : List Bool) (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (program index source destination)))^[MachineLookupSpec.steps values i] | |
| (some ⟨some .guard, (ambient, register), tapes index source destination base | |
| (encodeWord i ++ indexSuffix) (encodeWords values) output⟩) = | |
| some ⟨some .rejected, (ambient, none), tapes index source destination base | |
| (encodeWord 0 ++ indexSuffix) [] output⟩ := by | |
| induction values generalizing i register with | |
| | nil => | |
| simpa only [MachineLookupSpec.steps, encodeWords] using | |
| emptyTrace index source destination his hid hsd base i indexSuffix output ambient register | |
| | cons n values ih => | |
| cases i with | |
| | zero => simp at invalid | |
| | succ i => | |
| simp only [MachineLookupSpec.steps, encodeWords] | |
| rw [Nat.add_comm (n + 2), Function.iterate_add_apply, | |
| skipCycleTrace index source destination his hid hsd] | |
| exact ih i (by simpa using invalid) none | |
| /-- The two explicit control outcomes on a complete encoded table. -/ | |
| def resultLabel (values : List Nat) (i : Nat) : Label := | |
| match values[i]? with | |
| | some _ => .accepted | |
| | none => .rejected | |
| /-- Successful lookup prefixes a complete unary word; failure writes nothing. -/ | |
| def resultOutput (values : List Nat) (i : Nat) (output : List Bool) : List Bool := | |
| match values[i]? with | |
| | some value => encodeWord value ++ output | |
| | none => output | |
| /-- A total execution statement for every natural index and complete table. | |
| No bound or validity assumption on the index is needed. -/ | |
| theorem lookupTrace (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) (hsd : source ≠ destination) | |
| (base : K → List Bool) (values : List Nat) (i : Nat) | |
| (indexSuffix output : List Bool) (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (program index source destination)))^[MachineLookupSpec.steps values i] | |
| (some ⟨some .guard, (ambient, register), tapes index source destination base | |
| (encodeWord i ++ indexSuffix) (encodeWords values) output⟩) = | |
| some ⟨some (resultLabel values i), (ambient, none), tapes index source destination base | |
| (encodeWord 0 ++ indexSuffix) (encodeWords (values.drop (i + 1))) | |
| (resultOutput values i output)⟩ := by | |
| cases selected : values[i]? with | |
| | some value => | |
| simpa only [List.append_nil, resultLabel, resultOutput, selected] using | |
| lookupTrace_some index source destination his hid hsd base values i value selected | |
| indexSuffix [] output ambient register | |
| | none => | |
| have invalid : values.length ≤ i := List.getElem?_eq_none_iff.mp selected | |
| have hdrop : values.drop (i + 1) = [] := List.drop_eq_nil_of_le (by omega) | |
| simpa only [resultLabel, resultOutput, selected, hdrop, encodeWords] using | |
| lookupTrace_invalid index source destination his hid hsd base values i invalid | |
| indexSuffix output ambient register | |
| /-- A proved linear transition budget in the lengths of the encoded table and | |
| index, including invalid indices. The witness is the actual trace above. -/ | |
| def lookupInTime (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) (hsd : source ≠ destination) | |
| (base : K → List Bool) (values : List Nat) (i : Nat) | |
| (indexSuffix output : List Bool) (ambient : σ) (register : Option Bool) : | |
| StateTransition.EvalsToInTime (TM2.step (program index source destination)) | |
| ⟨some .guard, (ambient, register), tapes index source destination base | |
| (encodeWord i ++ indexSuffix) (encodeWords values) output⟩ | |
| (some ⟨some (resultLabel values i), (ambient, none), tapes index source destination base | |
| (encodeWord 0 ++ indexSuffix) (encodeWords (values.drop (i + 1))) | |
| (resultOutput values i output)⟩) | |
| ((encodeWords values).length + 2 * (encodeWord i).length) where | |
| steps := MachineLookupSpec.steps values i | |
| evals_in_steps := lookupTrace index source destination his hid hsd base values i | |
| indexSuffix output ambient register | |
| steps_le_m := MachineLookupSpec.steps_le_input_encoding_size values i | |
| /-- The final control label executes a genuine halt in one more transition. -/ | |
| theorem lookupHaltTrace (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) (hsd : source ≠ destination) | |
| (base : K → List Bool) (values : List Nat) (i : Nat) | |
| (indexSuffix output : List Bool) (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (program index source destination)))^[MachineLookupSpec.steps values i + 1] | |
| (some ⟨some .guard, (ambient, register), tapes index source destination base | |
| (encodeWord i ++ indexSuffix) (encodeWords values) output⟩) = | |
| some ⟨none, (ambient, none), tapes index source destination base | |
| (encodeWord 0 ++ indexSuffix) (encodeWords (values.drop (i + 1))) | |
| (resultOutput values i output)⟩ := by | |
| rw [Function.iterate_succ_apply', lookupTrace index source destination his hid hsd] | |
| simp only [advance_some] | |
| cases selected : values[i]? <;> simp [resultLabel, selected, TM2.step, program, TM2.stepAux] | |
| /-- A single polynomial in the combined two input lengths. -/ | |
| noncomputable def timePolynomial : Polynomial Nat := Polynomial.C 2 * Polynomial.X + 1 | |
| theorem timePolynomial_bounds (values : List Nat) (i : Nat) : | |
| MachineLookupSpec.steps values i + 1 ≤ | |
| timePolynomial.eval ((encodeWords values).length + (encodeWord i).length) := by | |
| have h := MachineLookupSpec.steps_le_input_encoding_size values i | |
| simp only [timePolynomial, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, | |
| Polynomial.eval_X, Polynomial.eval_one] | |
| omega | |
| /-- The same exact computation as a certificate for the concrete finite | |
| three-stack machine. Both valid and invalid indices terminate with reset local | |
| state, retaining the zero index delimiter and all other stated tape contents. -/ | |
| def machineInTime (base : Fin 3 → List Bool) (values : List Nat) (i : Nat) | |
| (indexSuffix output : List Bool) (register : Option Bool) : | |
| StateTransition.EvalsToInTime machine.step | |
| ⟨some .guard, ((), register), tapes (K := Fin 3) 0 1 2 base | |
| (encodeWord i ++ indexSuffix) (encodeWords values) output⟩ | |
| (some ⟨none, ((), none), tapes (K := Fin 3) 0 1 2 base | |
| (encodeWord 0 ++ indexSuffix) (encodeWords (values.drop (i + 1))) | |
| (resultOutput values i output)⟩) | |
| (timePolynomial.eval ((encodeWords values).length + (encodeWord i).length)) where | |
| steps := MachineLookupSpec.steps values i + 1 | |
| evals_in_steps := lookupHaltTrace (0 : Fin 3) 1 2 (by decide) (by decide) (by decide) | |
| base values i indexSuffix output () register | |
| steps_le_m := timePolynomial_bounds values i | |
| end UniqueGamesTheorem.Foundations.Complexity.MachineLookup | |
| /-! Actual lookup from a preserved table. A fresh copy is prefixed to the scan | |
| stack before each access. The old scan suffix is retained and need not be | |
| cleared between accesses. No graph/table data occurs in finite control. -/ | |
| namespace UniqueGamesTheorem.Foundations.Complexity.MachinePreservingLookup | |
| open Turing | |
| open MachineComposition | |
| variable {K Λ σ : Type} [DecidableEq K] | |
| abbrev Alphabet (_ : K) := Bool | |
| /-- Tape roles: original table, unary query, scan work, output, copy scratch. -/ | |
| def initialTapes (tape : Fin 5 → K) (base : K → List Bool) | |
| (index : Nat) (indexSuffix workSuffix output : List Bool) : K → List Bool := | |
| MachineLookup.tapes (tape 1) (tape 2) (tape 3) base | |
| (encodeWord index ++ indexSuffix) workSuffix output | |
| def finalTapes (tape : Fin 5 → K) (base : K → List Bool) | |
| (values : List Nat) (index value : Nat) | |
| (indexSuffix workSuffix output : List Bool) : K → List Bool := | |
| MachineLookup.tapes (tape 1) (tape 2) (tape 3) base | |
| (encodeWord 0 ++ indexSuffix) | |
| (encodeWords (values.drop (index + 1)) ++ workSuffix) (encodeWord value ++ output) | |
| theorem lookupFramedHaltTrace_some (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) (hsd : source ≠ destination) | |
| (base : K → List Bool) (values : List Nat) (i value : Nat) | |
| (selected : values[i]? = some value) (indexSuffix suffix output : List Bool) | |
| (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (MachineLookup.program index source destination)))^[ | |
| MachineLookupSpec.steps values i + 1] | |
| (some ⟨some MachineLookup.Label.guard, (ambient,register), | |
| MachineLookup.tapes index source destination base (encodeWord i ++ indexSuffix) | |
| (encodeWords values ++ suffix) output⟩) = | |
| some ⟨none, (ambient,none), MachineLookup.tapes index source destination base | |
| (encodeWord 0 ++ indexSuffix) (encodeWords (values.drop (i + 1)) ++ suffix) | |
| (encodeWord value ++ output)⟩ := by | |
| rw [Function.iterate_succ_apply', | |
| MachineLookup.lookupTrace_some index source destination his hid hsd | |
| base values i value selected indexSuffix suffix output ambient register] | |
| simp only [advance_some, TM2.step, MachineLookup.program, TM2.stepAux] | |
| /-- Code-level interfaces specify the actual two copy loops and six lookup | |
| instructions in the caller's program. No execution premise is supplied. -/ | |
| theorem preservingLookupTrace (tape : Fin 5 → K) (distinct : Function.Injective tape) | |
| (firstLabel secondLabel : Λ) (lookupLabels : MachineLookup.Label → Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) | |
| (atFirst : program firstLabel = Reduction.MachineTransfer.loopAt | |
| (tape 0) (tape 4) id false firstLabel (some secondLabel)) | |
| (atSecond : program secondLabel = MachineCopy.forkLoop | |
| (tape 4) (tape 0) (tape 2) false secondLabel (some (lookupLabels .guard))) | |
| (atLookup : ∀ l, program (lookupLabels l) = | |
| MachineSubroutine.statement lookupLabels exit (MachineLookup.program (tape 1) (tape 2) (tape 3) l)) | |
| (base : K → List Bool) (values : List Nat) (tableWord : base (tape 0) = encodeWords values) | |
| (scratchEmpty : base (tape 4) = []) (i value : Nat) (selected : values[i]? = some value) | |
| (indexSuffix workSuffix output : List Bool) (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step program))^[ | |
| 2 * ((encodeWords values).length + 1) + MachineLookupSpec.steps values i + 1] | |
| (some ⟨some firstLabel, (ambient,register), | |
| initialTapes tape base i indexSuffix workSuffix output⟩) = | |
| some ⟨exit, (ambient,none), finalTapes tape base values i value indexSuffix workSuffix output⟩ := by | |
| have hd (a b : Fin 5) (hne : a ≠ b) : tape a ≠ tape b := fun h => hne (distinct h) | |
| let start := initialTapes tape base i indexSuffix workSuffix output | |
| have htable : start (tape 0) = encodeWords values := by | |
| simpa only [start, initialTapes, MachineLookup.tapes_other _ _ _ _ | |
| (hd 0 1 (by decide)) (hd 0 2 (by decide)) (hd 0 3 (by decide))] using tableWord | |
| have hs : start (tape 4) = [] := by | |
| simpa only [start, initialTapes, MachineLookup.tapes_other _ _ _ _ | |
| (hd 4 1 (by decide)) (hd 4 2 (by decide)) (hd 4 3 (by decide))] using scratchEmpty | |
| have hwork : start (tape 2) = workSuffix := by | |
| simp only [start, initialTapes, MachineLookup.tapes_source _ _ _ (hd 2 3 (by decide))] | |
| have hcopy := MachineCopy.copyTrace (tape 0) (tape 2) (tape 4) | |
| (hd 0 2 (by decide)) (hd 0 4 (by decide)) (hd 2 4 (by decide)) false | |
| firstLabel secondLabel (some (lookupLabels .guard)) program atFirst atSecond start hs ambient register | |
| rw [htable, hwork] at hcopy | |
| simp only [start, initialTapes, MachineLookup.update_source _ _ _ (hd 2 3 (by decide))] at hcopy | |
| have hlookup := MachineSubroutine.trace lookupLabels exit | |
| (MachineLookup.program (tape 1) (tape 2) (tape 3)) program atLookup | |
| (MachineLookupSpec.steps values i + 1) _ _ | |
| (lookupFramedHaltTrace_some (tape 1) (tape 2) (tape 3) | |
| (hd 1 2 (by decide)) (hd 1 3 (by decide)) (hd 2 3 (by decide)) | |
| base values i value selected indexSuffix workSuffix output ambient none) | |
| simp only [MachineSubroutine.configuration, MachineSubroutine.label] at hlookup | |
| rw [show 2 * ((encodeWords values).length + 1) + MachineLookupSpec.steps values i + 1 = | |
| (MachineLookupSpec.steps values i + 1) + 2 * ((encodeWords values).length + 1) by omega, | |
| Function.iterate_add_apply] | |
| change (advance (TM2.step program))^[MachineLookupSpec.steps values i + 1] | |
| ((advance (TM2.step program))^[2 * ((encodeWords values).length + 1)] | |
| (some ⟨some firstLabel, (ambient,register), | |
| MachineLookup.tapes (tape 1) (tape 2) (tape 3) base | |
| (encodeWord i ++ indexSuffix) workSuffix output⟩)) = _ | |
| rw [hcopy] | |
| exact hlookup | |
| theorem preservingLookup_steps_le (values : List Nat) (i value : Nat) | |
| (selected : values[i]? = some value) : | |
| 2 * ((encodeWords values).length + 1) + MachineLookupSpec.steps values i + 1 ≤ | |
| 5 * (encodeWords values).length + 3 := by | |
| have ht := MachineLookupSpec.steps_le_input_encoding_size values i | |
| have hi := MachineLookupSpec.index_length_le values i value selected | |
| omega | |
| def preservingLookupInTime (tape : Fin 5 → K) (distinct : Function.Injective tape) | |
| (firstLabel secondLabel : Λ) (lookupLabels : MachineLookup.Label → Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) | |
| (atFirst : program firstLabel = Reduction.MachineTransfer.loopAt | |
| (tape 0) (tape 4) id false firstLabel (some secondLabel)) | |
| (atSecond : program secondLabel = MachineCopy.forkLoop | |
| (tape 4) (tape 0) (tape 2) false secondLabel (some (lookupLabels .guard))) | |
| (atLookup : ∀ l, program (lookupLabels l) = | |
| MachineSubroutine.statement lookupLabels exit (MachineLookup.program (tape 1) (tape 2) (tape 3) l)) | |
| (base : K → List Bool) (values : List Nat) (tableWord : base (tape 0) = encodeWords values) | |
| (scratchEmpty : base (tape 4) = []) (i value : Nat) (selected : values[i]? = some value) | |
| (indexSuffix workSuffix output : List Bool) (ambient : σ) (register : Option Bool) : | |
| StateTransition.EvalsToInTime (TM2.step program) | |
| ⟨some firstLabel, (ambient,register), initialTapes tape base i indexSuffix workSuffix output⟩ | |
| (some ⟨exit, (ambient,none), finalTapes tape base values i value indexSuffix workSuffix output⟩) | |
| (5 * (encodeWords values).length + 3) where | |
| steps := 2 * ((encodeWords values).length + 1) + MachineLookupSpec.steps values i + 1 | |
| evals_in_steps := preservingLookupTrace tape distinct firstLabel secondLabel lookupLabels exit | |
| program atFirst atSecond atLookup base values tableWord scratchEmpty i value selected | |
| indexSuffix workSuffix output ambient register | |
| steps_le_m := preservingLookup_steps_le values i value selected | |
| inductive Label | |
| | copyFirst | copySecond | lookup (l : MachineLookup.Label) | |
| deriving DecidableEq, Fintype | |
| def program (tape : Fin 5 → K) : | |
| Label → TM2.Stmt (Alphabet (K := K)) Label (σ × Option Bool) | |
| | .copyFirst => Reduction.MachineTransfer.loopAt (tape 0) (tape 4) id false .copyFirst (some .copySecond) | |
| | .copySecond => MachineCopy.forkLoop (tape 4) (tape 0) (tape 2) false .copySecond (some (.lookup .guard)) | |
| | .lookup l => MachineSubroutine.statement Label.lookup none | |
| (MachineLookup.program (tape 1) (tape 2) (tape 3) l) | |
| def machine : FinTM2 where | |
| K := Fin 5 | |
| k₀ := 0 | |
| k₁ := 3 | |
| Γ _ := Bool | |
| Λ := Label | |
| main := .copyFirst | |
| σ := Unit × Option Bool | |
| initialState := ((),none) | |
| m := program id | |
| end UniqueGamesTheorem.Foundations.Complexity.MachinePreservingLookup | |
| namespace UniqueGamesTheorem.Foundations.Complexity.MachineAffineLookup | |
| open Turing | |
| open MachineComposition | |
| variable {K Λ σ : Type} [DecidableEq K] | |
| abbrev Alphabet (_ : K) := Bool | |
| inductive Label | |
| | seed | scan | restore | copyFirst | copySecond | |
| | lookup (l : MachineLookup.Label) | |
| deriving DecidableEq, Fintype | |
| def instruction (source : K) (tape : Fin 5 → K) (coefficient offset : Nat) | |
| (labels : Label → Λ) (exit : Option Λ) : | |
| Label → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool) | |
| | .seed => MachineUnaryAffineAt.seed (tape 1) offset (labels .scan) | |
| | .scan => MachineUnaryAffineAt.scan source (tape 4) (tape 1) | |
| coefficient (labels .scan) (labels .restore) | |
| | .restore => Reduction.MachineTransfer.loopAt (tape 4) source id false | |
| (labels .restore) (some (labels .copyFirst)) | |
| | .copyFirst => Reduction.MachineTransfer.loopAt (tape 0) (tape 4) id false | |
| (labels .copyFirst) (some (labels .copySecond)) | |
| | .copySecond => MachineCopy.forkLoop (tape 4) (tape 0) (tape 2) false | |
| (labels .copySecond) (some (labels (.lookup .guard))) | |
| | .lookup l => MachineSubroutine.statement (fun q => labels (.lookup q)) exit | |
| (MachineLookup.program (tape 1) (tape 2) (tape 3) l) | |
| def steps (values : List Nat) (a coefficient offset : Nat) : Nat := | |
| (2 * (a + 1) + 1) + | |
| (2 * ((encodeWords values).length + 1) + | |
| MachineLookupSpec.steps values (coefficient * a + offset) + 1) | |
| def finalTapes (tape : Fin 5 → K) (base : K → List Bool) | |
| (values : List Nat) (index value : Nat) : K → List Bool := | |
| MachinePreservingLookup.finalTapes tape base values index value | |
| (base (tape 1)) (base (tape 2)) (base (tape 3)) | |
| theorem initialTapes_eq_update (tape : Fin 5 → K) (distinct : Function.Injective tape) | |
| (base : K → List Bool) (i : Nat) : | |
| MachinePreservingLookup.initialTapes tape base i | |
| (base (tape 1)) (base (tape 2)) (base (tape 3)) = | |
| Function.update base (tape 1) (encodeWord i ++ base (tape 1)) := by | |
| have hd (a b : Fin 5) (hne : a ≠ b) : tape a ≠ tape b := fun h => hne (distinct h) | |
| funext k | |
| by_cases h₁ : k = tape 1 | |
| · subst k | |
| simp [MachinePreservingLookup.initialTapes, MachineLookup.tapes, | |
| hd 1 2 (by decide), hd 1 3 (by decide)] | |
| · by_cases h₂ : k = tape 2 | |
| · subst k | |
| simp [MachinePreservingLookup.initialTapes, MachineLookup.tapes, h₁, | |
| hd 2 3 (by decide)] | |
| · by_cases h₃ : k = tape 3 | |
| · subst k | |
| simp [MachinePreservingLookup.initialTapes, MachineLookup.tapes, h₁] | |
| · simp [MachinePreservingLookup.initialTapes, MachineLookup.tapes, h₁, h₂, h₃] | |
| /-- An exact trace for the actual composite instructions. `atLabels` specifies | |
| the caller's finite program; there is no execution or runtime premise. -/ | |
| theorem affineLookupTrace (source : K) (tape : Fin 5 → K) | |
| (distinct : Function.Injective tape) (outside : ∀ i, source ≠ tape i) | |
| (coefficient offset : Nat) (labels : Label → Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) | |
| (atLabels : ∀ l, program (labels l) = instruction source tape coefficient offset labels exit l) | |
| (base : K → List Bool) (values : List Nat) | |
| (tableWord : base (tape 0) = encodeWords values) (scratchEmpty : base (tape 4) = []) | |
| (a : Nat) (suffix : List Bool) (sourceWord : base source = encodeWord a ++ suffix) | |
| (value : Nat) (selected : values[coefficient * a + offset]? = some value) | |
| (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step program))^[steps values a coefficient offset] | |
| (some ⟨some (labels .seed), (ambient,register), base⟩) = | |
| some ⟨exit, (ambient,none), finalTapes tape base values (coefficient * a + offset) value⟩ := by | |
| have hd (i j : Fin 5) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) | |
| have haffine := MachineUnaryAffineAt.seededAffineTrace source (tape 4) (tape 1) | |
| (outside 4) (outside 1) (hd 4 1 (by decide)) coefficient offset | |
| (labels .seed) (labels .scan) (labels .restore) (some (labels .copyFirst)) | |
| program (atLabels .seed) (atLabels .scan) (atLabels .restore) | |
| base a suffix sourceWord scratchEmpty ambient register | |
| have hlookup := MachinePreservingLookup.preservingLookupTrace tape distinct | |
| (labels .copyFirst) (labels .copySecond) (fun q => labels (.lookup q)) exit | |
| program (atLabels .copyFirst) (atLabels .copySecond) (fun q => atLabels (.lookup q)) | |
| base values tableWord scratchEmpty (coefficient * a + offset) value selected | |
| (base (tape 1)) (base (tape 2)) (base (tape 3)) ambient none | |
| rw [initialTapes_eq_update tape distinct] at hlookup | |
| rw [steps, Nat.add_comm, Function.iterate_add_apply, haffine] | |
| exact hlookup | |
| theorem steps_le (values : List Nat) (a coefficient offset value : Nat) | |
| (selected : values[coefficient * a + offset]? = some value) : | |
| steps values a coefficient offset ≤ 2 * a + 5 * (encodeWords values).length + 6 := by | |
| have h := MachinePreservingLookup.preservingLookup_steps_le values | |
| (coefficient * a + offset) value selected | |
| unfold steps | |
| omega | |
| theorem steps_le_table (values : List Nat) (a coefficient offset value : Nat) | |
| (selected : values[coefficient * a + offset]? = some value) | |
| (ha : a ≤ (encodeWords values).length) : | |
| steps values a coefficient offset ≤ 7 * (encodeWords values).length + 6 := by | |
| have h := steps_le values a coefficient offset value selected | |
| omega | |
| theorem finalTapes_other (tape : Fin 5 → K) (base : K → List Bool) | |
| (values : List Nat) (index value : Nat) (k : K) | |
| (h₁ : k ≠ tape 1) (h₂ : k ≠ tape 2) (h₃ : k ≠ tape 3) : | |
| finalTapes tape base values index value k = base k := | |
| MachineLookup.tapes_other _ _ _ _ h₁ h₂ h₃ _ _ _ _ | |
| theorem finalTapes_output (tape : Fin 5 → K) (base : K → List Bool) | |
| (values : List Nat) (index value : Nat) : | |
| finalTapes tape base values index value (tape 3) = encodeWord value ++ base (tape 3) := | |
| MachineLookup.tapes_destination _ _ _ _ _ _ _ | |
| def machine (coefficient offset : Nat) : FinTM2 where | |
| K := Fin 6 | |
| k₀ := 0 | |
| k₁ := 4 | |
| Γ _ := Bool | |
| Λ := Label | |
| main := .seed | |
| σ := Unit × Option Bool | |
| initialState := ((),none) | |
| m := instruction 0 Fin.succ coefficient offset id none | |
| end UniqueGamesTheorem.Foundations.Complexity.MachineAffineLookup | |
| /-! | |
| A fixed-width gather from an input-resident table. Every coordinate index lives | |
| on a unary tape. The coefficient and coordinate-dependent offsets alone live | |
| in finite control. Actual preserving affine lookup instructions read each | |
| requested field, and the proof below composes their traces. This is the gather | |
| phase of product-row construction, before the fixed-width Horner phase. | |
| -/ | |
| namespace UniqueGamesTheorem.Explicit.MachineProductGather | |
| open Turing | |
| open UniqueGamesTheorem.Foundations.Complexity | |
| open MachineComposition | |
| variable {K Λ σ : Type} [DecidableEq K] | |
| /-- Shared original table, query, scan and copy scratch, then input coordinate | |
| fields and the corresponding gathered output fields. -/ | |
| abbrev Tape (width : Nat) := Fin 4 ⊕ (Fin width ⊕ Fin width) | |
| def shift (width : Nat) : Tape width → Tape (width + 1) := | |
| Sum.map id (Sum.map Fin.succ Fin.succ) | |
| theorem shift_injective (width : Nat) : Function.Injective (shift width) := by | |
| intro a b h | |
| rcases a with a | (a | a) <;> rcases b with b | (b | b) <;> simp_all [shift] | |
| def lookupRole (width : Nat) : Fin 5 → Tape (width + 1) | |
| | 0 => .inl 0 | |
| | 1 => .inl 1 | |
| | 2 => .inl 2 | |
| | 3 => .inr (.inr 0) | |
| | 4 => .inl 3 | |
| theorem lookupRole_injective (width : Nat) : Function.Injective (lookupRole width) := by | |
| intro a b h | |
| fin_cases a <;> fin_cases b <;> simp_all [lookupRole] | |
| def Label : Nat → Type | |
| | 0 => Unit | |
| | width + 1 => MachineAffineLookup.Label ⊕ Label width | |
| instance labelFintype (width : Nat) : Fintype (Label width) := by | |
| induction width with | |
| | zero => exact inferInstanceAs (Fintype Unit) | |
| | succ width ih => | |
| letI := ih | |
| exact inferInstanceAs (Fintype (MachineAffineLookup.Label ⊕ Label width)) | |
| instance labelDecidableEq (width : Nat) : DecidableEq (Label width) := by | |
| induction width with | |
| | zero => exact inferInstanceAs (DecidableEq Unit) | |
| | succ width ih => | |
| letI := ih | |
| exact inferInstanceAs (DecidableEq (MachineAffineLookup.Label ⊕ Label width)) | |
| def entry : (width : Nat) → Label width | |
| | 0 => () | |
| | _ + 1 => .inl .seed | |
| def instruction : (width : Nat) → (Tape width → K) → Nat → (Fin width → Nat) → | |
| (Label width → Λ) → Option Λ → Label width → | |
| TM2.Stmt (fun _ : K => Bool) Λ (σ × Option Bool) | |
| | 0, placement, _, _, _, exit, _ => | |
| .load (fun state => (state.1, none)) | |
| (UniqueGamesTheorem.Reduction.MachineTransfer.exitAt (placement (.inl 0)) exit) | |
| | width + 1, placement, coefficient, offsets, labels, exit, label => | |
| match label with | |
| | .inl inner => MachineAffineLookup.instruction (placement (.inr (.inl 0))) | |
| (placement ∘ lookupRole width) coefficient (offsets 0) | |
| (fun l => labels (.inl l)) (some (labels (.inr (entry width)))) inner | |
| | .inr inner => instruction width (placement ∘ shift width) coefficient | |
| (fun i => offsets i.succ) (fun l => labels (.inr l)) exit inner | |
| def steps (values : List Nat) (coefficient : Nat) : | |
| (width : Nat) → (Fin width → Nat) → (Fin width → Nat) → Nat | |
| | 0, _, _ => 1 | |
| | width + 1, indices, offsets => | |
| MachineAffineLookup.steps values (indices 0) coefficient (offsets 0) + | |
| steps values coefficient width (fun i => indices i.succ) (fun i => offsets i.succ) | |
| def finalTapes (values : List Nat) (coefficient : Nat) : | |
| (width : Nat) → (Tape width → K) → (Fin width → Nat) → | |
| (Fin width → Nat) → (Fin width → Nat) → (K → List Bool) → K → List Bool | |
| | 0, _, _, _, _, base => base | |
| | width + 1, placement, indices, offsets, fields, base => | |
| finalTapes values coefficient width (placement ∘ shift width) | |
| (fun i => indices i.succ) (fun i => offsets i.succ) (fun i => fields i.succ) | |
| (MachineAffineLookup.finalTapes (placement ∘ lookupRole width) base values | |
| (coefficient * indices 0 + offsets 0) (fields 0)) | |
| theorem finalTapes_other (values : List Nat) (coefficient width : Nat) | |
| (placement : Tape width → K) (indices offsets fields : Fin width → Nat) | |
| (base : K → List Bool) (k : K) | |
| (query : k ≠ placement (.inl 1)) (scan : k ≠ placement (.inl 2)) | |
| (outputs : ∀ i, k ≠ placement (.inr (.inr i))) : | |
| finalTapes values coefficient width placement indices offsets fields base k = base k := by | |
| induction width generalizing base with | |
| | zero => rfl | |
| | succ width ih => | |
| rw [finalTapes] | |
| rw [ih (placement ∘ shift width) (fun i => indices i.succ) | |
| (fun i => offsets i.succ) (fun i => fields i.succ) _ query scan | |
| (fun i => outputs i.succ)] | |
| exact MachineAffineLookup.finalTapes_other _ _ _ _ _ k query scan (outputs 0) | |
| theorem gatherTrace (values : List Nat) (coefficient width : Nat) | |
| (placement : Tape width → K) (distinct : Function.Injective placement) | |
| (indices offsets fields : Fin width → Nat) | |
| (selected : ∀ i, values[coefficient * indices i + offsets i]? = some (fields i)) | |
| (labels : Label width → Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (σ × Option Bool)) | |
| (atLabels : ∀ l, program (labels l) = instruction width placement coefficient offsets labels exit l) | |
| (base : K → List Bool) (suffixes : Fin width → List Bool) | |
| (tableWord : base (placement (.inl 0)) = encodeWords values) | |
| (scratchEmpty : base (placement (.inl 3)) = []) | |
| (sourceWords : ∀ i, base (placement (.inr (.inl i))) = encodeWord (indices i) ++ suffixes i) | |
| (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step program))^[steps values coefficient width indices offsets] | |
| (some ⟨some (labels (entry width)), (ambient, register), base⟩) = | |
| some ⟨exit, (ambient, none), | |
| finalTapes values coefficient width placement indices offsets fields base⟩ := by | |
| induction width generalizing base register with | |
| | zero => | |
| change some (TM2.stepAux (program (labels ())) _ _) = _ | |
| erw [atLabels] | |
| cases exit <;> simp [instruction, TM2.stepAux, finalTapes, | |
| UniqueGamesTheorem.Reduction.MachineTransfer.exitAt] | |
| | succ width ih => | |
| have hd (a b : Tape (width + 1)) (hne : a ≠ b) : placement a ≠ placement b := | |
| fun h => hne (distinct h) | |
| have outside : ∀ i : Fin 5, | |
| placement (.inr (.inl 0)) ≠ (placement ∘ lookupRole width) i := by | |
| intro i | |
| apply hd | |
| fin_cases i <;> simp [lookupRole] | |
| have first := MachineAffineLookup.affineLookupTrace (placement (.inr (.inl 0))) | |
| (placement ∘ lookupRole width) (distinct.comp (lookupRole_injective width)) outside | |
| coefficient (offsets 0) (fun l => labels (.inl l)) | |
| (some (labels (.inr (entry width)))) program (fun l => atLabels (.inl l)) | |
| base values tableWord scratchEmpty (indices 0) (suffixes 0) (sourceWords 0) | |
| (fields 0) (selected 0) ambient register | |
| let mid := MachineAffineLookup.finalTapes (placement ∘ lookupRole width) base values | |
| (coefficient * indices 0 + offsets 0) (fields 0) | |
| have frame (k : Tape (width + 1)) (h1 : k ≠ .inl 1) (h2 : k ≠ .inl 2) | |
| (h3 : k ≠ .inr (.inr 0)) : mid (placement k) = base (placement k) := | |
| MachineAffineLookup.finalTapes_other _ _ _ _ _ _ (hd _ _ h1) (hd _ _ h2) (hd _ _ h3) | |
| have middleTable : mid (placement (.inl 0)) = encodeWords values := by | |
| rw [frame (.inl 0) (by simp) (by simp) (by simp)] | |
| exact tableWord | |
| have middleScratch : mid (placement (.inl 3)) = [] := by | |
| rw [frame (.inl 3) (by simp) (by simp) (by simp)] | |
| exact scratchEmpty | |
| have middleSources (i : Fin width) : mid (placement (.inr (.inl i.succ))) = | |
| encodeWord (indices i.succ) ++ suffixes i.succ := by | |
| rw [frame (.inr (.inl i.succ)) (by simp) (by simp) (by simp)] | |
| exact sourceWords i.succ | |
| have rest := ih (placement ∘ shift width) (distinct.comp (shift_injective width)) | |
| (fun i => indices i.succ) (fun i => offsets i.succ) (fun i => fields i.succ) | |
| (fun i => selected i.succ) (fun l => labels (.inr l)) | |
| (fun l => atLabels (.inr l)) mid (fun i => suffixes i.succ) | |
| middleTable middleScratch middleSources none | |
| simp only [entry] | |
| rw [steps, Nat.add_comm, Function.iterate_add_apply, first] | |
| exact rest | |
| /-- Every output is the physically gathered unary field, with its old suffix | |
| preserved. This identifies the operands consumed by the Horner phase. -/ | |
| theorem finalTapes_field (values : List Nat) (coefficient width : Nat) | |
| (placement : Tape width → K) (distinct : Function.Injective placement) | |
| (indices offsets fields : Fin width → Nat) (base : K → List Bool) (i : Fin width) : | |
| finalTapes values coefficient width placement indices offsets fields base | |
| (placement (.inr (.inr i))) = | |
| encodeWord (fields i) ++ base (placement (.inr (.inr i))) := by | |
| induction width generalizing base with | |
| | zero => exact Fin.elim0 i | |
| | succ width ih => | |
| have hd (a b : Tape (width + 1)) (hne : a ≠ b) : placement a ≠ placement b := | |
| fun h => hne (distinct h) | |
| let mid := MachineAffineLookup.finalTapes (placement ∘ lookupRole width) base values | |
| (coefficient * indices 0 + offsets 0) (fields 0) | |
| refine Fin.cases ?_ (fun j => ?_) i | |
| · change finalTapes values coefficient width (placement ∘ shift width) | |
| (fun j => indices j.succ) (fun j => offsets j.succ) (fun j => fields j.succ) | |
| mid (placement (.inr (.inr 0))) = _ | |
| rw [finalTapes_other values coefficient width _ _ _ _ mid _ | |
| (hd _ _ (by simp [shift])) (hd _ _ (by simp [shift])) | |
| (fun j => hd _ _ (by simp [shift, Fin.ext_iff]))] | |
| exact MachineAffineLookup.finalTapes_output _ _ _ _ _ | |
| · change finalTapes values coefficient width (placement ∘ shift width) | |
| (fun j => indices j.succ) (fun j => offsets j.succ) (fun j => fields j.succ) | |
| mid ((placement ∘ shift width) (.inr (.inr j))) = _ | |
| rw [ih (placement ∘ shift width) (distinct.comp (shift_injective width)) | |
| (fun j => indices j.succ) (fun j => offsets j.succ) (fun j => fields j.succ) mid j] | |
| congr 1 | |
| exact MachineAffineLookup.finalTapes_other (placement ∘ lookupRole width) base values | |
| (coefficient * indices 0 + offsets 0) (fields 0) | |
| (placement (.inr (.inr j.succ))) | |
| (hd _ _ (by simp [lookupRole])) (hd _ _ (by simp [lookupRole])) | |
| (hd _ _ (by simp [lookupRole, Fin.ext_iff])) | |
| /-- For fixed width, gather time is linear in the encoded table size plus | |
| coordinate magnitudes. Query and scan debris is never rescanned. -/ | |
| theorem steps_le (values : List Nat) (coefficient width magnitude : Nat) | |
| (indices offsets fields : Fin width → Nat) | |
| (selected : ∀ i, values[coefficient * indices i + offsets i]? = some (fields i)) | |
| (bounded : ∀ i, indices i ≤ magnitude) : | |
| steps values coefficient width indices offsets ≤ | |
| width * (2 * magnitude + 5 * (encodeWords values).length + 6) + 1 := by | |
| induction width with | |
| | zero => simp [steps] | |
| | succ width ih => | |
| have first := MachineAffineLookup.steps_le values (indices 0) coefficient (offsets 0) | |
| (fields 0) (selected 0) | |
| have rest := ih (fun i => indices i.succ) (fun i => offsets i.succ) | |
| (fun i => fields i.succ) (fun i => selected i.succ) (fun i => bounded i.succ) | |
| have hb := bounded 0 | |
| rw [steps, Nat.add_mul, Nat.one_mul] | |
| omega | |
| end UniqueGamesTheorem.Explicit.MachineProductGather | |
| end OAI | |
Xet Storage Details
- Size:
- 46 kB
- Xet hash:
- 6ddf493522f6f95193cc0842e48a70296d972591a291aae34a7d528b0e061b6c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.