Repository navigation
Expand file tree
/
Copy pathProgram.lean
More file actions
1844 lines (1737 loc) · 89.3 KB
/
Copy pathProgram.lean
File metadata and controls
1844 lines (1737 loc) · 89.3 KB
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
/-
Copyright (c) 2026 Google LLC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
-/
module
public import Cryptids.Semantics.Riemann120.Backend
public import Cryptids.Semantics.Riemann.Gadgets
public import Mathlib.Data.Nat.Prime.Basic
public import Mathlib.Data.Nat.Factorial.Basic
public import Mathlib.Algebra.BigOperators.Group.Finset.Basic
/-!
# Layer 2: 63-Node Control DAG, 5-Register Machine & Arithmetic Invariants (`riemann121`)
This module formalizes Layer 2 for the 121-state canonical predecessor machine `canon121`
(`riemann121`) of Shunia's 120-state Riemann Hypothesis counterexample search machine:
1. **63-Node Control DAG & Call-Stack Dispatcher**:
- `walkInternal`, `walkTape`, `outcomeToInstr`, `evalStack`: call-stack decision tree
dispatcher rooted at `State 27` (`main.0`) over `ConjectureMachines.Riemann120.rh121Trans`.
- Exhaustive `decide` verification across all valid control words and reachable call stacks
from `State 27` that `bits.length ≤ 18` and `[1, 1, 0] ++ bits ++ [1, 0]` contains
no four consecutive zeros (`hasNoFourZeros`).
2. **5-Register Abstract Machine (`RhRegCfg`, `rhRegStep`, `rhRegNSteps`, `RhRegHalts`)**:
- Operates on `[m, j, x, f, v]` (`c.regs : List ℕ`) and `c.stack : List (Fin 2)` starting
from `rhRegInit = ⟨[], [0, 0, 0, 0, 0]⟩`.
- Also provides equivalence with the dummy-zero-appending tape-register semantics
(`RhRegTapeHalts`).
3. **Shunia's Pure Mathematical Quantities & Self-Sieving Identity**:
- `primorial`, `shuniaProd` ($A_n = n! \prod_{p \le n} p$), `offsetDigits`,
`shuniaDeficit` ($\Delta_n$), `quarterClock` ($q_n$), and `ShuniaCounterexample`.
- Self-sieving theorem (`prime_iff_not_dvd_shuniaProd` and `shuniaProd_step`, Lemma 2.1 of
`rhtm.tex`).
4. **Subroutine Verification (`SubEval`) & Main Loop Halting Equivalence**:
- Complete functional specifications for `move`, `dec_f`, `seed_deficit`, `fold`,
`transform`, `twice`, `divide`, `product_step`, `product`, `digits`, `halve(j)`,
`halve(f)`, `width`, `compare`, and `main`, culminating in
`rhRegHalts_iff_exists_shuniaCounterexample`.
-/
@[expose] public section
namespace ConjectureMachines.Riemann120.Layer2
open Turing (Dir)
open StateTransition
open ConjectureMachines.Riemann120
(rh121Trans primorial shuniaProd offsetDigits shuniaDeficit quarterClock ShuniaCounterexample)
set_option linter.style.setOption false
set_option linter.flexible false
set_option maxRecDepth 200000
/-! ## Section 1: Control DAG & Leaf Classification of `riemann121` (`canon121`) -/
/-- Enumerates all exact length-`n` internal DAG paths starting at state `q`. -/
def dagExactPaths : ℕ → Fin 121 → List (List (Fin 2))
| 0, q => if isInternalNode q then [[]] else []
| n + 1, q =>
if isInternalNode q then
([0, 1] : List (Fin 2)).flatMap fun b =>
match rh121Trans q b with
| some (w, .right, q') =>
if w == b && isInternalNode q' then
(dagExactPaths n q').map (b :: ·)
else []
| _ => []
else []
theorem mem_dagExactPaths_of_walkInternal {P : List (Fin 2)} {q q' : Fin 121}
(h : walkInternal q P = some q') :
P ∈ dagExactPaths P.length q := by
induction P generalizing q with
| nil =>
simp only [walkInternal] at h
by_cases hi : isInternalNode q = true
· simp [dagExactPaths, hi]
· simp [hi] at h
| cons b bs ih =>
simp only [walkInternal] at h
by_cases hi : isInternalNode q = true
· simp only [hi, ↓reduceIte] at h
rcases htr : rh121Trans q b with _ | ⟨w, d, q₁⟩
· simp [htr] at h
· cases d with
| left => simp [htr] at h
| right =>
simp only [htr] at h
by_cases hcond : (w == b && isInternalNode q₁) = true
· simp only [hcond, ↓reduceIte] at h
have hmem := ih h
simp only [List.length_cons, dagExactPaths, hi, ↓reduceIte, List.mem_flatMap]
refine ⟨b, by fin_cases b <;> simp, ?_⟩
simp [htr, hcond, hmem]
· simp [hcond] at h
· simp [hi] at h
theorem dagExactPaths_15_eq_nil (q : Fin 121) : dagExactPaths 15 q = [] := by
revert q; decide
theorem walkInternal_take_of_some {k : ℕ} {P : List (Fin 2)} {q q' : Fin 121}
(h : walkInternal q P = some q') :
∃ qk, walkInternal q (P.take k) = some qk := by
induction k generalizing P q with
| zero =>
simp only [List.take_zero, walkInternal]
cases P with
| nil =>
simp only [walkInternal] at h
by_cases hi : isInternalNode q = true
· exact ⟨q, by simp [hi]⟩
· simp [hi] at h
| cons b bs =>
simp only [walkInternal] at h
by_cases hi : isInternalNode q = true
· exact ⟨q, by simp [hi]⟩
· simp [hi] at h
| succ k ih =>
cases P with
| nil =>
simp only [List.take_nil, walkInternal]
simp only [walkInternal] at h
by_cases hi : isInternalNode q = true
· exact ⟨q, by simp [hi]⟩
· simp [hi] at h
| cons b bs =>
simp only [walkInternal] at h
by_cases hi : isInternalNode q = true
· simp only [hi, ↓reduceIte] at h
rcases htr : rh121Trans q b with _ | ⟨w, d, q₁⟩
· simp [htr] at h
· cases d with
| left => simp [htr] at h
| right =>
simp only [htr] at h
by_cases hcond : (w == b && isInternalNode q₁) = true
· simp only [hcond, ↓reduceIte] at h
obtain ⟨qk, hqk⟩ := ih h
refine ⟨qk, ?_⟩
simp [List.take_succ_cons, walkInternal, hi, htr, hcond, hqk]
· simp [hcond] at h
· simp [hi] at h
theorem walkInternal_length_le_14 {P : List (Fin 2)} {q q' : Fin 121}
(h : walkInternal q P = some q') : P.length ≤ 14 := by
if hle : P.length ≤ 14 then
exact hle
else
exfalso
have h15 : 15 ≤ P.length := by omega
obtain ⟨q15, hq15⟩ := walkInternal_take_of_some (k := 15) h
have hmem := mem_dagExactPaths_of_walkInternal hq15
have hlen : (P.take 15).length = 15 := by simp; omega
rw [hlen, dagExactPaths_15_eq_nil q] at hmem
cases hmem
/-- All 120 valid internal control prefixes `P` reachable from `State 27` (`main.0`). -/
def allControlPrefixes : List (List (Fin 2)) :=
(List.range 15).flatMap fun n => dagExactPaths n 27
theorem mem_allControlPrefixes_of_walkInternal {P : List (Fin 2)} {q : Fin 121}
(h : walkInternal 27 P = some q) :
P ∈ allControlPrefixes := by
have hle := walkInternal_length_le_14 h
have hmem := mem_dagExactPaths_of_walkInternal h
simp only [allControlPrefixes, List.mem_flatMap, List.mem_range]
exact ⟨P.length, by omega, hmem⟩
/-- Every valid control prefix `bits` from `State 27` has length `≤ 18` and no `0000` run
in `[1, 1, 0] ++ bits ++ [1, 0]`. -/
theorem controlPrefix_bounds_and_noFourZeros :
∀ bits ∈ allControlPrefixes,
bits.length ≤ 18 ∧ hasNoFourZeros ([1, 1, 0] ++ bits ++ [1, 0]) = true := by
decide
/-- All 121 terminal control words `P ++ [b]` leaving the internal DAG from `State 27`. -/
def allTerminalWords : List (List (Fin 2)) :=
allControlPrefixes.flatMap fun P =>
match walkInternal 27 P with
| some q =>
([0, 1] : List (Fin 2)).filterMap fun b =>
match rh121Trans q b with
| some (_, .right, q') => if isInternalNode q' then none else some (P ++ [b])
| _ => some (P ++ [b])
| none => []
/-- Every terminal control word `bits` leaving `State 27` has length `≤ 18` and no `0000` run
in `[1, 1, 0] ++ bits ++ [1, 0]`. -/
theorem terminalWord_bounds_and_noFourZeros :
∀ bits ∈ allTerminalWords,
bits.length ≤ 18 ∧ hasNoFourZeros ([1, 1, 0] ++ bits ++ [1, 0]) = true := by
decide
theorem rhRegNSteps_stack_valid_and_noFourZeros {n : ℕ} {c : RhRegCfg}
(hn : rhRegNSteps n rhRegInit = some c) :
isValidStack c.stack = true ∧
c.stack.length ≤ 18 ∧
hasNoFourZeros ([1, 1, 0] ++ c.stack ++ [1, 0]) = true := by
have hvalid : ∀ m {c₀ c₁ : RhRegCfg},
isValidStack c₀.stack = true → rhRegNSteps m c₀ = some c₁ →
isValidStack c₁.stack = true := by
intro m
induction m with
| zero =>
intro c₀ c₁ hv hm
injection hm with h_eq; subst h_eq; exact hv
| succ m ih =>
intro c₀ c₁ hv hm
unfold rhRegNSteps at hm
rcases hs : rhRegStep c₀ with _ | cm
· simp [hs] at hm
· rw [hs, Option.bind_some] at hm
exact ih (rhRegStep_preserves_isValidStack hv hs) hm
have hv := hvalid n isValidStack_nil hn
have hmem : c.stack ∈ allValidStacks := List.contains_iff_mem.mp hv
have hprops := isValidStack_closed_and_noFourZeros c.stack hmem
exact ⟨hv, hprops.1, hprops.2.1⟩
/-- Tape-faithful variant of `rhRegStep` appending a dummy `0` register on `decnz > 0`. -/
def rhRegTapeStep (c : RhRegCfg) : Option RhRegCfg :=
match evalStack c.stack with
| .halt => none
| .jump stack' => some ⟨stack', c.regs⟩
| .inc r stack_succ =>
some ⟨stack_succ, c.regs.set r (c.regs.getD r 0 + 1)⟩
| .decnz r stack_succ stack_fail =>
if c.regs.getD r 0 == 0 then
some ⟨stack_fail, c.regs⟩
else
some ⟨stack_succ, c.regs.set r (c.regs.getD r 0 - 1) ++ [0]⟩
def rhRegTapeNSteps : ℕ → RhRegCfg → Option RhRegCfg
| 0, c => some c
| n + 1, c => (rhRegTapeStep c).bind (rhRegTapeNSteps n)
def RhRegTapeHalts : Prop :=
(StateTransition.eval rhRegTapeStep rhRegInit).Dom
def RhRegSim (c₁ c₂ : RhRegCfg) : Prop :=
c₁.stack = c₂.stack ∧ 5 ≤ c₁.regs.length ∧ 5 ≤ c₂.regs.length ∧
c₁.regs.take 5 = c₂.regs.take 5
theorem evalStack_reg_lt_5 (bits : List (Fin 2)) :
match evalStack bits with
| .inc r _ => r < 5
| .decnz r _ _ => r < 5
| _ => True := by
unfold evalStack outcomeToInstr
rcases walkTape 27 [] (bits ++ List.replicate 18 0) with _ | ⟨q', _, _⟩ | ⟨q', _, _⟩ | _
· trivial
· dsimp only
split <;> try trivial
all_goals (split at * <;> contradiction)
· by_cases h : isLeafInc q' = true <;> simp [h, leafReg_lt_5 q']
· trivial
theorem getD_eq_of_take_5 {r₁ r₂ : List ℕ} {k : ℕ} (hk : k < 5)
(h : r₁.take 5 = r₂.take 5) :
r₁.getD k 0 = r₂.getD k 0 := by
have h1 : r₁.getD k 0 = (r₁.take 5).getD k 0 := by
simp [List.getD_eq_getElem?_getD, List.getElem?_take_of_lt hk]
have h2 : r₂.getD k 0 = (r₂.take 5).getD k 0 := by
simp [List.getD_eq_getElem?_getD, List.getElem?_take_of_lt hk]
rw [h1, h2, h]
theorem rhRegTapeStep_sim {c₁ c₂ : RhRegCfg} (hsim : RhRegSim c₁ c₂) :
match rhRegTapeStep c₁, rhRegStep c₂ with
| none, none => True
| some c₁', some c₂' => RhRegSim c₁' c₂'
| _, _ => False := by
rcases hsim with ⟨hstack, hlen₁, hlen₂, htake⟩
have hr_lt := evalStack_reg_lt_5 c₁.stack
unfold rhRegTapeStep rhRegStep
rw [← hstack]
rcases hev : evalStack c₁.stack with _ | stack' | ⟨r, stack_succ⟩ | ⟨r, stack_succ, stack_fail⟩
· trivial
· exact ⟨rfl, hlen₁, hlen₂, htake⟩
· rw [hev] at hr_lt
have hget := getD_eq_of_take_5 hr_lt htake
refine ⟨rfl, by simp [hlen₁], by simp [hlen₂], ?_⟩
rw [List.take_set, List.take_set, hget, htake]
· rw [hev] at hr_lt
have hget := getD_eq_of_take_5 hr_lt htake
dsimp only
rw [hget]
by_cases hz : (c₂.regs.getD r 0 == 0) = true
· simp only [hz, ↓reduceIte]
exact ⟨rfl, hlen₁, hlen₂, htake⟩
· simp only [hz, Bool.false_eq_true, ↓reduceIte]
refine ⟨rfl, by simp; omega, by simp [hlen₂], ?_⟩
dsimp only
have hle₁ : 5 ≤ (c₁.regs.set r (c₂.regs.getD r 0 - 1)).length := by simp [hlen₁]
rw [List.take_append_of_le_length hle₁, List.take_set, List.take_set, htake]
/-! ## Section 4: Pure Mathematical Quantities & Self-Sieving Identity (`rhtm.tex`) -/
theorem primorial_pos (n : ℕ) : 0 < primorial n := by
induction n with
| zero => decide
| succ n ih =>
unfold primorial
split
· exact Nat.mul_pos (Nat.succ_pos n) ih
· exact ih
theorem shuniaProd_pos (n : ℕ) : 0 < shuniaProd n :=
Nat.mul_pos (Nat.factorial_pos n) (primorial_pos n)
theorem prime_le_of_dvd_primorial {p m : ℕ} (hp : p.Prime) :
p ∣ primorial m → p ≤ m := by
induction m with
| zero =>
intro h
have h1 : p = 1 := Nat.eq_one_of_dvd_one h
have hgt := hp.one_lt
omega
| succ m ih =>
unfold primorial
split
· intro h
rcases hp.dvd_mul.mp h with h1 | h2
· exact Nat.le_of_dvd (by omega) h1
· exact Nat.le_succ_of_le (ih h2)
· exact fun h => Nat.le_succ_of_le (ih h)
theorem prime_le_of_dvd_factorial {p m : ℕ} (hp : p.Prime) :
p ∣ m.factorial → p ≤ m := by
induction m with
| zero =>
intro h
have h1 : p = 1 := Nat.eq_one_of_dvd_one h
have hgt := hp.one_lt
omega
| succ m ih =>
rw [Nat.factorial_succ]
intro h
rcases hp.dvd_mul.mp h with h1 | h2
· exact Nat.le_of_dvd (by omega) h1
· exact Nat.le_succ_of_le (ih h2)
theorem mul_dvd_factorial_of_lt {a b n : ℕ} (ha : 1 ≤ a) (hab : a < b) (hbn : b ≤ n) :
a * b ∣ n.factorial := by
have hb_fac : b.factorial = b * (b - 1).factorial := by
cases b with
| zero => omega
| succ b' => simp [Nat.factorial_succ]
have ha_dvd : a ∣ (b - 1).factorial := Nat.dvd_factorial ha (by omega)
have hab_dvd : a * b ∣ b.factorial := by
rw [hb_fac, mul_comm]
exact Nat.mul_dvd_mul_left b ha_dvd
exact dvd_trans hab_dvd (Nat.factorial_dvd_factorial hbn)
/-- Lemma 2.1 of `rhtm.tex`: $J \ge 2$ is prime iff $J \nmid A_{J - 1}$. -/
theorem prime_iff_not_dvd_shuniaProd {J : ℕ} (hJ : 2 ≤ J) :
J.Prime ↔ ¬ (J ∣ shuniaProd (J - 1)) := by
constructor
· intro hprime hdvd
unfold shuniaProd at hdvd
rcases hprime.dvd_mul.mp hdvd with hfac | hprim
· have hle := prime_le_of_dvd_factorial hprime hfac
omega
· have hle := prime_le_of_dvd_primorial hprime hprim
omega
· intro hnotdvd
if hnotprime : J.Prime then
exact hnotprime
else
exfalso
apply hnotdvd
by_cases hJ4 : J = 4
· subst hJ4; decide
· have hfac_dvd : J ∣ (J - 1).factorial := by
let d := J.minFac
have hd_prime : d.Prime := Nat.minFac_prime (by omega)
have hd_gt1 : 1 < d := hd_prime.one_lt
have hd_dvd : d ∣ J := Nat.minFac_dvd J
have hd_ne : d ≠ J := fun heq => hnotprime (heq ▸ hd_prime)
have hd_le : d ≤ J := Nat.minFac_le (by omega)
have hd_lt : d < J := lt_of_le_of_ne hd_le hd_ne
let q := J / d
have hdq : d * q = J := Nat.mul_div_cancel' hd_dvd
have hq_lt : q < J := Nat.div_lt_self (by omega) hd_gt1
have hq_ge1 : 1 ≤ q := by
rcases q with _ | _
· simp at hdq; omega
· omega
by_cases heq_dq : d = q
· rw [← heq_dq] at hdq
have hd_ne2 : d ≠ 2 := by
intro h2
rw [h2] at hdq
omega
have hd_ge3 : 3 ≤ d := by omega
have h2d_lt : 2 * d < d * d := by nlinarith
have h2d_le : 2 * d ≤ J - 1 := by omega
have hmul := mul_dvd_factorial_of_lt (a := d) (b := 2 * d) (by omega) (by omega) h2d_le
have h2J : d * (2 * d) = 2 * J := by nlinarith
rw [h2J] at hmul
exact dvd_trans (dvd_mul_left J 2) hmul
· rcases lt_or_gt_of_ne heq_dq with hlt | hgt
· rw [← hdq]
exact mul_dvd_factorial_of_lt (by omega) hlt (by omega)
· have hmul : q * d ∣ (J - 1).factorial :=
mul_dvd_factorial_of_lt hq_ge1 hgt (by omega : d ≤ J - 1)
rwa [mul_comm, hdq] at hmul
exact dvd_mul_of_dvd_left hfac_dvd (primorial (J - 1))
theorem primorial_step {J : ℕ} (hJ : 2 ≤ J) :
primorial J = if J.Prime then J * primorial (J - 1) else primorial (J - 1) := by
rcases J with _ | n
· omega
· rfl
/-- Self-sieving recurrence for $A_J$:
$A_J = J \cdot A_{J-1}$ if $J \mid A_{J-1}$, else $J^2 \cdot A_{J-1}$. -/
theorem shuniaProd_step {J : ℕ} (hJ : 2 ≤ J) :
shuniaProd J =
if J ∣ shuniaProd (J - 1) then J * shuniaProd (J - 1)
else J ^ 2 * shuniaProd (J - 1) := by
have hfac : J.factorial = J * (J - 1).factorial := by
cases J with
| zero => omega
| succ J' => simp [Nat.factorial_succ]
have hp_iff := prime_iff_not_dvd_shuniaProd hJ
by_cases hdvd : J ∣ shuniaProd (J - 1)
· have hnotp : ¬ J.Prime := fun hp => (hp_iff.mp hp) hdvd
simp only [hdvd, ↓reduceIte]
simp only [shuniaProd, hfac, primorial_step hJ, hnotp, ↓reduceIte]
ring
· have hp : J.Prime := hp_iff.mpr hdvd
simp only [hdvd, ↓reduceIte]
simp only [shuniaProd, hfac, primorial_step hJ, hp, ↓reduceIte]
ring
/-! ## Section 5: Subroutine Specification (`SubEval`) & Combinators -/
@[simp] theorem retStack_append_zero (P : List (Fin 2)) :
retStack (P ++ [0]) = P ++ [1] := by simp [retStack, popBreakRev]
@[simp] theorem retStack_append_one (P : List (Fin 2)) :
retStack (P ++ [1]) = retStack P := by simp [retStack, popBreakRev]
@[simp] theorem trimZeros_append_zero (P : List (Fin 2)) :
trimZeros (P ++ [0]) = trimZeros P := by simp [trimZeros, dropLeadingZeros]
@[simp] theorem trimZeros_append_one (P : List (Fin 2)) :
trimZeros (P ++ [1]) = P ++ [1] := by simp [trimZeros, dropLeadingZeros]
theorem popContinueRev_zero_eq_dropLeadingZeros (rev : List (Fin 2)) :
popContinueRev 0 rev = dropLeadingZeros rev := by
induction rev with
| nil => rfl
| cons b rest ih =>
fin_cases b
· simp [popContinueRev, dropLeadingZeros, ih]
· rfl
theorem walkTape_of_walkInternal {q q' : Fin 121} {P : List (Fin 2)}
(h : walkInternal q P = some q') (walked rem : List (Fin 2)) :
walkTape q walked (P ++ rem) = walkTape q' (walked ++ P) rem := by
induction P generalizing q walked with
| nil =>
simp only [walkInternal] at h
by_cases hi : isInternalNode q = true
· simp [hi] at h; subst h; simp
· simp [hi] at h
| cons b bs ih =>
simp only [walkInternal] at h
by_cases hi : isInternalNode q = true
· simp only [hi, ↓reduceIte] at h
rcases htr : rh121Trans q b with _ | ⟨w, d, q₁⟩
· simp [htr] at h
· cases d with
| left => simp [htr] at h
| right =>
simp only [htr] at h
by_cases hcond : (w == b && isInternalNode q₁) = true
· simp only [hcond, ↓reduceIte] at h
rw [Bool.and_eq_true] at hcond
have hw : w = b := beq_iff_eq.mp hcond.1
subst hw
have hnotleaf := isLeafState_false_of_isInternalNode q₁ hcond.2
have hstep := ih h (walked ++ [w])
simpa [walkTape, htr, hnotleaf, List.append_assoc] using hstep
· simp [hcond] at h
· simp [hi] at h
theorem walkInternal_snoc {q q_mid q_end : Fin 121} {P : List (Fin 2)} {b : Fin 2}
(hP : walkInternal q P = some q_mid)
(htr : rh121Trans q_mid b = some (b, .right, q_end))
(hq_end : isInternalNode q_end = true) :
walkInternal q (P ++ [b]) = some q_end := by
induction P generalizing q with
| nil =>
simp only [walkInternal] at hP
by_cases hi : isInternalNode q = true
· simp [hi] at hP; subst hP
simp [walkInternal, hi, htr, hq_end]
· simp [hi] at hP
| cons x xs ih =>
simp only [List.cons_append, walkInternal] at hP ⊢
by_cases hi : isInternalNode q = true
· simp only [hi, ↓reduceIte] at hP ⊢
rcases htr_q : rh121Trans q x with _ | ⟨w, d, q₁⟩
· simp [htr_q] at hP
· cases d with
| left => simp [htr_q] at hP
| right =>
simp only [htr_q] at hP ⊢
by_cases hc : (w == x && isInternalNode q₁) = true
· simp only [hc, ↓reduceIte] at hP ⊢
exact ih hP
· simp [hc] at hP
· simp [hi] at hP
theorem evalStack_trimZeros_all :
∀ P ∈ allControlPrefixes, evalStack (trimZeros P) = evalStack P := by
decide
theorem evalStack_trimZeros_of_walkInternal {P : List (Fin 2)} {q : Fin 121}
(h : walkInternal 27 P = some q) :
evalStack (trimZeros P) = evalStack P :=
evalStack_trimZeros_all P (mem_allControlPrefixes_of_walkInternal h)
theorem padTo_of_le {bits : List (Fin 2)} {pos : ℕ} (h : bits.length ≤ pos) :
padTo bits pos = bits ++ List.replicate (pos - bits.length) 0 := by
unfold padTo
rw [List.take_of_length_le]
simp; omega
theorem padTo_of_ge {bits : List (Fin 2)} {pos : ℕ} (h : pos ≤ bits.length) :
padTo bits pos = bits.take pos := by
unfold padTo
have hsub : pos - bits.length = 0 := by omega
rw [hsub, List.replicate_zero, List.append_nil]
theorem evalStack_leaf0_of_walkInternal {P : List (Fin 2)} {q q' : Fin 121}
(h : walkInternal 27 P = some q)
(htr : rh121Trans q 0 = some (0, .right, q'))
(hleaf : isLeafState q' = true) :
evalStack P =
if isLeafInc q' then
.inc (leafReg q') (P ++ [1])
else
.decnz (leafReg q') (P ++ [1]) (retStack P) := by
have hwalk : walkTape 27 [] (P ++ List.replicate 18 0) =
.regLeaf q' (P ++ [0]) (List.replicate 17 0) := by
rw [walkTape_of_walkInternal h [] (List.replicate 18 0), List.nil_append]
change walkTape q P (0 :: List.replicate 17 0) = _
simp [walkTape, htr, hleaf]
have hpad : padTo P (P.length + 1) = P ++ [0] := by
rw [padTo_of_le (by omega)]; simp
have hbrk0 : applyBreak P (P.length + 1) 0 = P ++ [1] := by
simp [applyBreak, hpad, popBreakRev]
have hbrk1 : applyBreak P (P.length + 1) 1 = retStack P := by
simp [applyBreak, hpad, retStack, popBreakRev]
unfold evalStack
rw [hwalk]
simp [outcomeToInstr, hbrk0, hbrk1]
theorem evalStack_leaf1_inc_of_walkInternal {P : List (Fin 2)} {q q' : Fin 121}
(h : walkInternal 27 P = some q)
(htr : rh121Trans q 1 = some (1, .right, q'))
(hleaf : isLeafState q' = true) (hinc : isLeafInc q' = true) :
evalStack (P ++ [1]) = .inc (leafReg q') (retStack P) := by
have hwalk : walkTape 27 [] ((P ++ [1]) ++ List.replicate 18 0) =
.regLeaf q' (P ++ [1]) (List.replicate 18 0) := by
rw [List.append_assoc, walkTape_of_walkInternal h [] ([1] ++ List.replicate 18 0),
List.nil_append]
change walkTape q P (1 :: List.replicate 18 0) = _
simp [walkTape, htr, hleaf]
have hpad : padTo (P ++ [1]) (P.length + 1) = P ++ [1] := by
rw [padTo_of_le (by simp)]; simp
have hbrk0 : applyBreak (P ++ [1]) (P.length + 1) 0 = retStack P := by
simp [applyBreak, hpad, retStack, popBreakRev]
unfold evalStack
rw [hwalk]
simp [outcomeToInstr, hinc, hbrk0]
theorem evalStack_break1_of_walkInternal {P : List (Fin 2)} {q q' : Fin 121} {k : ℕ}
(h : walkInternal 27 P = some q)
(htr : rh121Trans q 1 = some (0, .left, q'))
(hq' : (q'.val = 19 ∧ k = 0) ∨ (q'.val = 91 ∧ k = 1)) :
evalStack (P ++ [1]) = .jump (popBreakRev k P.reverse).reverse := by
have hwalk : walkTape 27 [] ((P ++ [1]) ++ List.replicate 18 0) =
.jumpLeft q' P (List.replicate 18 0) := by
rw [List.append_assoc, walkTape_of_walkInternal h [] ([1] ++ List.replicate 18 0),
List.nil_append]
change walkTape q P (1 :: List.replicate 18 0) = _
simp [walkTape, htr]
have hpad : padTo (P ++ [1]) P.length = P := by
rw [padTo_of_ge (by simp)]; simp
unfold evalStack
rw [hwalk]
rcases hq' with ⟨hqv, rfl⟩ | ⟨hqv, rfl⟩
· simp [outcomeToInstr, hqv, applyBreak, hpad]
· simp [outcomeToInstr, hqv, applyBreak, hpad]
theorem evalStack_continue1_of_walkInternal {P : List (Fin 2)} {q q' : Fin 121} {k : ℕ}
(h : walkInternal 27 P = some q)
(htr : rh121Trans q 1 = some (0, .left, q'))
(hq' : (q'.val = 10 ∧ k = 0) ∨ (q'.val = 37 ∧ k = 1) ∨ (q'.val = 97 ∧ k = 2)) :
evalStack (P ++ [1]) = .jump (popContinueRev k P.reverse).reverse := by
have hwalk : walkTape 27 [] ((P ++ [1]) ++ List.replicate 18 0) =
.jumpLeft q' P (List.replicate 18 0) := by
rw [List.append_assoc, walkTape_of_walkInternal h [] ([1] ++ List.replicate 18 0),
List.nil_append]
change walkTape q P (1 :: List.replicate 18 0) = _
simp [walkTape, htr]
have hpad : padTo (P ++ [1]) P.length = P := by
rw [padTo_of_ge (by simp)]; simp
unfold evalStack
rw [hwalk]
rcases hq' with ⟨hqv, rfl⟩ | ⟨hqv, rfl⟩ | ⟨hqv, rfl⟩
· simp [outcomeToInstr, hqv, applyContinue, hpad]
· simp [outcomeToInstr, hqv, applyContinue, hpad]
· simp [outcomeToInstr, hqv, applyContinue, hpad]
@[simp] theorem rhRegNSteps_zero (c : RhRegCfg) : rhRegNSteps 0 c = some c := rfl
@[simp] theorem rhRegNSteps_succ (n : ℕ) (c : RhRegCfg) :
rhRegNSteps (n + 1) c = (rhRegStep c).bind (rhRegNSteps n) := rfl
theorem rhRegStep_one {c c' : RhRegCfg} (h : rhRegStep c = some c') :
rhRegNSteps 1 c = some c' := by simp [rhRegNSteps, h]
theorem step_rhRegNSteps {n : ℕ} {c₁ c₂ c₃ : RhRegCfg}
(h1 : rhRegStep c₁ = some c₂) (h2 : rhRegNSteps n c₂ = some c₃) :
rhRegNSteps (n + 1) c₁ = some c₃ := by simp [rhRegNSteps, h1, h2]
theorem rhRegNSteps_add (m n : ℕ) (c : RhRegCfg) :
rhRegNSteps (m + n) c = (rhRegNSteps m c).bind (rhRegNSteps n) := by
induction m generalizing c with
| zero => simp
| succ m ih =>
simp only [Nat.succ_add, rhRegNSteps_succ]
cases rhRegStep c <;> simp [ih]
theorem rhRegNSteps_bind_eq {m n : ℕ} {c₁ c₂ c₃ : RhRegCfg}
(h1 : rhRegNSteps m c₁ = some c₂) (h2 : rhRegNSteps n c₂ = some c₃) :
rhRegNSteps (m + n) c₁ = some c₃ := by
rw [rhRegNSteps_add, h1, Option.bind_some, h2]
theorem rhRegStep_of_jump {s s' : List (Fin 2)} {regs : List ℕ}
(h : evalStack s = .jump s') :
rhRegStep ⟨s, regs⟩ = some ⟨s', regs⟩ := by simp [rhRegStep, h]
theorem rhRegStep_of_inc {s s' : List (Fin 2)} {r : ℕ} {regs : List ℕ}
(h : evalStack s = .inc r s') :
rhRegStep ⟨s, regs⟩ = some ⟨s', regs.set r (regs.getD r 0 + 1)⟩ := by
simp [rhRegStep, h]
theorem rhRegStep_of_decnz_zero {s s_succ s_fail : List (Fin 2)} {r : ℕ} {regs : List ℕ}
(h : evalStack s = .decnz r s_succ s_fail) (hz : regs.getD r 0 = 0) :
rhRegStep ⟨s, regs⟩ = some ⟨s_fail, regs⟩ := by
unfold rhRegStep; rw [h]; dsimp only; rw [hz]; rfl
theorem rhRegStep_of_decnz_succ {s s_succ s_fail : List (Fin 2)} {r v : ℕ} {regs : List ℕ}
(h : evalStack s = .decnz r s_succ s_fail) (hv : regs.getD r 0 = v + 1) :
rhRegStep ⟨s, regs⟩ = some ⟨s_succ, regs.set r v⟩ := by
unfold rhRegStep; rw [h]; dsimp only; rw [hv]; rfl
/-- Universal specification of subroutine node `q` in the 63-node control DAG:
for any prefix `P` with `walkInternal 27 P = some q` and 5-register vector `regs`,
`rhRegNSteps` steps from `⟨trimZeros P, regs⟩` to `⟨retStack P, f regs⟩`. -/
def SubEval (q : Fin 121) (f : List ℕ → List ℕ) : Prop :=
∀ (P : List (Fin 2)),
walkInternal 27 P = some q →
∀ (regs : List ℕ), regs.length = 5 →
∃ s ≥ 1, rhRegNSteps s ⟨trimZeros P, regs⟩ = some ⟨retStack P, f regs⟩ ∧
(f regs).length = 5
theorem SubEval.congr {q : Fin 121} {f g : List ℕ → List ℕ}
(h : SubEval q f) (heq : ∀ regs, regs.length = 5 → f regs = g regs) :
SubEval q g := by
intro P hP regs hlen
obtain ⟨s, hs, hsteps, hflen⟩ := h P hP regs hlen
rw [heq regs hlen] at hsteps hflen
exact ⟨s, hs, hsteps, hflen⟩
theorem subEval_seq {q q₀ q₁ : Fin 121} {f₀ f₁ : List ℕ → List ℕ}
(htr0 : rh121Trans q 0 = some (0, .right, q₀)) (hq₀ : isInternalNode q₀ = true)
(htr1 : rh121Trans q 1 = some (1, .right, q₁)) (hq₁ : isInternalNode q₁ = true)
(h0 : SubEval q₀ f₀) (h1 : SubEval q₁ f₁) :
SubEval q (fun regs => f₁ (f₀ regs)) := by
intro P hP regs hlen
have hP0 := walkInternal_snoc hP htr0 hq₀
have hP1 := walkInternal_snoc hP htr1 hq₁
obtain ⟨s₀, hs₀, hsteps₀, hlen₀⟩ := h0 (P ++ [0]) hP0 regs hlen
rw [trimZeros_append_zero, retStack_append_zero] at hsteps₀
obtain ⟨s₁, hs₁, hsteps₁, hlen₁⟩ := h1 (P ++ [1]) hP1 (f₀ regs) hlen₀
rw [trimZeros_append_one, retStack_append_one] at hsteps₁
exact ⟨s₀ + s₁, by omega, rhRegNSteps_bind_eq hsteps₀ hsteps₁, hlen₁⟩
theorem subEval_inc_seq {q q₀ q₁ : Fin 121} {f₁ : List ℕ → List ℕ}
(htr0 : rh121Trans q 0 = some (0, .right, q₀))
(hleaf : isLeafState q₀ = true) (hinc : isLeafInc q₀ = true)
(htr1 : rh121Trans q 1 = some (1, .right, q₁)) (hq₁ : isInternalNode q₁ = true)
(h1 : SubEval q₁ f₁) :
SubEval q (fun regs =>
f₁ (regs.set (leafReg q₀) (regs.getD (leafReg q₀) 0 + 1))) := by
intro P hP regs hlen
have hev0 : evalStack (trimZeros P) = .inc (leafReg q₀) (P ++ [1]) := by
rw [evalStack_trimZeros_of_walkInternal hP, evalStack_leaf0_of_walkInternal hP htr0 hleaf]
simp [hinc]
let r₀ := regs.set (leafReg q₀) (regs.getD (leafReg q₀) 0 + 1)
have hstep0 : rhRegStep ⟨trimZeros P, regs⟩ = some ⟨P ++ [1], r₀⟩ :=
rhRegStep_of_inc hev0
have hP1 := walkInternal_snoc hP htr1 hq₁
obtain ⟨s₁, hs₁, hsteps₁, hlen₁⟩ := h1 (P ++ [1]) hP1 r₀ (by simp [r₀, hlen])
rw [trimZeros_append_one, retStack_append_one] at hsteps₁
exact ⟨s₁ + 1, by omega, step_rhRegNSteps hstep0 hsteps₁, hlen₁⟩
theorem subEval_seq_inc {q q₀ q₁ : Fin 121} {f₀ : List ℕ → List ℕ}
(htr0 : rh121Trans q 0 = some (0, .right, q₀)) (hq₀ : isInternalNode q₀ = true)
(htr1 : rh121Trans q 1 = some (1, .right, q₁))
(hleaf : isLeafState q₁ = true) (hinc : isLeafInc q₁ = true)
(h0 : SubEval q₀ f₀) :
SubEval q (fun regs =>
let r := f₀ regs
r.set (leafReg q₁) (r.getD (leafReg q₁) 0 + 1)) := by
intro P hP regs hlen
have hP0 := walkInternal_snoc hP htr0 hq₀
obtain ⟨s₀, hs₀, hsteps₀, hlen₀⟩ := h0 (P ++ [0]) hP0 regs hlen
rw [trimZeros_append_zero, retStack_append_zero] at hsteps₀
have hev1 := evalStack_leaf1_inc_of_walkInternal hP htr1 hleaf hinc
have hstep1 := rhRegStep_of_inc (regs := f₀ regs) hev1
refine ⟨s₀ + 1, by omega, rhRegNSteps_bind_eq hsteps₀ (rhRegStep_one hstep1), ?_⟩
simp [hlen₀]
theorem subEval_inc_inc {q q₀ q₁ : Fin 121}
(htr0 : rh121Trans q 0 = some (0, .right, q₀))
(hleaf0 : isLeafState q₀ = true) (hinc0 : isLeafInc q₀ = true)
(htr1 : rh121Trans q 1 = some (1, .right, q₁))
(hleaf1 : isLeafState q₁ = true) (hinc1 : isLeafInc q₁ = true) :
SubEval q (fun regs =>
let r₁ := regs.set (leafReg q₀) (regs.getD (leafReg q₀) 0 + 1)
r₁.set (leafReg q₁) (r₁.getD (leafReg q₁) 0 + 1)) := by
intro P hP regs hlen
have hev0 : evalStack (trimZeros P) = .inc (leafReg q₀) (P ++ [1]) := by
rw [evalStack_trimZeros_of_walkInternal hP, evalStack_leaf0_of_walkInternal hP htr0 hleaf0]
simp [hinc0]
have hstep0 := rhRegStep_of_inc (regs := regs) hev0
have hev1 := evalStack_leaf1_inc_of_walkInternal hP htr1 hleaf1 hinc1
let r₁ := regs.set (leafReg q₀) (regs.getD (leafReg q₀) 0 + 1)
have hstep1 := rhRegStep_of_inc (regs := r₁) hev1
refine ⟨2, by omega, step_rhRegNSteps hstep0 (rhRegStep_one hstep1), ?_⟩
simp [hlen]
theorem subEval_decnz_inc {q q₀ q₁ : Fin 121}
(htr0 : rh121Trans q 0 = some (0, .right, q₀))
(hleaf0 : isLeafState q₀ = true) (hdec0 : isLeafInc q₀ = false)
(htr1 : rh121Trans q 1 = some (1, .right, q₁))
(hleaf1 : isLeafState q₁ = true) (hinc1 : isLeafInc q₁ = true) :
SubEval q (fun regs =>
if regs.getD (leafReg q₀) 0 = 0 then regs
else
let r₁ := regs.set (leafReg q₀) (regs.getD (leafReg q₀) 0 - 1)
r₁.set (leafReg q₁) (r₁.getD (leafReg q₁) 0 + 1)) := by
intro P hP regs hlen
have hev0 : evalStack (trimZeros P) = .decnz (leafReg q₀) (P ++ [1]) (retStack P) := by
rw [evalStack_trimZeros_of_walkInternal hP, evalStack_leaf0_of_walkInternal hP htr0 hleaf0]
simp [hdec0]
rcases hv : regs.getD (leafReg q₀) 0 with _ | v
· have hstep0 := rhRegStep_of_decnz_zero hev0 hv
refine ⟨1, le_refl 1, ?_, ?_⟩
· dsimp only; rw [hv]; simp only [↓reduceIte]; exact rhRegStep_one hstep0
· dsimp only; rw [hv]; simp [hlen]
· have hstep0 := rhRegStep_of_decnz_succ hev0 hv
have hev1 := evalStack_leaf1_inc_of_walkInternal hP htr1 hleaf1 hinc1
have hstep1 := rhRegStep_of_inc (regs := regs.set (leafReg q₀) v) hev1
refine ⟨2, by omega, ?_, ?_⟩
· dsimp only; rw [hv]; simp only [Nat.succ_ne_zero, ↓reduceIte, Nat.add_sub_cancel]
exact step_rhRegNSteps hstep0 (rhRegStep_one hstep1)
· dsimp only; rw [hv]; simp [hlen]
theorem subEval_decnz_seq {q q₀ q₁ : Fin 121} {f₁ : List ℕ → List ℕ}
(htr0 : rh121Trans q 0 = some (0, .right, q₀))
(hleaf0 : isLeafState q₀ = true) (hdec0 : isLeafInc q₀ = false)
(htr1 : rh121Trans q 1 = some (1, .right, q₁)) (hq₁ : isInternalNode q₁ = true)
(h1 : SubEval q₁ f₁) :
SubEval q (fun regs =>
if regs.getD (leafReg q₀) 0 = 0 then regs
else f₁ (regs.set (leafReg q₀) (regs.getD (leafReg q₀) 0 - 1))) := by
intro P hP regs hlen
have hev0 : evalStack (trimZeros P) = .decnz (leafReg q₀) (P ++ [1]) (retStack P) := by
rw [evalStack_trimZeros_of_walkInternal hP, evalStack_leaf0_of_walkInternal hP htr0 hleaf0]
simp [hdec0]
rcases hv : regs.getD (leafReg q₀) 0 with _ | v
· have hstep0 := rhRegStep_of_decnz_zero hev0 hv
refine ⟨1, le_refl 1, ?_, ?_⟩
· dsimp only; rw [hv]; simp only [↓reduceIte]; exact rhRegStep_one hstep0
· dsimp only; rw [hv]; simp [hlen]
· have hstep0 := rhRegStep_of_decnz_succ hev0 hv
have hP1 := walkInternal_snoc hP htr1 hq₁
obtain ⟨s₁, hs₁, hsteps₁, hlen₁⟩ := h1 (P ++ [1]) hP1 (regs.set (leafReg q₀) v) (by simp [hlen])
rw [trimZeros_append_one, retStack_append_one] at hsteps₁
refine ⟨s₁ + 1, by omega, ?_, ?_⟩
· dsimp only; rw [hv]; simp only [Nat.succ_ne_zero, ↓reduceIte, Nat.add_sub_cancel]
exact step_rhRegNSteps hstep0 hsteps₁
· dsimp only; rw [hv]; simp only [Nat.succ_ne_zero, ↓reduceIte, Nat.add_sub_cancel]
exact hlen₁
/-- Generic `while_decnz(r, body)` loop combinator (`continue` via `37 = 6.continue.1`). -/
theorem subEval_while_body {q q_dec q_cont q_body : Fin 121}
(r : ℕ)
(htr0 : rh121Trans q 0 = some (0, .right, q_dec))
(hleaf : isLeafState q_dec = true) (hdec : isLeafInc q_dec = false)
(hlr : leafReg q_dec = r)
(htr1 : rh121Trans q 1 = some (1, .right, q_cont))
(hcont : isInternalNode q_cont = true)
(htr10 : rh121Trans q_cont 0 = some (0, .right, q_body))
(hbody : isInternalNode q_body = true)
(htr11 : rh121Trans q_cont 1 = some (0, .left, 37))
{body : List ℕ → List ℕ} (hSub : SubEval q_body body)
(g : List ℕ → List ℕ)
(hg_len : ∀ (regs : List ℕ), regs.length = 5 → (g regs).length = 5)
(hg_zero : ∀ (regs : List ℕ), regs.length = 5 → regs.getD r 0 = 0 → g regs = regs)
(hg_step : ∀ (regs : List ℕ) (v : ℕ), regs.length = 5 → regs.getD r 0 = v + 1 →
g (body (regs.set r v)) = g regs)
(hmeasure : ∀ (regs : List ℕ) (v : ℕ), regs.length = 5 → regs.getD r 0 = v + 1 →
(body (regs.set r v)).getD r 0 ≤ v) :
SubEval q g := by
intro P hP
have hP1 := walkInternal_snoc hP htr1 hcont
have hP10 := walkInternal_snoc hP1 htr10 hbody
have hev0 : evalStack (trimZeros P) = .decnz r (P ++ [1]) (retStack P) := by
rw [evalStack_trimZeros_of_walkInternal hP, evalStack_leaf0_of_walkInternal hP htr0 hleaf]
simp [hdec, hlr]
have hev11 : evalStack (P ++ [1] ++ [1]) = .jump (trimZeros P) := by
have h := evalStack_continue1_of_walkInternal (P := P ++ [1]) (k := 1) hP1 htr11
(Or.inr (Or.inl ⟨rfl, rfl⟩))
simpa [popContinueRev, popContinueRev_zero_eq_dropLeadingZeros, trimZeros] using h
have hloop : ∀ (k : ℕ) (regs : List ℕ), regs.length = 5 → regs.getD r 0 = k →
∃ s ≥ 1, rhRegNSteps s ⟨trimZeros P, regs⟩ = some ⟨retStack P, g regs⟩ ∧
(g regs).length = 5 := by
intro k
induction k using Nat.strong_induction_on with
| h k ih =>
intro regs hlen hk
rcases k with _ | v
· have hstep := rhRegStep_of_decnz_zero hev0 hk
rw [hg_zero regs hlen hk]
exact ⟨1, le_refl 1, rhRegStep_one hstep, hlen⟩
· have hstep1 := rhRegStep_of_decnz_succ hev0 hk
obtain ⟨sb, hsb, hsteps_b, hlen_b⟩ :=
hSub (P ++ [1] ++ [0]) hP10 (regs.set r v) (by simp [hlen])
rw [trimZeros_append_zero, trimZeros_append_one, retStack_append_zero] at hsteps_b
have hstep3 := rhRegStep_of_jump (regs := body (regs.set r v)) hev11
have hlt : (body (regs.set r v)).getD r 0 < v + 1 :=
Nat.lt_succ_of_le (hmeasure regs v hlen hk)
obtain ⟨s, hs, hsteps_s, _⟩ := ih _ hlt (body (regs.set r v)) hlen_b rfl
rw [hg_step regs v hlen hk] at hsteps_s
have hc1 := step_rhRegNSteps hstep1 hsteps_b
have hc2 := rhRegNSteps_bind_eq hc1 (step_rhRegNSteps hstep3 hsteps_s)
exact ⟨(sb + 1) + (s + 1), by omega, hc2, hg_len regs hlen⟩
intro regs hlen
exact hloop (regs.getD r 0) regs hlen rfl
/-- Generic two-stage `while_decnz(r, body₀; body₁)` loop combinator (`continue` via `97`). -/
theorem subEval_while_body2 {q q_dec q_c1 q_c2 : Fin 121}
(r : ℕ)
(htr0 : rh121Trans q 0 = some (0, .right, q_dec))
(hleaf : isLeafState q_dec = true) (hdec : isLeafInc q_dec = false)
(hlr : leafReg q_dec = r)
(htr1 : rh121Trans q 1 = some (1, .right, q_c1))
(hc1 : isInternalNode q_c1 = true)
(htr11 : rh121Trans q_c1 1 = some (1, .right, q_c2))
(hc2 : isInternalNode q_c2 = true)
{body : List ℕ → List ℕ}
(hBody : ∀ (P : List (Fin 2)), walkInternal 27 P = some q_c1 →
∀ (regs : List ℕ), regs.length = 5 →
∃ sb ≥ 1, rhRegNSteps sb ⟨trimZeros P, regs⟩ =
some ⟨P ++ [1] ++ [1], body regs⟩ ∧ (body regs).length = 5)
(htr_cont : rh121Trans q_c2 1 = some (0, .left, 97))
(g : List ℕ → List ℕ)
(hg_len : ∀ (regs : List ℕ), regs.length = 5 → (g regs).length = 5)
(hg_zero : ∀ (regs : List ℕ), regs.length = 5 → regs.getD r 0 = 0 → g regs = regs)
(hg_step : ∀ (regs : List ℕ) (v : ℕ), regs.length = 5 → regs.getD r 0 = v + 1 →
g (body (regs.set r v)) = g regs)
(hmeasure : ∀ (regs : List ℕ) (v : ℕ), regs.length = 5 → regs.getD r 0 = v + 1 →
(body (regs.set r v)).getD r 0 ≤ v) :
SubEval q g := by
intro P hP
have hP1 := walkInternal_snoc hP htr1 hc1
have hP11 := walkInternal_snoc hP1 htr11 hc2
have hev0 : evalStack (trimZeros P) = .decnz r (P ++ [1]) (retStack P) := by
rw [evalStack_trimZeros_of_walkInternal hP, evalStack_leaf0_of_walkInternal hP htr0 hleaf]
simp [hdec, hlr]
have hev_cont : evalStack (P ++ [1] ++ [1] ++ [1]) = .jump (trimZeros P) := by
have h := evalStack_continue1_of_walkInternal (P := P ++ [1] ++ [1]) (k := 2) hP11 htr_cont
(Or.inr (Or.inr ⟨rfl, rfl⟩))
simpa [popContinueRev, popContinueRev_zero_eq_dropLeadingZeros, trimZeros] using h
have hloop : ∀ (k : ℕ) (regs : List ℕ), regs.length = 5 → regs.getD r 0 = k →
∃ s ≥ 1, rhRegNSteps s ⟨trimZeros P, regs⟩ = some ⟨retStack P, g regs⟩ ∧
(g regs).length = 5 := by
intro k
induction k using Nat.strong_induction_on with
| h k ih =>
intro regs hlen hk
rcases k with _ | v
· have hstep := rhRegStep_of_decnz_zero hev0 hk
rw [hg_zero regs hlen hk]
exact ⟨1, le_refl 1, rhRegStep_one hstep, hlen⟩
· have hstep1 := rhRegStep_of_decnz_succ hev0 hk
obtain ⟨sb, hsb, hsteps_b, hlen_b⟩ := hBody (P ++ [1]) hP1 (regs.set r v) (by simp [hlen])
rw [trimZeros_append_one] at hsteps_b
have hstep_c := rhRegStep_of_jump (regs := body (regs.set r v)) hev_cont
have hlt : (body (regs.set r v)).getD r 0 < v + 1 :=
Nat.lt_succ_of_le (hmeasure regs v hlen hk)
obtain ⟨s, hs, hsteps_s, _⟩ := ih _ hlt (body (regs.set r v)) hlen_b rfl
rw [hg_step regs v hlen hk] at hsteps_s
have hc_a := step_rhRegNSteps hstep1 hsteps_b
have hc_b := rhRegNSteps_bind_eq hc_a (step_rhRegNSteps hstep_c hsteps_s)
exact ⟨(sb + 1) + (s + 1), by omega, hc_b, hg_len regs hlen⟩
intro regs hlen
exact hloop (regs.getD r 0) regs hlen rfl
/-- Generic `while_decnz(r₁, r₂.inc)` move combinator (`continue` via `37 = 6.continue.1`). -/
theorem subEval_move {q q_dec q₁ q_inc : Fin 121} (r₁ r₂ : ℕ)
(hr₁ : r₁ < 5) (hr₂ : r₂ < 5) (hne : r₁ ≠ r₂)
(htr0 : rh121Trans q 0 = some (0, .right, q_dec))
(hleaf_dec : isLeafState q_dec = true) (hdec : isLeafInc q_dec = false)
(hlr1 : leafReg q_dec = r₁)
(htr1 : rh121Trans q 1 = some (1, .right, q₁)) (hq₁ : isInternalNode q₁ = true)
(htr10 : rh121Trans q₁ 0 = some (0, .right, q_inc))
(hleaf_inc : isLeafState q_inc = true) (hinc : isLeafInc q_inc = true)
(hlr2 : leafReg q_inc = r₂)
(htr11 : rh121Trans q₁ 1 = some (0, .left, 37)) :
SubEval q (fun regs => (regs.set r₁ 0).set r₂ (regs.getD r₂ 0 + regs.getD r₁ 0)) := by
intro P hP
have hP1 := walkInternal_snoc hP htr1 hq₁
have hev0 : evalStack (trimZeros P) = .decnz r₁ (P ++ [1]) (retStack P) := by
rw [evalStack_trimZeros_of_walkInternal hP, evalStack_leaf0_of_walkInternal hP htr0 hleaf_dec]
simp [hdec, hlr1]
have hev10 : evalStack (P ++ [1]) = .inc r₂ (P ++ [1] ++ [1]) := by
have h := evalStack_leaf0_of_walkInternal hP1 htr10 hleaf_inc
simpa [hinc, hlr2] using h
have hev11 : evalStack (P ++ [1] ++ [1]) = .jump (trimZeros P) := by
have h := evalStack_continue1_of_walkInternal (P := P ++ [1]) (k := 1) hP1 htr11
(Or.inr (Or.inl ⟨rfl, rfl⟩))
simpa [popContinueRev, popContinueRev_zero_eq_dropLeadingZeros, trimZeros] using h
have hloop : ∀ (v : ℕ) (regs : List ℕ), regs.length = 5 → regs.getD r₁ 0 = v →
∃ s ≥ 1, rhRegNSteps s ⟨trimZeros P, regs⟩ =
some ⟨retStack P, (regs.set r₁ 0).set r₂ (regs.getD r₂ 0 + v)⟩ ∧
((regs.set r₁ 0).set r₂ (regs.getD r₂ 0 + v)).length = 5 := by
have hr₁' : r₁ = 0 ∨ r₁ = 1 ∨ r₁ = 2 ∨ r₁ = 3 ∨ r₁ = 4 := by omega
have hr₂' : r₂ = 0 ∨ r₂ = 1 ∨ r₂ = 2 ∨ r₂ = 3 ∨ r₂ = 4 := by omega
intro v
induction v with
| zero =>
intro regs hlen hv
rcases regs with _ | ⟨a0, _ | ⟨a1, _ | ⟨a2, _ | ⟨a3, _ | ⟨a4, _ | _⟩⟩⟩⟩⟩ <;>
try simp at hlen
have hstep := rhRegStep_of_decnz_zero hev0 hv
rcases hr₁' with rfl | rfl | rfl | rfl | rfl <;>
rcases hr₂' with rfl | rfl | rfl | rfl | rfl <;>
simp at hne hv ⊢ <;>
exact ⟨1, le_refl 1, by simpa [hv] using rhRegStep_one hstep⟩
| succ v ih =>
intro regs hlen hv
rcases regs with _ | ⟨a0, _ | ⟨a1, _ | ⟨a2, _ | ⟨a3, _ | ⟨a4, _ | _⟩⟩⟩⟩⟩ <;>
try simp at hlen
let regs₁ := ([a0, a1, a2, a3, a4].set r₁ v).set r₂ ([a0, a1, a2, a3, a4].getD r₂ 0 + 1)
have hstep1 := rhRegStep_of_decnz_succ hev0 hv
have hstep2 : rhRegStep ⟨P ++ [1], [a0, a1, a2, a3, a4].set r₁ v⟩ =
some ⟨P ++ [1] ++ [1], regs₁⟩ := by
rw [rhRegStep_of_inc hev10]
congr 2
rcases hr₁' with rfl | rfl | rfl | rfl | rfl <;>
rcases hr₂' with rfl | rfl | rfl | rfl | rfl <;>
simp at hne <;> rfl
have hstep3 := rhRegStep_of_jump (regs := regs₁) hev11
have hv₁ : regs₁.getD r₁ 0 = v := by
rcases hr₁' with rfl | rfl | rfl | rfl | rfl <;>
rcases hr₂' with rfl | rfl | rfl | rfl | rfl <;>
simp [regs₁] at hne ⊢
obtain ⟨s, hs, hsteps, _⟩ := ih regs₁ (by simp [regs₁]) hv₁
have heq : (regs₁.set r₁ 0).set r₂ (regs₁.getD r₂ 0 + v) =
([a0, a1, a2, a3, a4].set r₁ 0).set r₂
([a0, a1, a2, a3, a4].getD r₂ 0 + (v + 1)) := by
rcases hr₁' with rfl | rfl | rfl | rfl | rfl <;>
rcases hr₂' with rfl | rfl | rfl | rfl | rfl <;>
simp [regs₁] at hne ⊢ <;> omega
rw [heq] at hsteps
exact ⟨s + 3, by omega,
step_rhRegNSteps hstep1 (step_rhRegNSteps hstep2 (step_rhRegNSteps hstep3 hsteps)),
by simp⟩
intro regs hlen
exact hloop (regs.getD r₁ 0) regs hlen rfl
/-! ## Section 6: Verification of All Subroutines & Main Loop of `riemann121` -/
/-- `move(m, f)` (State `29`): `(m, j, x, f, v) ↦ (0, j, x, f + m, v)`. -/
theorem subEval_move_m_f :
SubEval 29 (fun regs => [0, regs.getD 1 0, regs.getD 2 0, regs.getD 3 0 + regs.getD 0 0,