| 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 |
(** * Tactics for typeclass-based setoids. |
| 12 |
|
| 13 |
Author: Matthieu Sozeau |
| 14 |
Institution: LRI, CNRS UMR 8623 - University Paris Sud |
| 15 |
*) |
| 16 |
|
| 17 |
Require Corelib.Classes.CRelationClasses Corelib.Classes.CMorphisms. |
| 18 |
Require Import Corelib.Classes.Morphisms Corelib.Classes.Morphisms_Prop. |
| 19 |
Require Export Corelib.Classes.RelationClasses Corelib.Relations.Relation_Definitions. |
| 20 |
Require Import Corelib.Classes.Equivalence Corelib.Program.Basics. |
| 21 |
|
| 22 |
Generalizable Variables A R. |
| 23 |
|
| 24 |
Export ProperNotations. |
| 25 |
|
| 26 |
Set Implicit Arguments. |
| 27 |
Unset Strict Implicit. |
| 28 |
|
| 29 |
(** Default relation on a given support. Can be used by tactics |
| 30 |
to find a sensible default relation on any carrier. Users can |
| 31 |
declare an [Instance def : DefaultRelation A RA] anywhere to |
| 32 |
declare a default relation. This is used by setoid_replace to infer |
| 33 |
the relation to use on a given type, in a given context. |
| 34 |
*) |
| 35 |
|
| 36 |
Class DefaultRelation A (R : relation A). |
| 37 |
Register DefaultRelation as rewrite.DefaultRelation. |
| 38 |
|
| 39 |
(** To search for the default relation, just call [default_relation]. *) |
| 40 |
|
| 41 |
Definition default_relation `{DefaultRelation A R} := R. |
| 42 |
|
| 43 |
(** Every [Equivalence] gives a default relation, if no other is given |
| 44 |
(lowest priority). *) |
| 45 |
|
| 46 |
#[global] |
| 47 |
Instance equivalence_default `(Equivalence A R) : DefaultRelation R | 4. |
| 48 |
Defined. |
| 49 |
|
| 50 |
(** The setoid_replace tactics in Ltac, defined in terms of default relations |
| 51 |
and the setoid_rewrite tactic. *) |
| 52 |
|
| 53 |
Ltac setoidreplace H t := |
| 54 |
let Heq := fresh "Heq" in |
| 55 |
cut(H) ; unfold default_relation ; [ intro Heq ; setoid_rewrite Heq ; clear Heq | t ]. |
| 56 |
|
| 57 |
Ltac setoidreplacein H H' t := |
| 58 |
let Heq := fresh "Heq" in |
| 59 |
cut(H) ; unfold default_relation ; [ intro Heq ; setoid_rewrite Heq in H' ; clear Heq | t ]. |
| 60 |
|
| 61 |
Ltac setoidreplaceinat H H' t occs := |
| 62 |
let Heq := fresh "Heq" in |
| 63 |
cut(H) ; unfold default_relation ; [ intro Heq ; setoid_rewrite Heq in H' at occs ; clear Heq | t ]. |
| 64 |
|
| 65 |
Ltac setoidreplaceat H t occs := |
| 66 |
let Heq := fresh "Heq" in |
| 67 |
cut(H) ; unfold default_relation ; [ intro Heq ; setoid_rewrite Heq at occs ; clear Heq | t ]. |
| 68 |
|
| 69 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) := |
| 70 |
setoidreplace (default_relation x y) idtac. |
| 71 |
|
| 72 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 73 |
"at" int_or_var_list(o) := |
| 74 |
setoidreplaceat (default_relation x y) idtac o. |
| 75 |
|
| 76 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 77 |
"in" hyp(id) := |
| 78 |
setoidreplacein (default_relation x y) id idtac. |
| 79 |
|
| 80 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 81 |
"in" hyp(id) |
| 82 |
"at" int_or_var_list(o) := |
| 83 |
setoidreplaceinat (default_relation x y) id idtac o. |
| 84 |
|
| 85 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 86 |
"by" tactic3(t) := |
| 87 |
setoidreplace (default_relation x y) ltac:(t). |
| 88 |
|
| 89 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 90 |
"at" int_or_var_list(o) |
| 91 |
"by" tactic3(t) := |
| 92 |
setoidreplaceat (default_relation x y) ltac:(t) o. |
| 93 |
|
| 94 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 95 |
"in" hyp(id) |
| 96 |
"by" tactic3(t) := |
| 97 |
setoidreplacein (default_relation x y) id ltac:(t). |
| 98 |
|
| 99 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 100 |
"in" hyp(id) |
| 101 |
"at" int_or_var_list(o) |
| 102 |
"by" tactic3(t) := |
| 103 |
setoidreplaceinat (default_relation x y) id ltac:(t) o. |
| 104 |
|
| 105 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 106 |
"using" "relation" constr(rel) := |
| 107 |
setoidreplace (rel x y) idtac. |
| 108 |
|
| 109 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 110 |
"using" "relation" constr(rel) |
| 111 |
"at" int_or_var_list(o) := |
| 112 |
setoidreplaceat (rel x y) idtac o. |
| 113 |
|
| 114 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 115 |
"using" "relation" constr(rel) |
| 116 |
"by" tactic3(t) := |
| 117 |
setoidreplace (rel x y) ltac:(t). |
| 118 |
|
| 119 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 120 |
"using" "relation" constr(rel) |
| 121 |
"at" int_or_var_list(o) |
| 122 |
"by" tactic3(t) := |
| 123 |
setoidreplaceat (rel x y) ltac:(t) o. |
| 124 |
|
| 125 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 126 |
"using" "relation" constr(rel) |
| 127 |
"in" hyp(id) := |
| 128 |
setoidreplacein (rel x y) id idtac. |
| 129 |
|
| 130 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 131 |
"using" "relation" constr(rel) |
| 132 |
"in" hyp(id) |
| 133 |
"at" int_or_var_list(o) := |
| 134 |
setoidreplaceinat (rel x y) id idtac o. |
| 135 |
|
| 136 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 137 |
"using" "relation" constr(rel) |
| 138 |
"in" hyp(id) |
| 139 |
"by" tactic3(t) := |
| 140 |
setoidreplacein (rel x y) id ltac:(t). |
| 141 |
|
| 142 |
Tactic Notation "setoid_replace" constr(x) "with" constr(y) |
| 143 |
"using" "relation" constr(rel) |
| 144 |
"in" hyp(id) |
| 145 |
"at" int_or_var_list(o) |
| 146 |
"by" tactic3(t) := |
| 147 |
setoidreplaceinat (rel x y) id ltac:(t) o. |
| 148 |
|
| 149 |
(** The [add_morphism_tactic] tactic is run at each [Add Morphism] |
| 150 |
command before giving the hand back to the user to discharge the |
| 151 |
proof. It essentially amounts to unfold the right amount of |
| 152 |
[respectful] calls and substitute leibniz equalities. One can |
| 153 |
redefine it using [Ltac add_morphism_tactic ::= t]. *) |
| 154 |
|
| 155 |
Require Import Corelib.Program.Tactics. |
| 156 |
|
| 157 |
Local Open Scope signature_scope. |
| 158 |
|
| 159 |
Ltac red_subst_eq_morphism concl := |
| 160 |
match concl with |
| 161 |
| @Logic.eq ?A ==> ?R' => red ; intros ; subst ; red_subst_eq_morphism R' |
| 162 |
| ?R ==> ?R' => red ; intros ; red_subst_eq_morphism R' |
| 163 |
| _ => idtac |
| 164 |
end. |
| 165 |
|
| 166 |
Ltac destruct_proper := |
| 167 |
match goal with |
| 168 |
| [ |- @Proper ?A ?R ?m ] => red |
| 169 |
end. |
| 170 |
|
| 171 |
Ltac reverse_arrows x := |
| 172 |
match x with |
| 173 |
| @Logic.eq ?A ==> ?R' => revert_last ; reverse_arrows R' |
| 174 |
| ?R ==> ?R' => do 3 revert_last ; reverse_arrows R' |
| 175 |
| _ => idtac |
| 176 |
end. |
| 177 |
|
| 178 |
Ltac default_add_morphism_tactic := |
| 179 |
unfold flip ; intros ; |
| 180 |
(try destruct_proper) ; |
| 181 |
match goal with |
| 182 |
| [ |- (?x ==> ?y) _ _ ] => red_subst_eq_morphism (x ==> y) ; reverse_arrows (x ==> y) |
| 183 |
end. |
| 184 |
|
| 185 |
Ltac add_morphism_tactic := default_add_morphism_tactic. |
| 186 |
|
| 187 |
#[global] Obligation Tactic := program_simpl. |
| 188 |
#[export] Obligation Tactic := program_simpl. |
| 189 |
|
| 190 |
(* Notation "'Morphism' s t " := (@Proper _ (s%signature) t) (at level 10, s at next level, t at next level). *) |