Debian Codegraph @

theories/Corelib/Classes/RelationClasses.v

raw
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 Require Import Corelib.Relations.Relation_Definitions.
24
25 Generalizable Variables A B C D R S T U l eqA eqB eqC eqD.
26
27 (** We allow to unfold the [relation] definition while doing morphism search. *)
28
29 Section Defs.
30 Context {A : Type}.
31
32 (** We rebind relational properties in separate classes to be able to overload each proof. *)
33
34 Class Reflexive (R : relation A) :=
35 reflexivity : forall x : A, R x x.
36
37 Definition complement (R : relation A) : relation A := fun x y => R x y -> False.
38
39 (** Opaque for proof-search. *)
40 Typeclasses Opaque complement.
41
42 (** These are convertible. *)
43 Lemma complement_inverse R : complement (flip R) = flip (complement R).
44 Proof. reflexivity. Qed.
45
46 Class Irreflexive (R : relation A) :=
47 irreflexivity : Reflexive (complement R).
48
49 Class Symmetric (R : relation A) :=
50 symmetry : forall {x y}, R x y -> R y x.
51
52 Class Asymmetric (R : relation A) :=
53 asymmetry : forall {x y}, R x y -> R y x -> False.
54
55 Class Transitive (R : relation A) :=
56 transitivity : forall {x y z}, R x y -> R y z -> R x z.
57
58 (** Various combinations of reflexivity, symmetry and transitivity. *)
59
60 (** A [PreOrder] is both Reflexive and Transitive. *)
61
62 Class PreOrder (R : relation A) : Prop := {
63 #[global] PreOrder_Reflexive :: Reflexive R | 2 ;
64 #[global] PreOrder_Transitive :: Transitive R | 2 }.
65
66 (** A [StrictOrder] is both Irreflexive and Transitive. *)
67
68 Class StrictOrder (R : relation A) : Prop := {
69 #[global] StrictOrder_Irreflexive :: Irreflexive R ;
70 #[global] StrictOrder_Transitive :: Transitive R }.
71
72 (** By definition, a strict order is also asymmetric *)
73 Global Instance StrictOrder_Asymmetric `(StrictOrder R) : Asymmetric R.
74 Proof. firstorder. Qed.
75
76 (** A partial equivalence relation is Symmetric and Transitive. *)
77
78 Class PER (R : relation A) : Prop := {
79 #[global] PER_Symmetric :: Symmetric R | 3 ;
80 #[global] PER_Transitive :: Transitive R | 3 }.
81
82 (** Equivalence relations. *)
83
84 Class Equivalence (R : relation A) : Prop := {
85 #[global] Equivalence_Reflexive :: Reflexive R ;
86 #[global] Equivalence_Symmetric :: Symmetric R ;
87 #[global] Equivalence_Transitive :: Transitive R }.
88
89 (** An Equivalence is a PER plus reflexivity. *)
90
91 Global Instance Equivalence_PER {R} `(E:Equivalence R) : PER R | 10 :=
92 { }.
93
94 (** An Equivalence is a PreOrder plus symmetry. *)
95
96 Global Instance Equivalence_PreOrder {R} `(E:Equivalence R) : PreOrder R | 10 :=
97 { }.
98
99 (** We can now define antisymmetry w.r.t. an equivalence relation on the carrier. *)
100
101 Class Antisymmetric eqA `{equ : Equivalence eqA} (R : relation A) :=
102 antisymmetry : forall {x y}, R x y -> R y x -> eqA x y.
103
104 Class subrelation (R R' : relation A) : Prop :=
105 is_subrelation : forall {x y}, R x y -> R' x y.
106
107 (** Any symmetric relation is equal to its inverse. *)
108
109 Lemma subrelation_symmetric R `(Symmetric R) : subrelation (flip R) R.
110 Proof. hnf. intros x y H0. red in H0. apply symmetry. assumption. Qed.
111
112 Section flip.
113
114 Lemma flip_Reflexive `{Reflexive R} : Reflexive (flip R).
115 Proof. tauto. Qed.
116
117 Program Definition flip_Irreflexive `(Irreflexive R) : Irreflexive (flip R) :=
118 irreflexivity (R:=R).
119
120 Program Definition flip_Symmetric `(Symmetric R) : Symmetric (flip R) :=
121 fun x y H => symmetry (R:=R) H.
122
123 Program Definition flip_Asymmetric `(Asymmetric R) : Asymmetric (flip R) :=
124 fun x y H H' => asymmetry (R:=R) H H'.
125
126 Program Definition flip_Transitive `(Transitive R) : Transitive (flip R) :=
127 fun x y z H H' => transitivity (R:=R) H' H.
128
129 Program Lemma flip_Antisymmetric `(Antisymmetric eqA R) :
130 Antisymmetric eqA (flip R).
131 Proof. firstorder. Qed.
132
133 (** Inversing the larger structures *)
134
135 Lemma flip_PreOrder `(PreOrder R) : PreOrder (flip R).
136 Proof. firstorder. Qed.
137
138 Lemma flip_StrictOrder `(StrictOrder R) : StrictOrder (flip R).
139 Proof. firstorder. Qed.
140
141 Lemma flip_PER `(PER R) : PER (flip R).
142 Proof. firstorder. Qed.
143
144 Lemma flip_Equivalence `(Equivalence R) : Equivalence (flip R).
145 Proof. firstorder. Qed.
146
147 End flip.
148
149 Section complement.
150
151 Lemma complement_Irreflexive `(Reflexive R)
152 : Irreflexive (complement R).
153 Proof. firstorder. Qed.
154
155 Lemma complement_Symmetric `(Symmetric R) : Symmetric (complement R).
156 Proof. firstorder. Qed.
157 End complement.
158
159
160 (** Rewrite relation on a given support: declares a relation as a rewrite
161 relation for use by the generalized rewriting tactic.
162 It helps choosing if a rewrite should be handled
163 by the generalized or the regular rewriting tactic using leibniz equality.
164 Users can declare an [RewriteRelation A RA] anywhere to declare default
165 relations on a given type `A`. This is also done automatically by
166 the [Declare Relation A RA] commands. It has no mode declaration:
167 it will assign `?A := Prop, ?R := iff` on an entirely unspecified query
168 `RewriteRelation ?A ?R`, or any prefered rewrite relation of priority < 2. *)
169
170 Class RewriteRelation (RA : relation A).
171
172 (** Leibniz equality. *)
173 Section Leibniz.
174 Global Instance eq_Reflexive : Reflexive (@eq A) := @eq_refl A.
175 Global Instance eq_Symmetric : Symmetric (@eq A) := @eq_sym A.
176 Global Instance eq_Transitive : Transitive (@eq A) := @eq_trans A.
177
178 (** Leibinz equality [eq] is an equivalence relation.
179 The instance has low priority as it is always applicable
180 if only the type is constrained. *)
181
182 Global Program Instance eq_equivalence : Equivalence (@eq A) | 10.
183 End Leibniz.
184
185 (** Leibniz disequality. *)
186 Section LeibnizNot.
187 (** Disequality is symmetric. *)
188 Global Instance neq_Symmetric : Symmetric (fun x y : A => x <> y) := (@not_eq_sym A).
189 End LeibnizNot.
190 End Defs.
191
192 (** Default rewrite relations handled by [setoid_rewrite] on Prop. *)
193 #[global]
194 Instance inverse_impl_rewrite_relation : RewriteRelation (flip impl) | 3 := {}.
195 #[global]
196 Instance impl_rewrite_relation : RewriteRelation impl | 3 := {}.
197 #[global]
198 Instance iff_rewrite_relation : RewriteRelation iff | 2 := {}.
199
200 (** Any [Equivalence] declared in the context is automatically considered
201 a rewrite relation. This only applies if the relation is at least partially
202 defined: setoid_rewrite won't try to infer arbitrary user rewrite relations. *)
203
204 Definition equivalence_rewrite_relation `(eqa : Equivalence A eqA) : RewriteRelation eqA :=
205 Build_RewriteRelation _.
206
207 Ltac equiv_rewrite_relation R :=
208 tryif is_evar R then fail
209 else class_apply equivalence_rewrite_relation.
210
211 #[global]
212 Hint Extern 10 (@RewriteRelation ?A ?R) => equiv_rewrite_relation R : typeclass_instances.
213
214 (** Hints to drive the typeclass resolution avoiding loops
215 due to the use of full unification. *)
216 #[global]
217 Hint Extern 1 (Reflexive (complement _)) => class_apply @irreflexivity : typeclass_instances.
218 #[global]
219 Hint Extern 3 (Symmetric (complement _)) => class_apply complement_Symmetric : typeclass_instances.
220 #[global]
221 Hint Extern 3 (Irreflexive (complement _)) => class_apply complement_Irreflexive : typeclass_instances.
222
223 #[global]
224 Hint Extern 3 (Reflexive (flip _)) => apply flip_Reflexive : typeclass_instances.
225 #[global]
226 Hint Extern 3 (Irreflexive (flip _)) => class_apply flip_Irreflexive : typeclass_instances.
227 #[global]
228 Hint Extern 3 (Symmetric (flip _)) => class_apply flip_Symmetric : typeclass_instances.
229 #[global]
230 Hint Extern 3 (Asymmetric (flip _)) => class_apply flip_Asymmetric : typeclass_instances.
231 #[global]
232 Hint Extern 3 (Antisymmetric (flip _)) => class_apply flip_Antisymmetric : typeclass_instances.
233 #[global]
234 Hint Extern 3 (Transitive (flip _)) => class_apply flip_Transitive : typeclass_instances.
235 #[global]
236 Hint Extern 3 (StrictOrder (flip _)) => class_apply flip_StrictOrder : typeclass_instances.
237 #[global]
238 Hint Extern 3 (PreOrder (flip _)) => class_apply flip_PreOrder : typeclass_instances.
239
240 #[global]
241 Hint Extern 4 (subrelation (flip _) _) =>
242 class_apply @subrelation_symmetric : typeclass_instances.
243
244 Arguments irreflexivity {A R Irreflexive} [x] _ : rename.
245 Arguments symmetry {A} {R} {_} [x] [y] _.
246 Arguments asymmetry {A} {R} {_} [x] [y] _ _.
247 Arguments transitivity {A} {R} {_} [x] [y] [z] _ _.
248 Arguments Antisymmetric A eqA {_} _.
249
250 #[global]
251 Create HintDb ord.
252
253 #[global]
254 Hint Resolve irreflexivity : ord.
255
256 Unset Implicit Arguments.
257
258 Ltac solve_relation :=
259 match goal with
260 | [ |- ?R ?x ?x ] => reflexivity
261 | [ H : ?R ?x ?y |- ?R ?y ?x ] => symmetry ; exact H
262 end.
263
264 #[global]
265 Create HintDb relations.
266
267 #[global]
268 Hint Extern 4 => solve_relation : relations.
269
270 (** We can already dualize all these properties. *)
271
272 (** * Standard instances. *)
273
274 Ltac reduce_hyp H :=
275 match type of H with
276 | context [ _ <-> _ ] => fail 1
277 | _ => red in H ; try reduce_hyp H
278 end.
279
280 Ltac reduce_goal :=
281 match goal with
282 | [ |- _ <-> _ ] => fail 1
283 | _ => red ; intros ; try reduce_goal
284 end.
285
286 Tactic Notation "reduce" "in" hyp(Hid) := reduce_hyp Hid.
287
288 Ltac reduce := reduce_goal.
289
290 Tactic Notation "apply" "*" constr(t) :=
291 first [ refine t | refine (t _) | refine (t _ _) | refine (t _ _ _) | refine (t _ _ _ _) |
292 refine (t _ _ _ _ _) | refine (t _ _ _ _ _ _) | refine (t _ _ _ _ _ _ _) ].
293
294 Ltac simpl_relation :=
295 unfold flip, impl, arrow ; try reduce ; program_simpl ;
296 try ( solve [ dintuition auto with relations ]).
297
298 Local Obligation Tactic := try solve [ simpl_relation ].
299
300 (** Logical implication. *)
301
302 #[global]
303 Program Instance impl_Reflexive : Reflexive impl.
304 #[global]
305 Program Instance impl_Transitive : Transitive impl.
306
307 (** Logical equivalence. *)
308
309 #[global]
310 Instance iff_Reflexive : Reflexive iff := iff_refl.
311 #[global]
312 Instance iff_Symmetric : Symmetric iff := iff_sym.
313 #[global]
314 Instance iff_Transitive : Transitive iff := iff_trans.
315
316 (** Logical equivalence [iff] is an equivalence relation. *)
317
318 #[global]
319 Program Instance iff_equivalence : Equivalence iff.
320
321 (** We now develop a generalization of results on relations for arbitrary predicates.
322 The resulting theory can be applied to homogeneous binary relations but also to
323 arbitrary n-ary predicates. *)
324
325 Local Open Scope list_scope.
326
327 (** A compact representation of non-dependent arities, with the codomain singled-out. *)
328
329 (* Note, we do not use [list Type] because it imposes unnecessary universe constraints *)
330 Inductive Tlist : Type := Tnil : Tlist | Tcons : Type -> Tlist -> Tlist.
331 Local Infix "::" := Tcons.
332
333 Fixpoint arrows (l : Tlist) (r : Type) : Type :=
334 match l with
335 | Tnil => r
336 | A :: l' => A -> arrows l' r
337 end.
338
339 (** We can define abbreviations for operation and relation types based on [arrows]. *)
340
341 Definition unary_operation A := arrows (A::Tnil) A.
342 Definition binary_operation A := arrows (A::A::Tnil) A.
343 Definition ternary_operation A := arrows (A::A::A::Tnil) A.
344
345 (** We define n-ary [predicate]s as functions into [Prop]. *)
346
347 Abbreviation predicate l := (arrows l Prop).
348
349 (** Unary predicates, or sets. *)
350
351 Definition unary_predicate A := predicate (A::Tnil).
352
353 (** Homogeneous binary relations, equivalent to [relation A]. *)
354
355 Definition binary_relation A := predicate (A::A::Tnil).
356
357 (** We can close a predicate by universal or existential quantification. *)
358
359 Fixpoint predicate_all (l : Tlist) : predicate l -> Prop :=
360 match l with
361 | Tnil => fun f => f
362 | A :: tl => fun f => forall x : A, predicate_all tl (f x)
363 end.
364
365 Fixpoint predicate_exists (l : Tlist) : predicate l -> Prop :=
366 match l with
367 | Tnil => fun f => f
368 | A :: tl => fun f => exists x : A, predicate_exists tl (f x)
369 end.
370
371 (** Pointwise extension of a binary operation on [T] to a binary operation
372 on functions whose codomain is [T].
373 For an operator on [Prop] this lifts the operator to a binary operation. *)
374
375 Fixpoint pointwise_extension {T : Type} (op : binary_operation T)
376 (l : Tlist) : binary_operation (arrows l T) :=
377 match l with
378 | Tnil => fun R R' => op R R'
379 | A :: tl => fun R R' =>
380 fun x => pointwise_extension op tl (R x) (R' x)
381 end.
382
383 (** Pointwise lifting, equivalent to doing [pointwise_extension] and closing using [predicate_all]. *)
384
385 Fixpoint pointwise_lifting (op : binary_relation Prop) (l : Tlist) : binary_relation (predicate l) :=
386 match l with
387 | Tnil => fun R R' => op R R'
388 | A :: tl => fun R R' =>
389 forall x, pointwise_lifting op tl (R x) (R' x)
390 end.
391
392 (** The n-ary equivalence relation, defined by lifting the 0-ary [iff] relation. *)
393
394 Definition predicate_equivalence {l : Tlist} : binary_relation (predicate l) :=
395 pointwise_lifting iff l.
396
397 (** The n-ary implication relation, defined by lifting the 0-ary [impl] relation. *)
398
399 Definition predicate_implication {l : Tlist} :=
400 pointwise_lifting impl l.
401
402 (** Notations for pointwise equivalence and implication of predicates. *)
403
404 Declare Scope predicate_scope.
405
406 Infix "<∙>" := predicate_equivalence (at level 95, no associativity) : predicate_scope.
407 Infix "-∙>" := predicate_implication (at level 70, right associativity) : predicate_scope.
408
409 Local Open Scope predicate_scope.
410
411 (** The pointwise liftings of conjunction and disjunctions.
412 Note that these are [binary_operation]s, building new relations out of old ones. *)
413
414 Definition predicate_intersection := pointwise_extension and.
415 Definition predicate_union := pointwise_extension or.
416
417 Infix "/∙\" := predicate_intersection (at level 80, right associativity) : predicate_scope.
418 Infix "\∙/" := predicate_union (at level 85, right associativity) : predicate_scope.
419
420 (** The always [True] and always [False] predicates. *)
421
422 Fixpoint true_predicate {l : Tlist} : predicate l :=
423 match l with
424 | Tnil => True
425 | A :: tl => fun _ => @true_predicate tl
426 end.
427
428 Fixpoint false_predicate {l : Tlist} : predicate l :=
429 match l with
430 | Tnil => False
431 | A :: tl => fun _ => @false_predicate tl
432 end.
433
434 Notation "∙⊤∙" := true_predicate : predicate_scope.
435 Notation "∙⊥∙" := false_predicate : predicate_scope.
436
437 (** Predicate equivalence is an equivalence, and predicate implication defines a preorder. *)
438
439 #[global]
440 Program Instance predicate_equivalence_equivalence {l} :
441 Equivalence (@predicate_equivalence l).
442
443 Next Obligation.
444 intro l; induction l ; firstorder.
445 Qed.
446 Next Obligation.
447 intro l; induction l ; firstorder.
448 Qed.
449 Next Obligation.
450 intro l.
451 fold pointwise_lifting.
452 induction l as [|T l IHl].
453 - firstorder.
454 - intros x y z H H0 x0. pose (IHl (x x0) (y x0) (z x0)).
455 firstorder.
456 Qed.
457
458 #[global]
459 Program Instance predicate_implication_preorder {l} :
460 PreOrder (@predicate_implication l).
461 Next Obligation.
462 intro l; induction l ; firstorder.
463 Qed.
464 Next Obligation.
465 intro l.
466 induction l as [|T l IHl].
467 - firstorder.
468 - intros x y z H H0 x0. pose (IHl (x x0) (y x0) (z x0)). firstorder.
469 Qed.
470
471 (** We define the various operations which define the algebra on binary relations,
472 from the general ones. *)
473
474 Section Binary.
475 Context {A : Type}.
476
477 Definition relation_equivalence : relation (relation A) :=
478 @predicate_equivalence (_::_::Tnil).
479
480 Global Instance relation_equivalence_rewrite_relation: RewriteRelation relation_equivalence := {}.
481
482 Definition relation_conjunction (R : relation A) (R' : relation A) : relation A :=
483 @predicate_intersection (A::A::Tnil) R R'.
484
485 Definition relation_disjunction (R : relation A) (R' : relation A) : relation A :=
486 @predicate_union (A::A::Tnil) R R'.
487
488 (** Relation equivalence is an equivalence, and subrelation defines a partial order. *)
489
490 Global Instance relation_equivalence_equivalence :
491 Equivalence relation_equivalence.
492 Proof. exact (@predicate_equivalence_equivalence (A::A::Tnil)). Qed.
493
494 Global Instance relation_implication_preorder : PreOrder (@subrelation A).
495 Proof. exact (@predicate_implication_preorder (A::A::Tnil)). Qed.
496
497 (** *** Partial Order.
498 A partial order is a preorder which is additionally antisymmetric.
499 We give an equivalent definition, up-to an equivalence relation
500 on the carrier. *)
501
502 Class PartialOrder eqA `{equ : Equivalence A eqA} R `{preo : PreOrder A R} :=
503 partial_order_equivalence : relation_equivalence eqA (relation_conjunction R (flip R)).
504
505 (** The equivalence proof is sufficient for proving that [R] must be a
506 morphism for equivalence (see Morphisms). It is also sufficient to
507 show that [R] is antisymmetric w.r.t. [eqA] *)
508
509 Global Instance partial_order_antisym `(PartialOrder eqA R) : Antisymmetric A eqA R.
510 Proof with auto.
511 reduce_goal.
512 pose proof partial_order_equivalence as poe. do 3 red in poe.
513 apply <- poe. firstorder.
514 Qed.
515
516
517 Lemma PartialOrder_inverse `(PartialOrder eqA R) : PartialOrder eqA (flip R).
518 Proof. firstorder. Qed.
519 End Binary.
520
521 #[global]
522 Hint Extern 3 (PartialOrder (flip _)) => class_apply PartialOrder_inverse : typeclass_instances.
523
524 (** The partial order defined by subrelation and relation equivalence. *)
525
526 #[global]
527 Program Instance subrelation_partial_order {A} :
528 PartialOrder (@relation_equivalence A) subrelation.
529
530 Next Obligation.
531 Proof.
532 unfold relation_equivalence in *. compute; firstorder.
533 Qed.
534
535 Global Typeclasses Opaque arrows predicate_implication predicate_equivalence
536 relation_equivalence pointwise_lifting.
537
538 (* Register bindings for the generalized rewriting tactic *)
539
540 Register relation as rewrite.prop.relation.
541 Register subrelation as rewrite.prop.subrelation.
542 Register Reflexive as rewrite.prop.Reflexive.
543 Register reflexivity as rewrite.prop.reflexivity.
544 Register Symmetric as rewrite.prop.Symmetric.
545 Register symmetry as rewrite.prop.symmetry.
546 Register Transitive as rewrite.prop.Transitive.
547 Register transitivity as rewrite.prop.transitivity.