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