-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompiler2.v
1286 lines (1194 loc) · 51.6 KB
/
Compiler2.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 Com.
Require Import Big_Step.
Require Import Star.
Require Import Compiler.
Require Import List String.
Local Open Scope Z_scope.
Local Open Scope list_scope.
Import ListNotations.
Fixpoint exec_n (l: list instr) (c1: config) (n: nat) (c2: config): Prop :=
match n with
| O => c1 = c2
| S m => exists c', (exec1 l c1 c') /\ (exec_n l c' m c2)
end.
Definition isuccs (i: instr) (n: Z): list Z :=
match i with
| JMP j => [n + 1 + j]
| JMPLESS j => [n + 1 + j; n + 1]
| JMPGE j => [n + 1 + j; n + 1]
| _ => [n + 1]
end.
Definition IsSucc (P : list instr) (n : Z) (s : Z) : Prop :=
exists i, 0 <= i /\ i < size P /\ List.In s (isuccs (znth i P ADD) (n+i)).
Definition IsExit (P : list instr) (s : Z) : Prop :=
IsSucc P 0 s /\ ~(0 <= s /\ s < size P).
Section BP_term_exec_n.
Lemma exec_n_exec: forall n P c1 c2,
exec_n P c1 n c2 -> exec P c1 c2.
Proof.
induction n; pose @star_step; ycrush.
Qed.
Lemma exec_0: forall P c, exec_n P c 0 c.
Proof. scrush. Qed.
Lemma exec_Suc: forall n P c1 c2 c3, exec1 P c1 c2 -> exec_n P c2 n c3 -> exec_n P c1 (S n) c3.
Proof. scrush. Qed.
Lemma exec_exec_n: forall P c1 c2,
exec P c1 c2 ->exists n, exec_n P c1 n c2.
Proof.
intros P c1 c2 H; induction H.
- exists O; scrush.
- pose exec_Suc; scrush.
Qed.
Lemma exec_eq_exec_n: forall P c1 c2,
exec P c1 c2 <-> exists n, exec_n P c1 n c2.
Proof.
pose exec_exec_n; pose exec_n_exec; scrush.
Qed.
Lemma exec_n_Nil: forall k c1 c2, exec_n [] c1 k c2 <-> (c1 = c2 /\ k = O).
Proof.
induction k; sauto.
Qed.
Lemma exec1_exec_n: forall P c1 c2, exec1 P c1 c2 <-> exec_n P c1 1 c2.
Proof. scrush. Qed.
Lemma exec1_exec: forall P c1 c2, exec1 P c1 c2 -> exec P c1 c2.
Proof.
intros.
apply exec_n_exec with (n := 1%nat).
apply exec1_exec_n.
auto.
Qed.
Lemma exec_n_step: forall k n n' P s s' stk stk',
n <> n' ->
exec_n P (n, stk, s) k (n', stk', s') <->
exists c, exec1 P (n, stk, s) c /\ exec_n P c (k - 1) (n', stk', s') /\ Nat.lt 0%nat k.
Proof.
destruct k.
- sauto.
- unfold exec_n; fold exec_n; split; intro; simp_hyps.
+ assert (HH: (S k - 1)%nat = k) by omega.
rewrite HH; clear HH.
exists c'.
eauto with arith.
+ assert (HH: (S k - 1)%nat = k) by omega.
rewrite HH in *; clear HH.
exists c; auto.
Qed.
Lemma exec1_end: forall P c1 c2, size P <= (fst (fst c1)) -> ~(exec1 P c1 c2).
Proof.
Reconstr.hobvious Reconstr.AllHyps
(@Coq.ZArith.BinInt.Z.lt_nge)
(@Coq.ZArith.BinInt.Z.lt).
Qed.
Lemma exec_n_end: forall k n n' P s s' stk stk',
size P <= n -> exec_n P (n, s, stk) k (n', s', stk') -> (n' = n /\ stk' = stk /\ s' = s /\ k = 0%nat).
Proof.
destruct k.
- sauto.
- unfold exec_n; fold exec_n; intros; simp_hyps.
exfalso; apply exec1_end with (P := P) (c1 := (n, s, stk)) (c2 := c'); scrush.
Qed.
End BP_term_exec_n.
Section BP_term_succs.
Lemma succs_simps: forall n v x i,
(forall s, IsSucc [ADD] n s <-> s = n + 1) /\
(forall s, IsSucc [LOADI v] n s <-> s = n + 1) /\
(forall s, IsSucc [LOAD x] n s <-> s = n + 1) /\
(forall s, IsSucc [STORE x] n s <-> s = n + 1) /\
(forall s, IsSucc [JMP i] n s <-> s = n + 1 + i) /\
(forall s, IsSucc [JMPGE i] n s <-> s = n + 1 + i \/ s = n + 1) /\
(forall s, IsSucc [JMPLESS i] n s <-> s = n + 1 + i \/ s = n + 1).
Proof.
unfold IsSucc.
intros; repeat split; intro; simp_hyps; cbn in *;
solve [ assert (i0 = 0) by omega; sauto | exists 0; sauto ].
Qed.
Lemma succs_empty: forall n s, ~(IsSucc [] n s).
Proof.
unfold IsSucc.
Reconstr.hobvious Reconstr.Empty
(@Coq.ZArith.BinInt.Z.abs_nonneg, @Coq.ZArith.BinInt.Z.le_refl, @Coq.ZArith.BinInt.Z.abs_eq,
@Coq.ZArith.BinInt.Z.ltb_nlt, @Coq.ZArith.BinInt.Z.nle_gt, @Coq.ZArith.BinInt.Z.le_lt_trans)
(@Compiler.size).
Qed.
Lemma succs_Cons: forall x xs n s,
IsSucc (x :: xs) n s <-> List.In s (isuccs x n) \/ IsSucc xs (n + 1) s.
Proof.
split; intro H; unfold IsSucc in H; simp_hyps.
- assert (H2: i = 0 \/ i > 0) by
Reconstr.hcrush (@H)
(@Coq.ZArith.BinInt.Z.lt_eq_cases, @Coq.ZArith.BinInt.Z.lt_nge, @Coq.ZArith.Zorder.Znot_le_gt)
Reconstr.Empty.
assert (H3: i = 0 \/ exists j, j >= 0 /\ i = j + 1) by
(sauto; [
Reconstr.hcrush Reconstr.AllHyps
(@Coq.ZArith.BinInt.Z.abs_nonneg, @Coq.ZArith.Znat.Z2Nat.id, @Coq.ZArith.Znat.Z2Nat.inj_0)
(@Coq.ZArith.BinIntDef.Z.abs) |
Reconstr.hcrush Reconstr.AllHyps
(@Coq.ZArith.BinInt.Z.le_exists_sub, @Coq.ZArith.BinInt.Z.le_gt_cases, @Coq.ZArith.BinInt.Z.add_0_l, @Coq.ZArith.Zcompare.Zcompare_Gt_not_Lt, @Coq.ZArith.BinInt.Z.ge_le_iff)
(@Coq.ZArith.BinInt.Z.lt) ]).
clear H2.
destruct H3; simp_hyps.
+ sauto.
+ subst; right; unfold IsSucc.
exists j.
repeat split.
* auto with zarith.
* Reconstr.hsimple Reconstr.AllHyps
(@Coq.ZArith.BinInt.Z.add_lt_mono_r, @Compiler.lem_size_succ)
Reconstr.Empty.
* Reconstr.hcrush Reconstr.AllHyps
(@Coq.ZArith.BinInt.Z.add_comm, @Coq.ZArith.BinInt.Z.ge_le_iff,
@Coq.ZArith.BinInt.Zplus_assoc_reverse, @Compiler.lem_n_succ_znth)
Reconstr.Empty.
- destruct H.
+ unfold IsSucc; exists 0; scrush.
+ unfold IsSucc; simp_hyps. exists (i + 1).
repeat split.
* omega.
* Reconstr.hcrush Reconstr.AllHyps
(@Coq.ZArith.BinInt.Z.add_lt_mono_r, @Compiler.lem_size_succ)
Reconstr.Empty.
* sauto; [
Reconstr.hcrush Reconstr.AllHyps
(@Coq.ZArith.Znat.Z2Nat.inj_succ)
(@Coq.ZArith.BinIntDef.Z.succ) |
Reconstr.hcrush Reconstr.AllHyps
(@Coq.ZArith.BinInt.Zplus_assoc_reverse, @Coq.ZArith.BinInt.Z.add_comm,
@Coq.ZArith.Znat.Z2Nat.inj_succ, @Coq.ZArith.BinInt.Z.add_succ_l, @Coq.ZArith.BinInt.Z.add_shuffle3)
(@Compiler.znth, @Coq.ZArith.BinIntDef.Z.succ) ].
Qed.
Lemma succs_iexec1: forall P n s stk c,
0 <= n -> n < size P -> c = iexec (znth n P ADD) (n, s, stk) -> IsSucc P 0 (fst (fst c)).
Proof.
unfold IsSucc; unfold iexec.
sauto; exists n; scrush.
Qed.
Lemma In_help: forall p n x, In (p - n) [x + 1] <-> In p [n + x + 1].
Proof. split; intros; unfold In in *.
destruct H. left.
Reconstr.heasy (@AllHyps)
(@Coq.ZArith.BinInt.Z.add_0_r, @Coq.ZArith.BinInt.Z.add_comm,
@Coq.ZArith.BinInt.Z.sub_add_simpl_r_l, @Coq.ZArith.BinInt.Z.add_succ_l)
(@Coq.ZArith.BinIntDef.Z.succ).
scrush.
destruct H. left.
Reconstr.heasy (@AllHyps)
(@Coq.ZArith.BinInt.Z.add_succ_l, @Coq.ZArith.BinInt.Z.add_0_r,
@Coq.ZArith.BinInt.Z.add_comm, @Coq.ZArith.BinInt.Z.add_assoc,
@Coq.ZArith.BinInt.Z.add_opp_r, @Coq.ZArith.BinInt.Z.sub_add_simpl_r_l, @Coq.ZArith.BinInt.Z.add_0_l)
(@Coq.ZArith.BinIntDef.Z.sub, @Coq.ZArith.BinIntDef.Z.succ).
scrush.
Qed.
Lemma succs_shift: forall n p P, IsSucc P 0 (p - n) <-> IsSucc P n p.
Proof. split; intros; unfold IsSucc, isuccs.
destruct H. exists x.
destruct H, H0.
split. easy.
split. easy. cbn in *.
case_eq (znth x P ADD); intros; unfold isuccs in *; rewrite H2 in *; try now apply In_help.
specialize (In_help p n (x + z)); intros.
assert (n + (x + z) + 1 = n + x + 1 + z) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.BinInt.Z.add_succ_l, @Coq.ZArith.BinInt.Z.add_assoc)
(@Coq.ZArith.BinIntDef.Z.succ); subst;
assert (x + z + 1 = x + 1 + z) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.BinInt.Z.add_succ_l)
(@Coq.ZArith.BinIntDef.Z.succ); subst.
(scrush;
unfold In in *;
destruct H1; left; omega;
destruct H1; right; left; omega;
scrush).
unfold In in *;
Reconstr.htrivial (@H1)
(@Coq.ZArith.BinInt.Zplus_assoc_reverse, @Coq.ZArith.BinInt.Zplus_minus)
(@Coq.ZArith.BinIntDef.Z.sub).
unfold In in *;
Reconstr.htrivial (@H1)
(@Coq.ZArith.BinInt.Zplus_assoc_reverse, @Coq.ZArith.BinInt.Zplus_minus)
(@Coq.ZArith.BinIntDef.Z.sub).
unfold IsSucc, isuccs in H. destruct H.
exists x;
split; try easy; split; try easy.
destruct H, H0; cbn in *;
case_eq (znth x P ADD); intros; rewrite H2 in *; try now apply In_help.
Reconstr.hcrush (@H1)
(@BP_term_succs.In_help, @Coq.ZArith.BinInt.Z.add_succ_r,
@Coq.ZArith.BinInt.Z.add_succ_l, @Coq.ZArith.BinInt.Z.add_comm,
@Coq.ZArith.BinInt.Zplus_assoc_reverse)
(@Coq.ZArith.BinIntDef.Z.sub, @Coq.ZArith.BinIntDef.Z.succ).
unfold In in H1; destruct H1;
Reconstr.hblast (@H1)
(@Coq.ZArith.BinInt.Zplus_assoc_reverse, @Coq.ZArith.BinInt.Z.add_simpl_l)
(@Coq.ZArith.BinIntDef.Z.sub).
Reconstr.hblast (@H1)
(@Coq.ZArith.BinInt.Zplus_assoc_reverse, @Coq.ZArith.BinInt.Z.add_simpl_l)
(@Coq.ZArith.BinIntDef.Z.sub).
Qed.
Lemma helper: forall y n, n + 1 + Z.of_nat y = n + Z.of_nat (1 + y).
Proof. intro n; induction n; sauto;
Reconstr.htrivial (@IHn)
(@Coq.ZArith.BinInt.Z.add_succ_comm, @Coq.ZArith.BinInt.Z.opp_0,
@Coq.ZArith.BinInt.Z.add_0_l, @Coq.ZArith.BinInt.Z.opp_add_distr,
@Coq.ZArith.BinInt.Pos2Z.inj_succ)
(@Coq.ZArith.BinIntDef.Z.opp, @Coq.ZArith.BinIntDef.Z.succ); scrush.
Qed.
Lemma succs_append_l: forall xs ys n s, IsSucc (xs ++ ys) n s -> IsSucc xs n s \/ IsSucc ys (n + size xs) s.
Proof. intro xs; induction xs; intros.
- cbn in *; assert (n + 0 = n) by scrush; scrush.
- intros. cbn in H. apply succs_Cons in H.
destruct H.
Reconstr.htrivial (@H)
(@BP_term_succs.succs_Cons)
Reconstr.Empty; scrush.
specialize (IHxs ys (n + 1) s H).
Reconstr.hblast (@IHxs)
(@Compiler.lem_size_succ, @Coq.ZArith.BinInt.Z.add_succ_l,
@Coq.ZArith.BinInt.Z.add_comm, @BP_term_succs.succs_Cons)
(@Coq.ZArith.BinIntDef.Z.succ).
Qed.
Lemma succs_append_r: forall xs ys n s, IsSucc xs n s \/ IsSucc ys (n + size xs) s -> IsSucc (xs ++ ys) n s.
Proof. intro xs; induction xs; intros.
- Reconstr.hobvious (@H)
(@BP_term_succs.succs_empty, @Coq.ZArith.BinInt.Z.add_0_l,
@Coq.ZArith.BinInt.Z.add_cancel_r, @Coq.Lists.List.app_nil_l,
@Compiler.lem_size_app, @Coq.ZArith.BinInt.Zplus_0_r_reverse)
Reconstr.Empty.
- destruct H. apply succs_Cons in H;
cbn; apply succs_Cons; destruct H; scrush.
cbn; apply succs_Cons; right; apply IHxs; unfold size;
Reconstr.hblast (@H)
(@Coq.ZArith.BinInt.Z.add_comm, @Coq.ZArith.BinInt.Z.add_succ_l, @Compiler.lem_size_succ)
(@Coq.ZArith.BinIntDef.Z.succ, @Compiler.size).
Qed.
Lemma succs_append: forall xs ys n s, IsSucc (xs ++ ys) n s <-> IsSucc xs n s \/ IsSucc ys (n + size xs) s.
Proof. pose succs_append_l; pose succs_append_r; scrush. Qed.
Lemma succs_set_shift:
forall xs i s, IsSucc xs 0 s <-> IsSucc xs i (s + i).
Proof.
intros xs i s; split; intro H.
- apply succs_shift with (n := i).
Reconstr.hobvious (@H)
(@Coq.ZArith.BinInt.Z.add_simpl_r)
(@Coq.ZArith.BinIntDef.Z.sub).
- assert (HH: s = (s + i) - i) by omega.
Reconstr.hobvious (@H, @HH)
(@BP_term_succs.succs_shift)
Reconstr.Empty.
Qed.
Lemma exits_append : forall xs ys s,
IsExit (xs ++ ys) s <-> (IsExit xs s \/ IsExit ys (s - size xs)) /\ ~(0 <= s /\ s < size xs + size ys).
Proof.
unfold IsExit.
intros xs ys s.
rewrite lem_size_app.
split; intro H.
- assert (Hsize0: forall l, 0 <= size l) by
Reconstr.hobvious Reconstr.Empty
(@Coq.ZArith.BinInt.Z.nle_gt, @Coq.ZArith.Zorder.Zle_0_nat)
(@Coq.ZArith.BinInt.Z.lt, @Coq.ZArith.BinInt.Z.ge, @Compiler.size).
assert (Hn1: forall n m k, 0 <= m -> n < k -> n < k + m) by (intros; omega).
assert (Hn2: forall n m k, n < k + m -> n - k < m) by (intros; omega).
assert (Hn3: forall n m, 0 <= m -> 0 <= n - m -> 0 <= n) by (intros; omega).
assert (~(0 <= s < size xs)) by scrush.
assert (~(0 <= s - size xs < size ys)) by
Reconstr.hobvious (@Hsize0, @Hn3, @H)
(@Coq.ZArith.BinInt.Z.lt_sub_lt_add_l)
(@Coq.ZArith.BinIntDef.Z.sub).
Reconstr.hobvious Reconstr.AllHyps
(@Coq.ZArith.BinInt.Z.add_0_l, @BP_term_succs.succs_append_l, @BP_term_succs.succs_shift)
Reconstr.Empty.
- split.
+ destruct H as [H H0].
destruct H.
* Reconstr.heasy (@H)
(@BP_term_succs.succs_append_r)
Reconstr.Empty.
* Reconstr.heasy (@H)
(@Coq.ZArith.BinInt.Z.add_0_l, @BP_term_succs.succs_shift,
@BP_term_succs.succs_append_r)
Reconstr.Empty.
+ scrush.
Qed.
Lemma exits_single_l: forall x s, IsExit [x] s -> List.In s (isuccs x 0) /\ s <> 0.
Proof. unfold IsExit, IsSucc.
sauto;
solve [
pose Coq.ZArith.BinInt.Z.lt_eq_cases; pose Coq.ZArith.Znat.Z2Nat.inj_0;
rewrite Coq.ZArith.Znat.Z2Nat.inj with (n := 0) (m := i); scrush |
Reconstr.heasy Reconstr.AllHyps
(@Coq.ZArith.Znat.Z2Nat.inj, @Coq.ZArith.Znat.Z2Nat.inj_0,
@Coq.ZArith.BinInt.Z.lt_eq_cases)
(@Coq.ZArith.BinInt.Z.lt) ].
Qed.
Lemma exits_single_r: forall x s, List.In s (isuccs x 0) /\ s <> 0 -> IsExit [x] s.
Proof. unfold IsExit, IsSucc; sauto.
Reconstr.hobvious (@H0)
(@Coq.ZArith.BinInt.Z.lt_0_1, @Coq.ZArith.BinInt.Z.le_refl, @Coq.ZArith.Znat.Z2Nat.inj_0)
Reconstr.Empty.
Reconstr.hcrush (@H2, @H1, @H4)
(@Coq.ZArith.Zcompare.Zcompare_Gt_not_Lt, @Coq.ZArith.BinInt.Z.compare_eq_iff,
@Coq.ZArith.BinInt.Z.compare_lt_iff, @Coq.ZArith.BinInt.Z.compare_eq,
@Coq.ZArith.BinInt.Z.lt_eq_cases, @Coq.ZArith.BinInt.Z.add_0_r,
@Coq.ZArith.BinInt.Z.add_comm, @Coq.ZArith.Zcompare.Zcompare_Gt_Lt_antisym)
Reconstr.Empty.
Qed.
Lemma exits_single: forall x s, IsExit [x] s <-> List.In s (isuccs x 0) /\ s <> 0.
Proof. pose exits_single_l; pose exits_single_r; scrush. Qed.
Lemma exits_Cons: forall x xs s,
IsExit (x :: xs) s <-> ((List.In s (isuccs x 0) /\ s <> 0) \/ IsExit xs (s - 1)) /\ ~(0 <= s < 1 + size xs).
Proof.
intros x xs s.
rewrite <- exits_single.
split; intro; apply exits_append with (xs := [x]); scrush.
Qed.
Lemma exits_empty: forall s, IsExit [] s <-> List.In s [].
Proof.
Reconstr.hobvious Reconstr.Empty
(@BP_term_succs.succs_empty, @Coq.Lists.List.in_nil)
(@IsExit).
Qed.
Lemma exits_simps: forall v x i s,
(IsExit [ADD] s <-> s = 1) /\
(IsExit [LOADI v] s <-> s = 1) /\
(IsExit [LOAD x] s <-> s = 1) /\
(IsExit [STORE x] s <-> s = 1) /\
(i <> -1 -> IsExit [JMP i] s <-> s = 1 + i) /\
(i <> -1 -> IsExit [JMPGE i] s <-> s = 1 + i \/ s = 1) /\
(i <> -1 -> IsExit [JMPLESS i] s <-> s = 1 + i \/ s = 1).
Proof.
unfold IsExit; unfold IsSucc; unfold isuccs.
assert (forall i, (i ?= 1) = Lt -> 0 <= i -> i + 1 = 1) by
(clear; intros; assert (i < 1) by scrush; omega).
assert (forall i, (i ?= 1) = Lt -> 0 <= i -> i = 0) by
(clear; intros; assert (i < 1) by scrush; omega).
sauto; try exists 0; scrush. (* 11s *)
Qed.
End BP_term_succs.
Lemma lem_acomp_succs_hlp1: forall l, znth (size l) (l ++ [ADD]) ADD = ADD.
Proof.
induction l; sauto.
- Reconstr.htrivial (@Heqn)
(@Coq.Arith.PeanoNat.Nat.sub_1_r, @Coq.Arith.PeanoNat.Nat.neq_succ_0, @Coq.Arith.PeanoNat.Nat.le_0_r, @Coq.Arith.PeanoNat.Nat.sub_0_le, @Coq.PArith.Pnat.Pos2Nat.inj_1, @Coq.PArith.Pnat.SuccNat2Pos.id_succ, @Coq.PArith.Pnat.SuccNat2Pos.pred_id, @Coq.Init.Peano.le_S)
Reconstr.Empty.
- unfold size in *; unfold znth in *.
assert (Z.to_nat (Z.of_nat (Datatypes.length l)) = n) by
Reconstr.hobvious (@Heqn)
(@Coq.Arith.PeanoNat.Nat.pred_succ, @Coq.PArith.Pnat.SuccNat2Pos.id_succ, @Coq.ZArith.Znat.Nat2Z.id)
Reconstr.Empty.
scrush.
Qed.
Ltac acomp_succs_blast_tac :=
match goal with
[ H: 0 <= ?x, H2: (?x ?= 1) = Lt |- _ ] =>
Reconstr.hblast (@H2, @H)
(@Coq.ZArith.BinInt.Z.succ_le_mono, @Coq.ZArith.BinInt.Z.add_comm,
@Coq.ZArith.BinInt.Z.one_succ, @Coq.ZArith.BinInt.Z.le_antisymm,
@Coq.ZArith.BinInt.Z.succ_m1, @Coq.ZArith.BinInt.Z.add_succ_r,
@Coq.ZArith.BinInt.Z.add_1_l, @Coq.ZArith.BinInt.Z.add_0_l,
@Coq.ZArith.Zcompare.Zcompare_Gt_not_Lt, @Coq.ZArith.BinInt.Z.le_refl)
(@Coq.ZArith.BinIntDef.Z.succ, @Coq.ZArith.BinInt.Z.le)
end.
Ltac ex_omega_tac := unfold IsSucc; unfold isuccs; exists 0; sauto; omega.
Lemma acomp_succs:
forall a n s, IsSucc (acomp a) n s <-> n + 1 <= s <= n + size (acomp a).
Proof.
induction a; sauto; try omega; try acomp_succs_blast_tac. (* 3s *)
- ex_omega_tac.
- ex_omega_tac.
- assert (IsSucc (acomp a1 ++ acomp a2 ++ [ADD]) n s) by (unfold IsSucc; scrush).
assert (IsSucc (acomp a1) n s \/ IsSucc (acomp a2) (n + size (acomp a1)) s \/
IsSucc [ADD] (n + size (acomp a1) + size (acomp a2)) s) by
(assert (IsSucc (acomp a1) n s \/ IsSucc (acomp a2 ++ [ADD]) (n + size (acomp a1)) s) by
(apply succs_append; scrush);
pose succs_append; scrush).
assert (forall l, 0 <= size l) by
Reconstr.hobvious Reconstr.Empty
(@Coq.ZArith.BinInt.Z.nle_gt, @Coq.ZArith.Zorder.Zle_0_nat)
(@Coq.ZArith.BinInt.Z.lt, @Coq.ZArith.BinInt.Z.ge, @Compiler.size).
intuition.
+ assert (n + size (acomp a1) + 1 <= s) by scrush.
assert (0 <= size (acomp a1)) by scrush.
omega.
+ unfold IsSucc in H8.
simp_hyps.
assert (HH: i = 0) by
(unfold size in *; simpl in *; omega).
rewrite HH in *; clear HH.
sauto.
assert (0 <= size (acomp a1)) by scrush.
assert (0 <= size (acomp a2)) by scrush.
omega.
- assert (IsSucc (acomp a1 ++ acomp a2 ++ [ADD]) n s) by (unfold IsSucc; scrush).
assert (IsSucc (acomp a1) n s \/ IsSucc (acomp a2) (n + size (acomp a1)) s \/
IsSucc [ADD] (n + size (acomp a1) + size (acomp a2)) s) by
(assert (IsSucc (acomp a1) n s \/ IsSucc (acomp a2 ++ [ADD]) (n + size (acomp a1)) s) by
(apply succs_append; scrush);
pose succs_append; scrush).
assert (forall l, 0 <= size l) by
Reconstr.hobvious Reconstr.Empty
(@Coq.ZArith.BinInt.Z.nle_gt, @Coq.ZArith.Zorder.Zle_0_nat)
(@Coq.ZArith.BinInt.Z.lt, @Coq.ZArith.BinInt.Z.ge, @Compiler.size).
repeat rewrite lem_size_app.
assert (0 <= size (acomp a2)) by scrush.
assert (HH: size [ADD] = 1) by scrush.
rewrite HH; clear HH.
intuition.
+ assert (s <= n + size (acomp a1)) by scrush.
omega.
+ assert (s <= n + size (acomp a1) + size (acomp a2)) by scrush.
omega.
+ unfold IsSucc in H8.
simp_hyps.
assert (HH: i = 0) by
(unfold size in *; simpl in *; omega).
rewrite HH in *; clear HH.
sauto.
- repeat rewrite lem_size_app in *.
assert (forall l, 0 <= size l) by
Reconstr.hobvious Reconstr.Empty
(@Coq.ZArith.BinInt.Z.nle_gt, @Coq.ZArith.Zorder.Zle_0_nat)
(@Coq.ZArith.BinInt.Z.lt, @Coq.ZArith.BinInt.Z.ge, @Compiler.size).
assert (0 <= size (acomp a2)) by scrush.
assert (HH: size [ADD] = 1) by scrush.
rewrite HH in *; clear HH.
assert (HH: s <= n + size (acomp a1) + size (acomp a2) \/
s = n + size (acomp a1) + size (acomp a2) + 1) by omega.
destruct HH.
+ assert (HH: n + size (acomp a1) + 1 <= s \/ s <= n + size (acomp a1)) by omega.
destruct HH.
* assert (IsSucc (acomp a2) (n + size (acomp a1)) s) by scrush.
pose succs_append; scrush.
* assert (IsSucc (acomp a1) n s) by scrush.
pose succs_append; scrush.
+ subst.
unfold IsSucc; unfold isuccs.
repeat rewrite lem_size_app.
exists (size (acomp a1) + size (acomp a2)).
assert (HH: size [ADD] = 1) by scrush.
rewrite HH; clear HH.
repeat split; simpl; try omega.
assert (HH: znth (size (acomp a1) + size (acomp a2)) (acomp a1 ++ acomp a2 ++ [ADD]) ADD = ADD) by
(rewrite <- lem_size_app; rewrite List.app_assoc; rewrite lem_acomp_succs_hlp1; trivial).
rewrite HH; clear HH.
Reconstr.hobvious Reconstr.Empty
(@Coq.Lists.List.not_in_cons, @Coq.ZArith.BinInt.Zplus_assoc_reverse)
(@Coq.ZArith.BinIntDef.Z.succ).
Qed.
Lemma acomp_size : forall a, 1 <= size (acomp a).
Proof.
induction a; sauto.
repeat rewrite lem_size_app.
assert (size [ADD] = 1) by scrush.
omega.
Qed.
Lemma acomp_exits : forall a s, IsExit (acomp a) s <-> s = size (acomp a).
Proof.
unfold IsExit; intros a s; split; intro H.
- Reconstr.hcrush (@H)
(acomp_succs, @Coq.ZArith.BinInt.Z.lt_succ_r, @Coq.ZArith.BinInt.Z.nle_gt,
@Coq.ZArith.BinInt.Z.gt_lt_iff, @Coq.ZArith.BinInt.Z.lt_eq_cases,
@Coq.ZArith.BinInt.Z.add_comm, @Coq.ZArith.BinInt.Z.add_0_r,
@Coq.ZArith.BinInt.Z.le_exists_sub, @Coq.ZArith.Zorder.Zle_0_nat,
@Coq.ZArith.BinInt.Z.one_succ)
(@Coq.ZArith.BinInt.Z.gt, @Coq.ZArith.BinInt.Z.le, @Compiler.size,
@Coq.ZArith.BinIntDef.Z.succ).
- Reconstr.hobvious (@H)
(@Coq.ZArith.BinInt.Z.add_0_r, @Coq.ZArith.BinInt.Z.add_comm,
@Coq.ZArith.BinInt.Z.le_refl, @Coq.ZArith.BinInt.Z.nle_gt,
@acomp_size, @acomp_succs, @Coq.ZArith.BinInt.Z.one_succ)
(@Coq.ZArith.BinIntDef.Z.succ).
Qed.
Lemma bcomp_succs :
forall b f i n, 0 <= i -> forall s,
IsSucc (bcomp b f i) n s ->
n <= s <= n + size (bcomp b f i) \/ s = n + i + size (bcomp b f i).
Proof.
induction b; sauto.
- Reconstr.hblast (@H3, @H0)
(@Coq.ZArith.BinInt.Z.add_succ_l, @Coq.ZArith.BinInt.Z.le_antisymm, @Coq.ZArith.BinInt.Z.add_0_l, @Coq.ZArith.Zcompare.Zcompare_Gt_not_Lt, @Coq.ZArith.BinInt.Z.add_0_r)
(@Coq.ZArith.BinInt.Z.le, @Coq.ZArith.BinIntDef.Z.succ).
- Reconstr.hblast (@H0, @H3, @Heqn0)
(@Coq.ZArith.BinInt.Z.le_antisymm, @Coq.ZArith.BinInt.Z.add_0_l, @Coq.ZArith.Zcompare.Zcompare_Gt_not_Lt, @Coq.ZArith.Znat.Z2Nat.inj_0)
(@Coq.ZArith.BinInt.Z.le).
- assert (x = 0) by
Reconstr.hblast (@H0, @H3)
(@Coq.ZArith.BinInt.Z.compare_eq_iff, @Coq.ZArith.BinInt.Z.compare_lt_iff, @Coq.ZArith.BinInt.Z.compare_nge_iff, @Coq.ZArith.BinInt.Z.le_succ_l, @Coq.ZArith.BinInt.Z.one_succ)
(@Coq.ZArith.BinInt.Z.le, @Coq.ZArith.BinIntDef.Z.min).
scrush.
- Reconstr.hobvious (@H0, @H3)
(@Coq.ZArith.BinInt.Z.ge_le_iff)
(@Coq.ZArith.BinInt.Z.ge).
- Reconstr.hobvious (@H3, @H0)
(@Coq.ZArith.BinInt.Z.lt_nge)
(@Coq.ZArith.BinInt.Z.lt).
- assert (IsSucc (bcomp b1 false (size (bcomp b2 true i)) ++ bcomp b2 true i) n s) by
(unfold IsSucc; scrush).
assert (H3: IsSucc (bcomp b1 false (size (bcomp b2 true i))) n s \/
IsSucc (bcomp b2 true i) (n + size (bcomp b1 false (size (bcomp b2 true i)))) s) by
(pose succs_append_l; scrush).
repeat rewrite lem_size_app.
destruct H3.
+ clear -H3 IHb1.
assert (H: 0 <= size (bcomp b2 true i)) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
specialize (IHb1 false (size (bcomp b2 true i)) n H s H3).
assert (0 <= size (bcomp b1 false (size (bcomp b2 true i)))) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
omega.
+ clear -H H3 IHb2.
specialize (IHb2 true i (n + size (bcomp b1 false (size (bcomp b2 true i)))) H s H3).
assert (0 <= size (bcomp b1 false (size (bcomp b2 true i)))) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
omega.
- assert (IsSucc (bcomp b1 false (size (bcomp b2 false i) + i) ++ bcomp b2 false i) n s) by
(unfold IsSucc; scrush).
assert (H3: IsSucc (bcomp b1 false (size (bcomp b2 false i) + i)) n s \/
IsSucc (bcomp b2 false i) (n + size (bcomp b1 false (size (bcomp b2 false i) + i))) s) by
(pose succs_append_l; scrush).
repeat rewrite lem_size_app.
destruct H3.
+ clear -H H3 IHb1.
assert (H0: 0 <= size (bcomp b2 false i)) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
assert (H1: 0 <= size (bcomp b2 false i) + i) by omega.
specialize (IHb1 false (size (bcomp b2 false i) + i) n H1 s H3).
assert (0 <= size (bcomp b1 false (size (bcomp b2 false i) + i))) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
omega.
+ clear -H H3 IHb2.
specialize (IHb2 false i (n + size (bcomp b1 false (size (bcomp b2 false i) + i))) H s H3).
assert (0 <= size (bcomp b1 false (size (bcomp b2 false i) + i))) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
omega.
- assert (IsSucc (acomp a ++ acomp a0 ++ [JMPLESS i]) n s) by (unfold IsSucc; scrush).
assert (H3: IsSucc (acomp a) n s \/ IsSucc (acomp a0 ++ [JMPLESS i]) (n + size (acomp a)) s) by
(pose succs_append_l; scrush).
repeat rewrite lem_size_app.
destruct H3.
+ assert (n + 1 <= s <= n + size (acomp a)) by (apply acomp_succs; scrush).
assert (0 <= size (acomp a0)) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
assert (0 <= size [JMPLESS i]) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
omega.
+ assert (HH: IsSucc (acomp a0) (n + size (acomp a)) s \/
IsSucc [JMPLESS i] (n + size (acomp a) + size (acomp a0)) s) by
(pose succs_append_l; scrush).
destruct HH.
* assert (n + size (acomp a) + 1 <= s <= n + size (acomp a) + size (acomp a0)) by
(apply acomp_succs; scrush).
assert (0 <= size (acomp a)) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
assert (0 <= size [JMPLESS i]) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
omega.
* assert (H6: s = n + size (acomp a) + size (acomp a0) + 1 + i \/
s = n + size (acomp a) + size (acomp a0) + 1) by (apply succs_simps; scrush).
assert (HH: size [JMPLESS i] = 1) by scrush.
repeat rewrite HH; clear HH.
assert (0 <= size (acomp a)) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
assert (0 <= size (acomp a0)) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
omega.
- assert (IsSucc (acomp a ++ acomp a0 ++ [JMPGE i]) n s) by (unfold IsSucc; scrush).
assert (H3: IsSucc (acomp a) n s \/ IsSucc (acomp a0 ++ [JMPGE i]) (n + size (acomp a)) s) by
(pose succs_append_l; scrush).
repeat rewrite lem_size_app.
destruct H3.
+ assert (n + 1 <= s <= n + size (acomp a)) by (apply acomp_succs; scrush).
assert (0 <= size (acomp a0)) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
assert (0 <= size [JMPGE i]) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
omega.
+ assert (HH: IsSucc (acomp a0) (n + size (acomp a)) s \/
IsSucc [JMPGE i] (n + size (acomp a) + size (acomp a0)) s) by
(pose succs_append_l; scrush).
destruct HH.
* assert (n + size (acomp a) + 1 <= s <= n + size (acomp a) + size (acomp a0)) by
(apply acomp_succs; scrush).
assert (0 <= size (acomp a)) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
assert (0 <= size [JMPGE i]) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
omega.
* assert (H6: s = n + size (acomp a) + size (acomp a0) + 1 + i \/
s = n + size (acomp a) + size (acomp a0) + 1) by
(pose succs_simps; simp_hyps; yelles 1%nat).
assert (HH: size [JMPGE i] = 1) by scrush.
repeat rewrite HH; clear HH.
assert (0 <= size (acomp a)) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
assert (0 <= size (acomp a0)) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat)
(@Compiler.size).
omega.
Qed.
Lemma bcomp_exits:
forall b f i, 0 <= i -> forall s,
IsExit (bcomp b f i) s ->
s = size (bcomp b f i) \/ s = i + size (bcomp b f i).
Proof. unfold IsExit; intros; unfold size in *;
destruct H0; apply bcomp_succs in H0; destruct H0; cbn in *;
sauto; unfold size in *;
Reconstr.hobvious (@H3, @H2)
(@Coq.ZArith.BinInt.Z.le_antisymm, @Coq.ZArith.BinInt.Z.lt_ge_cases)
(@Compiler.size).
Qed.
Lemma size_cons: forall x m, size (x :: m) = 1 + size m.
Proof.
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.BinInt.Z.one_succ, @Coq.ZArith.BinInt.Z.add_comm, @Compiler.lem_size_succ)
Reconstr.Empty.
Qed.
Lemma ccomp_size: forall c, size (ccomp c) >= 0.
Proof.
Reconstr.hobvious Reconstr.Empty
(@Coq.ZArith.BinInt.Z.ge_le_iff, @Coq.ZArith.Zorder.Zle_0_nat)
(@Coq.ZArith.BinInt.Z.ge, @Compiler.size).
Qed.
Lemma list_size: forall l, size l >= 0.
Proof.
Reconstr.hobvious Reconstr.Empty
(@Coq.ZArith.Zorder.Zle_0_nat, @Coq.ZArith.BinInt.Z.ge_le_iff, @Coq.ZArith.Zeven.Zquot2_odd_eqn)
(@Coq.ZArith.BinInt.Z.ge, @Compiler.size).
Qed.
Lemma ccomp_succs:
forall c i n, 0 <= i -> forall s,
IsSucc (ccomp c) n s -> n <= s <= n + size (ccomp c).
Proof. intro c; induction c; intros.
- sauto;
Reconstr.hobvious (@H3, @H0)
(@Coq.ZArith.BinInt.Z.compare_ge_iff)
Reconstr.Empty.
- cbn in *. specialize (succs_append (acomp a) [STORE v] n s); intros.
apply H1 in H0. destruct H0.
apply acomp_succs in H0.
assert (size (acomp a ++ [STORE v]) = size (acomp a) + 1) by
Reconstr.hobvious Reconstr.Empty
(@Coq.ZArith.auxiliary.Zegal_left, @Coq.Lists.List.app_nil_l,
@Compiler.lem_size_succ, @Coq.ZArith.BinInt.Z.add_simpl_r,
@Compiler.lem_size_app, @Coq.ZArith.BinInt.Z.one_succ)
(@Coq.ZArith.BinIntDef.Z.sub, @Coq.ZArith.BinIntDef.Z.succ).
Reconstr.hcrush (@H0, @H2)
(@Coq.ZArith.BinInt.Z.lt_succ_r, @Coq.ZArith.BinInt.Z.lt_eq_cases,
@Coq.ZArith.BinInt.Z.le_succ_l, @Coq.ZArith.BinInt.Z.add_succ_r)
(@Coq.ZArith.BinInt.Z.le, @Coq.ZArith.BinIntDef.Z.succ, @Coq.ZArith.BinInt.Z.lt).
unfold IsSucc in *. destruct H0, H0, H2.
assert (x = 0). unfold size in H2. cbn in H2.
Reconstr.htrivial (@H0, @H2)
(@Coq.ZArith.BinInt.Z.lt_succ_r, @Coq.ZArith.BinInt.Z.le_antisymm,
@Coq.ZArith.BinInt.Z.one_succ)
Reconstr.Empty.
rewrite H4 in *. cbn in *.
destruct H3. subst. split.
assert (size (acomp a) >= 0) by apply list_size.
omega.
rewrite lem_size_app. cbn. omega.
easy.
- cbn in *.
specialize (succs_append (ccomp c1) (ccomp c2) n s); intros.
apply H1 in H0. destruct H0.
specialize (IHc1 i n H s H0).
case_eq (ccomp c2); intros; subst. scrush.
unfold size. rewrite app_length. cbn.
unfold size in IHc1.
split. omega.
assert (Z.of_nat (Datatypes.length (ccomp c1) + S (Datatypes.length l)) =
Z.of_nat (Datatypes.length (ccomp c1) + (Datatypes.length l) + 1)) by
Reconstr.htrivial Reconstr.Empty
(@Coq.Arith.PeanoNat.Nat.add_1_r, @Coq.Init.Peano.plus_n_Sm)
Reconstr.Empty.
rewrite H3.
rewrite !Nat2Z.inj_add. omega.
specialize (IHc2 i (n + size (ccomp c1)) H s H0).
rewrite lem_size_app. split; try omega.
pose acomp_size;
destruct IHc2;
specialize (ccomp_size c1); intros; omega.
- cbn in *. apply succs_append in H0. destruct H0.
apply bcomp_succs in H0. destruct H0.
rewrite !lem_size_app. cbn; split; try omega.
destruct H0.
specialize (ccomp_size c1); intros.
specialize (ccomp_size c2); intros.
rewrite !Z.add_assoc.
assert ( Z.pos (Pos.of_succ_nat (Datatypes.length (ccomp c2))) >= 0) by scrush.
omega. subst. cbn. split.
rewrite Z.add_assoc.
specialize (ccomp_size c1); intros.
assert (size (bcomp b false (size (ccomp c1) + 1)) >= 0).
apply list_size. omega.
rewrite !lem_size_app. unfold size.
cbn. rewrite !Z.add_assoc.
rewrite Zpos_P_of_succ_nat. omega.
remember (ccomp_size c1); intros. omega.
apply succs_append in H0. destruct H0.
specialize (IHc1 i (n + size (bcomp b false (size (ccomp c1) + 1))) H s H0).
split. destruct IHc1.
assert (size (bcomp b false (size (ccomp c1) + 1)) >= 0).
apply list_size. omega.
rewrite !lem_size_app, !Z.add_assoc. cbn.
rewrite Zpos_P_of_succ_nat. omega.
apply succs_Cons in H0.
destruct H0. cbn in *.
destruct H0. subst. split.
assert (size (bcomp b false (size (ccomp c1) + 1)) >= 0) by apply list_size.
assert (size (ccomp c1) >= 0) by apply list_size.
assert (size (ccomp c2) >= 0) by apply list_size. omega.
rewrite !lem_size_app, !Z.add_assoc. cbn.
rewrite Zpos_P_of_succ_nat.
unfold size in *. omega. easy.
specialize (IHc2 i (n + size (bcomp b false (size (ccomp c1) + 1)) + size (ccomp c1) + 1) H s H0).
destruct IHc2. split.
assert (size (bcomp b false (size (ccomp c1) + 1)) >= 0) by apply list_size.
assert (size (ccomp c1) >= 0) by apply list_size.
assert (size (ccomp c2) >= 0) by apply list_size. omega.
rewrite !lem_size_app, !Z.add_assoc. cbn.
rewrite Zpos_P_of_succ_nat.
unfold size in *. omega.
- cbn in *. apply succs_append in H0. destruct H0.
apply bcomp_succs in H0. destruct H0.
assert (Datatypes.length [JMP (- (size (bcomp b false (size (ccomp c) + 1)) +
size (ccomp c) + 1))] = 1%nat) by easy.
split.
assert (size (bcomp b false (size (ccomp c) + 1)) >= 0) by apply list_size. omega.
rewrite !lem_size_app, !Z.add_assoc. cbn.
assert (size (bcomp b false (size (ccomp c) + 1)) >= 0) by apply list_size.
assert (size (ccomp c) >= 0) by apply list_size. omega.
subst. split.
assert (size (bcomp b false (size (ccomp c) + 1)) >= 0) by apply list_size.
assert (size (ccomp c) >= 0) by apply list_size. omega.
rewrite !lem_size_app, !Z.add_assoc. cbn. omega.
unfold size. omega.
apply succs_append in H0. destruct H0.
specialize (IHc i (n + size (bcomp b false (size (ccomp c) + 1))) H s H0).
split.
assert (size (bcomp b false (size (ccomp c) + 1)) >= 0) by apply list_size. omega.
rewrite !lem_size_app, !Z.add_assoc. cbn. omega.
unfold IsSucc in H0. destruct H0, H0, H1.
assert (size [JMP (- (size (bcomp b false (size (ccomp c) + 1)) + size (ccomp c) + 1))] = 1).
easy. rewrite H3 in H1.
assert (x = 0) by omega. subst. cbn in *.
destruct H2. split. subst. omega.
rewrite !lem_size_app. cbn. subst.
rewrite !Z.add_assoc.
assert (size (bcomp b false (size (ccomp c) + 1)) >= 0) by apply list_size.
assert (size (ccomp c) >= 0) by apply list_size. omega. easy.
Qed.
Lemma ccomp_exits:
forall c s,
IsExit (ccomp c) s -> s = size (ccomp c).
Proof. intros; unfold IsExit in H;
destruct H; apply (ccomp_succs c 0) in H;
Reconstr.hsimple (@H, @H0)
(@Coq.ZArith.BinInt.Z.max_l, @Coq.ZArith.BinInt.Z.max_r,
@Coq.ZArith.BinInt.Z.lt_ge_cases, @Coq.ZArith.BinInt.Z.add_0_l)
Reconstr.Empty;
scrush.
Qed.
Lemma lem_znth_app_r :
forall xs ys i x, i >= 0 -> i < size xs -> znth i (xs ++ ys) x = znth i xs x.
Proof.
intro xs. induction xs; intros.
- scrush.
- rewrite lem_nth_append.
Reconstr.hcrush (@H0)
(@Compiler.lem_size_succ, @Coq.Bool.Bool.diff_true_false, @Coq.ZArith.Zbool.Zlt_is_lt_bool)
(@Compiler.znth, @Coq.ZArith.BinIntDef.Z.succ).
omega.
Qed.
Lemma exec1_split i j: forall P c P' s s' stk stk',
exec1 (P ++ c ++ P') (size P + i, s, stk) (j, s', stk') ->
0 <= i -> i < size c -> exec1 c (i, s, stk) (j - size P, s', stk').
Proof. intros; apply lem_exec1I; try omega;
unfold exec1 in *; destruct H, H, H, H, H2;
inversion H;
subst; assert (i >= 0) by omega;
specialize (lem_znth_app P (c ++ P') i ADD H4); intros;
rewrite H5 in H2;
assert (znth i (c ++ P') ADD = znth i c ADD).
{ Reconstr.hobvious (@H0, @H1)
(@Compiler.lem_exec1_hlp1)
Reconstr.Empty. }
rewrite H6 in H2;
specialize (lem_iexec_shift (znth i c ADD) (size P) i
(j - (size P)) x0 s' x1 stk'); intros;
assert (size P + (j - size P) = j) by omega;
Reconstr.hcrush (@H8, @H2, @H7)
Reconstr.Empty
Reconstr.Empty.
Qed.
Lemma exec_n_split:
forall (n: nat) P c P' (i j: Z) (s s': state * stack),
0 <= i -> i < size c ->
exec_n (P ++ c ++ P') (size P + i, fst s, snd s) n (j, fst s', snd s') ->
(j < size P \/ j >= size P + size c) ->
exists s'': state * stack, exists i', exists k, exists m,
exec_n c (i, fst s, snd s) k (i', fst s'', snd s'') /\ IsExit c i' /\
exec_n (P ++ c ++ P') (size P + i', fst s'', snd s'') m (j, fst s', snd s') /\
n%nat = (k + m)%nat.
Proof. intros n; induction n; intros; cbn in *.
- exists s; exists i; exists O; exists O;
split; try scrush
unfold IsSucc; inversion H1;
destruct H2;
assert (size P + i < size P -> False) by omega; omega.
- destruct H1, H1, x, p;
pose proof H1 as H1a;
pose proof H3 as H3a;
apply exec1_split in H1; try easy.
assert (HH: (0 <= z - size P < size c) \/ ~(0 <= z - size P < size c)) by omega.
destruct HH.
destruct H4;
specialize (IHn P c P' (z - size P) j (s1, s0) s').
assert (size P + (z - size P) = z) by omega;
rewrite H6 in IHn; unfold fst, snd in IHn;
specialize (IHn H4 H5 H3 H2);
destruct IHn, H7, H7, H7, H7, x, H8, H9, s';
exists (s2, s3); exists x0; exists (S x1); exists x2;
split; cbn.
exists (z - size P, s1, s0); split; easy.
split; scrush.
unfold exec1 in H1; destruct H1, H1, H1, H1, H5;
apply succs_iexec1 in H5; try omega; cbn in *;
assert (IsExit c (z - size P));
unfold IsExit. split; easy.
exists (s1, s0); exists (z - size P); exists 1%nat; exists n;
split; cbn; inversion H1;
apply exec1_split in H1a; try easy.
exists ((z - size P, s1, s0));
inversion H1; scrush.
assert (size P + (z - size P) = z) by
Reconstr.htrivial Reconstr.Empty
(@Coq.ZArith.BinInt.Zplus_minus)
Reconstr.Empty; scrush.
Qed.
Definition ascii_eqb (a b: Ascii.ascii) :=
match a, b with
| Ascii.Ascii a1 a2 a3 a4 a5 a6 a7 a8, Ascii.Ascii b1 b2 b3 b4 b5 b6 b7 b8 =>
Bool.eqb a1 b1 &&
Bool.eqb a2 b2 &&
Bool.eqb a3 b3 &&
Bool.eqb a4 b4 &&
Bool.eqb a5 b5 &&
Bool.eqb a6 b6 &&
Bool.eqb a7 b7 &&
Bool.eqb a8 b8
end.
Lemma ascii_eqb_refl: forall a, ascii_eqb a a = true.
Proof.
induction a; sauto;
Reconstr.hsimple Reconstr.Empty
(@Coq.Bool.Bool.eqb_reflx)
(@Coq.Init.Datatypes.andb).
Qed.
Fixpoint string_eqb (x y: string) :=
match x, y with
| ""%string, ""%string => true
| String a s1, String b s2 =>
if ascii_eqb a b then
string_eqb s1 s2 else false
| _, _ => false
end.
Fixpoint instr_eqb (i1 i2: instr): bool :=
match i1, i2 with
| JMP i, JMP j => Z.eqb i j