-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerationProofs.v
1832 lines (1725 loc) · 63.8 KB
/
GenerationProofs.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 ZArith.
From QuickChick Require Import QuickChick.
Require Import TestingCommon Indist Generation.
Require Import GenerationProofsHelpers.
From mathcomp Require Import ssreflect ssrfun ssrbool ssrnat eqtype seq.
(* The old version of the semantics for sequence is preferrable for these proofs *)
Lemma Forall2_combine : forall {A} n (gs : list (G A)) l,
List.Forall2 (fun y => semGenSize y n) gs l ->
forall x, List.In x (seq.zip l gs) -> semGenSize (snd x) n (fst x).
Proof.
move => A size gs l H.
induction H as [| x y l l' H1 H2 Hforall ].
+ move => x HIn.
inv HIn.
+ move => [a ga] HIn.
simpl in *.
case: HIn => [[]* | HIn]; subst => //=.
by apply Hforall in HIn; simpl in HIn.
Qed.
Lemma seqzip__Forall2 : forall {A} n (gs : list (G A)) l,
length l == length gs ->
(forall x, List.In x (seq.zip l gs) -> semGenSize (snd x) n (fst x)) ->
List.Forall2 (fun y => semGenSize y n) gs l.
Proof.
move => A size gs.
induction gs as [ | g gs IHg].
+ move => l Hlen HIn.
destruct l.
- by constructor.
- inv Hlen.
+ move => l Hlen HIn.
destruct l.
- inv Hlen.
- constructor => //.
* apply (HIn (a,g)). by left.
* apply IHg => //=.
move => x HIn'.
apply HIn.
by right.
Qed.
Section Sized.
Variable size : nat.
Lemma existsHighObsLow : forall l obs,
isHigh l obs -> isHigh H obs.
move => l obs High.
destruct l; destruct obs; auto.
Qed.
Lemma gen_from_length_correct: forall l,
(RandomQC.leq 0 (l-1))%Z ->
semGenSize (gen_from_length l) size <-->
(fun z => (Z.le 0 z /\ Z.le z (l-1))).
Proof.
move => l H' z; split => /=.
+ move => /semChooseSize H.
apply H in H'; clear H.
split; move : H' => /andP [/= H1 H2]; by apply Zle_bool_imp_le.
+ move => [H1 H2].
apply semChooseSize => //=.
apply/andP.
split; by apply Zle_imp_le_bool.
Qed.
Lemma gen_from_nat_length_correct: forall l,
(RandomQC.leq 0 (Z.of_nat l - 1))%Z ->
semGenSize (gen_from_nat_length l) size <-->
(fun z => (Z.le 0 z /\ Z.le z ((Z.of_nat l)-1))).
Proof.
move => l z.
rewrite /gen_from_nat_length.
move/(_ (Z.of_nat l)): gen_from_length_correct; apply.
auto.
Qed.
Lemma gen_BinOpT_correct :
semGenSize gen_BinOpT size <--> [set: BinOpT].
Proof.
rewrite /gen_BinOpT /all. move => op.
split => // _.
apply semElementsSize.
case : op; simpl; auto.
- do 4 right; left; by []. (* I don't know why it doesn't get this *)
Qed.
Lemma gen_label_correct : semGenSize gen_label size <--> [set: Label].
Proof.
rewrite /gen_label /all => l. split => [|?] //=.
apply semElementsSize. case l; simpl; by tauto.
Qed.
Lemma gen_high_label_correct (obs : Label) :
isHigh H obs ->
semGenSize (gen_high_label obs) size <--> [set l : Label | isHigh l obs].
Proof.
rewrite /gen_high_label.
move => High l. split.
+ move => /semElementsSize H.
destruct obs; destruct l; auto;
simpl in H;
try by move: H => [? | [ ? | [ ? | ?]]];
try by move: H => [? | [ ? | ?]];
inv H.
+ move => H.
apply semElementsSize.
destruct obs; destruct l; auto;
simpl; try inv H.
- by left.
- by right;left.
- by right;right;left.
- by left.
- by right;left.
- by left.
- by right; left.
Qed.
Section WithDataLenNonEmpty.
Canonical lab4_eqType := (@LabelEqType.label_eqType Label _).
Variable inf : Info.
Hypothesis data_len_nonempty : data_len inf <> [::].
Hypothesis code_len_correct : RandomQC.leq Z0 ((code_len inf) - 1)%Z.
Hypothesis data_len_positive : forall mf z, (mf,z) \in (data_len inf) ->
RandomQC.leq Z0 (z-1)%Z.
Hypothesis no_regs_positive : RandomQC.leq Z0 ((Z.of_nat (no_regs inf)) - 1)%Z.
Hypothesis frame_sizes_correct : RandomQC.leq C.min_frame_size C.max_frame_size.
(* PC *)
Definition pc_in_bounds (pc : Ptr_atom) :=
let '(PAtm z l) := pc in
(0 <= z <= (code_len inf) - 1)%Z.
Lemma gen_PC_correct:
semGenSize (gen_PC inf) size <--> [set pc : Ptr_atom | pc_in_bounds pc].
Proof.
rewrite /gen_PC /smart_gen /smart_gen_label /pc_in_bounds
semBindSize (eq_bigcupl _ _ gen_label_correct) => pc.
split.
+ move => [label [_ /semBindSize[z [/gen_from_length_correct H1
/semReturnSize H2]]]].
apply H1 in code_len_correct.
by case H2.
+ move: pc => [z l] EqZ.
exists l; split => //.
apply semBindSize.
rewrite /bigcup.
exists z; split.
* by apply gen_from_length_correct.
* by apply semReturnSize.
Qed.
(* FIXME: move this somewhere else *)
Lemma seq_InP (A : eqType) (x : A) (xs : seq A) :
reflect (seq_In xs x) (x \in xs).
Proof.
elim: xs=> [|x' xs IH] /=; first by constructor.
by rewrite inE; apply/(iffP orP)=> [[/eqP ->|/IH]|[->|/IH]]; auto.
Qed.
(* Pointer *)
Definition valid_pointer (ptr : Pointer) :=
let '(Ptr mf addr) := ptr in
(exists len, (mf, len) \in (data_len inf) /\
(0 <= addr <= len - 1)%Z).
Lemma gen_pointer_correct:
semGenSize (gen_pointer inf) size <--> valid_pointer.
Proof.
move => ptr. remember inf as Inf.
destruct Inf.
rewrite /gen_pointer /valid_pointer.
split.
* move => /semBindSize[[mf z] [/semElementsSize H]].
destruct data_len.
- by case data_len_nonempty.
- move => /semBindSize [addr [/gen_from_length_correct CL]].
move => /semReturnSize Ret.
case Ret.
exists z; split.
simpl in H; case: H => H; subst; rewrite /= inE; apply/orP; [by left|right].
by apply/seq_InP.
apply CL.
by apply (data_len_positive mf z); apply/seq_InP.
* case: ptr => mf z.
destruct data_len as [|x xs] eqn:DL.
- by case data_len_nonempty.
- move => [len [HIn EqZ]].
apply semBindSize.
exists (mf, len); split.
+ apply semElementsSize.
subst; simpl in HIn.
by apply/seq_InP.
+ apply semBindSize.
exists z; split.
* apply gen_from_length_correct => //=.
move: EqZ => [HL HH].
assert (0 <= len - 1)%Z by (eapply Z.le_trans; eassumption).
by apply Zle_imp_le_bool.
* by apply semReturnSize.
Qed.
(* Int *)
Definition int_spec (z : Z) : Prop :=
(- Z.of_nat size <= z <= Z.max (Z.of_nat size) (code_len inf - 1))%Z.
Lemma gen_int_correct :
semGenSize (gen_int inf) size <--> int_spec.
move => z.
split.
+ move => /semFrequencySize /= [[freq g] [H1 H2]].
case: H1 => [[] * | [[] * | [[] * | //]]]; subst.
- move: H2 => /arbInt_correct [? ?].
split; [ omega | ].
eapply Z.le_trans; eauto.
by apply Z.le_max_l.
- move: H2 => /semReturnSize /= H; case H.
split; [ omega | ].
eapply (Z.le_trans _ (Z.of_nat size) _); [ omega | ].
by apply Z.le_max_l.
- move: H2 => /gen_from_length_correct H.
apply H in code_len_correct.
split; [ omega | ];
eapply (Z.le_trans _ (code_len inf - 1) _); [ omega | ].
by apply Z.le_max_r.
+ move => [ZMin ZMax].
apply semFrequencySize => /=.
case (Z_lt_le_dec z 0) => ZLt0.
- eexists; split.
* by left.
* apply arbInt_correct.
split; omega.
- case (Z.max_spec (Z.of_nat size) (code_len inf - 1)).
* move => [_ H]; subst; simpl in *.
rewrite H in ZMax.
eexists; split.
+ by [right; right; left].
+ apply gen_from_length_correct => //=; omega.
* move => [_ H]; subst; simpl in *.
rewrite H in ZMax.
eexists; split.
+ by left.
+ apply arbInt_correct; omega.
Qed.
(* Value *)
Definition val_spec (v : Value) : Prop :=
match v with
| Vint n => int_spec n
| Vptr ptr => valid_pointer ptr
| Vlab l => True
end.
(* Largely similar proofs now, should probably automate part of it *)
Lemma gen_value_correct:
semGenSize (gen_value inf) size <--> val_spec.
Proof.
rewrite /gen_value /val_spec.
remember inf as Inf.
clear data_len_nonempty.
clear code_len_correct.
clear data_len_positive.
clear no_regs_positive.
case : Inf HeqInf => def clen dlen reg HeqInf.
case; rewrite HeqInf.
+ (* VInt *)
Opaque gen_int.
move => z.
split => //.
- move => /semFrequencySize /=.
move => [[freq g] [H1 /= H2]].
Admitted.
(*
case: H1 => [[_ Heq] | [[_ Heq] | [[_ Heq] | //]]]; rewrite <- Heq in H2;
apply semLiftGenSize in H2;
move: H2 => [? [H1 H2]];
case: H2 => // <-.
by apply gen_int_correct in H1.
- move => ZSpec.
apply semFrequencySize => /=.
eexists; split.
* by left.
* simpl. apply semLiftGenSize.
exists z; split => //.
by apply gen_int_correct.
+ (* Vptr *)
Opaque gen_pointer valid_pointer.
case => mf addr.
split.
- move => /semFrequencySize /=.
move => [[freq g] [H1 /= H2]].
case: H1 => [[_ Heq] | [[_ Heq] | [[_ Heq] | //]]]; rewrite <- Heq in H2;
apply semLiftGenSize in H2;
move: H2 => [? [H1 H2]];
case: H2 => // <-.
by apply gen_pointer_correct in H1.
- move => ZSpec.
apply semFrequencySize => /=.
eexists; split.
* by right; left.
* simpl. apply semLiftGenSize.
exists (Ptr mf addr); split => //.
by apply gen_pointer_correct.
+ (* Vlab *)
move => L. split => // _.
apply semFrequencySize => /=.
eexists; split.
* by [right; right; left].
* simpl. apply semLiftGenSize.
eexists; split => //.
by apply gen_label_correct.
Qed.
*)
(* Atom *)
Definition atom_spec atm :=
let '(Atm val lab) := atm in
val_spec val.
Lemma gen_atom_correct:
semGenSize (gen_atom inf) size <--> atom_spec.
Proof.
move => [val lab]. rewrite /gen_atom.
split.
+ move => /semLiftGen2Size /=.
move => [[val' lab'] [H1 H2]].
inversion H2; subst; clear H2.
case: H1 => /= [H1 H2].
by apply gen_value_correct.
+ move => H /=.
apply semLiftGen2Size.
eexists; split => /= ; try split => /=.
- eapply gen_value_correct.
rewrite /atom_spec in H.
by instantiate (1 := (val, lab)).
- by apply gen_label_correct.
- by [].
Qed.
(* regSet *)
Definition regs_spec regs :=
(length regs = no_regs inf) /\
(forall reg, reg \in regs -> atom_spec reg).
Lemma gen_registers_correct:
semGenSize (gen_registers inf) size <--> regs_spec.
Proof.
move => regs.
rewrite /gen_registers. split.
+ move /semVectorOfSize => [H1 H2].
split => //. move => reg /seq_InP HIn. by apply/gen_atom_correct; apply H2.
+ move => [Hlen Hregs]. apply/semVectorOfSize. split => // x HIn.
by apply/gen_atom_correct; apply Hregs; apply/seq_InP.
Qed.
(* stack_loc *)
Definition valid_reg_ptr ptr_r :=
(Z0 <= ptr_r <= (Z.of_nat (no_regs inf) - 1))%Z.
Definition stack_loc_spec (t : StackFrame) : Prop :=
let '(SF ptr_a regs ptr_r lab) := t in
regs_spec regs /\
valid_reg_ptr ptr_r /\
let 'PAtm addr lab' := ptr_a in
(0 <= addr <= ((code_len inf) - 1))%Z.
Lemma gen_stack_loc_correct :
(semGenSize (smart_gen_stack_loc inf) size <--> stack_loc_spec).
Proof.
rewrite /smart_gen_stack_loc /smart_gen semBindSize.
split.
+ move => [regs' [/gen_registers_correct Hregs]].
move /semBindSize => [[pca pcl] []] Hpc.
move /semBindSize => [ptr_r []] Hptr_r.
move /semBindSize => [lab []] Hlab.
move => /semReturnSize H.
inv H.
rewrite /stack_loc_spec.
do 2 split => //.
- apply gen_from_nat_length_correct in Hptr_r.
* by case Hptr_r.
* by apply no_regs_positive.
- by apply gen_PC_correct in Hpc.
+ case: a => [[ret_pc_val ret_pc_lab] regs ptr_r lab] [[Hlen Hregs] [Hptr Hpc_val]].
exists regs; split => //=.
- by apply gen_registers_correct.
apply semBindSize.
exists (PAtm ret_pc_val ret_pc_lab); split.
- by apply gen_PC_correct.
apply semBindSize.
exists ptr_r; split.
- by apply gen_from_nat_length_correct.
apply semBindSize.
exists lab; split.
- by apply gen_label_correct.
by apply semReturnSize.
Qed.
(* Stack *)
Definition stack_spec (s: Stack) : Prop :=
s = ST nil \/
exists loc, s = ST (loc :: nil) /\ stack_loc_spec loc.
Lemma gen_stack_correct:
semGenSize (smart_gen_stack inf) size <--> stack_spec.
Proof.
Opaque smart_gen_stack_loc.
rewrite /smart_gen_stack /stack_spec. move => st.
split.
+ move/semFrequencySize => /= [[freq g] [H1 /= H2]].
case: H1 => [[] * | [[] * | //]]; subst.
- apply semReturnSize in H2. by left; case H2.
- move: H2 => /semBindSize [sf [/gen_stack_loc_correct H1 /semReturnSize H2]].
right; exists sf; split => /= //.
+ move => [StNil | [sf [H1 H2]]]; subst; apply semFrequencySize => /=.
- eexists; split => /= //.
* by left.
* by apply semReturnSize.
- eexists; split => /= //.
* by right; left.
* apply semBindSize.
exists sf; split => /= //.
+ by apply gen_stack_loc_correct.
+ by apply semReturnSize.
Qed.
(* frame *)
Definition mem_single_upd_spec mem mf (mem' : memory) :=
match Mem.get_frame mem mf with
| Some (Fr lab data) =>
exists fr, Mem.upd_frame mem mf fr = Some mem' /\
let 'Fr lab' data' := fr in
lab' = lab /\
length data' = length data /\
forall atm, atm \in data' -> atom_spec atm
| None => mem' = mem
end.
Lemma populate_frame_correct :
forall mem mf,
semGenSize (populate_frame inf mem mf) size <--> (mem_single_upd_spec mem mf).
Proof.
move=> mem mf mem'. rewrite /populate_frame /mem_single_upd_spec.
case Heq: (Mem.get_frame mem mf)=> [[lab data]|] //=.
- move/Mem.upd_get_frame : (Heq) => Hupd.
split.
+ move /semBindSize => [atmlst [/semVectorOfSize [Hl Hvec] HMatch]].
move/(_ (Fr lab atmlst)): Hupd => [fr Hfr].
rewrite Hfr /= in HMatch;
apply semReturnSize in HMatch.
inversion HMatch; subst; clear HMatch.
exists (Fr lab atmlst); repeat split => /= //.
move => atm HIn. apply gen_atom_correct. by apply Hvec; apply/seq_InP.
+ move => [fr [Hget H]].
case: fr Hupd Hget H =>
lab' data' Hupd Hget [Heq1 [Heq2 H]]; subst.
apply semBindSize.
exists data'. split.
apply semVectorOfSize. split => // x HIn. by apply/gen_atom_correct; apply H; apply/seq_InP; eauto.
rewrite Hget. by apply semReturnSize.
- split.
+ move => /semReturnSize H. by case H.
+ by move => H; subst; apply semReturnSize.
Qed.
(* Memory *)
Lemma zreplicate_spec :
forall {A : eqType} (v : A) (z : Z),
(0 <= z)%Z ->
exists (l : list A),
(forall x, x \in l -> x = v) /\
length l = Z.to_nat z /\
zreplicate z v = Some l.
Proof.
move => A v z Hle. exists (nseq (Z.to_nat z) v).
repeat split.
- move=> x HIn. apply Z2Nat.inj_le in Hle; try omega.
elim: (Z.to_nat z) HIn => [| n IHn] //=.
rewrite /= inE=> /orP [/eqP Heq | HIn]; try assumption; auto.
- apply Z2Nat.inj_le in Hle; try omega.
induction (Z.to_nat z) as [| n IHn].
+ reflexivity.
+ simpl. rewrite IHn; auto; simpl; omega.
- rewrite /zreplicate. destruct (Z_lt_dec z 0); try reflexivity.
omega.
Qed.
Lemma zreplicate_eq :
forall {A : eqType} (l: list A) v z,
(0 <= z)%Z ->
(forall x, x \in l -> x = v) ->
length l = Z.to_nat z ->
zreplicate z v = Some l.
Proof.
move => A l v x Hle HIn Heq.
rewrite /zreplicate. destruct ( Z_lt_dec x 0 ) as [H | H].
- omega.
- apply Z2Nat.inj_le in Hle; try omega; simpl.
clear H Hle. congr Some. rewrite -{}Heq.
elim: l HIn=> [|a l IH] //= HIn; congr cons.
by symmetry; apply: HIn; rewrite inE eqxx.
by apply: IH=> a' Pa'; apply: HIn; rewrite inE Pa' orbT.
Qed.
Definition init_mem_spec (size : nat) (m : memory)
(blocks : list (mframe * Z)) (m': memory)
(blocks': list (mframe * Z)) :=
exists (lst : list (Label * (list Atom))),
length lst = size /\
(forall l data,
(l, data) \in lst ->
(C.min_frame_size <= Z.of_nat (length data) <= C.max_frame_size)%Z /\
(forall v, v \in data -> v = (Vint 0 @ bot))) /\
(m', blocks') =
foldl
(fun (ml : memory * (list (mframe * Z))) (elem : Label * (list Atom)) =>
let '(l, data) := elem in
let '(m_i, bs) := ml in
let (b, m) := Mem.alloc Local m_i bot (Fr l data) in
(m, (b, Z.of_nat (length data)) :: bs)
) (m, blocks) lst.
Definition mem_constraints (m : memory) :=
forall b l data,
Mem.get_frame m b = Some (Fr l data) ->
(C.min_frame_size <= Z.of_nat (length data) <= C.max_frame_size)%Z /\
Mem.stamp b = bot.
(* (* CH: now unused *) *)
(* Lemma all_bellow_top : forall l, *)
(* In l (allThingsBelow top). *)
(* Proof. rewrite /allThingsBelow. case; simpl; tauto. Qed. *)
Lemma gen_init_mem_helper_correct:
forall (n: nat) (m : memory) (blocks : list (mframe * Z)),
(mem_constraints m) ->
semGenSize (gen_init_mem_helper n (m, blocks)) size <-->
(fun p => init_mem_spec n m blocks (fst p) (snd p)).
Proof.
move => n m blocks Hspec [m' lst']. rewrite /init_mem_spec. split.
{ move => Hgen. generalize dependent m. generalize dependent blocks.
induction n as [| n IHn]; intros blocks mem Hspec Hgen.
- exists [::]. repeat split => /= //.
rewrite /gen_init_mem_helper in Hgen. by apply semReturnSize in Hgen.
- rewrite /gen_init_mem_helper in Hgen.
fold gen_init_mem_helper in Hgen.
apply semBindSize in Hgen.
move : Hgen => [len [Hchoose /semBindSize [lab [Hlab Hgen]]]].
move: Hchoose => /semChooseSize H.
apply H in frame_sizes_correct; clear H.
move: frame_sizes_correct => /andP [/= /Zle_bool_imp_le Hle1 /Zle_bool_imp_le Hle2].
rewrite /C.min_frame_size /C.max_frame_size in Hle1 Hle2 *.
unfold alloc in Hgen.
destruct (zreplicate_spec (Vint 0 @ ⊥) len) as [data [HIn [Heq HSome]]].
simpl in *; omega. rewrite HSome in Hgen.
remember (Mem.alloc Local mem ⊥ (Fr lab data)) as alloc.
destruct alloc as [fr mem'].
destruct (IHn ((fr, Z.of_nat (length data)) :: blocks) mem')
as [lst [Hlen [Hforall Hfold]]] => //; clear IHn.
+ rewrite /init_mem_spec in Hspec *.
symmetry in Heqalloc. move : (Heqalloc) => Halloc.
move => fr' lab' data' Hget.
apply Mem.alloc_get_frame with (b' := fr') in Halloc.
move: Halloc; rewrite Hget.
have [efr|nefr] := altP (fr =P fr').
* move=> [??]; subst lab' data' fr'.
rewrite /C.min_frame_size /C.max_frame_size Heq.
repeat split => //=; try (simpl in *; rewrite Z2Nat.id; omega).
by eapply Mem.alloc_stamp; apply Heqalloc.
* move=> Halloc. eapply Hspec. rewrite -Halloc. eauto.
+ rewrite Heq. simpl in *; rewrite Z2Nat.id; last omega.
by move: Hgen; rewrite -Heqalloc.
+ exists ((lab, data) :: lst). split. by subst.
split => //=.
* move => l data'; rewrite inE=> /orP [/eqP [eq1 eq2] | H]; subst.
split => //. rewrite Heq //. rewrite Z2Nat.id. split => //.
eapply Z.le_trans; [| apply Hle1] => //.
edestruct Hforall as [Hrng HIn']. apply H.
split; auto. simpl in Hfold. rewrite Hfold.
by rewrite -Heqalloc. }
{ move => [lst [Hlen [HIn Hfold]]]. generalize dependent lst.
generalize dependent m. generalize dependent blocks.
induction n as [| n IHn]; intros blocks m Hspec lst Hlen HIn Hfold.
- destruct lst; simpl in *.
by apply semReturnSize.
congruence.
- rewrite /gen_init_mem_helper. fold gen_init_mem_helper. apply semBindSize.
destruct lst as [|[lab data] lst]. simpl in Hlen; congruence.
destruct (HIn lab data) as [[Hle1 Hle2] HInx]; try by apply in_eq.
by rewrite inE eqxx.
exists (Z.of_nat (length data)). split.
+ apply semChooseSize.
by apply frame_sizes_correct.
apply/andP.
split; by apply Zle_imp_le_bool.
+ apply semBindSize.
exists lab. split; try by apply gen_label_correct.
rewrite /alloc.
rewrite (zreplicate_eq data); auto; try omega; try by rewrite Nat2Z.id.
remember (Mem.alloc Local m L (Fr lab data)) as frm.
destruct frm as [fr1 m1]. rewrite -Heqfrm.
apply IHn with (lst := lst).
* rewrite /init_mem_spec in Hspec *.
move => block lab' data' Hget.
symmetry in Heqfrm. move: (Heqfrm)=> Halloc.
apply Mem.alloc_get_frame with (b' := block) in Halloc.
move: Halloc; have [efr|nefr] := altP (fr1 =P _).
- rewrite {}Hget => - [??]; subst lab' data' block.
split => //.
eapply Mem.alloc_stamp.
apply Heqfrm.
- rewrite Hget. rewrite /mem_constraints in Hspec => e.
eapply Hspec; by eauto.
* by inversion Hlen.
* move => lab' data' HIn'.
by apply: (HIn lab'); rewrite inE; apply/orP; right.
* simpl in Hfold. by rewrite -Heqfrm in Hfold. }
Qed.
Lemma gen_init_mem_correct:
forall (top : Label),
semGenSize gen_init_mem size <-->
(fun ml =>
(exists n,
C.min_no_frames <= n <= C.max_no_frames /\
init_mem_spec n (Mem.empty Atom Label) [::] (fst ml) (snd ml))).
Proof.
move => top init_mem. split.
{ unfold gen_init_mem.
move => /semBindSize [len [Hchoose Hgen]].
exists len. apply semChooseSize in Hchoose.
move: Hchoose => /andP [/= Hle1 Hle2]. simpl in *.
edestruct (gen_init_mem_helper_correct len (Mem.empty Atom Label))
as [Hl _].
- move => b l data Hget.
by rewrite Mem.get_empty in Hget.
- destruct (Hl Hgen) as [lst H].
split => //. apply/andP. split => //.
by eauto.
assumption.
}
{ move => [len [/andP [Hle1 Hle2] Hspec]].
edestruct (gen_init_mem_helper_correct len (Mem.empty Atom Label))
as [_ Hr].
- rewrite /init_mem_spec /=. move => b l data Hget.
by rewrite Mem.get_empty in Hget.
- rewrite /gen_init_mem.
apply semBindSize.
exists len. split.
+ apply semChooseSize. split => //.
apply/andP. by split.
by auto.
}
Qed.
Definition init_mem_single_upd_spec (mem : Mem.t Atom Label)
(mf : Mem.block Label) (mem' : memory) :=
match Mem.get_frame mem mf with
| Some (Fr lab data) =>
exists fr : Memory.frame,
Mem.upd_frame mem mf fr = Some mem' /\
(let 'Fr lab' data' := fr in
lab' = lab /\
length data' = length data /\
(forall atm : Atom, atm \in data' -> atom_spec atm))
| None => mem' = mem
end.
Definition populated_memory_spec (m : memory) (m': memory) :=
let blocks := [seq fst p | p <- data_len inf] in
foldr (fun block (p : memory -> Prop) m_prev =>
exists m, (mem_single_upd_spec m_prev block m) /\ p m)
(eq m') blocks m /\
(forall b lab d,
Mem.get_frame m' b = Some (Fr lab d) ->
Mem.stamp b = bot /\
(C.min_frame_size <= Z.of_nat (length d) <= C.max_frame_size)%Z).
(* Lemma semFoldGen_right :
forall {A B : Type} (f : A -> B -> G A) (bs : list B) (a0 : A) (s : nat),
semGenSize (foldGen f bs a0) s <-->
[ set an |
foldr (fun b p => [set a_prev | exists a, a \in (semGenSize (f a_prev b) s :&: p)])
[set an] bs a0].
Pr *)
Lemma populate_memory_correct:
forall (m : memory),
mem_constraints m ->
semGenSize (populate_memory inf m) size <-->
(populated_memory_spec m ).
Proof.
move => m Hcontent m'.
split.
{ unfold populate_memory.
move => /semFoldGen_right Hgen. rewrite /populated_memory_spec.
generalize dependent m.
set lst := ((map _ (data_len inf))).
induction lst as [| b bs IHbs]; move=> m Hinit Hfold.
rewrite /mem_constraints in Hinit.
- simpl in *; subst.
inversion Hfold; subst; clear Hfold.
split => // b l d /Hinit [? ?];
by repeat split => //.
- simpl in *. move: Hfold => [m'' [Hpop Hfold]].
have Hcnstr: mem_constraints m''.
{ rewrite /populate_frame in Hpop.
remember (Mem.get_frame m b) as get.
destruct get as [[l d]|].
* symmetry in Heqget.
apply semBindSize in Hpop.
case : Hpop => [d' [/semVectorOfSize [Hlen Hforall] Hupd]].
destruct (Mem.upd_get_frame (Fr l d') Heqget)
as [fr Hupd'].
rewrite Hupd' in Hupd.
apply semReturnSize in Hupd.
inv Hupd. rewrite /mem_constraints.
destruct (Hinit _ _ _ Heqget) as [? ?].
subst. move => b' l' d'' Hget.
move: (Mem.get_upd_frame Hupd' b') => Hget'.
case: (b =P b') Hget' => e Hget'. inv e.
- rewrite Hget in Hget'. inv Hget'. split => //.
split; [rewrite Hlen | repeat split => //]; omega.
- rewrite Hget in Hget'. symmetry in Hget'.
destruct (Hinit _ _ _ Hget') as [? ?].
by repeat split => //.
* apply semReturnSize in Hpop; by inv Hpop.
}
destruct (IHbs m'' Hcnstr Hfold) as [Hfold' Hcnstr']. split => //.
exists m''. split=> //.
by apply populate_frame_correct. }
{ rewrite /populated_memory_spec /populate_memory. move => [Hfold Hconstr].
apply semFoldGen_right. generalize dependent m.
set lst := ((map _ (data_len inf))).
induction lst as [| b bs IHbs]; move=> m Hinit Hfold.
+ by simpl in *.
+ simpl in *. move : Hfold => [m'' [Hupd Hfold]]. eexists. split => //.
* apply populate_frame_correct. eassumption.
* apply IHbs => //. rewrite /mem_single_upd_spec in Hupd.
case Heqget: (Mem.get_frame m b) Hupd=> [[l d]|] Hupd; try by inv Hupd.
move : Hupd => [[l' d'] [Hupd' [eq1 [Hlen Hforall]]]]; subst.
destruct (Mem.upd_get_frame (Fr l d') Heqget)
as [fr Hupd''].
rewrite Hupd'' in Hupd'. inv Hupd'. rewrite /mem_constraints.
destruct (Hinit _ _ _ Heqget) as [? ?].
subst. move => b' l' d'' Hget.
move: (Mem.get_upd_frame Hupd'' b') => Hget'.
case: (b =P b') Hget' => e Hget'. inv e.
- rewrite Hget in Hget'. inv Hget'. split => //.
split; [rewrite Hlen | repeat split => //]; omega.
- rewrite Hget in Hget'. symmetry in Hget'.
destruct (Hinit _ _ _ Hget') as [? ?].
repeat split => //. }
Qed.
(* Instruction *)
Definition Instruction_spec (st : State) (instr : @Instr Label) :=
let '(St im m stk regs pc ) := st in
let '(dptr, cptr, num, lab) :=
groupRegisters st regs [::] [::] [::] [::] Z0 in
match instr with
| PcLab z | PutLab _ z
| Put _ z => (0 <= z <= (Zlength regs -1))%Z
| Mov z1 z2 => (0 <= z1 <= (Zlength regs -1))%Z /\ (0 <= z2 <= (Zlength regs-1))%Z
| MLab z1 z2 | Load z1 z2 | Store z1 z2 | MSize z1 z2 | PGetOff z1 z2
| Write z1 z2 =>
dptr <> [::] /\ z1 \in dptr /\ (0 <= z2 <= (Zlength regs-1))%Z
| Nop => True | Halt => False
| Lab z1 z2 =>
(0 <= z1 <= (Zlength regs-1))%Z /\ (0 <= z2 <= (Zlength regs-1))%Z
| BCall z1 z2 z3 =>
cptr <> [::] /\ lab <> [::] /\
z1 \in cptr /\ z2 \in lab /\ (0 <= z3 <= (Zlength regs-1))%Z
| BRet =>
match stk with
| ST [::] => False
| ST _ => True
end
| Alloc z1 z2 z3 =>
num <> [::] /\ lab <> [::] /\
z1 \in num /\ z2 \in lab /\ (0 <= z3 <= (Zlength regs-1))%Z
| Jump z => cptr <> [::] /\ z \in cptr
| BNZ z1 z2 => num <> [::] /\ (-1 <= z1 <= 2)%Z /\ z2 \in num
| PSetOff z1 z2 z3 =>
dptr <> [::] /\ num <> [::] /\
z1 \in dptr /\ z2 \in num /\ (0 <= z3 <= (Zlength regs-1))%Z
| BinOp BAdd z1 z2 z3 | BinOp BMult z1 z2 z3 | BinOp BEq z1 z2 z3 =>
num <> [::] /\ z1 \in num /\ z2 \in num /\ (0 <= z3 <= (Zlength regs-1))%Z
| BinOp BJoin z1 z2 z3 | BinOp BFlowsTo z1 z2 z3 =>
lab <> [::] /\ z1 \in lab /\ z2 \in lab /\ (0 <= z3 <= (Zlength regs-1))%Z
end.
Ltac discr_gen_eq :=
match goal with
| H : (_, liftGen ?f ?gen) = (?n, ?g),
Hg : ?g _ |- _ =>
move : H => [Heq1 Heq2]; subst;
apply semLiftGenSize in Hg; move: Hg => [a [_ H]]; discriminate
| H : (_, returnGen ?a) = (?n, ?g),
Hg : ?g _ |- _ =>
move : H => [Heq1 Heq2]; subst; discriminate
| H : (_, liftGen2 ?f ?gen1 ?gen2) = (?n, ?g),
Hg : ?g _ |- _ =>
move : H => [Heq1 Heq2]; subst;
apply semLiftGen2Size in Hg;
move: Hg => [a [_ [a' [_ H]]]]; subst; discriminate
| H : (_, liftGen3 ?f ?gen1 ?gen2 ?gen3) = (?n, ?g),
Hg : ?g _ |- _ =>
move : H => [Heq1 Heq2]; subst;
apply semLiftGen3Size in Hg;
move: Hg => [a [_ [a' [_ [a'' [ _ H]]]]]]; subst; discriminate
| H : (_, liftGen4 _ _ _ _ _) = (_, ?g),
Hg : ?g _ |- _ =>
move : H => [Heq1 Heq2]; subst;
apply semLiftGen4Size in Hg;
move: Hg => [a [_ [a' [_ [a'' [ _ [a''' [ _ H]]]]]]]]; subst; discriminate
end.
Ltac discr_or_first :=
match goal with
| HIn : ((_, _) = (_ , _) \/ _) |- _ => case: HIn => [Heq | HIn]; [discr_gen_eq | ]
| HIn : (_, _) \in _ |- _ => case/orP: HIn => [Heq | HIn]; [discr_gen_eq | ]
end.
(*
Ltac unfold_gen :=
match goal with
| Hg : returnGen _ _ |- _ =>
rewrite returnGen_def in Hg; subst
| Hg : liftGen _ _ _ |- _ =>
rewrite liftGen_def in Hg; move: Hg => [b [H1 [Heq]]]; subst
| Hg : liftGen2 _ _ _ _ |- _ =>
rewrite liftGen2_def in Hg;
move: Hg => [b [H1 [b' [H2 [Heq1 Heq2]]]]]; subst
| Hg : liftGen3 _ _ _ _ _ |- _ =>
rewrite liftGen3_def in Hg;
move: Hg => [b [H1 [b' [H2 [b'' [H3 [Heq1 Heq2 Heq3]]]]]]]; subst
| Hg : liftGen4 _ _ _ _ _ _ |- _ =>
rewrite liftGen4_def in Hg;
move: Hg=>[b [H1 [b' [H2 [b'' [ H3 [b''' [H4 [Heq1 Heq2 Heq3 Heq4]]]]]]]]]; subst
end.
Ltac try_solve :=
match goal with
| |- _ /\ _ => split => //=; by try_solve
| Hand : _ /\ _ |- _ => destruct Hand; by try_solve
| Hor : _ \/ _ |- _ => destruct Hor; by try_solve
| |- ~ _ => move => contra; subst; by try_solve
| Hchoose : choose _ _ |- _ =>
rewrite choose_def /= in Hchoose; by try_solve
| Helem : elements _ _ _ |- _ =>
move/elements_equiv : Helem => [Helem //= | [Helem1 Helem2] //=]; subst;
by try_solve
| HIn: In _ [] |- _ => by []
| Hnonempty : ~ (onNonEmpty [] _ = 0) |- _ =>
by rewrite /onNonEmpty in Hnonempty
| Hnonempty : ~ (_ * (onNonEmpty [] _))%coq_nat = 0 |- _ =>
by rewrite [X in (_ * X)%coq_nat]/onNonEmpty -mult_n_O in Hnonempty
| Hif: ~ ((if ?b then _ else _ ) = _) |- ?b = true =>
by case: b Hif
| |- (_ <= _)%Z => by apply/Zle_bool_imp_le
| |- _ => by []
end.
Ltac find_instr instr lst k :=
match lst with
| nil => k 0 (pure Nop)
| (?n, liftGen4 ?c ?a1 ?a2 ?a3 ?a4) :: ?lst' => (* match with liftGen4 *)
match instr with
| c _ _ _ _ => k n (liftGen4 c a1 a2 a3 a4)
| _ => find_instr instr lst' k
end
| (?n, liftGen3 ?c ?a1 ?a2 ?a3) :: ?lst' => (* match with liftGen3 *)
match instr with
| c _ _ _ => k n (liftGen3 c a1 a2 a3)
| _ => find_instr instr lst' k
end
| (?n, liftGen2 ?c ?a1 ?a2) :: ?lst' => (* match with liftGen2 *)
match instr with
| c _ _ => k n (liftGen2 c a1 a2)
| _ => find_instr instr lst' k
end
| (?n, liftGen ?c ?a1) :: ?lst' => (* match with liftGen *)
match instr with
| c _ => k n (liftGen c a1)
| _ => find_instr instr lst' k
end
| (?n, ?f ?c) :: ?lst' => (* match with pure/returnGen *)
match instr with
| c => k n (f c)
| _ => find_instr instr lst' k
end
end.
Ltac instantiate_exists :=
match goal with
| |- exists n g, In (n, g) ?lst /\ _ ?cnstr /\ _ =>
find_instr cnstr lst ltac:(fun n g => pose cnstr; exists n; exists g);
split; [repeat ((try by apply in_eq); apply in_cons) | split => //]
end.
Ltac try_solve2 :=
match goal with
| Hand : _ /\ _ |- _ => destruct Hand; by try_solve2
| |- _ = _ => reflexivity
| |- _ /\ _ => split; [| by try_solve2]; by try_solve2
| |- liftGen4 _ _ _ _ _ _ => rewrite liftGen4_def; by try_solve2
| |- liftGen3 _ _ _ _ _ => rewrite liftGen3_def; by try_solve2
| |- liftGen2 _ _ _ _ => rewrite liftGen2_def; by try_solve2
| |- liftGen _ _ _ => rewrite liftGen_def; by try_solve2
| |- elements _ _ _ => apply/elements_equiv; left; by try_solve2
| |- choose _ _ => rewrite choose_def => /=; by try_solve2
| |- is_true (_ <=? _)%Z => by apply/Zle_imp_le_bool
| |- gen_from_length _ _ => rewrite /gen_from_length; try try_solve2
| |- ~ onNonEmpty ?l _ = 0 => by destruct l
| |- exists _, _ => eexists; by try_solve2
| |- ~ (( _ * onNonEmpty ?c _)%coq_nat * onNonEmpty ?l _)%coq_nat = 0 =>
by destruct c; destruct l
| |- gen_BinOpT _ => by apply gen_BinOpT_correct
| |- ~ (if ?b then _ else _) = 0 => by destruct b
| _ => by []
end.
*)
Ltac solver :=
match goal with
| Hl : _ (_,_) , Hsem : semGenSize _ _ _ |- _ =>
case : Hl => [[] * | Hl];
[ subst; simpl in Hsem;
match goal with
| H : semGenSize (pure _) size _ |- _ =>
apply semReturnSize in Hsem;
inv Hsem
| H : semGenSize (liftGen _ _) _ _ |- _ =>
let x := fresh in
move : Hsem => /semLiftGenSize [? [x H]];
inv H
| H : semGenSize (liftGen2 _ _ _) _ _ |- _ =>
let x := fresh in
move : Hsem => /semLiftGen2Size [[freq' g'] [x H]];
rewrite /prod_curry in H; [inv H]
| H : semGenSize (liftGen3 _ _ _ _) _ _ |- _ =>
move : Hsem => /semLiftGen3Size [? [? [? [? [? [? ?]]]]]];
discriminate
| H : semGenSize (liftGen4 _ _ _ _ _) _ _ |- _ =>
move : Hsem => /semLiftGen4Size [? [? [? [? [? [? [? [? ?]]]]]]]];
discriminate
| _ => idtac
end | solver ]
| Hl : (_,_) = (_,_) \/ _ , Hsem : semGenSize _ _ _ |- _ =>
case : Hl => [[] * | Hl];
[ subst; simpl in Hsem;
match goal with
| H : semGenSize (pure _) size _ |- _ =>
apply semReturnSize in Hsem;
inv Hsem
| H : semGenSize (liftGen _ _) _ _ |- _ =>
let x := fresh in
move : Hsem => /semLiftGenSize [? [x H]];
inv H
| H : semGenSize (liftGen2 _ _ _) _ _ |- _ =>
let x := fresh in
move : Hsem => /semLiftGen2Size [[freq' g'] [x H]];
rewrite /prod_curry in H; [inv H]
| H : semGenSize (liftGen3 _ _ _ _) _ _ |- _ =>
move : Hsem => /semLiftGen3Size [? [? [? [? [? [? ?]]]]]];
discriminate
| H : semGenSize (liftGen4 _ _ _ _ _) _ _ |- _ =>
move : Hsem => /semLiftGen4Size [? [? [? [? [? [? [? [? ?]]]]]]]];
discriminate
| _ => idtac
end | solver ]
| _ => idtac
end.