]> matita.cs.unibo.it Git - helm.git/blob - matita/library/nat/div_and_mod.ma
new gcd properties, and theorems for totient, and theorems for totient1
[helm.git] / matita / library / nat / div_and_mod.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 (*       \ /        Matita 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/div_and_mod".
16
17 include "datatypes/constructors.ma".
18 include "nat/minus.ma".
19
20
21 let rec mod_aux p m n: nat \def
22 match (leb m n) with
23 [ true \Rightarrow m
24 | false \Rightarrow
25   match p with
26   [O \Rightarrow m
27   |(S q) \Rightarrow mod_aux q (m-(S n)) n]].
28
29 definition mod : nat \to nat \to nat \def
30 \lambda n,m.
31 match m with 
32 [O \Rightarrow n
33 | (S p) \Rightarrow mod_aux n n p]. 
34
35 interpretation "natural remainder" 'module x y =
36   (cic:/matita/nat/div_and_mod/mod.con x y).
37
38 let rec div_aux p m n : nat \def
39 match (leb m n) with
40 [ true \Rightarrow O
41 | false \Rightarrow
42   match p with
43   [O \Rightarrow O
44   |(S q) \Rightarrow S (div_aux q (m-(S n)) n)]].
45
46 definition div : nat \to nat \to nat \def
47 \lambda n,m.
48 match m with 
49 [O \Rightarrow S n
50 | (S p) \Rightarrow div_aux n n p]. 
51
52 interpretation "natural divide" 'divide x y =
53   (cic:/matita/nat/div_and_mod/div.con x y).
54
55 theorem le_mod_aux_m_m: 
56 \forall p,n,m. n \leq p \to (mod_aux p n m) \leq m.
57 intro.elim p.
58 apply (le_n_O_elim n H (\lambda n.(mod_aux O n m) \leq m)).
59 simplify.apply le_O_n.
60 simplify.
61 apply (leb_elim n1 m).
62 simplify.intro.assumption.
63 simplify.intro.apply H.
64 cut (n1 \leq (S n) \to n1-(S m) \leq n).
65 apply Hcut.assumption.
66 elim n1.
67 simplify.apply le_O_n.
68 simplify.apply (trans_le ? n2 n).
69 apply le_minus_m.apply le_S_S_to_le.assumption.
70 qed.
71
72 theorem lt_mod_m_m: \forall n,m. O < m \to (n \mod m) < m.
73 intros 2.elim m.apply False_ind.
74 apply (not_le_Sn_O O H).
75 simplify.unfold lt.apply le_S_S.apply le_mod_aux_m_m.
76 apply le_n.
77 qed.
78
79 theorem div_aux_mod_aux: \forall p,n,m:nat. 
80 (n=(div_aux p n m)*(S m) + (mod_aux p n m)).
81 intro.elim p.
82 simplify.elim (leb n m).
83 simplify.apply refl_eq.
84 simplify.apply refl_eq.
85 simplify.
86 apply (leb_elim n1 m).
87 simplify.intro.apply refl_eq.
88 simplify.intro.
89 rewrite > assoc_plus. 
90 elim (H (n1-(S m)) m).
91 change with (n1=(S m)+(n1-(S m))).
92 rewrite < sym_plus.
93 apply plus_minus_m_m.
94 change with (m < n1).
95 apply not_le_to_lt.exact H1.
96 qed.
97
98 theorem div_mod: \forall n,m:nat. O < m \to n=(n / m)*m+(n \mod m).
99 intros 2.elim m.elim (not_le_Sn_O O H).
100 simplify.
101 apply div_aux_mod_aux.
102 qed.
103
104 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
105 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
106
107 (* 
108 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
109 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
110 *)
111
112 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq O.
113 intros 4.unfold Not.intros.elim H.absurd (le (S r) O).
114 rewrite < H1.assumption.
115 exact (not_le_Sn_O r).
116 qed.
117
118 theorem div_mod_spec_div_mod: 
119 \forall n,m. O < m \to (div_mod_spec n m (n / m) (n \mod m)).
120 intros.
121 apply div_mod_spec_intro.
122 apply lt_mod_m_m.assumption.
123 apply div_mod.assumption.
124 qed. 
125
126 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
127 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to 
128 (eq nat q q1).
129 intros.elim H.elim H1.
130 apply (nat_compare_elim q q1).intro.
131 apply False_ind.
132 cut (eq nat ((q1-q)*b+r1) r).
133 cut (b \leq (q1-q)*b+r1).
134 cut (b \leq r).
135 apply (lt_to_not_le r b H2 Hcut2).
136 elim Hcut.assumption.
137 apply (trans_le ? ((q1-q)*b)).
138 apply le_times_n.
139 apply le_SO_minus.exact H6.
140 rewrite < sym_plus.
141 apply le_plus_n.
142 rewrite < sym_times.
143 rewrite > distr_times_minus.
144 rewrite > plus_minus.
145 rewrite > sym_times.
146 rewrite < H5.
147 rewrite < sym_times. 
148 apply plus_to_minus.
149 apply H3.
150 apply le_times_r.
151 apply lt_to_le.
152 apply H6.
153 (* eq case *)
154 intros.assumption.
155 (* the following case is symmetric *)
156 intro.
157 apply False_ind.
158 cut (eq nat ((q-q1)*b+r) r1).
159 cut (b \leq (q-q1)*b+r).
160 cut (b \leq r1).
161 apply (lt_to_not_le r1 b H4 Hcut2).
162 elim Hcut.assumption.
163 apply (trans_le ? ((q-q1)*b)).
164 apply le_times_n.
165 apply le_SO_minus.exact H6.
166 rewrite < sym_plus.
167 apply le_plus_n.
168 rewrite < sym_times.
169 rewrite > distr_times_minus.
170 rewrite > plus_minus.
171 rewrite > sym_times.
172 rewrite < H3.
173 rewrite < sym_times.
174 apply plus_to_minus.
175 apply H5.
176 apply le_times_r.
177 apply lt_to_le.
178 apply H6.
179 qed.
180
181 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
182 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to 
183 (eq nat r r1).
184 intros.elim H.elim H1.
185 apply (inj_plus_r (q*b)).
186 rewrite < H3.
187 rewrite > (div_mod_spec_to_eq a b q r q1 r1 H H1).
188 assumption.
189 qed.
190
191 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
192 intros.constructor 1.
193 unfold lt.apply le_S_S.apply le_O_n.
194 rewrite < plus_n_O.rewrite < sym_times.reflexivity.
195 qed.
196
197 lemma div_plus_times: \forall m,q,r:nat. r < m \to  (q*m+r)/ m = q. 
198 intros.
199 apply (div_mod_spec_to_eq (q*m+r) m ? ((q*m+r) \mod m) ? r)
200   [apply div_mod_spec_div_mod.
201    apply (le_to_lt_to_lt ? r)
202     [apply le_O_n|assumption]
203   |apply div_mod_spec_intro[assumption|reflexivity]
204   ]
205 qed.
206
207 lemma mod_plus_times: \forall m,q,r:nat. r < m \to  (q*m+r) \mod m = r. 
208 intros.
209 apply (div_mod_spec_to_eq2 (q*m+r) m ((q*m+r)/ m) ((q*m+r) \mod m) q r)
210   [apply div_mod_spec_div_mod.
211    apply (le_to_lt_to_lt ? r)
212     [apply le_O_n|assumption]
213   |apply div_mod_spec_intro[assumption|reflexivity]
214   ]
215 qed.
216 (* some properties of div and mod *)
217 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
218 intros.
219 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O);
220 [2: apply div_mod_spec_div_mod.
221     unfold lt.apply le_S_S.apply le_O_n.
222 |   skip
223 |   apply div_mod_spec_times
224 ]
225 qed.
226
227 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
228 intros.
229 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O).
230 apply div_mod_spec_div_mod.assumption.
231 constructor 1.assumption.
232 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
233 qed.
234
235 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
236 intros.
237 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n).
238 apply div_mod_spec_div_mod.
239 apply (le_to_lt_to_lt O n m).
240 apply le_O_n.assumption.
241 constructor 1.assumption.reflexivity.
242 qed.
243
244 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
245 intros.
246 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O).
247 apply div_mod_spec_div_mod.assumption.
248 constructor 1.assumption.
249 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
250 qed.
251
252 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to 
253 ((S n) \mod m) = S (n \mod m).
254 intros.
255 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m))).
256 apply div_mod_spec_div_mod.assumption.
257 constructor 1.assumption.rewrite < plus_n_Sm.
258 apply eq_f.
259 apply div_mod.
260 assumption.
261 qed.
262
263 theorem mod_O_n: \forall n:nat.O \mod n = O.
264 intro.elim n.simplify.reflexivity.
265 simplify.reflexivity.
266 qed.
267
268 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
269 intros.
270 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n).
271 apply div_mod_spec_div_mod.
272 apply (le_to_lt_to_lt O n m).apply le_O_n.assumption.
273 constructor 1.
274 assumption.reflexivity.
275 qed.
276
277 (* injectivity *)
278 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
279 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
280 intros.
281 rewrite < (div_times n).
282 rewrite < (div_times n q).
283 apply eq_f2.assumption.
284 reflexivity.
285 qed.
286
287 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
288 injective_times_r.
289
290 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
291 simplify.
292 intros 4.
293 apply (lt_O_n_elim n H).intros.
294 apply (inj_times_r m).assumption.
295 qed.
296
297 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
298 \def lt_O_to_injective_times_r.
299
300 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
301 simplify.
302 intros.
303 apply (inj_times_r n x y).
304 rewrite < sym_times.
305 rewrite < (sym_times y).
306 assumption.
307 qed.
308
309 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
310 injective_times_l.
311
312 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
313 simplify.
314 intros 4.
315 apply (lt_O_n_elim n H).intros.
316 apply (inj_times_l m).assumption.
317 qed.
318
319 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
320 \def lt_O_to_injective_times_l.
321
322 (* n_divides computes the pair (div,mod) *)
323
324 (* p is just an upper bound, acc is an accumulator *)
325 let rec n_divides_aux p n m acc \def
326   match n \mod m with
327   [ O \Rightarrow 
328     match p with
329       [ O \Rightarrow pair nat nat acc n
330       | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
331   | (S a) \Rightarrow pair nat nat acc n].
332
333 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
334 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.
335
336
337 (*a simple variant of div_times theorem *)
338 theorem div_times_ltO: \forall a,b:nat. O \lt b \to
339 a*b/b = a.
340 intros.
341 rewrite > sym_times.
342 rewrite > (S_pred b H).
343 apply div_times.
344 qed.
345