src/HOL/Divides.thy
author haftmann
Mon Nov 06 16:28:31 2006 +0100 (2006-11-06)
changeset 21191 c00161fbf990
parent 20640 05e6042394b9
child 21408 fff1731da03b
permissions -rw-r--r--
code generator module naming improved
     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 
     7 header {* The division operators div, mod and the divides relation "dvd" *}
     8 
     9 theory Divides
    10 imports Datatype
    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 linorder_not_less)
    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 linorder_not_less)
   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_tac 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"], K 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;  x < 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]:
   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 declare div_less_dividend [simp]
   479 
   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
   491 
   492 lemma nat_mod_div_trivial [simp]: "m mod n div n = (0 :: nat)"
   493 by (case_tac "n=0", auto)
   494 
   495 lemma nat_mod_mod_trivial [simp]: "m mod n mod n = (m mod n :: nat)"
   496 by (case_tac "n=0", auto)
   497 
   498 
   499 subsection{*The Divides Relation*}
   500 
   501 lemma dvdI [intro?]: "n = m * k ==> m dvd n"
   502 by (unfold dvd_def, blast)
   503 
   504 lemma dvdE [elim?]: "!!P. [|m dvd n;  !!k. n = m*k ==> P|] ==> P"
   505 by (unfold dvd_def, blast)
   506 
   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
   511 
   512 lemma dvd_0_left: "0 dvd m ==> m = (0::nat)"
   513 by (force simp add: dvd_def)
   514 
   515 lemma dvd_0_left_iff [iff]: "(0 dvd (m::nat)) = (m = 0)"
   516 by (blast intro: dvd_0_left)
   517 
   518 lemma dvd_1_left [iff]: "Suc 0 dvd k"
   519 by (unfold dvd_def, simp)
   520 
   521 lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)"
   522 by (simp add: dvd_def)
   523 
   524 lemma dvd_refl [simp]: "m dvd (m::nat)"
   525 apply (unfold dvd_def)
   526 apply (blast intro: mult_1_right [symmetric])
   527 done
   528 
   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
   533 
   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
   538 
   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
   543 
   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
   548 
   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
   553 
   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)
   556 
   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
   561 
   562 lemma dvd_mult2: "k dvd m ==> k dvd (m*n :: nat)"
   563 apply (subst mult_commute)
   564 apply (erule dvd_mult)
   565 done
   566 
   567 lemma dvd_triv_right [iff]: "k dvd (m*k :: nat)"
   568 by (rule dvd_refl [THEN dvd_mult])
   569 
   570 lemma dvd_triv_left [iff]: "k dvd (k*m :: nat)"
   571 by (rule dvd_refl [THEN dvd_mult2])
   572 
   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
   583 
   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
   589 
   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
   595 
   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)
   598 
   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
   604 
   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
   610 
   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
   615 
   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
   621 
   622 lemma dvd_mult_left: "(i*j :: nat) dvd k ==> i dvd k"
   623 by (simp add: dvd_def mult_assoc, blast)
   624 
   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
   630 
   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
   639 
   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
   647 
   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
   653 
   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)
   656 
   657 lemmas mod_eq_0D = mod_eq_0_iff [THEN iffD1]
   658 declare mod_eq_0D [dest!]
   659 
   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
   666 
   667 
   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
   707 
   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) apply arith
   714   apply (rule conjI)
   715   apply (rule_tac P="%x. n * (m div n) \<le> x" in
   716     subst [OF mod_div_equality [of _ n]])
   717   apply (simp only: add: mult_ac)
   718   apply (rule_tac P="%x. x < n + n * (m div n)" in
   719     subst [OF mod_div_equality [of _ n]])
   720   apply (simp only: add: mult_ac add_ac)
   721   apply (rule add_less_mono1, simp)
   722   done
   723 
   724 theorem split_div':
   725   "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
   726    (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
   727   apply (case_tac "0 < n")
   728   apply (simp only: add: split_div_lemma)
   729   apply (simp_all add: DIVISION_BY_ZERO_DIV)
   730   done
   731 
   732 lemma split_mod:
   733  "P(n mod k :: nat) =
   734  ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
   735  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   736 proof
   737   assume P: ?P
   738   show ?Q
   739   proof (cases)
   740     assume "k = 0"
   741     with P show ?Q by(simp add:DIVISION_BY_ZERO_MOD)
   742   next
   743     assume not0: "k \<noteq> 0"
   744     thus ?Q
   745     proof (simp, intro allI impI)
   746       fix i j
   747       assume "n = k*i + j" "j < k"
   748       thus "P j" using not0 P by(simp add:add_ac mult_ac)
   749     qed
   750   qed
   751 next
   752   assume Q: ?Q
   753   show ?P
   754   proof (cases)
   755     assume "k = 0"
   756     with Q show ?P by(simp add:DIVISION_BY_ZERO_MOD)
   757   next
   758     assume not0: "k \<noteq> 0"
   759     with Q have R: ?R by simp
   760     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   761     show ?P by simp
   762   qed
   763 qed
   764 
   765 theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
   766   apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
   767     subst [OF mod_div_equality [of _ n]])
   768   apply arith
   769   done
   770 
   771 subsection {*An ``induction'' law for modulus arithmetic.*}
   772 
   773 lemma mod_induct_0:
   774   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
   775   and base: "P i" and i: "i<p"
   776   shows "P 0"
   777 proof (rule ccontr)
   778   assume contra: "\<not>(P 0)"
   779   from i have p: "0<p" by simp
   780   have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
   781   proof
   782     fix k
   783     show "?A k"
   784     proof (induct k)
   785       show "?A 0" by simp  -- "by contradiction"
   786     next
   787       fix n
   788       assume ih: "?A n"
   789       show "?A (Suc n)"
   790       proof (clarsimp)
   791 	assume y: "P (p - Suc n)"
   792 	have n: "Suc n < p"
   793 	proof (rule ccontr)
   794 	  assume "\<not>(Suc n < p)"
   795 	  hence "p - Suc n = 0"
   796 	    by simp
   797 	  with y contra show "False"
   798 	    by simp
   799 	qed
   800 	hence n2: "Suc (p - Suc n) = p-n" by arith
   801 	from p have "p - Suc n < p" by arith
   802 	with y step have z: "P ((Suc (p - Suc n)) mod p)"
   803 	  by blast
   804 	show "False"
   805 	proof (cases "n=0")
   806 	  case True
   807 	  with z n2 contra show ?thesis by simp
   808 	next
   809 	  case False
   810 	  with p have "p-n < p" by arith
   811 	  with z n2 False ih show ?thesis by simp
   812 	qed
   813       qed
   814     qed
   815   qed
   816   moreover
   817   from i obtain k where "0<k \<and> i+k=p"
   818     by (blast dest: less_imp_add_positive)
   819   hence "0<k \<and> i=p-k" by auto
   820   moreover
   821   note base
   822   ultimately
   823   show "False" by blast
   824 qed
   825 
   826 lemma mod_induct:
   827   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
   828   and base: "P i" and i: "i<p" and j: "j<p"
   829   shows "P j"
   830 proof -
   831   have "\<forall>j<p. P j"
   832   proof
   833     fix j
   834     show "j<p \<longrightarrow> P j" (is "?A j")
   835     proof (induct j)
   836       from step base i show "?A 0"
   837 	by (auto elim: mod_induct_0)
   838     next
   839       fix k
   840       assume ih: "?A k"
   841       show "?A (Suc k)"
   842       proof
   843 	assume suc: "Suc k < p"
   844 	hence k: "k<p" by simp
   845 	with ih have "P k" ..
   846 	with step k have "P (Suc k mod p)"
   847 	  by blast
   848 	moreover
   849 	from suc have "Suc k mod p = Suc k"
   850 	  by simp
   851 	ultimately
   852 	show "P (Suc k)" by simp
   853       qed
   854     qed
   855   qed
   856   with j show ?thesis by blast
   857 qed
   858 
   859 
   860 lemma mod_add_left_eq: "((a::nat) + b) mod c = (a mod c + b) mod c"
   861   apply (rule trans [symmetric])
   862   apply (rule mod_add1_eq, simp)
   863   apply (rule mod_add1_eq [symmetric])
   864   done
   865 
   866 lemma mod_add_right_eq: "(a+b) mod (c::nat) = (a + (b mod c)) mod c"
   867 apply (rule trans [symmetric])
   868 apply (rule mod_add1_eq, simp)
   869 apply (rule mod_add1_eq [symmetric])
   870 done
   871 
   872 
   873 subsection {* Code generation for div and mod *}
   874 
   875 definition
   876   "divmod (m\<Colon>nat) n = (m div n, m mod n)"
   877 
   878 lemma divmod_zero [code]:
   879   "divmod m 0 = (0, m)"
   880   unfolding divmod_def by simp
   881 
   882 lemma divmod_succ [code]:
   883   "divmod m (Suc k) = (if m < Suc k then (0, m) else
   884     let
   885       (p, q) = divmod (m - Suc k) (Suc k)
   886     in (Suc p, q)
   887   )"
   888   unfolding divmod_def Let_def split_def
   889   by (auto intro: div_geq mod_geq)
   890 
   891 lemma div_divmod [code]:
   892   "m div n = fst (divmod m n)"
   893   unfolding divmod_def by simp
   894 
   895 lemma mod_divmod [code]:
   896   "m mod n = snd (divmod m n)"
   897   unfolding divmod_def by simp
   898 
   899 code_modulename SML
   900   Divides Integer
   901 
   902 hide (open) const divmod
   903 
   904 
   905 subsection {* Legacy bindings *}
   906 
   907 ML
   908 {*
   909 val div_def = thm "div_def"
   910 val mod_def = thm "mod_def"
   911 val dvd_def = thm "dvd_def"
   912 val quorem_def = thm "quorem_def"
   913 
   914 val wf_less_trans = thm "wf_less_trans";
   915 val mod_eq = thm "mod_eq";
   916 val div_eq = thm "div_eq";
   917 val DIVISION_BY_ZERO_DIV = thm "DIVISION_BY_ZERO_DIV";
   918 val DIVISION_BY_ZERO_MOD = thm "DIVISION_BY_ZERO_MOD";
   919 val mod_less = thm "mod_less";
   920 val mod_geq = thm "mod_geq";
   921 val le_mod_geq = thm "le_mod_geq";
   922 val mod_if = thm "mod_if";
   923 val mod_1 = thm "mod_1";
   924 val mod_self = thm "mod_self";
   925 val mod_add_self2 = thm "mod_add_self2";
   926 val mod_add_self1 = thm "mod_add_self1";
   927 val mod_mult_self1 = thm "mod_mult_self1";
   928 val mod_mult_self2 = thm "mod_mult_self2";
   929 val mod_mult_distrib = thm "mod_mult_distrib";
   930 val mod_mult_distrib2 = thm "mod_mult_distrib2";
   931 val mod_mult_self_is_0 = thm "mod_mult_self_is_0";
   932 val mod_mult_self1_is_0 = thm "mod_mult_self1_is_0";
   933 val div_less = thm "div_less";
   934 val div_geq = thm "div_geq";
   935 val le_div_geq = thm "le_div_geq";
   936 val div_if = thm "div_if";
   937 val mod_div_equality = thm "mod_div_equality";
   938 val mod_div_equality2 = thm "mod_div_equality2";
   939 val div_mod_equality = thm "div_mod_equality";
   940 val div_mod_equality2 = thm "div_mod_equality2";
   941 val mult_div_cancel = thm "mult_div_cancel";
   942 val mod_less_divisor = thm "mod_less_divisor";
   943 val div_mult_self_is_m = thm "div_mult_self_is_m";
   944 val div_mult_self1_is_m = thm "div_mult_self1_is_m";
   945 val unique_quotient_lemma = thm "unique_quotient_lemma";
   946 val unique_quotient = thm "unique_quotient";
   947 val unique_remainder = thm "unique_remainder";
   948 val div_0 = thm "div_0";
   949 val mod_0 = thm "mod_0";
   950 val div_mult1_eq = thm "div_mult1_eq";
   951 val mod_mult1_eq = thm "mod_mult1_eq";
   952 val mod_mult1_eq' = thm "mod_mult1_eq'";
   953 val mod_mult_distrib_mod = thm "mod_mult_distrib_mod";
   954 val div_add1_eq = thm "div_add1_eq";
   955 val mod_add1_eq = thm "mod_add1_eq";
   956 val mod_add_left_eq = thm "mod_add_left_eq";
   957  val mod_add_right_eq = thm "mod_add_right_eq";
   958 val mod_lemma = thm "mod_lemma";
   959 val div_mult2_eq = thm "div_mult2_eq";
   960 val mod_mult2_eq = thm "mod_mult2_eq";
   961 val div_mult_mult_lemma = thm "div_mult_mult_lemma";
   962 val div_mult_mult1 = thm "div_mult_mult1";
   963 val div_mult_mult2 = thm "div_mult_mult2";
   964 val div_1 = thm "div_1";
   965 val div_self = thm "div_self";
   966 val div_add_self2 = thm "div_add_self2";
   967 val div_add_self1 = thm "div_add_self1";
   968 val div_mult_self1 = thm "div_mult_self1";
   969 val div_mult_self2 = thm "div_mult_self2";
   970 val div_le_mono = thm "div_le_mono";
   971 val div_le_mono2 = thm "div_le_mono2";
   972 val div_le_dividend = thm "div_le_dividend";
   973 val div_less_dividend = thm "div_less_dividend";
   974 val mod_Suc = thm "mod_Suc";
   975 val dvdI = thm "dvdI";
   976 val dvdE = thm "dvdE";
   977 val dvd_0_right = thm "dvd_0_right";
   978 val dvd_0_left = thm "dvd_0_left";
   979 val dvd_0_left_iff = thm "dvd_0_left_iff";
   980 val dvd_1_left = thm "dvd_1_left";
   981 val dvd_1_iff_1 = thm "dvd_1_iff_1";
   982 val dvd_refl = thm "dvd_refl";
   983 val dvd_trans = thm "dvd_trans";
   984 val dvd_anti_sym = thm "dvd_anti_sym";
   985 val dvd_add = thm "dvd_add";
   986 val dvd_diff = thm "dvd_diff";
   987 val dvd_diffD = thm "dvd_diffD";
   988 val dvd_diffD1 = thm "dvd_diffD1";
   989 val dvd_mult = thm "dvd_mult";
   990 val dvd_mult2 = thm "dvd_mult2";
   991 val dvd_reduce = thm "dvd_reduce";
   992 val dvd_mod = thm "dvd_mod";
   993 val dvd_mod_imp_dvd = thm "dvd_mod_imp_dvd";
   994 val dvd_mod_iff = thm "dvd_mod_iff";
   995 val dvd_mult_cancel = thm "dvd_mult_cancel";
   996 val dvd_mult_cancel1 = thm "dvd_mult_cancel1";
   997 val dvd_mult_cancel2 = thm "dvd_mult_cancel2";
   998 val mult_dvd_mono = thm "mult_dvd_mono";
   999 val dvd_mult_left = thm "dvd_mult_left";
  1000 val dvd_mult_right = thm "dvd_mult_right";
  1001 val dvd_imp_le = thm "dvd_imp_le";
  1002 val dvd_eq_mod_eq_0 = thm "dvd_eq_mod_eq_0";
  1003 val dvd_mult_div_cancel = thm "dvd_mult_div_cancel";
  1004 val mod_eq_0_iff = thm "mod_eq_0_iff";
  1005 val mod_eqD = thm "mod_eqD";
  1006 *}
  1007 
  1008 
  1009 (*
  1010 lemma split_div:
  1011 assumes m: "m \<noteq> 0"
  1012 shows "P(n div m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P i)"
  1013        (is "?P = ?Q")
  1014 proof
  1015   assume P: ?P
  1016   show ?Q
  1017   proof (intro allI impI)
  1018     fix i j
  1019     assume n: "n = m*i + j" and j: "j < m"
  1020     show "P i"
  1021     proof (cases)
  1022       assume "i = 0"
  1023       with n j P show "P i" by simp
  1024     next
  1025       assume "i \<noteq> 0"
  1026       with n j P show "P i" by (simp add:add_ac div_mult_self1)
  1027     qed
  1028   qed
  1029 next
  1030   assume Q: ?Q
  1031   from m Q[THEN spec,of "n div m",THEN spec, of "n mod m"]
  1032   show ?P by simp
  1033 qed
  1034 
  1035 lemma split_mod:
  1036 assumes m: "m \<noteq> 0"
  1037 shows "P(n mod m :: nat) = (!i. !j<m. n = m*i + j \<longrightarrow> P j)"
  1038        (is "?P = ?Q")
  1039 proof
  1040   assume P: ?P
  1041   show ?Q
  1042   proof (intro allI impI)
  1043     fix i j
  1044     assume "n = m*i + j" "j < m"
  1045     thus "P j" using m P by(simp add:add_ac mult_ac)
  1046   qed
  1047 next
  1048   assume Q: ?Q
  1049   from m Q[THEN spec,of "n div m",THEN spec, of "n mod m"]
  1050   show ?P by simp
  1051 qed
  1052 *)
  1053 end