]> matita.cs.unibo.it Git - helm.git/blob - matita/library/nat/factorization.ma
Minor changes.
[helm.git] / matita / library / nat / factorization.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/factorization".
16
17 include "nat/ord.ma".
18 include "nat/gcd.ma".
19 include "nat/nth_prime.ma".
20
21 (* the following factorization algorithm looks for the largest prime
22    factor. *)
23 definition max_prime_factor \def \lambda n:nat.
24 (max n (\lambda p:nat.eqb (n \mod (nth_prime p)) O)).
25
26 (* max_prime_factor is indeed a factor *)
27 theorem divides_max_prime_factor_n:
28   \forall n:nat. (S O) < n
29   \to nth_prime (max_prime_factor n) \divides n.
30 intros; apply divides_b_true_to_divides;
31 [ apply lt_O_nth_prime_n;
32 | apply (f_max_true  (\lambda p:nat.eqb (n \mod (nth_prime p)) O) n);
33   cut (\exists i. nth_prime i = smallest_factor n);
34   [ elim Hcut.
35     apply (ex_intro nat ? a);
36     split;
37     [ apply (trans_le a (nth_prime a));
38       [ apply le_n_fn;
39         exact lt_nth_prime_n_nth_prime_Sn;
40       | rewrite > H1;
41         apply le_smallest_factor_n; ]
42     | rewrite > H1;
43       (*CSC: simplify here does something nasty! *)
44       change with (divides_b (smallest_factor n) n = true);
45       apply divides_to_divides_b_true;
46       [ apply (trans_lt ? (S O));
47         [ unfold lt; apply le_n;
48         | apply lt_SO_smallest_factor; assumption; ]
49       | letin x \def le.auto new.
50          (*       
51        apply divides_smallest_factor_n;
52         apply (trans_lt ? (S O));
53         [ unfold lt; apply le_n;
54         | assumption; ] *) ] ]
55   | auto. 
56     (* 
57     apply prime_to_nth_prime;
58     apply prime_smallest_factor_n;
59     assumption; *) ] ]
60 qed.
61
62 theorem divides_to_max_prime_factor : \forall n,m. (S O) < n \to O < m \to n \divides m \to 
63 max_prime_factor n \le max_prime_factor m.
64 intros.unfold max_prime_factor.
65 apply f_m_to_le_max.
66 apply (trans_le ? n).
67 apply le_max_n.apply divides_to_le.assumption.assumption.
68 change with (divides_b (nth_prime (max_prime_factor n)) m = true).
69 apply divides_to_divides_b_true.
70 cut (prime (nth_prime (max_prime_factor n))).
71 apply lt_O_nth_prime_n.apply prime_nth_prime.
72 cut (nth_prime (max_prime_factor n) \divides n).
73 auto.
74 auto.
75 (*
76   [ apply (transitive_divides ? n);
77     [ apply divides_max_prime_factor_n.
78       assumption.
79     | assumption. 
80     ]
81   | apply divides_b_true_to_divides;
82     [ apply lt_O_nth_prime_n.
83     | apply divides_to_divides_b_true;
84       [ apply lt_O_nth_prime_n.
85       | apply divides_max_prime_factor_n.
86         assumption.
87       ]
88     ]
89   ]
90 *)  
91 qed.
92
93 theorem p_ord_to_lt_max_prime_factor: \forall n,p,q,r. O < n \to
94 p = max_prime_factor n \to 
95 (pair nat nat q r) = p_ord n (nth_prime p) \to
96 (S O) < r \to max_prime_factor r < p.
97 intros.
98 rewrite > H1.
99 cut (max_prime_factor r \lt max_prime_factor n \lor
100     max_prime_factor r = max_prime_factor n).
101 elim Hcut.assumption.
102 absurd (nth_prime (max_prime_factor n) \divides r).
103 rewrite < H4.
104 apply divides_max_prime_factor_n.
105 assumption.unfold Not.
106 intro.
107 cut (r \mod (nth_prime (max_prime_factor n)) \neq O);
108   [unfold Not in Hcut1.auto new.
109     (*
110     apply Hcut1.apply divides_to_mod_O;
111     [ apply lt_O_nth_prime_n.
112     | assumption.
113     ]
114     *)
115   |letin z \def le.
116    cut(pair nat nat q r=p_ord_aux n n (nth_prime (max_prime_factor n)));
117    [2: rewrite < H1.assumption.|letin x \def le.auto width = 4 new]
118    (* CERCA COME MAI le_n non lo applica se lo trova come Const e non Rel *)
119   ].
120 (*
121     apply (p_ord_aux_to_not_mod_O n n ? q r);
122     [ apply lt_SO_nth_prime_n.
123     | assumption.
124     | apply le_n.
125     | rewrite < H1.assumption.
126     ]
127   ].
128 *)  
129 apply (le_to_or_lt_eq (max_prime_factor r)  (max_prime_factor n)).
130 apply divides_to_max_prime_factor.
131 assumption.assumption.
132 apply (witness r n ((nth_prime p) \sup q)).
133 rewrite < sym_times.
134 apply (p_ord_aux_to_exp n n ? q r).
135 apply lt_O_nth_prime_n.assumption.
136 qed.
137
138 theorem p_ord_to_lt_max_prime_factor1: \forall n,p,q,r. O < n \to
139 max_prime_factor n \le p \to 
140 (pair nat nat q r) = p_ord n (nth_prime p) \to
141 (S O) < r \to max_prime_factor r < p.
142 intros.
143 cut (max_prime_factor n < p \lor max_prime_factor n = p).
144 elim Hcut.apply (le_to_lt_to_lt ? (max_prime_factor n)).
145 apply divides_to_max_prime_factor.assumption.assumption.
146 apply (witness r n ((nth_prime p) \sup q)).
147 rewrite > sym_times.
148 apply (p_ord_aux_to_exp n n).
149 apply lt_O_nth_prime_n.
150 assumption.assumption.
151 apply (p_ord_to_lt_max_prime_factor n ? q).
152 assumption.apply sym_eq.assumption.assumption.assumption.
153 apply (le_to_or_lt_eq ? p H1).
154 qed.
155
156 (* datatypes and functions *)
157
158 inductive nat_fact : Set \def
159     nf_last : nat \to nat_fact   
160   | nf_cons : nat \to nat_fact \to nat_fact.
161
162 inductive nat_fact_all : Set \def
163     nfa_zero : nat_fact_all
164   | nfa_one : nat_fact_all
165   | nfa_proper : nat_fact \to nat_fact_all.
166
167 let rec factorize_aux p n acc \def
168   match p with 
169   [ O \Rightarrow acc
170   | (S p1) \Rightarrow 
171     match p_ord n (nth_prime p1) with
172     [ (pair q r) \Rightarrow 
173       factorize_aux p1 r (nf_cons q acc)]].
174   
175 definition factorize : nat \to nat_fact_all \def \lambda n:nat.
176   match n with
177     [ O \Rightarrow nfa_zero
178     | (S n1) \Rightarrow
179       match n1 with
180       [ O \Rightarrow nfa_one
181     | (S n2) \Rightarrow 
182       let p \def (max (S(S n2)) (\lambda p:nat.eqb ((S(S n2)) \mod (nth_prime p)) O)) in
183       match p_ord (S(S n2)) (nth_prime p) with
184       [ (pair q r) \Rightarrow 
185            nfa_proper (factorize_aux p r (nf_last (pred q)))]]].
186            
187 let rec defactorize_aux f i \def
188   match f with
189   [ (nf_last n) \Rightarrow (nth_prime i) \sup (S n)
190   | (nf_cons n g) \Rightarrow 
191       (nth_prime i) \sup n *(defactorize_aux g (S i))].
192       
193 definition defactorize : nat_fact_all \to nat \def
194 \lambda f : nat_fact_all. 
195 match f with 
196 [ nfa_zero \Rightarrow O
197 | nfa_one \Rightarrow (S O)
198 | (nfa_proper g) \Rightarrow defactorize_aux g O]. 
199
200 theorem lt_O_defactorize_aux:
201  \forall f:nat_fact.
202  \forall i:nat.
203  O < defactorize_aux f i.
204 intro; elim f;
205 [1,2:
206   simplify; unfold lt;
207   rewrite > times_n_SO;
208   apply le_times;
209   [ change with (O < nth_prime i);
210     apply lt_O_nth_prime_n;
211   |2,3:
212     change with (O < exp (nth_prime i) n);
213     apply lt_O_exp;
214     apply lt_O_nth_prime_n;
215   | change with (O < defactorize_aux n1 (S i));
216     apply H; ] ]
217 qed.
218
219 theorem lt_SO_defactorize_aux: \forall f:nat_fact.\forall i:nat.
220 S O < defactorize_aux f i.
221 intro.elim f.simplify.unfold lt.
222 rewrite > times_n_SO.
223 apply le_times.
224 change with (S O < nth_prime i).
225 apply lt_SO_nth_prime_n.
226 change with (O < exp (nth_prime i) n).
227 apply lt_O_exp.
228 apply lt_O_nth_prime_n.
229 simplify.unfold lt.
230 rewrite > times_n_SO.
231 rewrite > sym_times.
232 apply le_times.
233 change with (O < exp (nth_prime i) n).
234 apply lt_O_exp.
235 apply lt_O_nth_prime_n.
236 change with (S O < defactorize_aux n1 (S i)).
237 apply H.
238 qed.
239
240 theorem defactorize_aux_factorize_aux : 
241 \forall p,n:nat.\forall acc:nat_fact.O < n \to
242 ((n=(S O) \land p=O) \lor max_prime_factor n < p) \to
243 defactorize_aux (factorize_aux p n acc) O = n*(defactorize_aux acc p).
244 intro.elim p.simplify.
245 elim H1.elim H2.rewrite > H3.
246 rewrite > sym_times. apply times_n_SO.
247 apply False_ind.apply (not_le_Sn_O (max_prime_factor n) H2).
248 simplify.
249 (* generalizing the goal: I guess there exists a better way *)
250 cut (\forall q,r.(pair nat nat q r) = (p_ord_aux n1 n1 (nth_prime n)) \to
251 defactorize_aux match (p_ord_aux n1 n1 (nth_prime n)) with
252 [(pair q r)  \Rightarrow (factorize_aux n r (nf_cons q acc))] O =
253 n1*defactorize_aux acc (S n)).
254 apply (Hcut (fst ? ? (p_ord_aux n1 n1 (nth_prime n)))
255 (snd ? ? (p_ord_aux n1 n1 (nth_prime n)))).
256 apply sym_eq.apply eq_pair_fst_snd.
257 intros.
258 rewrite < H3.
259 simplify.
260 cut (n1 = r * (nth_prime n) \sup q).
261 rewrite > H.
262 simplify.rewrite < assoc_times.
263 rewrite < Hcut.reflexivity.
264 cut (O < r \lor O = r).
265 elim Hcut1.assumption.absurd (n1 = O).
266 rewrite > Hcut.rewrite < H4.reflexivity.
267 unfold Not. intro.apply (not_le_Sn_O O).
268 rewrite < H5 in \vdash (? ? %).assumption.
269 apply le_to_or_lt_eq.apply le_O_n.
270 cut ((S O) < r \lor (S O) \nlt r).
271 elim Hcut1.
272 right.
273 apply (p_ord_to_lt_max_prime_factor1 n1 ? q r).
274 assumption.elim H2.
275 elim H5.
276 apply False_ind.
277 apply (not_eq_O_S n).apply sym_eq.assumption.
278 apply le_S_S_to_le.
279 exact H5.
280 assumption.assumption.
281 cut (r=(S O)).
282 apply (nat_case n).
283 left.split.assumption.reflexivity.
284 intro.right.rewrite > Hcut2.
285 simplify.unfold lt.apply le_S_S.apply le_O_n.
286 cut (r < (S O) âˆ¨ r=(S O)).
287 elim Hcut2.absurd (O=r).
288 apply le_n_O_to_eq.apply le_S_S_to_le.exact H5.
289 unfold Not.intro.
290 cut (O=n1).
291 apply (not_le_Sn_O O).
292 rewrite > Hcut3 in âŠ¢ (? ? %).
293 assumption.rewrite > Hcut. 
294 rewrite < H6.reflexivity.
295 assumption.
296 apply (le_to_or_lt_eq r (S O)).
297 apply not_lt_to_le.assumption.
298 apply (decidable_lt (S O) r).
299 rewrite > sym_times.
300 apply (p_ord_aux_to_exp n1 n1).
301 apply lt_O_nth_prime_n.assumption.
302 qed.
303
304 theorem defactorize_factorize: \forall n:nat.defactorize (factorize n) = n.
305 intro.
306 apply (nat_case n).reflexivity.
307 intro.apply (nat_case m).reflexivity.
308 intro.(*CSC: simplify here does something really nasty *)
309 change with  
310 (let p \def (max (S(S m1)) (\lambda p:nat.eqb ((S(S m1)) \mod (nth_prime p)) O)) in
311 defactorize (match p_ord (S(S m1)) (nth_prime p) with
312 [ (pair q r) \Rightarrow 
313    nfa_proper (factorize_aux p r (nf_last (pred q)))])=(S(S m1))).
314 intro.
315 (* generalizing the goal; find a better way *)
316 cut (\forall q,r.(pair nat nat q r) = (p_ord (S(S m1)) (nth_prime p)) \to
317 defactorize (match p_ord (S(S m1)) (nth_prime p) with
318 [ (pair q r) \Rightarrow 
319    nfa_proper (factorize_aux p r (nf_last (pred q)))])=(S(S m1))).
320 apply (Hcut (fst ? ? (p_ord (S(S m1)) (nth_prime p)))
321 (snd ? ? (p_ord (S(S m1)) (nth_prime p)))).
322 apply sym_eq.apply eq_pair_fst_snd.
323 intros.
324 rewrite < H.
325 simplify.
326 cut ((S(S m1)) = (nth_prime p) \sup q *r).
327 cut (O<r).
328 rewrite > defactorize_aux_factorize_aux.
329 (*CSC: simplify here does something really nasty *)
330 change with (r*(nth_prime p) \sup (S (pred q)) = (S(S m1))).
331 cut ((S (pred q)) = q).
332 rewrite > Hcut2.
333 rewrite > sym_times.
334 apply sym_eq.
335 apply (p_ord_aux_to_exp (S(S m1))).
336 apply lt_O_nth_prime_n.
337 assumption.
338 (* O < q *)
339 apply sym_eq. apply S_pred.
340 cut (O < q \lor O = q).
341 elim Hcut2.assumption.
342 absurd (nth_prime p \divides S (S m1)).
343 apply (divides_max_prime_factor_n (S (S m1))).
344 unfold lt.apply le_S_S.apply le_S_S. apply le_O_n.
345 cut ((S(S m1)) = r).
346 rewrite > Hcut3 in \vdash (? (? ? %)).
347 (*CSC: simplify here does something really nasty *)
348 change with (nth_prime p \divides r \to False).
349 intro.
350 apply (p_ord_aux_to_not_mod_O (S(S m1)) (S(S m1)) (nth_prime p) q r).
351 apply lt_SO_nth_prime_n.
352 unfold lt.apply le_S_S.apply le_O_n.apply le_n.
353 assumption.
354 apply divides_to_mod_O.apply lt_O_nth_prime_n.assumption.
355 rewrite > times_n_SO in \vdash (? ? ? %).
356 rewrite < sym_times.
357 rewrite > (exp_n_O (nth_prime p)).
358 rewrite > H1 in \vdash (? ? ? (? (? ? %) ?)).
359 assumption.
360 apply le_to_or_lt_eq.apply le_O_n.assumption.
361 (* e adesso l'ultimo goal. TASSI: che ora non e' piu' l'ultimo :P *)
362 cut ((S O) < r \lor S O \nlt r).
363 elim Hcut2.
364 right. 
365 apply (p_ord_to_lt_max_prime_factor1 (S(S m1)) ? q r).
366 unfold lt.apply le_S_S. apply le_O_n.
367 apply le_n.
368 assumption.assumption.
369 cut (r=(S O)).
370 apply (nat_case p).
371 left.split.assumption.reflexivity.
372 intro.right.rewrite > Hcut3.
373 simplify.unfold lt.apply le_S_S.apply le_O_n.
374 cut (r \lt (S O) \or r=(S O)).
375 elim Hcut3.absurd (O=r).
376 apply le_n_O_to_eq.apply le_S_S_to_le.exact H2.
377 unfold Not.intro.
378 apply (not_le_Sn_O O).
379 rewrite > H3 in \vdash (? ? %).assumption.assumption.
380 apply (le_to_or_lt_eq r (S O)).
381 apply not_lt_to_le.assumption.
382 apply (decidable_lt (S O) r).
383 (* O < r *)
384 cut (O < r \lor O = r).
385 elim Hcut1.assumption. 
386 apply False_ind.
387 apply (not_eq_O_S (S m1)).
388 rewrite > Hcut.rewrite < H1.rewrite < times_n_O.reflexivity.
389 apply le_to_or_lt_eq.apply le_O_n.
390 (* prova del cut *)
391 goal 20.
392 apply (p_ord_aux_to_exp (S(S m1))).
393 apply lt_O_nth_prime_n.
394 assumption.
395 (* fine prova cut *)
396 qed.
397
398 let rec max_p f \def
399 match f with
400 [ (nf_last n) \Rightarrow O
401 | (nf_cons n g) \Rightarrow S (max_p g)].
402
403 let rec max_p_exponent f \def
404 match f with
405 [ (nf_last n) \Rightarrow n
406 | (nf_cons n g) \Rightarrow max_p_exponent g].
407
408 theorem divides_max_p_defactorize: \forall f:nat_fact.\forall i:nat. 
409 nth_prime ((max_p f)+i) \divides defactorize_aux f i.
410 intro.
411 elim f.simplify.apply (witness ? ? ((nth_prime i) \sup n)).
412 reflexivity.
413 change with 
414 (nth_prime (S(max_p n1)+i) \divides
415 (nth_prime i) \sup n *(defactorize_aux n1 (S i))).
416 elim (H (S i)).
417 rewrite > H1.
418 rewrite < sym_times.
419 rewrite > assoc_times.
420 rewrite < plus_n_Sm.
421 apply (witness ? ? (n2* (nth_prime i) \sup n)).
422 reflexivity.
423 qed.
424
425 theorem divides_exp_to_divides: 
426 \forall p,n,m:nat. prime p \to 
427 p \divides n \sup m \to p \divides n.
428 intros 3.elim m.simplify in H1.
429 apply (transitive_divides p (S O)).assumption.
430 apply divides_SO_n.
431 cut (p \divides n \lor p \divides n \sup n1).
432 elim Hcut.assumption.
433 apply H.assumption.assumption.
434 apply divides_times_to_divides.assumption.
435 exact H2.
436 qed.
437
438 theorem divides_exp_to_eq: 
439 \forall p,q,m:nat. prime p \to prime q \to
440 p \divides q \sup m \to p = q.
441 intros.
442 unfold prime in H1.
443 elim H1.apply H4.
444 apply (divides_exp_to_divides p q m).
445 assumption.assumption.
446 unfold prime in H.elim H.assumption.
447 qed.
448
449 theorem  not_divides_defactorize_aux: \forall f:nat_fact. \forall i,j:nat.
450 i < j \to nth_prime i \ndivides defactorize_aux f j.
451 intro.elim f.
452 change with
453 (nth_prime i \divides (nth_prime j) \sup (S n) \to False).
454 intro.absurd ((nth_prime i) = (nth_prime j)).
455 apply (divides_exp_to_eq ? ? (S n)).
456 apply prime_nth_prime.apply prime_nth_prime.
457 assumption.unfold Not.
458 intro.cut (i = j).
459 apply (not_le_Sn_n i).rewrite > Hcut in \vdash (? ? %).assumption.
460 apply (injective_nth_prime ? ? H2).
461 unfold Not.simplify.
462 intro.
463 cut (nth_prime i \divides (nth_prime j) \sup n
464 \lor nth_prime i \divides defactorize_aux n1 (S j)).
465 elim Hcut.
466 absurd ((nth_prime i) = (nth_prime j)).
467 apply (divides_exp_to_eq ? ? n).
468 apply prime_nth_prime.apply prime_nth_prime.
469 assumption.unfold Not.
470 intro.
471 cut (i = j).
472 apply (not_le_Sn_n i).rewrite > Hcut1 in \vdash (? ? %).assumption.
473 apply (injective_nth_prime ? ? H4).
474 apply (H i (S j)).
475 apply (trans_lt ? j).assumption.unfold lt.apply le_n.
476 assumption.
477 apply divides_times_to_divides.
478 apply prime_nth_prime.assumption.
479 qed.
480
481 lemma not_eq_nf_last_nf_cons: \forall g:nat_fact.\forall n,m,i:nat.
482 \lnot (defactorize_aux (nf_last n) i= defactorize_aux (nf_cons m g) i).
483 intros.
484 change with 
485 (exp (nth_prime i) (S n) = defactorize_aux (nf_cons m g) i \to False).
486 intro.
487 cut (S(max_p g)+i= i).
488 apply (not_le_Sn_n i).
489 rewrite < Hcut in \vdash (? ? %).
490 simplify.apply le_S_S.
491 apply le_plus_n.
492 apply injective_nth_prime.
493 apply (divides_exp_to_eq ? ? (S n)).
494 apply prime_nth_prime.apply prime_nth_prime.
495 rewrite > H.
496 change with (divides (nth_prime ((max_p (nf_cons m g))+i)) 
497 (defactorize_aux (nf_cons m g) i)).
498 apply divides_max_p_defactorize.
499 qed.
500
501 lemma not_eq_nf_cons_O_nf_cons: \forall f,g:nat_fact.\forall n,i:nat.
502 \lnot (defactorize_aux (nf_cons O f) i= defactorize_aux (nf_cons (S n) g) i).
503 intros.
504 simplify.unfold Not.rewrite < plus_n_O.
505 intro.
506 apply (not_divides_defactorize_aux f i (S i) ?).
507 unfold lt.apply le_n.
508 rewrite > H.
509 rewrite > assoc_times.
510 apply (witness ? ? ((exp (nth_prime i) n)*(defactorize_aux g (S i)))).
511 reflexivity.
512 qed.
513
514 theorem eq_defactorize_aux_to_eq: \forall f,g:nat_fact.\forall i:nat.
515 defactorize_aux f i = defactorize_aux g i \to f = g.
516 intro.
517 elim f.
518 generalize in match H.
519 elim g.
520 apply eq_f.
521 apply inj_S. apply (inj_exp_r (nth_prime i)).
522 apply lt_SO_nth_prime_n.
523 assumption.
524 apply False_ind.
525 apply (not_eq_nf_last_nf_cons n2 n n1 i H2).
526 generalize in match H1.
527 elim g.
528 apply False_ind.
529 apply (not_eq_nf_last_nf_cons n1 n2 n i).
530 apply sym_eq. assumption.
531 simplify in H3.
532 generalize in match H3.
533 apply (nat_elim2 (\lambda n,n2.
534 ((nth_prime i) \sup n)*(defactorize_aux n1 (S i)) =
535 ((nth_prime i) \sup n2)*(defactorize_aux n3 (S i)) \to
536 nf_cons n n1 = nf_cons n2 n3)).
537 intro.
538 elim n4. apply eq_f.
539 apply (H n3 (S i)).
540 simplify in H4.
541 rewrite > plus_n_O.
542 rewrite > (plus_n_O (defactorize_aux n3 (S i))).assumption.
543 apply False_ind.
544 apply (not_eq_nf_cons_O_nf_cons n1 n3 n5 i).assumption.
545 intros.
546 apply False_ind.
547 apply (not_eq_nf_cons_O_nf_cons n3 n1 n4 i).
548 apply sym_eq.assumption.
549 intros.
550 cut (nf_cons n4 n1 = nf_cons m n3).
551 cut (n4=m).
552 cut (n1=n3).
553 rewrite > Hcut1.rewrite > Hcut2.reflexivity.
554 change with 
555 (match nf_cons n4 n1 with
556 [ (nf_last m) \Rightarrow n1
557 | (nf_cons m g) \Rightarrow g ] = n3).
558 rewrite > Hcut.simplify.reflexivity.
559 change with 
560 (match nf_cons n4 n1 with
561 [ (nf_last m) \Rightarrow m
562 | (nf_cons m g) \Rightarrow m ] = m).
563 rewrite > Hcut.simplify.reflexivity.
564 apply H4.simplify in H5.
565 apply (inj_times_r1 (nth_prime i)).
566 apply lt_O_nth_prime_n.
567 rewrite < assoc_times.rewrite < assoc_times.assumption.
568 qed.
569
570 theorem injective_defactorize_aux: \forall i:nat.
571 injective nat_fact nat (\lambda f.defactorize_aux f i).
572 simplify.
573 intros.
574 apply (eq_defactorize_aux_to_eq x y i H).
575 qed.
576
577 theorem injective_defactorize: 
578 injective nat_fact_all nat defactorize.
579 unfold injective.
580 change with (\forall f,g.defactorize f = defactorize g \to f=g).
581 intro.elim f.
582 generalize in match H.elim g.
583 (* zero - zero *)
584 reflexivity.
585 (* zero - one *)
586 simplify in H1.
587 apply False_ind.
588 apply (not_eq_O_S O H1).
589 (* zero - proper *)
590 simplify in H1.
591 apply False_ind.
592 apply (not_le_Sn_n O).
593 rewrite > H1 in \vdash (? ? %).
594 change with (O < defactorize_aux n O).
595 apply lt_O_defactorize_aux.
596 generalize in match H.
597 elim g.
598 (* one - zero *)
599 simplify in H1.
600 apply False_ind.
601 apply (not_eq_O_S O).apply sym_eq. assumption.
602 (* one - one *)
603 reflexivity.
604 (* one - proper *)
605 simplify in H1.
606 apply False_ind.
607 apply (not_le_Sn_n (S O)).
608 rewrite > H1 in \vdash (? ? %).
609 change with ((S O) < defactorize_aux n O).
610 apply lt_SO_defactorize_aux.
611 generalize in match H.elim g.
612 (* proper - zero *)
613 simplify in H1.
614 apply False_ind.
615 apply (not_le_Sn_n O).
616 rewrite < H1 in \vdash (? ? %).
617 change with (O < defactorize_aux n O).
618 apply lt_O_defactorize_aux.
619 (* proper - one *)
620 simplify in H1.
621 apply False_ind.
622 apply (not_le_Sn_n (S O)).
623 rewrite < H1 in \vdash (? ? %).
624 change with ((S O) < defactorize_aux n O).
625 apply lt_SO_defactorize_aux.
626 (* proper - proper *)
627 apply eq_f.
628 apply (injective_defactorize_aux O).
629 exact H1.
630 qed.
631
632 theorem factorize_defactorize: 
633 \forall f,g: nat_fact_all. factorize (defactorize f) = f.
634 intros.
635 apply injective_defactorize.
636 apply defactorize_factorize.
637 qed.