| 1 |
(************************************************************************) |
| 2 |
(* * The Rocq Prover / The Rocq Development Team *) |
| 3 |
(* v * Copyright INRIA, CNRS and contributors *) |
| 4 |
(* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 |
(* \VV/ **************************************************************) |
| 6 |
(* // * This file is distributed under the terms of the *) |
| 7 |
(* * GNU Lesser General Public License Version 2.1 *) |
| 8 |
(* * (see LICENSE file for the text of the license) *) |
| 9 |
(************************************************************************) |
| 10 |
|
| 11 |
(** * Typeclass-based setoids. Definitions on [Equivalence]. |
| 12 |
|
| 13 |
Author: Matthieu Sozeau |
| 14 |
Institution: LRI, CNRS UMR 8623 - University Paris Sud |
| 15 |
*) |
| 16 |
|
| 17 |
Require Import Corelib.Program.Basics. |
| 18 |
Require Import Corelib.Program.Tactics. |
| 19 |
|
| 20 |
Require Import Corelib.Classes.Init. |
| 21 |
Require Import Relation_Definitions. |
| 22 |
Require Export Corelib.Classes.RelationClasses. |
| 23 |
Require Import Corelib.Classes.Morphisms. |
| 24 |
|
| 25 |
Set Implicit Arguments. |
| 26 |
Unset Strict Implicit. |
| 27 |
|
| 28 |
Generalizable Variables A R eqA B S eqB. |
| 29 |
Local Obligation Tactic := try solve [simpl_relation]. |
| 30 |
|
| 31 |
Local Open Scope signature_scope. |
| 32 |
|
| 33 |
Definition equiv `{Equivalence A R} : relation A := R. |
| 34 |
|
| 35 |
(** Overloaded notations for setoid equivalence and inequivalence. |
| 36 |
Not to be confused with [eq] and [=]. *) |
| 37 |
|
| 38 |
Declare Scope equiv_scope. |
| 39 |
|
| 40 |
Notation " x === y " := (equiv x y) (at level 70, no associativity) : equiv_scope. |
| 41 |
|
| 42 |
Notation " x =/= y " := (complement equiv x y) (at level 70, no associativity) : equiv_scope. |
| 43 |
|
| 44 |
Local Open Scope equiv_scope. |
| 45 |
|
| 46 |
(** Overloading for [PER]. *) |
| 47 |
|
| 48 |
Definition pequiv `{PER A R} : relation A := R. |
| 49 |
|
| 50 |
(** Overloaded notation for partial equivalence. *) |
| 51 |
|
| 52 |
Infix "=~=" := pequiv (at level 70, no associativity) : equiv_scope. |
| 53 |
|
| 54 |
(** Shortcuts to make proof search easier. *) |
| 55 |
|
| 56 |
#[global] |
| 57 |
Program Instance equiv_reflexive `(sa : Equivalence A) : Reflexive equiv | 1. |
| 58 |
|
| 59 |
#[global] |
| 60 |
Program Instance equiv_symmetric `(sa : Equivalence A) : Symmetric equiv | 1. |
| 61 |
|
| 62 |
#[global] |
| 63 |
Program Instance equiv_transitive `(sa : Equivalence A) : Transitive equiv | 1. |
| 64 |
|
| 65 |
Next Obligation. |
| 66 |
Proof. |
| 67 |
intros A R sa x y z Hxy Hyz. |
| 68 |
now transitivity y. |
| 69 |
Qed. |
| 70 |
|
| 71 |
Arguments equiv_symmetric {A R} sa x y : rename. |
| 72 |
Arguments equiv_transitive {A R} sa x y z : rename. |
| 73 |
|
| 74 |
(** Use the [substitute] command which substitutes an equivalence in every hypothesis. *) |
| 75 |
|
| 76 |
Ltac setoid_subst H := |
| 77 |
match type of H with |
| 78 |
?x === ?y => substitute H ; clear H x |
| 79 |
end. |
| 80 |
|
| 81 |
Ltac setoid_subst_nofail := |
| 82 |
match goal with |
| 83 |
| [ H : ?x === ?y |- _ ] => setoid_subst H ; setoid_subst_nofail |
| 84 |
| _ => idtac |
| 85 |
end. |
| 86 |
|
| 87 |
(** [subst*] will try its best at substituting every equality in the goal. *) |
| 88 |
|
| 89 |
Tactic Notation "subst" "*" := subst_no_fail ; setoid_subst_nofail. |
| 90 |
|
| 91 |
(** Simplify the goal w.r.t. equivalence. *) |
| 92 |
|
| 93 |
Ltac equiv_simplify_one := |
| 94 |
match goal with |
| 95 |
| [ H : ?x === ?x |- _ ] => clear H |
| 96 |
| [ H : ?x === ?y |- _ ] => setoid_subst H |
| 97 |
| [ |- ?x =/= ?y ] => let name:=fresh "Hneq" in intro name |
| 98 |
| [ |- ~ ?x === ?y ] => let name:=fresh "Hneq" in intro name |
| 99 |
end. |
| 100 |
|
| 101 |
Ltac equiv_simplify := repeat equiv_simplify_one. |
| 102 |
|
| 103 |
(** "reify" relations which are equivalences to applications of the overloaded [equiv] method |
| 104 |
for easy recognition in tactics. *) |
| 105 |
|
| 106 |
Ltac equivify_tac := |
| 107 |
match goal with |
| 108 |
| [ s : Equivalence ?A ?R, H : ?R ?x ?y |- _ ] => change R with (@equiv A R s) in H |
| 109 |
| [ s : Equivalence ?A ?R |- context C [ ?R ?x ?y ] ] => change (R x y) with (@equiv A R s x y) |
| 110 |
end. |
| 111 |
|
| 112 |
Ltac equivify := repeat equivify_tac. |
| 113 |
|
| 114 |
Section Respecting. |
| 115 |
|
| 116 |
(** Here we build an equivalence instance for functions which relates respectful ones only, |
| 117 |
we do not export it. *) |
| 118 |
|
| 119 |
Definition respecting `(eqa : Equivalence A (R : relation A), |
| 120 |
eqb : Equivalence B (R' : relation B)) : Type := |
| 121 |
{ morph : A -> B | respectful R R' morph morph }. |
| 122 |
|
| 123 |
Program Instance respecting_equiv `(eqa : Equivalence A R, eqb : Equivalence B R') : |
| 124 |
Equivalence (fun (f g : respecting eqa eqb) => |
| 125 |
forall (x y : A), R x y -> R' (proj1_sig f x) (proj1_sig g y)). |
| 126 |
|
| 127 |
Solve Obligations with unfold respecting in * ; simpl_relation ; program_simpl. |
| 128 |
|
| 129 |
Next Obligation. |
| 130 |
Proof. |
| 131 |
intros. intros f g h H H' x y Rxy. |
| 132 |
unfold respecting in *. program_simpl. transitivity (g y); auto. firstorder. |
| 133 |
Qed. |
| 134 |
|
| 135 |
End Respecting. |
| 136 |
|
| 137 |
(** The default equivalence on function spaces, with higher priority than [eq]. *) |
| 138 |
|
| 139 |
#[global] |
| 140 |
Instance pointwise_reflexive {A} `(reflb : Reflexive B eqB) : |
| 141 |
Reflexive (pointwise_relation A eqB) | 9. |
| 142 |
Proof. firstorder. Qed. |
| 143 |
#[global] |
| 144 |
Instance pointwise_symmetric {A} `(symb : Symmetric B eqB) : |
| 145 |
Symmetric (pointwise_relation A eqB) | 9. |
| 146 |
Proof. firstorder. Qed. |
| 147 |
#[global] |
| 148 |
Instance pointwise_transitive {A} `(transb : Transitive B eqB) : |
| 149 |
Transitive (pointwise_relation A eqB) | 9. |
| 150 |
Proof. firstorder. Qed. |
| 151 |
#[global] |
| 152 |
Instance pointwise_equivalence {A} `(eqb : Equivalence B eqB) : |
| 153 |
Equivalence (pointwise_relation A eqB) | 9. |
| 154 |
Proof. split; apply _. Qed. |