]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/compare.ma
permutation.ma added to the repository.
[helm.git] / helm / matita / library / nat / compare.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||       A.Asperti, C.Sacerdoti Coen,                          *)
8 (*      ||A||       E.Tassi, S.Zacchiroli                                 *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU Lesser General Public License Version 2.1         *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 set "baseuri" "cic:/matita/nat/compare".
16
17 include "datatypes/bool.ma".
18 include "datatypes/compare.ma".
19 include "nat/orders.ma".
20
21 let rec eqb n m \def 
22 match n with 
23   [ O \Rightarrow 
24      match m with 
25      [ O \Rightarrow true
26            | (S q) \Rightarrow false] 
27   | (S p) \Rightarrow
28            match m with 
29      [ O \Rightarrow false
30            | (S q) \Rightarrow eqb p q]].
31            
32 theorem eqb_to_Prop: \forall n,m:nat. 
33 match (eqb n m) with
34 [ true  \Rightarrow n = m 
35 | false \Rightarrow n \neq m].
36 intros.
37 apply nat_elim2
38 (\lambda n,m:nat.match (eqb n m) with
39 [ true  \Rightarrow n = m 
40 | false \Rightarrow n \neq m]).
41 intro.elim n1.
42 simplify.reflexivity.
43 simplify.apply not_eq_O_S.
44 intro.
45 simplify.
46 intro. apply not_eq_O_S n1.apply sym_eq.assumption.
47 intros.simplify.
48 generalize in match H.
49 elim (eqb n1 m1).
50 simplify.apply eq_f.apply H1.
51 simplify.intro.apply H1.apply inj_S.assumption.
52 qed.
53
54 theorem eqb_elim : \forall n,m:nat.\forall P:bool \to Prop.
55 (n=m \to (P true)) \to (n \neq m \to (P false)) \to (P (eqb n m)). 
56 intros.
57 cut 
58 match (eqb n m) with
59 [ true  \Rightarrow n = m
60 | false \Rightarrow n \neq m] \to (P (eqb n m)).
61 apply Hcut.apply eqb_to_Prop.
62 elim eqb n m.
63 apply (H H2).
64 apply (H1 H2).
65 qed.
66
67 theorem eqb_n_n: \forall n. eqb n n = true.
68 intro.elim n.simplify.reflexivity.
69 simplify.assumption.
70 qed.
71
72 theorem eq_to_eqb_true: \forall n,m:nat.
73 n = m \to eqb n m = true.
74 intros.apply eqb_elim n m.
75 intros. reflexivity.
76 intros.apply False_ind.apply H1 H.
77 qed.
78
79 theorem not_eq_to_eqb_false: \forall n,m:nat.
80 \lnot (n = m) \to eqb n m = false.
81 intros.apply eqb_elim n m.
82 intros. apply False_ind.apply H H1.
83 intros.reflexivity.
84 qed.
85
86 let rec leb n m \def 
87 match n with 
88     [ O \Rightarrow true
89     | (S p) \Rightarrow
90         match m with 
91         [ O \Rightarrow false
92         | (S q) \Rightarrow leb p q]].
93         
94 theorem leb_to_Prop: \forall n,m:nat. 
95 match (leb n m) with
96 [ true  \Rightarrow n \leq m 
97 | false \Rightarrow n \nleq m].
98 intros.
99 apply nat_elim2
100 (\lambda n,m:nat.match (leb n m) with
101 [ true  \Rightarrow n \leq m 
102 | false \Rightarrow n \nleq m]).
103 simplify.exact le_O_n.
104 simplify.exact not_le_Sn_O.
105 intros 2.simplify.elim (leb n1 m1).
106 simplify.apply le_S_S.apply H.
107 simplify.intros.apply H.apply le_S_S_to_le.assumption.
108 qed.
109
110 theorem leb_elim: \forall n,m:nat. \forall P:bool \to Prop. 
111 (n \leq m \to (P true)) \to (n \nleq m \to (P false)) \to
112 P (leb n m).
113 intros.
114 cut 
115 match (leb n m) with
116 [ true  \Rightarrow n \leq m
117 | false \Rightarrow n \nleq m] \to (P (leb n m)).
118 apply Hcut.apply leb_to_Prop.
119 elim leb n m.
120 apply (H H2).
121 apply (H1 H2).
122 qed.
123
124 let rec nat_compare n m: compare \def
125 match n with
126 [ O \Rightarrow 
127     match m with 
128       [ O \Rightarrow EQ
129       | (S q) \Rightarrow LT ]
130 | (S p) \Rightarrow 
131     match m with 
132       [ O \Rightarrow GT
133       | (S q) \Rightarrow nat_compare p q]].
134
135 theorem nat_compare_n_n: \forall n:nat. nat_compare n n = EQ.
136 intro.elim n.
137 simplify.reflexivity.
138 simplify.assumption.
139 qed.
140
141 theorem nat_compare_S_S: \forall n,m:nat. 
142 nat_compare n m = nat_compare (S n) (S m).
143 intros.simplify.reflexivity.
144 qed.
145
146 theorem S_pred: \forall n:nat.lt O n \to eq nat n (S (pred n)).
147 intro.elim n.apply False_ind.exact not_le_Sn_O O H.
148 apply eq_f.apply pred_Sn.
149 qed.
150
151 theorem nat_compare_pred_pred: 
152 \forall n,m:nat.lt O n \to lt O m \to 
153 eq compare (nat_compare n m) (nat_compare (pred n) (pred m)).
154 intros.
155 apply lt_O_n_elim n H.
156 apply lt_O_n_elim m H1.
157 intros.
158 simplify.reflexivity.
159 qed.
160
161 theorem nat_compare_to_Prop: \forall n,m:nat. 
162 match (nat_compare n m) with
163   [ LT \Rightarrow n < m
164   | EQ \Rightarrow n=m
165   | GT \Rightarrow m < n ].
166 intros.
167 apply nat_elim2 (\lambda n,m.match (nat_compare n m) with
168   [ LT \Rightarrow n < m
169   | EQ \Rightarrow n=m
170   | GT \Rightarrow m < n ]).
171 intro.elim n1.simplify.reflexivity.
172 simplify.apply le_S_S.apply le_O_n.
173 intro.simplify.apply le_S_S. apply le_O_n.
174 intros 2.simplify.elim (nat_compare n1 m1).
175 simplify. apply le_S_S.apply H.
176 simplify. apply eq_f. apply H.
177 simplify. apply le_S_S.apply H.
178 qed.
179
180 theorem nat_compare_n_m_m_n: \forall n,m:nat. 
181 nat_compare n m = compare_invert (nat_compare m n).
182 intros. 
183 apply nat_elim2 (\lambda n,m. nat_compare n m = compare_invert (nat_compare m n)).
184 intros.elim n1.simplify.reflexivity.
185 simplify.reflexivity.
186 intro.elim n1.simplify.reflexivity.
187 simplify.reflexivity.
188 intros.simplify.elim H.reflexivity.
189 qed.
190      
191 theorem nat_compare_elim : \forall n,m:nat. \forall P:compare \to Prop.
192 (n < m \to P LT) \to (n=m \to P EQ) \to (m < n \to P GT) \to 
193 (P (nat_compare n m)).
194 intros.
195 cut match (nat_compare n m) with
196 [ LT \Rightarrow n < m
197 | EQ \Rightarrow n=m
198 | GT \Rightarrow m < n] \to
199 (P (nat_compare n m)).
200 apply Hcut.apply nat_compare_to_Prop.
201 elim (nat_compare n m).
202 apply (H H3).
203 apply (H1 H3).
204 apply (H2 H3).
205 qed.