File size: 24,556 Bytes
8df23f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import Mathlib.Tactic.DeriveFintype
import OAI.Computability.BinPacking.Arithmetic.GraphTallyMachine

namespace OAI

namespace BinPackingGap.ExtensionShapeMachine

open BinaryEncoding
open BinPackingCompleteness.BinaryNameMachine (finalDigit canonical)

inductive Kind
  | extension
  | assignments
  deriving DecidableEq

protected abbrev Kind.enumList : List Kind := [.extension, .assignments]

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

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

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

inductive Continuation
  | bins
  | numerator
  | denominator
  | fixed
  | assignment
  deriving DecidableEq

protected abbrev Continuation.enumList : List Continuation := [.bins, .numerator, .denominator,
  .fixed, .assignment]

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

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

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

inductive Mode
  | name (next : Continuation) (last : Bool)
  | digit (next : Continuation)
  | items
  | fixedList
  | fixedOption
  | assignments
  | done
  | reject
  deriving DecidableEq, Fintype

def start : Kind → Mode
  | .extension => .name .bins true
  | .assignments => .assignments

def afterName : Continuation → Mode
  | .bins => .items
  | .numerator => .name .denominator true
  | .denominator => .items
  | .fixed => .fixedList
  | .assignment => .assignments

def nextMode : Mode → Bool → Mode
  | .name next _, true => .digit next
  | .name next last, false => if last then afterName next else .reject
  | .digit next, bit => .name next bit
  | .items, true => .name .numerator true
  | .items, false => .fixedList
  | .fixedList, true => .fixedOption
  | .fixedList, false => .done
  | .fixedOption, true => .name .fixed true
  | .fixedOption, false => .fixedList
  | .assignments, true => .name .assignment true
  | .assignments, false => .done
  | .done, _ => .reject
  | .reject, _ => .reject

def run : Mode → List Bool → Mode
  | mode, [] => mode
  | mode, bit :: rest => run (nextMode mode bit) rest

@[simp] theorem run_reject (input : List Bool) : run .reject input = .reject := by
  induction input with
  | nil => rfl
  | cons bit input ih => simpa only [run, nextMode] using ih

theorem run_done_iff (input : List Bool) : run .done input = .done ↔ input = [] := by
  cases input with
  | nil => simp [run]
  | cons bit rest => simp [run, nextMode]

private theorem finalDigit_true_iff (bits : List Bool) :
    finalDigit bits (some true) = some true ↔ canonical bits = true := by
  cases bits with
  | nil => simp [finalDigit, canonical]
  | cons bit bits =>
      simp only [finalDigit, canonical]
      have hsome : ∀ (ds : List Bool) (b : Bool),
          ∃ last, finalDigit ds (some b) = some last := by
        intro ds
        induction ds with
        | nil => intro b; exact ⟨b, rfl⟩
        | cons d ds ih => intro b; exact ih d
      obtain ⟨last, hlast⟩ := hsome bits bit
      rw [hlast]
      simp

theorem run_frame (next : Continuation) (bits rest : List Bool) (last : Bool)
    (valid : finalDigit bits (some last) = some true) :
    run (.name next last) (BinPackingCompleteness.BinaryEncoding.frame bits ++ rest) =
      run (afterName next) rest := by
  induction bits generalizing last with
  | nil =>
      have hlast : last = true := by simpa [finalDigit] using valid
      subst last
      simp [BinPackingCompleteness.BinaryEncoding.frame, run, nextMode]
  | cons bit bits ih =>
      have hvalid : finalDigit bits (some bit) = some true := valid
      simpa only [BinPackingCompleteness.BinaryEncoding.frame, List.cons_append, run, nextMode]
        using ih bit hvalid

theorem run_name (next : Continuation) (name : Nat) (rest : List Bool) :
    run (.name next true) (natBits name ++ rest) = run (afterName next) rest := by
  apply run_frame
  exact (finalDigit_true_iff name.bits).mpr
    (BinPackingCompleteness.BinaryParsing.canonical_nat_bits name)

theorem run_items (r : RawInstance) (rest : List Bool) :
    run .items (rawInstanceBits r ++ rest) = run .fixedList rest := by
  induction r with
  | nil => simp [rawInstanceBits, listBits, run, nextMode]
  | cons q r ih =>
      simp only [rawInstanceBits, listBits, List.cons_append, run, nextMode,
        pairBits, List.append_assoc, run_name, afterName]
      exact ih

theorem run_fixed (fixed : List (Option Nat)) (rest : List Bool) :
    run .fixedList (listBits (optionBits natBits) fixed ++ rest) = run .done rest := by
  induction fixed with
  | nil => simp [listBits, run, nextMode]
  | cons value fixed ih =>
      cases value with
      | none => simpa only [listBits, optionBits, List.cons_append, List.singleton_append,
          List.nil_append, run, nextMode] using ih
      | some n =>
          simpa only [listBits, optionBits, List.cons_append, List.append_assoc,
            run, nextMode, run_name, afterName] using ih

theorem run_assignments (a : List Nat) (rest : List Bool) :
    run .assignments (assignmentBits a ++ rest) = run .done rest := by
  induction a with
  | nil => simp [assignmentBits, listBits, run, nextMode]
  | cons n a ih =>
      simpa only [assignmentBits, listBits, List.cons_append, List.append_assoc,
        run, nextMode, run_name, afterName] using ih

theorem run_extension (bins : Nat) (r : RawInstance) (fixed : List (Option Nat)) :
    run (start .extension) (extensionBits bins r fixed) = .done := by
  simp only [start, extensionBits, List.append_assoc, run_name, afterName,
    run_items]
  simpa only [List.append_nil, run] using run_fixed fixed []

theorem run_assignmentBits (a : List Nat) :
    run (start .assignments) (assignmentBits a) = .done := by
  simpa only [start, List.append_nil, run] using run_assignments a []

theorem frame_inverse (next : Continuation) (last : Bool) (input : List Bool)
    (accepted : run (.name next last) input = .done) :
    ∃ bits rest, input = BinPackingCompleteness.BinaryEncoding.frame bits ++ rest ∧
      finalDigit bits (some last) = some true ∧
      run (afterName next) rest = .done := by
  induction input using List.twoStepInduction generalizing last with
  | nil => simp [run] at accepted
  | singleton flag =>
      cases flag with
      | false =>
          cases last with
          | false => simp [run, nextMode] at accepted
          | true =>
              exact ⟨[], [], rfl, rfl, by simpa [run, nextMode] using accepted⟩
      | true => simp [run, nextMode] at accepted
  | cons_cons flag bit input ih _ =>
      cases flag with
      | false =>
          cases last with
          | false => simp [run, nextMode] at accepted
          | true =>
              exact ⟨[], bit :: input, rfl, rfl,
                by simpa [run, nextMode] using accepted⟩
      | true =>
          have haccepted : run (.name next bit) input = .done := by
            simpa only [run, nextMode] using accepted
          obtain ⟨bits, rest, hinput, hvalid, hrest⟩ := ih bit haccepted
          refine ⟨bit :: bits, rest, ?_, hvalid, hrest⟩
          simp [BinPackingCompleteness.BinaryEncoding.frame, hinput]

theorem name_inverse (next : Continuation) (input : List Bool)
    (accepted : run (.name next true) input = .done) :
    ∃ name rest, input = natBits name ++ rest ∧ run (afterName next) rest = .done := by
  obtain ⟨bits, rest, hinput, hvalid, hrest⟩ := frame_inverse next true input accepted
  have hcanonical := (finalDigit_true_iff bits).mp hvalid
  have hbits := (BinPackingCompleteness.BinaryParsing.canonical_iff bits).mp hcanonical
  refine ⟨BinPackingCompleteness.BinaryEncoding.bitsValue bits, rest, ?_, hrest⟩
  simpa only [natBits, BinPackingCompleteness.BinaryEncoding.nameBits, hbits] using hinput

private theorem fixed_inverse_aux (fuel : Nat) (input : List Bool)
    (enough : input.length < fuel) (accepted : run .fixedList input = .done) :
    ∃ fixed, input = listBits (optionBits natBits) fixed := by
  induction fuel generalizing input with
  | zero => omega
  | succ fuel ih =>
      cases input with
      | nil => simp [run] at accepted
      | cons flag input =>
          cases flag with
          | false =>
              have hdone : run .done input = .done := accepted
              have hempty := (run_done_iff input).mp hdone
              subst input
              exact ⟨[], rfl⟩
          | true =>
              cases input with
              | nil => simp [run, nextMode] at accepted
              | cons present input =>
                  cases present with
                  | false =>
                      have haccepted : run .fixedList input = .done := accepted
                      have henough : input.length < fuel := by
                        simp only [List.length_cons] at enough
                        omega
                      obtain ⟨fixed, hfixed⟩ := ih input henough haccepted
                      exact ⟨none :: fixed, by simp [listBits, optionBits, hfixed]⟩
                  | true =>
                      have haccepted : run (.name .fixed true) input = .done := accepted
                      obtain ⟨n, rest, hinput, hrest⟩ := name_inverse .fixed input haccepted
                      have hlength := congrArg List.length hinput
                      simp only [List.length_append] at hlength
                      have henough : rest.length < fuel := by
                        simp only [List.length_cons] at enough
                        omega
                      obtain ⟨fixed, hfixed⟩ := ih rest henough hrest
                      exact ⟨some n :: fixed, by
                        simp [listBits, optionBits, hinput, hfixed]⟩

private theorem assignments_inverse_aux (fuel : Nat) (input : List Bool)
    (enough : input.length < fuel) (accepted : run .assignments input = .done) :
    ∃ a, input = assignmentBits a := by
  induction fuel generalizing input with
  | zero => omega
  | succ fuel ih =>
      cases input with
      | nil => simp [run] at accepted
      | cons flag input =>
          cases flag with
          | false =>
              have hdone : run .done input = .done := accepted
              have hempty := (run_done_iff input).mp hdone
              subst input
              exact ⟨[], rfl⟩
          | true =>
              have haccepted : run (.name .assignment true) input = .done := accepted
              obtain ⟨n, rest, hinput, hrest⟩ := name_inverse .assignment input haccepted
              have hlength := congrArg List.length hinput
              simp only [List.length_append] at hlength
              have henough : rest.length < fuel := by
                simp only [List.length_cons] at enough
                omega
              obtain ⟨a, ha⟩ := ih rest henough hrest
              exact ⟨n :: a, by simp [assignmentBits, listBits, hinput, ha]⟩

private theorem items_inverse_aux (fuel : Nat) (input : List Bool)
    (enough : input.length < fuel) (accepted : run .items input = .done) :
    ∃ r fixed, input = rawInstanceBits r ++ listBits (optionBits natBits) fixed := by
  induction fuel generalizing input with
  | zero => omega
  | succ fuel ih =>
      cases input with
      | nil => simp [run] at accepted
      | cons flag input =>
          cases flag with
          | false =>
              have haccepted : run .fixedList input = .done := accepted
              obtain ⟨fixed, hfixed⟩ :=
                fixed_inverse_aux (input.length + 1) input (by omega) haccepted
              exact ⟨[], fixed, by simp [rawInstanceBits, listBits, hfixed]⟩
          | true =>
              have haccepted : run (.name .numerator true) input = .done := accepted
              obtain ⟨num, afterNum, hnum, hafterNum⟩ :=
                name_inverse .numerator input haccepted
              obtain ⟨den, rest, hden, hrest⟩ :=
                name_inverse .denominator afterNum hafterNum
              have hlength := congrArg List.length hnum
              have hlength' := congrArg List.length hden
              simp only [List.length_append] at hlength hlength'
              have henough : rest.length < fuel := by
                simp only [List.length_cons] at enough
                omega
              obtain ⟨r, fixed, htail⟩ := ih rest henough hrest
              refine ⟨(num, den) :: r, fixed, ?_⟩
              simp [rawInstanceBits, listBits, pairBits, hnum, hden, htail, List.append_assoc]

theorem run_extension_iff (input : List Bool) :
    run (start .extension) input = .done ↔
      ∃ bins r fixed, input = extensionBits bins r fixed := by
  constructor
  · intro accepted
    obtain ⟨bins, rest, hinput, hrest⟩ := name_inverse .bins input accepted
    obtain ⟨r, fixed, htail⟩ := items_inverse_aux (rest.length + 1) rest (by omega) hrest
    exact ⟨bins, r, fixed, by simp [extensionBits, hinput, htail, List.append_assoc]⟩
  · rintro ⟨bins, r, fixed, rfl⟩
    exact run_extension bins r fixed

theorem run_assignments_iff (input : List Bool) :
    run (start .assignments) input = .done ↔ ∃ a, input = assignmentBits a := by
  constructor
  · intro accepted
    exact assignments_inverse_aux (input.length + 1) input (by omega) accepted
  · rintro ⟨a, rfl⟩
    exact run_assignmentBits a

def accepts (kind : Kind) (input : List Bool) : Bool :=
  decide (run (start kind) input = .done)

def decodeAccepts : Kind → List Bool → Bool
  | .extension, input => (decodeExtension input).isSome
  | .assignments, input => (decodeAssignments input).isSome

theorem accepts_eq_decode (kind : Kind) (input : List Bool) :
    accepts kind input = decodeAccepts kind input := by
  cases kind with
  | extension =>
      cases parsed : decodeExtension input with
      | none =>
          have rejected : run (start .extension) input ≠ .done := by
            intro accepted
            obtain ⟨bins, r, fixed, encoded⟩ := (run_extension_iff input).mp accepted
            have roundtrip := decodeExtension_extensionBits bins r fixed
            rw [← encoded, parsed] at roundtrip
            cases roundtrip
          simp [accepts, decodeAccepts, parsed, rejected]
      | some value =>
          rcases value with ⟨bins, r, fixed⟩
          have encoded := decodeExtension_sound parsed
          have accepted := (run_extension_iff input).mpr ⟨bins, r, fixed, encoded⟩
          simp [accepts, decodeAccepts, parsed, accepted]
  | assignments =>
      cases parsed : decodeAssignments input with
      | none =>
          have rejected : run (start .assignments) input ≠ .done := by
            intro accepted
            obtain ⟨a, encoded⟩ := (run_assignments_iff input).mp accepted
            have roundtrip := decodeAssignments_assignmentBits a
            rw [← encoded, parsed] at roundtrip
            cases roundtrip
          simp [accepts, decodeAccepts, parsed, rejected]
      | some a =>
          have encoded := decodeAssignments_sound parsed
          have accepted := (run_assignments_iff input).mpr ⟨a, encoded⟩
          simp [accepts, decodeAccepts, parsed, accepted]

open Turing
open BinPackingGames.Foundations.Complexity
open MachineComposition
open BinPackingGames.Reduction.MachineTransfer
open BinPackingGames.Reduction.MachineSubstitution

inductive Label
  | scan
  | restore
  | finish
  deriving DecidableEq

protected abbrev Label.enumList : List Label := [.scan, .restore, .finish]

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

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

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

abbrev Alphabet (_ : Fin 3) := Bool
abbrev State := Mode × Option Bool

def instruction (kind : Kind) : Label → TM2.Stmt Alphabet Label State
  | .scan =>
      .pop 0 (fun state head => (state.1, head))
        (.branch (fun state => state.2.isSome)
          (.push 2 (fun state => state.2.getD false)
            (.load (fun state => (nextMode state.1 (state.2.getD false), none))
              (.goto fun _ => .scan)))
          (.load (fun state => (state.1, none)) (.goto fun _ => .restore)))
  | .restore => loopAt 2 1 id false .restore (some .finish)
  | .finish =>
      .push 1 (fun state => decide (state.1 = .done))
        (.load (fun _ => (start kind, none)) .halt)

abbrev machine (kind : Kind) : FinTM2 where
  K := Fin 3
  k₀ := 0
  k₁ := 1
  Γ := Alphabet
  Λ := Label
  main := .scan
  σ := State
  initialState := (start kind, none)
  m := instruction kind

def tapes (input output saved : List Bool) : Fin 3 → List Bool
  | 0 => input
  | 1 => output
  | _ => saved

def cfg (kind : Kind) (label : Option Label) (input output saved : List Bool)
    (mode : Mode) (register : Option Bool := none) : (machine kind).Cfg :=
  ⟨label, (mode, register), tapes input output saved⟩

@[simp] private theorem tapes_input (input output saved : List Bool) :
    tapes input output saved 0 = input := rfl

@[simp] private theorem tapes_output (input output saved : List Bool) :
    tapes input output saved 1 = output := rfl

@[simp] private theorem tapes_saved (input output saved : List Bool) :
    tapes input output saved 2 = saved := rfl

private theorem update_input (input output saved replacement : List Bool) :
    Function.update (tapes input output saved) 0 replacement =
      tapes replacement output saved := by
  funext k
  fin_cases k <;> rfl

private theorem update_output (input output saved replacement : List Bool) :
    Function.update (tapes input output saved) 1 replacement =
      tapes input replacement saved := by
  funext k
  fin_cases k <;> rfl

private theorem update_saved (input output saved replacement : List Bool) :
    Function.update (tapes input output saved) 2 replacement =
      tapes input output replacement := by
  funext k
  fin_cases k <;> rfl

theorem step_scan_empty (kind : Kind) (output saved : List Bool)
    (mode : Mode) (register : Option Bool) :
    (machine kind).step (cfg kind (some .scan) [] output saved mode register) =
      some (cfg kind (some .restore) [] output saved mode) := by
  change some (TM2.stepAux (instruction kind .scan) _ _) = _
  simp [instruction, cfg, TM2.stepAux, update_input]
  all_goals rfl

theorem step_scan_cons (kind : Kind) (bit : Bool) (input output saved : List Bool)
    (mode : Mode) (register : Option Bool) :
    (machine kind).step (cfg kind (some .scan) (bit :: input) output saved mode register) =
      some (cfg kind (some .scan) input output (bit :: saved) (nextMode mode bit)) := by
  change some (TM2.stepAux (instruction kind .scan) _ _) = _
  simp [instruction, cfg, TM2.stepAux, update_input, update_saved]
  all_goals rfl

theorem scanTrace (kind : Kind) (input output saved : List Bool)
    (mode : Mode) (register : Option Bool) :
    (advance (machine kind).step)^[input.length + 1]
      (some (cfg kind (some .scan) input output saved mode register)) =
      some (cfg kind (some .restore) [] output (input.reverse ++ saved) (run mode input)) := by
  induction input generalizing saved mode register with
  | nil =>
      simpa only [List.length_nil, Nat.zero_add, Function.iterate_one,
        advance_some, run, List.reverse_nil, List.nil_append] using
          step_scan_empty kind output saved mode register
  | cons bit input ih =>
      rw [List.length_cons, Function.iterate_succ_apply]
      simp only [advance_some]
      rw [step_scan_cons]
      simpa only [run, List.reverse_cons, List.append_assoc, List.singleton_append] using
        ih (bit :: saved) (nextMode mode bit) none

theorem restoreTrace (kind : Kind) (saved output : List Bool)
    (mode : Mode) (register : Option Bool) :
    (advance (machine kind).step)^[saved.length + 1]
      (some (cfg kind (some .restore) [] output saved mode register)) =
      some (cfg kind (some .finish) [] (saved.reverse ++ output) [] mode) := by
  change (nextAt (1 : Fin 3) (instruction kind))^[saved.length + 1]
    (some ⟨some .restore, (mode, register), tapes [] output saved⟩) =
    some ⟨some .finish, (mode, none), tapes [] (saved.reverse ++ output) []⟩
  have trace := transferAt_steps (2 : Fin 3) 1 (by decide) id false .restore (some .finish)
    (instruction kind) rfl (tapes [] [] []) saved output mode register
  simpa only [tapesAt, update_saved, update_output, List.map_id_fun, id_eq] using trace

theorem step_finish (kind : Kind) (output : List Bool) (mode : Mode)
    (register : Option Bool) :
    (machine kind).step (cfg kind (some .finish) [] output [] mode register) =
      some (cfg kind none [] (decide (mode = .done) :: output) [] (start kind)) := by
  change some (TM2.stepAux (instruction kind .finish) _ _) = _
  simp [instruction, cfg, TM2.stepAux, update_output]
  all_goals rfl

private theorem joinTrace {X : Type*} {f : X → X} {a b c : X} {n m : Nat}
    (first : f^[n] a = b) (second : f^[m] b = c) : f^[n + m] a = c := by
  rw [Nat.add_comm, Function.iterate_add_apply, first, second]

theorem rawTrace (kind : Kind) (input : List Bool) :
    (advance (machine kind).step)^[2 * input.length + 3]
      (some (cfg kind (some .scan) input [] [] (start kind))) =
      some (cfg kind none [] (accepts kind input :: input) [] (start kind)) := by
  have first := scanTrace kind input [] [] (start kind) none
  simp only [List.append_nil] at first
  have second := restoreTrace kind input.reverse [] (run (start kind) input) none
  simp only [List.length_reverse, List.reverse_reverse, List.append_nil] at second
  have third : (advance (machine kind).step)^[1]
      (some (cfg kind (some .finish) [] input [] (run (start kind) input))) =
      some (cfg kind none [] (accepts kind input :: input) [] (start kind)) := by
    simpa only [Function.iterate_one, advance_some, accepts] using
      step_finish kind input (run (start kind) input) none
  have time : 2 * input.length + 3 = (input.length + 1) + (input.length + 1) + 1 := by omega
  rw [time]
  exact joinTrace (joinTrace first second) third

theorem initList_eq (kind : Kind) (input : List Bool) :
    initList (machine kind) input = cfg kind (some .scan) input [] [] (start kind) := by
  unfold initList cfg
  congr 1
  funext k
  fin_cases k <;> rfl

theorem haltList_eq (kind : Kind) (output : List Bool) :
    haltList (machine kind) output = cfg kind none [] output [] (start kind) := by
  unfold haltList cfg
  congr 1
  funext k
  fin_cases k <;> rfl

theorem totalTrace (kind : Kind) (input : List Bool) :
    (advance (machine kind).step)^[2 * input.length + 3]
      (some (initList (machine kind) input)) =
      some (haltList (machine kind) (decodeAccepts kind input :: input)) := by
  rw [initList_eq, haltList_eq]
  simpa only [accepts_eq_decode] using rawTrace kind input

def outputsInTime (kind : Kind) (input : List Bool) :
    TM2OutputsInTime (machine kind) input (some (decodeAccepts kind input :: input))
      (2 * input.length + 3) where
  steps := 2 * input.length + 3
  evals_in_steps := totalTrace kind input
  steps_le_m := Nat.le_refl _

noncomputable def computableInPolyTime (kind : Kind) :
    TM2ComputableInPolyTime (id : List Bool → List Bool) (id : List Bool → List Bool)
      (fun input => decodeAccepts kind input :: input) where
  tm := machine kind
  inputAlphabet := Equiv.refl Bool
  outputAlphabet := Equiv.refl Bool
  time := 2 * Polynomial.X + 3
  outputsFun input := by
    change TM2OutputsInTime (machine kind) (input.map id)
      (some ((decodeAccepts kind input :: input).map id))
      ((2 * Polynomial.X + 3 : Polynomial Nat).eval input.length)
    simpa only [List.map_id_fun, id_eq, Polynomial.eval_add, Polynomial.eval_mul,
      Polynomial.eval_X, Polynomial.eval_ofNat] using outputsInTime kind input

theorem machine_finiteAlphabet (kind : Kind) (k : (machine kind).K) :
    Finite ((machine kind).Γ k) := by
  change Finite Bool
  infer_instance

theorem computation_finiteAlphabet (kind : Kind) :
    MachineFiniteAlphabet.FiniteAlphabet (computableInPolyTime kind).tm :=
  machine_finiteAlphabet kind

end BinPackingGap.ExtensionShapeMachine

end OAI