decidable.v 5.02 KB
 Robbert Krebbers committed Aug 29, 2012 1 2 3 4 5 ``````(* Copyright (c) 2012, Robbert Krebbers. *) (* 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 Oct 19, 2012 6 7 8 9 ``````Require Export base tactics. Lemma dec_stable `{Decision P} : ¬¬P → P. Proof. firstorder. Qed. `````` Robbert Krebbers committed Jun 11, 2012 10 `````` `````` Robbert Krebbers committed Aug 29, 2012 11 12 13 14 15 16 17 18 19 ``````(** 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 20 ``````Proof. done. Qed. `````` Robbert Krebbers committed Jun 11, 2012 21 `````` `````` Robbert Krebbers committed Aug 29, 2012 22 ``````(** The tactic [case_decide] performs case analysis on an arbitrary occurrence `````` Robbert Krebbers committed Oct 19, 2012 23 ``````of [decide] or [decide_rel] in the conclusion or hypotheses. *) `````` Robbert Krebbers committed Aug 29, 2012 24 ``````Ltac case_decide := `````` Robbert Krebbers committed Jun 11, 2012 25 `````` match goal with `````` Robbert Krebbers committed Aug 29, 2012 26 27 `````` | H : context [@decide ?P ?dec] |- _ => destruct (@decide P dec) `````` Robbert Krebbers committed Aug 21, 2012 28 `````` | H : context [@decide_rel _ _ ?R ?x ?y ?dec] |- _ => `````` Robbert Krebbers committed Aug 29, 2012 29 30 31 `````` destruct (@decide_rel _ _ R x y dec) | |- context [@decide ?P ?dec] => destruct (@decide P dec) `````` Robbert Krebbers committed Aug 21, 2012 32 `````` | |- context [@decide_rel _ _ ?R ?x ?y ?dec] => `````` Robbert Krebbers committed Aug 29, 2012 33 `````` destruct (@decide_rel _ _ R x y dec) `````` Robbert Krebbers committed Jun 11, 2012 34 35 `````` end. `````` Robbert Krebbers committed Aug 29, 2012 36 37 ``````(** 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 38 39 ``````Ltac solve_trivial_decision := match goal with `````` Robbert Krebbers committed Oct 19, 2012 40 41 `````` | |- Decision (?P) => apply _ | |- sumbool ?P (¬?P) => change (Decision P); apply _ `````` Robbert Krebbers committed Jun 11, 2012 42 `````` end. `````` Robbert Krebbers committed Oct 19, 2012 43 44 45 ``````Ltac solve_decision := intros; first [ solve_trivial_decision | unfold Decision; decide equality; solve_trivial_decision ]. `````` Robbert Krebbers committed Jun 11, 2012 46 `````` `````` Robbert Krebbers committed Aug 29, 2012 47 48 49 ``````(** 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 50 51 `````` Lemma bool_decide_unpack (P : Prop) {dec : Decision P} : bool_decide P → P. `````` Robbert Krebbers committed Oct 19, 2012 52 ``````Proof. unfold bool_decide. by destruct dec. Qed. `````` Robbert Krebbers committed Jun 11, 2012 53 ``````Lemma bool_decide_pack (P : Prop) {dec : Decision P} : P → bool_decide P. `````` Robbert Krebbers committed Oct 19, 2012 54 ``````Proof. unfold bool_decide. by destruct dec. Qed. `````` Robbert Krebbers committed Jun 11, 2012 55 `````` `````` Robbert Krebbers committed Aug 29, 2012 56 57 58 59 60 ``````(** * 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 irrelevant. That is [∀ x y : dsig P, x = y ↔ `x = `y]. *) `````` Robbert Krebbers committed Aug 21, 2012 61 62 63 64 65 66 ``````Definition dsig `(P : A → Prop) `{∀ x : A, Decision (P x)} := { x | bool_decide (P x) }. 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 Jun 11, 2012 67 `````` `````` Robbert Krebbers committed Aug 21, 2012 68 ``````Lemma dsig_eq {A} (P : A → Prop) {dec : ∀ x, Decision (P x)} `````` Robbert Krebbers committed Aug 29, 2012 69 `````` (x y : dsig P) : x = y ↔ `x = `y. `````` Robbert Krebbers committed Jun 11, 2012 70 ``````Proof. `````` Robbert Krebbers committed Aug 29, 2012 71 72 73 74 75 `````` split. * destruct x, y. apply proj1_sig_inj. * intro. destruct x as [x Hx], y as [y Hy]. simpl in *. subst. f_equal. revert Hx Hy. case (bool_decide (P y)). `````` Robbert Krebbers committed Oct 19, 2012 76 77 `````` + by intros [] []. + done. `````` Robbert Krebbers committed Jun 11, 2012 78 ``````Qed. `````` Robbert Krebbers committed Aug 29, 2012 79 `````` `````` Robbert Krebbers committed Oct 19, 2012 80 81 82 83 84 85 86 87 ``````(** The following combinators are useful to create Decision proofs in combination with the [refine] tactic. *) 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_or S1 S2 := (if S1 then left _ else cast_if S2). Notation cast_if_not S := (if S then right _ else left _). `````` Robbert Krebbers committed Aug 29, 2012 88 89 ``````(** * Instances of Decision *) (** Instances of [Decision] for operators of propositional logic. *) `````` Robbert Krebbers committed Oct 19, 2012 90 91 92 93 94 95 ``````Instance True_dec: Decision True := left I. Instance False_dec: Decision False := right (False_rect False). Section prop_dec. Context `(P_dec : Decision P) `(Q_dec : Decision Q). `````` Robbert Krebbers committed Nov 12, 2012 96 97 `````` Global Instance not_dec: Decision (¬P). Proof. refine (cast_if_not P_dec); intuition. Defined. `````` Robbert Krebbers committed Oct 19, 2012 98 `````` Global Instance and_dec: Decision (P ∧ Q). `````` Robbert Krebbers committed Nov 12, 2012 99 `````` Proof. refine (cast_if_and P_dec Q_dec); intuition. Defined. `````` Robbert Krebbers committed Oct 19, 2012 100 `````` Global Instance or_dec: Decision (P ∨ Q). `````` Robbert Krebbers committed Nov 12, 2012 101 `````` Proof. refine (cast_if_or P_dec Q_dec); intuition. Defined. `````` Robbert Krebbers committed Oct 19, 2012 102 `````` Global Instance impl_dec: Decision (P → Q). `````` Robbert Krebbers committed Nov 12, 2012 103 `````` Proof. refine (if P_dec then cast_if Q_dec else left _); intuition. Defined. `````` Robbert Krebbers committed Oct 19, 2012 104 ``````End prop_dec. `````` Robbert Krebbers committed Aug 29, 2012 105 106 `````` (** Instances of [Decision] for common data types. *) `````` Robbert Krebbers committed Oct 19, 2012 107 108 109 110 111 112 113 114 115 116 117 ``````Instance unit_eq_dec (x y : unit) : Decision (x = y). Proof. refine (left _); by destruct x, y. Defined. Instance prod_eq_dec `(A_dec : ∀ x y : A, Decision (x = y)) `(B_dec : ∀ x y : B, Decision (x = y)) (x y : A * B) : Decision (x = y). Proof. refine (cast_if_and (A_dec (fst x) (fst y)) (B_dec (snd x) (snd y))); abstract (destruct x, y; simpl in *; congruence). Defined. Instance sum_eq_dec `(A_dec : ∀ x y : A, Decision (x = y)) `(B_dec : ∀ x y : B, Decision (x = y)) (x y : A + B) : Decision (x = y). Proof. solve_decision. Defined.``````