File size: 23,355 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import Mathlib.Data.Fin.VecNotation
import OAI.Computability.PerfectCompleteness.Machines.PositionLemmas
import OAI.Computability.UniqueGames.Machines.MachineLemmas
import OAI.Computability.UniqueGames.Machines.MachineTransducerCopy
import OAI.Computability.UniqueGames.Machines.MachineUnaryLessAtLemmas

namespace OAI


namespace UniqueGamesTheorem.Foundations.Complexity.CookLevin.ClashMachine

open Turing MachineComposition PostfixModel InitializationTemplate
open Reduction.MachineSubstitution (pushWord stepAux_pushWord)

variable {K Λ σ : Type} [DecidableEq K]

abbrev Alphabet (_ : K) := Bool
abbrev State (σ : Type) := (σ × Bool) × Option Bool

def clean (ambient : σ) : State σ := ((ambient, false), none)

def emitted (output count : K) (base : K → List Bool) (tokens : List Token) : K → List Bool :=
  Function.update (Function.update base output ((tokenBits tokens).reverse ++ base output))
    count (List.replicate tokens.length true ++ base count)

@[simp] theorem emitted_output (output count : K) (hne : output ≠ count)
    (base : K → List Bool) (tokens : List Token) :
    emitted output count base tokens output = (tokenBits tokens).reverse ++ base output := by
  simp [emitted, hne]

@[simp] theorem emitted_count (output count : K) (base : K → List Bool) (tokens : List Token) :
    emitted output count base tokens count = List.replicate tokens.length true ++ base count := by
  simp [emitted]

theorem emitted_other (output count k : K) (ho : k ≠ output) (hc : k ≠ count)
    (base : K → List Bool) (tokens : List Token) : emitted output count base tokens k = base k := by
  simp [emitted, ho, hc]

theorem tokenBits_append (first second : List Token) :
    tokenBits (first ++ second) = tokenBits first ++ tokenBits second := by
  simp [tokenBits, tokenWords]

theorem emitted_append (output count : K) (hne : output ≠ count)
    (base : K → List Bool) (first second : List Token) :
    emitted output count (emitted output count base first) second =
      emitted output count base (first ++ second) := by
  funext k
  by_cases ho : k = output
  · subst k
    simp [emitted, hne, tokenBits_append, List.reverse_append, List.append_assoc]
  · by_cases hc : k = count
    · subst k
      have hr : List.replicate second.length true ++ List.replicate first.length true =
          List.replicate (first.length + second.length) true := by
        rw [← List.replicate_add, Nat.add_comm]
      simp only [emitted_count, List.length_append, ← List.append_assoc, hr]
    · simp [emitted, ho, hc]

@[simp] theorem emitted_nil (output count : K) (base : K → List Bool) :
    emitted output count base [] = base := by
  simp [emitted, tokenBits, tokenWords, encodeWords]

theorem chain {A : Type*} {f : A → A} {x y z : A} {n m : Nat}
    (first : f^[n] x = y) (second : f^[m] y = z) : f^[n + m] x = z := by
  rw [Nat.add_comm n m, Function.iterate_add_apply, first, second]

def literal (output count : K) (tokens : List Token)
    (continuation : TM2.Stmt (Alphabet (K := K)) Λ (State σ)) :
    TM2.Stmt (Alphabet (K := K)) Λ (State σ) :=
  pushWord output (tokenBits tokens)
    (pushWord count (List.replicate tokens.length true) continuation)

theorem stepAux_literal (output count : K) (hne : output ≠ count)
    (tokens : List Token) (continuation : TM2.Stmt (Alphabet (K := K)) Λ (State σ))
    (state : State σ) (base : K → List Bool) :
    TM2.stepAux (literal output count tokens continuation) state base =
      TM2.stepAux continuation state (emitted output count base tokens) := by
  simp [literal, stepAux_pushWord, emitted, Ne.symm hne]

namespace Input

inductive Label where
  | tag | scan | emit (b : Bool) | restore
  deriving DecidableEq

instance : Fintype Label where
  elems := { .tag, .scan, .emit false, .emit true, .restore }
  complete l := by cases l with
    | emit b => cases b <;> simp
    | tag => simp
    | scan => simp
    | restore => simp

def statement (slots : Fin 4 ↪ K) (labels : Label → Λ) (exit : Option Λ) :
    Label → TM2.Stmt (Alphabet (K := K)) Λ (State σ)
  | .tag => pushWord (slots 2) (encodeWord 5)
      (.push (slots 3) (fun _ => true) (.goto fun _ => labels .scan))
  | .scan => MachineTransducerCopy.scanLoop (slots 0) (slots 1) false
      (fun _ b => labels (.emit b)) (labels .restore)
  | .emit b => MachineTransducerCopy.emitter (slots 2) (fun (_ : Bool) _ => false)
      (fun _ b => [b]) (labels .scan) false b
  | .restore => Reduction.MachineTransfer.loopAt (slots 1) (slots 0) id false
      (labels .restore) exit

def steps (n : Nat) : Nat := 3 * (n + 1) + 3

private theorem identity_output (xs : List Bool) (control : Bool) :
    Reduction.MachineTransducer.output (fun (_ : Bool) _ => false)
      (fun _ b => [b]) control xs = xs := by
  induction xs generalizing control with
  | nil => rfl
  | cons b xs ih => simp [Reduction.MachineTransducer.output, ih]

theorem trace (slots : Fin 4 ↪ K) (labels : Label → Λ) (exit : Option Λ)
    (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ))
    (atLabels : ∀ l, program (labels l) = statement slots labels exit l)
    (base : K → List Bool) (n : Nat)
    (sourceWord : base (slots 0) = encodeWord n) (scratchEmpty : base (slots 1) = [])
    (ambient : σ) :
    (advance (TM2.step program))^[steps n]
      (some ⟨some (labels .tag), clean ambient, base⟩) =
      some ⟨exit, clean ambient, emitted (slots 2) (slots 3) base [.input n]⟩ := by
  let tagged := Function.update (Function.update base (slots 2)
    ((encodeWord 5).reverse ++ base (slots 2))) (slots 3) (true :: base (slots 3))
  have htag : (advance (TM2.step program))^[1]
      (some ⟨some (labels .tag), clean ambient, base⟩) =
      some ⟨some (labels .scan), clean ambient, tagged⟩ := by
    change some (TM2.stepAux (program (labels .tag)) _ _) = _
    rw [atLabels, statement, stepAux_pushWord]
    simp [TM2.stepAux, tagged, slots.injective.eq_iff]
  have hs : tagged (slots 0) = encodeWord n := by
    simpa [tagged, slots.injective.eq_iff] using sourceWord
  have ht : tagged (slots 1) = [] := by
    simpa [tagged, slots.injective.eq_iff] using scratchEmpty
  have hcopy := MachineTransducerCopy.transduceCopyTrace (slots 0) (slots 1) (slots 2)
    (slots.injective.ne (by decide)) (slots.injective.ne (by decide))
    (slots.injective.ne (by decide)) false (fun (_ : Bool) _ => false) (fun _ b => [b])
    (labels .scan) (labels .restore) (fun _ b => labels (.emit b)) exit program
    (atLabels .scan)
    (fun (control symbol : Bool) => by
      simpa only [statement, MachineTransducerCopy.emitter] using (atLabels (Label.emit symbol)))
    (atLabels .restore) tagged ht ambient false none
  rw [hs, encodeWord_length, identity_output] at hcopy
  have hout : Function.update tagged (slots 2) ((encodeWord n).reverse ++ tagged (slots 2)) =
      emitted (slots 2) (slots 3) base [.input n] := by
    funext k
    by_cases ho : k = slots 2
    · subst k
      simp [tagged, emitted, tokenBits, tokenWords, Token.words, encodeWords,
        slots.injective.eq_iff, List.reverse_append, List.append_assoc]
    · by_cases hc : k = slots 3
      · subst k
        simp [tagged, emitted, slots.injective.eq_iff]
      · simp [tagged, emitted, ho, hc]
  rw [hout] at hcopy
  have h := chain htag hcopy
  simpa only [steps, clean, Nat.add_comm 1] using h

end Input

namespace Pair

def leftMap : Fin 4 ↪ Fin 6 := ⟨![0, 1, 2, 3], by decide⟩
def rightMap : Fin 4 ↪ Fin 6 := ⟨![1, 0, 2, 3], by decide⟩
def inputLeftMap : Fin 4 ↪ Fin 6 := ⟨![0, 2, 4, 5], by decide⟩
def inputRightMap : Fin 4 ↪ Fin 6 := ⟨![1, 2, 4, 5], by decide⟩

inductive Label where
  | left (l : MachineUnaryLessAt.Label)
  | right (l : MachineUnaryLessAt.Label)
  | branchLeft | branchRight | diagonal
  | inputLeft (l : Input.Label)
  | inputRight (l : Input.Label)
  | conjunction
  deriving DecidableEq, Fintype

def choose (yes no : Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State σ) :=
  .branch (fun s => s.1.2)
    (.load (fun s => clean s.1.1) (.goto fun _ => yes))
    (.load (fun s => clean s.1.1) (.goto fun _ => no))

theorem stepAux_choose (yes no : Λ) (base : K → List Bool) (ambient : σ) (flag : Bool) :
    TM2.stepAux (choose yes no) ((ambient, flag), none) base =
      ⟨some (if flag then yes else no), clean ambient, base⟩ := by
  cases flag <;> rfl

def statement (slots : Fin 6 ↪ K) (labels : Label → Λ) (exit : Option Λ) :
    Label → TM2.Stmt (Alphabet (K := K)) Λ (State σ)
  | .left l => MachineUnaryLessAt.statement (leftMap.trans slots)
      (fun l => labels (.left l)) (some (labels .branchLeft)) l
  | .right l => MachineUnaryLessAt.statement (rightMap.trans slots)
      (fun l => labels (.right l)) (some (labels .branchRight)) l
  | .branchLeft => choose (labels (.inputLeft .tag)) (labels (.right .scan))
  | .branchRight => choose (labels (.inputLeft .tag)) (labels .diagonal)
  | .diagonal => literal (slots 4) (slots 5) [.const false]
      (Reduction.MachineTransfer.exitAt (slots 4) exit)
  | .inputLeft l => Input.statement (inputLeftMap.trans slots)
      (fun l => labels (.inputLeft l)) (some (labels (.inputRight .tag))) l
  | .inputRight l => Input.statement (inputRightMap.trans slots)
      (fun l => labels (.inputRight l)) (some (labels .conjunction)) l
  | .conjunction => literal (slots 4) (slots 5) [.and]
      (Reduction.MachineTransfer.exitAt (slots 4) exit)

def offSteps (i j : Nat) : Nat := Input.steps i + Input.steps j + 1

theorem offTrace (slots : Fin 6 ↪ K) (labels : Label → Λ) (exit : Option Λ)
    (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ))
    (atLabels : ∀ l, program (labels l) = statement slots labels exit l)
    (base : K → List Bool) (i j : Nat)
    (leftWord : base (slots 0) = encodeWord i) (rightWord : base (slots 1) = encodeWord j)
    (leftEmpty : base (slots 2) = []) (ambient : σ) :
    (advance (TM2.step program))^[offSteps i j]
      (some ⟨some (labels (.inputLeft .tag)), clean ambient, base⟩) =
      some ⟨exit, clean ambient, emitted (slots 4) (slots 5) base [.input i, .input j, .and]⟩ := by
  have hi := Input.trace (inputLeftMap.trans slots) (fun l => labels (.inputLeft l))
    (some (labels (.inputRight .tag))) program (fun _ => atLabels _)
    base i leftWord leftEmpty ambient
  change (advance (TM2.step program))^[Input.steps i]
    (some ⟨some (labels (.inputLeft .tag)), clean ambient, base⟩) =
    some ⟨some (labels (.inputRight .tag)), clean ambient,
      emitted (slots 4) (slots 5) base [.input i]⟩ at hi
  let afterLeft := emitted (slots 4) (slots 5) base [.input i]
  have hjWord : afterLeft (slots 1) = encodeWord j := by
    simpa [afterLeft, emitted, slots.injective.eq_iff] using rightWord
  have hjScratch : afterLeft (slots 2) = [] := by
    simpa [afterLeft, emitted, slots.injective.eq_iff] using leftEmpty
  have hj := Input.trace (inputRightMap.trans slots) (fun l => labels (.inputRight l))
    (some (labels .conjunction)) program (fun _ => atLabels _)
    afterLeft j hjWord hjScratch ambient
  change (advance (TM2.step program))^[Input.steps j]
    (some ⟨some (labels (.inputRight .tag)), clean ambient, afterLeft⟩) =
    some ⟨some (labels .conjunction), clean ambient,
      emitted (slots 4) (slots 5) afterLeft [.input j]⟩ at hj
  let afterRight := emitted (slots 4) (slots 5) afterLeft [.input j]
  have hand : (advance (TM2.step program))^[1]
      (some ⟨some (labels .conjunction), clean ambient, afterRight⟩) =
      some ⟨exit, clean ambient, emitted (slots 4) (slots 5) afterRight [.and]⟩ := by
    change some (TM2.stepAux (program (labels .conjunction)) _ _) = _
    rw [atLabels, statement, stepAux_literal _ _ (slots.injective.ne (by decide))]
    cases exit <;> rfl
  have h := chain (chain hi hj) hand
  simp only [afterRight, afterLeft,
    emitted_append (slots 4) (slots 5) (slots.injective.ne (by decide : (4 : Fin 6) ≠ 5)),
    List.singleton_append] at h
  exact h

def steps (i j : Nat) : Nat := MachineUnaryLessAt.steps i j + 1 +
  if i < j then offSteps i j else
    MachineUnaryLessAt.steps j i + 1 + if j < i then offSteps i j else 1

theorem trace (slots : Fin 6 ↪ K) (labels : Label → Λ) (exit : Option Λ)
    (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ))
    (atLabels : ∀ l, program (labels l) = statement slots labels exit l)
    (base : K → List Bool) (i j : Nat)
    (leftWord : base (slots 0) = encodeWord i) (rightWord : base (slots 1) = encodeWord j)
    (leftEmpty : base (slots 2) = []) (rightEmpty : base (slots 3) = []) (ambient : σ) :
    (advance (TM2.step program))^[steps i j]
      (some ⟨some (labels (.left .scan)), clean ambient, base⟩) =
      some ⟨exit, clean ambient, emitted (slots 4) (slots 5) base (clashPairTokens i j)⟩ := by
  have hl := MachineUnaryLessAt.lessThanTrace (leftMap.trans slots)
    (fun l => labels (.left l)) (some (labels .branchLeft)) program (fun _ => atLabels _)
    base i j [] [] (by simpa [leftMap] using leftWord) (by simpa [leftMap] using rightWord)
    leftEmpty rightEmpty ambient false none
  change (advance (TM2.step program))^[MachineUnaryLessAt.steps i j]
    (some ⟨some (labels (.left .scan)), clean ambient, base⟩) =
    some ⟨some (labels .branchLeft), ((ambient, decide (i < j)), none), base⟩ at hl
  have hr := MachineUnaryLessAt.lessThanTrace (rightMap.trans slots)
    (fun l => labels (.right l)) (some (labels .branchRight)) program (fun _ => atLabels _)
    base j i [] [] (by simpa [rightMap] using rightWord) (by simpa [rightMap] using leftWord)
    leftEmpty rightEmpty ambient false none
  change (advance (TM2.step program))^[MachineUnaryLessAt.steps j i]
    (some ⟨some (labels (.right .scan)), clean ambient, base⟩) =
    some ⟨some (labels .branchRight), ((ambient, decide (j < i)), none), base⟩ at hr
  have hbl : (advance (TM2.step program))^[1]
      (some ⟨some (labels .branchLeft), ((ambient, decide (i < j)), none), base⟩) =
      some ⟨some (if i < j then labels (.inputLeft .tag) else labels (.right .scan)),
        clean ambient, base⟩ := by
    change some (TM2.stepAux (program (labels .branchLeft)) _ _) = _
    rw [atLabels, statement, stepAux_choose]
    simp
  have hbr : (advance (TM2.step program))^[1]
      (some ⟨some (labels .branchRight), ((ambient, decide (j < i)), none), base⟩) =
      some ⟨some (if j < i then labels (.inputLeft .tag) else labels .diagonal),
        clean ambient, base⟩ := by
    change some (TM2.stepAux (program (labels .branchRight)) _ _) = _
    rw [atLabels, statement, stepAux_choose]
    simp
  have hoff := offTrace slots labels exit program atLabels base i j leftWord rightWord leftEmpty ambient
  by_cases hij : i < j
  · rw [ite_eq_left hij] at hbl
    have h := chain (chain hl hbl) hoff
    simpa only [steps, ite_eq_left hij, clashPairTokens, ite_eq_right (Nat.ne_of_lt hij)] using h
  · rw [ite_eq_right hij] at hbl
    by_cases hji : j < i
    · rw [ite_eq_left hji] at hbr
      have h := chain (chain (chain (chain hl hbl) hr) hbr) hoff
      simpa only [steps, ite_eq_right hij, ite_eq_left hji, clashPairTokens,
        ite_eq_right (Ne.symm (Nat.ne_of_lt hji)), Nat.add_assoc] using h
    · rw [ite_eq_right hji] at hbr
      have hd : (advance (TM2.step program))^[1]
          (some ⟨some (labels .diagonal), clean ambient, base⟩) =
          some ⟨exit, clean ambient, emitted (slots 4) (slots 5) base [.const false]⟩ := by
        change some (TM2.stepAux (program (labels .diagonal)) _ _) = _
        rw [atLabels, statement, stepAux_literal _ _ (slots.injective.ne (by decide))]
        cases exit <;> rfl
      have heq : i = j := by omega
      have h := chain (chain (chain (chain hl hbl) hr) hbr) hd
      simpa only [steps, ite_eq_right hij, ite_eq_right hji, clashPairTokens,
        ite_eq_left heq, Nat.add_assoc] using h

theorem steps_le (i j : Nat) : steps i j ≤ 12 * (i + j + 2) := by
  unfold steps offSteps Input.steps MachineUnaryLessAt.steps
  have h₁ := Nat.min_le_left i j
  have h₂ := Nat.min_le_left j i
  split <;> (try split) <;> omega

end Pair

namespace Row

def pairMap : Fin 6 ↪ Fin 7 := ⟨![1, 2, 3, 4, 5, 6], by decide⟩

inductive Label where
  | guard
  | pair (l : Pair.Label)
  | increment
  deriving DecidableEq, Fintype

def statement (slots : Fin 7 ↪ K) (labels : Label → Λ) (done : Λ) :
    Label → TM2.Stmt (Alphabet (K := K)) Λ (State σ)
  | .guard => MachineUnaryCounter.guard (slots 0) (labels (.pair (.left .scan))) done
  | .pair l => Pair.statement (pairMap.trans slots) (fun l => labels (.pair l))
      (some (labels .increment)) l
  | .increment => .push (slots 2) (fun _ => true) (.goto fun _ => labels .guard)

def frame (slots : Fin 7 ↪ K) (base : K → List Bool) (remaining current : Nat) : K → List Bool :=
  Function.update (Function.update base (slots 0) (encodeWord remaining)) (slots 2) (encodeWord current)

@[simp] theorem frame_current (slots : Fin 7 ↪ K) (base : K → List Bool) (r j : Nat) :
    frame slots base r j (slots 2) = encodeWord j := by simp [frame]

@[simp] theorem frame_remaining (slots : Fin 7 ↪ K) (base : K → List Bool) (r j : Nat) :
    frame slots base r j (slots 0) = encodeWord r := by simp [frame, slots.injective.eq_iff]

theorem counter_frame (slots : Fin 7 ↪ K) (base : K → List Bool) (r r' j : Nat) :
    MachineUnaryCounter.counterTapes (slots 0) (frame slots base r j) r' [] =
      frame slots base r' j := by
  funext k
  by_cases h₀ : k = slots 0
  · subst k; simp [MachineUnaryCounter.counterTapes, frame, slots.injective.eq_iff]
  · by_cases h₂ : k = slots 2 <;> simp [MachineUnaryCounter.counterTapes, frame, h₀, h₂]

theorem emitted_frame (slots : Fin 7 ↪ K) (base : K → List Bool) (r j : Nat) (tokens : List Token) :
    emitted (slots 5) (slots 6) (frame slots base r j) tokens =
      frame slots (emitted (slots 5) (slots 6) base tokens) r j := by
  funext k
  by_cases h₀ : k = slots 0
  · subst k; simp [emitted, frame, slots.injective.eq_iff]
  · by_cases h₂ : k = slots 2
    · subst k; simp [emitted, frame, slots.injective.eq_iff]
    · by_cases h₅ : k = slots 5
      · subst k; simp [emitted, frame, slots.injective.eq_iff]
      · by_cases h₆ : k = slots 6
        · subst k; simp [emitted, frame, slots.injective.eq_iff]
        · simp [emitted, frame, h₀, h₂, h₅, h₆]

theorem increment_frame (slots : Fin 7 ↪ K) (base : K → List Bool) (r j : Nat) :
    Function.update (frame slots base r j) (slots 2) (true :: encodeWord j) =
      frame slots base r (j + 1) := by
  simp [frame, encodeWord, List.replicate_succ]

def tokens (count i start : Nat) : List Token :=
  forTokens count (fun j => clashPairTokens i (start + j))

theorem tokens_zero (i start : Nat) : tokens 0 i start = [] := rfl

theorem tokens_succ (count i start : Nat) :
    tokens (count + 1) i start = clashPairTokens i start ++ tokens count i (start + 1) := by
  rw [tokens, forTokens_succ_first]
  simp only [Nat.add_zero]
  apply congrArg (List.append (clashPairTokens i start))
  apply forTokens_congr
  intro j _
  rw [show start + (j + 1) = start + 1 + j by omega]

def steps : Nat → Nat → Nat → Nat
  | 0, _, _ => 1
  | count + 1, i, start => 1 + Pair.steps i start + 1 + steps count i (start + 1)

theorem trace (slots : Fin 7 ↪ K) (labels : Label → Λ) (done : Λ)
    (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ))
    (atLabels : ∀ l, program (labels l) = statement slots labels done l)
    (base : K → List Bool) (count i start : Nat)
    (leftWord : base (slots 1) = encodeWord i)
    (leftEmpty : base (slots 3) = []) (rightEmpty : base (slots 4) = []) (ambient : σ) :
    (advance (TM2.step program))^[steps count i start]
      (some ⟨some (labels .guard), clean ambient, frame slots base count start⟩) =
      some ⟨some done, clean ambient,
        frame slots (emitted (slots 5) (slots 6) base (tokens count i start)) 0 (start + count)⟩ := by
  induction count generalizing base start with
  | zero =>
    have h := MachineUnaryCounter.guardTrace_zero (slots 0) (labels .guard)
      (labels (.pair (.left .scan))) done program (atLabels .guard)
      (frame slots base 0 start) [] (ambient, false) none
    simpa only [counter_frame, steps, tokens_zero, emitted_nil, Nat.add_zero, clean] using h
  | succ count ih =>
    have hg := MachineUnaryCounter.guardTrace_succ (slots 0) (labels .guard)
      (labels (.pair (.left .scan))) done program (atLabels .guard)
      (frame slots base count start) count [] (ambient, false) none
    simp only [counter_frame] at hg
    have hiWord : frame slots base count start (slots 1) = encodeWord i := by
      simpa [frame, slots.injective.eq_iff] using leftWord
    have hsLeft : frame slots base count start (slots 3) = [] := by
      simpa [frame, slots.injective.eq_iff] using leftEmpty
    have hsRight : frame slots base count start (slots 4) = [] := by
      simpa [frame, slots.injective.eq_iff] using rightEmpty
    have hp := Pair.trace (pairMap.trans slots) (fun l => labels (.pair l))
      (some (labels .increment)) program (fun _ => atLabels _)
      (frame slots base count start) i start hiWord (frame_current slots base count start)
      hsLeft hsRight ambient
    change (advance (TM2.step program))^[Pair.steps i start]
      (some ⟨some (labels (.pair (.left .scan))), clean ambient, frame slots base count start⟩) =
      some ⟨some (labels .increment), clean ambient,
        emitted (slots 5) (slots 6) (frame slots base count start) (clashPairTokens i start)⟩ at hp
    rw [emitted_frame] at hp
    let nextBase := emitted (slots 5) (slots 6) base (clashPairTokens i start)
    have hn : (advance (TM2.step program))^[1]
        (some ⟨some (labels .increment), clean ambient, frame slots nextBase count start⟩) =
        some ⟨some (labels .guard), clean ambient, frame slots nextBase count (start + 1)⟩ := by
      change some (TM2.stepAux (program (labels .increment)) _ _) = _
      rw [atLabels, statement]
      simp only [TM2.stepAux, frame_current, increment_frame]
    have hni : nextBase (slots 1) = encodeWord i := by
      simpa [nextBase, emitted, slots.injective.eq_iff] using leftWord
    have hnl : nextBase (slots 3) = [] := by
      simpa [nextBase, emitted, slots.injective.eq_iff] using leftEmpty
    have hnr : nextBase (slots 4) = [] := by
      simpa [nextBase, emitted, slots.injective.eq_iff] using rightEmpty
    have hr := ih nextBase (start + 1) hni hnl hnr
    have h := chain (chain (chain hg hp) hn) hr
    rw [show start + 1 + count = start + (count + 1) by omega] at h
    simp only [nextBase,
      emitted_append (slots 5) (slots 6) (slots.injective.ne (by decide : (5 : Fin 7) ≠ 6)),
      ← tokens_succ] at h
    exact h

theorem steps_le (count i start B : Nat) (hB : i + start + count ≤ B) :
    steps count i start ≤ count * (12 * (B + 2) + 2) + 1 := by
  induction count generalizing start with
  | zero => simp [steps]
  | succ count ih =>
    have hr := ih (start + 1) (by omega)
    have hp := Pair.steps_le i start
    have hpos : i + start + 2 ≤ B + 2 := by omega
    have hm := Nat.mul_le_mul_left 12 hpos
    rw [steps, Nat.succ_mul]
    omega

end Row

end UniqueGamesTheorem.Foundations.Complexity.CookLevin.ClashMachine

end OAI