summaryrefslogtreecommitdiff
path: root/theories/NArith/Pminmax.v
blob: 4cc48af66eae4341b77095ecc56735d72daad2c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)

Require Import Orders BinPos Pnat POrderedType GenericMinMax.

(** * Maximum and Minimum of two positive numbers *)

Local Open Scope positive_scope.

(** The functions [Pmax] and [Pmin] implement indeed
    a maximum and a minimum *)

Lemma Pmax_l : forall x y, y<=x -> Pmax x y = x.
Proof.
 unfold Ple, Pmax. intros x y.
 rewrite (ZC4 y x). generalize (Pcompare_eq_iff x y).
 destruct ((x ?= y) Eq); intuition.
Qed.

Lemma Pmax_r : forall x y, x<=y -> Pmax x y = y.
Proof.
 unfold Ple, Pmax. intros x y. destruct ((x ?= y) Eq); intuition.
Qed.

Lemma Pmin_l : forall x y, x<=y -> Pmin x y = x.
Proof.
 unfold Ple, Pmin. intros x y. destruct ((x ?= y) Eq); intuition.
Qed.

Lemma Pmin_r : forall x y, y<=x -> Pmin x y = y.
Proof.
 unfold Ple, Pmin. intros x y.
 rewrite (ZC4 y x). generalize (Pcompare_eq_iff x y).
 destruct ((x ?= y) Eq); intuition.
Qed.

Module PositiveHasMinMax <: HasMinMax Positive_as_OT.
 Definition max := Pmax.
 Definition min := Pmin.
 Definition max_l := Pmax_l.
 Definition max_r := Pmax_r.
 Definition min_l := Pmin_l.
 Definition min_r := Pmin_r.
End PositiveHasMinMax.


Module P.
(** We obtain hence all the generic properties of max and min. *)

Include UsualMinMaxProperties Positive_as_OT PositiveHasMinMax.

(** * Properties specific to the [positive] domain *)

(** Simplifications *)

Lemma max_1_l : forall n, Pmax 1 n = n.
Proof.
 intros. unfold Pmax. rewrite ZC4. generalize (Pcompare_1 n).
 destruct (n ?= 1); intuition.
Qed.

Lemma max_1_r : forall n, Pmax n 1 = n.
Proof. intros. rewrite P.max_comm. apply max_1_l. Qed.

Lemma min_1_l : forall n, Pmin 1 n = 1.
Proof.
 intros. unfold Pmin. rewrite ZC4. generalize (Pcompare_1 n).
 destruct (n ?= 1); intuition.
Qed.

Lemma min_1_r : forall n, Pmin n 1 = 1.
Proof. intros. rewrite P.min_comm. apply min_1_l. Qed.

(** Compatibilities (consequences of monotonicity) *)

Lemma succ_max_distr :
 forall n m, Psucc (Pmax n m) = Pmax (Psucc n) (Psucc m).
Proof.
 intros. symmetry. apply max_monotone.
 intros x x'. unfold Ple.
 rewrite 2 nat_of_P_compare_morphism, 2 nat_of_P_succ_morphism.
 simpl; auto.
Qed.

Lemma succ_min_distr : forall n m, Psucc (Pmin n m) = Pmin (Psucc n) (Psucc m).
Proof.
 intros. symmetry. apply min_monotone.
 intros x x'. unfold Ple.
 rewrite 2 nat_of_P_compare_morphism, 2 nat_of_P_succ_morphism.
 simpl; auto.
Qed.

Lemma plus_max_distr_l : forall n m p, Pmax (p + n) (p + m) = p + Pmax n m.
Proof.
 intros. apply max_monotone.
 intros x x'. unfold Ple.
 rewrite 2 nat_of_P_compare_morphism, 2 nat_of_P_plus_morphism.
 rewrite <- 2 Compare_dec.nat_compare_le. auto with arith.
Qed.

Lemma plus_max_distr_r : forall n m p, Pmax (n + p) (m + p) = Pmax n m + p.
Proof.
 intros. rewrite (Pplus_comm n p), (Pplus_comm m p), (Pplus_comm _ p).
 apply plus_max_distr_l.
Qed.

Lemma plus_min_distr_l : forall n m p, Pmin (p + n) (p + m) = p + Pmin n m.
Proof.
 intros. apply min_monotone.
 intros x x'. unfold Ple.
 rewrite 2 nat_of_P_compare_morphism, 2 nat_of_P_plus_morphism.
 rewrite <- 2 Compare_dec.nat_compare_le. auto with arith.
Qed.

Lemma plus_min_distr_r : forall n m p, Pmin (n + p) (m + p) = Pmin n m + p.
Proof.
 intros. rewrite (Pplus_comm n p), (Pplus_comm m p), (Pplus_comm _ p).
 apply plus_min_distr_l.
Qed.

End P.