forked from ccz181078/Coq-BB5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkelet1.v
1687 lines (1548 loc) · 47.1 KB
/
Skelet1.v
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
(** * Skelet #1 *)
(** Following https://www.sligocki.com/2023/03/13/skelet-1-infinite.html *)
Set Warnings "-abstract-large-number".
From Coq Require Import PeanoNat.
From Coq Require Import List. Import ListNotations.
From Coq Require Import Lia.
From Coq Require Import PArith.BinPos PArith.Pnat.
From Coq Require Import NArith.BinNat NArith.Nnat.
From Coq Require Import Program.Tactics.
From Coq Require Import ZifyBool.
From BusyCoq Require Import Individual52.
Set Default Goal Selector "!".
Definition tm : TM := fun '(q, s) =>
match q, s with
| A, 0 => Some (1, R, B) | A, 1 => Some (1, R, D)
| B, 0 => Some (1, L, C) | B, 1 => Some (0, R, C)
| C, 0 => Some (1, R, A) | C, 1 => Some (1, L, D)
| D, 0 => Some (0, R, E) | D, 1 => Some (0, L, B)
| E, 0 => None | E, 1 => Some (1, R, C)
end.
Notation "c --> c'" := (c -[ tm ]-> c') (at level 40).
Notation "c -->* c'" := (c -[ tm ]->* c') (at level 40).
Notation "c -->+ c'" := (c -[ tm ]->+ c') (at level 40).
Notation "l <| r" := (l <{{C}} 1 >> 0 >> r) (at level 30).
Notation "l |> r" := (l {{A}}> r) (at level 30).
Fixpoint run (n : nat) :=
match n with
| O => [0]
| S n => run n <: 1
end.
Definition x := run 2 <+ run 2.
Definition Dl := run 2 <+ run 1.
Definition Dr := run 2 +> run 1.
Definition C0 := run 2 <+ run 3 <+ run 2.
Definition C1 := run 2 <+ run 0 <+ run 1.
Definition C2 := run 4 <+ run 2.
Definition C3 := run 1 <+ run 1.
Notation C := C3.
Definition R := const 0.
Definition L := const 0.
Definition P := run 2.
Definition F0 := run 4 <+ run 3 <+ run 2.
Definition F1 := run 4 <+ run 0 <+ run 1.
Definition F2 := run 6 <+ run 2.
Definition F3 := run 3 <+ run 1.
Definition G0 := run 2 <+ run 3 <+ run 3 <+ run 2.
Definition G1 := run 2 <+ run 3 <+ run 0 <+ run 1.
Definition G2 := run 2 <+ run 5 <+ run 2.
Definition Fl := C2 <+ x^^7640 <+ Dl <+ x^^10344.
Definition Gl :=
x^^300 <+ Dl <+ x^^30826 <+ Dl <+ x^^72142 <+ Dl <+
x^^3076 <+ Dl <+ x^^1538 <+ Dl.
Definition Gr :=
x^^300 +> Dr +> x^^30826 +> Dr +> x^^72142 +> Dr +>
x^^3076 +> Dr +> x^^1538 +> Dr.
Definition Hl :=
C1 <+ Dl <+ x^^299 <+ C1 <+ Dl <+ x^^30825 <+
C1 <+ Dl <+ x^^72141 <+ C1 <+ Dl <+ x^^3075 <+
C1 <+ Dl <+ x^^1537.
Lemma rule_x_left : forall l r, l <* x <| r -->* l <| x *> r.
Proof. triv. Qed.
Lemma rule_D_left : forall l r, l <* Dl <| r -->* l <| Dr *> r.
Proof. triv. Qed.
Lemma rule_C_left : forall l r, l <* C <| r -->* l <| C *> r.
Proof. triv. Qed.
Lemma rule_x_right : forall l r, l |> x *> r -->* l <* x |> r.
Proof. triv. Qed.
Lemma rule_D_right : forall l r, l |> Dr *> r -->* l <* Dl |> r.
Proof. triv. Qed.
Lemma rule_xR : forall l, l <* x |> R -->* l <| C *> x *> P *> R.
Proof. unfold R, C, x, P. triv. Qed.
Lemma rule_DR : forall l, l <* Dl |> R -->* l <| x *> R.
Proof. unfold R, x. triv. Qed.
Lemma rule_L : forall r, L <| C *> x *> r -->* L <* C1 <* Dl |> P *> r.
Proof. unfold L, C1, Dl, C. triv. Qed.
Lemma rule_C30 : forall l r, l <* x |> C *> r -->* l <* C0 |> r.
Proof. triv. Qed.
Lemma rule_C01 : forall l r, l <* C0 <| r -->* l <* C1 <* x |> r.
Proof. triv. Qed.
Lemma rule_C12 : forall l r, l <* C1 <| r -->* l <* C2 |> r.
Proof. triv. Qed.
Lemma rule_C23 : forall l r, l <* C2 <| r -->* l <* C <* x |> r.
Proof. triv. Qed.
Lemma rule_DC : forall l r, l <* Dl |> C *> r -->* l <* P <* x |> r.
Proof. triv. Qed.
Lemma rule_C2_C : forall l r, l <* C2 |> C *> r -->* l <* F0 |> r.
Proof. triv. Qed.
Lemma rule_F0 : forall l r, l <* F0 <| r -->* l <* F1 <* x |> r.
Proof. triv. Qed.
Lemma rule_F1 : forall l r, l <* F1 <| r -->* l <* F2 |> r.
Proof. triv. Qed.
Lemma rule_F2 : forall l r, l <* F2 <| r -->* l <* F3 <* x |> r.
Proof. triv. Qed.
Lemma rule_F3 : forall l r, l <* x <* F3 <| r -->* l <* P <* C1 <* Dl |> r.
Proof. triv. Qed.
Lemma rule_C03 : forall l r, l <* C0 |> C *> r -->* l <* G0 |> r.
Proof. triv. Qed.
Lemma rule_G0 : forall l r, l <* G0 <| r -->* l <* G1 <* x |> r.
Proof. triv. Qed.
Lemma rule_G1 : forall l r, l <* G1 <| r -->* l <* G2 |> r.
Proof. triv. Qed.
Lemma rule_G2 : forall l r, l <* G2 <| r -->* l <* P <* Dl <* x |> r.
Proof. triv. Qed.
Lemma rule_P_left : forall l r, l <* P <| r -->* l <| P *> r.
Proof. triv. Qed.
Lemma rule_P_P : forall l r, l |> P *> P *> r -->* l <* x |> r.
Proof. triv. Qed.
Lemma rule_P_x : forall l r, l |> P *> x *> r -->* l <* x |> P *> r.
Proof. triv. Qed.
Lemma rule_P_R : forall l, l |> P *> R -->* l <| P *> R.
Proof. unfold P, R. triv. Qed.
Lemma rule_P_Dx : forall l r,
l |> P *> Dr *> x *> r -->* l <* C1 <* Dl |> P *> r.
Proof. triv. Qed.
Lemma rule_P_Cx : forall l r,
l |> P *> C *> x *> r -->* l <| P *> Dr *> P *> r.
Proof. triv. Qed.
Lemma rule_P_DP : forall l r,
l |> P *> Dr *> P *> r -->* l <* C1 <* Dl |> r.
Proof. triv. Qed.
Lemma rule_P_DDx : forall l r,
l |> P *> Dr *> Dr *> x *> r -->* l <* C2 <* C1 <* Dl |> r.
Proof. triv. Qed.
Lemma rule_P_DCx : forall l r,
l |> P *> Dr *> C *> x *> r -->* l <* G1 <* Dl |> P *> r.
Proof. triv. Qed.
Opaque x Dl Dr C0 C1 C2 C3 P F0 F1 F2 F3 G0 G1 G2.
Lemma rule_xn_left : forall n l r,
l <* x^^n <| r -->* l <| x^^n *> r.
Proof.
induction n; introv.
- finish.
- simpl_tape.
follow rule_x_left.
follow IHn.
simpl_tape. finish.
Qed.
Lemma rule_xn_right : forall n l r,
l |> x^^n *> r -->* l <* x^^n |> r.
Proof.
induction n; introv.
- finish.
- simpl_tape.
follow rule_x_right.
follow IHn.
simpl_tape. finish.
Qed.
Lemma rule_P_xn : forall n l r,
l |> P *> x^^n *> r -->* l <* x^^n |> P *> r.
Proof.
induction n; introv.
- finish.
- simpl_tape.
follow rule_P_x. follow IHn.
simpl_tape. finish.
Qed.
Lemma rule_P_DG : forall l r,
l |> P *> Dr *> Gr *> r -->* l <* Hl |> P *> Dr *> r.
Proof.
introv. unfold Gr.
rewrite (lpow_S 299). autorewrite with tape_post.
follow rule_P_Dx. follow rule_P_xn.
rewrite (lpow_S 30825). autorewrite with tape_post.
follow rule_P_Dx. follow rule_P_xn.
rewrite (lpow_S 72141). autorewrite with tape_post.
follow rule_P_Dx. follow rule_P_xn.
rewrite (lpow_S 3075). autorewrite with tape_post.
follow rule_P_Dx. follow rule_P_xn.
rewrite (lpow_S 1537). autorewrite with tape_post.
follow rule_P_Dx. follow rule_P_xn.
unfold Hl. autorewrite with tape_post.
finish.
Qed.
Lemma rule_P_DGn : forall n l r,
l |> P *> Dr *> Gr^^n *> r -->* l <* Hl^^n |> P *> Dr *> r.
Proof.
induction n; introv.
- finish.
- simpl_tape. follow rule_P_DG. follow IHn.
simpl_tape. finish.
Qed.
Lemma rule_G_right : forall l r, l |> Gr *> r -->* l <* Gl |> r.
Proof.
introv. unfold Gl, Gr.
autorewrite with tape_post.
repeat (follow rule_xn_right; follow rule_D_right).
finish.
Qed.
Lemma rule_Gn_right : forall n l r,
l |> Gr^^n *> r -->* l <* Gl^^n |> r.
Proof.
induction n; introv.
- finish.
- simpl_tape. follow rule_G_right.
follow IHn.
simpl_tape. finish.
Qed.
Lemma rule_G_left : forall l r, l <* Gl <| r -->* l <| Gr *> r.
Proof.
introv. unfold Gl, Gr.
autorewrite with tape_post.
repeat (follow rule_D_left; follow rule_xn_left).
finish.
Qed.
Lemma rule_Gn_left : forall n l r,
l <* Gl^^n <| r -->* l <| Gr^^n *> r.
Proof.
induction n; introv.
- finish.
- simpl_tape. follow rule_G_left.
follow IHn.
simpl_tape. finish.
Qed.
Inductive lsym :=
| l_xs (n : positive)
| l_D
| l_P
| l_C0 | l_C1 | l_C2 | l_C3
| l_F0 | l_F1 | l_F2 | l_F3
| l_G0 | l_G1 | l_G2
| l_Fs (n : positive)
| l_Gs (n : positive)
| l_Hs (n : positive).
Inductive rsym :=
| r_xs (n : positive)
| r_D
| r_C
| r_P
| r_Gs (n : positive).
Notation ltape := (list lsym).
Notation rtape := (list rsym).
Definition F := [l_xs 10344; l_D; l_xs 7640; l_C2].
Definition G := [r_xs 300; r_D; r_xs 30826; r_D; r_xs 72142; r_D;
r_xs 3076; r_D; r_xs 1538; r_D].
Definition J := [l_D; l_C2; l_xs 95; l_C0;
l_xs 7713; l_D; l_D; l_xs 1866; l_C1;
l_xs 13231; l_D; l_xs 6197; l_C3;
l_xs 11066; l_D; l_xs 7279; l_C0;
l_xs 10524; l_D; l_xs 7550; l_C2;
l_xs 10389; l_D; l_xs 7618; l_C1;
l_xs 10355; l_D; l_xs 7635; l_C3;
l_xs 10347; l_D; l_xs 7639; l_C3;
l_xs 10345; l_D; l_xs 7640; l_C1].
Definition K : rtape :=
[r_xs 7639; r_D; r_xs 10347; r_C;
r_xs 7635; r_D; r_xs 10355; r_C;
r_xs 7619; r_D; r_xs 10387; r_C;
r_xs 7555; r_D; r_xs 10515; r_C;
r_xs 7299; r_D; r_xs 11027; r_C;
r_xs 6275; r_D; r_xs 13075; r_C;
r_xs 2179; r_D; r_D; r_xs 7088; r_C;
r_xs 1; r_C; r_xs 3849; r_P].
Definition uni_P : positive := 53946.
Definition uni_T : positive := 4 * uni_P - 5.
Definition eqb_l (a b : lsym) : bool :=
match a,b with
| l_xs n1,l_xs n2 => Pos.eqb n1 n2
| l_D,l_D
| l_P,l_P
| l_C0,l_C0 | l_C1,l_C1 | l_C2,l_C2 | l_C3,l_C3
| l_F0,l_F0 | l_F1,l_F1 | l_F2,l_F2 | l_F3,l_F3
| l_G0,l_G0 | l_G1,l_G1 | l_G2,l_G2 => true
| l_Fs n1,l_Fs n2 => Pos.eqb n1 n2
| l_Gs n1,l_Gs n2 => Pos.eqb n1 n2
| l_Hs n1,l_Hs n2 => Pos.eqb n1 n2
| _,_ => false
end.
Lemma eqb_l_spec a b: if eqb_l a b then a=b else a<>b.
Proof.
destruct a,b; cbn; try congruence.
all: destruct (Pos.eqb_spec n n0); congruence.
Qed.
Definition eqb_r (a b : rsym) : bool :=
match a,b with
| r_xs n1,r_xs n2 => Pos.eqb n1 n2
| r_D,r_D => true
| r_C,r_C => true
| r_P,r_P => true
| r_Gs n1,r_Gs n2 => Pos.eqb n1 n2
| _,_ => false
end.
Fixpoint eqb_rtape (xs ys : rtape): bool :=
match xs,ys with
| xh::xt,yh::yt => if eqb_r xh yh then eqb_rtape xt yt else false
| nil,nil => true
| _,_ => false
end.
Notation left := TM.L.
Notation right := TM.R.
Notation conf := (dir * ltape * rtape)%type.
Fixpoint lift_right (t : rtape) : side :=
match t with
| [] => R
| r_xs n :: t => x^^(Pos.to_nat n) *> lift_right t
| r_D :: t => Dr *> lift_right t
| r_C :: t => C *> lift_right t
| r_P :: t => P *> lift_right t
| r_Gs n :: t => Gr^^(Pos.to_nat n) *> lift_right t
end.
Fixpoint lift_left (t : ltape) : side :=
match t with
| [] => L
| l_xs n :: t => lift_left t <* x^^(Pos.to_nat n)
| l_D :: t => lift_left t <* Dl
| l_P :: t => lift_left t <* P
| l_C0 :: t => lift_left t <* C0
| l_C1 :: t => lift_left t <* C1
| l_C2 :: t => lift_left t <* C2
| l_C3 :: t => lift_left t <* C3
| l_F0 :: t => lift_left t <* F0
| l_F1 :: t => lift_left t <* F1
| l_F2 :: t => lift_left t <* F2
| l_F3 :: t => lift_left t <* F3
| l_G0 :: t => lift_left t <* G0
| l_G1 :: t => lift_left t <* G1
| l_G2 :: t => lift_left t <* G2
| l_Fs n :: t => lift_left t <* Fl^^(Pos.to_nat n)
| l_Gs n :: t => lift_left t <* Gl^^(Pos.to_nat n)
| l_Hs n :: t => lift_left t <* Hl^^(Pos.to_nat n)
end.
Definition lift (c : conf) : Q * tape :=
match c with
| (left, l, r) => lift_left l <| lift_right r
| (right, l, r) => lift_left l |> lift_right r
end.
Definition decr (n : positive) : N :=
N.pred (N.pos n).
Lemma decr_nat : forall n, N.to_nat (decr (Pos.of_succ_nat n)) = n.
Proof. unfold decr. lia. Qed.
Definition lxs (n : N) (l : ltape) : ltape :=
match n with
| N0 => l
| Npos n =>
match l with
| l_xs m :: l => l_xs (n + m) :: l
| _ => l_xs n :: l
end
end.
Definition rxs (n : N) (r : rtape) : rtape :=
match n with
| N0 => r
| Npos n =>
match r with
| r_xs m :: r => r_xs (n + m) :: r
| _ => r_xs n :: r
end
end.
Definition Fls (n : N) (l : ltape) : ltape :=
match n with
| N0 => l
| Npos n =>
match l with
| l_Fs m :: l => l_Fs (n + m) :: l
| _ => l_Fs n :: l
end
end.
Definition Gls (n : N) (l : ltape) : ltape :=
match n with
| N0 => l
| Npos n =>
match l with
| l_Gs m :: l => l_Gs (n + m) :: l
| _ => l_Gs n :: l
end
end.
Definition Grs (n : N) (r : rtape) : rtape :=
match n with
| N0 => r
| Npos n =>
match r with
| r_Gs m :: r => r_Gs (n + m) :: r
| _ => r_Gs n :: r
end
end.
Definition Hls (n : N) (l : ltape) : ltape :=
match n with
| N0 => l
| Npos n =>
match l with
| l_Hs m :: l => l_Hs (n + m) :: l
| _ => l_Hs n :: l
end
end.
Ltac prove_liftrule :=
intros n t; destruct n; try reflexivity;
destruct t as [| [] t]; try reflexivity;
simpl; rewrite Pos2Nat.inj_add;
rewrite lpow_add; simpl_tape; reflexivity.
Lemma lift_lxs : forall n l,
lift_left (lxs n l) = lift_left l <* x^^(N.to_nat n).
Proof. prove_liftrule. Qed.
Lemma lift_rxs : forall n r,
lift_right (rxs n r) = x^^(N.to_nat n) *> lift_right r.
Proof. prove_liftrule. Qed.
Lemma lift_Fls : forall n l,
lift_left (Fls n l) = lift_left l <* Fl^^(N.to_nat n).
Proof. prove_liftrule. Qed.
Lemma lift_Gls : forall n l,
lift_left (Gls n l) = lift_left l <* Gl^^(N.to_nat n).
Proof. prove_liftrule. Qed.
Lemma lift_Grs : forall n r,
lift_right (Grs n r) = Gr^^(N.to_nat n) *> lift_right r.
Proof. prove_liftrule. Qed.
Lemma lift_Hls : forall n l,
lift_left (Hls n l) = lift_left l <* Hl^^(N.to_nat n).
Proof. prove_liftrule. Qed.
Ltac pos_nat p :=
let p' := fresh p in
let E := fresh in
destruct (Pos2Nat.is_succ p) as [p' E];
try rewrite E in *;
apply SuccNat2Pos.inv in E; subst p;
rename p' into p.
Ltac handle_decr :=
lazymatch goal with
| |- context [decr ?p] => pos_nat p; try rewrite decr_nat
end.
Ltac simp :=
simpl;
try rewrite lift_lxs;
try rewrite lift_rxs;
try rewrite lift_Fls;
try rewrite lift_Gls;
try rewrite lift_Grs;
try rewrite lift_Hls;
try rewrite Pos2Nat.inj_succ;
try handle_decr;
simpl.
Definition unrxs (r : rtape) : option rtape :=
match r with
| r_xs n :: r => Some (rxs (decr n) r)
| r_Gs n :: r =>
Some (r_xs 299 :: r_D :: r_xs 30826 :: r_D :: r_xs 72142 :: r_D ::
r_xs 3076 :: r_D :: r_xs 1538 :: r_D :: Grs (decr n) r)
| _ => None
end.
Lemma unrxs_spec : forall r r',
unrxs r = Some r' ->
lift_right r = x *> lift_right r'.
Proof.
introv H.
destruct r as [| [] r]; try discriminate; inverts H.
- simp. reflexivity.
- cbn[lift_right].
rewrite lift_Grs.
handle_decr.
cbn[lpow].
unfold Gr at 1.
replace 30826 with (Pos.to_nat 30826) by (rewrite <-Nat.eqb_eq; vm_compute; reflexivity).
replace 72142 with (Pos.to_nat 72142) by (rewrite <-Nat.eqb_eq; vm_compute; reflexivity).
replace 3076 with (Pos.to_nat 3076) by (rewrite <-Nat.eqb_eq; vm_compute; reflexivity).
replace 1538 with (Pos.to_nat 1538) by (rewrite <-Nat.eqb_eq; vm_compute; reflexivity).
autorewrite with tape_post.
generalize (x ^^ Pos.to_nat 30826 *> Dr *> x ^^ Pos.to_nat 72142 *> Dr *> x ^^ Pos.to_nat 3076 *> Dr *> x ^^ Pos.to_nat 1538 *> Dr *> Gr ^^ n *> lift_right r).
intro s.
replace (Pos.to_nat 299) with 299 by reflexivity.
reflexivity.
Time Qed.
Arguments unrxs _ : simpl never.
Definition simple_step (c : conf) : option conf :=
match c with
| (right, l, r) =>
match r with
| [] =>
match l with
(* x > R --> < C x P R *)
| l_xs n :: l =>
Some (left, lxs (decr n) l, [r_C; r_xs 1; r_P])
(* D > R --> < x R *)
| l_D :: l =>
Some (left, l, [r_xs 1])
| _ => None
end
(* > x^n --> x^n > *)
| r_xs n :: r =>
Some (right, lxs (N.pos n) l, r)
(* > D --> D > *)
| r_D :: r =>
Some (right, l_D :: l, r)
| r_C :: r =>
match l with
(* x > C --> C0 > *)
| l_xs n :: l =>
Some (right, l_C0 :: lxs (decr n) l, r)
(* D > C --> P x > *)
| l_D :: l =>
Some (right, l_xs 1 :: l_P :: l, r)
(* C0 > C --> G0 > *)
| l_C0 :: l =>
Some (right, l_G0 :: l, r)
(* C2 > C --> F0 > *)
| l_C2 :: l =>
Some (right, l_F0 :: l, r)
| _ => None
end
(* > P R --> < P R *)
| [r_P] =>
Some (left, l, [r_P])
(* > P x^n --> x^n > P *)
| r_P :: r_xs n :: r =>
Some (right, lxs (N.pos n) l, r_P :: r)
(* > P D x --> C1 D > P *)
(* Note: this rule doesn't use unrxs to let a more specialized rule
trigger on > P D G^n. *)
| r_P :: r_D :: r_xs n :: r =>
Some (right, l_D :: l_C1 :: l, r_P :: rxs (decr n) r)
(* > P DDx --> C2 C1 D > *)
| r_P :: r_D :: r_D :: r =>
match unrxs r with
| Some r => Some (right, l_D :: l_C1 :: l_C2 :: l, r)
| None => None
end
(* > P DCx --> G1 D > P *)
| r_P :: r_D :: r_C :: r =>
match unrxs r with
| Some r => Some (right, l_D :: l_G1 :: l, r_P :: r)
| None => None
end
(* > P D P --> C1 D > *)
| r_P :: r_D :: r_P :: r =>
Some (right, l_D :: l_C1 :: l, r)
(* > P D G^n --> H^n > P D *)
| r_P :: r_D :: r_Gs n :: r =>
Some (right, Hls (N.pos n) l, r_P :: r_D :: r)
(* > P C x --> < P D P *)
| r_P :: r_C :: r =>
match unrxs r with
| Some r => Some (left, l, r_P :: r_D :: r_P :: r)
| None => None
end
(* > P P --> x > *)
| r_P :: r_P :: r =>
Some (right, lxs 1 l, r)
(* > G^n --> G^n > *)
| r_Gs n :: r =>
Some (right, Gls (N.pos n) l, r)
| _ => None
end
| (left, l, r) =>
match l with
(* L < C x --> L C1 D > P *)
| [] =>
match r with
| r_C :: r =>
match unrxs r with
| Some r => Some (right, [l_D; l_C1], r_P :: r)
| None => None
end
| _ => None
end
(* x^n < --> < x^n *)
| l_xs n :: l =>
Some (left, l, rxs (N.pos n) r)
(* D < --> < D *)
| l_D :: l =>
Some (left, l, r_D :: r)
(* P < --> < P *)
| l_P :: l =>
Some (left, l, r_P :: r)
(* C0 < --> C1 x > *)
| l_C0 :: l =>
Some (right, l_xs 1 :: l_C1 :: l, r)
(* C1 < --> C2 > *)
| l_C1 :: l =>
Some (right, l_C2 :: l, r)
(* C2 < --> C3 x > *)
| l_C2 :: l =>
Some (right, l_xs 1 :: l_C3 :: l, r)
(* C < --> < C *)
| l_C3 :: l =>
Some (left, l, r_C :: r)
(* F0 < --> F1 x > *)
| l_F0 :: l =>
Some (right, l_xs 1 :: l_F1 :: l, r)
(* F1 < --> F2 > *)
| l_F1 :: l =>
Some (right, l_F2 :: l, r)
(* F2 < --> F3 x > *)
| l_F2 :: l =>
Some (right, l_xs 1 :: l_F3 :: l, r)
(* x F3 < --> P C1 D > *)
| l_F3 :: l_xs n :: l =>
Some (right, l_D :: l_C1 :: l_P :: lxs (decr n) l, r)
(* G0 < --> G1 x > *)
| l_G0 :: l =>
Some (right, l_xs 1 :: l_G1 :: l, r)
(* G1 < --> G2 > *)
| l_G1 :: l =>
Some (right, l_G2 :: l, r)
(* G2 < --> P D x > *)
| l_G2 :: l =>
Some (right, l_xs 1 :: l_D :: l_P :: l, r)
(* G^n < --> < G^n *)
| l_Gs n :: l =>
Some (left, l, Grs (N.pos n) r)
| _ => None
end
end.
Arguments lxs _ _ : simpl never.
Arguments rxs _ _ : simpl never.
Arguments Fls _ _ : simpl never.
Arguments Gls _ _ : simpl never.
Arguments Grs _ _ : simpl never.
Arguments Hls _ _ : simpl never.
Ltac unrxs :=
lazymatch goal with
| H: context [unrxs ?r] |- _ =>
let E := fresh "E" in
let r' := fresh "r'" in
destruct (unrxs r) as [r' |] eqn:E; try discriminate;
inverts H as H;
apply unrxs_spec in E; simp; simpl in E; try rewrite E
end.
Lemma simple_step_spec : forall c c',
simple_step c = Some c' ->
lift c -->* lift c'.
Proof.
introv H.
destruct c as [[[] l] r]; simpl in H.
- (* left *)
destruct l as [| [] l]; inverts H as H; simp.
+ (* L < C x --> L C1 D > P *)
destruct r as [| [] r]; try discriminate; unrxs.
apply rule_L.
+ (* x^n < --> < x^n *)
apply rule_xn_left.
+ (* D < --> < D *)
apply rule_D_left.
+ (* P < --> < P *)
apply rule_P_left.
+ (* C0 < --> C1 x > *)
apply rule_C01.
+ (* C1 < --> C2 > *)
apply rule_C12.
+ (* C2 < --> C3 x > *)
apply rule_C23.
+ (* C < --> < C *)
apply rule_C_left.
+ (* F0 < --> F1 x > *)
apply rule_F0.
+ (* F1 < --> F2 > *)
apply rule_F1.
+ (* F2 < --> F3 x > *)
apply rule_F2.
+ (* x F3 < --> P C1 D > *)
destruct l as [| [] l]; inverts H; simp.
apply rule_F3.
+ (* G0 < --> G1 x > *)
apply rule_G0.
+ (* G1 < --> G2 > *)
apply rule_G1.
+ (* G2 < --> P D x > *)
apply rule_G2.
+ (* G^n < --> < G^n *)
apply rule_Gn_left.
- (* right *)
destruct r as [| [] r]; inverts H as H; simp.
+ destruct l as [| [] l]; inverts H; simp.
* (* x > R --> < C x P R *)
apply rule_xR.
* (* D > R --> < x R *)
apply rule_DR.
+ (* > x^n --> x^n > *)
apply rule_xn_right.
+ (* > D --> D > *)
apply rule_D_right.
+ destruct l as [| [] l]; inverts H; simp.
* (* x > C --> C0 > *)
apply rule_C30.
* (* D > C --> P x *)
apply rule_DC.
* (* C0 > C --> G0 > *)
apply rule_C03.
* (* C2 > C --> F0 > *)
apply rule_C2_C.
+ destruct r as [| [] r]; inverts H as H; simp.
* (* > P R --> < P R *)
apply rule_P_R.
* (* > P x^n --> x^n > P *)
apply rule_P_xn.
* destruct r as [| [] r]; (unrxs || inverts H; simp).
** (* > P Dx --> C1 D > P *)
apply rule_P_Dx.
** (* > P DDx --> C2 C1 D > *)
apply rule_P_DDx.
** (* > P DCx --> G1 D > P *)
apply rule_P_DCx.
** (* > P DP --> C1 D *)
apply rule_P_DP.
** (* > P DG^n --> H^n P D *)
apply rule_P_DGn.
* (* > P C x --> < P D P *)
unrxs. apply rule_P_Cx.
* (* > P P --> x > *)
apply rule_P_P.
+ (* > G^n --> G^n > *)
apply rule_Gn_right.
Qed.
(** [max_stride] returns the maximum number of times the stride rule can
be applied to a tape before it becomes no longer possible. If [None]
is returned, that means the rule can be applied an arbitrarily high
amount of times – that happens for the very tail of the tape, without
any [r_C] symbols.
You'll note that [max_stride] is not critical for correctness in any way,
since if it returns a value that's too large, the acceleration will simply
not be applied. As such, we don't really prove anything about [max_stride].
*)
Fixpoint max_stride (xs : N) (t : rtape) : option N :=
match t with
| [r_P] => None
| r_P :: _ => Some 0%N
| [] => Some 0%N
| r_xs xs' :: t => max_stride (xs + N.pos xs') t
| r_D :: t => max_stride 0 t
| r_C :: t =>
match max_stride 0 t with
| Some n' => Some (N.min xs (N.shiftr n' 2))
| None => Some xs
end
| r_Gs _ :: t => max_stride 0 t
end.
Fixpoint stride (xs : N) (n : positive) (t : rtape) : option rtape :=
match t with
| [r_P] => Some (rxs xs [r_P])
| r_P :: _ => None
| [] => None
| r_xs xs' :: t => stride (xs + N.pos xs') n t
| r_D :: t =>
match stride 0 n t with
| Some t => Some (rxs xs (r_D :: t))
| None => None
end
| r_C :: t =>
if (N.pos n <=? xs)%N then
match stride 0 n~0~0 t with
| Some t => Some (rxs (xs - N.pos n) (r_C :: rxs (N.pos n~0) t))
| None => None
end
else
None
| r_Gs gs :: t =>
match stride 0 n t with
| Some t => Some (rxs xs (Grs (N.pos gs) t))
| None => None
end
end.
Lemma stride_rxs : forall t xs xs' n,
stride xs n (rxs xs' t) = stride (xs + xs') n t.
Proof.
introv.
destruct xs'.
- rewrite N.add_0_r. reflexivity.
- destruct t as [| [] t]; try reflexivity.
simpl. f_equal. lia.
Qed.
Lemma rxs_rxs : forall n m t,
rxs n (rxs m t) = rxs (n + m) t.
Proof.
introv.
destruct n, m; try reflexivity.
destruct t as [| [] t]; try reflexivity.
unfold rxs. simpl.
repeat (lia || f_equal).
Qed.
Lemma Fls_Fls : forall n m t,
Fls n (Fls m t) = Fls (n + m) t.
Proof.
introv.
destruct n, m; try reflexivity.
destruct t as [| [] t]; try reflexivity.
unfold Fls. simpl.
repeat (lia || f_equal).
Qed.
Lemma Grs_Grs : forall n m t,
Grs n (Grs m t) = Grs (n + m) t.
Proof.
introv.
destruct n, m; try reflexivity.
destruct t as [| [] t]; try reflexivity.
unfold Grs. simpl.
repeat (lia || f_equal).
Qed.
(** A "tail recursive" implementation of [stride] that hopefully, perhaps,
possibly, might be better performance-wise when evaluating within Coq.
Actual tail recursion would be a huge pain with smart constructors like
[rxs], so let's try explicit continuations first. *)
Fixpoint stride' (xs : N) (n :positive) (t : rtape)
(k : rtape -> rtape) : option rtape :=
match t with
| [r_P] => Some (k (rxs xs [r_P]))
| r_P :: _ => None
| [] => None
| r_xs xs' :: t => stride' (xs + N.pos xs') n t k
| r_D :: t => stride' 0 n t (fun t => k (rxs xs (r_D :: t)))
| r_C :: t =>
if (N.pos n <=? xs)%N then
stride' 0 n~0~0 t (fun t => k (rxs (xs - N.pos n)
(r_C :: rxs (N.pos n~0) t)))
else
None
| r_Gs gs :: t =>
stride' 0 n t (fun t => k (rxs xs (Grs (N.pos gs) t)))
end.
Lemma stride'_spec : forall t xs n k,
stride' xs n t k = option_map k (stride xs n t).
Proof.
induction t as [| [xs | | | | gs] t IH]; introv.
- reflexivity.
- simpl. rewrite IH. reflexivity.
- simpl. rewrite IH.
destruct (stride 0 n t); reflexivity.
- simpl.
destruct (N.pos n <=? xs)%N; try reflexivity.
rewrite IH. simpl.
destruct (stride 0 n~0~0 t); reflexivity.
- destruct t; reflexivity.
- simpl. rewrite IH.
destruct (stride 0 n t); reflexivity.
Qed.
Lemma stride_more : forall t t' xs xs' n,
stride xs' n t = Some t' ->
stride (xs + xs') n t = Some (rxs xs t').
Proof.
induction t as [| [xs'' | | | | gs] t IH]; introv H; simpl; simpl in H.
- (* [] *) discriminate.
(* simpl. rewrite rxs_rxs. reflexivity. *)
- (* r_xs xs'' :: t *)
simpl in H.
eapply IH in H.
rewrite <- N.add_assoc.
apply H.
- (* r_D :: t *)
simpl in H.
destruct (stride 0 n t) as [t1 |] eqn:E; inverts H.
rewrite rxs_rxs. reflexivity.
- (* r_C :: t *)
destruct (N.leb_spec (N.pos n) xs') as [Hle |]; try discriminate.
destruct (N.leb_spec (N.pos n) (xs + xs')) as [Hle' |]; try lia.
destruct (stride 0 n~0~0 t) as [t1 |] eqn:E; inverts H.
rewrite rxs_rxs.
repeat (lia || f_equal).
- (* r_P :: t *)
destruct t; inverts H.
rewrite rxs_rxs. reflexivity.
- (* r_Gs gs :: t *)
simpl in H.
destruct (stride 0 n t) as [t1 |] eqn:E; inverts H.
rewrite rxs_rxs. reflexivity.
Qed.
Lemma stride_Grs : forall t t' xs gs n,
stride 0 n t = Some t' ->
stride xs n (Grs gs t) = Some (rxs xs (Grs gs t')).
Proof.
introv H.
destruct gs.
- eapply stride_more in H. rewrite N.add_0_r in H. exact H.
- destruct t as [| [] t]; try discriminate;
try (unfold Grs at 1; simpl; simpl in H; rewrite H; reflexivity).
simpl. simpl in H. unfold rxs in H.
destruct (stride 0 n t) as [t1|]; inverts H.
rewrite Grs_Grs.
repeat (lia || f_equal).
Qed.
Lemma stride_add : forall t t2 xs n m,
stride xs (n + m) t = Some t2 ->
exists t1, stride xs n t = Some t1 /\ stride 0 m t1 = Some t2.
Proof.
induction t as [| [xs' | | | | gs] t IH]; introv H.
- (* [] *) discriminate.
- (* r_xs xs' :: t *)
simpl in H.
apply IH in H. destruct H as [t1 [H1 H2]].
exists t1. intuition.
- (* r_D :: t *)
simpl in H.
destruct (stride 0 (n + m) t) as [t2' |] eqn:E; inverts H.
apply IH in E. destruct E as [t1 [H1 H2]].
exists (rxs xs (r_D :: t1)).
simpl. rewrite H1.
repeat split.
rewrite stride_rxs. simpl.
rewrite H2. reflexivity.
- (* r_C :: t *)
simpl in H. simpl.
destruct (N.leb_spec (N.pos (n + m)) xs) as [Hle |]; try discriminate.
destruct (stride 0 (n + m)~0~0 t) as [t2' |] eqn:E; inverts H.
destruct (N.leb_spec (N.pos n) xs) as [Hle' |]; try lia.
replace (n + m)~0~0%positive with (n~0~0 + m~0~0)%positive in E by lia.
apply IH in E. destruct E as [t1 [H1 H2]].
rewrite H1.
eexists. repeat split.
rewrite stride_rxs. simpl.
destruct (N.leb_spec (N.pos m) (xs - N.pos n)) as [Hle'' |]; try lia.
rewrite stride_rxs. simpl.
eapply stride_more in H2.
rewrite N.add_0_r in H2. rewrite H2.
rewrite rxs_rxs.
repeat (lia || f_equal).
- (* r_P :: t *)
simpl in H.
destruct t; inverts H.
eexists. repeat split.
rewrite stride_rxs. reflexivity.
- (* r_Gs gs :: t *)
simpl in H.
destruct (stride 0 (n + m) t) as [t2' |] eqn:E; inverts H.
apply IH in E. destruct E as [t1 [H1 H2]].
exists (rxs xs (Grs (N.pos gs) t1)).
simpl. rewrite H1.
repeat split.
rewrite stride_rxs.
eapply stride_Grs in H2. rewrite H2.
reflexivity.
Qed.
Fixpoint stride_level (t : rtape) : nat :=
match t with
| [] => 0
| r_C :: t => S (stride_level t)
| _ :: t => stride_level t
end.
Lemma stride_level_rxs : forall xs t,
stride_level (rxs xs t) = stride_level t.
Proof.
introv. destruct xs; try reflexivity.
destruct t as [| [] t]; reflexivity.
Qed.