src/HOL/Divides.thy
author paulson
Tue Jun 28 15:27:45 2005 +0200 (2005-06-28)
changeset 16587 b34c8aa657a5
parent 15439 71c0f98e31f1
child 16733 236dfafbeb63
permissions -rw-r--r--
Constant "If" is now local
     1 (*  Title:      HOL/Divides.thy
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1999  University of Cambridge
     5 
     6 The division operators div, mod and the divides relation "dvd"
     7 *)
     8 
     9 theory Divides
    10 imports NatArith
    11 begin
    12 
    13 (*We use the same class for div and mod;
    14   moreover, dvd is defined whenever multiplication is*)
    15 axclass
    16   div < type
    17 
    18 instance  nat :: div ..
    19 
    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)
    24 
    25 
    26 defs
    27 
    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"
    30 
    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"
    33 
    34 (*The definition of dvd is polymorphic!*)
    35   dvd_def:   "m dvd n == \<exists>k. n = m*k"
    36 
    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)"
    44 
    45 
    46 
    47 subsection{*Initial Lemmas*}
    48 
    49 lemmas wf_less_trans = 
    50        def_wfrec [THEN trans, OF eq_reflection wf_pred_nat [THEN wf_trancl],
    51                   standard]
    52 
    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)
    56 
    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)
    60 
    61 
    62 (** Aribtrary definitions for division by zero.  Useful to simplify 
    63     certain equations **)
    64 
    65 lemma DIVISION_BY_ZERO_DIV [simp]: "a div 0 = (0::nat)"
    66 by (rule div_eq [THEN wf_less_trans], simp)
    67 
    68 lemma DIVISION_BY_ZERO_MOD [simp]: "a mod 0 = (a::nat)"
    69 by (rule mod_eq [THEN wf_less_trans], simp)
    70 
    71 
    72 subsection{*Remainder*}
    73 
    74 lemma mod_less [simp]: "m<n ==> m mod n = (m::nat)"
    75 by (rule mod_eq [THEN wf_less_trans], simp)
    76 
    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
    82 
    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)
    86 
    87 lemma mod_if: "m mod (n::nat) = (if m<n then m else (m-n) mod n)"
    88 by (simp add: mod_geq)
    89 
    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
    94 
    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
    99 
   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") 
   102 apply (simp add: add_commute)
   103 apply (subst mod_geq [symmetric], simp_all)
   104 done
   105 
   106 lemma mod_add_self1 [simp]: "(n+m) mod n = m mod (n::nat)"
   107 by (simp add: add_commute mod_add_self2)
   108 
   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
   113 
   114 lemma mod_mult_self2 [simp]: "(m + n*k) mod n = m mod (n::nat)"
   115 by (simp add: mult_commute mod_mult_self1)
   116 
   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
   124 
   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)
   127 
   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)
   133 apply (simp add: add_commute)
   134 done
   135 
   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)
   138 
   139 
   140 subsection{*Quotient*}
   141 
   142 lemma div_less [simp]: "m<n ==> m div n = (0::nat)"
   143 by (rule div_eq [THEN wf_less_trans], simp)
   144 
   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
   149 
   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)
   153 
   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)
   156 
   157 
   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)
   163 apply (simp_all (no_asm_simp) add: add_assoc div_geq add_diff_inverse)
   164 done
   165 
   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
   170 
   171 subsection{*Simproc for Cancelling Div and Mod*}
   172 
   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
   176 
   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
   180 
   181 ML
   182 {*
   183 val div_mod_equality = thm "div_mod_equality";
   184 val div_mod_equality2 = thm "div_mod_equality2";
   185 
   186 
   187 structure CancelDivModData =
   188 struct
   189 
   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;
   195 
   196 (*logic*)
   197 
   198 val div_mod_eqs = map mk_meta_eq [div_mod_equality,div_mod_equality2]
   199 
   200 val trans = trans
   201 
   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 simps) end
   205 
   206 end;
   207 
   208 structure CancelDivMod = CancelDivModFun(CancelDivModData);
   209 
   210 val cancel_div_mod_proc = NatArithUtils.prep_simproc
   211       ("cancel_div_mod", ["(m::nat) + n"], CancelDivMod.proc);
   212 
   213 Addsimprocs[cancel_div_mod_proc];
   214 *}
   215 
   216 
   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)
   220 
   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
   227 
   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
   232 
   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)
   235 
   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)
   238 
   239 (*mod_mult_distrib2 above is the counterpart for remainder*)
   240 
   241 
   242 subsection{*Proving facts about Quotient and Remainder*}
   243 
   244 lemma unique_quotient_lemma:
   245      "[| b*q' + r'  \<le> b*q + r;  0 < b;  r < b |]  
   246       ==> q' \<le> (q::nat)"
   247 apply (rule leI)
   248 apply (subst less_iff_Suc_add)
   249 apply (auto simp add: add_mult_distrib2)
   250 done
   251 
   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
   259 
   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
   267 
   268 lemma quorem_div_mod: "0 < b ==> quorem ((a, b), (a div b, a mod b))"
   269 by (auto simp add: quorem_def)
   270 
   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])
   273 
   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])
   276 
   277 (** A dividend of zero **)
   278 
   279 lemma div_0 [simp]: "0 div m = (0::nat)"
   280 by (case_tac "m=0", simp_all)
   281 
   282 lemma mod_0 [simp]: "0 mod m = (0::nat)"
   283 by (case_tac "m=0", simp_all)
   284 
   285 (** proving (a*b) div c = a * (b div c) + a * (b mod c) **)
   286 
   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
   292 
   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
   297 
   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
   302 
   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
   309 
   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
   314 
   315 (** proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) **)
   316 
   317 lemma quorem_add1_eq:
   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)
   321 
   322 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
   323 lemma div_add1_eq:
   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
   328 
   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
   334 
   335 
   336 subsection{*Proving @{term "a div (b*c) = (a div b) div c"}*}
   337 
   338 (** first, a lemma to bound the remainder **)
   339 
   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)
   344 apply (simp add: add_mult_distrib2)
   345 done
   346 
   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
   351 
   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
   357 
   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
   363 
   364 
   365 subsection{*Cancellation of Common Factors in Division*}
   366 
   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)
   370 
   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
   375 
   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
   380 
   381 
   382 (*Distribution of Factors over Remainders:
   383 
   384 Could prove these as in Integ/IntDiv.ML, but we already have
   385 mod_mult_distrib and mod_mult_distrib2 above!
   386 
   387 Goal "(c*a) mod (c*b) = (c::nat) * (a mod b)"
   388 qed "mod_mult_mult1";
   389 
   390 Goal "(a*c) mod (b*c) = (a mod b) * (c::nat)";
   391 qed "mod_mult_mult2";
   392  ***)
   393 
   394 subsection{*Further Facts about Quotient and Remainder*}
   395 
   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
   400 
   401 lemma div_self [simp]: "0<n ==> n div n = (1::nat)"
   402 by (simp add: div_geq)
   403 
   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) ")
   406 apply (simp add: add_commute)
   407 apply (subst div_geq [symmetric], simp_all)
   408 done
   409 
   410 lemma div_add_self1: "0<n ==> (n+m) div n = Suc (m div n)"
   411 by (simp add: add_commute div_add_self2)
   412 
   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
   417 
   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)
   420 
   421 
   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
   437 
   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
   454 
   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
   461 
   462 (* Similar for "less than" *) 
   463 lemma div_less_dividend [rule_format, simp]:
   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
   477 
   478 text{*A fact for the mutilated chess board*}
   479 lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
   480 apply (case_tac "n=0", simp)
   481 apply (induct "m" rule: nat_less_induct)
   482 apply (case_tac "Suc (na) <n")
   483 (* case Suc(na) < n *)
   484 apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
   485 (* case n \<le> Suc(na) *)
   486 apply (simp add: not_less_iff_le le_Suc_eq mod_geq)
   487 apply (auto simp add: Suc_diff_le le_mod_geq)
   488 done
   489 
   490 lemma nat_mod_div_trivial [simp]: "m mod n div n = (0 :: nat)"
   491 by (case_tac "n=0", auto)
   492 
   493 lemma nat_mod_mod_trivial [simp]: "m mod n mod n = (m mod n :: nat)"
   494 by (case_tac "n=0", auto)
   495 
   496 
   497 subsection{*The Divides Relation*}
   498 
   499 lemma dvdI [intro?]: "n = m * k ==> m dvd n"
   500 by (unfold dvd_def, blast)
   501 
   502 lemma dvdE [elim?]: "!!P. [|m dvd n;  !!k. n = m*k ==> P|] ==> P"
   503 by (unfold dvd_def, blast)
   504 
   505 lemma dvd_0_right [iff]: "m dvd (0::nat)"
   506 apply (unfold dvd_def)
   507 apply (blast intro: mult_0_right [symmetric])
   508 done
   509 
   510 lemma dvd_0_left: "0 dvd m ==> m = (0::nat)"
   511 by (force simp add: dvd_def)
   512 
   513 lemma dvd_0_left_iff [iff]: "(0 dvd (m::nat)) = (m = 0)"
   514 by (blast intro: dvd_0_left)
   515 
   516 lemma dvd_1_left [iff]: "Suc 0 dvd k"
   517 by (unfold dvd_def, simp)
   518 
   519 lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)"
   520 by (simp add: dvd_def)
   521 
   522 lemma dvd_refl [simp]: "m dvd (m::nat)"
   523 apply (unfold dvd_def)
   524 apply (blast intro: mult_1_right [symmetric])
   525 done
   526 
   527 lemma dvd_trans [trans]: "[| m dvd n; n dvd p |] ==> m dvd (p::nat)"
   528 apply (unfold dvd_def)
   529 apply (blast intro: mult_assoc)
   530 done
   531 
   532 lemma dvd_anti_sym: "[| m dvd n; n dvd m |] ==> m = (n::nat)"
   533 apply (unfold dvd_def)
   534 apply (force dest: mult_eq_self_implies_10 simp add: mult_assoc mult_eq_1_iff)
   535 done
   536 
   537 lemma dvd_add: "[| k dvd m; k dvd n |] ==> k dvd (m+n :: nat)"
   538 apply (unfold dvd_def)
   539 apply (blast intro: add_mult_distrib2 [symmetric])
   540 done
   541 
   542 lemma dvd_diff: "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)"
   543 apply (unfold dvd_def)
   544 apply (blast intro: diff_mult_distrib2 [symmetric])
   545 done
   546 
   547 lemma dvd_diffD: "[| k dvd m-n; k dvd n; n\<le>m |] ==> k dvd (m::nat)"
   548 apply (erule not_less_iff_le [THEN iffD2, THEN add_diff_inverse, THEN subst])
   549 apply (blast intro: dvd_add)
   550 done
   551 
   552 lemma dvd_diffD1: "[| k dvd m-n; k dvd m; n\<le>m |] ==> k dvd (n::nat)"
   553 by (drule_tac m = m in dvd_diff, auto)
   554 
   555 lemma dvd_mult: "k dvd n ==> k dvd (m*n :: nat)"
   556 apply (unfold dvd_def)
   557 apply (blast intro: mult_left_commute)
   558 done
   559 
   560 lemma dvd_mult2: "k dvd m ==> k dvd (m*n :: nat)"
   561 apply (subst mult_commute)
   562 apply (erule dvd_mult)
   563 done
   564 
   565 (* k dvd (m*k) *)
   566 declare dvd_refl [THEN dvd_mult, iff] dvd_refl [THEN dvd_mult2, iff]
   567 
   568 lemma dvd_reduce: "(k dvd n + k) = (k dvd (n::nat))"
   569 apply (rule iffI)
   570 apply (erule_tac [2] dvd_add)
   571 apply (rule_tac [2] dvd_refl)
   572 apply (subgoal_tac "n = (n+k) -k")
   573  prefer 2 apply simp 
   574 apply (erule ssubst)
   575 apply (erule dvd_diff)
   576 apply (rule dvd_refl)
   577 done
   578 
   579 lemma dvd_mod: "!!n::nat. [| f dvd m; f dvd n |] ==> f dvd m mod n"
   580 apply (unfold dvd_def)
   581 apply (case_tac "n=0", auto)
   582 apply (blast intro: mod_mult_distrib2 [symmetric])
   583 done
   584 
   585 lemma dvd_mod_imp_dvd: "[| (k::nat) dvd m mod n;  k dvd n |] ==> k dvd m"
   586 apply (subgoal_tac "k dvd (m div n) *n + m mod n")
   587  apply (simp add: mod_div_equality)
   588 apply (simp only: dvd_add dvd_mult)
   589 done
   590 
   591 lemma dvd_mod_iff: "k dvd n ==> ((k::nat) dvd m mod n) = (k dvd m)"
   592 by (blast intro: dvd_mod_imp_dvd dvd_mod)
   593 
   594 lemma dvd_mult_cancel: "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n"
   595 apply (unfold dvd_def)
   596 apply (erule exE)
   597 apply (simp add: mult_ac)
   598 done
   599 
   600 lemma dvd_mult_cancel1: "0<m ==> (m*n dvd m) = (n = (1::nat))"
   601 apply auto
   602 apply (subgoal_tac "m*n dvd m*1")
   603 apply (drule dvd_mult_cancel, auto)
   604 done
   605 
   606 lemma dvd_mult_cancel2: "0<m ==> (n*m dvd m) = (n = (1::nat))"
   607 apply (subst mult_commute)
   608 apply (erule dvd_mult_cancel1)
   609 done
   610 
   611 lemma mult_dvd_mono: "[| i dvd m; j dvd n|] ==> i*j dvd (m*n :: nat)"
   612 apply (unfold dvd_def, clarify)
   613 apply (rule_tac x = "k*ka" in exI)
   614 apply (simp add: mult_ac)
   615 done
   616 
   617 lemma dvd_mult_left: "(i*j :: nat) dvd k ==> i dvd k"
   618 by (simp add: dvd_def mult_assoc, blast)
   619 
   620 lemma dvd_mult_right: "(i*j :: nat) dvd k ==> j dvd k"
   621 apply (unfold dvd_def, clarify)
   622 apply (rule_tac x = "i*k" in exI)
   623 apply (simp add: mult_ac)
   624 done
   625 
   626 lemma dvd_imp_le: "[| k dvd n; 0 < n |] ==> k \<le> (n::nat)"
   627 apply (unfold dvd_def, clarify)
   628 apply (simp_all (no_asm_use) add: zero_less_mult_iff)
   629 apply (erule conjE)
   630 apply (rule le_trans)
   631 apply (rule_tac [2] le_refl [THEN mult_le_mono])
   632 apply (erule_tac [2] Suc_leI, simp)
   633 done
   634 
   635 lemma dvd_eq_mod_eq_0: "!!k::nat. (k dvd n) = (n mod k = 0)"
   636 apply (unfold dvd_def)
   637 apply (case_tac "k=0", simp, safe)
   638 apply (simp add: mult_commute)
   639 apply (rule_tac t = n and n1 = k in mod_div_equality [THEN subst])
   640 apply (subst mult_commute, simp)
   641 done
   642 
   643 lemma dvd_mult_div_cancel: "n dvd m ==> n * (m div n) = (m::nat)"
   644 apply (subgoal_tac "m mod n = 0")
   645  apply (simp add: mult_div_cancel)
   646 apply (simp only: dvd_eq_mod_eq_0)
   647 done
   648 
   649 lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
   650 by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
   651 declare mod_eq_0_iff [THEN iffD1, dest!]
   652 
   653 (*Loses information, namely we also have r<d provided d is nonzero*)
   654 lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
   655 apply (cut_tac m = m in mod_div_equality)
   656 apply (simp only: add_ac)
   657 apply (blast intro: sym)
   658 done
   659 
   660 
   661 lemma split_div:
   662  "P(n div k :: nat) =
   663  ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
   664  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   665 proof
   666   assume P: ?P
   667   show ?Q
   668   proof (cases)
   669     assume "k = 0"
   670     with P show ?Q by(simp add:DIVISION_BY_ZERO_DIV)
   671   next
   672     assume not0: "k \<noteq> 0"
   673     thus ?Q
   674     proof (simp, intro allI impI)
   675       fix i j
   676       assume n: "n = k*i + j" and j: "j < k"
   677       show "P i"
   678       proof (cases)
   679 	assume "i = 0"
   680 	with n j P show "P i" by simp
   681       next
   682 	assume "i \<noteq> 0"
   683 	with not0 n j P show "P i" by(simp add:add_ac)
   684       qed
   685     qed
   686   qed
   687 next
   688   assume Q: ?Q
   689   show ?P
   690   proof (cases)
   691     assume "k = 0"
   692     with Q show ?P by(simp add:DIVISION_BY_ZERO_DIV)
   693   next
   694     assume not0: "k \<noteq> 0"
   695     with Q have R: ?R by simp
   696     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   697     show ?P by simp
   698   qed
   699 qed
   700 
   701 lemma split_div_lemma:
   702   "0 < n \<Longrightarrow> (n * q \<le> m \<and> m < n * (Suc q)) = (q = ((m::nat) div n))"
   703   apply (rule iffI)
   704   apply (rule_tac a=m and r = "m - n * q" and r' = "m mod n" in unique_quotient)
   705   apply (simp_all add: quorem_def, arith)
   706   apply (rule conjI)
   707   apply (rule_tac P="%x. n * (m div n) \<le> x" in
   708     subst [OF mod_div_equality [of _ n]])
   709   apply (simp only: add: mult_ac)
   710   apply (rule_tac P="%x. x < n + n * (m div n)" in
   711     subst [OF mod_div_equality [of _ n]])
   712   apply (simp only: add: mult_ac add_ac)
   713   apply (rule add_less_mono1, simp)
   714   done
   715 
   716 theorem split_div':
   717   "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
   718    (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
   719   apply (case_tac "0 < n")
   720   apply (simp only: add: split_div_lemma)
   721   apply (simp_all add: DIVISION_BY_ZERO_DIV)
   722   done
   723 
   724 lemma split_mod:
   725  "P(n mod k :: nat) =
   726  ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
   727  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   728 proof
   729   assume P: ?P
   730   show ?Q
   731   proof (cases)
   732     assume "k = 0"
   733     with P show ?Q by(simp add:DIVISION_BY_ZERO_MOD)
   734   next
   735     assume not0: "k \<noteq> 0"
   736     thus ?Q
   737     proof (simp, intro allI impI)
   738       fix i j
   739       assume "n = k*i + j" "j < k"
   740       thus "P j" using not0 P by(simp add:add_ac mult_ac)
   741     qed
   742   qed
   743 next
   744   assume Q: ?Q
   745   show ?P
   746   proof (cases)
   747     assume "k = 0"
   748     with Q show ?P by(simp add:DIVISION_BY_ZERO_MOD)
   749   next
   750     assume not0: "k \<noteq> 0"
   751     with Q have R: ?R by simp
   752     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   753     show ?P by simp
   754   qed
   755 qed
   756 
   757 theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
   758   apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
   759     subst [OF mod_div_equality [of _ n]])
   760   apply arith
   761   done
   762 
   763 subsection {*An ``induction'' law for modulus arithmetic.*}
   764 
   765 lemma mod_induct_0:
   766   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
   767   and base: "P i" and i: "i<p"
   768   shows "P 0"
   769 proof (rule ccontr)
   770   assume contra: "\<not>(P 0)"
   771   from i have p: "0<p" by simp
   772   have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
   773   proof
   774     fix k
   775     show "?A k"
   776     proof (induct k)
   777       show "?A 0" by simp  -- "by contradiction"
   778     next
   779       fix n
   780       assume ih: "?A n"
   781       show "?A (Suc n)"
   782       proof (clarsimp)
   783 	assume y: "P (p - Suc n)"
   784 	have n: "Suc n < p"
   785 	proof (rule ccontr)
   786 	  assume "\<not>(Suc n < p)"
   787 	  hence "p - Suc n = 0"
   788 	    by simp
   789 	  with y contra show "False"
   790 	    by simp
   791 	qed
   792 	hence n2: "Suc (p - Suc n) = p-n" by arith
   793 	from p have "p - Suc n < p" by arith
   794 	with y step have z: "P ((Suc (p - Suc n)) mod p)"
   795 	  by blast
   796 	show "False"
   797 	proof (cases "n=0")
   798 	  case True
   799 	  with z n2 contra show ?thesis by simp
   800 	next
   801 	  case False
   802 	  with p have "p-n < p" by arith
   803 	  with z n2 False ih show ?thesis by simp
   804 	qed
   805       qed
   806     qed
   807   qed
   808   moreover
   809   from i obtain k where "0<k \<and> i+k=p"
   810     by (blast dest: less_imp_add_positive)
   811   hence "0<k \<and> i=p-k" by auto
   812   moreover
   813   note base
   814   ultimately
   815   show "False" by blast
   816 qed
   817 
   818 lemma mod_induct:
   819   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
   820   and base: "P i" and i: "i<p" and j: "j<p"
   821   shows "P j"
   822 proof -
   823   have "\<forall>j<p. P j"
   824   proof
   825     fix j
   826     show "j<p \<longrightarrow> P j" (is "?A j")
   827     proof (induct j)
   828       from step base i show "?A 0"
   829 	by (auto elim: mod_induct_0)
   830     next
   831       fix k
   832       assume ih: "?A k"
   833       show "?A (Suc k)"
   834       proof
   835 	assume suc: "Suc k < p"
   836 	hence k: "k<p" by simp
   837 	with ih have "P k" ..
   838 	with step k have "P (Suc k mod p)"
   839 	  by blast
   840 	moreover
   841 	from suc have "Suc k mod p = Suc k"
   842 	  by simp
   843 	ultimately
   844 	show "P (Suc k)" by simp
   845       qed
   846     qed
   847   qed
   848   with j show ?thesis by blast
   849 qed
   850 
   851 
   852 ML
   853 {*
   854 val div_def = thm "div_def"
   855 val mod_def = thm "mod_def"
   856 val dvd_def = thm "dvd_def"
   857 val quorem_def = thm "quorem_def"
   858 
   859 val wf_less_trans = thm "wf_less_trans";
   860 val mod_eq = thm "mod_eq";
   861 val div_eq = thm "div_eq";
   862 val DIVISION_BY_ZERO_DIV = thm "DIVISION_BY_ZERO_DIV";
   863 val DIVISION_BY_ZERO_MOD = thm "DIVISION_BY_ZERO_MOD";
   864 val mod_less = thm "mod_less";
   865 val mod_geq = thm "mod_geq";
   866 val le_mod_geq = thm "le_mod_geq";
   867 val mod_if = thm "mod_if";
   868 val mod_1 = thm "mod_1";
   869 val mod_self = thm "mod_self";
   870 val mod_add_self2 = thm "mod_add_self2";
   871 val mod_add_self1 = thm "mod_add_self1";
   872 val mod_mult_self1 = thm "mod_mult_self1";
   873 val mod_mult_self2 = thm "mod_mult_self2";
   874 val mod_mult_distrib = thm "mod_mult_distrib";
   875 val mod_mult_distrib2 = thm "mod_mult_distrib2";
   876 val mod_mult_self_is_0 = thm "mod_mult_self_is_0";
   877 val mod_mult_self1_is_0 = thm "mod_mult_self1_is_0";
   878 val div_less = thm "div_less";
   879 val div_geq = thm "div_geq";
   880 val le_div_geq = thm "le_div_geq";
   881 val div_if = thm "div_if";
   882 val mod_div_equality = thm "mod_div_equality";
   883 val mod_div_equality2 = thm "mod_div_equality2";
   884 val div_mod_equality = thm "div_mod_equality";
   885 val div_mod_equality2 = thm "div_mod_equality2";
   886 val mult_div_cancel = thm "mult_div_cancel";
   887 val mod_less_divisor = thm "mod_less_divisor";
   888 val div_mult_self_is_m = thm "div_mult_self_is_m";
   889 val div_mult_self1_is_m = thm "div_mult_self1_is_m";
   890 val unique_quotient_lemma = thm "unique_quotient_lemma";
   891 val unique_quotient = thm "unique_quotient";
   892 val unique_remainder = thm "unique_remainder";
   893 val div_0 = thm "div_0";
   894 val mod_0 = thm "mod_0";
   895 val div_mult1_eq = thm "div_mult1_eq";
   896 val mod_mult1_eq = thm "mod_mult1_eq";
   897 val mod_mult1_eq' = thm "mod_mult1_eq'";
   898 val mod_mult_distrib_mod = thm "mod_mult_distrib_mod";
   899 val div_add1_eq = thm "div_add1_eq";
   900 val mod_add1_eq = thm "mod_add1_eq";
   901 val mod_lemma = thm "mod_lemma";
   902 val div_mult2_eq = thm "div_mult2_eq";
   903 val mod_mult2_eq = thm "mod_mult2_eq";
   904 val div_mult_mult_lemma = thm "div_mult_mult_lemma";
   905 val div_mult_mult1 = thm "div_mult_mult1";
   906 val div_mult_mult2 = thm "div_mult_mult2";
   907 val div_1 = thm "div_1";
   908 val div_self = thm "div_self";
   909 val div_add_self2 = thm "div_add_self2";
   910 val div_add_self1 = thm "div_add_self1";
   911 val div_mult_self1 = thm "div_mult_self1";
   912 val div_mult_self2 = thm "div_mult_self2";
   913 val div_le_mono = thm "div_le_mono";
   914 val div_le_mono2 = thm "div_le_mono2";
   915 val div_le_dividend = thm "div_le_dividend";
   916 val div_less_dividend = thm "div_less_dividend";
   917 val mod_Suc = thm "mod_Suc";
   918 val dvdI = thm "dvdI";
   919 val dvdE = thm "dvdE";
   920 val dvd_0_right = thm "dvd_0_right";
   921 val dvd_0_left = thm "dvd_0_left";
   922 val dvd_0_left_iff = thm "dvd_0_left_iff";
   923 val dvd_1_left = thm "dvd_1_left";
   924 val dvd_1_iff_1 = thm "dvd_1_iff_1";
   925 val dvd_refl = thm "dvd_refl";
   926 val dvd_trans = thm "dvd_trans";
   927 val dvd_anti_sym = thm "dvd_anti_sym";
   928 val dvd_add = thm "dvd_add";
   929 val dvd_diff = thm "dvd_diff";
   930 val dvd_diffD = thm "dvd_diffD";
   931 val dvd_diffD1 = thm "dvd_diffD1";
   932 val dvd_mult = thm "dvd_mult";
   933 val dvd_mult2 = thm "dvd_mult2";
   934 val dvd_reduce = thm "dvd_reduce";
   935 val dvd_mod = thm "dvd_mod";
   936 val dvd_mod_imp_dvd = thm "dvd_mod_imp_dvd";
   937 val dvd_mod_iff = thm "dvd_mod_iff";
   938 val dvd_mult_cancel = thm "dvd_mult_cancel";
   939 val dvd_mult_cancel1 = thm "dvd_mult_cancel1";
   940 val dvd_mult_cancel2 = thm "dvd_mult_cancel2";
   941 val mult_dvd_mono = thm "mult_dvd_mono";
   942 val dvd_mult_left = thm "dvd_mult_left";
   943 val dvd_mult_right = thm "dvd_mult_right";
   944 val dvd_imp_le = thm "dvd_imp_le";
   945 val dvd_eq_mod_eq_0 = thm "dvd_eq_mod_eq_0";
   946 val dvd_mult_div_cancel = thm "dvd_mult_div_cancel";
   947 val mod_eq_0_iff = thm "mod_eq_0_iff";
   948 val mod_eqD = thm "mod_eqD";
   949 *}
   950 
   951 
   952 (*
   953 lemma split_div:
   954 assumes m: "m \<noteq> 0"
   955 shows "P(n div m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P i)"
   956        (is "?P = ?Q")
   957 proof
   958   assume P: ?P
   959   show ?Q
   960   proof (intro allI impI)
   961     fix i j
   962     assume n: "n = m*i + j" and j: "j < m"
   963     show "P i"
   964     proof (cases)
   965       assume "i = 0"
   966       with n j P show "P i" by simp
   967     next
   968       assume "i \<noteq> 0"
   969       with n j P show "P i" by (simp add:add_ac div_mult_self1)
   970     qed
   971   qed
   972 next
   973   assume Q: ?Q
   974   from m Q[THEN spec,of "n div m",THEN spec, of "n mod m"]
   975   show ?P by simp
   976 qed
   977 
   978 lemma split_mod:
   979 assumes m: "m \<noteq> 0"
   980 shows "P(n mod m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P j)"
   981        (is "?P = ?Q")
   982 proof
   983   assume P: ?P
   984   show ?Q
   985   proof (intro allI impI)
   986     fix i j
   987     assume "n = m*i + j" "j < m"
   988     thus "P j" using m P by(simp add:add_ac mult_ac)
   989   qed
   990 next
   991   assume Q: ?Q
   992   from m Q[THEN spec,of "n div m",THEN spec, of "n mod m"]
   993   show ?P by simp
   994 qed
   995 *)
   996 end