File size: 57,165 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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
import OAI.Computability.BinPacking.Arithmetic.BinaryOrderMachine
import OAI.Computability.BinPacking.Arithmetic.BinaryTallyMachine
import OAI.Computability.BinPacking.Search.SearchInitializeEncoding

namespace OAI

namespace BinPackingGap.ExtensionMachineRegisters

open Turing.TM2
open BinPackingGames.Foundations.Complexity

structure Lens (Big Small : Type) where
  get : Big → Small
  set : Big → Small → Big
  get_set : ∀ s v, get (set s v) = v
  set_get : ∀ s, set s (get s) = s
  set_set : ∀ s v w, set (set s v) w = set s w

namespace Lens

variable {Big Small Middle : Type}

def identity (State : Type) : Lens State State where
  get := id
  set := fun _ v => v
  get_set _ _ := rfl
  set_get _ := rfl
  set_set _ _ _ := rfl

def ofEquiv (e : Small ≃ Big) : Lens Big Small where
  get := e.symm
  set := fun _ v => e v
  get_set _ v := e.symm_apply_apply v
  set_get s := e.apply_symm_apply s
  set_set _ _ _ := rfl

def first (Left Right : Type) : Lens (Left × Right) Left where
  get := Prod.fst
  set := fun s v => (v, s.2)
  get_set _ _ := rfl
  set_get _ := rfl
  set_set _ _ _ := rfl

def second (Left Right : Type) : Lens (Left × Right) Right where
  get := Prod.snd
  set := fun s v => (s.1, v)
  get_set _ _ := rfl
  set_get _ := rfl
  set_set _ _ _ := rfl

def compose (outer : Lens Big Middle) (inner : Lens Middle Small) : Lens Big Small where
  get s := inner.get (outer.get s)
  set s v := outer.set s (inner.set (outer.get s) v)
  get_set s v := by rw [outer.get_set, inner.get_set]
  set_get s := by rw [inner.set_get, outer.set_get]
  set_set s v w := by rw [outer.get_set, inner.set_set, outer.set_set]

end Lens

variable {K Label Big Small : Type} {Γ : K → Type}

def statement (lens : Lens Big Small) : Stmt Γ Label Small → Stmt Γ Label Big
  | .push k f next => .push k (fun s => f (lens.get s)) (statement lens next)
  | .peek k f next => .peek k (fun s symbol => lens.set s (f (lens.get s) symbol))
      (statement lens next)
  | .pop k f next => .pop k (fun s symbol => lens.set s (f (lens.get s) symbol))
      (statement lens next)
  | .load f next => .load (fun s => lens.set s (f (lens.get s))) (statement lens next)
  | .branch f yes no => .branch (fun s => f (lens.get s))
      (statement lens yes) (statement lens no)
  | .goto f => .goto (fun s => f (lens.get s))
  | .halt => .halt

@[simp] theorem statement_identity (q : Stmt Γ Label Small) :
    statement (Lens.identity Small) q = q := by
  induction q <;> simp_all [statement, Lens.identity]

theorem statement_compose {Middle : Type} (outer : Lens Big Middle)
    (inner : Lens Middle Small) (q : Stmt Γ Label Small) :
    statement (Lens.compose outer inner) q = statement outer (statement inner q) := by
  induction q <;> simp_all [statement, Lens.compose]

@[simp] theorem statement_ofEquiv_symm (e : Small ≃ Big) (q : Stmt Γ Label Big) :
    statement (Lens.ofEquiv e) (statement (Lens.ofEquiv e.symm) q) = q := by
  induction q <;> simp_all [statement, Lens.ofEquiv]

def equivConfiguration (e : Small ≃ Big) (c : Cfg Γ Label Small) : Cfg Γ Label Big :=
  ⟨c.l, e c.var, c.stk⟩

def configuration (lens : Lens Big Small) (base : Big) (c : Cfg Γ Label Small) :
    Cfg Γ Label Big := ⟨c.l, lens.set base c.var, c.stk⟩

def project (lens : Lens Big Small) (c : Cfg Γ Label Big) : Cfg Γ Label Small :=
  ⟨c.l, lens.get c.var, c.stk⟩

@[simp] theorem configuration_ofEquiv (e : Small ≃ Big) (base : Big)
    (c : Cfg Γ Label Small) :
    configuration (Lens.ofEquiv e) base c = equivConfiguration e c := rfl

@[simp] theorem equivConfiguration_symm (e : Small ≃ Big) (c : Cfg Γ Label Big) :
    equivConfiguration e (equivConfiguration e.symm c) = c := by
  cases c
  simp [equivConfiguration]

@[simp] theorem project_configuration (lens : Lens Big Small) (base : Big)
    (c : Cfg Γ Label Small) : project lens (configuration lens base c) = c := by
  cases c
  simp [project, configuration, lens.get_set]

@[simp] theorem configuration_project (lens : Lens Big Small) (c : Cfg Γ Label Big) :
    configuration lens c.var (project lens c) = c := by
  cases c
  simp [project, configuration, lens.set_get]

variable [DecidableEq K]

theorem stepAux_transport (lens : Lens Big Small) (base : Big)
    (q : Stmt Γ Label Small) (state : Small) (tapes : ∀ k, List (Γ k)) :
    stepAux (statement lens q) (lens.set base state) tapes =
      configuration lens base (stepAux q state tapes) := by
  induction q generalizing state tapes with
  | push k f next ih =>
      simp only [statement, stepAux, lens.get_set]
      exact ih state (Function.update tapes k (f state :: tapes k))
  | peek k f next ih =>
      simpa only [statement, stepAux, lens.get_set, lens.set_set] using
        ih (f state (tapes k).head?) tapes
  | pop k f next ih =>
      simpa only [statement, stepAux, lens.get_set, lens.set_set] using
        ih (f state (tapes k).head?) (Function.update tapes k (tapes k).tail)
  | load f next ih =>
      simpa only [statement, stepAux, lens.get_set, lens.set_set] using ih (f state) tapes
  | branch f yes no ihYes ihNo =>
      cases test : f state with
      | false =>
          simpa only [statement, stepAux, lens.get_set, test, Bool.cond_false] using
            ihNo state tapes
      | true =>
          simpa only [statement, stepAux, lens.get_set, test, Bool.cond_true] using
            ihYes state tapes
  | goto f => simp [statement, stepAux, lens.get_set, configuration]
  | halt => rfl

theorem stepAux_transport_project (lens : Lens Big Small)
    (q : Stmt Γ Label Small) (state : Big) (tapes : ∀ k, List (Γ k)) :
    stepAux (statement lens q) state tapes =
      configuration lens state (stepAux q (lens.get state) tapes) := by
  simpa only [lens.set_get] using
    stepAux_transport lens state q (lens.get state) tapes

def program (lens : Lens Big Small) (source : Label → Stmt Γ Label Small) :
    Label → Stmt Γ Label Big := fun label => statement lens (source label)

omit [DecidableEq K] in
@[simp] theorem program_identity (source : Label → Stmt Γ Label Small) :
    program (Lens.identity Small) source = source := by
  funext label
  exact statement_identity (source label)

omit [DecidableEq K] in
@[simp] theorem program_ofEquiv_symm (e : Small ≃ Big)
    (target : Label → Stmt Γ Label Big) :
    program (Lens.ofEquiv e) (program (Lens.ofEquiv e.symm) target) = target := by
  funext label
  exact statement_ofEquiv_symm e (target label)

theorem step_transport (lens : Lens Big Small) (base : Big)
    (source : Label → Stmt Γ Label Small) (c : Cfg Γ Label Small) :
    step (program lens source) (configuration lens base c) =
      (step source c).map (configuration lens base) := by
  cases c with
  | mk label state tapes =>
      cases label with
      | none => rfl
      | some label =>
          change some (stepAux (statement lens (source label)) (lens.set base state) tapes) = _
          rw [stepAux_transport]
          rfl

theorem advance_transport (lens : Lens Big Small) (base : Big)
    (source : Label → Stmt Γ Label Small) (c : Option (Cfg Γ Label Small)) :
    MachineComposition.advance (step (program lens source)) (c.map (configuration lens base)) =
      (MachineComposition.advance (step source) c).map (configuration lens base) := by
  cases c with
  | none => rfl
  | some c => exact step_transport lens base source c

theorem iterate_transport (lens : Lens Big Small) (base : Big)
    (source : Label → Stmt Γ Label Small) (steps : Nat) (c : Option (Cfg Γ Label Small)) :
    (MachineComposition.advance (step (program lens source)))^[steps]
        (c.map (configuration lens base)) =
      ((MachineComposition.advance (step source))^[steps] c).map (configuration lens base) := by
  induction steps with
  | zero => rfl
  | succ steps ih =>
      rw [Function.iterate_succ_apply', ih, advance_transport,
        Function.iterate_succ_apply']

theorem trace (lens : Lens Big Small) (base : Big)
    (source : Label → Stmt Γ Label Small) (steps : Nat)
    (start finish : Cfg Γ Label Small)
    (run : (MachineComposition.advance (step source))^[steps] (some start) = some finish) :
    (MachineComposition.advance (step (program lens source)))^[steps]
      (some (configuration lens base start)) = some (configuration lens base finish) := by
  change (MachineComposition.advance (step (program lens source)))^[steps]
      ((some start).map (configuration lens base)) = (some finish).map (configuration lens base)
  rw [iterate_transport, run]

def execution (lens : Lens Big Small) (base : Big)
    (source : Label → Stmt Γ Label Small)
    {start : Cfg Γ Label Small} {finish : Option (Cfg Γ Label Small)} {budget : Nat}
    (run : StateTransition.EvalsToInTime (step source) start finish budget) :
    StateTransition.EvalsToInTime (step (program lens source))
      (configuration lens base start) (finish.map (configuration lens base)) budget where
  steps := run.steps
  evals_in_steps := by
    have executed := run.evals_in_steps
    change (MachineComposition.advance (step source))^[run.steps] (some start) = finish at executed
    change (MachineComposition.advance (step (program lens source)))^[run.steps]
      ((some start).map (configuration lens base)) = finish.map (configuration lens base)
    rw [iterate_transport, executed]
  steps_le_m := run.steps_le_m

def equivExecution (e : Small ≃ Big) (target : Label → Stmt Γ Label Big)
    {start : Cfg Γ Label Small} {finish : Option (Cfg Γ Label Small)} {budget : Nat}
    (run : StateTransition.EvalsToInTime
      (step (program (Lens.ofEquiv e.symm) target)) start finish budget) :
    StateTransition.EvalsToInTime (step target) (equivConfiguration e start)
      (finish.map (equivConfiguration e)) budget := by
  have mapped : configuration (Γ := Γ) (Label := Label) (Lens.ofEquiv e) (e start.var) =
      equivConfiguration e := by
    funext c
    exact configuration_ofEquiv e _ c
  simpa only [program_ofEquiv_symm, mapped] using
    execution (Lens.ofEquiv e) (e start.var)
      (program (Lens.ofEquiv e.symm) target) run

theorem equivTrace (e : Small ≃ Big) (target : Label → Stmt Γ Label Big)
    (steps : Nat) (start finish : Cfg Γ Label Small)
    (run : (MachineComposition.advance
      (step (program (Lens.ofEquiv e.symm) target)))^[steps] (some start) = some finish) :
    (MachineComposition.advance (step target))^[steps]
      (some (equivConfiguration e start)) = some (equivConfiguration e finish) := by
  simpa only [program_ofEquiv_symm, configuration_ofEquiv] using
    trace (Lens.ofEquiv e) (e start.var) (program (Lens.ofEquiv e.symm) target)
      steps start finish run

omit [DecidableEq K] in
theorem observer_configuration {Observed : Type} (lens : Lens Big Small)
    (observe : Big → Observed) (preserved : ∀ s v, observe (lens.set s v) = observe s)
    (base : Big) (c : Cfg Γ Label Small) :
    observe (configuration lens base c).var = observe base := preserved base c.var

end BinPackingGap.ExtensionMachineRegisters

namespace BinPackingGap.ExtensionMachinePlacement

open Turing BinPackingGames.Foundations.Complexity
open ExtensionMachineRegisters (Lens)

variable {K K' Label Label' Small Big : Type}

def tapes (view : K' → Option K) (source : K → List Bool)
    (extra : K' → List Bool) : K' → List Bool :=
  fun j => match view j with
    | some k => source k
    | none => extra j

def label (labels : Label → Label') (exit : Option Label') : Option Label → Option Label'
  | some l => some (labels l)
  | none => exit

def configuration (view : K' → Option K) (labels : Label → Label')
    (exit : Option Label') (lens : Lens Big Small) (base : Big)
    (extra : K' → List Bool) (c : TM2.Cfg (fun _ : K => Bool) Label Small) :
    TM2.Cfg (fun _ : K' => Bool) Label' Big :=
  ⟨label labels exit c.l, lens.set base c.var, tapes view c.stk extra⟩

def statement (tape : K → K') (labels : Label → Label') (exit : Option Label')
    (lens : Lens Big Small) :
    TM2.Stmt (fun _ : K => Bool) Label Small → TM2.Stmt (fun _ : K' => Bool) Label' Big
  | .push k f next => .push (tape k) (fun s => f (lens.get s))
      (statement tape labels exit lens next)
  | .peek k f next => .peek (tape k) (fun s bit => lens.set s (f (lens.get s) bit))
      (statement tape labels exit lens next)
  | .pop k f next => .pop (tape k) (fun s bit => lens.set s (f (lens.get s) bit))
      (statement tape labels exit lens next)
  | .load f next => .load (fun s => lens.set s (f (lens.get s)))
      (statement tape labels exit lens next)
  | .branch f yes no => .branch (fun s => f (lens.get s))
      (statement tape labels exit lens yes) (statement tape labels exit lens no)
  | .goto f => .goto (fun s => labels (f (lens.get s)))
  | .halt => match exit with
    | some l => .goto (fun _ => l)
    | none => .halt

@[simp] theorem tapes_apply (tape : K → K') (view : K' → Option K)
    (left : ∀ k, view (tape k) = some k)
    (source : K → List Bool) (extra : K' → List Bool) (k : K) :
    tapes view source extra (tape k) = source k := by
  simp only [tapes, left]

@[simp] theorem tapes_unmapped (view : K' → Option K)
    (source : K → List Bool) (extra : K' → List Bool) (j : K')
    (unmapped : view j = none) : tapes view source extra j = extra j := by
  simp only [tapes, unmapped]

theorem tapes_restrict (tape : K → K') (view : K' → Option K)
    (right : ∀ j k, view j = some k → tape k = j) (base : K' → List Bool) :
    tapes view (fun k => base (tape k)) base = base := by
  funext j
  cases found : view j with
  | none => simp [tapes, found]
  | some k => simp [tapes, found, right j k found]

variable [DecidableEq K] [DecidableEq K']

theorem tapes_update (tape : K → K') (view : K' → Option K)
    (left : ∀ k, view (tape k) = some k)
    (right : ∀ j k, view j = some k → tape k = j)
    (source : K → List Bool) (extra : K' → List Bool) (k : K) (word : List Bool) :
    tapes view (Function.update source k word) extra =
      Function.update (tapes view source extra) (tape k) word := by
  funext j
  by_cases same : j = tape k
  · subst j
    simp [tapes, left]
  · cases found : view j with
    | none => simp [tapes, found, same]
    | some i =>
      have distinct : i ≠ k := by
        intro equal
        subst i
        exact same (right j k found).symm
      simp [tapes, found, same, distinct]

theorem stepAux_simulation (tape : K → K') (view : K' → Option K)
    (left : ∀ k, view (tape k) = some k)
    (right : ∀ j k, view j = some k → tape k = j)
    (labels : Label → Label') (exit : Option Label') (lens : Lens Big Small)
    (base : Big) (extra : K' → List Bool)
    (q : TM2.Stmt (fun _ : K => Bool) Label Small)
    (state : Small) (source : K → List Bool) :
    TM2.stepAux (statement tape labels exit lens q) (lens.set base state)
        (tapes view source extra) =
      configuration view labels exit lens base extra (TM2.stepAux q state source) := by
  induction q generalizing state source with
  | push k f next ih =>
      simp only [statement, TM2.stepAux, lens.get_set, tapes_apply tape view left]
      rw [← tapes_update tape view left right]
      exact ih state (Function.update source k (f state :: source k))
  | peek k f next ih =>
      simpa only [statement, TM2.stepAux, lens.get_set, lens.set_set,
        tapes_apply tape view left] using ih (f state (source k).head?) source
  | pop k f next ih =>
      simp only [statement, TM2.stepAux, lens.get_set, lens.set_set,
        tapes_apply tape view left]
      rw [← tapes_update tape view left right]
      exact ih (f state (source k).head?) (Function.update source k (source k).tail)
  | load f next ih =>
      simpa only [statement, TM2.stepAux, lens.get_set, lens.set_set] using ih (f state) source
  | branch f yes no ihYes ihNo =>
      cases test : f state with
      | false =>
          simpa only [statement, TM2.stepAux, lens.get_set, test, Bool.cond_false] using
            ihNo state source
      | true =>
          simpa only [statement, TM2.stepAux, lens.get_set, test, Bool.cond_true] using
            ihYes state source
  | goto f => simp [statement, TM2.stepAux, lens.get_set, configuration, label]
  | halt => cases exit <;> rfl

theorem step_simulation (tape : K → K') (view : K' → Option K)
    (left : ∀ k, view (tape k) = some k)
    (right : ∀ j k, view j = some k → tape k = j)
    (labels : Label → Label') (exit : Option Label') (lens : Lens Big Small)
    (base : Big) (extra : K' → List Bool)
    (source : Label → TM2.Stmt (fun _ : K => Bool) Label Small)
    (target : Label' → TM2.Stmt (fun _ : K' => Bool) Label' Big)
    (atLabels : ∀ l, target (labels l) = statement tape labels exit lens (source l))
    (a b : TM2.Cfg (fun _ : K => Bool) Label Small)
    (run : TM2.step source a = some b) :
    TM2.step target (configuration view labels exit lens base extra a) =
      some (configuration view labels exit lens base extra b) := by
  cases a with
  | mk current state sourceTapes =>
      cases current with
      | none => simp [TM2.step] at run
      | some current =>
          have finish : TM2.stepAux (source current) state sourceTapes = b :=
            Option.some.inj run
          rw [← finish]
          change some (TM2.stepAux (target (labels current)) (lens.set base state)
            (tapes view sourceTapes extra)) = _
          rw [atLabels, stepAux_simulation tape view left right]

theorem trace (tape : K → K') (view : K' → Option K)
    (left : ∀ k, view (tape k) = some k)
    (right : ∀ j k, view j = some k → tape k = j)
    (labels : Label → Label') (exit : Option Label') (lens : Lens Big Small)
    (base : Big) (extra : K' → List Bool)
    (source : Label → TM2.Stmt (fun _ : K => Bool) Label Small)
    (target : Label' → TM2.Stmt (fun _ : K' => Bool) Label' Big)
    (atLabels : ∀ l, target (labels l) = statement tape labels exit lens (source l))
    (n : Nat) (a b : TM2.Cfg (fun _ : K => Bool) Label Small)
    (run : (MachineComposition.advance (TM2.step source))^[n] (some a) = some b) :
    (MachineComposition.advance (TM2.step target))^[n]
      (some (configuration view labels exit lens base extra a)) =
      some (configuration view labels exit lens base extra b) :=
  MachineComposition.liftSuccessfulTrace _ _ _
    (step_simulation tape view left right labels exit lens base extra source target atLabels)
    n a b run

def execution (tape : K → K') (view : K' → Option K)
    (left : ∀ k, view (tape k) = some k)
    (right : ∀ j k, view j = some k → tape k = j)
    (labels : Label → Label') (exit : Option Label') (lens : Lens Big Small)
    (base : Big) (extra : K' → List Bool)
    (source : Label → TM2.Stmt (fun _ : K => Bool) Label Small)
    (target : Label' → TM2.Stmt (fun _ : K' => Bool) Label' Big)
    (atLabels : ∀ l, target (labels l) = statement tape labels exit lens (source l))
    {start finish : TM2.Cfg (fun _ : K => Bool) Label Small} {budget : Nat}
    (run : StateTransition.EvalsToInTime (TM2.step source) start (some finish) budget) :
    StateTransition.EvalsToInTime (TM2.step target)
      (configuration view labels exit lens base extra start)
      (some (configuration view labels exit lens base extra finish)) budget :=
  MachineComposition.liftExecutionInTime _ _ _
    (step_simulation tape view left right labels exit lens base extra source target atLabels) run

omit [DecidableEq K] [DecidableEq K'] in
theorem configuration_observer {Observed : Type} (lens : Lens Big Small)
    (observe : Big → Observed) (preserved : ∀ s v, observe (lens.set s v) = observe s)
    (view : K' → Option K) (labels : Label → Label') (exit : Option Label')
    (base : Big) (extra : K' → List Bool)
    (c : TM2.Cfg (fun _ : K => Bool) Label Small) :
    observe (configuration view labels exit lens base extra c).var = observe base :=
  preserved base c.var

end BinPackingGap.ExtensionMachinePlacement

namespace BinPackingGap.ExtensionNatMachine

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

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

inductive Label
  | scan | restore
  deriving DecidableEq

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

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 scannerEquiv (A : Type) : BinPackingCompleteness.BinaryNameMachine.State (A × Bool) ≃ State A where
  toFun s := ((s.1, s.2.1), s.2.2)
  invFun s := (s.1.1, s.1.2, s.2)
  left_inv _ := rfl
  right_inv _ := rfl

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

def statement (slots : Fin 3 ↪ K) (labels : Label → Λ)
    (accepted rejected : Option Λ) : Label → TM2.Stmt (Alphabet (K := K)) Λ (State A)
  | .scan =>
      ExtensionMachineRegisters.statement
        (ExtensionMachineRegisters.Lens.ofEquiv (scannerEquiv A))
        (BinPackingCompleteness.BinaryNameMachine.scan (slots 0) (slots 2)
          (labels .scan) (some (labels .restore)) rejected)
  | .restore => loopAt (slots 2) (slots 1) id false (labels .restore) accepted

def resultTapes (slots : Fin 3 ↪ K) (base : K → List Bool) (n : Nat)
    (suffix : List Bool) : K → List Bool :=
  Function.update (Function.update base (slots 0) suffix) (slots 1) (n.bits ++ base (slots 1))

theorem natTrace (slots : Fin 3 ↪ K) (labels : Label → Λ)
    (accepted rejected : Option Λ) (p : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
    (atLabels : ∀ label, p (labels label) = statement slots labels accepted rejected label)
    (base : K → List Bool) (n : Nat) (suffix : List Bool)
    (input : base (slots 0) = BinaryEncoding.natBits n ++ suffix)
    (scratchEmpty : base (slots 2) = []) (ambient : A) :
    (MachineComposition.advance (TM2.step p))^[2 * n.size + 2]
      (some ⟨some (labels .scan), BinaryAddMachine.clean ambient, base⟩) =
      some ⟨accepted, BinaryAddMachine.clean ambient, resultTapes slots base n suffix⟩ := by
  let source := ExtensionMachineRegisters.program
    (ExtensionMachineRegisters.Lens.ofEquiv (scannerEquiv A).symm) p
  have atScan : source (labels .scan) =
      BinPackingCompleteness.BinaryNameMachine.scan (slots 0) (slots 2)
        (labels .scan) (some (labels .restore)) rejected := by
    change ExtensionMachineRegisters.statement
      (ExtensionMachineRegisters.Lens.ofEquiv (scannerEquiv A).symm) (p (labels .scan)) = _
    rw [atLabels .scan]
    exact ExtensionMachineRegisters.statement_ofEquiv_symm (scannerEquiv A).symm _
  have hd (i j : Fin 3) (h : i ≠ j) : slots i ≠ slots j := slots.injective.ne h
  have first := BinPackingCompleteness.BinaryNameMachine.framedTrace
    (slots 0) (slots 2) (hd 0 2 (by decide)) (labels .scan)
    (some (labels .restore)) rejected source atScan base (ambient, false)
    n.bits suffix [] none
  have canonical := BinPackingCompleteness.BinaryParsing.canonical_nat_bits n
  rw [canonical] at first
  simp only [↓reduceIte, List.append_nil] at first
  have translated := ExtensionMachineRegisters.equivTrace (scannerEquiv A) p _ _ _ first
  let afterScan := tapesAt (slots 0) (slots 2) base suffix n.bits.reverse
  have initial : tapesAt (slots 0) (slots 2) base
      (BinPackingCompleteness.BinaryNameMachine.frame n.bits ++ suffix) [] = base := by
    rw [BinPackingCompleteness.BinaryParsing.frame_eq]
    change tapesAt (slots 0) (slots 2) base (BinaryEncoding.natBits n ++ suffix) [] = base
    rw [← input, ← scratchEmpty]
    exact tapesAt_self _ _ _
  have translated' : (MachineComposition.advance (TM2.step p))^[n.size + 1]
      (some ⟨some (labels .scan), BinaryAddMachine.clean ambient, base⟩) =
      some ⟨some (labels .restore), BinaryAddMachine.clean ambient, afterScan⟩ := by
    simpa only [ExtensionMachineRegisters.equivConfiguration, scannerEquiv,
      Equiv.coe_fn_mk, BinPackingCompleteness.BinaryNameMachine.clean,
      BinaryAddMachine.clean, BinaryAddMachine.state, initial, afterScan,
      ← Nat.size_eq_bits_len] using translated
  have saved : afterScan (slots 2) = n.bits.reverse := by simp [afterScan, tapesAt]
  have destination : afterScan (slots 1) = base (slots 1) := by
    simp [afterScan, tapesAt, hd 1 0 (by decide), hd 1 2 (by decide)]
  have second := transferAt_fromTapes (slots 2) (slots 1) (hd 2 1 (by decide))
    (id : Bool → Bool) false (labels .restore) accepted p
    (by simpa only [statement] using atLabels .restore) afterScan
    ((ambient, false), none) none
  rw [saved, destination] at second
  have final : tapesAt (slots 2) (slots 1) afterScan []
      (n.bits ++ base (slots 1)) = resultTapes slots base n suffix := by
    funext k
    by_cases hout : k = slots 1
    · subst k; simp [tapesAt, resultTapes]
    by_cases hscratch : k = slots 2
    · subst k
      simp [tapesAt, resultTapes, hd 2 1 (by decide), hd 2 0 (by decide), scratchEmpty]
    simp [tapesAt, afterScan, resultTapes, hout, hscratch]
  have second' : (MachineComposition.advance (TM2.step p))^[n.size + 1]
      (some ⟨some (labels .restore), BinaryAddMachine.clean ambient, afterScan⟩) =
      some ⟨accepted, BinaryAddMachine.clean ambient, resultTapes slots base n suffix⟩ := by
    change (nextAt (Γ := Alphabet) (slots 1) p)^[n.size + 1]
      (some ⟨some (labels .restore), BinaryAddMachine.clean ambient, afterScan⟩) =
      some ⟨accepted, BinaryAddMachine.clean ambient, resultTapes slots base n suffix⟩
    simpa only [List.length_reverse, List.reverse_reverse, List.map_id_fun,
      id_eq, ← Nat.size_eq_bits_len, final, BinaryAddMachine.clean,
      BinaryAddMachine.state] using second
  rw [show 2 * n.size + 2 = (n.size + 1) + (n.size + 1) by omega,
    Function.iterate_add_apply, translated', second']

def natInTime (slots : Fin 3 ↪ K) (labels : Label → Λ)
    (accepted rejected : Option Λ) (p : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
    (atLabels : ∀ label, p (labels label) = statement slots labels accepted rejected label)
    (base : K → List Bool) (n : Nat) (suffix : List Bool)
    (input : base (slots 0) = BinaryEncoding.natBits n ++ suffix)
    (scratchEmpty : base (slots 2) = []) (ambient : A) :
    StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .scan), BinaryAddMachine.clean ambient, base⟩
      (some ⟨accepted, BinaryAddMachine.clean ambient, resultTapes slots base n suffix⟩)
      (2 * n.size + 2) where
  steps := 2 * n.size + 2
  evals_in_steps := natTrace slots labels accepted rejected p atLabels base n suffix
    input scratchEmpty ambient
  steps_le_m := Nat.le_refl _

def orderEquiv (A : Type) : BinaryOrderMachine.State (A × Bool) ≃ State (A × Ordering) where
  toFun
    | ((((ambient, carry), order), left), right) => ((((ambient, order), carry), left), right)
  invFun
    | ((((ambient, order), carry), left), right) => ((((ambient, carry), order), left), right)
  left_inv _ := rfl
  right_inv _ := rfl

def orderStatement (slots : Fin 5 ↪ K) (labels : BinaryOrderMachine.PreserveLabel → Λ)
    (exits : Ordering → Option Λ) (label : BinaryOrderMachine.PreserveLabel) :
    TM2.Stmt (Alphabet (K := K)) Λ (State (A × Ordering)) :=
  ExtensionMachineRegisters.statement
    (ExtensionMachineRegisters.Lens.ofEquiv (orderEquiv A))
    (BinaryOrderMachine.preservingInstruction slots labels exits label)

def preservingOrderInTime (slots : Fin 5 ↪ K)
    (labels : BinaryOrderMachine.PreserveLabel → Λ) (exits : Ordering → Option Λ)
    (p : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State (A × Ordering)))
    (atLabels : ∀ label, p (labels label) = orderStatement slots labels exits label)
    (base : K → List Bool) (a b : Nat)
    (leftWord : base (slots 0) = a.bits) (rightWord : base (slots 1) = b.bits)
    (leftEmpty : base (slots 2) = []) (rightEmpty : base (slots 3) = [])
    (scratchEmpty : base (slots 4) = []) (ambient : A) :
    StateTransition.EvalsToInTime (TM2.step p)
      ⟨some (labels .leftOut), BinaryAddMachine.clean (ambient, .eq), base⟩
      (some ⟨exits (BinaryOrderMachine.orderNat a b .eq),
        BinaryAddMachine.clean (ambient, .eq), base⟩)
      (BinaryOrderMachine.preservingSteps a.size b.size) := by
  let source := ExtensionMachineRegisters.program
    (ExtensionMachineRegisters.Lens.ofEquiv (orderEquiv A).symm) p
  have atSource : ∀ label, source (labels label) =
      BinaryOrderMachine.preservingInstruction slots labels exits label := by
    intro label
    change ExtensionMachineRegisters.statement
      (ExtensionMachineRegisters.Lens.ofEquiv (orderEquiv A).symm) (p (labels label)) = _
    rw [atLabels]
    exact ExtensionMachineRegisters.statement_ofEquiv_symm (orderEquiv A).symm _
  have run := BinaryOrderMachine.preservingNatInTime slots labels exits source atSource
    base a b leftWord rightWord leftEmpty rightEmpty scratchEmpty (ambient, false)
  have lifted := ExtensionMachineRegisters.equivExecution (orderEquiv A) p run
  simpa only [ExtensionMachineRegisters.equivConfiguration, orderEquiv,
    Equiv.coe_fn_mk, BinaryOrderMachine.clean, BinaryAddMachine.clean,
    BinaryAddMachine.state, Option.map_some] using lifted

end BinPackingGap.ExtensionNatMachine

namespace BinPackingGap.SearchInitialItemsMachine

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

inductive Number
  | numerator | denominator
  deriving DecidableEq

protected abbrev Number.enumList : List Number := [.numerator, .denominator]

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

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

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

inductive Control
  | entry | malformedDrain | malformedFinish | copyOut | copyBack | seed
  | marker | positive | failed | clearNumerator | clearDenominator
  | countOut | countBack | finish
  deriving DecidableEq

protected abbrev Control.enumList : List Control := [.entry, .malformedDrain, .malformedFinish,
  .copyOut, .copyBack, .seed, .marker, .positive, .failed, .clearNumerator, .clearDenominator,
  .countOut, .countBack, .finish]

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

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

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

inductive Label
  | control (phase : Control)
  | number (which : Number) (phase : ExtensionNatMachine.Label)
  | order (phase : BinaryOrderMachine.PreserveLabel)
  deriving DecidableEq, Fintype

abbrev Tape := Fin 8
abbrev Alphabet (_ : Tape) := Bool
abbrev State := BinaryAddMachine.State (Bool × Ordering)

def clean (valid : Bool) : State := BinaryAddMachine.clean (valid, .eq)

def validity (s : State) : Bool := s.1.1.1.1

def jump (label : Label) : TM2.Stmt Alphabet Label State :=
  .load (fun s => clean (validity s)) (.goto fun _ => label)

def scanTape (which : Number) : Fin 3 → Tape
  | 0 => 1
  | 1 => match which with | .numerator => 3 | .denominator => 4
  | _ => 2

def scanPorts (which : Number) : Fin 3 ↪ Tape :=
  ⟨scanTape which, by
    intro i j h
    cases which <;> fin_cases i <;> fin_cases j <;> simp_all [scanTape]⟩

def orderTape : Fin 5 → Tape
  | 0 => 3
  | 1 => 4
  | 2 => 5
  | 3 => 6
  | _ => 2

def orderPorts : Fin 5 ↪ Tape :=
  ⟨orderTape, by intro i j h; fin_cases i <;> fin_cases j <;> simp_all [orderTape]⟩

def scanSuccess : Number → Label
  | .numerator => .control .positive
  | .denominator => .order .leftOut

def orderExit : Ordering → Option Label
  | .lt | .eq => some (.control .clearNumerator)
  | .gt => some (.control .failed)

def instruction : Control → TM2.Stmt Alphabet Label State
  | .entry => .pop 0 (fun s bit => (s.1, bit))
      (.branch (fun s => s.2.getD false)
        (jump (.control .copyOut)) (jump (.control .malformedDrain)))
  | .malformedDrain => MachineDrain.drain 0 (.control .malformedDrain)
      (some (.control .malformedFinish))
  | .malformedFinish => .push 0 (fun _ => false) (.push 0 (fun _ => false)
      (.push 0 (fun _ => false) (.load (fun _ => clean true) .halt)))
  | .copyOut => loopAt 0 2 id false (.control .copyOut) (some (.control .copyBack))
  | .copyBack => MachineCopy.forkLoop 2 0 1 false (.control .copyBack)
      (some (.control .seed))
  | .seed => .push 7 (fun _ => false) (jump (.control .marker))
  | .marker => .pop 1 (fun s bit => (s.1, bit))
      (.branch (fun s => s.2.getD false)
        (.push 7 (fun _ => true) (jump (.number .numerator .scan)))
        (jump (.control .countOut)))
  | .positive => .pop 3 (fun s bit => (s.1, bit))
      (.branch (fun s => s.2.isSome)
        (.push 3 (fun s => s.2.getD false) (jump (.number .denominator .scan)))
        (.load (fun _ => clean false) (.goto fun _ => .number .denominator .scan)))
  | .failed => .load (fun _ => clean false) (.goto fun _ => .control .clearNumerator)
  | .clearNumerator => MachineDrain.drain 3 (.control .clearNumerator)
      (some (.control .clearDenominator))
  | .clearDenominator => MachineDrain.drain 4 (.control .clearDenominator)
      (some (.control .marker))
  | .countOut => loopAt 7 2 id false (.control .countOut) (some (.control .countBack))
  | .countBack => loopAt 2 0 id false (.control .countBack) (some (.control .finish))
  | .finish => .push 0 validity (.load (fun _ => clean true) .halt)

def program : Label → TM2.Stmt Alphabet Label State
  | .control phase => instruction phase
  | .number which phase => ExtensionNatMachine.statement (scanPorts which)
      (Label.number which) (some (scanSuccess which))
      (some (.control .malformedDrain)) phase
  | .order phase => ExtensionNatMachine.orderStatement orderPorts Label.order orderExit phase

abbrev machine : FinTM2 where
  K := Tape
  k₀ := 0
  k₁ := 0
  Γ := Alphabet
  Λ := Label
  main := .control .entry
  σ := State
  initialState := clean true
  m := program

def tapes (raw stream scratch numerator denominator count : List Bool) : Tape → List Bool
  | 0 => raw
  | 1 => stream
  | 2 => scratch
  | 3 => numerator
  | 4 => denominator
  | 7 => count
  | _ => []

def cfg (label : Option Label) (valid : Bool) (base : Tape → List Bool) : machine.Cfg :=
  ⟨label, clean valid, base⟩

private theorem update_raw (r i s n d c x : List Bool) :
    Function.update (tapes r i s n d c) 0 x = tapes x i s n d c := by
  funext k; fin_cases k <;> rfl

private theorem update_stream (r i s n d c x : List Bool) :
    Function.update (tapes r i s n d c) 1 x = tapes r x s n d c := by
  funext k; fin_cases k <;> rfl

private theorem update_scratch (r i s n d c x : List Bool) :
    Function.update (tapes r i s n d c) 2 x = tapes r i x n d c := by
  funext k; fin_cases k <;> rfl

private theorem update_numerator (r i s n d c x : List Bool) :
    Function.update (tapes r i s n d c) 3 x = tapes r i s x d c := by
  funext k; fin_cases k <;> rfl

private theorem update_denominator (r i s n d c x : List Bool) :
    Function.update (tapes r i s n d c) 4 x = tapes r i s n x c := by
  funext k; fin_cases k <;> rfl

private theorem update_count (r i s n d c x : List Bool) :
    Function.update (tapes r i s n d c) 7 x = tapes r i s n d x := by
  funext k; fin_cases k <;> rfl

def oneStep {start finish : machine.Cfg} (step : machine.step start = some finish) :
    StateTransition.EvalsToInTime machine.step start (some finish) 1 where
  steps := 1
  evals_in_steps := by
    change machine.step start = some finish
    exact step
  steps_le_m := Nat.le_refl _

def join {start middle finish : machine.Cfg} {a b : Nat}
    (first : StateTransition.EvalsToInTime machine.step start (some middle) a)
    (second : StateTransition.EvalsToInTime machine.step middle (some finish) b) :
    StateTransition.EvalsToInTime machine.step start (some finish) (a + b) := by
  simpa only [Nat.add_comm] using StateTransition.EvalsToInTime.trans machine.step
    a b start middle (some finish) first second

def enlarge {start finish : machine.Cfg} {a b : Nat}
    (run : StateTransition.EvalsToInTime machine.step start (some finish) a)
    (bound : a ≤ b) : StateTransition.EvalsToInTime machine.step start (some finish) b where
  toEvalsTo := run.toEvalsTo
  steps_le_m := Nat.le_trans run.steps_le_m bound

theorem entryStep (flag : Bool) (raw : List Bool) :
    machine.step (cfg (some (.control .entry)) true (tapes (flag :: raw) [] [] [] [] [])) =
      some (cfg (some (.control (if flag then .copyOut else .malformedDrain))) true
        (tapes raw [] [] [] [] [])) := by
  change some (TM2.stepAux (program (.control .entry)) (clean true)
    (tapes (flag :: raw) [] [] [] [] [])) = _
  cases flag <;>
    simp [program, instruction, jump, cfg, clean, validity, BinaryAddMachine.clean,
      BinaryAddMachine.state, TM2.stepAux, tapes, update_raw] <;> rfl

theorem markerStep (valid flag : Bool) (raw stream count : List Bool) :
    machine.step (cfg (some (.control .marker)) valid
      (tapes raw (flag :: stream) [] [] [] count)) =
      some (cfg (some (if flag then .number .numerator .scan else .control .countOut))
        valid (tapes raw stream [] [] [] (if flag then true :: count else count))) := by
  change some (TM2.stepAux (program (.control .marker)) (clean valid)
    (tapes raw (flag :: stream) [] [] [] count)) = _
  cases flag <;>
    simp [program, instruction, jump, cfg, clean, validity, BinaryAddMachine.clean,
      BinaryAddMachine.state, TM2.stepAux, tapes, update_stream, update_count] <;> rfl

theorem positiveStep (valid : Bool) (raw stream numerator count : List Bool) :
    machine.step (cfg (some (.control .positive)) valid
      (tapes raw stream [] numerator [] count)) =
      some (cfg (some (.number .denominator .scan))
        (valid && decide (numerator ≠ [])) (tapes raw stream [] numerator [] count)) := by
  change some (TM2.stepAux (program (.control .positive)) (clean valid)
    (tapes raw stream [] numerator [] count)) = _
  cases numerator <;>
    simp [program, instruction, jump, cfg, clean, validity, BinaryAddMachine.clean,
      BinaryAddMachine.state, TM2.stepAux, tapes, update_numerator] <;> rfl

def numeratorInTime (valid : Bool) (raw suffix count : List Bool) (n : Nat) :
    StateTransition.EvalsToInTime machine.step
      (cfg (some (.number .numerator .scan)) valid
        (tapes raw (BinaryEncoding.natBits n ++ suffix) [] [] [] count))
      (some (cfg (some (.control .positive)) valid (tapes raw suffix [] n.bits [] count)))
      (2 * n.size + 2) := by
  change StateTransition.EvalsToInTime (TM2.step program)
    ⟨some (.number .numerator .scan), BinaryAddMachine.clean (valid, .eq),
      tapes raw (BinaryEncoding.natBits n ++ suffix) [] [] [] count⟩
    (some ⟨some (.control .positive), BinaryAddMachine.clean (valid, .eq),
      tapes raw suffix [] n.bits [] count⟩) _
  have result : ExtensionNatMachine.resultTapes (scanPorts .numerator)
      (tapes raw (BinaryEncoding.natBits n ++ suffix) [] [] [] count) n suffix =
      tapes raw suffix [] n.bits [] count := by
    simp only [scanPorts]
    funext k
    fin_cases k <;> first | rfl | exact List.append_nil _
  simpa only [cfg, result, scanSuccess] using!
    ExtensionNatMachine.natInTime (scanPorts .numerator) (Label.number .numerator)
      (some (scanSuccess .numerator)) (some (.control .malformedDrain)) program
      (fun _ => rfl) (tapes raw (BinaryEncoding.natBits n ++ suffix) [] [] [] count)
      n suffix rfl rfl (valid, .eq)

def denominatorInTime (valid : Bool) (raw suffix count : List Bool) (n d : Nat) :
    StateTransition.EvalsToInTime machine.step
      (cfg (some (.number .denominator .scan)) valid
        (tapes raw (BinaryEncoding.natBits d ++ suffix) [] n.bits [] count))
      (some (cfg (some (.order .leftOut)) valid (tapes raw suffix [] n.bits d.bits count)))
      (2 * d.size + 2) := by
  change StateTransition.EvalsToInTime (TM2.step program)
    ⟨some (.number .denominator .scan), BinaryAddMachine.clean (valid, .eq),
      tapes raw (BinaryEncoding.natBits d ++ suffix) [] n.bits [] count⟩
    (some ⟨some (.order .leftOut), BinaryAddMachine.clean (valid, .eq),
      tapes raw suffix [] n.bits d.bits count⟩) _
  have result : ExtensionNatMachine.resultTapes (scanPorts .denominator)
      (tapes raw (BinaryEncoding.natBits d ++ suffix) [] n.bits [] count) d suffix =
      tapes raw suffix [] n.bits d.bits count := by
    simp only [scanPorts]
    funext k
    fin_cases k <;> first | rfl | exact List.append_nil _
  simpa only [cfg, result, scanSuccess] using!
    ExtensionNatMachine.natInTime (scanPorts .denominator) (Label.number .denominator)
      (some (scanSuccess .denominator)) (some (.control .malformedDrain)) program
      (fun _ => rfl) (tapes raw (BinaryEncoding.natBits d ++ suffix) [] n.bits [] count)
      d suffix rfl rfl (valid, .eq)

theorem bits_eq_nil_iff (n : Nat) : n.bits = [] ↔ n = 0 := by
  constructor
  · intro h
    have hv := congrArg BinPackingCompleteness.BinaryEncoding.bitsValue h
    simpa only [BinPackingCompleteness.BinaryEncoding.bitsValue_bits,
      BinPackingCompleteness.BinaryEncoding.bitsValue] using hv
  · rintro rfl; rfl

def comparisonInTime (valid : Bool) (raw stream count : List Bool) (a d : Nat) :
    StateTransition.EvalsToInTime machine.step
      (cfg (some (.order .leftOut)) valid (tapes raw stream [] a.bits d.bits count))
      (some (cfg (some (.control .clearNumerator)) (valid && decide (a ≤ d))
        (tapes raw stream [] a.bits d.bits count)))
      (BinaryOrderMachine.preservingSteps a.size d.size + 1) := by
  have trace := ExtensionNatMachine.preservingOrderInTime orderPorts Label.order orderExit
    program (fun _ => rfl) (tapes raw stream [] a.bits d.bits count) a d
    rfl rfl rfl rfl rfl valid
  have exit : orderExit (BinaryOrderMachine.orderNat a d .eq) =
      some (.control (if a ≤ d then .clearNumerator else .failed)) := by
    unfold BinaryOrderMachine.orderNat
    split_ifs <;> simp_all [orderExit] <;> omega
  rw [exit] at trace
  by_cases h : a ≤ d
  · simpa only [h, ite_true, decide_true, Bool.and_true, cfg, clean] using
      enlarge (b := BinaryOrderMachine.preservingSteps a.size d.size + 1) trace (by omega)
  · have failure : machine.step (cfg (some (.control .failed)) valid
        (tapes raw stream [] a.bits d.bits count)) =
        some (cfg (some (.control .clearNumerator)) false
          (tapes raw stream [] a.bits d.bits count)) := by rfl
    simp only [h, ite_false] at trace
    simpa only [h, ite_false, decide_false, Bool.and_false, cfg, clean] using
      join trace (oneStep failure)

def clearInTime (valid : Bool) (raw stream numerator denominator count : List Bool) :
    StateTransition.EvalsToInTime machine.step
      (cfg (some (.control .clearNumerator)) valid
        (tapes raw stream [] numerator denominator count))
      (some (cfg (some (.control .marker)) valid (tapes raw stream [] [] [] count)))
      (numerator.length + denominator.length + 2) := by
  have first := MachineDrain.drainInTime (3 : Tape) (.control .clearNumerator)
    (some (.control .clearDenominator)) program rfl
    (tapes raw stream [] numerator denominator count) (((valid, Ordering.eq), false), none) none
  have second := MachineDrain.drainInTime (4 : Tape) (.control .clearDenominator)
    (some (.control .marker)) program rfl
    (tapes raw stream [] [] denominator count) (((valid, Ordering.eq), false), none) none
  simp only [update_numerator, tapes] at first
  simp only [update_denominator, tapes] at second
  have both := join first second
  change StateTransition.EvalsToInTime (TM2.step program)
    ⟨some (.control .clearNumerator), BinaryAddMachine.clean (valid, .eq),
      tapes raw stream [] numerator denominator count⟩
    (some ⟨some (.control .marker), BinaryAddMachine.clean (valid, .eq),
      tapes raw stream [] [] [] count⟩) _
  exact { toEvalsTo := both.toEvalsTo
          steps_le_m := by
            have bound := both.steps_le_m
            omega }

def rowTime (a d : Nat) : Nat := 6 * (a.size + d.size) + 14

def loopTime : RawInstance → Nat
  | [] => 1
  | (a, d) :: items => rowTime a d + loopTime items

theorem validItems_cons (a d : Nat) (items : RawInstance) :
    ExtensionCertificate.validItems ((a, d) :: items) =
      (decide (0 < a) && decide (a ≤ d) && ExtensionCertificate.validItems items) := by
  simp [ExtensionCertificate.validItems, Bool.and_assoc]

def itemsInTime (items : RawInstance) (valid : Bool) (raw : List Bool) (count : Nat) :
    StateTransition.EvalsToInTime machine.step
      (cfg (some (.control .marker)) valid
        (tapes raw (BinaryEncoding.rawInstanceBits items) [] [] [] (encodeWord count)))
      (some (cfg (some (.control .countOut))
        (valid && ExtensionCertificate.validItems items)
        (tapes raw [] [] [] [] (encodeWord (count + items.length)))))
      (loopTime items) := by
  induction items generalizing valid count with
  | nil =>
      simpa [loopTime, ExtensionCertificate.validItems,
        BinaryEncoding.rawInstanceBits, BinaryEncoding.listBits] using
        oneStep (markerStep valid false raw [] (encodeWord count))
  | cons item items ih =>
      rcases item with ⟨a, d⟩
      have marker := oneStep (markerStep valid true raw
        (BinaryEncoding.natBits a ++ BinaryEncoding.natBits d ++
          BinaryEncoding.rawInstanceBits items) (encodeWord count))
      have numerator := numeratorInTime valid raw
        (BinaryEncoding.natBits d ++ BinaryEncoding.rawInstanceBits items)
        (encodeWord (count + 1)) a
      have positive := oneStep (positiveStep valid raw
        (BinaryEncoding.natBits d ++ BinaryEncoding.rawInstanceBits items) a.bits
        (encodeWord (count + 1)))
      have nonempty : decide (a.bits ≠ []) = decide (0 < a) := by
        simp only [ne_eq, bits_eq_nil_iff, Nat.pos_iff_ne_zero]
      rw [nonempty] at positive
      have denominator := denominatorInTime (valid && decide (0 < a)) raw
        (BinaryEncoding.rawInstanceBits items) (encodeWord (count + 1)) a d
      have comparison := comparisonInTime (valid && decide (0 < a)) raw
        (BinaryEncoding.rawInstanceBits items) (encodeWord (count + 1)) a d
      have clear := clearInTime (valid && decide (0 < a) && decide (a ≤ d)) raw
        (BinaryEncoding.rawInstanceBits items) a.bits d.bits (encodeWord (count + 1))
      have rest := ih (valid && decide (0 < a) && decide (a ≤ d)) (count + 1)
      have count_succ : true :: encodeWord count = encodeWord (count + 1) := by
        simp [encodeWord, List.replicate_succ]
      rw [count_succ] at marker
      simp only [List.append_assoc] at marker
      have trace := join (join (join (join (join (join marker numerator) positive)
        denominator) comparison) clear) rest
      have bounded : 1 + (2 * a.size + 2) + 1 + (2 * d.size + 2) +
          (BinaryOrderMachine.preservingSteps a.size d.size + 1) +
          (a.bits.length + d.bits.length + 2) + loopTime items ≤
          loopTime ((a, d) :: items) := by
        have h := BinaryOrderMachine.preservingSteps_le a.size d.size
        simp only [loopTime, rowTime, Nat.size_eq_bits_len]
        omega
      simpa only [BinaryEncoding.rawInstanceBits, BinaryEncoding.listBits,
        BinaryEncoding.pairBits, List.length_cons, List.append_assoc,
        validItems_cons, Bool.and_assoc, Nat.add_assoc, Nat.add_comm 1 items.length] using
        enlarge trace bounded

theorem loopTime_le (items : RawInstance) :
    loopTime items ≤ 5 * (BinaryEncoding.rawInstanceBits items).length := by
  induction items with
  | nil => simp [loopTime, BinaryEncoding.rawInstanceBits, BinaryEncoding.listBits]
  | cons item items ih =>
      rcases item with ⟨a, d⟩
      have length : (BinaryEncoding.rawInstanceBits ((a, d) :: items)).length =
          2 * a.size + 2 * d.size + 3 + (BinaryEncoding.rawInstanceBits items).length := by
        simp only [BinaryEncoding.rawInstanceBits, BinaryEncoding.listBits,
          BinaryEncoding.pairBits, List.length_cons, List.length_append,
          BinaryEncoding.natBits_length]
        omega
      rw [length]
      simp only [loopTime, rowTime]
      omega

def copyInTime (raw : List Bool) :
    StateTransition.EvalsToInTime machine.step
      (cfg (some (.control .copyOut)) true (tapes raw [] [] [] [] []))
      (some (cfg (some (.control .seed)) true (tapes raw raw [] [] [] [])))
      (2 * (raw.length + 1)) := by
  change StateTransition.EvalsToInTime (TM2.step program)
    ⟨some (.control .copyOut), BinaryAddMachine.clean (true, .eq),
      tapes raw [] [] [] [] []⟩
    (some ⟨some (.control .seed), BinaryAddMachine.clean (true, .eq),
      tapes raw raw [] [] [] []⟩) _
  simpa only [cfg, clean, BinaryAddMachine.clean, BinaryAddMachine.state,
    update_stream, tapes, List.append_nil] using
    MachineCopy.copyInTime (0 : Tape) 1 2 (by decide) (by decide) (by decide)
      false (.control .copyOut) (.control .copyBack) (some (.control .seed)) program
      rfl rfl (tapes raw [] [] [] [] []) rfl (((true, Ordering.eq), false), none) none

theorem seedStep (raw : List Bool) :
    machine.step (cfg (some (.control .seed)) true (tapes raw raw [] [] [] [])) =
      some (cfg (some (.control .marker)) true (tapes raw raw [] [] [] (encodeWord 0))) := by
  change some (TM2.stepAux (program (.control .seed)) (clean true)
    (tapes raw raw [] [] [] [])) = _
  simp [program, instruction, jump, cfg, clean, validity, BinaryAddMachine.clean,
    BinaryAddMachine.state, TM2.stepAux, tapes, update_count, encodeWord]
  rfl

def countInTime (valid : Bool) (raw : List Bool) (count : Nat) :
    StateTransition.EvalsToInTime machine.step
      (cfg (some (.control .countOut)) valid (tapes raw [] [] [] [] (encodeWord count)))
      (some (cfg (some (.control .finish)) valid
        (tapes (encodeWord count ++ raw) [] [] [] [] [])))
      (2 * (count + 2)) := by
  have first := transferAtInTime (7 : Tape) 2 (by decide) id false
    (.control .countOut) (some (.control .countBack)) program rfl
    (tapes raw [] [] [] [] (encodeWord count)) (((valid, Ordering.eq), false), none) none
  simp only [tapesAt, update_count, update_scratch, tapes, List.map_id_fun, id_eq,
    List.append_nil] at first
  have second := transferAtInTime (2 : Tape) 0 (by decide) id false
    (.control .countBack) (some (.control .finish)) program rfl
    (tapes raw [] (encodeWord count).reverse [] [] [])
    (((valid, Ordering.eq), false), none) none
  simp only [tapesAt, update_scratch, update_raw, tapes, List.map_id_fun, id_eq,
    List.reverse_reverse, List.length_reverse] at second
  have length : (encodeWord count).length = count + 1 := by simp [encodeWord]
  have both := join first second
  change StateTransition.EvalsToInTime (TM2.step program)
    ⟨some (.control .countOut), BinaryAddMachine.clean (valid, .eq),
      tapes raw [] [] [] [] (encodeWord count)⟩
    (some ⟨some (.control .finish), BinaryAddMachine.clean (valid, .eq),
      tapes (encodeWord count ++ raw) [] [] [] [] []⟩) _
  exact { toEvalsTo := both.toEvalsTo
          steps_le_m := by
            have bound := both.steps_le_m
            simp only [length] at bound
            omega }

theorem finishStep (valid : Bool) (output : List Bool) :
    machine.step (cfg (some (.control .finish)) valid (tapes output [] [] [] [] [])) =
      some (cfg none true (tapes (valid :: output) [] [] [] [] [])) := by
  change some (TM2.stepAux (program (.control .finish)) (clean valid)
    (tapes output [] [] [] [] [])) = _
  simp [program, instruction, cfg, clean, validity, BinaryAddMachine.clean,
    BinaryAddMachine.state, TM2.stepAux, tapes, update_raw]
  rfl

theorem initList_eq (input : List Bool) :
    initList machine input = cfg (some (.control .entry)) true (tapes input [] [] [] [] []) := by
  unfold initList cfg
  congr 1
  funext k; fin_cases k <;> rfl

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

theorem item_count_le (items : RawInstance) :
    items.length ≤ (BinaryEncoding.rawInstanceBits items).length := by
  induction items with
  | nil => simp
  | cons item items ih =>
      rcases item with ⟨a, d⟩
      simp only [BinaryEncoding.rawInstanceBits, BinaryEncoding.listBits,
        BinaryEncoding.pairBits, List.length_cons, List.length_append] at ih ⊢
      omega

def encodedInTime (items : RawInstance) :
    TM2OutputsInTime machine (true :: BinaryEncoding.rawInstanceBits items)
      (some (dataBits (items, ExtensionCertificate.validItems items)))
      (16 * ((BinaryEncoding.rawInstanceBits items).length + 2)) := by
  have entry := oneStep (entryStep true (BinaryEncoding.rawInstanceBits items))
  have copy := copyInTime (BinaryEncoding.rawInstanceBits items)
  have seed := oneStep (seedStep (BinaryEncoding.rawInstanceBits items))
  have loop := itemsInTime items true (BinaryEncoding.rawInstanceBits items) 0
  simp only [Bool.true_and, Nat.zero_add] at loop
  have count := countInTime (ExtensionCertificate.validItems items)
    (BinaryEncoding.rawInstanceBits items) items.length
  have finish := oneStep (finishStep (ExtensionCertificate.validItems items)
    (encodeWord items.length ++ BinaryEncoding.rawInstanceBits items))
  have trace := join (join (join (join (join entry copy) seed) loop) count) finish
  have bounded : 1 + 2 * ((BinaryEncoding.rawInstanceBits items).length + 1) + 1 +
      loopTime items + 2 * (items.length + 2) + 1 ≤
      16 * ((BinaryEncoding.rawInstanceBits items).length + 2) := by
    have loopBound := loopTime_le items
    have countBound := item_count_le items
    omega
  change StateTransition.EvalsToInTime machine.step
    (initList machine (true :: BinaryEncoding.rawInstanceBits items))
    (some (haltList machine (dataBits (items, ExtensionCertificate.validItems items)))) _
  rw [initList_eq, haltList_eq]
  exact enlarge trace bounded

def malformedInTime (raw : List Bool) :
    TM2OutputsInTime machine (false :: raw) (some (dataBits ([], false)))
      (16 * (raw.length + 2)) := by
  have entry := oneStep (entryStep false raw)
  have drain := MachineDrain.drainInTime (0 : Tape) (.control .malformedDrain)
    (some (.control .malformedFinish)) program rfl (tapes raw [] [] [] [] [])
    (((true, Ordering.eq), false), none) none
  simp only [update_raw, tapes] at drain
  have finish : machine.step (cfg (some (.control .malformedFinish)) true
      (tapes [] [] [] [] [] [])) =
      some (cfg none true (tapes (dataBits ([], false)) [] [] [] [] [])) := by
    change some (TM2.stepAux (program (.control .malformedFinish)) (clean true)
      (tapes [] [] [] [] [] [])) = _
    simp [program, instruction, cfg, dataBits, encodeWord, BinaryEncoding.rawInstanceBits,
      BinaryEncoding.listBits, TM2.stepAux, update_raw, tapes]
    rfl
  have trace := join (join entry drain) (oneStep finish)
  change StateTransition.EvalsToInTime machine.step
    (initList machine (false :: raw)) (some (haltList machine (dataBits ([], false)))) _
  rw [initList_eq, haltList_eq]
  exact enlarge trace (by omega)

def outputsInTime (input : List Bool) :
    TM2OutputsInTime machine (shapeBits input) (some (dataBits (classify input)))
      (16 * ((shapeBits input).length + 1)) := by
  cases parsed : BinaryEncoding.decodeRawInstance input with
  | none =>
      simpa only [shapeBits, classify, parsed, Option.isSome_none, List.length_cons]
        using malformedInTime input
  | some items =>
      have encoded := BinaryEncoding.decodeRawInstance_sound parsed
      subst input
      simpa only [shapeBits, classify, BinaryEncoding.decodeRawInstance_rawInstanceBits,
        Option.isSome_some, List.length_cons] using encodedInTime items

noncomputable def computation : TM2ComputableInPolyTime shapeBits dataBits classify where
  tm := machine
  inputAlphabet := Equiv.refl Bool
  outputAlphabet := Equiv.refl Bool
  time := 16 * (Polynomial.X + 1)
  outputsFun input := by
    change TM2OutputsInTime machine ((shapeBits input).map id)
      (some ((dataBits (classify input)).map id))
      ((16 * (Polynomial.X + 1) : Polynomial Nat).eval (shapeBits input).length)
    simpa only [List.map_id_fun, id_eq, Polynomial.eval_mul, Polynomial.eval_add,
      Polynomial.eval_X, Polynomial.eval_ofNat, Polynomial.eval_one] using outputsInTime input

theorem finiteAlphabet : MachineFiniteAlphabet.FiniteAlphabet computation.tm := by
  intro k
  change Finite Bool
  infer_instance

end BinPackingGap.SearchInitialItemsMachine

end OAI