list.v 43.8 KB
 Robbert Krebbers committed Aug 29, 2012 1 2 3 4 ``````(* Copyright (c) 2012, Robbert Krebbers. *) (* This file is distributed under the terms of the BSD license. *) (** This file collects general purpose definitions and theorems on lists that are not in the Coq standard library. *) `````` Robbert Krebbers committed Nov 12, 2012 5 6 ``````Require Import Permutation. Require Export base decidable option numbers. `````` Robbert Krebbers committed Jun 11, 2012 7 `````` `````` Robbert Krebbers committed Oct 19, 2012 8 ``````Arguments length {_} _. `````` Robbert Krebbers committed Jun 11, 2012 9 10 11 ``````Arguments cons {_} _ _. Arguments app {_} _ _. Arguments Permutation {_} _ _. `````` Robbert Krebbers committed Nov 12, 2012 12 ``````Arguments Forall_cons {_} _ _ _ _ _. `````` Robbert Krebbers committed Jun 11, 2012 13 `````` `````` Robbert Krebbers committed Oct 19, 2012 14 15 16 ``````Notation tail := tl. Notation take := firstn. Notation drop := skipn. `````` Robbert Krebbers committed Nov 12, 2012 17 ``````Notation take_drop := firstn_skipn. `````` Robbert Krebbers committed Oct 19, 2012 18 19 20 ``````Arguments take {_} !_ !_ /. Arguments drop {_} !_ !_ /. `````` Robbert Krebbers committed Jun 11, 2012 21 22 23 24 25 26 27 ``````Notation "(::)" := cons (only parsing) : C_scope. Notation "( x ::)" := (cons x) (only parsing) : C_scope. Notation "(:: l )" := (λ x, cons x l) (only parsing) : C_scope. Notation "(++)" := app (only parsing) : C_scope. Notation "( l ++)" := (app l) (only parsing) : C_scope. Notation "(++ k )" := (λ l, app l k) (only parsing) : C_scope. `````` Robbert Krebbers committed Aug 29, 2012 28 29 ``````(** * General definitions *) (** Looking up elements and updating elements in a list. *) `````` Robbert Krebbers committed Nov 12, 2012 30 31 ``````Instance list_lookup {A} : Lookup nat (list A) A := fix go (i : nat) (l : list A) {struct l} : option A := `````` Robbert Krebbers committed Aug 21, 2012 32 33 34 35 36 `````` match l with | [] => None | x :: l => match i with | 0 => Some x `````` Robbert Krebbers committed Nov 12, 2012 37 `````` | S i => @lookup _ _ _ go i l `````` Robbert Krebbers committed Aug 21, 2012 38 39 `````` end end. `````` Robbert Krebbers committed Nov 12, 2012 40 41 ``````Instance list_alter {A} (f : A → A) : AlterD nat (list A) A f := fix go (i : nat) (l : list A) {struct l} := `````` Robbert Krebbers committed Aug 21, 2012 42 43 44 45 46 `````` match l with | [] => [] | x :: l => match i with | 0 => f x :: l `````` Robbert Krebbers committed Nov 12, 2012 47 `````` | S i => x :: @alter _ _ _ f go i l `````` Robbert Krebbers committed Aug 21, 2012 48 49 `````` end end. `````` Robbert Krebbers committed Nov 12, 2012 50 51 ``````Instance list_delete {A} : Delete nat (list A) := fix go (i : nat) (l : list A) {struct l} : list A := `````` Robbert Krebbers committed Oct 19, 2012 52 53 54 55 56 `````` match l with | [] => [] | x :: l => match i with | 0 => l `````` Robbert Krebbers committed Nov 12, 2012 57 `````` | S i => x :: @delete _ _ go i l `````` Robbert Krebbers committed Oct 19, 2012 58 `````` end `````` Robbert Krebbers committed Aug 29, 2012 59 `````` end. `````` Robbert Krebbers committed Nov 12, 2012 60 61 ``````Instance list_insert {A} : Insert nat (list A) A := λ i x, alter (λ _, x) i. `````` Robbert Krebbers committed Aug 29, 2012 62 `````` `````` Robbert Krebbers committed Oct 19, 2012 63 64 65 ``````Tactic Notation "discriminate_list_equality" hyp(H) := apply (f_equal length) in H; repeat (simpl in H || rewrite app_length in H); `````` Robbert Krebbers committed Nov 12, 2012 66 `````` exfalso; lia. `````` Robbert Krebbers committed Oct 19, 2012 67 68 69 70 71 72 73 74 75 76 77 ``````Tactic Notation "discriminate_list_equality" := repeat_on_hyps (fun H => discriminate_list_equality H). Ltac simplify_list_equality := repeat match goal with | _ => progress simplify_equality | H : _ ++ _ = _ ++ _ |- _ => first [ apply app_inv_head in H | apply app_inv_tail in H ] | H : _ |- _ => discriminate_list_equality H end. `````` Robbert Krebbers committed Nov 12, 2012 78 `````` `````` Robbert Krebbers committed Aug 29, 2012 79 80 ``````(** The function [option_list] converts an element of the option type into a list. *) `````` Robbert Krebbers committed Aug 21, 2012 81 ``````Definition option_list {A} : option A → list A := option_rect _ (λ x, [x]) []. `````` Robbert Krebbers committed Aug 29, 2012 82 83 84 `````` (** The predicate [prefix_of] holds if the first list is a prefix of the second. The predicate [suffix_of] holds if the first list is a suffix of the second. *) `````` Robbert Krebbers committed Aug 21, 2012 85 86 87 ``````Definition prefix_of {A} (l1 l2 : list A) : Prop := ∃ k, l2 = l1 ++ k. Definition suffix_of {A} (l1 l2 : list A) : Prop := ∃ k, l2 = k ++ l1. `````` Robbert Krebbers committed Nov 12, 2012 88 89 90 91 92 93 94 95 96 ``````(** The function [replicate n x] generates a list with length [n] of elements [x]. *) Fixpoint replicate {A} (n : nat) (x : A) : list A := match n with | 0 => [] | S n => x :: replicate n x end. Definition reverse {A} (l : list A) : list A := rev_append l []. `````` Robbert Krebbers committed Aug 29, 2012 97 98 ``````(** * General theorems *) Section general_properties. `````` Robbert Krebbers committed Jun 11, 2012 99 100 ``````Context {A : Type}. `````` Robbert Krebbers committed Nov 12, 2012 101 102 103 104 105 ``````Global Instance: ∀ k : list A, Injective (=) (=) (k ++). Proof. intros ???. apply app_inv_head. Qed. Global Instance: ∀ k : list A, Injective (=) (=) (++ k). Proof. intros ???. apply app_inv_tail. Qed. `````` Robbert Krebbers committed Aug 21, 2012 106 107 108 ``````Lemma list_eq (l1 l2 : list A) : (∀ i, l1 !! i = l2 !! i) → l1 = l2. Proof. revert l2. induction l1; intros [|??] H. `````` Robbert Krebbers committed Oct 19, 2012 109 `````` * done. `````` Robbert Krebbers committed Aug 21, 2012 110 111 `````` * discriminate (H 0). * discriminate (H 0). `````` Robbert Krebbers committed Nov 12, 2012 112 113 `````` * f_equal; [by injection (H 0) |]. apply IHl1. intro. apply (H (S _)). `````` Robbert Krebbers committed Aug 21, 2012 114 ``````Qed. `````` Robbert Krebbers committed Nov 12, 2012 115 116 ``````Lemma list_eq_nil (l : list A) : (∀ i, l !! i = None) → l = nil. Proof. intros. by apply list_eq. Qed. `````` Robbert Krebbers committed Aug 21, 2012 117 `````` `````` Robbert Krebbers committed Nov 12, 2012 118 119 120 121 122 123 124 125 ``````Global Instance list_eq_dec {dec : ∀ x y : A, Decision (x = y)} : ∀ l k, Decision (l = k) := list_eq_dec dec. Lemma nil_or_length_pos (l : list A) : l = [] ∨ length l ≠ 0. Proof. destruct l; simpl; auto with lia. Qed. Lemma nil_length (l : list A) : length l = 0 → l = []. Proof. by destruct l. Qed. Lemma lookup_nil i : @nil A !! i = None. `````` Robbert Krebbers committed Oct 19, 2012 126 ``````Proof. by destruct i. Qed. `````` Robbert Krebbers committed Nov 12, 2012 127 ``````Lemma lookup_tail (l : list A) i : tail l !! i = l !! S i. `````` Robbert Krebbers committed Oct 19, 2012 128 ``````Proof. by destruct l. Qed. `````` Robbert Krebbers committed Aug 21, 2012 129 `````` `````` Robbert Krebbers committed Nov 12, 2012 130 131 ``````Lemma lookup_lt_length (l : list A) i : is_Some (l !! i) ↔ i < length l. `````` Robbert Krebbers committed Aug 21, 2012 132 ``````Proof. `````` Robbert Krebbers committed Nov 12, 2012 133 134 135 136 137 `````` revert i. induction l. * split; by inversion 1. * intros [|?]; simpl. + split; eauto with arith. + by rewrite <-NPeano.Nat.succ_lt_mono. `````` Robbert Krebbers committed Aug 21, 2012 138 ``````Qed. `````` Robbert Krebbers committed Nov 12, 2012 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 ``````Lemma lookup_lt_length_1 (l : list A) i : is_Some (l !! i) → i < length l. Proof. apply lookup_lt_length. Qed. Lemma lookup_lt_length_alt (l : list A) i x : l !! i = Some x → i < length l. Proof. intros Hl. by rewrite <-lookup_lt_length, Hl. Qed. Lemma lookup_lt_length_2 (l : list A) i : i < length l → is_Some (l !! i). Proof. apply lookup_lt_length. Qed. Lemma lookup_ge_length (l : list A) i : l !! i = None ↔ length l ≤ i. Proof. rewrite eq_None_not_Some, lookup_lt_length. lia. Qed. Lemma lookup_ge_length_1 (l : list A) i : l !! i = None → length l ≤ i. Proof. by rewrite lookup_ge_length. Qed. Lemma lookup_ge_length_2 (l : list A) i : length l ≤ i → l !! i = None. Proof. by rewrite lookup_ge_length. Qed. Lemma lookup_app_l (l1 l2 : list A) i : i < length l1 → (l1 ++ l2) !! i = l1 !! i. Proof. revert i. induction l1; intros [|?]; simpl; auto with lia. Qed. Lemma lookup_app_l_Some (l1 l2 : list A) i x : l1 !! i = Some x → (l1 ++ l2) !! i = Some x. Proof. intros. rewrite lookup_app_l; eauto using lookup_lt_length_alt. Qed. Lemma lookup_app_r (l1 l2 : list A) i : (l1 ++ l2) !! (length l1 + i) = l2 !! i. `````` Robbert Krebbers committed Aug 21, 2012 170 ``````Proof. `````` Robbert Krebbers committed Nov 12, 2012 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 `````` revert i. induction l1; intros [|i]; simpl in *; simplify_equality; auto. Qed. Lemma lookup_app_r_alt (l1 l2 : list A) i : length l1 ≤ i → (l1 ++ l2) !! i = l2 !! (i - length l1). Proof. intros. assert (i = length l1 + (i - length l1)) as Hi by lia. rewrite Hi at 1. by apply lookup_app_r. Qed. Lemma lookup_app_r_Some (l1 l2 : list A) i x : l2 !! i = Some x → (l1 ++ l2) !! (length l1 + i) = Some x. Proof. by rewrite lookup_app_r. Qed. Lemma lookup_app_r_Some_alt (l1 l2 : list A) i x : length l1 ≤ i → l2 !! (i - length l1) = Some x → (l1 ++ l2) !! i = Some x. Proof. intro. by rewrite lookup_app_r_alt. Qed. Lemma lookup_app_inv (l1 l2 : list A) i x : (l1 ++ l2) !! i = Some x → l1 !! i = Some x ∨ l2 !! (i - length l1) = Some x. Proof. revert i. induction l1; intros [|i] ?; simpl in *; simplify_equality; auto. `````` Robbert Krebbers committed Aug 21, 2012 197 198 ``````Qed. `````` Robbert Krebbers committed Oct 19, 2012 199 ``````Lemma list_lookup_middle (l1 l2 : list A) (x : A) : `````` Robbert Krebbers committed Aug 21, 2012 200 `````` (l1 ++ x :: l2) !! length l1 = Some x. `````` Robbert Krebbers committed Oct 19, 2012 201 ``````Proof. by induction l1; simpl. Qed. `````` Robbert Krebbers committed Aug 21, 2012 202 `````` `````` Robbert Krebbers committed Nov 12, 2012 203 ``````Lemma lookup_take i j (l : list A) : `````` Robbert Krebbers committed Oct 19, 2012 204 205 206 207 208 209 `````` j < i → take i l !! j = l !! j. Proof. revert i j. induction l; intros [|i] j ?; trivial. * by destruct (le_Sn_0 j). * destruct j; simpl; auto with arith. Qed. `````` Robbert Krebbers committed Aug 21, 2012 210 `````` `````` Robbert Krebbers committed Nov 12, 2012 211 ``````Lemma lookup_take_le i j (l : list A) : `````` Robbert Krebbers committed Oct 19, 2012 212 213 214 215 216 217 218 `````` i ≤ j → take i l !! j = None. Proof. revert i j. induction l; intros [|i] [|j] ?; trivial. * by destruct (le_Sn_0 i). * simpl; auto with arith. Qed. `````` Robbert Krebbers committed Nov 12, 2012 219 ``````Lemma lookup_drop i j (l : list A) : `````` Robbert Krebbers committed Oct 19, 2012 220 221 222 `````` drop i l !! j = l !! (i + j). Proof. revert i j. induction l; intros [|i] ?; simpl; auto. Qed. `````` Robbert Krebbers committed Nov 12, 2012 223 224 225 226 227 228 229 230 231 ``````Lemma alter_length (f : A → A) l i : length (alter f i l) = length l. Proof. revert i. induction l; intros [|?]; simpl; auto with lia. Qed. Lemma insert_length (l : list A) i x : length (<[i:=x]>l) = length l. Proof. apply alter_length. Qed. Lemma list_lookup_alter (f : A → A) l i : alter f i l !! i = f <\$> l !! i. `````` Robbert Krebbers committed Oct 19, 2012 232 ``````Proof. revert i. induction l. done. intros [|i]. done. apply (IHl i). Qed. `````` Robbert Krebbers committed Nov 12, 2012 233 ``````Lemma list_lookup_alter_ne (f : A → A) l i j : `````` Robbert Krebbers committed Oct 19, 2012 234 235 236 237 238 `````` i ≠ j → alter f i l !! j = l !! j. Proof. revert i j. induction l; [done|]. intros [|i] [|j] ?; try done. apply (IHl i). congruence. Qed. `````` Robbert Krebbers committed Nov 12, 2012 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 ``````Lemma list_lookup_insert (l : list A) i x : i < length l → <[i:=x]>l !! i = Some x. Proof. intros Hi. unfold insert, list_insert. rewrite list_lookup_alter. by feed inversion (lookup_lt_length_2 l i). Qed. Lemma list_lookup_insert_ne (l : list A) i j x : i ≠ j → <[i:=x]>l !! j = l !! j. Proof. apply list_lookup_alter_ne. Qed. Lemma alter_app_l (f : A → A) (l1 l2 : list A) i : i < length l1 → alter f i (l1 ++ l2) = alter f i l1 ++ l2. Proof. revert i. induction l1; intros [|?] ?; simpl in *; f_equal; auto with lia. Qed. Lemma alter_app_r (f : A → A) (l1 l2 : list A) i : alter f (length l1 + i) (l1 ++ l2) = l1 ++ alter f i l2. Proof. revert i. induction l1; intros [|?]; simpl in *; f_equal; auto. Qed. Lemma alter_app_r_alt (f : A → A) (l1 l2 : list A) i : length l1 ≤ i → alter f i (l1 ++ l2) = l1 ++ alter f (i - length l1) l2. Proof. intros. assert (i = length l1 + (i - length l1)) as Hi by lia. rewrite Hi at 1. by apply alter_app_r. Qed. `````` Robbert Krebbers committed Oct 19, 2012 271 `````` `````` Robbert Krebbers committed Nov 12, 2012 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 ``````Lemma insert_app_l (l1 l2 : list A) i x : i < length l1 → <[i:=x]>(l1 ++ l2) = <[i:=x]>l1 ++ l2. Proof. apply alter_app_l. Qed. Lemma insert_app_r (l1 l2 : list A) i x : <[length l1 + i:=x]>(l1 ++ l2) = l1 ++ <[i:=x]>l2. Proof. apply alter_app_r. Qed. Lemma insert_app_r_alt (l1 l2 : list A) i x : length l1 ≤ i → <[i:=x]>(l1 ++ l2) = l1 ++ <[i - length l1:=x]>l2. Proof. apply alter_app_r_alt. Qed. Lemma take_nil i : take i (@nil A) = []. Proof. by destruct i. Qed. `````` Robbert Krebbers committed Oct 19, 2012 287 288 289 290 ``````Lemma take_alter (f : A → A) i j l : i ≤ j → take i (alter f j l) = take i l. Proof. intros. apply list_eq. intros jj. destruct (le_lt_dec i jj). `````` Robbert Krebbers committed Nov 12, 2012 291 292 `````` * by rewrite !lookup_take_le. * by rewrite !lookup_take, !list_lookup_alter_ne by lia. `````` Robbert Krebbers committed Oct 19, 2012 293 294 295 296 297 298 299 300 301 ``````Qed. Lemma take_insert i j (x : A) l : i ≤ j → take i (<[j:=x]>l) = take i l. Proof take_alter _ _ _ _. Lemma drop_alter (f : A → A) i j l : j < i → drop i (alter f j l) = drop i l. Proof. intros. apply list_eq. intros jj. `````` Robbert Krebbers committed Nov 12, 2012 302 `````` by rewrite !lookup_drop, !list_lookup_alter_ne by lia. `````` Robbert Krebbers committed Oct 19, 2012 303 304 305 306 307 ``````Qed. Lemma drop_insert i j (x : A) l : j < i → drop i (<[j:=x]>l) = drop i l. Proof drop_alter _ _ _ _. `````` Robbert Krebbers committed Nov 12, 2012 308 309 310 ``````Lemma insert_consecutive_length (l : list A) i k : length (insert_consecutive i k l) = length l. Proof. revert i. by induction k; intros; simpl; rewrite ?insert_length. Qed. `````` Robbert Krebbers committed Aug 29, 2012 311 `````` `````` Robbert Krebbers committed Nov 12, 2012 312 313 314 315 316 317 318 319 ``````Lemma not_elem_of_nil (x : A) : x ∉ []. Proof. by inversion 1. Qed. Lemma elem_of_nil (x : A) : x ∈ [] ↔ False. Proof. intuition. by destruct (not_elem_of_nil x). Qed. Lemma elem_of_nil_inv (l : list A) : (∀ x, x ∉ l) → l = []. Proof. destruct l. done. by edestruct 1; constructor. Qed. Lemma elem_of_cons (x y : A) l : x ∈ y :: l ↔ x = y ∨ x ∈ l. `````` Robbert Krebbers committed Oct 19, 2012 320 321 ``````Proof. split. `````` Robbert Krebbers committed Nov 12, 2012 322 323 `````` * inversion 1; subst. by left. by right. * intros [?|?]; subst. by left. by right. `````` Robbert Krebbers committed Oct 19, 2012 324 ``````Qed. `````` Robbert Krebbers committed Nov 12, 2012 325 326 ``````Lemma elem_of_app (x : A) l1 l2 : x ∈ l1 ++ l2 ↔ x ∈ l1 ∨ x ∈ l2. `````` Robbert Krebbers committed Oct 19, 2012 327 ``````Proof. `````` Robbert Krebbers committed Nov 12, 2012 328 329 330 331 `````` induction l1. * split; [by right|]. intros [Hx|]; [|done]. by destruct (elem_of_nil x). * simpl. rewrite !elem_of_cons, IHl1. tauto. `````` Robbert Krebbers committed Oct 19, 2012 332 ``````Qed. `````` Robbert Krebbers committed Nov 12, 2012 333 334 ``````Lemma elem_of_list_singleton (x y : A) : x ∈ [y] ↔ x = y. Proof. rewrite elem_of_cons, elem_of_nil. tauto. Qed. `````` Robbert Krebbers committed Aug 21, 2012 335 `````` `````` Robbert Krebbers committed Nov 12, 2012 336 337 338 ``````Global Instance elem_of_list_permutation_proper (x : A) : Proper (Permutation ==> iff) (x ∈). Proof. induction 1; rewrite ?elem_of_nil, ?elem_of_cons; intuition. Qed. `````` Robbert Krebbers committed Aug 21, 2012 339 `````` `````` Robbert Krebbers committed Nov 12, 2012 340 341 342 343 344 345 346 ``````Lemma elem_of_list_split (x : A) l : x ∈ l → ∃ l1 l2, l = l1 ++ x :: l2. Proof. induction 1 as [x l|x y l ? [l1 [l2 ?]]]. * by eexists [], l. * subst. by exists (y :: l1) l2. Qed. `````` Robbert Krebbers committed Aug 21, 2012 347 `````` `````` Robbert Krebbers committed Nov 12, 2012 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 ``````Global Instance elem_of_list_dec {dec : ∀ x y : A, Decision (x = y)} : ∀ (x : A) l, Decision (x ∈ l). Proof. intros x. refine ( fix go l := match l return Decision (x ∈ l) with | [] => right (not_elem_of_nil _) | y :: l => cast_if_or (decide_rel (=) x y) (go l) end); clear go dec; subst; try (by constructor); by inversion 1. Defined. Lemma elem_of_list_lookup_1 (l : list A) x : x ∈ l → ∃ i, l !! i = Some x. Proof. induction 1 as [|???? IH]. * by exists 0. * destruct IH as [i ?]; auto. by exists (S i). Qed. Lemma elem_of_list_lookup_2 (l : list A) i x : l !! i = Some x → x ∈ l. Proof. revert i. induction l; intros [|i] ?; simpl; simplify_equality; constructor; eauto. Qed. Lemma elem_of_list_lookup (l : list A) x : x ∈ l ↔ ∃ i, l !! i = Some x. `````` Robbert Krebbers committed Aug 29, 2012 374 ``````Proof. `````` Robbert Krebbers committed Nov 12, 2012 375 376 `````` firstorder eauto using elem_of_list_lookup_1, elem_of_list_lookup_2. `````` Robbert Krebbers committed Aug 29, 2012 377 ``````Qed. `````` Robbert Krebbers committed Jun 11, 2012 378 `````` `````` Robbert Krebbers committed Nov 12, 2012 379 380 381 382 383 384 385 386 ``````Lemma NoDup_nil : NoDup (@nil A) ↔ True. Proof. split; constructor. Qed. Lemma NoDup_cons (x : A) l : NoDup (x :: l) ↔ x ∉ l ∧ NoDup l. Proof. split. by inversion 1. intros [??]. by constructor. Qed. Lemma NoDup_cons_11 (x : A) l : NoDup (x :: l) → x ∉ l. Proof. rewrite NoDup_cons. by intros [??]. Qed. Lemma NoDup_cons_12 (x : A) l : NoDup (x :: l) → NoDup l. Proof. rewrite NoDup_cons. by intros [??]. Qed. `````` Robbert Krebbers committed Jun 11, 2012 387 ``````Lemma NoDup_singleton (x : A) : NoDup [x]. `````` Robbert Krebbers committed Nov 12, 2012 388 389 ``````Proof. constructor. apply not_elem_of_nil. constructor. Qed. `````` Robbert Krebbers committed Jun 11, 2012 390 ``````Lemma NoDup_app (l k : list A) : `````` Robbert Krebbers committed Nov 12, 2012 391 `````` NoDup (l ++ k) ↔ NoDup l ∧ (∀ x, x ∈ l → x ∉ k) ∧ NoDup k. `````` Robbert Krebbers committed Jun 11, 2012 392 ``````Proof. `````` Robbert Krebbers committed Nov 12, 2012 393 394 395 396 397 398 `````` induction l; simpl. * rewrite NoDup_nil. setoid_rewrite elem_of_nil. naive_solver. * rewrite !NoDup_cons. setoid_rewrite elem_of_cons. setoid_rewrite elem_of_app. naive_solver. `````` Robbert Krebbers committed Jun 11, 2012 399 400 ``````Qed. `````` Robbert Krebbers committed Nov 12, 2012 401 402 403 404 405 406 407 408 409 ``````Global Instance NoDup_permutation_proper: Proper (Permutation ==> iff) (@NoDup A). Proof. induction 1 as [|x l k Hlk IH | |]. * by rewrite !NoDup_nil. * by rewrite !NoDup_cons, IH, Hlk. * rewrite !NoDup_cons, !elem_of_cons. intuition. * intuition. Qed. `````` Robbert Krebbers committed Jun 11, 2012 410 `````` `````` Robbert Krebbers committed Nov 12, 2012 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 ``````Lemma NoDup_Permutation (l k : list A) : NoDup l → NoDup k → (∀ x, x ∈ l ↔ x ∈ k) → Permutation l k. Proof. intros Hl. revert k. induction Hl as [|x l Hin ? IH]. * intros k _ Hk. rewrite (elem_of_nil_inv k); [done |]. intros x. rewrite <-Hk, elem_of_nil. intros []. * intros k Hk Hlk. destruct (elem_of_list_split x k) as [l1 [l2 ?]]; subst. { rewrite <-Hlk. by constructor. } rewrite <-Permutation_middle, NoDup_cons in Hk. destruct Hk as [??]. apply Permutation_cons_app, IH; [done |]. intros y. specialize (Hlk y). rewrite <-Permutation_middle, !elem_of_cons in Hlk. naive_solver. Qed. `````` Robbert Krebbers committed Jun 11, 2012 428 `````` `````` Robbert Krebbers committed Aug 21, 2012 429 430 ``````Global Instance NoDup_dec {dec : ∀ x y : A, Decision (x = y)} : ∀ (l : list A), Decision (NoDup l) := `````` Robbert Krebbers committed Jun 11, 2012 431 432 `````` fix NoDup_dec l := match l return Decision (NoDup l) with `````` Robbert Krebbers committed Nov 12, 2012 433 `````` | [] => left NoDup_nil_2 `````` Robbert Krebbers committed Jun 11, 2012 434 `````` | x :: l => `````` Robbert Krebbers committed Nov 12, 2012 435 436 `````` match decide_rel (∈) x l with | left Hin => right (λ H, NoDup_cons_11 _ _ H Hin) `````` Robbert Krebbers committed Jun 11, 2012 437 438 `````` | right Hin => match NoDup_dec l with `````` Robbert Krebbers committed Nov 12, 2012 439 440 `````` | left H => left (NoDup_cons_2 _ _ Hin H) | right H => right (H ∘ NoDup_cons_12 _ _) `````` Robbert Krebbers committed Jun 11, 2012 441 442 443 444 `````` end end end. `````` Robbert Krebbers committed Nov 12, 2012 445 446 ``````Section remove_dups. Context `{!∀ x y : A, Decision (x = y)}. `````` Robbert Krebbers committed Oct 19, 2012 447 `````` `````` Robbert Krebbers committed Nov 12, 2012 448 449 450 451 452 453 `````` Fixpoint remove_dups (l : list A) : list A := match l with | [] => [] | x :: l => if decide_rel (∈) x l then remove_dups l else x :: remove_dups l end. `````` Robbert Krebbers committed Oct 19, 2012 454 `````` `````` Robbert Krebbers committed Nov 12, 2012 455 456 457 458 459 460 `````` Lemma elem_of_remove_dups l x : x ∈ remove_dups l ↔ x ∈ l. Proof. split; induction l; simpl; repeat case_decide; rewrite ?elem_of_cons; intuition (simplify_equality; auto). Qed. `````` Robbert Krebbers committed Oct 19, 2012 461 `````` `````` Robbert Krebbers committed Nov 12, 2012 462 463 464 465 466 467 `````` Lemma remove_dups_nodup l : NoDup (remove_dups l). Proof. induction l; simpl; repeat case_decide; try constructor; auto. by rewrite elem_of_remove_dups. Qed. End remove_dups. `````` Robbert Krebbers committed Oct 19, 2012 468 `````` `````` Robbert Krebbers committed Nov 12, 2012 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 ``````Lemma reverse_nil : reverse [] = @nil A. Proof. done. Qed. Lemma reverse_cons (l : list A) x : reverse (x :: l) = reverse l ++ [x]. Proof. unfold reverse. by rewrite <-!rev_alt. Qed. Lemma reverse_snoc (l : list A) x : reverse (l ++ [x]) = x :: reverse l. Proof. unfold reverse. by rewrite <-!rev_alt, rev_unit. Qed. Lemma reverse_app (l1 l2 : list A) : reverse (l1 ++ l2) = reverse l2 ++ reverse l1. Proof. unfold reverse. rewrite <-!rev_alt. apply rev_app_distr. Qed. Lemma reverse_length (l : list A) : length (reverse l) = length l. Proof. unfold reverse. rewrite <-!rev_alt. apply rev_length. Qed. Lemma reverse_involutive (l : list A) : reverse (reverse l) = l. Proof. unfold reverse. rewrite <-!rev_alt. apply rev_involutive. Qed. Lemma replicate_length n (x : A) : length (replicate n x) = n. Proof. induction n; simpl; auto. Qed. Lemma lookup_replicate n (x : A) i : i < n → replicate n x !! i = Some x. `````` Robbert Krebbers committed Oct 19, 2012 488 ``````Proof. `````` Robbert Krebbers committed Nov 12, 2012 489 490 491 492 493 494 495 496 497 `````` revert i. induction n; intros [|?]; naive_solver auto with lia. Qed. Lemma lookup_replicate_inv n (x y : A) i : replicate n x !! i = Some y → y = x ∧ i < n. Proof. revert i. induction n; intros [|?]; naive_solver auto with lia. Qed. `````` Robbert Krebbers committed Oct 19, 2012 498 `````` `````` Robbert Krebbers committed Nov 12, 2012 499 500 ``````Section Forall_Exists. Context (P : A → Prop). `````` Robbert Krebbers committed Oct 19, 2012 501 `````` `````` Robbert Krebbers committed Nov 12, 2012 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 `````` Lemma Forall_forall l : Forall P l ↔ ∀ x, x ∈ l → P x. Proof. split. * induction 1; inversion 1; subst; auto. * intros Hin. induction l; constructor. + apply Hin. constructor. + apply IHl. intros ??. apply Hin. by constructor. Qed. Lemma Forall_inv x l : Forall P (x :: l) → P x ∧ Forall P l. Proof. by inversion 1. Qed. Lemma Forall_inv_1 x l : Forall P (x :: l) → P x. Proof. by inversion 1. Qed. Lemma Forall_inv_2 x l : Forall P (x :: l) → Forall P l. Proof. by inversion 1. Qed. Lemma Forall_app l1 l2 : Forall P (l1 ++ l2) ↔ Forall P l1 ∧ Forall P l2. Proof. split. * induction l1; inversion 1; intuition. * intros [H ?]. induction H; simpl; intuition. Qed. Lemma Forall_true l : (∀ x, P x) → Forall P l. Proof. induction l; auto. Qed. Lemma Forall_impl l (Q : A → Prop) : Forall P l → (∀ x, P x → Q x) → Forall Q l. Proof. intros H ?. induction H; auto. Defined. Lemma Forall_delete l i : Forall P l → Forall P (delete i l). Proof. intros H. revert i. by induction H; intros [|i]; try constructor. Qed. Lemma Forall_lookup l : Forall P l ↔ ∀ i x, l !! i = Some x → P x. Proof. rewrite Forall_forall. setoid_rewrite elem_of_list_lookup. naive_solver. Qed. Lemma Forall_alter f l i : Forall P l → (∀ x, l !! i = Some x → P x → P (f x)) → Forall P (alter f i l). Proof. intros Hl. revert i. induction Hl; simpl; intros [|i]; constructor; auto. Qed. `````` Robbert Krebbers committed Jun 11, 2012 549 `````` `````` Robbert Krebbers committed Nov 12, 2012 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 `````` Lemma Exists_exists l : Exists P l ↔ ∃ x, x ∈ l ∧ P x. Proof. split. * induction 1 as [x|y ?? IH]. + exists x. split. constructor. done. + destruct IH as [x [??]]. exists x. split. by constructor. done. * intros [x [Hin ?]]. induction l. + by destruct (not_elem_of_nil x). + inversion Hin; subst. by left. right; auto. Qed. Lemma Exists_inv x l : Exists P (x :: l) → P x ∨ Exists P l. Proof. inversion 1; intuition trivial. Qed. Lemma Exists_app l1 l2 : Exists P (l1 ++ l2) ↔ Exists P l1 ∨ Exists P l2. Proof. split. * induction l1; inversion 1; intuition. * intros [H|H]. + induction H; simpl; intuition. + induction l1; simpl; intuition. Qed. `````` Robbert Krebbers committed Oct 19, 2012 571 `````` `````` Robbert Krebbers committed Nov 12, 2012 572 573 574 575 `````` Lemma Exists_not_Forall l : Exists (not ∘ P) l → ¬Forall P l. Proof. induction 1; inversion_clear 1; contradiction. Qed. Lemma Forall_not_Exists l : Forall (not ∘ P) l → ¬Exists P l. Proof. induction 1; inversion_clear 1; contradiction. Qed. `````` Robbert Krebbers committed Oct 19, 2012 576 `````` `````` Robbert Krebbers committed Nov 12, 2012 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 `````` Context {dec : ∀ x, Decision (P x)}. Fixpoint Forall_Exists_dec l : {Forall P l} + {Exists (not ∘ P) l}. Proof. refine ( match l with | [] => left _ | x :: l => cast_if_and (dec x) (Forall_Exists_dec l) end); clear Forall_Exists_dec; abstract intuition. Defined. Lemma not_Forall_Exists l : ¬Forall P l → Exists (not ∘ P) l. Proof. intro. destruct (Forall_Exists_dec l); intuition. Qed. Global Instance Forall_dec l : Decision (Forall P l) := match Forall_Exists_dec l with | left H => left H | right H => right (Exists_not_Forall _ H) end. Fixpoint Exists_Forall_dec l : {Exists P l} + {Forall (not ∘ P) l}. Proof. refine ( match l with | [] => right _ | x :: l => cast_if_or (dec x) (Exists_Forall_dec l) end); clear Exists_Forall_dec; abstract intuition. Defined. Lemma not_Exists_Forall l : ¬Exists P l → Forall (not ∘ P) l. Proof. intro. destruct (Exists_Forall_dec l); intuition. Qed. Global Instance Exists_dec l : Decision (Exists P l) := match Exists_Forall_dec l with | left H => left H | right H => right (Forall_not_Exists _ H) end. `````` Robbert Krebbers committed Oct 19, 2012 614 ``````End Forall_Exists. `````` Robbert Krebbers committed Nov 12, 2012 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 `````` Section Forall2. Context {B} (P : A → B → Prop). Lemma Forall2_length l1 l2 : Forall2 P l1 l2 → length l1 = length l2. Proof. induction 1; simpl; auto. Qed. Lemma Forall2_impl (Q : A → B → Prop) l1 l2 : Forall2 P l1 l2 → (∀ x y, P x y → Q x y) → Forall2 Q l1 l2. Proof. induction 1; auto. Qed. Lemma Forall2_unique l k1 k2 : Forall2 P l k1 → Forall2 P l k2 → (∀ x y1 y2, P x y1 → P x y2 → y1 = y2) → k1 = k2. Proof. intros H. revert k2. induction H; inversion_clear 1; intros; f_equal; eauto. Qed. Lemma Forall2_Forall_1 (Q : A → Prop) l k : Forall2 P l k → Forall (λ y, ∀ x, P x y → Q x) k → Forall Q l. Proof. induction 1; inversion_clear 1; constructor; eauto. Qed. Lemma Forall2_Forall_2 (Q : B → Prop) l k : Forall2 P l k → Forall (λ x, ∀ y, P x y → Q y) l → Forall Q k. Proof. induction 1; inversion_clear 1; constructor; eauto. Qed. End Forall2. Section Forall2_order. Context (R : relation A). Global Instance: PreOrder R → PreOrder (Forall2 R). Proof. split. * intros l. induction l; by constructor. * intros l k k' Hlk. revert k'. induction Hlk; inversion_clear 1; constructor. + etransitivity; eauto. + eauto. Qed. End Forall2_order. `````` Robbert Krebbers committed Aug 29, 2012 661 662 ``````End general_properties. `````` Robbert Krebbers committed Nov 12, 2012 663 664 665 666 667 668 669 670 671 672 673 674 675 ``````Ltac decompose_elem_of_list := repeat match goal with | H : ?x ∈ [] |- _ => by destruct (not_elem_of_nil x) | H : _ ∈ _ :: _ |- _ => apply elem_of_cons in H; destruct H | H : _ ∈ _ ++ _ |- _ => apply elem_of_app in H; destruct H end. Ltac decompose_Forall := repeat match goal with | H : Forall _ [] |- _ => clear H | H : Forall _ (_ :: _) |- _ => apply Forall_inv in H; destruct H | H : Forall _ (_ ++ _) |- _ => apply Forall_app in H; destruct H end. `````` Robbert Krebbers committed Aug 29, 2012 676 677 ``````(** * Theorems on the prefix and suffix predicates *) Section prefix_postfix. `````` Robbert Krebbers committed Nov 12, 2012 678 `````` Context {A : Type}. `````` Robbert Krebbers committed Jun 11, 2012 679 `````` `````` Robbert Krebbers committed Nov 12, 2012 680 681 682 683 684 685 686 `````` Global Instance: PreOrder (@prefix_of A). Proof. split. * intros ?. eexists []. by rewrite app_nil_r. * intros ??? [k1 ?] [k2 ?]. exists (k1 ++ k2). subst. by rewrite app_assoc. Qed. `````` Robbert Krebbers committed Jun 11, 2012 687 `````` `````` Robbert Krebbers committed Nov 12, 2012 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 `````` Lemma prefix_of_nil (l : list A) : prefix_of [] l. Proof. by exists l. Qed. Lemma prefix_of_nil_not x (l : list A) : ¬prefix_of (x :: l) []. Proof. by intros [k E]. Qed. Lemma prefix_of_cons x y (l1 l2 : list A) : x = y → prefix_of l1 l2 → prefix_of (x :: l1) (y :: l2). Proof. intros ? [k E]. exists k. by subst. Qed. Lemma prefix_of_cons_inv_1 x y (l1 l2 : list A) : prefix_of (x :: l1) (y :: l2) → x = y. Proof. intros [k E]. by injection E. Qed. Lemma prefix_of_cons_inv_2 x y (l1 l2 : list A) : prefix_of (x :: l1) (y :: l2) → prefix_of l1 l2. Proof. intros [k E]. exists k. by injection E. Qed. Lemma prefix_of_app_l (l1 l2 l3 : list A) : prefix_of (l1 ++ l3) l2 → prefix_of l1 l2. Proof. intros [k ?]. red. exists (l3 ++ k). subst. by rewrite <-app_assoc. Qed. Lemma prefix_of_app_r (l1 l2 l3 : list A) : prefix_of l1 l2 → prefix_of l1 (l2 ++ l3). Proof. intros [k ?]. exists (k ++ l3). subst. by rewrite app_assoc. Qed. Global Instance: PreOrder (@suffix_of A). Proof. split. * intros ?. by eexists []. * intros ??? [k1 ?] [k2 ?]. exists (k2 ++ k1). subst. by rewrite app_assoc. Qed. `````` Robbert Krebbers committed Jun 11, 2012 716 `````` `````` Robbert Krebbers committed Nov 12, 2012 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 `````` Lemma prefix_suffix_reverse (l1 l2 : list A) : prefix_of l1 l2 ↔ suffix_of (reverse l1) (reverse l2). Proof. split; intros [k E]; exists (reverse k). * by rewrite E, reverse_app. * by rewrite <-(reverse_involutive l2), E, reverse_app, reverse_involutive. Qed. Lemma suffix_prefix_reverse (l1 l2 : list A) : suffix_of l1 l2 ↔ prefix_of (reverse l1) (reverse l2). Proof. by rewrite prefix_suffix_reverse, !reverse_involutive. Qed. Lemma suffix_of_nil (l : list A) : suffix_of [] l. Proof. exists l. by rewrite app_nil_r. Qed. Lemma suffix_of_nil_inv (l : list A) : suffix_of l [] → l = []. Proof. by intros [[|?] ?]; simplify_list_equality. Qed. Lemma suffix_of_cons_nil_inv x (l : list A) : ¬suffix_of (x :: l) []. Proof. by intros [[] ?]. Qed. Lemma suffix_of_app (l1 l2 k : list A) : suffix_of l1 l2 → suffix_of (l1 ++ k) (l2 ++ k). Proof. intros [k' E]. exists k'. subst. by rewrite app_assoc. Qed. Lemma suffix_of_snoc_inv_1 x y (l1 l2 : list A) : suffix_of (l1 ++ [x]) (l2 ++ [y]) → x = y. Proof. rewrite suffix_prefix_reverse, !reverse_snoc. by apply prefix_of_cons_inv_1. Qed. Lemma suffix_of_snoc_inv_2 x y (l1 l2 : list A) : suffix_of (l1 ++ [x]) (l2 ++ [y]) → suffix_of l1 l2. Proof. rewrite !suffix_prefix_reverse, !reverse_snoc. by apply prefix_of_cons_inv_2. Qed. `````` Robbert Krebbers committed Jun 11, 2012 751 `````` `````` Robbert Krebbers committed Nov 12, 2012 752 753 754 755 `````` Lemma suffix_of_cons_l (l1 l2 : list A) x : suffix_of (x :: l1) l2 → suffix_of l1 l2. Proof. intros [k ?]. exists (k ++ [x]). subst. by rewrite <-app_assoc. Qed. Lemma suffix_of_app_l (l1 l2 l3 : list A) : `````` Robbert Krebbers committed Jun 11, 2012 756 `````` `````` Robbert Krebbers committed Aug 21, 2012 757 `````` suffix_of (l3 ++ l1) l2 → suffix_of l1 l2. `````` Robbert Krebbers committed Nov 12, 2012 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 `````` Proof. intros [k ?]. exists (k ++ l3). subst. by rewrite <-app_assoc. Qed. Lemma suffix_of_cons_r (l1 l2 : list A) x : suffix_of l1 l2 → suffix_of l1 (x :: l2). Proof. intros [k ?]. exists (x :: k). by subst. Qed. Lemma suffix_of_app_r (l1 l2 l3 : list A) : suffix_of l1 l2 → suffix_of l1 (l3 ++ l2). Proof. intros [k ?]. exists (l3 ++ k). subst. by rewrite app_assoc. Qed. Lemma suffix_of_cons_inv (l1 l2 : list A) x y : suffix_of (x :: l1) (y :: l2) → x :: l1 = y :: l2 ∨ suffix_of (x :: l1) l2. Proof. intros [[|? k] E]. * by left. * right. simplify_equality. by apply suffix_of_app_r. Qed. `````` Robbert Krebbers committed Oct 19, 2012 774 `````` `````` Robbert Krebbers committed Nov 12, 2012 775 776 `````` Lemma suffix_of_cons_not x (l : list A) : ¬suffix_of (x :: l) l. Proof. intros [??]. discriminate_list_equality. Qed. `````` Robbert Krebbers committed Jun 11, 2012 777 `````` `````` Robbert Krebbers committed Nov 12, 2012 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 `````` Context `{∀ x y : A, Decision (x = y)}. Fixpoint strip_prefix (l1 l2 : list A) : list A * (list A * list A) := match l1, l2 with | [], l2 => ([], ([], l2)) | l1, [] => ([], (l1, [])) | x :: l1, y :: l2 => if decide_rel (=) x y then fst_map (x ::) (strip_prefix l1 l2) else ([], (x :: l1, y :: l2)) end. Global Instance prefix_of_dec: ∀ l1 l2 : list A, Decision (prefix_of l1 l2) := fix go l1 l2 := match l1, l2 return { prefix_of l1 l2 } + { ¬prefix_of l1 l2 } with | [], _ => left (prefix_of_nil _) | _, [] => right (prefix_of_nil_not _ _) | x :: l1, y :: l2 => match decide_rel (=) x y with | left Exy => match go l1 l2 with | left Hl1l2 => left (prefix_of_cons _ _ _ _ Exy Hl1l2) | right Hl1l2 => right (Hl1l2 ∘ prefix_of_cons_inv_2 _ _ _ _) end | right Exy => right (Exy ∘ prefix_of_cons_inv_1 _ _ _ _) end end. Global Instance suffix_of_dec (l1 l2 : list A) : Decision (suffix_of l1 l2). Proof. refine (cast_if (decide_rel prefix_of (reverse l1) (reverse l2))); abstract (by rewrite suffix_prefix_reverse). Defined. `````` Robbert Krebbers committed Aug 29, 2012 813 ``````End prefix_postfix. `````` Robbert Krebbers committed Jun 11, 2012 814 `````` `````` Robbert Krebbers committed Nov 12, 2012 815 ``````(** The [simplify_suffix_of] tactic removes [suffix_of] hypotheses that are `````` Robbert Krebbers committed Oct 19, 2012 816 ``````tautologies, and simplifies [suffix_of] hypotheses involving [(::)] and `````` Robbert Krebbers committed Aug 29, 2012 817 ``````[(++)]. *) `````` Robbert Krebbers committed Jun 11, 2012 818 819 ``````Ltac simplify_suffix_of := repeat match goal with `````` Robbert Krebbers committed Aug 29, 2012 820 821 822 `````` | H : suffix_of (_ :: _) _ |- _ => destruct (suffix_of_cons_not _ _ H) | H : suffix_of (_ :: _) [] |- _ => `````` Robbert Krebbers committed Oct 19, 2012 823 `````` apply suffix_of_nil_inv in H `````` Robbert Krebbers committed Aug 21, 2012 824 825 `````` | H : suffix_of (_ :: _) (_ :: _) |- _ => destruct (suffix_of_cons_inv _ _ _ _ H); clear H `````` Robbert Krebbers committed Jun 11, 2012 826 827 828 `````` | H : suffix_of ?x ?x |- _ => clear H | H : suffix_of ?x (_ :: ?x) |- _ => clear H | H : suffix_of ?x (_ ++ ?x) |- _ => clear H `````` Robbert Krebbers committed Aug 29, 2012 829 `````` | _ => progress simplify_equality `````` Robbert Krebbers committed Jun 11, 2012 830 831 `````` end. `````` Robbert Krebbers committed Nov 12, 2012 832 833 ``````(** The [solve_suffix_of] tactic tries to solve goals involving [suffix_of]. It uses [simplify_suffix_of] to simplify hypotheses and tries to solve [suffix_of] `````` Robbert Krebbers committed Oct 19, 2012 834 835 ``````conclusions. This tactic either fails or proves the goal. *) Ltac solve_suffix_of := solve [intuition (repeat `````` Robbert Krebbers committed Aug 21, 2012 836 `````` match goal with `````` Robbert Krebbers committed Oct 19, 2012 837 838 `````` | _ => done | _ => progress simplify_suffix_of `````` Robbert Krebbers committed Aug 29, 2012 839 840 `````` | |- suffix_of [] _ => apply suffix_of_nil | |- suffix_of _ _ => reflexivity `````` Robbert Krebbers committed Oct 19, 2012 841 842 843 844 `````` | |- suffix_of _ (_ :: _) => apply suffix_of_cons_r | |- suffix_of _ (_ ++ _) => apply suffix_of_app_r | H : suffix_of _ _ → False |- _ => destruct H end)]. `````` Robbert Krebbers committed Aug 29, 2012 845 846 ``````Hint Extern 0 (PropHolds (suffix_of _ _)) => unfold PropHolds; solve_suffix_of : typeclass_instances. `````` Robbert Krebbers committed Jun 11, 2012 847 `````` `````` Robbert Krebbers committed Nov 12, 2012 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 ``````(** * Folding lists *) Notation foldr := fold_right. Notation foldr_app := fold_right_app. Lemma foldr_permutation {A B} (R : relation B) `{!Equivalence R} (f : A → B → B) (b : B) `{!Proper ((=) ==> R ==> R) f} (Hf : ∀ a1 a2 b, R (f a1 (f a2 b)) (f a2 (f a1 b))) : Proper (Permutation ==> R) (foldr f b). Proof. induction 1; simpl. * done. * by f_equiv. * apply Hf. * etransitivity; eauto. Qed. (** We redefine [foldl] with the arguments in the same order as in Haskell. *) Definition foldl {A B} (f : A → B → A) : A → list B → A := fix go a l := match l with | [] => a | x :: l => go (f a x) l end. Lemma foldl_app {A B} (f : A → B → A) (l k : list B) (a : A) : foldl f a (l ++ k) = foldl f (foldl f a l) k. Proof. revert a. induction l; simpl; auto. Qed. `````` Robbert Krebbers committed Aug 29, 2012 878 ``````(** * Monadic operations *) `````` Robbert Krebbers committed Nov 12, 2012 879 880 ``````Instance list_ret: MRet list := λ A x, x :: @nil A. Instance list_fmap {A B} (f : A → B) : FMapD list f := `````` Robbert Krebbers committed Oct 19, 2012 881 `````` fix go (l : list A) := `````` Robbert Krebbers committed Jun 11, 2012 882 883 `````` match l with | [] => [] `````` Robbert Krebbers committed Oct 19, 2012 884 `````` | x :: l => f x :: @fmap _ _ _ f go l `````` Robbert Krebbers committed Jun 11, 2012 885 `````` end. `````` Robbert Krebbers committed Nov 12, 2012 886 887 ``````Instance list_bind {A B} (f : A → list B) : MBindD list f := fix go (l : list A) := `````` Robbert Krebbers committed Jun 11, 2012 888 `````` match l with `````` Robbert Krebbers committed Nov 12, 2012 889 890 891 892 893 894 895 896 897 898 899 `````` | [] => [] | x :: l => f x ++ @mbind _ _ _ f go l end. Instance list_join: MJoin list := λ A, mbind id. Definition mapM `{!MBind M} `{!MRet M} {A B} (f : A → M B) : list A → M (list B) := fix go l := match l with | [] => mret [] | x :: l => y ← f x; k ← go l; mret (y :: k) `````` Robbert Krebbers committed Jun 11, 2012 900 901 `````` end. `````` Robbert Krebbers committed Aug 21, 2012 902 903 ``````Section list_fmap. Context {A B : Type} (f : A → B). `````` Robbert Krebbers committed Jun 11, 2012 904 `````` `````` Robbert Krebbers committed Oct 19, 2012 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 `````` Global Instance: Injective (=) (=) f → Injective (=) (=) (fmap f). Proof. intros ? l1. induction l1 as [|x l1 IH]. * by intros [|??]. * intros [|??]; [done |]; simpl; intros; simplify_equality. by f_equal; [apply (injective f) | auto]. Qed. Lemma fmap_app l1 l2 : f <\$> l1 ++ l2 = (f <\$> l1) ++ (f <\$> l2). Proof. induction l1; simpl; by f_equal. Qed. Lemma fmap_cons_inv y l k : f <\$> l = y :: k → ∃ x l', y = f x ∧ l = x :: l'. Proof. intros. destruct l; simpl; simplify_equality; eauto. Qed. Lemma fmap_app_inv l k1 k2 : f <\$> l = k1 ++ k2 → ∃ l1 l2, k1 = f <\$> l1 ∧ k2 = f <\$> l2 ∧ l = l1 ++ l2. Proof. revert l. induction k1 as [|y k1 IH]; simpl. * intros l ?. by eexists [], l. * intros [|x l] ?; simpl; simplify_equality. destruct (IH l) as [l1 [l2 [? [??]]]]; subst; [done |]. by exists (x :: l1) l2. Qed. `````` Robbert Krebbers committed Aug 29, 2012 926 `````` `````` Robbert Krebbers committed Aug 21, 2012 927 `````` Lemma fmap_length l : length (f <\$> l) = length l. `````` Robbert Krebbers committed Oct 19, 2012 928 929 930 931 932 933 `````` Proof. induction l; simpl; by f_equal. Qed. Lemma fmap_reverse l : f <\$> reverse l = reverse (f <\$> l). Proof. induction l; simpl; [done |]. by rewrite !reverse_cons, fmap_app, IHl. Qed. `````` Robbert Krebbers committed Aug 21, 2012 934 935 `````` Lemma list_lookup_fmap l i : (f <\$> l) !! i = f <\$> (l !! i). `````` Robbert Krebbers committed Oct 19, 2012 936 `````` Proof. revert i. induction l; by intros [|]. Qed. `````` Robbert Krebbers committed Nov 12, 2012 937 938 939 `````` Lemma list_alter_fmap (g : A → A) (h : B → B) l i : Forall (λ x, f (g x) = h (f x)) l → f <\$> alter g i l = alter h i (f <\$> l). `````` Robbert Krebbers committed Aug 21, 2012 940 `````` Proof. `````` Robbert Krebbers committed Nov 12, 2012 941 942 `````` intros Hl. revert i. induction Hl; intros [|i]; simpl; f_equal; auto. `````` Robbert Krebbers committed Aug 21, 2012 943 `````` Qed. `````` Robbert Krebbers committed Nov 12, 2012 944 945 946 947 948 `````` Lemma elem_of_list_fmap_1 l x : x ∈ l → f x ∈ f <\$> l. Proof. induction 1; simpl; rewrite elem_of_cons; intuition. Qed. Lemma elem_of_list_fmap_1_alt l x y : x ∈ l → y = f x → y ∈ f <\$> l. Proof. intros. subst. by apply elem_of_list_fmap_1. Qed. Lemma elem_of_list_fmap_2 l x : x ∈ f <\$> l → ∃ y, x = f y ∧ y ∈ l. `````` Robbert Krebbers committed Jun 11, 2012 949 `````` Proof. `````` Robbert Krebbers committed Nov 12, 2012 950 951 952 `````` induction l as [|y l IH]; simpl; intros; decompose_elem_of_list. + exists y. split; [done | by left]. + destruct IH as [z [??]]. done. exists z. split; [done | by right]. `````` Robbert Krebbers committed Jun 11, 2012 953 `````` Qed. `````` Robbert Krebbers committed Nov 12, 2012 954 955 `````` Lemma elem_of_list_fmap l x : x ∈ f <\$> l ↔ ∃ y, x = f y ∧ y ∈ l. Proof. firstorder eauto using elem_of_list_fmap_1_alt, elem_of_list_fmap_2. Qed. `````` Robbert Krebbers committed Oct 19, 2012 956 957 `````` Lemma Forall_fmap (l : list A) (P : B → Prop) : `````` Robbert Krebbers committed Nov 12, 2012 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 `````` Forall P (f <\$> l) ↔ Forall (P ∘ f) l. Proof. induction l; split; inversion_clear 1; constructor; firstorder auto. Qed. Lemma mapM_fmap (g : B → option A) (l : list A) : (∀ x, g (f x) = Some x) → mapM g (f <\$> l) = Some l. Proof. intros E. induction l; simpl. * done. * by rewrite E, IHl. Qed. Lemma mapM_fmap_inv (g : B → option A) (l : list A) (k : list B) : (∀ x y, g y = Some x → y = f x) → mapM g k = Some l → k = f <\$> l. Proof. intros Hgf. revert l; induction k as [|y k]; intros [|x l] ?; simplify_option_equality; f_equiv; eauto. Qed. `````` Robbert Krebbers committed Aug 21, 2012 979 980 ``````End list_fmap. `````` Robbert Krebbers committed Nov 12, 2012 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 ``````Section list_bind. Context {A B : Type} (f : A → list B). Lemma bind_app (l1 l2 : list A) : (l1 ++ l2) ≫= f = (l1 ≫= f) ++ (l2 ≫= f). Proof. induction l1; simpl; [done|]. by rewrite <-app_assoc, IHl1. Qed. Lemma elem_of_list_bind (x : B) (l : list A) : x ∈ l ≫= f ↔ ∃ y, x ∈ f y ∧ y ∈ l. Proof. split. * induction l as [|y l IH]; simpl; intros; decompose_elem_of_list. + exists y. split; [done | by left]. + destruct IH as [z [??]]. done. exists z. split; [done | by right]. * intros [y [Hx Hy]]. induction Hy; simpl; rewrite elem_of_app; intuition. Qed. End list_bind. Section list_ret_join. Context {A : Type}. Lemma elem_of_list_ret (x y : A) : x ∈ @mret list _ A y ↔ x = y. Proof. apply elem_of_list_singleton. Qed. Lemma elem_of_list_join (x : A) (ll : list (list A)) : x ∈ mjoin ll ↔ ∃ l, x ∈ l ∧ l ∈ ll. Proof. unfold mjoin, list_join. by rewrite elem_of_list_bind. Qed. Lemma join_nil (ls : list (list A)) : mjoin ls = [] ↔ Forall (= nil) ls. Proof. unfold mjoin, list_join. split. * by induction ls as [|[|??] ?]; constructor; auto. * by induction 1 as [|[|??] ?]. Qed. Lemma join_nil_1 (ls : list (list A)) : mjoin ls = [] → Forall (= nil) ls. Proof. by rewrite join_nil. Qed. Lemma join_nil_2 (ls : list (list A)) : Forall (= nil) ls → mjoin ls = []. Proof. by rewrite join_nil. Qed. Lemma join_length (ls : list (list A)) : length (mjoin ls) = foldr (plus ∘ length) 0 ls. Proof. unfold mjoin, list_join. by induction ls; simpl; rewrite ?app_length; f_equal. Qed. Lemma join_length_same (ls : list (list A)) n : Forall (λ l, length l = n) ls → length (mjoin ls) = length ls * n. Proof. rewrite join_length. by induction 1; simpl; f_equal. Qed. Lemma lookup_join_same_length (ls : list (list A)) n i : n ≠ 0 → Forall (λ l, length l = n) ls → mjoin ls !! i = ls !! (i `div` n) ≫= (!! (i `mod` n)). Proof. intros Hn Hls. revert i. unfold mjoin, list_join. induction Hls as [|l ls ? Hls IH]; simpl; [done |]. intros i. destruct (decide (i < n)) as [Hin|Hin]. * rewrite <-(NPeano.Nat.div_unique i n 0 i) by lia. rewrite <-(NPeano.Nat.mod_unique i n 0 i) by lia. simpl. rewrite lookup_app_l; auto with lia. * replace i with ((i - n) + 1 * n) by lia. rewrite NPeano.Nat.div_add, NPeano.Nat.mod_add by done. replace (i - n + 1 * n) with i by lia. rewrite (plus_comm _ 1), lookup_app_r_alt, IH by lia. by subst. Qed. (* This should be provable using the previous lemma in a shorter way *) Lemma alter_join_same_length f (ls : list (list A)) n i : n ≠ 0 → Forall (λ l, length l = n) ls → alter f i (mjoin ls) = mjoin (alter (alter f (i `mod` n)) (i `div` n) ls). Proof. intros Hn Hls. revert i. unfold mjoin, list_join. induction Hls as [|l ls ? Hls IH]; simpl; [done |]. intros i. destruct (decide (i < n)) as [Hin|Hin]. * rewrite <-(NPeano.Nat.div_unique i n 0 i) by lia. rewrite <-(NPeano.Nat.mod_unique i n 0 i) by lia. simpl. rewrite alter_app_l; auto with lia. * replace i with ((i - n) + 1 * n) by lia. rewrite NPeano.Nat.div_add, NPeano.Nat.mod_add by done. replace (i - n + 1 * n) with i by lia. rewrite (plus_comm _ 1), alter_app_r_alt, IH by lia. by subst. Qed. Lemma insert_join_same_length (ls : list (list A)) n i x : n ≠ 0 → Forall (λ l, length l = n) ls → <[i:=x]>(mjoin ls) = mjoin (alter <[i `mod` n:=x]> (i `div` n) ls). Proof. apply alter_join_same_length. Qed. End list_ret_join. `````` Robbert Krebbers committed Oct 19, 2012 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 ``````Ltac simplify_list_fmap_equality := repeat match goal with | _ => progress simplify_equality | H : _ <\$> _ = _ :: _ |- _ => apply fmap_cons_inv in H; destruct H as [? [? [??]]] | H : _ :: _ = _ <\$> _ |- _ => symmetry in H | H : _ <\$> _ = _ ++ _ |- _ => apply fmap_app_inv in H; destruct H as [? [? [? [??]]]] | H : _ ++ _ = _ <\$> _ |- _ => symmetry in H end. `````` Robbert Krebbers committed Aug 21, 2012 1091 `````` `````` Robbert Krebbers committed Aug 29, 2012 1092 1093 1094 ``````(** * Indexed folds and maps *) (** We define stronger variants of map and fold that also take the index of the element into account. *) `````` Robbert Krebbers committed Aug 21, 2012 1095 1096 1097 1098 1099 1100 1101 ``````Definition imap_go {A B} (f : nat → A → B) : nat → list A → list B := fix go (n : nat) (l : list A) := match l with | [] => [] | x :: l => f n x :: go (S n) l end. Definition imap {A B} (f : nat → A → B) : list A → list B := imap_go f 0. `````` Robbert Krebbers committed Jun 11, 2012 1102 `````` `````` Robbert Krebbers committed Oct 19, 2012 1103 ``````Definition ifoldr {A B} (f : nat → B → A → A) `````` Robbert Krebbers committed Aug 29, 2012 1104 `````` (a : nat → A) : nat → list B → A := `````` Robbert Krebbers committed Aug 21, 2012 1105 1106 1107 1108 1109 `````` fix go (n : nat) (l : list B) : A := match l with | nil => a n | b :: l => f n b (go (S n) l) end. `````` Robbert Krebbers committed Jun 11, 2012 1110 `````` `````` Robbert Krebbers committed Oct 19, 2012 1111 ``````Lemma ifoldr_app {A B} (f : nat → B → A → A) (a : nat → A) `````` Robbert Krebbers committed Aug 29, 2012 1112 `````` (l1 l2 : list B) n : `````` Robbert Krebbers committed Oct 19, 2012 1113 `````` ifoldr f a n (l1 ++ l2) = ifoldr f (λ n, ifoldr f a n l2) n l1. `````` Robbert Krebbers committed Aug 29, 2012 1114 1115 1116 ``````Proof. revert n a. induction l1 as [| b l1 IH ]; intros; simpl; f_equal; auto. Qed. `````` Robbert Krebbers committed Jun 11, 2012 1117 `````` `````` Robbert Krebbers committed Aug 29, 2012 1118 1119 1120 ``````(** * Lists of the same length *) (** The [same_length] view allows convenient induction over two lists with the same length. *) `````` Robbert Krebbers committed Aug 21, 2012 1121 1122 ``````Section same_length. Context {A B : Type}. `````` Robbert Krebbers committed Jun 11, 2012 1123 `````` `````` Robbert Krebbers committed Aug 21, 2012 1124 1125 1126 1127 1128 1129 1130 `````` Inductive same_length : list A → list B → Prop := | same_length_nil : same_length [] [] | same_length_cons x y l k : same_length l k → same_length (x :: l) (y :: k). Lemma same_length_length l k : same_length l k ↔ length l = length k. `````` Robbert Krebbers committed