]> matita.cs.unibo.it Git - helm.git/blob - matita/library/nat/div_and_mod.ma
f7f2883d590a8118ae6a39a8c348a75dcb2ff565
[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 theorem eq_times_div_minus_mod:
105 \forall a,b:nat. O \lt b \to
106 (a /b)*b = a - (a \mod b).
107 intros.
108 rewrite > (div_mod a b) in \vdash (? ? ? (? % ?))
109 [ apply (minus_plus_m_m (times (div a b) b) (mod a b))
110 | assumption
111 ]
112 qed.
113
114 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
115 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
116
117 (* 
118 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
119 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
120 *)
121
122 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq O.
123 intros 4.unfold Not.intros.elim H.absurd (le (S r) O).
124 rewrite < H1.assumption.
125 exact (not_le_Sn_O r).
126 qed.
127
128 theorem div_mod_spec_div_mod: 
129 \forall n,m. O < m \to (div_mod_spec n m (n / m) (n \mod m)).
130 intros.
131 apply div_mod_spec_intro.
132 apply lt_mod_m_m.assumption.
133 apply div_mod.assumption.
134 qed. 
135
136 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
137 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to 
138 (eq nat q q1).
139 intros.elim H.elim H1.
140 apply (nat_compare_elim q q1).intro.
141 apply False_ind.
142 cut (eq nat ((q1-q)*b+r1) r).
143 cut (b \leq (q1-q)*b+r1).
144 cut (b \leq r).
145 apply (lt_to_not_le r b H2 Hcut2).
146 elim Hcut.assumption.
147 apply (trans_le ? ((q1-q)*b)).
148 apply le_times_n.
149 apply le_SO_minus.exact H6.
150 rewrite < sym_plus.
151 apply le_plus_n.
152 rewrite < sym_times.
153 rewrite > distr_times_minus.
154 rewrite > plus_minus.
155 rewrite > sym_times.
156 rewrite < H5.
157 rewrite < sym_times. 
158 apply plus_to_minus.
159 apply H3.
160 apply le_times_r.
161 apply lt_to_le.
162 apply H6.
163 (* eq case *)
164 intros.assumption.
165 (* the following case is symmetric *)
166 intro.
167 apply False_ind.
168 cut (eq nat ((q-q1)*b+r) r1).
169 cut (b \leq (q-q1)*b+r).
170 cut (b \leq r1).
171 apply (lt_to_not_le r1 b H4 Hcut2).
172 elim Hcut.assumption.
173 apply (trans_le ? ((q-q1)*b)).
174 apply le_times_n.
175 apply le_SO_minus.exact H6.
176 rewrite < sym_plus.
177 apply le_plus_n.
178 rewrite < sym_times.
179 rewrite > distr_times_minus.
180 rewrite > plus_minus.
181 rewrite > sym_times.
182 rewrite < H3.
183 rewrite < sym_times.
184 apply plus_to_minus.
185 apply H5.
186 apply le_times_r.
187 apply lt_to_le.
188 apply H6.
189 qed.
190
191 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
192 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to 
193 (eq nat r r1).
194 intros.elim H.elim H1.
195 apply (inj_plus_r (q*b)).
196 rewrite < H3.
197 rewrite > (div_mod_spec_to_eq a b q r q1 r1 H H1).
198 assumption.
199 qed.
200
201 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
202 intros.constructor 1.
203 unfold lt.apply le_S_S.apply le_O_n.
204 rewrite < plus_n_O.rewrite < sym_times.reflexivity.
205 qed.
206
207 lemma div_plus_times: \forall m,q,r:nat. r < m \to  (q*m+r)/ m = q. 
208 intros.
209 apply (div_mod_spec_to_eq (q*m+r) m ? ((q*m+r) \mod m) ? 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
217 lemma mod_plus_times: \forall m,q,r:nat. r < m \to  (q*m+r) \mod m = r. 
218 intros.
219 apply (div_mod_spec_to_eq2 (q*m+r) m ((q*m+r)/ m) ((q*m+r) \mod m) q r)
220   [apply div_mod_spec_div_mod.
221    apply (le_to_lt_to_lt ? r)
222     [apply le_O_n|assumption]
223   |apply div_mod_spec_intro[assumption|reflexivity]
224   ]
225 qed.
226 (* some properties of div and mod *)
227 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
228 intros.
229 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O);
230 [2: apply div_mod_spec_div_mod.
231     unfold lt.apply le_S_S.apply le_O_n.
232 |   skip
233 |   apply div_mod_spec_times
234 ]
235 qed.
236
237 (*a simple variant of div_times theorem *)
238 theorem lt_O_to_div_times: \forall a,b:nat. O \lt b \to
239 a*b/b = a.
240 intros.
241 rewrite > sym_times.
242 rewrite > (S_pred b H).
243 apply div_times.
244 qed.
245
246 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
247 intros.
248 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O).
249 apply div_mod_spec_div_mod.assumption.
250 constructor 1.assumption.
251 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
252 qed.
253
254 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
255 intros.
256 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n).
257 apply div_mod_spec_div_mod.
258 apply (le_to_lt_to_lt O n m).
259 apply le_O_n.assumption.
260 constructor 1.assumption.reflexivity.
261 qed.
262
263 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
264 intros.
265 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O).
266 apply div_mod_spec_div_mod.assumption.
267 constructor 1.assumption.
268 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
269 qed.
270
271 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to 
272 ((S n) \mod m) = S (n \mod m).
273 intros.
274 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m))).
275 apply div_mod_spec_div_mod.assumption.
276 constructor 1.assumption.rewrite < plus_n_Sm.
277 apply eq_f.
278 apply div_mod.
279 assumption.
280 qed.
281
282 theorem mod_O_n: \forall n:nat.O \mod n = O.
283 intro.elim n.simplify.reflexivity.
284 simplify.reflexivity.
285 qed.
286
287 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
288 intros.
289 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n).
290 apply div_mod_spec_div_mod.
291 apply (le_to_lt_to_lt O n m).apply le_O_n.assumption.
292 constructor 1.
293 assumption.reflexivity.
294 qed.
295
296 theorem mod_SO: \forall n:nat. mod n (S O) = O.
297 intro.
298 apply sym_eq.
299 apply le_n_O_to_eq.
300 apply le_S_S_to_le.
301 apply lt_mod_m_m.
302 apply le_n.
303 qed.
304
305 theorem div_SO: \forall n:nat. div n (S O) = n.
306 intro.
307 rewrite > (div_mod ? (S O)) in \vdash (? ? ? %)
308   [rewrite > mod_SO.
309    rewrite < plus_n_O.
310    apply times_n_SO
311   |apply le_n
312   ]
313 qed.
314
315 theorem le_div: \forall n,m. O < n \to m/n \le m.
316 intros.
317 rewrite > (div_mod m n) in \vdash (? ? %)
318   [apply (trans_le ? (m/n*n))
319     [rewrite > times_n_SO in \vdash (? % ?).
320      apply le_times
321       [apply le_n|assumption]
322     |apply le_plus_n_r
323     ]
324   |assumption
325   ]
326 qed.
327
328 (* injectivity *)
329 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
330 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
331 intros.
332 rewrite < (div_times n).
333 rewrite < (div_times n q).
334 apply eq_f2.assumption.
335 reflexivity.
336 qed.
337
338 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
339 injective_times_r.
340
341 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
342 simplify.
343 intros 4.
344 apply (lt_O_n_elim n H).intros.
345 apply (inj_times_r m).assumption.
346 qed.
347
348 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
349 \def lt_O_to_injective_times_r.
350
351 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
352 simplify.
353 intros.
354 apply (inj_times_r n x y).
355 rewrite < sym_times.
356 rewrite < (sym_times y).
357 assumption.
358 qed.
359
360 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
361 injective_times_l.
362
363 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
364 simplify.
365 intros 4.
366 apply (lt_O_n_elim n H).intros.
367 apply (inj_times_l m).assumption.
368 qed.
369
370 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
371 \def lt_O_to_injective_times_l.
372
373 (* n_divides computes the pair (div,mod) *)
374
375 (* p is just an upper bound, acc is an accumulator *)
376 let rec n_divides_aux p n m acc \def
377   match n \mod m with
378   [ O \Rightarrow 
379     match p with
380       [ O \Rightarrow pair nat nat acc n
381       | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
382   | (S a) \Rightarrow pair nat nat acc n].
383
384 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
385 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.
386