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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
|
(************************************************************************)
(* * The Rocq Prover / The Rocq Development Team *)
(* v * Copyright INRIA, CNRS and contributors *)
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(* * (see LICENSE file for the text of the license) *)
(************************************************************************)
From Stdlib Require Export List.
From Stdlib Require Export Sorted.
From Stdlib Require Export Setoid Basics Morphisms.
Set Implicit Arguments.
Unset Strict Implicit.
(* Set Universe Polymorphism. *)
(** * Logical relations over lists with respect to a setoid equality
or ordering. *)
(** This can be seen as a complement of predicate [lelistA] and [sort]
found in [Sorting]. *)
Section Type_with_equality.
Variable A : Type.
Variable eqA : A -> A -> Prop.
(** Being in a list modulo an equality relation over type [A]. *)
Inductive InA (x : A) : list A -> Prop :=
| InA_cons_hd : forall y l, eqA x y -> InA x (y :: l)
| InA_cons_tl : forall y l, InA x l -> InA x (y :: l).
#[local]
Hint Constructors InA : core.
(** TODO: it would be nice to have a generic definition instead
of the previous one. Having [InA = Exists eqA] raises too
many compatibility issues. For now, we only state the equivalence: *)
Lemma InA_altdef : forall x l, InA x l <-> Exists (eqA x) l.
Proof. split; induction 1; auto. Qed.
Lemma InA_cons : forall x y l, InA x (y::l) <-> eqA x y \/ InA x l.
Proof.
intuition. invlist InA; auto.
Qed.
Lemma InA_nil : forall x, InA x nil <-> False.
Proof.
intuition. invlist InA.
Qed.
(** An alternative definition of [InA]. *)
Lemma InA_alt : forall x l, InA x l <-> exists y, eqA x y /\ In y l.
Proof.
intros; rewrite InA_altdef, Exists_exists; firstorder.
Qed.
(** A list without redundancy modulo the equality over [A]. *)
Inductive NoDupA : list A -> Prop :=
| NoDupA_nil : NoDupA nil
| NoDupA_cons : forall x l, ~ InA x l -> NoDupA l -> NoDupA (x::l).
#[local]
Hint Constructors NoDupA : core.
(** An alternative definition of [NoDupA] based on [ForallOrdPairs] *)
Lemma NoDupA_altdef : forall l,
NoDupA l <-> ForallOrdPairs (complement eqA) l.
Proof.
split; induction 1 as [|a l H rest]; constructor; auto.
- rewrite Forall_forall. intros b Hb.
intro Eq; elim H. rewrite InA_alt. exists b; auto.
- rewrite InA_alt; intros (a' & Haa' & Ha').
rewrite Forall_forall in H. exact (H a' Ha' Haa').
Qed.
(** lists with same elements modulo [eqA] *)
Definition inclA l l' := forall x, InA x l -> InA x l'.
Definition equivlistA l l' := forall x, InA x l <-> InA x l'.
Lemma incl_nil l : inclA nil l.
Proof. intros a H. inversion H. Qed.
#[local]
Hint Resolve incl_nil : list.
(** lists with same elements modulo [eqA] at the same place *)
Inductive eqlistA : list A -> list A -> Prop :=
| eqlistA_nil : eqlistA nil nil
| eqlistA_cons : forall x x' l l',
eqA x x' -> eqlistA l l' -> eqlistA (x::l) (x'::l').
#[local]
Hint Constructors eqlistA : core.
(** We could also have written [eqlistA = Forall2 eqA]. *)
Lemma eqlistA_altdef : forall l l', eqlistA l l' <-> Forall2 eqA l l'.
Proof. split; induction 1; auto. Qed.
(** Results concerning lists modulo [eqA] *)
Hypothesis eqA_equiv : Equivalence eqA.
Definition eqarefl := (@Equivalence_Reflexive _ _ eqA_equiv).
Definition eqatrans := (@Equivalence_Transitive _ _ eqA_equiv).
Definition eqasym := (@Equivalence_Symmetric _ _ eqA_equiv).
#[local]
Hint Resolve eqarefl eqatrans : core.
#[local]
Hint Immediate eqasym : core.
Ltac inv := invlist InA; invlist sort; invlist lelistA; invlist NoDupA.
(** First, the two notions [equivlistA] and [eqlistA] are indeed equivlances *)
Global Instance equivlist_equiv : Equivalence equivlistA.
Proof.
firstorder.
Qed.
Global Instance eqlistA_equiv : Equivalence eqlistA.
Proof.
constructor; red.
- intros x; induction x; auto.
- induction 1; auto.
- intros x y z H; revert z; induction H; auto.
inversion 1; subst; auto. invlist eqlistA; eauto with *.
Qed.
(** Moreover, [eqlistA] implies [equivlistA]. A reverse result
will be proved later for sorted list without duplicates. *)
Global Instance eqlistA_equivlistA : subrelation eqlistA equivlistA.
Proof.
intros x x' H. induction H as [|? ? ? ? H ? IHeqlistA].
- intuition auto with relations.
- red; intros x0.
rewrite 2 InA_cons.
rewrite (IHeqlistA x0), H; intuition.
Qed.
(** InA is compatible with eqA (for its first arg) and with
equivlistA (and hence eqlistA) for its second arg *)
Global Instance InA_compat : Proper (eqA==>equivlistA==>iff) InA.
Proof.
intros x x' Hxx' l l' Hll'. rewrite (Hll' x).
rewrite 2 InA_alt; firstorder.
Qed.
(** For compatibility, an immediate consequence of [InA_compat] *)
Lemma InA_eqA : forall l x y, eqA x y -> InA x l -> InA y l.
Proof.
intros l x y H H'. rewrite <- H. auto.
Qed.
#[local]
Hint Immediate InA_eqA : core.
Lemma In_InA : forall l x, In x l -> InA x l.
Proof.
intros l; induction l; simpl; intuition.
subst; auto.
Qed.
#[local]
Hint Resolve In_InA : core.
Lemma InA_split : forall l x, InA x l ->
exists l1 y l2, eqA x y /\ l = l1++y::l2.
Proof.
intros l; induction l as [|a l IHl]; intros x H; inv.
- exists (@nil A); exists a; exists l; auto.
- match goal with H' : InA x l |- _ => rename H' into H0 end.
destruct (IHl x H0) as (l1,(y,(l2,(H1,H2)))).
exists (a::l1); exists y; exists l2; auto.
split; simpl; f_equal; auto.
Qed.
Lemma InA_app : forall l1 l2 x,
InA x (l1 ++ l2) -> InA x l1 \/ InA x l2.
Proof.
intros l1; induction l1 as [|a l1 IHl1]; simpl in *; intuition.
inv; auto.
match goal with H0' : InA _ (l1 ++ _) |- _ => rename H0' into H0 end.
elim (IHl1 _ _ H0); auto.
Qed.
Lemma InA_app_iff : forall l1 l2 x,
InA x (l1 ++ l2) <-> InA x l1 \/ InA x l2.
Proof.
split.
- apply InA_app.
- destruct 1 as [H|H]; generalize H; do 2 rewrite InA_alt.
+ destruct 1 as (y,(H1,H2)); exists y; split; auto.
apply in_or_app; auto.
+ destruct 1 as (y,(H1,H2)); exists y; split; auto.
apply in_or_app; auto.
Qed.
Lemma InA_rev : forall p m,
InA p (rev m) <-> InA p m.
Proof.
intros; do 2 rewrite InA_alt.
split; intros (y,H); exists y; intuition.
- rewrite In_rev; auto.
- rewrite <- In_rev; auto.
Qed.
(** Some more facts about InA *)
Lemma InA_singleton x y : InA x (y::nil) <-> eqA x y.
Proof.
rewrite InA_cons, InA_nil; tauto.
Qed.
Lemma InA_double_head x y l :
InA x (y :: y :: l) <-> InA x (y :: l).
Proof.
rewrite !InA_cons; tauto.
Qed.
Lemma InA_permute_heads x y z l :
InA x (y :: z :: l) <-> InA x (z :: y :: l).
Proof.
rewrite !InA_cons; tauto.
Qed.
Lemma InA_app_idem x l : InA x (l ++ l) <-> InA x l.
Proof.
rewrite InA_app_iff; tauto.
Qed.
Section NoDupA.
Lemma NoDupA_app : forall l l', NoDupA l -> NoDupA l' ->
(forall x, InA x l -> InA x l' -> False) ->
NoDupA (l++l').
Proof.
intros l; induction l as [|a l IHl]; simpl; auto; intros l' H H0 H1.
inv.
constructor.
- rewrite InA_alt; intros (y,(H4,H5)).
destruct (in_app_or _ _ _ H5).
+ match goal with H2' : ~ InA a l |- _ => rename H2' into H2 end.
elim H2.
rewrite InA_alt.
exists y; auto.
+ apply (H1 a).
* auto.
* rewrite InA_alt.
exists y; auto.
- apply IHl; auto.
intros x ? ?.
apply (H1 x); auto.
Qed.
Lemma NoDupA_rev : forall l, NoDupA l -> NoDupA (rev l).
Proof.
intros l; induction l.
- simpl; auto.
- simpl; intros.
inv.
apply NoDupA_app; auto.
+ constructor; auto.
intro; inv.
+ intros x.
rewrite InA_alt.
intros (x1,(H2,H3)).
intro; inv.
match goal with H0 : ~ InA _ _ |- _ => destruct H0 end.
match goal with H4 : eqA x ?x' |- InA ?x' _ => rewrite <- H4, H2 end.
apply In_InA.
rewrite In_rev; auto.
Qed.
Lemma NoDupA_split : forall l l' x, NoDupA (l++x::l') -> NoDupA (l++l').
Proof.
intros l; induction l; simpl in *; intros; inv; auto.
constructor; eauto.
match goal with H0 : ~ InA _ _ |- _ => contradict H0 end.
rewrite InA_app_iff in *.
rewrite InA_cons.
intuition.
Qed.
Lemma NoDupA_swap : forall l l' x, NoDupA (l++x::l') -> NoDupA (x::l++l').
Proof.
intros l; induction l as [|a l IHl]; simpl in *; intros l' x H; inv; auto.
constructor; eauto.
- match goal with H1 : NoDupA (l ++ x :: l') |- _ => assert (H2:=IHl _ _ H1) end.
inv.
rewrite InA_cons.
red; destruct 1.
+ match goal with H0 : ~ InA a (l ++ x :: l') |- _ => apply H0 end.
rewrite InA_app_iff in *; rewrite InA_cons; auto.
+ auto.
- constructor.
+ match goal with H0 : ~ InA a (l ++ x :: l') |- _ => contradict H0 end.
rewrite InA_app_iff in *; rewrite InA_cons; intuition.
+ eapply NoDupA_split; eauto.
Qed.
Lemma NoDupA_singleton x : NoDupA (x::nil).
Proof.
repeat constructor. inversion 1.
Qed.
End NoDupA.
Section EquivlistA.
Global Instance equivlistA_cons_proper:
Proper (eqA ==> equivlistA ==> equivlistA) (@cons A).
Proof.
intros ? ? E1 ? ? E2 ?; now rewrite !InA_cons, E1, E2.
Qed.
Global Instance equivlistA_app_proper:
Proper (equivlistA ==> equivlistA ==> equivlistA) (@app A).
Proof.
intros ? ? E1 ? ? E2 ?. now rewrite !InA_app_iff, E1, E2.
Qed.
Lemma equivlistA_cons_nil x l : ~ equivlistA (x :: l) nil.
Proof.
intros E. now eapply InA_nil, E, InA_cons_hd.
Qed.
Lemma equivlistA_nil_eq l : equivlistA l nil -> l = nil.
Proof.
destruct l.
- trivial.
- intros H. now apply equivlistA_cons_nil in H.
Qed.
Lemma equivlistA_double_head x l : equivlistA (x :: x :: l) (x :: l).
Proof.
intro. apply InA_double_head.
Qed.
Lemma equivlistA_permute_heads x y l :
equivlistA (x :: y :: l) (y :: x :: l).
Proof.
intro. apply InA_permute_heads.
Qed.
Lemma equivlistA_app_idem l : equivlistA (l ++ l) l.
Proof.
intro. apply InA_app_idem.
Qed.
Lemma equivlistA_NoDupA_split l l1 l2 x y : eqA x y ->
NoDupA (x::l) -> NoDupA (l1++y::l2) ->
equivlistA (x::l) (l1++y::l2) -> equivlistA l (l1++l2).
Proof.
intros H H0 H1 H2; intro a.
generalize (H2 a).
rewrite !InA_app_iff, !InA_cons.
inv.
assert (SW:=NoDupA_swap H1). inv.
rewrite InA_app_iff in *.
split; intros.
- match goal with H3 : ~ InA x l |- _ =>
assert (~eqA a x) by (contradict H3; rewrite <- H3; auto)
end.
assert (~eqA a y) by (rewrite <- H; auto).
tauto.
- assert (OR : eqA a x \/ InA a l) by intuition.
destruct OR as [EQN|INA]; auto.
match goal with H0 : ~ (InA y l1 \/ InA y l2) |- _ => elim H0 end.
rewrite <-H,<-EQN; auto.
Qed.
End EquivlistA.
Section Fold.
Variable B:Type.
Variable eqB:B->B->Prop.
Variable st:Equivalence eqB.
Variable f:A->B->B.
Variable i:B.
Variable Comp:Proper (eqA==>eqB==>eqB) f.
Lemma fold_right_eqlistA :
forall s s', eqlistA s s' ->
eqB (fold_right f i s) (fold_right f i s').
Proof.
induction 1; simpl; auto with relations.
apply Comp; auto.
Qed.
(** Fold with restricted [transpose] hypothesis. *)
Section Fold_With_Restriction.
Variable R : A -> A -> Prop.
Hypothesis R_sym : Symmetric R.
Hypothesis R_compat : Proper (eqA==>eqA==>iff) R.
(*
(** [ForallOrdPairs R] is compatible with [equivlistA] over the
lists without duplicates, as long as the relation [R]
is symmetric and compatible with [eqA]. To prove this fact,
we use an auxiliary notion: "forall distinct pairs, ...".
*)
Definition ForallNeqPairs :=
ForallPairs (fun a b => ~eqA a b -> R a b).
(** [ForallOrdPairs] and [ForallNeqPairs] are related, but not completely
equivalent. For proving one implication, we need to know that the
list has no duplicated elements... *)
Lemma ForallNeqPairs_ForallOrdPairs : forall l, NoDupA l ->
ForallNeqPairs l -> ForallOrdPairs R l.
Proof.
induction l; auto.
constructor. inv.
rewrite Forall_forall; intros b Hb.
apply H0; simpl; auto.
contradict H1; rewrite H1; auto.
apply IHl.
inv; auto.
intros b c Hb Hc Hneq.
apply H0; simpl; auto.
Qed.
(** ... and for proving the other implication, we need to be able
to reverse relation [R]. *)
Lemma ForallOrdPairs_ForallNeqPairs : forall l,
ForallOrdPairs R l -> ForallNeqPairs l.
Proof.
intros l Hl x y Hx Hy N.
destruct (ForallOrdPairs_In Hl x y Hx Hy) as [H|[H|H]].
subst; elim N; auto.
assumption.
apply R_sym; assumption.
Qed.
*)
(** Compatibility of [ForallOrdPairs] with respect to [inclA]. *)
Lemma ForallOrdPairs_inclA : forall l l',
NoDupA l' -> inclA l' l -> ForallOrdPairs R l -> ForallOrdPairs R l'.
Proof.
intros l l'. induction l' as [|x l' IH].
- constructor.
- intros ND Incl FOP. apply FOP_cons; inv; unfold inclA in *; auto.
rewrite Forall_forall; intros y Hy.
assert (Ix : InA x (x::l')) by (rewrite InA_cons; auto).
apply Incl in Ix. rewrite InA_alt in Ix. destruct Ix as (x' & Hxx' & Hx').
assert (Iy : InA y (x::l')) by (apply In_InA; simpl; auto).
apply Incl in Iy. rewrite InA_alt in Iy. destruct Iy as (y' & Hyy' & Hy').
rewrite Hxx', Hyy'.
destruct (ForallOrdPairs_In FOP x' y' Hx' Hy') as [E|[?|?]]; auto.
absurd (InA x l'); auto. rewrite Hxx', E, <- Hyy'; auto.
Qed.
(** Two-argument functions that allow to reorder their arguments. *)
Definition transpose (f : A -> B -> B) :=
forall (x y : A) (z : B), eqB (f x (f y z)) (f y (f x z)).
(** A version of transpose with restriction on where it should hold *)
Definition transpose_restr (R : A -> A -> Prop)(f : A -> B -> B) :=
forall (x y : A) (z : B), R x y -> eqB (f x (f y z)) (f y (f x z)).
Variable TraR :transpose_restr R f.
Lemma fold_right_commutes_restr :
forall s1 s2 x, ForallOrdPairs R (s1++x::s2) ->
eqB (fold_right f i (s1++x::s2)) (f x (fold_right f i (s1++s2))).
Proof.
intros s1; induction s1 as [|a s1 IHs1]; simpl; auto; intros s2 x H.
- reflexivity.
- transitivity (f a (f x (fold_right f i (s1++s2)))).
+ apply Comp; auto.
apply IHs1.
invlist ForallOrdPairs; auto.
+ apply TraR.
invlist ForallOrdPairs; auto.
match goal with H0 : Forall (R a) (s1 ++ x :: s2) |- R a x =>
rewrite Forall_forall in H0; apply H0
end.
apply in_or_app; simpl; auto.
Qed.
Lemma fold_right_equivlistA_restr :
forall s s', NoDupA s -> NoDupA s' -> ForallOrdPairs R s ->
equivlistA s s' -> eqB (fold_right f i s) (fold_right f i s').
Proof.
intros s; induction s as [|x l Hrec].
- intros s'; destruct s' as [|a s']; simpl.
+ intros; reflexivity.
+ unfold equivlistA; intros H H0 H1 H2.
destruct (H2 a).
assert (InA a nil) by auto; inv.
- intros s' N N' F E; simpl in *.
assert (InA x s') as H by (rewrite <- (E x); auto).
destruct (InA_split H) as (s1,(y,(s2,(H1,H2)))).
subst s'.
transitivity (f x (fold_right f i (s1++s2))).
+ apply Comp; auto.
apply Hrec; auto.
* inv; auto.
* eapply NoDupA_split; eauto.
* invlist ForallOrdPairs; auto.
* eapply equivlistA_NoDupA_split; eauto.
+ transitivity (f y (fold_right f i (s1++s2))).
* apply Comp; auto. reflexivity.
* symmetry; apply fold_right_commutes_restr.
apply ForallOrdPairs_inclA with (x::l); auto.
red; intros; rewrite E; auto.
Qed.
Lemma fold_right_add_restr :
forall s' s x, NoDupA s -> NoDupA s' -> ForallOrdPairs R s' -> ~ InA x s ->
equivlistA s' (x::s) -> eqB (fold_right f i s') (f x (fold_right f i s)).
Proof.
intros s' s x **; apply (@fold_right_equivlistA_restr s' (x::s)); auto.
Qed.
End Fold_With_Restriction.
(** we now state similar results, but without restriction on transpose. *)
Variable Tra :transpose f.
Lemma fold_right_commutes : forall s1 s2 x,
eqB (fold_right f i (s1++x::s2)) (f x (fold_right f i (s1++s2))).
Proof.
intros s1; induction s1 as [|a s1 IHs1]; simpl; auto; intros s2 x.
- reflexivity.
- transitivity (f a (f x (fold_right f i (s1++s2)))); auto.
apply Comp; auto.
Qed.
Lemma fold_right_equivlistA :
forall s s', NoDupA s -> NoDupA s' ->
equivlistA s s' -> eqB (fold_right f i s) (fold_right f i s').
Proof.
intros; apply (fold_right_equivlistA_restr (R:=fun _ _ => True));
repeat red; auto.
apply ForallPairs_ForallOrdPairs; try red; auto.
Qed.
Lemma fold_right_add :
forall s' s x, NoDupA s -> NoDupA s' -> ~ InA x s ->
equivlistA s' (x::s) -> eqB (fold_right f i s') (f x (fold_right f i s)).
Proof.
intros s' s x **; apply (@fold_right_equivlistA s' (x::s)); auto.
Qed.
End Fold.
Section Fold2.
Variable B:Type.
Variable eqB:B->B->Prop.
Variable st:Equivalence eqB.
Variable f:A->B->B.
Variable Comp:Proper (eqA==>eqB==>eqB) f.
Lemma fold_right_eqlistA2 :
forall s s' (i j:B) (heqij: eqB i j) (heqss': eqlistA s s'),
eqB (fold_right f i s) (fold_right f j s').
Proof.
intros s.
induction s as [|a s IHs];intros s' i j heqij heqss'.
- inversion heqss'.
subst.
simpl.
assumption.
- inversion heqss'.
subst.
simpl.
apply Comp.
+ assumption.
+ apply IHs;assumption.
Qed.
Section Fold2_With_Restriction.
Variable R : A -> A -> Prop.
Hypothesis R_sym : Symmetric R.
Hypothesis R_compat : Proper (eqA==>eqA==>iff) R.
(** Two-argument functions that allow to reorder their arguments. *)
Definition transpose2 (f : A -> B -> B) :=
forall (x y : A) (z z': B), eqB z z' -> eqB (f x (f y z)) (f y (f x z')).
(** A version of transpose with restriction on where it should hold *)
Definition transpose_restr2 (R : A -> A -> Prop)(f : A -> B -> B) :=
forall (x y : A) (z z': B), R x y -> eqB z z' -> eqB (f x (f y z)) (f y (f x z')).
Variable TraR :transpose_restr2 R f.
Lemma fold_right_commutes_restr2 :
forall s1 s2 x (i j:B) (heqij: eqB i j), ForallOrdPairs R (s1++x::s2) ->
eqB (fold_right f i (s1++x::s2)) (f x (fold_right f j (s1++s2))).
Proof.
intros s1; induction s1 as [|a s1 IHs1]; simpl; auto; intros s2 x i j heqij ?.
- apply Comp.
+ destruct eqA_equiv. apply Equivalence_Reflexive.
+ eapply fold_right_eqlistA2.
* assumption.
* reflexivity.
- transitivity (f a (f x (fold_right f j (s1++s2)))).
+ apply Comp; auto.
eapply IHs1.
* assumption.
* invlist ForallOrdPairs; auto.
+ apply TraR.
* invlist ForallOrdPairs; auto.
match goal with H0 : Forall (R a) (s1 ++ x :: s2) |- _ =>
rewrite Forall_forall in H0; apply H0
end.
apply in_or_app; simpl; auto.
* reflexivity.
Qed.
Lemma fold_right_equivlistA_restr2 :
forall s s' i j,
NoDupA s -> NoDupA s' -> ForallOrdPairs R s ->
equivlistA s s' -> eqB i j ->
eqB (fold_right f i s) (fold_right f j s').
Proof.
intros s; induction s as [|x l Hrec].
{ intros s'; destruct s' as [|a s']; simpl.
- intros. assumption.
- unfold equivlistA; intros ? ? H H0 H1 H2 **.
destruct (H2 a).
assert (InA a nil) by auto; inv.
}
intros s' i j N N' F E eqij; simpl in *.
assert (InA x s') as H by (rewrite <- (E x); auto).
destruct (InA_split H) as (s1,(y,(s2,(H1,H2)))).
subst s'.
transitivity (f x (fold_right f j (s1++s2))).
- apply Comp; auto.
apply Hrec; auto.
+ inv; auto.
+ eapply NoDupA_split; eauto.
+ invlist ForallOrdPairs; auto.
+ eapply equivlistA_NoDupA_split; eauto.
- transitivity (f y (fold_right f i (s1++s2))).
+ apply Comp; auto.
symmetry.
apply fold_right_eqlistA2.
* assumption.
* reflexivity.
+ symmetry.
apply fold_right_commutes_restr2.
* symmetry.
assumption.
* apply ForallOrdPairs_inclA with (x::l); auto.
red; intros; rewrite E; auto.
Qed.
Lemma fold_right_add_restr2 :
forall s' s i j x, NoDupA s -> NoDupA s' -> eqB i j -> ForallOrdPairs R s' -> ~ InA x s ->
equivlistA s' (x::s) -> eqB (fold_right f i s') (f x (fold_right f j s)).
Proof.
intros s' s i j x **; apply (@fold_right_equivlistA_restr2 s' (x::s) i j); auto.
Qed.
End Fold2_With_Restriction.
Variable Tra :transpose2 f.
Lemma fold_right_commutes2 : forall s1 s2 i x x',
eqA x x' ->
eqB (fold_right f i (s1++x::s2)) (f x' (fold_right f i (s1++s2))).
Proof.
intros s1; induction s1 as [|a s1 IHs1];simpl;intros s2 i x x' H.
- apply Comp;auto.
reflexivity.
- transitivity (f a (f x' (fold_right f i (s1++s2)))); auto.
+ apply Comp;auto.
+ apply Tra.
reflexivity.
Qed.
Lemma fold_right_equivlistA2 :
forall s s' i j, NoDupA s -> NoDupA s' -> eqB i j ->
equivlistA s s' -> eqB (fold_right f i s) (fold_right f j s').
Proof.
red in Tra.
intros; apply (fold_right_equivlistA_restr2 (R:=fun _ _ => True));
repeat red; auto.
apply ForallPairs_ForallOrdPairs; try red; auto.
Qed.
Lemma fold_right_add2 :
forall s' s i j x, NoDupA s -> NoDupA s' -> eqB i j -> ~ InA x s ->
equivlistA s' (x::s) -> eqB (fold_right f i s') (f x (fold_right f j s)).
Proof.
intros s' s i j x **.
replace (f x (fold_right f j s)) with (fold_right f j (x::s)) by auto.
eapply fold_right_equivlistA2;auto.
Qed.
End Fold2.
Section Remove.
Hypothesis eqA_dec : forall x y : A, {eqA x y}+{~(eqA x y)}.
Lemma InA_dec : forall x l, { InA x l } + { ~ InA x l }.
Proof.
intros x l; induction l as [|a l IHl].
- right; auto.
intro; inv.
- destruct (eqA_dec x a).
+ left; auto.
+ destruct IHl.
* left; auto.
* right; intro; inv; contradiction.
Defined.
Fixpoint removeA (x : A) (l : list A) : list A :=
match l with
| nil => nil
| y::tl => if (eqA_dec x y) then removeA x tl else y::(removeA x tl)
end.
Lemma removeA_filter : forall x l,
removeA x l = filter (fun y => if eqA_dec x y then false else true) l.
Proof.
intros x l; induction l as [|a l IHl]; simpl; auto.
destruct (eqA_dec x a); auto.
rewrite IHl; auto.
Qed.
Lemma removeA_InA : forall l x y, InA y (removeA x l) <-> InA y l /\ ~eqA x y.
Proof.
intros l; induction l as [|a l IHl]; simpl; auto.
- intros x y; split.
+ intro; inv.
+ destruct 1; inv.
- intros x y.
destruct (eqA_dec x a) as [Heq|Hnot]; simpl; auto.
+ rewrite IHl; split; destruct 1; split; auto.
inv; auto.
match goal with H0 : ~ eqA x y |- _ => destruct H0 end; transitivity a; auto.
+ split.
* intro; inv.
-- split; auto.
contradict Hnot.
transitivity y; auto.
-- match goal with H0 : InA y (removeA x l) |- _ =>
rewrite (IHl x y) in H0; destruct H0; auto
end.
* destruct 1; inv; auto.
right; rewrite IHl; auto.
Qed.
Lemma removeA_NoDupA :
forall s x, NoDupA s -> NoDupA (removeA x s).
Proof.
intros s; induction s as [|a s IHs]; simpl; intros x ?.
- auto.
- inv.
destruct (eqA_dec x a); simpl; auto.
constructor; auto.
rewrite removeA_InA.
intuition.
Qed.
Lemma removeA_equivlistA : forall l l' x,
~InA x l -> equivlistA (x :: l) l' -> equivlistA l (removeA x l').
Proof.
unfold equivlistA; intros l l' x H H0 x0.
rewrite removeA_InA.
split; intros H1.
- rewrite <- H0; split; auto.
contradict H.
apply InA_eqA with x0; auto.
- rewrite <- (H0 x0) in H1.
destruct H1.
inv; auto.
match goal with H2 : ~ eqA x x0 |- _ => elim H2; auto end.
Qed.
End Remove.
(** Results concerning lists modulo [eqA] and [ltA] *)
Variable ltA : A -> A -> Prop.
Hypothesis ltA_strorder : StrictOrder ltA.
Hypothesis ltA_compat : Proper (eqA==>eqA==>iff) ltA.
Let sotrans := (@StrictOrder_Transitive _ _ ltA_strorder).
#[local]
Hint Resolve sotrans : core.
Notation InfA:=(lelistA ltA).
Notation SortA:=(sort ltA).
#[local]
Hint Constructors lelistA sort : core.
Lemma InfA_ltA :
forall l x y, ltA x y -> InfA y l -> InfA x l.
Proof.
intros l; destruct l; constructor. inv; eauto.
Qed.
Global Instance InfA_compat : Proper (eqA==>eqlistA==>iff) InfA.
Proof using eqA_equiv ltA_compat. (* and not ltA_strorder *)
intros x x' Hxx' l l' Hll'.
inversion_clear Hll'.
- intuition.
- split; intro; inv; constructor.
+ match goal with H : eqA _ _ |- _ => rewrite <- Hxx', <- H; auto end.
+ match goal with H : eqA _ _ |- _ => rewrite Hxx', H; auto end.
Qed.
(** For compatibility, can be deduced from [InfA_compat] *)
Lemma InfA_eqA l x y : eqA x y -> InfA y l -> InfA x l.
Proof using eqA_equiv ltA_compat.
intros H; now rewrite H.
Qed.
#[local]
Hint Immediate InfA_ltA InfA_eqA : core.
Lemma SortA_InfA_InA :
forall l x a, SortA l -> InfA a l -> InA x l -> ltA a x.
Proof.
intros l; induction l as [|a l IHl].
- intros x a **. inv.
- intros x a0 **. inv.
+ setoid_replace x with a; auto.
+ eauto.
Qed.
Lemma In_InfA :
forall l x, (forall y, In y l -> ltA x y) -> InfA x l.
Proof.
intros l; induction l; simpl; intros; constructor; auto.
Qed.
Lemma InA_InfA :
forall l x, (forall y, InA y l -> ltA x y) -> InfA x l.
Proof.
intros l; induction l; simpl; intros; constructor; auto.
Qed.
(* In fact, this may be used as an alternative definition for InfA: *)
Lemma InfA_alt :
forall l x, SortA l -> (InfA x l <-> (forall y, InA y l -> ltA x y)).
Proof.
split.
- intros; eapply SortA_InfA_InA; eauto.
- apply InA_InfA.
Qed.
Lemma InfA_app : forall l1 l2 a, InfA a l1 -> InfA a l2 -> InfA a (l1++l2).
Proof.
intros l1; induction l1; simpl; auto.
intros; inv; auto.
Qed.
Lemma SortA_app :
forall l1 l2, SortA l1 -> SortA l2 ->
(forall x y, InA x l1 -> InA y l2 -> ltA x y) ->
SortA (l1 ++ l2).
Proof.
intros l1; induction l1; intros l2; simpl in *; intuition.
inv.
constructor; auto.
apply InfA_app; auto.
destruct l2; auto.
Qed.
Lemma SortA_NoDupA : forall l, SortA l -> NoDupA l.
Proof.
intros l; induction l as [|x l' H]; auto.
intros H0.
inv.
constructor; auto.
intro.
apply (StrictOrder_Irreflexive x).
eapply SortA_InfA_InA; eauto.
Qed.
(** Some results about [eqlistA] *)
Section EqlistA.
Lemma eqlistA_length : forall l l', eqlistA l l' -> length l = length l'.
Proof.
induction 1; auto; simpl; congruence.
Qed.
Global Instance app_eqlistA_compat :
Proper (eqlistA==>eqlistA==>eqlistA) (@app A).
Proof.
repeat red; induction 1; simpl; auto.
Qed.
(** For compatibility, can be deduced from app_eqlistA_compat **)
Lemma eqlistA_app : forall l1 l1' l2 l2',
eqlistA l1 l1' -> eqlistA l2 l2' -> eqlistA (l1++l2) (l1'++l2').
Proof.
intros l1 l1' l2 l2' H H'; rewrite H, H'; reflexivity.
Qed.
Lemma eqlistA_rev_app : forall l1 l1',
eqlistA l1 l1' -> forall l2 l2', eqlistA l2 l2' ->
eqlistA ((rev l1)++l2) ((rev l1')++l2').
Proof.
induction 1; auto.
simpl; intros.
do 2 rewrite <- app_assoc; simpl; auto.
Qed.
Global Instance rev_eqlistA_compat : Proper (eqlistA==>eqlistA) (@rev A).
Proof.
repeat red. intros x y ?.
rewrite <- (app_nil_r (rev x)), <- (app_nil_r (rev y)).
apply eqlistA_rev_app; auto.
Qed.
Lemma eqlistA_rev : forall l1 l1',
eqlistA l1 l1' -> eqlistA (rev l1) (rev l1').
Proof.
apply rev_eqlistA_compat.
Qed.
Lemma SortA_equivlistA_eqlistA : forall l l',
SortA l -> SortA l' -> equivlistA l l' -> eqlistA l l'.
Proof.
intros l; induction l as [|a l IHl]; intros l'; destruct l' as [|a0 l']; simpl; intros H H0 H1; auto.
- destruct (H1 a0); assert (InA a0 nil) by auto; inv.
- destruct (H1 a); assert (InA a nil) by auto; inv.
- inv.
assert (forall y, InA y l -> ltA a y) by
(intros; eapply (SortA_InfA_InA (l:=l)); eauto).
assert (forall y, InA y l' -> ltA a0 y) by
(intros; eapply (SortA_InfA_InA (l:=l')); eauto).
do 2 match goal with H : InfA _ _ |- _ => clear H end.
assert (eqA a a0).
+ destruct (H1 a).
destruct (H1 a0).
assert (InA a (a0::l')) by auto. inv; auto.
assert (InA a0 (a::l)) by auto. inv; auto.
elim (StrictOrder_Irreflexive a); eauto.
+ constructor; auto.
apply IHl; auto.
intros x; split; intros.
* destruct (H1 x).
assert (InA x (a0::l')) by auto. inv; auto.
match goal with H3 : eqA a a0, H4 : InA x l, H9 : eqA x a0 |- InA x l' =>
rewrite H9,<-H3 in H4
end.
elim (StrictOrder_Irreflexive a); eauto.
* destruct (H1 x).
assert (InA x (a::l)) by auto. inv; auto.
match goal with H3 : eqA a a0, H4 : InA x l', H9 : eqA x a |- InA x l =>
rewrite H9,H3 in H4
end.
elim (StrictOrder_Irreflexive a0); eauto.
Qed.
End EqlistA.
(** A few things about [filter] *)
Section Filter.
Lemma filter_sort : forall f l, SortA l -> SortA (List.filter f l).
Proof.
intros f l; induction l as [|a l IHl]; simpl; auto.
intros; inv; auto.
destruct (f a); auto.
constructor; auto.
apply In_InfA; auto.
intros y H.
rewrite filter_In in H; destruct H.
eapply SortA_InfA_InA; eauto.
Qed.
Arguments eq {A} x _.
Lemma filter_InA : forall f, Proper (eqA==>eq) f ->
forall l x, InA x (List.filter f l) <-> InA x l /\ f x = true.
Proof.
(* Unset Mangle Names. *)
clear sotrans ltA ltA_strorder ltA_compat.
intros f H l x; do 2 rewrite InA_alt; intuition;
match goal with Hex' : exists _, _ |- _ => rename Hex' into Hex end.
- destruct Hex as (y,(H0,H1)); rewrite filter_In in H1; exists y; intuition.
- destruct Hex as (y,(H0,H1)); rewrite filter_In in H1; intuition.
rewrite (H _ _ H0); auto.
- destruct Hex as (y,(H0,H1)); exists y; rewrite filter_In; intuition.
rewrite <- (H _ _ H0); auto.
Qed.
Lemma filter_split :
forall f, (forall x y, f x = true -> f y = false -> ltA x y) ->
forall l, SortA l -> l = filter f l ++ filter (fun x=>negb (f x)) l.
Proof.
intros f H l; induction l as [|a l IHl]; simpl; intros H0; auto.
inv.
match goal with H1' : SortA l, H2' : InfA a l |- _ => rename H1' into H1, H2' into H2 end.
rewrite IHl at 1; auto.
case_eq (f a); simpl; intros; auto.
assert (forall e, In e l -> f e = false) as H3. {
intros e H3.
assert (H4:=SortA_InfA_InA H1 H2 (In_InA H3)).
case_eq (f e); simpl; intros; auto.
elim (StrictOrder_Irreflexive e).
transitivity a; auto.
}
replace (List.filter f l) with (@nil A); auto.
generalize H3; clear; induction l as [|a l IHl]; simpl; auto.
case_eq (f a); auto; intros H H3.
rewrite H3 in H; auto; try discriminate.
Qed.
End Filter.
End Type_with_equality.
#[global]
Hint Constructors InA eqlistA NoDupA sort lelistA : core.
Arguments equivlistA_cons_nil {A} eqA {eqA_equiv} x l _.
Arguments equivlistA_nil_eq {A} eqA {eqA_equiv} l _.
Section Find.
Variable A B : Type.
Variable eqA : A -> A -> Prop.
Hypothesis eqA_equiv : Equivalence eqA.
Hypothesis eqA_dec : forall x y : A, {eqA x y}+{~(eqA x y)}.
Fixpoint findA (f : A -> bool) (l:list (A*B)) : option B :=
match l with
| nil => None
| (a,b)::l => if f a then Some b else findA f l
end.
Lemma findA_NoDupA :
forall l a b,
NoDupA (fun p p' => eqA (fst p) (fst p')) l ->
(InA (fun p p' => eqA (fst p) (fst p') /\ snd p = snd p') (a,b) l <->
findA (fun a' => if eqA_dec a a' then true else false) l = Some b).
Proof.
set (eqk := fun p p' : A*B => eqA (fst p) (fst p')).
set (eqke := fun p p' : A*B => eqA (fst p) (fst p') /\ snd p = snd p').
intros l; induction l as [|a l IHl]; intros a0 b H; simpl.
- split; intros H0; try discriminate.
invlist InA.
- destruct a as (a',b'); rename a0 into a.
invlist NoDupA.
split; intros.
+ invlist InA.
* match goal with H2 : eqke (a, b) (a', b') |- _ => compute in H2; destruct H2 end.
subst b'.
destruct (eqA_dec a a'); intuition.
* destruct (eqA_dec a a') as [HeqA|]; simpl.
-- match goal with H0 : ~ InA eqk (a', b') l |- _ => contradict H0 end.
match goal with H2 : InA eqke (a, b) l |- _ => revert HeqA H2; clear - eqA_equiv end.
induction l.
++ intros; invlist InA.
++ intros; invlist InA; auto.
match goal with |- InA eqk _ (?p :: _) => destruct p as [a0 b0] end.
match goal with H : eqke (a, b) (a0, b0) |- _ => compute in H; destruct H end.
subst b.
left; auto.
compute.
transitivity a; auto. symmetry; auto.
-- rewrite <- IHl; auto.
+ destruct (eqA_dec a a'); simpl in *.
* left; split; simpl; congruence.
* right. rewrite IHl; auto.
Qed.
End Find.
(** Compatibility aliases. [Proper] is rather to be used directly now.*)
Definition compat_bool {A} (eqA:A->A->Prop)(f:A->bool) :=
Proper (eqA==>Logic.eq) f.
Definition compat_nat {A} (eqA:A->A->Prop)(f:A->nat) :=
Proper (eqA==>Logic.eq) f.
Definition compat_P {A} (eqA:A->A->Prop)(P:A->Prop) :=
Proper (eqA==>impl) P.
Definition compat_op {A B} (eqA:A->A->Prop)(eqB:B->B->Prop)(f:A->B->B) :=
Proper (eqA==>eqB==>eqB) f.
|