Debian Codegraph @

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