src/HOL/Divides.thy
 author paulson Tue Nov 25 10:37:03 2003 +0100 (2003-11-25) changeset 14267 b963e9cee2a0 parent 14208 144f45277d5a child 14430 5cb24165a2e1 permissions -rw-r--r--
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
to Isar script.
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 = NatArith:
11 (*We use the same class for div and mod;
12   moreover, dvd is defined whenever multiplication is*)
13 axclass
14   div < type
16 instance  nat :: div ..
17 instance  nat :: plus_ac0
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
486 subsection{*The Divides Relation*}
488 lemma dvdI [intro?]: "n = m * k ==> m dvd n"
489 by (unfold dvd_def, blast)
491 lemma dvdE [elim?]: "!!P. [|m dvd n;  !!k. n = m*k ==> P|] ==> P"
492 by (unfold dvd_def, blast)
494 lemma dvd_0_right [iff]: "m dvd (0::nat)"
495 apply (unfold dvd_def)
496 apply (blast intro: mult_0_right [symmetric])
497 done
499 lemma dvd_0_left: "0 dvd m ==> m = (0::nat)"
500 by (force simp add: dvd_def)
502 lemma dvd_0_left_iff [iff]: "(0 dvd (m::nat)) = (m = 0)"
503 by (blast intro: dvd_0_left)
505 lemma dvd_1_left [iff]: "Suc 0 dvd k"
506 by (unfold dvd_def, simp)
508 lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)"
511 lemma dvd_refl [simp]: "m dvd (m::nat)"
512 apply (unfold dvd_def)
513 apply (blast intro: mult_1_right [symmetric])
514 done
516 lemma dvd_trans [trans]: "[| m dvd n; n dvd p |] ==> m dvd (p::nat)"
517 apply (unfold dvd_def)
518 apply (blast intro: mult_assoc)
519 done
521 lemma dvd_anti_sym: "[| m dvd n; n dvd m |] ==> m = (n::nat)"
522 apply (unfold dvd_def)
523 apply (force dest: mult_eq_self_implies_10 simp add: mult_assoc mult_eq_1_iff)
524 done
526 lemma dvd_add: "[| k dvd m; k dvd n |] ==> k dvd (m+n :: nat)"
527 apply (unfold dvd_def)
528 apply (blast intro: add_mult_distrib2 [symmetric])
529 done
531 lemma dvd_diff: "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)"
532 apply (unfold dvd_def)
533 apply (blast intro: diff_mult_distrib2 [symmetric])
534 done
536 lemma dvd_diffD: "[| k dvd m-n; k dvd n; n\<le>m |] ==> k dvd (m::nat)"
537 apply (erule not_less_iff_le [THEN iffD2, THEN add_diff_inverse, THEN subst])
539 done
541 lemma dvd_diffD1: "[| k dvd m-n; k dvd m; n\<le>m |] ==> k dvd (n::nat)"
542 by (drule_tac m = m in dvd_diff, auto)
544 lemma dvd_mult: "k dvd n ==> k dvd (m*n :: nat)"
545 apply (unfold dvd_def)
546 apply (blast intro: mult_left_commute)
547 done
549 lemma dvd_mult2: "k dvd m ==> k dvd (m*n :: nat)"
550 apply (subst mult_commute)
551 apply (erule dvd_mult)
552 done
554 (* k dvd (m*k) *)
555 declare dvd_refl [THEN dvd_mult, iff] dvd_refl [THEN dvd_mult2, iff]
557 lemma dvd_reduce: "(k dvd n + k) = (k dvd (n::nat))"
558 apply (rule iffI)
560 apply (rule_tac [2] dvd_refl)
561 apply (subgoal_tac "n = (n+k) -k")
562  prefer 2 apply simp
563 apply (erule ssubst)
564 apply (erule dvd_diff)
565 apply (rule dvd_refl)
566 done
568 lemma dvd_mod: "!!n::nat. [| f dvd m; f dvd n |] ==> f dvd m mod n"
569 apply (unfold dvd_def)
570 apply (case_tac "n=0", auto)
571 apply (blast intro: mod_mult_distrib2 [symmetric])
572 done
574 lemma dvd_mod_imp_dvd: "[| (k::nat) dvd m mod n;  k dvd n |] ==> k dvd m"
575 apply (subgoal_tac "k dvd (m div n) *n + m mod n")
577 apply (simp only: dvd_add dvd_mult)
578 done
580 lemma dvd_mod_iff: "k dvd n ==> ((k::nat) dvd m mod n) = (k dvd m)"
581 by (blast intro: dvd_mod_imp_dvd dvd_mod)
583 lemma dvd_mult_cancel: "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n"
584 apply (unfold dvd_def)
585 apply (erule exE)
587 done
589 lemma dvd_mult_cancel1: "0<m ==> (m*n dvd m) = (n = (1::nat))"
590 apply auto
591 apply (subgoal_tac "m*n dvd m*1")
592 apply (drule dvd_mult_cancel, auto)
593 done
595 lemma dvd_mult_cancel2: "0<m ==> (n*m dvd m) = (n = (1::nat))"
596 apply (subst mult_commute)
597 apply (erule dvd_mult_cancel1)
598 done
600 lemma mult_dvd_mono: "[| i dvd m; j dvd n|] ==> i*j dvd (m*n :: nat)"
601 apply (unfold dvd_def, clarify)
602 apply (rule_tac x = "k*ka" in exI)
604 done
606 lemma dvd_mult_left: "(i*j :: nat) dvd k ==> i dvd k"
607 by (simp add: dvd_def mult_assoc, blast)
609 lemma dvd_mult_right: "(i*j :: nat) dvd k ==> j dvd k"
610 apply (unfold dvd_def, clarify)
611 apply (rule_tac x = "i*k" in exI)
613 done
615 lemma dvd_imp_le: "[| k dvd n; 0 < n |] ==> k \<le> (n::nat)"
616 apply (unfold dvd_def, clarify)
617 apply (simp_all (no_asm_use) add: zero_less_mult_iff)
618 apply (erule conjE)
619 apply (rule le_trans)
620 apply (rule_tac [2] le_refl [THEN mult_le_mono])
621 apply (erule_tac [2] Suc_leI, simp)
622 done
624 lemma dvd_eq_mod_eq_0: "!!k::nat. (k dvd n) = (n mod k = 0)"
625 apply (unfold dvd_def)
626 apply (case_tac "k=0", simp, safe)
628 apply (rule_tac t = n and n1 = k in mod_div_equality [THEN subst])
629 apply (subst mult_commute, simp)
630 done
632 lemma dvd_mult_div_cancel: "n dvd m ==> n * (m div n) = (m::nat)"
633 apply (subgoal_tac "m mod n = 0")
635 apply (simp only: dvd_eq_mod_eq_0)
636 done
638 lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
639 by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
640 declare mod_eq_0_iff [THEN iffD1, dest!]
642 (*Loses information, namely we also have r<d provided d is nonzero*)
643 lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
644 apply (cut_tac m = m in mod_div_equality)
646 apply (blast intro: sym)
647 done
650 lemma split_div:
651  "P(n div k :: nat) =
652  ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
653  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
654 proof
655   assume P: ?P
656   show ?Q
657   proof (cases)
658     assume "k = 0"
659     with P show ?Q by(simp add:DIVISION_BY_ZERO_DIV)
660   next
661     assume not0: "k \<noteq> 0"
662     thus ?Q
663     proof (simp, intro allI impI)
664       fix i j
665       assume n: "n = k*i + j" and j: "j < k"
666       show "P i"
667       proof (cases)
668 	assume "i = 0"
669 	with n j P show "P i" by simp
670       next
671 	assume "i \<noteq> 0"
673       qed
674     qed
675   qed
676 next
677   assume Q: ?Q
678   show ?P
679   proof (cases)
680     assume "k = 0"
681     with Q show ?P by(simp add:DIVISION_BY_ZERO_DIV)
682   next
683     assume not0: "k \<noteq> 0"
684     with Q have R: ?R by simp
685     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
686     show ?P by simp
687   qed
688 qed
690 lemma split_div_lemma:
691   "0 < n \<Longrightarrow> (n * q \<le> m \<and> m < n * (Suc q)) = (q = ((m::nat) div n))"
692   apply (rule iffI)
693   apply (rule_tac a=m and r = "m - n * q" and r' = "m mod n" in unique_quotient)
694   apply (simp_all add: quorem_def, arith)
695   apply (rule conjI)
696   apply (rule_tac P="%x. n * (m div n) \<le> x" in
697     subst [OF mod_div_equality [of _ n]])
698   apply (simp only: add: mult_ac)
699   apply (rule_tac P="%x. x < n + n * (m div n)" in
700     subst [OF mod_div_equality [of _ n]])
703   done
705 theorem split_div':
706   "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
707    (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
708   apply (case_tac "0 < n")
709   apply (simp only: add: split_div_lemma)
711   done
713 lemma split_mod:
714  "P(n mod k :: nat) =
715  ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
716  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
717 proof
718   assume P: ?P
719   show ?Q
720   proof (cases)
721     assume "k = 0"
722     with P show ?Q by(simp add:DIVISION_BY_ZERO_MOD)
723   next
724     assume not0: "k \<noteq> 0"
725     thus ?Q
726     proof (simp, intro allI impI)
727       fix i j
728       assume "n = k*i + j" "j < k"
730     qed
731   qed
732 next
733   assume Q: ?Q
734   show ?P
735   proof (cases)
736     assume "k = 0"
737     with Q show ?P by(simp add:DIVISION_BY_ZERO_MOD)
738   next
739     assume not0: "k \<noteq> 0"
740     with Q have R: ?R by simp
741     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
742     show ?P by simp
743   qed
744 qed
746 theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
747   apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
748     subst [OF mod_div_equality [of _ n]])
749   apply arith
750   done
752 ML
753 {*
754 val div_def = thm "div_def"
755 val mod_def = thm "mod_def"
756 val dvd_def = thm "dvd_def"
757 val quorem_def = thm "quorem_def"
759 val wf_less_trans = thm "wf_less_trans";
760 val mod_eq = thm "mod_eq";
761 val div_eq = thm "div_eq";
762 val DIVISION_BY_ZERO_DIV = thm "DIVISION_BY_ZERO_DIV";
763 val DIVISION_BY_ZERO_MOD = thm "DIVISION_BY_ZERO_MOD";
764 val mod_less = thm "mod_less";
765 val mod_geq = thm "mod_geq";
766 val le_mod_geq = thm "le_mod_geq";
767 val mod_if = thm "mod_if";
768 val mod_1 = thm "mod_1";
769 val mod_self = thm "mod_self";
772 val mod_mult_self1 = thm "mod_mult_self1";
773 val mod_mult_self2 = thm "mod_mult_self2";
774 val mod_mult_distrib = thm "mod_mult_distrib";
775 val mod_mult_distrib2 = thm "mod_mult_distrib2";
776 val mod_mult_self_is_0 = thm "mod_mult_self_is_0";
777 val mod_mult_self1_is_0 = thm "mod_mult_self1_is_0";
778 val div_less = thm "div_less";
779 val div_geq = thm "div_geq";
780 val le_div_geq = thm "le_div_geq";
781 val div_if = thm "div_if";
782 val mod_div_equality = thm "mod_div_equality";
783 val mod_div_equality2 = thm "mod_div_equality2";
784 val div_mod_equality = thm "div_mod_equality";
785 val div_mod_equality2 = thm "div_mod_equality2";
786 val mult_div_cancel = thm "mult_div_cancel";
787 val mod_less_divisor = thm "mod_less_divisor";
788 val div_mult_self_is_m = thm "div_mult_self_is_m";
789 val div_mult_self1_is_m = thm "div_mult_self1_is_m";
790 val unique_quotient_lemma = thm "unique_quotient_lemma";
791 val unique_quotient = thm "unique_quotient";
792 val unique_remainder = thm "unique_remainder";
793 val div_0 = thm "div_0";
794 val mod_0 = thm "mod_0";
795 val div_mult1_eq = thm "div_mult1_eq";
796 val mod_mult1_eq = thm "mod_mult1_eq";
797 val mod_mult1_eq' = thm "mod_mult1_eq'";
798 val mod_mult_distrib_mod = thm "mod_mult_distrib_mod";
801 val mod_lemma = thm "mod_lemma";
802 val div_mult2_eq = thm "div_mult2_eq";
803 val mod_mult2_eq = thm "mod_mult2_eq";
804 val div_mult_mult_lemma = thm "div_mult_mult_lemma";
805 val div_mult_mult1 = thm "div_mult_mult1";
806 val div_mult_mult2 = thm "div_mult_mult2";
807 val div_1 = thm "div_1";
808 val div_self = thm "div_self";
811 val div_mult_self1 = thm "div_mult_self1";
812 val div_mult_self2 = thm "div_mult_self2";
813 val div_le_mono = thm "div_le_mono";
814 val div_le_mono2 = thm "div_le_mono2";
815 val div_le_dividend = thm "div_le_dividend";
816 val div_less_dividend = thm "div_less_dividend";
817 val mod_Suc = thm "mod_Suc";
818 val dvdI = thm "dvdI";
819 val dvdE = thm "dvdE";
820 val dvd_0_right = thm "dvd_0_right";
821 val dvd_0_left = thm "dvd_0_left";
822 val dvd_0_left_iff = thm "dvd_0_left_iff";
823 val dvd_1_left = thm "dvd_1_left";
824 val dvd_1_iff_1 = thm "dvd_1_iff_1";
825 val dvd_refl = thm "dvd_refl";
826 val dvd_trans = thm "dvd_trans";
827 val dvd_anti_sym = thm "dvd_anti_sym";
829 val dvd_diff = thm "dvd_diff";
830 val dvd_diffD = thm "dvd_diffD";
831 val dvd_diffD1 = thm "dvd_diffD1";
832 val dvd_mult = thm "dvd_mult";
833 val dvd_mult2 = thm "dvd_mult2";
834 val dvd_reduce = thm "dvd_reduce";
835 val dvd_mod = thm "dvd_mod";
836 val dvd_mod_imp_dvd = thm "dvd_mod_imp_dvd";
837 val dvd_mod_iff = thm "dvd_mod_iff";
838 val dvd_mult_cancel = thm "dvd_mult_cancel";
839 val dvd_mult_cancel1 = thm "dvd_mult_cancel1";
840 val dvd_mult_cancel2 = thm "dvd_mult_cancel2";
841 val mult_dvd_mono = thm "mult_dvd_mono";
842 val dvd_mult_left = thm "dvd_mult_left";
843 val dvd_mult_right = thm "dvd_mult_right";
844 val dvd_imp_le = thm "dvd_imp_le";
845 val dvd_eq_mod_eq_0 = thm "dvd_eq_mod_eq_0";
846 val dvd_mult_div_cancel = thm "dvd_mult_div_cancel";
847 val mod_eq_0_iff = thm "mod_eq_0_iff";
848 val mod_eqD = thm "mod_eqD";
849 *}
852 (*
853 lemma split_div:
854 assumes m: "m \<noteq> 0"
855 shows "P(n div m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P i)"
856        (is "?P = ?Q")
857 proof
858   assume P: ?P
859   show ?Q
860   proof (intro allI impI)
861     fix i j
862     assume n: "n = m*i + j" and j: "j < m"
863     show "P i"
864     proof (cases)
865       assume "i = 0"
866       with n j P show "P i" by simp
867     next
868       assume "i \<noteq> 0"
869       with n j P show "P i" by (simp add:add_ac div_mult_self1)
870     qed
871   qed
872 next
873   assume Q: ?Q
874   from m Q[THEN spec,of "n div m",THEN spec, of "n mod m"]
875   show ?P by simp
876 qed
878 lemma split_mod:
879 assumes m: "m \<noteq> 0"
880 shows "P(n mod m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P j)"
881        (is "?P = ?Q")
882 proof
883   assume P: ?P
884   show ?Q
885   proof (intro allI impI)
886     fix i j
887     assume "n = m*i + j" "j < m"