File size: 21,159 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
import OAI.Computability.PerfectCompleteness.Machines.VerifierCircuit
import OAI.Computability.UniqueGames.PCP.SourceHeaderCounts

namespace OAI

section


noncomputable section
namespace UniqueGamesTheorem.Foundations.Complexity.CookLevin.ClockPreparation

open Turing MachineComposition
open UniqueGamesTheorem.Foundations.Hastad.SourceHeaderCounts
open PolynomialMachine

abbrev PhasePrivate (p : Polynomial Nat) := Fin 5 ⊕ Fin (width p)

def Private : List (Polynomial Nat) → Type
  | [] => Empty
  | p :: ps => PhasePrivate p ⊕ Private ps

abbrev Layout (ps : List (Polynomial Nat)) := Unit ⊕ Private ps

def Label : List (Polynomial Nat) → Type
  | [] => Empty
  | p :: ps => Eval.Label p ⊕ Label ps

def Output : List (Polynomial Nat) → Type
  | [] => Empty
  | _ :: ps => Unit ⊕ Output ps

instance privateFintype (ps : List (Polynomial Nat)) : Fintype (Private ps) := by
  induction ps with
  | nil => exact inferInstanceAs (Fintype Empty)
  | cons p ps ih =>
    letI := ih
    exact inferInstanceAs (Fintype (PhasePrivate p ⊕ Private ps))

instance privateDecidableEq (ps : List (Polynomial Nat)) : DecidableEq (Private ps) := by
  induction ps with
  | nil => exact inferInstanceAs (DecidableEq Empty)
  | cons p ps ih =>
    letI := ih
    exact inferInstanceAs (DecidableEq (PhasePrivate p ⊕ Private ps))

instance labelFintype (ps : List (Polynomial Nat)) : Fintype (Label ps) := by
  induction ps with
  | nil => exact inferInstanceAs (Fintype Empty)
  | cons p ps ih =>
    letI := ih
    exact inferInstanceAs (Fintype (Eval.Label p ⊕ Label ps))

instance labelDecidableEq (ps : List (Polynomial Nat)) : DecidableEq (Label ps) := by
  induction ps with
  | nil => exact inferInstanceAs (DecidableEq Empty)
  | cons p ps ih =>
    letI := ih
    exact inferInstanceAs (DecidableEq (Eval.Label p ⊕ Label ps))

instance outputFintype (ps : List (Polynomial Nat)) : Fintype (Output ps) := by
  induction ps with
  | nil => exact inferInstanceAs (Fintype Empty)
  | cons p ps ih =>
    letI := ih
    exact inferInstanceAs (Fintype (Unit ⊕ Output ps))

def controlSlots (p : Polynomial Nat) (ps : List (Polynomial Nat)) : Fin 6 ↪ Layout (p :: ps) where
  toFun i := match i.val with
    | 0 => .inl ()
    | 1 => .inr (.inl (.inl 0))
    | 2 => .inr (.inl (.inl 1))
    | 3 => .inr (.inl (.inl 2))
    | 4 => .inr (.inl (.inl 3))
    | _ => .inr (.inl (.inl 4))
  inj' := by
    intro i j h
    fin_cases i <;> fin_cases j <;> cases h <;> rfl

def headLayout (p : Polynomial Nat) (ps : List (Polynomial Nat)) :
    MachineHorner.Layout (width p) ↪ Layout (p :: ps) where
  toFun
    | .inl i => controlSlots p ps i
    | .inr i => .inr (.inl (.inr i))
  inj' := by
    intro a b h
    cases a with
    | inl a =>
      cases b with
      | inl b => exact congrArg Sum.inl ((controlSlots p ps).injective h)
      | inr b => fin_cases a <;> cases h
    | inr a =>
      cases b with
      | inl b => fin_cases b <;> cases h
      | inr b =>
        exact congrArg Sum.inr (Sum.inr.inj (Sum.inl.inj (Sum.inr.inj h)))

def tailLayout (p : Polynomial Nat) (ps : List (Polynomial Nat)) :
    Layout ps ↪ Layout (p :: ps) where
  toFun
    | .inl _ => .inl ()
    | .inr i => .inr (.inr i)
  inj' := by
    intro a b h
    cases a with
    | inl a => (cases b <;> cases h); rfl
    | inr a =>
      cases b with
      | inl b => cases h
      | inr b => exact congrArg Sum.inr (Sum.inr.inj (Sum.inr.inj h))

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

def headSlots {p : Polynomial Nat} {ps : List (Polynomial Nat)}
    (slots : Layout (p :: ps) ↪ K) : MachineHorner.Layout (width p) ↪ K :=
  (headLayout p ps).trans slots

def tailSlots {p : Polynomial Nat} {ps : List (Polynomial Nat)}
    (slots : Layout (p :: ps) ↪ K) : Layout ps ↪ K :=
  (tailLayout p ps).trans slots

omit [DecidableEq K] in
@[simp] theorem headSlots_input {p : Polynomial Nat} {ps : List (Polynomial Nat)}
    (slots : Layout (p :: ps) ↪ K) : headSlots slots (.inl 0) = slots (.inl ()) := rfl

omit [DecidableEq K] in
@[simp] theorem headSlots_output {p : Polynomial Nat} {ps : List (Polynomial Nat)}
    (slots : Layout (p :: ps) ↪ K) :
    headSlots slots (.inl 3) = slots (.inr (.inl (.inl 2))) := rfl

omit [DecidableEq K] in
@[simp] theorem tailSlots_input {p : Polynomial Nat} {ps : List (Polynomial Nat)}
    (slots : Layout (p :: ps) ↪ K) : tailSlots slots (.inl ()) = slots (.inl ()) := rfl

omit [DecidableEq K] in
theorem head_output_ne_tail {p : Polynomial Nat} {ps : List (Polynomial Nat)}
    (slots : Layout (p :: ps) ↪ K) (i : Layout ps) :
    headSlots slots (.inl 3) ≠ tailSlots slots i := by
  have controlPort : controlSlots p ps 3 = (.inr (.inl (.inl 2)) : Layout (p :: ps)) := by
    rw [controlSlots.eq_1]
    rfl
  have headPort : headLayout p ps (.inl 3) = (.inr (.inl (.inl 2)) : Layout (p :: ps)) := by
    rw [headLayout.eq_1]
    exact controlPort
  rw [headSlots.eq_1, tailSlots.eq_1]
  change slots (headLayout p ps (.inl 3)) ≠ slots (tailLayout p ps i)
  rw [headPort]
  cases i with
  | inl =>
    change slots (.inr (.inl (.inl 2))) ≠ slots (.inl ())
    exact slots.injective.ne (by intro h; cases h)
  | inr i =>
    change slots (.inr (.inl (.inl 2))) ≠ slots (.inr (.inr i))
    exact slots.injective.ne (by intro h; cases h)

def Clean (ps : List (Polynomial Nat)) (slots : Layout ps ↪ K) (base : K → List Bool) : Prop :=
  ∀ i : Private ps, base (slots (.inr i)) = []

omit [DecidableEq K] in
theorem head_clean {p : Polynomial Nat} {ps : List (Polynomial Nat)}
    (slots : Layout (p :: ps) ↪ K) (base : K → List Bool) (h : Clean (p :: ps) slots base) :
    MachineHorner.Clean (headSlots slots) base :=
  ⟨h (.inl (.inl 0)), h (.inl (.inl 1)), h (.inl (.inl 3)), h (.inl (.inl 4))⟩

omit [DecidableEq K] in
theorem head_coefficients_clean {p : Polynomial Nat} {ps : List (Polynomial Nat)}
    (slots : Layout (p :: ps) ↪ K) (base : K → List Bool) (h : Clean (p :: ps) slots base)
    (i : Fin (width p)) : base (headSlots slots (.inr i)) = [] :=
  h (.inl (.inr i))

theorem tail_clean_after_head {p : Polynomial Nat} {ps : List (Polynomial Nat)}
    (slots : Layout (p :: ps) ↪ K) (base : K → List Bool)
    (h : Clean (p :: ps) slots base) (value : Nat) :
    Clean ps (tailSlots slots) (MachineHorner.resultTapes (headSlots slots) base value) := by
  intro i
  rw [MachineHorner.resultTapes, Function.update_of_ne (head_output_ne_tail slots (.inr i)).symm]
  exact h (.inr i)

def entry : (ps : List (Polynomial Nat)) → (Label ps → Λ) → Option Λ → Option Λ
  | [], _, exit => exit
  | p :: _, labels, _ => some (labels (.inl (Eval.start p)))

def statement : (ps : List (Polynomial Nat)) → (slots : Layout ps ↪ K) →
    (Label ps → Λ) → Option Λ → Label ps →
      TM2.Stmt (fun _ : K => Bool) Λ (MachineHorner.State σ)
  | [], _, _, _, l => Empty.elim l
  | p :: ps, slots, labels, exit, .inl l =>
      Eval.statement p (headSlots slots) (fun l => labels (.inl l))
        (entry ps (fun l => labels (.inr l)) exit) l
  | _ :: ps, slots, labels, exit, .inr l =>
      statement ps (tailSlots slots) (fun l => labels (.inr l)) exit l

def resultTapes : (ps : List (Polynomial Nat)) → (Layout ps ↪ K) →
    (K → List Bool) → Nat → K → List Bool
  | [], _, base, _ => base
  | p :: ps, slots, base, n =>
      resultTapes ps (tailSlots slots)
        (MachineHorner.resultTapes (headSlots slots) base (p.eval n)) n

def steps : List (Polynomial Nat) → Nat → Nat
  | [], _ => 0
  | p :: ps, n => Eval.steps p n + steps ps n

def timePolynomial : List (Polynomial Nat) → Polynomial Nat
  | [] => 0
  | p :: ps => PolynomialMachine.timePolynomial p + timePolynomial ps

theorem steps_le_timePolynomial (ps : List (Polynomial Nat)) (n : Nat) :
    steps ps n ≤ (timePolynomial ps).eval n := by
  induction ps with
  | nil => simp [steps, timePolynomial]
  | cons p ps ih =>
    have hp : Eval.steps p n ≤ (PolynomialMachine.timePolynomial p).eval n := by
      rw [PolynomialMachine.timePolynomial_eval]
      unfold Eval.steps
      omega
    simpa only [steps, timePolynomial, Polynomial.eval_add] using Nat.add_le_add hp ih

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

theorem trace (ps : List (Polynomial Nat)) (slots : Layout ps ↪ K)
    (labels : Label ps → Λ) (exit : Option Λ)
    (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (MachineHorner.State σ))
    (atLabels : ∀ l, program (labels l) = statement ps slots labels exit l)
    (base : K → List Bool) (n : Nat)
    (hinput : base (slots (.inl ())) = encodeWord n) (clean : Clean ps slots base)
    (ambient : σ) :
    (advance (TM2.step program))^[steps ps n]
      (some ⟨entry ps labels exit, ((ambient, ()), none), base⟩) =
      some ⟨exit, ((ambient, ()), none), resultTapes ps slots base n⟩ := by
  induction ps generalizing base with
  | nil => rfl
  | cons p ps ih =>
    have first := Eval.trace p (headSlots slots) (fun l => labels (.inl l))
      (entry ps (fun l => labels (.inr l)) exit) program
      (fun l => atLabels (.inl l)) base n (by simpa using hinput)
      (head_coefficients_clean slots base clean) (head_clean slots base clean) ambient none
    have hnext : MachineHorner.resultTapes (headSlots slots) base (p.eval n)
        (tailSlots slots (.inl ())) = encodeWord n := by
      rw [MachineHorner.resultTapes,
        Function.update_of_ne (head_output_ne_tail slots (.inl ())).symm]
      exact hinput
    have second := ih (tailSlots slots) (fun l => labels (.inr l))
      (fun l => atLabels (.inr l))
      (MachineHorner.resultTapes (headSlots slots) base (p.eval n)) hnext
      (tail_clean_after_head slots base clean (p.eval n))
    exact trace_trans _ first second

def outputSlot : (ps : List (Polynomial Nat)) → Output ps → Layout ps
  | [], i => Empty.elim i
  | _ :: _, .inl _ => .inr (.inl (.inl 2))
  | p :: ps, .inr i => tailLayout p ps (outputSlot ps i)

def outputPolynomial : (ps : List (Polynomial Nat)) → Output ps → Polynomial Nat
  | [], i => Empty.elim i
  | p :: _, .inl _ => p
  | _ :: ps, .inr i => outputPolynomial ps i

theorem outputSlot_private (ps : List (Polynomial Nat)) (i : Output ps) :
    ∃ k : Private ps, outputSlot ps i = .inr k := by
  induction ps with
  | nil => exact Empty.elim i
  | cons p ps ih =>
    cases i with
    | inl i => exact ⟨.inl (.inl 2), rfl⟩
    | inr i =>
      obtain ⟨k, hk⟩ := ih i
      refine ⟨.inr k, ?_⟩
      simp only [outputSlot, hk, tailLayout]
      rfl

theorem outputSlot_ne_input (ps : List (Polynomial Nat)) (i : Output ps) :
    outputSlot ps i ≠ .inl () := by
  obtain ⟨k, hk⟩ := outputSlot_private ps i
  rw [hk]
  intro h
  cases h

theorem outputSlot_injective (ps : List (Polynomial Nat)) :
    Function.Injective (outputSlot ps) := by
  induction ps with
  | nil => intro i; exact Empty.elim i
  | cons p ps ih =>
    intro i j h
    cases i with
    | inl i =>
      cases j with
      | inl j => cases i; cases j; rfl
      | inr j =>
        exact False.elim ((head_output_ne_tail (Function.Embedding.refl (Layout (p :: ps)))
          (outputSlot ps j)) h)
    | inr i =>
      cases j with
      | inl j =>
        exact False.elim ((head_output_ne_tail (Function.Embedding.refl (Layout (p :: ps)))
          (outputSlot ps i)) h.symm)
      | inr j => exact congrArg Sum.inr (ih ((tailLayout p ps).injective h))

theorem resultTapes_off_outputs (ps : List (Polynomial Nat)) (slots : Layout ps ↪ K)
    (base : K → List Bool) (n : Nat) (k : K)
    (hk : ∀ i : Output ps, slots (outputSlot ps i) ≠ k) :
    resultTapes ps slots base n k = base k := by
  induction ps generalizing base with
  | nil => rfl
  | cons p ps ih =>
    change resultTapes ps (tailSlots slots)
      (MachineHorner.resultTapes (headSlots slots) base (p.eval n)) n k = base k
    rw [ih _ _ (fun i => hk (.inr i))]
    exact Function.update_of_ne (hk (.inl ())).symm _ _

theorem resultTapes_input (ps : List (Polynomial Nat)) (slots : Layout ps ↪ K)
    (base : K → List Bool) (n : Nat) :
    resultTapes ps slots base n (slots (.inl ())) = base (slots (.inl ())) := by
  apply resultTapes_off_outputs
  intro i hi
  exact outputSlot_ne_input ps i (slots.injective hi)

theorem resultTapes_output (ps : List (Polynomial Nat)) (slots : Layout ps ↪ K)
    (base : K → List Bool) (n : Nat) (clean : Clean ps slots base) (i : Output ps) :
    resultTapes ps slots base n (slots (outputSlot ps i)) =
      encodeWord ((outputPolynomial ps i).eval n) := by
  induction ps generalizing base with
  | nil => exact Empty.elim i
  | cons p ps ih =>
    cases i with
    | inl i =>
      change resultTapes ps (tailSlots slots)
        (MachineHorner.resultTapes (headSlots slots) base (p.eval n)) n
          (headSlots slots (.inl 3)) = encodeWord (p.eval n)
      rw [resultTapes_off_outputs _ _ _ _ _
        (fun i => (head_output_ne_tail slots (outputSlot ps i)).symm)]
      rw [MachineHorner.resultTapes_output]
      simp only [headSlots_output, clean (.inl (.inl 2)), List.append_nil]
    | inr i =>
      exact ih (tailSlots slots) _ (tail_clean_after_head slots base clean _) i

theorem resultTapes_work_clear (ps : List (Polynomial Nat)) (slots : Layout ps ↪ K)
    (base : K → List Bool) (n : Nat) (clean : Clean ps slots base) (k : Private ps)
    (hk : ∀ i : Output ps, outputSlot ps i ≠ .inr k) :
    resultTapes ps slots base n (slots (.inr k)) = [] := by
  rw [resultTapes_off_outputs _ _ _ _ _ (fun i h => hk i (slots.injective h))]
  exact clean k

theorem resultTapes_ambient (ps : List (Polynomial Nat)) (slots : Layout ps ↪ K)
    (base : K → List Bool) (n : Nat) (k : K) (hk : k ∉ Set.range slots) :
    resultTapes ps slots base n k = base k := by
  apply resultTapes_off_outputs
  intro i hi
  exact hk ⟨outputSlot ps i, hi⟩

abbrev ProgramLabel (ps : List (Polynomial Nat)) := Unit ⊕ Label ps

def program (ps : List (Polynomial Nat)) (slots : Layout ps ↪ K) :
    ProgramLabel ps → TM2.Stmt (fun _ : K => Bool) (ProgramLabel ps) (MachineHorner.State σ)
  | .inl _ => .load (fun state => (state.1, none))
      (Reduction.MachineTransfer.exitAt (slots (.inl ())) (entry ps Sum.inr none))
  | .inr l => statement ps slots Sum.inr none l

theorem programTrace (ps : List (Polynomial Nat)) (slots : Layout ps ↪ K)
    (base : K → List Bool) (n : Nat)
    (hinput : base (slots (.inl ())) = encodeWord n) (clean : Clean ps slots base)
    (ambient : σ) (register : Option Bool) :
    (advance (TM2.step (program ps slots)))^[1 + steps ps n]
      (some ⟨some (.inl ()), ((ambient, ()), register), base⟩) =
      some ⟨none, ((ambient, ()), none), resultTapes ps slots base n⟩ := by
  have first : (advance (TM2.step (program ps slots)))^[1]
      (some ⟨some (.inl ()), ((ambient, ()), register), base⟩) =
      some ⟨entry ps Sum.inr none, ((ambient, ()), none), base⟩ := by
    change some (TM2.stepAux (program ps slots (.inl ())) _ _) = _
    simp only [program, TM2.stepAux]
    cases h : entry ps (Sum.inr : Label ps → ProgramLabel ps) none <;> rfl
  have rest := trace ps slots Sum.inr none (program ps slots) (fun _ => rfl)
    base n hinput clean ambient
  exact trace_trans _ first rest

def programInTime (ps : List (Polynomial Nat)) (slots : Layout ps ↪ K)
    (base : K → List Bool) (n : Nat)
    (hinput : base (slots (.inl ())) = encodeWord n) (clean : Clean ps slots base)
    (ambient : σ) (register : Option Bool) :
    StateTransition.EvalsToInTime (TM2.step (program ps slots))
      ⟨some (.inl ()), ((ambient, ()), register), base⟩
      (some ⟨none, ((ambient, ()), none), resultTapes ps slots base n⟩)
      ((1 + timePolynomial ps).eval n) where
  steps := 1 + steps ps n
  evals_in_steps := programTrace ps slots base n hinput clean ambient register
  steps_le_m := by
    simp only [Polynomial.eval_add, Polynomial.eval_one]
    exact Nat.add_le_add_left (steps_le_timePolynomial ps n) 1

abbrev Tape (ps : List (Polynomial Nat)) := Layout ps ⊕ Unit

def machineSlots (ps : List (Polynomial Nat)) : Layout ps ↪ Tape ps :=
  ⟨Sum.inl, Sum.inl_injective⟩

def machine (ps : List (Polynomial Nat)) : FinTM2 where
  K := Tape ps
  k₀ := .inr ()
  k₁ := .inr ()
  Γ _ := Bool
  Λ := ProgramLabel ps
  main := .inl ()
  σ := MachineHorner.State Unit
  initialState := (((), ()), none)
  m := program ps (machineSlots ps)

theorem machine_rawInput_preserved (ps : List (Polynomial Nat))
    (base : Tape ps → List Bool) (n : Nat) :
    resultTapes ps (machineSlots ps) base n (.inr ()) = base (.inr ()) := by
  apply resultTapes_ambient
  rintro ⟨i, hi⟩
  cases hi

def machineInTime (ps : List (Polynomial Nat)) (base : Tape ps → List Bool) (n : Nat)
    (hinput : base ((machineSlots ps) (.inl ())) = encodeWord n)
    (clean : Clean ps (machineSlots ps) base) (register : Option Bool) :
    StateTransition.EvalsToInTime (machine ps).step
      ⟨some (.inl ()), (((), ()), register), base⟩
      (some ⟨none, (((), ()), none), resultTapes ps (machineSlots ps) base n⟩)
      ((1 + timePolynomial ps).eval n) :=
  programInTime ps (machineSlots ps) base n hinput clean () register

inductive Clock
  | witness | horizon | capacity | width | frameWidth | inputCount
  deriving DecidableEq

/-- A list enumerating every element of the type, which are all zero-argument constructors. (Generated by the `Fintype` deriving handler.)-/
protected abbrev Clock.enumList : List Clock := [.witness, .horizon, .capacity, .width,
  .frameWidth, .inputCount]

protected theorem Clock.enumList_getElem?_ctorIdx_eq (x : Clock) :
    Clock.enumList[x.ctorIdx]? = some x := by
  cases x <;> rfl

protected theorem Clock.enumList_nodup : Clock.enumList.Nodup := by decide

instance : Fintype Clock where
  elems := ⟨Clock.enumList, Clock.enumList_nodup⟩
  complete x := by cases x <;> decide

def clockPolynomial (V : NPVerifier) : Clock → Polynomial Nat
  | .witness => V.witnessBound
  | .horizon => Bounds.horizonPolynomial V.computation.time V.witnessBound
  | .capacity => Bounds.capacityPolynomial V.computation.time V.witnessBound
      (Runtime.programPushBound V.computation.tm)
  | .width => VerifierCircuit.widthPolynomial V
  | .frameWidth => VerifierCircuit.widthPolynomial V + 1
  | .inputCount => WitnessEncoding.freeInputPolynomial V.witnessBound

def clockPolynomials (V : NPVerifier) : List (Polynomial Nat) :=
  [clockPolynomial V .witness, clockPolynomial V .horizon, clockPolynomial V .capacity,
    clockPolynomial V .width, clockPolynomial V .frameWidth, clockPolynomial V .inputCount]

def clockOutput (V : NPVerifier) : Clock → Output (clockPolynomials V)
  | .witness => .inl ()
  | .horizon => .inr (.inl ())
  | .capacity => .inr (.inr (.inl ()))
  | .width => .inr (.inr (.inr (.inl ())))
  | .frameWidth => .inr (.inr (.inr (.inr (.inl ()))))
  | .inputCount => .inr (.inr (.inr (.inr (.inr (.inl ())))))

def clockValue (V : NPVerifier) (n : Nat) : Clock → Nat
  | .witness => V.witnessBound.eval n
  | .horizon => V.horizon n
  | .capacity => VerifierCircuit.capacity V n
  | .width => (VerifierCircuit.indexing V).width (VerifierCircuit.capacity V n)
  | .frameWidth => (VerifierCircuit.indexing V).width (VerifierCircuit.capacity V n) + 1
  | .inputCount => 2 * V.witnessBound.eval n + 1

@[simp] theorem outputPolynomial_clockOutput (V : NPVerifier) (c : Clock) :
    outputPolynomial (clockPolynomials V) (clockOutput V c) = clockPolynomial V c := by
  cases c <;> rfl

theorem clockPolynomial_eval (V : NPVerifier) (n : Nat) (c : Clock) :
    (clockPolynomial V c).eval n = clockValue V n c := by
  cases c <;>
    simp [clockPolynomial, clockValue, VerifierCircuit.widthPolynomial,
      VerifierCircuit.capacity, NPVerifier.horizon, ConfigIndex.Indexing.width, Nat.mul_comm]

theorem clockResult_output (V : NPVerifier) (slots : Layout (clockPolynomials V) ↪ K)
    (base : K → List Bool) (n : Nat) (clean : Clean (clockPolynomials V) slots base)
    (c : Clock) :
    resultTapes (clockPolynomials V) slots base n
      (slots (outputSlot (clockPolynomials V) (clockOutput V c))) =
      encodeWord (clockValue V n c) := by
  rw [resultTapes_output _ _ _ _ clean, outputPolynomial_clockOutput, clockPolynomial_eval]

def clocksInTime (V : NPVerifier) (slots : Layout (clockPolynomials V) ↪ K)
    (base : K → List Bool) (n : Nat)
    (hinput : base (slots (.inl ())) = encodeWord n)
    (clean : Clean (clockPolynomials V) slots base) (ambient : σ) (register : Option Bool) :
    StateTransition.EvalsToInTime (TM2.step (program (clockPolynomials V) slots))
      ⟨some (.inl ()), ((ambient, ()), register), base⟩
      (some ⟨none, ((ambient, ()), none), resultTapes (clockPolynomials V) slots base n⟩)
      ((1 + timePolynomial (clockPolynomials V)).eval n) :=
  programInTime (clockPolynomials V) slots base n hinput clean ambient register

end UniqueGamesTheorem.Foundations.Complexity.CookLevin.ClockPreparation

end

end

end OAI