cfahlgren1/math / lean /OAI /Computability /MaxCut /PCP /PreprocessingPaddingWords.lean
cfahlgren1's picture
download
raw
21.1 kB
import Mathlib
import OAI.Computability.MaxCut.PCP.PreprocessingInternalRows
import OAI.Computability.MaxCut.Machines.MachineDummyRows
namespace OAI
/-!
# Exact serialization of whole-vertex padding
The original dart rows remain in their original order, with every old tail,
reverse index, and relation bit unchanged. The suffix is exactly the bit stream
emitted by the checked dummy-vertex machine, starting at vertex `n` and dart
`n*d`. This is an equality for the actual table codec, with no runtime premise.
-/
namespace MaxCutGames.Foundations.PCP.PreprocessingPaddingWords
open Complexity PortTables
variable {n m d : Nat}
private theorem encodeWords_flatMap_inline_PreprocessingPaddingWords {α : Type*} (xs : List α) (words : α → List Nat) :
encodeWords (xs.flatMap words) = xs.flatMap (fun x => encodeWords (words x)) := by
induction xs with
| nil => rfl
| cons x xs ih => simp only [List.flatMap_cons, encodeWords_append, ih]
/-- Split any indexed list at the actual old/new vertex boundary. -/
theorem ofFn_split {α : Type*} (h : n ≤ m) (f : Fin m → α) :
List.ofFn f =
List.ofFn (fun v : Fin n => f (v.castLE h)) ++
List.ofFn (fun v : Fin (m - n) => f (PreprocessingPaddingTables.vertexEquiv h (.inr v))) := by
calc
List.ofFn f = List.ofFn
(fun v : Fin (n + (m - n)) => f (Fin.cast (Nat.add_sub_of_le h) v)) :=
List.ofFn_congr (Nat.add_sub_of_le h).symm f
_ = _ := by rw [List.ofFn_add]; rfl
/-- Mixed-radix row order is vertex-major, then increasing port order. -/
theorem flatten_ofFn_rowIndex {α : Type*} (f : Fin (n * d) → List α) :
(List.ofFn f).flatten =
(List.ofFn (fun v : Fin n =>
(List.ofFn (fun p : Fin d => f (rowIndex n d (v, p)))).flatten)).flatten := by
rw [List.ofFn_mul, List.flatten_flatten, List.map_ofFn]
apply congrArg List.flatten
apply congrArg List.ofFn
funext v
apply congrArg List.flatten
apply congrArg List.ofFn
funext p
apply congrArg f
apply Fin.ext
simp only [rowIndex_val, Nat.mul_comm, Nat.add_comm]
/-- The bits of one actual table row, in the existing graph codec. -/
def rowBits (table : Table n d) (v : Fin n) (p : Fin d) : List Bool :=
encodeWord v.val ++ encodeWord (table.reverseIndex[rowIndex n d (v, p)]).val ++
encodeWords (GraphTables.relationWords table.relations[rowIndex n d (v, p)])
def vertexBits (table : Table n d) (v : Fin n) : List Bool :=
(List.ofFn (fun p : Fin d => rowBits table v p)).flatten
/-- The old encoded rows, without their two header words. -/
def rowsBits (table : Table n d) : List Bool :=
encodeWords ((flatRows table).toList.flatMap GraphTables.rowWords)
/-- Recover the original flat row from its exact mixed-radix coordinates. -/
theorem rowBits_flat (table : Table n d) (i : Fin (n * d)) :
encodeWords (GraphTables.rowWords ((flatRows table)[i])) =
rowBits table ((rowIndex n d).symm i).1 ((rowIndex n d).symm i).2 := by
simp only [flatRows, Vector.getElem_ofFn, Fin.getElem_fin,
MachineTableRows.rowBits_eq, rowBits, Prod.eta, Equiv.apply_symm_apply]
/-- Serializing all rows agrees with grouping their bits by vertex and port. -/
theorem rowsBits_eq_vertices (table : Table n d) :
rowsBits table = (List.ofFn (fun v : Fin n => vertexBits table v)).flatten := by
have hflat : (flatRows table).toList =
List.ofFn (fun i : Fin (n * d) => (flatRows table)[i]) := by
simp only [flatRows, Vector.toList_ofFn, Vector.getElem_ofFn, Fin.getElem_fin]
unfold rowsBits
rw [hflat, encodeWords_flatMap_inline_PreprocessingPaddingWords, List.flatMap_def, List.map_ofFn]
change (List.ofFn (fun i : Fin (n * d) =>
encodeWords (GraphTables.rowWords ((flatRows table)[i])))).flatten = _
simp_rw [rowBits_flat]
simpa only [Equiv.symm_apply_apply, vertexBits] using
flatten_ofFn_rowIndex (fun i : Fin (n * d) =>
rowBits table ((rowIndex n d).symm i).1 ((rowIndex n d).symm i).2)
/-- Every bit in an original row survives padding unchanged. -/
theorem rowBits_pad_old (table : Table n d) (h : n ≤ m) (v : Fin n) (p : Fin d) :
rowBits (PreprocessingPaddingTables.pad table h) (v.castLE h) p = rowBits table v p := by
unfold rowBits
rw [PreprocessingPaddingTables.reverseIndex_pad_old,
PreprocessingPaddingTables.relations_pad_old]
have he : (rowIndex m d ((rotation table (v, p)).1.castLE h,
(rotation table (v, p)).2)).val = (table.reverseIndex[rowIndex n d (v, p)]).val := by
rw [← rowIndex_rotation]
rfl
rw [he]
rfl
/-- A new row is the machine's self-loop row with its actual global index. -/
theorem rowBits_pad_new (table : Table n d) (h : n ≤ m)
(v : Fin (m - n)) (p : Fin d) :
rowBits (PreprocessingPaddingTables.pad table h)
(PreprocessingPaddingTables.vertexEquiv h (.inr v)) p =
MachineDummyRows.rowBits (n + v.val) (p.val + d * (n + v.val)) := by
unfold rowBits
rw [PreprocessingPaddingTables.reverseIndex_pad_new,
PreprocessingPaddingTables.relations_pad_new]
rfl
/-- The row emitter's recursive stream enumerates successive reverse indices. -/
theorem dummy_rowsBits_eq_ofFn (v e count : Nat) :
MachineDummyRows.rowsBits v e count =
(List.ofFn (fun p : Fin count => MachineDummyRows.rowBits v (e + p.val))).flatten := by
induction count generalizing e with
| zero => rfl
| succ count ih =>
rw [MachineDummyRows.rowsBits, List.ofFn_succ, List.flatten_cons, ih]
simp only [Fin.val_zero, Nat.add_zero, Fin.val_succ]
congr 1
apply congrArg (fun f : Fin count → List Bool => (List.ofFn f).flatten)
funext p
congr 1
omega
/-- The vertex emitter's recursive stream enumerates consecutive vertex blocks. -/
theorem paddingBits_eq_ofFn (d v e count : Nat) :
MachinePaddingRows.paddingBits d v e count =
(List.ofFn (fun k : Fin count =>
MachineDummyRows.rowsBits (v + k.val) (e + k.val * d) d)).flatten := by
induction count generalizing v e with
| zero => rfl
| succ count ih =>
rw [MachinePaddingRows.paddingBits, List.ofFn_succ, List.flatten_cons, ih]
simp only [Fin.val_zero, Nat.zero_mul, Nat.add_zero, Fin.val_succ, Nat.add_mul, Nat.one_mul]
congr 1
apply congrArg (fun f : Fin count → List Bool => (List.ofFn f).flatten)
funext k
congr 1 <;> omega
@[simp] theorem vertexBits_pad_old (table : Table n d) (h : n ≤ m) (v : Fin n) :
vertexBits (PreprocessingPaddingTables.pad table h) (v.castLE h) = vertexBits table v := by
simp only [vertexBits, rowBits_pad_old]
/-- The new vertex's complete port block equals the actual machine block. -/
theorem vertexBits_pad_new (table : Table n d) (h : n ≤ m) (v : Fin (m - n)) :
vertexBits (PreprocessingPaddingTables.pad table h)
(PreprocessingPaddingTables.vertexEquiv h (.inr v)) =
MachineDummyRows.rowsBits (n + v.val) (n * d + v.val * d) d := by
unfold vertexBits
simp_rw [rowBits_pad_new]
rw [dummy_rowsBits_eq_ofFn]
apply congrArg (fun f : Fin d → List Bool => (List.ofFn f).flatten)
funext p
congr 1
ring
/-- The actual padded row codec is the old row codec followed by the emitted suffix. -/
theorem rowsBits_pad (table : Table n d) (h : n ≤ m) :
rowsBits (PreprocessingPaddingTables.pad table h) =
rowsBits table ++ MachinePaddingRows.paddingBits d n (n * d) (m - n) := by
rw [rowsBits_eq_vertices, ofFn_split h, List.flatten_append]
simp_rw [vertexBits_pad_old, vertexBits_pad_new]
rw [← rowsBits_eq_vertices, ← paddingBits_eq_ofFn]
/-- Exact final codec identity used by the machine-output correspondence. -/
theorem tableBits_pad (table : Table n d) (h : n ≤ m) :
tableBits (PreprocessingPaddingTables.pad table h) =
encodeWords [m, m * d] ++ rowsBits table ++
MachinePaddingRows.paddingBits d n (n * d) (m - n) := by
change encodeWords (PortTables.tableWords (PreprocessingPaddingTables.pad table h)) = _
rw [tableWords_eq, encodeWords_append]
change encodeWords [m, m * d] ++ rowsBits (PreprocessingPaddingTables.pad table h) = _
rw [rowsBits_pad, List.append_assoc]
end MaxCutGames.Foundations.PCP.PreprocessingPaddingWords
/-!
# Complete serialization of the actual regularized table
Each global vertex emits its internal ports in increasing order, then its
inherited port. Original darts are the first global vertices. Dummy vertices
follow in increasing owner and local-dummy order. These are identities for the
actual stored rows and existing codec, with arbitrary actual cloud tables.
-/
namespace MaxCutGames.Foundations.PCP.PreprocessingRegularWords
open DegreeReplacement PreprocessingCloudIndex PreprocessingRegularTables Complexity
open scoped BigOperators
variable (t : GraphTables.Table) (padding : Fin t.vertices → Nat) {q : Nat}
variable (tables : ∀ v, ExpanderTables.Table (cloudSize t v + padding v) q)
/-- Read one row of the actual output, in the chosen global coordinates. -/
def actualRow (x : Vertex t padding) (p : Fin q ⊕ Unit) :
GraphTables.DartRow (vertexCount t padding) (vertexCount t padding * (q + 1)) :=
(PortTables.flatRows (ofCloudTables t padding tables))[
PortTables.rowIndex _ _ (vertexOrder t padding x, portOrder q p)]
/-- A vertex emits every internal port, followed by its inherited port. -/
def vertexRows (x : Vertex t padding) :
List (GraphTables.DartRow (vertexCount t padding) (vertexCount t padding * (q + 1))) :=
List.ofFn (fun p : Fin q => actualRow t padding tables x (.inl p)) ++
[actualRow t padding tables x (.inr ())]
def originalVertexRows (e : Fin t.darts) := vertexRows t padding tables (.inl e)
def dummyVertexRows (v : Fin t.vertices) (j : Fin (padding v)) :=
vertexRows t padding tables (.inr ⟨v, j⟩)
def vertexBits (x : Vertex t padding) : List Bool :=
encodeWords ((vertexRows t padding tables x).flatMap GraphTables.rowWords)
def originalVertexBits (e : Fin t.darts) : List Bool := vertexBits t padding tables (.inl e)
def dummyVertexBits (v : Fin t.vertices) (j : Fin (padding v)) : List Bool :=
vertexBits t padding tables (.inr ⟨v, j⟩)
/-- The internal row is exactly the checked lookup/select row. -/
theorem actualRow_internal (v : Fin t.vertices) (x : PaddedCloud t padding v) (p : Fin q) :
actualRow t padding tables x.val (.inl p) =
PreprocessingInternalRows.row t padding tables v x p := rfl
theorem actualRow_eq (x : Vertex t padding) (p : Fin q ⊕ Unit) :
actualRow t padding tables x p =
⟨vertexOrder t padding x,
(ofCloudTables t padding tables).reverseIndex[
PortTables.rowIndex _ _ (vertexOrder t padding x, portOrder q p)],
(ofCloudTables t padding tables).relations[
PortTables.rowIndex _ _ (vertexOrder t padding x, portOrder q p)]⟩ := by
simp only [actualRow, PortTables.flatRows, Vector.getElem_ofFn,
Fin.getElem_fin, Fin.eta, Equiv.symm_apply_apply]
@[simp] theorem actualRow_tail (x : Vertex t padding) (p : Fin q ⊕ Unit) :
(actualRow t padding tables x p).tail.val = (vertexOrder t padding x).val := by
rw [actualRow_eq]
@[simp] theorem inherited_original_reverse (e : Fin t.darts) :
(actualRow t padding tables (.inl e) (.inr ())).reverseIndex.val =
(q + 1) * t.rows[e].reverseIndex.val + q := by
rw [actualRow_eq]
change ((ofCloudTables t padding tables).reverseIndex[PortTables.rowIndex _ _
(vertexOrder t padding (.inl e), portOrder q (.inr ()))]).val = _
rw [← PortTables.rowIndex_rotation, rotation_ofCloudTables]
change (PortTables.rowIndex _ _
(vertexOrder t padding (.inl (GraphTables.reverseAt t.rows e)), portOrder q (.inr ()))).val = _
rw [PortTables.rowIndex_val, vertexOrder_original, portOrder_inherited]
exact Nat.add_comm _ _
@[simp] theorem inherited_dummy_reverse (v : Fin t.vertices) (j : Fin (padding v)) :
(actualRow t padding tables (.inr ⟨v, j⟩) (.inr ())).reverseIndex.val =
(q + 1) * (t.darts + PreprocessingPaddingOffsets.offset padding v.val + j.val) + q := by
rw [actualRow_eq]
change ((ofCloudTables t padding tables).reverseIndex[PortTables.rowIndex _ _
(vertexOrder t padding (.inr ⟨v, j⟩), portOrder q (.inr ()))]).val = _
rw [← PortTables.rowIndex_rotation, rotation_ofCloudTables]
change (PortTables.rowIndex _ _
(vertexOrder t padding (.inr ⟨v, j⟩), portOrder q (.inr ()))).val = _
rw [PortTables.rowIndex_val, PreprocessingPaddingOffsets.vertexOrder_dummy_val,
portOrder_inherited]
exact Nat.add_comm _ _
private theorem relation_ext_inline_PreprocessingRegularWords {r s : GraphTables.RelationTable}
(h : ∀ a b, GraphTables.relationAt r a b = GraphTables.relationAt s a b) : r = s := by
apply Vector.ext
intro i hi
simpa only [GraphTables.relationAt, Prod.eta, Equiv.apply_symm_apply,
Fin.getElem_fin] using h (GraphTables.relationIndex.symm ⟨i, hi⟩).1
(GraphTables.relationIndex.symm ⟨i, hi⟩).2
@[simp] theorem inherited_original_relation (e : Fin t.darts) :
(actualRow t padding tables (.inl e) (.inr ())).relation = t.rows[e].relation := by
rw [actualRow_eq]
apply relation_ext_inline_PreprocessingRegularWords
intro a b
exact accepts_original t padding tables e a b
@[simp] theorem inherited_dummy_relation (v : Fin t.vertices) (j : Fin (padding v)) :
(actualRow t padding tables (.inr ⟨v, j⟩) (.inr ())).relation =
MachineDummyRows.trueRelation := by
rw [actualRow_eq]
apply relation_ext_inline_PreprocessingRegularWords
intro a b
have h := accepts_dummy t padding tables ⟨v, j⟩ a b
simpa only [PortTables.accepts, MachineDummyRows.trueRelation,
GraphTables.relationAt, Fin.getElem_fin, Vector.getElem_replicate] using h
/-- The inherited original row keeps the complete input predicate. -/
theorem inherited_original_words (e : Fin t.darts) :
GraphTables.rowWords (actualRow t padding tables (.inl e) (.inr ())) =
[e.val, (q + 1) * t.rows[e].reverseIndex.val + q] ++
GraphTables.relationWords t.rows[e].relation := by
simp only [GraphTables.rowWords, actualRow_tail, vertexOrder_original,
inherited_original_reverse, inherited_original_relation]
/-- The inherited dummy row is an always-accepting self-loop. -/
theorem inherited_dummy_words (v : Fin t.vertices) (j : Fin (padding v)) :
GraphTables.rowWords (actualRow t padding tables (.inr ⟨v, j⟩) (.inr ())) =
[t.darts + PreprocessingPaddingOffsets.offset padding v.val + j.val,
(q + 1) * (t.darts + PreprocessingPaddingOffsets.offset padding v.val + j.val) + q] ++
GraphTables.relationWords MachineDummyRows.trueRelation := by
simp only [GraphTables.rowWords, actualRow_tail,
PreprocessingPaddingOffsets.vertexOrder_dummy_val,
inherited_dummy_reverse, inherited_dummy_relation]
private theorem encodeWords_flatMap_inline_PreprocessingRegularWords {α : Type*} (xs : List α) (words : α → List Nat) :
encodeWords (xs.flatMap words) = xs.flatMap (fun x => encodeWords (words x)) := by
induction xs with
| nil => rfl
| cons x xs ih => simp only [List.flatMap_cons, encodeWords_append, ih]
/-- Per-row serialization agrees with the generic table codec. -/
theorem actualRow_bits (x : Vertex t padding) (p : Fin q ⊕ Unit) :
encodeWords (GraphTables.rowWords (actualRow t padding tables x p)) =
PreprocessingPaddingWords.rowBits (ofCloudTables t padding tables)
(vertexOrder t padding x) (portOrder q p) := by
simpa only [actualRow, Equiv.symm_apply_apply] using
PreprocessingPaddingWords.rowBits_flat (ofCloudTables t padding tables)
(PortTables.rowIndex _ _ (vertexOrder t padding x, portOrder q p))
/-- Each block uses exactly the port order of the actual stored table. -/
theorem vertexBits_eq (x : Vertex t padding) :
vertexBits t padding tables x =
PreprocessingPaddingWords.vertexBits (ofCloudTables t padding tables)
(vertexOrder t padding x) := by
unfold vertexBits vertexRows
rw [List.flatMap_append, encodeWords_append]
simp only [List.flatMap_cons, List.flatMap_nil, List.append_nil]
rw [encodeWords_flatMap_inline_PreprocessingRegularWords, List.flatMap_def, List.map_ofFn]
change (List.ofFn (fun p : Fin q =>
encodeWords (GraphTables.rowWords (actualRow t padding tables x (.inl p))))).flatten ++
encodeWords (GraphTables.rowWords (actualRow t padding tables x (.inr ()))) = _
simp_rw [actualRow_bits]
unfold PreprocessingPaddingWords.vertexBits
rw [List.ofFn_succ_last, List.flatten_append]
simp only [List.flatten_cons, List.flatten_nil, List.append_nil]
rfl
/-- The explicit padding list is the inverse enumeration of its numeric order. -/
theorem paddingOrder_ofFn :
List.ofFn (paddingOrder padding).symm = paddingList padding := by
calc
List.ofFn (paddingOrder padding).symm =
List.ofFn (fun i : Fin (paddingList padding).length =>
(paddingOrder padding).symm (Fin.cast (paddingList_length padding) i)) :=
List.ofFn_congr (paddingList_length padding).symm (paddingOrder padding).symm
_ = paddingList padding := by
change List.ofFn (fun i => (paddingList padding).get i) = _
exact List.ofFn_get _
/-- Any global-vertex payload follows old darts, then the explicit dummy list. -/
theorem vertexOrder_ofFn {α : Type*} (f : Vertex t padding → α) :
List.ofFn (fun i : Fin (vertexCount t padding) => f ((vertexOrder t padding).symm i)) =
List.ofFn (fun e : Fin t.darts => f (.inl e)) ++
(paddingList padding).map (fun z => f (.inr z)) := by
change List.ofFn (fun i : Fin (t.darts + ∑ v, padding v) =>
f ((vertexOrder t padding).symm i)) = _
rw [List.ofFn_add]
have hold (e : Fin t.darts) :
(vertexOrder t padding).symm (e.castLE (Nat.le_add_right t.darts (∑ v, padding v))) =
Sum.inl e := (vertexOrder t padding).symm_apply_apply (Sum.inl e)
have hnew (i : Fin (∑ v, padding v)) :
(vertexOrder t padding).symm (i.natAdd t.darts) =
Sum.inr ((paddingOrder padding).symm i) := by
apply (vertexOrder t padding).injective
erw [Equiv.apply_symm_apply]
change i.natAdd t.darts = Fin.natAdd t.darts
((paddingOrder padding) ((paddingOrder padding).symm i))
rw [Equiv.apply_symm_apply]
have oldPart := congrArg List.ofFn
(funext (fun e => congrArg f (hold e)))
have newPart := congrArg List.ofFn
(funext (fun i => congrArg f (hnew i)))
erw [oldPart, newPart]
congr 1
calc
List.ofFn (fun i => f (.inr ((paddingOrder padding).symm i))) =
(List.ofFn (paddingOrder padding).symm).map (fun z => f (.inr z)) :=
(List.map_ofFn (f := (paddingOrder padding).symm) (g := fun z => f (.inr z))).symm
_ = _ := by rw [paddingOrder_ofFn]
/-- The stored dummy list uses exactly the nested owner/local-index loop order. -/
theorem paddingList_flatMap {α : Type*} (f : (Σ v : Fin t.vertices, Fin (padding v)) → List α) :
(paddingList padding).flatMap f =
(List.ofFn (fun v : Fin t.vertices =>
(List.ofFn (fun j : Fin (padding v) => f ⟨v, j⟩)).flatten)).flatten := by
simp only [paddingList, List.sigma, List.finRange, List.flatMap_def,
List.map_flatten, List.flatten_flatten, List.map_ofFn, Function.comp_def]
/-- The whole actual row stream, split into the two executable outer loops. -/
theorem rowsBits_ofCloudTables :
PreprocessingPaddingWords.rowsBits (ofCloudTables t padding tables) =
(List.ofFn (originalVertexBits t padding tables)).flatten ++
(List.ofFn (fun v : Fin t.vertices =>
(List.ofFn (dummyVertexBits t padding tables v)).flatten)).flatten := by
rw [PreprocessingPaddingWords.rowsBits_eq_vertices]
have hblocks :
List.ofFn (fun i : Fin (vertexCount t padding) =>
PreprocessingPaddingWords.vertexBits (ofCloudTables t padding tables) i) =
List.ofFn (fun i : Fin (vertexCount t padding) =>
vertexBits t padding tables ((vertexOrder t padding).symm i)) := by
apply congrArg List.ofFn
funext i
rw [vertexBits_eq, Equiv.apply_symm_apply]
rw [hblocks, vertexOrder_ofFn, List.flatten_append]
change (List.ofFn (originalVertexBits t padding tables)).flatten ++
((paddingList padding).map (fun z => dummyVertexBits t padding tables z.1 z.2)).flatten = _
rw [← List.flatMap_def, paddingList_flatMap]
/-- Exact complete codec identity: new headers, original blocks, then dummy blocks. -/
theorem tableBits_ofCloudTables :
PortTables.tableBits (ofCloudTables t padding tables) =
encodeWords [vertexCount t padding, vertexCount t padding * (q + 1)] ++
(List.ofFn (originalVertexBits t padding tables)).flatten ++
(List.ofFn (fun v : Fin t.vertices =>
(List.ofFn (dummyVertexBits t padding tables v)).flatten)).flatten := by
change encodeWords (PortTables.tableWords (ofCloudTables t padding tables)) = _
rw [PortTables.tableWords_eq, encodeWords_append]
change encodeWords [vertexCount t padding, vertexCount t padding * (q + 1)] ++
PreprocessingPaddingWords.rowsBits (ofCloudTables t padding tables) = _
rw [rowsBits_ofCloudTables, List.append_assoc]
end MaxCutGames.Foundations.PCP.PreprocessingRegularWords
end OAI

Xet Storage Details

Size:
21.1 kB
·
Xet hash:
4857843bed4045e7c1670d1472fe5ecaa6cd1084bdab477cd820edbcf8af198a

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.