import OAI.Computability.UniqueGames.PCP.EmitRows import OAI.Computability.UniqueGames.PCP.RuntimeModel namespace OAI section /-! Exact tape invariant for the old-dart loop. The cursor stores the remaining input rows, while the reversed tape stores precisely the output headers and the blocks already emitted. The scan and index-scan tapes are unconstrained: the next lookup prefixes fresh copies and retains old residues as suffixes. Finite control state is specified by the actual execution theorems using this tape invariant. -/ namespace UniqueGamesTheorem.Foundations.PCP.AlphabetTable.LoopState open UniqueGamesTheorem.Foundations.Complexity open RuntimeModel variable {q : Nat} def inputBits (input : GenericGraphTables.Table q) : List Bool := GenericGraphTables.tableBits input /-- The output headers are included even before the first old row is read. -/ def prefixWords (input : GenericGraphTables.Table q) (e : Nat) : List Nat := [Enumeration.vertexCount input.vertices input.darts q, Enumeration.dartCount input.darts q] ++ ((List.finRange input.darts).take e).flatMap (EmitRows.blockWords input) def prefixBits (input : GenericGraphTables.Table q) (e : Nat) : List Bool := encodeWords (prefixWords input e) def cursorWords (input : GenericGraphTables.Table q) (e : Nat) : List Nat := ((GenericGraphTables.rowList input).drop e).flatMap GenericGraphTables.rowWords def cursorBits (input : GenericGraphTables.Table q) (e : Nat) : List Bool := encodeWords (cursorWords input e) @[simp] theorem prefixWords_zero (input : GenericGraphTables.Table q) : prefixWords input 0 = [Enumeration.vertexCount input.vertices input.darts q, Enumeration.dartCount input.darts q] := by simp [prefixWords] @[simp] theorem prefixBits_zero (input : GenericGraphTables.Table q) : prefixBits input 0 = encodeWords [Enumeration.vertexCount input.vertices input.darts q, Enumeration.dartCount input.darts q] := by rw [prefixBits, prefixWords_zero] theorem prefixWords_succ (input : GenericGraphTables.Table q) (e : Nat) (he : e < input.darts) : prefixWords input (e + 1) = prefixWords input e ++ EmitRows.blockWords input ⟨e, he⟩ := by have bound : e < (List.finRange input.darts).length := by simpa using he have take := List.take_succ_eq_append_getElem bound have atIndex : (List.finRange input.darts)[e]'bound = (⟨e, he⟩ : Fin input.darts) := by simp rw [atIndex] at take unfold prefixWords rw [take, List.flatMap_append] simp only [List.flatMap_cons, List.flatMap_nil, List.append_nil, List.append_assoc] theorem prefixBits_succ (input : GenericGraphTables.Table q) (e : Nat) (he : e < input.darts) : prefixBits input (e + 1) = prefixBits input e ++ encodeWords (EmitRows.blockWords input ⟨e, he⟩) := by rw [prefixBits, prefixWords_succ input e he, encodeWords_append] rfl theorem prefixBits_reverse_succ (input : GenericGraphTables.Table q) (e : Nat) (he : e < input.darts) : (prefixBits input (e + 1)).reverse = (encodeWords (EmitRows.blockWords input ⟨e, he⟩)).reverse ++ (prefixBits input e).reverse := by rw [prefixBits_succ input e he, List.reverse_append] theorem prefixWords_full (input : GenericGraphTables.Table q) : prefixWords input input.darts = GraphTables.tableWords (Table.build input) := by have allRows : (List.finRange input.darts).take input.darts = List.finRange input.darts := by apply List.take_of_length_le simp unfold prefixWords rw [allRows] exact (EmitRows.tableWords_eq_blocks input).symm /-- At the terminal guard the stored prefix is the complete concrete output encoding. -/ theorem prefixBits_full (input : GenericGraphTables.Table q) : prefixBits input input.darts = GraphTables.tableBits (Table.build input) := by rw [prefixBits, prefixWords_full] rfl @[simp] theorem cursorBits_zero (input : GenericGraphTables.Table q) : cursorBits input 0 = encodeWords ((GenericGraphTables.rowList input).flatMap GenericGraphTables.rowWords) := by simp [cursorBits, cursorWords] theorem cursorWords_succ (input : GenericGraphTables.Table q) (e : Nat) (he : e < input.darts) : cursorWords input e = GenericGraphTables.rowWords input.rows[(⟨e, he⟩ : Fin input.darts)] ++ cursorWords input (e + 1) := by have bound : e < (GenericGraphTables.rowList input).length := by simpa only [GenericGraphTables.rowList_length] using he unfold cursorWords rw [List.drop_eq_getElem_cons bound, List.flatMap_cons] simp only [GenericGraphTables.rowList, Vector.getElem_toList, Fin.getElem_fin] theorem cursorBits_succ (input : GenericGraphTables.Table q) (e : Nat) (he : e < input.darts) : cursorBits input e = encodeWords (GenericGraphTables.rowWords input.rows[(⟨e, he⟩ : Fin input.darts)]) ++ cursorBits input (e + 1) := by rw [cursorBits, cursorWords_succ input e he, encodeWords_append] rfl @[simp] theorem cursorBits_full (input : GenericGraphTables.Table q) : cursorBits input input.darts = [] := by have finished := List.drop_length (l := GenericGraphTables.rowList input) rw [GenericGraphTables.rowList_length] at finished unfold cursorBits cursorWords rw [finished] rfl /-- Guard-to-guard tape contract. Only scan and indexScan may contain residues. -/ structure Ready (input : GenericGraphTables.Table q) (e : Nat) (base : Tape → List Bool) : Prop where le_darts : e ≤ input.darts original : base .original = inputBits input cursor : base .cursor = cursorBits input e vertices : base .vertices = encodeWord input.vertices darts : base .darts = encodeWord input.darts counter : base .counter = encodeWord (input.darts - e) edge : base .edge = encodeWord e tail : base .tail = [] reverseIndex : base .reverseIndex = [] head : base .head = [] scratch : base .scratch = [] compareLeft : base .compareLeft = [] compareRight : base .compareRight = [] output : base .output = [] reversed : base .reversed = (prefixBits input e).reverse /-- Changing only lookup residues preserves the complete loop contract. -/ theorem Ready.of_agree {input : GenericGraphTables.Table q} {e : Nat} {base next : Tape → List Bool} (ready : Ready input e base) (agree : ∀ tape, tape ≠ .scan → tape ≠ .indexScan → next tape = base tape) : Ready input e next where le_darts := ready.le_darts original := (agree .original (by decide) (by decide)).trans ready.original cursor := (agree .cursor (by decide) (by decide)).trans ready.cursor vertices := (agree .vertices (by decide) (by decide)).trans ready.vertices darts := (agree .darts (by decide) (by decide)).trans ready.darts counter := (agree .counter (by decide) (by decide)).trans ready.counter edge := (agree .edge (by decide) (by decide)).trans ready.edge tail := (agree .tail (by decide) (by decide)).trans ready.tail reverseIndex := (agree .reverseIndex (by decide) (by decide)).trans ready.reverseIndex head := (agree .head (by decide) (by decide)).trans ready.head scratch := (agree .scratch (by decide) (by decide)).trans ready.scratch compareLeft := (agree .compareLeft (by decide) (by decide)).trans ready.compareLeft compareRight := (agree .compareRight (by decide) (by decide)).trans ready.compareRight output := (agree .output (by decide) (by decide)).trans ready.output reversed := (agree .reversed (by decide) (by decide)).trans ready.reversed theorem Ready.update_scan {input : GenericGraphTables.Table q} {e : Nat} {base : Tape → List Bool} (ready : Ready input e base) (word : List Bool) : Ready input e (Function.update base .scan word) := by apply ready.of_agree intro tape notScan _ simp [notScan] theorem Ready.update_indexScan {input : GenericGraphTables.Table q} {e : Nat} {base : Tape → List Bool} (ready : Ready input e base) (word : List Bool) : Ready input e (Function.update base .indexScan word) := by apply ready.of_agree intro tape _ notIndexScan simp [notIndexScan] theorem Ready.counter_succ {input : GenericGraphTables.Table q} {e : Nat} {base : Tape → List Bool} (ready : Ready input e base) (he : e < input.darts) : base .counter = encodeWord (input.darts - (e + 1) + 1) := by rw [ready.counter] congr 1 omega theorem Ready.counter_at_end {input : GenericGraphTables.Table q} {base : Tape → List Bool} (ready : Ready input input.darts base) : base .counter = encodeWord 0 := by simpa only [Nat.sub_self] using ready.counter theorem Ready.cursor_at_end {input : GenericGraphTables.Table q} {base : Tape → List Bool} (ready : Ready input input.darts base) : base .cursor = [] := ready.cursor.trans (cursorBits_full input) theorem Ready.reversed_at_end {input : GenericGraphTables.Table q} {base : Tape → List Bool} (ready : Ready input input.darts base) : base .reversed = (GraphTables.tableBits (Table.build input)).reverse := by rw [ready.reversed, prefixBits_full] end UniqueGamesTheorem.Foundations.PCP.AlphabetTable.LoopState end end OAI