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