orders.v 24.4 KB
 Robbert Krebbers committed Feb 08, 2015 1 ``````(* Copyright (c) 2012-2015, Robbert Krebbers. *) `````` Robbert Krebbers committed Aug 29, 2012 2 3 4 ``````(* This file is distributed under the terms of the BSD license. *) (** This file collects common properties of pre-orders and semi lattices. This theory will mainly be used for the theory on collections and finite maps. *) `````` Robbert Krebbers committed Feb 13, 2016 5 6 ``````From Coq Require Export Sorted. From stdpp Require Export base decidable tactics list. `````` Robbert Krebbers committed Jun 11, 2012 7 `````` `````` Robbert Krebbers committed Aug 12, 2013 8 9 10 11 12 13 14 15 16 17 18 19 ``````(** * Arbitrary pre-, parial and total orders *) (** Properties about arbitrary pre-, partial, and total orders. We do not use the relation [⊆] because we often have multiple orders on the same structure *) Section orders. Context {A} {R : relation A}. Implicit Types X Y : A. Infix "⊆" := R. Notation "X ⊈ Y" := (¬X ⊆ Y). Infix "⊂" := (strict R). Lemma reflexive_eq `{!Reflexive R} X Y : X = Y → X ⊆ Y. Proof. by intros <-. Qed. `````` Robbert Krebbers committed Feb 11, 2016 20 21 `````` Lemma anti_symm_iff `{!PartialOrder R} X Y : X = Y ↔ R X Y ∧ R Y X. Proof. split. by intros ->. by intros [??]; apply (anti_symm _). Qed. `````` Robbert Krebbers committed Aug 12, 2013 22 23 24 25 26 27 28 29 30 31 `````` Lemma strict_spec X Y : X ⊂ Y ↔ X ⊆ Y ∧ Y ⊈ X. Proof. done. Qed. Lemma strict_include X Y : X ⊂ Y → X ⊆ Y. Proof. by intros [? _]. Qed. Lemma strict_ne X Y : X ⊂ Y → X ≠ Y. Proof. by intros [??] <-. Qed. Lemma strict_ne_sym X Y : X ⊂ Y → Y ≠ X. Proof. by intros [??] <-. Qed. Lemma strict_transitive_l `{!Transitive R} X Y Z : X ⊂ Y → Y ⊆ Z → X ⊂ Z. Proof. `````` Ralf Jung committed Feb 20, 2016 32 33 `````` intros [? HXY] ?. split; [by trans Y|]. contradict HXY. by trans Z. `````` Robbert Krebbers committed Aug 12, 2013 34 35 36 `````` Qed. Lemma strict_transitive_r `{!Transitive R} X Y Z : X ⊆ Y → Y ⊂ Z → X ⊂ Z. Proof. `````` Ralf Jung committed Feb 20, 2016 37 38 `````` intros ? [? HYZ]. split; [by trans Y|]. contradict HYZ. by trans X. `````` Robbert Krebbers committed Aug 12, 2013 39 40 41 42 43 44 45 46 47 48 `````` Qed. Global Instance: Irreflexive (strict R). Proof. firstorder. Qed. Global Instance: Transitive R → StrictOrder (strict R). Proof. split; try apply _. eauto using strict_transitive_r, strict_include. Qed. Global Instance preorder_subset_dec_slow `{∀ X Y, Decision (X ⊆ Y)} (X Y : A) : Decision (X ⊂ Y) | 100 := _. `````` Robbert Krebbers committed Feb 11, 2016 49 `````` Lemma strict_spec_alt `{!AntiSymm (=) R} X Y : X ⊂ Y ↔ X ⊆ Y ∧ X ≠ Y. `````` Robbert Krebbers committed Aug 12, 2013 50 51 `````` Proof. split. `````` Robbert Krebbers committed Feb 17, 2016 52 53 `````` - intros [? HYX]. split. done. by intros <-. - intros [? HXY]. split. done. by contradict HXY; apply (anti_symm R). `````` Robbert Krebbers committed Aug 12, 2013 54 `````` Qed. `````` Robbert Krebbers committed Nov 15, 2014 55 `````` Lemma po_eq_dec `{!PartialOrder R, ∀ X Y, Decision (X ⊆ Y)} (X Y : A) : `````` Robbert Krebbers committed Aug 12, 2013 56 57 58 `````` Decision (X = Y). Proof. refine (cast_if_and (decide (X ⊆ Y)) (decide (Y ⊆ X))); `````` Robbert Krebbers committed Feb 11, 2016 59 `````` abstract (rewrite anti_symm_iff; tauto). `````` Robbert Krebbers committed Aug 12, 2013 60 61 62 63 64 `````` Defined. Lemma total_not `{!Total R} X Y : X ⊈ Y → Y ⊆ X. Proof. intros. destruct (total R X Y); tauto. Qed. Lemma total_not_strict `{!Total R} X Y : X ⊈ Y → Y ⊂ X. Proof. red; auto using total_not. Qed. `````` Robbert Krebbers committed Sep 09, 2013 65 `````` Global Instance trichotomy_total `````` Robbert Krebbers committed Nov 15, 2014 66 `````` `{!Trichotomy (strict R), !Reflexive R} : Total R. `````` Robbert Krebbers committed Sep 09, 2013 67 68 69 70 71 72 73 74 75 76 77 78 79 `````` Proof. intros X Y. destruct (trichotomy (strict R) X Y) as [[??]|[<-|[??]]]; intuition. Qed. End orders. Section strict_orders. Context {A} {R : relation A}. Implicit Types X Y : A. Infix "⊂" := R. Lemma irreflexive_eq `{!Irreflexive R} X Y : X = Y → ¬X ⊂ Y. Proof. intros ->. apply (irreflexivity R). Qed. `````` Robbert Krebbers committed Feb 11, 2016 80 `````` Lemma strict_anti_symm `{!StrictOrder R} X Y : `````` Robbert Krebbers committed Sep 09, 2013 81 `````` X ⊂ Y → Y ⊂ X → False. `````` Ralf Jung committed Feb 20, 2016 82 `````` Proof. intros. apply (irreflexivity R X). by trans Y. Qed. `````` Robbert Krebbers committed Nov 15, 2014 83 84 `````` Global Instance trichotomyT_dec `{!TrichotomyT R, !StrictOrder R} X Y : Decision (X ⊂ Y) := `````` Robbert Krebbers committed Aug 12, 2013 85 `````` match trichotomyT R X Y with `````` Robbert Krebbers committed Sep 09, 2013 86 87 `````` | inleft (left H) => left H | inleft (right H) => right (irreflexive_eq _ _ H) `````` Robbert Krebbers committed Feb 11, 2016 88 `````` | inright H => right (strict_anti_symm _ _ H) `````` Robbert Krebbers committed Aug 12, 2013 89 90 91 `````` end. Global Instance trichotomyT_trichotomy `{!TrichotomyT R} : Trichotomy R. Proof. intros X Y. destruct (trichotomyT R X Y) as [[|]|]; tauto. Qed. `````` Robbert Krebbers committed Sep 09, 2013 92 ``````End strict_orders. `````` Robbert Krebbers committed Aug 12, 2013 93 94 95 `````` Ltac simplify_order := repeat match goal with `````` Robbert Krebbers committed Feb 17, 2016 96 `````` | _ => progress simplify_eq/= `````` Robbert Krebbers committed Sep 09, 2013 97 `````` | H : ?R ?x ?x |- _ => by destruct (irreflexivity _ _ H) `````` Robbert Krebbers committed Aug 12, 2013 98 99 100 `````` | H1 : ?R ?x ?y |- _ => match goal with | H2 : R y x |- _ => `````` Robbert Krebbers committed Feb 11, 2016 101 `````` assert (x = y) by (by apply (anti_symm R)); clear H1 H2 `````` Robbert Krebbers committed Aug 12, 2013 102 103 `````` | H2 : R y ?z |- _ => unless (R x z) by done; `````` Ralf Jung committed Feb 20, 2016 104 `````` assert (R x z) by (by trans y) `````` Robbert Krebbers committed Aug 12, 2013 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 `````` end end. (** * Sorting *) (** Merge sort. Adapted from the implementation of Hugo Herbelin in the Coq standard library, but without using the module system. *) Section merge_sort. Context {A} (R : relation A) `{∀ x y, Decision (R x y)}. Fixpoint list_merge (l1 : list A) : list A → list A := fix list_merge_aux l2 := match l1, l2 with | [], _ => l2 | _, [] => l1 | x1 :: l1, x2 :: l2 => if decide_rel R x1 x2 then x1 :: list_merge l1 (x2 :: l2) else x2 :: list_merge_aux l2 end. Global Arguments list_merge !_ !_ /. Local Notation stack := (list (option (list A))). Fixpoint merge_list_to_stack (st : stack) (l : list A) : stack := match st with | [] => [Some l] | None :: st => Some l :: st | Some l' :: st => None :: merge_list_to_stack st (list_merge l' l) end. Fixpoint merge_stack (st : stack) : list A := match st with | [] => [] | None :: st => merge_stack st | Some l :: st => list_merge l (merge_stack st) end. Fixpoint merge_sort_aux (st : stack) (l : list A) : list A := match l with | [] => merge_stack st | x :: l => merge_sort_aux (merge_list_to_stack st [x]) l end. Definition merge_sort : list A → list A := merge_sort_aux []. End merge_sort. (** ** Properties of the [Sorted] and [StronglySorted] predicate *) Section sorted. Context {A} (R : relation A). Lemma Sorted_StronglySorted `{!Transitive R} l : Sorted R l → StronglySorted R l. Proof. by apply Sorted.Sorted_StronglySorted. Qed. `````` Robbert Krebbers committed Feb 11, 2016 153 `````` Lemma StronglySorted_unique `{!AntiSymm (=) R} l1 l2 : `````` Robbert Krebbers committed Aug 12, 2013 154 155 156 157 158 159 160 161 162 163 `````` StronglySorted R l1 → StronglySorted R l2 → l1 ≡ₚ l2 → l1 = l2. Proof. intros Hl1; revert l2. induction Hl1 as [|x1 l1 ? IH Hx1]; intros l2 Hl2 E. { symmetry. by apply Permutation_nil. } destruct Hl2 as [|x2 l2 ? Hx2]. { by apply Permutation_nil in E. } assert (x1 = x2); subst. { rewrite Forall_forall in Hx1, Hx2. assert (x2 ∈ x1 :: l1) as Hx2' by (by rewrite E; left). assert (x1 ∈ x2 :: l2) as Hx1' by (by rewrite <-E; left). `````` Robbert Krebbers committed Feb 17, 2016 164 `````` inversion Hx1'; inversion Hx2'; simplify_eq; auto. } `````` Robbert Krebbers committed Feb 11, 2016 165 `````` f_equal. by apply IH, (inj (x2 ::)). `````` Robbert Krebbers committed Aug 12, 2013 166 `````` Qed. `````` Robbert Krebbers committed Feb 11, 2016 167 `````` Lemma Sorted_unique `{!Transitive R, !AntiSymm (=) R} l1 l2 : `````` Robbert Krebbers committed Aug 12, 2013 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 `````` Sorted R l1 → Sorted R l2 → l1 ≡ₚ l2 → l1 = l2. Proof. auto using StronglySorted_unique, Sorted_StronglySorted. Qed. Global Instance HdRel_dec x `{∀ y, Decision (R x y)} l : Decision (HdRel R x l). Proof. refine match l with | [] => left _ | y :: l => cast_if (decide (R x y)) end; abstract first [by constructor | by inversion 1]. Defined. Global Instance Sorted_dec `{∀ x y, Decision (R x y)} : ∀ l, Decision (Sorted R l). Proof. refine (fix go l := match l return Decision (Sorted R l) with | [] => left _ | x :: l => cast_if_and (decide (HdRel R x l)) (go l) end); clear go; abstract first [by constructor | by inversion 1]. Defined. Global Instance StronglySorted_dec `{∀ x y, Decision (R x y)} : ∀ l, Decision (StronglySorted R l). Proof. refine (fix go l := match l return Decision (StronglySorted R l) with | [] => left _ | x :: l => cast_if_and (decide (Forall (R x) l)) (go l) end); clear go; abstract first [by constructor | by inversion 1]. Defined. Context {B} (f : A → B). Lemma HdRel_fmap (R1 : relation A) (R2 : relation B) x l : (∀ y, R1 x y → R2 (f x) (f y)) → HdRel R1 x l → HdRel R2 (f x) (f <\$> l). Proof. destruct 2; constructor; auto. Qed. Lemma Sorted_fmap (R1 : relation A) (R2 : relation B) l : (∀ x y, R1 x y → R2 (f x) (f y)) → Sorted R1 l → Sorted R2 (f <\$> l). Proof. induction 2; simpl; constructor; eauto using HdRel_fmap. Qed. Lemma StronglySorted_fmap (R1 : relation A) (R2 : relation B) l : (∀ x y, R1 x y → R2 (f x) (f y)) → StronglySorted R1 l → StronglySorted R2 (f <\$> l). Proof. `````` Robbert Krebbers committed Jun 16, 2014 212 `````` induction 2; csimpl; constructor; `````` Robbert Krebbers committed Aug 12, 2013 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 `````` rewrite ?Forall_fmap; eauto using Forall_impl. Qed. End sorted. (** ** Correctness of merge sort *) Section merge_sort_correct. Context {A} (R : relation A) `{∀ x y, Decision (R x y)} `{!Total R}. Lemma list_merge_cons x1 x2 l1 l2 : list_merge R (x1 :: l1) (x2 :: l2) = if decide (R x1 x2) then x1 :: list_merge R l1 (x2 :: l2) else x2 :: list_merge R (x1 :: l1) l2. Proof. done. Qed. Lemma HdRel_list_merge x l1 l2 : HdRel R x l1 → HdRel R x l2 → HdRel R x (list_merge R l1 l2). Proof. destruct 1 as [|x1 l1 IH1], 1 as [|x2 l2 IH2]; rewrite ?list_merge_cons; simpl; repeat case_decide; auto. Qed. Lemma Sorted_list_merge l1 l2 : Sorted R l1 → Sorted R l2 → Sorted R (list_merge R l1 l2). Proof. intros Hl1. revert l2. induction Hl1 as [|x1 l1 IH1]; induction 1 as [|x2 l2 IH2]; rewrite ?list_merge_cons; simpl; repeat case_decide; constructor; eauto using HdRel_list_merge, HdRel_cons, total_not. Qed. Lemma merge_Permutation l1 l2 : list_merge R l1 l2 ≡ₚ l1 ++ l2. Proof. revert l2. induction l1 as [|x1 l1 IH1]; intros l2; induction l2 as [|x2 l2 IH2]; rewrite ?list_merge_cons; simpl; repeat case_decide; auto. `````` Robbert Krebbers committed Feb 17, 2016 245 246 `````` - by rewrite (right_id_L [] (++)). - by rewrite IH2, Permutation_middle. `````` Robbert Krebbers committed Aug 12, 2013 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 `````` Qed. Local Notation stack := (list (option (list A))). Inductive merge_stack_Sorted : stack → Prop := | merge_stack_Sorted_nil : merge_stack_Sorted [] | merge_stack_Sorted_cons_None st : merge_stack_Sorted st → merge_stack_Sorted (None :: st) | merge_stack_Sorted_cons_Some l st : Sorted R l → merge_stack_Sorted st → merge_stack_Sorted (Some l :: st). Fixpoint merge_stack_flatten (st : stack) : list A := match st with | [] => [] | None :: st => merge_stack_flatten st | Some l :: st => l ++ merge_stack_flatten st end. Lemma Sorted_merge_list_to_stack st l : merge_stack_Sorted st → Sorted R l → merge_stack_Sorted (merge_list_to_stack R st l). Proof. intros Hst. revert l. induction Hst; repeat constructor; naive_solver auto using Sorted_list_merge. Qed. Lemma merge_list_to_stack_Permutation st l : merge_stack_flatten (merge_list_to_stack R st l) ≡ₚ l ++ merge_stack_flatten st. Proof. revert l. induction st as [|[l'|] st IH]; intros l; simpl; auto. `````` Robbert Krebbers committed Feb 11, 2016 275 `````` by rewrite IH, merge_Permutation, (assoc_L _), (comm (++) l). `````` Robbert Krebbers committed Aug 12, 2013 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 `````` Qed. Lemma Sorted_merge_stack st : merge_stack_Sorted st → Sorted R (merge_stack R st). Proof. induction 1; simpl; auto using Sorted_list_merge. Qed. Lemma merge_stack_Permutation st : merge_stack R st ≡ₚ merge_stack_flatten st. Proof. induction st as [|[] ? IH]; intros; simpl; auto. by rewrite merge_Permutation, IH. Qed. Lemma Sorted_merge_sort_aux st l : merge_stack_Sorted st → Sorted R (merge_sort_aux R st l). Proof. revert st. induction l; simpl; auto using Sorted_merge_stack, Sorted_merge_list_to_stack. Qed. Lemma merge_sort_aux_Permutation st l : merge_sort_aux R st l ≡ₚ merge_stack_flatten st ++ l. Proof. revert st. induction l as [|?? IH]; simpl; intros. `````` Robbert Krebbers committed Feb 17, 2016 295 296 `````` - by rewrite (right_id_L [] (++)), merge_stack_Permutation. - rewrite IH, merge_list_to_stack_Permutation; simpl. `````` Robbert Krebbers committed Aug 12, 2013 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 `````` by rewrite Permutation_middle. Qed. Lemma Sorted_merge_sort l : Sorted R (merge_sort R l). Proof. apply Sorted_merge_sort_aux. by constructor. Qed. Lemma merge_sort_Permutation l : merge_sort R l ≡ₚ l. Proof. unfold merge_sort. by rewrite merge_sort_aux_Permutation. Qed. Lemma StronglySorted_merge_sort `{!Transitive R} l : StronglySorted R (merge_sort R l). Proof. auto using Sorted_StronglySorted, Sorted_merge_sort. Qed. End merge_sort_correct. (** * Canonical pre and partial orders *) (** We extend the canonical pre-order [⊆] to a partial order by defining setoid equality as [λ X Y, X ⊆ Y ∧ Y ⊆ X]. We prove that this indeed gives rise to a setoid. *) `````` Robbert Krebbers committed Nov 18, 2015 312 ``````Instance preorder_equiv `{SubsetEq A} : Equiv A | 20 := λ X Y, X ⊆ Y ∧ Y ⊆ X. `````` Robbert Krebbers committed May 02, 2014 313 `````` `````` Robbert Krebbers committed Jun 11, 2012 314 ``````Section preorder. `````` Robbert Krebbers committed Nov 15, 2014 315 `````` Context `{SubsetEq A, !PreOrder (@subseteq A _)}. `````` Robbert Krebbers committed Jun 11, 2012 316 317 `````` Instance preorder_equivalence: @Equivalence A (≡). `````` Robbert Krebbers committed Aug 21, 2012 318 319 `````` Proof. split. `````` Robbert Krebbers committed Feb 17, 2016 320 321 `````` - done. - by intros ?? [??]. `````` Ralf Jung committed Feb 20, 2016 322 `````` - by intros X Y Z [??] [??]; split; trans Y. `````` Robbert Krebbers committed Aug 21, 2012 323 `````` Qed. `````` Robbert Krebbers committed Nov 18, 2015 324 `````` Global Instance: Proper ((≡) ==> (≡) ==> iff) ((⊆) : relation A). `````` Robbert Krebbers committed Jun 11, 2012 325 `````` Proof. `````` Robbert Krebbers committed May 02, 2014 326 `````` unfold equiv, preorder_equiv. intros X1 Y1 ? X2 Y2 ?. split; intro. `````` Ralf Jung committed Feb 20, 2016 327 328 `````` - trans X1. tauto. trans X2; tauto. - trans Y1. tauto. trans Y2; tauto. `````` Robbert Krebbers committed Jun 11, 2012 329 `````` Qed. `````` Robbert Krebbers committed Nov 18, 2015 330 `````` Lemma subset_spec (X Y : A) : X ⊂ Y ↔ X ⊆ Y ∧ X ≢ Y. `````` Robbert Krebbers committed Feb 19, 2013 331 332 `````` Proof. split. `````` Robbert Krebbers committed Feb 17, 2016 333 334 `````` - intros [? HYX]. split. done. contradict HYX. by rewrite <-HYX. - intros [? HXY]. split. done. by contradict HXY. `````` Robbert Krebbers committed Feb 19, 2013 335 `````` Qed. `````` Robbert Krebbers committed Jan 05, 2013 336 `````` `````` Robbert Krebbers committed Feb 19, 2013 337 338 339 340 341 `````` Section dec. Context `{∀ X Y : A, Decision (X ⊆ Y)}. Global Instance preorder_equiv_dec_slow (X Y : A) : Decision (X ≡ Y) | 100 := _. Lemma subseteq_inv X Y : X ⊆ Y → X ⊂ Y ∨ X ≡ Y. `````` Robbert Krebbers committed Aug 12, 2013 342 `````` Proof. rewrite subset_spec. destruct (decide (X ≡ Y)); tauto. Qed. `````` Robbert Krebbers committed Feb 19, 2013 343 `````` Lemma not_subset_inv X Y : X ⊄ Y → X ⊈ Y ∨ X ≡ Y. `````` Robbert Krebbers committed Aug 12, 2013 344 `````` Proof. rewrite subset_spec. destruct (decide (X ≡ Y)); tauto. Qed. `````` Robbert Krebbers committed Feb 19, 2013 345 `````` End dec. `````` Robbert Krebbers committed May 02, 2014 346 `````` `````` Robbert Krebbers committed Nov 15, 2014 347 348 349 350 351 352 353 354 355 356 357 `````` Section leibniz. Context `{!LeibnizEquiv A}. Lemma subset_spec_L X Y : X ⊂ Y ↔ X ⊆ Y ∧ X ≠ Y. Proof. unfold_leibniz. apply subset_spec. Qed. Context `{∀ X Y : A, Decision (X ⊆ Y)}. Lemma subseteq_inv_L X Y : X ⊆ Y → X ⊂ Y ∨ X = Y. Proof. unfold_leibniz. apply subseteq_inv. Qed. Lemma not_subset_inv_L X Y : X ⊄ Y → X ⊈ Y ∨ X = Y. Proof. unfold_leibniz. apply not_subset_inv. Qed. End leibniz. End preorder. `````` Robbert Krebbers committed May 02, 2014 358 `````` `````` Robbert Krebbers committed Oct 19, 2012 359 ``````Typeclasses Opaque preorder_equiv. `````` Robbert Krebbers committed Aug 21, 2012 360 361 ``````Hint Extern 0 (@Equivalence _ (≡)) => class_apply preorder_equivalence : typeclass_instances. `````` Robbert Krebbers committed Jun 11, 2012 362 `````` `````` Robbert Krebbers committed Feb 19, 2013 363 ``````(** * Partial orders *) `````` Robbert Krebbers committed Nov 15, 2014 364 365 ``````Section partial_order. Context `{SubsetEq A, !PartialOrder (@subseteq A _)}. `````` Robbert Krebbers committed Feb 19, 2013 366 `````` Global Instance: LeibnizEquiv A. `````` Robbert Krebbers committed Feb 11, 2016 367 `````` Proof. intros ?? [??]; by apply (anti_symm (⊆)). Qed. `````` Robbert Krebbers committed Nov 15, 2014 368 ``````End partial_order. `````` Robbert Krebbers committed Feb 19, 2013 369 `````` `````` Robbert Krebbers committed Aug 29, 2012 370 371 ``````(** * Join semi lattices *) (** General purpose theorems on join semi lattices. *) `````` Robbert Krebbers committed Nov 15, 2014 372 373 ``````Section join_semi_lattice. Context `{Empty A, JoinSemiLattice A, !EmptySpec A}. `````` Robbert Krebbers committed May 02, 2014 374 375 `````` Implicit Types X Y : A. Implicit Types Xs Ys : list A. `````` Robbert Krebbers committed Jun 11, 2012 376 `````` `````` Robbert Krebbers committed Feb 19, 2013 377 `````` Hint Resolve subseteq_empty union_subseteq_l union_subseteq_r union_least. `````` Robbert Krebbers committed May 02, 2014 378 `````` Lemma union_subseteq_l_transitive X1 X2 Y : X1 ⊆ X2 → X1 ⊆ X2 ∪ Y. `````` Ralf Jung committed Feb 20, 2016 379 `````` Proof. intros. trans X2; auto. Qed. `````` Robbert Krebbers committed May 02, 2014 380 `````` Lemma union_subseteq_r_transitive X1 X2 Y : X1 ⊆ X2 → X1 ⊆ Y ∪ X2. `````` Ralf Jung committed Feb 20, 2016 381 `````` Proof. intros. trans X2; auto. Qed. `````` Robbert Krebbers committed May 02, 2014 382 383 `````` Hint Resolve union_subseteq_l_transitive union_subseteq_r_transitive. Lemma union_preserving_l X Y1 Y2 : Y1 ⊆ Y2 → X ∪ Y1 ⊆ X ∪ Y2. `````` Robbert Krebbers committed Feb 19, 2013 384 `````` Proof. auto. Qed. `````` Robbert Krebbers committed May 02, 2014 385 `````` Lemma union_preserving_r X1 X2 Y : X1 ⊆ X2 → X1 ∪ Y ⊆ X2 ∪ Y. `````` Robbert Krebbers committed Feb 19, 2013 386 `````` Proof. auto. Qed. `````` Robbert Krebbers committed May 02, 2014 387 `````` Lemma union_preserving X1 X2 Y1 Y2 : X1 ⊆ X2 → Y1 ⊆ Y2 → X1 ∪ Y1 ⊆ X2 ∪ Y2. `````` Robbert Krebbers committed Jun 11, 2012 388 `````` Proof. auto. Qed. `````` Robbert Krebbers committed May 02, 2014 389 `````` Lemma union_empty X : X ∪ ∅ ⊆ X. `````` Robbert Krebbers committed Oct 19, 2012 390 `````` Proof. by apply union_least. Qed. `````` Robbert Krebbers committed Nov 18, 2015 391 `````` Global Instance union_proper : Proper ((≡) ==> (≡) ==> (≡)) (@union A _). `````` Robbert Krebbers committed Aug 21, 2012 392 `````` Proof. `````` Robbert Krebbers committed Feb 19, 2013 393 394 `````` unfold equiv, preorder_equiv. split; apply union_preserving; simpl in *; tauto. `````` Robbert Krebbers committed Aug 21, 2012 395 `````` Qed. `````` Robbert Krebbers committed Feb 11, 2016 396 `````` Global Instance: IdemP ((≡) : relation A) (∪). `````` Robbert Krebbers committed Jun 11, 2012 397 `````` Proof. split; eauto. Qed. `````` Robbert Krebbers committed Nov 18, 2015 398 `````` Global Instance: LeftId ((≡) : relation A) ∅ (∪). `````` Robbert Krebbers committed Jun 11, 2012 399 `````` Proof. split; eauto. Qed. `````` Robbert Krebbers committed Nov 18, 2015 400 `````` Global Instance: RightId ((≡) : relation A) ∅ (∪). `````` Robbert Krebbers committed Jun 11, 2012 401 `````` Proof. split; eauto. Qed. `````` Robbert Krebbers committed Feb 11, 2016 402 `````` Global Instance: Comm ((≡) : relation A) (∪). `````` Robbert Krebbers committed May 02, 2014 403 `````` Proof. split; auto. Qed. `````` Robbert Krebbers committed Feb 11, 2016 404 `````` Global Instance: Assoc ((≡) : relation A) (∪). `````` Robbert Krebbers committed May 02, 2014 405 `````` Proof. split; auto. Qed. `````` Robbert Krebbers committed Jun 11, 2012 406 `````` Lemma subseteq_union X Y : X ⊆ Y ↔ X ∪ Y ≡ Y. `````` Robbert Krebbers committed May 02, 2014 407 `````` Proof. repeat split; eauto. intros HXY. rewrite <-HXY. auto. Qed. `````` Robbert Krebbers committed Jun 11, 2012 408 409 410 411 412 413 `````` Lemma subseteq_union_1 X Y : X ⊆ Y → X ∪ Y ≡ Y. Proof. apply subseteq_union. Qed. Lemma subseteq_union_2 X Y : X ∪ Y ≡ Y → X ⊆ Y. Proof. apply subseteq_union. Qed. Lemma equiv_empty X : X ⊆ ∅ → X ≡ ∅. Proof. split; eauto. Qed. `````` Robbert Krebbers committed Nov 18, 2015 414 415 `````` Global Instance union_list_proper: Proper ((≡) ==> (≡)) (union_list (A:=A)). Proof. by induction 1; simpl; try apply union_proper. Qed. `````` Robbert Krebbers committed Feb 19, 2013 416 417 `````` Lemma union_list_nil : ⋃ @nil A = ∅. Proof. done. Qed. `````` Robbert Krebbers committed May 02, 2014 418 `````` Lemma union_list_cons X Xs : ⋃ (X :: Xs) = X ∪ ⋃ Xs. `````` Robbert Krebbers committed Feb 19, 2013 419 `````` Proof. done. Qed. `````` Robbert Krebbers committed May 02, 2014 420 `````` Lemma union_list_singleton X : ⋃ [X] ≡ X. `````` Robbert Krebbers committed Feb 19, 2013 421 `````` Proof. simpl. by rewrite (right_id ∅ _). Qed. `````` Robbert Krebbers committed May 02, 2014 422 `````` Lemma union_list_app Xs1 Xs2 : ⋃ (Xs1 ++ Xs2) ≡ ⋃ Xs1 ∪ ⋃ Xs2. `````` Robbert Krebbers committed Feb 19, 2013 423 `````` Proof. `````` Robbert Krebbers committed May 02, 2014 424 `````` induction Xs1 as [|X Xs1 IH]; simpl; [by rewrite (left_id ∅ _)|]. `````` Robbert Krebbers committed Feb 11, 2016 425 `````` by rewrite IH, (assoc _). `````` Robbert Krebbers committed Feb 19, 2013 426 `````` Qed. `````` Robbert Krebbers committed May 02, 2014 427 `````` Lemma union_list_reverse Xs : ⋃ (reverse Xs) ≡ ⋃ Xs. `````` Robbert Krebbers committed Feb 19, 2013 428 429 430 `````` Proof. induction Xs as [|X Xs IH]; simpl; [done |]. by rewrite reverse_cons, union_list_app, `````` Robbert Krebbers committed Feb 11, 2016 431 `````` union_list_singleton, (comm _), IH. `````` Robbert Krebbers committed Feb 19, 2013 432 `````` Qed. `````` Robbert Krebbers committed May 02, 2014 433 `````` Lemma union_list_preserving Xs Ys : Xs ⊆* Ys → ⋃ Xs ⊆ ⋃ Ys. `````` Robbert Krebbers committed Feb 19, 2013 434 `````` Proof. induction 1; simpl; auto using union_preserving. Qed. `````` Robbert Krebbers committed Oct 19, 2012 435 436 437 `````` Lemma empty_union X Y : X ∪ Y ≡ ∅ ↔ X ≡ ∅ ∧ Y ≡ ∅. Proof. split. `````` Robbert Krebbers committed Feb 17, 2016 438 `````` - intros HXY. split; apply equiv_empty; `````` Ralf Jung committed Feb 20, 2016 439 `````` by trans (X ∪ Y); [auto | rewrite HXY]. `````` Robbert Krebbers committed Feb 17, 2016 440 `````` - intros [HX HY]. by rewrite HX, HY, (left_id _ _). `````` Robbert Krebbers committed Oct 19, 2012 441 `````` Qed. `````` Robbert Krebbers committed Feb 22, 2013 442 `````` Lemma empty_union_list Xs : ⋃ Xs ≡ ∅ ↔ Forall (≡ ∅) Xs. `````` Robbert Krebbers committed Oct 19, 2012 443 444 `````` Proof. split. `````` Robbert Krebbers committed Feb 17, 2016 445 446 `````` - induction Xs; simpl; rewrite ?empty_union; intuition. - induction 1 as [|?? E1 ? E2]; simpl. done. by apply empty_union. `````` Robbert Krebbers committed Oct 19, 2012 447 448 `````` Qed. `````` Robbert Krebbers committed Feb 19, 2013 449 450 `````` Section leibniz. Context `{!LeibnizEquiv A}. `````` Robbert Krebbers committed Feb 11, 2016 451 452 `````` Global Instance: IdemP (=) (∪). Proof. intros ?. unfold_leibniz. apply (idemp _). Qed. `````` Robbert Krebbers committed Feb 19, 2013 453 454 455 456 `````` Global Instance: LeftId (=) ∅ (∪). Proof. intros ?. unfold_leibniz. apply (left_id _ _). Qed. Global Instance: RightId (=) ∅ (∪). Proof. intros ?. unfold_leibniz. apply (right_id _ _). Qed. `````` Robbert Krebbers committed Feb 11, 2016 457 458 459 460 `````` Global Instance: Comm (=) (∪). Proof. intros ??. unfold_leibniz. apply (comm _). Qed. Global Instance: Assoc (=) (∪). Proof. intros ???. unfold_leibniz. apply (assoc _). Qed. `````` Robbert Krebbers committed Feb 19, 2013 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 `````` Lemma subseteq_union_L X Y : X ⊆ Y ↔ X ∪ Y = Y. Proof. unfold_leibniz. apply subseteq_union. Qed. Lemma subseteq_union_1_L X Y : X ⊆ Y → X ∪ Y = Y. Proof. unfold_leibniz. apply subseteq_union_1. Qed. Lemma subseteq_union_2_L X Y : X ∪ Y = Y → X ⊆ Y. Proof. unfold_leibniz. apply subseteq_union_2. Qed. Lemma equiv_empty_L X : X ⊆ ∅ → X = ∅. Proof. unfold_leibniz. apply equiv_empty. Qed. Lemma union_list_singleton_L (X : A) : ⋃ [X] = X. Proof. unfold_leibniz. apply union_list_singleton. Qed. Lemma union_list_app_L (Xs1 Xs2 : list A) : ⋃ (Xs1 ++ Xs2) = ⋃ Xs1 ∪ ⋃ Xs2. Proof. unfold_leibniz. apply union_list_app. Qed. Lemma union_list_reverse_L (Xs : list A) : ⋃ (reverse Xs) = ⋃ Xs. Proof. unfold_leibniz. apply union_list_reverse. Qed. Lemma empty_union_L X Y : X ∪ Y = ∅ ↔ X = ∅ ∧ Y = ∅. Proof. unfold_leibniz. apply empty_union. Qed. `````` Robbert Krebbers committed Feb 22, 2013 477 `````` Lemma empty_union_list_L Xs : ⋃ Xs = ∅ ↔ Forall (= ∅) Xs. `````` Robbert Krebbers committed May 02, 2014 478 `````` Proof. unfold_leibniz. by rewrite empty_union_list. Qed. `````` Robbert Krebbers committed Feb 19, 2013 479 480 481 482 `````` End leibniz. Section dec. Context `{∀ X Y : A, Decision (X ⊆ Y)}. `````` Robbert Krebbers committed Jun 05, 2014 483 `````` Lemma non_empty_union X Y : X ∪ Y ≢ ∅ ↔ X ≢ ∅ ∨ Y ≢ ∅. `````` Robbert Krebbers committed Feb 19, 2013 484 `````` Proof. rewrite empty_union. destruct (decide (X ≡ ∅)); intuition. Qed. `````` Robbert Krebbers committed Feb 22, 2013 485 486 `````` Lemma non_empty_union_list Xs : ⋃ Xs ≢ ∅ → Exists (≢ ∅) Xs. Proof. rewrite empty_union_list. apply (not_Forall_Exists _). Qed. `````` Robbert Krebbers committed Feb 19, 2013 487 `````` Context `{!LeibnizEquiv A}. `````` Robbert Krebbers committed Jun 05, 2014 488 `````` Lemma non_empty_union_L X Y : X ∪ Y ≠ ∅ ↔ X ≠ ∅ ∨ Y ≠ ∅. `````` Robbert Krebbers committed Feb 19, 2013 489 `````` Proof. unfold_leibniz. apply non_empty_union. Qed. `````` Robbert Krebbers committed Feb 22, 2013 490 491 `````` Lemma non_empty_union_list_L Xs : ⋃ Xs ≠ ∅ → Exists (≠ ∅) Xs. Proof. unfold_leibniz. apply non_empty_union_list. Qed. `````` Robbert Krebbers committed Feb 19, 2013 492 `````` End dec. `````` Robbert Krebbers committed Nov 15, 2014 493 ``````End join_semi_lattice. `````` Robbert Krebbers committed Jun 11, 2012 494 `````` `````` Robbert Krebbers committed Aug 29, 2012 495 496 ``````(** * Meet semi lattices *) (** The dual of the above section, but now for meet semi lattices. *) `````` Robbert Krebbers committed Nov 15, 2014 497 ``````Section meet_semi_lattice. `````` Robbert Krebbers committed Jun 11, 2012 498 `````` Context `{MeetSemiLattice A}. `````` Robbert Krebbers committed May 02, 2014 499 500 `````` Implicit Types X Y : A. Implicit Types Xs Ys : list A. `````` Robbert Krebbers committed Jun 11, 2012 501 `````` `````` Robbert Krebbers committed Feb 19, 2013 502 `````` Hint Resolve intersection_subseteq_l intersection_subseteq_r `````` Robbert Krebbers committed Aug 21, 2012 503 `````` intersection_greatest. `````` Robbert Krebbers committed May 02, 2014 504 `````` Lemma intersection_subseteq_l_transitive X1 X2 Y : X1 ⊆ X2 → X1 ∩ Y ⊆ X2. `````` Ralf Jung committed Feb 20, 2016 505 `````` Proof. intros. trans X1; auto. Qed. `````` Robbert Krebbers committed May 02, 2014 506 `````` Lemma intersection_subseteq_r_transitive X1 X2 Y : X1 ⊆ X2 → Y ∩ X1 ⊆ X2. `````` Ralf Jung committed Feb 20, 2016 507 `````` Proof. intros. trans X1; auto. Qed. `````` Robbert Krebbers committed May 02, 2014 508 509 510 `````` Hint Resolve intersection_subseteq_l_transitive intersection_subseteq_r_transitive. Lemma intersection_preserving_l X Y1 Y2 : Y1 ⊆ Y2 → X ∩ Y1 ⊆ X ∩ Y2. `````` Robbert Krebbers committed Jun 11, 2012 511 `````` Proof. auto. Qed. `````` Robbert Krebbers committed May 02, 2014 512 `````` Lemma intersection_preserving_r X1 X2 Y : X1 ⊆ X2 → X1 ∩ Y ⊆ X2 ∩ Y. `````` Robbert Krebbers committed Jun 11, 2012 513 `````` Proof. auto. Qed. `````` Robbert Krebbers committed May 02, 2014 514 515 `````` Lemma intersection_preserving X1 X2 Y1 Y2 : X1 ⊆ X2 → Y1 ⊆ Y2 → X1 ∩ Y1 ⊆ X2 ∩ Y2. `````` Robbert Krebbers committed Jun 11, 2012 516 `````` Proof. auto. Qed. `````` Robbert Krebbers committed Nov 18, 2015 517 `````` Global Instance: Proper ((≡) ==> (≡) ==> (≡)) (@intersection A _). `````` Robbert Krebbers committed Aug 21, 2012 518 519 `````` Proof. unfold equiv, preorder_equiv. split; `````` Robbert Krebbers committed Feb 19, 2013 520 `````` apply intersection_preserving; simpl in *; tauto. `````` Robbert Krebbers committed Aug 21, 2012 521 `````` Qed. `````` Robbert Krebbers committed Feb 11, 2016 522 `````` Global Instance: IdemP ((≡) : relation A) (∩). `````` Robbert Krebbers committed Jun 11, 2012 523 `````` Proof. split; eauto. Qed. `````` Robbert Krebbers committed Feb 11, 2016 524 `````` Global Instance: Comm ((≡) : relation A) (∩). `````` Robbert Krebbers committed May 02, 2014 525 `````` Proof. split; auto. Qed. `````` Robbert Krebbers committed Feb 11, 2016 526 `````` Global Instance: Assoc ((≡) : relation A) (∩). `````` Robbert Krebbers committed May 02, 2014 527 `````` Proof. split; auto. Qed. `````` Robbert Krebbers committed Jun 11, 2012 528 `````` Lemma subseteq_intersection X Y : X ⊆ Y ↔ X ∩ Y ≡ X. `````` Robbert Krebbers committed May 02, 2014 529 `````` Proof. repeat split; eauto. intros HXY. rewrite <-HXY. auto. Qed. `````` Robbert Krebbers committed Jun 11, 2012 530 531 532 533 `````` Lemma subseteq_intersection_1 X Y : X ⊆ Y → X ∩ Y ≡ X. Proof. apply subseteq_intersection. Qed. Lemma subseteq_intersection_2 X Y : X ∩ Y ≡ X → X ⊆ Y. Proof. apply subseteq_intersection. Qed. `````` Robbert Krebbers committed Feb 19, 2013 534 535 536 `````` Section leibniz. Context `{!LeibnizEquiv A}. `````` Robbert Krebbers committed Feb 11, 2016 537 538 539 540 541 542 `````` Global Instance: IdemP (=) (∩). Proof. intros ?. unfold_leibniz. apply (idemp _). Qed. Global Instance: Comm (=) (∩). Proof. intros ??. unfold_leibniz. apply (comm _). Qed. Global Instance: Assoc (=) (∩). Proof. intros ???. unfold_leibniz. apply (assoc _). Qed. `````` Robbert Krebbers committed Feb 19, 2013 543 544 545 546 547 548 549 `````` Lemma subseteq_intersection_L X Y : X ⊆ Y ↔ X ∩ Y = X. Proof. unfold_leibniz. apply subseteq_intersection. Qed. Lemma subseteq_intersection_1_L X Y : X ⊆ Y → X ∩ Y = X. Proof. unfold_leibniz. apply subseteq_intersection_1. Qed. Lemma subseteq_intersection_2_L X Y : X ∩ Y = X → X ⊆ Y. Proof. unfold_leibniz. apply subseteq_intersection_2. Qed. End leibniz. `````` Robbert Krebbers committed Nov 15, 2014 550 ``````End meet_semi_lattice. `````` Robbert Krebbers committed Oct 19, 2012 551 552 `````` (** * Lower bounded lattices *) `````` Robbert Krebbers committed Nov 15, 2014 553 554 ``````Section lattice. Context `{Empty A, Lattice A, !EmptySpec A}. `````` Robbert Krebbers committed Oct 19, 2012 555 `````` `````` Robbert Krebbers committed Nov 18, 2015 556 `````` Global Instance: LeftAbsorb ((≡) : relation A) ∅ (∩). `````` Robbert Krebbers committed Nov 15, 2014 557 `````` Proof. split. by apply intersection_subseteq_l. by apply subseteq_empty. Qed. `````` Robbert Krebbers committed Nov 18, 2015 558 `````` Global Instance: RightAbsorb ((≡) : relation A) ∅ (∩). `````` Robbert Krebbers committed Feb 11, 2016 559 `````` Proof. intros ?. by rewrite (comm _), (left_absorb _ _). Qed. `````` Robbert Krebbers committed Jan 16, 2016 560 `````` Lemma union_intersection_l (X Y Z : A) : X ∪ (Y ∩ Z) ≡ (X ∪ Y) ∩ (X ∪ Z). `````` Robbert Krebbers committed Feb 19, 2013 561 `````` Proof. `````` Robbert Krebbers committed Jan 16, 2016 562 `````` split; [apply union_least|apply lattice_distr]. `````` Robbert Krebbers committed Nov 15, 2014 563 564 `````` { apply intersection_greatest; auto using union_subseteq_l. } apply intersection_greatest. `````` Robbert Krebbers committed Feb 17, 2016 565 566 `````` - apply union_subseteq_r_transitive, intersection_subseteq_l. - apply union_subseteq_r_transitive, intersection_subseteq_r. `````` Robbert Krebbers committed Feb 19, 2013 567 `````` Qed. `````` Robbert Krebbers committed Jan 16, 2016 568 `````` Lemma union_intersection_r (X Y Z : A) : (X ∩ Y) ∪ Z ≡ (X ∪ Z) ∩ (Y ∪ Z). `````` Robbert Krebbers committed Feb 11, 2016 569 `````` Proof. by rewrite !(comm _ _ Z), union_intersection_l. Qed. `````` Robbert Krebbers committed Jan 16, 2016 570 `````` Lemma intersection_union_l (X Y Z : A) : X ∩ (Y ∪ Z) ≡ (X ∩ Y) ∪ (X ∩ Z). `````` Robbert Krebbers committed Feb 19, 2013 571 `````` Proof. `````` Robbert Krebbers committed Jan 16, 2016 572 `````` split. `````` Robbert Krebbers committed Feb 17, 2016 573 `````` - rewrite union_intersection_l. `````` Robbert Krebbers committed Feb 19, 2013 574 `````` apply intersection_greatest. `````` Robbert Krebbers committed May 02, 2014 575 `````` { apply union_subseteq_r_transitive, intersection_subseteq_l. } `````` Robbert Krebbers committed Jan 16, 2016 576 `````` rewrite union_intersection_r. `````` Robbert Krebbers committed Nov 15, 2014 577 `````` apply intersection_preserving; auto using union_subseteq_l. `````` Robbert Krebbers committed Feb 17, 2016 578 `````` - apply intersection_greatest. `````` Robbert Krebbers committed Feb 19, 2013 579 580 `````` { apply union_least; auto using intersection_subseteq_l. } apply union_least. `````` Robbert Krebbers committed May 02, 2014 581 582 `````` + apply intersection_subseteq_r_transitive, union_subseteq_l. + apply intersection_subseteq_r_transitive, union_subseteq_r. `````` Robbert Krebbers committed Feb 19, 2013 583 `````` Qed. `````` Robbert Krebbers committed Jan 16, 2016 584 `````` Lemma intersection_union_r (X Y Z : A) : (X ∪ Y) ∩ Z ≡ (X ∩ Z) ∪ (Y ∩ Z). `````` Robbert Krebbers committed Feb 11, 2016 585 `````` Proof. by rewrite !(comm _ _ Z), intersection_union_l. Qed. `````` Robbert Krebbers committed Feb 19, 2013 586 587 588 589 590 591 592 `````` Section leibniz. Context `{!LeibnizEquiv A}. Global Instance: LeftAbsorb (=) ∅ (∩). Proof. intros ?. unfold_leibniz. apply (left_absorb _ _). Qed. Global Instance: RightAbsorb (=) ∅ (∩). Proof. intros ?. unfold_leibniz. apply (right_absorb _ _). Qed. `````` Robbert Krebbers committed Jan 16, 2016 593 594 595 596 597 598 599 600 `````` Lemma union_intersection_l_L (X Y Z : A) : X ∪ (Y ∩ Z) = (X ∪ Y) ∩ (X ∪ Z). Proof. unfold_leibniz; apply union_intersection_l. Qed. Lemma union_intersection_r_L (X Y Z : A) : (X ∩ Y) ∪ Z = (X ∪ Z) ∩ (Y ∪ Z). Proof. unfold_leibniz; apply union_intersection_r. Qed. Lemma intersection_union_l_L (X Y Z : A) : X ∩ (Y ∪ Z) ≡ (X ∩ Y) ∪ (X ∩ Z). Proof. unfold_leibniz; apply intersection_union_l. Qed. Lemma intersection_union_r_L (X Y Z : A) : (X ∪ Y) ∩ Z ≡ (X ∩ Z) ∪ (Y ∩ Z). Proof. unfold_leibniz; apply intersection_union_r. Qed. `````` Robbert Krebbers committed Feb 19, 2013 601 `````` End leibniz. `````` Robbert Krebbers committed Nov 15, 2014 602 ``End lattice.``