src/HOL/Divides.thy
 author huffman Fri Nov 11 00:09:37 2005 +0100 (2005-11-11) changeset 18154 0c05abaf6244 parent 17609 5156b731ebc8 child 18202 46af82efd311 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
5 *)
7 header {* The division operators div, mod and the divides relation "dvd" *}
9 theory Divides
10 imports Datatype
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))"
55 by (simp add: mod_def)
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)))"
59 by (simp add: div_def)
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: 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 linorder_not_less)
87 lemma mod_if: "m mod (n::nat) = (if m<n then m else (m-n) mod n)"
88 by (simp add: mod_geq)
90 lemma mod_1 [simp]: "m mod Suc 0 = 0"
91 apply (induct "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")
97 apply (simp_all add: mod_geq)
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 "k")
111 apply (simp_all add: add_left_commute [of _ n])
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 "m" rule: nat_less_induct)
121 apply (subst mod_if, simp)
122 apply (simp add: mod_geq 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 "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: 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 linorder_not_less)
154 lemma div_if: "0<n ==> m div n = (if m<n then 0 else Suc((m-n) div n))"
155 by (simp add: div_geq)
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 "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)
168 apply(simp add: mult_commute)
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"
174 apply(simp add: mod_div_equality)
175 done
177 lemma div_mod_equality2: "(n*(m div n) + m mod n) + k = (m::nat) + k"
178 apply(simp add: mod_div_equality2)
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 =
203   let val simps = add_0 :: add_0_right :: add_ac
204   in NatArithUtils.prove_conv all_tac (NatArithUtils.simp_all_tac 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 "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)
226 done
228 lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
229 apply(drule mod_less_divisor[where m = m])
230 apply simp
231 done
233 lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
234 by (cut_tac m = "m*n" and n = n in mod_div_equality, auto)
236 lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
237 by (simp add: mult_commute div_mult_self_is_m)
239 (*mod_mult_distrib2 above is the counterpart for remainder*)
242 subsection{*Proving facts about Quotient and Remainder*}
244 lemma unique_quotient_lemma:
245      "[| b*q' + r'  \<le> b*q + r;  x < b;  r < b |]
246       ==> q' \<le> (q::nat)"
247 apply (rule leI)
248 apply (subst less_iff_Suc_add)
250 done
252 lemma unique_quotient:
253      "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  0 < b |]
254       ==> q = q'"
255 apply (simp add: split_ifs quorem_def)
256 apply (blast intro: order_antisym
257              dest: order_eq_refl [THEN unique_quotient_lemma] sym)
258 done
260 lemma unique_remainder:
261      "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  0 < b |]
262       ==> r = r'"
263 apply (subgoal_tac "q = q'")
264 prefer 2 apply (blast intro: unique_quotient)
265 apply (simp add: quorem_def)
266 done
268 lemma quorem_div_mod: "0 < b ==> quorem ((a, b), (a div b, a mod b))"
269 by (auto simp add: quorem_def)
271 lemma quorem_div: "[| quorem((a,b),(q,r));  0 < b |] ==> a div b = q"
272 by (simp add: quorem_div_mod [THEN unique_quotient])
274 lemma quorem_mod: "[| quorem((a,b),(q,r));  0 < b |] ==> a mod b = r"
275 by (simp add: quorem_div_mod [THEN unique_remainder])
277 (** A dividend of zero **)
279 lemma div_0 [simp]: "0 div m = (0::nat)"
280 by (case_tac "m=0", simp_all)
282 lemma mod_0 [simp]: "0 mod m = (0::nat)"
283 by (case_tac "m=0", simp_all)
285 (** proving (a*b) div c = a * (b div c) + a * (b mod c) **)
287 lemma quorem_mult1_eq:
288      "[| quorem((b,c),(q,r));  0 < c |]
289       ==> quorem ((a*b, c), (a*q + a*r div c, a*r mod c))"
290 apply (auto simp add: split_ifs mult_ac quorem_def add_mult_distrib2)
291 done
293 lemma div_mult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::nat)"
294 apply (case_tac "c = 0", simp)
295 apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_div])
296 done
298 lemma mod_mult1_eq: "(a*b) mod c = a*(b mod c) mod (c::nat)"
299 apply (case_tac "c = 0", simp)
300 apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_mod])
301 done
303 lemma mod_mult1_eq': "(a*b) mod (c::nat) = ((a mod c) * b) mod c"
304 apply (rule trans)
305 apply (rule_tac s = "b*a mod c" in trans)
306 apply (rule_tac [2] mod_mult1_eq)
307 apply (simp_all (no_asm) add: mult_commute)
308 done
310 lemma mod_mult_distrib_mod: "(a*b) mod (c::nat) = ((a mod c) * (b mod c)) mod c"
311 apply (rule mod_mult1_eq' [THEN trans])
312 apply (rule mod_mult1_eq)
313 done
315 (** proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) **)
318      "[| quorem((a,c),(aq,ar));  quorem((b,c),(bq,br));  0 < c |]
319       ==> quorem ((a+b, c), (aq + bq + (ar+br) div c, (ar+br) mod c))"
320 by (auto simp add: split_ifs mult_ac quorem_def add_mult_distrib2)
322 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
324      "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
325 apply (case_tac "c = 0", simp)
326 apply (blast intro: quorem_add1_eq [THEN quorem_div] quorem_div_mod quorem_div_mod)
327 done
329 lemma mod_add1_eq: "(a+b) mod (c::nat) = (a mod c + b mod c) mod c"
330 apply (case_tac "c = 0", simp)
331 apply (blast intro: quorem_div_mod quorem_div_mod
332                     quorem_add1_eq [THEN quorem_mod])
333 done
336 subsection{*Proving @{term "a div (b*c) = (a div b) div c"}*}
338 (** first, a lemma to bound the remainder **)
340 lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
341 apply (cut_tac m = q and n = c in mod_less_divisor)
342 apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
343 apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
345 done
347 lemma quorem_mult2_eq: "[| quorem ((a,b), (q,r));  0 < b;  0 < c |]
348       ==> quorem ((a, b*c), (q div c, b*(q mod c) + r))"
349 apply (auto simp add: mult_ac quorem_def add_mult_distrib2 [symmetric] mod_lemma)
350 done
352 lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
353 apply (case_tac "b=0", simp)
354 apply (case_tac "c=0", simp)
355 apply (force simp add: quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_div])
356 done
358 lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
359 apply (case_tac "b=0", simp)
360 apply (case_tac "c=0", simp)
361 apply (auto simp add: mult_commute quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_mod])
362 done
365 subsection{*Cancellation of Common Factors in Division*}
367 lemma div_mult_mult_lemma:
368      "[| (0::nat) < b;  0 < c |] ==> (c*a) div (c*b) = a div b"
369 by (auto simp add: div_mult2_eq)
371 lemma div_mult_mult1 [simp]: "(0::nat) < c ==> (c*a) div (c*b) = a div b"
372 apply (case_tac "b = 0")
373 apply (auto simp add: linorder_neq_iff [of b] div_mult_mult_lemma)
374 done
376 lemma div_mult_mult2 [simp]: "(0::nat) < c ==> (a*c) div (b*c) = a div b"
377 apply (drule div_mult_mult1)
378 apply (auto simp add: mult_commute)
379 done
382 (*Distribution of Factors over Remainders:
384 Could prove these as in Integ/IntDiv.ML, but we already have
385 mod_mult_distrib and mod_mult_distrib2 above!
387 Goal "(c*a) mod (c*b) = (c::nat) * (a mod b)"
388 qed "mod_mult_mult1";
390 Goal "(a*c) mod (b*c) = (a mod b) * (c::nat)";
391 qed "mod_mult_mult2";
392  ***)
394 subsection{*Further Facts about Quotient and Remainder*}
396 lemma div_1 [simp]: "m div Suc 0 = m"
397 apply (induct "m")
398 apply (simp_all (no_asm_simp) add: div_geq)
399 done
401 lemma div_self [simp]: "0<n ==> n div n = (1::nat)"
402 by (simp add: div_geq)
404 lemma div_add_self2: "0<n ==> (m+n) div n = Suc (m div n)"
405 apply (subgoal_tac "(n + m) div n = Suc ((n+m-n) div n) ")
407 apply (subst div_geq [symmetric], simp_all)
408 done
410 lemma div_add_self1: "0<n ==> (n+m) div n = Suc (m div n)"
413 lemma div_mult_self1 [simp]: "!!n::nat. 0<n ==> (m + k*n) div n = k + m div n"
414 apply (subst div_add1_eq)
415 apply (subst div_mult1_eq, simp)
416 done
418 lemma div_mult_self2 [simp]: "0<n ==> (m + n*k) div n = k + m div (n::nat)"
419 by (simp add: mult_commute div_mult_self1)
422 (* Monotonicity of div in first argument *)
423 lemma div_le_mono [rule_format (no_asm)]:
424      "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
425 apply (case_tac "k=0", simp)
426 apply (induct "n" rule: nat_less_induct, clarify)
427 apply (case_tac "n<k")
428 (* 1  case n<k *)
429 apply simp
430 (* 2  case n >= k *)
431 apply (case_tac "m<k")
432 (* 2.1  case m<k *)
433 apply simp
434 (* 2.2  case m>=k *)
435 apply (simp add: div_geq diff_le_mono)
436 done
438 (* Antimonotonicity of div in second argument *)
439 lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
440 apply (subgoal_tac "0<n")
441  prefer 2 apply simp
442 apply (induct_tac k rule: nat_less_induct)
443 apply (rename_tac "k")
444 apply (case_tac "k<n", simp)
445 apply (subgoal_tac "~ (k<m) ")
446  prefer 2 apply simp
447 apply (simp add: div_geq)
448 apply (subgoal_tac "(k-n) div n \<le> (k-m) div n")
449  prefer 2
450  apply (blast intro: div_le_mono diff_le_mono2)
451 apply (rule le_trans, simp)
452 apply (simp)
453 done
455 lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
456 apply (case_tac "n=0", simp)
457 apply (subgoal_tac "m div n \<le> m div 1", simp)
458 apply (rule div_le_mono2)
459 apply (simp_all (no_asm_simp))
460 done
462 (* Similar for "less than" *)
463 lemma div_less_dividend [rule_format]:
464      "!!n::nat. 1<n ==> 0 < m --> m div n < m"
465 apply (induct_tac m rule: nat_less_induct)
466 apply (rename_tac "m")
467 apply (case_tac "m<n", simp)
468 apply (subgoal_tac "0<n")
469  prefer 2 apply simp
470 apply (simp add: div_geq)
471 apply (case_tac "n<m")
472  apply (subgoal_tac "(m-n) div n < (m-n) ")
473   apply (rule impI less_trans_Suc)+
474 apply assumption
475   apply (simp_all)
476 done
478 declare div_less_dividend [simp]
480 text{*A fact for the mutilated chess board*}
481 lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
482 apply (case_tac "n=0", simp)
483 apply (induct "m" rule: nat_less_induct)
484 apply (case_tac "Suc (na) <n")
485 (* case Suc(na) < n *)
486 apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
487 (* case n \<le> Suc(na) *)
488 apply (simp add: linorder_not_less le_Suc_eq mod_geq)
489 apply (auto simp add: Suc_diff_le le_mod_geq)
490 done
492 lemma nat_mod_div_trivial [simp]: "m mod n div n = (0 :: nat)"
493 by (case_tac "n=0", auto)
495 lemma nat_mod_mod_trivial [simp]: "m mod n mod n = (m mod n :: nat)"
496 by (case_tac "n=0", auto)
499 subsection{*The Divides Relation*}
501 lemma dvdI [intro?]: "n = m * k ==> m dvd n"
502 by (unfold dvd_def, blast)
504 lemma dvdE [elim?]: "!!P. [|m dvd n;  !!k. n = m*k ==> P|] ==> P"
505 by (unfold dvd_def, blast)
507 lemma dvd_0_right [iff]: "m dvd (0::nat)"
508 apply (unfold dvd_def)
509 apply (blast intro: mult_0_right [symmetric])
510 done
512 lemma dvd_0_left: "0 dvd m ==> m = (0::nat)"
513 by (force simp add: dvd_def)
515 lemma dvd_0_left_iff [iff]: "(0 dvd (m::nat)) = (m = 0)"
516 by (blast intro: dvd_0_left)
518 lemma dvd_1_left [iff]: "Suc 0 dvd k"
519 by (unfold dvd_def, simp)
521 lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)"
522 by (simp add: dvd_def)
524 lemma dvd_refl [simp]: "m dvd (m::nat)"
525 apply (unfold dvd_def)
526 apply (blast intro: mult_1_right [symmetric])
527 done
529 lemma dvd_trans [trans]: "[| m dvd n; n dvd p |] ==> m dvd (p::nat)"
530 apply (unfold dvd_def)
531 apply (blast intro: mult_assoc)
532 done
534 lemma dvd_anti_sym: "[| m dvd n; n dvd m |] ==> m = (n::nat)"
535 apply (unfold dvd_def)
536 apply (force dest: mult_eq_self_implies_10 simp add: mult_assoc mult_eq_1_iff)
537 done
539 lemma dvd_add: "[| k dvd m; k dvd n |] ==> k dvd (m+n :: nat)"
540 apply (unfold dvd_def)
541 apply (blast intro: add_mult_distrib2 [symmetric])
542 done
544 lemma dvd_diff: "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)"
545 apply (unfold dvd_def)
546 apply (blast intro: diff_mult_distrib2 [symmetric])
547 done
549 lemma dvd_diffD: "[| k dvd m-n; k dvd n; n\<le>m |] ==> k dvd (m::nat)"
550 apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
551 apply (blast intro: dvd_add)
552 done
554 lemma dvd_diffD1: "[| k dvd m-n; k dvd m; n\<le>m |] ==> k dvd (n::nat)"
555 by (drule_tac m = m in dvd_diff, auto)
557 lemma dvd_mult: "k dvd n ==> k dvd (m*n :: nat)"
558 apply (unfold dvd_def)
559 apply (blast intro: mult_left_commute)
560 done
562 lemma dvd_mult2: "k dvd m ==> k dvd (m*n :: nat)"
563 apply (subst mult_commute)
564 apply (erule dvd_mult)
565 done
567 lemma dvd_triv_right [iff]: "k dvd (m*k :: nat)"
568 by (rule dvd_refl [THEN dvd_mult])
570 lemma dvd_triv_left [iff]: "k dvd (k*m :: nat)"
571 by (rule dvd_refl [THEN dvd_mult2])
573 lemma dvd_reduce: "(k dvd n + k) = (k dvd (n::nat))"
574 apply (rule iffI)
575 apply (erule_tac [2] dvd_add)
576 apply (rule_tac [2] dvd_refl)
577 apply (subgoal_tac "n = (n+k) -k")
578  prefer 2 apply simp
579 apply (erule ssubst)
580 apply (erule dvd_diff)
581 apply (rule dvd_refl)
582 done
584 lemma dvd_mod: "!!n::nat. [| f dvd m; f dvd n |] ==> f dvd m mod n"
585 apply (unfold dvd_def)
586 apply (case_tac "n=0", auto)
587 apply (blast intro: mod_mult_distrib2 [symmetric])
588 done
590 lemma dvd_mod_imp_dvd: "[| (k::nat) dvd m mod n;  k dvd n |] ==> k dvd m"
591 apply (subgoal_tac "k dvd (m div n) *n + m mod n")
592  apply (simp add: mod_div_equality)
593 apply (simp only: dvd_add dvd_mult)
594 done
596 lemma dvd_mod_iff: "k dvd n ==> ((k::nat) dvd m mod n) = (k dvd m)"
597 by (blast intro: dvd_mod_imp_dvd dvd_mod)
599 lemma dvd_mult_cancel: "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n"
600 apply (unfold dvd_def)
601 apply (erule exE)
602 apply (simp add: mult_ac)
603 done
605 lemma dvd_mult_cancel1: "0<m ==> (m*n dvd m) = (n = (1::nat))"
606 apply auto
607 apply (subgoal_tac "m*n dvd m*1")
608 apply (drule dvd_mult_cancel, auto)
609 done
611 lemma dvd_mult_cancel2: "0<m ==> (n*m dvd m) = (n = (1::nat))"
612 apply (subst mult_commute)
613 apply (erule dvd_mult_cancel1)
614 done
616 lemma mult_dvd_mono: "[| i dvd m; j dvd n|] ==> i*j dvd (m*n :: nat)"
617 apply (unfold dvd_def, clarify)
618 apply (rule_tac x = "k*ka" in exI)
619 apply (simp add: mult_ac)
620 done
622 lemma dvd_mult_left: "(i*j :: nat) dvd k ==> i dvd k"
623 by (simp add: dvd_def mult_assoc, blast)
625 lemma dvd_mult_right: "(i*j :: nat) dvd k ==> j dvd k"
626 apply (unfold dvd_def, clarify)
627 apply (rule_tac x = "i*k" in exI)
628 apply (simp add: mult_ac)
629 done
631 lemma dvd_imp_le: "[| k dvd n; 0 < n |] ==> k \<le> (n::nat)"
632 apply (unfold dvd_def, clarify)
633 apply (simp_all (no_asm_use) add: zero_less_mult_iff)
634 apply (erule conjE)
635 apply (rule le_trans)
636 apply (rule_tac [2] le_refl [THEN mult_le_mono])
637 apply (erule_tac [2] Suc_leI, simp)
638 done
640 lemma dvd_eq_mod_eq_0: "!!k::nat. (k dvd n) = (n mod k = 0)"
641 apply (unfold dvd_def)
642 apply (case_tac "k=0", simp, safe)
643 apply (simp add: mult_commute)
644 apply (rule_tac t = n and n1 = k in mod_div_equality [THEN subst])
645 apply (subst mult_commute, simp)
646 done
648 lemma dvd_mult_div_cancel: "n dvd m ==> n * (m div n) = (m::nat)"
649 apply (subgoal_tac "m mod n = 0")
650  apply (simp add: mult_div_cancel)
651 apply (simp only: dvd_eq_mod_eq_0)
652 done
654 lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
655 by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
657 lemmas mod_eq_0D = mod_eq_0_iff [THEN iffD1]
658 declare mod_eq_0D [dest!]
660 (*Loses information, namely we also have r<d provided d is nonzero*)
661 lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
662 apply (cut_tac m = m in mod_div_equality)
663 apply (simp only: add_ac)
664 apply (blast intro: sym)
665 done
668 lemma split_div:
669  "P(n div k :: nat) =
670  ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
671  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
672 proof
673   assume P: ?P
674   show ?Q
675   proof (cases)
676     assume "k = 0"
677     with P show ?Q by(simp add:DIVISION_BY_ZERO_DIV)
678   next
679     assume not0: "k \<noteq> 0"
680     thus ?Q
681     proof (simp, intro allI impI)
682       fix i j
683       assume n: "n = k*i + j" and j: "j < k"
684       show "P i"
685       proof (cases)
686 	assume "i = 0"
687 	with n j P show "P i" by simp
688       next
689 	assume "i \<noteq> 0"
690 	with not0 n j P show "P i" by(simp add:add_ac)
691       qed
692     qed
693   qed
694 next
695   assume Q: ?Q
696   show ?P
697   proof (cases)
698     assume "k = 0"
699     with Q show ?P by(simp add:DIVISION_BY_ZERO_DIV)
700   next
701     assume not0: "k \<noteq> 0"
702     with Q have R: ?R by simp
703     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
704     show ?P by simp
705   qed
706 qed
708 lemma split_div_lemma:
709   "0 < n \<Longrightarrow> (n * q \<le> m \<and> m < n * (Suc q)) = (q = ((m::nat) div n))"
710   apply (rule iffI)
711   apply (rule_tac a=m and r = "m - n * q" and r' = "m mod n" in unique_quotient)
712 prefer 3; apply assumption
713   apply (simp_all add: quorem_def)
714   apply arith
715   apply (rule conjI)
716   apply (rule_tac P="%x. n * (m div n) \<le> x" in
717     subst [OF mod_div_equality [of _ n]])
718   apply (simp only: add: mult_ac)
719   apply (rule_tac P="%x. x < n + n * (m div n)" in
720     subst [OF mod_div_equality [of _ n]])
721   apply (simp only: add: mult_ac add_ac)
722   apply (rule add_less_mono1, simp)
723   done
725 theorem split_div':
726   "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
727    (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
728   apply (case_tac "0 < n")
729   apply (simp only: add: split_div_lemma)
730   apply (simp_all add: DIVISION_BY_ZERO_DIV)
731   done
733 lemma split_mod:
734  "P(n mod k :: nat) =
735  ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
736  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
737 proof
738   assume P: ?P
739   show ?Q
740   proof (cases)
741     assume "k = 0"
742     with P show ?Q by(simp add:DIVISION_BY_ZERO_MOD)
743   next
744     assume not0: "k \<noteq> 0"
745     thus ?Q
746     proof (simp, intro allI impI)
747       fix i j
748       assume "n = k*i + j" "j < k"
749       thus "P j" using not0 P by(simp add:add_ac mult_ac)
750     qed
751   qed
752 next
753   assume Q: ?Q
754   show ?P
755   proof (cases)
756     assume "k = 0"
757     with Q show ?P by(simp add:DIVISION_BY_ZERO_MOD)
758   next
759     assume not0: "k \<noteq> 0"
760     with Q have R: ?R by simp
761     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
762     show ?P by simp
763   qed
764 qed
766 theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
767   apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
768     subst [OF mod_div_equality [of _ n]])
769   apply arith
770   done
772 subsection {*An ``induction'' law for modulus arithmetic.*}
774 lemma mod_induct_0:
775   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
776   and base: "P i" and i: "i<p"
777   shows "P 0"
778 proof (rule ccontr)
779   assume contra: "\<not>(P 0)"
780   from i have p: "0<p" by simp
781   have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
782   proof
783     fix k
784     show "?A k"
785     proof (induct k)
786       show "?A 0" by simp  -- "by contradiction"
787     next
788       fix n
789       assume ih: "?A n"
790       show "?A (Suc n)"
791       proof (clarsimp)
792 	assume y: "P (p - Suc n)"
793 	have n: "Suc n < p"
794 	proof (rule ccontr)
795 	  assume "\<not>(Suc n < p)"
796 	  hence "p - Suc n = 0"
797 	    by simp
798 	  with y contra show "False"
799 	    by simp
800 	qed
801 	hence n2: "Suc (p - Suc n) = p-n" by arith
802 	from p have "p - Suc n < p" by arith
803 	with y step have z: "P ((Suc (p - Suc n)) mod p)"
804 	  by blast
805 	show "False"
806 	proof (cases "n=0")
807 	  case True
808 	  with z n2 contra show ?thesis by simp
809 	next
810 	  case False
811 	  with p have "p-n < p" by arith
812 	  with z n2 False ih show ?thesis by simp
813 	qed
814       qed
815     qed
816   qed
817   moreover
818   from i obtain k where "0<k \<and> i+k=p"
819     by (blast dest: less_imp_add_positive)
820   hence "0<k \<and> i=p-k" by auto
821   moreover
822   note base
823   ultimately
824   show "False" by blast
825 qed
827 lemma mod_induct:
828   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
829   and base: "P i" and i: "i<p" and j: "j<p"
830   shows "P j"
831 proof -
832   have "\<forall>j<p. P j"
833   proof
834     fix j
835     show "j<p \<longrightarrow> P j" (is "?A j")
836     proof (induct j)
837       from step base i show "?A 0"
838 	by (auto elim: mod_induct_0)
839     next
840       fix k
841       assume ih: "?A k"
842       show "?A (Suc k)"
843       proof
844 	assume suc: "Suc k < p"
845 	hence k: "k<p" by simp
846 	with ih have "P k" ..
847 	with step k have "P (Suc k mod p)"
848 	  by blast
849 	moreover
850 	from suc have "Suc k mod p = Suc k"
851 	  by simp
852 	ultimately
853 	show "P (Suc k)" by simp
854       qed
855     qed
856   qed
857   with j show ?thesis by blast
858 qed
861 ML
862 {*
863 val div_def = thm "div_def"
864 val mod_def = thm "mod_def"
865 val dvd_def = thm "dvd_def"
866 val quorem_def = thm "quorem_def"
868 val wf_less_trans = thm "wf_less_trans";
869 val mod_eq = thm "mod_eq";
870 val div_eq = thm "div_eq";
871 val DIVISION_BY_ZERO_DIV = thm "DIVISION_BY_ZERO_DIV";
872 val DIVISION_BY_ZERO_MOD = thm "DIVISION_BY_ZERO_MOD";
873 val mod_less = thm "mod_less";
874 val mod_geq = thm "mod_geq";
875 val le_mod_geq = thm "le_mod_geq";
876 val mod_if = thm "mod_if";
877 val mod_1 = thm "mod_1";
878 val mod_self = thm "mod_self";
881 val mod_mult_self1 = thm "mod_mult_self1";
882 val mod_mult_self2 = thm "mod_mult_self2";
883 val mod_mult_distrib = thm "mod_mult_distrib";
884 val mod_mult_distrib2 = thm "mod_mult_distrib2";
885 val mod_mult_self_is_0 = thm "mod_mult_self_is_0";
886 val mod_mult_self1_is_0 = thm "mod_mult_self1_is_0";
887 val div_less = thm "div_less";
888 val div_geq = thm "div_geq";
889 val le_div_geq = thm "le_div_geq";
890 val div_if = thm "div_if";
891 val mod_div_equality = thm "mod_div_equality";
892 val mod_div_equality2 = thm "mod_div_equality2";
893 val div_mod_equality = thm "div_mod_equality";
894 val div_mod_equality2 = thm "div_mod_equality2";
895 val mult_div_cancel = thm "mult_div_cancel";
896 val mod_less_divisor = thm "mod_less_divisor";
897 val div_mult_self_is_m = thm "div_mult_self_is_m";
898 val div_mult_self1_is_m = thm "div_mult_self1_is_m";
899 val unique_quotient_lemma = thm "unique_quotient_lemma";
900 val unique_quotient = thm "unique_quotient";
901 val unique_remainder = thm "unique_remainder";
902 val div_0 = thm "div_0";
903 val mod_0 = thm "mod_0";
904 val div_mult1_eq = thm "div_mult1_eq";
905 val mod_mult1_eq = thm "mod_mult1_eq";
906 val mod_mult1_eq' = thm "mod_mult1_eq'";
907 val mod_mult_distrib_mod = thm "mod_mult_distrib_mod";
910 val mod_lemma = thm "mod_lemma";
911 val div_mult2_eq = thm "div_mult2_eq";
912 val mod_mult2_eq = thm "mod_mult2_eq";
913 val div_mult_mult_lemma = thm "div_mult_mult_lemma";
914 val div_mult_mult1 = thm "div_mult_mult1";
915 val div_mult_mult2 = thm "div_mult_mult2";
916 val div_1 = thm "div_1";
917 val div_self = thm "div_self";
920 val div_mult_self1 = thm "div_mult_self1";
921 val div_mult_self2 = thm "div_mult_self2";
922 val div_le_mono = thm "div_le_mono";
923 val div_le_mono2 = thm "div_le_mono2";
924 val div_le_dividend = thm "div_le_dividend";
925 val div_less_dividend = thm "div_less_dividend";
926 val mod_Suc = thm "mod_Suc";
927 val dvdI = thm "dvdI";
928 val dvdE = thm "dvdE";
929 val dvd_0_right = thm "dvd_0_right";
930 val dvd_0_left = thm "dvd_0_left";
931 val dvd_0_left_iff = thm "dvd_0_left_iff";
932 val dvd_1_left = thm "dvd_1_left";
933 val dvd_1_iff_1 = thm "dvd_1_iff_1";
934 val dvd_refl = thm "dvd_refl";
935 val dvd_trans = thm "dvd_trans";
936 val dvd_anti_sym = thm "dvd_anti_sym";
938 val dvd_diff = thm "dvd_diff";
939 val dvd_diffD = thm "dvd_diffD";
940 val dvd_diffD1 = thm "dvd_diffD1";
941 val dvd_mult = thm "dvd_mult";
942 val dvd_mult2 = thm "dvd_mult2";
943 val dvd_reduce = thm "dvd_reduce";
944 val dvd_mod = thm "dvd_mod";
945 val dvd_mod_imp_dvd = thm "dvd_mod_imp_dvd";
946 val dvd_mod_iff = thm "dvd_mod_iff";
947 val dvd_mult_cancel = thm "dvd_mult_cancel";
948 val dvd_mult_cancel1 = thm "dvd_mult_cancel1";
949 val dvd_mult_cancel2 = thm "dvd_mult_cancel2";
950 val mult_dvd_mono = thm "mult_dvd_mono";
951 val dvd_mult_left = thm "dvd_mult_left";
952 val dvd_mult_right = thm "dvd_mult_right";
953 val dvd_imp_le = thm "dvd_imp_le";
954 val dvd_eq_mod_eq_0 = thm "dvd_eq_mod_eq_0";
955 val dvd_mult_div_cancel = thm "dvd_mult_div_cancel";
956 val mod_eq_0_iff = thm "mod_eq_0_iff";
957 val mod_eqD = thm "mod_eqD";
958 *}
961 (*
962 lemma split_div:
963 assumes m: "m \<noteq> 0"
964 shows "P(n div m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P i)"
965        (is "?P = ?Q")
966 proof
967   assume P: ?P
968   show ?Q
969   proof (intro allI impI)
970     fix i j
971     assume n: "n = m*i + j" and j: "j < m"
972     show "P i"
973     proof (cases)
974       assume "i = 0"
975       with n j P show "P i" by simp
976     next
977       assume "i \<noteq> 0"
978       with n j P show "P i" by (simp add:add_ac div_mult_self1)
979     qed
980   qed
981 next
982   assume Q: ?Q
983   from m Q[THEN spec,of "n div m",THEN spec, of "n mod m"]
984   show ?P by simp
985 qed
987 lemma split_mod:
988 assumes m: "m \<noteq> 0"
989 shows "P(n mod m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P j)"
990        (is "?P = ?Q")
991 proof
992   assume P: ?P
993   show ?Q
994   proof (intro allI impI)
995     fix i j
996     assume "n = m*i + j" "j < m"
997     thus "P j" using m P by(simp add:add_ac mult_ac)
998   qed
999 next
1000   assume Q: ?Q
1001   from m Q[THEN spec,of "n div m",THEN spec, of "n mod m"]
1002   show ?P by simp
1003 qed
1004 *)
1005 end