File size: 14,904 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import OAI.Computability.PerfectCompleteness.Machines.ExactTargetOdometer
import OAI.Computability.PerfectCompleteness.Machines.SourceClauseCountMachine
import OAI.Computability.PerfectCompleteness.Machines.TupleInitializationMachine

namespace OAI


namespace PerfectCompleteness.NormalizedTargetInitialization


open Turing UniqueGamesTheorem.Foundations.Complexity
open MachineComposition
open scoped Classical

noncomputable section

abbrev Tape (width : Nat) := SignedTupleLayout.Tape width (Fin 4)
abbrev Alphabet {width : Nat} (_ : Tape width) := Bool
abbrev State (A : Type) := A × Option Bool

def clean {A : Type} (ambient : A) : State A := (ambient, none)

variable {width : Nat}

def countTape : Fin 4 → Tape width
  | 0 => .source .table
  | 1 => .extra 1
  | 2 => .extra 2
  | 3 => .extra 0

theorem countTape_injective : Function.Injective (countTape (width := width)) := by
  intro i j same
  fin_cases i <;> fin_cases j <;> simp [countTape] at same ⊢

def currents (width : Nat) : List (Tape width) := List.ofFn SignedTupleLayout.Tape.current
def remaining (width : Nat) : List (Tape width) := List.ofFn SignedTupleLayout.Tape.remaining

theorem destinations_nodup (width : Nat) : (currents width ++ remaining width).Nodup := by
  unfold currents remaining
  apply List.nodup_append.mpr
  refine ⟨?_, ?_, ?_⟩
  · apply List.nodup_ofFn.mpr
    intro i j same
    simpa using same
  · apply List.nodup_ofFn.mpr
    intro i j same
    simpa using same
  · intro a ha b hb
    obtain ⟨i, rfl⟩ := List.mem_ofFn.mp ha
    obtain ⟨j, rfl⟩ := List.mem_ofFn.mp hb
    intro same
    cases same

theorem count_not_destination :
    (SignedTupleLayout.Tape.extra 0 : Tape width) ∉ currents width ++ remaining width := by
  simp [currents, remaining, List.mem_ofFn]

inductive Label
  | seed
  | count (label : SourceClauseCountMachine.Label)
  | tuple (label : TupleInitializationMachine.Label)
  deriving DecidableEq, Fintype

variable {Λ A : Type}

def instruction (labels : Label → Λ) (done rejected : Option Λ) :
    Label → TM2.Stmt (Alphabet (width := width)) Λ (State A)
  | .seed => .push (.extra 0) (fun _ => false)
      (.push .edgeCount (fun _ => false)
        (.load (fun state => clean state.1)
          (.goto (fun _ => labels (.count SourceClauseCountMachine.main)))))
  | .count label => SourceClauseCountMachine.instruction countTape
      (fun l => labels (.count l)) (some (labels (.tuple .seed))) rejected label
  | .tuple label => TupleInitializationMachine.instruction (.extra 0)
      (currents width) (remaining width) (fun l => labels (.tuple l)) done label

theorem instruction_pushBound (labels : Label → Λ) (done rejected : Option Λ) (label : Label) :
    Runtime.statementPushBound (instruction (width := width) (A := A) labels done rejected label) ≤
      2 * width + 2 := by
  have exitBound (destination : Tape width) (exit : Option Λ) :
      Runtime.statementPushBound
        (UniqueGamesTheorem.Reduction.MachineTransfer.exitAt
          (Γ := Alphabet (width := width)) (σ := A) destination exit) = 0 := by
    cases exit <;> rfl
  cases label with
  | seed => simp [instruction, Runtime.statementPushBound]
  | count label =>
      cases label <;>
        simp [instruction, SourceClauseCountMachine.instruction,
          UniqueGamesTheorem.Reduction.MachineTransfer.loopAt, MachineCopy.forkLoop,
          UnaryBlockSkipMachine.instruction, UnaryBlockSkipMachine.finish,
          UniqueGamesTheorem.Foundations.Hastad.SourceMachine.fieldLoop,
          MachineDrain.drain, Runtime.statementPushBound, exitBound]
  | tuple label =>
      have bound := TupleInitializationMachine.instruction_pushBound
        (σ := A) (SignedTupleLayout.Tape.extra 0 : Tape width)
        (currents width) (remaining width) (fun l => labels (.tuple l)) done label
      change Runtime.statementPushBound
        (TupleInitializationMachine.instruction (.extra 0)
          (currents width) (remaining width) (fun l => labels (.tuple l)) done label) ≤ _
      refine bound.trans ?_
      simp only [currents, remaining, List.length_ofFn]
      omega

def inputTapes (input : NormalizedSourceInput.Input) : Tape width → List Bool
  | .source .table => NormalizedSourceInput.bits input
  | _ => []

def seededTapes (input : NormalizedSourceInput.Input) : Tape width → List Bool :=
  Function.update (Function.update (inputTapes input) (.extra 0) [false]) .edgeCount [false]

def countedTapes (input : NormalizedSourceInput.Input) : Tape width → List Bool :=
  Function.update (seededTapes input) (.extra 0)
    (encodeWord (NormalizedSourceInput.clauseCount input))

def zeroDigits (input : NormalizedSourceInput.Input) :
    Fin width → Fin (NormalizedSourceInput.clauseCount input) :=
  fun _ => ⟨0, NormalizedSourceInput.clauseCount_positive input⟩

def readyTapes (input : NormalizedSourceInput.Input) : Tape width → List Bool :=
  Function.update (ExactTargetOdometer.baseTapes input (zeroDigits input) (fun _ => []))
    .edgeCount (encodeWord 0)

@[simp] theorem seeded_count (input : NormalizedSourceInput.Input) :
    seededTapes (width := width) input (.extra 0) = [false] := by
  simp [seededTapes]

@[simp] theorem counted_count (input : NormalizedSourceInput.Input) :
    countedTapes (width := width) input (.extra 0) =
      encodeWord (NormalizedSourceInput.clauseCount input) := by
  simp [countedTapes]

theorem initializedTapes_eq_ready (input : NormalizedSourceInput.Input) :
    TupleInitializationMachine.initializedTapes (.extra 0) (currents width) (remaining width)
        (countedTapes input) (NormalizedSourceInput.clauseCount input - 1) [] =
      readyTapes input := by
  funext key
  cases key with
  | source source =>
      cases source <;>
        simp [TupleInitializationMachine.initializedTapes, TupleInitializationMachine.prefixOn,
          currents, remaining, List.mem_ofFn, countedTapes, seededTapes, inputTapes,
          readyTapes, ExactTargetOdometer.baseTapes]
  | current position =>
      simp [TupleInitializationMachine.initializedTapes, TupleInitializationMachine.prefixOn,
        currents, remaining, List.mem_ofFn, countedTapes, seededTapes, inputTapes,
        readyTapes, ExactTargetOdometer.baseTapes, zeroDigits, encodeWord]
  | remaining position =>
      simp [TupleInitializationMachine.initializedTapes, TupleInitializationMachine.prefixOn,
        currents, remaining, List.mem_ofFn, countedTapes, seededTapes, inputTapes,
        readyTapes, ExactTargetOdometer.baseTapes, zeroDigits, encodeWord]
  | encodedVariable position slot =>
      simp [TupleInitializationMachine.initializedTapes, TupleInitializationMachine.prefixOn,
        currents, remaining, List.mem_ofFn, countedTapes, seededTapes, inputTapes,
        readyTapes, ExactTargetOdometer.baseTapes]
  | adapterScratch =>
      simp [TupleInitializationMachine.initializedTapes, TupleInitializationMachine.prefixOn,
        currents, remaining, List.mem_ofFn, countedTapes, seededTapes, inputTapes,
        readyTapes, ExactTargetOdometer.baseTapes]
  | edgeWork slot =>
      simp [TupleInitializationMachine.initializedTapes, TupleInitializationMachine.prefixOn,
        currents, remaining, List.mem_ofFn, countedTapes, seededTapes, inputTapes,
        readyTapes, ExactTargetOdometer.baseTapes]
  | edgeCount =>
      simp [TupleInitializationMachine.initializedTapes, TupleInitializationMachine.prefixOn,
        currents, remaining, List.mem_ofFn, countedTapes, seededTapes,
        readyTapes, encodeWord]
  | extra slot =>
      fin_cases slot <;>
        simp [TupleInitializationMachine.initializedTapes, TupleInitializationMachine.prefixOn,
          currents, remaining, List.mem_ofFn, countedTapes, seededTapes, inputTapes,
          readyTapes, ExactTargetOdometer.baseTapes]

@[simp] theorem ready_table (input : NormalizedSourceInput.Input) :
    readyTapes (width := width) input (.source .table) = NormalizedSourceInput.bits input := by
  simp [readyTapes, ExactTargetOdometer.baseTapes]

@[simp] theorem ready_current (input : NormalizedSourceInput.Input) (position : Fin width) :
    readyTapes input (.current position) = encodeWord 0 := by
  simp [readyTapes, ExactTargetOdometer.baseTapes, zeroDigits]

@[simp] theorem ready_remaining (input : NormalizedSourceInput.Input) (position : Fin width) :
    readyTapes input (.remaining position) =
      encodeWord (NormalizedSourceInput.clauseCount input - 1) := by
  simp [readyTapes, ExactTargetOdometer.baseTapes, zeroDigits]

@[simp] theorem ready_extra (input : NormalizedSourceInput.Input) (slot : Fin 4) :
    readyTapes (width := width) input (.extra slot) = [] := by
  simp [readyTapes, ExactTargetOdometer.baseTapes]

theorem ready_eq_native {branch : Nat → Nat} {n t : Nat}
    (input : NormalizedSourceInput.Input) :
    readyTapes (width := TreeCanonical.locationCount branch n t) input =
      ExactTargetOdometer.nativeTapes (branch := branch) (n := n) (t := t)
        input (zeroDigits input) ⟨[], [], [], 0⟩ (fun _ : Fin 4 => []) := by
  funext key
  cases key with
  | source source =>
      cases source <;>
        simp [readyTapes, ExactTargetOdometer.nativeTapes, SignedTupleBodyMachine.tapes,
          ExactTargetOdometer.baseTapes]
  | current position =>
      simp [readyTapes, ExactTargetOdometer.nativeTapes, SignedTupleBodyMachine.tapes]
  | remaining position =>
      simp [readyTapes, ExactTargetOdometer.nativeTapes, SignedTupleBodyMachine.tapes]
  | encodedVariable position slot =>
      simp [readyTapes, ExactTargetOdometer.nativeTapes, SignedTupleBodyMachine.tapes]
  | adapterScratch =>
      simp [readyTapes, ExactTargetOdometer.nativeTapes, SignedTupleBodyMachine.tapes]
  | edgeWork slot =>
      fin_cases slot <;>
        simp [readyTapes, ExactTargetOdometer.nativeTapes, SignedTupleBodyMachine.tapes,
          ExactTargetOdometer.baseTapes, CanonicalVertexNames.tokens]
  | edgeCount =>
      simp [readyTapes, ExactTargetOdometer.nativeTapes, SignedTupleBodyMachine.tapes]
  | extra slot =>
      simp [readyTapes, ExactTargetOdometer.nativeTapes, SignedTupleBodyMachine.tapes]

variable (labels : Label → Λ) (done rejected : Option Λ)
    (program : Λ → TM2.Stmt (Alphabet (width := width)) Λ (State A))
    (atLabels : ∀ label, program (labels label) = instruction labels done rejected label)

include atLabels in
theorem seedStep (input : NormalizedSourceInput.Input) (ambient : A) (register : Option Bool) :
    TM2.step program ⟨some (labels .seed), (ambient, register), inputTapes input⟩ =
      some ⟨some (labels (.count SourceClauseCountMachine.main)), clean ambient, seededTapes input⟩ := by
  change some (TM2.stepAux (program (labels .seed)) _ _) = _
  rw [atLabels .seed]
  simp [instruction, TM2.stepAux, seededTapes, inputTapes, clean]

def initializeExactInTime (input : NormalizedSourceInput.Input)
    (ambient : A) (register : Option Bool) :
    StateTransition.EvalsToInTime (TM2.step program)
      ⟨some (labels .seed), (ambient, register), inputTapes input⟩
      (some ⟨done, clean ambient, readyTapes input⟩)
      (1 + 3 * ((NormalizedSourceInput.bits input).length + 1) +
        (NormalizedSourceInput.clauseCount input + 1)) := by
  have seeded : StateTransition.EvalsToInTime (TM2.step program)
      ⟨some (labels .seed), (ambient, register), inputTapes input⟩
      (some ⟨some (labels (.count SourceClauseCountMachine.main)),
        clean ambient, seededTapes input⟩) 1 := by
    refine { steps := 1, evals_in_steps := ?_, steps_le_m := Nat.le_refl 1 }
    exact seedStep labels done rejected program atLabels input ambient register
  have counted : StateTransition.EvalsToInTime (TM2.step program)
      ⟨some (labels (.count SourceClauseCountMachine.main)), clean ambient, seededTapes input⟩
      (some ⟨some (labels (.tuple .seed)), clean ambient, countedTapes input⟩)
      (3 * ((NormalizedSourceInput.bits input).length + 1)) := by
    have run := SourceClauseCountMachine.countInTime countTape countTape_injective
      (fun label => labels (.count label)) (some (labels (.tuple .seed))) rejected
      program (fun label => atLabels (.count label)) (seededTapes input) ambient input.formula
      (by simp [countTape, seededTapes, inputTapes, NormalizedSourceInput.bits])
      (by simp [countTape, seededTapes, inputTapes])
      (by simp [countTape, seededTapes, inputTapes])
    simpa only [SourceClauseCountMachine.clean, clean, countTape, countedTapes, seeded_count,
      NormalizedSourceInput.bits, NormalizedSourceInput.clauseCount, encodeWord] using run
  have initialized : StateTransition.EvalsToInTime (TM2.step program)
      ⟨some (labels (.tuple .seed)), clean ambient, countedTapes input⟩
      (some ⟨done, clean ambient, readyTapes input⟩)
      (NormalizedSourceInput.clauseCount input + 1) := by
    have run := TupleInitializationMachine.initializeInTime (σ := A)
      (SignedTupleLayout.Tape.extra 0 : Tape width)
      (currents width) (remaining width) (destinations_nodup width) count_not_destination
      (fun label => labels (.tuple label)) done program
      (fun label => by simpa only [instruction] using atLabels (.tuple label))
      (countedTapes input) ambient (NormalizedSourceInput.clauseCount input)
      (NormalizedSourceInput.clauseCount_positive input) []
      (by simp only [counted_count, List.append_nil])
    simpa only [TupleInitializationMachine.clean, clean, initializedTapes_eq_ready] using run
  have first := StateTransition.EvalsToInTime.trans (TM2.step program) _ _ _ _ _ seeded counted
  have full := StateTransition.EvalsToInTime.trans (TM2.step program) _ _ _ _ _ first initialized
  simpa only [Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] using full

theorem count_length_le (input : NormalizedSourceInput.Input) :
    NormalizedSourceInput.clauseCount input + 1 ≤ (NormalizedSourceInput.bits input).length := by
  rw [NormalizedSourceInput.bits_eq]
  simp only [List.length_append, encodeWord_length]
  omega

def initializeInTime (input : NormalizedSourceInput.Input) (ambient : A) (register : Option Bool) :
    StateTransition.EvalsToInTime (TM2.step program)
      ⟨some (labels .seed), (ambient, register), inputTapes input⟩
      (some ⟨done, clean ambient, readyTapes input⟩)
      (4 * ((NormalizedSourceInput.bits input).length + 1)) := by
  let run := initializeExactInTime labels done rejected program atLabels input ambient register
  refine
    { steps := run.steps
      evals_in_steps := run.evals_in_steps
      steps_le_m := run.steps_le_m.trans ?_ }
  have bounded := count_length_le input
  omega

theorem finite_tapes : Finite (Tape width) := inferInstance
theorem finite_state [Finite A] : Finite (State A) := inferInstance
theorem finite_alphabet (key : Tape width) : Finite (Alphabet key) := inferInstance

end
end PerfectCompleteness.NormalizedTargetInitialization

end OAI