File size: 49,687 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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
import Mathlib.Data.Fin.VecNotation
import Mathlib.Tactic.FinCases
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.Positivity
import OAI.Computability.BinPacking.Arithmetic.BinaryTallyMachine

namespace OAI

namespace BinPackingGap.BinaryMulMachine

open Turing
open BinPackingGames.Foundations.Complexity
open BinPackingGames.Reduction

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

abbrev Alphabet (_ : K) := Bool
abbrev State (A : Type) := BinaryAddMachine.State A

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

inductive Label
  | copyLeft | forkLeft | copyRight | forkRight | guard
  | copyAddend | forkAddend | add | restoreSum | shift
  | clearShift | reverseOutput | output
  deriving DecidableEq

protected abbrev Label.enumList : List Label := [.copyLeft, .forkLeft, .copyRight, .forkRight,
  .guard, .copyAddend, .forkAddend, .add, .restoreSum, .shift, .clearShift, .reverseOutput,
  .output]

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

def statement (slots : Fin 9 ↪ K) (labels : Label → Λ) (exit : Option Λ) :
    Label → TM2.Stmt (Alphabet (K := K)) Λ (State A)
  | .copyLeft => MachineTransfer.loopAt (slots 0) (slots 8) id false
      (labels .copyLeft) (some (labels .forkLeft))
  | .forkLeft => MachineCopy.forkLoop (slots 8) (slots 0) (slots 3) false
      (labels .forkLeft) (some (labels .copyRight))
  | .copyRight => MachineTransfer.loopAt (slots 1) (slots 8) id false
      (labels .copyRight) (some (labels .forkRight))
  | .forkRight => MachineCopy.forkLoop (slots 8) (slots 1) (slots 4) false
      (labels .forkRight) (some (labels .guard))
  | .guard => .pop (slots 4) (fun state head => (state.1, head))
      (.branch (fun state => state.2.isSome)
        (.branch (fun state => state.2.getD false)
          (.load (fun state => clean state.1.1.1) (.goto fun _ => labels .copyAddend))
          (.load (fun state => clean state.1.1.1) (.goto fun _ => labels .shift)))
        (.load (fun state => clean state.1.1.1) (.goto fun _ => labels .clearShift)))
  | .copyAddend => MachineTransfer.loopAt (slots 3) (slots 8) id false
      (labels .copyAddend) (some (labels .forkAddend))
  | .forkAddend => MachineCopy.forkLoop (slots 8) (slots 3) (slots 6) false
      (labels .forkAddend) (some (labels .add))
  | .add => BinaryAddMachine.addLoop (slots 6) (slots 5) (slots 7)
      (labels .add) (some (labels .restoreSum))
  | .restoreSum => MachineTransfer.loopAt (slots 7) (slots 5) id false
      (labels .restoreSum) (some (labels .shift))
  | .shift => .peek (slots 3) (fun state head => (state.1, head))
      (.branch (fun state => state.2.isSome)
        (.push (slots 3) (fun _ => false)
          (.load (fun state => clean state.1.1.1) (.goto fun _ => labels .guard)))
        (.load (fun state => clean state.1.1.1) (.goto fun _ => labels .guard)))
  | .clearShift => MachineDrain.drain (slots 3) (labels .clearShift)
      (some (labels .reverseOutput))
  | .reverseOutput => MachineTransfer.loopAt (slots 5) (slots 7) id false
      (labels .reverseOutput) (some (labels .output))
  | .output => MachineTransfer.loopAt (slots 7) (slots 2) id false
      (labels .output) exit

def program (slots : Fin 9 ↪ K) : Label → TM2.Stmt (Alphabet (K := K)) Label (State A) :=
  statement slots id none

def machine : FinTM2 where
  K := Fin 9
  k₀ := 0
  k₁ := 2
  Γ _ := Bool
  Λ := Label
  main := .copyLeft
  σ := State Unit
  initialState := clean ()
  m := program (Function.Embedding.refl (Fin 9))

noncomputable def framedTapes (slots : Fin 9 ↪ K) (base : K → List Bool)
    (words : Fin 9 → List Bool) : K → List Bool := fun k =>
  if h : ∃ i, slots i = k then words (Classical.choose h) else base k

@[simp] theorem framedTapes_slot (slots : Fin 9 ↪ K) (base : K → List Bool)
    (words : Fin 9 → List Bool) (i : Fin 9) :
    framedTapes slots base words (slots i) = words i := by
  unfold framedTapes
  rw [dite_eq_left ⟨i, rfl⟩]
  congr 1
  exact slots.injective (Classical.choose_spec (show ∃ j, slots j = slots i from ⟨i, rfl⟩))

theorem framedTapes_other (slots : Fin 9 ↪ K) (base : K → List Bool)
    (words : Fin 9 → List Bool) (k : K) (hk : ¬ ∃ i, slots i = k) :
    framedTapes slots base words k = base k := by
  simp [framedTapes, hk]

theorem framedTapes_update (slots : Fin 9 ↪ K) (base : K → List Bool)
    (words : Fin 9 → List Bool) (i : Fin 9) (word : List Bool) :
    Function.update (framedTapes slots base words) (slots i) word =
      framedTapes slots base (Function.update words i word) := by
  funext k
  by_cases hk : ∃ j, slots j = k
  · obtain ⟨j, rfl⟩ := hk
    by_cases hji : j = i
    · subst j; simp
    · simp [Function.update_of_ne, hji, slots.injective.ne hji]
  · have hki : k ≠ slots i := fun h => hk ⟨i, h.symm⟩
    simp [Function.update_of_ne hki, framedTapes_other slots base _ k hk]

noncomputable def tapes (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right output shifted remaining accumulator addend reversed scratch : List Bool) :
    K → List Bool :=
  framedTapes slots base ![left, right, output, shifted, remaining, accumulator,
    addend, reversed, scratch]

def loopSteps (xs : List Bool) : List Bool → List Bool → Nat
  | [], acc => xs.length + 2 * acc.length + 4
  | false :: ys, acc => loopSteps (BinaryArithmetic.shift xs) ys acc + 2
  | true :: ys, acc =>
      loopSteps (BinaryArithmetic.shift xs) ys (BinaryArithmetic.addCarry xs acc false) +
        (2 * xs.length + max xs.length acc.length +
          (BinaryArithmetic.addCarry xs acc false).length + 6)

def steps (xs ys : List Bool) : Nat :=
  loopSteps xs ys [] + (2 * (ys.length + 1) + 2 * (xs.length + 1))

@[simp] theorem tapes_slot (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right output shifted remaining accumulator addend reversed scratch : List Bool) (i : Fin 9) :
    tapes slots base left right output shifted remaining accumulator addend reversed scratch (slots i) =
      ![left, right, output, shifted, remaining, accumulator, addend, reversed, scratch] i :=
  framedTapes_slot slots base _ i

@[simp] private theorem tapes_update0 (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right output shifted remaining accumulator addend reversed scratch replacement : List Bool) :
    Function.update (tapes slots base left right output shifted remaining accumulator addend reversed scratch)
      (slots 0) replacement =
      tapes slots base replacement right output shifted remaining accumulator addend reversed scratch := by
  unfold tapes
  rw [framedTapes_update]
  congr 1
  funext j
  fin_cases j <;> simp

@[simp] private theorem tapes_update1 (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right output shifted remaining accumulator addend reversed scratch replacement : List Bool) :
    Function.update (tapes slots base left right output shifted remaining accumulator addend reversed scratch)
      (slots 1) replacement =
      tapes slots base left replacement output shifted remaining accumulator addend reversed scratch := by
  unfold tapes
  rw [framedTapes_update]
  congr 1
  funext j
  fin_cases j <;> simp

@[simp] private theorem tapes_update2 (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right output shifted remaining accumulator addend reversed scratch replacement : List Bool) :
    Function.update (tapes slots base left right output shifted remaining accumulator addend reversed scratch)
      (slots 2) replacement =
      tapes slots base left right replacement shifted remaining accumulator addend reversed scratch := by
  unfold tapes
  rw [framedTapes_update]
  congr 1
  funext j
  fin_cases j <;> simp

@[simp] private theorem tapes_update3 (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right output shifted remaining accumulator addend reversed scratch replacement : List Bool) :
    Function.update (tapes slots base left right output shifted remaining accumulator addend reversed scratch)
      (slots 3) replacement =
      tapes slots base left right output replacement remaining accumulator addend reversed scratch := by
  unfold tapes
  rw [framedTapes_update]
  congr 1
  funext j
  fin_cases j <;> simp

@[simp] private theorem tapes_update4 (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right output shifted remaining accumulator addend reversed scratch replacement : List Bool) :
    Function.update (tapes slots base left right output shifted remaining accumulator addend reversed scratch)
      (slots 4) replacement =
      tapes slots base left right output shifted replacement accumulator addend reversed scratch := by
  unfold tapes
  rw [framedTapes_update]
  congr 1
  funext j
  fin_cases j <;> simp

@[simp] private theorem tapes_update5 (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right output shifted remaining accumulator addend reversed scratch replacement : List Bool) :
    Function.update (tapes slots base left right output shifted remaining accumulator addend reversed scratch)
      (slots 5) replacement =
      tapes slots base left right output shifted remaining replacement addend reversed scratch := by
  unfold tapes
  rw [framedTapes_update]
  congr 1
  funext j
  fin_cases j <;> simp

@[simp] private theorem tapes_update6 (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right output shifted remaining accumulator addend reversed scratch replacement : List Bool) :
    Function.update (tapes slots base left right output shifted remaining accumulator addend reversed scratch)
      (slots 6) replacement =
      tapes slots base left right output shifted remaining accumulator replacement reversed scratch := by
  unfold tapes
  rw [framedTapes_update]
  congr 1
  funext j
  fin_cases j <;> simp

@[simp] private theorem tapes_update7 (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right output shifted remaining accumulator addend reversed scratch replacement : List Bool) :
    Function.update (tapes slots base left right output shifted remaining accumulator addend reversed scratch)
      (slots 7) replacement =
      tapes slots base left right output shifted remaining accumulator addend replacement scratch := by
  unfold tapes
  rw [framedTapes_update]
  congr 1
  funext j
  fin_cases j <;> simp

@[simp] private theorem tapes_update8 (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right output shifted remaining accumulator addend reversed scratch replacement : List Bool) :
    Function.update (tapes slots base left right output shifted remaining accumulator addend reversed scratch)
      (slots 8) replacement =
      tapes slots base left right output shifted remaining accumulator addend reversed replacement := by
  unfold tapes
  rw [framedTapes_update]
  congr 1
  funext j
  fin_cases j <;> simp

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

section Traces

variable (slots : Fin 9 ↪ K) (labels : Label → Λ) (exit : Option Λ)
variable (p : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
variable (atLabels : ∀ l, p (labels l) = statement slots labels exit l)
variable (base : K → List Bool) (left right out : List Bool) (ambient : A)

include atLabels

private theorem copyInputsTrace :
    (MachineComposition.advance (TM2.step p))^[2 * (right.length + 1) + 2 * (left.length + 1)]
      (some ⟨some (labels .copyLeft), clean ambient,
        tapes slots base left right out [] [] [] [] [] []⟩) =
      some ⟨some (labels .guard), clean ambient,
        tapes slots base left right out left right [] [] [] []⟩ := by
  have hleft :
      (MachineComposition.advance (TM2.step p))^[2 * (left.length + 1)]
        (some ⟨some (labels .copyLeft), clean ambient,
          tapes slots base left right out [] [] [] [] [] []⟩) =
        some ⟨some (labels .copyRight), clean ambient,
          tapes slots base left right out left [] [] [] [] []⟩ := by
    simpa [clean] using MachineCopy.copyTrace (slots 0) (slots 3) (slots 8)
      (slots.injective.ne (by decide)) (slots.injective.ne (by decide))
      (slots.injective.ne (by decide)) false (labels .copyLeft) (labels .forkLeft)
      (some (labels .copyRight)) p (atLabels .copyLeft) (atLabels .forkLeft)
      (tapes slots base left right out [] [] [] [] [] []) (by simp)
      ((ambient, false), none) none
  have hright :
      (MachineComposition.advance (TM2.step p))^[2 * (right.length + 1)]
        (some ⟨some (labels .copyRight), clean ambient,
          tapes slots base left right out left [] [] [] [] []⟩) =
        some ⟨some (labels .guard), clean ambient,
          tapes slots base left right out left right [] [] [] []⟩ := by
    simpa [clean] using MachineCopy.copyTrace (slots 1) (slots 4) (slots 8)
      (slots.injective.ne (by decide)) (slots.injective.ne (by decide))
      (slots.injective.ne (by decide)) false (labels .copyRight) (labels .forkRight)
      (some (labels .guard)) p (atLabels .copyRight) (atLabels .forkRight)
      (tapes slots base left right out left [] [] [] [] []) (by simp)
      ((ambient, false), none) none
  exact composeTrace hleft hright

private theorem guardEmptyTrace (xs acc : List Bool) :
    (MachineComposition.advance (TM2.step p))^[1]
      (some ⟨some (labels .guard), clean ambient,
        tapes slots base left right out xs [] acc [] [] []⟩) =
      some ⟨some (labels .clearShift), clean ambient,
        tapes slots base left right out xs [] acc [] [] []⟩ := by
  change some (TM2.stepAux (p (labels .guard)) _ _) = _
  rw [atLabels .guard]
  simp [statement, TM2.stepAux, clean]

private theorem guardConsTrace (xs ys acc : List Bool) (bit : Bool) :
    (MachineComposition.advance (TM2.step p))^[1]
      (some ⟨some (labels .guard), clean ambient,
        tapes slots base left right out xs (bit :: ys) acc [] [] []⟩) =
      some ⟨some (labels (if bit then .copyAddend else .shift)), clean ambient,
        tapes slots base left right out xs ys acc [] [] []⟩ := by
  change some (TM2.stepAux (p (labels .guard)) _ _) = _
  rw [atLabels .guard]
  cases bit <;> simp [statement, TM2.stepAux, clean]

private theorem shiftTrace (xs ys acc : List Bool) :
    (MachineComposition.advance (TM2.step p))^[1]
      (some ⟨some (labels .shift), clean ambient,
        tapes slots base left right out xs ys acc [] [] []⟩) =
      some ⟨some (labels .guard), clean ambient,
        tapes slots base left right out (BinaryArithmetic.shift xs) ys acc [] [] []⟩ := by
  change some (TM2.stepAux (p (labels .shift)) _ _) = _
  rw [atLabels .shift]
  cases xs <;> simp [statement, TM2.stepAux, clean, BinaryArithmetic.shift]

private theorem addPhaseTrace (xs ys acc : List Bool) :
    (MachineComposition.advance (TM2.step p))^[
        ((BinaryArithmetic.addCarry xs acc false).length + 1) +
          ((max xs.length acc.length + 1) + 2 * (xs.length + 1))]
      (some ⟨some (labels .copyAddend), clean ambient,
        tapes slots base left right out xs ys acc [] [] []⟩) =
      some ⟨some (labels .shift), clean ambient,
        tapes slots base left right out xs ys (BinaryArithmetic.addCarry xs acc false)
          [] [] []⟩ := by
  have hcopy :
      (MachineComposition.advance (TM2.step p))^[2 * (xs.length + 1)]
        (some ⟨some (labels .copyAddend), clean ambient,
          tapes slots base left right out xs ys acc [] [] []⟩) =
        some ⟨some (labels .add), clean ambient,
          tapes slots base left right out xs ys acc xs [] []⟩ := by
    simpa [clean] using MachineCopy.copyTrace (slots 3) (slots 6) (slots 8)
      (slots.injective.ne (by decide)) (slots.injective.ne (by decide))
      (slots.injective.ne (by decide)) false (labels .copyAddend) (labels .forkAddend)
      (some (labels .add)) p (atLabels .copyAddend) (atLabels .forkAddend)
      (tapes slots base left right out xs ys acc [] [] []) (by simp)
      ((ambient, false), none) none
  have hadd :
      (MachineComposition.advance (TM2.step p))^[max xs.length acc.length + 1]
        (some ⟨some (labels .add), clean ambient,
          tapes slots base left right out xs ys acc xs [] []⟩) =
        some ⟨some (labels .restoreSum), clean ambient,
          tapes slots base left right out xs ys [] []
            (BinaryArithmetic.addCarry xs acc false).reverse []⟩ := by
    simpa [BinaryAddMachine.tapes, BinaryAddMachine.clean, BinaryAddMachine.state, clean]
      using BinaryAddMachine.addTrace (slots 6) (slots 5) (slots 7)
        (slots.injective.ne (by decide)) (slots.injective.ne (by decide))
        (slots.injective.ne (by decide)) (labels .add) (some (labels .restoreSum))
        p (atLabels .add) (tapes slots base left right out xs ys acc xs [] [])
        ambient xs acc [] false none none
  have hrestore :
      (MachineComposition.advance (TM2.step p))^[(BinaryArithmetic.addCarry xs acc false).length + 1]
        (some ⟨some (labels .restoreSum), clean ambient,
          tapes slots base left right out xs ys [] []
            (BinaryArithmetic.addCarry xs acc false).reverse []⟩) =
        some ⟨some (labels .shift), clean ambient,
          tapes slots base left right out xs ys (BinaryArithmetic.addCarry xs acc false)
            [] [] []⟩ := by
    have hnext : MachineTransfer.nextAt (Γ := fun _ : K => Bool) (slots 5) p =
        MachineComposition.advance (TM2.step p) := rfl
    simpa [MachineTransfer.tapesAt, hnext, clean]
      using MachineTransfer.transferAt_fromTapes (Γ := fun _ : K => Bool) (slots 7) (slots 5)
        (slots.injective.ne (by decide)) id false (labels .restoreSum)
        (some (labels .shift)) p (atLabels .restoreSum)
        (tapes slots base left right out xs ys [] []
          (BinaryArithmetic.addCarry xs acc false).reverse [])
        ((ambient, false), none) none
  exact composeTrace (composeTrace hcopy hadd) hrestore

private theorem cleanupTrace (xs acc : List Bool) :
    (MachineComposition.advance (TM2.step p))^[xs.length + 2 * acc.length + 3]
      (some ⟨some (labels .clearShift), clean ambient,
        tapes slots base left right out xs [] acc [] [] []⟩) =
      some ⟨exit, clean ambient,
        tapes slots base left right (acc ++ out) [] [] [] [] [] []⟩ := by
  have hclear :
      (MachineComposition.advance (TM2.step p))^[xs.length + 1]
        (some ⟨some (labels .clearShift), clean ambient,
          tapes slots base left right out xs [] acc [] [] []⟩) =
        some ⟨some (labels .reverseOutput), clean ambient,
          tapes slots base left right out [] [] acc [] [] []⟩ := by
    simpa [clean] using MachineDrain.drainTrace (slots 3) (labels .clearShift)
      (some (labels .reverseOutput)) p (atLabels .clearShift)
      (tapes slots base left right out xs [] acc [] [] []) xs ((ambient, false), none) none
  have hreverse :
      (MachineComposition.advance (TM2.step p))^[acc.length + 1]
        (some ⟨some (labels .reverseOutput), clean ambient,
          tapes slots base left right out [] [] acc [] [] []⟩) =
        some ⟨some (labels .output), clean ambient,
          tapes slots base left right out [] [] [] [] acc.reverse []⟩ := by
    have hnext : MachineTransfer.nextAt (Γ := fun _ : K => Bool) (slots 7) p =
        MachineComposition.advance (TM2.step p) := rfl
    simpa [MachineTransfer.tapesAt, hnext, clean]
      using MachineTransfer.transferAt_fromTapes (Γ := fun _ : K => Bool) (slots 5) (slots 7)
        (slots.injective.ne (by decide)) id false (labels .reverseOutput)
        (some (labels .output)) p (atLabels .reverseOutput)
        (tapes slots base left right out [] [] acc [] [] []) ((ambient, false), none) none
  have houtput :
      (MachineComposition.advance (TM2.step p))^[acc.length + 1]
        (some ⟨some (labels .output), clean ambient,
          tapes slots base left right out [] [] [] [] acc.reverse []⟩) =
        some ⟨exit, clean ambient,
          tapes slots base left right (acc ++ out) [] [] [] [] [] []⟩ := by
    have hnext : MachineTransfer.nextAt (Γ := fun _ : K => Bool) (slots 2) p =
        MachineComposition.advance (TM2.step p) := rfl
    simpa [MachineTransfer.tapesAt, hnext, clean]
      using MachineTransfer.transferAt_fromTapes (Γ := fun _ : K => Bool) (slots 7) (slots 2)
        (slots.injective.ne (by decide)) id false (labels .output) exit p (atLabels .output)
        (tapes slots base left right out [] [] [] [] acc.reverse [])
        ((ambient, false), none) none
  rw [show xs.length + 2 * acc.length + 3 =
    (acc.length + 1) + ((acc.length + 1) + (xs.length + 1)) by omega]
  exact composeTrace (composeTrace hclear hreverse) houtput

theorem loopTrace (xs ys acc : List Bool) :
    (MachineComposition.advance (TM2.step p))^[loopSteps xs ys acc]
      (some ⟨some (labels .guard), clean ambient,
        tapes slots base left right out xs ys acc [] [] []⟩) =
      some ⟨exit, clean ambient,
        tapes slots base left right (BinaryArithmetic.mulAcc xs ys acc ++ out)
          [] [] [] [] [] []⟩ := by
  induction ys generalizing xs acc with
  | nil =>
      rw [show loopSteps xs [] acc = (xs.length + 2 * acc.length + 3) + 1 by
        simp only [loopSteps]]
      exact composeTrace
        (guardEmptyTrace slots labels exit p atLabels base left right out ambient xs acc)
        (cleanupTrace slots labels exit p atLabels base left right out ambient xs acc)
  | cons bit ys ih =>
      cases bit with
      | false =>
          rw [show loopSteps xs (false :: ys) acc =
            (loopSteps (BinaryArithmetic.shift xs) ys acc + 1) + 1 by
              simp only [loopSteps]]
          exact composeTrace
            (guardConsTrace slots labels exit p atLabels base left right out ambient xs ys acc false)
            (composeTrace
              (shiftTrace slots labels exit p atLabels base left right out ambient xs ys acc)
              (ih (BinaryArithmetic.shift xs) acc))
      | true =>
          rw [show loopSteps xs (true :: ys) acc =
            ((loopSteps (BinaryArithmetic.shift xs) ys (BinaryArithmetic.addCarry xs acc false) + 1) +
              (((BinaryArithmetic.addCarry xs acc false).length + 1) +
                ((max xs.length acc.length + 1) + 2 * (xs.length + 1)))) + 1 by
                  simp only [loopSteps]
                  omega]
          exact composeTrace
            (guardConsTrace slots labels exit p atLabels base left right out ambient xs ys acc true)
            (composeTrace
              (addPhaseTrace slots labels exit p atLabels base left right out ambient xs ys acc)
              (composeTrace
                (shiftTrace slots labels exit p atLabels base left right out ambient xs ys
                  (BinaryArithmetic.addCarry xs acc false))
                (ih (BinaryArithmetic.shift xs) (BinaryArithmetic.addCarry xs acc false))))

theorem multiplyTrace :
    (MachineComposition.advance (TM2.step p))^[steps left right]
      (some ⟨some (labels .copyLeft), clean ambient,
        tapes slots base left right out [] [] [] [] [] []⟩) =
      some ⟨exit, clean ambient,
        tapes slots base left right (BinaryArithmetic.mulAcc left right [] ++ out)
          [] [] [] [] [] []⟩ :=
  composeTrace (copyInputsTrace slots labels exit p atLabels base left right out ambient)
    (loopTrace slots labels exit p atLabels base left right out ambient left right [])

end Traces

theorem loopSteps_le (xs ys acc : List Bool) :
    loopSteps xs ys acc ≤
      (ys.length + 1) * (4 * (max xs.length acc.length + ys.length) + 8) := by
  induction ys generalizing xs acc with
  | nil =>
      have hx := Nat.le_max_left xs.length acc.length
      have ha := Nat.le_max_right xs.length acc.length
      simp only [loopSteps, List.length_nil, Nat.zero_add, Nat.add_zero, Nat.one_mul]
      omega
  | cons bit ys ih =>
      have hx := Nat.le_max_left xs.length acc.length
      have ha := Nat.le_max_right xs.length acc.length
      have hs := BinaryArithmetic.shift_length_le xs
      cases bit with
      | false =>
          have hp : max (BinaryArithmetic.shift xs).length acc.length + ys.length ≤
              max xs.length acc.length + (ys.length + 1) := by omega
          have h := (ih (BinaryArithmetic.shift xs) acc).trans
            (Nat.mul_le_mul_left (ys.length + 1)
              (Nat.add_le_add_right (Nat.mul_le_mul_left 4 hp) 8))
          simp only [loopSteps, List.length_cons]
          nlinarith
      | true =>
          have hadd := BinaryArithmetic.addCarry_length xs acc false
          have hp : max (BinaryArithmetic.shift xs).length
                (BinaryArithmetic.addCarry xs acc false).length + ys.length ≤
              max xs.length acc.length + (ys.length + 1) := by omega
          have h := (ih (BinaryArithmetic.shift xs) (BinaryArithmetic.addCarry xs acc false)).trans
            (Nat.mul_le_mul_left (ys.length + 1)
              (Nat.add_le_add_right (Nat.mul_le_mul_left 4 hp) 8))
          simp only [loopSteps, List.length_cons]
          nlinarith

noncomputable def timePolynomial : Polynomial Nat :=
  Polynomial.C 4 * Polynomial.X ^ 2 + Polynomial.C 14 * Polynomial.X + Polynomial.C 12

theorem steps_le_timePolynomial (xs ys : List Bool) :
    steps xs ys ≤ timePolynomial.eval (xs.length + ys.length) := by
  have hloop := loopSteps_le xs ys []
  simp only [List.length_nil, Nat.max_zero] at hloop
  have hproduct := Nat.mul_le_mul_right (4 * (xs.length + ys.length) + 8)
    (show ys.length + 1 ≤ (xs.length + ys.length) + 1 by omega)
  simp only [timePolynomial, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C,
    Polynomial.eval_pow, Polynomial.eval_X]
  unfold steps
  nlinarith

def multiplyInTime (slots : Fin 9 ↪ K) (labels : Label → Λ) (exit : Option Λ)
    (p : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
    (atLabels : ∀ l, p (labels l) = statement slots labels exit l)
    (base : K → List Bool) (left right out : List Bool) (ambient : A) :
    StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .copyLeft), clean ambient,
        tapes slots base left right out [] [] [] [] [] []⟩
      (some ⟨exit, clean ambient,
        tapes slots base left right (BinaryArithmetic.mulAcc left right [] ++ out)
          [] [] [] [] [] []⟩) (steps left right) where
  steps := steps left right
  evals_in_steps := multiplyTrace slots labels exit p atLabels base left right out ambient
  steps_le_m := Nat.le_refl _

def multiplyInPolynomialTime (slots : Fin 9 ↪ K) (labels : Label → Λ) (exit : Option Λ)
    (p : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
    (atLabels : ∀ l, p (labels l) = statement slots labels exit l)
    (base : K → List Bool) (left right out : List Bool) (ambient : A) :
    StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .copyLeft), clean ambient,
        tapes slots base left right out [] [] [] [] [] []⟩
      (some ⟨exit, clean ambient,
        tapes slots base left right (BinaryArithmetic.mulAcc left right [] ++ out)
          [] [] [] [] [] []⟩)
      (timePolynomial.eval (left.length + right.length)) where
  steps := steps left right
  evals_in_steps := multiplyTrace slots labels exit p atLabels base left right out ambient
  steps_le_m := steps_le_timePolynomial left right

theorem multiplyNatTrace (slots : Fin 9 ↪ K) (labels : Label → Λ) (exit : Option Λ)
    (p : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
    (atLabels : ∀ l, p (labels l) = statement slots labels exit l)
    (base : K → List Bool) (a b : Nat) (out : List Bool) (ambient : A) :
    (MachineComposition.advance (TM2.step p))^[steps a.bits b.bits]
      (some ⟨some (labels .copyLeft), clean ambient,
        tapes slots base a.bits b.bits out [] [] [] [] [] []⟩) =
      some ⟨exit, clean ambient,
        tapes slots base a.bits b.bits ((a * b).bits ++ out) [] [] [] [] [] []⟩ := by
  simpa only [BinaryArithmetic.mulAcc_bits] using
    multiplyTrace slots labels exit p atLabels base a.bits b.bits out ambient

theorem multiplyFrame (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right out result : List Bool) (k : K) (hk : k ≠ slots 2) :
    tapes slots base left right (result ++ out) [] [] [] [] [] [] k =
      tapes slots base left right out [] [] [] [] [] [] k := by
  rw [← tapes_update2 slots base left right out [] [] [] [] [] [] (result ++ out)]
  simp only [Function.update_of_ne hk]

private theorem initialTapes_eq (slots : Fin 9 ↪ K) (base : K → List Bool)
    (left right : List Bool) (hleft : base (slots 0) = left) (hright : base (slots 1) = right)
    (workspace : ∀ i : Fin 9, 3 ≤ i.val → base (slots i) = []) :
    tapes slots base left right (base (slots 2)) [] [] [] [] [] [] = base := by
  have h3 := workspace 3 (by decide)
  have h4 := workspace 4 (by decide)
  have h5 := workspace 5 (by decide)
  have h6 := workspace 6 (by decide)
  have h7 := workspace 7 (by decide)
  have h8 := workspace 8 (by decide)
  funext k
  by_cases hk : ∃ i, slots i = k
  · obtain ⟨i, rfl⟩ := hk
    rw [tapes_slot]
    fin_cases i <;> simp_all
  · exact framedTapes_other slots base _ k hk

def multiplyNatInPolynomialTime (slots : Fin 9 ↪ K) (labels : Label → Λ) (exit : Option Λ)
    (p : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
    (atLabels : ∀ l, p (labels l) = statement slots labels exit l)
    (base : K → List Bool) (a b : Nat)
    (operandA : base (slots 0) = a.bits) (operandB : base (slots 1) = b.bits)
    (workspace : ∀ i : Fin 9, 3 ≤ i.val → base (slots i) = []) (ambient : A) :
    StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .copyLeft), clean ambient, base⟩
      (some ⟨exit, clean ambient,
        Function.update base (slots 2) ((a * b).bits ++ base (slots 2))⟩)
      (timePolynomial.eval (a.size + b.size)) := by
  have hbase := initialTapes_eq slots base a.bits b.bits operandA operandB workspace
  have hfinish : tapes slots base a.bits b.bits ((a * b).bits ++ base (slots 2))
      [] [] [] [] [] [] = Function.update base (slots 2) ((a * b).bits ++ base (slots 2)) := by
    rw [← tapes_update2 slots base a.bits b.bits (base (slots 2)) [] [] [] [] [] [], hbase]
  have h := multiplyInPolynomialTime slots labels exit p atLabels base a.bits b.bits
    (base (slots 2)) ambient
  simpa only [BinaryArithmetic.mulAcc_bits, hbase, hfinish, ← Nat.size_eq_bits_len] using h

def programNatInPolynomialTime (slots : Fin 9 ↪ K) (base : K → List Bool) (a b : Nat)
    (operandA : base (slots 0) = a.bits) (operandB : base (slots 1) = b.bits)
    (workspace : ∀ i : Fin 9, 3 ≤ i.val → base (slots i) = []) (ambient : A) :
    StateTransition.EvalsToInTime (TM2.step (program (A := A) slots))
      ⟨some .copyLeft, clean ambient, base⟩
      (some ⟨none, clean ambient,
        Function.update base (slots 2) ((a * b).bits ++ base (slots 2))⟩)
      (timePolynomial.eval (a.size + b.size)) :=
  multiplyNatInPolynomialTime slots id none (program slots) (fun _ => rfl)
    base a b operandA operandB workspace ambient

end BinPackingGap.BinaryMulMachine

namespace BinPackingGap.BinaryPowerMachine

open Turing
open BinPackingGames.Foundations.Complexity
open BinPackingGames.Reduction.MachineTransfer
open BinaryAddMachine (State clean)

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

inductive Label
  | initialize | guard | multiply (phase : BinaryMulMachine.Label)
  | clearAccumulator | reverseProduct | restoreAccumulator
  deriving DecidableEq, Fintype

def mulSlots (slots : Fin 11 ↪ K) : Fin 9 ↪ K where
  toFun i := slots ⟨i.val, by omega⟩
  inj' := by
    intro i j h
    have h' := congrArg Fin.val (slots.injective h)
    exact Fin.ext h'

def instruction (slots : Fin 11 ↪ K) (labels : Label → Λ) (exit : Option Λ) :
    Label → TM2.Stmt (BinaryAddMachine.Alphabet (K := K)) Λ (State A)
  | .initialize => .push (slots 1) (fun _ => true)
      (.load (fun s => clean s.1.1.1) (.goto fun _ => labels .guard))
  | .guard => .pop (slots 9) (fun s head => (s.1, head))
      (.branch (fun s => s.2.isSome)
        (.load (fun s => clean s.1.1.1)
          (.goto fun _ => labels (.multiply .copyLeft)))
        (BinaryAddMachine.finish exit))
  | .multiply phase => BinaryMulMachine.statement (mulSlots slots)
      (fun phase => labels (.multiply phase)) (some (labels .clearAccumulator)) phase
  | .clearAccumulator => MachineDrain.drain (slots 1) (labels .clearAccumulator)
      (some (labels .reverseProduct))
  | .reverseProduct => loopAt (slots 2) (slots 10) id false
      (labels .reverseProduct) (some (labels .restoreAccumulator))
  | .restoreAccumulator => loopAt (slots 10) (slots 1) id false
      (labels .restoreAccumulator) (some (labels .guard))

def program (slots : Fin 11 ↪ K) :
    Label → TM2.Stmt (BinaryAddMachine.Alphabet (K := K)) Label (State A) :=
  instruction slots id none

def machine : FinTM2 where
  K := Fin 11
  k₀ := 0
  k₁ := 1
  Γ _ := Bool
  Λ := Label
  main := .initialize
  σ := State Unit
  initialState := clean ()
  m := program (Function.Embedding.refl (Fin 11))

theorem machine_finiteAlphabet : MachineFiniteAlphabet.FiniteAlphabet machine := by
  intro k
  change Finite Bool
  infer_instance

def Workspace (slots : Fin 11 ↪ K) (base : K → List Bool) : Prop :=
  ∀ i : Fin 11, 2 ≤ i.val → i.val ≠ 9 → base (slots i) = []

noncomputable def bodyBudget (a r : Nat) : Nat :=
  1 + BinaryMulMachine.timePolynomial.eval (a.size + r.size) +
    (r.size + 1) + ((a * r).size + 1) + ((a * r).size + 1)

noncomputable def loopBudget (a : Nat) : Nat → List Bool → Nat
  | _, [] => 1
  | r, _ :: tally => bodyBudget a r + loopBudget a (a * r) tally

def appendRuns {X : Type} (step : X → Option X) {a b c : X} {m n : Nat}
    (first : StateTransition.EvalsToInTime step a (some b) m)
    (second : StateTransition.EvalsToInTime step b (some c) n) :
    StateTransition.EvalsToInTime step a (some c) (m + n) where
  steps := first.steps + second.steps
  evals_in_steps := by
    change (MachineComposition.advance step)^[first.steps + second.steps] (some a) = some c
    rw [Nat.add_comm, Function.iterate_add_apply]
    have hfirst := first.evals_in_steps
    have hsecond := second.evals_in_steps
    change (MachineComposition.advance step)^[first.steps] (some a) = some b at hfirst
    change (MachineComposition.advance step)^[second.steps] (some b) = some c at hsecond
    rw [hfirst]
    exact hsecond
  steps_le_m := Nat.add_le_add first.steps_le_m second.steps_le_m

section Executions

variable (slots : Fin 11 ↪ K) (labels : Label → Λ) (exit : Option Λ)
variable (p : Λ → TM2.Stmt (BinaryAddMachine.Alphabet (K := K)) Λ (State A))
variable (atLabels : ∀ label, p (labels label) = instruction slots labels exit label)

noncomputable def bodyInTime (base : K → List Bool) (a r : Nat) (token : Bool) (tally : List Bool)
    (ha : base (slots 0) = a.bits) (hr : base (slots 1) = r.bits)
    (ht : base (slots 9) = token :: tally) (hw : Workspace slots base) (ambient : A) :
    StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .guard), clean ambient, base⟩
      (some ⟨some (labels .guard), clean ambient,
        Function.update (Function.update base (slots 9) tally) (slots 1) (a * r).bits⟩)
      (bodyBudget a r) := by
  have hd (i j : Fin 11) (hne : i ≠ j) : slots i ≠ slots j := slots.injective.ne hne
  let bt := Function.update base (slots 9) tally
  let bm := Function.update bt (slots 2) (a * r).bits
  let bc := Function.update bm (slots 1) []
  let bv := Function.update (Function.update bc (slots 2) []) (slots 10) (a * r).bits.reverse
  let done := Function.update bt (slots 1) (a * r).bits
  have h2 := hw 2 (by decide) (by decide)
  have h10 := hw 10 (by decide) (by decide)
  have guardRun : StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .guard), clean ambient, base⟩
      (some ⟨some (labels (.multiply .copyLeft)), clean ambient, bt⟩) 1 := by
    refine { steps := 1, evals_in_steps := ?_, steps_le_m := Nat.le_refl _ }
    change some (TM2.stepAux (p (labels .guard)) _ _) = _
    rw [atLabels]
    simp [instruction, TM2.stepAux, clean, BinaryAddMachine.state, ht, bt]
  have ms0 : mulSlots slots 0 = slots 0 := rfl
  have ms1 : mulSlots slots 1 = slots 1 := rfl
  have ms2 : mulSlots slots 2 = slots 2 := rfl
  have hma : bt ((mulSlots slots) 0) = a.bits := by
    simpa [ms0, bt, hd 0 9 (by decide)] using ha
  have hmr : bt ((mulSlots slots) 1) = r.bits := by
    simpa [ms1, bt, hd 1 9 (by decide)] using hr
  have hmw : ∀ i : Fin 9, 3 ≤ i.val → bt ((mulSlots slots) i) = [] := by
    intro i hi
    let j : Fin 11 := ⟨i.val, by omega⟩
    have hjval : j.val = i.val := rfl
    have hj9 : j.val ≠ 9 := by rw [hjval]; omega
    have hj : j ≠ 9 := by
      intro h
      apply hj9
      exact congrArg Fin.val h
    change bt (slots j) = []
    simpa [bt, hd j 9 hj] using hw j (by rw [hjval]; omega) hj9
  have mulRun : StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels (.multiply .copyLeft)), clean ambient, bt⟩
      (some ⟨some (labels .clearAccumulator), clean ambient, bm⟩)
      (BinaryMulMachine.timePolynomial.eval (a.size + r.size)) := by
    have run := BinaryMulMachine.multiplyNatInPolynomialTime (mulSlots slots)
      (fun phase => labels (.multiply phase)) (some (labels .clearAccumulator)) p
      (fun phase => atLabels (.multiply phase)) bt a r hma hmr hmw ambient
    change StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels (.multiply .copyLeft)), clean ambient, bt⟩
      (some ⟨some (labels .clearAccumulator), clean ambient,
        Function.update bt (slots 2) ((a * r).bits ++ bt (slots 2))⟩)
      (BinaryMulMachine.timePolynomial.eval (a.size + r.size)) at run
    have hbt2 : bt (slots 2) = [] := by simp [bt, hd 2 9 (by decide), h2]
    simpa only [hbt2, List.append_nil] using run
  have clearRun : StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .clearAccumulator), clean ambient, bm⟩
      (some ⟨some (labels .reverseProduct), clean ambient, bc⟩) (r.size + 1) := by
    have run := MachineDrain.drainInTime (slots 1) (labels .clearAccumulator)
      (some (labels .reverseProduct)) p (atLabels .clearAccumulator) bm
      ((ambient, false), none) none
    simpa [clean, BinaryAddMachine.state, bc, bm, bt, hd 1 2 (by decide),
      hd 1 9 (by decide), hr, Nat.size_eq_bits_len] using run
  have reverseRun : StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .reverseProduct), clean ambient, bc⟩
      (some ⟨some (labels .restoreAccumulator), clean ambient, bv⟩)
      ((a * r).size + 1) := by
    have run := transferAtInTime (Γ := fun _ : K => Bool) (slots 2) (slots 10) (hd 2 10 (by decide)) id false
      (labels .reverseProduct) (some (labels .restoreAccumulator)) p
      (by simpa only [instruction] using atLabels .reverseProduct) bc ((ambient, false), none) none
    simpa [clean, BinaryAddMachine.state, tapesAt, bv, bc, bm, bt,
      hd 2 1 (by decide), hd 10 1 (by decide), hd 10 2 (by decide),
      hd 10 9 (by decide), h10, Nat.size_eq_bits_len] using run
  have restoreTape : tapesAt (slots 10) (slots 1) bv []
      ((bv (slots 10)).reverse.map id ++ bv (slots 1)) = done := by
    funext k
    by_cases h1 : k = slots 1
    · subst k
      simp [tapesAt, bv, bc, bm, bt, done, hd 1 10 (by decide), hd 1 2 (by decide)]
    · by_cases h2k : k = slots 2
      · subst k
        simp [tapesAt, bv, bc, bm, bt, done, hd 2 1 (by decide), hd 2 10 (by decide),
          hd 2 9 (by decide), h2]
      · by_cases h10k : k = slots 10
        · subst k
          simp [tapesAt, bv, bc, bm, bt, done, hd 10 1 (by decide),
            hd 10 9 (by decide), h10]
        · simp [tapesAt, bv, bc, bm, done, h1, h2k, h10k]
  have restoreRun : StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .restoreAccumulator), clean ambient, bv⟩
      (some ⟨some (labels .guard), clean ambient, done⟩) ((a * r).size + 1) := by
    have run := transferAtInTime (Γ := fun _ : K => Bool) (slots 10) (slots 1) (hd 10 1 (by decide)) id false
      (labels .restoreAccumulator) (some (labels .guard)) p
      (by simpa only [instruction] using atLabels .restoreAccumulator) bv ((ambient, false), none) none
    change StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .restoreAccumulator), clean ambient, bv⟩
      (some ⟨some (labels .guard), clean ambient,
        tapesAt (slots 10) (slots 1) bv []
          ((bv (slots 10)).reverse.map id ++ bv (slots 1))⟩)
      ((bv (slots 10)).length + 1) at run
    rw [restoreTape] at run
    have hword : bv (slots 10) = (a * r).bits.reverse := by simp [bv]
    simpa only [hword, List.length_reverse, Nat.size_eq_bits_len] using run
  exact appendRuns _ (appendRuns _ (appendRuns _ (appendRuns _ guardRun mulRun) clearRun)
    reverseRun) restoreRun

noncomputable def loopInTime (base : K → List Bool) (a r : Nat) (tally : List Bool)
    (ha : base (slots 0) = a.bits) (hr : base (slots 1) = r.bits)
    (ht : base (slots 9) = tally) (hw : Workspace slots base) (ambient : A) :
    StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .guard), clean ambient, base⟩
      (some ⟨exit, clean ambient,
        Function.update (Function.update base (slots 9) []) (slots 1)
          (a ^ tally.length * r).bits⟩) (loopBudget a r tally) := by
  induction tally generalizing base r with
  | nil =>
      refine { steps := 1, evals_in_steps := ?_, steps_le_m := Nat.le_refl _ }
      change some (TM2.stepAux (p (labels .guard)) _ _) = _
      rw [atLabels]
      have hself : Function.update (Function.update base (slots 9) []) (slots 1) r.bits =
          Function.update base (slots 9) [] := by
        have hd : slots 1 ≠ slots 9 := slots.injective.ne (by decide)
        have hs : (Function.update base (slots 9) []) (slots 1) = r.bits := by simp [hd, hr]
        rw [← hs, Function.update_eq_self]
      cases exit <;> simp [instruction, BinaryAddMachine.finish, BinaryAddMachine.exitAt,
        clean, BinaryAddMachine.state, TM2.stepAux, ht, hself]
  | cons token tally ih =>
      let next := Function.update (Function.update base (slots 9) tally) (slots 1) (a * r).bits
      have hd (i j : Fin 11) (h : i ≠ j) : slots i ≠ slots j := slots.injective.ne h
      have hna : next (slots 0) = a.bits := by
        simp [next, hd 0 1 (by decide), hd 0 9 (by decide), ha]
      have hnr : next (slots 1) = (a * r).bits := by simp [next]
      have hnt : next (slots 9) = tally := by simp [next, hd 9 1 (by decide)]
      have hnw : Workspace slots next := by
        intro i hi hi9
        have hi1 : i ≠ 1 := by intro h; have := congrArg Fin.val h; omega
        have hi9' : i ≠ 9 := by intro h; exact hi9 (congrArg Fin.val h)
        simp [next, hd i 1 hi1, hd i 9 hi9', hw i hi hi9]
      have first := bodyInTime slots labels exit p atLabels base a r token tally ha hr ht hw ambient
      have second := ih next (a * r) hna hnr hnt hnw
      have joined := appendRuns _ first second
      have hvalue : a ^ tally.length * (a * r) = a ^ (token :: tally).length * r := by
        simp [pow_succ, Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm]
      have hfinal : Function.update (Function.update next (slots 9) []) (slots 1)
          (a ^ tally.length * (a * r)).bits =
          Function.update (Function.update base (slots 9) []) (slots 1)
            (a ^ (token :: tally).length * r).bits := by
        funext k
        by_cases h1 : k = slots 1
        · subst k; simp [hvalue]
        · by_cases h9 : k = slots 9
          · subst k; simp [hd 9 1 (by decide)]
          · simp [next, h1, h9]
      simpa only [loopBudget, hfinal] using joined

noncomputable def powerInTime (base : K → List Bool) (a : Nat) (tally : List Bool)
    (ha : base (slots 0) = a.bits) (outputEmpty : base (slots 1) = [])
    (ht : base (slots 9) = tally) (hw : Workspace slots base) (ambient : A) :
    StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .initialize), clean ambient, base⟩
      (some ⟨exit, clean ambient,
        Function.update (Function.update base (slots 9) []) (slots 1) (a ^ tally.length).bits⟩)
      (1 + loopBudget a 1 tally) := by
  let initial := Function.update base (slots 1) [true]
  have hd (i j : Fin 11) (h : i ≠ j) : slots i ≠ slots j := slots.injective.ne h
  have start : StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .initialize), clean ambient, base⟩
      (some ⟨some (labels .guard), clean ambient, initial⟩) 1 := by
    refine { steps := 1, evals_in_steps := ?_, steps_le_m := Nat.le_refl _ }
    change some (TM2.stepAux (p (labels .initialize)) _ _) = _
    rw [atLabels]
    simp [instruction, TM2.stepAux, clean, BinaryAddMachine.state, outputEmpty, initial]
  have hai : initial (slots 0) = a.bits := by simp [initial, hd 0 1 (by decide), ha]
  have hri : initial (slots 1) = (1 : Nat).bits := by simp [initial]
  have hti : initial (slots 9) = tally := by simp [initial, hd 9 1 (by decide), ht]
  have hwi : Workspace slots initial := by
    intro i hi hi9
    have hi1 : i ≠ 1 := by intro h; have := congrArg Fin.val h; omega
    simp [initial, hd i 1 hi1, hw i hi hi9]
  have run := loopInTime slots labels exit p atLabels initial a 1 tally hai hri hti hwi ambient
  have hfinal : Function.update (Function.update initial (slots 9) []) (slots 1)
      (a ^ tally.length * 1).bits =
      Function.update (Function.update base (slots 9) []) (slots 1) (a ^ tally.length).bits := by
    funext k
    by_cases h1 : k = slots 1
    · subst k; simp
    · by_cases h9 : k = slots 9
      · subst k; simp [hd 9 1 (by decide)]
      · simp [initial, h1, h9]
  simpa only [hfinal] using appendRuns _ start run

end Executions

noncomputable def perIteration (width : Nat) : Nat :=
  BinaryMulMachine.timePolynomial.eval (2 * width) + 3 * width + 5

theorem loopBudget_le (a r : Nat) (tally : List Bool) (width : Nat)
    (ha : a.size ≤ width) (hr : r.size + tally.length * a.size ≤ width) :
    loopBudget a r tally ≤ (tally.length + 1) * perIteration width := by
  induction tally generalizing r with
  | nil => simp [loopBudget, perIteration]
  | cons token tally ih =>
      have hp := BinaryArithmetic.size_mul_le a r
      have hnext : (a * r).size + tally.length * a.size ≤ width := by
        simp only [List.length_cons, Nat.add_mul] at hr
        omega
      have hbound := ih (a * r) hnext
      have hrs : r.size ≤ width := by omega
      have hps : (a * r).size ≤ width := by omega
      have hm := MachineComposition.natPolynomial_eval_mono BinaryMulMachine.timePolynomial
        (show a.size + r.size ≤ 2 * width by omega)
      have hb : bodyBudget a r ≤ perIteration width := by
        unfold bodyBudget perIteration
        omega
      simp only [loopBudget, List.length_cons]
      nlinarith

noncomputable def timePolynomial : Polynomial Nat :=
  Polynomial.C 100 * (Polynomial.X + Polynomial.C 2) ^ 5

theorem budget_le_timePolynomial (a : Nat) (tally : List Bool) :
    1 + loopBudget a 1 tally ≤ timePolynomial.eval (a.size + tally.length) := by
  let width := (a.size + tally.length + 2) ^ 2
  have hs : 1 ≤ a.size + tally.length + 2 := by omega
  have hsq : a.size + tally.length + 2 ≤ width := le_self_pow hs (by decide)
  have hw1 : 1 ≤ width := hs.trans hsq
  have ha : a.size ≤ width := (show a.size ≤ a.size + tally.length + 2 by omega).trans hsq
  have hr : (1 : Nat).size + tally.length * a.size ≤ width := by
    simp only [Nat.size_one]
    dsimp [width]
    nlinarith
  have hloop := loopBudget_le a 1 tally width ha hr
  have hper : perIteration width ≤ 64 * width ^ 2 := by
    simp only [perIteration, BinaryMulMachine.timePolynomial, Polynomial.eval_add,
      Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_pow, Polynomial.eval_X]
    nlinarith
  have hcount : tally.length + 1 ≤ a.size + tally.length + 2 := by omega
  have hmul := Nat.mul_le_mul hcount hper
  have hfull := hloop.trans hmul
  simp only [timePolynomial, Polynomial.eval_mul, Polynomial.eval_C,
    Polynomial.eval_pow, Polynomial.eval_add, Polynomial.eval_X]
  dsimp [width] at hfull
  have hs5 : 1 ≤ (a.size + tally.length + 2) ^ 5 := hs.trans (le_self_pow hs (by decide))
  nlinarith [hs5]

noncomputable def powerInPolynomialTime (slots : Fin 11 ↪ K) (labels : Label → Λ) (exit : Option Λ)
    (p : Λ → TM2.Stmt (BinaryAddMachine.Alphabet (K := K)) Λ (State A))
    (atLabels : ∀ label, p (labels label) = instruction slots labels exit label)
    (base : K → List Bool) (a : Nat) (tally : List Bool)
    (ha : base (slots 0) = a.bits) (outputEmpty : base (slots 1) = [])
    (ht : base (slots 9) = tally) (hw : Workspace slots base) (ambient : A) :
    StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .initialize), clean ambient, base⟩
      (some ⟨exit, clean ambient,
        Function.update (Function.update base (slots 9) []) (slots 1) (a ^ tally.length).bits⟩)
      (timePolynomial.eval (a.size + tally.length)) := by
  let run := powerInTime slots labels exit p atLabels base a tally ha outputEmpty ht hw ambient
  exact {
    steps := run.steps
    evals_in_steps := run.evals_in_steps
    steps_le_m := run.steps_le_m.trans (budget_le_timePolynomial a tally)
  }

end BinPackingGap.BinaryPowerMachine

end OAI