File size: 11,212 Bytes
2f887db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import OAI.Computability.PerfectCompleteness.Construction.FlexibleLocalCompletionLemmas
import OAI.Computability.PerfectCompleteness.Foundations.LegalOutputLemmas
import OAI.Computability.PerfectCompleteness.Machines.CanonicalLabelEncoding
import OAI.Computability.PerfectCompleteness.Machines.LocalCompletionMachineLemmas

namespace OAI

section

namespace PerfectCompleteness.CanonicalLocalCompletion

open scoped Classical
open Turing UniqueGamesTheorem.Foundations.Complexity UniqueGamesTheorem.Foundations.Games
open SourceClause CanonicalKeys CanonicalGame CanonicalKeyEncoding CanonicalLabelEncoding
open CompletionSoundness CompletionSoundness.LegalProjectionGame

noncomputable section

def alphabet (n : Nat) (δ : ℚ) : Nat := CompletionAlphabet.size (partitionWidth n) δ

theorem alphabet_positive (n : Nat) (δ : ℚ) : 0 < alphabet n δ :=
  CompletionAlphabet.size_positive (partitionWidth n) δ

theorem partitionWidth_le_alphabet (n : Nat) (δ : ℚ) : partitionWidth n ≤ alphabet n δ :=
  CompletionAlphabet.cap_le_size (partitionWidth n) δ

theorem partitionWidth_le_leftAlphabet (n : Nat) (δ : ℚ) :
    partitionWidth n ≤ 2 * alphabet n δ := by
  have h := partitionWidth_le_alphabet n δ
  omega

theorem alphabet_ge_two (n : Nat) (δ : ℚ) : 2 ≤ alphabet n δ := by
  have hpos : 0 < partitionWidth n := by
    rw [partitionWidth_eq]
    exact pow_pos (by decide) _
  have hcap : partitionWidth n + 1 ≤ alphabet n δ := le_max_left _ _
  omega

local instance rightAlphabet_nonempty (n : Nat) (δ : ℚ) : Nonempty (Fin (alphabet n δ)) :=
  ⟨⟨0, alphabet_positive n δ⟩⟩

local instance leftAlphabet_nonempty (n : Nat) (δ : ℚ) : Nonempty (Fin (2 * alphabet n δ)) :=
  ⟨⟨0, Nat.mul_pos (by decide) (alphabet_positive n δ)⟩⟩

variable {v m n : Nat} {E I B : Type*} {V : I → Type*}
  [DecidableEq I] [∀ i, AddCommGroup (V i)]
  [∀ i, Module DirectionQuotient.F2 (V i)]
  [Finite I] [∀ i, Finite (V i)] [Finite B]
  {clauses : Fin m → NormalizedClause v}
  (F : BlockFamily clauses n E I V B)

def legalL (δ : ℚ) (x : LeftVertex F) : LeftLabel F x ↪ Fin (2 * alphabet n δ) :=
  labelEmbedding x.val (partitionWidth_le_leftAlphabet n δ)

def legalR (δ : ℚ) (y : RightVertex F) : RightLabel F y ↪ Fin (alphabet n δ) :=
  labelEmbedding y.val (partitionWidth_le_alphabet n δ)

omit [DecidableEq I] [Finite I] [∀ index, Finite (V index)] [Finite B] in
@[simp] theorem legalL_val (δ : ℚ) (x : LeftVertex F) (label : LeftLabel F x) :
    (legalL F δ x label).val = (partEnum n label.val).val := rfl

omit [Finite I] [∀ index, Finite (V index)] [Finite B] in
@[simp] theorem legalR_val (δ : ℚ) (y : RightVertex F) (label : RightLabel F y) :
    (legalR F δ y label).val = (partEnum n label.val).val := rfl

def defaultL (x : LeftVertex F) : LeftLabel F x := Classical.choice inferInstance

variable [Fintype E] [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ x, Fintype (LeftLabel F x)] [∀ y, Fintype (RightLabel F y)]

omit [DecidableEq I] [Finite I] [∀ index, Finite (V index)] [Finite B] [Fintype E]
  [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
theorem leftLabel_card_le (δ : ℚ) (x : LeftVertex F) :
    Fintype.card (LeftLabel F x) ≤ alphabet n δ := by
  let : Fintype (KeyLabel x.val) := inferInstanceAs (Fintype (LeftLabel F x))
  exact (label_card_le x.val).trans (partitionWidth_le_alphabet n δ)

omit [Finite I] [∀ index, Finite (V index)] [Finite B] [Fintype E]
  [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (LeftLabel F vertex)] in
theorem rightLabel_card_le (δ : ℚ) (y : RightVertex F) :
    Fintype.card (RightLabel F y) ≤ alphabet n δ := by
  let : Fintype (KeyLabel y.val) := inferInstanceAs (Fintype (RightLabel F y))
  exact (label_card_le y.val).trans (partitionWidth_le_alphabet n δ)

omit [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
theorem small (μ : FiniteDistribution E) :
    ∀ e b, Fintype.card
      {a : LeftLabel F ((game F μ).left e) // (game F μ).projection e a = b} ≤ 2 := by
  intro e b
  rw [Fintype.card_eq_nat_card]
  change Nat.card {a : LeftLabel F (leftAt F e) // edge F e a = b} ≤ 2
  exact edge_at_most_two F e b

def input (μ : FiniteDistribution E) (δ : ℚ) (e : E) :
    LocalCompletionFamily.Input (alphabet n δ) :=
  LocalCompletionGame.input (game F μ) (legalL F δ) (legalR F δ) (defaultL F) e

omit [Finite I] [∀ index, Finite (V index)] [Finite B]
  [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
theorem input_mask (μ : FiniteDistribution E) (δ : ℚ) (e : E) :
    (input F μ δ e).1 = paddedMask (leftKey F e) := by
  funext a
  apply Bool.eq_iff_iff.mpr
  exact (LocalCompletionGame.input_mask_true
    (game F μ) (legalL F δ) (legalR F δ) (defaultL F) e a).trans
    (paddedMask_eq_true_iff (leftKey F e) (partitionWidth_le_leftAlphabet n δ) a).symm

omit [Finite I] [∀ index, Finite (V index)] [Finite B]
  [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
theorem input_image_legal (μ : FiniteDistribution E) (δ : ℚ) (e : E)
    (label : LeftLabel F (leftAt F e)) :
    (input F μ δ e).2 (legalL F δ (leftAt F e) label) =
      legalR F δ (rightAt F e) (edge F e label) :=
  LocalCompletionGame.input_image_legal
    (game F μ) (legalL F δ) (legalR F δ) (defaultL F) e label

omit [Finite I] [∀ index, Finite (V index)] [Finite B]
  [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
theorem input_independent_of_law (μ ν : FiniteDistribution E) (δ : ℚ) (e : E) :
    input F μ δ e = input F ν δ e := rfl

omit [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
theorem input_admissible (μ : FiniteDistribution E) (δ : ℚ) (e : E) :
    LocalCompletionFamily.Admissible (input F μ δ e) :=
  LocalCompletionGame.input_admissible
    (game F μ) (legalL F δ) (legalR F δ) (defaultL F) (by
      intro occurrence label
      rw [Fintype.card_eq_nat_card]
      have h := small F μ occurrence label
      rw [Fintype.card_eq_nat_card] at h
      exact h) e

def result (μ : FiniteDistribution E) (δ : ℚ) (e : E) :
    LocalCompletionFamily.Result (alphabet n δ) :=
  LocalCompletionFamily.lookup (alphabet_positive n δ) (input F μ δ e)

omit [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
theorem result_correct (μ : FiniteDistribution E) (δ : ℚ) (e : E) :
    LocalCompletionFamily.Correct (input F μ δ e) (result F μ δ e) :=
  LocalCompletionFamily.lookup_correct (alphabet_positive n δ) _ (input_admissible F μ δ e)

def family (μ : FiniteDistribution E) (δ : ℚ) :
    (game F μ).CompletionFamily (legalL F δ) (legalR F δ) :=
  LocalCompletionGame.family (game F μ) (legalL F δ) (legalR F δ) (defaultL F)
    (alphabet_positive n δ) (by
      intro occurrence label
      rw [Fintype.card_eq_nat_card]
      have h := small F μ occurrence label
      rw [Fintype.card_eq_nat_card] at h
      exact h)

omit [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
@[simp] theorem family_size (μ : FiniteDistribution E) (δ : ℚ) (e : E) :
    (family F μ δ).size e = LocalCompletionFamily.activeCount (result F μ δ e) := rfl

omit [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
@[simp] theorem family_map (μ : FiniteDistribution E) (δ : ℚ) (e : E) :
    (family F μ δ).map e = LocalCompletionFamily.table (result F μ δ e) := rfl

omit [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
theorem family_size_le (μ : FiniteDistribution E) (δ : ℚ) (e : E) :
    (family F μ δ).size e ≤ 2 * alphabet n δ :=
  LocalCompletionFamily.activeCount_le (result F μ δ e)

def machine_output (μ : FiniteDistribution E) (δ : ℚ) (e : E) :
    TM2OutputsInTime
      (LocalCompletionSerialization.computation (alphabet_positive n δ)).tm
      (LocalCompletionMachine.inputBits (input F μ δ e))
      (some (LocalCompletionSerialization.payloadBits (result F μ δ e))) 1 :=
  LocalCompletionSerialization.machine_output (alphabet_positive n δ) (input F μ δ e)

omit [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
theorem payload_parses (μ : FiniteDistribution E) (δ : ℚ) (e : E) :
    decodeWords (LocalCompletionSerialization.payloadBits (result F μ δ e)) >>=
        LocalCompletionSerialization.parsePayload (alphabet n δ) =
      some ((List.finRange (LocalCompletionFamily.activeCount (result F μ δ e))).map
        (LocalCompletionSerialization.projection (result_correct F μ δ e))) := by
  rw [LocalCompletionSerialization.decode_payloadBits]
  exact LocalCompletionSerialization.parse_payload (result_correct F μ δ e)

omit [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
theorem parsed_projection_eq_family_map (μ : FiniteDistribution E) (δ : ℚ) (e : E)
    (seed : Fin ((family F μ δ).size e)) (a : Fin (2 * alphabet n δ)) :
    (LocalCompletionSerialization.projection (result_correct F μ δ e) seed).images[a] =
      (family F μ δ).map e seed a :=
  LocalCompletionSerialization.projection_apply (result_correct F μ δ e) seed a

omit [Finite I] [∀ index, Finite (V index)] [Finite B]
  [Fintype (LeftVertex F)] [Fintype (RightVertex F)]
  [∀ vertex, Fintype (RightLabel F vertex)] in
theorem payload_length_le (μ : FiniteDistribution E) (δ : ℚ) (e : E) :
    (LocalCompletionSerialization.payloadBits (result F μ δ e)).length ≤
      (1 + (2 * alphabet n δ) * (2 * alphabet n δ)) * (2 * alphabet n δ + 1) :=
  LocalCompletionSerialization.payloadBits_length_le _

theorem family_value_le (μ : FiniteDistribution E) (δ : ℚ) (hδ : 0 < δ) :
    (family F μ δ).game.value ≤ (game F μ).value + (δ : ℝ) / 3 := by
  apply (family F μ δ).value_le_of_bound ((δ : ℝ) / 3)
  intro e
  have hδreal : (0 : ℝ) < (δ : ℝ) := by exact_mod_cast hδ
  have h := CompletionParameters.residual_le_third (alphabet_positive n δ)
    (leftLabel_card_le F δ (leftAt F e)) hδreal
    (CompletionAlphabet.size_budget (partitionWidth n) δ hδ)
  simp only [Fintype.card_fin]
  rw [Fintype.card_eq_nat_card]
  rw [Fintype.card_eq_nat_card] at h
  simpa only [CanonicalGame.game] using h

theorem family_value_of_satisfying (μ : FiniteDistribution E) (δ : ℚ)
    (assignment : Fin v → Bool) (hsat : ∀ c, (clauses c).clause.eval assignment = true) :
    (family F μ δ).game.value = 1 := by
  let : Nonempty E := by
    by_contra hempty
    have : IsEmpty E := ⟨fun e => hempty ⟨e⟩⟩
    have hnormalized := μ.normalized
    simp at hnormalized
  apply (family F μ δ).value_eq_one
  exact CanonicalOutput.game_value_of_satisfying F μ assignment hsat

end
end PerfectCompleteness.CanonicalLocalCompletion

end

end OAI