src/HOL/Divides.thy
author paulson
Fri Mar 05 15:26:04 2004 +0100 (2004-03-05)
changeset 14437 92f6aa05b7bb
parent 14430 5cb24165a2e1
child 14640 b31870c50c68
permissions -rw-r--r--
some new results
     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 = NatArith:
    10 
    11 (*We use the same class for div and mod;
    12   moreover, dvd is defined whenever multiplication is*)
    13 axclass
    14   div < type
    15 
    16 instance  nat :: div ..
    17 
    18 consts
    19   div  :: "'a::div \<Rightarrow> 'a \<Rightarrow> 'a"          (infixl 70)
    20   mod  :: "'a::div \<Rightarrow> 'a \<Rightarrow> 'a"          (infixl 70)
    21   dvd  :: "'a::times \<Rightarrow> 'a \<Rightarrow> bool"      (infixl 50)
    22 
    23 
    24 defs
    25 
    26   mod_def:   "m mod n == wfrec (trancl pred_nat)
    27                           (%f j. if j<n | n=0 then j else f (j-n)) m"
    28 
    29   div_def:   "m div n == wfrec (trancl pred_nat) 
    30                           (%f j. if j<n | n=0 then 0 else Suc (f (j-n))) m"
    31 
    32 (*The definition of dvd is polymorphic!*)
    33   dvd_def:   "m dvd n == \<exists>k. n = m*k"
    34 
    35 (*This definition helps prove the harder properties of div and mod.
    36   It is copied from IntDiv.thy; should it be overloaded?*)
    37 constdefs
    38   quorem :: "(nat*nat) * (nat*nat) => bool"
    39     "quorem == %((a,b), (q,r)).
    40                       a = b*q + r &
    41                       (if 0<b then 0\<le>r & r<b else b<r & r \<le>0)"
    42 
    43 
    44 
    45 subsection{*Initial Lemmas*}
    46 
    47 lemmas wf_less_trans = 
    48        def_wfrec [THEN trans, OF eq_reflection wf_pred_nat [THEN wf_trancl],
    49                   standard]
    50 
    51 lemma mod_eq: "(%m. m mod n) = 
    52               wfrec (trancl pred_nat) (%f j. if j<n | n=0 then j else f (j-n))"
    53 by (simp add: mod_def)
    54 
    55 lemma div_eq: "(%m. m div n) = wfrec (trancl pred_nat)  
    56                (%f j. if j<n | n=0 then 0 else Suc (f (j-n)))"
    57 by (simp add: div_def)
    58 
    59 
    60 (** Aribtrary definitions for division by zero.  Useful to simplify 
    61     certain equations **)
    62 
    63 lemma DIVISION_BY_ZERO_DIV [simp]: "a div 0 = (0::nat)"
    64 by (rule div_eq [THEN wf_less_trans], simp)
    65 
    66 lemma DIVISION_BY_ZERO_MOD [simp]: "a mod 0 = (a::nat)"
    67 by (rule mod_eq [THEN wf_less_trans], simp)
    68 
    69 
    70 subsection{*Remainder*}
    71 
    72 lemma mod_less [simp]: "m<n ==> m mod n = (m::nat)"
    73 by (rule mod_eq [THEN wf_less_trans], simp)
    74 
    75 lemma mod_geq: "~ m < (n::nat) ==> m mod n = (m-n) mod n"
    76 apply (case_tac "n=0", simp) 
    77 apply (rule mod_eq [THEN wf_less_trans])
    78 apply (simp add: diff_less cut_apply less_eq)
    79 done
    80 
    81 (*Avoids the ugly ~m<n above*)
    82 lemma le_mod_geq: "(n::nat) \<le> m ==> m mod n = (m-n) mod n"
    83 by (simp add: mod_geq not_less_iff_le)
    84 
    85 lemma mod_if: "m mod (n::nat) = (if m<n then m else (m-n) mod n)"
    86 by (simp add: mod_geq)
    87 
    88 lemma mod_1 [simp]: "m mod Suc 0 = 0"
    89 apply (induct_tac "m")
    90 apply (simp_all (no_asm_simp) add: mod_geq)
    91 done
    92 
    93 lemma mod_self [simp]: "n mod n = (0::nat)"
    94 apply (case_tac "n=0")
    95 apply (simp_all add: mod_geq)
    96 done
    97 
    98 lemma mod_add_self2 [simp]: "(m+n) mod n = m mod (n::nat)"
    99 apply (subgoal_tac " (n + m) mod n = (n+m-n) mod n") 
   100 apply (simp add: add_commute)
   101 apply (subst mod_geq [symmetric], simp_all)
   102 done
   103 
   104 lemma mod_add_self1 [simp]: "(n+m) mod n = m mod (n::nat)"
   105 by (simp add: add_commute mod_add_self2)
   106 
   107 lemma mod_mult_self1 [simp]: "(m + k*n) mod n = m mod (n::nat)"
   108 apply (induct_tac "k")
   109 apply (simp_all add: add_left_commute [of _ n])
   110 done
   111 
   112 lemma mod_mult_self2 [simp]: "(m + n*k) mod n = m mod (n::nat)"
   113 by (simp add: mult_commute mod_mult_self1)
   114 
   115 lemma mod_mult_distrib: "(m mod n) * (k::nat) = (m*k) mod (n*k)"
   116 apply (case_tac "n=0", simp)
   117 apply (case_tac "k=0", simp)
   118 apply (induct_tac "m" rule: nat_less_induct)
   119 apply (subst mod_if, simp)
   120 apply (simp add: mod_geq diff_less diff_mult_distrib)
   121 done
   122 
   123 lemma mod_mult_distrib2: "(k::nat) * (m mod n) = (k*m) mod (k*n)"
   124 by (simp add: mult_commute [of k] mod_mult_distrib)
   125 
   126 lemma mod_mult_self_is_0 [simp]: "(m*n) mod n = (0::nat)"
   127 apply (case_tac "n=0", simp)
   128 apply (induct_tac "m", simp)
   129 apply (rename_tac "k")
   130 apply (cut_tac m = "k*n" and n = n in mod_add_self2)
   131 apply (simp add: add_commute)
   132 done
   133 
   134 lemma mod_mult_self1_is_0 [simp]: "(n*m) mod n = (0::nat)"
   135 by (simp add: mult_commute mod_mult_self_is_0)
   136 
   137 
   138 subsection{*Quotient*}
   139 
   140 lemma div_less [simp]: "m<n ==> m div n = (0::nat)"
   141 by (rule div_eq [THEN wf_less_trans], simp)
   142 
   143 lemma div_geq: "[| 0<n;  ~m<n |] ==> m div n = Suc((m-n) div n)"
   144 apply (rule div_eq [THEN wf_less_trans])
   145 apply (simp add: diff_less cut_apply less_eq)
   146 done
   147 
   148 (*Avoids the ugly ~m<n above*)
   149 lemma le_div_geq: "[| 0<n;  n\<le>m |] ==> m div n = Suc((m-n) div n)"
   150 by (simp add: div_geq not_less_iff_le)
   151 
   152 lemma div_if: "0<n ==> m div n = (if m<n then 0 else Suc((m-n) div n))"
   153 by (simp add: div_geq)
   154 
   155 
   156 (*Main Result about quotient and remainder.*)
   157 lemma mod_div_equality: "(m div n)*n + m mod n = (m::nat)"
   158 apply (case_tac "n=0", simp)
   159 apply (induct_tac "m" rule: nat_less_induct)
   160 apply (subst mod_if)
   161 apply (simp_all (no_asm_simp) add: add_assoc div_geq add_diff_inverse diff_less)
   162 done
   163 
   164 lemma mod_div_equality2: "n * (m div n) + m mod n = (m::nat)"
   165 apply(cut_tac m = m and n = n in mod_div_equality)
   166 apply(simp add: mult_commute)
   167 done
   168 
   169 subsection{*Simproc for Cancelling Div and Mod*}
   170 
   171 lemma div_mod_equality: "((m div n)*n + m mod n) + k = (m::nat) + k"
   172 apply(simp add: mod_div_equality)
   173 done
   174 
   175 lemma div_mod_equality2: "(n*(m div n) + m mod n) + k = (m::nat) + k"
   176 apply(simp add: mod_div_equality2)
   177 done
   178 
   179 ML
   180 {*
   181 val div_mod_equality = thm "div_mod_equality";
   182 val div_mod_equality2 = thm "div_mod_equality2";
   183 
   184 
   185 structure CancelDivModData =
   186 struct
   187 
   188 val div_name = "Divides.op div";
   189 val mod_name = "Divides.op mod";
   190 val mk_binop = HOLogic.mk_binop;
   191 val mk_sum = NatArithUtils.mk_sum;
   192 val dest_sum = NatArithUtils.dest_sum;
   193 
   194 (*logic*)
   195 
   196 val div_mod_eqs = map mk_meta_eq [div_mod_equality,div_mod_equality2]
   197 
   198 val trans = trans
   199 
   200 val prove_eq_sums =
   201   let val simps = add_0 :: add_0_right :: add_ac
   202   in NatArithUtils.prove_conv all_tac (NatArithUtils.simp_all simps) end
   203 
   204 end;
   205 
   206 structure CancelDivMod = CancelDivModFun(CancelDivModData);
   207 
   208 val cancel_div_mod_proc = NatArithUtils.prep_simproc
   209       ("cancel_div_mod", ["(m::nat) + n"], CancelDivMod.proc);
   210 
   211 Addsimprocs[cancel_div_mod_proc];
   212 *}
   213 
   214 
   215 (* a simple rearrangement of mod_div_equality: *)
   216 lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
   217 by (cut_tac m = m and n = n in mod_div_equality2, arith)
   218 
   219 lemma mod_less_divisor [simp]: "0<n ==> m mod n < (n::nat)"
   220 apply (induct_tac "m" rule: nat_less_induct)
   221 apply (case_tac "na<n", simp) 
   222 txt{*case @{term "n \<le> na"}*}
   223 apply (simp add: mod_geq diff_less)
   224 done
   225 
   226 lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
   227 by (cut_tac m = "m*n" and n = n in mod_div_equality, auto)
   228 
   229 lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
   230 by (simp add: mult_commute div_mult_self_is_m)
   231 
   232 (*mod_mult_distrib2 above is the counterpart for remainder*)
   233 
   234 
   235 subsection{*Proving facts about Quotient and Remainder*}
   236 
   237 lemma unique_quotient_lemma:
   238      "[| b*q' + r'  \<le> b*q + r;  0 < b;  r < b |]  
   239       ==> q' \<le> (q::nat)"
   240 apply (rule leI)
   241 apply (subst less_iff_Suc_add)
   242 apply (auto simp add: add_mult_distrib2)
   243 done
   244 
   245 lemma unique_quotient:
   246      "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  0 < b |]  
   247       ==> q = q'"
   248 apply (simp add: split_ifs quorem_def)
   249 apply (blast intro: order_antisym 
   250              dest: order_eq_refl [THEN unique_quotient_lemma] sym)+
   251 done
   252 
   253 lemma unique_remainder:
   254      "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  0 < b |]  
   255       ==> r = r'"
   256 apply (subgoal_tac "q = q'")
   257 prefer 2 apply (blast intro: unique_quotient)
   258 apply (simp add: quorem_def)
   259 done
   260 
   261 lemma quorem_div_mod: "0 < b ==> quorem ((a, b), (a div b, a mod b))"
   262 by (auto simp add: quorem_def)
   263 
   264 lemma quorem_div: "[| quorem((a,b),(q,r));  0 < b |] ==> a div b = q"
   265 by (simp add: quorem_div_mod [THEN unique_quotient])
   266 
   267 lemma quorem_mod: "[| quorem((a,b),(q,r));  0 < b |] ==> a mod b = r"
   268 by (simp add: quorem_div_mod [THEN unique_remainder])
   269 
   270 (** A dividend of zero **)
   271 
   272 lemma div_0 [simp]: "0 div m = (0::nat)"
   273 by (case_tac "m=0", simp_all)
   274 
   275 lemma mod_0 [simp]: "0 mod m = (0::nat)"
   276 by (case_tac "m=0", simp_all)
   277 
   278 (** proving (a*b) div c = a * (b div c) + a * (b mod c) **)
   279 
   280 lemma quorem_mult1_eq:
   281      "[| quorem((b,c),(q,r));  0 < c |]  
   282       ==> quorem ((a*b, c), (a*q + a*r div c, a*r mod c))"
   283 apply (auto simp add: split_ifs mult_ac quorem_def add_mult_distrib2)
   284 done
   285 
   286 lemma div_mult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::nat)"
   287 apply (case_tac "c = 0", simp)
   288 apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_div])
   289 done
   290 
   291 lemma mod_mult1_eq: "(a*b) mod c = a*(b mod c) mod (c::nat)"
   292 apply (case_tac "c = 0", simp)
   293 apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_mod])
   294 done
   295 
   296 lemma mod_mult1_eq': "(a*b) mod (c::nat) = ((a mod c) * b) mod c"
   297 apply (rule trans)
   298 apply (rule_tac s = "b*a mod c" in trans)
   299 apply (rule_tac [2] mod_mult1_eq)
   300 apply (simp_all (no_asm) add: mult_commute)
   301 done
   302 
   303 lemma mod_mult_distrib_mod: "(a*b) mod (c::nat) = ((a mod c) * (b mod c)) mod c"
   304 apply (rule mod_mult1_eq' [THEN trans])
   305 apply (rule mod_mult1_eq)
   306 done
   307 
   308 (** proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) **)
   309 
   310 lemma quorem_add1_eq:
   311      "[| quorem((a,c),(aq,ar));  quorem((b,c),(bq,br));  0 < c |]  
   312       ==> quorem ((a+b, c), (aq + bq + (ar+br) div c, (ar+br) mod c))"
   313 by (auto simp add: split_ifs mult_ac quorem_def add_mult_distrib2)
   314 
   315 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
   316 lemma div_add1_eq:
   317      "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
   318 apply (case_tac "c = 0", simp)
   319 apply (blast intro: quorem_add1_eq [THEN quorem_div] quorem_div_mod quorem_div_mod)
   320 done
   321 
   322 lemma mod_add1_eq: "(a+b) mod (c::nat) = (a mod c + b mod c) mod c"
   323 apply (case_tac "c = 0", simp)
   324 apply (blast intro: quorem_div_mod quorem_div_mod
   325                     quorem_add1_eq [THEN quorem_mod])
   326 done
   327 
   328 
   329 subsection{*Proving @{term "a div (b*c) = (a div b) div c"}*}
   330 
   331 (** first, a lemma to bound the remainder **)
   332 
   333 lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
   334 apply (cut_tac m = q and n = c in mod_less_divisor)
   335 apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
   336 apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
   337 apply (simp add: add_mult_distrib2)
   338 done
   339 
   340 lemma quorem_mult2_eq: "[| quorem ((a,b), (q,r));  0 < b;  0 < c |]  
   341       ==> quorem ((a, b*c), (q div c, b*(q mod c) + r))"
   342 apply (auto simp add: mult_ac quorem_def add_mult_distrib2 [symmetric] mod_lemma)
   343 done
   344 
   345 lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
   346 apply (case_tac "b=0", simp)
   347 apply (case_tac "c=0", simp)
   348 apply (force simp add: quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_div])
   349 done
   350 
   351 lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
   352 apply (case_tac "b=0", simp)
   353 apply (case_tac "c=0", simp)
   354 apply (auto simp add: mult_commute quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_mod])
   355 done
   356 
   357 
   358 subsection{*Cancellation of Common Factors in Division*}
   359 
   360 lemma div_mult_mult_lemma:
   361      "[| (0::nat) < b;  0 < c |] ==> (c*a) div (c*b) = a div b"
   362 by (auto simp add: div_mult2_eq)
   363 
   364 lemma div_mult_mult1 [simp]: "(0::nat) < c ==> (c*a) div (c*b) = a div b"
   365 apply (case_tac "b = 0")
   366 apply (auto simp add: linorder_neq_iff [of b] div_mult_mult_lemma)
   367 done
   368 
   369 lemma div_mult_mult2 [simp]: "(0::nat) < c ==> (a*c) div (b*c) = a div b"
   370 apply (drule div_mult_mult1)
   371 apply (auto simp add: mult_commute)
   372 done
   373 
   374 
   375 (*Distribution of Factors over Remainders:
   376 
   377 Could prove these as in Integ/IntDiv.ML, but we already have
   378 mod_mult_distrib and mod_mult_distrib2 above!
   379 
   380 Goal "(c*a) mod (c*b) = (c::nat) * (a mod b)"
   381 qed "mod_mult_mult1";
   382 
   383 Goal "(a*c) mod (b*c) = (a mod b) * (c::nat)";
   384 qed "mod_mult_mult2";
   385  ***)
   386 
   387 subsection{*Further Facts about Quotient and Remainder*}
   388 
   389 lemma div_1 [simp]: "m div Suc 0 = m"
   390 apply (induct_tac "m")
   391 apply (simp_all (no_asm_simp) add: div_geq)
   392 done
   393 
   394 lemma div_self [simp]: "0<n ==> n div n = (1::nat)"
   395 by (simp add: div_geq)
   396 
   397 lemma div_add_self2: "0<n ==> (m+n) div n = Suc (m div n)"
   398 apply (subgoal_tac " (n + m) div n = Suc ((n+m-n) div n) ")
   399 apply (simp add: add_commute)
   400 apply (subst div_geq [symmetric], simp_all)
   401 done
   402 
   403 lemma div_add_self1: "0<n ==> (n+m) div n = Suc (m div n)"
   404 by (simp add: add_commute div_add_self2)
   405 
   406 lemma div_mult_self1 [simp]: "!!n::nat. 0<n ==> (m + k*n) div n = k + m div n"
   407 apply (subst div_add1_eq)
   408 apply (subst div_mult1_eq, simp)
   409 done
   410 
   411 lemma div_mult_self2 [simp]: "0<n ==> (m + n*k) div n = k + m div (n::nat)"
   412 by (simp add: mult_commute div_mult_self1)
   413 
   414 
   415 (* Monotonicity of div in first argument *)
   416 lemma div_le_mono [rule_format (no_asm)]:
   417      "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
   418 apply (case_tac "k=0", simp)
   419 apply (induct_tac "n" rule: nat_less_induct, clarify)
   420 apply (case_tac "n<k")
   421 (* 1  case n<k *)
   422 apply simp
   423 (* 2  case n >= k *)
   424 apply (case_tac "m<k")
   425 (* 2.1  case m<k *)
   426 apply simp
   427 (* 2.2  case m>=k *)
   428 apply (simp add: div_geq diff_less diff_le_mono)
   429 done
   430 
   431 (* Antimonotonicity of div in second argument *)
   432 lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
   433 apply (subgoal_tac "0<n")
   434  prefer 2 apply simp 
   435 apply (induct_tac "k" rule: nat_less_induct)
   436 apply (rename_tac "k")
   437 apply (case_tac "k<n", simp)
   438 apply (subgoal_tac "~ (k<m) ")
   439  prefer 2 apply simp 
   440 apply (simp add: div_geq)
   441 apply (subgoal_tac " (k-n) div n \<le> (k-m) div n")
   442  prefer 2
   443  apply (blast intro: div_le_mono diff_le_mono2)
   444 apply (rule le_trans, simp)
   445 apply (simp add: diff_less)
   446 done
   447 
   448 lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
   449 apply (case_tac "n=0", simp)
   450 apply (subgoal_tac "m div n \<le> m div 1", simp)
   451 apply (rule div_le_mono2)
   452 apply (simp_all (no_asm_simp))
   453 done
   454 
   455 (* Similar for "less than" *) 
   456 lemma div_less_dividend [rule_format, simp]:
   457      "!!n::nat. 1<n ==> 0 < m --> m div n < m"
   458 apply (induct_tac "m" rule: nat_less_induct)
   459 apply (rename_tac "m")
   460 apply (case_tac "m<n", simp)
   461 apply (subgoal_tac "0<n")
   462  prefer 2 apply simp 
   463 apply (simp add: div_geq)
   464 apply (case_tac "n<m")
   465  apply (subgoal_tac " (m-n) div n < (m-n) ")
   466   apply (rule impI less_trans_Suc)+
   467 apply assumption
   468   apply (simp_all add: diff_less)
   469 done
   470 
   471 text{*A fact for the mutilated chess board*}
   472 lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
   473 apply (case_tac "n=0", simp)
   474 apply (induct_tac "m" rule: nat_less_induct)
   475 apply (case_tac "Suc (na) <n")
   476 (* case Suc(na) < n *)
   477 apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
   478 (* case n \<le> Suc(na) *)
   479 apply (simp add: not_less_iff_le le_Suc_eq mod_geq)
   480 apply (auto simp add: Suc_diff_le diff_less le_mod_geq)
   481 done
   482 
   483 lemma nat_mod_div_trivial [simp]: "m mod n div n = (0 :: nat)"
   484 by (case_tac "n=0", auto)
   485 
   486 lemma nat_mod_mod_trivial [simp]: "m mod n mod n = (m mod n :: nat)"
   487 by (case_tac "n=0", auto)
   488 
   489 
   490 subsection{*The Divides Relation*}
   491 
   492 lemma dvdI [intro?]: "n = m * k ==> m dvd n"
   493 by (unfold dvd_def, blast)
   494 
   495 lemma dvdE [elim?]: "!!P. [|m dvd n;  !!k. n = m*k ==> P|] ==> P"
   496 by (unfold dvd_def, blast)
   497 
   498 lemma dvd_0_right [iff]: "m dvd (0::nat)"
   499 apply (unfold dvd_def)
   500 apply (blast intro: mult_0_right [symmetric])
   501 done
   502 
   503 lemma dvd_0_left: "0 dvd m ==> m = (0::nat)"
   504 by (force simp add: dvd_def)
   505 
   506 lemma dvd_0_left_iff [iff]: "(0 dvd (m::nat)) = (m = 0)"
   507 by (blast intro: dvd_0_left)
   508 
   509 lemma dvd_1_left [iff]: "Suc 0 dvd k"
   510 by (unfold dvd_def, simp)
   511 
   512 lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)"
   513 by (simp add: dvd_def)
   514 
   515 lemma dvd_refl [simp]: "m dvd (m::nat)"
   516 apply (unfold dvd_def)
   517 apply (blast intro: mult_1_right [symmetric])
   518 done
   519 
   520 lemma dvd_trans [trans]: "[| m dvd n; n dvd p |] ==> m dvd (p::nat)"
   521 apply (unfold dvd_def)
   522 apply (blast intro: mult_assoc)
   523 done
   524 
   525 lemma dvd_anti_sym: "[| m dvd n; n dvd m |] ==> m = (n::nat)"
   526 apply (unfold dvd_def)
   527 apply (force dest: mult_eq_self_implies_10 simp add: mult_assoc mult_eq_1_iff)
   528 done
   529 
   530 lemma dvd_add: "[| k dvd m; k dvd n |] ==> k dvd (m+n :: nat)"
   531 apply (unfold dvd_def)
   532 apply (blast intro: add_mult_distrib2 [symmetric])
   533 done
   534 
   535 lemma dvd_diff: "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)"
   536 apply (unfold dvd_def)
   537 apply (blast intro: diff_mult_distrib2 [symmetric])
   538 done
   539 
   540 lemma dvd_diffD: "[| k dvd m-n; k dvd n; n\<le>m |] ==> k dvd (m::nat)"
   541 apply (erule not_less_iff_le [THEN iffD2, THEN add_diff_inverse, THEN subst])
   542 apply (blast intro: dvd_add)
   543 done
   544 
   545 lemma dvd_diffD1: "[| k dvd m-n; k dvd m; n\<le>m |] ==> k dvd (n::nat)"
   546 by (drule_tac m = m in dvd_diff, auto)
   547 
   548 lemma dvd_mult: "k dvd n ==> k dvd (m*n :: nat)"
   549 apply (unfold dvd_def)
   550 apply (blast intro: mult_left_commute)
   551 done
   552 
   553 lemma dvd_mult2: "k dvd m ==> k dvd (m*n :: nat)"
   554 apply (subst mult_commute)
   555 apply (erule dvd_mult)
   556 done
   557 
   558 (* k dvd (m*k) *)
   559 declare dvd_refl [THEN dvd_mult, iff] dvd_refl [THEN dvd_mult2, iff]
   560 
   561 lemma dvd_reduce: "(k dvd n + k) = (k dvd (n::nat))"
   562 apply (rule iffI)
   563 apply (erule_tac [2] dvd_add)
   564 apply (rule_tac [2] dvd_refl)
   565 apply (subgoal_tac "n = (n+k) -k")
   566  prefer 2 apply simp 
   567 apply (erule ssubst)
   568 apply (erule dvd_diff)
   569 apply (rule dvd_refl)
   570 done
   571 
   572 lemma dvd_mod: "!!n::nat. [| f dvd m; f dvd n |] ==> f dvd m mod n"
   573 apply (unfold dvd_def)
   574 apply (case_tac "n=0", auto)
   575 apply (blast intro: mod_mult_distrib2 [symmetric])
   576 done
   577 
   578 lemma dvd_mod_imp_dvd: "[| (k::nat) dvd m mod n;  k dvd n |] ==> k dvd m"
   579 apply (subgoal_tac "k dvd (m div n) *n + m mod n")
   580  apply (simp add: mod_div_equality)
   581 apply (simp only: dvd_add dvd_mult)
   582 done
   583 
   584 lemma dvd_mod_iff: "k dvd n ==> ((k::nat) dvd m mod n) = (k dvd m)"
   585 by (blast intro: dvd_mod_imp_dvd dvd_mod)
   586 
   587 lemma dvd_mult_cancel: "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n"
   588 apply (unfold dvd_def)
   589 apply (erule exE)
   590 apply (simp add: mult_ac)
   591 done
   592 
   593 lemma dvd_mult_cancel1: "0<m ==> (m*n dvd m) = (n = (1::nat))"
   594 apply auto
   595 apply (subgoal_tac "m*n dvd m*1")
   596 apply (drule dvd_mult_cancel, auto)
   597 done
   598 
   599 lemma dvd_mult_cancel2: "0<m ==> (n*m dvd m) = (n = (1::nat))"
   600 apply (subst mult_commute)
   601 apply (erule dvd_mult_cancel1)
   602 done
   603 
   604 lemma mult_dvd_mono: "[| i dvd m; j dvd n|] ==> i*j dvd (m*n :: nat)"
   605 apply (unfold dvd_def, clarify)
   606 apply (rule_tac x = "k*ka" in exI)
   607 apply (simp add: mult_ac)
   608 done
   609 
   610 lemma dvd_mult_left: "(i*j :: nat) dvd k ==> i dvd k"
   611 by (simp add: dvd_def mult_assoc, blast)
   612 
   613 lemma dvd_mult_right: "(i*j :: nat) dvd k ==> j dvd k"
   614 apply (unfold dvd_def, clarify)
   615 apply (rule_tac x = "i*k" in exI)
   616 apply (simp add: mult_ac)
   617 done
   618 
   619 lemma dvd_imp_le: "[| k dvd n; 0 < n |] ==> k \<le> (n::nat)"
   620 apply (unfold dvd_def, clarify)
   621 apply (simp_all (no_asm_use) add: zero_less_mult_iff)
   622 apply (erule conjE)
   623 apply (rule le_trans)
   624 apply (rule_tac [2] le_refl [THEN mult_le_mono])
   625 apply (erule_tac [2] Suc_leI, simp)
   626 done
   627 
   628 lemma dvd_eq_mod_eq_0: "!!k::nat. (k dvd n) = (n mod k = 0)"
   629 apply (unfold dvd_def)
   630 apply (case_tac "k=0", simp, safe)
   631 apply (simp add: mult_commute)
   632 apply (rule_tac t = n and n1 = k in mod_div_equality [THEN subst])
   633 apply (subst mult_commute, simp)
   634 done
   635 
   636 lemma dvd_mult_div_cancel: "n dvd m ==> n * (m div n) = (m::nat)"
   637 apply (subgoal_tac "m mod n = 0")
   638  apply (simp add: mult_div_cancel)
   639 apply (simp only: dvd_eq_mod_eq_0)
   640 done
   641 
   642 lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
   643 by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
   644 declare mod_eq_0_iff [THEN iffD1, dest!]
   645 
   646 (*Loses information, namely we also have r<d provided d is nonzero*)
   647 lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
   648 apply (cut_tac m = m in mod_div_equality)
   649 apply (simp only: add_ac)
   650 apply (blast intro: sym)
   651 done
   652 
   653 
   654 lemma split_div:
   655  "P(n div k :: nat) =
   656  ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
   657  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   658 proof
   659   assume P: ?P
   660   show ?Q
   661   proof (cases)
   662     assume "k = 0"
   663     with P show ?Q by(simp add:DIVISION_BY_ZERO_DIV)
   664   next
   665     assume not0: "k \<noteq> 0"
   666     thus ?Q
   667     proof (simp, intro allI impI)
   668       fix i j
   669       assume n: "n = k*i + j" and j: "j < k"
   670       show "P i"
   671       proof (cases)
   672 	assume "i = 0"
   673 	with n j P show "P i" by simp
   674       next
   675 	assume "i \<noteq> 0"
   676 	with not0 n j P show "P i" by(simp add:add_ac)
   677       qed
   678     qed
   679   qed
   680 next
   681   assume Q: ?Q
   682   show ?P
   683   proof (cases)
   684     assume "k = 0"
   685     with Q show ?P by(simp add:DIVISION_BY_ZERO_DIV)
   686   next
   687     assume not0: "k \<noteq> 0"
   688     with Q have R: ?R by simp
   689     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   690     show ?P by simp
   691   qed
   692 qed
   693 
   694 lemma split_div_lemma:
   695   "0 < n \<Longrightarrow> (n * q \<le> m \<and> m < n * (Suc q)) = (q = ((m::nat) div n))"
   696   apply (rule iffI)
   697   apply (rule_tac a=m and r = "m - n * q" and r' = "m mod n" in unique_quotient)
   698   apply (simp_all add: quorem_def, arith)
   699   apply (rule conjI)
   700   apply (rule_tac P="%x. n * (m div n) \<le> x" in
   701     subst [OF mod_div_equality [of _ n]])
   702   apply (simp only: add: mult_ac)
   703   apply (rule_tac P="%x. x < n + n * (m div n)" in
   704     subst [OF mod_div_equality [of _ n]])
   705   apply (simp only: add: mult_ac add_ac)
   706   apply (rule add_less_mono1, simp)
   707   done
   708 
   709 theorem split_div':
   710   "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
   711    (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
   712   apply (case_tac "0 < n")
   713   apply (simp only: add: split_div_lemma)
   714   apply (simp_all add: DIVISION_BY_ZERO_DIV)
   715   done
   716 
   717 lemma split_mod:
   718  "P(n mod k :: nat) =
   719  ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
   720  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   721 proof
   722   assume P: ?P
   723   show ?Q
   724   proof (cases)
   725     assume "k = 0"
   726     with P show ?Q by(simp add:DIVISION_BY_ZERO_MOD)
   727   next
   728     assume not0: "k \<noteq> 0"
   729     thus ?Q
   730     proof (simp, intro allI impI)
   731       fix i j
   732       assume "n = k*i + j" "j < k"
   733       thus "P j" using not0 P by(simp add:add_ac mult_ac)
   734     qed
   735   qed
   736 next
   737   assume Q: ?Q
   738   show ?P
   739   proof (cases)
   740     assume "k = 0"
   741     with Q show ?P by(simp add:DIVISION_BY_ZERO_MOD)
   742   next
   743     assume not0: "k \<noteq> 0"
   744     with Q have R: ?R by simp
   745     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   746     show ?P by simp
   747   qed
   748 qed
   749 
   750 theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
   751   apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
   752     subst [OF mod_div_equality [of _ n]])
   753   apply arith
   754   done
   755 
   756 ML
   757 {*
   758 val div_def = thm "div_def"
   759 val mod_def = thm "mod_def"
   760 val dvd_def = thm "dvd_def"
   761 val quorem_def = thm "quorem_def"
   762 
   763 val wf_less_trans = thm "wf_less_trans";
   764 val mod_eq = thm "mod_eq";
   765 val div_eq = thm "div_eq";
   766 val DIVISION_BY_ZERO_DIV = thm "DIVISION_BY_ZERO_DIV";
   767 val DIVISION_BY_ZERO_MOD = thm "DIVISION_BY_ZERO_MOD";
   768 val mod_less = thm "mod_less";
   769 val mod_geq = thm "mod_geq";
   770 val le_mod_geq = thm "le_mod_geq";
   771 val mod_if = thm "mod_if";
   772 val mod_1 = thm "mod_1";
   773 val mod_self = thm "mod_self";
   774 val mod_add_self2 = thm "mod_add_self2";
   775 val mod_add_self1 = thm "mod_add_self1";
   776 val mod_mult_self1 = thm "mod_mult_self1";
   777 val mod_mult_self2 = thm "mod_mult_self2";
   778 val mod_mult_distrib = thm "mod_mult_distrib";
   779 val mod_mult_distrib2 = thm "mod_mult_distrib2";
   780 val mod_mult_self_is_0 = thm "mod_mult_self_is_0";
   781 val mod_mult_self1_is_0 = thm "mod_mult_self1_is_0";
   782 val div_less = thm "div_less";
   783 val div_geq = thm "div_geq";
   784 val le_div_geq = thm "le_div_geq";
   785 val div_if = thm "div_if";
   786 val mod_div_equality = thm "mod_div_equality";
   787 val mod_div_equality2 = thm "mod_div_equality2";
   788 val div_mod_equality = thm "div_mod_equality";
   789 val div_mod_equality2 = thm "div_mod_equality2";
   790 val mult_div_cancel = thm "mult_div_cancel";
   791 val mod_less_divisor = thm "mod_less_divisor";
   792 val div_mult_self_is_m = thm "div_mult_self_is_m";
   793 val div_mult_self1_is_m = thm "div_mult_self1_is_m";
   794 val unique_quotient_lemma = thm "unique_quotient_lemma";
   795 val unique_quotient = thm "unique_quotient";
   796 val unique_remainder = thm "unique_remainder";
   797 val div_0 = thm "div_0";
   798 val mod_0 = thm "mod_0";
   799 val div_mult1_eq = thm "div_mult1_eq";
   800 val mod_mult1_eq = thm "mod_mult1_eq";
   801 val mod_mult1_eq' = thm "mod_mult1_eq'";
   802 val mod_mult_distrib_mod = thm "mod_mult_distrib_mod";
   803 val div_add1_eq = thm "div_add1_eq";
   804 val mod_add1_eq = thm "mod_add1_eq";
   805 val mod_lemma = thm "mod_lemma";
   806 val div_mult2_eq = thm "div_mult2_eq";
   807 val mod_mult2_eq = thm "mod_mult2_eq";
   808 val div_mult_mult_lemma = thm "div_mult_mult_lemma";
   809 val div_mult_mult1 = thm "div_mult_mult1";
   810 val div_mult_mult2 = thm "div_mult_mult2";
   811 val div_1 = thm "div_1";
   812 val div_self = thm "div_self";
   813 val div_add_self2 = thm "div_add_self2";
   814 val div_add_self1 = thm "div_add_self1";
   815 val div_mult_self1 = thm "div_mult_self1";
   816 val div_mult_self2 = thm "div_mult_self2";
   817 val div_le_mono = thm "div_le_mono";
   818 val div_le_mono2 = thm "div_le_mono2";
   819 val div_le_dividend = thm "div_le_dividend";
   820 val div_less_dividend = thm "div_less_dividend";
   821 val mod_Suc = thm "mod_Suc";
   822 val dvdI = thm "dvdI";
   823 val dvdE = thm "dvdE";
   824 val dvd_0_right = thm "dvd_0_right";
   825 val dvd_0_left = thm "dvd_0_left";
   826 val dvd_0_left_iff = thm "dvd_0_left_iff";
   827 val dvd_1_left = thm "dvd_1_left";
   828 val dvd_1_iff_1 = thm "dvd_1_iff_1";
   829 val dvd_refl = thm "dvd_refl";
   830 val dvd_trans = thm "dvd_trans";
   831 val dvd_anti_sym = thm "dvd_anti_sym";
   832 val dvd_add = thm "dvd_add";
   833 val dvd_diff = thm "dvd_diff";
   834 val dvd_diffD = thm "dvd_diffD";
   835 val dvd_diffD1 = thm "dvd_diffD1";
   836 val dvd_mult = thm "dvd_mult";
   837 val dvd_mult2 = thm "dvd_mult2";
   838 val dvd_reduce = thm "dvd_reduce";
   839 val dvd_mod = thm "dvd_mod";
   840 val dvd_mod_imp_dvd = thm "dvd_mod_imp_dvd";
   841 val dvd_mod_iff = thm "dvd_mod_iff";
   842 val dvd_mult_cancel = thm "dvd_mult_cancel";
   843 val dvd_mult_cancel1 = thm "dvd_mult_cancel1";
   844 val dvd_mult_cancel2 = thm "dvd_mult_cancel2";
   845 val mult_dvd_mono = thm "mult_dvd_mono";
   846 val dvd_mult_left = thm "dvd_mult_left";
   847 val dvd_mult_right = thm "dvd_mult_right";
   848 val dvd_imp_le = thm "dvd_imp_le";
   849 val dvd_eq_mod_eq_0 = thm "dvd_eq_mod_eq_0";
   850 val dvd_mult_div_cancel = thm "dvd_mult_div_cancel";
   851 val mod_eq_0_iff = thm "mod_eq_0_iff";
   852 val mod_eqD = thm "mod_eqD";
   853 *}
   854 
   855 
   856 (*
   857 lemma split_div:
   858 assumes m: "m \<noteq> 0"
   859 shows "P(n div m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P i)"
   860        (is "?P = ?Q")
   861 proof
   862   assume P: ?P
   863   show ?Q
   864   proof (intro allI impI)
   865     fix i j
   866     assume n: "n = m*i + j" and j: "j < m"
   867     show "P i"
   868     proof (cases)
   869       assume "i = 0"
   870       with n j P show "P i" by simp
   871     next
   872       assume "i \<noteq> 0"
   873       with n j P show "P i" by (simp add:add_ac div_mult_self1)
   874     qed
   875   qed
   876 next
   877   assume Q: ?Q
   878   from m Q[THEN spec,of "n div m",THEN spec, of "n mod m"]
   879   show ?P by simp
   880 qed
   881 
   882 lemma split_mod:
   883 assumes m: "m \<noteq> 0"
   884 shows "P(n mod m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P j)"
   885        (is "?P = ?Q")
   886 proof
   887   assume P: ?P
   888   show ?Q
   889   proof (intro allI impI)
   890     fix i j
   891     assume "n = m*i + j" "j < m"
   892     thus "P j" using m P by(simp add:add_ac mult_ac)
   893   qed
   894 next
   895   assume Q: ?Q
   896   from m Q[THEN spec,of "n div m",THEN spec, of "n mod m"]
   897   show ?P by simp
   898 qed
   899 *)
   900 end