File size: 8,456 Bytes
92416a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import Mathlib.Data.List.Basic
import OAI.Computability.UniqueGames.Reduction.CanonicalBodyTemplateLemmas
import OAI.Computability.UniqueGames.Reduction.SourceIncidenceLemmas

namespace OAI

section

namespace UniqueGamesTheorem.Reduction.CloneTable

open CloneGap ActualSource

variable {n : Nat}

/-- A total bounded-name helper. Every index used by the table is below 48,
    and `cloneName_value` removes the remainder operation on those indices. -/
def cloneName (v : Fin n) (i : Nat) : Fin (n * 48) :=
  ⟨i % 48 + 48 * v.val, by
    have hi := Nat.mod_lt i (show 0 < 48 by decide)
    have hv := v.isLt
    omega⟩

theorem cloneName_value (v : Fin n) (i : Nat) (hi : i < 48) :
    (cloneName v i).val = i + 48 * v.val := by
  simp only [cloneName, Nat.mod_eq_of_lt hi]

def cloneEquation (e : Equation (Fin n)) (t : Nat × Nat × Nat) :
    Equation (Fin (n * 48)) :=
  ⟨cloneName e.first t.1, cloneName e.second t.2.1,
    cloneName e.third t.2.2, e.rhs⟩

def clonedEquations (es : List (Equation (Fin n))) : List (Equation (Fin (n * 48))) :=
  es.flatMap fun e => distinctTriples.map (cloneEquation e)

private theorem length_flatMap_map {α β γ : Type*}
    (xs : List α) (ys : List β) (f : α → β → γ) :
    (xs.flatMap fun x => ys.map (f x)).length = xs.length * ys.length := by
  induction xs with
  | nil => simp
  | cons x xs ih =>
      simp only [List.flatMap_cons, List.length_append, List.length_map, List.length_cons]
      rw [ih]
      simp [Nat.add_mul, Nat.add_comm]

theorem clonedEquations_length (es : List (Equation (Fin n))) :
    (clonedEquations es).length = es.length * distinctTriples.length := by
  exact length_flatMap_map es distinctTriples cloneEquation

theorem clonedEquations_nonempty (es : List (Equation (Fin n))) (hne : es ≠ []) :
    clonedEquations es ≠ [] := by
  apply List.length_pos_iff.mp
  rw [clonedEquations_length]
  exact Nat.mul_pos (List.length_pos_iff.mpr hne) FiniteSource.distinctTriples_nonempty

/-- Exact equality of typed ordered lists, not just permutation or equal counts. -/
theorem cloned_sourceList (S : Source) :
    (FiniteSource.cloned S).sourceList = clonedEquations S.sourceList := by
  let encode : Equation (Fin S.variables × Nat) → Equation (Fin (S.variables * 48)) :=
    fun e => FiniteSource.mapEquation (fun z => cloneName z.1 z.2) e
  have hmap : (FiniteSource.cloned S).sourceList = (cloneList S.sourceList).map encode := by
    have hm := congrArg (List.map encode)
      (List.ofFn_getElem (xs := cloneList S.sourceList))
    change List.ofFn (fun i : Fin (cloneList S.sourceList).length =>
      encode (cloneList S.sourceList)[i.val]) = _
    simpa only [List.map_ofFn, Function.comp_def] using hm
  rw [hmap]
  simp only [cloneList, clonedEquations, List.map_flatMap, List.map_map, Function.comp_def]
  rfl

theorem cloned_ofList_sourceList (es : List (Equation (Fin n))) (hne : es ≠ []) :
    (FiniteSource.cloned (Source.ofList es hne)).sourceList = clonedEquations es := by
  rw [cloned_sourceList, Source.sourceList_ofList]
  rfl

theorem source_reconstructed (S : Source) :
    Source.ofList S.sourceList S.sourceList_nonempty = S := by
  cases S with
  | mk n m hm eqn =>
      simp only [Source.ofList, Source.sourceList]
      erw [Source.mk.injEq]
      refine ⟨rfl, List.length_ofFn, ?_⟩
      apply (Fin.heq_fun_iff List.length_ofFn).mpr
      intro i
      simp only [List.get_eq_getElem, List.getElem_ofFn]

private theorem ofList_congr {es fs : List (Equation (Fin n))}
    (he : es = fs) (hne : es ≠ []) (hnf : fs ≠ []) :
    Source.ofList es hne = Source.ofList fs hnf := by
  cases he
  rfl

def eraseEquation (e : Equation (Fin n)) : Equation Nat :=
  ⟨e.first.val, e.second.val, e.third.val, e.rhs⟩

/-- Numeric machine target: no remainder, bounded-name proofs, or function
    lookup is needed in the three generated variable names. -/
def natCloneEquation (e : Equation Nat) (t : Nat × Nat × Nat) : Equation Nat :=
  ⟨t.1 + 48 * e.first, t.2.1 + 48 * e.second, t.2.2 + 48 * e.third, e.rhs⟩

def natClonedEquations (es : List (Equation Nat)) : List (Equation Nat) :=
  es.flatMap fun e => distinctTriples.map (natCloneEquation e)

theorem erase_cloneEquation (e : Equation (Fin n)) (t : Nat × Nat × Nat)
    (ht : t ∈ distinctTriples) :
    eraseEquation (cloneEquation e t) = natCloneEquation (eraseEquation e) t := by
  obtain ⟨h₁, h₂, h₃⟩ := FiniteSource.distinctTriple_bounds ht
  simp only [eraseEquation, cloneEquation, natCloneEquation, cloneName,
    Nat.mod_eq_of_lt h₁, Nat.mod_eq_of_lt h₂, Nat.mod_eq_of_lt h₃]

theorem erase_clonedEquations (es : List (Equation (Fin n))) :
    (clonedEquations es).map eraseEquation = natClonedEquations (es.map eraseEquation) := by
  simp only [clonedEquations, natClonedEquations, List.map_flatMap, List.map_map,
    List.flatMap_map, Function.comp_def]
  apply List.flatMap_congr
  intro e _
  apply List.map_congr_left
  intro t ht
  exact erase_cloneEquation e t ht

def cloneEquationWords (e : Equation (Fin n)) (t : Nat × Nat × Nat) : List Nat :=
  [t.1 + 48 * e.first.val, t.2.1 + 48 * e.second.val,
    t.2.2 + 48 * e.third.val, if e.rhs then 1 else 0]

def clonedBodyWords (es : List (Equation (Fin n))) : List Nat :=
  es.flatMap fun e => distinctTriples.flatMap (cloneEquationWords e)

theorem cloneEquation_words (e : Equation (Fin n)) (t : Nat × Nat × Nat)
    (ht : t ∈ distinctTriples) :
    SourceEncoding.equationWords (cloneEquation e t) = cloneEquationWords e t := by
  obtain ⟨h₁, h₂, h₃⟩ := FiniteSource.distinctTriple_bounds ht
  simp only [SourceEncoding.equationWords, cloneEquationWords, cloneEquation, cloneName,
    Nat.mod_eq_of_lt h₁, Nat.mod_eq_of_lt h₂, Nat.mod_eq_of_lt h₃]

theorem clonedEquations_words (es : List (Equation (Fin n))) :
    (clonedEquations es).flatMap SourceEncoding.equationWords = clonedBodyWords es := by
  simp only [clonedEquations, clonedBodyWords, List.flatMap_assoc, List.flatMap_map]
  apply List.flatMap_congr
  intro e _
  apply List.flatMap_congr
  intro t ht
  exact cloneEquation_words e t ht

def clonedInput (input : SourceEncoding.Input) : SourceEncoding.Input where
  «variables» := input.variables * 48
  equations := clonedEquations input.equations
  nonempty := clonedEquations_nonempty input.equations input.nonempty

def clonedInputWords (input : SourceEncoding.Input) : List Nat :=
  [input.variables * 48, input.equations.length * distinctTriples.length] ++
    clonedBodyWords input.equations

theorem clonedInput_equations (input : SourceEncoding.Input) :
    (clonedInput input).equations =
      (FiniteSource.cloned (Source.ofList input.equations input.nonempty)).sourceList := by
  exact (cloned_ofList_sourceList input.equations input.nonempty).symm

theorem clonedInput_source (input : SourceEncoding.Input) :
    Source.ofList (clonedInput input).equations (clonedInput input).nonempty =
      FiniteSource.cloned (Source.ofList input.equations input.nonempty) := by
  have hs := source_reconstructed
    (FiniteSource.cloned (Source.ofList input.equations input.nonempty))
  have he := clonedInput_equations input
  exact (ofList_congr he _ _).trans hs

theorem clonedInput_words (input : SourceEncoding.Input) :
    SourceEncoding.inputWords (clonedInput input) = clonedInputWords input := by
  simp only [SourceEncoding.inputWords, clonedInput, clonedInputWords,
    clonedEquations_length, clonedEquations_words]

theorem clonedInput_decodes (input : SourceEncoding.Input) :
    SourceEncoding.decodeInputWords (clonedInputWords input) = some (clonedInput input) := by
  rw [← clonedInput_words]
  exact SourceEncoding.decodeInputWords_encoded _

/-- A size bound for the actual serialized intermediate table. This is not
    a machine-time bound. -/
theorem clonedInput_bits_length_le (input : SourceEncoding.Input) :
    (SourceEncoding.inputBits (clonedInput input)).length ≤
      48 * input.variables + distinctTriples.length * input.equations.length + 2 +
        distinctTriples.length * input.equations.length * (144 * input.variables + 2) := by
  calc
    _ ≤ (clonedInput input).variables + (clonedInput input).equations.length + 2 +
        (clonedInput input).equations.length * (3 * (clonedInput input).variables + 2) :=
      SourceEncoding.inputBits_length_le _
    _ = _ := by
      simp only [clonedInput, clonedEquations_length]
      ring

end UniqueGamesTheorem.Reduction.CloneTable

end

end OAI