-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.v
1131 lines (983 loc) · 34.1 KB
/
functions.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
(* Function library. Part of the CertiCoq project.
* Author: Zoe Paraskevopoulou, 2016
*)
From SFS Require Import Ensembles_util.
From SFS Require Import Coqlib.
From Coq Require Import Numbers.BinNums NArith.BinNat PArith.BinPos Relations.Relations
Classes.Morphisms Lists.List Sets.Ensembles Program.Basics.
Import ListNotations.
Open Scope program_scope.
(** ** Usefull definitions and lemmas about functions. *)
Definition codomain {A B} (f : A -> B) : Ensemble B :=
fun y => exists x, f x = y.
Definition image {A B} (f : A -> B) (S : Ensemble A) :=
fun y => exists x, In _ S x /\ f x = y.
(** A function is injective in a subdomain *)
Definition injective_subdomain {A B} P (f : A -> B) :=
forall x x', In _ P x -> In _ P x' -> f x = f x' -> x = x'.
Definition injective {A B} (f : A -> B) := injective_subdomain (Full_set _) f.
(** Extensional equality on a subdomain *)
Definition f_eq_subdomain {A B} S (f1 f2 : A -> B) :=
forall x, In _ S x -> f1 x = f2 x.
(** Extensional equality *)
Definition f_eq {A B} (f1 f2 : A -> B) : Prop := forall x, f1 x = f2 x.
(** Extend a function. Only works for positives to avoid parameterizing the definition with
* the decidable equality proof. TODO: fix this *)
Definition extend {A} (f: positive -> A) (x : positive) (x' : A) : (positive -> A) :=
fun z => if peq z x then x' else f z.
Notation " f '{' x '~>' y '}' " := (extend f x y) (at level 10, no associativity)
: fun_scope.
Open Scope fun_scope.
Fixpoint extend_lst {A} (f: positive -> A) (xs : list positive) (xs' : list A)
: positive -> A :=
match xs with
| [] => f
| x :: xs =>
match xs' with
| [] => f
| x' :: xs' =>
(extend_lst f xs xs') {x ~> x'}
end
end.
Notation " f '<{' xs '~>' xs' '}>' " := (extend_lst f xs xs') (at level 10, no associativity)
: fun_scope.
(** Apply a function n times *)
Fixpoint app {A} (f : A -> A) (n : nat) : A -> A :=
fun x =>
match n with
| 0%nat => x
| S n' => f (app f n' x)
end.
Infix "^" := app (at level 30, right associativity) : fun_scope.
(** ** Some support for partial functions *)
Definition domain {A B} (f : A -> option B) : Ensemble A :=
fun x => exists y, f x = Some y.
Definition image' {A B} (f : A -> option B) S : Ensemble B :=
fun y => exists x, In _ S x /\ f x = Some y.
Definition injective_subdomain' {A B} P (f : A -> option B) :=
forall x x' v, In _ P x -> In _ P x' -> f x = Some v -> f x' = Some v -> x = x'.
Definition lift {A B} (f : A -> B) : option A -> option B :=
fun x => match x with
| Some x => Some (f x)
| None => None
end.
Definition inverse_subdomain {A B: Type} S (f : A -> B) g :=
f_eq_subdomain (image f S) (f ∘ g) id /\
f_eq_subdomain S (g ∘ f) id.
(** * Lemmas about [f_eq_subdomain] and [f_eq] *)
Instance equivalence_f_eq_subdomain {A B} S : Equivalence (@f_eq_subdomain A B S).
Proof.
constructor; try now constructor.
intros f1 f2 Heq x HS; rewrite Heq. reflexivity. eassumption.
intros f1 f2 f3 Heq1 Heq2 x HS; rewrite Heq1. now eauto. eassumption.
Qed.
Instance equivalence_f_eq {A B} : Equivalence (@f_eq A B).
Proof.
constructor; congruence.
Qed.
Instance f_eq_subdomain_Proper_Same_set {A B} :
Proper (Same_set A ==> eq ==> eq ==> iff) (@f_eq_subdomain A B).
Proof.
intros S1 S2 Hseq f1 f1' Heq1 f2 f2' Heq2; subst; split; intros Hfeq x HS;
apply Hfeq; eapply Hseq; eassumption.
Qed.
Instance f_eq_subdomain_Proper_f_eq_l {A B} :
Proper (eq ==> f_eq ==> eq ==> iff) (@f_eq_subdomain A B).
Proof.
intros S1 S2 Heq f1 f1' Heq1 f2 f2' Heq2; subst; split; intros Hfeq x HS.
now rewrite <- Heq1; eauto. now rewrite Heq1; eauto.
Qed.
Instance f_eq_subdomain_Proper_f_eq_r {A B} :
Proper (eq ==> eq ==> f_eq ==> iff) (@f_eq_subdomain A B).
Proof.
intros S1 S2 Heq f1 f1' Heq1 f2 f2' Heq2; subst; split; intros Hfeq x HS.
now rewrite <- Heq2; eauto. now rewrite Heq2; eauto.
Qed.
Instance f_eq_Proper_f_eq_l {A B} :
Proper (f_eq ==> eq ==> iff) (@f_eq A B).
Proof.
intros f1 f1' Heq1 f2 f2' Heq2; subst; split; intros Hfeq x.
now rewrite <- Heq1; eauto. now rewrite Heq1; eauto.
Qed.
Instance f_eq_Proper_f_eq_r {A B} :
Proper (eq ==> f_eq ==> iff) (@f_eq A B).
Proof.
intros f1 f1' Heq1 f2 f2' Heq2; subst; split; intros Hfeq.
now rewrite <- Heq2; eauto. now rewrite Heq2; eauto.
Qed.
Instance map_proper {A B} : Proper (f_eq ==> eq ==> eq) (@map A B).
Proof.
intros f1 f2 Hfeq x1 x2 Heq; subst.
induction x2; eauto.
simpl. rewrite Hfeq. congruence.
Qed.
Lemma f_eq_subdomain_antimon {A B} S S' (f f' : A -> B) :
Included _ S' S ->
f_eq_subdomain S f f' ->
f_eq_subdomain S' f f'.
Proof.
intros Hinc Hfeq x Hin; eauto.
Qed.
Lemma f_eq_subdomain_Union {A B} P1 P2 (f1 f2 : A -> B) :
f_eq_subdomain P1 f1 f2 ->
f_eq_subdomain P2 f1 f2 ->
f_eq_subdomain (Union _ P1 P2) f1 f2.
Proof.
intros H1 H2 x1 HP; inv HP; eauto.
Qed.
Lemma f_eq_subdomain_compose_compat {A B C} S (f1 f2: A -> B) (g1 g2 : B -> C) :
f_eq_subdomain S f1 f2 ->
f_eq_subdomain (image f1 S) g1 g2 ->
f_eq_subdomain S (g1 ∘ f1) (g2 ∘ f2).
Proof.
intros Heq1 Heq2 x Hin. unfold compose.
rewrite <- Heq1; eauto. rewrite Heq2. reflexivity.
eexists; split; eauto.
Qed.
Lemma compose_extend_l S (C : Type) f (g : positive -> positive) (x : positive) (y : C) :
injective_subdomain S g ->
x \in S ->
f_eq_subdomain S (f {g x ~> y} ∘ g) ((f ∘ g) {x ~> y}).
Proof.
intros Hinj Hin z Hinz. unfold compose. simpl. compute.
destruct (peq z x); subst; eauto.
- rewrite peq_true. reflexivity.
- rewrite peq_false. reflexivity. intros Hc.
eapply n. eapply Hinj; eassumption.
Qed.
(** * Lemmas about [image] *)
Instance image_Proper_Same_set {A B} : Proper (eq ==> Same_set A ==> Same_set B) image.
Proof.
intros x1 x2 Heq1 s1 s2 Hseq; subst; split; intros x [y [Hin Heq]]; subst;
eexists; split; eauto; apply Hseq; eauto.
Qed.
Instance image_Proper_f_eq {A B} : Proper (f_eq ==> eq ==> Same_set B) (@image A B).
Proof.
intros x1 x2 Heq1 s1 s2 Hseq2; subst; split; intros x [y [Hin Heq]]; subst;
eexists; split; eauto; rewrite Heq1; eauto; now (constructor; eauto).
Qed.
Lemma image_Union {A B} S1 S2 (g : A -> B) :
Same_set _ (image g (Union _ S1 S2)) (Union _ (image g S1) (image g S2)).
Proof.
split; intros x Hi.
- destruct Hi as [x' [Hin Heq]]; subst. inv Hin.
left. eexists; eauto.
right. eexists; eauto.
- inv Hi; destruct H as [x' [Hin Heq]]; subst;
eexists; split; eauto.
Qed.
Lemma image_id {A : Type} (S : Ensemble A) :
image id S <--> S.
Proof.
split; intros x.
- intros [x' [Hin Heq]]. inv Heq.
eassumption.
- intros HS. eexists; split; eauto.
Qed.
Lemma image_compose {A B C : Type} (f : A -> B) (g : B -> C) (S : Ensemble A):
image (g ∘ f) S <--> image g (image f S).
Proof.
split.
- intros c [a [Hin Heq]].
eexists. split; eauto. eexists. split; eauto.
- intros c [b' [[a [Hin Heqa]] Heqb]]. subst.
eexists. split; eauto.
Qed.
Lemma image_Singleton {A B} x (g : A -> B) :
Same_set _ (image g (Singleton _ x)) (Singleton _ (g x)).
Proof.
split; intros y Hi.
- destruct Hi as [x' [Hin Heq]]; subst. inv Hin.
eauto.
- destruct Hi. eexists; eauto.
Qed.
Lemma image_Empty_set {A B} (g : A -> B) :
Same_set _ (image g (Empty_set _)) ((Empty_set _)).
Proof.
split; intros y Hi.
- destruct Hi as [x' [Hin Heq]]; subst. inv Hin.
- inv Hi.
Qed.
Lemma image_f_eq_subdomain {A B} (f1 f2 : A -> B) S :
f_eq_subdomain S f1 f2 ->
Same_set _ (image f1 S) (image f2 S).
Proof.
intros Heq; split; intros x [y [Hin Heq']]; subst;
(eexists; split; [ now eauto |]); now rewrite Heq.
Qed.
Lemma image_monotonic {A B} (f : A -> B) S S' :
Included _ S S' ->
Included _ (image f S) (image f S').
Proof.
intros Hin x [y [Hin' Heq]]; subst. eexists; eauto.
Qed.
Lemma FromList_map_image_FromList {A B} l (f : A -> B):
Same_set B (FromList (map f l)) (image f (FromList l)).
Proof with now eauto with Ensembles_DB.
induction l; simpl.
- rewrite !FromList_nil, image_Empty_set...
- rewrite !FromList_cons, image_Union, image_Singleton...
Qed.
(** * Lemmas about [extend] *)
Instance extend_Proper {A} : Proper (f_eq ==> Logic.eq ==> Logic.eq ==> f_eq) (@extend A).
Proof.
intros f1 f2 Hfeq x1 x2 Heq1 x3 x4 Hfeq2; subst.
intros x. unfold extend. destruct (peq x x2); eauto.
Qed.
Lemma extend_gss {A} f x (x' : A) :
f {x ~> x'} x = x'.
Proof.
unfold extend. rewrite peq_true. reflexivity.
Qed.
Lemma extend_gso {A} f x (x' : A) y :
y <> x ->
f {x ~> x'} y = f y.
Proof.
intros Heq. unfold extend. rewrite peq_false; eauto.
Qed.
Lemma extend_same f x y :
f x = x ->
f {y ~> y} x = x.
Proof.
unfold extend. destruct (peq x y); eauto.
Qed.
Lemma extend_commut {A} f y x x' (y' : A) :
x' <> x -> y' <> y ->
f_eq ((f {x ~> y}) {x' ~> y'}) ((f {x' ~> y'}) {x ~> y}).
Proof.
intros Hnin Hnin' z.
destruct (peq x z); subst.
- rewrite extend_gss, extend_gso; eauto.
now rewrite extend_gss.
- destruct (peq x' z); subst.
+ rewrite extend_gss, (extend_gso _ x y z); eauto.
now rewrite extend_gss.
+ repeat rewrite extend_gso; eauto.
Qed.
Lemma f_eq_extend {A} (f f' : positive -> A) x y :
f_eq f f' ->
f_eq (f{x ~> y}) (f'{x ~> y}).
Proof.
intros Heq.
unfold extend. intros z.
destruct (peq z x); eauto.
Qed.
Lemma f_eq_extend_same {A} (f : positive -> A) x y :
f x = y ->
f_eq (f{x ~> y}) f.
Proof.
intros Heq x'.
unfold extend. destruct (peq x' x); eauto.
congruence.
Qed.
Lemma f_eq_subdomain_extend {A} S (f f' : positive -> A) x y :
f_eq_subdomain S f f' ->
f_eq_subdomain (Union _ (Singleton _ x) S) (f{x ~> y}) (f'{x ~> y}).
Proof.
intros Heq.
unfold extend. intros z Hin.
destruct (peq z x). now eauto.
apply Heq. inv Hin; [| now eauto ]. inv H; congruence.
Qed.
Lemma f_eq_subdomain_extend_not_In_S_l {A} f1 S f2 x (x' : A) :
~ In _ S x ->
f_eq_subdomain S f1 f2 ->
f_eq_subdomain S (f1{x~>x'}) f2.
Proof.
intros Hin Hfeq y HIn.
rewrite extend_gso. now eauto.
intros Hc. subst. contradiction.
Qed.
Lemma f_eq_subdomain_extend_not_In_S_r' {A} P (f1 f2 : positive -> A) v v' :
f_eq_subdomain (Union _ P (Singleton _ v)) f1 (f2 {v ~> v'}) ->
~ In _ P v ->
f_eq_subdomain P f1 f2.
Proof.
intros Heq Hin x y. erewrite <- (extend_gso f2).
apply Heq; constructor; eauto.
intros Hc. subst. eauto.
Qed.
Lemma f_eq_subdomain_extend_not_In_S_r {A} f1 S f2 x (x' : A) :
~ In _ S x ->
f_eq_subdomain S f1 f2 ->
f_eq_subdomain S f1 (f2{x~>x'}).
Proof.
intros Hin Hfeq y HIn.
rewrite extend_gso. now eauto.
intros Hc. subst. contradiction.
Qed.
Lemma map_extend_not_In {A} f l x (x' : A) :
~ In _ (FromList l) x ->
map (f{x~>x'}) l = map f l.
Proof.
induction l; intros Hnin; eauto.
simpl. rewrite extend_gso.
f_equal. eapply IHl.
intros Hc; eapply Hnin; rewrite FromList_cons; eauto.
intros Hc; eapply Hnin; subst; rewrite FromList_cons; eauto.
Qed.
Lemma image_extend_not_In_S {A} f x (x' : A) S :
~ In _ S x ->
Same_set _ (image (f {x ~> x'} ) S) (image f S).
Proof.
intros Hnin.
split; intros y [y' [Hin Heq]]. rewrite extend_gso in Heq.
now eexists; eauto. intros Hc. subst. contradiction.
eexists; split; eauto. rewrite extend_gso. now eauto.
intros Hc. subst. contradiction.
Qed.
Lemma image_extend_In_S f x (x' : positive) S :
In _ S x ->
Same_set _ (image (f {x ~> x'}) S)
(Union _ (image f (Setminus _ S (Singleton _ x)))
(Singleton _ x')).
Proof.
intros HinS. split.
- intros y [y' [Hin Heq]]; subst.
destruct (peq x y').
+ subst. rewrite extend_gss. eauto.
+ rewrite extend_gso; eauto. left.
eexists; split; eauto. constructor; eauto.
intros Hc; inv Hc; congruence.
- intros y Hin.
destruct (peq x' y); subst.
+ eexists; split; eauto. rewrite extend_gss; eauto.
+ inv Hin.
* destruct H as [y' [Hin Heq]]; subst. inv Hin.
eexists; split. now eauto. rewrite extend_gso; eauto.
intros Heq; subst. eauto.
* inv H. congruence.
Qed.
Lemma image_Setminus_extend f v (v' : positive) S :
Included _ (image (f {v ~> v'}) (Setminus _ S (Singleton positive v)))
(image f S).
Proof.
rewrite image_extend_not_In_S.
apply image_monotonic.
now apply Setminus_Included.
intros Hc. inv Hc. eapply H0; eauto.
Qed.
Lemma image_extend_Included {A} f x (x' : A) S :
Included _ (image (f {x ~> x'}) S) (Union _ (image f S) (Singleton _ x')).
Proof.
intros y [y' [Hin Heq]]. unfold extend in Heq.
destruct (peq y' x); subst; [ now eauto |] .
left. eexists; eauto.
Qed.
Lemma image_extend_Included' {A} f x x' S :
Included A (image (f {x ~> x'}) S)
(Union A (image f (Setminus _ S (Singleton _ x))) (Singleton A x')).
Proof.
intros y [y' [Hin Heq]]; subst.
destruct (peq x y'); subst.
- rewrite extend_gss. eauto.
- left. rewrite extend_gso; eauto.
eexists. split; eauto.
constructor; eauto. intros Hc; inv Hc; congruence.
Qed.
Lemma In_image {A B} S f x:
In A S x ->
In B (image f S) (f x).
Proof.
intros; repeat eexists; eauto.
Qed.
Lemma Included_image_extend g S x (x' : positive) :
~ In _ S x ->
Included _ (image g S)
(image (g {x ~> x'}) (Union _ (Singleton _ x) S)).
Proof.
intros H.
eapply Included_trans.
eapply image_extend_not_In_S. eassumption. eapply image_monotonic.
now apply Included_Union_r.
Qed.
Hint Resolve In_image Included_image_extend : functions_BD.
(** * Lemmas about [extend_lst] *)
Lemma extend_lst_gso {A} f l (l' : list A) x :
~ In _ (FromList l) x ->
f <{l ~> l' }> x = f x.
Proof.
revert l'; induction l; intros l' Hnin; simpl; eauto.
destruct l'; eauto. rewrite FromList_cons in Hnin.
rewrite extend_gso. eapply IHl.
intros Hc. eapply Hnin; eauto.
intros Hc. subst; eapply Hnin; eauto.
Qed.
Lemma extend_lst_gss {A} f l (l' : list A) x :
In _ (FromList l) x ->
length l = length l' ->
exists x', f <{ l ~> l' }> x = x' /\ List.In x' l'.
Proof.
revert l'; induction l; intros l' Hnin Hlen; simpl; eauto.
- inv Hnin.
- destruct l'; try discriminate. rewrite FromList_cons in Hnin.
destruct (peq x a); subst.
+ rewrite extend_gss.
eexists; split; eauto. now constructor.
+ rewrite extend_gso; eauto. edestruct IHl as [x' [Heq HIn]].
inv Hnin; eauto. inv H; congruence.
inv Hlen. eassumption.
eexists; split; eauto. now constructor 2.
Qed.
Lemma extend_lst_app {A} (f : positive -> A) xs xs' ys ys' :
length xs = length ys ->
f_eq (f <{xs ++ xs' ~> ys ++ ys'}>)
(f <{xs' ~> ys'}> <{xs ~> ys}>).
Proof.
revert ys f. induction xs; intros ys f Hlen.
- simpl. destruct ys; try discriminate. reflexivity.
- destruct ys; try discriminate. simpl.
eapply f_eq_extend.
eapply IHxs. inv Hlen. reflexivity.
Qed.
Lemma image_extend_lst_Included {A} f S xs xs' :
length xs = length xs' ->
Included _ (image (f <{xs ~> xs'}>) S)
(Union A (image f (Setminus _ S (FromList xs))) (FromList xs')).
Proof with now eauto with Ensembles_DB.
revert xs' f S; induction xs; intros xs' f S Hlen.
- simpl. rewrite FromList_nil, Setminus_Empty_set_neut_r...
- destruct xs'; try discriminate. inv Hlen.
simpl. eapply Included_trans.
apply image_extend_Included'. rewrite !FromList_cons.
eapply Union_Included; [| now eauto with Ensembles_DB ].
eapply Included_trans. eapply IHxs; eauto.
apply Included_Union_compat.
eapply image_monotonic...
now eauto with Ensembles_DB.
Qed.
Lemma FromList_image_exists {A B} (f : A -> B) l S :
FromList l \subset image f S ->
exists l', l = map f l'.
Proof with (now eauto with Ensembles_DB).
revert S; induction l; intros S Heq; eauto.
- eexists []. reflexivity.
- rewrite FromList_cons in Heq.
edestruct IHl as [l' Heql'].
+ eapply Included_trans; try eassumption...
+ edestruct Heq as [a' [Heqa Hina]]. now left.
eexists (a' :: l'). simpl; f_equal; eauto.
Qed.
Lemma extend_extend_lst_commut {A} f ys xs x (y : A) :
~ List.In x xs ->
~ List.In y ys ->
length xs = length ys ->
f_eq ((f {x ~> y}) <{xs ~> ys}>) ((f <{xs ~> ys}>) {x ~> y}).
Proof.
revert f ys; induction xs; intros f ys Hnin1 Hnin2 Hlen; simpl.
- reflexivity.
- destruct ys; try discriminate. inv Hlen.
rewrite IHxs. rewrite extend_commut. reflexivity.
intros Hc; subst. now eapply Hnin1; constructor.
intros Hc; subst. now eapply Hnin2; constructor.
intros Hc; subst. now eapply Hnin1; constructor 2.
intros Hc; subst. now eapply Hnin2; constructor 2.
eassumption.
Qed.
Lemma map_extend_lst_Disjoint {A} f l xs (xs' : list A) :
Disjoint _ (FromList l) (FromList xs) ->
map (f <{xs ~> xs'}> ) l = map f l.
Proof.
revert xs'; induction xs; intros xs' HD; eauto.
destruct xs'; eauto. simpl.
rewrite FromList_cons in HD.
rewrite map_extend_not_In. eapply IHxs.
now eauto with Ensembles_DB.
intros Hc. eapply HD; eauto.
Qed.
Lemma map_extend_lst_same {A} f xs (xs' : list A) :
NoDup xs ->
length xs = length xs' ->
map (f <{xs ~> xs'}> ) xs = xs'.
Proof.
revert xs'; induction xs; intros xs' Hnd Hlen; eauto.
- destruct xs'; try discriminate. reflexivity.
- simpl. destruct xs'; try discriminate.
inv Hnd. inv Hlen.
rewrite extend_gss. f_equal.
rewrite map_extend_not_In; eauto.
Qed.
Instance extend_lst_Proper {A} : Proper (f_eq ==> eq ==> eq ==> f_eq) (@extend_lst A).
Proof.
intros f1 f2 f_eq l1 l2 Heq1 l1' l2' Heq2; subst.
revert l2'. induction l2; simpl; intros l2'; eauto.
destruct l2'; eauto. rewrite IHl2. reflexivity.
Qed.
(** * Lemmas about [injective_subdomain] and [injective] *)
Instance injective_subdomain_Proper_f_eq {A B} : Proper (eq ==> f_eq ==> iff)
(@injective_subdomain A B).
Proof.
intros s1 s2 Hseq f1 f2 Hfeq; split; intros Hinj x y Hin1 Hin2 Heq; subst;
eapply Hinj; eauto. now rewrite !Hfeq. now rewrite <- !Hfeq.
Qed.
Instance injective_subdomain_Proper_Same_set {A B} : Proper (Same_set _ ==> eq ==> iff)
(@injective_subdomain A B).
Proof.
intros s1 s2 Hseq f1 f2 Hfeq; split; intros Hinj x y Hin1 Hin2 Heq; subst;
eapply Hinj; eauto; now apply Hseq.
Qed.
Instance injective_Proper {A B} : Proper (f_eq ==> iff)
(@injective A B).
Proof.
now apply injective_subdomain_Proper_f_eq.
Qed.
Lemma injective_subdomain_antimon {A B} (σ : A -> B) S S' :
injective_subdomain S σ ->
Included _ S' S ->
injective_subdomain S' σ .
Proof.
intros Hinj Hinc x y Hin Hin' Heq. eauto.
Qed.
Lemma injective_subdomain_Union {A B} (f : A -> B) S1 S2 :
injective_subdomain S1 f ->
injective_subdomain S2 f ->
Disjoint _ (image f S1) (image f S2) ->
injective_subdomain (Union A S1 S2) f.
Proof.
intros Hinj1 Hinj2 HD x1 x2 Hin1 Hin2 Heq.
inv Hin1; inv Hin2.
- eauto.
- exfalso. eapply HD. constructor; eexists; eauto.
- exfalso. eapply HD. constructor; eexists; eauto.
- eauto.
Qed.
Lemma injective_subdomain_Singleton {A B} (f : A -> B) x :
injective_subdomain (Singleton _ x) f.
Proof.
intros x1 x2 Hin1 Hin2 Heq. inv Hin1. inv Hin2.
reflexivity.
Qed.
Lemma injective_subdomain_Empty_set {A B} (f : A -> B) :
injective_subdomain (Empty_set _) f.
Proof.
intros x1 x2 Hin1 Hin2 Heq. inv Hin1.
Qed.
Lemma injective_subdomain_extend {A} f x (x' : A) S :
injective_subdomain S f ->
~ In _ (image f (Setminus _ S (Singleton _ x))) x' ->
injective_subdomain (Union _ (Singleton _ x) S) (f {x~>x'}).
Proof.
intros Hinj Hnin.
intros y y' Hin1 Hin2.
destruct (peq x y); subst.
- rewrite extend_gss. intros Heq.
destruct (peq y y'); [ now eauto | ].
rewrite extend_gso in Heq; [| now eauto ].
exfalso. eapply Hnin. eexists; split; [| now eauto ].
inv Hin2. inv H. congruence.
constructor; eauto. intros Hc; inv Hc; congruence.
- rewrite extend_gso; [| now eauto ].
destruct (peq x y'); subst.
+ rewrite extend_gss. intros Heq. subst.
exfalso. apply Hnin. eexists.
split; [| reflexivity ].
inv Hin1. inv H; congruence.
constructor; eauto. intros Hc; inv Hc; congruence.
+ rewrite extend_gso; [| now eauto ].
intros Heq. eapply Hinj; eauto.
inv Hin1. inv H; congruence. eassumption.
inv Hin2. inv H; congruence. eassumption.
Qed.
Lemma injective_subdomain_extend'' {A} f x (x' : A) S :
injective_subdomain S f ->
~ In _ (image f (Setminus _ S (Singleton _ x))) x' ->
injective_subdomain S (f {x~>x'}).
Proof.
intros Hinj Hnin.
intros y y' Hin1 Hin2.
destruct (peq x y); subst.
- rewrite extend_gss. intros Heq.
destruct (peq y y'); [ now eauto | ].
rewrite extend_gso in Heq; [| now eauto ].
exfalso. eapply Hnin. eexists; split; [| now eauto ].
constructor; eauto. intros Hc; eapply n. now inv Hc.
- rewrite extend_gso; [| now eauto ].
destruct (peq x y'); subst.
+ rewrite extend_gss. intros Heq. subst.
exfalso. apply Hnin. eexists.
split; [| reflexivity ].
constructor; eauto. intros Hc; eapply n. now inv Hc.
+ rewrite extend_gso; [| now eauto ].
intros Heq. eapply Hinj; eauto.
Qed.
Lemma injective_subdomain_extend' S f x x' :
injective_subdomain (Setminus _ S (Singleton _ x)) f ->
~ In positive (image f (Setminus positive S (Singleton _ x))) x' ->
injective_subdomain S (f {x ~> x'}).
Proof.
intros Hinj Hnin y z Hin Hin' Heq.
destruct (peq x y); destruct (peq x z); subst; eauto;
try rewrite extend_gss in Heq; try rewrite !extend_gso in Heq; eauto.
- subst. exfalso. eapply Hnin. eexists; split; eauto.
constructor; eauto.
intros Hc; inv Hc; subst; congruence.
- subst. exfalso. eapply Hnin. eexists; split; eauto.
constructor; eauto.
intros Hc; inv Hc; subst; congruence.
- subst. eapply Hinj in Heq; eauto.
constructor; eauto.
intros Hc; inv Hc; subst; congruence.
constructor; eauto.
intros Hc; inv Hc; subst; congruence.
Qed.
Lemma injective_subdomain_extend_lst S f xs xs' :
injective_subdomain (Setminus _ S (FromList xs)) f ->
Disjoint positive (image f (Setminus positive S (FromList xs))) (FromList xs') ->
NoDup xs' ->
length xs = length xs' ->
injective_subdomain S (f <{xs ~> xs'}>).
Proof with now eauto with Ensembles_DB.
revert xs' f S. induction xs; intros xs' f S Hinj HD Hnd Hlen.
- simpl. rewrite FromList_nil, Setminus_Empty_set_neut_r in Hinj. eassumption.
- destruct xs'; try discriminate.
inv Hlen. simpl.
rewrite !FromList_cons in HD. rewrite !FromList_cons in Hinj. inv Hnd.
eapply injective_subdomain_extend'.
+ eapply IHxs. rewrite Setminus_Union. eassumption.
eapply Disjoint_Included; [ | | eassumption ].
now eauto with Ensembles_DB.
rewrite Setminus_Union. reflexivity.
eassumption. eassumption.
+ intros Hc. eapply image_extend_lst_Included in Hc; eauto.
inv Hc.
eapply HD. constructor.
rewrite <- Setminus_Union. eassumption.
now eauto with Ensembles_DB.
eapply H2; eassumption.
Qed.
Lemma injective_subdomain_compose {A B C} (S1 : Ensemble A) (f1 : A -> B) (f2 : B -> C) :
injective_subdomain S1 f1 ->
injective_subdomain (image f1 S1) f2 ->
injective_subdomain S1 (f2 ∘ f1).
Proof.
intros Hi1 Hi2 x y Hin1 Hin2 Heq.
unfold compose in *. eapply Hi2 in Heq; eauto.
eexists; split; [| reflexivity ]; eauto.
eexists; split; [| reflexivity ]; eauto.
Qed.
Lemma injective_extend (f : positive -> positive) x y :
injective f ->
~ In _ (codomain f) y ->
injective (f{x ~> y}).
Proof.
intros Hinj Hin x1 x2 __ _ Heq.
unfold extend in *.
edestruct (peq x1 x).
- rewrite <- e in Heq.
edestruct (peq x2 x1); [ now eauto |].
exfalso. eapply Hin. eexists; eauto.
- edestruct (peq x2 x).
+ exfalso. eapply Hin. eexists; eauto.
+ eapply Hinj; try now constructor; eauto.
assumption.
Qed.
Lemma injective_subdomain_Union_not_In_image {A B} (f : A -> B) S1 S2 :
injective_subdomain (Union _ S1 S2) f ->
Disjoint _ S1 S2 ->
Disjoint _ (image f S1) (image f S2).
Proof.
intros Hinj. constructor; intros x Hin. inv Hin.
destruct H0 as [y [Hin Heq]]. destruct H1 as [y' [Hin' Heq']].
subst. eapply Hinj in Heq'; eauto. eapply H. subst; eauto.
Qed.
Lemma FromList_image_injective_exists (f : positive -> positive) l S :
FromList l <--> image f S ->
injective_subdomain S f ->
exists l', l = map f l' /\ FromList l' <--> S.
Proof with (now eauto with Ensembles_DB).
revert S; induction l; intros S Heq Hinj; eauto.
- eexists []. split; eauto.
rewrite !FromList_nil in Heq.
rewrite FromList_nil.
split; intros x Hin; try now inv Hin.
assert (Hc : Empty_set _ (f x)).
{ eapply Heq. eexists; split; eauto. }
inv Hc.
- rewrite FromList_cons in Heq.
assert (Ha : image f S a).
{ eapply Heq; now left. }
destruct Ha as [a' [Heqa Hina]]. subst.
destruct (Decidable_FromList l) as [Decl].
destruct (Decl (f a')).
+ rewrite Union_Same_set in Heq;
[| eapply Singleton_Included; now eauto ].
edestruct IHl as [l' [Heql Heqs]]; eauto.
subst.
eexists (a' :: l'). split. now simpl; f_equal; eauto.
rewrite FromList_cons.
rewrite Union_Same_set. eassumption.
rewrite Heqs. eapply Singleton_Included. eassumption.
+ assert (Heq' := Heq).
rewrite (Union_Setminus_Same_set S [set a']) in Heq;
[| eapply Singleton_Included; now eauto ].
rewrite image_Union, image_Singleton in Heq.
eapply Union_Same_set_Disjoint in Heq.
* edestruct IHl as [l' [Heql Heqs]]; try now apply Heq; eauto; subst.
eapply injective_subdomain_antimon; try eassumption...
eexists (a' :: l'). split. now simpl; f_equal; eauto.
rewrite FromList_cons.
rewrite Heqs.
rewrite <- (Union_Setminus_Same_set S [set a']);
[| eapply Singleton_Included; now eauto ].
reflexivity.
* eapply Disjoint_Singleton_l. eauto.
* rewrite <- image_Singleton.
eapply injective_subdomain_Union_not_In_image.
eapply injective_subdomain_antimon; try eassumption...
eapply Disjoint_Singleton_l.
intros Hc; inv Hc; eauto.
Qed.
(** * Lemmas about [domain] *)
Instance Proper_domain {A B} : Proper (f_eq ==> Same_set A) (@domain A B).
Proof.
constructor; intros x' [y' H'].
rewrite H in H'. repeat eexists; eauto.
rewrite <- H in H'. repeat eexists; eauto.
Qed.
Lemma domain_extend_None {A} (f : positive -> option A) x :
Included _ (domain f) (Union _ (Singleton _ x) (domain (f {x ~> None}))).
Proof.
intros y [z Hin].
destruct (peq x y); subst; eauto.
right. eexists. rewrite extend_gso; eauto.
Qed.
Lemma domain_extend_Some (A : Type) (f : positive -> option A) (x : positive) y :
Same_set positive
(Union positive (Singleton positive x) (domain f))
(domain (f {x ~> Some y})).
Proof.
split.
- intros z Hin. destruct (peq x z); subst.
repeat eexists; eauto. rewrite extend_gss. reflexivity.
inv Hin. inv H. congruence.
edestruct H.
repeat eexists; eauto. rewrite extend_gso; eauto.
- intros z Hin. destruct (peq x z); subst; eauto.
right. destruct Hin as [w H1]. rewrite extend_gso in H1; eauto.
repeat eexists; eauto.
Qed.
(** * Lemmas about [image'] *)
Instance Proper_image' {A B} :
Proper (f_eq ==> Same_set _ ==> Same_set B) (@image' A B).
Proof.
constructor; intros x' [y' [H1 H2]]; inv H0.
rewrite H in H2. repeat eexists; eauto.
rewrite <- H in H2. repeat eexists; eauto.
Qed.
Lemma image'_Empty_set {A B} (f : A -> option B) :
image' f (Empty_set _) <--> Empty_set _.
Proof.
firstorder.
Qed.
Lemma image'_Singleton_Some {A B} f (x : A) (y : B) :
f x = Some y ->
image' f [set x] <--> [set y].
Proof.
intros Heq.
split; intros z Hin.
- destruct Hin as [z' [Hin Heq']]. inv Hin.
rewrite Heq in Heq'. inv Heq'. reflexivity.
- inv Hin. eexists; split; eauto.
Qed.
Lemma image'_Singleton_None {A B} (f : A -> option B) (x : A) :
f x = None ->
image' f [set x] <--> Empty_set _.
Proof.
intros Heq.
split; intros z Hin.
- destruct Hin as [z' [Hin Heq']]. inv Hin.
rewrite Heq in Heq'. congruence.
- inv Hin.
Qed.
Lemma image'_monotonic {A B} (S1 S2 : Ensemble A) (f : A -> option B) :
S1 \subset S2 ->
image' f S1 \subset image' f S2.
Proof.
firstorder.
Qed.
Lemma image'_Union (A B : Type) (S1 S2 : Ensemble A) (g : A -> option B) :
image' g (S1 :|: S2) <--> image' g S1 :|: image' g S2.
Proof.
split; intros x Hin.
- destruct Hin as [y [Hin Heq]]; subst; inv Hin.
left; eexists; split; eauto.
right; eexists; split; eauto.
- inv Hin.
+ destruct H as [z [Hin Heq]].
eexists; split; eauto.
+ destruct H as [z [Hin Heq]].
eexists; split; eauto.
Qed.
Lemma image'_extend_Included_Some (A : Type) (f : positive -> option A) (x : positive)
(x' : A) (S : Ensemble positive):
image' (f {x ~> Some x'}) S \subset image' f (S \\ [set x]) :|: [set x'].
Proof.
intros z [y [Heq Hin]].
destruct (peq x y); subst.
- rewrite extend_gss in Hin. inv Hin; eauto.
- rewrite extend_gso in Hin; eauto.
left. eexists; split; eauto.
constructor; eauto. intros Hc; inv Hc; eauto.
Qed.
Lemma image'_extend_Included_None (A : Type) (f : positive -> option A) (x : positive)
(S : Ensemble positive):
image' (f {x ~> None}) S \subset image' f (S \\ [set x]).
Proof.
intros z [y [Heq Hin]].
destruct (peq x y); subst.
- rewrite extend_gss in Hin. inv Hin; eauto.
- rewrite extend_gso in Hin; eauto.
eexists; split; eauto.
constructor; eauto. intros Hc; inv Hc; eauto.
Qed.
Lemma image'_extend_In_S (f : positive -> option positive) (x x' : positive)
(S : Ensemble positive) :
In positive S x ->
image' (f {x ~> Some x'}) S <--> image' f (S \\ [set x]) :|: [set x'].
Proof.
intros Hin; split; intros y Him.
- destruct Him as [y' [Heq Him]].
destruct (peq x y'); subst.
+ rewrite extend_gss in Him. inv Him.
right. reflexivity.
+ rewrite extend_gso in Him.
left. eexists; split; eauto. constructor; eauto.
intros Hc; inv Hc; eauto.
intros Hc; inv Hc; eauto.
- destruct Him as [z [y' [Heq Him]] | z Heq ].
+ eexists. split. now eapply Heq.
rewrite extend_gso. eassumption. intros Hc.
subst. inv Heq; eauto.
+ eexists; split; eauto. inv Heq.
rewrite extend_gss. reflexivity.
Qed.
Instance Proper_image'_Same_set {A B} :
Proper (eq ==> Same_set A ==> Same_set B) image'.
Proof.
intros f1 f2 Hfeq s1 s2 Hseq; split; intros x [y [Hin Heq]];
subst; eexists; split; eauto; eapply Hseq; eauto.
Qed.
(** * Lemmas about [injective_subdomain'] *)
Lemma injective_subdomain'_antimon {A B} P1 P2 (f : A -> option B) :
injective_subdomain' P2 f ->
P1 \subset P2 ->
injective_subdomain' P1 f.
Proof.
intros Hinj Hsub x1 x2 v Hin1 Hin2 Heq1 Heq2.
eapply Hinj; eauto.
Qed.
Instance Proper_injective_subdomain' A B :
Proper (Same_set A ==> eq ==> iff) (@injective_subdomain' A B).
Proof.
intros s1 s2 Hseq f1 f2 Hfeq; subst; split; intros Hinj x y v Hin1 Hin2 Heq1 Heq2;
eapply Hinj; try eassumption; eapply Hseq; eauto.
Qed.