Commit 487cdead authored by Robbert Krebbers's avatar Robbert Krebbers

Some minor cleanup, and more lemmas on prefix/postfixes of lists.

parent a89a1e98
......@@ -290,7 +290,7 @@ Notation "(≫=)" := (λ m f, mbind f m) (only parsing) : C_scope.
Notation "x ← y ; z" := (y = (λ x : _, z))
(at level 65, only parsing, next at level 35, right associativity) : C_scope.
Infix "<$>" := fmap (at level 65, right associativity) : C_scope.
Infix "<$>" := fmap (at level 60, right associativity) : C_scope.
Class MGuard (M : Type Type) :=
mguard: P {dec : Decision P} {A}, M A M A.
......@@ -425,6 +425,10 @@ Arguments left_absorb {_ _} _ _ {_} _.
Arguments right_absorb {_ _} _ _ {_} _.
Arguments anti_symmetric {_} _ {_} _ _ _ _.
Instance: Commutative () (@eq A).
Proof. red. intuition. Qed.
Instance: Commutative () (λ x y, @eq A y x).
Proof. red. intuition. Qed.
Instance: Commutative () ().
Proof. red. intuition. Qed.
Instance: Commutative () ().
......@@ -672,13 +676,11 @@ Section prod_relation.
End prod_relation.
(** ** Other *)
Definition lift_relation {A B} (R : relation A)
Definition proj_relation {A B} (R : relation A)
(f : B A) : relation B := λ x y, R (f x) (f y).
Definition lift_relation_equivalence {A B} (R : relation A) (f : B A) :
Equivalence R Equivalence (lift_relation R f).
Proof. unfold lift_relation. firstorder auto. Qed.
Hint Extern 0 (Equivalence (lift_relation _ _)) =>
eapply @lift_relation_equivalence : typeclass_instances.
Definition proj_relation_equivalence {A B} (R : relation A) (f : B A) :
Equivalence R Equivalence (proj_relation R f).
Proof. unfold proj_relation. firstorder auto. Qed.
Instance: A B (x : B), Commutative (=) (λ _ _ : A, x).
Proof. red. trivial. Qed.
......
......@@ -46,6 +46,17 @@ Ltac solve_decision := intros; first
[ solve_trivial_decision
| unfold Decision; decide equality; solve_trivial_decision ].
(** 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_and4 S1 S2 S3 S4 :=
(if S1 then cast_if_and3 S2 S3 S4 else right _).
Notation cast_if_or S1 S2 := (if S1 then left _ else cast_if S2).
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 _).
(** We can convert decidable propositions to booleans. *)
Definition bool_decide (P : Prop) {dec : Decision P} : bool :=
if dec then true else false.
......@@ -66,8 +77,7 @@ 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 :=
xbool_decide_pack _ p.
Lemma dsig_eq {A} (P : A Prop) {dec : x, Decision (P x)}
Lemma dsig_eq `(P : A Prop) `{ x, Decision (P x)}
(x y : dsig P) : x = y `x = `y.
Proof.
split.
......@@ -78,16 +88,13 @@ Proof.
+ by intros [] [].
+ done.
Qed.
Lemma dexists_proj1 `(P : A Prop) `{ x, Decision (P x)} (x : dsig P) p :
dexist (`x) p = x.
Proof. by apply dsig_eq. Qed.
(** 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_and4 S1 S2 S3 S4 :=
(if S1 then cast_if_and3 S2 S3 S4 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 _).
Global Instance dsig_eq_dec `(P : A Prop) `{ x, Decision (P x)}
`{ x y : A, Decision (x = y)} (x y : dsig P) : Decision (x = y).
Proof. refine (cast_if (decide (`x = `y))); by rewrite dsig_eq. Defined.
(** * Instances of Decision *)
(** Instances of [Decision] for operators of propositional logic. *)
......
This diff is collapsed.
......@@ -6,6 +6,8 @@ notations. *)
Require Export PArith NArith ZArith.
Require Export base decidable.
Coercion Z.of_nat : nat >-> Z.
Reserved Notation "x ≤ y ≤ z" (at level 70, y at next level).
Reserved Notation "x ≤ y < z" (at level 70, y at next level).
Reserved Notation "x < y < z" (at level 70, y at next level).
......@@ -105,17 +107,50 @@ Definition Z_to_option_N (x : Z) : option N :=
| Zpos p => Some (Npos p)
| Zneg _ => None
end.
Definition Z_to_option_nat (x : Z) : option nat :=
match x with
| Z0 => Some 0
| Zpos p => Some (Pos.to_nat p)
| Zneg _ => None
end.
(** The function [Z_decide] converts a decidable proposition [P] into an integer
by yielding one if [P] holds and zero if [P] does not. *)
Definition Z_decide (P : Prop) {dec : Decision P} : Z :=
(if dec then 1 else 0)%Z.
Lemma Z_to_option_N_Some x y :
Z_to_option_N x = Some y (0 x)%Z y = Z.to_N x.
Proof.
split.
* intros. by destruct x; simpl in *; simplify_equality;
auto using Zle_0_pos.
* intros [??]. subst. destruct x; simpl; auto; lia.
Qed.
Lemma Z_to_option_N_Some_alt x y :
Z_to_option_N x = Some y (0 x)%Z x = Z.of_N y.
Proof.
rewrite Z_to_option_N_Some.
split; intros [??]; subst; auto using N2Z.id, Z2N.id, eq_sym.
Qed.
(** The function [Z_decide_rel] is the more efficient variant of [Z_decide] when
used for binary relations. It yields one if [R x y] and zero if not [R x y]. *)
Definition Z_decide_rel {A B} (R : A B Prop)
{dec : x y, Decision (R x y)} (x : A) (y : B) : Z :=
(if dec x y then 1 else 0)%Z.
Lemma Z_to_option_nat_Some x y :
Z_to_option_nat x = Some y (0 x)%Z y = Z.to_nat x.
Proof.
split.
* intros. by destruct x; simpl in *; simplify_equality;
auto using Zle_0_pos.
* intros [??]. subst. destruct x; simpl; auto; lia.
Qed.
Lemma Z_to_option_nat_Some_alt x y :
Z_to_option_nat x = Some y (0 x)%Z x = Z.of_nat y.
Proof.
rewrite Z_to_option_nat_Some.
split; intros [??]; subst; auto using Nat2Z.id, Z2Nat.id, eq_sym.
Qed.
Lemma Z_to_option_of_nat x :
Z_to_option_nat (Z.of_nat x) = Some x.
Proof. apply Z_to_option_nat_Some_alt. auto using Nat2Z.is_nonneg. Qed.
(** The function [Z_of_sumbool] converts a sumbool [P] into an integer
by yielding one if [P] and zero if [Q]. *)
Definition Z_of_sumbool {P Q : Prop} (p : {P} + {Q}) : Z :=
(if p then 1 else 0)%Z.
(** Some correspondence lemmas between [nat] and [N] that are not part of the
standard library. We declare a hint database [natify] to rewrite a goal
......
......@@ -5,12 +5,12 @@ the development. *)
Require Export Psatz.
Require Export base.
(** We declare hint databases [lia] and [congruence] containing solely the
following hints. These hint database are useful in combination with another
hint database [db] that contain lemmas with premises that should be solved by
[lia] or [congruence]. One can now just say [auto with db,lia]. *)
(** We declare hint databases [f_equal], [congruence] and [lia] and containing
solely the tactic corresponding to its name. These hint database are useful in
to be combined in combination with other hint database. *)
Hint Extern 998 (_ = _) => f_equal : f_equal.
Hint Extern 999 => congruence : congruence.
Hint Extern 1000 => lia : lia.
Hint Extern 1000 => congruence : congruence.
(** The tactic [intuition] expands to [intuition auto with *] by default. This
is rather efficient when having big hint databases, or expensive [Hint Extern]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment