option.v 8.39 KB
 Robbert Krebbers committed Feb 13, 2016 1 ``````From algebra Require Export cmra. `````` Robbert Krebbers committed Feb 13, 2016 2 ``````From algebra Require Import functor upred. `````` Robbert Krebbers committed Dec 15, 2015 3 4 `````` (* COFE *) `````` Robbert Krebbers committed Jan 14, 2016 5 6 7 ``````Section cofe. Context {A : cofeT}. Inductive option_dist : Dist (option A) := `````` Ralf Jung committed Feb 10, 2016 8 9 `````` | Some_dist n x y : x ≡{n}≡ y → Some x ≡{n}≡ Some y | None_dist n : None ≡{n}≡ None. `````` Robbert Krebbers committed Dec 15, 2015 10 ``````Existing Instance option_dist. `````` Robbert Krebbers committed Jan 14, 2016 11 ``````Program Definition option_chain `````` Robbert Krebbers committed Dec 15, 2015 12 13 14 `````` (c : chain (option A)) (x : A) (H : c 1 = Some x) : chain A := {| chain_car n := from_option x (c n) |}. Next Obligation. `````` Robbert Krebbers committed Feb 10, 2016 15 16 17 `````` intros c x ? n [|i] ?; [omega|]; simpl. destruct (c 1) eqn:?; simplify_equality'. by feed inversion (chain_cauchy c n (S i)). `````` Robbert Krebbers committed Dec 15, 2015 18 ``````Qed. `````` Robbert Krebbers committed Jan 14, 2016 19 ``````Instance option_compl : Compl (option A) := λ c, `````` Robbert Krebbers committed Dec 15, 2015 20 21 22 `````` match Some_dec (c 1) with | inleft (exist x H) => Some (compl (option_chain c x H)) | inright _ => None end. `````` Robbert Krebbers committed Jan 14, 2016 23 ``````Definition option_cofe_mixin : CofeMixin (option A). `````` Robbert Krebbers committed Dec 15, 2015 24 25 ``````Proof. split. `````` Robbert Krebbers committed Feb 17, 2016 26 `````` - intros mx my; split; [by destruct 1; constructor; apply equiv_dist|]. `````` Robbert Krebbers committed Dec 15, 2015 27 28 `````` intros Hxy; feed inversion (Hxy 1); subst; constructor; apply equiv_dist. by intros n; feed inversion (Hxy n). `````` Robbert Krebbers committed Feb 17, 2016 29 `````` - intros n; split. `````` Robbert Krebbers committed Dec 15, 2015 30 31 32 `````` + by intros [x|]; constructor. + by destruct 1; constructor. + destruct 1; inversion_clear 1; constructor; etransitivity; eauto. `````` Robbert Krebbers committed Feb 17, 2016 33 34 `````` - by inversion_clear 1; constructor; apply dist_S. - intros c n; unfold compl, option_compl. `````` Robbert Krebbers committed Dec 15, 2015 35 `````` destruct (Some_dec (c 1)) as [[x Hx]|]. `````` Robbert Krebbers committed Feb 10, 2016 36 37 `````` { assert (is_Some (c (S n))) as [y Hy]. { feed inversion (chain_cauchy c 0 (S n)); eauto with lia congruence. } `````` Robbert Krebbers committed Dec 15, 2015 38 `````` rewrite Hy; constructor. `````` Robbert Krebbers committed Feb 10, 2016 39 40 41 `````` by rewrite (conv_compl (option_chain c x Hx) n) /= Hy. } feed inversion (chain_cauchy c 0 (S n)); eauto with lia congruence. constructor. `````` Robbert Krebbers committed Dec 15, 2015 42 ``````Qed. `````` Robbert Krebbers committed Jan 14, 2016 43 44 ``````Canonical Structure optionC := CofeT option_cofe_mixin. Global Instance Some_ne : Proper (dist n ==> dist n) (@Some A). `````` Robbert Krebbers committed Dec 15, 2015 45 ``````Proof. by constructor. Qed. `````` Robbert Krebbers committed Feb 10, 2016 46 ``````Global Instance is_Some_ne n : Proper (dist n ==> iff) (@is_Some A). `````` Robbert Krebbers committed Jan 16, 2016 47 ``````Proof. inversion_clear 1; split; eauto. Qed. `````` Robbert Krebbers committed Feb 11, 2016 48 ``````Global Instance Some_dist_inj : Inj (dist n) (dist n) (@Some A). `````` Robbert Krebbers committed Jan 16, 2016 49 ``````Proof. by inversion_clear 1. Qed. `````` Robbert Krebbers committed Jan 14, 2016 50 ``````Global Instance None_timeless : Timeless (@None A). `````` Robbert Krebbers committed Dec 15, 2015 51 ``````Proof. inversion_clear 1; constructor. Qed. `````` Robbert Krebbers committed Jan 14, 2016 52 ``````Global Instance Some_timeless x : Timeless x → Timeless (Some x). `````` Robbert Krebbers committed Dec 15, 2015 53 ``````Proof. by intros ?; inversion_clear 1; constructor; apply timeless. Qed. `````` Ralf Jung committed Feb 13, 2016 54 55 ``````Global Instance option_timeless `{!∀ a : A, Timeless a} (mx : option A) : Timeless mx. Proof. destruct mx; auto with typeclass_instances. Qed. `````` Robbert Krebbers committed Jan 14, 2016 56 57 58 59 ``````End cofe. Arguments optionC : clear implicits. `````` Robbert Krebbers committed Dec 15, 2015 60 ``````(* CMRA *) `````` Robbert Krebbers committed Jan 14, 2016 61 62 63 64 ``````Section cmra. Context {A : cmraT}. Instance option_validN : ValidN (option A) := λ n mx, `````` Robbert Krebbers committed Dec 15, 2015 65 `````` match mx with Some x => ✓{n} x | None => True end. `````` Robbert Krebbers committed Feb 04, 2016 66 ``````Global Instance option_empty : Empty (option A) := None. `````` Robbert Krebbers committed Jan 14, 2016 67 68 69 ``````Instance option_unit : Unit (option A) := fmap unit. Instance option_op : Op (option A) := union_with (λ x y, Some (x ⋅ y)). Instance option_minus : Minus (option A) := `````` Robbert Krebbers committed Dec 15, 2015 70 `````` difference_with (λ x y, Some (x ⩪ y)). `````` Robbert Krebbers committed Jan 14, 2016 71 ``````Lemma option_includedN n (mx my : option A) : `````` Robbert Krebbers committed Feb 10, 2016 72 `````` mx ≼{n} my ↔ mx = None ∨ ∃ x y, mx = Some x ∧ my = Some y ∧ x ≼{n} y. `````` Robbert Krebbers committed Dec 15, 2015 73 74 ``````Proof. split. `````` Robbert Krebbers committed Feb 17, 2016 75 `````` - intros [mz Hmz]. `````` Robbert Krebbers committed Dec 15, 2015 76 77 78 `````` destruct mx as [x|]; [right|by left]. destruct my as [y|]; [exists x, y|destruct mz; inversion_clear Hmz]. destruct mz as [z|]; inversion_clear Hmz; split_ands; auto; `````` Robbert Krebbers committed Feb 01, 2016 79 `````` cofe_subst; eauto using cmra_includedN_l. `````` Robbert Krebbers committed Feb 17, 2016 80 `````` - intros [->|(x&y&->&->&z&Hz)]; try (by exists my; destruct my; constructor). `````` Robbert Krebbers committed Dec 15, 2015 81 82 `````` by exists (Some z); constructor. Qed. `````` Robbert Krebbers committed Jan 16, 2016 83 84 85 86 ``````Lemma None_includedN n (mx : option A) : None ≼{n} mx. Proof. rewrite option_includedN; auto. Qed. Lemma Some_Some_includedN n (x y : A) : x ≼{n} y → Some x ≼{n} Some y. Proof. rewrite option_includedN; eauto 10. Qed. `````` 87 ``````Definition Some_op a b : Some (a ⋅ b) = Some a ⋅ Some b := eq_refl. `````` Robbert Krebbers committed Jan 16, 2016 88 `````` `````` Robbert Krebbers committed Jan 14, 2016 89 ``````Definition option_cmra_mixin : CMRAMixin (option A). `````` Robbert Krebbers committed Dec 15, 2015 90 91 ``````Proof. split. `````` Robbert Krebbers committed Feb 17, 2016 92 93 94 95 96 97 98 99 100 101 `````` - by intros n [x|]; destruct 1; constructor; cofe_subst. - by destruct 1; constructor; cofe_subst. - by destruct 1; rewrite /validN /option_validN //=; cofe_subst. - by destruct 1; inversion_clear 1; constructor; cofe_subst. - intros n [x|]; unfold validN, option_validN; eauto using cmra_validN_S. - intros [x|] [y|] [z|]; constructor; rewrite ?assoc; auto. - intros [x|] [y|]; constructor; rewrite 1?comm; auto. - by intros [x|]; constructor; rewrite cmra_unit_l. - by intros [x|]; constructor; rewrite cmra_unit_idemp. - intros n mx my; rewrite !option_includedN;intros [->|(x&y&->&->&?)]; auto. `````` Robbert Krebbers committed Feb 10, 2016 102 `````` right; exists (unit x), (unit y); eauto using cmra_unit_preservingN. `````` Robbert Krebbers committed Feb 17, 2016 103 `````` - intros n [x|] [y|]; rewrite /validN /option_validN /=; `````` Robbert Krebbers committed Feb 01, 2016 104 `````` eauto using cmra_validN_op_l. `````` Robbert Krebbers committed Feb 17, 2016 105 `````` - intros n mx my; rewrite option_includedN. `````` Robbert Krebbers committed Feb 10, 2016 106 `````` intros [->|(x&y&->&->&?)]; [by destruct my|]. `````` Robbert Krebbers committed Dec 15, 2015 107 108 `````` by constructor; apply cmra_op_minus. Qed. `````` Robbert Krebbers committed Jan 14, 2016 109 ``````Definition option_cmra_extend_mixin : CMRAExtendMixin (option A). `````` Robbert Krebbers committed Dec 15, 2015 110 ``````Proof. `````` Robbert Krebbers committed Feb 10, 2016 111 `````` intros n mx my1 my2. `````` Robbert Krebbers committed Dec 15, 2015 112 113 `````` destruct mx as [x|], my1 as [y1|], my2 as [y2|]; intros Hx Hx'; try (by exfalso; inversion Hx'; auto). `````` Robbert Krebbers committed Feb 17, 2016 114 `````` - destruct (cmra_extend_op n x y1 y2) as ([z1 z2]&?&?&?); auto. `````` Robbert Krebbers committed Dec 15, 2015 115 116 `````` { by inversion_clear Hx'. } by exists (Some z1, Some z2); repeat constructor. `````` Robbert Krebbers committed Feb 17, 2016 117 118 119 `````` - by exists (Some x,None); inversion Hx'; repeat constructor. - by exists (None,Some x); inversion Hx'; repeat constructor. - exists (None,None); repeat constructor. `````` Robbert Krebbers committed Dec 15, 2015 120 ``````Qed. `````` Robbert Krebbers committed Jan 14, 2016 121 122 ``````Canonical Structure optionRA := CMRAT option_cofe_mixin option_cmra_mixin option_cmra_extend_mixin. `````` Robbert Krebbers committed Feb 04, 2016 123 124 ``````Global Instance option_cmra_identity : CMRAIdentity optionRA. Proof. split. done. by intros []. by inversion_clear 1. Qed. `````` Robbert Krebbers committed Jan 14, 2016 125 `````` `````` Robbert Krebbers committed Feb 13, 2016 126 ``````(** Misc *) `````` Robbert Krebbers committed Feb 02, 2016 127 128 129 130 ``````Lemma op_is_Some mx my : is_Some (mx ⋅ my) ↔ is_Some mx ∨ is_Some my. Proof. destruct mx, my; rewrite /op /option_op /= -!not_eq_None_Some; naive_solver. Qed. `````` Ralf Jung committed Feb 10, 2016 131 ``````Lemma option_op_positive_dist_l n mx my : mx ⋅ my ≡{n}≡ None → mx ≡{n}≡ None. `````` Robbert Krebbers committed Feb 02, 2016 132 ``````Proof. by destruct mx, my; inversion_clear 1. Qed. `````` Ralf Jung committed Feb 10, 2016 133 ``````Lemma option_op_positive_dist_r n mx my : mx ⋅ my ≡{n}≡ None → my ≡{n}≡ None. `````` Robbert Krebbers committed Feb 02, 2016 134 135 ``````Proof. by destruct mx, my; inversion_clear 1. Qed. `````` Robbert Krebbers committed Feb 13, 2016 136 137 138 139 140 141 142 143 144 145 146 ``````(** Internalized properties *) Lemma option_equivI {M} (x y : option A) : (x ≡ y)%I ≡ (match x, y with | Some a, Some b => a ≡ b | None, None => True | _, _ => False end : uPred M)%I. Proof. split. by destruct 1. by destruct x, y; try constructor. Qed. Lemma option_validI {M} (x : option A) : (✓ x)%I ≡ (match x with Some a => ✓ a | None => True end : uPred M)%I. Proof. by destruct x. Qed. (** Updates *) `````` Robbert Krebbers committed Feb 02, 2016 147 ``````Lemma option_updateP (P : A → Prop) (Q : option A → Prop) x : `````` Ralf Jung committed Feb 03, 2016 148 `````` x ~~>: P → (∀ y, P y → Q (Some y)) → Some x ~~>: Q. `````` Robbert Krebbers committed Feb 02, 2016 149 150 151 152 153 154 ``````Proof. intros Hx Hy [y|] n ?. { destruct (Hx y n) as (y'&?&?); auto. exists (Some y'); auto. } destruct (Hx (unit x) n) as (y'&?&?); rewrite ?cmra_unit_r; auto. by exists (Some y'); split; [auto|apply cmra_validN_op_l with (unit x)]. Qed. `````` Robbert Krebbers committed Feb 02, 2016 155 ``````Lemma option_updateP' (P : A → Prop) x : `````` Ralf Jung committed Feb 03, 2016 156 `````` x ~~>: P → Some x ~~>: λ y, default False y P. `````` Robbert Krebbers committed Feb 02, 2016 157 ``````Proof. eauto using option_updateP. Qed. `````` Ralf Jung committed Feb 03, 2016 158 ``````Lemma option_update x y : x ~~> y → Some x ~~> Some y. `````` Robbert Krebbers committed Jan 16, 2016 159 ``````Proof. `````` Robbert Krebbers committed Feb 02, 2016 160 `````` rewrite !cmra_update_updateP; eauto using option_updateP with congruence. `````` Robbert Krebbers committed Jan 16, 2016 161 ``````Qed. `````` Robbert Krebbers committed Feb 08, 2016 162 163 164 165 166 ``````Lemma option_update_None `{Empty A, !CMRAIdentity A} : ∅ ~~> Some ∅. Proof. intros [x|] n ?; rewrite /op /cmra_op /validN /cmra_validN /= ?left_id; auto using cmra_empty_valid. Qed. `````` Robbert Krebbers committed Jan 14, 2016 167 168 169 ``````End cmra. Arguments optionRA : clear implicits. `````` Robbert Krebbers committed Feb 04, 2016 170 171 172 173 ``````(** Functor *) Instance option_fmap_ne {A B : cofeT} (f : A → B) n: Proper (dist n ==> dist n) f → Proper (dist n==>dist n) (fmap (M:=option) f). Proof. by intros Hf; destruct 1; constructor; apply Hf. Qed. `````` Robbert Krebbers committed Jan 14, 2016 174 175 ``````Instance option_fmap_cmra_monotone {A B : cmraT} (f: A → B) `{!CMRAMonotone f} : CMRAMonotone (fmap f : option A → option B). `````` Robbert Krebbers committed Dec 15, 2015 176 177 ``````Proof. split. `````` Robbert Krebbers committed Feb 17, 2016 178 `````` - intros n mx my; rewrite !option_includedN. `````` Robbert Krebbers committed Feb 10, 2016 179 `````` intros [->|(x&y&->&->&?)]; simpl; eauto 10 using @includedN_preserving. `````` Robbert Krebbers committed Feb 17, 2016 180 `````` - by intros n [x|] ?; rewrite /cmra_validN /=; try apply validN_preserving. `````` Robbert Krebbers committed Jan 14, 2016 181 ``````Qed. `````` Robbert Krebbers committed Feb 04, 2016 182 183 184 185 ``````Definition optionC_map {A B} (f : A -n> B) : optionC A -n> optionC B := CofeMor (fmap f : optionC A → optionC B). Instance optionC_map_ne A B n : Proper (dist n ==> dist n) (@optionC_map A B). Proof. by intros f f' Hf []; constructor; apply Hf. Qed. `````` Ralf Jung committed Feb 05, 2016 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 `````` Program Definition optionF (Σ : iFunctor) : iFunctor := {| ifunctor_car := optionRA ∘ Σ; ifunctor_map A B := optionC_map ∘ ifunctor_map Σ |}. Next Obligation. by intros Σ A B n f g Hfg; apply optionC_map_ne, ifunctor_map_ne. Qed. Next Obligation. intros Σ A x. rewrite /= -{2}(option_fmap_id x). apply option_fmap_setoid_ext=>y; apply ifunctor_map_id. Qed. Next Obligation. intros Σ A B C f g x. rewrite /= -option_fmap_compose. apply option_fmap_setoid_ext=>y; apply ifunctor_map_compose. Qed.``````