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