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