forked from ccz181078/Coq-BB5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBB52Theorem.v
19024 lines (17828 loc) · 745 KB
/
BB52Theorem.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
Require Import List.
Require Import ZArith.
Require Import Logic.FunctionalExtensionality.
Require Import Lia.
Require Import FSets.FMapPositive.
From BusyCoq Require Import BB52Statement.
Set Warnings "-abstract-large-number".
Ltac invst H := inversion H; subst.
Ltac ctor := constructor.
Ltac ector := econstructor.
Ltac fext := apply functional_extensionality; intro.
Ltac gd x := generalize dependent x.
Ltac cg := try congruence.
Ltac clr_eqrefl:=
repeat match goal with
| H: ?A = ?A |- _ => clear H
end.
Ltac clr_dup:=
repeat match goal with
| H:?A, H':?A |- _ => clear H'
end.
Ltac clrs:=
clr_eqrefl; clr_dup.
Definition is_inj{T1 T2}(f:T1->T2):=
forall a b, f a = f b -> a = b.
Lemma ffx_eq_x_inj{A}:
forall f:A->A,
(forall x:A, f (f x) = x) ->
forall x y:A, f x = f y -> x = y.
Proof.
intros.
assert ((f (f x)) = (f (f y))) as H1. {
rewrite H0,H. reflexivity.
}
rewrite H,H in H1.
apply H1.
Qed.
Ltac ff_inj RR H := pose proof (ffx_eq_x_inj _ RR _ _ H); subst.
Fixpoint positive_len(x:positive):nat :=
match x with
| xH => O
| xI x0 => S (positive_len x0)
| xO x0 => S (positive_len x0)
end.
Fixpoint enc_v1(x:positive):positive :=
match x with
| xH => xH
| xI x0 => xI (xI (enc_v1 x0))
| xO x0 => xI (xO (enc_v1 x0))
end.
Lemma enc_v1_eq a1 b1 a2 b2:
append (enc_v1 a1) (xO b1) = append (enc_v1 a2) (xO b2) ->
(a1 = a2 /\ b1 = b2).
Proof.
gd a2.
induction a1; destruct a2; cbn; intros; cg; invst H.
1,2: destruct (IHa1 a2 H1).
all: split; cg.
Qed.
Definition enc_pair(x:positive*positive):positive :=
let (x1,x2):=x in
append (enc_v1 (Pos.of_succ_nat (positive_len x1))) (xO (append x1 x2)).
Lemma enc_pair_inj: is_inj enc_pair.
Proof.
intros x1 x2 H.
destruct x1 as [a1 b1].
destruct x2 as [a2 b2].
unfold enc_pair in H.
destruct (enc_v1_eq _ _ _ _ H) as [Ha Hb]. clear H.
assert (positive_len a1 = positive_len a2) by lia. clear Ha.
gd a2.
induction a1; destruct a2; cbn; intros; cg;
invst Hb;
invst H;
assert ((a1,b1)=(a2,b2)) by auto 2;
cg.
Qed.
Fixpoint enc_list(x:list positive):positive :=
match x with
| nil => xH
| h::t => enc_pair (h,enc_list t)
end.
Lemma enc_list_inj: is_inj enc_list.
Proof.
intros x1 x2 H.
gd x2.
induction x1 as [|h1 t1]; destruct x2 as [|h2 t2]; intros; cg.
- cbn in H. destruct ((Pos.of_succ_nat (positive_len h2))); cbn in H; cg.
- cbn in H. destruct ((Pos.of_succ_nat (positive_len h1))); cbn in H; cg.
- epose proof (enc_pair_inj _ _ H).
invst H0.
f_equal.
apply IHt1; assumption.
Qed.
Notation "x &&& y" := (if x then y else false) (at level 80, right associativity).
Notation "x ||| y" := (if x then true else y) (at level 85, right associativity).
Lemma andb_shortcut_spec(a b:bool):
(a&&&b) = (a&&b)%bool.
Proof.
reflexivity.
Qed.
Lemma orb_shortcut_spec(a b:bool):
(a|||b) = (a||b)%bool.
Proof.
reflexivity.
Qed.
Definition set_ins{T}(enc:T->positive)(s:list T * PositiveMap.tree unit)(x:T):(list T * PositiveMap.tree unit)*bool :=
let enc := (enc x) in
match PositiveMap.find enc (snd s) with
| None => ((x::fst s, PositiveMap.add enc tt (snd s)),false)
| Some _ => (s,true)
end.
Definition set_in{T}(enc:T->positive)(s:list T * PositiveMap.tree unit)(x:T):Prop :=
PositiveMap.find (enc x) (snd s) = Some tt.
Definition set_WF{T}(enc:T->positive)(s:list T * PositiveMap.tree unit):Prop :=
forall (x:T),
set_in enc s x <->
In x (fst s).
Lemma set_ins_spec{T} enc (enc_inj: is_inj enc) s (x:T) s' flag:
set_WF enc s ->
set_ins enc s x = (s',flag) ->
(set_WF enc s' /\
(flag=true -> (s'=s /\ set_in enc s x))).
Proof.
unfold set_WF,set_ins,set_in.
intros.
destruct (PositiveMap.find (enc x) (snd s)) as [v|] eqn:E.
- invst H0.
split.
1: assumption.
intros.
destruct v.
split; cg.
- invst H0. clear H0.
split; intros. 2: cg.
cbn.
rewrite (PositiveMapAdditionalFacts.gsspec).
destruct (PositiveMap.E.eq_dec (enc x0) (enc x)) as [e|e].
+ pose proof (enc_inj _ _ e); subst.
split; tauto.
+ assert (x<>x0) by cg.
split; intro.
* right. rewrite <-H. apply H1.
* rewrite <-H in H1.
destruct H1 as [H1|H1]; cg.
Qed.
Lemma empty_set_WF{T}(enc:T->positive):
set_WF enc (nil, PositiveMap.empty unit).
Proof.
unfold set_WF.
intros. cbn.
split; intro.
2: contradiction.
unfold set_in in H.
rewrite PositiveMap.gempty in H. cg.
Qed.
Fixpoint pop_back{T}(x:T)(ls:list T):(list T) :=
match ls with
| h::t =>
x::(pop_back h t)
| nil => nil
end.
Lemma pop_back_len{T} (h:T) t:
length (pop_back h t) = length t.
Proof.
gd h.
induction t; intros; cbn.
- reflexivity.
- f_equal; apply IHt.
Qed.
Lemma pop_back__nth_error{T} (h:T) t:
forall n:nat,
n<length t ->
nth_error (pop_back h t) n =
nth_error (h::t) n.
Proof.
gd h.
induction t; intros.
- cbn in H. lia.
- cbn.
destruct n.
1: reflexivity.
cbn.
apply IHt. cbn in H. lia.
Qed.
Lemma list_eq__nth_error{T}(ls1 ls2:list T):
length ls1 = length ls2 ->
(forall n:nat,
n<length ls1 ->
nth_error ls1 n = nth_error ls2 n) ->
ls1=ls2.
Proof.
gd ls2.
induction ls1.
- intros.
destruct ls2.
+ reflexivity.
+ cbn in H. cg.
- intros.
destruct ls2.
+ cbn in H. cg.
+ assert (a=t) as H1. {
assert (Some a = Some t) as H1 by (apply (H0 0); cbn; lia).
cg.
}
subst. f_equal.
cbn in H. invst H.
eapply IHls1.
1: assumption.
intros.
apply (H0 (S n)).
cbn. lia.
Qed.
Fixpoint pop_back'{T}(x:T)(ls:list T):(list T)*T :=
match ls with
| nil => (nil,x)
| h :: t => let (a,b):=pop_back' h t in (x::a,b)
end.
Lemma pop_back'__push_back{T} (h:T) t x2:
pop_back' h (t ++ x2 :: nil) = (h::t,x2).
Proof.
gd h.
induction t; intros; cbn; cg.
rewrite IHt. reflexivity.
Qed.
Definition St_enc(x:St):positive :=
match x with
| St0 => xH
| St1 => xO xH
| St2 => xI xH
| St3 => xO (xO xH)
| St4 => xI (xO xH)
end.
Lemma St_enc_inj: is_inj St_enc.
intros x1 x2.
destruct x1,x2; cbn; cg.
Qed.
Definition St_eqb(s1 s2:St) :=
match s1,s2 with
| St0,St0 | St1,St1 | St2,St2 | St3,St3 | St4,St4 => true
| _,_ => false
end.
Lemma St_eqb_spec s1 s2:
if St_eqb s1 s2 then s1=s2 else s1<>s2.
Proof.
destruct s1,s2; cbn; congruence.
Qed.
Ltac eq_dec eqb_spec eqb s1 s2 :=
pose proof (eqb_spec s1 s2);
destruct (eqb s1 s2).
Ltac St_eq_dec s1 s2 :=
eq_dec St_eqb_spec St_eqb s1 s2.
Definition Σ_eqb(i1 i2:Σ) :=
match i1,i2 with
| Σ0,Σ0 | Σ1,Σ1 => true
| _,_ => false
end.
Lemma Σ_eqb_spec i1 i2:
if Σ_eqb i1 i2 then i1=i2 else i1<>i2.
Proof.
destruct i1,i2; cbn; congruence.
Qed.
Definition Σ_enc(x:Σ):positive :=
match x with
| Σ0 => xH
| Σ1 => xO xH
end.
Lemma Σ_enc_inj: is_inj Σ_enc.
intros x1 x2.
destruct x1,x2; cbn; cg.
Qed.
Fixpoint listΣ_enc(x:list Σ):positive :=
match x with
| nil => xH
| Σ0::t => xO (listΣ_enc t)
| Σ1::t => xI (listΣ_enc t)
end.
Lemma listΣ_inj: is_inj listΣ_enc.
Proof.
intros x1 x2 H.
gd x2.
induction x1 as [|h1 t1]; destruct x2 as [|h2 t2]; cbn; intros; cg.
- destruct h2; invst H.
- destruct h1; invst H.
- destruct h1,h2; invst H.
1,2: f_equal; apply IHt1,H1.
Qed.
Lemma map_inj{T1 T2}(f:T1->T2): is_inj f -> is_inj (map f).
Proof.
intros H x1 x2 H0.
gd x2.
induction x1 as [|h1 t1]; destruct x2 as [|h2 t2]; cbn; intros; cg.
invst H0.
rewrite (IHt1 _ H3).
f_equal.
apply H,H2.
Qed.
Definition listT_enc{T}(f:T->positive)(x:list T):positive :=
enc_list (map f x).
Lemma listT_enc_inj{T}(f:T->positive): is_inj f -> is_inj (listT_enc f).
Proof.
intros H x1 x2 H0.
unfold listT_enc.
apply (map_inj _ H).
unfold listT_enc in H0.
apply enc_list_inj,H0.
Qed.
Definition Dir_rev(d:Dir) :=
match d with
| Dneg => Dpos
| Dpos => Dneg
end.
Definition Dir_eqb(d1 d2:Dir):bool :=
match d1,d2 with
| Dpos,Dpos | Dneg,Dneg => true
| _,_ => false
end.
Lemma Dir_eqb_spec d1 d2:
if Dir_eqb d1 d2 then d1=d2 else d1<>d2.
Proof.
destruct d1,d2; cbn; cg.
Qed.
Ltac Dir_eq_dec s1 s2 :=
eq_dec Dir_eqb_spec Dir_eqb s1 s2.
Definition St_list:= St0::St1::St2::St3::St4::nil.
Definition Σ_list:= Σ0::Σ1::nil.
Definition Dir_list := Dpos::Dneg::nil.
Lemma St_list_spec:
forall s, In s St_list.
Proof.
intro s.
destruct s; cbn; tauto.
Qed.
Lemma Σ_list_spec:
forall s, In s Σ_list.
Proof.
intro s.
destruct s; cbn; tauto.
Qed.
Lemma Dir_list_spec:
forall s, In s Dir_list.
Proof.
intro s.
destruct s; cbn; tauto.
Qed.
Definition forallb_St f :=
forallb f St_list.
Definition forallb_Σ f :=
forallb f Σ_list.
Definition forallb_Dir f :=
forallb f Dir_list.
Lemma forallb_St_spec f:
forallb_St f = true <-> forall s, f s = true.
Proof.
unfold forallb_St.
rewrite forallb_forall.
split; intros.
- apply H,St_list_spec.
- apply H.
Qed.
Lemma forallb_Σ_spec f:
forallb_Σ f = true <-> forall s, f s = true.
Proof.
unfold forallb_Σ.
rewrite forallb_forall.
split; intros.
- apply H,Σ_list_spec.
- apply H.
Qed.
Lemma forallb_Dir_spec f:
forallb_Dir f = true <-> forall s, f s = true.
Proof.
unfold forallb_Dir.
rewrite forallb_forall.
split; intros.
- apply H,Dir_list_spec.
- apply H.
Qed.
Section TM.
Hypothesis Σ:Set.
Hypothesis Σ0:Σ.
Definition HaltsAt(tm:TM Σ)(n:nat)(st:ExecState Σ): Prop :=
exists st', Steps Σ tm n st st' /\ step Σ tm st' = None.
Definition Halts(tm:TM Σ)(st:ExecState Σ): Prop :=
exists n, HaltsAt tm n st.
Definition HaltsFromInit(tm:TM Σ): Prop :=
Halts tm (InitES Σ Σ0).
Lemma Steps_trans {tm n m st st0 st1}:
Steps Σ tm n st st0 ->
Steps Σ tm m st0 st1 ->
Steps Σ tm (m+n) st st1.
Proof.
intro H.
gd st1.
induction m; intros; cbn; invst H0.
- assumption.
- ector; eauto.
Qed.
Lemma Steps_unique {tm n st st0 st1}:
Steps Σ tm n st st0 ->
Steps Σ tm n st st1 ->
st0 = st1.
Proof.
gd st1.
gd st0.
induction n; intros st0 st1 H H0; invst H; invst H0.
- reflexivity.
- specialize (IHn _ _ H2 H3). subst.
cg.
Qed.
Lemma Steps_NonHalt {tm m n st st0}:
m<n ->
Steps Σ tm n st st0 ->
~HaltsAt tm m st.
Proof.
intros.
gd st0.
induction n; intros.
- lia.
- assert (H1:m<n\/m=n) by lia.
destruct H1 as [H1|H1].
+ invst H0.
apply (IHn H1 _ H3).
+ subst.
invst H0.
unfold HaltsAt,Halts.
intro H1.
destruct H1 as [st' [H1a H1b]].
rewrite <-(Steps_unique H2 H1a) in H1b.
destruct st2. cg.
Qed.
Lemma HaltsAt_unique {tm n1 n2 st}:
HaltsAt tm n1 st ->
HaltsAt tm n2 st ->
n1=n2.
Proof.
intros.
pose proof H as H''.
pose proof H0 as H0''.
unfold HaltsAt in H,H0.
pose proof H as H'.
pose proof H0 as H0'.
destruct H as [st0 [Ha Hb]].
destruct H0 as [st1 [H0a H0b]].
assert (n1=n2\/n1<n2\/n2<n1) by lia.
destruct H as [H|[H|H]]; cg.
- destruct (Steps_NonHalt H H0a H'').
- destruct (Steps_NonHalt H Ha H0'').
Qed.
Definition NonHalt tm st :=
forall n, exists st', Steps Σ tm n st st'.
Lemma NonHalt_iff {tm st}:
NonHalt tm st <-> ~Halts tm st.
Proof.
split; intro.
- intro H0.
destruct H0 as [n H0].
specialize (H (S n)).
destruct H as [st' H].
eapply Steps_NonHalt.
2,3: eassumption.
lia.
- intro n.
induction n.
+ eexists. ector.
+ destruct IHn as [st' IHn].
unfold Halts,HaltsAt in H.
destruct (step Σ tm st') as [st''|] eqn:E.
* exists st''. ector; eassumption.
* assert False by (apply H; eexists; eexists; split; eassumption).
contradiction.
Qed.
Definition LE(tm tm':TM Σ): Prop :=
forall (s:St)(i:Σ),
tm s i = tm' s i \/
tm s i = None.
Lemma LE_step tm tm' st st0:
LE tm tm' ->
step Σ tm st = Some st0 ->
step Σ tm' st = Some st0.
Proof.
unfold LE,step.
destruct st as [s t].
intros.
specialize (H s (t Z0)).
destruct (tm s (t Z0)) as [[s' d o]|]; cg.
destruct H; cg.
rewrite <-H. cg.
Qed.
Lemma LE_Steps {tm tm' n st st0}:
LE tm tm' ->
Steps Σ tm n st st0 ->
Steps Σ tm' n st st0.
Proof.
intros.
induction H0.
- ctor.
- ector.
1: apply IHSteps,H.
eapply LE_step; eassumption.
Qed.
Lemma LE_NonHalts {tm tm' st}:
LE tm tm' ->
~Halts tm st ->
~Halts tm' st.
Proof.
repeat rewrite <-NonHalt_iff.
unfold NonHalt.
intros.
destruct (H0 n) as [st' H1].
exists st'.
eapply LE_Steps; eassumption.
Qed.
Hypothesis BB:nat.
Definition HaltTimeUpperBound(st:ExecState Σ)(P:TM Σ->Prop):Prop :=
forall (tm:TM Σ)(n0:nat), P tm -> HaltsAt tm n0 st -> n0<=BB.
Lemma HaltTimeUpperBound_LE_NonHalt {st tm}:
~Halts tm st ->
HaltTimeUpperBound st (LE tm).
Proof.
unfold HaltTimeUpperBound.
intros.
pose proof (LE_NonHalts H0 H) as H2.
assert False by (apply H2; eexists; apply H1).
contradiction.
Qed.
Hypothesis Σ_eqb:Σ->Σ->bool.
Hypothesis Σ_eqb_spec: forall i1 i2, if Σ_eqb i1 i2 then i1=i2 else i1<>i2.
Definition TM_upd tm s i t: TM Σ :=
fun s0 i0 =>
if (andb (St_eqb s0 s) (Σ_eqb i0 i)) then t else tm s0 i0.
Lemma LE_HaltsAtES_1 {tm tm0 n st s t}:
LE tm tm0 ->
HaltsAt tm n st ->
Steps Σ tm n st (s,t) ->
tm0 s (t 0%Z) = None ->
HaltsAt tm0 n st.
Proof.
intros.
unfold HaltsAt.
epose proof (LE_Steps H H1).
exists (s,t).
split. 1: assumption.
cbn. rewrite H2. reflexivity.
Qed.
Ltac Σ_eq_dec s1 s2 :=
eq_dec Σ_eqb_spec Σ_eqb s1 s2.
Lemma LE_HaltsAtES_2 {tm tm0 n st s t tr}:
LE tm tm0 ->
HaltsAt tm n st ->
Steps Σ tm n st (s,t) ->
tm0 s (t 0%Z) = Some tr ->
LE (TM_upd tm s (t 0%Z) (Some tr)) tm0.
Proof.
unfold LE.
intros.
unfold TM_upd.
St_eq_dec s0 s.
- Σ_eq_dec i (t Z0); cbn.
+ left; cg.
+ apply H.
- apply H.
Qed.
Lemma HaltTimeUpperBound_LE_Halt st tm n s t:
HaltsAt tm n st ->
Steps Σ tm n st (s,t) ->
n<=BB ->
(forall tr, HaltTimeUpperBound st (LE (TM_upd tm s (t Z0) (Some tr)))) ->
HaltTimeUpperBound st (LE tm).
Proof.
intros.
unfold HaltTimeUpperBound.
intros.
destruct (tm0 s (t Z0)) as [tr|] eqn:E.
- specialize (H2 tr).
eapply H2.
2: apply H4.
eapply LE_HaltsAtES_2; eassumption.
- pose proof (LE_HaltsAtES_1 H3 H H0 E).
rewrite (HaltsAt_unique H4 H5).
assumption.
Qed.
Section swap.
Hypothesis s1 s2:St.
Hypothesis Hneq12: s1<>s2.
Hypothesis Hneq01: s1 <> St0.
Hypothesis Hneq02: s2 <> St0.
Definition St_swap s :=
if St_eqb s1 s then s2 else
if St_eqb s2 s then s1 else s.
Definition Trans_swap(tr:Trans Σ) :=
let (s',d,o):=tr in
{| nxt:=St_swap s'; dir:=d; out:=o |}.
Definition option_Trans_swap(x:option (Trans Σ)) :=
match x with
| Some x0 => Some (Trans_swap x0)
| None => None
end.
Definition TM_swap(tm:TM Σ) :=
fun s i => option_Trans_swap (tm (St_swap s) i).
Definition ExecState_swap(st:ExecState Σ) :=
let (s,t):=st in
(St_swap s,t).
Lemma St_swap_swap:
forall s,
St_swap (St_swap s) = s.
Proof.
intros.
unfold St_swap.
St_eq_dec s1 s;St_eq_dec s2 s;St_eq_dec s1 s2; auto; try cg.
- St_eq_dec s2 s2; cg.
- St_eq_dec s1 s1; cg.
- St_eq_dec s1 s; try cg.
St_eq_dec s2 s; try cg.
Qed.
Lemma Trans_swap_swap:
forall t,
Trans_swap (Trans_swap t) = t.
Proof.
intros.
destruct t.
unfold Trans_swap.
f_equal.
apply St_swap_swap.
Qed.
Lemma option_Trans_swap_swap:
forall s,
option_Trans_swap (option_Trans_swap s) = s.
Proof.
intros.
destruct s; auto 1.
unfold option_Trans_swap. f_equal.
apply Trans_swap_swap.
Qed.
Lemma TM_swap_swap:
forall tm,
TM_swap (TM_swap tm) = tm.
Proof.
intros.
unfold TM_swap.
fext. fext.
rewrite option_Trans_swap_swap,St_swap_swap.
reflexivity.
Qed.
Lemma ExecState_swap_swap:
forall st,
ExecState_swap (ExecState_swap st) = st.
Proof.
intros.
destruct st as [s t].
unfold ExecState_swap.
f_equal.
apply St_swap_swap.
Qed.
Lemma step_swap {tm st st0}:
step Σ (TM_swap tm) st = Some st0 <->
step Σ tm (ExecState_swap st) = Some (ExecState_swap st0).
Proof.
destruct st,st0. cbn.
unfold TM_swap.
destruct (tm (St_swap s) (σ 0%Z)) eqn:E; cbn.
2: split; intro; cg.
destruct t. cbn.
split; intro; f_equal.
- invst H.
f_equal.
rewrite St_swap_swap; reflexivity.
- invst H.
f_equal.
apply St_swap_swap.
Qed.
Lemma step_halt_swap tm st:
step Σ (TM_swap tm) st = None <->
step Σ tm (ExecState_swap st) = None.
Proof.
destruct st. cbn.
unfold TM_swap.
destruct (tm (St_swap s) (σ 0%Z)) eqn:E; cbn.
2: split; intro; cg.
destruct t. cbn.
split; intro; cg.
Qed.
Lemma Steps_swap tm n st st0:
Steps Σ (TM_swap tm) n st st0 <->
Steps Σ tm n (ExecState_swap st) (ExecState_swap st0).
Proof.
gd st0.
induction n; intros.
- split; intros; invst H.
+ ctor.
+ ff_inj ExecState_swap_swap H1.
ctor.
- split; intros.
+ invst H.
rewrite IHn in H1.
ector; eauto.
apply step_swap,H3.
+ invst H.
pose proof (IHn (ExecState_swap st2)) as IHn'.
rewrite ExecState_swap_swap in IHn'.
rewrite <-IHn' in H1.
ector; eauto.
apply step_swap.
rewrite ExecState_swap_swap.
apply H3.
Qed.
Lemma LE_swap_0 tm tm':
LE tm tm' -> LE (TM_swap tm) (TM_swap tm').
Proof.
unfold LE.
intros.
unfold TM_swap.
specialize (H (St_swap s) i).
destruct H as [H|H]; rewrite H; tauto.
Qed.
Lemma LE_swap tm tm':
LE tm tm' <-> LE (TM_swap tm) (TM_swap tm').
Proof.
split.
- apply LE_swap_0.
- pose proof (LE_swap_0 (TM_swap tm) (TM_swap tm')) as H.
repeat rewrite TM_swap_swap in H.
apply H.
Qed.
Lemma InitES_swap:
ExecState_swap (InitES Σ Σ0) = InitES Σ Σ0.
Proof.
unfold InitES. cbn.
f_equal.
unfold St_swap.
St_eq_dec s1 St0; try cg.
St_eq_dec s2 St0; try cg.
Qed.
Lemma HaltsAt_swap_0 tm n st:
HaltsAt tm n st ->
HaltsAt (TM_swap tm) n (ExecState_swap st).
Proof.
unfold HaltsAt.
intros.
destruct H as [st' [H H0]].
eexists.
split.
- rewrite Steps_swap.
rewrite <-ExecState_swap_swap in H.
rewrite ExecState_swap_swap.
apply H.
- rewrite step_halt_swap,ExecState_swap_swap.
apply H0.
Qed.
Lemma HaltsAt_swap tm n st:
HaltsAt tm n st <->
HaltsAt (TM_swap tm) n (ExecState_swap st).
Proof.
split.
- apply HaltsAt_swap_0.
- pose proof (HaltsAt_swap_0 (TM_swap tm) n (ExecState_swap st)) as H.
rewrite TM_swap_swap,ExecState_swap_swap in H.
apply H.
Qed.
Lemma HaltTimeUpperBound_LE_swap tm st:
HaltTimeUpperBound st (LE tm) -> HaltTimeUpperBound (ExecState_swap st) (LE (TM_swap tm)).
Proof.
unfold HaltTimeUpperBound.
intros.
rewrite LE_swap,TM_swap_swap in H0.
eapply H.
1: apply H0.
rewrite <-ExecState_swap_swap.
rewrite <-HaltsAt_swap.
apply H1.
Qed.
Lemma HaltTimeUpperBound_LE_swap_InitES tm:
HaltTimeUpperBound (InitES Σ Σ0) (LE tm) -> HaltTimeUpperBound (InitES Σ Σ0) (LE (TM_swap tm)).
Proof.
intro.
rewrite <-InitES_swap.
apply HaltTimeUpperBound_LE_swap,H.
Qed.
End swap.
Section rev.
Definition Trans_rev(tr:Trans Σ) :=
let (s',d,o):=tr in
{| nxt:=s'; dir:=Dir_rev d; out:=o |}.
Definition option_Trans_rev(o:option (Trans Σ)) :=
match o with
| None => None
| Some tr => Some (Trans_rev tr)
end.
Definition TM_rev(tm:TM Σ) :=
fun s i => option_Trans_rev (tm s i).
Definition Tape_rev(t:Z->Σ) :=
fun x:Z => t (-x)%Z.
Definition ExecState_rev(st:ExecState Σ) :=
let (s,t):=st in
(s,Tape_rev t).
Lemma Trans_rev_rev:
forall t,
Trans_rev (Trans_rev t) = t.
Proof.
intros.
destruct t.
unfold Trans_rev.
f_equal.
destruct dir; auto.
Qed.
Lemma option_Trans_rev_rev:
forall t,
option_Trans_rev (option_Trans_rev t) = t.
Proof.
intros.
destruct t.
2: reflexivity.