File size: 46,046 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
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.Tactic.FinCases
import OAI.Computability.BinPacking.Machines.MachineControlEmbedding
import OAI.Computability.BinPacking.Machines.PackingPoolMachine
import OAI.Computability.BinPacking.Machines.PackingPowerProgram

namespace OAI

namespace BinPackingGap.GraphFieldMachine

open Turing
open BinaryEncoding
open BinPackingGames.Foundations.Complexity
open BinPackingGames.Reduction
open GraphTallyMachine (Mode Slot afterName initial transition)

inductive Field
  | threshold
  | endpoints
  deriving DecidableEq

instance : Fintype Field where
  elems := {.threshold, .endpoints}
  complete field := by cases field <;> simp

def keepsName : Field → Slot → Bool
  | .threshold, .header => true
  | .endpoints, .edgeFirst => true
  | .endpoints, .edgeSecond => true
  | _, _ => false

def emit : Field → Mode → Bool → List Bool
  | .threshold, .name .header, bit => [bit]
  | .threshold, .digit .header, bit => [bit]
  | .endpoints, .endpoints, bit => [bit]
  | .endpoints, .name .edgeFirst, bit => [bit]
  | .endpoints, .digit .edgeFirst, bit => [bit]
  | .endpoints, .name .edgeSecond, bit => [bit]
  | .endpoints, .digit .edgeSecond, bit => [bit]
  | _, _, _ => []

def scanOutput (field : Field) : Mode → List Bool → List Bool :=
  MachineTransducer.output transition (emit field)

@[simp] theorem scanOutput_nil (field : Field) (mode : Mode) :
    scanOutput field mode [] = [] := rfl

theorem scanOutput_cons (field : Field) (mode : Mode) (bit : Bool) (rest : List Bool) :
    scanOutput field mode (bit :: rest) =
      emit field mode bit ++ scanOutput field (transition mode bit) rest := rfl

theorem scanOutput_done (field : Field) (input : List Bool) :
    scanOutput field .done input = [] := by
  induction input with
  | nil => rfl
  | cons bit rest ih =>
      cases field <;> simp only [scanOutput_cons, transition, emit, List.nil_append, ih]

theorem scanOutput_frame (field : Field) (slot : Slot) (bits suffix : List Bool) :
    scanOutput field (.name slot)
        (BinPackingCompleteness.BinaryEncoding.frame bits ++ suffix) =
      (if keepsName field slot then BinPackingCompleteness.BinaryEncoding.frame bits else []) ++
        scanOutput field (afterName slot) suffix := by
  induction bits with
  | nil => cases field <;> cases slot <;> rfl
  | cons bit bits ih =>
      cases field <;> cases slot <;>
        simp [BinPackingCompleteness.BinaryEncoding.frame, List.cons_append,
          scanOutput_cons, transition, emit, keepsName, afterName, ih]

theorem scanOutput_natBits (field : Field) (slot : Slot) (n : Nat) (suffix : List Bool) :
    scanOutput field (.name slot) (natBits n ++ suffix) =
      (if keepsName field slot then natBits n else []) ++
        scanOutput field (afterName slot) suffix :=
  scanOutput_frame field slot n.bits suffix

theorem scanOutput_pairBits (field : Field) (edge : Nat × Nat) (suffix : List Bool) :
    scanOutput field (.name .edgeFirst) (pairBits natBits natBits edge ++ suffix) =
      (if field = .endpoints then pairBits natBits natBits edge else []) ++
        scanOutput field .endpoints suffix := by
  cases field <;>
    simp [pairBits, List.append_assoc, scanOutput_natBits, keepsName, afterName]

theorem scanOutput_vertices (field : Field) (vertices : List Nat) (suffix : List Bool) :
    scanOutput field .vertices (listBits natBits vertices ++ suffix) =
      scanOutput field .endpoints suffix := by
  induction vertices with
  | nil => cases field <;> rfl
  | cons vertex vertices ih =>
      cases field <;>
        simp [listBits, List.cons_append, List.append_assoc, scanOutput_cons,
          transition, emit, scanOutput_natBits, keepsName, afterName, ih]

theorem scanOutput_endpoints (field : Field) (endpoints : List (Nat × Nat))
    (suffix : List Bool) :
    scanOutput field .endpoints (listBits (pairBits natBits natBits) endpoints ++ suffix) =
      (if field = .endpoints then listBits (pairBits natBits natBits) endpoints else []) ++
        scanOutput field .done suffix := by
  induction endpoints with
  | nil => cases field <;> rfl
  | cons edge endpoints ih =>
      cases field <;>
        simp [listBits, List.cons_append, List.append_assoc, scanOutput_cons,
          transition, emit, scanOutput_pairBits, ih]

def fieldsOutput : Field → GraphFields → List Bool
  | .threshold, fields => natBits fields.k
  | .endpoints, fields => listBits (pairBits natBits natBits) fields.endpoints

def fieldBits (field : Field) (input : GraphReductionInput) : List Bool :=
  fieldsOutput field input.fields

@[simp] theorem thresholdBits (input : GraphReductionInput) :
    fieldBits .threshold input = natBits input.k := rfl

@[simp] theorem endpointsBits (input : GraphReductionInput) :
    fieldBits .endpoints input =
      listBits (pairBits natBits natBits) input.graph.endpointPairs := rfl

theorem output_graphFields (field : Field) (fields : GraphFields) :
    scanOutput field initial fields.bits = fieldsOutput field fields := by
  have hend := scanOutput_endpoints field fields.endpoints []
  simp only [List.append_nil, scanOutput_nil] at hend
  cases field <;>
    simp [GraphFields.bits, List.append_assoc, initial, scanOutput_natBits,
      keepsName, afterName, scanOutput_vertices, fieldsOutput, hend]

theorem output_graphBits (field : Field) (input : GraphReductionInput) :
    scanOutput field initial (graphBits input) = fieldBits field input :=
  output_graphFields field input.fields

theorem emit_length_le (field : Field) (mode : Mode) (bit : Bool) :
    (emit field mode bit).length ≤ 1 := by
  cases mode with
  | vertices => cases field <;> simp [emit]
  | endpoints => cases field <;> simp [emit]
  | name slot => cases field <;> cases slot <;> simp [emit]
  | digit slot => cases field <;> cases slot <;> simp [emit]
  | done => cases field <;> simp [emit]

theorem scanOutput_length_le (field : Field) (mode : Mode) (input : List Bool) :
    (scanOutput field mode input).length ≤ input.length := by
  induction input generalizing mode with
  | nil => exact Nat.le_refl _
  | cons bit input ih =>
      rw [scanOutput_cons, List.length_append, List.length_cons]
      have he := emit_length_le field mode bit
      have hi := ih (transition mode bit)
      omega

theorem fieldBits_length_le (field : Field) (input : GraphReductionInput) :
    (fieldBits field input).length ≤ (graphBits input).length := by
  rw [← output_graphBits]
  exact scanOutput_length_le field initial (graphBits input)

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

abbrev State (σ : Type) := MachineTransducerCopy.State σ Mode

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

def fieldCopyInTime (field : Field) (source scratch destination : K)
    (sourceScratch : source ≠ scratch) (sourceDestination : source ≠ destination)
    (scratchDestination : scratch ≠ destination)
    (scanLabel restoreLabel : Λ) (emitterLabel : Mode → Bool → Λ) (exit : Option Λ)
    (program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State σ))
    (atScan : program scanLabel =
      MachineTransducerCopy.scanLoop source scratch initial emitterLabel restoreLabel)
    (atEmitter : ∀ control symbol, program (emitterLabel control symbol) =
      MachineTransducerCopy.emitter destination transition (emit field) scanLabel control symbol)
    (atRestore : program restoreLabel =
      MachineTransfer.loopAt scratch source id false restoreLabel exit)
    (base : K → List Bool) (scratchEmpty : base scratch = [])
    (input : GraphReductionInput) (sourceWord : base source = graphBits input)
    (ambient : σ) (register : Option Bool) :
    StateTransition.EvalsToInTime (TM2.step program)
      ⟨some scanLabel, ((ambient, initial), register), base⟩
      (some ⟨exit, clean ambient, Function.update base destination
        ((fieldBits field input).reverse ++ base destination)⟩)
      (3 * (graphBits input).length + 2) := by
  have run := MachineTransducerCopy.transduceCopyInTime source scratch destination
    sourceScratch sourceDestination scratchDestination initial transition (emit field)
    scanLabel restoreLabel emitterLabel exit program atScan atEmitter atRestore
    base scratchEmpty ambient initial register
  have hout : MachineTransducer.output transition (emit field) initial (base source) =
      fieldBits field input := by
    change scanOutput field initial (base source) = _
    rw [sourceWord, output_graphBits]
  rw [hout] at run
  simpa only [sourceWord, clean] using run

inductive Label
  | scan | restore | transfer
  | emitter (mode : Mode) (bit : Bool)
  deriving DecidableEq, Fintype

def instruction (field : Field) (slots : Fin 4 ↪ K) (labels : Label → Λ)
    (exit : Option Λ) : Label → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State σ)
  | .scan => MachineTransducerCopy.scanLoop (slots 0) (slots 1) initial
      (fun mode bit => labels (.emitter mode bit)) (labels .restore)
  | .emitter mode bit => MachineTransducerCopy.emitter (slots 2) transition (emit field)
      (labels .scan) mode bit
  | .restore => MachineTransfer.loopAt (slots 1) (slots 0) id false
      (labels .restore) (some (labels .transfer))
  | .transfer => MachineTransfer.loopAt (slots 2) (slots 3) id false
      (labels .transfer) exit

def resultTapes (field : Field) (slots : Fin 4 ↪ K) (base : K → List Bool)
    (input : GraphReductionInput) : K → List Bool :=
  Function.update base (slots 3) (fieldBits field input ++ base (slots 3))

def steps (field : Field) (input : GraphReductionInput) : Nat :=
  (3 * (graphBits input).length + 2) + ((fieldBits field input).length + 1)

theorem steps_le (field : Field) (input : GraphReductionInput) :
    steps field input ≤ 4 * (graphBits input).length + 3 := by
  have h := fieldBits_length_le field input
  unfold steps
  omega

theorem extractTrace (field : Field) (slots : Fin 4 ↪ K) (labels : Label → Λ)
    (exit : Option Λ)
    (program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State σ))
    (atLabels : ∀ label, program (labels label) = instruction field slots labels exit label)
    (base : K → List Bool) (input : GraphReductionInput)
    (sourceWord : base (slots 0) = graphBits input)
    (scratchEmpty : base (slots 1) = []) (temporaryEmpty : base (slots 2) = [])
    (ambient : σ) :
    (MachineComposition.advance (TM2.step program))^[steps field input]
      (some ⟨some (labels .scan), clean ambient, base⟩) =
      some ⟨exit, clean ambient, resultTapes field slots base input⟩ := by
  have hd (i j : Fin 4) (hne : i ≠ j) : slots i ≠ slots j := slots.injective.ne hne
  let middle := Function.update base (slots 2) (fieldBits field input).reverse
  have first : (MachineComposition.advance (TM2.step program))^[3 * (graphBits input).length + 2]
      (some ⟨some (labels .scan), clean ambient, base⟩) =
      some ⟨some (labels .transfer), clean ambient, middle⟩ := by
    have run := MachineTransducerCopy.transduceCopyTrace (slots 0) (slots 1) (slots 2)
      (hd 0 1 (by decide)) (hd 0 2 (by decide)) (hd 1 2 (by decide))
      initial transition (emit field) (labels .scan) (labels .restore)
      (fun mode bit => labels (.emitter mode bit)) (some (labels .transfer))
      program (atLabels .scan) (fun mode bit => atLabels (.emitter mode bit))
      (atLabels .restore) base scratchEmpty ambient initial none
    have hout : MachineTransducer.output transition (emit field) initial (base (slots 0)) =
        fieldBits field input := by
      change scanOutput field initial (base (slots 0)) = _
      rw [sourceWord, output_graphBits]
    rw [hout] at run
    simpa only [sourceWord, temporaryEmpty, List.append_nil, clean, middle] using run
  have middleSource : middle (slots 2) = (fieldBits field input).reverse := by simp [middle]
  have middleDestination : middle (slots 3) = base (slots 3) := by
    simp [middle, hd 3 2 (by decide)]
  have finalTapes : MachineTransfer.tapesAt (slots 2) (slots 3) middle []
      (fieldBits field input ++ base (slots 3)) = resultTapes field slots base input := by
    funext k
    by_cases ht : k = slots 2
    · subst k
      simp [MachineTransfer.tapesAt, resultTapes, temporaryEmpty, hd 2 3 (by decide)]
    · by_cases ho : k = slots 3
      · subst k
        simp [MachineTransfer.tapesAt, resultTapes]
      · simp [MachineTransfer.tapesAt, resultTapes, middle, ht, ho]
  have second : (MachineComposition.advance (TM2.step program))^[(fieldBits field input).length + 1]
      (some ⟨some (labels .transfer), clean ambient, middle⟩) =
      some ⟨exit, clean ambient, resultTapes field slots base input⟩ := by
    have run := MachineTransfer.transferAt_fromTapes
      (Γ := MachineTransducerCopy.Alphabet K) (σ := σ × Mode) (slots 2) (slots 3)
      (hd 2 3 (by decide)) id false (labels .transfer) exit program
      (by simpa only [instruction] using atLabels .transfer) middle (ambient, initial) none
    have next_eq : MachineTransfer.nextAt (slots 3) program =
        MachineComposition.advance (TM2.step program) := by
      funext configuration
      rfl
    rw [next_eq, middleSource, middleDestination] at run
    simpa only [List.length_reverse, List.reverse_reverse, List.map_id,
      finalTapes, clean] using run
  rw [steps, Nat.add_comm, Function.iterate_add_apply, first, second]

def extractInTime (field : Field) (slots : Fin 4 ↪ K) (labels : Label → Λ)
    (exit : Option Λ)
    (program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State σ))
    (atLabels : ∀ label, program (labels label) = instruction field slots labels exit label)
    (base : K → List Bool) (input : GraphReductionInput)
    (sourceWord : base (slots 0) = graphBits input)
    (scratchEmpty : base (slots 1) = []) (temporaryEmpty : base (slots 2) = [])
    (ambient : σ) :
    StateTransition.EvalsToInTime (TM2.step program)
      ⟨some (labels .scan), clean ambient, base⟩
      (some ⟨exit, clean ambient, resultTapes field slots base input⟩)
      (4 * (graphBits input).length + 3) where
  steps := steps field input
  evals_in_steps := extractTrace field slots labels exit program atLabels base input
    sourceWord scratchEmpty temporaryEmpty ambient
  steps_le_m := steps_le field input

end BinPackingGap.GraphFieldMachine

namespace BinPackingGap.GraphTokenMachine

open Turing
open BinaryEncoding
open BinPackingGames.Foundations.Complexity
open BinPackingGames.Reduction
open GraphTallyMachine (Mode Slot afterName initial transition)

inductive Field
  | vertex
  | edge
  deriving DecidableEq

instance : Fintype Field where
  elems := {.vertex, .edge}
  complete field := by cases field <;> simp

def emit : Field → Mode → Bool → List Bool
  | .vertex, .vertices, true => [true]
  | .edge, .endpoints, true => [true]
  | _, _, _ => []

def scanOutput (field : Field) : Mode → List Bool → List Bool :=
  MachineTransducer.output transition (emit field)

@[simp] theorem scanOutput_nil (field : Field) (mode : Mode) :
    scanOutput field mode [] = [] := rfl

theorem scanOutput_cons (field : Field) (mode : Mode) (bit : Bool) (rest : List Bool) :
    scanOutput field mode (bit :: rest) =
      emit field mode bit ++ scanOutput field (transition mode bit) rest := rfl

theorem scanOutput_done (field : Field) (input : List Bool) :
    scanOutput field .done input = [] := by
  induction input with
  | nil => rfl
  | cons bit rest ih =>
      cases field <;> simp only [scanOutput_cons, transition, emit, List.nil_append, ih]

theorem scanOutput_frame (field : Field) (slot : Slot) (bits suffix : List Bool) :
    scanOutput field (.name slot)
        (BinPackingCompleteness.BinaryEncoding.frame bits ++ suffix) =
      scanOutput field (afterName slot) suffix := by
  induction bits with
  | nil => cases field <;> rfl
  | cons bit bits ih =>
      cases field <;>
        simpa only [BinPackingCompleteness.BinaryEncoding.frame, List.cons_append,
          scanOutput_cons, transition, emit, List.nil_append] using ih

theorem scanOutput_natBits (field : Field) (slot : Slot) (n : Nat) (suffix : List Bool) :
    scanOutput field (.name slot) (natBits n ++ suffix) =
      scanOutput field (afterName slot) suffix :=
  scanOutput_frame field slot n.bits suffix

theorem scanOutput_pairBits (field : Field) (edge : Nat × Nat) (suffix : List Bool) :
    scanOutput field (.name .edgeFirst) (pairBits natBits natBits edge ++ suffix) =
      scanOutput field .endpoints suffix := by
  simp only [pairBits, List.append_assoc, scanOutput_natBits, afterName]

theorem scanOutput_vertices (field : Field) (vertices : List Nat) (suffix : List Bool) :
    scanOutput field .vertices (listBits natBits vertices ++ suffix) =
      (if field = .vertex then List.replicate vertices.length true else []) ++
        scanOutput field .endpoints suffix := by
  induction vertices with
  | nil => cases field <;> rfl
  | cons vertex vertices ih =>
      cases field <;>
        simp [listBits, List.cons_append, List.append_assoc, scanOutput_cons,
          transition, emit, scanOutput_natBits, afterName, ih, List.replicate_succ]

theorem scanOutput_endpoints (field : Field) (endpoints : List (Nat × Nat))
    (suffix : List Bool) :
    scanOutput field .endpoints (listBits (pairBits natBits natBits) endpoints ++ suffix) =
      (if field = .edge then List.replicate endpoints.length true else []) ++
        scanOutput field .done suffix := by
  induction endpoints with
  | nil => cases field <;> rfl
  | cons edge endpoints ih =>
      cases field <;>
        simp [listBits, List.cons_append, List.append_assoc, scanOutput_cons,
          transition, emit, scanOutput_pairBits, ih, List.replicate_succ]

def fieldsCount : Field → GraphFields → Nat
  | .vertex, fields => fields.vertices.length
  | .edge, fields => fields.endpoints.length

def count : Field → GraphReductionInput → Nat
  | .vertex, input => input.graph.n
  | .edge, input => input.graph.edges.length

def tokenBits (field : Field) (input : GraphReductionInput) : List Bool :=
  List.replicate (count field input) true

@[simp] theorem count_vertex (input : GraphReductionInput) :
    count .vertex input = input.graph.n := rfl

@[simp] theorem count_edge (input : GraphReductionInput) :
    count .edge input = input.graph.edges.length := rfl

@[simp] theorem tokenBits_length (field : Field) (input : GraphReductionInput) :
    (tokenBits field input).length = count field input := by simp [tokenBits]

@[simp] theorem tokenBits_reverse (field : Field) (input : GraphReductionInput) :
    (tokenBits field input).reverse = tokenBits field input := by simp [tokenBits]

theorem output_graphFields (field : Field) (fields : GraphFields) :
    scanOutput field initial fields.bits = List.replicate (fieldsCount field fields) true := by
  have hend := scanOutput_endpoints field fields.endpoints []
  simp only [List.append_nil, scanOutput_nil] at hend
  cases field <;>
    simp [GraphFields.bits, List.append_assoc, initial, scanOutput_natBits,
      afterName, scanOutput_vertices, fieldsCount, hend]

theorem fieldsCount_fields (field : Field) (input : GraphReductionInput) :
    fieldsCount field input.fields = count field input := by
  cases field <;> simp [fieldsCount, count, GraphReductionInput.fields]

theorem output_graphBits (field : Field) (input : GraphReductionInput) :
    scanOutput field initial (graphBits input) = tokenBits field input := by
  simpa only [graphBits, fieldsCount_fields, tokenBits] using
    output_graphFields field input.fields

theorem count_le_bits (field : Field) (input : GraphReductionInput) :
    count field input ≤ (graphBits input).length := by
  cases field with
  | vertex => exact graph_vertexCount_le_bits input
  | edge => exact graph_edgeCount_le_bits input

theorem emit_length_le (field : Field) (mode : Mode) (bit : Bool) :
    (emit field mode bit).length ≤ 1 := by
  cases field <;> cases mode <;> cases bit <;> simp [emit]

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

abbrev State (σ : Type) := MachineTransducerCopy.State σ Mode

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

inductive Label
  | scan
  | restore
  | emitter (mode : Mode) (bit : Bool)
  deriving DecidableEq, Fintype

def instruction (field : Field) (slots : Fin 3 ↪ K) (labels : Label → Λ)
    (exit : Option Λ) : Label → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State σ)
  | .scan => MachineTransducerCopy.scanLoop (slots 0) (slots 1) initial
      (fun mode bit => labels (.emitter mode bit)) (labels .restore)
  | .emitter mode bit => MachineTransducerCopy.emitter (slots 2) transition (emit field)
      (labels .scan) mode bit
  | .restore => MachineTransfer.loopAt (slots 1) (slots 0) id false
      (labels .restore) exit

def resultTapes (field : Field) (slots : Fin 3 ↪ K) (base : K → List Bool)
    (input : GraphReductionInput) : K → List Bool :=
  Function.update base (slots 2) (tokenBits field input ++ base (slots 2))

theorem resultTapes_other (field : Field) (slots : Fin 3 ↪ K) (base : K → List Bool)
    (input : GraphReductionInput) (k : K) (hne : k ≠ slots 2) :
    resultTapes field slots base input k = base k := by simp [resultTapes, hne]

@[simp] theorem resultTapes_destination (field : Field) (slots : Fin 3 ↪ K)
    (base : K → List Bool) (input : GraphReductionInput) :
    resultTapes field slots base input (slots 2) = tokenBits field input ++ base (slots 2) := by
  simp [resultTapes]

theorem tokensTrace (field : Field) (slots : Fin 3 ↪ K) (labels : Label → Λ)
    (exit : Option Λ)
    (program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State σ))
    (atLabels : ∀ label, program (labels label) = instruction field slots labels exit label)
    (base : K → List Bool) (input : GraphReductionInput)
    (sourceWord : base (slots 0) = graphBits input) (scratchEmpty : base (slots 1) = [])
    (ambient : σ) :
    (MachineComposition.advance (TM2.step program))^[3 * (graphBits input).length + 2]
      (some ⟨some (labels .scan), clean ambient, base⟩) =
      some ⟨exit, clean ambient, resultTapes field slots base input⟩ := by
  have hd (i j : Fin 3) (hne : i ≠ j) : slots i ≠ slots j := slots.injective.ne hne
  have run := MachineTransducerCopy.transduceCopyTrace (slots 0) (slots 1) (slots 2)
    (hd 0 1 (by decide)) (hd 0 2 (by decide)) (hd 1 2 (by decide))
    initial transition (emit field) (labels .scan) (labels .restore)
    (fun mode bit => labels (.emitter mode bit)) exit program
    (atLabels .scan) (fun mode bit => atLabels (.emitter mode bit))
    (atLabels .restore) base scratchEmpty ambient initial none
  have hout : MachineTransducer.output transition (emit field) initial (base (slots 0)) =
      tokenBits field input := by
    change scanOutput field initial (base (slots 0)) = _
    rw [sourceWord, output_graphBits]
  rw [hout] at run
  simpa only [sourceWord, tokenBits_reverse, clean, resultTapes] using run

def tokensInTime (field : Field) (slots : Fin 3 ↪ K) (labels : Label → Λ)
    (exit : Option Λ)
    (program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State σ))
    (atLabels : ∀ label, program (labels label) = instruction field slots labels exit label)
    (base : K → List Bool) (input : GraphReductionInput)
    (sourceWord : base (slots 0) = graphBits input) (scratchEmpty : base (slots 1) = [])
    (ambient : σ) :
    StateTransition.EvalsToInTime (TM2.step program)
      ⟨some (labels .scan), clean ambient, base⟩
      (some ⟨exit, clean ambient, resultTapes field slots base input⟩)
      (3 * (graphBits input).length + 2) where
  steps := 3 * (graphBits input).length + 2
  evals_in_steps := tokensTrace field slots labels exit program atLabels base input
    sourceWord scratchEmpty ambient
  steps_le_m := Nat.le_refl _

theorem resultTapes_counter (field : Field) (slots : Fin 3 ↪ K)
    (base : K → List Bool) (input : GraphReductionInput)
    (destinationTerminated : base (slots 2) = [false]) :
    resultTapes field slots base input =
      Function.update base (slots 2) (encodeWord (count field input)) := by
  simp only [resultTapes, destinationTerminated, tokenBits, encodeWord]

def counterInTime (field : Field) (slots : Fin 3 ↪ K) (labels : Label → Λ)
    (exit : Option Λ)
    (program : Λ → TM2.Stmt (MachineTransducerCopy.Alphabet K) Λ (State σ))
    (atLabels : ∀ label, program (labels label) = instruction field slots labels exit label)
    (base : K → List Bool) (input : GraphReductionInput)
    (sourceWord : base (slots 0) = graphBits input) (scratchEmpty : base (slots 1) = [])
    (destinationTerminated : base (slots 2) = [false]) (ambient : σ) :
    StateTransition.EvalsToInTime (TM2.step program)
      ⟨some (labels .scan), clean ambient, base⟩
      (some ⟨exit, clean ambient,
        Function.update base (slots 2) (encodeWord (count field input))⟩)
      (3 * (graphBits input).length + 2) := by
  have run := tokensInTime field slots labels exit program atLabels base input
    sourceWord scratchEmpty ambient
  rw [resultTapes_counter field slots base input destinationTerminated] at run
  exact run

end BinPackingGap.GraphTokenMachine

namespace BinPackingGap.GraphPreparationMachine

open Turing
open BinPackingGames.Foundations.Complexity

abbrev State (A : Type) := GraphFieldMachine.State A

inductive Label
  | threshold (label : GraphFieldMachine.Label)
  | endpoints (label : GraphFieldMachine.Label)
  | vertices (label : GraphTokenMachine.Label)
  | edges (label : GraphTokenMachine.Label)
  deriving DecidableEq, Fintype

def fieldIndices (edge : Bool) : Fin 4 ↪ Fin 7 where
  toFun := if edge then ![0, 1, 2, 4] else ![0, 1, 2, 3]
  inj' := by cases edge <;> decide

def tokenIndices (edge : Bool) : Fin 3 ↪ Fin 7 where
  toFun := if edge then ![0, 1, 6] else ![0, 1, 5]
  inj' := by cases edge <;> decide

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

def fieldSlots (slots : Fin 7 ↪ K) (edge : Bool) : Fin 4 ↪ K :=
  (fieldIndices edge).trans slots

def tokenSlots (slots : Fin 7 ↪ K) (edge : Bool) : Fin 3 ↪ K :=
  (tokenIndices edge).trans slots

omit [DecidableEq K] in
@[simp] theorem fieldSlots_zero [DecidableEq K] (slots : Fin 7 ↪ K) (edge : Bool) :
    fieldSlots slots edge 0 = slots 0 := by cases edge <;> rfl

omit [DecidableEq K] in
@[simp] theorem fieldSlots_one [DecidableEq K] (slots : Fin 7 ↪ K) (edge : Bool) :
    fieldSlots slots edge 1 = slots 1 := by cases edge <;> rfl

omit [DecidableEq K] in
@[simp] theorem fieldSlots_two [DecidableEq K] (slots : Fin 7 ↪ K) (edge : Bool) :
    fieldSlots slots edge 2 = slots 2 := by cases edge <;> rfl

omit [DecidableEq K] in
@[simp] theorem fieldSlots_three [DecidableEq K] (slots : Fin 7 ↪ K) (edge : Bool) :
    fieldSlots slots edge 3 = slots (if edge then 4 else 3) := by cases edge <;> rfl

omit [DecidableEq K] in
@[simp] theorem tokenSlots_zero [DecidableEq K] (slots : Fin 7 ↪ K) (edge : Bool) :
    tokenSlots slots edge 0 = slots 0 := by cases edge <;> rfl

omit [DecidableEq K] in
@[simp] theorem tokenSlots_one [DecidableEq K] (slots : Fin 7 ↪ K) (edge : Bool) :
    tokenSlots slots edge 1 = slots 1 := by cases edge <;> rfl

omit [DecidableEq K] in
@[simp] theorem tokenSlots_two [DecidableEq K] (slots : Fin 7 ↪ K) (edge : Bool) :
    tokenSlots slots edge 2 = slots (if edge then 6 else 5) := by cases edge <;> rfl

def instruction (slots : Fin 7 ↪ K) (labels : Label → Λ) (exit : Option Λ) :
    Label → TM2.Stmt (fun _ : K => Bool) Λ (State A)
  | .threshold l => GraphFieldMachine.instruction .threshold (fieldSlots slots false)
      (fun l => labels (.threshold l)) (some (labels (.endpoints .scan))) l
  | .endpoints l => GraphFieldMachine.instruction .endpoints (fieldSlots slots true)
      (fun l => labels (.endpoints l)) (some (labels (.vertices .scan))) l
  | .vertices l => GraphTokenMachine.instruction .vertex (tokenSlots slots false)
      (fun l => labels (.vertices l)) (some (labels (.edges .scan))) l
  | .edges l => GraphTokenMachine.instruction .edge (tokenSlots slots true)
      (fun l => labels (.edges l)) exit l

def preparedTapes (slots : Fin 7 ↪ K) (base : K → List Bool)
    (input : GraphReductionInput) : K → List Bool :=
  GraphTokenMachine.resultTapes .edge (tokenSlots slots true)
    (GraphTokenMachine.resultTapes .vertex (tokenSlots slots false)
      (GraphFieldMachine.resultTapes .endpoints (fieldSlots slots true)
        (GraphFieldMachine.resultTapes .threshold (fieldSlots slots false) base input)
        input) input) input

def steps (input : GraphReductionInput) : Nat :=
  GraphFieldMachine.steps .threshold input + GraphFieldMachine.steps .endpoints input +
    (3 * (graphBits input).length + 2) + (3 * (graphBits input).length + 2)

theorem preparedTapes_register (slots : Fin 7 ↪ K) (base : K → List Bool)
    (input : GraphReductionInput) (j : Fin 7) :
    preparedTapes slots base input (slots j) =
      match j.val with
      | 3 => GraphFieldMachine.fieldBits .threshold input ++ base (slots 3)
      | 4 => GraphFieldMachine.fieldBits .endpoints input ++ base (slots 4)
      | 5 => List.replicate input.graph.n true ++ base (slots 5)
      | 6 => List.replicate input.graph.edges.length true ++ base (slots 6)
      | _ => base (slots j) := by
  fin_cases j <;>
    simp [preparedTapes, GraphFieldMachine.resultTapes, GraphTokenMachine.resultTapes,
      GraphTokenMachine.tokenBits, GraphTokenMachine.count, slots.injective.eq_iff]

theorem preparedTapes_other (slots : Fin 7 ↪ K) (base : K → List Bool)
    (input : GraphReductionInput) (k : K)
    (h₃ : k ≠ slots 3) (h₄ : k ≠ slots 4) (h₅ : k ≠ slots 5) (h₆ : k ≠ slots 6) :
    preparedTapes slots base input k = base k := by
  simp [preparedTapes, GraphFieldMachine.resultTapes, GraphTokenMachine.resultTapes,
    h₃, h₄, h₅, h₆]

theorem steps_le (input : GraphReductionInput) :
    steps input ≤ 14 * (graphBits input).length + 10 := by
  have hk := GraphFieldMachine.steps_le .threshold input
  have he := GraphFieldMachine.steps_le .endpoints input
  unfold steps
  omega

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 preparationTrace (slots : Fin 7 ↪ K) (labels : Label → Λ) (exit : Option Λ)
    (P : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State A))
    (atLabels : ∀ l, P (labels l) = instruction slots labels exit l)
    (base : K → List Bool) (input : GraphReductionInput)
    (graphWord : base (slots 0) = graphBits input)
    (scratchEmpty : base (slots 1) = []) (temporaryEmpty : base (slots 2) = [])
    (ambient : A) :
    (MachineComposition.advance (TM2.step P))^[steps input]
      (some ⟨some (labels (.threshold .scan)), GraphFieldMachine.clean ambient, base⟩) =
      some ⟨exit, GraphFieldMachine.clean ambient, preparedTapes slots base input⟩ := by
  have hd (i j : Fin 7) (h : i ≠ j) : slots i ≠ slots j := slots.injective.ne h
  let b₁ := GraphFieldMachine.resultTapes .threshold (fieldSlots slots false) base input
  let b₂ := GraphFieldMachine.resultTapes .endpoints (fieldSlots slots true) b₁ input
  let b₃ := GraphTokenMachine.resultTapes .vertex (tokenSlots slots false) b₂ input
  have g₁ : b₁ (slots 0) = graphBits input := by
    simp [b₁, GraphFieldMachine.resultTapes, hd 0 3 (by decide), graphWord]
  have s₁ : b₁ (slots 1) = [] := by
    simp [b₁, GraphFieldMachine.resultTapes, hd 1 3 (by decide), scratchEmpty]
  have t₁ : b₁ (slots 2) = [] := by
    simp [b₁, GraphFieldMachine.resultTapes, hd 2 3 (by decide), temporaryEmpty]
  have g₂ : b₂ (slots 0) = graphBits input := by
    simp [b₂, GraphFieldMachine.resultTapes, hd 0 4 (by decide), g₁]
  have s₂ : b₂ (slots 1) = [] := by
    simp [b₂, GraphFieldMachine.resultTapes, hd 1 4 (by decide), s₁]
  have g₃ : b₃ (slots 0) = graphBits input := by
    simp [b₃, GraphTokenMachine.resultTapes, hd 0 5 (by decide), g₂]
  have s₃ : b₃ (slots 1) = [] := by
    simp [b₃, GraphTokenMachine.resultTapes, hd 1 5 (by decide), s₂]
  have r₁ := GraphFieldMachine.extractTrace .threshold (fieldSlots slots false)
    (fun l => labels (.threshold l)) (some (labels (.endpoints .scan))) P
    (fun l => atLabels (.threshold l)) base input
    (by simpa using graphWord) (by simpa using scratchEmpty)
    (by simpa using temporaryEmpty) ambient
  have r₂ := GraphFieldMachine.extractTrace .endpoints (fieldSlots slots true)
    (fun l => labels (.endpoints l)) (some (labels (.vertices .scan))) P
    (fun l => atLabels (.endpoints l)) b₁ input
    (by simpa using g₁) (by simpa using s₁) (by simpa using t₁) ambient
  have r₃ := GraphTokenMachine.tokensTrace .vertex (tokenSlots slots false)
    (fun l => labels (.vertices l)) (some (labels (.edges .scan))) P
    (fun l => atLabels (.vertices l)) b₂ input
    (by simpa using g₂) (by simpa using s₂) ambient
  have r₄ := GraphTokenMachine.tokensTrace .edge (tokenSlots slots true)
    (fun l => labels (.edges l)) exit P (fun l => atLabels (.edges l)) b₃ input
    (by simpa using g₃) (by simpa using s₃) ambient
  exact joinTrace (joinTrace (joinTrace r₁ r₂) r₃) r₄

def preparationInTime (slots : Fin 7 ↪ K) (labels : Label → Λ) (exit : Option Λ)
    (P : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State A))
    (atLabels : ∀ l, P (labels l) = instruction slots labels exit l)
    (base : K → List Bool) (input : GraphReductionInput)
    (graphWord : base (slots 0) = graphBits input)
    (scratchEmpty : base (slots 1) = []) (temporaryEmpty : base (slots 2) = [])
    (ambient : A) :
    StateTransition.EvalsToInTime (TM2.step P)
      ⟨some (labels (.threshold .scan)), GraphFieldMachine.clean ambient, base⟩
      (some ⟨exit, GraphFieldMachine.clean ambient, preparedTapes slots base input⟩)
      (14 * (graphBits input).length + 10) where
  steps := steps input
  evals_in_steps := preparationTrace slots labels exit P atLabels base input
    graphWord scratchEmpty temporaryEmpty ambient
  steps_le_m := steps_le input

end BinPackingGap.GraphPreparationMachine

namespace BinPackingGap.PackingMachineBlocks

open Turing
open BinPackingGames.Foundations.Complexity
open FiniteTapeProgram

inductive State
  | graph (state : GraphFieldMachine.State Unit)
  | arithmetic (state : BinaryAddMachine.State Unit)
  | emission (state : MachineFieldTemplate.State Unit)
  deriving DecidableEq, Fintype

def graphControl : MachineControlEmbedding.Control (GraphFieldMachine.State Unit) State where
  put := State.graph
  get
    | .graph s => s
    | _ => GraphFieldMachine.clean ()
  get_put _ := rfl

def arithmeticControl : MachineControlEmbedding.Control (BinaryAddMachine.State Unit) State where
  put := State.arithmetic
  get
    | .arithmetic s => s
    | _ => BinaryAddMachine.clean ()
  get_put _ := rfl

def emissionControl : MachineControlEmbedding.Control (MachineFieldTemplate.State Unit) State where
  put := State.emission
  get
    | .emission s => s
    | _ => (((), ()), none)
  get_put _ := rfl

variable {K : Type} [DecidableEq K]

def invoke {σ L : Type} [Fintype L] (control : MachineControlEmbedding.Control σ State)
    (initial : σ) (main : L) (program : L → TM2.Stmt (fun _ : K => Bool) L σ) :
    Code (K := K) (S := State) :=
  .seq (.atom (.load (fun _ => control.put initial) .done))
    (.routine L inferInstance main
      (fun l => MachineControlEmbedding.statement control id none (program l)))

theorem invokeExec {σ L : Type} [Fintype L]
    (control : MachineControlEmbedding.Control σ State)
    (initial : σ) (main : L) (program : L → TM2.Stmt (fun _ : K => Bool) L σ)
    (start : Data K State) (finishState : σ) (finishTapes : K → List Bool) (steps : Nat)
    (run : (MachineComposition.advance (TM2.step program))^[steps]
      (some ⟨some main, initial, start.tapes⟩) =
      some ⟨none, finishState, finishTapes⟩) :
    Exec (invoke control initial main program) start (1 + steps)
      ⟨control.put finishState, finishTapes⟩ := by
  refine Exec.seq (Exec.atom (.load (fun _ => control.put initial) .done) start) ?_
  apply Exec.routine inferInstance main
    (fun l => MachineControlEmbedding.statement control id none (program l))
  have translated := MachineControlEmbedding.trace control id none program
    (fun l => MachineControlEmbedding.statement control id none (program l))
    (fun _ => rfl) steps ⟨some main, initial, start.tapes⟩
    ⟨none, finishState, finishTapes⟩ run
  simpa only [Action.eval, MachineControlEmbedding.configuration,
    MachineSubroutine.label, id_eq] using translated

theorem invokeExecInTime {σ L : Type} [Fintype L]
    (control : MachineControlEmbedding.Control σ State)
    (initial : σ) (main : L) (program : L → TM2.Stmt (fun _ : K => Bool) L σ)
    (start : Data K State) (finishState : σ) (finishTapes : K → List Bool) (budget : Nat)
    (run : StateTransition.EvalsToInTime (TM2.step program)
      ⟨some main, initial, start.tapes⟩ (some ⟨none, finishState, finishTapes⟩) budget) :
    ∃ steps ≤ budget + 1, Exec (invoke control initial main program) start steps
      ⟨control.put finishState, finishTapes⟩ := by
  refine ⟨1 + run.steps, ?_, ?_⟩
  · have h := run.steps_le_m
    omega
  · exact invokeExec control initial main program start finishState finishTapes
      run.steps run.evals_in_steps

def prepareGraph (slots : Fin 7 ↪ K) : Code (K := K) (S := State) :=
  invoke graphControl (GraphFieldMachine.clean ()) (.threshold .scan)
    (GraphPreparationMachine.instruction slots id none)

theorem prepareGraph_exec (slots : Fin 7 ↪ K) (start : Data K State)
    (input : GraphReductionInput) (graphWord : start.tapes (slots 0) = graphBits input)
    (scratchEmpty : start.tapes (slots 1) = []) (temporaryEmpty : start.tapes (slots 2) = []) :
    Exec (prepareGraph slots) start (1 + GraphPreparationMachine.steps input)
      ⟨.graph (GraphFieldMachine.clean ()),
        GraphPreparationMachine.preparedTapes slots start.tapes input⟩ := by
  apply invokeExec
  exact GraphPreparationMachine.preparationTrace slots id none
    (GraphPreparationMachine.instruction slots id none) (fun _ => rfl)
    start.tapes input graphWord scratchEmpty temporaryEmpty ()

theorem prepareGraph_cost (input : GraphReductionInput) :
    1 + GraphPreparationMachine.steps input ≤ 14 * (graphBits input).length + 11 := by
  have h := GraphPreparationMachine.steps_le input
  omega

def emitPool (field : Fin 2 → K) (scratch output counter : K) :
    Code (K := K) (S := State) :=
  invoke emissionControl (((), ()), none) .guard
    (PackingPoolMachine.instruction field scratch output counter id none)

theorem emitPool_exec
    (field : Fin 2 → K) (scratch output counter : K)
    (fieldScratch : ∀ j, field j ≠ scratch) (fieldOutput : ∀ j, field j ≠ output)
    (fieldCounter : ∀ j, field j ≠ counter)
    (scratchOutput : scratch ≠ output) (scratchCounter : scratch ≠ counter)
    (outputCounter : output ≠ counter)
    (base : K → List Bool) (scratchEmpty : base scratch = [])
    (numerator denominator : List Bool)
    (numWord : base (field 0) = numerator) (denWord : base (field 1) = denominator)
    (startState : State) (suffix : List Bool) (n : Nat) :
    ∃ steps ≤ n * (3 * (numerator.length + denominator.length) + 9) + 3,
      Exec (emitPool field scratch output counter)
        ⟨startState, MachineUnaryCounter.counterTapes counter base n suffix⟩ steps
        ⟨.emission (((), ()), none), MachineUnaryCounter.counterTapes counter
          (Function.update base output
            (PackingPoolMachine.repeatWord
              (PackingPoolMachine.record numerator denominator).reverse n ++ base output))
          0 suffix⟩ := by
  have run := PackingPoolMachine.poolInTime field scratch output counter
    fieldScratch fieldOutput fieldCounter scratchOutput scratchCounter outputCounter
    id none (PackingPoolMachine.instruction field scratch output counter id none)
    (fun _ => rfl) base scratchEmpty numerator denominator numWord denWord () suffix n
  have lifted := invokeExecInTime emissionControl (((), ()), none) .guard
    (PackingPoolMachine.instruction field scratch output counter id none)
    ⟨startState, MachineUnaryCounter.counterTapes counter base n suffix⟩
    (((), ()), none) _ _ run
  simpa only [emitPool, emissionControl, Nat.add_assoc, Nat.reduceAdd] using lifted

end BinPackingGap.PackingMachineBlocks

namespace BinPackingGap.PackingArithmeticProgram

open FiniteTapeProgram PackingMachineBlocks

variable {K : Type} [DecidableEq K]

def code (body : Code (K := K) (S := BinaryAddMachine.State Unit)) :
    Code (K := K) (S := State) :=
  invoke arithmeticControl (BinaryAddMachine.clean ()) body.entry
    (body.instruction id none)

theorem exec (body : Code (K := K) (S := BinaryAddMachine.State Unit))
    (before after : K → List Bool) (steps : Nat) (state : State)
    (run : Exec body ⟨BinaryAddMachine.clean (), before⟩ steps
      ⟨BinaryAddMachine.clean (), after⟩) :
    Exec (code body) ⟨state, before⟩ (1 + steps)
      ⟨.arithmetic (BinaryAddMachine.clean ()), after⟩ := by
  exact invokeExec arithmeticControl (BinaryAddMachine.clean ()) body.entry
    (body.instruction id none) ⟨state, before⟩ (BinaryAddMachine.clean ()) after steps
    (run.trace (body.instruction id none) id none (fun _ => rfl))

theorem exec_in_time (body : Code (K := K) (S := BinaryAddMachine.State Unit))
    (before after : K → List Bool) (budget : Nat) (state : State)
    (run : ∃ steps ≤ budget, Exec body ⟨BinaryAddMachine.clean (), before⟩ steps
      ⟨BinaryAddMachine.clean (), after⟩) :
    ∃ steps ≤ budget + 1, Exec (code body) ⟨state, before⟩ steps
      ⟨.arithmetic (BinaryAddMachine.clean ()), after⟩ := by
  obtain ⟨steps, bound, actual⟩ := run
  exact ⟨1 + steps, by omega, exec body before after steps state actual⟩

end BinPackingGap.PackingArithmeticProgram

namespace BinPackingGap.PackingCountedProgram

open FiniteTapeProgram PackingMachineBlocks
open scoped BigOperators

variable {K : Type} [DecidableEq K]

def guardState (head : Option Bool) : State :=
  .arithmetic ((((), false), none), head)

def guard (counter : K) : Action K State :=
  .pop counter (fun _ head => guardState head) .done

def more : State → Bool
  | .arithmetic state => state.2.isSome
  | _ => false

def loop (counter : K) (body : Code (K := K) (S := State)) : Code (K := K) (S := State) :=
  .loop (guard counter) more body

def counterTapes (counter : K) (base : K → List Bool) (n : Nat) : K → List Bool :=
  Function.update base counter (List.replicate n true)

theorem guard_succ (counter : K) (base : K → List Bool) (n : Nat) (state : State) :
    (guard counter).eval ⟨state, counterTapes counter base (n + 1)⟩ =
      ⟨guardState (some true), counterTapes counter base n⟩ := by
  simp [guard, Action.eval, counterTapes, List.replicate_succ]

theorem guard_zero (counter : K) (base : K → List Bool) (state : State) :
    (guard counter).eval ⟨state, counterTapes counter base 0⟩ =
      ⟨guardState none, counterTapes counter base 0⟩ := by
  simp [guard, Action.eval, counterTapes]

def totalBudget (bodyCost : Nat → Nat) (n : Nat) : Nat :=
  (∑ r ∈ Finset.range n, bodyCost r) + n + 1

@[simp] theorem totalBudget_zero (bodyCost : Nat → Nat) : totalBudget bodyCost 0 = 1 := by
  simp [totalBudget]

theorem totalBudget_succ (bodyCost : Nat → Nat) (n : Nat) :
    totalBudget bodyCost (n + 1) = bodyCost n + totalBudget bodyCost n + 1 := by
  simp only [totalBudget, Finset.sum_range_succ]
  omega

theorem exec (counter : K) (body : Code (K := K) (S := State))
    (base : Nat → K → List Bool) (state : Nat → State) (bodyCost : Nat → Nat) (n : Nat)
    (bodyRuns : ∀ r, r < n → ∃ steps ≤ bodyCost r,
      Exec body ⟨guardState (some true), counterTapes counter (base (r + 1)) r⟩ steps
        ⟨state r, counterTapes counter (base r) r⟩) :
    ∃ steps ≤ totalBudget bodyCost n,
      Exec (loop counter body) ⟨state n, counterTapes counter (base n) n⟩ steps
        ⟨guardState none, counterTapes counter (base 0) 0⟩ := by
  revert bodyRuns
  induction n with
  | zero =>
      intro _
      refine ⟨1, by simp, ?_⟩
      have h : more ((guard counter).eval
          ⟨state 0, counterTapes counter (base 0) 0⟩).state = false := by
        rw [guard_zero]
        rfl
      simpa only [loop, guard_zero] using
        (Exec.loop_false (a := body) h)
  | succ n ih =>
      intro bodyRuns
      obtain ⟨tailSteps, htail, tailRun⟩ := ih (fun r hr => bodyRuns r (by omega))
      obtain ⟨bodySteps, hbody, bodyRun⟩ := bodyRuns n (by omega)
      refine ⟨bodySteps + tailSteps + 1, ?_, ?_⟩
      · rw [totalBudget_succ]
        omega
      · have h : more ((guard counter).eval
            ⟨state (n + 1), counterTapes counter (base (n + 1)) (n + 1)⟩).state = true := by
          rw [guard_succ]
          rfl
        have bodyRun' : Exec body ((guard counter).eval
            ⟨state (n + 1), counterTapes counter (base (n + 1)) (n + 1)⟩) bodySteps
            ⟨state n, counterTapes counter (base n) n⟩ := by
          rw [guard_succ]
          exact bodyRun
        exact Exec.loop_true h bodyRun' tailRun

theorem totalBudget_le (bodyCost : Nat → Nat) (n bound : Nat)
    (bounded : ∀ r, r < n → bodyCost r ≤ bound) :
    totalBudget bodyCost n ≤ n * (bound + 1) + 1 := by
  have hsum : (∑ r ∈ Finset.range n, bodyCost r) ≤ n * bound := by
    calc
      _ ≤ ∑ _r ∈ Finset.range n, bound :=
        Finset.sum_le_sum (fun r hr => bounded r (Finset.mem_range.mp hr))
      _ = _ := by simp
  simp only [totalBudget, Nat.mul_add, Nat.mul_one]
  omega

end BinPackingGap.PackingCountedProgram

end OAI