| 1 |
(* -*- coding: utf-8 -*- *) |
| 2 |
(************************************************************************) |
| 3 |
(* * The Rocq Prover / The Rocq Development Team *) |
| 4 |
(* v * Copyright INRIA, CNRS and contributors *) |
| 5 |
(* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 6 |
(* \VV/ **************************************************************) |
| 7 |
(* // * This file is distributed under the terms of the *) |
| 8 |
(* * GNU Lesser General Public License Version 2.1 *) |
| 9 |
(* * (see LICENSE file for the text of the license) *) |
| 10 |
(************************************************************************) |
| 11 |
|
| 12 |
(** * Typeclass-based relations, tactics and standard instances |
| 13 |
|
| 14 |
This is the basic theory needed to formalize morphisms and setoids. |
| 15 |
|
| 16 |
Author: Matthieu Sozeau |
| 17 |
Institution: LRI, CNRS UMR 8623 - University Paris Sud |
| 18 |
*) |
| 19 |
|
| 20 |
Require Export Corelib.Classes.Init. |
| 21 |
Require Import Corelib.Program.Basics. |
| 22 |
Require Import Corelib.Program.Tactics. |
| 23 |
|
| 24 |
Generalizable Variables A B C D R S T U l eqA eqB eqC eqD. |
| 25 |
|
| 26 |
Set Universe Polymorphism. |
| 27 |
|
| 28 |
Definition crelation (A : Type) := A -> A -> Type. |
| 29 |
|
| 30 |
Definition arrow (A B : Type) := A -> B. |
| 31 |
|
| 32 |
Definition flip {A B C : Type} (f : A -> B -> C) := fun x y => f y x. |
| 33 |
|
| 34 |
Definition iffT (A B : Type) := ((A -> B) * (B -> A))%type. |
| 35 |
|
| 36 |
Global Typeclasses Opaque flip arrow iffT. |
| 37 |
|
| 38 |
(** We allow to unfold the [crelation] definition while doing morphism search. *) |
| 39 |
|
| 40 |
Section Defs. |
| 41 |
Context {A : Type}. |
| 42 |
|
| 43 |
(** We rebind crelational properties in separate classes to be able to overload each proof. *) |
| 44 |
|
| 45 |
Class Reflexive (R : crelation A) := |
| 46 |
reflexivity : forall x : A, R x x. |
| 47 |
|
| 48 |
Definition complement (R : crelation A) : crelation A := |
| 49 |
fun x y => R x y -> False. |
| 50 |
|
| 51 |
(** Opaque for proof-search. *) |
| 52 |
Typeclasses Opaque complement iffT. |
| 53 |
|
| 54 |
(** These are convertible. *) |
| 55 |
Lemma complement_inverse R : complement (flip R) = flip (complement R). |
| 56 |
Proof. reflexivity. Qed. |
| 57 |
|
| 58 |
Class Irreflexive (R : crelation A) := |
| 59 |
irreflexivity : Reflexive (complement R). |
| 60 |
|
| 61 |
Class Symmetric (R : crelation A) := |
| 62 |
symmetry : forall {x y}, R x y -> R y x. |
| 63 |
|
| 64 |
Class Asymmetric (R : crelation A) := |
| 65 |
asymmetry : forall {x y}, R x y -> (complement R y x : Type). |
| 66 |
|
| 67 |
Class Transitive (R : crelation A) := |
| 68 |
transitivity : forall {x y z}, R x y -> R y z -> R x z. |
| 69 |
|
| 70 |
(** Various combinations of reflexivity, symmetry and transitivity. *) |
| 71 |
|
| 72 |
(** A [PreOrder] is both Reflexive and Transitive. *) |
| 73 |
|
| 74 |
Class PreOrder (R : crelation A) := { |
| 75 |
#[global] PreOrder_Reflexive :: Reflexive R | 2 ; |
| 76 |
#[global] PreOrder_Transitive :: Transitive R | 2 }. |
| 77 |
|
| 78 |
(** A [StrictOrder] is both Irreflexive and Transitive. *) |
| 79 |
|
| 80 |
Class StrictOrder (R : crelation A) := { |
| 81 |
#[global] StrictOrder_Irreflexive :: Irreflexive R ; |
| 82 |
#[global] StrictOrder_Transitive :: Transitive R }. |
| 83 |
|
| 84 |
(** By definition, a strict order is also asymmetric *) |
| 85 |
Global Instance StrictOrder_Asymmetric `(StrictOrder R) : Asymmetric R. |
| 86 |
Proof. firstorder. Qed. |
| 87 |
|
| 88 |
(** A partial equivalence crelation is Symmetric and Transitive. *) |
| 89 |
|
| 90 |
Class PER (R : crelation A) := { |
| 91 |
#[global] PER_Symmetric :: Symmetric R | 3 ; |
| 92 |
#[global] PER_Transitive :: Transitive R | 3 }. |
| 93 |
|
| 94 |
(** Equivalence crelations. *) |
| 95 |
|
| 96 |
Class Equivalence (R : crelation A) := { |
| 97 |
#[global] Equivalence_Reflexive :: Reflexive R ; |
| 98 |
#[global] Equivalence_Symmetric :: Symmetric R ; |
| 99 |
#[global] Equivalence_Transitive :: Transitive R }. |
| 100 |
|
| 101 |
(** An Equivalence is a PER plus reflexivity. *) |
| 102 |
|
| 103 |
Global Instance Equivalence_PER {R} `(Equivalence R) : PER R | 10 := |
| 104 |
{ PER_Symmetric := Equivalence_Symmetric ; |
| 105 |
PER_Transitive := Equivalence_Transitive }. |
| 106 |
|
| 107 |
(** We can now define antisymmetry w.r.t. an equivalence crelation on the carrier. *) |
| 108 |
|
| 109 |
Class Antisymmetric eqA `{equ : Equivalence eqA} (R : crelation A) := |
| 110 |
antisymmetry : forall {x y}, R x y -> R y x -> eqA x y. |
| 111 |
|
| 112 |
Class subrelation (R R' : crelation A) := |
| 113 |
is_subrelation : forall {x y}, R x y -> R' x y. |
| 114 |
|
| 115 |
(** Any symmetric crelation is equal to its inverse. *) |
| 116 |
|
| 117 |
Lemma subrelation_symmetric R `(Symmetric R) : subrelation (flip R) R. |
| 118 |
Proof. hnf. intros x y H'. red in H'. apply symmetry. assumption. Qed. |
| 119 |
|
| 120 |
Section flip. |
| 121 |
|
| 122 |
Lemma flip_Reflexive `{Reflexive R} : Reflexive (flip R). |
| 123 |
Proof. tauto. Qed. |
| 124 |
|
| 125 |
Program Definition flip_Irreflexive `(Irreflexive R) : Irreflexive (flip R) := |
| 126 |
irreflexivity (R:=R). |
| 127 |
|
| 128 |
Program Definition flip_Symmetric `(Symmetric R) : Symmetric (flip R) := |
| 129 |
fun x y H => symmetry (R:=R) H. |
| 130 |
|
| 131 |
Program Definition flip_Asymmetric `(Asymmetric R) : Asymmetric (flip R) := |
| 132 |
fun x y H H' => asymmetry (R:=R) H H'. |
| 133 |
|
| 134 |
Program Definition flip_Transitive `(Transitive R) : Transitive (flip R) := |
| 135 |
fun x y z H H' => transitivity (R:=R) H' H. |
| 136 |
|
| 137 |
Program Lemma flip_Antisymmetric `(Antisymmetric eqA R) : |
| 138 |
Antisymmetric eqA (flip R). |
| 139 |
Proof. firstorder. Qed. |
| 140 |
|
| 141 |
(** Inversing the larger structures *) |
| 142 |
|
| 143 |
Lemma flip_PreOrder `(PreOrder R) : PreOrder (flip R). |
| 144 |
Proof. firstorder. Qed. |
| 145 |
|
| 146 |
Lemma flip_StrictOrder `(StrictOrder R) : StrictOrder (flip R). |
| 147 |
Proof. firstorder. Qed. |
| 148 |
|
| 149 |
Lemma flip_PER `(PER R) : PER (flip R). |
| 150 |
Proof. firstorder. Qed. |
| 151 |
|
| 152 |
Lemma flip_Equivalence `(Equivalence R) : Equivalence (flip R). |
| 153 |
Proof. firstorder. Qed. |
| 154 |
|
| 155 |
End flip. |
| 156 |
|
| 157 |
Section complement. |
| 158 |
|
| 159 |
Lemma complement_Irreflexive `(Reflexive R) |
| 160 |
: Irreflexive (complement R). |
| 161 |
Proof. firstorder. Qed. |
| 162 |
|
| 163 |
Lemma complement_Symmetric `(Symmetric R) : Symmetric (complement R). |
| 164 |
Proof. firstorder. Qed. |
| 165 |
End complement. |
| 166 |
|
| 167 |
|
| 168 |
(** Rewrite crelation on a given support: declares a crelation as a rewrite |
| 169 |
crelation for use by the generalized rewriting tactic. |
| 170 |
It helps choosing if a rewrite should be handled |
| 171 |
by the generalized or the regular rewriting tactic using leibniz equality. |
| 172 |
Users can declare an [RewriteRelation A RA] anywhere to declare default |
| 173 |
crelations. This is also done automatically by the [Declare Relation A RA] |
| 174 |
commands. *) |
| 175 |
|
| 176 |
Class RewriteRelation (RA : crelation A). |
| 177 |
|
| 178 |
(** Any [Equivalence] declared in the context is automatically considered |
| 179 |
a rewrite crelation. *) |
| 180 |
|
| 181 |
Global Instance equivalence_rewrite_crelation `(Equivalence eqA) : RewriteRelation eqA. |
| 182 |
Defined. |
| 183 |
|
| 184 |
(** Leibniz equality. *) |
| 185 |
Section Leibniz. |
| 186 |
Global Instance eq_Reflexive : Reflexive (@eq A) := @eq_refl A. |
| 187 |
Global Instance eq_Symmetric : Symmetric (@eq A) := @eq_sym A. |
| 188 |
Global Instance eq_Transitive : Transitive (@eq A) := @eq_trans A. |
| 189 |
|
| 190 |
(** Leibinz equality [eq] is an equivalence crelation. |
| 191 |
The instance has low priority as it is always applicable |
| 192 |
if only the type is constrained. *) |
| 193 |
|
| 194 |
Global Program Instance eq_equivalence : Equivalence (@eq A) | 10. |
| 195 |
End Leibniz. |
| 196 |
|
| 197 |
End Defs. |
| 198 |
|
| 199 |
(** Default rewrite crelations handled by [setoid_rewrite]. *) |
| 200 |
#[global] |
| 201 |
Instance: RewriteRelation impl. |
| 202 |
Defined. |
| 203 |
|
| 204 |
#[global] |
| 205 |
Instance: RewriteRelation iff. |
| 206 |
Defined. |
| 207 |
|
| 208 |
(** Hints to drive the typeclass resolution avoiding loops |
| 209 |
due to the use of full unification. *) |
| 210 |
#[global] |
| 211 |
Hint Extern 1 (Reflexive (complement _)) => class_apply @irreflexivity : typeclass_instances. |
| 212 |
#[global] |
| 213 |
Hint Extern 3 (Symmetric (complement _)) => class_apply complement_Symmetric : typeclass_instances. |
| 214 |
#[global] |
| 215 |
Hint Extern 3 (Irreflexive (complement _)) => class_apply complement_Irreflexive : typeclass_instances. |
| 216 |
|
| 217 |
#[global] |
| 218 |
Hint Extern 3 (Reflexive (flip _)) => apply flip_Reflexive : typeclass_instances. |
| 219 |
#[global] |
| 220 |
Hint Extern 3 (Irreflexive (flip _)) => class_apply flip_Irreflexive : typeclass_instances. |
| 221 |
#[global] |
| 222 |
Hint Extern 3 (Symmetric (flip _)) => class_apply flip_Symmetric : typeclass_instances. |
| 223 |
#[global] |
| 224 |
Hint Extern 3 (Asymmetric (flip _)) => class_apply flip_Asymmetric : typeclass_instances. |
| 225 |
#[global] |
| 226 |
Hint Extern 3 (Antisymmetric (flip _)) => class_apply flip_Antisymmetric : typeclass_instances. |
| 227 |
#[global] |
| 228 |
Hint Extern 3 (Transitive (flip _)) => class_apply flip_Transitive : typeclass_instances. |
| 229 |
#[global] |
| 230 |
Hint Extern 3 (StrictOrder (flip _)) => class_apply flip_StrictOrder : typeclass_instances. |
| 231 |
#[global] |
| 232 |
Hint Extern 3 (PreOrder (flip _)) => class_apply flip_PreOrder : typeclass_instances. |
| 233 |
|
| 234 |
#[global] |
| 235 |
Hint Extern 4 (subrelation (flip _) _) => |
| 236 |
class_apply @subrelation_symmetric : typeclass_instances. |
| 237 |
|
| 238 |
#[global] |
| 239 |
Create HintDb ord. |
| 240 |
|
| 241 |
#[global] |
| 242 |
Hint Resolve irreflexivity : ord. |
| 243 |
|
| 244 |
Unset Implicit Arguments. |
| 245 |
|
| 246 |
Ltac solve_crelation := |
| 247 |
match goal with |
| 248 |
| [ |- ?R ?x ?x ] => reflexivity |
| 249 |
| [ H : ?R ?x ?y |- ?R ?y ?x ] => symmetry ; exact H |
| 250 |
end. |
| 251 |
|
| 252 |
#[global] |
| 253 |
Create HintDb crelations. |
| 254 |
|
| 255 |
#[global] |
| 256 |
Hint Extern 4 => solve_crelation : crelations. |
| 257 |
|
| 258 |
(** We can already dualize all these properties. *) |
| 259 |
|
| 260 |
(** * Standard instances. *) |
| 261 |
|
| 262 |
Ltac reduce_hyp H := |
| 263 |
match type of H with |
| 264 |
| context [ _ <-> _ ] => fail 1 |
| 265 |
| _ => red in H ; try reduce_hyp H |
| 266 |
end. |
| 267 |
|
| 268 |
Ltac reduce_goal := |
| 269 |
match goal with |
| 270 |
| [ |- _ <-> _ ] => fail 1 |
| 271 |
| _ => red ; intros ; try reduce_goal |
| 272 |
end. |
| 273 |
|
| 274 |
Tactic Notation "reduce" "in" hyp(Hid) := reduce_hyp Hid. |
| 275 |
|
| 276 |
Ltac reduce := reduce_goal. |
| 277 |
|
| 278 |
Tactic Notation "apply" "*" constr(t) := |
| 279 |
first [ refine t | refine (t _) | refine (t _ _) | refine (t _ _ _) | refine (t _ _ _ _) | |
| 280 |
refine (t _ _ _ _ _) | refine (t _ _ _ _ _ _) | refine (t _ _ _ _ _ _ _) ]. |
| 281 |
|
| 282 |
Ltac simpl_crelation := |
| 283 |
unfold flip, impl, arrow ; try reduce ; program_simpl ; |
| 284 |
try ( solve [ dintuition auto with crelations ]). |
| 285 |
|
| 286 |
Local Obligation Tactic := simpl_crelation. |
| 287 |
|
| 288 |
(** Logical implication. *) |
| 289 |
|
| 290 |
#[global] |
| 291 |
Program Instance impl_Reflexive : Reflexive impl. |
| 292 |
#[global] |
| 293 |
Program Instance impl_Transitive : Transitive impl. |
| 294 |
|
| 295 |
(** Logical equivalence. *) |
| 296 |
|
| 297 |
#[global] |
| 298 |
Instance iff_Reflexive : Reflexive iff := iff_refl. |
| 299 |
#[global] |
| 300 |
Instance iff_Symmetric : Symmetric iff := iff_sym. |
| 301 |
#[global] |
| 302 |
Instance iff_Transitive : Transitive iff := iff_trans. |
| 303 |
|
| 304 |
(** Logical equivalence [iff] is an equivalence crelation. *) |
| 305 |
|
| 306 |
#[global] |
| 307 |
Program Instance iff_equivalence : Equivalence iff. |
| 308 |
#[global] |
| 309 |
Program Instance arrow_Reflexive : Reflexive arrow. |
| 310 |
#[global] |
| 311 |
Program Instance arrow_Transitive : Transitive arrow. |
| 312 |
|
| 313 |
#[global] |
| 314 |
Instance iffT_Reflexive : Reflexive iffT. |
| 315 |
Proof. firstorder. Defined. |
| 316 |
#[global] |
| 317 |
Instance iffT_Symmetric : Symmetric iffT. |
| 318 |
Proof. firstorder. Defined. |
| 319 |
#[global] |
| 320 |
Instance iffT_Transitive : Transitive iffT. |
| 321 |
Proof. firstorder. Defined. |
| 322 |
|
| 323 |
(** We now develop a generalization of results on crelations for arbitrary predicates. |
| 324 |
The resulting theory can be applied to homogeneous binary crelations but also to |
| 325 |
arbitrary n-ary predicates. *) |
| 326 |
|
| 327 |
Local Open Scope list_scope. |
| 328 |
|
| 329 |
(** A compact representation of non-dependent arities, with the codomain singled-out. *) |
| 330 |
|
| 331 |
(** We define the various operations which define the algebra on binary crelations *) |
| 332 |
Section Binary. |
| 333 |
Context {A : Type}. |
| 334 |
|
| 335 |
Definition relation_equivalence : crelation (crelation A) := |
| 336 |
fun R R' => forall x y, iffT (R x y) (R' x y). |
| 337 |
|
| 338 |
Global Instance: RewriteRelation relation_equivalence. |
| 339 |
Defined. |
| 340 |
|
| 341 |
Definition relation_conjunction (R : crelation A) (R' : crelation A) : crelation A := |
| 342 |
fun x y => prod (R x y) (R' x y). |
| 343 |
|
| 344 |
Definition relation_disjunction (R : crelation A) (R' : crelation A) : crelation A := |
| 345 |
fun x y => sum (R x y) (R' x y). |
| 346 |
|
| 347 |
(** Relation equivalence is an equivalence, and subrelation defines a partial order. *) |
| 348 |
|
| 349 |
Global Instance relation_equivalence_equivalence : |
| 350 |
Equivalence relation_equivalence. |
| 351 |
Proof. |
| 352 |
split; red; unfold relation_equivalence, iffT. |
| 353 |
- firstorder. |
| 354 |
- firstorder. |
| 355 |
- intros x y z X X0 x0 y0. specialize (X x0 y0). specialize (X0 x0 y0). firstorder. |
| 356 |
Qed. |
| 357 |
|
| 358 |
Global Instance relation_implication_preorder : PreOrder (@subrelation A). |
| 359 |
Proof. firstorder. Qed. |
| 360 |
|
| 361 |
(** *** Partial Order. |
| 362 |
A partial order is a preorder which is additionally antisymmetric. |
| 363 |
We give an equivalent definition, up-to an equivalence crelation |
| 364 |
on the carrier. *) |
| 365 |
|
| 366 |
Class PartialOrder eqA `{equ : Equivalence A eqA} R `{preo : PreOrder A R} := |
| 367 |
partial_order_equivalence : relation_equivalence eqA (relation_conjunction R (flip R)). |
| 368 |
|
| 369 |
(** The equivalence proof is sufficient for proving that [R] must be a |
| 370 |
morphism for equivalence (see Morphisms). It is also sufficient to |
| 371 |
show that [R] is antisymmetric w.r.t. [eqA] *) |
| 372 |
|
| 373 |
Global Instance partial_order_antisym `(PartialOrder eqA R) : Antisymmetric eqA R. |
| 374 |
Proof with auto. |
| 375 |
reduce_goal. |
| 376 |
firstorder. |
| 377 |
Qed. |
| 378 |
|
| 379 |
Lemma PartialOrder_inverse `(PartialOrder eqA R) : PartialOrder eqA (flip R). |
| 380 |
Proof. |
| 381 |
firstorder. |
| 382 |
Qed. |
| 383 |
End Binary. |
| 384 |
|
| 385 |
#[global] |
| 386 |
Hint Extern 3 (PartialOrder (flip _)) => class_apply PartialOrder_inverse : typeclass_instances. |
| 387 |
|
| 388 |
(** The partial order defined by subrelation and crelation equivalence. *) |
| 389 |
|
| 390 |
(* Program Instance subrelation_partial_order : *) |
| 391 |
(* ! PartialOrder (crelation A) relation_equivalence subrelation. *) |
| 392 |
(* Obligation Tactic := idtac. *) |
| 393 |
|
| 394 |
(* Next Obligation. *) |
| 395 |
(* Proof. *) |
| 396 |
(* intros x. refine (fun x => x). *) |
| 397 |
(* Qed. *) |
| 398 |
|
| 399 |
Global Typeclasses Opaque relation_equivalence. |
| 400 |
|
| 401 |
(* Register bindings for the generalized rewriting tactic *) |
| 402 |
|
| 403 |
Register arrow as rewrite.type.arrow. |
| 404 |
Register flip as rewrite.type.flip. |
| 405 |
Register crelation as rewrite.type.relation. |
| 406 |
Register subrelation as rewrite.type.subrelation. |
| 407 |
Register Reflexive as rewrite.type.Reflexive. |
| 408 |
Register reflexivity as rewrite.type.reflexivity. |
| 409 |
Register Symmetric as rewrite.type.Symmetric. |
| 410 |
Register symmetry as rewrite.type.symmetry. |
| 411 |
Register Transitive as rewrite.type.Transitive. |
| 412 |
Register transitivity as rewrite.type.transitivity. |
| 413 |
Register RewriteRelation as rewrite.type.RewriteRelation. |