| 1 |
(* Compat file for newer results from the stdlib *) |
| 2 |
From Coq Require Import Lia. |
| 3 |
From Coq Require Import Arith. |
| 4 |
From Coq Require Import Permutation. |
| 5 |
From Coq Require Import List. |
| 6 |
Require Export Crypto.Util.FixCoqMistakes. |
| 7 |
Import ListNotations. |
| 8 |
Local Open Scope bool_scope. |
| 9 |
Local Open Scope list_scope. |
| 10 |
Local Set Implicit Arguments. |
| 11 |
|
| 12 |
Module Export List. |
| 13 |
Section Facts. |
| 14 |
|
| 15 |
Variable A : Type. |
| 16 |
|
| 17 |
(** *** Generic facts *) |
| 18 |
|
| 19 |
(** Destruction *) |
| 20 |
|
| 21 |
(** *** Head and tail *) |
| 22 |
(**************************) |
| 23 |
(** *** Facts about [app] *) |
| 24 |
(**************************) |
| 25 |
|
| 26 |
Lemma elt_eq_unit l1 l2 (a b : A) : |
| 27 |
l1 ++ a :: l2 = [b] -> a = b /\ l1 = [] /\ l2 = []. |
| 28 |
Proof using Type. |
| 29 |
intros Heq. |
| 30 |
apply app_eq_unit in Heq. |
| 31 |
now destruct Heq as [[Heq1 Heq2]|[Heq1 Heq2]]; inversion_clear Heq2. |
| 32 |
Qed. |
| 33 |
|
| 34 |
Theorem app_eq_app X (x1 x2 y1 y2: list X) : x1++x2 = y1++y2 -> |
| 35 |
exists l, (x1 = y1++l /\ y2 = l++x2) \/ (y1 = x1++l /\ x2 = l++y2). |
| 36 |
Proof using Type. |
| 37 |
revert y1. induction x1 as [|a x1 IH]. |
| 38 |
- cbn. intros y1 ->. exists y1. now right. |
| 39 |
- intros [|b y1]; cbn. |
| 40 |
+ intros <-. exists (a :: x1). now left. |
| 41 |
+ intros [=-> [l Hl] %IH]. exists l. |
| 42 |
now destruct Hl as [[-> ->]|[-> ->]]; [left|right]. |
| 43 |
Qed. |
| 44 |
|
| 45 |
Lemma app_inj_tail_iff : |
| 46 |
forall (x y:list A) (a b:A), x ++ [a] = y ++ [b] <-> x = y /\ a = b. |
| 47 |
Proof using Type. |
| 48 |
intros. now split; [apply app_inj_tail|intros [-> ->]]. |
| 49 |
Qed. |
| 50 |
|
| 51 |
(** Compatibility with other operations *) |
| 52 |
|
| 53 |
Lemma last_length : forall (l : list A) a, length (l ++ a :: nil) = S (length l). |
| 54 |
Proof using Type. |
| 55 |
intros ; rewrite app_length ; simpl. |
| 56 |
rewrite Nat.add_succ_r, Nat.add_0_r; reflexivity. |
| 57 |
Qed. |
| 58 |
|
| 59 |
Lemma app_inv_head_iff: |
| 60 |
forall l l1 l2 : list A, l ++ l1 = l ++ l2 <-> l1 = l2. |
| 61 |
Proof using Type. |
| 62 |
intro l; induction l as [|? l IHl]; split; intros H; simpl; auto. |
| 63 |
- apply IHl. inversion H. auto. |
| 64 |
- subst. auto. |
| 65 |
Qed. |
| 66 |
|
| 67 |
Lemma app_inv_tail_iff: |
| 68 |
forall l l1 l2 : list A, l1 ++ l = l2 ++ l <-> l1 = l2. |
| 69 |
Proof using Type. |
| 70 |
split; [apply app_inv_tail | now intros ->]. |
| 71 |
Qed. |
| 72 |
|
| 73 |
(************************) |
| 74 |
(** *** Facts about [In] *) |
| 75 |
(************************) |
| 76 |
|
| 77 |
|
| 78 |
(** Characterization of [In] *) |
| 79 |
|
| 80 |
Lemma in_elt : forall (x:A) l1 l2, In x (l1 ++ x :: l2). |
| 81 |
Proof using Type. |
| 82 |
intros. |
| 83 |
apply in_or_app. |
| 84 |
right; left; reflexivity. |
| 85 |
Qed. |
| 86 |
|
| 87 |
Lemma in_elt_inv : forall (x y : A) l1 l2, |
| 88 |
In x (l1 ++ y :: l2) -> x = y \/ In x (l1 ++ l2). |
| 89 |
Proof using Type. |
| 90 |
intros x y l1 l2 Hin. |
| 91 |
apply in_app_or in Hin. |
| 92 |
destruct Hin as [Hin|[Hin|Hin]]; [right|left|right]; try apply in_or_app; intuition. |
| 93 |
Qed. |
| 94 |
|
| 95 |
End Facts. |
| 96 |
|
| 97 |
(*******************************************) |
| 98 |
(** * Operations on the elements of a list *) |
| 99 |
(*******************************************) |
| 100 |
|
| 101 |
Section Elts. |
| 102 |
|
| 103 |
Variable A : Type. |
| 104 |
|
| 105 |
(*****************************) |
| 106 |
(** ** Nth element of a list *) |
| 107 |
(*****************************) |
| 108 |
|
| 109 |
|
| 110 |
(** Results about [nth] *) |
| 111 |
|
| 112 |
Lemma app_nth2_plus : forall l l' d n, |
| 113 |
@nth A (length l + n) (l ++ l') d = nth n l' d. |
| 114 |
Proof using Type. |
| 115 |
intros. |
| 116 |
now rewrite app_nth2, Nat.add_comm, Nat.add_sub; [|apply Nat.le_add_r]. |
| 117 |
Qed. |
| 118 |
|
| 119 |
Lemma nth_middle : forall l l' a d, |
| 120 |
@nth A (length l) (l ++ a :: l') d = a. |
| 121 |
Proof using Type. |
| 122 |
intros. |
| 123 |
rewrite <- Nat.add_0_r at 1. |
| 124 |
apply app_nth2_plus. |
| 125 |
Qed. |
| 126 |
|
| 127 |
Lemma nth_ext : forall l l' d d', length l = length l' -> |
| 128 |
(forall n, n < length l -> @nth A n l d = nth n l' d') -> l = l'. |
| 129 |
Proof using Type. |
| 130 |
intro l; induction l as [|a l IHl]; |
| 131 |
intros l' d d' Hlen Hnth; destruct l' as [| b l']. |
| 132 |
- reflexivity. |
| 133 |
- inversion Hlen. |
| 134 |
- inversion Hlen. |
| 135 |
- change a with (nth 0 (a :: l) d). |
| 136 |
change b with (nth 0 (b :: l') d'). |
| 137 |
rewrite Hnth; f_equal. |
| 138 |
+ apply IHl with d d'; [ now inversion Hlen | ]. |
| 139 |
intros n Hlen'; apply (Hnth (S n)). |
| 140 |
now apply (Nat.succ_lt_mono n (length l)). |
| 141 |
+ simpl; apply Nat.lt_0_succ. |
| 142 |
Qed. |
| 143 |
|
| 144 |
(** Results about [nth_error] *) |
| 145 |
|
| 146 |
(** Results directly relating [nth] and [nth_error] *) |
| 147 |
|
| 148 |
(******************************) |
| 149 |
(** ** Last element of a list *) |
| 150 |
(******************************) |
| 151 |
|
| 152 |
(** [last l d] returns the last element of the list [l], |
| 153 |
or the default value [d] if [l] is empty. *) |
| 154 |
|
| 155 |
Lemma last_last : forall l a d, @last A (l ++ [a]) d = a. |
| 156 |
Proof using Type. |
| 157 |
intro l; induction l as [|? l IHl]; intros; [ reflexivity | ]. |
| 158 |
simpl; rewrite IHl. |
| 159 |
destruct l; reflexivity. |
| 160 |
Qed. |
| 161 |
|
| 162 |
(** [removelast l] remove the last element of [l] *) |
| 163 |
|
| 164 |
Lemma removelast_last : forall l a, @removelast A (l ++ [a]) = l. |
| 165 |
Proof using Type. |
| 166 |
intros. rewrite removelast_app. |
| 167 |
- apply app_nil_r. |
| 168 |
- intros Heq; inversion Heq. |
| 169 |
Qed. |
| 170 |
|
| 171 |
|
| 172 |
(*****************) |
| 173 |
(** ** Remove *) |
| 174 |
(*****************) |
| 175 |
|
| 176 |
Hypothesis eq_dec : forall x y : A, {x = y}+{x <> y}. |
| 177 |
|
| 178 |
Local Notation remove := (@remove A eq_dec). |
| 179 |
|
| 180 |
Lemma remove_cons : forall x l, remove x (x :: l) = remove x l. |
| 181 |
Proof using Type. |
| 182 |
intros x l; simpl; destruct (eq_dec x x); [ reflexivity | now exfalso ]. |
| 183 |
Qed. |
| 184 |
|
| 185 |
Lemma remove_app : forall x l1 l2, |
| 186 |
remove x (l1 ++ l2) = remove x l1 ++ remove x l2. |
| 187 |
Proof using Type. |
| 188 |
intros x l1; induction l1 as [|a l1 IHl1]; intros l2; simpl. |
| 189 |
- reflexivity. |
| 190 |
- destruct (eq_dec x a). |
| 191 |
+ apply IHl1. |
| 192 |
+ rewrite <- app_comm_cons; f_equal. |
| 193 |
apply IHl1. |
| 194 |
Qed. |
| 195 |
|
| 196 |
Lemma notin_remove: forall l x, ~ In x l -> remove x l = l. |
| 197 |
Proof using Type. |
| 198 |
intros l x; induction l as [|y l IHl]; simpl; intros Hnin. |
| 199 |
- reflexivity. |
| 200 |
- destruct (eq_dec x y); [subst|f_equal]; tauto. |
| 201 |
Qed. |
| 202 |
|
| 203 |
Lemma in_remove: forall l x y, In x (remove y l) -> In x l /\ x <> y. |
| 204 |
Proof using Type. |
| 205 |
intro l; induction l as [|z l IHl]; intros x y Hin. |
| 206 |
- inversion Hin. |
| 207 |
- simpl in Hin. |
| 208 |
destruct (eq_dec y z) as [Heq|Hneq]; subst; split. |
| 209 |
+ right; now apply IHl with z. |
| 210 |
+ intros Heq; revert Hin; subst; apply remove_In. |
| 211 |
+ inversion Hin; subst; [left; reflexivity|right]. |
| 212 |
now apply IHl with y. |
| 213 |
+ destruct Hin as [Hin|Hin]; subst. |
| 214 |
* now intros Heq; apply Hneq. |
| 215 |
* intros Heq; revert Hin; subst; apply remove_In. |
| 216 |
Qed. |
| 217 |
|
| 218 |
Lemma in_in_remove : forall l x y, x <> y -> In x l -> In x (remove y l). |
| 219 |
Proof using Type. |
| 220 |
intro l; induction l as [|z l IHl]; simpl; intros x y Hneq Hin. |
| 221 |
- apply Hin. |
| 222 |
- destruct (eq_dec y z); subst. |
| 223 |
+ destruct Hin. |
| 224 |
* exfalso; now apply Hneq. |
| 225 |
* now apply IHl. |
| 226 |
+ simpl; destruct Hin; [now left|right]. |
| 227 |
now apply IHl. |
| 228 |
Qed. |
| 229 |
|
| 230 |
Lemma remove_remove_comm : forall l x y, |
| 231 |
remove x (remove y l) = remove y (remove x l). |
| 232 |
Proof using Type. |
| 233 |
intro l; induction l as [| z l IHl]; simpl; intros x y. |
| 234 |
- reflexivity. |
| 235 |
- destruct (eq_dec y z); simpl; destruct (eq_dec x z); try rewrite IHl; auto. |
| 236 |
+ subst; symmetry; apply remove_cons. |
| 237 |
+ simpl; destruct (eq_dec y z); tauto. |
| 238 |
Qed. |
| 239 |
|
| 240 |
Lemma remove_remove_eq : forall l x, remove x (remove x l) = remove x l. |
| 241 |
Proof using Type. intros l x; now rewrite (notin_remove _ _ (remove_In eq_dec l x)). Qed. |
| 242 |
|
| 243 |
Lemma remove_length_le : forall l x, length (remove x l) <= length l. |
| 244 |
Proof using Type. |
| 245 |
intro l; induction l as [|y l IHl]; simpl; intros x; trivial. |
| 246 |
destruct (eq_dec x y); simpl. |
| 247 |
- rewrite IHl; constructor; reflexivity. |
| 248 |
- apply (proj1 (Nat.succ_le_mono _ _) (IHl x)). |
| 249 |
Qed. |
| 250 |
|
| 251 |
Lemma remove_length_lt : forall l x, In x l -> length (remove x l) < length l. |
| 252 |
Proof using Type. |
| 253 |
intro l; induction l as [|y l IHl]; simpl; intros x Hin. |
| 254 |
- contradiction Hin. |
| 255 |
- destruct Hin as [-> | Hin]. |
| 256 |
+ destruct (eq_dec x x); [|easy]. |
| 257 |
apply Nat.lt_succ_r, remove_length_le. |
| 258 |
+ specialize (IHl _ Hin); destruct (eq_dec x y); simpl; auto. |
| 259 |
now apply Nat.succ_lt_mono in IHl. |
| 260 |
Qed. |
| 261 |
|
| 262 |
|
| 263 |
(******************************************) |
| 264 |
(** ** Counting occurrences of an element *) |
| 265 |
(******************************************) |
| 266 |
|
| 267 |
(** Compatibility of count_occ with operations on list *) |
| 268 |
Local Notation count_occ := (@count_occ A eq_dec). |
| 269 |
|
| 270 |
Lemma count_occ_app l1 l2 x : |
| 271 |
count_occ (l1 ++ l2) x = count_occ l1 x + count_occ l2 x. |
| 272 |
Proof using Type. |
| 273 |
induction l1 as [ | h l1 IHl1]; cbn; trivial. |
| 274 |
now destruct (eq_dec h x); [ rewrite IHl1 | ]. |
| 275 |
Qed. |
| 276 |
|
| 277 |
Lemma count_occ_elt_eq l1 l2 x y : x = y -> |
| 278 |
count_occ (l1 ++ x :: l2) y = S (count_occ (l1 ++ l2) y). |
| 279 |
Proof using Type. |
| 280 |
intros ->. |
| 281 |
rewrite ? count_occ_app; cbn. |
| 282 |
destruct (eq_dec y y) as [Heq | Hneq]; |
| 283 |
[ apply Nat.add_succ_r | now contradiction Hneq ]. |
| 284 |
Qed. |
| 285 |
|
| 286 |
Lemma count_occ_elt_neq l1 l2 x y : x <> y -> |
| 287 |
count_occ (l1 ++ x :: l2) y = count_occ (l1 ++ l2) y. |
| 288 |
Proof using Type. |
| 289 |
intros Hxy. |
| 290 |
rewrite ? count_occ_app; cbn. |
| 291 |
now destruct (eq_dec x y) as [Heq | Hneq]; [ contradiction Hxy | ]. |
| 292 |
Qed. |
| 293 |
|
| 294 |
Lemma count_occ_bound x l : count_occ l x <= length l. |
| 295 |
Proof using Type. |
| 296 |
induction l as [|h l]; cbn; auto. |
| 297 |
destruct (eq_dec h x); [ apply (proj1 (Nat.succ_le_mono _ _)) | ]; intuition. |
| 298 |
Qed. |
| 299 |
|
| 300 |
End Elts. |
| 301 |
|
| 302 |
(*******************************) |
| 303 |
(** * Manipulating whole lists *) |
| 304 |
(*******************************) |
| 305 |
|
| 306 |
Section ListOps. |
| 307 |
|
| 308 |
Variable A : Type. |
| 309 |
|
| 310 |
(*************************) |
| 311 |
(** ** Reverse *) |
| 312 |
(*************************) |
| 313 |
Local Notation rev := (@rev A). |
| 314 |
Lemma rev_eq_app : forall l l1 l2, rev l = l1 ++ l2 -> l = rev l2 ++ rev l1. |
| 315 |
Proof using Type. |
| 316 |
intros l l1 l2 Heq. |
| 317 |
rewrite <- (rev_involutive l), Heq. |
| 318 |
apply rev_app_distr. |
| 319 |
Qed. |
| 320 |
|
| 321 |
(*********************************************) |
| 322 |
(** Reverse Induction Principle on Lists *) |
| 323 |
(*********************************************) |
| 324 |
|
| 325 |
(** Compatibility with other operations *) |
| 326 |
|
| 327 |
(** An alternative tail-recursive definition for reverse *) |
| 328 |
|
| 329 |
(*************************) |
| 330 |
(** ** Concatenation *) |
| 331 |
(*************************) |
| 332 |
|
| 333 |
Local Notation concat := (@concat A). |
| 334 |
|
| 335 |
Lemma in_concat : forall l y, |
| 336 |
In y (concat l) <-> exists x, In x l /\ In y x. |
| 337 |
Proof using Type. |
| 338 |
intro l; induction l as [|a l IHl]; simpl; intro y; split; intros H. |
| 339 |
contradiction. |
| 340 |
destruct H as (x,(H,_)); contradiction. |
| 341 |
destruct (in_app_or _ _ _ H) as [H0|H0]. |
| 342 |
exists a; auto. |
| 343 |
destruct (IHl y) as (H1,_); destruct (H1 H0) as (x,(H2,H3)). |
| 344 |
exists x; auto. |
| 345 |
apply in_or_app. |
| 346 |
destruct H as (x,(H0,H1)); destruct H0. |
| 347 |
subst; auto. |
| 348 |
right; destruct (IHl y) as (_,H2); apply H2. |
| 349 |
exists x; auto. |
| 350 |
Qed. |
| 351 |
|
| 352 |
|
| 353 |
(***********************************) |
| 354 |
(** ** Decidable equality on lists *) |
| 355 |
(***********************************) |
| 356 |
|
| 357 |
Hypothesis eq_dec : forall (x y : A), {x = y}+{x <> y}. |
| 358 |
|
| 359 |
Lemma count_occ_rev l x : count_occ eq_dec (rev l) x = count_occ eq_dec l x. |
| 360 |
Proof using Type. |
| 361 |
induction l as [|a l IHl]; trivial. |
| 362 |
cbn; rewrite count_occ_app, IHl; cbn. |
| 363 |
destruct (eq_dec a x); rewrite Nat.add_comm; reflexivity. |
| 364 |
Qed. |
| 365 |
|
| 366 |
End ListOps. |
| 367 |
|
| 368 |
(***************************************************) |
| 369 |
(** * Applying functions to the elements of a list *) |
| 370 |
(***************************************************) |
| 371 |
|
| 372 |
(************) |
| 373 |
(** ** Map *) |
| 374 |
(************) |
| 375 |
|
| 376 |
Section Map. |
| 377 |
Variables (A : Type) (B : Type). |
| 378 |
Variable f : A -> B. |
| 379 |
|
| 380 |
Local Notation map := (@map A B f). |
| 381 |
|
| 382 |
Lemma nth_error_map : forall n l, |
| 383 |
nth_error (map l) n = option_map f (nth_error l n). |
| 384 |
Proof using Type. |
| 385 |
intro n. induction n as [|n IHn]; intro l. |
| 386 |
- now destruct l. |
| 387 |
- destruct l as [|? l]; [reflexivity|exact (IHn l)]. |
| 388 |
Qed. |
| 389 |
|
| 390 |
Lemma map_last : forall l a, |
| 391 |
map (l ++ [a]) = (map l) ++ [f a]. |
| 392 |
Proof using Type. |
| 393 |
intro l; induction l as [|a l IHl]; intros; [ reflexivity | ]. |
| 394 |
simpl; rewrite IHl; reflexivity. |
| 395 |
Qed. |
| 396 |
|
| 397 |
Lemma map_eq_cons : forall l l' b, |
| 398 |
map l = b :: l' -> exists a tl, l = a :: tl /\ f a = b /\ map tl = l'. |
| 399 |
Proof using Type. |
| 400 |
intros l l' b Heq. |
| 401 |
destruct l as [|a l]; inversion_clear Heq. |
| 402 |
exists a, l; repeat split. |
| 403 |
Qed. |
| 404 |
|
| 405 |
Lemma map_eq_app : forall l l1 l2, |
| 406 |
map l = l1 ++ l2 -> exists l1' l2', l = l1' ++ l2' /\ map l1' = l1 /\ map l2' = l2. |
| 407 |
Proof using Type. |
| 408 |
intro l; induction l as [|a l IHl]; simpl; intros l1 l2 Heq. |
| 409 |
- symmetry in Heq; apply app_eq_nil in Heq; destruct Heq; subst. |
| 410 |
exists nil, nil; repeat split. |
| 411 |
- destruct l1; simpl in Heq; inversion Heq as [[Heq2 Htl]]. |
| 412 |
+ exists nil, (a :: l); repeat split. |
| 413 |
+ destruct (IHl _ _ Htl) as (l1' & l2' & ? & ? & ?); subst. |
| 414 |
exists (a :: l1'), l2'; repeat split. |
| 415 |
Qed. |
| 416 |
|
| 417 |
(** [map] and count of occurrences *) |
| 418 |
|
| 419 |
End Map. |
| 420 |
|
| 421 |
(*****************) |
| 422 |
(** ** Flat Map *) |
| 423 |
(*****************) |
| 424 |
|
| 425 |
Section FlatMap. |
| 426 |
Variables (A : Type) (B : Type). |
| 427 |
Variable f : A -> list B. |
| 428 |
|
| 429 |
(** [flat_map] *) |
| 430 |
|
| 431 |
Local Notation flat_map := (@flat_map A B f). |
| 432 |
|
| 433 |
Lemma flat_map_app l1 l2 : |
| 434 |
flat_map (l1 ++ l2) = flat_map l1 ++ flat_map l2. |
| 435 |
Proof using Type. |
| 436 |
now rewrite !flat_map_concat_map, map_app, concat_app. |
| 437 |
Qed. |
| 438 |
|
| 439 |
End FlatMap. |
| 440 |
|
| 441 |
Lemma remove_concat A (eq_dec : forall x y : A, {x = y}+{x <> y}) : forall l x, |
| 442 |
remove eq_dec x (concat l) = flat_map (remove eq_dec x) l. |
| 443 |
Proof using Type. |
| 444 |
intros l x; induction l as [|? ? IHl]; [ reflexivity | simpl ]. |
| 445 |
rewrite remove_app, IHl; reflexivity. |
| 446 |
Qed. |
| 447 |
|
| 448 |
Lemma flat_map_ext : forall (A B : Type)(f g : A -> list B), |
| 449 |
(forall a, f a = g a) -> forall l, flat_map f l = flat_map g l. |
| 450 |
Proof using Type. |
| 451 |
intros A B f g Hext l. |
| 452 |
rewrite 2 flat_map_concat_map. |
| 453 |
now rewrite (map_ext _ g). |
| 454 |
Qed. |
| 455 |
|
| 456 |
Lemma nth_nth_nth_map A : forall (l : list A) n d ln dn, n < length ln \/ length l <= dn -> |
| 457 |
nth (nth n ln dn) l d = nth n (map (fun x => nth x l d) ln) d. |
| 458 |
Proof using Type. |
| 459 |
intros l n d ln dn Hlen. |
| 460 |
rewrite <- (map_nth (fun m => nth m l d)). |
| 461 |
destruct Hlen. |
| 462 |
- apply nth_indep. now rewrite map_length. |
| 463 |
- now rewrite (nth_overflow l). |
| 464 |
Qed. |
| 465 |
|
| 466 |
|
| 467 |
(************************************) |
| 468 |
(** Left-to-right iterator on lists *) |
| 469 |
(************************************) |
| 470 |
|
| 471 |
(************************************) |
| 472 |
(** Right-to-left iterator on lists *) |
| 473 |
(************************************) |
| 474 |
|
| 475 |
(*************************************) |
| 476 |
(** ** Boolean operations over lists *) |
| 477 |
(*************************************) |
| 478 |
|
| 479 |
(*******************************) |
| 480 |
(** ** Further filtering facts *) |
| 481 |
(*******************************) |
| 482 |
|
| 483 |
Section Filtering. |
| 484 |
Variables (A : Type). |
| 485 |
|
| 486 |
(** Remove by filtering *) |
| 487 |
|
| 488 |
Hypothesis eq_dec : forall x y : A, {x = y}+{x <> y}. |
| 489 |
|
| 490 |
Definition remove' (x : A) : list A -> list A := |
| 491 |
filter (fun y => if eq_dec x y then false else true). |
| 492 |
|
| 493 |
Lemma remove_alt (x : A) (l : list A) : remove' x l = remove eq_dec x l. |
| 494 |
Proof using Type. |
| 495 |
induction l; [reflexivity|]. |
| 496 |
simpl. now destruct eq_dec; [|f_equal]. |
| 497 |
Qed. |
| 498 |
|
| 499 |
(** Counting occurrences by filtering *) |
| 500 |
|
| 501 |
Definition count_occ' (l : list A) (x : A) : nat := |
| 502 |
length (filter (fun y => if eq_dec y x then true else false) l). |
| 503 |
|
| 504 |
Lemma count_occ_alt (l : list A) (x : A) : |
| 505 |
count_occ' l x = count_occ eq_dec l x. |
| 506 |
Proof using Type. |
| 507 |
unfold count_occ'. induction l; [reflexivity|]. |
| 508 |
simpl. now destruct eq_dec; simpl; [f_equal|]. |
| 509 |
Qed. |
| 510 |
|
| 511 |
End Filtering. |
| 512 |
|
| 513 |
|
| 514 |
(******************************************************) |
| 515 |
(** ** Operations on lists of pairs or lists of lists *) |
| 516 |
(******************************************************) |
| 517 |
|
| 518 |
(******************************) |
| 519 |
(** ** Set inclusion on list *) |
| 520 |
(******************************) |
| 521 |
|
| 522 |
Section SetIncl. |
| 523 |
|
| 524 |
Variable A : Type. |
| 525 |
|
| 526 |
Local Notation incl := (@incl A). |
| 527 |
|
| 528 |
Lemma incl_nil_l : forall l, incl nil l. |
| 529 |
Proof using Type. |
| 530 |
intros l a Hin; inversion Hin. |
| 531 |
Qed. |
| 532 |
|
| 533 |
Lemma incl_l_nil : forall l, incl l nil -> l = nil. |
| 534 |
Proof using Type. |
| 535 |
intro l; destruct l as [|a l]; intros Hincl. |
| 536 |
- reflexivity. |
| 537 |
- exfalso; apply Hincl with a; simpl; auto. |
| 538 |
Qed. |
| 539 |
|
| 540 |
Lemma incl_cons_inv : forall (a:A) (l m:list A), |
| 541 |
incl (a :: l) m -> In a m /\ incl l m. |
| 542 |
Proof using Type. |
| 543 |
intros a l m Hi. |
| 544 |
split; [ | intros ? ? ]; apply Hi; simpl; auto. |
| 545 |
Qed. |
| 546 |
|
| 547 |
Lemma incl_app_app : forall l1 l2 m1 m2:list A, |
| 548 |
incl l1 m1 -> incl l2 m2 -> incl (l1 ++ l2) (m1 ++ m2). |
| 549 |
Proof using Type. |
| 550 |
intros. |
| 551 |
apply incl_app; [ apply incl_appl | apply incl_appr]; assumption. |
| 552 |
Qed. |
| 553 |
|
| 554 |
Lemma incl_app_inv : forall l1 l2 m : list A, |
| 555 |
incl (l1 ++ l2) m -> incl l1 m /\ incl l2 m. |
| 556 |
Proof using Type. |
| 557 |
intro l1; induction l1 as [|a l1 IHl1]; intros l2 m Hin; split; auto. |
| 558 |
- apply incl_nil_l. |
| 559 |
- intros b Hb; inversion_clear Hb; subst; apply Hin. |
| 560 |
+ now constructor. |
| 561 |
+ simpl; apply in_cons. |
| 562 |
apply incl_appl with l1; [ apply incl_refl | assumption ]. |
| 563 |
- apply IHl1. |
| 564 |
now apply incl_cons_inv in Hin. |
| 565 |
Qed. |
| 566 |
|
| 567 |
Lemma incl_filter f l : incl (filter f l) l. |
| 568 |
Proof using Type. intros x Hin; now apply filter_In in Hin. Qed. |
| 569 |
|
| 570 |
Lemma remove_incl (eq_dec : forall x y : A, {x = y} + {x <> y}) : forall l1 l2 x, |
| 571 |
incl l1 l2 -> incl (remove eq_dec x l1) (remove eq_dec x l2). |
| 572 |
Proof using Type. |
| 573 |
intros l1 l2 x Hincl y Hin. |
| 574 |
apply in_remove in Hin; destruct Hin as [Hin Hneq]. |
| 575 |
apply in_in_remove; intuition. |
| 576 |
Qed. |
| 577 |
|
| 578 |
End SetIncl. |
| 579 |
|
| 580 |
Lemma incl_map A B (f : A -> B) l1 l2 : incl l1 l2 -> incl (map f l1) (map f l2). |
| 581 |
Proof using Type. |
| 582 |
intros Hincl x Hinx. |
| 583 |
destruct (proj1 (in_map_iff _ _ _) Hinx) as [y [<- Hiny]]. |
| 584 |
now apply in_map, Hincl. |
| 585 |
Qed. |
| 586 |
|
| 587 |
#[global] |
| 588 |
Hint Resolve incl_refl incl_tl incl_tran incl_appl incl_appr incl_cons |
| 589 |
incl_app incl_map: datatypes. |
| 590 |
|
| 591 |
|
| 592 |
(**************************************) |
| 593 |
(** * Cutting a list at some position *) |
| 594 |
(**************************************) |
| 595 |
|
| 596 |
Section Cutting. |
| 597 |
|
| 598 |
Variable A : Type. |
| 599 |
Local Notation firstn := (@firstn A). |
| 600 |
|
| 601 |
Lemma removelast_firstn_len : forall l, |
| 602 |
removelast l = firstn (pred (length l)) l. |
| 603 |
Proof using Type. |
| 604 |
intro l; induction l as [|a l IHl]; [ reflexivity | simpl ]. |
| 605 |
destruct l; [ | rewrite IHl ]; reflexivity. |
| 606 |
Qed. |
| 607 |
|
| 608 |
End Cutting. |
| 609 |
|
| 610 |
Section CuttingMap. |
| 611 |
Variables A B : Type. |
| 612 |
Variable f : A -> B. |
| 613 |
|
| 614 |
Lemma firstn_map : forall n l, |
| 615 |
firstn n (map f l) = map f (firstn n l). |
| 616 |
Proof using Type. |
| 617 |
intro n; induction n; intros []; simpl; f_equal; trivial. |
| 618 |
Qed. |
| 619 |
|
| 620 |
Lemma skipn_map : forall n l, |
| 621 |
skipn n (map f l) = map f (skipn n l). |
| 622 |
Proof using Type. |
| 623 |
intro n; induction n; intros []; simpl; trivial. |
| 624 |
Qed. |
| 625 |
End CuttingMap. |
| 626 |
|
| 627 |
(**************************************************************) |
| 628 |
(** ** Combining pairs of lists of possibly-different lengths *) |
| 629 |
(**************************************************************) |
| 630 |
|
| 631 |
(**********************************************************************) |
| 632 |
(** ** Predicate for List addition/removal (no need for decidability) *) |
| 633 |
(**********************************************************************) |
| 634 |
|
| 635 |
(********************************) |
| 636 |
(** ** Lists without redundancy *) |
| 637 |
(********************************) |
| 638 |
|
| 639 |
Section ReDun. |
| 640 |
|
| 641 |
Variable A : Type. |
| 642 |
|
| 643 |
Local Notation NoDup := (@NoDup A). |
| 644 |
Lemma NoDup_rev l : NoDup l -> NoDup (rev l). |
| 645 |
Proof using Type. |
| 646 |
induction l as [|a l IHl]; simpl; intros Hnd; [ constructor | ]. |
| 647 |
inversion_clear Hnd as [ | ? ? Hnin Hndl ]. |
| 648 |
assert (Add a (rev l) (rev l ++ a :: nil)) as Hadd |
| 649 |
by (rewrite <- (app_nil_r (rev l)) at 1; apply Add_app). |
| 650 |
apply NoDup_Add in Hadd; apply Hadd; intuition. |
| 651 |
now apply Hnin, in_rev. |
| 652 |
Qed. |
| 653 |
|
| 654 |
Lemma NoDup_filter f l : NoDup l -> NoDup (filter f l). |
| 655 |
Proof using Type. |
| 656 |
induction l as [|a l IHl]; simpl; intros Hnd; auto. |
| 657 |
apply NoDup_cons_iff in Hnd. |
| 658 |
destruct (f a); [ | intuition ]. |
| 659 |
apply NoDup_cons_iff; split; [intro H|]; intuition. |
| 660 |
apply filter_In in H; intuition. |
| 661 |
Qed. |
| 662 |
|
| 663 |
(** Effective computation of a list without duplicates *) |
| 664 |
|
| 665 |
Hypothesis decA: forall x y : A, {x = y} + {x <> y}. |
| 666 |
|
| 667 |
Local Notation nodup := (@nodup A decA). |
| 668 |
|
| 669 |
Lemma nodup_incl l1 l2 : incl l1 (nodup l2) <-> incl l1 l2. |
| 670 |
Proof using Type. |
| 671 |
split; intros Hincl a Ha; eapply nodup_In; intuition. |
| 672 |
Qed. |
| 673 |
|
| 674 |
Lemma NoDup_incl_NoDup (l l' : list A) : NoDup l -> |
| 675 |
length l' <= length l -> incl l l' -> NoDup l'. |
| 676 |
Proof using Type. |
| 677 |
revert l'; induction l as [|a l IHl]; simpl; intros l' Hnd Hlen Hincl. |
| 678 |
- now destruct l'; inversion Hlen. |
| 679 |
- assert (In a l') as Ha by now apply Hincl; left. |
| 680 |
apply in_split in Ha as [l1' [l2' ->]]. |
| 681 |
inversion_clear Hnd as [|? ? Hnin Hnd']. |
| 682 |
apply (NoDup_Add (Add_app a l1' l2')); split. |
| 683 |
+ apply IHl; auto. |
| 684 |
* rewrite app_length. |
| 685 |
rewrite app_length in Hlen; simpl in Hlen; rewrite Nat.add_succ_r in Hlen. |
| 686 |
now apply Nat.succ_le_mono. |
| 687 |
* apply (incl_Add_inv (u:= l1' ++ l2')) in Hincl; auto. |
| 688 |
apply Add_app. |
| 689 |
+ intros Hnin'. |
| 690 |
assert (incl (a :: l) (l1' ++ l2')) as Hincl''. |
| 691 |
{ apply incl_tran with (l1' ++ a :: l2'); auto. |
| 692 |
intros x Hin. |
| 693 |
apply in_app_or in Hin as [Hin|[->|Hin]]; intuition; apply in_or_app; intuition. } |
| 694 |
apply NoDup_incl_length in Hincl''; [ | now constructor ]. |
| 695 |
apply (Nat.nle_succ_diag_l (length l1' + length l2')). |
| 696 |
rewrite_all app_length. |
| 697 |
simpl in Hlen; rewrite Nat.add_succ_r in Hlen. |
| 698 |
now transitivity (S (length l)). |
| 699 |
Qed. |
| 700 |
|
| 701 |
End ReDun. |
| 702 |
|
| 703 |
(** NoDup and map *) |
| 704 |
|
| 705 |
(** NB: the reciprocal result holds only for injective functions, |
| 706 |
see FinFun.v *) |
| 707 |
|
| 708 |
(***********************************) |
| 709 |
(** ** Sequence of natural numbers *) |
| 710 |
(***********************************) |
| 711 |
|
| 712 |
Section NatSeq. |
| 713 |
|
| 714 |
(** [seq] computes the sequence of [len] contiguous integers |
| 715 |
that starts at [start]. For instance, [seq 2 3] is [2::3::4::nil]. *) |
| 716 |
|
| 717 |
Lemma cons_seq : forall len start, start :: seq (S start) len = seq start (S len). |
| 718 |
Proof using Type. |
| 719 |
reflexivity. |
| 720 |
Qed. |
| 721 |
|
| 722 |
Lemma seq_S : forall len start, seq start (S len) = seq start len ++ [start + len]. |
| 723 |
Proof using Type. |
| 724 |
intros len start. |
| 725 |
change [start + len] with (seq (start + len) 1). |
| 726 |
rewrite <- seq_app. |
| 727 |
rewrite Nat.add_succ_r, Nat.add_0_r; reflexivity. |
| 728 |
Qed. |
| 729 |
|
| 730 |
End NatSeq. |
| 731 |
|
| 732 |
Section Exists_Forall. |
| 733 |
|
| 734 |
(** * Existential and universal predicates over lists *) |
| 735 |
|
| 736 |
Variable A:Type. |
| 737 |
|
| 738 |
Section One_predicate. |
| 739 |
|
| 740 |
Variable P:A->Prop. |
| 741 |
|
| 742 |
#[local] |
| 743 |
Hint Constructors Exists : core. |
| 744 |
|
| 745 |
Local Notation Exists := (@Exists A P). |
| 746 |
|
| 747 |
Lemma Exists_nth l : |
| 748 |
Exists l <-> exists i d, i < length l /\ P (nth i l d). |
| 749 |
Proof using Type. |
| 750 |
split. |
| 751 |
- intros HE; apply Exists_exists in HE. |
| 752 |
destruct HE as [a [Hin HP]]. |
| 753 |
apply (In_nth _ _ a) in Hin; destruct Hin as [i [Hl Heq]]. |
| 754 |
rewrite <- Heq in HP. |
| 755 |
now exists i; exists a. |
| 756 |
- intros [i [d [Hl HP]]]. |
| 757 |
apply Exists_exists; exists (nth i l d); split. |
| 758 |
apply nth_In; assumption. |
| 759 |
assumption. |
| 760 |
Qed. |
| 761 |
|
| 762 |
Lemma Exists_app l1 l2 : |
| 763 |
Exists (l1 ++ l2) <-> Exists l1 \/ Exists l2. |
| 764 |
Proof using Type. |
| 765 |
induction l1; simpl; split; intros HE; try now intuition. |
| 766 |
- inversion_clear HE; intuition. |
| 767 |
- destruct HE as [HE|HE]; intuition. |
| 768 |
inversion_clear HE; intuition. |
| 769 |
Qed. |
| 770 |
|
| 771 |
Lemma Exists_rev l : Exists l -> Exists (rev l). |
| 772 |
Proof using Type. |
| 773 |
induction l; intros HE; intuition. |
| 774 |
inversion_clear HE; simpl; apply Exists_app; intuition. |
| 775 |
Qed. |
| 776 |
|
| 777 |
Lemma Exists_fold_right l : |
| 778 |
Exists l <-> fold_right (fun x => or (P x)) False l. |
| 779 |
Proof using Type. |
| 780 |
induction l; simpl; split; intros HE; try now inversion HE; intuition. |
| 781 |
Qed. |
| 782 |
|
| 783 |
Lemma incl_Exists l1 l2 : incl l1 l2 -> Exists l1 -> Exists l2. |
| 784 |
Proof using Type. |
| 785 |
intros Hincl HE. |
| 786 |
apply Exists_exists in HE; destruct HE as [a [Hin HP]]. |
| 787 |
apply Exists_exists; exists a; intuition. |
| 788 |
Qed. |
| 789 |
|
| 790 |
#[local] |
| 791 |
Hint Constructors Forall : core. |
| 792 |
|
| 793 |
Local Notation Forall := (@Forall A P). |
| 794 |
|
| 795 |
Lemma Forall_nil_iff : Forall [] <-> True. |
| 796 |
Proof using Type. |
| 797 |
easy. |
| 798 |
Qed. |
| 799 |
|
| 800 |
Lemma Forall_cons_iff : forall (a:A) l, Forall (a :: l) <-> P a /\ Forall l. |
| 801 |
Proof using Type. |
| 802 |
intros. now split; [intro H; inversion H|constructor]. |
| 803 |
Qed. |
| 804 |
|
| 805 |
Lemma Forall_nth l : |
| 806 |
Forall l <-> forall i d, i < length l -> P (nth i l d). |
| 807 |
Proof using Type. |
| 808 |
split. |
| 809 |
- intros HF i d Hl. |
| 810 |
apply (Forall_forall P l). |
| 811 |
assumption. |
| 812 |
apply nth_In; assumption. |
| 813 |
- intros HF. |
| 814 |
apply Forall_forall; intros a Hin. |
| 815 |
apply (In_nth _ _ a) in Hin; destruct Hin as [i [Hl Heq]]. |
| 816 |
rewrite <- Heq; intuition. |
| 817 |
Qed. |
| 818 |
|
| 819 |
Lemma Forall_app l1 l2 : |
| 820 |
Forall (l1 ++ l2) <-> Forall l1 /\ Forall l2. |
| 821 |
Proof using Type. |
| 822 |
induction l1 as [|a l1 IH]; cbn. |
| 823 |
- now rewrite Forall_nil_iff. |
| 824 |
- now rewrite !Forall_cons_iff, IH, and_assoc. |
| 825 |
Qed. |
| 826 |
|
| 827 |
Lemma Forall_elt a l1 l2 : Forall (l1 ++ a :: l2) -> P a. |
| 828 |
Proof using Type. |
| 829 |
intros HF; apply Forall_app in HF; destruct HF as [HF1 HF2]; now inversion HF2. |
| 830 |
Qed. |
| 831 |
|
| 832 |
Lemma Forall_rev l : Forall l -> Forall (rev l). |
| 833 |
Proof using Type. |
| 834 |
induction l; intros HF; [assumption|]. |
| 835 |
inversion_clear HF; simpl; apply Forall_app; intuition. |
| 836 |
Qed. |
| 837 |
|
| 838 |
Lemma Forall_fold_right l : |
| 839 |
Forall l <-> fold_right (fun x => and (P x)) True l. |
| 840 |
Proof using Type. |
| 841 |
induction l; simpl; split; intros HF; try now inversion HF; intuition. |
| 842 |
Qed. |
| 843 |
|
| 844 |
Lemma incl_Forall l1 l2 : incl l2 l1 -> Forall l1 -> Forall l2. |
| 845 |
Proof using Type. |
| 846 |
intros Hincl HF. |
| 847 |
apply Forall_forall; intros a Ha. |
| 848 |
apply (Forall_forall P l1); intuition. |
| 849 |
Qed. |
| 850 |
|
| 851 |
End One_predicate. |
| 852 |
|
| 853 |
Lemma map_ext_Forall B : forall (f g : A -> B) l, |
| 854 |
Forall (fun x => f x = g x) l -> map f l = map g l. |
| 855 |
Proof using Type. |
| 856 |
intros; apply map_ext_in, Forall_forall; assumption. |
| 857 |
Qed. |
| 858 |
|
| 859 |
Lemma Exists_or : forall (P Q : A -> Prop) l, |
| 860 |
Exists P l \/ Exists Q l -> Exists (fun x => P x \/ Q x) l. |
| 861 |
Proof using Type. |
| 862 |
intros P Q l; induction l as [|a l IHl]; intros [H | H]; inversion H; subst. |
| 863 |
1,3: apply Exists_cons_hd; auto. |
| 864 |
all: apply Exists_cons_tl, IHl; auto. |
| 865 |
Qed. |
| 866 |
|
| 867 |
Lemma Exists_or_inv : forall (P Q : A -> Prop) l, |
| 868 |
Exists (fun x => P x \/ Q x) l -> Exists P l \/ Exists Q l. |
| 869 |
Proof using Type. |
| 870 |
intros P Q l; induction l as [|a l IHl]; |
| 871 |
intro Hl; inversion Hl as [ ? ? H | ? ? H ]; subst. |
| 872 |
- inversion H; now repeat constructor. |
| 873 |
- destruct (IHl H); now repeat constructor. |
| 874 |
Qed. |
| 875 |
|
| 876 |
Lemma Forall_and : forall (P Q : A -> Prop) l, |
| 877 |
Forall P l -> Forall Q l -> Forall (fun x => P x /\ Q x) l. |
| 878 |
Proof using Type. |
| 879 |
intros P Q l; induction l; intros HP HQ; constructor; inversion HP; inversion HQ; auto. |
| 880 |
Qed. |
| 881 |
|
| 882 |
Lemma Forall_and_inv : forall (P Q : A -> Prop) l, |
| 883 |
Forall (fun x => P x /\ Q x) l -> Forall P l /\ Forall Q l. |
| 884 |
Proof using Type. |
| 885 |
intros P Q l; induction l; intro Hl; split; constructor; inversion Hl; firstorder. |
| 886 |
Qed. |
| 887 |
|
| 888 |
Lemma incl_Forall_in_iff l l' : |
| 889 |
incl l l' <-> @Forall A (fun x => In x l') l. |
| 890 |
Proof using Type. now rewrite Forall_forall; split. Qed. |
| 891 |
|
| 892 |
End Exists_Forall. |
| 893 |
|
| 894 |
Lemma Exists_map A B (f : A -> B) P l : |
| 895 |
Exists P (map f l) <-> Exists (fun x => P (f x)) l. |
| 896 |
Proof using Type. |
| 897 |
induction l as [|a l IHl]. |
| 898 |
- cbn. now rewrite Exists_nil. |
| 899 |
- cbn. now rewrite ?Exists_cons, IHl. |
| 900 |
Qed. |
| 901 |
|
| 902 |
Lemma Exists_concat A P (ls : list (list A)) : |
| 903 |
Exists P (concat ls) <-> Exists (Exists P) ls. |
| 904 |
Proof using Type. |
| 905 |
induction ls as [|l ls IHls]. |
| 906 |
- cbn. now rewrite Exists_nil. |
| 907 |
- cbn. now rewrite Exists_app, Exists_cons, IHls. |
| 908 |
Qed. |
| 909 |
|
| 910 |
Lemma Exists_flat_map A B P ls (f : A -> list B) : |
| 911 |
Exists P (flat_map f ls) <-> Exists (fun d => Exists P (f d)) ls. |
| 912 |
Proof using Type. |
| 913 |
now rewrite flat_map_concat_map, Exists_concat, Exists_map. |
| 914 |
Qed. |
| 915 |
|
| 916 |
Lemma Forall_map A B (f : A -> B) P l : |
| 917 |
Forall P (map f l) <-> Forall (fun x => P (f x)) l. |
| 918 |
Proof using Type. |
| 919 |
induction l as [|a l IHl]; cbn. |
| 920 |
- now rewrite !Forall_nil_iff. |
| 921 |
- now rewrite !Forall_cons_iff, IHl. |
| 922 |
Qed. |
| 923 |
|
| 924 |
Lemma Forall_concat A P (ls : list (list A)) : |
| 925 |
Forall P (concat ls) <-> Forall (Forall P) ls. |
| 926 |
Proof using Type. |
| 927 |
induction ls as [|l ls IHls]; cbn. |
| 928 |
- now rewrite !Forall_nil_iff. |
| 929 |
- now rewrite Forall_app, Forall_cons_iff, IHls. |
| 930 |
Qed. |
| 931 |
|
| 932 |
Lemma Forall_flat_map A B P ls (f : A -> list B) : |
| 933 |
Forall P (flat_map f ls) <-> Forall (fun d => Forall P (f d)) ls. |
| 934 |
Proof using Type. |
| 935 |
now rewrite flat_map_concat_map, Forall_concat, Forall_map. |
| 936 |
Qed. |
| 937 |
|
| 938 |
Lemma exists_Forall A B : forall (P : A -> B -> Prop) l, |
| 939 |
(exists k, Forall (P k) l) -> Forall (fun x => exists k, P k x) l. |
| 940 |
Proof using Type. |
| 941 |
intros P l; induction l as [|a l IHl]; intros [k HF]; constructor; inversion_clear HF. |
| 942 |
- now exists k. |
| 943 |
- now apply IHl; exists k. |
| 944 |
Qed. |
| 945 |
|
| 946 |
Lemma Forall_image A B : forall (f : A -> B) l, |
| 947 |
Forall (fun y => exists x, y = f x) l <-> exists l', l = map f l'. |
| 948 |
Proof using Type. |
| 949 |
intros f l; induction l as [|a l IHl]; split; intros HF. |
| 950 |
- exists nil; reflexivity. |
| 951 |
- constructor. |
| 952 |
- apply Forall_cons_iff in HF as [[x ->] [l' ->] %IHl]. |
| 953 |
now exists (x :: l'). |
| 954 |
- destruct HF as [l' Heq]. |
| 955 |
symmetry in Heq; apply map_eq_cons in Heq. |
| 956 |
destruct Heq as (x & tl & ? & ? & ?); subst. |
| 957 |
constructor. |
| 958 |
+ now exists x. |
| 959 |
+ now apply IHl; exists tl. |
| 960 |
Qed. |
| 961 |
|
| 962 |
Lemma concat_nil_Forall A : forall (l : list (list A)), |
| 963 |
concat l = nil <-> Forall (fun x => x = nil) l. |
| 964 |
Proof using Type. |
| 965 |
intro l; induction l as [|a l IHl]; simpl; split; intros Hc; auto. |
| 966 |
- apply app_eq_nil in Hc. |
| 967 |
constructor; firstorder. |
| 968 |
- inversion Hc; subst; simpl. |
| 969 |
now apply IHl. |
| 970 |
Qed. |
| 971 |
|
| 972 |
Lemma in_flat_map_Exists A B : forall (f : A -> list B) x l, |
| 973 |
In x (flat_map f l) <-> Exists (fun y => In x (f y)) l. |
| 974 |
Proof using Type. |
| 975 |
intros f x l; rewrite in_flat_map. |
| 976 |
split; apply Exists_exists. |
| 977 |
Qed. |
| 978 |
|
| 979 |
Lemma notin_flat_map_Forall A B : forall (f : A -> list B) x l, |
| 980 |
~ In x (flat_map f l) <-> Forall (fun y => ~ In x (f y)) l. |
| 981 |
Proof using Type. |
| 982 |
intros f x l; rewrite Forall_Exists_neg. |
| 983 |
apply not_iff_compat, in_flat_map_Exists. |
| 984 |
Qed. |
| 985 |
|
| 986 |
Section Repeat. |
| 987 |
|
| 988 |
Variable A : Type. |
| 989 |
Local Notation repeat := (@repeat A). |
| 990 |
Lemma repeat_cons n a : |
| 991 |
a :: repeat a n = repeat a n ++ (a :: nil). |
| 992 |
Proof using Type. |
| 993 |
induction n as [|n IHn]; simpl. |
| 994 |
- reflexivity. |
| 995 |
- f_equal; apply IHn. |
| 996 |
Qed. |
| 997 |
|
| 998 |
Lemma repeat_app x n m : |
| 999 |
repeat x (n + m) = repeat x n ++ repeat x m. |
| 1000 |
Proof using Type. |
| 1001 |
induction n as [|n IHn]; simpl; auto. |
| 1002 |
now rewrite IHn. |
| 1003 |
Qed. |
| 1004 |
|
| 1005 |
Lemma repeat_eq_app x n l1 l2 : |
| 1006 |
repeat x n = l1 ++ l2 -> repeat x (length l1) = l1 /\ repeat x (length l2) = l2. |
| 1007 |
Proof using Type. |
| 1008 |
revert n; induction l1 as [|a l1 IHl1]; simpl; intros n Hr; subst. |
| 1009 |
- repeat split; now rewrite repeat_length. |
| 1010 |
- destruct n; inversion Hr as [ [Heq Hr0] ]; subst. |
| 1011 |
now apply IHl1 in Hr0 as [-> ->]. |
| 1012 |
Qed. |
| 1013 |
|
| 1014 |
Lemma repeat_eq_cons x y n l : |
| 1015 |
repeat x n = y :: l -> x = y /\ repeat x (pred n) = l. |
| 1016 |
Proof using Type. |
| 1017 |
intros Hr. |
| 1018 |
destruct n; inversion_clear Hr; auto. |
| 1019 |
Qed. |
| 1020 |
|
| 1021 |
Lemma repeat_eq_elt x y n l1 l2 : |
| 1022 |
repeat x n = l1 ++ y :: l2 -> x = y /\ repeat x (length l1) = l1 /\ repeat x (length l2) = l2. |
| 1023 |
Proof using Type. |
| 1024 |
intros Hr; apply repeat_eq_app in Hr as [Hr1 Hr2]; subst. |
| 1025 |
apply repeat_eq_cons in Hr2; intuition. |
| 1026 |
Qed. |
| 1027 |
|
| 1028 |
Lemma Forall_eq_repeat x l : |
| 1029 |
Forall (eq x) l -> l = repeat x (length l). |
| 1030 |
Proof using Type. |
| 1031 |
induction l as [|a l IHl]; simpl; intros HF; auto. |
| 1032 |
inversion_clear HF as [ | ? ? ? HF']; subst. |
| 1033 |
now rewrite (IHl HF') at 1. |
| 1034 |
Qed. |
| 1035 |
|
| 1036 |
Hypothesis decA : forall x y : A, {x = y}+{x <> y}. |
| 1037 |
|
| 1038 |
Lemma count_occ_repeat_eq x y n : x = y -> count_occ decA (repeat y n) x = n. |
| 1039 |
Proof using Type. |
| 1040 |
intros ->. |
| 1041 |
induction n; cbn; auto. |
| 1042 |
destruct (decA y y); auto. |
| 1043 |
exfalso; intuition. |
| 1044 |
Qed. |
| 1045 |
|
| 1046 |
Lemma count_occ_repeat_neq x y n : x <> y -> count_occ decA (repeat y n) x = 0. |
| 1047 |
Proof using Type. |
| 1048 |
intros Hneq. |
| 1049 |
induction n; cbn; auto. |
| 1050 |
destruct (decA y x); auto. |
| 1051 |
exfalso; intuition. |
| 1052 |
Qed. |
| 1053 |
|
| 1054 |
Lemma count_occ_unique x l : count_occ decA l x = length l -> l = repeat x (length l). |
| 1055 |
Proof using Type. |
| 1056 |
induction l as [|h l]; cbn; intros Hocc; auto. |
| 1057 |
destruct (decA h x). |
| 1058 |
- f_equal; intuition. |
| 1059 |
- assert (Hb := count_occ_bound decA x l). |
| 1060 |
rewrite Hocc in Hb. |
| 1061 |
exfalso; apply (Nat.nle_succ_diag_l _ Hb). |
| 1062 |
Qed. |
| 1063 |
|
| 1064 |
Lemma count_occ_repeat_excl x l : |
| 1065 |
(forall y, y <> x -> count_occ decA l y = 0) -> l = repeat x (length l). |
| 1066 |
Proof using Type. |
| 1067 |
intros Hocc. |
| 1068 |
apply Forall_eq_repeat, Forall_forall; intros z Hin. |
| 1069 |
destruct (decA z x) as [Heq|Hneq]; auto. |
| 1070 |
apply Hocc, count_occ_not_In in Hneq; intuition. |
| 1071 |
Qed. |
| 1072 |
|
| 1073 |
Lemma count_occ_sgt l x : l = x :: nil <-> |
| 1074 |
count_occ decA l x = 1 /\ forall y, y <> x -> count_occ decA l y = 0. |
| 1075 |
Proof using Type. |
| 1076 |
split. |
| 1077 |
- intros ->; cbn; split; intros; destruct decA; subst; intuition. |
| 1078 |
- intros [Heq Hneq]. |
| 1079 |
apply count_occ_repeat_excl in Hneq. |
| 1080 |
rewrite Hneq, count_occ_repeat_eq in Heq; trivial. |
| 1081 |
now rewrite Heq in Hneq. |
| 1082 |
Qed. |
| 1083 |
|
| 1084 |
Lemma nth_repeat a m n : |
| 1085 |
nth n (repeat a m) a = a. |
| 1086 |
Proof using Type. |
| 1087 |
revert n. induction m as [|m IHm]. |
| 1088 |
- now intros [|n]. |
| 1089 |
- intros [|n]; [reflexivity|exact (IHm n)]. |
| 1090 |
Qed. |
| 1091 |
|
| 1092 |
Lemma nth_error_repeat a m n : |
| 1093 |
n < m -> nth_error (repeat a m) n = Some a. |
| 1094 |
Proof using Type. |
| 1095 |
intro Hnm. rewrite (nth_error_nth' _ a). |
| 1096 |
- now rewrite nth_repeat. |
| 1097 |
- now rewrite repeat_length. |
| 1098 |
Qed. |
| 1099 |
|
| 1100 |
End Repeat. |
| 1101 |
|
| 1102 |
Lemma repeat_to_concat A n (a:A) : |
| 1103 |
repeat a n = concat (repeat [a] n). |
| 1104 |
Proof using Type. |
| 1105 |
induction n as [|n IHn]; simpl. |
| 1106 |
- reflexivity. |
| 1107 |
- f_equal; apply IHn. |
| 1108 |
Qed. |
| 1109 |
|
| 1110 |
|
| 1111 |
(** Sum of elements of a list of [nat]: [list_sum] *) |
| 1112 |
|
| 1113 |
Definition list_sum l := fold_right plus 0 l. |
| 1114 |
|
| 1115 |
Lemma list_sum_app : forall l1 l2, |
| 1116 |
list_sum (l1 ++ l2) = list_sum l1 + list_sum l2. |
| 1117 |
Proof using Type. |
| 1118 |
intro l1; induction l1 as [|a l1 IHl1]; intros l2; [ reflexivity | ]. |
| 1119 |
simpl; rewrite IHl1. |
| 1120 |
apply Nat.add_assoc. |
| 1121 |
Qed. |
| 1122 |
|
| 1123 |
(** Max of elements of a list of [nat]: [list_max] *) |
| 1124 |
|
| 1125 |
Definition list_max l := fold_right max 0 l. |
| 1126 |
|
| 1127 |
Lemma list_max_app : forall l1 l2, |
| 1128 |
list_max (l1 ++ l2) = max (list_max l1) (list_max l2). |
| 1129 |
Proof using Type. |
| 1130 |
intro l1; induction l1 as [|a l1 IHl1]; intros l2; [ reflexivity | ]. |
| 1131 |
now simpl; rewrite IHl1, Nat.max_assoc. |
| 1132 |
Qed. |
| 1133 |
|
| 1134 |
Lemma list_max_le : forall l n, |
| 1135 |
list_max l <= n <-> Forall (fun k => k <= n) l. |
| 1136 |
Proof using Type. |
| 1137 |
intro l; induction l as [|a l IHl]; simpl; intros n; split. |
| 1138 |
- now intros. |
| 1139 |
- intros. now apply Nat.le_0_l. |
| 1140 |
- intros [? ?] %Nat.max_lub_iff. now constructor; [|apply IHl]. |
| 1141 |
- now rewrite Forall_cons_iff, <- IHl, Nat.max_lub_iff. |
| 1142 |
Qed. |
| 1143 |
|
| 1144 |
Lemma list_max_lt : forall l n, l <> nil -> |
| 1145 |
list_max l < n <-> Forall (fun k => k < n) l. |
| 1146 |
Proof using Type. |
| 1147 |
intro l; induction l as [|a l IHl]; simpl; intros n Hnil; split; intros H; intuition. |
| 1148 |
- destruct l. |
| 1149 |
+ repeat constructor. |
| 1150 |
now simpl in H; rewrite Nat.max_0_r in H. |
| 1151 |
+ apply Nat.max_lub_lt_iff in H. |
| 1152 |
now constructor; [ | apply IHl ]. |
| 1153 |
- destruct l; inversion_clear H as [ | ? ? Hlt HF ]. |
| 1154 |
+ now simpl; rewrite Nat.max_0_r. |
| 1155 |
+ apply IHl in HF. |
| 1156 |
* now apply Nat.max_lub_lt_iff. |
| 1157 |
* intros Heq; inversion Heq. |
| 1158 |
Qed. |
| 1159 |
End List. |
| 1160 |
|
| 1161 |
From Coq Require Import List Setoid Compare_dec Morphisms FinFun PeanoNat. |
| 1162 |
Import ListNotations. (* For notations [] and [a;b;c] *) |
| 1163 |
Module Export Sorting. |
| 1164 |
Module Export Permutation. |
| 1165 |
(************************************************************************) |
| 1166 |
(* * The Coq Proof Assistant / The Coq Development Team *) |
| 1167 |
(* v * Copyright INRIA, CNRS and contributors *) |
| 1168 |
(* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 1169 |
(* \VV/ **************************************************************) |
| 1170 |
(* // * This file is distributed under the terms of the *) |
| 1171 |
(* * GNU Lesser General Public License Version 2.1 *) |
| 1172 |
(* * (see LICENSE file for the text of the license) *) |
| 1173 |
(************************************************************************) |
| 1174 |
|
| 1175 |
(*********************************************************************) |
| 1176 |
(** * List permutations as a composition of adjacent transpositions *) |
| 1177 |
(*********************************************************************) |
| 1178 |
|
| 1179 |
(* Adapted in May 2006 by Jean-Marc Notin from initial contents by |
| 1180 |
Laurent Théry (Huffmann contribution, October 2003) *) |
| 1181 |
|
| 1182 |
(* Set Universe Polymorphism. *) |
| 1183 |
|
| 1184 |
|
| 1185 |
(* These hints do not reduce the size of the problem to solve and they |
| 1186 |
must be used with care to avoid combinatoric explosions *) |
| 1187 |
|
| 1188 |
Local Hint Resolve perm_swap perm_trans : core. |
| 1189 |
Local Hint Resolve Permutation_sym Permutation_trans : core. |
| 1190 |
|
| 1191 |
(* This provides reflexivity, symmetry and transitivity and rewriting |
| 1192 |
on morphims to come *) |
| 1193 |
|
| 1194 |
Lemma Permutation_morph_transp A : forall P : list A -> Prop, |
| 1195 |
(forall a b l1 l2, P (l1 ++ a :: b :: l2) -> P (l1 ++ b :: a :: l2)) -> |
| 1196 |
Proper (@Permutation A ==> Basics.impl) P. |
| 1197 |
Proof using Type. |
| 1198 |
intros P HT l1 l2 HP. |
| 1199 |
enough (forall l0, P (l0 ++ l1) -> P (l0 ++ l2)) as IH |
| 1200 |
by (intro; rewrite <- (app_nil_l l2); now apply (IH nil)). |
| 1201 |
induction HP; intuition. |
| 1202 |
rewrite <- (app_nil_l l'), app_comm_cons, app_assoc. |
| 1203 |
now apply IHHP; rewrite <- app_assoc. |
| 1204 |
Qed. |
| 1205 |
|
| 1206 |
Section Permutation_properties. |
| 1207 |
|
| 1208 |
Variable A B:Type. |
| 1209 |
|
| 1210 |
Implicit Types a b : A. |
| 1211 |
Implicit Types l m : list A. |
| 1212 |
|
| 1213 |
(** Compatibility with others operations on lists *) |
| 1214 |
|
| 1215 |
Local Hint Resolve Permutation_app_comm : core. |
| 1216 |
|
| 1217 |
Lemma Permutation_app_rot : forall l1 l2 l3: list A, |
| 1218 |
Permutation (l1 ++ l2 ++ l3) (l2 ++ l3 ++ l1). |
| 1219 |
Proof using Type. |
| 1220 |
intros l1 l2 l3; now rewrite (app_assoc l2). |
| 1221 |
Qed. |
| 1222 |
Local Hint Resolve Permutation_app_rot : core. |
| 1223 |
|
| 1224 |
Lemma Permutation_app_swap_app : forall l1 l2 l3: list A, |
| 1225 |
Permutation (l1 ++ l2 ++ l3) (l2 ++ l1 ++ l3). |
| 1226 |
Proof using Type. |
| 1227 |
intros. |
| 1228 |
rewrite 2 app_assoc. |
| 1229 |
apply Permutation_app_tail, Permutation_app_comm. |
| 1230 |
Qed. |
| 1231 |
Local Hint Resolve Permutation_app_swap_app : core. |
| 1232 |
|
| 1233 |
Lemma Permutation_app_middle : forall l l1 l2 l3 l4, |
| 1234 |
Permutation (l1 ++ l2) (l3 ++ l4) -> |
| 1235 |
Permutation (l1 ++ l ++ l2) (l3 ++ l ++ l4). |
| 1236 |
Proof using Type. |
| 1237 |
intros l l1 l2 l3 l4 HP. |
| 1238 |
now rewrite Permutation_app_swap_app, HP, Permutation_app_swap_app. |
| 1239 |
Qed. |
| 1240 |
|
| 1241 |
Local Hint Resolve Permutation_cons_app : core. |
| 1242 |
Local Hint Resolve Permutation_middle : core. |
| 1243 |
|
| 1244 |
Lemma Permutation_middle2 : forall l1 l2 l3 a b, |
| 1245 |
Permutation (a :: b :: l1 ++ l2 ++ l3) (l1 ++ a :: l2 ++ b :: l3). |
| 1246 |
Proof using Type. |
| 1247 |
intros l1 l2 l3 a b. |
| 1248 |
apply Permutation_cons_app. |
| 1249 |
rewrite 2 app_assoc. |
| 1250 |
now apply Permutation_cons_app. |
| 1251 |
Qed. |
| 1252 |
Local Hint Resolve Permutation_middle2 : core. |
| 1253 |
|
| 1254 |
Lemma Permutation_elt : forall l1 l2 l1' l2' (a:A), |
| 1255 |
Permutation (l1 ++ l2) (l1' ++ l2') -> |
| 1256 |
Permutation (l1 ++ a :: l2) (l1' ++ a :: l2'). |
| 1257 |
Proof using Type. |
| 1258 |
intros l1 l2 l1' l2' a HP. |
| 1259 |
transitivity (a :: l1 ++ l2); auto. |
| 1260 |
Qed. |
| 1261 |
|
| 1262 |
Global Instance Permutation_Forall (P : A -> Prop) : |
| 1263 |
Proper ((@Permutation A) ==> Basics.impl) (Forall P). |
| 1264 |
Proof using Type. |
| 1265 |
intros l1 l2 HP. |
| 1266 |
induction HP; intro HF; auto. |
| 1267 |
- inversion_clear HF; auto. |
| 1268 |
- inversion_clear HF as [ | ? ? HF1 HF2]. |
| 1269 |
inversion_clear HF2; auto. |
| 1270 |
Qed. |
| 1271 |
|
| 1272 |
Global Instance Permutation_Exists (P : A -> Prop) : |
| 1273 |
Proper ((@Permutation A) ==> Basics.impl) (Exists P). |
| 1274 |
Proof using Type. |
| 1275 |
intros l1 l2 HP. |
| 1276 |
induction HP; intro HF; auto. |
| 1277 |
- inversion_clear HF; auto. |
| 1278 |
- inversion_clear HF as [ | ? ? HF1 ]; auto. |
| 1279 |
inversion_clear HF1; auto. |
| 1280 |
Qed. |
| 1281 |
|
| 1282 |
Lemma Permutation_Forall2 (P : A -> B -> Prop) : |
| 1283 |
forall l1 l1' (l2 : list B), Permutation l1 l1' -> Forall2 P l1 l2 -> |
| 1284 |
exists l2' : list B, Permutation l2 l2' /\ Forall2 P l1' l2'. |
| 1285 |
Proof using Type. |
| 1286 |
intros l1 l1' l2 HP. |
| 1287 |
revert l2; induction HP; intros l2 HF; inversion HF as [ | ? b ? ? HF1 HF2 ]; subst. |
| 1288 |
- now exists nil. |
| 1289 |
- apply IHHP in HF2 as [l2' [HP2 HF2]]. |
| 1290 |
exists (b :: l2'); auto. |
| 1291 |
- inversion_clear HF2 as [ | ? b' ? l2' HF3 HF4 ]. |
| 1292 |
exists (b' :: b :: l2'); auto. |
| 1293 |
- apply Permutation_nil in HP1; subst. |
| 1294 |
apply Permutation_nil in HP2; subst. |
| 1295 |
now exists nil. |
| 1296 |
- apply IHHP1 in HF as [l2' [HP2' HF2']]. |
| 1297 |
apply IHHP2 in HF2' as [l2'' [HP2'' HF2'']]. |
| 1298 |
exists l2''; split; auto. |
| 1299 |
now transitivity l2'. |
| 1300 |
Qed. |
| 1301 |
|
| 1302 |
Ltac InvAdd := repeat (match goal with |
| 1303 |
| H: Add ?x _ (_ :: _) |- _ => inversion H; clear H; subst |
| 1304 |
end). |
| 1305 |
|
| 1306 |
Ltac finish_basic_perms H := |
| 1307 |
try constructor; try rewrite perm_swap; try constructor; trivial; |
| 1308 |
(rewrite <- H; now apply Permutation_Add) || |
| 1309 |
(rewrite H; symmetry; now apply Permutation_Add). |
| 1310 |
|
| 1311 |
Lemma Permutation_app_inv_m l l1 l2 l3 l4 : |
| 1312 |
Permutation (l1 ++ l ++ l2) (l3 ++ l ++ l4) -> |
| 1313 |
Permutation (l1 ++ l2) (l3 ++ l4). |
| 1314 |
Proof using Type. |
| 1315 |
intros HP. |
| 1316 |
apply (Permutation_app_inv_l l). |
| 1317 |
transitivity (l1 ++ l ++ l2); auto. |
| 1318 |
transitivity (l3 ++ l ++ l4); auto. |
| 1319 |
Qed. |
| 1320 |
|
| 1321 |
Lemma Permutation_vs_elt_inv : forall l l1 l2 a, |
| 1322 |
Permutation l (l1 ++ a :: l2) -> exists l' l'', l = l' ++ a :: l''. |
| 1323 |
Proof using Type. |
| 1324 |
intros l l1 l2 a HP. |
| 1325 |
symmetry in HP. |
| 1326 |
apply (Permutation_in a), in_split in HP; trivial. |
| 1327 |
apply in_elt. |
| 1328 |
Qed. |
| 1329 |
|
| 1330 |
Lemma Permutation_vs_cons_inv : forall l l1 a, |
| 1331 |
Permutation l (a :: l1) -> exists l' l'', l = l' ++ a :: l''. |
| 1332 |
Proof using Type. |
| 1333 |
intros l l1 a HP. |
| 1334 |
rewrite <- (app_nil_l (a :: l1)) in HP. |
| 1335 |
apply (Permutation_vs_elt_inv _ _ _ HP). |
| 1336 |
Qed. |
| 1337 |
|
| 1338 |
Lemma Permutation_vs_cons_cons_inv : forall l l' a b, |
| 1339 |
Permutation l (a :: b :: l') -> |
| 1340 |
exists l1 l2 l3, l = l1 ++ a :: l2 ++ b :: l3 \/ l = l1 ++ b :: l2 ++ a :: l3. |
| 1341 |
Proof using Type. |
| 1342 |
intros l l' a b HP. |
| 1343 |
destruct (Permutation_vs_cons_inv HP) as [l1 [l2]]; subst. |
| 1344 |
symmetry in HP. |
| 1345 |
apply Permutation_cons_app_inv in HP. |
| 1346 |
apply (Permutation_in b), in_app_or in HP; [|now apply in_eq]. |
| 1347 |
destruct HP as [(l3 & l4 & ->)%in_split | (l3 & l4 & ->)%in_split]. |
| 1348 |
- exists l3, l4, l2; right. |
| 1349 |
now rewrite <-app_assoc; simpl. |
| 1350 |
- now exists l1, l3, l4; left. |
| 1351 |
Qed. |
| 1352 |
|
| 1353 |
Lemma Permutation_repeat x n l : |
| 1354 |
Permutation l (repeat x n) -> l = repeat x n. |
| 1355 |
Proof using Type. |
| 1356 |
revert n; induction l as [|y l IHl] ; simpl; intros n HP; auto. |
| 1357 |
- now apply Permutation_nil in HP; inversion HP. |
| 1358 |
- assert (y = x) as Heq by (now apply repeat_spec with n, (Permutation_in _ HP); left); subst. |
| 1359 |
destruct n; simpl; simpl in HP. |
| 1360 |
+ symmetry in HP; apply Permutation_nil in HP; inversion HP. |
| 1361 |
+ f_equal; apply IHl. |
| 1362 |
now apply Permutation_cons_inv with x. |
| 1363 |
Qed. |
| 1364 |
|
| 1365 |
Hypothesis eq_dec : forall x y : A, {x = y}+{x <> y}. |
| 1366 |
|
| 1367 |
Lemma Permutation_count_occ l1 l2 : |
| 1368 |
Permutation l1 l2 <-> forall x, count_occ eq_dec l1 x = count_occ eq_dec l2 x. |
| 1369 |
Proof using Type. |
| 1370 |
split. |
| 1371 |
- induction 1 as [ | y l1 l2 HP IHP | y z l | l1 l2 l3 HP1 IHP1 HP2 IHP2 ]; |
| 1372 |
cbn; intros a; auto. |
| 1373 |
+ now rewrite IHP. |
| 1374 |
+ destruct (eq_dec y a); destruct (eq_dec z a); auto. |
| 1375 |
+ now rewrite IHP1, IHP2. |
| 1376 |
- revert l2; induction l1 as [|y l1 IHl1]; cbn; intros l2 Hocc. |
| 1377 |
+ replace l2 with (@nil A); auto. |
| 1378 |
symmetry; apply (count_occ_inv_nil eq_dec); intuition. |
| 1379 |
+ assert (exists l2' l2'', l2 = l2' ++ y :: l2'') as [l2' [l2'' ->]]. |
| 1380 |
{ specialize (Hocc y). |
| 1381 |
destruct (eq_dec y y); intuition. |
| 1382 |
apply in_split, (count_occ_In eq_dec). |
| 1383 |
rewrite <- Hocc; apply Nat.lt_0_succ. } |
| 1384 |
apply Permutation_cons_app, IHl1. |
| 1385 |
intros z; specialize (Hocc z); destruct (eq_dec y z) as [Heq | Hneq]. |
| 1386 |
* rewrite (count_occ_elt_eq _ _ _ Heq) in Hocc. |
| 1387 |
now injection Hocc. |
| 1388 |
* now rewrite (count_occ_elt_neq _ _ _ Hneq) in Hocc. |
| 1389 |
Qed. |
| 1390 |
|
| 1391 |
End Permutation_properties. |
| 1392 |
|
| 1393 |
Section Permutation_map. |
| 1394 |
|
| 1395 |
Variable A B : Type. |
| 1396 |
Variable f : A -> B. |
| 1397 |
|
| 1398 |
Lemma Permutation_map_inv : forall l1 l2, |
| 1399 |
Permutation l1 (map f l2) -> exists l3, l1 = map f l3 /\ Permutation l2 l3. |
| 1400 |
Proof using Type. |
| 1401 |
induction l1; intros l2 HP. |
| 1402 |
- exists nil; split; auto. |
| 1403 |
apply Permutation_nil in HP. |
| 1404 |
destruct l2; auto. |
| 1405 |
inversion HP. |
| 1406 |
- symmetry in HP. |
| 1407 |
destruct (Permutation_vs_cons_inv HP) as [l3 [l4 Heq]]. |
| 1408 |
destruct (map_eq_app _ _ _ _ Heq) as [l1' [l2' [Heq1 [Heq2 Heq3]]]]; subst. |
| 1409 |
destruct (map_eq_cons _ _ Heq3) as [b [l1'' [Heq1' [Heq2' Heq3']]]]; subst. |
| 1410 |
rewrite map_app in HP; simpl in HP. |
| 1411 |
symmetry in HP. |
| 1412 |
apply Permutation_cons_app_inv in HP. |
| 1413 |
rewrite <- map_app in HP. |
| 1414 |
destruct (IHl1 _ HP) as [l3 [Heq1'' Heq2'']]; subst. |
| 1415 |
exists (b :: l3); split; auto. |
| 1416 |
symmetry in Heq2''; symmetry; apply (Permutation_cons_app _ _ _ Heq2''). |
| 1417 |
Qed. |
| 1418 |
|
| 1419 |
Lemma Permutation_image : forall a l l', |
| 1420 |
Permutation (a :: l) (map f l') -> exists a', a = f a'. |
| 1421 |
Proof using Type. |
| 1422 |
intros a l l' HP. |
| 1423 |
destruct (Permutation_map_inv _ HP) as [l'' [Heq _]]. |
| 1424 |
destruct l'' as [ | a' l'']; inversion_clear Heq. |
| 1425 |
now exists a'. |
| 1426 |
Qed. |
| 1427 |
|
| 1428 |
Lemma Permutation_elt_map_inv: forall l1 l2 l3 l4 a, |
| 1429 |
Permutation (l1 ++ a :: l2) (l3 ++ map f l4) -> (forall b, a <> f b) -> |
| 1430 |
exists l1' l2', l3 = l1' ++ a :: l2'. |
| 1431 |
Proof using Type. |
| 1432 |
intros l1 l2 l3 l4 a HP Hf. |
| 1433 |
apply (Permutation_in a), in_app_or in HP; [| now apply in_elt]. |
| 1434 |
destruct HP as [HP%in_split | (x & Heq & ?)%in_map_iff]; trivial; subst. |
| 1435 |
now contradiction (Hf x). |
| 1436 |
Qed. |
| 1437 |
|
| 1438 |
Global Instance Permutation_flat_map (g : A -> list B) : |
| 1439 |
Proper ((@Permutation A) ==> (@Permutation B)) (flat_map g). |
| 1440 |
Proof using Type. |
| 1441 |
intros l1; induction l1; intros l2 HP. |
| 1442 |
- now apply Permutation_nil in HP; subst. |
| 1443 |
- symmetry in HP. |
| 1444 |
destruct (Permutation_vs_cons_inv HP) as [l' [l'']]; subst. |
| 1445 |
symmetry in HP. |
| 1446 |
apply Permutation_cons_app_inv in HP. |
| 1447 |
rewrite flat_map_app; simpl. |
| 1448 |
rewrite <- (app_nil_l _). |
| 1449 |
apply Permutation_app_middle; simpl. |
| 1450 |
rewrite <- flat_map_app. |
| 1451 |
apply (IHl1 _ HP). |
| 1452 |
Qed. |
| 1453 |
|
| 1454 |
End Permutation_map. |
| 1455 |
|
| 1456 |
#[global] |
| 1457 |
Instance Permutation_list_sum : Proper (@Permutation nat ==> eq) list_sum | 10. |
| 1458 |
Proof using Type. |
| 1459 |
intros l1 l2 HP; induction HP; simpl; intuition. |
| 1460 |
- rewrite 2 (Nat.add_comm x). |
| 1461 |
apply Nat.add_assoc. |
| 1462 |
- now transitivity (list_sum l'). |
| 1463 |
Qed. |
| 1464 |
|
| 1465 |
#[global] |
| 1466 |
Instance Permutation_list_max : Proper (@Permutation nat ==> eq) list_max | 10. |
| 1467 |
Proof using Type. |
| 1468 |
intros l1 l2 HP; induction HP; simpl; intuition. |
| 1469 |
- rewrite 2 (Nat.max_comm x). |
| 1470 |
apply Nat.max_assoc. |
| 1471 |
- now transitivity (list_max l'). |
| 1472 |
Qed. |
| 1473 |
|
| 1474 |
Section Permutation_transp. |
| 1475 |
|
| 1476 |
Variable A:Type. |
| 1477 |
|
| 1478 |
(** Permutation definition based on transpositions for induction with fixed length *) |
| 1479 |
Inductive Permutation_transp : list A -> list A -> Prop := |
| 1480 |
| perm_t_refl : forall l, Permutation_transp l l |
| 1481 |
| perm_t_swap : forall x y l1 l2, Permutation_transp (l1 ++ y :: x :: l2) (l1 ++ x :: y :: l2) |
| 1482 |
| perm_t_trans l l' l'' : |
| 1483 |
Permutation_transp l l' -> Permutation_transp l' l'' -> Permutation_transp l l''. |
| 1484 |
|
| 1485 |
Instance Permutation_transp_sym : Symmetric Permutation_transp. |
| 1486 |
Proof using Type. |
| 1487 |
intros l1 l2 HP; induction HP; subst; try (now constructor). |
| 1488 |
now apply (perm_t_trans IHHP2). |
| 1489 |
Qed. |
| 1490 |
|
| 1491 |
Global Instance Permutation_transp_equiv : Equivalence Permutation_transp | 100. |
| 1492 |
Proof using Type. |
| 1493 |
split. |
| 1494 |
- intros l; apply perm_t_refl. |
| 1495 |
- apply Permutation_transp_sym. |
| 1496 |
- intros l1 l2 l3 ;apply perm_t_trans. |
| 1497 |
Qed. |
| 1498 |
|
| 1499 |
Lemma Permutation_transp_cons : forall (x : A) l1 l2, |
| 1500 |
Permutation_transp l1 l2 -> Permutation_transp (x :: l1) (x :: l2). |
| 1501 |
Proof using Type. |
| 1502 |
intros x l1 l2 HP. |
| 1503 |
induction HP. |
| 1504 |
- reflexivity. |
| 1505 |
- rewrite 2 app_comm_cons. |
| 1506 |
apply perm_t_swap. |
| 1507 |
- now transitivity (x :: l'). |
| 1508 |
Qed. |
| 1509 |
|
| 1510 |
Lemma Permutation_Permutation_transp : forall l1 l2 : list A, |
| 1511 |
Permutation l1 l2 <-> Permutation_transp l1 l2. |
| 1512 |
Proof using Type. |
| 1513 |
intros l1 l2; split; intros HP; induction HP; intuition; try now constructor. |
| 1514 |
- now apply Permutation_transp_cons. |
| 1515 |
- rewrite <- (app_nil_l (y :: _)). |
| 1516 |
rewrite <- (app_nil_l (x :: y :: _)). |
| 1517 |
apply perm_t_swap. |
| 1518 |
- now transitivity l'. |
| 1519 |
- apply Permutation_app_head. |
| 1520 |
apply perm_swap. |
| 1521 |
- now transitivity l'. |
| 1522 |
Qed. |
| 1523 |
|
| 1524 |
Lemma Permutation_ind_transp : forall P : list A -> list A -> Prop, |
| 1525 |
(forall l, P l l) -> |
| 1526 |
(forall x y l1 l2, P (l1 ++ y :: x :: l2) (l1 ++ x :: y :: l2)) -> |
| 1527 |
(forall l l' l'', |
| 1528 |
Permutation l l' -> P l l' -> Permutation l' l'' -> P l' l'' -> P l l'') -> |
| 1529 |
forall l1 l2, Permutation l1 l2 -> P l1 l2. |
| 1530 |
Proof using Type. |
| 1531 |
intros P Hr Ht Htr l1 l2 HP; apply Permutation_Permutation_transp in HP. |
| 1532 |
revert Hr Ht Htr; induction HP; intros Hr Ht Htr; auto. |
| 1533 |
apply (Htr _ l'); intuition; now apply Permutation_Permutation_transp. |
| 1534 |
Qed. |
| 1535 |
|
| 1536 |
End Permutation_transp. |
| 1537 |
|
| 1538 |
(* begin hide *) |
| 1539 |
Notation Permutation_app_swap := Permutation_app_comm (only parsing). |
| 1540 |
(* end hide *) |
| 1541 |
End Permutation. |
| 1542 |
End Sorting. |