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