decidable.v 8.81 KB
Newer Older
 Robbert Krebbers committed Feb 08, 2015 1 ``````(* Copyright (c) 2012-2015, Robbert Krebbers. *) `````` Robbert Krebbers committed Aug 29, 2012 2 3 4 5 ``````(* This file is distributed under the terms of the BSD license. *) (** This file collects theorems, definitions, tactics, related to propositions with a decidable equality. Such propositions are collected by the [Decision] type class. *) `````` Robbert Krebbers committed Mar 14, 2013 6 ``````Require Export proof_irrel. `````` Robbert Krebbers committed Oct 19, 2012 7 `````` `````` Robbert Krebbers committed Jan 05, 2013 8 9 ``````Hint Extern 200 (Decision _) => progress (lazy beta) : typeclass_instances. `````` Robbert Krebbers committed Oct 19, 2012 10 11 ``````Lemma dec_stable `{Decision P} : ¬¬P → P. Proof. firstorder. Qed. `````` Robbert Krebbers committed Jun 11, 2012 12 `````` `````` Robbert Krebbers committed Feb 19, 2013 13 14 ``````Lemma Is_true_reflect (b : bool) : reflect b b. Proof. destruct b. by left. right. intros []. Qed. `````` 15 16 ``````Instance: Injective (=) (↔) Is_true. Proof. intros [] []; simpl; intuition. Qed. `````` Robbert Krebbers committed Feb 19, 2013 17 `````` `````` Robbert Krebbers committed Aug 29, 2012 18 19 20 21 22 23 24 25 26 ``````(** We introduce [decide_rel] to avoid inefficienct computation due to eager evaluation of propositions by [vm_compute]. This inefficiency occurs if [(x = y) := (f x = f y)] as [decide (x = y)] evaluates to [decide (f x = f y)] which then might lead to evaluation of [f x] and [f y]. Using [decide_rel] we hide [f] under a lambda abstraction to avoid this unnecessary evaluation. *) Definition decide_rel {A B} (R : A → B → Prop) {dec : ∀ x y, Decision (R x y)} (x : A) (y : B) : Decision (R x y) := dec x y. Lemma decide_rel_correct {A B} (R : A → B → Prop) `{∀ x y, Decision (R x y)} (x : A) (y : B) : decide_rel R x y = decide (R x y). `````` Robbert Krebbers committed Oct 19, 2012 27 ``````Proof. done. Qed. `````` Robbert Krebbers committed Jun 11, 2012 28 `````` `````` Robbert Krebbers committed Jun 24, 2013 29 ``````Lemma decide_True {A} `{Decision P} (x y : A) : `````` Robbert Krebbers committed Mar 14, 2013 30 31 `````` P → (if decide P then x else y) = x. Proof. by destruct (decide P). Qed. `````` Robbert Krebbers committed Jun 24, 2013 32 ``````Lemma decide_False {A} `{Decision P} (x y : A) : `````` Robbert Krebbers committed Mar 14, 2013 33 34 `````` ¬P → (if decide P then x else y) = y. Proof. by destruct (decide P). Qed. `````` Robbert Krebbers committed May 22, 2014 35 36 37 ``````Lemma decide_iff {A} P Q `{Decision P, Decision Q} (x y : A) : (P ↔ Q) → (if decide P then x else y) = (if decide Q then x else y). Proof. intros [??]. destruct (decide P), (decide Q); intuition. Qed. `````` Robbert Krebbers committed Mar 14, 2013 38 `````` `````` Robbert Krebbers committed Feb 19, 2013 39 40 ``````(** The tactic [destruct_decide] destructs a sumbool [dec]. If one of the components is double negated, it will try to remove the double negation. *) `````` Robbert Krebbers committed Mar 14, 2013 41 ``````Tactic Notation "destruct_decide" constr(dec) "as" ident(H) := `````` Robbert Krebbers committed Feb 19, 2013 42 43 44 45 `````` destruct dec as [H|H]; try match type of H with | ¬¬_ => apply dec_stable in H end. `````` Robbert Krebbers committed Mar 14, 2013 46 47 ``````Tactic Notation "destruct_decide" constr(dec) := let H := fresh in destruct_decide dec as H. `````` Robbert Krebbers committed Feb 19, 2013 48 `````` `````` Robbert Krebbers committed Aug 29, 2012 49 ``````(** The tactic [case_decide] performs case analysis on an arbitrary occurrence `````` Robbert Krebbers committed Oct 19, 2012 50 ``````of [decide] or [decide_rel] in the conclusion or hypotheses. *) `````` Robbert Krebbers committed Mar 14, 2013 51 ``````Tactic Notation "case_decide" "as" ident(Hd) := `````` Robbert Krebbers committed Jun 11, 2012 52 `````` match goal with `````` Robbert Krebbers committed Aug 29, 2012 53 `````` | H : context [@decide ?P ?dec] |- _ => `````` Robbert Krebbers committed Mar 14, 2013 54 `````` destruct_decide (@decide P dec) as Hd `````` Robbert Krebbers committed Aug 21, 2012 55 `````` | H : context [@decide_rel _ _ ?R ?x ?y ?dec] |- _ => `````` Robbert Krebbers committed Mar 14, 2013 56 `````` destruct_decide (@decide_rel _ _ R x y dec) as Hd `````` Robbert Krebbers committed Aug 29, 2012 57 `````` | |- context [@decide ?P ?dec] => `````` Robbert Krebbers committed Mar 14, 2013 58 `````` destruct_decide (@decide P dec) as Hd `````` Robbert Krebbers committed Aug 21, 2012 59 `````` | |- context [@decide_rel _ _ ?R ?x ?y ?dec] => `````` Robbert Krebbers committed Mar 14, 2013 60 `````` destruct_decide (@decide_rel _ _ R x y dec) as Hd `````` Robbert Krebbers committed Jun 11, 2012 61 `````` end. `````` Robbert Krebbers committed Mar 14, 2013 62 63 ``````Tactic Notation "case_decide" := let H := fresh in case_decide as H. `````` Robbert Krebbers committed Jun 11, 2012 64 `````` `````` Robbert Krebbers committed Aug 29, 2012 65 66 ``````(** The tactic [solve_decision] uses Coq's [decide equality] tactic together with instance resolution to automatically generate decision procedures. *) `````` Robbert Krebbers committed Jun 11, 2012 67 68 ``````Ltac solve_trivial_decision := match goal with `````` Robbert Krebbers committed Oct 19, 2012 69 70 `````` | |- Decision (?P) => apply _ | |- sumbool ?P (¬?P) => change (Decision P); apply _ `````` Robbert Krebbers committed Jun 11, 2012 71 `````` end. `````` Robbert Krebbers committed Oct 19, 2012 72 73 74 ``````Ltac solve_decision := intros; first [ solve_trivial_decision | unfold Decision; decide equality; solve_trivial_decision ]. `````` Robbert Krebbers committed Jun 11, 2012 75 `````` `````` Robbert Krebbers committed Feb 01, 2013 76 77 ``````(** The following combinators are useful to create Decision proofs in combination with the [refine] tactic. *) `````` Robbert Krebbers committed May 02, 2014 78 ``````Notation swap_if S := (match S with left H => right H | right H => left H end). `````` Robbert Krebbers committed Feb 01, 2013 79 80 81 82 83 ``````Notation cast_if S := (if S then left _ else right _). Notation cast_if_and S1 S2 := (if S1 then cast_if S2 else right _). Notation cast_if_and3 S1 S2 S3 := (if S1 then cast_if_and S2 S3 else right _). Notation cast_if_and4 S1 S2 S3 S4 := (if S1 then cast_if_and3 S2 S3 S4 else right _). `````` Robbert Krebbers committed May 21, 2013 84 85 ``````Notation cast_if_and5 S1 S2 S3 S4 S5 := (if S1 then cast_if_and4 S2 S3 S4 S5 else right _). `````` Robbert Krebbers committed May 02, 2014 86 87 ``````Notation cast_if_and6 S1 S2 S3 S4 S5 S6 := (if S1 then cast_if_and5 S2 S3 S4 S5 S6 else right _). `````` Robbert Krebbers committed Feb 01, 2013 88 ``````Notation cast_if_or S1 S2 := (if S1 then left _ else cast_if S2). `````` Robbert Krebbers committed May 07, 2013 89 ``````Notation cast_if_or3 S1 S2 S3 := (if S1 then left _ else cast_if_or S2 S3). `````` Robbert Krebbers committed Feb 01, 2013 90 91 92 ``````Notation cast_if_not_or S1 S2 := (if S1 then cast_if S2 else left _). Notation cast_if_not S := (if S then right _ else left _). `````` Robbert Krebbers committed Aug 29, 2012 93 94 95 ``````(** We can convert decidable propositions to booleans. *) Definition bool_decide (P : Prop) {dec : Decision P} : bool := if dec then true else false. `````` Robbert Krebbers committed Jun 11, 2012 96 `````` `````` Robbert Krebbers committed Feb 19, 2013 97 98 99 ``````Lemma bool_decide_reflect P `{dec : Decision P} : reflect P (bool_decide P). Proof. unfold bool_decide. destruct dec. by left. by right. Qed. `````` Robbert Krebbers committed May 21, 2013 100 ``````Tactic Notation "case_bool_decide" "as" ident (Hd) := `````` Robbert Krebbers committed Feb 19, 2013 101 102 `````` match goal with | H : context [@bool_decide ?P ?dec] |- _ => `````` Robbert Krebbers committed May 21, 2013 103 `````` destruct_decide (@bool_decide_reflect P dec) as Hd `````` Robbert Krebbers committed Feb 19, 2013 104 `````` | |- context [@bool_decide ?P ?dec] => `````` Robbert Krebbers committed May 21, 2013 105 `````` destruct_decide (@bool_decide_reflect P dec) as Hd `````` Robbert Krebbers committed Feb 19, 2013 106 `````` end. `````` Robbert Krebbers committed May 21, 2013 107 108 ``````Tactic Notation "case_bool_decide" := let H := fresh in case_bool_decide as H. `````` Robbert Krebbers committed Feb 19, 2013 109 `````` `````` 110 ``````Lemma bool_decide_spec (P : Prop) {dec : Decision P} : bool_decide P ↔ P. `````` Robbert Krebbers committed Oct 19, 2012 111 ``````Proof. unfold bool_decide. by destruct dec. Qed. `````` 112 113 ``````Lemma bool_decide_unpack (P : Prop) {dec : Decision P} : bool_decide P → P. Proof. by rewrite bool_decide_spec. Qed. `````` Robbert Krebbers committed Jun 11, 2012 114 ``````Lemma bool_decide_pack (P : Prop) {dec : Decision P} : P → bool_decide P. `````` 115 ``````Proof. by rewrite bool_decide_spec. Qed. `````` Robbert Krebbers committed Dec 23, 2014 116 117 118 119 120 121 122 ``````Lemma bool_decide_true (P : Prop) `{Decision P} : P → bool_decide P = true. Proof. by case_bool_decide. Qed. Lemma bool_decide_false (P : Prop) `{Decision P} : ¬P → bool_decide P = false. Proof. by case_bool_decide. Qed. Lemma bool_decide_iff (P Q : Prop) `{Decision P, Decision Q} : (P ↔ Q) → bool_decide P = bool_decide Q. Proof. repeat case_bool_decide; tauto. Qed. `````` Robbert Krebbers committed Jun 11, 2012 123 `````` `````` Robbert Krebbers committed Aug 29, 2012 124 125 126 127 ``````(** * Decidable Sigma types *) (** Leibniz equality on Sigma types requires the equipped proofs to be equal as Coq does not support proof irrelevance. For decidable we propositions we define the type [dsig P] whose Leibniz equality is proof `````` Robbert Krebbers committed Feb 01, 2017 128 ``````irrelevant. That is [∀ x y : dsig P, x = y ↔ `x = `y]. *) `````` Robbert Krebbers committed Aug 21, 2012 129 130 ``````Definition dsig `(P : A → Prop) `{∀ x : A, Decision (P x)} := { x | bool_decide (P x) }. `````` Robbert Krebbers committed Feb 19, 2013 131 `````` `````` Robbert Krebbers committed Aug 21, 2012 132 133 134 135 ``````Definition proj2_dsig `{∀ x : A, Decision (P x)} (x : dsig P) : P (`x) := bool_decide_unpack _ (proj2_sig x). Definition dexist `{∀ x : A, Decision (P x)} (x : A) (p : P x) : dsig P := x↾bool_decide_pack _ p. `````` Robbert Krebbers committed Feb 01, 2013 136 ``````Lemma dsig_eq `(P : A → Prop) `{∀ x, Decision (P x)} `````` Robbert Krebbers committed Aug 29, 2012 137 `````` (x y : dsig P) : x = y ↔ `x = `y. `````` Robbert Krebbers committed Mar 14, 2013 138 ``````Proof. apply (sig_eq_pi _). Qed. `````` Robbert Krebbers committed Feb 01, 2013 139 140 141 ``````Lemma dexists_proj1 `(P : A → Prop) `{∀ x, Decision (P x)} (x : dsig P) p : dexist (`x) p = x. Proof. by apply dsig_eq. Qed. `````` Robbert Krebbers committed Aug 29, 2012 142 143 144 `````` (** * Instances of Decision *) (** Instances of [Decision] for operators of propositional logic. *) `````` Robbert Krebbers committed Oct 19, 2012 145 146 ``````Instance True_dec: Decision True := left I. Instance False_dec: Decision False := right (False_rect False). `````` Robbert Krebbers committed May 02, 2014 147 148 ``````Instance Is_true_dec b : Decision (Is_true b). Proof. destruct b; apply _. Defined. `````` Robbert Krebbers committed Oct 19, 2012 149 150 151 152 `````` Section prop_dec. Context `(P_dec : Decision P) `(Q_dec : Decision Q). `````` Robbert Krebbers committed Nov 12, 2012 153 154 `````` Global Instance not_dec: Decision (¬P). Proof. refine (cast_if_not P_dec); intuition. Defined. `````` Robbert Krebbers committed Oct 19, 2012 155 `````` Global Instance and_dec: Decision (P ∧ Q). `````` Robbert Krebbers committed Nov 12, 2012 156 `````` Proof. refine (cast_if_and P_dec Q_dec); intuition. Defined. `````` Robbert Krebbers committed Oct 19, 2012 157 `````` Global Instance or_dec: Decision (P ∨ Q). `````` Robbert Krebbers committed Nov 12, 2012 158 `````` Proof. refine (cast_if_or P_dec Q_dec); intuition. Defined. `````` Robbert Krebbers committed Oct 19, 2012 159 `````` Global Instance impl_dec: Decision (P → Q). `````` Robbert Krebbers committed Nov 12, 2012 160 `````` Proof. refine (if P_dec then cast_if Q_dec else left _); intuition. Defined. `````` Robbert Krebbers committed Oct 19, 2012 161 ``````End prop_dec. `````` Robbert Krebbers committed May 02, 2014 162 163 ``````Instance iff_dec `(P_dec : Decision P) `(Q_dec : Decision Q) : Decision (P ↔ Q) := and_dec _ _. `````` Robbert Krebbers committed Aug 29, 2012 164 165 `````` (** Instances of [Decision] for common data types. *) `````` Robbert Krebbers committed Jan 05, 2013 166 167 ``````Instance bool_eq_dec (x y : bool) : Decision (x = y). Proof. solve_decision. Defined. `````` Robbert Krebbers committed Oct 19, 2012 168 ``````Instance unit_eq_dec (x y : unit) : Decision (x = y). `````` Robbert Krebbers committed May 02, 2014 169 ``````Proof. solve_decision. Defined. `````` Robbert Krebbers committed Oct 19, 2012 170 ``````Instance prod_eq_dec `(A_dec : ∀ x y : A, Decision (x = y)) `````` Robbert Krebbers committed Jan 05, 2013 171 `````` `(B_dec : ∀ x y : B, Decision (x = y)) (x y : A * B) : Decision (x = y). `````` Robbert Krebbers committed May 02, 2014 172 ``````Proof. solve_decision. Defined. `````` Robbert Krebbers committed Oct 19, 2012 173 ``````Instance sum_eq_dec `(A_dec : ∀ x y : A, Decision (x = y)) `````` Robbert Krebbers committed Jan 05, 2013 174 `````` `(B_dec : ∀ x y : B, Decision (x = y)) (x y : A + B) : Decision (x = y). `````` Robbert Krebbers committed Oct 19, 2012 175 ``````Proof. solve_decision. Defined. `````` Robbert Krebbers committed Jan 05, 2013 176 177 178 179 180 181 182 183 `````` Instance curry_dec `(P_dec : ∀ (x : A) (y : B), Decision (P x y)) p : Decision (curry P p) := match p as p return Decision (curry P p) with | (x,y) => P_dec x y end. Instance uncurry_dec `(P_dec : ∀ (p : A * B), Decision (P p)) x y : Decision (uncurry P x y) := P_dec (x,y). `````` Robbert Krebbers committed Mar 14, 2013 184 185 186 187 `````` Instance sig_eq_dec `(P : A → Prop) `{∀ x, ProofIrrel (P x)} `{∀ x y : A, Decision (x = y)} (x y : sig P) : Decision (x = y). Proof. refine (cast_if (decide (`x = `y))); by rewrite sig_eq_pi. Defined. `````` Robbert Krebbers committed Aug 21, 2013 188 189 `````` (** Some laws for decidable propositions *) `````` Robbert Krebbers committed May 02, 2014 190 191 192 193 194 ``````Lemma not_and_l {P Q : Prop} `{Decision P} : ¬(P ∧ Q) ↔ ¬P ∨ ¬Q. Proof. destruct (decide P); tauto. Qed. Lemma not_and_r {P Q : Prop} `{Decision Q} : ¬(P ∧ Q) ↔ ¬P ∨ ¬Q. Proof. destruct (decide Q); tauto. Qed. Lemma not_and_l_alt {P Q : Prop} `{Decision P} : ¬(P ∧ Q) ↔ ¬P ∨ (¬Q ∧ P). `````` Robbert Krebbers committed Aug 21, 2013 195 ``````Proof. destruct (decide P); tauto. Qed. `````` Robbert Krebbers committed May 02, 2014 196 ``````Lemma not_and_r_alt {P Q : Prop} `{Decision Q} : ¬(P ∧ Q) ↔ (¬P ∧ Q) ∨ ¬Q. `````` Robbert Krebbers committed Aug 21, 2013 197 ``Proof. destruct (decide Q); tauto. Qed.``