blob: a03f15c42d4fab996fa15b3e06d7938da21a83c2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
(************************************************************************)
(* v * The Coq Proof Assistant / The Coq Development Team *)
(* <O___,, * INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2010 *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(************************************************************************)
(** * A typeclass to ease the handling of decidable properties. *)
(** A proposition is decidable whenever it is reflected by a boolean. *)
Class Decidable (P : Prop) := {
Decidable_witness : bool;
Decidable_spec : Decidable_witness = true <-> P
}.
(** Alternative ways of specifying the reflection property. *)
Lemma Decidable_sound : forall P (H : Decidable P),
Decidable_witness = true -> P.
Proof.
intros P H Hp; apply -> Decidable_spec; assumption.
Qed.
Lemma Decidable_complete : forall P (H : Decidable P),
P -> Decidable_witness = true.
Proof.
intros P H Hp; apply <- Decidable_spec; assumption.
Qed.
Lemma Decidable_sound_alt : forall P (H : Decidable P),
~ P -> Decidable_witness = false.
Proof.
intros P [wit spec] Hd; simpl; destruct wit; tauto.
Qed.
Lemma Decidable_complete_alt : forall P (H : Decidable P),
Decidable_witness = false -> ~ P.
Proof.
intros P [wit spec] Hd Hc; simpl in *; intuition congruence.
Qed.
(** The generic function that should be used to program, together with some
useful tactics. *)
Definition decide P {H : Decidable P} := @Decidable_witness P H.
Ltac _decide_ P H :=
let b := fresh "b" in
set (b := decide P) in *;
assert (H : decide P = b) by reflexivity;
clearbody b;
destruct b; [apply Decidable_sound in H|apply Decidable_complete_alt in H].
Tactic Notation "decide" constr(P) "as" ident(H) :=
_decide_ P H.
Tactic Notation "decide" constr(P) :=
let H := fresh "H" in _decide_ P H.
(** Some usual instances. *)
Require Import Bool Arith ZArith.
Program Instance Decidable_eq_bool : forall (x y : bool), Decidable (eq x y) := {
Decidable_witness := eqb x y
}.
Next Obligation.
split.
now apply eqb_prop.
now destruct 1; apply eqb_reflx.
Qed.
Program Instance Decidable_eq_nat : forall (x y : nat), Decidable (eq x y) := {
Decidable_witness := beq_nat x y
}.
Next Obligation.
split.
now intros H; symmetry in H; apply beq_nat_eq in H; auto.
now destruct 1; symmetry; apply beq_nat_refl.
Qed.
Program Instance Decidable_le_nat : forall (x y : nat), Decidable (x <= y) := {
Decidable_witness := leb x y
}.
Next Obligation.
apply leb_iff.
Qed.
Program Instance Decidable_eq_Z : forall (x y : Z), Decidable (eq x y) := {
Decidable_witness := Zeq_bool x y
}.
Next Obligation.
split; apply Zeq_is_eq_bool.
Qed.
|