import OAI.Computability.UniqueGames.PCP.Addresses import OAI.Computability.UniqueGames.PCP.Table namespace OAI /-! The actual alphabet-reduction row template. The old predicate and the loop flag are finite registers. All unbounded quantities reside in five preserved unary tapes. The fixed event/slot/orientation order is retained without deduplication. -/ namespace UniqueGamesTheorem.Foundations.PCP.AlphabetTable.EmitRows open Turing open UniqueGamesTheorem.Foundations.Complexity open Emitter abbrev Context (q : Nat) := Vector Bool (q * q) × Bool abbrev RowCommand (q : Nat) := Command 5 (Context q) (Addresses.fieldBound q) def relationBits (q : Nat) (event : AlphabetGraph.LocalEvent (Fin q)) (slot : Fin 6) (orientation : Bool) : List (Context q → Bool) := List.ofFn fun index : Fin 4096 => fun context => (Relations.relationFromTable context.1 context.2 event slot orientation)[index] private theorem vector_list_ofFn {α : Type} {n : Nat} (vector : Vector α n) : List.ofFn (fun index : Fin n => vector[index]) = vector.toList := by have equality := congrArg Vector.toList (Vector.ofFn_getElem (xs := vector)) simpa only [Vector.toList_ofFn, Fin.getElem_fin] using equality private theorem bitValues_ofFn {σ : Type} {n : Nat} (relation : σ → Vector Bool n) (context : σ) : (List.ofFn (fun index : Fin n => fun state => (relation state)[index])).map (fun bit => if bit context then 1 else 0) = (relation context).toList.map GraphTables.bitWord := by rw [← vector_list_ofFn (relation context)] simp only [List.map_ofFn] rfl theorem relationBits_values (q : Nat) (event : AlphabetGraph.LocalEvent (Fin q)) (slot : Fin 6) (orientation : Bool) (context : Context q) : (relationBits q event slot orientation).map (fun bit => if bit context then 1 else 0) = GraphTables.relationWords (Relations.relationFromTable context.1 context.2 event slot orientation) := by exact bitValues_ofFn (fun state : Context q => Relations.relationFromTable state.1 state.2 event slot orientation) context /-- Two actual affine fields followed by the 4096 stored relation entries. -/ def rowCommands (q : Nat) (event : AlphabetGraph.LocalEvent (Fin q)) (slot : Fin 6) (orientation : Bool) : List (RowCommand q) := affineCommands (Addresses.dartTailTerms q event slot orientation) (fun context => Addresses.dartTailOffset q event slot orientation (Relations.oldPredicate context.1)) ++ affineCommands (Addresses.reverseDartField q event slot orientation).terms (fun _ => (Addresses.reverseDartField q event slot orientation).offset) ++ bitCommands (relationBits q event slot orientation) def rowContext {q : Nat} (input : GenericGraphTables.Table q) (edge : Fin input.darts) : Context q := let old := input.rows[edge] (old.relation, decide (old.tail = input.rows[old.reverseIndex].tail)) def rowValues {q : Nat} (input : GenericGraphTables.Table q) (edge : Fin input.darts) : Fin 5 → Nat := let old := input.rows[edge] Addresses.values input.vertices input.darts edge.val old.tail.val input.rows[old.reverseIndex].tail.val private theorem encodedRow {n m : Nat} (row : GraphTables.DartRow n m) : encodeWords (GraphTables.rowWords row) = encodeWord row.tail.val ++ (encodeWord row.reverseIndex.val ++ encodeWords (GraphTables.relationWords row.relation)) := by simp only [GraphTables.rowWords, List.cons_append, List.nil_append, encodeWords] /-- The executable command semantics equals the precise stored row, including the orientation-specific tail, reverse index, and every predicate bit. -/ theorem rowCommands_bits {q : Nat} (input : GenericGraphTables.Table q) (edge : Fin input.darts) (event : AlphabetGraph.LocalEvent (Fin q)) (slot : Fin 6) (orientation : Bool) : (rowCommands q event slot orientation).flatMap (commandBits (rowValues input edge) (rowContext input edge)) = encodeWords (GraphTables.rowWords (Table.rowFromDart input (((edge, event), slot), orientation))) := by rw [rowCommands, List.flatMap_append, List.flatMap_append, affineCommands_bits, affineCommands_bits, bitCommands_bits, relationBits_values] have htail := Addresses.fieldForDartTail_eval input.vertices input.darts q edge input.rows[edge].tail input.rows[input.rows[edge].reverseIndex].tail event slot orientation (Relations.oldPredicate input.rows[edge].relation) have hreverse := Addresses.reverseDartField_eval input.vertices input.darts q edge event slot orientation input.rows[edge].tail.val input.rows[input.rows[edge].reverseIndex].tail.val let row := Table.rowFromDart input (((edge, event), slot), orientation) have tailEq : affineValue (Addresses.dartTailTerms q event slot orientation) (rowValues input edge) (Addresses.dartTailOffset q event slot orientation (Relations.oldPredicate (rowContext input edge).1)).val = row.tail.val := htail have reverseEq : affineValue (Addresses.reverseDartField q event slot orientation).terms (rowValues input edge) (Addresses.reverseDartField q event slot orientation).offset.val = row.reverseIndex.val := hreverse have relationEq : Relations.relationFromTable (rowContext input edge).1 (rowContext input edge).2 event slot orientation = row.relation := rfl rw [encodedRow] have combined := congrArg₂ (fun a b : List Bool => a ++ b) (congrArg encodeWord tailEq) (congrArg₂ (fun a b : List Bool => a ++ b) (congrArg encodeWord reverseEq) (congrArg (fun relation => encodeWords (GraphTables.relationWords relation)) relationEq)) simpa only [List.append_assoc] using combined /-- One old dart emits every local event, then all six slots, false orientation before true. These loops are fixed by q and form part of the finite template. -/ def blockCommands (q : Nat) : List (RowCommand q) := (Enumeration.localEvents q).flatMap fun event => (List.finRange 6).flatMap fun slot => [false, true].flatMap fun orientation => rowCommands q event slot orientation def blockWords {q : Nat} (input : GenericGraphTables.Table q) (edge : Fin input.darts) : List Nat := (Enumeration.localEvents q).flatMap fun event => (List.finRange 6).flatMap fun slot => [false, true].flatMap fun orientation => GraphTables.rowWords (Table.rowFromDart input (((edge, event), slot), orientation)) theorem tableWords_eq_blocks {q : Nat} (input : GenericGraphTables.Table q) : GraphTables.tableWords (Table.build input) = [Enumeration.vertexCount input.vertices input.darts q, Enumeration.dartCount input.darts q] ++ (List.finRange input.darts).flatMap (blockWords input) := by rw [Table.tableWords_eq_ordered, Enumeration.ordered_dart] unfold blockWords simp only [List.flatMap_assoc, List.flatMap_cons, List.flatMap_nil, List.append_nil] /-- Headers use only the preserved n and m tapes; all relation registers may still have their initial values. -/ def headerCommands (q : Nat) (σ : Type) : List (Command 2 σ (Addresses.fieldBound q)) := affineCommands [(0, Enumeration.tapeCount q), (1, Enumeration.localCount q + Enumeration.pairTapeCount q)] (fun _ => Addresses.zeroOffset q) ++ affineCommands [(1, 12 * Enumeration.localCount q)] (fun _ => Addresses.zeroOffset q) theorem headerCommands_bits {σ : Type} (q n m : Nat) (ambient : σ) : (headerCommands q σ).flatMap (commandBits ![n, m] ambient) = encodeWords [Enumeration.vertexCount n m q, Enumeration.dartCount m q] := by rw [headerCommands, List.flatMap_append, affineCommands_bits, affineCommands_bits] have vertices : affineValue [(0, Enumeration.tapeCount q), (1, Enumeration.localCount q + Enumeration.pairTapeCount q)] ![n, m] (Addresses.zeroOffset q).val = Enumeration.vertexCount n m q := by exact Addresses.headerVertices_eval q n m 0 0 0 have darts : affineValue [(1, 12 * Enumeration.localCount q)] ![n, m] (Addresses.zeroOffset q).val = Enumeration.dartCount m q := by exact Addresses.headerDarts_eval q n m 0 0 0 rw [vertices, darts] simp [encodeWords] @[simp] theorem headerCommands_length (q : Nat) (σ : Type) : (headerCommands q σ).length = 7 := by simp [headerCommands, affineCommands_length] def headerInTime {K Λ σ : Type} [DecidableEq K] (q n m : Nat) (sources : Fin 2 → K) (scratch output : K) (sourceScratch : ∀ i, sources i ≠ scratch) (sourceOutput : ∀ i, sources i ≠ output) (scratchOutput : scratch ≠ output) (labels : Label (headerCommands q σ).length (Addresses.fieldBound q) → Λ) (exit : Option Λ) (p : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ)) (atLabels : ∀ label, p (labels label) = statement (listCommands (headerCommands q σ)) sources scratch output labels exit label) (base : K → List Bool) (operands : ∀ i, base (sources i) = encodeWord (![n, m] i)) (scratchEmpty : base scratch = []) (ambient : σ) (N : Nat) (hn : n ≤ N) (hm : m ≤ N) : StateTransition.EvalsToInTime (TM2.step p) ⟨some (labels (labelAt (headerCommands q σ).length (Addresses.fieldBound q) 0 .entry)), ((ambient, ()), none), base⟩ (some ⟨exit, ((ambient, ()), none), resultTapes output base (encodeWords [Enumeration.vertexCount n m q, Enumeration.dartCount m q])⟩) (7 * (3 * (N + 1) + 3) + 1) := by have bounded : ∀ i : Fin 2, ![n, m] i ≤ N := by intro i fin_cases i <;> simp [hn, hm] have run := planInTime (listCommands (headerCommands q σ)) sources scratch output sourceScratch sourceOutput scratchOutput labels exit p atLabels ![n, m] base operands scratchEmpty ambient N bounded rw [bits_listCommands, headerCommands_bits] at run simpa only [headerCommands_length] using run private theorem encodeWords_flatMap {α : Type} (items : List α) (words : α → List Nat) : encodeWords (items.flatMap words) = items.flatMap (fun item => encodeWords (words item)) := by induction items with | nil => rfl | cons item items ih => simp [encodeWords_append, ih] theorem blockCommands_bits {q : Nat} (input : GenericGraphTables.Table q) (edge : Fin input.darts) : (blockCommands q).flatMap (commandBits (rowValues input edge) (rowContext input edge)) = encodeWords (blockWords input edge) := by simp only [blockCommands, blockWords, List.flatMap_assoc, encodeWords_flatMap, rowCommands_bits] theorem rowCommands_length_le (q : Nat) (event : AlphabetGraph.LocalEvent (Fin q)) (slot : Fin 6) (orientation : Bool) : (rowCommands q event slot orientation).length ≤ 4104 := by have htail := Addresses.dartTailTerms_length_le q event slot orientation simp only [rowCommands, List.length_append, affineCommands_length, Addresses.reverseDartField, List.length_cons, List.length_nil, bitCommands, List.length_map, relationBits, List.length_ofFn] omega private theorem flatMap_length_le {α β : Type} (items : List α) (f : α → List β) (bound : Nat) (bounded : ∀ item ∈ items, (f item).length ≤ bound) : (items.flatMap f).length ≤ items.length * bound := by induction items with | nil => simp | cons item items ih => have hh := bounded item (by simp) have ht := ih (fun x hx => bounded x (by simp [hx])) simp only [List.flatMap_cons, List.length_append, List.length_cons, Nat.succ_mul] omega theorem blockCommands_length_le (q : Nat) : (blockCommands q).length ≤ Enumeration.localCount q * (6 * (2 * 4104)) := by unfold blockCommands apply le_trans (flatMap_length_le _ _ (6 * (2 * 4104)) ?_) (by simp) intro event _ apply le_trans (flatMap_length_le _ _ (2 * 4104) ?_) (by simp) intro slot _ apply le_trans (flatMap_length_le _ _ 4104 ?_) (by simp) intro orientation _ exact rowCommands_length_le q event slot orientation /-- Actual execution of all rows belonging to one old dart. The finite relation register is supplied by the checked row parser; only code equations are assumed about the enclosing program. The trace restores the five operand tapes. -/ def blockInTime {K Λ : Type} [DecidableEq K] {q : Nat} (input : GenericGraphTables.Table q) (edge : Fin input.darts) (sources : Fin 5 → K) (scratch output : K) (sourceScratch : ∀ i, sources i ≠ scratch) (sourceOutput : ∀ i, sources i ≠ output) (scratchOutput : scratch ≠ output) (labels : Label (blockCommands q).length (Addresses.fieldBound q) → Λ) (exit : Option Λ) (p : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State (Context q))) (atLabels : ∀ label, p (labels label) = statement (listCommands (blockCommands q)) sources scratch output labels exit label) (base : K → List Bool) (operands : ∀ i, base (sources i) = encodeWord (rowValues input edge i)) (scratchEmpty : base scratch = []) (N : Nat) (bounded : ∀ i, rowValues input edge i ≤ N) : StateTransition.EvalsToInTime (TM2.step p) ⟨some (labels (labelAt (blockCommands q).length (Addresses.fieldBound q) 0 .entry)), ((rowContext input edge, ()), none), base⟩ (some ⟨exit, ((rowContext input edge, ()), none), resultTapes output base (encodeWords (blockWords input edge))⟩) (Enumeration.localCount q * (6 * (2 * 4104)) * (3 * (N + 1) + 3) + 1) := by let run := planInTime (listCommands (blockCommands q)) sources scratch output sourceScratch sourceOutput scratchOutput labels exit p atLabels (rowValues input edge) base operands scratchEmpty (rowContext input edge) N bounded have hbits := bits_listCommands (blockCommands q) (rowValues input edge) (rowContext input edge) rw [blockCommands_bits] at hbits rw [hbits] at run exact { toEvalsTo := run.toEvalsTo, steps_le_m := run.steps_le_m.trans (Nat.add_le_add_right (Nat.mul_le_mul_right _ (blockCommands_length_le q)) 1) } /-- Preserve additional finite parser/driver registers through a projection. -/ def blockCommandsIn {σ : Type} (q : Nat) (context : σ → Context q) : List (Command 5 σ (Addresses.fieldBound q)) := (blockCommands q).map (fun command => command.mapAmbient context) theorem blockCommandsIn_bits {σ : Type} {q : Nat} (input : GenericGraphTables.Table q) (edge : Fin input.darts) (context : σ → Context q) (ambient : σ) (hcontext : context ambient = rowContext input edge) : (blockCommandsIn q context).flatMap (commandBits (rowValues input edge) ambient) = encodeWords (blockWords input edge) := by simp only [blockCommandsIn, List.flatMap_map, commandBits_mapAmbient] rw [hcontext] exact blockCommands_bits input edge def blockInTimeAmbient {K Λ σ : Type} [DecidableEq K] {q : Nat} (input : GenericGraphTables.Table q) (edge : Fin input.darts) (context : σ → Context q) (ambient : σ) (hcontext : context ambient = rowContext input edge) (sources : Fin 5 → K) (scratch output : K) (sourceScratch : ∀ i, sources i ≠ scratch) (sourceOutput : ∀ i, sources i ≠ output) (scratchOutput : scratch ≠ output) (labels : Label (blockCommandsIn q context).length (Addresses.fieldBound q) → Λ) (exit : Option Λ) (p : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ)) (atLabels : ∀ label, p (labels label) = statement (listCommands (blockCommandsIn q context)) sources scratch output labels exit label) (base : K → List Bool) (operands : ∀ i, base (sources i) = encodeWord (rowValues input edge i)) (scratchEmpty : base scratch = []) (N : Nat) (bounded : ∀ i, rowValues input edge i ≤ N) : StateTransition.EvalsToInTime (TM2.step p) ⟨some (labels (labelAt (blockCommandsIn q context).length (Addresses.fieldBound q) 0 .entry)), ((ambient, ()), none), base⟩ (some ⟨exit, ((ambient, ()), none), resultTapes output base (encodeWords (blockWords input edge))⟩) (Enumeration.localCount q * (6 * (2 * 4104)) * (3 * (N + 1) + 3) + 1) := by have run := planInTime (listCommands (blockCommandsIn q context)) sources scratch output sourceScratch sourceOutput scratchOutput labels exit p atLabels (rowValues input edge) base operands scratchEmpty ambient N bounded rw [bits_listCommands, blockCommandsIn_bits input edge context ambient hcontext] at run have lengthBound : (blockCommandsIn q context).length ≤ Enumeration.localCount q * (6 * (2 * 4104)) := by simpa only [blockCommandsIn, List.length_map] using blockCommands_length_le q refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ } exact run.steps_le_m.trans (Nat.add_le_add_right (Nat.mul_le_mul_right _ lengthBound) 1) end UniqueGamesTheorem.Foundations.PCP.AlphabetTable.EmitRows end OAI