Debian Codegraph @

theories/Corelib/Classes/CMorphisms.v

raw
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 morphism definition and standard, minimal instances
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 Require Export Corelib.Classes.CRelationClasses.
20
21 Generalizable Variables A eqA B C D R RA RB RC m f x y.
22 Local Obligation Tactic := try solve [ simpl_crelation ].
23
24 Local Arguments transitivity {A R Transitive x} y {z}.
25
26 Set Universe Polymorphism.
27
28 (** * Morphisms.
29
30 We now turn to the definition of [Proper] and declare standard instances.
31 These will be used by the [setoid_rewrite] tactic later. *)
32
33 (** A morphism for a relation [R] is a proper element of the relation.
34 The relation [R] will be instantiated by [respectful] and [A] by an arrow
35 type for usual morphisms. *)
36 Section Proper.
37 Context {A : Type}.
38
39 Class Proper (R : crelation A) (m : A) :=
40 proper_prf : R m m.
41
42 (** Every element in the carrier of a reflexive relation is a morphism
43 for this relation. We use a proxy class for this case which is used
44 internally to discharge reflexivity constraints. The [Reflexive]
45 instance will almost always be used, but it won't apply in general to
46 any kind of [Proper (A -> B) _ _] goal, making proof-search much
47 slower. A cleaner solution would be to be able to set different
48 priorities in different hint bases and select a particular hint
49 database for resolution of a type class constraint. *)
50
51 Class ProperProxy (R : crelation A) (m : A) :=
52 proper_proxy : R m m.
53
54 Lemma eq_proper_proxy (x : A) : ProperProxy (@eq A) x.
55 Proof. firstorder. Qed.
56
57 Lemma reflexive_proper_proxy `(Reflexive A R) (x : A) : ProperProxy R x.
58 Proof. firstorder. Qed.
59
60 Lemma proper_proper_proxy x `(Proper R x) : ProperProxy R x.
61 Proof. firstorder. Qed.
62
63 (** Respectful morphisms. *)
64
65 (** The fully dependent version, not used yet. *)
66
67 Definition respectful_hetero
68 (A B : Type)
69 (C : A -> Type) (D : B -> Type)
70 (R : A -> B -> Type)
71 (R' : forall (x : A) (y : B), C x -> D y -> Type) :
72 (forall x : A, C x) -> (forall x : B, D x) -> Type :=
73 fun f g => forall x y, R x y -> R' x y (f x) (g y).
74
75 (** The non-dependent version is an instance where we forget dependencies. *)
76
77 Definition respectful {B} (R : crelation A) (R' : crelation B) : crelation (A -> B) :=
78 Eval compute in @respectful_hetero A A (fun _ => B) (fun _ => B) R (fun _ _ => R').
79 End Proper.
80
81 (** We favor the use of Leibniz equality or a declared reflexive crelation
82 when resolving [ProperProxy], otherwise, if the crelation is given (not an evar),
83 we fall back to [Proper]. *)
84 #[global]
85 Hint Extern 1 (ProperProxy _ _) =>
86 class_apply @eq_proper_proxy || class_apply @reflexive_proper_proxy : typeclass_instances.
87
88 #[global]
89 Hint Extern 2 (ProperProxy ?R _) =>
90 not_evar R; class_apply @proper_proper_proxy : typeclass_instances.
91
92 (** Notations reminiscent of the old syntax for declaring morphisms. *)
93 Declare Scope signatureT_scope.
94 Delimit Scope signatureT_scope with signatureT.
95
96 Module ProperNotations.
97
98 Notation " R ++> R' " := (@respectful _ _ (R%signatureT) (R'%signatureT))
99 (right associativity, at level 55) : signatureT_scope.
100
101 Notation " R ==> R' " := (@respectful _ _ (R%signatureT) (R'%signatureT))
102 (right associativity, at level 55) : signatureT_scope.
103
104 Notation " R --> R' " := (@respectful _ _ (flip (R%signatureT)) (R'%signatureT))
105 (right associativity, at level 55) : signatureT_scope.
106
107 End ProperNotations.
108
109 Arguments Proper {A}%_type R%_signatureT m.
110 Arguments respectful {A B}%_type (R R')%_signatureT _ _.
111
112 Export ProperNotations.
113
114 Local Open Scope signatureT_scope.
115
116 (** [solve_proper] try to solve the goal [Proper (?==> ... ==>?) f]
117 by repeated introductions and setoid rewrites. It should work
118 fine when [f] is a combination of already known morphisms and
119 quantifiers. *)
120
121 Ltac solve_respectful t :=
122 match goal with
123 | |- respectful _ _ _ _ =>
124 let H := fresh "H" in
125 intros ? ? H; solve_respectful ltac:(setoid_rewrite H; t)
126 | _ => t; reflexivity
127 end.
128
129 Ltac solve_proper := unfold Proper; solve_respectful ltac:(idtac).
130
131 (** [f_equiv] is a clone of [f_equal] that handles setoid equivalences.
132 For example, if we know that [f] is a morphism for [E1==>E2==>E],
133 then the goal [E (f x y) (f x' y')] will be transformed by [f_equiv]
134 into the subgoals [E1 x x'] and [E2 y y'].
135 *)
136
137 Ltac f_equiv :=
138 match goal with
139 | |- ?R (?f ?x) (?f' _) =>
140 let T := type of x in
141 let Rx := fresh "R" in
142 evar (Rx : crelation T);
143 let H := fresh in
144 assert (H : (Rx==>R)%signatureT f f');
145 unfold Rx in *; clear Rx; [ f_equiv | apply H; clear H; try reflexivity ]
146 | |- ?R ?f ?f' =>
147 solve [change (Proper R f); eauto with typeclass_instances | reflexivity ]
148 | _ => idtac
149 end.
150
151 Section Relations.
152 Context {A : Type}.
153
154 (** [forall_def] reifies the dependent product as a definition. *)
155
156 Definition forall_def (P : A -> Type) : Type := forall x : A, P x.
157
158 (** Dependent pointwise lifting of a crelation on the range. *)
159
160 Definition forall_relation (P : A -> Type)
161 (sig : forall a, crelation (P a)) : crelation (forall x, P x) :=
162 fun f g => forall a, sig a (f a) (g a).
163
164 (** Non-dependent pointwise lifting *)
165 Definition pointwise_relation {B} (R : crelation B) : crelation (A -> B) :=
166 fun f g => forall a, R (f a) (g a).
167
168 Lemma pointwise_pointwise {B} (R : crelation B) :
169 relation_equivalence (pointwise_relation R) (@eq A ==> R).
170 Proof.
171 intros. split.
172 - simpl_crelation.
173 - firstorder.
174 Qed.
175
176 (** Subcrelations induce a morphism on the identity. *)
177
178 Global Instance subrelation_id_proper `(subrelation A RA RA') : Proper (RA ==> RA') id.
179 Proof. firstorder. Qed.
180
181 (** The subrelation property goes through products as usual. *)
182
183 Lemma subrelation_respectful `(subl : subrelation A RA' RA, subr : subrelation B RB RB') :
184 subrelation (RA ==> RB) (RA' ==> RB').
185 Proof. simpl_crelation. Qed.
186
187 (** And of course it is reflexive. *)
188
189 Lemma subrelation_refl R : @subrelation A R R.
190 Proof. simpl_crelation. Qed.
191
192 (** [Proper] is itself a covariant morphism for [subrelation].
193 We use an unconvertible premise to avoid looping.
194 *)
195
196 Lemma subrelation_proper `(mor : Proper A R' m)
197 `(unc : Unconvertible (crelation A) R R')
198 `(sub : subrelation A R' R) : Proper R m.
199 Proof.
200 intros. apply sub. apply mor.
201 Qed.
202
203 Global Instance proper_subrelation_proper_arrow :
204 Proper (subrelation ++> eq ==> arrow) (@Proper A).
205 Proof. reduce. subst. firstorder. Qed.
206
207 Global Instance pointwise_subrelation `(sub : subrelation B R R') :
208 subrelation (pointwise_relation R) (pointwise_relation R') | 4.
209 Proof. reduce. unfold pointwise_relation in *. apply sub. auto. Qed.
210
211 (** For dependent function types. *)
212 Lemma forall_subrelation (P : A -> Type) (R S : forall x : A, crelation (P x)) :
213 (forall a, subrelation (R a) (S a)) ->
214 subrelation (forall_relation P R) (forall_relation P S).
215 Proof. reduce. firstorder. Qed.
216 End Relations.
217 Global Typeclasses Opaque respectful pointwise_relation forall_relation.
218 Arguments forall_relation {A P}%_type sig%_signatureT _ _.
219 Arguments pointwise_relation A%_type {B}%_type R%_signatureT _ _.
220
221 #[global]
222 Hint Unfold Reflexive : core.
223 #[global]
224 Hint Unfold Symmetric : core.
225 #[global]
226 Hint Unfold Transitive : core.
227
228 (** Resolution with subrelation: favor decomposing products over applying reflexivity
229 for unconstrained goals. *)
230 Ltac subrelation_tac T U :=
231 (is_ground T ; is_ground U ; class_apply @subrelation_refl) ||
232 class_apply @subrelation_respectful || class_apply @subrelation_refl.
233
234 #[global]
235 Hint Extern 3 (@subrelation _ ?T ?U) => subrelation_tac T U : typeclass_instances.
236
237 CoInductive apply_subrelation : Prop := do_subrelation.
238
239 Ltac proper_subrelation :=
240 match goal with
241 [ H : apply_subrelation |- _ ] => clear H ; class_apply @subrelation_proper
242 end.
243
244 #[global]
245 Hint Extern 5 (@Proper _ ?H _) => proper_subrelation : typeclass_instances.
246
247 (** Essential subrelation instances for [iff], [impl] and [pointwise_relation]. *)
248
249 #[global]
250 Instance iff_impl_subrelation : subrelation iff impl | 2.
251 Proof. firstorder. Qed.
252
253 #[global]
254 Instance iff_flip_impl_subrelation : subrelation iff (flip impl) | 2.
255 Proof. firstorder. Qed.
256
257 (** Essential subrelation instances for [iffT] and [arrow]. *)
258
259 #[global]
260 Instance iffT_arrow_subrelation : subrelation iffT arrow | 2.
261 Proof. firstorder. Qed.
262
263 #[global]
264 Instance iffT_flip_arrow_subrelation : subrelation iffT (flip arrow) | 2.
265 Proof. firstorder. Qed.
266
267 (** We use an extern hint to help unification. *)
268
269 #[global]
270 Hint Extern 4 (subrelation (@forall_relation ?A ?B ?R) (@forall_relation _ _ ?S)) =>
271 apply (@forall_subrelation A B R S) ; intro : typeclass_instances.
272
273 Section GenericInstances.
274 (* Share universes *)
275 Implicit Types A B C : Type.
276
277 (** We can build a PER on the Rocq function space if we have PERs on the domain and
278 codomain. *)
279
280 Program Instance respectful_per `(PER A R, PER B R') : PER (R ==> R').
281
282 Next Obligation.
283 Proof.
284 intros A R H B R' H0 x y z X X0 x0 y0 X1.
285 assert(R x0 x0).
286 - eapply transitivity with y0; auto. now apply symmetry.
287 - eapply transitivity with (y x0); auto.
288 Qed.
289
290 Unset Strict Universe Declaration.
291
292 (** The complement of a crelation conserves its proper elements. *)
293
294 (** The [flip] too, actually the [flip] instance is a bit more general. *)
295 Program Definition flip_proper
296 `(mor : Proper (A -> B -> C) (RA ==> RB ==> RC) f) :
297 Proper (RB ==> RA ==> RC) (flip f) := _.
298
299 Next Obligation.
300 Proof.
301 intros A B C RA RB RC f mor x y X x0 y0 X0.
302 apply mor ; auto.
303 Qed.
304
305
306 (** Every Transitive crelation gives rise to a binary morphism on [impl],
307 contravariant in the first argument, covariant in the second. *)
308
309 Global Program
310 Instance trans_contra_co_type_morphism
311 `(Transitive A R) : Proper (R --> R ++> arrow) R.
312
313 Next Obligation.
314 Proof.
315 intros A R H x y X x0 y0 X0 X1.
316 apply transitivity with x; auto.
317 apply transitivity with x0; auto.
318 Qed.
319
320 (** Proper declarations for partial applications. *)
321
322 Global Program
323 Instance trans_contra_inv_impl_type_morphism
324 `(Transitive A R) {x} : Proper (R --> flip arrow) (R x) | 3.
325
326 Next Obligation.
327 Proof.
328 intros A R H x x0 y X X0.
329 apply transitivity with y; auto.
330 Qed.
331
332 Global Program
333 Instance trans_co_impl_type_morphism
334 `(Transitive A R) {x} : Proper (R ++> arrow) (R x) | 3.
335
336 Next Obligation.
337 Proof.
338 intros A R H x x0 y X X0.
339 apply transitivity with x0; auto.
340 Qed.
341
342 Global Program
343 Instance trans_sym_co_inv_impl_type_morphism
344 `(PER A R) {x} : Proper (R ++> flip arrow) (R x) | 3.
345
346 Next Obligation.
347 Proof.
348 intros A R H x x0 y X X0.
349 apply transitivity with y; auto. apply symmetry; auto.
350 Qed.
351
352 Global Program Instance trans_sym_contra_arrow_morphism
353 `(PER A R) {x} : Proper (R --> arrow) (R x) | 3.
354
355 Next Obligation.
356 Proof.
357 intros A R H x x0 y X X0.
358 apply transitivity with x0; auto. apply symmetry; auto.
359 Qed.
360
361 Global Program Instance per_partial_app_type_morphism
362 `(PER A R) {x} : Proper (R ==> iffT) (R x) | 2.
363
364 Next Obligation.
365 Proof.
366 intros A R H x x0 y X.
367 split.
368 - intros ; apply transitivity with x0; auto.
369 - intros.
370 apply transitivity with y; auto.
371 apply symmetry; auto.
372 Qed.
373
374 (** Every Transitive crelation induces a morphism by "pushing" an [R x y] on the left of an [R x z] proof to get an [R y z] goal. *)
375
376 Global Program
377 Instance trans_co_eq_inv_arrow_morphism
378 `(Transitive A R) : Proper (R ==> (@eq A) ==> flip arrow) R | 2.
379
380 Next Obligation.
381 Proof.
382 intros A R H x y X y0 y1 e X0; destruct e.
383 apply transitivity with y; auto.
384 Qed.
385
386 (** Every Symmetric and Transitive crelation gives rise to an equivariant morphism. *)
387
388 Global Program
389 Instance PER_type_morphism `(PER A R) : Proper (R ==> R ==> iffT) R | 1.
390
391 Next Obligation.
392 Proof.
393 intros A R H x y X x0 y0 X0.
394 split ; intros.
395 - apply transitivity with x0; auto.
396 apply transitivity with x; auto. apply symmetry; auto.
397
398 - apply transitivity with y; auto. apply transitivity with y0; auto.
399 apply symmetry; auto.
400 Qed.
401
402 Lemma symmetric_equiv_flip `(Symmetric A R) : relation_equivalence R (flip R).
403 Proof. firstorder. Qed.
404
405 Global Program Instance compose_proper A B C RA RB RC :
406 Proper ((RB ==> RC) ==> (RA ==> RB) ==> (RA ==> RC)) (@compose A B C).
407
408 Next Obligation.
409 Proof.
410 simpl_crelation.
411 unfold compose. firstorder.
412 Qed.
413
414 (** Rocq functions are morphisms for Leibniz equality,
415 applied only if really needed. *)
416
417 Global Instance reflexive_eq_dom_reflexive `(Reflexive B R') {A} :
418 Reflexive (@Logic.eq A ==> R').
419 Proof. simpl_crelation. Qed.
420
421 (** [respectful] is a morphism for crelation equivalence . *)
422
423 Global Instance respectful_morphism {A B} :
424 Proper (relation_equivalence ++> relation_equivalence ++> relation_equivalence)
425 (@respectful A B).
426 Proof.
427 intros R R' HRR' S S' HSS' f g.
428 unfold respectful , relation_equivalence in *; simpl in *.
429 split ; intros H x y Hxy.
430 - apply (fst (HSS' _ _)). apply H. now apply (snd (HRR' _ _)).
431 - apply (snd (HSS' _ _)). apply H. now apply (fst (HRR' _ _)).
432 Qed.
433
434 (** [R] is Reflexive, hence we can build the needed proof. *)
435
436 Lemma Reflexive_partial_app_morphism `(Proper (A -> B) (R ==> R') m, ProperProxy A R x) :
437 Proper R' (m x).
438 Proof. simpl_crelation. Qed.
439
440 Class Params {A} (of : A) (arity : nat).
441
442 Lemma flip_respectful {A B} (R : crelation A) (R' : crelation B) :
443 relation_equivalence (flip (R ==> R')) (flip R ==> flip R').
444 Proof.
445 intros.
446 unfold flip, respectful.
447 split ; intros ; intuition.
448 Qed.
449
450
451 (** Treating flip: can't make them direct instances as we
452 need at least a [flip] present in the goal. *)
453
454 Lemma flip1 `(subrelation A R' R) : subrelation (flip (flip R')) R.
455 Proof. firstorder. Qed.
456
457 Lemma flip2 `(subrelation A R R') : subrelation R (flip (flip R')).
458 Proof. firstorder. Qed.
459
460 (** That's if and only if *)
461
462 Lemma eq_subrelation `(Reflexive A R) : subrelation (@eq A) R.
463 Proof. simpl_crelation. Qed.
464
465 (** Once we have normalized, we will apply this instance to simplify the problem. *)
466
467 Definition proper_flip_proper `(mor : Proper A R m) : Proper (flip R) m := mor.
468
469 (** Every reflexive crelation gives rise to a morphism,
470 only for immediately solving goals without variables. *)
471
472 Lemma reflexive_proper `{Reflexive A R} (x : A) : Proper R x.
473 Proof. firstorder. Qed.
474
475 Lemma proper_eq {A} (x : A) : Proper (@eq A) x.
476 Proof. intros. apply reflexive_proper. Qed.
477
478 End GenericInstances.
479
480 Class PartialApplication.
481
482 CoInductive normalization_done : Prop := did_normalization.
483
484 Ltac partial_application_tactic :=
485 let rec do_partial_apps H m cont :=
486 match m with
487 | ?m' ?x => class_apply @Reflexive_partial_app_morphism ;
488 [(do_partial_apps H m' ltac:(idtac))|clear H]
489 | _ => cont
490 end
491 in
492 let rec do_partial H ar m :=
493 lazymatch ar with
494 | 0%nat => do_partial_apps H m ltac:(fail 1)
495 | S ?n' =>
496 match m with
497 ?m' ?x => do_partial H n' m'
498 end
499 end
500 in
501 let params m sk fk :=
502 (let m' := fresh in Ltac.Internal.head_of_constr m' m ;
503 let n := fresh in evar (n:nat) ;
504 let v := eval compute in n in clear n ;
505 let H := fresh in
506 assert(H:Params m' v) by (subst m'; once typeclasses eauto) ;
507 let v' := eval compute in v in subst m';
508 (sk H v' || fail 1))
509 || fk
510 in
511 let on_morphism m cont :=
512 params m ltac:(fun H n => do_partial H n m)
513 ltac:(cont)
514 in
515 match goal with
516 | [ _ : normalization_done |- _ ] => fail 1
517 | [ _ : @Params _ _ _ |- _ ] => fail 1
518 | [ |- @Proper ?T _ (?m ?x) ] =>
519 match goal with
520 | [ H : PartialApplication |- _ ] =>
521 class_apply @Reflexive_partial_app_morphism; [|clear H]
522 | _ => on_morphism (m x)
523 ltac:(class_apply @Reflexive_partial_app_morphism)
524 end
525 end.
526
527 (** Bootstrap !!! *)
528
529 #[global]
530 Instance proper_proper {A} : Proper (relation_equivalence ==> eq ==> iffT) (@Proper A).
531 Proof.
532 intros R R' HRR' x y <-. red in HRR'.
533 split ; red ; intros.
534 - now apply (fst (HRR' _ _)).
535 - now apply (snd (HRR' _ _)).
536 Qed.
537
538 Ltac proper_reflexive :=
539 match goal with
540 | [ _ : normalization_done |- _ ] => fail 1
541 | _ => class_apply proper_eq || class_apply @reflexive_proper
542 end.
543
544
545 #[global]
546 Hint Extern 1 (subrelation (flip _) _) => class_apply @flip1 : typeclass_instances.
547 #[global]
548 Hint Extern 1 (subrelation _ (flip _)) => class_apply @flip2 : typeclass_instances.
549
550 (* Hint Extern 1 (Proper _ (complement _)) => apply @complement_proper *)
551 (* : typeclass_instances. *)
552 #[global]
553 Hint Extern 1 (Proper _ (flip _)) => apply @flip_proper
554 : typeclass_instances.
555 #[global]
556 Hint Extern 2 (@Proper _ (flip _) _) => class_apply @proper_flip_proper
557 : typeclass_instances.
558 #[global]
559 Hint Extern 4 (@Proper _ _ _) => partial_application_tactic
560 : typeclass_instances.
561 #[global]
562 Hint Extern 7 (@Proper _ _ _) => proper_reflexive
563 : typeclass_instances.
564
565 (** Special-purpose class to do normalization of signatures w.r.t. flip. *)
566
567 Section Normalize.
568 Context (A : Type).
569
570 Class Normalizes (m : crelation A) (m' : crelation A) :=
571 normalizes : relation_equivalence m m'.
572
573 (** Current strategy: add [flip] everywhere and reduce using [subrelation]
574 afterwards. *)
575
576 Lemma proper_normalizes_proper `(Normalizes R0 R1, Proper A R1 m) : Proper R0 m.
577 Proof.
578 apply (_ : Normalizes R0 R1). assumption.
579 Qed.
580
581 Lemma flip_atom R : Normalizes R (flip (flip R)).
582 Proof.
583 firstorder.
584 Qed.
585
586 End Normalize.
587
588 Lemma flip_arrow `(NA : Normalizes A R (flip R'''), NB : Normalizes B R' (flip R'')) :
589 Normalizes (A -> B) (R ==> R') (flip (R''' ==> R'')%signatureT).
590 Proof.
591 unfold Normalizes in *. intros.
592 eapply transitivity; [|eapply symmetry, flip_respectful].
593 now apply respectful_morphism.
594 Qed.
595
596 Ltac normalizes :=
597 match goal with
598 | [ |- Normalizes _ (respectful _ _) _ ] => class_apply @flip_arrow
599 | _ => class_apply @flip_atom
600 end.
601
602 Ltac proper_normalization :=
603 match goal with
604 | [ _ : normalization_done |- _ ] => fail 1
605 | [ _ : apply_subrelation |- @Proper _ ?R _ ] =>
606 let H := fresh "H" in
607 set(H:=did_normalization) ; class_apply @proper_normalizes_proper
608 end.
609
610 #[global]
611 Hint Extern 1 (Normalizes _ _ _) => normalizes : typeclass_instances.
612 #[global]
613 Hint Extern 6 (@Proper _ _ _) => proper_normalization
614 : typeclass_instances.
615
616 (** When the crelation on the domain is symmetric, we can
617 flip the crelation on the codomain. Same for binary functions. *)
618
619 Lemma proper_sym_flip :
620 forall `(Symmetric A R1)`(Proper (A->B) (R1==>R2) f),
621 Proper (R1==>flip R2) f.
622 Proof.
623 intros A R1 Sym B R2 f Hf.
624 intros x x' Hxx'. apply Hf, Sym, Hxx'.
625 Qed.
626
627 Lemma proper_sym_flip_2 :
628 forall `(Symmetric A R1)`(Symmetric B R2)`(Proper (A->B->C) (R1==>R2==>R3) f),
629 Proper (R1==>R2==>flip R3) f.
630 Proof.
631 intros A R1 Sym1 B R2 Sym2 C R3 f Hf.
632 intros x x' Hxx' y y' Hyy'. apply Hf; auto.
633 Qed.
634
635 (** When the crelation on the domain is symmetric, a predicate is
636 compatible with [iff] as soon as it is compatible with [impl].
637 Same with a binary crelation. *)
638
639 Lemma proper_sym_impl_iff : forall `(Symmetric A R)`(Proper _ (R==>impl) f),
640 Proper (R==>iff) f.
641 Proof.
642 intros A R Sym f Hf x x' Hxx'. repeat red in Hf. split; eauto.
643 Qed.
644
645 Lemma proper_sym_arrow_iffT : forall `(Symmetric A R)`(Proper _ (R==>arrow) f),
646 Proper (R==>iffT) f.
647 Proof.
648 intros A R Sym f Hf x x' Hxx'. repeat red in Hf. split; eauto.
649 Qed.
650
651 Lemma proper_sym_impl_iff_2 :
652 forall `(Symmetric A R)`(Symmetric B R')`(Proper _ (R==>R'==>impl) f),
653 Proper (R==>R'==>iff) f.
654 Proof.
655 intros A R Sym B R' Sym' f Hf x x' Hxx' y y' Hyy'.
656 repeat red in Hf. split; eauto.
657 Qed.
658
659 Lemma proper_sym_arrow_iffT_2 :
660 forall `(Symmetric A R)`(Symmetric B R')`(Proper _ (R==>R'==>arrow) f),
661 Proper (R==>R'==>iffT) f.
662 Proof.
663 intros A R Sym B R' Sym' f Hf x x' Hxx' y y' Hyy'.
664 repeat red in Hf. split; eauto.
665 Qed.
666
667 (** A [PartialOrder] is compatible with its underlying equivalence. *)
668 Require Import Relation_Definitions.
669
670 #[global]
671 Instance PartialOrder_proper_type `(PartialOrder A eqA R) :
672 Proper (eqA==>eqA==>iffT) R.
673 Proof.
674 intros.
675 apply proper_sym_arrow_iffT_2. 1-2: typeclasses eauto.
676 intros x x' Hx y y' Hy Hr.
677 apply transitivity with x.
678 - generalize (partial_order_equivalence x x'); compute; intuition.
679 - apply transitivity with y; auto.
680 generalize (partial_order_equivalence y y'); compute; intuition.
681 Qed.
682
683 (** From a [PartialOrder] to the corresponding [StrictOrder]:
684 [lt = le /\ ~eq].
685 If the order is total, we could also say [gt = ~le]. *)
686
687 Lemma PartialOrder_StrictOrder `(PartialOrder A eqA R) :
688 StrictOrder (relation_conjunction R (complement eqA)).
689 Proof.
690 split; compute.
691 - intros x (_,Hx). apply Hx, Equivalence_Reflexive.
692 - intros x y z (Hxy,Hxy') (Hyz,Hyz'). split.
693 + apply PreOrder_Transitive with y; assumption.
694 + intro Hxz.
695 apply Hxy'.
696 apply partial_order_antisym; auto.
697 apply transitivity with z; [assumption|].
698 now apply H.
699 Qed.
700
701 (** From a [StrictOrder] to the corresponding [PartialOrder]:
702 [le = lt \/ eq].
703 If the order is total, we could also say [ge = ~lt]. *)
704
705 Lemma StrictOrder_PreOrder
706 `(Equivalence A eqA, StrictOrder A R, Proper _ (eqA==>eqA==>iffT) R) :
707 PreOrder (relation_disjunction R eqA).
708 Proof.
709 split.
710 - intros x. right. apply reflexivity.
711 - intros x y z [Hxy|Hxy] [Hyz|Hyz].
712 + left. apply transitivity with y; auto.
713 + left. eapply H1; try eassumption.
714 * apply reflexivity.
715 * now apply symmetry.
716 + left. eapply H1; [eassumption|apply reflexivity|eassumption].
717 + right. apply transitivity with y; auto.
718 Qed.
719
720 #[global]
721 Hint Extern 4 (PreOrder (relation_disjunction _ _)) =>
722 class_apply StrictOrder_PreOrder : typeclass_instances.
723
724 Lemma StrictOrder_PartialOrder
725 `(Equivalence A eqA, StrictOrder A R, Proper _ (eqA==>eqA==>iffT) R) :
726 PartialOrder eqA (relation_disjunction R eqA).
727 Proof.
728 intros. intros x y. compute. intuition auto.
729 - right; now apply symmetry.
730 - elim (StrictOrder_Irreflexive x).
731 eapply transitivity with y; eauto.
732 - now apply symmetry.
733 Qed.
734
735 #[global]
736 Hint Extern 4 (StrictOrder (relation_conjunction _ _)) =>
737 class_apply PartialOrder_StrictOrder : typeclass_instances.
738
739 #[global]
740 Hint Extern 4 (PartialOrder _ (relation_disjunction _ _)) =>
741 class_apply StrictOrder_PartialOrder : typeclass_instances.
742
743 (* Register bindings for the generalized rewriting tactic *)
744
745 Register forall_relation as rewrite.type.forall_relation.
746 Register pointwise_relation as rewrite.type.pointwise_relation.
747 Register respectful as rewrite.type.respectful.
748 Register forall_def as rewrite.type.forall_def.
749 Register do_subrelation as rewrite.type.do_subrelation.
750 Register apply_subrelation as rewrite.type.apply_subrelation.
751 Register Proper as rewrite.type.Proper.
752 Register proper_prf as rewrite.type.proper_prf.
753 Register ProperProxy as rewrite.type.ProperProxy.