File size: 11,579 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import Mathlib.Data.List.Basic
import OAI.Computability.PerfectCompleteness.Foundations.CanonicalEdgesLemmas
import OAI.Computability.PerfectCompleteness.Foundations.CanonicalVertexNames
import OAI.Computability.PerfectCompleteness.Machines.FiniteBlockMachineLemmas

namespace OAI


namespace PerfectCompleteness.CanonicalKeyShapeMachine

open Turing
open UniqueGamesTheorem.Foundations.Complexity
open CanonicalKeyShape CanonicalKeyEncoding CanonicalKeys

noncomputable section

def inputEnum (n k : Nat) : Input n k ≃ Fin (Fintype.card (Input n k)) :=
  Fintype.equivFin _

def inputBits {n k : Nat} (data : Input n k) : List Bool :=
  FiniteBlockMachine.word (inputEnum n k) data

def defaultInput (n : Nat) {k : Nat} (hk : 0 < k) : Input n k :=
  ⟨fun _ => Shape.bit, fun _ => ⟨0, hk⟩⟩

def modeEnum : SupportMode ≃ Fin (Fintype.card SupportMode) := Fintype.equivFin _

def modeWords {n : Nat} (support : Fin n → SupportMode) : List Nat :=
  List.ofFn (fun i => (modeEnum (support i)).val)

@[simp] theorem modeWords_length {n : Nat} (support : Fin n → SupportMode) :
    (modeWords support).length = n := List.length_ofFn

theorem modeWords_injective (n : Nat) : Function.Injective (modeWords (n := n)) := by
  intro a b h
  have hfun := List.ofFn_injective h
  funext i
  apply modeEnum.injective
  exact Fin.ext (congrFun hfun i)

def outputWords {n k : Nat} (data : Input n k) : List Nat :=
  modeWords (inputModes data) ++ partitionWords (inputPartition data)

def outputBits {n k : Nat} (data : Input n k) : List Bool := encodeWords (outputWords data)

@[simp] theorem outputWords_length {n k : Nat} (data : Input n k) :
    (outputWords data).length = n + partitionWidth n := by
  simp only [outputWords, List.length_append, modeWords_length, partitionWords_length]

theorem outputWords_eq_iff {n k : Nat} (a b : Input n k) :
    outputWords a = outputWords b ↔
      inputModes a = inputModes b ∧ inputPartition a = inputPartition b := by
  constructor
  · intro h
    have hlen : (modeWords (inputModes a)).length = (modeWords (inputModes b)).length := by
      simp only [modeWords_length]
    have hp := List.append_inj h hlen
    exact ⟨modeWords_injective n hp.1, partitionWords_injective n hp.2⟩
  · rintro ⟨hm, hp⟩
    simp only [outputWords, hm, hp]

theorem outputBits_eq_iff {n k : Nat} (a b : Input n k) :
    outputBits a = outputBits b ↔
      inputModes a = inputModes b ∧ inputPartition a = inputPartition b := by
  constructor
  · intro h
    exact (outputWords_eq_iff a b).mp (encodeWords_injective h)
  · intro h
    exact congrArg encodeWords ((outputWords_eq_iff a b).mpr h)

def computation (n : Nat) {k : Nat} (hk : 0 < k) :
    TM2ComputableInPolyTime (inputBits (n := n) (k := k)) id outputBits :=
  FiniteWordMachine.computation (inputEnum n k) (defaultInput n hk) outputBits

def machine_output {n k : Nat} (hk : 0 < k) (data : Input n k) :
    TM2OutputsInTime (computation n hk).tm (inputBits data) (some (outputBits data)) 1 := by
  have h := (computation n hk).outputsFun data
  change TM2OutputsInTime (computation n hk).tm ((inputBits data).map id)
    (some ((outputBits data).map id)) ((1 : Polynomial Nat).eval _) at h
  rw [Polynomial.eval_one] at h
  exact Eq.mp (congrArg₂
    (fun (i o : List Bool) => TM2OutputsInTime (computation n hk).tm i (some o) 1)
    (List.map_id (inputBits data)) (List.map_id (outputBits data))) h

def recoverKey {n k : Nat} (side : Side) (slots : Fin n → MixedSupport.Slot)
    (data : Input n k) : Key n :=
  ⟨side, CanonicalKeyShape.retained slots (inputModes data), inputPartition data⟩

theorem recoverKey_input {n k : Nat} (side : Side) (slots : Fin n → MixedSupport.Slot)
    (f : MixedSupport.Assignment slots → Fin k) :
    recoverKey side slots (CanonicalKeyShape.input slots f) = CanonicalKeys.key side slots f := by
  simp only [recoverKey, CanonicalKeys.key, inputModes_recover, inputPartition_eq]

theorem recoverKey_of_same_output {n k : Nat} (side : Side)
    (slots : Fin n → MixedSupport.Slot) (a b : Input n k)
    (same : outputBits a = outputBits b) : recoverKey side slots a = recoverKey side slots b := by
  obtain ⟨hm, hp⟩ := (outputBits_eq_iff a b).mp same
  simp only [recoverKey, hm, hp]

theorem workAlphabet_finite (n : Nat) {k : Nat} (hk : 0 < k)
    (tape : (computation n hk).tm.K) : Finite ((computation n hk).tm.Γ tape) := by
  change Finite Bool
  infer_instance

end
end PerfectCompleteness.CanonicalKeyShapeMachine


namespace PerfectCompleteness.CanonicalOnlineNames

open CanonicalKeys

section Lists

variable {α : Type*} [BEq α] [LawfulBEq α]

theorem firstIndex_append_current (earlier suffix : List α) (key : α) :
    (earlier ++ [key]).idxOf key = (earlier ++ key :: suffix).idxOf key := by
  have present : key ∈ earlier ++ [key] := by simp
  have found := List.idxOf_append_of_mem (l₂ := suffix) present
  simpa only [List.append_assoc, List.singleton_append] using found.symm

theorem firstIndex_le_prefix (earlier : List α) (key : α) :
    (earlier ++ [key]).idxOf key ≤ earlier.length := by
  have found := List.idxOf_lt_length_of_mem (by simp : key ∈ earlier ++ [key])
  simp only [List.length_append, List.length_singleton] at found
  omega

theorem firstIndex_eq_prefix (earlier : List α) (key : α) :
    (earlier ++ [key]).idxOf key = earlier.idxOf key := by
  classical
  by_cases present : key ∈ earlier
  · exact List.idxOf_append_of_mem present
  · rw [List.idxOf_append_of_notMem present, List.idxOf_of_notMem present]
    simp

end Lists

noncomputable section

variable {n : Nat}

def onlineID (earlier : List (Key n)) (key : Key n) : Nat :=
  (BinaryNameSearch.payloads (CanonicalVertexNames.tokens (earlier ++ [key]))).idxOf
    (CanonicalVertexNames.payload key)

theorem tokens_append_current (earlier : List (Key n)) (key : Key n) :
    CanonicalVertexNames.tokens (earlier ++ [key]) =
      CanonicalVertexNames.tokens earlier ++ [(false, CanonicalVertexNames.payload key)] := by
  simp only [CanonicalVertexNames.tokens, List.map_append, List.map_cons, List.map_nil]

theorem current_payload_mem (earlier : List (Key n)) (key : Key n) :
    CanonicalVertexNames.payload key ∈
      BinaryNameSearch.payloads (CanonicalVertexNames.tokens (earlier ++ [key])) :=
  CanonicalVertexNames.payload_mem _ _ (by simp)

theorem dictionary_canonical (earlier : List (Key n)) (key : Key n) :
    ∀ token ∈ CanonicalVertexNames.tokens (earlier ++ [key]),
      BinaryNameMachine.canonical token.2 = true :=
  CanonicalVertexNames.tokens_canonical _

theorem onlineID_le_prefix (earlier : List (Key n)) (key : Key n) :
    onlineID earlier key ≤ earlier.length := by
  have bound := firstIndex_le_prefix (earlier.map CanonicalVertexNames.payload)
    (CanonicalVertexNames.payload key)
  simpa only [onlineID, CanonicalVertexNames.tokens_payloads, List.map_append,
    List.map_cons, List.map_nil, List.length_map] using bound

theorem onlineID_eq_global (earlier suffix : List (Key n)) (key : Key n) :
    onlineID earlier key =
      (BinaryNameSearch.payloads
        (CanonicalVertexNames.tokens (earlier ++ key :: suffix))).idxOf
          (CanonicalVertexNames.payload key) := by
  simpa only [onlineID, CanonicalVertexNames.tokens_payloads, List.map_append,
    List.map_cons, List.map_nil] using
      firstIndex_append_current (earlier.map CanonicalVertexNames.payload)
        (suffix.map CanonicalVertexNames.payload) (CanonicalVertexNames.payload key)

theorem onlineID_eq_name_val (earlier suffix : List (Key n)) (key : Key n) :
    onlineID earlier key =
      (CanonicalVertexNames.name (earlier ++ key :: suffix) key (by simp)).val :=
  onlineID_eq_global earlier suffix key

theorem onlineID_eq_prefix_index (earlier : List (Key n)) (key : Key n) :
    onlineID earlier key =
      (BinaryNameSearch.payloads (CanonicalVertexNames.tokens earlier)).idxOf
        (CanonicalVertexNames.payload key) := by
  simpa only [onlineID, CanonicalVertexNames.tokens_payloads, List.map_append,
    List.map_cons, List.map_nil] using
      firstIndex_eq_prefix (earlier.map CanonicalVertexNames.payload)
        (CanonicalVertexNames.payload key)

theorem onlineID_repeat (earlier middle : List (Key n)) (key : Key n) :
    onlineID (earlier ++ key :: middle) key = onlineID earlier key := by
  calc
    _ = (BinaryNameSearch.payloads
        (CanonicalVertexNames.tokens (earlier ++ key :: middle))).idxOf
          (CanonicalVertexNames.payload key) := onlineID_eq_prefix_index _ key
    _ = _ := (onlineID_eq_global earlier middle key).symm

theorem onlineID_eq_iff_same_final (keys prefixA suffixA prefixB suffixB : List (Key n))
    (a b : Key n)
    (first : keys = prefixA ++ a :: suffixA)
    (second : keys = prefixB ++ b :: suffixB) :
    onlineID prefixA a = onlineID prefixB b ↔ a = b := by
  have presentA : a ∈ keys := by rw [first]; simp
  rw [onlineID_eq_global prefixA suffixA a, onlineID_eq_global prefixB suffixB b,
    ← first, ← second]
  constructor
  · intro same
    apply CanonicalVertexNames.payload_injective n
    exact (List.idxOf_inj (CanonicalVertexNames.payload_mem keys a presentA)).mp same
  · intro same
    subst b
    rfl

def onlineName (earlier : List (Key n)) (key : Key n) : Fin (earlier.length + 1) :=
  ⟨onlineID earlier key, by have := onlineID_le_prefix earlier key; omega⟩

@[simp] theorem onlineName_val (earlier : List (Key n)) (key : Key n) :
    (onlineName earlier key).val = onlineID earlier key := rfl

def paddedName (keys : List (Key n)) (vertices : Nat) (enough : keys.length ≤ vertices)
    (key : Key n) (present : key ∈ keys) : Fin vertices :=
  ⟨(CanonicalVertexNames.name keys key present).val,
    lt_of_lt_of_le (CanonicalVertexNames.name keys key present).isLt enough⟩

@[simp] theorem paddedName_val (keys : List (Key n)) (vertices : Nat)
    (enough : keys.length ≤ vertices) (key : Key n) (present : key ∈ keys) :
    (paddedName keys vertices enough key present).val =
      (CanonicalVertexNames.name keys key present).val := rfl

theorem paddedName_eq_iff (keys : List (Key n)) (vertices : Nat)
    (enough : keys.length ≤ vertices) (a b : Key n) (ha : a ∈ keys) (hb : b ∈ keys) :
    paddedName keys vertices enough a ha = paddedName keys vertices enough b hb ↔ a = b := by
  constructor
  · intro same
    apply (CanonicalVertexNames.name_eq_iff keys a b ha hb).mp
    apply Fin.ext
    exact congrArg (fun value : Fin vertices => value.val) same
  · intro same
    subst b
    rfl

def paddedEmbedding (keys : List (Key n)) (vertices : Nat) (enough : keys.length ≤ vertices) :
    {key // key ∈ keys} ↪ Fin vertices where
  toFun key := paddedName keys vertices enough key.val key.property
  inj' := by
    intro a b same
    exact Subtype.ext ((paddedName_eq_iff keys vertices enough
      a.val b.val a.property b.property).mp same)

def onlinePaddedName (earlier : List (Key n)) (key : Key n) (vertices : Nat)
    (room : earlier.length < vertices) : Fin vertices :=
  ⟨onlineID earlier key, lt_of_le_of_lt (onlineID_le_prefix earlier key) room⟩

theorem onlinePaddedName_eq_final (earlier suffix : List (Key n)) (key : Key n)
    (vertices : Nat) (enough : (earlier ++ key :: suffix).length ≤ vertices) :
    onlinePaddedName earlier key vertices (by
      simp only [List.length_append, List.length_cons] at enough
      omega) =
      paddedName (earlier ++ key :: suffix) vertices enough key (by simp) := by
  apply Fin.ext
  exact onlineID_eq_name_val earlier suffix key

end
end PerfectCompleteness.CanonicalOnlineNames

end OAI