option.v 6.54 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 the option data type that are not in the Coq standard library. *) `````` Robbert Krebbers committed Oct 19, 2012 5 ``````Require Export base tactics decidable. `````` Robbert Krebbers committed Aug 29, 2012 6 7 8 `````` (** * General definitions and theorems *) (** Basic properties about equality. *) `````` Robbert Krebbers committed Jun 11, 2012 9 10 11 12 13 14 ``````Lemma None_ne_Some `(a : A) : None ≠ Some a. Proof. congruence. Qed. Lemma Some_ne_None `(a : A) : Some a ≠ None. Proof. congruence. Qed. Lemma eq_None_ne_Some `(x : option A) a : x = None → x ≠ Some a. Proof. congruence. Qed. `````` Robbert Krebbers committed Aug 21, 2012 15 ``````Instance Some_inj {A} : Injective (=) (=) (@Some A). `````` Robbert Krebbers committed Jun 11, 2012 16 17 ``````Proof. congruence. Qed. `````` Robbert Krebbers committed Aug 29, 2012 18 19 ``````(** The non dependent elimination principle on the option type. *) Definition option_case {A B} (f : A → B) (b : B) (x : option A) : B := `````` Robbert Krebbers committed Jun 11, 2012 20 21 22 23 24 `````` match x with | None => b | Some a => f a end. `````` Robbert Krebbers committed Aug 29, 2012 25 26 27 ``````(** The [maybe] function allows us to get the value out of the option type by specifying a default value. *) Definition maybe {A} (a : A) (x : option A) : A := `````` Robbert Krebbers committed Aug 21, 2012 28 29 `````` match x with | None => a `````` Robbert Krebbers committed Aug 29, 2012 30 `````` | Some b => b `````` Robbert Krebbers committed Aug 21, 2012 31 32 `````` end. `````` Robbert Krebbers committed Aug 29, 2012 33 34 ``````(** An alternative, but equivalent, definition of equality on the option data type. This theorem is useful to prove that two options are the same. *) `````` Robbert Krebbers committed Jun 11, 2012 35 36 37 38 ``````Lemma option_eq {A} (x y : option A) : x = y ↔ ∀ a, x = Some a ↔ y = Some a. Proof. split. `````` Robbert Krebbers committed Oct 19, 2012 39 `````` { intros. by subst. } `````` Robbert Krebbers committed Aug 29, 2012 40 `````` intros E. destruct x, y. `````` Robbert Krebbers committed Oct 19, 2012 41 42 43 44 `````` + by apply E. + symmetry. by apply E. + by apply E. + done. `````` Robbert Krebbers committed Jun 11, 2012 45 46 ``````Qed. `````` Robbert Krebbers committed Aug 29, 2012 47 48 49 ``````(** We define [is_Some] as a [sig] instead of a [sigT] as extraction of witnesses can be derived (see [is_Some_sigT] below). *) Definition is_Some `(x : option A) : Prop := ∃ a, x = Some a. `````` Robbert Krebbers committed Jun 11, 2012 50 51 ``````Hint Extern 10 (is_Some _) => solve [eexists; eauto]. `````` Robbert Krebbers committed Aug 21, 2012 52 ``````Ltac simplify_is_Some := repeat intro; repeat `````` Robbert Krebbers committed Jun 11, 2012 53 `````` match goal with `````` Robbert Krebbers committed Aug 29, 2012 54 `````` | _ => progress simplify_equality `````` Robbert Krebbers committed Jun 11, 2012 55 56 `````` | H : is_Some _ |- _ => destruct H as [??] | |- is_Some _ => eauto `````` Robbert Krebbers committed Aug 21, 2012 57 `````` end. `````` Robbert Krebbers committed Jun 11, 2012 58 59 60 61 62 63 `````` Lemma Some_is_Some `(a : A) : is_Some (Some a). Proof. simplify_is_Some. Qed. Lemma None_not_is_Some {A} : ¬is_Some (@None A). Proof. simplify_is_Some. Qed. `````` Robbert Krebbers committed Aug 29, 2012 64 ``````Definition is_Some_sigT `(x : option A) : is_Some x → { a | x = Some a } := `````` Robbert Krebbers committed Jun 11, 2012 65 66 67 68 `````` match x with | None => False_rect _ ∘ ex_ind None_ne_Some | Some a => λ _, a↾eq_refl end. `````` Robbert Krebbers committed Aug 29, 2012 69 ``````Lemma eq_Some_is_Some `(x : option A) a : x = Some a → is_Some x. `````` Robbert Krebbers committed Jun 11, 2012 70 71 72 73 74 ``````Proof. simplify_is_Some. Qed. Lemma eq_None_not_Some `(x : option A) : x = None ↔ ¬is_Some x. Proof. destruct x; simpl; firstorder congruence. Qed. `````` Robbert Krebbers committed Aug 29, 2012 75 ``````Lemma make_eq_Some {A} (x : option A) a : `````` Robbert Krebbers committed Jun 11, 2012 76 77 78 `````` is_Some x → (∀ b, x = Some b → b = a) → x = Some a. Proof. intros [??] H. subst. f_equal. auto. Qed. `````` Robbert Krebbers committed Aug 29, 2012 79 80 81 ``````(** Equality on [option] is decidable. *) Instance option_eq_dec `{dec : ∀ x y : A, Decision (x = y)} (x y : option A) : Decision (x = y) := `````` Robbert Krebbers committed Jun 11, 2012 82 83 84 85 86 87 `````` match x with | Some a => match y with | Some b => match dec a b with | left H => left (f_equal _ H) `````` Robbert Krebbers committed Aug 21, 2012 88 `````` | right H => right (H ∘ injective Some _ _) `````` Robbert Krebbers committed Jun 11, 2012 89 90 91 92 93 94 95 96 97 98 `````` end | None => right (Some_ne_None _) end | None => match y with | Some _ => right (None_ne_Some _) | None => left eq_refl end end. `````` Robbert Krebbers committed Aug 29, 2012 99 ``````(** * Monadic operations *) `````` Robbert Krebbers committed Oct 19, 2012 100 ``````Instance option_bind {A B} (f : A → option B) : MBind option f := λ x, `````` Robbert Krebbers committed Jun 11, 2012 101 102 103 104 `````` match x with | Some a => f a | None => None end. `````` Robbert Krebbers committed Oct 19, 2012 105 ``````Instance option_join {A} : MJoin option := λ x : option (option A), `````` Robbert Krebbers committed Jun 11, 2012 106 107 108 109 `````` match x with | Some x => x | None => None end. `````` Robbert Krebbers committed Oct 19, 2012 110 ``````Instance option_fmap {A B} (f : A → B) : FMap option f := option_map f. `````` Robbert Krebbers committed Jun 11, 2012 111 `````` `````` Robbert Krebbers committed Aug 21, 2012 112 113 ``````Lemma option_fmap_is_Some {A B} (f : A → B) (x : option A) : is_Some x ↔ is_Some (f <\$> x). `````` Robbert Krebbers committed Oct 19, 2012 114 ``````Proof. destruct x; split; intros [??]; subst; compute; by eauto. Qed. `````` Robbert Krebbers committed Aug 21, 2012 115 116 ``````Lemma option_fmap_is_None {A B} (f : A → B) (x : option A) : x = None ↔ f <\$> x = None. `````` Robbert Krebbers committed Jun 11, 2012 117 118 ``````Proof. unfold fmap, option_fmap. destruct x; simpl; split; congruence. Qed. `````` Robbert Krebbers committed Oct 19, 2012 119 ``````Tactic Notation "simplify_option_bind" "by" tactic3(tac) := repeat `````` Robbert Krebbers committed Aug 29, 2012 120 `````` match goal with `````` Robbert Krebbers committed Oct 19, 2012 121 122 123 124 125 126 127 `````` | _ => first [progress simpl in * | progress simplify_equality] | H : mbind (M:=option) (A:=?A) ?f ?o = ?x |- _ => let x := fresh in evar (x:A); let x' := eval unfold x in x in clear x; let Hx := fresh in assert (o = Some x') as Hx by tac; rewrite Hx in H; clear Hx `````` Robbert Krebbers committed Aug 29, 2012 128 `````` | H : mbind (M:=option) ?f ?o = ?x |- _ => `````` Robbert Krebbers committed Oct 19, 2012 129 130 `````` match o with Some _ => fail 1 | None => fail 1 | _ => idtac end; match x with Some _ => idtac | None => idtac | _ => fail 1 end; `````` Robbert Krebbers committed Aug 29, 2012 131 `````` destruct o eqn:? `````` Robbert Krebbers committed Oct 19, 2012 132 133 134 135 136 137 `````` | |- mbind (M:=option) (A:=?A) ?f ?o = ?x => let x := fresh in evar (x:A); let x' := eval unfold x in x in clear x; let Hx := fresh in assert (o = Some x') as Hx by tac; rewrite Hx; clear Hx `````` Robbert Krebbers committed Aug 29, 2012 138 `````` end. `````` Robbert Krebbers committed Oct 19, 2012 139 ``````Tactic Notation "simplify_option_bind" := simplify_option_bind by eauto. `````` Robbert Krebbers committed Aug 29, 2012 140 141 `````` (** * Union, intersection and difference *) `````` Robbert Krebbers committed Jun 11, 2012 142 143 144 145 146 147 148 ``````Instance option_union: UnionWith option := λ A f x y, match x, y with | Some a, Some b => Some (f a b) | Some a, None => Some a | None, Some b => Some b | None, None => None end. `````` Robbert Krebbers committed Aug 21, 2012 149 ``````Instance option_intersection: IntersectionWith option := λ A f x y, `````` Robbert Krebbers committed Jun 11, 2012 150 151 152 153 `````` match x, y with | Some a, Some b => Some (f a b) | _, _ => None end. `````` Robbert Krebbers committed Aug 21, 2012 154 155 156 157 158 159 ``````Instance option_difference: DifferenceWith option := λ A f x y, match x, y with | Some a, Some b => f a b | Some a, None => Some a | None, _ => None end. `````` Robbert Krebbers committed Jun 11, 2012 160 `````` `````` Robbert Krebbers committed Aug 21, 2012 161 ``````Section option_union_intersection. `````` Robbert Krebbers committed Jun 11, 2012 162 163 164 `````` Context {A} (f : A → A → A). Global Instance: LeftId (=) None (union_with f). `````` Robbert Krebbers committed Oct 19, 2012 165 `````` Proof. by intros [?|]. Qed. `````` Robbert Krebbers committed Jun 11, 2012 166 `````` Global Instance: RightId (=) None (union_with f). `````` Robbert Krebbers committed Oct 19, 2012 167 `````` Proof. by intros [?|]. Qed. `````` Robbert Krebbers committed Jun 11, 2012 168 `````` Global Instance: Commutative (=) f → Commutative (=) (union_with f). `````` Robbert Krebbers committed Aug 29, 2012 169 170 `````` Proof. intros ? [?|] [?|]; compute; try reflexivity. `````` Robbert Krebbers committed Oct 19, 2012 171 `````` by rewrite (commutative f). `````` Robbert Krebbers committed Aug 29, 2012 172 `````` Qed. `````` Robbert Krebbers committed Jun 11, 2012 173 `````` Global Instance: Associative (=) f → Associative (=) (union_with f). `````` Robbert Krebbers committed Aug 29, 2012 174 175 `````` Proof. intros ? [?|] [?|] [?|]; compute; try reflexivity. `````` Robbert Krebbers committed Oct 19, 2012 176 `````` by rewrite (associative f). `````` Robbert Krebbers committed Aug 29, 2012 177 `````` Qed. `````` Robbert Krebbers committed Jun 11, 2012 178 `````` Global Instance: Idempotent (=) f → Idempotent (=) (union_with f). `````` Robbert Krebbers committed Aug 29, 2012 179 180 `````` Proof. intros ? [?|]; compute; try reflexivity. `````` Robbert Krebbers committed Oct 19, 2012 181 `````` by rewrite (idempotent f). `````` Robbert Krebbers committed Aug 29, 2012 182 183 184 185 186 `````` Qed. Global Instance: Commutative (=) f → Commutative (=) (intersection_with f). Proof. intros ? [?|] [?|]; compute; try reflexivity. `````` Robbert Krebbers committed Oct 19, 2012 187 `````` by rewrite (commutative f). `````` Robbert Krebbers committed Aug 29, 2012 188 189 190 191 `````` Qed. Global Instance: Associative (=) f → Associative (=) (intersection_with f). Proof. intros ? [?|] [?|] [?|]; compute; try reflexivity. `````` Robbert Krebbers committed Oct 19, 2012 192 `````` by rewrite (associative f). `````` Robbert Krebbers committed Aug 29, 2012 193 194 195 196 `````` Qed. Global Instance: Idempotent (=) f → Idempotent (=) (intersection_with f). Proof. intros ? [?|]; compute; try reflexivity. `````` Robbert Krebbers committed Oct 19, 2012 197 `````` by rewrite (idempotent f). `````` Robbert Krebbers committed Aug 29, 2012 198 `````` Qed. `````` Robbert Krebbers committed Aug 21, 2012 199 200 201 202 203 204 ``````End option_union_intersection. Section option_difference. Context {A} (f : A → A → option A). Global Instance: RightId (=) None (difference_with f). `````` Robbert Krebbers committed Oct 19, 2012 205 `````` Proof. by intros [?|]. Qed. `````` Robbert Krebbers committed Aug 21, 2012 206 ``End option_difference.``