This repository was archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquivAndRefine.v
More file actions
1553 lines (1431 loc) · 42.1 KB
/
EquivAndRefine.v
File metadata and controls
1553 lines (1431 loc) · 42.1 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
Require Import Coq.ZArith.ZArith.
Require Import Coq.micromega.Psatz.
Require Import Coq.Strings.String.
Require Import Coq.Classes.Morphisms.
Require Import Coq.Classes.Morphisms_Prop.
Require Import Coq.Classes.RelationClasses.
Require Import Coq.Logic.Classical_Pred_Type.
Require Import Coq.Logic.Classical_Prop.
Require Import SetsClass.SetsClass. Import SetsNotation.
Require Import compcert.lib.Integers.
Require Import PV.Syntax.
Require Import PV.PracticalDenotations.
Import Lang_While DntSem_While.
Import EDenote CDenote.
Local Open Scope string.
Local Open Scope Z.
Local Open Scope sets.
(** * 定义与例子 *)
Definition EVar': string -> expr := EVar.
Coercion EConst: Z >-> expr.
Coercion EVar: var_name >-> expr.
Coercion EVar': string >-> expr.
Notation "[[ e ]]" := e
(at level 0, e custom expr_entry at level 99).
Notation "( x )" := x
(in custom expr_entry, x custom expr_entry at level 99).
Notation "x" := x
(in custom expr_entry at level 0, x constr at level 0).
Notation "f x" := (f x)
(in custom expr_entry at level 1, only parsing,
f custom expr_entry,
x custom expr_entry at level 0).
Notation "x + y" := (EBinop OPlus x y)
(in custom expr_entry at level 12, left associativity).
Notation "x - y" := (EBinop OMinus x y)
(in custom expr_entry at level 12, left associativity).
Notation "x * y" := (EBinop OMul x y)
(in custom expr_entry at level 11, left associativity).
Notation "x / y" := (EBinop ODiv x y)
(in custom expr_entry at level 11, left associativity).
Notation "x % y" := (EBinop OMod x y)
(in custom expr_entry at level 11, left associativity).
Notation "x <= y" := (EBinop OLe x y)
(in custom expr_entry at level 13, no associativity).
Notation "x < y" := (EBinop OLt x y)
(in custom expr_entry at level 13, no associativity).
Notation "x >= y" := (EBinop OGe x y)
(in custom expr_entry at level 13, no associativity).
Notation "x > y" := (EBinop OGt x y)
(in custom expr_entry at level 13, no associativity).
Notation "x == y" := (EBinop OEq x y)
(in custom expr_entry at level 13, no associativity).
Notation "x != y" := (EBinop ONe x y)
(in custom expr_entry at level 13, no associativity).
Notation "x && y" := (EBinop OAnd x y)
(in custom expr_entry at level 14, left associativity).
Notation "x || y" := (EBinop OOr x y)
(in custom expr_entry at level 15, left associativity).
Notation "! x" := (EUnop ONot x)
(in custom expr_entry at level 10).
Notation "- x" := (EUnop ONeg x)
(in custom expr_entry at level 10).
Notation "c1 ; c2" := (CSeq c1 c2)
(in custom expr_entry at level 20, right associativity).
Notation "'skip'" := (CSkip)
(in custom expr_entry at level 10).
Notation "'if' e 'then' '{' c1 '}' 'else' '{' c2 '}'" := (CIf e c1 c2)
(in custom expr_entry at level 19,
e custom expr_entry at level 5,
c1 custom expr_entry at level 99,
c2 custom expr_entry at level 99,
format "'if' e 'then' '{' c1 '}' 'else' '{' c2 '}'").
Notation "'while' e 'do' '{' c1 '}'" := (CWhile e c1)
(in custom expr_entry at level 19,
e custom expr_entry at level 5,
c1 custom expr_entry at level 99).
Arguments Rels.id: simpl never.
Arguments Rels.test: simpl never.
Arguments Rels.concat: simpl never.
Arguments Sets.indexed_union: simpl never.
Arguments Sets.indexed_intersect: simpl never.
Notation "⟦ e ⟧" := (eval_expr e)
(at level 0, only printing, e custom expr_entry at level 99).
Notation "⟦ c ⟧" := (eval_com c)
(at level 0, only printing, c custom expr_entry at level 99).
Ltac any_eval x :=
match goal with
| |- EDenote => exact (eval_expr x)
| |- CDenote => exact (eval_com x)
| _ => match type of x with
| expr => exact (eval_expr x)
| com => exact (eval_com x)
end
end.
Notation "⟦ x ⟧" := (ltac:(any_eval x))
(at level 0, only parsing, x custom expr_entry at level 99).
Notation "x × y" := (@Sets.test1 _ (y -> Prop) _ x)
(no associativity, at level 61): sets_scope.
(** 表达式语义等价 *)
Record eequiv (e1 e2: expr): Prop := {
nrm_eequiv:
⟦ e1 ⟧.(nrm) == ⟦ e2 ⟧.(nrm);
err_eequiv:
⟦ e1 ⟧.(err) == ⟦ e2 ⟧.(err);
}.
(** 表达式精化关系 *)
Record erefine (e1 e2: expr): Prop := {
nrm_erefine:
⟦ e1 ⟧.(nrm) ⊆ ⟦ e2 ⟧.(nrm) ∪ (⟦ e2 ⟧.(err) × int64);
err_erefine:
⟦ e1 ⟧.(err) ⊆ ⟦ e2 ⟧.(err);
}.
(** 程序语句语义等价 *)
Record cequiv (c1 c2: com): Prop := {
nrm_cequiv: ⟦ c1 ⟧.(nrm) == ⟦ c2 ⟧.(nrm);
err_cequiv: ⟦ c1 ⟧.(err) == ⟦ c2 ⟧.(err);
inf_cequiv: ⟦ c1 ⟧.(inf) == ⟦ c2 ⟧.(inf);
}.
(** 程序语句精化关系 *)
Record crefine (c1 c2: com): Prop := {
nrm_crefine:
⟦ c1 ⟧.(nrm) ⊆ ⟦ c2 ⟧.(nrm) ∪ (⟦ c2 ⟧.(err) × state);
err_crefine:
⟦ c1 ⟧.(err) ⊆ ⟦ c2 ⟧.(err);
inf_crefine:
⟦ c1 ⟧.(inf) ⊆ ⟦ c2 ⟧.(inf) ∪ ⟦ c2 ⟧.(err);
}.
Notation "e1 '~=~' e2" := (eequiv e1 e2)
(at level 69, only printing, no associativity).
Notation "e1 '<<=' e2" := (erefine e1 e2)
(at level 69, only printing, no associativity).
Notation "c1 '~=~' c2" := (cequiv c1 c2)
(at level 69, only printing, no associativity).
Notation "c1 '<<=' c2" := (crefine c1 c2)
(at level 69, only printing, no associativity).
Ltac any_equiv x y :=
match type of x with
| expr => exact (eequiv x y)
| com => exact (cequiv x y)
| _ => match type of y with
| expr => exact (eequiv x y)
| com => exact (cequiv x y)
end
end.
Ltac any_refine x y :=
match type of x with
| expr => exact (erefine x y)
| com => exact (crefine x y)
| _ => match type of y with
| expr => exact (erefine x y)
| com => exact (crefine x y)
end
end.
Notation "x '~=~' y" := (ltac:(any_equiv x y))
(at level 69, only parsing, no associativity).
Notation "x '<<=' y" := (ltac:(any_refine x y))
(at level 69, only parsing, no associativity).
Notation "H '.(nrm_eequiv)'" := (nrm_eequiv _ _ H)
(at level 1).
Notation "H '.(err_eequiv)'" := (err_eequiv _ _ H)
(at level 1).
Notation "H '.(nrm_erefine)'" := (nrm_erefine _ _ H)
(at level 1).
Notation "H '.(err_erefine)'" := (err_erefine _ _ H)
(at level 1).
Notation "H '.(nrm_cequiv)'" := (nrm_cequiv _ _ H)
(at level 1).
Notation "H '.(err_cequiv)'" := (err_cequiv _ _ H)
(at level 1).
Notation "H '.(inf_cequiv)'" := (inf_cequiv _ _ H)
(at level 1).
Notation "H '.(nrm_crefine)'" := (nrm_crefine _ _ H)
(at level 1).
Notation "H '.(err_crefine)'" := (err_crefine _ _ H)
(at level 1).
Notation "H '.(inf_crefine)'" := (inf_crefine _ _ H)
(at level 1).
(** 精化的例子 *)
Lemma const_plus_const_refine: forall n m: Z,
EConst (n + m) <<= [[n + m]].
(** 证明见Coq代码。*)
Proof.
intros.
assert (Int64.min_signed <= n <= Int64.max_signed \/
n < Int64.min_signed \/
n > Int64.max_signed) as Hn by lia.
assert (Int64.min_signed <= m <= Int64.max_signed \/
m < Int64.min_signed \/
m > Int64.max_signed) as Hm by lia.
split.
+ sets_unfold; intros s i ?.
simpl in H.
destruct H.
destruct Hn; [destruct Hm |]; [left | right | right]; simpl; sets_unfold.
- unfold arith_sem1_nrm, arith_compute1_nrm.
exists (Int64.repr n), (Int64.repr m).
pose proof Int64.signed_repr _ H1.
pose proof Int64.signed_repr _ H2.
rewrite H3, H4.
tauto.
- tauto.
- tauto.
+ sets_unfold; intros s ?.
simpl in H.
simpl; sets_unfold.
unfold arith_sem1_err, arith_compute1_err.
destruct Hn; [destruct Hm |]; [right | left | left]; simpl; sets_unfold.
- exists (Int64.repr n), (Int64.repr m).
pose proof Int64.signed_repr _ H0.
pose proof Int64.signed_repr _ H1.
rewrite H2, H3.
tauto.
- tauto.
- tauto.
Qed.
(** 语义等价的例子:顺序执行有结合律 *)
Theorem CSeq_assoc: forall (c1 c2 c3: com),
[[c1; (c2; c3)]] ~=~ [[(c1; c2); c3]].
Proof.
intros.
split.
+ simpl.
rewrite Rels_concat_assoc.
reflexivity.
+ simpl.
rewrite Rels_concat_union_distr_l.
rewrite Sets_union_assoc.
rewrite Rels_concat_assoc.
reflexivity.
+ simpl.
rewrite Rels_concat_union_distr_l.
rewrite Sets_union_assoc.
rewrite Rels_concat_assoc.
reflexivity.
Qed.
Theorem CIf_CSeq: forall e c1 c2 c3,
[[ if e then { c1 } else { c2 }; c3 ]] ~=~
[[ if e then { c1; c3 } else { c2; c3 } ]].
Proof.
intros.
split.
+ simpl.
rewrite <- ! Rels_concat_assoc.
apply Rels_concat_union_distr_r.
+ simpl.
rewrite ! Rels_concat_union_distr_r.
rewrite ! Rels_concat_union_distr_l.
rewrite <- ! Rels_concat_assoc.
sets_unfold; intros s; tauto.
+ simpl.
rewrite ! Rels_concat_union_distr_r.
rewrite ! Rels_concat_union_distr_l.
rewrite <- ! Rels_concat_assoc.
sets_unfold; intros s; tauto.
Qed.
(** * 语义等价与精化的性质 *)
(** 接下去,我们介绍语义等价的两条重要性质。其一:语义等价是一种等价关系。*)
(** 在Coq标准库中,_[Reflexive]_、_[Symmetric]_、_[Transitive]_以及
_[Equivalence]_定义了自反性、对称性、传递性以及等价关系。下面证明中,我们统
一使用了_[Instance]_关键字,而非之前证明中常用的_[Theorem]_与_[Lemma]_,我们
将稍后再解释_[Instance]_关键字的特殊作用。*)
Instance eequiv_refl: Reflexive eequiv.
Proof.
unfold Reflexive; intros.
split.
+ reflexivity.
+ reflexivity.
Qed.
Instance eequiv_sym: Symmetric eequiv.
Proof.
unfold Symmetric; intros.
split.
+ rewrite H.(nrm_eequiv).
reflexivity.
+ rewrite H.(err_eequiv).
reflexivity.
Qed.
Instance eequiv_trans: Transitive eequiv.
Proof.
unfold Transitive; intros.
split.
+ rewrite H.(nrm_eequiv), H0.(nrm_eequiv).
reflexivity.
+ rewrite H.(err_eequiv), H0.(err_eequiv).
reflexivity.
Qed.
Instance eequiv_equiv: Equivalence eequiv.
Proof.
split.
+ apply eequiv_refl.
+ apply eequiv_sym.
+ apply eequiv_trans.
Qed.
(** 下面还可以证明精化关系也具有自反性和传递性。*)
Instance erefine_refl: Reflexive erefine.
Proof.
unfold Reflexive; intros.
split.
+ apply Sets_included_union1.
+ reflexivity.
Qed.
Instance erefine_trans: Transitive erefine.
Proof.
unfold Transitive; intros.
split.
+ rewrite H.(nrm_erefine).
rewrite H0.(nrm_erefine).
rewrite H0.(err_erefine).
sets_unfold; intros s1 s2; tauto.
+ rewrite H.(err_erefine).
rewrite H0.(err_erefine).
reflexivity.
Qed.
(** 并且精化关系在语义等价变换下不变。*)
Instance erefine_well_defined:
Proper (eequiv ==> eequiv ==> iff) erefine.
Proof.
unfold Proper, respectful; intros.
split; intros.
+ split.
- rewrite <- H.(nrm_eequiv).
rewrite <- H0.(nrm_eequiv).
rewrite <- H0.(err_eequiv).
apply H1.(nrm_erefine).
- rewrite <- H.(err_eequiv).
rewrite <- H0.(err_eequiv).
apply H1.(err_erefine).
+ split.
- rewrite H.(nrm_eequiv).
rewrite H0.(nrm_eequiv).
rewrite H0.(err_eequiv).
apply H1.(nrm_erefine).
- rewrite H.(err_eequiv).
rewrite H0.(err_eequiv).
apply H1.(err_erefine).
Qed.
(** 程序语句间的语义等价关系也是等价关系,程序语句间的精化关系也具有自反性与传递
性。*)
Instance cequiv_refl: Reflexive cequiv.
Proof.
unfold Reflexive; intros.
split.
+ reflexivity.
+ reflexivity.
+ reflexivity.
Qed.
Instance cequiv_sym: Symmetric cequiv.
Proof.
unfold Symmetric; intros.
split.
+ rewrite H.(nrm_cequiv).
reflexivity.
+ rewrite H.(err_cequiv).
reflexivity.
+ rewrite H.(inf_cequiv).
reflexivity.
Qed.
Instance cequiv_trans: Transitive cequiv.
Proof.
unfold Transitive; intros.
split.
+ rewrite H.(nrm_cequiv), H0.(nrm_cequiv).
reflexivity.
+ rewrite H.(err_cequiv), H0.(err_cequiv).
reflexivity.
+ rewrite H.(inf_cequiv), H0.(inf_cequiv).
reflexivity.
Qed.
Instance cequiv_equiv: Equivalence cequiv.
Proof.
split.
+ apply cequiv_refl.
+ apply cequiv_sym.
+ apply cequiv_trans.
Qed.
Instance crefine_refl: Reflexive crefine.
Proof.
unfold Reflexive; intros.
split.
+ apply Sets_included_union1.
+ reflexivity.
+ apply Sets_included_union1.
Qed.
Instance crefine_trans: Transitive crefine.
Proof.
unfold Transitive; intros.
split.
+ rewrite H.(nrm_crefine).
rewrite H0.(nrm_crefine).
rewrite H0.(err_crefine).
sets_unfold; intros s1 s2; tauto.
+ rewrite H.(err_crefine).
rewrite H0.(err_crefine).
reflexivity.
+ rewrite H.(inf_crefine).
rewrite H0.(inf_crefine).
rewrite H0.(err_crefine).
sets_unfold; intros s; tauto.
Qed.
Instance crefine_well_defined:
Proper (cequiv ==> cequiv ==> iff) crefine.
Proof.
unfold Proper, respectful; intros.
split; intros.
+ split.
- rewrite <- H.(nrm_cequiv).
rewrite <- H0.(nrm_cequiv).
rewrite <- H0.(err_cequiv).
apply H1.(nrm_crefine).
- rewrite <- H.(err_cequiv).
rewrite <- H0.(err_cequiv).
apply H1.(err_crefine).
- rewrite <- H.(inf_cequiv).
rewrite <- H0.(inf_cequiv).
rewrite <- H0.(err_cequiv).
apply H1.(inf_crefine).
+ split.
- rewrite H.(nrm_cequiv).
rewrite H0.(nrm_cequiv).
rewrite H0.(err_cequiv).
apply H1.(nrm_crefine).
- rewrite H.(err_cequiv).
rewrite H0.(err_cequiv).
apply H1.(err_crefine).
- rewrite H.(inf_cequiv).
rewrite H0.(inf_cequiv).
rewrite H0.(err_cequiv).
apply H1.(inf_crefine).
Qed.
(** 两条重要性质之二是:所有语法连接词能保持语义等价关系(congruence),也能保持
精化关系(monotonicity)。下面先证明加法、减法、乘法的情况。*)
Lemma arith_sem1_nrm_congr: forall Zfun (e11 e12 e21 e22: expr),
e11 ~=~ e12 ->
e21 ~=~ e22 ->
arith_sem1_nrm Zfun ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ==
arith_sem1_nrm Zfun ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm).
Proof.
sets_unfold.
intros ? ? ? ? ? ? ? s i.
unfold arith_sem1_nrm.
apply ex_iff_morphism; intros i1.
apply ex_iff_morphism; intros i2.
apply and_iff_morphism; try apply H.(nrm_eequiv).
apply and_iff_morphism; try apply H0.(nrm_eequiv).
reflexivity.
Qed.
Lemma arith_sem1_err_congr: forall Zfun (e11 e12 e21 e22: expr),
e11 ~=~ e12 ->
e21 ~=~ e22 ->
⟦ e11 ⟧.(err) ∪ ⟦ e21 ⟧.(err) ∪
arith_sem1_err Zfun ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ==
⟦ e12 ⟧.(err) ∪ ⟦ e22 ⟧.(err) ∪
arith_sem1_err Zfun ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm).
Proof.
sets_unfold.
intros ? ? ? ? ? ? ? s.
unfold arith_sem1_err.
apply or_iff_morphism.
+ apply or_iff_morphism.
- apply H.(err_eequiv).
- apply H0.(err_eequiv).
+ apply ex_iff_morphism; intros i1.
apply ex_iff_morphism; intros i2.
apply and_iff_morphism; [apply H.(nrm_eequiv) |].
apply and_iff_morphism; [apply H0.(nrm_eequiv) |].
reflexivity.
Qed.
Lemma arith_sem1_nrm_mono: forall Zfun (e11 e12 e21 e22: expr),
e11 <<= e12 ->
e21 <<= e22 ->
arith_sem1_nrm Zfun ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ⊆
arith_sem1_nrm Zfun ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm) ∪
((⟦ e12 ⟧.(err) ∪ ⟦ e22 ⟧.(err) ∪
arith_sem1_err Zfun ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm)) × int64).
Proof.
intros.
sets_unfold.
intros s i.
unfold arith_sem1_nrm, arith_sem1_err.
intros [i1 [i2 [? [? ?] ] ] ].
apply H.(nrm_erefine) in H1.
apply H0.(nrm_erefine) in H2.
sets_unfold in H1.
sets_unfold in H2.
destruct H1; [| tauto].
destruct H2; [| tauto].
left.
exists i1, i2.
tauto.
Qed.
Lemma arith_sem1_err_mono: forall Zfun (e11 e12 e21 e22: expr),
e11 <<= e12 ->
e21 <<= e22 ->
⟦ e11 ⟧.(err) ∪ ⟦ e21 ⟧.(err) ∪
arith_sem1_err Zfun ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ⊆
⟦ e12 ⟧.(err) ∪ ⟦ e22 ⟧.(err) ∪
arith_sem1_err Zfun ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm).
Proof.
intros.
sets_unfold.
intros s.
unfold arith_sem1_err.
intros [ [? | ?] | [i1 [i2 [? [? ?] ] ] ] ].
+ apply H.(err_erefine) in H1.
tauto.
+ apply H0.(err_erefine) in H1.
tauto.
+ apply H.(nrm_erefine) in H1.
apply H0.(nrm_erefine) in H2.
sets_unfold in H1.
sets_unfold in H2.
destruct H1; [| tauto].
destruct H2; [| tauto].
right.
exists i1, i2.
tauto.
Qed.
(** 很多其它情况的证明是类似的。*)
Lemma arith_sem2_nrm_congr: forall int64fun (e11 e12 e21 e22: expr),
e11 ~=~ e12 ->
e21 ~=~ e22 ->
arith_sem2_nrm int64fun ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ==
arith_sem2_nrm int64fun ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm).
Proof.
sets_unfold.
intros ? ? ? ? ? ? ? s i.
unfold arith_sem2_nrm.
apply ex_iff_morphism; intros i1.
apply ex_iff_morphism; intros i2.
apply and_iff_morphism; [apply H.(nrm_eequiv) |].
apply and_iff_morphism; [apply H0.(nrm_eequiv) |].
reflexivity.
Qed.
Lemma arith_sem2_err_congr: forall (e11 e12 e21 e22: expr),
e11 ~=~ e12 ->
e21 ~=~ e22 ->
⟦ e11 ⟧.(err) ∪ ⟦ e21 ⟧.(err) ∪
arith_sem2_err ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ==
⟦ e12 ⟧.(err) ∪ ⟦ e22 ⟧.(err) ∪
arith_sem2_err ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm).
Proof.
sets_unfold.
intros ? ? ? ? ? ? s.
unfold arith_sem2_err.
apply or_iff_morphism.
+ apply or_iff_morphism.
- apply H.(err_eequiv).
- apply H0.(err_eequiv).
+ apply ex_iff_morphism; intros i1.
apply ex_iff_morphism; intros i2.
apply and_iff_morphism; [apply H.(nrm_eequiv) |].
apply and_iff_morphism; [apply H0.(nrm_eequiv) |].
reflexivity.
Qed.
Lemma arith_sem2_nrm_mono: forall int64fun (e11 e12 e21 e22: expr),
e11 <<= e12 ->
e21 <<= e22 ->
arith_sem2_nrm int64fun ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ⊆
arith_sem2_nrm int64fun ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm) ∪
((⟦ e12 ⟧.(err) ∪ ⟦ e22 ⟧.(err) ∪
arith_sem2_err ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm)) × int64).
Proof.
intros.
sets_unfold.
intros s i.
unfold arith_sem2_nrm, arith_sem2_err.
intros [i1 [i2 [? [? ?] ] ] ].
apply H.(nrm_erefine) in H1.
apply H0.(nrm_erefine) in H2.
sets_unfold in H1.
sets_unfold in H2.
destruct H1; [| tauto].
destruct H2; [| tauto].
left.
exists i1, i2.
tauto.
Qed.
Lemma arith_sem2_err_mono: forall (e11 e12 e21 e22: expr),
e11 <<= e12 ->
e21 <<= e22 ->
⟦ e11 ⟧.(err) ∪ ⟦ e21 ⟧.(err) ∪
arith_sem2_err ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ⊆
⟦ e12 ⟧.(err) ∪ ⟦ e22 ⟧.(err) ∪
arith_sem2_err ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm).
Proof.
intros.
sets_unfold.
intros s.
unfold arith_sem2_err.
intros [ [? | ?] | [i1 [i2 [? [? ?] ] ] ] ].
+ apply H.(err_erefine) in H1.
tauto.
+ apply H0.(err_erefine) in H1.
tauto.
+ apply H.(nrm_erefine) in H1.
apply H0.(nrm_erefine) in H2.
sets_unfold in H1.
sets_unfold in H2.
destruct H1; [| tauto].
destruct H2; [| tauto].
right.
exists i1, i2.
tauto.
Qed.
Lemma cmp_sem_nrm_congr: forall op (e11 e12 e21 e22: expr),
e11 ~=~ e12 ->
e21 ~=~ e22 ->
cmp_sem_nrm op ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ==
cmp_sem_nrm op ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm).
Proof.
sets_unfold.
intros ? ? ? ? ? ? ? s i.
unfold cmp_sem_nrm.
apply ex_iff_morphism; intros i1.
apply ex_iff_morphism; intros i2.
apply and_iff_morphism; [apply H.(nrm_eequiv) |].
apply and_iff_morphism; [apply H0.(nrm_eequiv) |].
reflexivity.
Qed.
Lemma cmp_sem_err_congr: forall (e11 e12 e21 e22: expr),
e11 ~=~ e12 ->
e21 ~=~ e22 ->
⟦ e11 ⟧.(err) ∪ ⟦ e21 ⟧.(err) ==
⟦ e12 ⟧.(err) ∪ ⟦ e22 ⟧.(err).
Proof.
sets_unfold.
intros ? ? ? ? ? ? s.
apply or_iff_morphism.
+ apply H.(err_eequiv).
+ apply H0.(err_eequiv).
Qed.
Lemma cmp_sem_nrm_mono: forall op (e11 e12 e21 e22: expr),
e11 <<= e12 ->
e21 <<= e22 ->
cmp_sem_nrm op ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ⊆
cmp_sem_nrm op ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm) ∪
((⟦ e12 ⟧.(err) ∪ ⟦ e22 ⟧.(err)) × int64).
Proof.
intros.
sets_unfold.
intros s i.
unfold cmp_sem_nrm.
intros [i1 [i2 [? [? ?] ] ] ].
apply H.(nrm_erefine) in H1.
apply H0.(nrm_erefine) in H2.
sets_unfold in H1.
sets_unfold in H2.
destruct H1; [| tauto].
destruct H2; [| tauto].
left.
exists i1, i2.
tauto.
Qed.
Lemma cmp_sem_err_mono: forall (e11 e12 e21 e22: expr),
e11 <<= e12 ->
e21 <<= e22 ->
⟦ e11 ⟧.(err) ∪ ⟦ e21 ⟧.(err) ⊆
⟦ e12 ⟧.(err) ∪ ⟦ e22 ⟧.(err).
Proof.
intros.
sets_unfold.
intros s.
intros [? | ?].
+ apply H.(err_erefine) in H1.
tauto.
+ apply H0.(err_erefine) in H1.
tauto.
Qed.
Lemma and_sem_nrm_congr: forall (e11 e12 e21 e22: expr),
e11 ~=~ e12 ->
e21 ~=~ e22 ->
and_sem_nrm ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ==
and_sem_nrm ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm).
Proof.
sets_unfold.
intros ? ? ? ? ? ? s i.
unfold and_sem_nrm.
apply ex_iff_morphism; intros i1.
apply and_iff_morphism; [apply H.(nrm_eequiv) |].
apply or_iff_morphism; [reflexivity |].
apply and_iff_morphism; [reflexivity |].
apply ex_iff_morphism; intros i2.
apply and_iff_morphism; [apply H0.(nrm_eequiv) |].
reflexivity.
Qed.
Lemma and_sem_err_congr: forall (e11 e12 e21 e22: expr),
e11 ~=~ e12 ->
e21 ~=~ e22 ->
⟦ e11 ⟧.(err) ∪ and_sem_err ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(err) ==
⟦ e12 ⟧.(err) ∪ and_sem_err ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(err).
Proof.
sets_unfold.
intros ? ? ? ? ? ? s.
unfold and_sem_err.
apply or_iff_morphism; [apply H.(err_eequiv) |].
apply ex_iff_morphism; intros i1.
apply and_iff_morphism; [apply H.(nrm_eequiv) |].
apply and_iff_morphism; [| apply H0.(err_eequiv)].
reflexivity.
Qed.
Lemma and_sem_nrm_mono: forall (e11 e12 e21 e22: expr),
e11 <<= e12 ->
e21 <<= e22 ->
and_sem_nrm ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ⊆
and_sem_nrm ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm) ∪
((⟦ e12 ⟧.(err) ∪ and_sem_err ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(err)) × int64).
Proof.
intros.
sets_unfold.
intros s i.
unfold and_sem_nrm, and_sem_err.
intros [i1 ?].
destruct H1.
apply H.(nrm_erefine) in H1.
sets_unfold in H1.
destruct H1; [| tauto].
destruct H2; [left; exists i1; tauto |].
destruct H2 as [? [i2 [? ?] ] ].
apply H0.(nrm_erefine) in H3.
sets_unfold in H3.
destruct H3; [| right; right; exists i1; tauto].
left; exists i1.
split; [tauto |].
right.
split; [tauto |].
exists i2; tauto.
Qed.
Lemma and_sem_err_mono: forall (e11 e12 e21 e22: expr),
e11 <<= e12 ->
e21 <<= e22 ->
⟦ e11 ⟧.(err) ∪ and_sem_err ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(err) ⊆
⟦ e12 ⟧.(err) ∪ and_sem_err ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(err).
Proof.
intros.
sets_unfold.
intros s.
unfold and_sem_err.
intros [? | ?]; [left; apply H.(err_erefine); tauto |].
destruct H1 as [i1 [? [? ?] ] ].
apply H.(nrm_erefine) in H1.
sets_unfold in H1.
destruct H1; [| tauto].
right; exists i1.
apply H0.(err_erefine) in H3.
tauto.
Qed.
Lemma or_sem_nrm_congr: forall (e11 e12 e21 e22: expr),
e11 ~=~ e12 ->
e21 ~=~ e22 ->
or_sem_nrm ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ==
or_sem_nrm ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm).
Proof.
sets_unfold.
intros ? ? ? ? ? ? s i.
unfold or_sem_nrm.
apply ex_iff_morphism; intros i1.
apply and_iff_morphism; [apply H.(nrm_eequiv) |].
apply or_iff_morphism; [reflexivity |].
apply and_iff_morphism; [reflexivity |].
apply ex_iff_morphism; intros i2.
apply and_iff_morphism; [apply H0.(nrm_eequiv) |].
reflexivity.
Qed.
Lemma or_sem_err_congr: forall (e11 e12 e21 e22: expr),
e11 ~=~ e12 ->
e21 ~=~ e22 ->
⟦ e11 ⟧.(err) ∪ or_sem_err ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(err) ==
⟦ e12 ⟧.(err) ∪ or_sem_err ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(err).
Proof.
sets_unfold.
intros ? ? ? ? ? ? s.
unfold or_sem_err.
apply or_iff_morphism; [apply H.(err_eequiv) |].
apply ex_iff_morphism; intros i1.
apply and_iff_morphism; [apply H.(nrm_eequiv) |].
apply and_iff_morphism; [| apply H0.(err_eequiv)].
reflexivity.
Qed.
Lemma or_sem_nrm_mono: forall (e11 e12 e21 e22: expr),
e11 <<= e12 ->
e21 <<= e22 ->
or_sem_nrm ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(nrm) ⊆
or_sem_nrm ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(nrm) ∪
((⟦ e12 ⟧.(err) ∪ or_sem_err ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(err)) × int64).
Proof.
intros.
sets_unfold.
intros s i.
unfold or_sem_nrm, or_sem_err.
intros [i1 ?].
destruct H1.
apply H.(nrm_erefine) in H1.
sets_unfold in H1.
destruct H1; [| tauto].
destruct H2; [left; exists i1; tauto |].
destruct H2 as [? [i2 [? ?] ] ].
apply H0.(nrm_erefine) in H3.
sets_unfold in H3.
destruct H3; [| right; right; exists i1; tauto].
left; exists i1.
split; [tauto |].
right.
split; [tauto |].
exists i2; tauto.
Qed.
Lemma or_sem_err_mono: forall (e11 e12 e21 e22: expr),
e11 <<= e12 ->
e21 <<= e22 ->
⟦ e11 ⟧.(err) ∪ or_sem_err ⟦ e11 ⟧.(nrm) ⟦ e21 ⟧.(err) ⊆
⟦ e12 ⟧.(err) ∪ or_sem_err ⟦ e12 ⟧.(nrm) ⟦ e22 ⟧.(err).
Proof.
intros.
sets_unfold.
intros s.
unfold or_sem_err.
intros [? | ?]; [left; apply H.(err_erefine); tauto |].
destruct H1 as [i1 [? [? ?] ] ].
apply H.(nrm_erefine) in H1.
sets_unfold in H1.
destruct H1; [| tauto].
right; exists i1.
apply H0.(err_erefine) in H3.
tauto.
Qed.
(** 下面把二元运算的情况汇总起来。*)
Instance EBinop_congr: forall op,
Proper (eequiv ==> eequiv ==> eequiv) (EBinop op).
Proof.
unfold Proper, respectful.
intros.
destruct op.
(** 布尔二元运算的情况 *)
+ split.
- apply or_sem_nrm_congr; tauto.
- apply or_sem_err_congr; tauto.
+ split.
- apply and_sem_nrm_congr; tauto.
- apply and_sem_err_congr; tauto.
(** 大小比较的情况 *)
+ split.
- apply cmp_sem_nrm_congr; tauto.
- apply cmp_sem_err_congr; tauto.
+ split.
- apply cmp_sem_nrm_congr; tauto.
- apply cmp_sem_err_congr; tauto.
+ split.
- apply cmp_sem_nrm_congr; tauto.
- apply cmp_sem_err_congr; tauto.
+ split.
- apply cmp_sem_nrm_congr; tauto.
- apply cmp_sem_err_congr; tauto.
+ split.
- apply cmp_sem_nrm_congr; tauto.
- apply cmp_sem_err_congr; tauto.
+ split.
- apply cmp_sem_nrm_congr; tauto.
- apply cmp_sem_err_congr; tauto.
(** 加减乘运算的情况 *)
+ split.
- apply arith_sem1_nrm_congr; tauto.
- apply arith_sem1_err_congr; tauto.
+ split.
- apply arith_sem1_nrm_congr; tauto.
- apply arith_sem1_err_congr; tauto.
+ split.
- apply arith_sem1_nrm_congr; tauto.
- apply arith_sem1_err_congr; tauto.
(** 除法与取余的情况 *)
+ split.
- apply arith_sem2_nrm_congr; tauto.
- apply arith_sem2_err_congr; tauto.
+ split.
- apply arith_sem2_nrm_congr; tauto.
- apply arith_sem2_err_congr; tauto.
Qed.
Instance EBinop_mono: forall op,
Proper (erefine ==> erefine ==> erefine) (EBinop op).
Proof.
unfold Proper, respectful.
intros.
destruct op.
(** 布尔二元运算的情况 *)
+ split.
- apply or_sem_nrm_mono; tauto.
- apply or_sem_err_mono; tauto.
+ split.
- apply and_sem_nrm_mono; tauto.
- apply and_sem_err_mono; tauto.
(** 大小比较的情况 *)
+ split.
- apply cmp_sem_nrm_mono; tauto.
- apply cmp_sem_err_mono; tauto.
+ split.
- apply cmp_sem_nrm_mono; tauto.
- apply cmp_sem_err_mono; tauto.
+ split.
- apply cmp_sem_nrm_mono; tauto.
- apply cmp_sem_err_mono; tauto.
+ split.
- apply cmp_sem_nrm_mono; tauto.
- apply cmp_sem_err_mono; tauto.
+ split.
- apply cmp_sem_nrm_mono; tauto.
- apply cmp_sem_err_mono; tauto.
+ split.
- apply cmp_sem_nrm_mono; tauto.
- apply cmp_sem_err_mono; tauto.
(** 加减乘运算的情况 *)
+ split.
- apply arith_sem1_nrm_mono; tauto.
- apply arith_sem1_err_mono; tauto.
+ split.
- apply arith_sem1_nrm_mono; tauto.
- apply arith_sem1_err_mono; tauto.
+ split.
- apply arith_sem1_nrm_mono; tauto.
- apply arith_sem1_err_mono; tauto.
(** 除法与取余的情况 *)
+ split.
- apply arith_sem2_nrm_mono; tauto.
- apply arith_sem2_err_mono; tauto.
+ split.