openAI-math-dataset / lean /OAI /Combinatorics /IndependentSets /Expansion /PreprocessingOverlayWords.lean
roseking's picture
Add lean source and binary files (part 44)
e320e82 verified
Raw History Blame Contribute Delete
8.07 kB
14
abbrev row := @PreprocessingLazyWords.row
15
abbrev trueRelation := PreprocessingLazyWords.trueRelation
17
def oldReverseMap (d e j : Nat) : Nat := (d + e) * (j / d) + j % d
18
def expanderReverseMap (d e j : Nat) : Nat := (d + e) * (j / e) + d + j % e
20
def oldRow (G : PortTables.Table n d) (e : Nat) (v : Fin n) (p : Fin d) :
    DartRow n (n * (d + e)) :=
body · 3 lines
  ⟨v, rowIndex n (d + e) ((rotation G (v, p)).1,
    overlayPorts d e (.inl (rotation G (v, p)).2)),
    G.relations[rowIndex n d (v, p)]⟩
26
def expanderRow (d : Nat) (H : ExpanderTables.Table n e) (v : Fin n) (p : Fin e) :
    DartRow n (n * (d + e)) :=
body · 2 lines
  ⟨v, rowIndex n (d + e) ((ExpanderTables.lookup H (v, p)).1,
    overlayPorts d e (.inr (ExpanderTables.lookup H (v, p)).2)), trueRelation⟩
31
private theorem relation_ext {r s : RelationTable}
    (h : ∀ a b, relationAt r a b = relationAt s a b) : r = s := by
proof · 4 lines
  apply Vector.ext
  intro i hi
  simpa only [relationAt, Prod.eta, Equiv.apply_symm_apply, Fin.getElem_fin] using
    h (relationIndex.symm ⟨i, hi⟩).1 (relationIndex.symm ⟨i, hi⟩).2
38
theorem row_overlay_old (G : PortTables.Table n d) (H : ExpanderTables.Table n e)
    (v : Fin n) (p : Fin d) :
    row (overlay G H) v (overlayPorts d e (.inl p)) = oldRow G e v p := by
proof · 9 lines
  have hr : (overlay G H).reverseIndex[rowIndex n (d + e) (v, overlayPorts d e (.inl p))] =
      rowIndex n (d + e) ((rotation G (v, p)).1,
        overlayPorts d e (.inl (rotation G (v, p)).2)) := by
    rw [← rowIndex_rotation, overlay_rotation_old]
  have hp : (overlay G H).relations[rowIndex n (d + e) (v, overlayPorts d e (.inl p))] =
      G.relations[rowIndex n d (v, p)] := by
    apply relation_ext
    exact overlay_accepts_old G H v p
  exact congrArg₂ (DartRow.mk v) hr hp
51
theorem row_overlay_expander (G : PortTables.Table n d) (H : ExpanderTables.Table n e)
    (v : Fin n) (p : Fin e) :
    row (overlay G H) v (overlayPorts d e (.inr p)) = expanderRow d H v p := by
proof · 12 lines
  have hr : (overlay G H).reverseIndex[rowIndex n (d + e) (v, overlayPorts d e (.inr p))] =
      rowIndex n (d + e) ((ExpanderTables.lookup H (v, p)).1,
        overlayPorts d e (.inr (ExpanderTables.lookup H (v, p)).2)) := by
    rw [← rowIndex_rotation, overlay_rotation_expander]
  have hp : (overlay G H).relations[rowIndex n (d + e) (v, overlayPorts d e (.inr p))] =
      trueRelation := by
    apply relation_ext
    intro a b
    simpa only [PortTables.accepts, relationAt, trueRelation,
      PreprocessingLazyWords.trueRelation, Fin.getElem_fin, Vector.getElem_replicate] using
      overlay_accepts_expander G H v p a b
  exact congrArg₂ (DartRow.mk v) hr hp
67
theorem oldRow_reverse_val (G : PortTables.Table n d) (v : Fin n) (p : Fin d) :
    (oldRow G e v p).reverseIndex.val = oldReverseMap d e (row G v p).reverseIndex.val := by
proof · 5 lines
  simp only [oldRow, rowIndex_val, overlayPorts_inl_val, oldReverseMap, row,
    PreprocessingLazyWords.row]
  change (G.reverseIndex[rowIndex n d (v, p)]).val % d +
      (d + e) * ((G.reverseIndex[rowIndex n d (v, p)]).val / d) = _
  omega
75
theorem expanderRow_reverse_val (H : ExpanderTables.Table n e) (v : Fin n) (p : Fin e) :
    (expanderRow d H v p).reverseIndex.val =
      expanderReverseMap d e (ExpanderTables.reverseIndex H (ExpanderTables.rowIndex n e (v, p))).val := by
proof · 4 lines
  simp only [expanderRow, rowIndex_val, overlayPorts_inr_val, expanderReverseMap]
  change d + (ExpanderTables.reverseIndex H (ExpanderTables.rowIndex n e (v, p))).val % e +
      (d + e) * ((ExpanderTables.reverseIndex H (ExpanderTables.rowIndex n e (v, p))).val / e) = _
  omega
83
theorem oldRow_words (G : PortTables.Table n d) (v : Fin n) (p : Fin d) :
    rowWords (oldRow G e v p) =
      [v.val, oldReverseMap d e (row G v p).reverseIndex.val] ++
        relationWords (row G v p).relation := by
proof · 2 lines
  rw [rowWords, oldRow_reverse_val]
  rfl
90
theorem expanderRow_words (H : ExpanderTables.Table n e) (v : Fin n) (p : Fin e) :
    rowWords (expanderRow d H v p) =
      [v.val, expanderReverseMap d e
        (ExpanderTables.reverseIndex H (ExpanderTables.rowIndex n e (v, p))).val] ++
        List.replicate 4096 1 := by
proof · 4 lines
  rw [rowWords, expanderRow_reverse_val]
  exact congrArg (fun rs => [v.val, expanderReverseMap d e
    (ExpanderTables.reverseIndex H (ExpanderTables.rowIndex n e (v, p))).val] ++ rs)
      PreprocessingLazyWords.relationWords_true
100
theorem ofFn_overlayPorts {α : Type*} (f : Fin (d + e) → α) :
    List.ofFn f = List.ofFn (fun p => f (overlayPorts d e (.inl p))) ++
      List.ofFn (fun p => f (overlayPorts d e (.inr p))) := by
proof · 2 lines
  rw [List.ofFn_add]
  rfl
106
def vertexRows (G : PortTables.Table n d) (H : ExpanderTables.Table n e) (v : Fin n) :
    List (DartRow n (n * (d + e))) :=
body · 1 line
  List.ofFn (oldRow G e v) ++ List.ofFn (expanderRow d H v)
110
theorem vertexRows_length (G : PortTables.Table n d) (H : ExpanderTables.Table n e) (v : Fin n) :
    (vertexRows G H v).length = d + e := by
proof · 1 line
  simp only [vertexRows, List.length_append, List.length_ofFn]
114
theorem flatRows_overlay (G : PortTables.Table n d) (H : ExpanderTables.Table n e) :
    (flatRows (overlay G H)).toList = (List.ofFn (vertexRows G H)).flatten := by
proof · 7 lines
  rw [PreprocessingLazyWords.flatRows_list]
  apply congrArg List.flatten
  congr 1
  funext v
  rw [ofFn_overlayPorts]
  simp only [row_overlay_old, row_overlay_expander]
  rfl
124
theorem vertexRows_words (G : PortTables.Table n d) (H : ExpanderTables.Table n e) (v : Fin n) :
    (vertexRows G H v).flatMap rowWords =
      (List.ofFn fun p : Fin d =>
        [v.val, oldReverseMap d e (row G v p).reverseIndex.val] ++
          relationWords (row G v p).relation).flatten ++
      (List.ofFn fun p : Fin e =>
        [v.val, expanderReverseMap d e
          (ExpanderTables.reverseIndex H (ExpanderTables.rowIndex n e (v, p))).val] ++
          List.replicate 4096 1).flatten := by
proof · 13 lines
  have ho : rowWords ∘ oldRow G e v = fun p : Fin d =>
      [v.val, oldReverseMap d e (row G v p).reverseIndex.val] ++
        relationWords (row G v p).relation := by
    funext p
    exact oldRow_words G v p
  have he : rowWords ∘ expanderRow d H v = fun p : Fin e =>
      [v.val, expanderReverseMap d e
        (ExpanderTables.reverseIndex H (ExpanderTables.rowIndex n e (v, p))).val] ++
        List.replicate 4096 1 := by
    funext p
    exact expanderRow_words H v p
  simp only [vertexRows, List.flatMap_def, List.map_append, List.map_ofFn, List.flatten_append]
  rw [ho, he]
147
theorem tableWords_overlay (G : PortTables.Table n d) (H : ExpanderTables.Table n e) :
    PortTables.tableWords (overlay G H) =
      [n, n * (d + e)] ++ (List.ofFn (vertexRows G H)).flatten.flatMap rowWords := by
proof · 1 line
  rw [PortTables.tableWords_eq, flatRows_overlay]
152
theorem tableBits_overlay (G : PortTables.Table n d) (H : ExpanderTables.Table n e) :
    PortTables.tableBits (overlay G H) = encodeWords
      ([n, n * (d + e)] ++ (List.ofFn (vertexRows G H)).flatten.flatMap rowWords) := by
proof · 2 lines
  change encodeWords (PortTables.tableWords (overlay G H)) = _
  rw [tableWords_overlay]
158
theorem rotationWords_vertices (H : ExpanderTables.Table n e) :
    ExpanderTableWords.rotationWords H =
      (List.ofFn fun v : Fin n => List.ofFn fun p : Fin e =>
        (ExpanderTables.reverseIndex H (ExpanderTables.rowIndex n e (v, p))).val).flatten := by
proof · 9 lines
  have hflat : ExpanderTableWords.rotationWords H =
      List.ofFn (fun i : Fin (n * e) => (ExpanderTables.reverseIndex H i).val) := by
    apply List.ext_getElem
    · simp [ExpanderTableWords.rotationWords_length]
    · intro i hi hj
      rw [ExpanderTableWords.rotationWords_getElem H i (by simpa using hi)]
      simp only [List.getElem_ofFn]
  rw [hflat, PreprocessingLazyWords.ofFn_rowIndex]
  rfl