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