-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmultiset.v
2034 lines (1831 loc) · 61.4 KB
/
multiset.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 Utf8_core Lia.
Require Import FMapInterface.
Require Import FMapFacts.
Require Import FMapAVL.
Require Import OrderedType.
Require Import FunInd.
Require Import basic multiset_spec.
Module PreMake(X:OrderedType)(Maps:FMapInterface.S with Module E:=X) <: S(X).
Module MapsFact := WFacts(Maps).
Local Notation A := X.t.
Definition t := Maps.t nat.
Definition empty : t := Maps.empty nat.
Definition is_empty : t -> bool := @Maps.is_empty nat.
Definition add_multiple : A -> nat -> t -> t := fun a n ms =>
match Maps.find a ms with
| Some v => Maps.add a (S (plus n v)) ms
| None => Maps.add a n ms
end.
Fixpoint iter (B:Type) (f:A -> B -> B) (k:A) (v:nat) (acc:B) {struct v} : B :=
match v with
| 0 => f k acc
| S n => f k (iter B f k n acc)
end.
Definition fold (B:Type) (f:A -> B -> B) ms v0 :=
Maps.fold (iter B f) ms v0.
(* Definition add : A -> t -> t := fun a ms => add_multiple a 1 ms. *)
Definition add : A -> t -> t := fun a ms =>
match Maps.find a ms with
| Some v => Maps.add a (S v) ms
| None => Maps.add a 0 ms
end.
Lemma add_add_multiple : forall a ms, add a ms = add_multiple a 0 ms.
Proof.
intros a ms.
unfold add,add_multiple.
simpl.
reflexivity.
Qed.
(* remove a ms retire UNE occurrence de a dans ms *)
Definition remove : A -> t -> t :=
fun a ms =>
match Maps.find a ms with
| Some 0 => Maps.remove a ms
| Some (S v) => (Maps.add a v ms)
| None => ms
end.
Definition mem : A -> t -> bool := @Maps.mem nat.
Definition eq_bool : t -> t -> bool := Maps.equal nat_eq_bool.
Definition eq : t -> t -> Prop := @Maps.Equal nat.
Definition eq_refl : forall ms, eq ms ms := @MapsFact.Equal_refl nat.
Definition eq_sym : forall ms ms', eq ms ms' -> eq ms' ms := @MapsFact.Equal_sym nat.
Definition eq_trans : forall ms1 ms2 ms3, eq ms1 ms2 -> eq ms2 ms3 -> eq ms1 ms3 := @MapsFact.Equal_trans nat.
Lemma add_morph_eq : forall a a', X.eq a a' -> forall ms ms', eq ms ms' -> eq (add a ms) (add a' ms').
Proof.
intros a a' H ms ms' H0.
unfold eq,add in *.
rewrite H0.
rewrite H.
destruct (Maps.find a' ms').
rewrite H0.
rewrite H.
reflexivity.
rewrite H0.
rewrite H.
reflexivity.
Qed.
Definition union : t -> t -> t := fun ms1 ms2 =>
Maps.fold add_multiple ms1 ms2.
Module MapsPtes := FMapFacts.Properties(Maps).
Add Morphism add_multiple with signature X.eq ==> (@Logic.eq nat) ==> Maps.Equal ==> Maps.Equal as add_multiple_morph.
Proof.
intros x y H y0 x0 y1 H0.
unfold add_multiple.
rewrite H.
rewrite H0.
destruct (Maps.find y y1); rewrite H; rewrite H0;reflexivity.
Qed.
Lemma add_comm_aux :
forall (B : Type)
(k : A)
(k' : A)
(v : B)
(v' : B)
(m : Maps.t B)
(H : ~ X.eq k k')
(alpha : Maps.key)
(e : B),
Maps.MapsTo alpha e (Maps.add k v (Maps.add k' v' m)) ->
Maps.MapsTo alpha e (Maps.add k' v' (Maps.add k v m)).
Proof.
intros B k k' v v' m H alpha e H0.
case_eq (X.eq_dec alpha k);intros alpha_eq_k _.
rewrite alpha_eq_k in H0|-*.
clear alpha_eq_k.
assert(H1:Maps.MapsTo k v (Maps.add k v (Maps.add k' v' m))).
apply Maps.add_1.
reflexivity.
assert (h1:=MapsPtes.F.MapsTo_fun H0 H1).
subst.
apply Maps.add_2.
intros abs.
symmetry in abs.
elim H.
rewrite abs.
reflexivity.
apply Maps.add_1.
reflexivity.
case_eq (X.eq_dec alpha k');intros alpha_eq_k' _.
rewrite alpha_eq_k' in H0|-*.
clear alpha_eq_k'.
apply Maps.add_3 in H0;[ | assumption].
assert(H1:Maps.MapsTo k' v' (Maps.add k' v' m)).
apply Maps.add_1.
reflexivity.
assert (h1:=MapsPtes.F.MapsTo_fun H0 H1).
subst.
apply Maps.add_1.
reflexivity.
apply Maps.add_2.
intros abs.
symmetry in abs.
elim alpha_eq_k'.
assumption.
apply Maps.add_2.
intros abs.
symmetry in abs.
elim alpha_eq_k.
assumption.
apply Maps.add_3 in H0.
apply Maps.add_3 in H0.
assumption.
intros abs.
symmetry in abs.
elim alpha_eq_k'.
assumption.
intros abs.
symmetry in abs.
elim alpha_eq_k.
assumption.
Qed.
Lemma add_comm' : forall B k k' (v v':B) m ,
~ X.eq k k' ->
Maps.Equal (Maps.add k v (Maps.add k' v' m))
(Maps.add k' v' (Maps.add k v m)).
Proof.
intros B k k' v v' m H.
rewrite MapsPtes.F.Equal_mapsto_iff.
intros alpha e.
split;intros H1.
apply add_comm_aux;assumption.
apply add_comm_aux;try assumption.
intros abs.
symmetry in abs.
elim H.
assumption.
Qed.
Lemma transpose_neqkey_equal_add_multiple : MapsPtes.transpose_neqkey Maps.Equal add_multiple.
Proof.
red.
intros k k' e e' a H.
unfold add_multiple.
case_eq (Maps.find k' a);case_eq (Maps.find k a).
- intros n2 Heq2.
intros n1 Heq1.
rewrite MapsPtes.F.add_neq_o;[ | intro abs;elim H;symmetry;assumption ].
rewrite MapsPtes.F.add_neq_o;[ | assumption].
rewrite Heq1;rewrite Heq2.
apply add_comm'.
assumption.
- intros Heq2.
intros n1 Heq1.
rewrite MapsPtes.F.add_neq_o;[ | intro abs;elim H;symmetry;assumption ].
rewrite MapsPtes.F.add_neq_o;[ | assumption].
rewrite Heq1;rewrite Heq2.
apply add_comm'.
assumption.
- intros n2 Heq2.
intros Heq1.
rewrite MapsPtes.F.add_neq_o;[ | intro abs;elim H;symmetry;assumption ].
rewrite MapsPtes.F.add_neq_o;[ | assumption].
rewrite Heq1;rewrite Heq2.
apply add_comm'.
assumption.
- intros Heq2.
intros Heq1.
rewrite MapsPtes.F.add_neq_o;[ | intro abs;elim H;symmetry;assumption ].
rewrite MapsPtes.F.add_neq_o;[ | assumption].
rewrite Heq1;rewrite Heq2.
apply add_comm'.
assumption.
Qed.
Lemma union_morph_eq : forall a a', eq a a' -> forall ms ms', eq ms ms' -> eq (union a ms) (union a' ms').
Proof. (* fold_rec *)
intros a a' H ms ms' H0.
revert a' H ms' H0.
unfold union,eq.
pattern a,(Maps.fold add_multiple a ms).
apply MapsPtes.fold_rec_bis.
- intros m m' a0 H H0 a' H1 ms' H2.
apply H0.
+ rewrite H;exact H1.
+ assumption.
- intros a' H ms' H0.
rewrite MapsPtes.fold_Empty.
+ assumption.
+ auto with *.
+ clear -H.
unfold Maps.Equal, Maps.Empty in *.
intros a e.
intros abs.
rewrite MapsPtes.F.find_mapsto_iff in abs.
generalize (H a).
rewrite abs.
rewrite MapsPtes.F.empty_o.
discriminate.
- intros k e a0 m' H H0 H1 a' H2 ms' H3.
rewrite <- MapsPtes.fold_Equal.
5:{ eexact H2. }
+ rewrite MapsPtes.fold_add.
* rewrite H1.
-- reflexivity.
-- reflexivity.
-- assumption.
* auto with *.
* apply add_multiple_morph_Proper.
* apply transpose_neqkey_equal_add_multiple.
* apply H0.
+ auto with *.
+ apply add_multiple_morph_Proper.
+ apply transpose_neqkey_equal_add_multiple.
Qed.
Lemma is_empty_empty : is_empty empty = true.
Proof.
unfold is_empty,empty.
rewrite <- MapsPtes.F.is_empty_iff.
apply Maps.empty_1.
Qed.
Lemma is_empty_no_mem : forall ms, is_empty ms = true <-> (forall a, mem a ms = false).
Proof.
intros ms.
unfold is_empty,mem.
rewrite <- MapsPtes.F.is_empty_iff.
unfold Maps.Empty.
split;intros H a.
rewrite MapsFact.mem_find_b.
case_eq (Maps.find a ms).
intros n abs;elim (H a n).
rewrite (MapsPtes.F.find_mapsto_iff);assumption.
reflexivity.
intros v abs.
rewrite (MapsPtes.F.find_mapsto_iff) in abs.
assert (H1:=H a).
generalize (MapsFact.mem_find_b ms a).
rewrite abs.
rewrite H1;discriminate.
Qed.
Lemma add_is_not_empty : forall a ms, is_empty (add a ms) = false.
Proof.
unfold is_empty, add,add_multiple.
intros a ms.
destruct (Maps.find a ms).
assert (H:~ (Maps.Empty (Maps.add a (S n) ms))).
intros abs;unfold Maps.Empty in abs.
apply (abs a (S n)).
rewrite MapsPtes.F.add_mapsto_iff.
now left.
rewrite MapsPtes.F.is_empty_iff in H.
destruct (Maps.is_empty (Maps.add a (S n) ms)).
elim H;reflexivity.
reflexivity.
assert (H:~ (Maps.Empty (Maps.add a 0 ms))).
intros abs;unfold Maps.Empty in abs.
apply (abs a 0).
rewrite MapsPtes.F.add_mapsto_iff.
now left.
rewrite MapsPtes.F.is_empty_iff in H.
destruct (Maps.is_empty (Maps.add a 0 ms)).
elim H;reflexivity.
reflexivity.
Qed.
Lemma add_is_mem : forall a b ms, X.eq a b -> mem a (add b ms) = true.
Proof.
unfold mem,add,add_multiple.
intros a b ms Heq.
now destruct (Maps.find b ms); apply MapsPtes.F.add_eq_b.
Qed.
Lemma mem_destruct : forall a b ms, mem a (add b ms) = true -> X.eq a b \/ mem a ms = true.
Proof.
intros a b ms.
case (X.eq_dec a b).
- auto.
- unfold mem,add,add_multiple.
intros neq.
destruct (Maps.find b ms); rewrite MapsPtes.F.add_neq_b; auto with relations.
Qed.
Lemma mem_add_is_mem : forall a b ms, mem a ms = true -> mem a (add b ms) = true.
Proof.
intros a b ms.
case (X.eq_dec a b).
intros e H.
apply add_is_mem;assumption.
unfold mem,add,add_multiple.
intros n H.
destruct (Maps.find b ms); rewrite MapsPtes.F.add_neq_b; auto with relations.
Qed.
Lemma add_multiple_comm :
forall a v1 b v2 ms,
eq
(add_multiple a v1 (add_multiple b v2 ms))
(add_multiple b v2 (add_multiple a v1 ms)).
Proof.
unfold eq,add_multiple.
intros a v1 b v2 ms.
case (X.eq_dec a b);intros a_eq_b.
- rewrite a_eq_b.
case_eq (Maps.find b ms);[intro n1 |];intro Heq1.
+ rewrite MapsPtes.F.add_eq_o;[ | reflexivity].
rewrite a_eq_b.
rewrite MapsPtes.F.add_eq_o;[ | reflexivity].
rewrite a_eq_b.
rewrite Heq1.
rewrite a_eq_b.
intros k.
case (X.eq_dec k b);intros Heq2.
* rewrite Heq2.
rewrite MapsPtes.F.add_eq_o.
-- rewrite MapsPtes.F.add_eq_o.
++ f_equal;lia.
++ reflexivity.
-- reflexivity.
* do 4 (rewrite MapsPtes.F.add_neq_o;[|intros abs;elim Heq2;rewrite abs;reflexivity]).
reflexivity.
+ rewrite a_eq_b.
rewrite MapsPtes.F.add_eq_o.
* rewrite MapsPtes.F.add_eq_o.
-- rewrite a_eq_b.
rewrite Heq1.
rewrite a_eq_b.
intro k.
case (X.eq_dec k b);intros Heq2.
++ rewrite Heq2.
rewrite MapsPtes.F.add_eq_o.
** rewrite MapsPtes.F.add_eq_o.
--- f_equal; lia.
--- reflexivity.
** reflexivity.
++ do 4 (rewrite MapsPtes.F.add_neq_o;[|intros abs;elim Heq2;rewrite abs;reflexivity]).
reflexivity.
-- reflexivity.
* reflexivity.
- case_eq (Maps.find b ms);[intro n1 |];intro Heq1.
+ rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim a_eq_b;rewrite abs;reflexivity].
case_eq (Maps.find a ms);[intro n2 |];intro Heq2.
* rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim a_eq_b;rewrite abs;reflexivity].
rewrite Heq1.
intros k.
case(X.eq_dec k a);intros Heq3.
-- rewrite Heq3.
rewrite MapsPtes.F.add_eq_o;[ | reflexivity].
rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim a_eq_b;rewrite abs;reflexivity].
rewrite MapsPtes.F.add_eq_o;[ | reflexivity].
reflexivity.
-- rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq3;rewrite abs;reflexivity].
case(X.eq_dec k b);intros Heq4.
++ rewrite Heq4.
repeat (rewrite MapsPtes.F.add_eq_o;[ | reflexivity]).
reflexivity.
++ do 2 (rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq4;rewrite abs;reflexivity]).
rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq3;rewrite abs;reflexivity].
reflexivity.
* rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim a_eq_b;rewrite abs;reflexivity].
rewrite Heq1.
intro k.
case(X.eq_dec k a);intros Heq3.
-- rewrite Heq3.
rewrite MapsPtes.F.add_eq_o;[ | reflexivity].
rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim a_eq_b;rewrite abs;reflexivity].
rewrite MapsPtes.F.add_eq_o;[ | reflexivity].
reflexivity.
-- rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq3;rewrite abs;reflexivity].
case(X.eq_dec k b);intros Heq4.
++ rewrite Heq4.
repeat (rewrite MapsPtes.F.add_eq_o;[ | reflexivity]).
reflexivity.
++ do 2 (rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq4;rewrite abs;reflexivity]).
rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq3;rewrite abs;reflexivity].
reflexivity.
+ rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim a_eq_b;rewrite abs;reflexivity].
case_eq (Maps.find a ms);[intro n2 |];intro Heq2.
* rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim a_eq_b;rewrite abs;reflexivity].
rewrite Heq1.
intro k.
case(X.eq_dec k a);intros Heq3.
-- rewrite Heq3.
rewrite MapsPtes.F.add_eq_o;[ | reflexivity].
rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim a_eq_b;rewrite abs;reflexivity].
rewrite MapsPtes.F.add_eq_o;[ | reflexivity].
reflexivity.
-- rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq3;rewrite abs;reflexivity].
case(X.eq_dec k b);intros Heq4.
++ rewrite Heq4.
repeat (rewrite MapsPtes.F.add_eq_o;[ | reflexivity]).
reflexivity.
++ do 2 (rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq4;rewrite abs;reflexivity]).
rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq3;rewrite abs;reflexivity].
reflexivity.
* rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim a_eq_b;rewrite abs;reflexivity].
rewrite Heq1.
intro k.
case(X.eq_dec k a);intros Heq3.
-- rewrite Heq3.
rewrite MapsPtes.F.add_eq_o;[ | reflexivity].
rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim a_eq_b;rewrite abs;reflexivity].
rewrite MapsPtes.F.add_eq_o;[ | reflexivity].
reflexivity.
-- rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq3;rewrite abs;reflexivity].
case(X.eq_dec k b);intros Heq4.
++ rewrite Heq4.
repeat (rewrite MapsPtes.F.add_eq_o;[ | reflexivity]).
reflexivity.
++ do 2 (rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq4;rewrite abs;reflexivity]).
rewrite MapsPtes.F.add_neq_o;[ | intros abs;elim Heq3;rewrite abs;reflexivity].
reflexivity.
Qed.
Lemma add_comm : forall a b ms, eq (add a (add b ms)) (add b (add a ms)).
Proof.
intros a b ms.
rewrite add_add_multiple.
rewrite add_add_multiple.
rewrite add_add_multiple.
rewrite add_add_multiple.
apply add_multiple_comm.
Qed.
Lemma mem_add_comm : forall a b ms, mem a ms = true -> mem a (add b ms) = true.
Proof.
unfold mem,add,add_multiple.
intros a b ms H.
destruct (Maps.find b ms).
case (X.eq_dec b a);intro b_eq_a.
rewrite b_eq_a.
apply MapsPtes.F.add_eq_b;reflexivity.
rewrite MapsPtes.F.add_neq_b; assumption.
case (X.eq_dec b a);intro b_eq_a.
rewrite b_eq_a.
apply MapsPtes.F.add_eq_b;reflexivity.
rewrite MapsPtes.F.add_neq_b; assumption.
Qed.
Lemma union_empty_left : forall ms, eq (union empty ms) ms.
Proof.
intros ms.
unfold eq,union,empty.
apply MapsPtes.fold_Empty.
auto with *.
apply Maps.empty_1.
Qed.
Lemma fold_pseudo_morph :
forall
(f : Maps.key -> nat -> t -> t)
(f_morph:
forall k k' n ms ms',
X.eq k k' ->
Maps.Equal ms ms' ->
Maps.Equal (f k n ms) (f k n ms'))
(f_proper : (Proper (X.eq ==> Logic.eq ==> Maps.Equal ==> Maps.Equal) f))
(f_transpose:MapsPtes.transpose_neqkey Maps.Equal f )
(* (f_in : forall k e a, Maps.find k (f k e a) = Some e) *)
(f_in' : forall k e a k', ~X.eq k k' -> Maps.find k' (f k e a) = Maps.find k' a)
(ms1 : Maps.t nat)
(ms1' : Maps.t nat)
(H1 : Maps.Equal ms1 ms1')
(ms2 : Maps.t nat)
(ms2' : Maps.t nat)
(H2 : Maps.Equal ms2 ms2'),
Maps.Equal (Maps.fold f ms1 ms2) (Maps.fold f ms1' ms2').
Proof.
intros f f_morph f_proper f_transpose (* f_in *) f_in' ms1 ms1' H1 ms2 ms2' H2.
revert ms1' H1 ms2' H2.
pattern ms1,(Maps.fold f ms1 ms2).
apply MapsPtes.fold_rec.
- intros m H ms1' H1 ms2' H2.
rewrite MapsPtes.fold_Empty.
+ assumption.
+ auto with *.
+ rewrite <- H1.
assumption.
- intros k e a m' m'' H H0 H1 H2 ms1' H3 ms2' H4.
intros k'.
case (X.eq_dec k k');intros k_eq_k'.
+ rewrite <- k_eq_k' in *.
assert (Equivalence (@Maps.Equal nat)) by auto with *.
rewrite (@MapsPtes.fold_Add nat _ (@Maps.Equal nat) H5 f f_proper f_transpose m' ms1' k e);trivial .
* apply (f_morph k k' e a (Maps.fold f m' ms2'));trivial.
apply H2;trivial.
reflexivity.
* intro k''.
rewrite <- H3.
apply H1.
+ rewrite f_in'.
* assert(MapsPtes.Add k e m' ms1').
{ intro k''.
rewrite <- H3.
apply H1. }
rewrite MapsPtes.fold_Add.
6:{ eexact H5. }
-- rewrite f_in'.
++ apply H2.
** reflexivity.
** assumption.
++ assumption.
-- auto with *.
-- assumption.
-- assumption.
-- assumption.
* assumption.
Qed.
Lemma empty_no_mem : forall a, mem a empty = false.
Proof.
unfold mem.
apply MapsPtes.F.empty_a.
Qed.
Lemma union_empty_right : forall ms, eq (union ms empty) ms.
Proof.
intros ms.
unfold eq,union,empty.
assert (morph:
forall (ms1 : Maps.t nat)
(ms1' : Maps.t nat)
(H1 : Maps.Equal ms1 ms1')
(ms2 : Maps.t nat)
(ms2' : Maps.t nat)
(H2 : Maps.Equal ms2 ms2'),
Maps.Equal (Maps.fold add_multiple ms1 ms2) (Maps.fold add_multiple ms1' ms2')).
apply fold_pseudo_morph.
intros k k' n ms0 ms'0 H H0.
now apply add_multiple_morph.
apply add_multiple_morph_Proper.
apply transpose_neqkey_equal_add_multiple.
intros k e a k' H.
unfold add_multiple.
destruct (Maps.find k a).
apply MapsPtes.F.add_neq_o;trivial.
apply MapsPtes.F.add_neq_o;trivial.
pattern ms,(Maps.fold add_multiple ms (Maps.empty nat)).
apply MapsPtes.fold_rec_bis.
intros.
transitivity m;assumption.
reflexivity.
intros k e a m' H H0 H1.
unfold add_multiple.
case_eq (Maps.find k a);[intros n|];intro Heq.
rewrite MapsPtes.F.not_find_in_iff in H0.
rewrite H1 in Heq.
rewrite Heq in H0;discriminate.
rewrite H1;reflexivity.
Qed.
Lemma Empty_not_find :
forall elt a (m:Maps.t elt),
Maps.Empty m -> Maps.find a m = (@None elt).
Proof.
intros elt a m H.
red in H.
case_eq (Maps.find a m);[intro e|];intro Heq.
elim (H a e).
rewrite MapsPtes.F.find_mapsto_iff;assumption.
reflexivity.
Qed.
Lemma add_multiple_multiple : forall a v1 v2 m,
Maps.Equal (add_multiple a v1 (add_multiple a v2 m)) (add_multiple a (S (v1+v2)) m).
Proof.
unfold add_multiple.
intros a v1 v2 m.
case_eq (Maps.find a m);[intro n| ];intro Heq1.
rewrite MapsPtes.F.add_eq_o;[|reflexivity].
intros k.
case(X.eq_dec k a);intro Heq2.
rewrite MapsPtes.F.add_eq_o;[|symmetry;assumption].
rewrite MapsPtes.F.add_eq_o;[|symmetry;assumption].
f_equal;lia.
do 3 (rewrite MapsPtes.F.add_neq_o;[|intros abs;elim Heq2;rewrite abs;reflexivity]).
reflexivity.
rewrite MapsPtes.F.add_eq_o;[|reflexivity].
intro k.
case(X.eq_dec k a);intro Heq2.
rewrite MapsPtes.F.add_eq_o;[|symmetry;assumption].
rewrite MapsPtes.F.add_eq_o;[|symmetry;assumption].
reflexivity.
do 3 (rewrite MapsPtes.F.add_neq_o;[|intros abs;elim Heq2;rewrite abs;reflexivity]).
reflexivity.
Qed.
Lemma add_add_add:
forall k k' v v' (m:t),
X.eq k k' ->
Maps.Equal (Maps.add k v (Maps.add k' v' m)) (Maps.add k v m).
Proof.
intros k k' v v' m H.
intros k''.
rewrite H;clear H.
case (X.eq_dec k' k'');intro Heq.
do 2 (rewrite MapsPtes.F.add_eq_o;[|rewrite Heq;reflexivity]);reflexivity.
do 3 (rewrite MapsPtes.F.add_neq_o;[|intro abs;elim Heq;rewrite abs;reflexivity]);reflexivity.
Qed.
Lemma addm_rec_left :
forall (m1 m2: Maps.t nat) (k : Maps.key) (e : nat),
Maps.Equal (Maps.fold add_multiple (add_multiple k e m1) m2) (add_multiple k e (Maps.fold add_multiple m1 m2)).
Proof.
intros m1 m2.
assert (morph:
forall (ms1 : Maps.t nat)
(ms1' : Maps.t nat)
(H1 : Maps.Equal ms1 ms1')
(ms2 : Maps.t nat)
(ms2' : Maps.t nat)
(H2 : Maps.Equal ms2 ms2'),
Maps.Equal (Maps.fold add_multiple ms1 ms2) (Maps.fold add_multiple ms1' ms2')).
clear.
apply fold_pseudo_morph.
intros k k' n ms0 ms'0 H H0.
now apply add_multiple_morph.
apply add_multiple_morph_Proper.
apply transpose_neqkey_equal_add_multiple.
intros k e a k' H.
unfold add_multiple.
destruct (Maps.find k a).
apply MapsPtes.F.add_neq_o;trivial.
apply MapsPtes.F.add_neq_o;trivial.
pattern m1,(Maps.fold add_multiple m1 m2).
apply MapsPtes.fold_rec.
- intros m H k e.
unfold add_multiple at 2.
replace (Maps.find k m) with (@None nat) by (symmetry;apply Empty_not_find;assumption).
rewrite MapsPtes.fold_Add with (m1:=m) (k:=k) (e:=e);auto with *.
+ rewrite MapsPtes.fold_Empty;auto with *.
+ apply transpose_neqkey_equal_add_multiple.
+ intros abs.
red in abs.
destruct abs as [v H1];elim (H k v H1).
+ red;tauto.
- intros k e a m' m'' H H0 H1 H2 k0 e0.
rewrite morph with (ms1':= (add_multiple k0 e0 (add_multiple k e m')))
(ms2':=m2);trivial; try reflexivity.
case (X.eq_dec k k0);intro Heq.
unfold add_multiple at 2 3 4 5.
case_eq (Maps.find k a);[intro n|];intro Heq1.
rewrite <- Heq.
rewrite (MapsPtes.F.not_find_in_iff m' k) in H0.
rewrite H0.
do 2 (rewrite MapsPtes.F.add_eq_o;[|reflexivity]).
rewrite morph with (ms1':=(add_multiple k0 (S (e0 + e)) m')) (ms2':=m2);trivial;try reflexivity.
rewrite (H2 k0 (S (e0+e))).
rewrite Heq.
rewrite add_add_add.
unfold add_multiple.
rewrite Heq in Heq1;rewrite Heq1.
replace ((S(e0 + e) + n))%nat with (e0 + (S (e + n)))%nat by lia.
reflexivity.
reflexivity.
rewrite add_add_add.
unfold add_multiple.
rewrite Heq in H0.
rewrite H0.
reflexivity.
rewrite <- Heq;reflexivity.
rewrite <- Heq.
rewrite (MapsPtes.F.not_find_in_iff m' k) in H0.
rewrite H0.
do 2 (rewrite MapsPtes.F.add_eq_o;[|reflexivity]).
rewrite morph with (ms1':=(add_multiple k0 (S(e0 + e)) m')) (ms2':=m2);trivial;try reflexivity.
rewrite (H2 k0 (S(e0+e))).
rewrite Heq.
rewrite add_add_add.
unfold add_multiple.
rewrite Heq in Heq1;rewrite Heq1.
reflexivity.
reflexivity.
rewrite add_add_add.
unfold add_multiple.
rewrite Heq in H0.
rewrite H0.
reflexivity.
rewrite <- Heq;reflexivity.
rewrite morph with (ms1':= (add_multiple k e (add_multiple k0 e0 m')))
(ms2':=m2);trivial;try reflexivity.
rewrite MapsPtes.fold_Add with (k:=k) (e:=e) (m1:=(add_multiple k0 e0 m'));auto with *.
rewrite H2.
rewrite add_multiple_comm;reflexivity.
apply transpose_neqkey_equal_add_multiple.
unfold add_multiple.
destruct (Maps.find k0 m').
rewrite MapsPtes.F.add_in_iff.
intros abs;destruct abs.
apply Heq;rewrite H3;reflexivity.
apply H0;apply H3.
rewrite MapsPtes.F.add_in_iff.
intros abs;destruct abs.
apply Heq;rewrite H3;reflexivity.
apply H0;apply H3.
red.
intro y.
replace (Maps.add k e (add_multiple k0 e0 m')) with
(add_multiple k e (add_multiple k0 e0 m')).
rewrite add_multiple_comm;reflexivity.
unfold add_multiple at 1.
assert (~Maps.In k (add_multiple k0 e0 m')).
unfold add_multiple.
destruct (Maps.find k0 m').
rewrite MapsPtes.F.add_in_iff.
intros abs;destruct abs.
apply Heq;rewrite H3;reflexivity.
apply H0;apply H3.
rewrite MapsPtes.F.add_in_iff.
intros abs;destruct abs.
apply Heq;rewrite H3;reflexivity.
apply H0;apply H3.
rewrite MapsPtes.F.not_find_in_iff in H3.
rewrite H3;reflexivity.
rewrite add_multiple_comm;reflexivity.
setoid_replace m'' with (add_multiple k e m').
reflexivity.
replace (add_multiple k e m') with (Maps.add k e m').
apply H1.
unfold add_multiple.
rewrite MapsPtes.F.not_find_in_iff in H0.
rewrite H0;reflexivity.
Qed.
Lemma union_rec_left : forall a ms ms', eq (union (add a ms) ms') (add a (union ms ms')).
Proof.
intros a ms ms'.
repeat rewrite add_add_multiple.
unfold eq,union,add.
apply addm_rec_left.
Qed.
Lemma addm_rec_right :
forall (m1 m2: Maps.t nat) (k : Maps.key) (e : nat),
Maps.Equal (Maps.fold add_multiple m1 (add_multiple k e m2)) (add_multiple k e (Maps.fold add_multiple m1 m2)).
Proof.
intros m1 m2.
assert (morph:
forall (ms1 : Maps.t nat)
(ms1' : Maps.t nat)
(H1 : Maps.Equal ms1 ms1')
(ms2 : Maps.t nat)
(ms2' : Maps.t nat)
(H2 : Maps.Equal ms2 ms2'),
Maps.Equal (Maps.fold add_multiple ms1 ms2) (Maps.fold add_multiple ms1' ms2')).
clear.
apply fold_pseudo_morph.
intros k k' n ms0 ms'0 H H0.
now apply add_multiple_morph.
apply add_multiple_morph_Proper.
apply transpose_neqkey_equal_add_multiple.
intros k e a k' H.
unfold add_multiple.
destruct (Maps.find k a).
apply MapsPtes.F.add_neq_o;trivial.
apply MapsPtes.F.add_neq_o;trivial.
pattern m1,(Maps.fold add_multiple m1 m2).
apply MapsPtes.fold_rec.
- intros m H k e.
rewrite MapsPtes.fold_Empty;auto with *.
- intros k e a m' m'' H H0 H1 H2 k0 e0.
rewrite morph with (ms1':= (add_multiple k e m'))
(ms2':=(add_multiple k0 e0 m2));trivial; try reflexivity.
rewrite MapsPtes.fold_Add with (k:=k) (e:=e) (m1:= m');auto with *.
rewrite H2.
apply add_multiple_comm.
apply transpose_neqkey_equal_add_multiple.
unfold add_multiple.
rewrite (MapsPtes.F.not_find_in_iff m' k) in H0.
rewrite H0.
red;tauto.
unfold add_multiple.
rewrite (MapsPtes.F.not_find_in_iff m' k) in H0.
rewrite H0.
exact H1.
Qed.
Lemma union_rec_right : forall a ms ms', eq (union ms (add a ms')) (add a (union ms ms')).
Proof.
intros a ms ms'.
repeat rewrite add_add_multiple.
unfold eq,union,add.
apply addm_rec_right.
Qed.
Lemma mem_morph_eq :
forall (φ : A) (Γ Γ' : t), eq Γ Γ' -> mem φ Γ = mem φ Γ'.
Proof.
intros φ Γ Γ' H.
unfold eq,mem in *.
apply MapsPtes.F.mem_m.
reflexivity.
assumption.
Qed.
Lemma eq_bool_correct : forall m1 m2, eq_bool m1 m2 = true -> eq m1 m2.
Proof.
unfold eq,eq_bool.
intros m1 m2 H.
apply Maps.equal_2 in H.
rewrite <- MapsPtes.F.Equal_Equivb in H.
assumption.
clear m1 m2 H.
intros m n.
functional induction (nat_eq_bool m n).
tauto.
split;intro H;destruct IHb.
rewrite (H0 H);reflexivity.
injection H;clear H;intro H.
auto.
destruct n;destruct m;try tauto;
split;intro;discriminate.
Qed.
Lemma eq_bool_complete : forall m1 m2, eq m1 m2 -> eq_bool m1 m2 = true.
Proof.
unfold eq,eq_bool.
intros m1 m2 H.
apply Maps.equal_1.
rewrite <- MapsPtes.F.Equal_Equivb .
assumption.
clear m1 m2 H.
intros m n.
functional induction (nat_eq_bool m n).
tauto.
split;intro H;destruct IHb.
rewrite (H0 H);reflexivity.
injection H;clear H;intro H.
auto.
destruct n;destruct m;try tauto;
split;intro;discriminate.
Qed.
Lemma remove_empty : forall φ, remove φ empty = empty.
Proof.
intros φ.
unfold remove,empty.
rewrite MapsFact.empty_o.
reflexivity.
Qed.
Lemma remove_same_add : forall φ φ' Γ, X.eq φ φ' -> eq (remove φ (add φ' Γ)) Γ.
Proof.
intros φ φ' Γ H.
rewrite add_add_multiple.
unfold remove,add,add_multiple,eq.
case_eq (Maps.find φ' Γ).
intros n Heq.
rewrite MapsFact.add_eq_o.
simpl.
rewrite MapsFact.Equal_mapsto_iff.
intros k e.
split;intro.
rewrite MapsFact.add_mapsto_iff in H0.
destruct H0.
destruct H0;subst.
apply Maps.find_2.
rewrite <- (MapsPtes.F.find_o _ H0).
rewrite (MapsPtes.F.find_o _ H).
assumption.
destruct H0.
apply Maps.add_3 in H1.
assumption.
intro;elim H0;apply X.eq_trans with φ'.
assumption.
exact H2.
case (X.eq_dec φ k);intros.
replace e with n.
apply Maps.add_1.
assumption.
rewrite MapsFact.find_mapsto_iff in H0.
rewrite <- (MapsFact.find_o _ e0) in H0.
rewrite (MapsFact.find_o _ H) in H0.
rewrite H0 in Heq;injection Heq;clear Heq;auto.
apply Maps.add_2.
assumption.
apply Maps.add_2.
intro;elim n0;apply X.eq_trans with φ';assumption.
assumption.
symmetry.
auto.
intros Heq.
rewrite MapsFact.add_eq_o.
rewrite MapsFact.Equal_mapsto_iff.
intros k e.
case (X.eq_dec k φ);intros Heq'.
split;intro.
apply Maps.find_1 in H0.
rewrite MapsFact.remove_eq_o in H0.
discriminate.
symmetry;assumption.
rewrite MapsPtes.F.find_mapsto_iff in H0.
rewrite (MapsPtes.F.find_o _ Heq') in H0.
rewrite (MapsPtes.F.find_o _ H) in H0.
rewrite Heq in H0;discriminate.
do 2 rewrite MapsPtes.F.find_mapsto_iff.
rewrite MapsPtes.F.remove_neq_o.
rewrite MapsPtes.F.add_neq_o.
tauto.
intro;elim Heq';apply X.eq_trans with φ'.
symmetry;assumption.
symmetry;assumption.
intro;elim Heq'; symmetry;assumption.
symmetry;assumption.
Qed.
Lemma remove_diff_add : forall φ φ' Γ, ~X.eq φ φ' ->
eq (remove φ (add φ' Γ)) (add φ' (remove φ Γ)).
Proof.
intros φ φ' Γ H.
repeat rewrite add_add_multiple.
unfold remove,add,add_multiple,eq.
simpl.
case_eq (Maps.find φ' Γ);
[intros n|]; intros Heq;
(case_eq (Maps.find φ Γ);
[intros n'|];intros Heq').
- destruct n'.
+ rewrite MapsFact.add_neq_o;[|intro;elim H;symmetry;assumption].
rewrite Heq'.
rewrite MapsFact.remove_neq_o;[| assumption].
rewrite Heq.
rewrite MapsFact.Equal_mapsto_iff.
intros k e.
do 2 rewrite MapsFact.find_mapsto_iff.
case (X.eq_dec k φ); case (X.eq_dec k φ');intros Heq1 Heq2.
* elim H;apply X.eq_trans with k.
-- symmetry;assumption.
-- assumption.
* rewrite MapsFact.remove_eq_o;[|symmetry;assumption ].
rewrite MapsFact.add_neq_o;[|intro;elim Heq1;symmetry;assumption ].
rewrite MapsFact.remove_eq_o;[|symmetry;assumption ].
reflexivity.
* rewrite MapsFact.remove_neq_o;[|intro;elim Heq2;symmetry;assumption ].
rewrite MapsFact.add_eq_o;[|symmetry;assumption ].
rewrite MapsFact.add_eq_o;[|symmetry;assumption ].
reflexivity.
* rewrite MapsFact.remove_neq_o;[|intro;elim Heq2;symmetry;assumption ].
rewrite MapsFact.add_neq_o;[|intro;elim Heq1;symmetry;assumption ].
rewrite MapsFact.add_neq_o;[|intro;elim Heq1;symmetry;assumption ].
rewrite MapsFact.remove_neq_o;[|intro;elim Heq2;symmetry;assumption ].