-
Notifications
You must be signed in to change notification settings - Fork 5
/
AbstrInterp2.v
1226 lines (1025 loc) · 38.8 KB
/
AbstrInterp2.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
From Coq Require Import ZArith Psatz Bool String List FMaps.
From Coq Require Import FunctionalExtensionality.
From CDF Require Import Sequences IMP.
From CDF Require AbstrInterp.
Local Open Scope string_scope.
Local Open Scope Z_scope.
(** * 5. Analyse statique par interprétation abstraite, version améliorée *)
(** ** 5.5. Interface des domaines abstraits améliorés *)
(** Nous ajoutons à l'interface des valeurs abstraites des opérations
abstraites pour l'analyse inverse des conditionnelles et pour
l'élargissement. *)
Module Type VALUE_ABSTRACTION.
(** On reprend toutes les déclarations de l'interface simplifiée. *)
Include AbstrInterp.VALUE_ABSTRACTION.
(** [meet] calcule un minorant de ses deux arguments. *)
Parameter meet: t -> t -> t.
Axiom meet_1: forall n N1 N2, In n N1 -> In n N2 -> In n (meet N1 N2).
(** [isIn] teste si une valeur concrète appartient à une valeur abstraite. *)
Parameter isIn: Z -> t -> bool.
Axiom isIn_1: forall n N, In n N -> isIn n N = true.
(** Opérateurs abstraits pour l'analyse inverse des comparaisons. *)
(** Considérons un test [a1 = a2] qui s'évalue à vrai dans le programme.
Soit [N1] une abstraction de [a1] et [N2] une abstraction de [a2].
[eq_inv N1 N2] produit une paire de valeurs abstraites [N1', N2'].
[N1'] est une abstraction possiblement plus précise pour [a1]
qui prend en compte le fait que [a1 = a2].
[N2'] est une abstraction possiblement plus précise pour [a2]
qui prend en compte le fait que [a1 = a2]. *)
Parameter eq_inv: t -> t -> t * t.
Axiom eq_inv_1:
forall n1 n2 N1 N2,
In n1 N1 -> In n2 N2 -> n1 = n2 ->
In n1 (fst (eq_inv N1 N2)) /\ In n2 (snd (eq_inv N1 N2)).
Parameter ne_inv: t -> t -> t * t.
Axiom ne_inv_1:
forall n1 n2 N1 N2,
In n1 N1 -> In n2 N2 -> n1 <> n2 ->
In n1 (fst (ne_inv N1 N2)) /\ In n2 (snd (ne_inv N1 N2)).
Parameter le_inv: t -> t -> t * t.
Axiom le_inv_1:
forall n1 n2 N1 N2,
In n1 N1 -> In n2 N2 -> n1 <= n2 ->
In n1 (fst (le_inv N1 N2)) /\ In n2 (snd (le_inv N1 N2)).
Parameter gt_inv: t -> t -> t * t.
Axiom gt_inv_1:
forall n1 n2 N1 N2,
In n1 N1 -> In n2 N2 -> n1 > n2 ->
In n1 (fst (gt_inv N1 N2)) /\ In n2 (snd (gt_inv N1 N2)).
(** [widen N1 N2] calcule un majorant de son premier arguments, choisi
de sorte à garantir et accélérer la convergence de l'itération
de point fixe. *)
Parameter widen: t -> t -> t.
Axiom widen_1: forall N1 N2, le N1 (widen N1 N2).
(** Pour garantir la convergence, on fournit une mesure à valeurs
entières positives qui décroît strictement le long de l'itération
de point fixe avec élargissement. *)
Parameter measure: t -> nat.
Axiom measure_top: measure top = 0%nat.
Axiom widen_2: forall N1 N2, (measure (widen N1 N2) <= measure N1)%nat.
Axiom widen_3:
forall N1 N2, ble N2 N1 = false -> (measure (widen N1 N2) < measure N1)%nat.
End VALUE_ABSTRACTION.
(** Nous ajoutons à l'interface des états abstraits
une opération d'élargissement. *)
Module Type STORE_ABSTRACTION.
Declare Module V: VALUE_ABSTRACTION.
Parameter t: Type.
Parameter get: ident -> t -> V.t.
Definition In (s: store) (S: t) : Prop := forall x, V.In (s x) (get x S).
Parameter set: ident -> V.t -> t -> t.
Axiom set_1: forall x n N s S, V.In n N -> In s S -> In (update x n s) (set x N S).
Definition le (S1 S2: t) : Prop :=
forall s, In s S1 -> In s S2.
Parameter ble: t -> t -> bool.
Axiom ble_1: forall S1 S2, ble S1 S2 = true -> le S1 S2.
Parameter bot: t.
Axiom bot_1: forall s, ~(In s bot).
Parameter top: t.
Parameter top_1: forall s, In s top.
Parameter join: t -> t -> t.
Axiom join_1: forall s S1 S2, In s S1 -> In s (join S1 S2).
Axiom join_2: forall s S1 S2, In s S2 -> In s (join S1 S2).
(** Voici le nouvel opérateur d'élargissement. *)
Parameter widen: t -> t -> t.
Axiom widen_1: forall S1 S2, le S1 (widen S1 S2).
(** L'ordre ci-dessous correspond aux itérations successives de l'itération
de point fixe avec élargissement. Il faut qu'il soit bien fondé
afin de garantir la terminaison. *)
Definition widen_order (S S1: t) :=
exists S2, S = widen S1 S2 /\ ble S2 S1 = false.
Axiom widen_order_wf: well_founded widen_order.
End STORE_ABSTRACTION.
(** ** 5.6. L'analyseur générique amélioré. *)
Module Analysis (ST: STORE_ABSTRACTION).
Module V := ST.V.
(** *** Calcul de post-points fixes avec élargissement et rétrécissement *)
Section FIXPOINT.
Variable F: ST.t -> ST.t.
Program Definition is_true (b: bool) : { b = true } + { b = false } :=
match b with true => left _ | false => right _ end.
Lemma iter_up_acc:
forall (S: ST.t) (acc: Acc ST.widen_order S) (S': ST.t),
ST.ble S' S = false ->
Acc ST.widen_order (ST.widen S S').
Proof.
intros. eapply Acc_inv; eauto. exists S'. auto.
Defined.
Fixpoint iter_up (S: ST.t) (acc: Acc ST.widen_order S) : ST.t :=
let S' := F S in
match is_true (ST.ble S' S) with
| left LE => S
| right NOTLE => iter_up (ST.widen S S') (iter_up_acc S acc S' NOTLE)
end.
Fixpoint iter_down (n: nat) (S: ST.t) : ST.t :=
match n with
| O => S
| S n => let S' := F S in
if ST.ble (F S') S' then iter_down n S' else S
end.
Definition niter_down := 3%nat.
Definition postfixpoint : ST.t :=
iter_down niter_down (iter_up ST.bot (ST.widen_order_wf ST.bot)).
Lemma iter_up_sound:
forall S acc, ST.le (F (iter_up S acc)) (iter_up S acc).
Proof.
induction S using (well_founded_induction ST.widen_order_wf).
intros acc. destruct acc. cbn. destruct (is_true (ST.ble (F S) S)).
- apply ST.ble_1; auto.
- apply H. exists (F S); auto.
Qed.
Lemma iter_down_sound:
forall n S, ST.le (F S) S -> ST.le (F (iter_down n S)) (iter_down n S).
Proof.
induction n; intros; cbn.
- auto.
- destruct (ST.ble (F (F S)) (F S)) eqn:BLE.
+ apply IHn. apply ST.ble_1; auto.
+ auto.
Qed.
Lemma postfixpoint_sound: ST.le (F postfixpoint) postfixpoint.
Proof.
apply iter_down_sound. apply iter_up_sound.
Qed.
End FIXPOINT.
(** *** Interprétation abstraite des expressions arithmétiques *)
(** Même définition que dans la version simplifiée. *)
Fixpoint Aeval (a: aexp) (S: ST.t) : V.t :=
match a with
| CONST n => V.const n
| VAR x => ST.get x S
| PLUS a1 a2 => V.add (Aeval a1 S) (Aeval a2 S)
| MINUS a1 a2 => V.sub (Aeval a1 S) (Aeval a2 S)
end.
Lemma Aeval_sound:
forall s S a,
ST.In s S -> V.In (aeval a s) (Aeval a S).
Proof.
induction a; cbn; intros.
- apply V.const_1.
- apply H.
- apply V.add_1; auto.
- apply V.sub_1; auto.
Qed.
(** *** Analyse inverse des expressions arithmétiques et booléennes *)
(** En supposant que la valeur concrète de [a] appartient à la valeur
abstraite [Res], qu'apprenons-nous sur les valeurs des variables
qui apparaissent dans [a]? Les faits que nous apprenons
sont utilisés pour affiner les valeurs abstraites de ces variables
dans l'état abstrait [S]. *)
Fixpoint assume_eval (a: aexp) (Res: V.t) (S: ST.t) : ST.t :=
match a with
| CONST n =>
if V.isIn n Res then S else ST.bot
| VAR x =>
ST.set x (V.meet Res (ST.get x S)) S
| PLUS a1 a2 =>
let N1 := Aeval a1 S in
let N2 := Aeval a2 S in
let Res1 := V.meet N1 (V.sub Res N2) in
let Res2 := V.meet N2 (V.sub Res N1) in
assume_eval a1 Res1 (assume_eval a2 Res2 S)
| MINUS a1 a2 =>
let N1 := Aeval a1 S in
let N2 := Aeval a2 S in
let Res1 := V.meet N1 (V.add Res N2) in
let Res2 := V.meet N2 (V.sub N1 Res) in
assume_eval a1 Res1 (assume_eval a2 Res2 S)
end.
Lemma assume_eval_sound:
forall a Res S s,
V.In (aeval a s) Res -> ST.In s S -> ST.In s (assume_eval a Res S).
Proof.
induction a; cbn; intros.
- (* CONST *)
rewrite V.isIn_1 by auto. auto.
- (* VAR *)
replace s with (update x (s x) s).
apply ST.set_1; auto.
apply V.meet_1; auto.
apply functional_extensionality; intros y.
unfold update; destruct (string_dec x y); congruence.
- (* PLUS *)
set (n1 := aeval a1 s) in *. set (n2 := aeval a2 s) in *.
set (N1 := Aeval a1 S). set (N2 := Aeval a2 S).
assert (V.In n1 N1) by (apply Aeval_sound; auto).
assert (V.In n2 N2) by (apply Aeval_sound; auto).
apply IHa1; fold n1.
apply V.meet_1. auto. replace n1 with ((n1 + n2) - n2) by lia. apply V.sub_1; auto.
apply IHa2; fold n2.
apply V.meet_1. auto. replace n2 with ((n1 + n2) - n1) by lia. apply V.sub_1; auto.
auto.
- (* MINUS *)
set (n1 := aeval a1 s) in *. set (n2 := aeval a2 s) in *.
set (N1 := Aeval a1 S). set (N2 := Aeval a2 S).
assert (V.In n1 N1) by (apply Aeval_sound; auto).
assert (V.In n2 N2) by (apply Aeval_sound; auto).
apply IHa1; fold n1.
apply V.meet_1. auto. replace n1 with ((n1 - n2) + n2) by lia. apply V.add_1; auto.
apply IHa2; fold n2.
apply V.meet_1. auto. replace n2 with (n1 - (n1 - n2)) by lia. apply V.sub_1; auto.
auto.
Qed.
(** En supposant que l'expression booléenne [b] s'évalue concrètement à [res],
qu'apprenons-nous sur les valeurs des variables
qui apparaissent dans [b]? Les faits que nous apprenons
sont utilisés pour affiner les valeurs abstraites de ces variables
dans l'état abstrait [S]. *)
Fixpoint assume_test (b: bexp) (res: bool) (S: ST.t): ST.t :=
match b with
| TRUE => if res then S else ST.bot
| FALSE => if res then ST.bot else S
| EQUAL a1 a2 =>
let (Res1, Res2) :=
if res
then V.eq_inv (Aeval a1 S) (Aeval a2 S)
else V.ne_inv (Aeval a1 S) (Aeval a2 S) in
assume_eval a1 Res1 (assume_eval a2 Res2 S)
| LESSEQUAL a1 a2 =>
let (Res1, Res2) :=
if res
then V.le_inv (Aeval a1 S) (Aeval a2 S)
else V.gt_inv (Aeval a1 S) (Aeval a2 S) in
assume_eval a1 Res1 (assume_eval a2 Res2 S)
| NOT b1 =>
assume_test b1 (negb res) S
| AND b1 b2 =>
if res
then assume_test b1 true (assume_test b2 true S)
else ST.join (assume_test b1 false S) (assume_test b2 false S)
end.
Lemma assume_test_sound:
forall b res S s,
beval b s = res -> ST.In s S -> ST.In s (assume_test b res S).
Proof.
induction b; cbn; intros.
- (* TRUE *)
subst res; auto.
- (* FALSE *)
subst res; auto.
- (* EQUAL *)
set (Res := if res
then V.eq_inv (Aeval a1 S) (Aeval a2 S)
else V.ne_inv (Aeval a1 S) (Aeval a2 S)).
assert (A: V.In (aeval a1 s) (fst Res) /\ V.In (aeval a2 s) (snd Res)).
{ unfold Res; destruct res;
[ apply V.eq_inv_1 | apply V.ne_inv_1 ]; auto using Aeval_sound.
- apply Z.eqb_eq; auto.
- apply Z.eqb_neq; auto.
}
destruct A as [A1 A2]. destruct Res as [Res1 Res2]. auto using assume_eval_sound.
- (* LESSEQUAL *)
set (Res := if res
then V.le_inv (Aeval a1 S) (Aeval a2 S)
else V.gt_inv (Aeval a1 S) (Aeval a2 S)).
assert (A: V.In (aeval a1 s) (fst Res) /\ V.In (aeval a2 s) (snd Res)).
{ unfold Res; destruct res;
[ apply V.le_inv_1 | apply V.gt_inv_1 ]; auto using Aeval_sound.
- apply Z.leb_le; auto.
- apply Z.leb_nle in H. lia.
}
destruct A as [A1 A2]. destruct Res as [Res1 Res2]. auto using assume_eval_sound.
- (* NOT *)
apply IHb; auto. rewrite <- H. rewrite negb_involutive; auto.
- (* AND *)
destruct res.
+ (* AND, true *)
destruct (andb_prop _ _ H).
auto.
+ (* AND, false *)
destruct (andb_false_elim _ _ H); [apply ST.join_1 | apply ST.join_2]; auto.
Qed.
(** *** Interprétation abstraite améliorée des commandes *)
(** On ajoute des appels à [assume_test] à chaque fois qu'une condition booléenne
est connue comme vraie ou comme fausse. *)
Fixpoint Cexec (c: com) (S: ST.t) : ST.t :=
match c with
| SKIP => S
| ASSIGN x a => ST.set x (Aeval a S) S
| SEQ c1 c2 => Cexec c2 (Cexec c1 S)
| IFTHENELSE b c1 c2 =>
ST.join (Cexec c1 (assume_test b true S))
(Cexec c2 (assume_test b false S))
| WHILE b c =>
assume_test b false
(postfixpoint (fun X => ST.join S (Cexec c (assume_test b true X))))
end.
Theorem Cexec_sound:
forall c s s' S,
ST.In s S -> cexec s c s' -> ST.In s' (Cexec c S).
Proof.
Opaque niter_down.
induction c; intros s s' S PRE EXEC; cbn.
- (* SKIP *)
inversion EXEC; subst. auto.
- (* ASSIGN *)
inversion EXEC; subst. apply ST.set_1; auto. apply Aeval_sound; auto.
- (* SEQ *)
inversion EXEC; subst. eauto.
- (* IFTHENELSE *)
inversion EXEC; subst. destruct (beval b s) eqn:B.
apply ST.join_1. eapply IHc1; eauto. apply assume_test_sound; auto.
apply ST.join_2. eapply IHc2; eauto. apply assume_test_sound; auto.
- (* WHILE *)
set (F := fun X => ST.join S (Cexec c (assume_test b true X))).
set (X := postfixpoint F).
assert (L : ST.le (F X) X) by (apply postfixpoint_sound).
assert (REC: forall s1 c1 s2,
cexec s1 c1 s2 ->
c1 = WHILE b c ->
ST.In s1 X ->
ST.In s2 (assume_test b false X)).
{ induction 1; intro EQ; inversion EQ; subst; intros.
- (* WHILE done *)
apply assume_test_sound; auto.
- (* WHILE loop *)
apply IHcexec2; auto. apply L. unfold F. apply ST.join_2.
eapply IHc; eauto. apply assume_test_sound; auto.
}
eapply REC; eauto. apply L. unfold F. apply ST.join_1. auto.
Qed.
End Analysis.
(** ** 5.7. Un domaine abstrait des états mémoire *)
(** On reprend le domaine abstrait du fichier [AbstrInterp], section 5.3,
et on lui ajoute l'opérateur d'élargissement et ses propriétés. *)
Module IdentMap := AbstrInterp.IdentMap.
Module IMFact := AbstrInterp.IMFact.
Module IMProp := AbstrInterp.IMProp.
Module StoreAbstr (VA: VALUE_ABSTRACTION) <: STORE_ABSTRACTION.
Module V := VA.
Inductive abstr_state : Type :=
| Bot
| Top_except (m: IdentMap.t V.t).
Definition t := abstr_state.
Definition get (x: ident) (S: t) : V.t :=
match S with
| Bot => V.bot
| Top_except m =>
match IdentMap.find x m with
| None => V.top
| Some v => v
end
end.
Definition In (s: store) (S: t) : Prop :=
forall x, V.In (s x) (get x S).
Definition set (x: ident) (N: V.t) (S: t): t :=
if V.ble N V.bot then Bot else
match S with
| Bot => Bot
| Top_except m => Top_except (IdentMap.add x N m)
end.
Lemma set_1:
forall x n N s S,
V.In n N -> In s S -> In (update x n s) (set x N S).
Proof.
unfold In, get, set; intros.
destruct (V.ble N V.bot) eqn:BLE; [ | destruct S ].
- apply V.ble_1 in BLE. apply BLE in H. elim (V.bot_1 n); auto.
- elim (V.bot_1 (s "")). auto.
- rewrite IMFact.add_o. change IdentMap.E.eq_dec with string_dec. unfold update.
destruct (string_dec x x0); auto.
Qed.
(** L'ordre entre les états abstraits. *)
Definition le (S1 S2: t) : Prop :=
forall s, In s S1 -> In s S2.
Definition ble (S1 S2: t) : bool :=
match S1, S2 with
| Bot, _ => true
| _, Bot => false
| Top_except m1, Top_except m2 =>
IMProp.for_all (fun x v => V.ble (get x S1) v) m2
end.
Lemma ble_1: forall S1 S2, ble S1 S2 = true -> le S1 S2.
Proof.
unfold ble, le; intros.
destruct S1 as [ | m1].
- elim (V.bot_1 (s "")). apply H0.
- destruct S2 as [ | m2]. discriminate.
red; cbn; intros. destruct (IdentMap.find x m2) as [N2|] eqn:F2.
+ apply IdentMap.find_2 in F2. eapply IMProp.for_all_iff in H; eauto.
apply V.ble_1 in H. apply H. apply H0.
hnf. intros; subst x0. hnf; intros. subst x0. auto.
+ apply V.top_1.
Qed.
Lemma ble_false: forall s1 s2,
s2 <> Bot -> ble s1 s2 = false -> exists x, V.ble (get x s1) (get x s2) = false.
Proof.
unfold ble; intros.
destruct s1 as [ | m1]. discriminate. destruct s2 as [ | m2]. congruence.
- set (p := fun (x: IdentMap.key) v => V.ble (get x (Top_except m1)) v) in *.
set (m' := IMProp.filter (fun x v => negb (p x v)) m2).
destruct (IdentMap.elements m') as [ | [x1 v1] l1] eqn:ELT.
+ assert (IMProp.for_all p m2 = true).
{ apply IMProp.for_all_iff.
repeat (hnf; intros). congruence.
intros. destruct (p k e) eqn:P; auto.
assert (M: IdentMap.MapsTo k e m').
{ apply IMProp.filter_iff.
repeat (hnf; intros). congruence.
rewrite P; auto. }
apply IdentMap.elements_1 in M. rewrite ELT in M. inversion M.
}
congruence.
+ assert (M: IdentMap.MapsTo x1 v1 m').
{ apply IdentMap.elements_2. rewrite ELT. constructor. hnf; auto. }
apply IMProp.filter_iff in M. destruct M as [M N]. apply negb_true_iff in N.
exists x1. unfold get at 2. erewrite IdentMap.find_1 by eauto. exact N.
repeat (hnf; intros). congruence.
Qed.
(** Les opérations de treillis. *)
Definition bot: t := Bot.
Lemma bot_1: forall s, ~(In s bot).
Proof.
unfold In; cbn. intros s IN. apply (V.bot_1 (s "")). apply IN.
Qed.
Definition top: t := Top_except (IdentMap.empty V.t).
Lemma top_1: forall s, In s top.
Proof.
unfold In, top, get; cbn. intros. apply V.top_1.
Qed.
Definition join_aux (ov1 ov2: option V.t) : option V.t :=
match ov1, ov2 with
| Some v1, Some v2 => Some (V.join v1 v2)
| _, _ => None
end.
Definition join (S1 S2: t) : t :=
match S1, S2 with
| Bot, _ => S2
| _, Bot => S1
| Top_except m1, Top_except m2 =>
Top_except (IdentMap.map2 join_aux m1 m2)
end.
Lemma join_1:
forall s S1 S2, In s S1 -> In s (join S1 S2).
Proof.
unfold join; intros.
destruct S1 as [ | m1]. elim (bot_1 s); auto.
destruct S2 as [ | m2]. auto.
- red; unfold get; intros. rewrite IMFact.map2_1bis; auto.
unfold join_aux. specialize (H x). unfold get in H.
destruct (IdentMap.find x m1).
+ destruct (IdentMap.find x m2).
* apply V.join_1; auto.
* apply V.top_1.
+ apply V.top_1.
Qed.
Lemma join_2:
forall s S1 S2, In s S2 -> In s (join S1 S2).
Proof.
unfold join; intros.
destruct S1 as [ | m1]. auto.
destruct S2 as [ | m2]. elim (bot_1 s); auto.
- red; unfold get; intros. rewrite IMFact.map2_1bis; auto.
unfold join_aux. specialize (H x). unfold get in H.
destruct (IdentMap.find x m1).
+ destruct (IdentMap.find x m2).
* apply V.join_2; auto.
* apply V.top_1.
+ apply V.top_1.
Qed.
(** L'opérateur d'élargissement. On applique point-à-point
l'élargissement [V.widen] fourni par le domaine des valeurs,
avec des cas par défaut pour les variables non décrites,
qui sont implicitement à [V.top]. *)
Definition widen_aux (ov1 ov2: option V.t) : option V.t :=
match ov1, ov2 with
| Some v1, Some v2 => Some (V.widen v1 v2)
| None, _ => None
| _, None => None
end.
Definition widen (s1 s2: t) : t :=
match s1, s2 with
| Bot, _ => s2
| _, Bot => s1
| Top_except m1, Top_except m2 => Top_except (IdentMap.map2 widen_aux m1 m2)
end.
Lemma widen_1: forall s1 s2, le s1 (widen s1 s2).
Proof.
unfold le, widen; intros.
destruct s1 as [ | m1]. elim (bot_1 _ H).
destruct s2 as [ | m2]. auto.
red; unfold get; intros. specialize (H x); cbn in H.
rewrite IMFact.map2_1bis; auto. unfold widen_aux.
destruct (IdentMap.find x m1); destruct (IdentMap.find x m2);
auto using V.top_1.
apply V.widen_1; auto.
Qed.
(** La construction de l'ordre bien fondé qui garantit la terminaison
est délicate. On commence par définir une mesure à valeurs entières
positives pour une fonction finie [IdentMap.t V.t], qui est la somme
des mesures des valeurs abstraites dans l'image de cette fonction finie. *)
Definition measure_map (m: IdentMap.t V.t) : nat :=
IdentMap.fold (fun x v n => (V.measure v + n)%nat) m 0%nat.
Remark measure_map_empty:
forall m, IdentMap.Empty m -> measure_map m = 0%nat.
Proof.
intros. apply IMProp.fold_Empty; auto.
Qed.
Remark measure_map_add:
forall m x v m', ~IdentMap.In x m -> IMProp.Add x v m m' ->
measure_map m' = (V.measure v + measure_map m)%nat.
Proof.
intros. unfold measure_map; eapply IMProp.fold_Add with (f := fun x v n => (V.measure v + n)%nat); eauto.
repeat (hnf; intros). congruence.
hnf; intros. lia.
Qed.
Remark measure_map_remove:
forall m x,
measure_map m = (V.measure (get x (Top_except m)) + measure_map (IdentMap.remove x m))%nat.
Proof.
intros. unfold get. destruct (IdentMap.find x m) as [v|] eqn:F.
- apply measure_map_add with x.
apply IMFact.not_find_in_iff. rewrite IMFact.remove_eq_o; auto.
red; intros. rewrite IMFact.add_o, IMFact.remove_o.
destruct (AbstrInterp.IdentMap.E.eq_dec x y); congruence.
- rewrite V.measure_top. unfold measure_map. eapply IMProp.fold_Equal. auto.
repeat (hnf; intros). congruence.
hnf; intros; lia.
red; intros. rewrite IMFact.remove_o.
destruct (AbstrInterp.IdentMap.E.eq_dec x y); congruence.
Qed.
Lemma measure_map_le: forall m1 m2,
(forall x, V.measure (get x (Top_except m1)) <= V.measure (get x (Top_except m2)))%nat ->
(measure_map m1 <= measure_map m2)%nat.
Proof.
intros m0. pattern m0. unfold measure_map at 1; apply IMProp.fold_rec.
- intros m EMPTY m2 LE. lia.
- intros x v1 n m' m'' MAP NOTIN ADD REC m2 LE.
set (m2' := IdentMap.remove x m2).
assert (LE': forall x, (V.measure (get x (Top_except m')) <= V.measure (get x (Top_except m2')))%nat).
{ intros y. generalize (LE y). unfold get, m2'. rewrite ADD, IMFact.add_o, IMFact.remove_o.
destruct (AbstrInterp.IdentMap.E.eq_dec x y).
+ subst y. apply IMFact.not_find_in_iff in NOTIN. rewrite NOTIN. rewrite ! V.measure_top. lia.
+ auto. }
apply REC in LE'.
rewrite (measure_map_remove m2 x). fold m2'.
specialize (LE x). unfold get at 1 in LE. rewrite ADD, IMFact.add_eq_o in LE by auto.
lia.
Qed.
Lemma measure_map_lt: forall m1 m2,
(forall x, V.measure (get x (Top_except m1)) <= V.measure (get x (Top_except m2)))%nat ->
(exists x, V.measure (get x (Top_except m1)) < V.measure (get x (Top_except m2)))%nat ->
(measure_map m1 < measure_map m2)%nat.
Proof.
intros m1 m2 LE (x & LT).
rewrite (measure_map_remove m1 x), (measure_map_remove m2 x).
assert ((measure_map (IdentMap.remove x m1) <= measure_map (IdentMap.remove x m2))%nat).
{ apply measure_map_le.
intros y; unfold get. rewrite ! IMFact.remove_o.
destruct (AbstrInterp.IdentMap.E.eq_dec x y).
lia.
apply LE. }
lia.
Qed.
(** On montre ensuite que cette mesure décroît strictement lors d'une étape
d'élargissement qui n'implique pas [Bot]. *)
Lemma measure_widen_lt: forall m1 m2,
ble (Top_except m2) (Top_except m1) = false ->
(measure_map (IdentMap.map2 widen_aux m1 m2) < measure_map m1)%nat.
Proof.
intros. apply ble_false in H. 2: congruence. destruct H as (x & BL).
apply measure_map_lt.
- intros y. unfold get. rewrite IMFact.map2_1bis by auto. unfold widen_aux.
destruct (IdentMap.find y m1) as [ v1 |].
destruct (IdentMap.find y m2) as [ v2 |].
apply V.widen_2.
rewrite V.measure_top; lia.
rewrite V.measure_top; lia.
- exists x. unfold get in *. rewrite IMFact.map2_1bis by auto. unfold widen_aux.
destruct (IdentMap.find x m1) as [ v1 |].
destruct (IdentMap.find x m2) as [ v2 |].
apply V.widen_3 in BL; auto.
apply V.widen_3 in BL; rewrite V.measure_top; lia.
assert (T: forall z, V.ble z V.top = true).
{ intros. apply V.ble_2. red; intros. apply V.top_1. }
rewrite T in BL. congruence.
Qed.
(** On en déduit que l'ordre d'élargissement est bien fondé. *)
Definition widen_order (S S1: t) := exists S2, S = widen S1 S2 /\ ble S2 S1 = false.
Lemma widen_order_wf: well_founded widen_order.
Proof.
assert (A: forall m, Acc widen_order (Top_except m)).
{ induction m using (well_founded_ind (well_founded_ltof _ measure_map)).
constructor. intros S (S2 & EQ & BLE). subst S.
destruct S2. discriminate. apply H. apply measure_widen_lt. auto. }
assert (B: Acc widen_order Bot).
{ constructor. intros S (S2 & EQ & BLE). subst S.
unfold ble in BLE. destruct S2. discriminate. apply A. }
red. destruct a; auto.
Defined.
End StoreAbstr.
(** ** 5.8. Le domaine abstrait des intervalles *)
(** Nous définissons d'abord le type [zinf] des entiers relatifs
complétés par "plus l'infini". *)
Inductive zinf : Type := Fin (h: Z) | Inf.
Coercion Fin : Z >-> zinf.
Module Zinf.
Definition In (n: Z) (N: zinf) : Prop :=
match N with Fin h => n <= h | Inf => True end.
Lemma In_mono: forall n1 n2 N, n1 <= n2 -> In n2 N -> In n1 N.
Proof.
unfold In; destruct N; intros. lia. auto.
Qed.
Definition le (N1 N2: zinf) : Prop :=
forall n, In n N1 -> In n N2.
Lemma le_Fin: forall n1 N2, le (Fin n1) N2 <-> In n1 N2.
Proof.
unfold le; cbn; intros; split; intros.
- apply H. lia.
- destruct N2; cbn in *; auto. lia.
Qed.
Lemma le_is_Inf: forall N h, (forall n, h <= n -> In n N) -> N = Inf.
Proof.
destruct N; cbn; intros; auto.
specialize (H (Z.max h0 (h + 1))). lia.
Qed.
Lemma le_Inf: forall N, le Inf N <-> N = Inf.
Proof.
unfold le; intros; split; intros.
- apply le_is_Inf with 0. intros; apply H; exact I.
- subst N; exact I.
Qed.
Definition ble (N1 N2: zinf) : bool :=
match N1, N2 with _, Inf => true | Inf, _ => false | Fin h1, Fin h2 => h1 <=? h2 end.
Lemma ble_1: forall N1 N2, ble N1 N2 = true -> le N1 N2.
Proof.
unfold ble, le, In; intros.
destruct N1, N2; auto.
apply Z.leb_le in H. lia.
discriminate.
Qed.
Lemma ble_2: forall N1 N2, le N1 N2 -> ble N1 N2 = true.
Proof.
unfold ble; intros. destruct N1.
- apply le_Fin in H. destruct N2; auto. apply Z.leb_le; auto.
- apply le_Inf in H. rewrite H. auto.
Qed.
Definition max (N1 N2: zinf) : zinf :=
match N1, N2 with Inf, _ => Inf | _, Inf => Inf | Fin h1, Fin h2 => Fin (Z.max h1 h2) end.
Lemma max_1: forall n N1 N2, In n N1 -> In n (max N1 N2).
Proof.
unfold In, max; intros. destruct N1; auto. destruct N2; auto. lia.
Qed.
Lemma max_2: forall n N1 N2, In n N2 -> In n (max N1 N2).
Proof.
unfold In, max; intros. destruct N1; auto. destruct N2; auto. lia.
Qed.
Definition min (N1 N2: zinf) : zinf :=
match N1, N2 with Inf, _ => N2 | _, Inf => N1 | Fin h1, Fin h2 => Fin (Z.min h1 h2) end.
Lemma min_1: forall n N1 N2, In n N1 -> In n N2 -> In n (min N1 N2).
Proof.
unfold In, min; intros. destruct N1; auto. destruct N2; auto. lia.
Qed.
Definition add (N1 N2: zinf) : zinf :=
match N1, N2 with Inf, _ => Inf | _, Inf => Inf | Fin h1, Fin h2 => Fin (h1 + h2) end.
Lemma add_1: forall n1 n2 N1 N2, In n1 N1 -> In n2 N2 -> In (n1 + n2) (add N1 N2).
Proof.
unfold In, add; intros. destruct N1; auto. destruct N2; auto. lia.
Qed.
Definition isIn (n: Z) (N: zinf) : bool :=
match N with Fin h => n <=? h | Inf => true end.
Lemma isIn_1:
forall n N, In n N -> isIn n N = true.
Proof.
unfold In, isIn; intros. destruct N; auto. apply Z.leb_le; auto.
Qed.
Definition pred (N: zinf) : zinf :=
match N with Inf => Inf | Fin n => Fin (n - 1) end.
Lemma pred_1: forall n N, In n N -> In (n - 1) (pred N).
Proof.
unfold pred, In; intros; destruct N; auto. lia.
Qed.
(** On définit l'élargissement entre deux entiers possiblement infinis comme suit:
si l'entier augmente strictement, on part à l'infini, sinon on conserve le premier entier. *)
Definition widen (N1 N2: zinf) : zinf :=
if ble N2 N1 then N1 else Inf.
Lemma widen_1: forall N1 N2, le N1 (widen N1 N2).
Proof.
unfold widen; intros. destruct (ble N2 N1) eqn:LE.
red; auto.
red; unfold In; auto.
Qed.
Definition measure (N: zinf) : nat :=
match N with Inf => 0%nat | Fin _ => 1%nat end.
Lemma measure_1: forall N, (measure N <= 1)%nat.
Proof.
destruct N; cbn; lia.
Qed.
Lemma widen_2:
forall N1 N2, (measure (widen N1 N2) <= measure N1)%nat.
Proof.
intros. unfold widen. destruct (ble N2 N1) eqn:BLE.
- lia.
- destruct N1. cbn; lia. destruct N2; discriminate.
Qed.
Lemma widen_3:
forall N1 N2, ble N2 N1 = false -> (measure (widen N1 N2) < measure N1)%nat.
Proof.
destruct N1, N2; cbn; intros; auto; try discriminate.
unfold widen. cbn. rewrite H. cbn. lia.
Qed.
End Zinf.
(** Un intervalle est représenté par une paire de [zinf].
Le second est la borne supérieure, le premier est l'opposé de la borne inférieure.
Cette astuce de représentation permet de n'avoir qu'un infini [Inf],
au lieu d'avoir un infini négatif pour les bornes inférieures
et un infini positif pour les bornes supérieures. *)
Module Intervals <: VALUE_ABSTRACTION.
(** Le type des valeurs abstraites. *)
Record interval : Type := intv { low: zinf; high: zinf }.
Definition t := interval.
(** L'appartenance: [n] doit être en dessous de la borne supérieure,
et l'opposé de [n] en dessous de l'opposé de la borne inférieure. *)
Definition In (n: Z) (N: t) : Prop :=
Zinf.In n (high N) /\ Zinf.In (-n) (low N).
Definition le (N1 N2: t) : Prop :=
forall n, In n N1 -> In n N2.
(** Teste si un intervalle est vide. *)
Definition isempty (N: t) : bool :=
match N with
| {| low := Fin l; high := Fin h |} => h <? (-l)
| _ => false
end.
Lemma isempty_1: forall n N, isempty N = true -> In n N -> False.
Proof.
unfold isempty, In; intros. destruct N as [[l|] [h|]]; try discriminate.
apply Z.ltb_lt in H. cbn in H0. lia.
Qed.
Lemma isempty_2: forall N, isempty N = false -> exists n, In n N.
Proof.
unfold isempty, In; intros. destruct N as [[l|] [h|]]; cbn.
- apply Z.ltb_ge in H. exists h; lia.
- exists (- l); lia.
- exists h; lia.
- exists 0; auto.
Qed.
Lemma nonempty_le: forall N1 N2,
le N1 N2 -> isempty N1 = false -> (Zinf.le (high N1) (high N2) /\ Zinf.le (low N1) (low N2)).
Proof.
unfold le, In, isempty; intros.
destruct N1 as [[l1 |] [h1|]]; cbn in *; rewrite ? Zinf.le_Fin, ? Zinf.le_Inf.
- apply Z.ltb_ge in H0. split.
+ apply H; lia.
+ replace l1 with (- - l1) by lia. apply H. lia.
- split.
+ apply Zinf.le_is_Inf with (-l1). intros; apply H. intuition lia.
+ replace l1 with (- - l1) by lia. apply H. intuition lia.
- split.
+ apply H. intuition lia.
+ apply Zinf.le_is_Inf with(- h1).
intros. replace n with (- - n) by lia. apply H. intuition lia.
- split; apply Zinf.le_is_Inf with 0; intros.
+ apply H; auto.
+ replace n with (- - n) by lia. apply H. auto.
Qed.
(** [ble] est une fonction à valeurs booléennes qui décide la relation [le]. *)
Definition ble (N1 N2: t) : bool :=
isempty N1 || (Zinf.ble (high N1) (high N2) && Zinf.ble (low N1) (low N2)).
Lemma ble_1: forall N1 N2, ble N1 N2 = true -> le N1 N2.
Proof.
unfold ble, le, In; intros.
destruct (isempty N1) eqn:E.
elim (isempty_1 _ _ E H0).
apply andb_prop in H. destruct H as [B1 B2].
apply Zinf.ble_1 in B1. apply Zinf.ble_1 in B2.
intuition.
Qed.
Lemma ble_2: forall N1 N2, le N1 N2 -> ble N1 N2 = true.
Proof.
unfold ble; intros. destruct (isempty N1) eqn:E; auto.
destruct (nonempty_le N1 N2) as [P Q]; auto.
apply andb_true_intro; split; apply Zinf.ble_2; auto.
Qed.
(** [const n] est la valeur abstraite pour l'ensemble singleton [{n}]. *)
Definition const (n: Z) : t := {| low := Fin (-n); high := Fin n |}.
Lemma const_1: forall n, In n (const n).
Proof.
unfold const, In, Zinf.In; intros; cbn. lia.
Qed.
(** [bot] représente l'ensemble vide. *)
Definition bot: t := {| low := Fin 0; high := Fin (-1) |}.
Lemma bot_1: forall n, ~(In n bot).
Proof.
unfold bot, In, Zinf.In; intros; cbn. lia.
Qed.
(** [top] représente l'ensemble de tous les entiers. *)
Definition top: t := {| low := Inf; high := Inf |}.
Lemma top_1: forall n, In n top.
Proof.
intros. split; exact I.
Qed.
(** [join] calcule un majorant de ses deux arguments. *)
Definition join (N1 N2: t) : t :=
{| low := Zinf.max (low N1) (low N2);
high := Zinf.max (high N1) (high N2) |}.
Lemma join_1:
forall n N1 N2, In n N1 -> In n (join N1 N2).
Proof.
unfold In, join; intros; cbn. split; apply Zinf.max_1; tauto.
Qed.
Lemma join_2:
forall n N1 N2, In n N2 -> In n (join N1 N2).
Proof.
unfold In, join; intros; cbn. split; apply Zinf.max_2; tauto.
Qed.
(** Les opérateurs abstraits correspondant à l'addition et à la soustraction. *)
Definition add (N1 N2: t) : t :=
if isempty N1 || isempty N2 then bot else
{| low := Zinf.add (low N1) (low N2);
high := Zinf.add (high N1) (high N2) |}.
Lemma add_1:
forall n1 n2 N1 N2, In n1 N1 -> In n2 N2 -> In (n1 + n2) (add N1 N2).
Proof.
unfold add; intros.
destruct (isempty N1) eqn:E1. elim (isempty_1 n1 N1); auto.
destruct (isempty N2) eqn:E2. elim (isempty_1 n2 N2); auto.
destruct H; destruct H0; split; cbn.
apply Zinf.add_1; auto.
replace (- (n1 + n2)) with ((-n1) + (-n2)) by lia. apply Zinf.add_1; auto.
Qed.