]> matita.cs.unibo.it Git - helm.git/blob - matita/library/nat/div_and_mod.ma
experimental branch with no set baseuri command and no developments
[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
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
227 (* some properties of div and mod *)
228 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
229 intros.
230 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O);
231 [2: apply div_mod_spec_div_mod.
232     unfold lt.apply le_S_S.apply le_O_n.
233 |   skip
234 |   apply div_mod_spec_times
235 ]
236 qed.
237
238 (*a simple variant of div_times theorem *)
239 theorem lt_O_to_div_times: \forall a,b:nat. O \lt b \to
240 a*b/b = a.
241 intros.
242 rewrite > sym_times.
243 rewrite > (S_pred b H).
244 apply div_times.
245 qed.
246
247 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
248 intros.
249 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O).
250 apply div_mod_spec_div_mod.assumption.
251 constructor 1.assumption.
252 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
253 qed.
254
255 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
256 intros.
257 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n).
258 apply div_mod_spec_div_mod.
259 apply (le_to_lt_to_lt O n m).
260 apply le_O_n.assumption.
261 constructor 1.assumption.reflexivity.
262 qed.
263
264 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
265 intros.
266 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O).
267 apply div_mod_spec_div_mod.assumption.
268 constructor 1.assumption.
269 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
270 qed.
271
272 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to 
273 ((S n) \mod m) = S (n \mod m).
274 intros.
275 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m))).
276 apply div_mod_spec_div_mod.assumption.
277 constructor 1.assumption.rewrite < plus_n_Sm.
278 apply eq_f.
279 apply div_mod.
280 assumption.
281 qed.
282
283 theorem mod_O_n: \forall n:nat.O \mod n = O.
284 intro.elim n.simplify.reflexivity.
285 simplify.reflexivity.
286 qed.
287
288 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
289 intros.
290 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n).
291 apply div_mod_spec_div_mod.
292 apply (le_to_lt_to_lt O n m).apply le_O_n.assumption.
293 constructor 1.
294 assumption.reflexivity.
295 qed.
296
297 theorem mod_SO: \forall n:nat. mod n (S O) = O.
298 intro.
299 apply sym_eq.
300 apply le_n_O_to_eq.
301 apply le_S_S_to_le.
302 apply lt_mod_m_m.
303 apply le_n.
304 qed.
305
306 theorem div_SO: \forall n:nat. div n (S O) = n.
307 intro.
308 rewrite > (div_mod ? (S O)) in \vdash (? ? ? %)
309   [rewrite > mod_SO.
310    rewrite < plus_n_O.
311    apply times_n_SO
312   |apply le_n
313   ]
314 qed.
315
316 theorem or_div_mod: \forall n,q. O < q \to
317 ((S (n \mod q)=q) \land S n = (S (div n q)) * q \lor
318 ((S (n \mod q)<q) \land S n= (div n q) * q + S (n\mod q))).
319 intros.
320 elim (le_to_or_lt_eq ? ? (lt_mod_m_m n q H))
321   [right.split
322     [assumption
323     |rewrite < plus_n_Sm.
324      apply eq_f.
325      apply div_mod.
326      assumption
327     ]
328   |left.split
329     [assumption
330     |simplify.
331      rewrite > sym_plus.
332      rewrite < H1 in ⊢ (? ? ? (? ? %)).
333      rewrite < plus_n_Sm.
334      apply eq_f.
335      apply div_mod.
336      assumption
337     ]
338   ]
339 qed.
340
341 (* injectivity *)
342 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
343 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
344 intros.
345 rewrite < (div_times n).
346 rewrite < (div_times n q).
347 apply eq_f2.assumption.
348 reflexivity.
349 qed.
350
351 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
352 injective_times_r.
353
354 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
355 simplify.
356 intros 4.
357 apply (lt_O_n_elim n H).intros.
358 apply (inj_times_r m).assumption.
359 qed.
360
361 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
362 \def lt_O_to_injective_times_r.
363
364 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
365 simplify.
366 intros.
367 apply (inj_times_r n x y).
368 rewrite < sym_times.
369 rewrite < (sym_times y).
370 assumption.
371 qed.
372
373 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
374 injective_times_l.
375
376 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
377 simplify.
378 intros 4.
379 apply (lt_O_n_elim n H).intros.
380 apply (inj_times_l m).assumption.
381 qed.
382
383 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
384 \def lt_O_to_injective_times_l.
385
386       
387 (* n_divides computes the pair (div,mod) *)
388
389 (* p is just an upper bound, acc is an accumulator *)
390 let rec n_divides_aux p n m acc \def
391   match n \mod m with
392   [ O \Rightarrow 
393     match p with
394       [ O \Rightarrow pair nat nat acc n
395       | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
396   | (S a) \Rightarrow pair nat nat acc n].
397
398 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
399 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.
400