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