src/HOL/Divides.thy
 author nipkow Mon Aug 16 14:22:27 2004 +0200 (2004-08-16) changeset 15131 c69542757a4d parent 14640 b31870c50c68 child 15140 322485b816ac permissions -rw-r--r--
1 (*  Title:      HOL/Divides.thy
2     ID:         \$Id\$
3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
4     Copyright   1999  University of Cambridge
6 The division operators div, mod and the divides relation "dvd"
7 *)
9 theory Divides
10 import NatArith
11 begin
13 (*We use the same class for div and mod;
14   moreover, dvd is defined whenever multiplication is*)
15 axclass
16   div < type
18 instance  nat :: div ..
20 consts
21   div  :: "'a::div \<Rightarrow> 'a \<Rightarrow> 'a"          (infixl 70)
22   mod  :: "'a::div \<Rightarrow> 'a \<Rightarrow> 'a"          (infixl 70)
23   dvd  :: "'a::times \<Rightarrow> 'a \<Rightarrow> bool"      (infixl 50)
26 defs
28   mod_def:   "m mod n == wfrec (trancl pred_nat)
29                           (%f j. if j<n | n=0 then j else f (j-n)) m"
31   div_def:   "m div n == wfrec (trancl pred_nat)
32                           (%f j. if j<n | n=0 then 0 else Suc (f (j-n))) m"
34 (*The definition of dvd is polymorphic!*)
35   dvd_def:   "m dvd n == \<exists>k. n = m*k"
37 (*This definition helps prove the harder properties of div and mod.
38   It is copied from IntDiv.thy; should it be overloaded?*)
39 constdefs
40   quorem :: "(nat*nat) * (nat*nat) => bool"
41     "quorem == %((a,b), (q,r)).
42                       a = b*q + r &
43                       (if 0<b then 0\<le>r & r<b else b<r & r \<le>0)"
47 subsection{*Initial Lemmas*}
49 lemmas wf_less_trans =
50        def_wfrec [THEN trans, OF eq_reflection wf_pred_nat [THEN wf_trancl],
51                   standard]
53 lemma mod_eq: "(%m. m mod n) =
54               wfrec (trancl pred_nat) (%f j. if j<n | n=0 then j else f (j-n))"
57 lemma div_eq: "(%m. m div n) = wfrec (trancl pred_nat)
58                (%f j. if j<n | n=0 then 0 else Suc (f (j-n)))"
62 (** Aribtrary definitions for division by zero.  Useful to simplify
63     certain equations **)
65 lemma DIVISION_BY_ZERO_DIV [simp]: "a div 0 = (0::nat)"
66 by (rule div_eq [THEN wf_less_trans], simp)
68 lemma DIVISION_BY_ZERO_MOD [simp]: "a mod 0 = (a::nat)"
69 by (rule mod_eq [THEN wf_less_trans], simp)
72 subsection{*Remainder*}
74 lemma mod_less [simp]: "m<n ==> m mod n = (m::nat)"
75 by (rule mod_eq [THEN wf_less_trans], simp)
77 lemma mod_geq: "~ m < (n::nat) ==> m mod n = (m-n) mod n"
78 apply (case_tac "n=0", simp)
79 apply (rule mod_eq [THEN wf_less_trans])
80 apply (simp add: diff_less cut_apply less_eq)
81 done
83 (*Avoids the ugly ~m<n above*)
84 lemma le_mod_geq: "(n::nat) \<le> m ==> m mod n = (m-n) mod n"
85 by (simp add: mod_geq not_less_iff_le)
87 lemma mod_if: "m mod (n::nat) = (if m<n then m else (m-n) mod n)"
90 lemma mod_1 [simp]: "m mod Suc 0 = 0"
91 apply (induct_tac "m")
92 apply (simp_all (no_asm_simp) add: mod_geq)
93 done
95 lemma mod_self [simp]: "n mod n = (0::nat)"
96 apply (case_tac "n=0")
98 done
100 lemma mod_add_self2 [simp]: "(m+n) mod n = m mod (n::nat)"
101 apply (subgoal_tac " (n + m) mod n = (n+m-n) mod n")
103 apply (subst mod_geq [symmetric], simp_all)
104 done
106 lemma mod_add_self1 [simp]: "(n+m) mod n = m mod (n::nat)"
109 lemma mod_mult_self1 [simp]: "(m + k*n) mod n = m mod (n::nat)"
110 apply (induct_tac "k")
112 done
114 lemma mod_mult_self2 [simp]: "(m + n*k) mod n = m mod (n::nat)"
115 by (simp add: mult_commute mod_mult_self1)
117 lemma mod_mult_distrib: "(m mod n) * (k::nat) = (m*k) mod (n*k)"
118 apply (case_tac "n=0", simp)
119 apply (case_tac "k=0", simp)
120 apply (induct_tac "m" rule: nat_less_induct)
121 apply (subst mod_if, simp)
122 apply (simp add: mod_geq diff_less diff_mult_distrib)
123 done
125 lemma mod_mult_distrib2: "(k::nat) * (m mod n) = (k*m) mod (k*n)"
126 by (simp add: mult_commute [of k] mod_mult_distrib)
128 lemma mod_mult_self_is_0 [simp]: "(m*n) mod n = (0::nat)"
129 apply (case_tac "n=0", simp)
130 apply (induct_tac "m", simp)
131 apply (rename_tac "k")
132 apply (cut_tac m = "k*n" and n = n in mod_add_self2)
134 done
136 lemma mod_mult_self1_is_0 [simp]: "(n*m) mod n = (0::nat)"
137 by (simp add: mult_commute mod_mult_self_is_0)
140 subsection{*Quotient*}
142 lemma div_less [simp]: "m<n ==> m div n = (0::nat)"
143 by (rule div_eq [THEN wf_less_trans], simp)
145 lemma div_geq: "[| 0<n;  ~m<n |] ==> m div n = Suc((m-n) div n)"
146 apply (rule div_eq [THEN wf_less_trans])
147 apply (simp add: diff_less cut_apply less_eq)
148 done
150 (*Avoids the ugly ~m<n above*)
151 lemma le_div_geq: "[| 0<n;  n\<le>m |] ==> m div n = Suc((m-n) div n)"
152 by (simp add: div_geq not_less_iff_le)
154 lemma div_if: "0<n ==> m div n = (if m<n then 0 else Suc((m-n) div n))"
158 (*Main Result about quotient and remainder.*)
159 lemma mod_div_equality: "(m div n)*n + m mod n = (m::nat)"
160 apply (case_tac "n=0", simp)
161 apply (induct_tac "m" rule: nat_less_induct)
162 apply (subst mod_if)
164 done
166 lemma mod_div_equality2: "n * (m div n) + m mod n = (m::nat)"
167 apply(cut_tac m = m and n = n in mod_div_equality)
169 done
171 subsection{*Simproc for Cancelling Div and Mod*}
173 lemma div_mod_equality: "((m div n)*n + m mod n) + k = (m::nat) + k"
175 done
177 lemma div_mod_equality2: "(n*(m div n) + m mod n) + k = (m::nat) + k"
179 done
181 ML
182 {*
183 val div_mod_equality = thm "div_mod_equality";
184 val div_mod_equality2 = thm "div_mod_equality2";
187 structure CancelDivModData =
188 struct
190 val div_name = "Divides.op div";
191 val mod_name = "Divides.op mod";
192 val mk_binop = HOLogic.mk_binop;
193 val mk_sum = NatArithUtils.mk_sum;
194 val dest_sum = NatArithUtils.dest_sum;
196 (*logic*)
198 val div_mod_eqs = map mk_meta_eq [div_mod_equality,div_mod_equality2]
200 val trans = trans
202 val prove_eq_sums =
204   in NatArithUtils.prove_conv all_tac (NatArithUtils.simp_all simps) end
206 end;
208 structure CancelDivMod = CancelDivModFun(CancelDivModData);
210 val cancel_div_mod_proc = NatArithUtils.prep_simproc
211       ("cancel_div_mod", ["(m::nat) + n"], CancelDivMod.proc);
214 *}
217 (* a simple rearrangement of mod_div_equality: *)
218 lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
219 by (cut_tac m = m and n = n in mod_div_equality2, arith)
221 lemma mod_less_divisor [simp]: "0<n ==> m mod n < (n::nat)"
222 apply (induct_tac "m" rule: nat_less_induct)
223 apply (case_tac "na<n", simp)
224 txt{*case @{term "n \<le> na"}*}
225 apply (simp add: mod_geq diff_less)
226 done
228 lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
229 by (cut_tac m = "m*n" and n = n in mod_div_equality, auto)
231 lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
232 by (simp add: mult_commute div_mult_self_is_m)
234 (*mod_mult_distrib2 above is the counterpart for remainder*)
237 subsection{*Proving facts about Quotient and Remainder*}
239 lemma unique_quotient_lemma:
240      "[| b*q' + r'  \<le> b*q + r;  0 < b;  r < b |]
241       ==> q' \<le> (q::nat)"
242 apply (rule leI)
245 done
247 lemma unique_quotient:
248      "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  0 < b |]
249       ==> q = q'"
250 apply (simp add: split_ifs quorem_def)
251 apply (blast intro: order_antisym
252              dest: order_eq_refl [THEN unique_quotient_lemma] sym)+
253 done
255 lemma unique_remainder:
256      "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  0 < b |]
257       ==> r = r'"
258 apply (subgoal_tac "q = q'")
259 prefer 2 apply (blast intro: unique_quotient)
261 done
263 lemma quorem_div_mod: "0 < b ==> quorem ((a, b), (a div b, a mod b))"
264 by (auto simp add: quorem_def)
266 lemma quorem_div: "[| quorem((a,b),(q,r));  0 < b |] ==> a div b = q"
267 by (simp add: quorem_div_mod [THEN unique_quotient])
269 lemma quorem_mod: "[| quorem((a,b),(q,r));  0 < b |] ==> a mod b = r"
270 by (simp add: quorem_div_mod [THEN unique_remainder])
272 (** A dividend of zero **)
274 lemma div_0 [simp]: "0 div m = (0::nat)"
275 by (case_tac "m=0", simp_all)
277 lemma mod_0 [simp]: "0 mod m = (0::nat)"
278 by (case_tac "m=0", simp_all)
280 (** proving (a*b) div c = a * (b div c) + a * (b mod c) **)
282 lemma quorem_mult1_eq:
283      "[| quorem((b,c),(q,r));  0 < c |]
284       ==> quorem ((a*b, c), (a*q + a*r div c, a*r mod c))"
286 done
288 lemma div_mult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::nat)"
289 apply (case_tac "c = 0", simp)
290 apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_div])
291 done
293 lemma mod_mult1_eq: "(a*b) mod c = a*(b mod c) mod (c::nat)"
294 apply (case_tac "c = 0", simp)
295 apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_mod])
296 done
298 lemma mod_mult1_eq': "(a*b) mod (c::nat) = ((a mod c) * b) mod c"
299 apply (rule trans)
300 apply (rule_tac s = "b*a mod c" in trans)
301 apply (rule_tac [2] mod_mult1_eq)
302 apply (simp_all (no_asm) add: mult_commute)
303 done
305 lemma mod_mult_distrib_mod: "(a*b) mod (c::nat) = ((a mod c) * (b mod c)) mod c"
306 apply (rule mod_mult1_eq' [THEN trans])
307 apply (rule mod_mult1_eq)
308 done
310 (** proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) **)
313      "[| quorem((a,c),(aq,ar));  quorem((b,c),(bq,br));  0 < c |]
314       ==> quorem ((a+b, c), (aq + bq + (ar+br) div c, (ar+br) mod c))"
317 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
319      "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
320 apply (case_tac "c = 0", simp)
321 apply (blast intro: quorem_add1_eq [THEN quorem_div] quorem_div_mod quorem_div_mod)
322 done
324 lemma mod_add1_eq: "(a+b) mod (c::nat) = (a mod c + b mod c) mod c"
325 apply (case_tac "c = 0", simp)
326 apply (blast intro: quorem_div_mod quorem_div_mod
328 done
331 subsection{*Proving @{term "a div (b*c) = (a div b) div c"}*}
333 (** first, a lemma to bound the remainder **)
335 lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
336 apply (cut_tac m = q and n = c in mod_less_divisor)
337 apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
338 apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
340 done
342 lemma quorem_mult2_eq: "[| quorem ((a,b), (q,r));  0 < b;  0 < c |]
343       ==> quorem ((a, b*c), (q div c, b*(q mod c) + r))"
345 done
347 lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
348 apply (case_tac "b=0", simp)
349 apply (case_tac "c=0", simp)
350 apply (force simp add: quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_div])
351 done
353 lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
354 apply (case_tac "b=0", simp)
355 apply (case_tac "c=0", simp)
356 apply (auto simp add: mult_commute quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_mod])
357 done
360 subsection{*Cancellation of Common Factors in Division*}
362 lemma div_mult_mult_lemma:
363      "[| (0::nat) < b;  0 < c |] ==> (c*a) div (c*b) = a div b"
364 by (auto simp add: div_mult2_eq)
366 lemma div_mult_mult1 [simp]: "(0::nat) < c ==> (c*a) div (c*b) = a div b"
367 apply (case_tac "b = 0")
368 apply (auto simp add: linorder_neq_iff [of b] div_mult_mult_lemma)
369 done
371 lemma div_mult_mult2 [simp]: "(0::nat) < c ==> (a*c) div (b*c) = a div b"
372 apply (drule div_mult_mult1)
373 apply (auto simp add: mult_commute)
374 done
377 (*Distribution of Factors over Remainders:
379 Could prove these as in Integ/IntDiv.ML, but we already have
380 mod_mult_distrib and mod_mult_distrib2 above!
382 Goal "(c*a) mod (c*b) = (c::nat) * (a mod b)"
383 qed "mod_mult_mult1";
385 Goal "(a*c) mod (b*c) = (a mod b) * (c::nat)";
386 qed "mod_mult_mult2";
387  ***)
389 subsection{*Further Facts about Quotient and Remainder*}
391 lemma div_1 [simp]: "m div Suc 0 = m"
392 apply (induct_tac "m")
393 apply (simp_all (no_asm_simp) add: div_geq)
394 done
396 lemma div_self [simp]: "0<n ==> n div n = (1::nat)"
399 lemma div_add_self2: "0<n ==> (m+n) div n = Suc (m div n)"
400 apply (subgoal_tac " (n + m) div n = Suc ((n+m-n) div n) ")
402 apply (subst div_geq [symmetric], simp_all)
403 done
405 lemma div_add_self1: "0<n ==> (n+m) div n = Suc (m div n)"
408 lemma div_mult_self1 [simp]: "!!n::nat. 0<n ==> (m + k*n) div n = k + m div n"
410 apply (subst div_mult1_eq, simp)
411 done
413 lemma div_mult_self2 [simp]: "0<n ==> (m + n*k) div n = k + m div (n::nat)"
414 by (simp add: mult_commute div_mult_self1)
417 (* Monotonicity of div in first argument *)
418 lemma div_le_mono [rule_format (no_asm)]:
419      "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
420 apply (case_tac "k=0", simp)
421 apply (induct_tac "n" rule: nat_less_induct, clarify)
422 apply (case_tac "n<k")
423 (* 1  case n<k *)
424 apply simp
425 (* 2  case n >= k *)
426 apply (case_tac "m<k")
427 (* 2.1  case m<k *)
428 apply simp
429 (* 2.2  case m>=k *)
430 apply (simp add: div_geq diff_less diff_le_mono)
431 done
433 (* Antimonotonicity of div in second argument *)
434 lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
435 apply (subgoal_tac "0<n")
436  prefer 2 apply simp
437 apply (induct_tac "k" rule: nat_less_induct)
438 apply (rename_tac "k")
439 apply (case_tac "k<n", simp)
440 apply (subgoal_tac "~ (k<m) ")
441  prefer 2 apply simp
443 apply (subgoal_tac " (k-n) div n \<le> (k-m) div n")
444  prefer 2
445  apply (blast intro: div_le_mono diff_le_mono2)
446 apply (rule le_trans, simp)
448 done
450 lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
451 apply (case_tac "n=0", simp)
452 apply (subgoal_tac "m div n \<le> m div 1", simp)
453 apply (rule div_le_mono2)
454 apply (simp_all (no_asm_simp))
455 done
457 (* Similar for "less than" *)
458 lemma div_less_dividend [rule_format, simp]:
459      "!!n::nat. 1<n ==> 0 < m --> m div n < m"
460 apply (induct_tac "m" rule: nat_less_induct)
461 apply (rename_tac "m")
462 apply (case_tac "m<n", simp)
463 apply (subgoal_tac "0<n")
464  prefer 2 apply simp
466 apply (case_tac "n<m")
467  apply (subgoal_tac " (m-n) div n < (m-n) ")
468   apply (rule impI less_trans_Suc)+
469 apply assumption
471 done
473 text{*A fact for the mutilated chess board*}
474 lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
475 apply (case_tac "n=0", simp)
476 apply (induct_tac "m" rule: nat_less_induct)
477 apply (case_tac "Suc (na) <n")
478 (* case Suc(na) < n *)
479 apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
480 (* case n \<le> Suc(na) *)
481 apply (simp add: not_less_iff_le le_Suc_eq mod_geq)
482 apply (auto simp add: Suc_diff_le diff_less le_mod_geq)
483 done
485 lemma nat_mod_div_trivial [simp]: "m mod n div n = (0 :: nat)"
486 by (case_tac "n=0", auto)
488 lemma nat_mod_mod_trivial [simp]: "m mod n mod n = (m mod n :: nat)"
489 by (case_tac "n=0", auto)
492 subsection{*The Divides Relation*}
494 lemma dvdI [intro?]: "n = m * k ==> m dvd n"
495 by (unfold dvd_def, blast)
497 lemma dvdE [elim?]: "!!P. [|m dvd n;  !!k. n = m*k ==> P|] ==> P"
498 by (unfold dvd_def, blast)
500 lemma dvd_0_right [iff]: "m dvd (0::nat)"
501 apply (unfold dvd_def)
502 apply (blast intro: mult_0_right [symmetric])
503 done
505 lemma dvd_0_left: "0 dvd m ==> m = (0::nat)"
506 by (force simp add: dvd_def)
508 lemma dvd_0_left_iff [iff]: "(0 dvd (m::nat)) = (m = 0)"
509 by (blast intro: dvd_0_left)
511 lemma dvd_1_left [iff]: "Suc 0 dvd k"
512 by (unfold dvd_def, simp)
514 lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)"
517 lemma dvd_refl [simp]: "m dvd (m::nat)"
518 apply (unfold dvd_def)
519 apply (blast intro: mult_1_right [symmetric])
520 done
522 lemma dvd_trans [trans]: "[| m dvd n; n dvd p |] ==> m dvd (p::nat)"
523 apply (unfold dvd_def)
524 apply (blast intro: mult_assoc)
525 done
527 lemma dvd_anti_sym: "[| m dvd n; n dvd m |] ==> m = (n::nat)"
528 apply (unfold dvd_def)
529 apply (force dest: mult_eq_self_implies_10 simp add: mult_assoc mult_eq_1_iff)
530 done
532 lemma dvd_add: "[| k dvd m; k dvd n |] ==> k dvd (m+n :: nat)"
533 apply (unfold dvd_def)
534 apply (blast intro: add_mult_distrib2 [symmetric])
535 done
537 lemma dvd_diff: "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)"
538 apply (unfold dvd_def)
539 apply (blast intro: diff_mult_distrib2 [symmetric])
540 done
542 lemma dvd_diffD: "[| k dvd m-n; k dvd n; n\<le>m |] ==> k dvd (m::nat)"
543 apply (erule not_less_iff_le [THEN iffD2, THEN add_diff_inverse, THEN subst])
545 done
547 lemma dvd_diffD1: "[| k dvd m-n; k dvd m; n\<le>m |] ==> k dvd (n::nat)"
548 by (drule_tac m = m in dvd_diff, auto)
550 lemma dvd_mult: "k dvd n ==> k dvd (m*n :: nat)"
551 apply (unfold dvd_def)
552 apply (blast intro: mult_left_commute)
553 done
555 lemma dvd_mult2: "k dvd m ==> k dvd (m*n :: nat)"
556 apply (subst mult_commute)
557 apply (erule dvd_mult)
558 done
560 (* k dvd (m*k) *)
561 declare dvd_refl [THEN dvd_mult, iff] dvd_refl [THEN dvd_mult2, iff]
563 lemma dvd_reduce: "(k dvd n + k) = (k dvd (n::nat))"
564 apply (rule iffI)
566 apply (rule_tac [2] dvd_refl)
567 apply (subgoal_tac "n = (n+k) -k")
568  prefer 2 apply simp
569 apply (erule ssubst)
570 apply (erule dvd_diff)
571 apply (rule dvd_refl)
572 done
574 lemma dvd_mod: "!!n::nat. [| f dvd m; f dvd n |] ==> f dvd m mod n"
575 apply (unfold dvd_def)
576 apply (case_tac "n=0", auto)
577 apply (blast intro: mod_mult_distrib2 [symmetric])
578 done
580 lemma dvd_mod_imp_dvd: "[| (k::nat) dvd m mod n;  k dvd n |] ==> k dvd m"
581 apply (subgoal_tac "k dvd (m div n) *n + m mod n")
583 apply (simp only: dvd_add dvd_mult)
584 done
586 lemma dvd_mod_iff: "k dvd n ==> ((k::nat) dvd m mod n) = (k dvd m)"
587 by (blast intro: dvd_mod_imp_dvd dvd_mod)
589 lemma dvd_mult_cancel: "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n"
590 apply (unfold dvd_def)
591 apply (erule exE)
593 done
595 lemma dvd_mult_cancel1: "0<m ==> (m*n dvd m) = (n = (1::nat))"
596 apply auto
597 apply (subgoal_tac "m*n dvd m*1")
598 apply (drule dvd_mult_cancel, auto)
599 done
601 lemma dvd_mult_cancel2: "0<m ==> (n*m dvd m) = (n = (1::nat))"
602 apply (subst mult_commute)
603 apply (erule dvd_mult_cancel1)
604 done
606 lemma mult_dvd_mono: "[| i dvd m; j dvd n|] ==> i*j dvd (m*n :: nat)"
607 apply (unfold dvd_def, clarify)
608 apply (rule_tac x = "k*ka" in exI)
610 done
612 lemma dvd_mult_left: "(i*j :: nat) dvd k ==> i dvd k"
613 by (simp add: dvd_def mult_assoc, blast)
615 lemma dvd_mult_right: "(i*j :: nat) dvd k ==> j dvd k"
616 apply (unfold dvd_def, clarify)
617 apply (rule_tac x = "i*k" in exI)
619 done
621 lemma dvd_imp_le: "[| k dvd n; 0 < n |] ==> k \<le> (n::nat)"
622 apply (unfold dvd_def, clarify)
623 apply (simp_all (no_asm_use) add: zero_less_mult_iff)
624 apply (erule conjE)
625 apply (rule le_trans)
626 apply (rule_tac [2] le_refl [THEN mult_le_mono])
627 apply (erule_tac [2] Suc_leI, simp)
628 done
630 lemma dvd_eq_mod_eq_0: "!!k::nat. (k dvd n) = (n mod k = 0)"
631 apply (unfold dvd_def)
632 apply (case_tac "k=0", simp, safe)
634 apply (rule_tac t = n and n1 = k in mod_div_equality [THEN subst])
635 apply (subst mult_commute, simp)
636 done
638 lemma dvd_mult_div_cancel: "n dvd m ==> n * (m div n) = (m::nat)"
639 apply (subgoal_tac "m mod n = 0")
641 apply (simp only: dvd_eq_mod_eq_0)
642 done
644 lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
645 by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
646 declare mod_eq_0_iff [THEN iffD1, dest!]
648 (*Loses information, namely we also have r<d provided d is nonzero*)
649 lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
650 apply (cut_tac m = m in mod_div_equality)
652 apply (blast intro: sym)
653 done
656 lemma split_div:
657  "P(n div k :: nat) =
658  ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
659  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
660 proof
661   assume P: ?P
662   show ?Q
663   proof (cases)
664     assume "k = 0"
665     with P show ?Q by(simp add:DIVISION_BY_ZERO_DIV)
666   next
667     assume not0: "k \<noteq> 0"
668     thus ?Q
669     proof (simp, intro allI impI)
670       fix i j
671       assume n: "n = k*i + j" and j: "j < k"
672       show "P i"
673       proof (cases)
674 	assume "i = 0"
675 	with n j P show "P i" by simp
676       next
677 	assume "i \<noteq> 0"
679       qed
680     qed
681   qed
682 next
683   assume Q: ?Q
684   show ?P
685   proof (cases)
686     assume "k = 0"
687     with Q show ?P by(simp add:DIVISION_BY_ZERO_DIV)
688   next
689     assume not0: "k \<noteq> 0"
690     with Q have R: ?R by simp
691     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
692     show ?P by simp
693   qed
694 qed
696 lemma split_div_lemma:
697   "0 < n \<Longrightarrow> (n * q \<le> m \<and> m < n * (Suc q)) = (q = ((m::nat) div n))"
698   apply (rule iffI)
699   apply (rule_tac a=m and r = "m - n * q" and r' = "m mod n" in unique_quotient)
700   apply (simp_all add: quorem_def, arith)
701   apply (rule conjI)
702   apply (rule_tac P="%x. n * (m div n) \<le> x" in
703     subst [OF mod_div_equality [of _ n]])
704   apply (simp only: add: mult_ac)
705   apply (rule_tac P="%x. x < n + n * (m div n)" in
706     subst [OF mod_div_equality [of _ n]])
709   done
711 theorem split_div':
712   "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
713    (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
714   apply (case_tac "0 < n")
715   apply (simp only: add: split_div_lemma)
717   done
719 lemma split_mod:
720  "P(n mod k :: nat) =
721  ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
722  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
723 proof
724   assume P: ?P
725   show ?Q
726   proof (cases)
727     assume "k = 0"
728     with P show ?Q by(simp add:DIVISION_BY_ZERO_MOD)
729   next
730     assume not0: "k \<noteq> 0"
731     thus ?Q
732     proof (simp, intro allI impI)
733       fix i j
734       assume "n = k*i + j" "j < k"
736     qed
737   qed
738 next
739   assume Q: ?Q
740   show ?P
741   proof (cases)
742     assume "k = 0"
743     with Q show ?P by(simp add:DIVISION_BY_ZERO_MOD)
744   next
745     assume not0: "k \<noteq> 0"
746     with Q have R: ?R by simp
747     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
748     show ?P by simp
749   qed
750 qed
752 theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
753   apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
754     subst [OF mod_div_equality [of _ n]])
755   apply arith
756   done
758 subsection {*An ``induction'' law for modulus arithmetic.*}
760 lemma mod_induct_0:
761   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
762   and base: "P i" and i: "i<p"
763   shows "P 0"
764 proof (rule ccontr)
765   assume contra: "\<not>(P 0)"
766   from i have p: "0<p" by simp
767   have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
768   proof
769     fix k
770     show "?A k"
771     proof (induct k)
772       show "?A 0" by simp  -- "by contradiction"
773     next
774       fix n
775       assume ih: "?A n"
776       show "?A (Suc n)"
777       proof (clarsimp)
778 	assume y: "P (p - Suc n)"
779 	have n: "Suc n < p"
780 	proof (rule ccontr)
781 	  assume "\<not>(Suc n < p)"
782 	  hence "p - Suc n = 0"
783 	    by simp
784 	  with y contra show "False"
785 	    by simp
786 	qed
787 	hence n2: "Suc (p - Suc n) = p-n" by arith
788 	from p have "p - Suc n < p" by arith
789 	with y step have z: "P ((Suc (p - Suc n)) mod p)"
790 	  by blast
791 	show "False"
792 	proof (cases "n=0")
793 	  case True
794 	  with z n2 contra show ?thesis by simp
795 	next
796 	  case False
797 	  with p have "p-n < p" by arith
798 	  with z n2 False ih show ?thesis by simp
799 	qed
800       qed
801     qed
802   qed
803   moreover
804   from i obtain k where "0<k \<and> i+k=p"
806   hence "0<k \<and> i=p-k" by auto
807   moreover
808   note base
809   ultimately
810   show "False" by blast
811 qed
813 lemma mod_induct:
814   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
815   and base: "P i" and i: "i<p" and j: "j<p"
816   shows "P j"
817 proof -
818   have "\<forall>j<p. P j"
819   proof
820     fix j
821     show "j<p \<longrightarrow> P j" (is "?A j")
822     proof (induct j)
823       from step base i show "?A 0"
824 	by (auto elim: mod_induct_0)
825     next
826       fix k
827       assume ih: "?A k"
828       show "?A (Suc k)"
829       proof
830 	assume suc: "Suc k < p"
831 	hence k: "k<p" by simp
832 	with ih have "P k" ..
833 	with step k have "P (Suc k mod p)"
834 	  by blast
835 	moreover
836 	from suc have "Suc k mod p = Suc k"
837 	  by simp
838 	ultimately
839 	show "P (Suc k)" by simp
840       qed
841     qed
842   qed
843   with j show ?thesis by blast
844 qed
847 ML
848 {*
849 val div_def = thm "div_def"
850 val mod_def = thm "mod_def"
851 val dvd_def = thm "dvd_def"
852 val quorem_def = thm "quorem_def"
854 val wf_less_trans = thm "wf_less_trans";
855 val mod_eq = thm "mod_eq";
856 val div_eq = thm "div_eq";
857 val DIVISION_BY_ZERO_DIV = thm "DIVISION_BY_ZERO_DIV";
858 val DIVISION_BY_ZERO_MOD = thm "DIVISION_BY_ZERO_MOD";
859 val mod_less = thm "mod_less";
860 val mod_geq = thm "mod_geq";
861 val le_mod_geq = thm "le_mod_geq";
862 val mod_if = thm "mod_if";
863 val mod_1 = thm "mod_1";
864 val mod_self = thm "mod_self";
867 val mod_mult_self1 = thm "mod_mult_self1";
868 val mod_mult_self2 = thm "mod_mult_self2";
869 val mod_mult_distrib = thm "mod_mult_distrib";
870 val mod_mult_distrib2 = thm "mod_mult_distrib2";
871 val mod_mult_self_is_0 = thm "mod_mult_self_is_0";
872 val mod_mult_self1_is_0 = thm "mod_mult_self1_is_0";
873 val div_less = thm "div_less";
874 val div_geq = thm "div_geq";
875 val le_div_geq = thm "le_div_geq";
876 val div_if = thm "div_if";
877 val mod_div_equality = thm "mod_div_equality";
878 val mod_div_equality2 = thm "mod_div_equality2";
879 val div_mod_equality = thm "div_mod_equality";
880 val div_mod_equality2 = thm "div_mod_equality2";
881 val mult_div_cancel = thm "mult_div_cancel";
882 val mod_less_divisor = thm "mod_less_divisor";
883 val div_mult_self_is_m = thm "div_mult_self_is_m";
884 val div_mult_self1_is_m = thm "div_mult_self1_is_m";
885 val unique_quotient_lemma = thm "unique_quotient_lemma";
886 val unique_quotient = thm "unique_quotient";
887 val unique_remainder = thm "unique_remainder";
888 val div_0 = thm "div_0";
889 val mod_0 = thm "mod_0";
890 val div_mult1_eq = thm "div_mult1_eq";
891 val mod_mult1_eq = thm "mod_mult1_eq";
892 val mod_mult1_eq' = thm "mod_mult1_eq'";
893 val mod_mult_distrib_mod = thm "mod_mult_distrib_mod";
896 val mod_lemma = thm "mod_lemma";
897 val div_mult2_eq = thm "div_mult2_eq";
898 val mod_mult2_eq = thm "mod_mult2_eq";
899 val div_mult_mult_lemma = thm "div_mult_mult_lemma";
900 val div_mult_mult1 = thm "div_mult_mult1";
901 val div_mult_mult2 = thm "div_mult_mult2";
902 val div_1 = thm "div_1";
903 val div_self = thm "div_self";
906 val div_mult_self1 = thm "div_mult_self1";
907 val div_mult_self2 = thm "div_mult_self2";
908 val div_le_mono = thm "div_le_mono";
909 val div_le_mono2 = thm "div_le_mono2";
910 val div_le_dividend = thm "div_le_dividend";
911 val div_less_dividend = thm "div_less_dividend";
912 val mod_Suc = thm "mod_Suc";
913 val dvdI = thm "dvdI";
914 val dvdE = thm "dvdE";
915 val dvd_0_right = thm "dvd_0_right";
916 val dvd_0_left = thm "dvd_0_left";
917 val dvd_0_left_iff = thm "dvd_0_left_iff";
918 val dvd_1_left = thm "dvd_1_left";
919 val dvd_1_iff_1 = thm "dvd_1_iff_1";
920 val dvd_refl = thm "dvd_refl";
921 val dvd_trans = thm "dvd_trans";
922 val dvd_anti_sym = thm "dvd_anti_sym";
924 val dvd_diff = thm "dvd_diff";
925 val dvd_diffD = thm "dvd_diffD";
926 val dvd_diffD1 = thm "dvd_diffD1";
927 val dvd_mult = thm "dvd_mult";
928 val dvd_mult2 = thm "dvd_mult2";
929 val dvd_reduce = thm "dvd_reduce";
930 val dvd_mod = thm "dvd_mod";
931 val dvd_mod_imp_dvd = thm "dvd_mod_imp_dvd";
932 val dvd_mod_iff = thm "dvd_mod_iff";
933 val dvd_mult_cancel = thm "dvd_mult_cancel";
934 val dvd_mult_cancel1 = thm "dvd_mult_cancel1";
935 val dvd_mult_cancel2 = thm "dvd_mult_cancel2";
936 val mult_dvd_mono = thm "mult_dvd_mono";
937 val dvd_mult_left = thm "dvd_mult_left";
938 val dvd_mult_right = thm "dvd_mult_right";
939 val dvd_imp_le = thm "dvd_imp_le";
940 val dvd_eq_mod_eq_0 = thm "dvd_eq_mod_eq_0";
941 val dvd_mult_div_cancel = thm "dvd_mult_div_cancel";
942 val mod_eq_0_iff = thm "mod_eq_0_iff";
943 val mod_eqD = thm "mod_eqD";
944 *}
947 (*
948 lemma split_div:
949 assumes m: "m \<noteq> 0"
950 shows "P(n div m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P i)"
951        (is "?P = ?Q")
952 proof
953   assume P: ?P
954   show ?Q
955   proof (intro allI impI)
956     fix i j
957     assume n: "n = m*i + j" and j: "j < m"
958     show "P i"
959     proof (cases)
960       assume "i = 0"
961       with n j P show "P i" by simp
962     next
963       assume "i \<noteq> 0"
964       with n j P show "P i" by (simp add:add_ac div_mult_self1)
965     qed
966   qed
967 next
968   assume Q: ?Q
969   from m Q[THEN spec,of "n div m",THEN spec, of "n mod m"]
970   show ?P by simp
971 qed
973 lemma split_mod:
974 assumes m: "m \<noteq> 0"
975 shows "P(n mod m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P j)"
976        (is "?P = ?Q")
977 proof
978   assume P: ?P
979   show ?Q
980   proof (intro allI impI)
981     fix i j
982     assume "n = m*i + j" "j < m"