src/HOL/Divides.thy
author haftmann
Tue Oct 16 23:12:45 2007 +0200 (2007-10-16)
changeset 25062 af5ef0d4d655
parent 24993 92dfacb32053
child 25112 98824cc791c0
permissions -rw-r--r--
global class syntax
     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 class div = times +
    15   fixes div :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "div" 70)
    16   fixes mod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "mod" 70)
    17 
    18 instance nat :: Divides.div
    19   div_def: "m div n == wfrec (pred_nat^+)
    20                           (%f j. if j<n | n=0 then 0 else Suc (f (j-n))) m"
    21   mod_def: "m mod n == wfrec (pred_nat^+)
    22                           (%f j. if j<n | n=0 then j else f (j-n)) m" ..
    23 
    24 definition (in div)
    25   dvd  :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infixl "dvd" 50)
    26 where
    27   [code func del]: "m dvd n \<longleftrightarrow> (\<exists>k. n = m * k)"
    28 
    29 class dvd_mod = div + zero + -- {* for code generation *}
    30   assumes dvd_def_mod [code func]: "x dvd y \<longleftrightarrow> y mod x = 0"
    31 
    32 definition
    33   quorem :: "(nat*nat) * (nat*nat) => bool" where
    34   (*This definition helps prove the harder properties of div and mod.
    35     It is copied from IntDiv.thy; should it be overloaded?*)
    36   "quorem = (%((a,b), (q,r)).
    37                     a = b*q + r &
    38                     (if 0<b then 0\<le>r & r<b else b<r & r \<le>0))"
    39 
    40 
    41 
    42 subsection{*Initial Lemmas*}
    43 
    44 lemmas wf_less_trans =
    45        def_wfrec [THEN trans, OF eq_reflection wf_pred_nat [THEN wf_trancl],
    46                   standard]
    47 
    48 lemma mod_eq: "(%m. m mod n) =
    49               wfrec (pred_nat^+) (%f j. if j<n | n=0 then j else f (j-n))"
    50 by (simp add: mod_def)
    51 
    52 lemma div_eq: "(%m. m div n) = wfrec (pred_nat^+)
    53                (%f j. if j<n | n=0 then 0 else Suc (f (j-n)))"
    54 by (simp add: div_def)
    55 
    56 
    57 (** Aribtrary definitions for division by zero.  Useful to simplify
    58     certain equations **)
    59 
    60 lemma DIVISION_BY_ZERO_DIV [simp]: "a div 0 = (0::nat)"
    61   by (rule div_eq [THEN wf_less_trans], simp)
    62 
    63 lemma DIVISION_BY_ZERO_MOD [simp]: "a mod 0 = (a::nat)"
    64   by (rule mod_eq [THEN wf_less_trans], simp)
    65 
    66 
    67 subsection{*Remainder*}
    68 
    69 lemma mod_less [simp]: "m<n ==> m mod n = (m::nat)"
    70   by (rule mod_eq [THEN wf_less_trans]) simp
    71 
    72 lemma mod_geq: "~ m < (n::nat) ==> m mod n = (m-n) mod n"
    73   apply (cases "n=0")
    74    apply simp
    75   apply (rule mod_eq [THEN wf_less_trans])
    76   apply (simp add: cut_apply less_eq)
    77   done
    78 
    79 (*Avoids the ugly ~m<n above*)
    80 lemma le_mod_geq: "(n::nat) \<le> m ==> m mod n = (m-n) mod n"
    81   by (simp add: mod_geq linorder_not_less)
    82 
    83 lemma mod_if: "m mod (n::nat) = (if m<n then m else (m-n) mod n)"
    84   by (simp add: mod_geq)
    85 
    86 lemma mod_1 [simp]: "m mod Suc 0 = 0"
    87   by (induct m) (simp_all add: mod_geq)
    88 
    89 lemma mod_self [simp]: "n mod n = (0::nat)"
    90   by (cases "n = 0") (simp_all add: mod_geq)
    91 
    92 lemma mod_add_self2 [simp]: "(m+n) mod n = m mod (n::nat)"
    93   apply (subgoal_tac "(n + m) mod n = (n+m-n) mod n")
    94    apply (simp add: add_commute)
    95   apply (subst mod_geq [symmetric], simp_all)
    96   done
    97 
    98 lemma mod_add_self1 [simp]: "(n+m) mod n = m mod (n::nat)"
    99   by (simp add: add_commute mod_add_self2)
   100 
   101 lemma mod_mult_self1 [simp]: "(m + k*n) mod n = m mod (n::nat)"
   102   by (induct k) (simp_all add: add_left_commute [of _ n])
   103 
   104 lemma mod_mult_self2 [simp]: "(m + n*k) mod n = m mod (n::nat)"
   105   by (simp add: mult_commute mod_mult_self1)
   106 
   107 lemma mod_mult_distrib: "(m mod n) * (k::nat) = (m*k) mod (n*k)"
   108   apply (cases "n = 0", simp)
   109   apply (cases "k = 0", simp)
   110   apply (induct m rule: nat_less_induct)
   111   apply (subst mod_if, simp)
   112   apply (simp add: mod_geq diff_mult_distrib)
   113   done
   114 
   115 lemma mod_mult_distrib2: "(k::nat) * (m mod n) = (k*m) mod (k*n)"
   116   by (simp add: mult_commute [of k] mod_mult_distrib)
   117 
   118 lemma mod_mult_self_is_0 [simp]: "(m*n) mod n = (0::nat)"
   119   apply (cases "n = 0", simp)
   120   apply (induct m, simp)
   121   apply (rename_tac k)
   122   apply (cut_tac m = "k * n" and n = n in mod_add_self2)
   123   apply (simp add: add_commute)
   124   done
   125 
   126 lemma mod_mult_self1_is_0 [simp]: "(n*m) mod n = (0::nat)"
   127   by (simp add: mult_commute mod_mult_self_is_0)
   128 
   129 
   130 subsection{*Quotient*}
   131 
   132 lemma div_less [simp]: "m<n ==> m div n = (0::nat)"
   133   by (rule div_eq [THEN wf_less_trans], simp)
   134 
   135 lemma div_geq: "[| 0<n;  ~m<n |] ==> m div n = Suc((m-n) div n)"
   136   apply (rule div_eq [THEN wf_less_trans])
   137   apply (simp add: cut_apply less_eq)
   138   done
   139 
   140 (*Avoids the ugly ~m<n above*)
   141 lemma le_div_geq: "[| 0<n;  n\<le>m |] ==> m div n = Suc((m-n) div n)"
   142   by (simp add: div_geq linorder_not_less)
   143 
   144 lemma div_if: "0<n ==> m div n = (if m<n then 0 else Suc((m-n) div n))"
   145   by (simp add: div_geq)
   146 
   147 
   148 (*Main Result about quotient and remainder.*)
   149 lemma mod_div_equality: "(m div n)*n + m mod n = (m::nat)"
   150   apply (cases "n = 0", simp)
   151   apply (induct m rule: nat_less_induct)
   152   apply (subst mod_if)
   153   apply (simp_all add: add_assoc div_geq add_diff_inverse)
   154   done
   155 
   156 lemma mod_div_equality2: "n * (m div n) + m mod n = (m::nat)"
   157   apply (cut_tac m = m and n = n in mod_div_equality)
   158   apply (simp add: mult_commute)
   159   done
   160 
   161 subsection{*Simproc for Cancelling Div and Mod*}
   162 
   163 lemma div_mod_equality: "((m div n)*n + m mod n) + k = (m::nat) + k"
   164   by (simp add: mod_div_equality)
   165 
   166 lemma div_mod_equality2: "(n*(m div n) + m mod n) + k = (m::nat) + k"
   167   by (simp add: mod_div_equality2)
   168 
   169 ML
   170 {*
   171 structure CancelDivModData =
   172 struct
   173 
   174 val div_name = @{const_name Divides.div};
   175 val mod_name = @{const_name Divides.mod};
   176 val mk_binop = HOLogic.mk_binop;
   177 val mk_sum = NatArithUtils.mk_sum;
   178 val dest_sum = NatArithUtils.dest_sum;
   179 
   180 (*logic*)
   181 
   182 val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}]
   183 
   184 val trans = trans
   185 
   186 val prove_eq_sums =
   187   let val simps = @{thm add_0} :: @{thm add_0_right} :: @{thms add_ac}
   188   in NatArithUtils.prove_conv all_tac (NatArithUtils.simp_all_tac simps) end;
   189 
   190 end;
   191 
   192 structure CancelDivMod = CancelDivModFun(CancelDivModData);
   193 
   194 val cancel_div_mod_proc = NatArithUtils.prep_simproc
   195       ("cancel_div_mod", ["(m::nat) + n"], K CancelDivMod.proc);
   196 
   197 Addsimprocs[cancel_div_mod_proc];
   198 *}
   199 
   200 
   201 (* a simple rearrangement of mod_div_equality: *)
   202 lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
   203   by (cut_tac m = m and n = n in mod_div_equality2, arith)
   204 
   205 lemma mod_less_divisor [simp]: "0<n ==> m mod n < (n::nat)"
   206   apply (induct m rule: nat_less_induct)
   207   apply (rename_tac m)
   208   apply (case_tac "m<n", simp)
   209   txt{*case @{term "n \<le> m"}*}
   210   apply (simp add: mod_geq)
   211   done
   212 
   213 lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
   214   apply (drule mod_less_divisor [where m = m])
   215   apply simp
   216   done
   217 
   218 lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
   219   by (cut_tac m = "m*n" and n = n in mod_div_equality, auto)
   220 
   221 lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
   222   by (simp add: mult_commute div_mult_self_is_m)
   223 
   224 (*mod_mult_distrib2 above is the counterpart for remainder*)
   225 
   226 
   227 subsection{*Proving facts about Quotient and Remainder*}
   228 
   229 lemma unique_quotient_lemma:
   230      "[| b*q' + r'  \<le> b*q + r;  x < b;  r < b |]
   231       ==> q' \<le> (q::nat)"
   232   apply (rule leI)
   233   apply (subst less_iff_Suc_add)
   234   apply (auto simp add: add_mult_distrib2)
   235   done
   236 
   237 lemma unique_quotient:
   238      "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  0 < b |]
   239       ==> q = q'"
   240   apply (simp add: split_ifs quorem_def)
   241   apply (blast intro: order_antisym
   242     dest: order_eq_refl [THEN unique_quotient_lemma] sym)
   243   done
   244 
   245 lemma unique_remainder:
   246      "[| quorem ((a,b), (q,r));  quorem ((a,b), (q',r'));  0 < b |]
   247       ==> r = r'"
   248   apply (subgoal_tac "q = q'")
   249    prefer 2 apply (blast intro: unique_quotient)
   250   apply (simp add: quorem_def)
   251   done
   252 
   253 lemma quorem_div_mod: "0 < b ==> quorem ((a, b), (a div b, a mod b))"
   254   unfolding quorem_def by simp
   255 
   256 lemma quorem_div: "[| quorem((a,b),(q,r));  0 < b |] ==> a div b = q"
   257   by (simp add: quorem_div_mod [THEN unique_quotient])
   258 
   259 lemma quorem_mod: "[| quorem((a,b),(q,r));  0 < b |] ==> a mod b = r"
   260   by (simp add: quorem_div_mod [THEN unique_remainder])
   261 
   262 (** A dividend of zero **)
   263 
   264 lemma div_0 [simp]: "0 div m = (0::nat)"
   265   by (cases "m = 0") simp_all
   266 
   267 lemma mod_0 [simp]: "0 mod m = (0::nat)"
   268   by (cases "m = 0") simp_all
   269 
   270 (** proving (a*b) div c = a * (b div c) + a * (b mod c) **)
   271 
   272 lemma quorem_mult1_eq:
   273      "[| quorem((b,c),(q,r));  0 < c |]
   274       ==> quorem ((a*b, c), (a*q + a*r div c, a*r mod c))"
   275   by (auto simp add: split_ifs mult_ac quorem_def add_mult_distrib2)
   276 
   277 lemma div_mult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::nat)"
   278   apply (cases "c = 0", simp)
   279   apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_div])
   280   done
   281 
   282 lemma mod_mult1_eq: "(a*b) mod c = a*(b mod c) mod (c::nat)"
   283   apply (cases "c = 0", simp)
   284   apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_mod])
   285   done
   286 
   287 lemma mod_mult1_eq': "(a*b) mod (c::nat) = ((a mod c) * b) mod c"
   288   apply (rule trans)
   289    apply (rule_tac s = "b*a mod c" in trans)
   290     apply (rule_tac [2] mod_mult1_eq)
   291    apply (simp_all add: mult_commute)
   292   done
   293 
   294 lemma mod_mult_distrib_mod: "(a*b) mod (c::nat) = ((a mod c) * (b mod c)) mod c"
   295   apply (rule mod_mult1_eq' [THEN trans])
   296   apply (rule mod_mult1_eq)
   297   done
   298 
   299 (** proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) **)
   300 
   301 lemma quorem_add1_eq:
   302      "[| quorem((a,c),(aq,ar));  quorem((b,c),(bq,br));  0 < c |]
   303       ==> quorem ((a+b, c), (aq + bq + (ar+br) div c, (ar+br) mod c))"
   304   by (auto simp add: split_ifs mult_ac quorem_def add_mult_distrib2)
   305 
   306 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
   307 lemma div_add1_eq:
   308      "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
   309   apply (cases "c = 0", simp)
   310   apply (blast intro: quorem_add1_eq [THEN quorem_div] quorem_div_mod quorem_div_mod)
   311   done
   312 
   313 lemma mod_add1_eq: "(a+b) mod (c::nat) = (a mod c + b mod c) mod c"
   314   apply (cases "c = 0", simp)
   315   apply (blast intro: quorem_div_mod quorem_div_mod quorem_add1_eq [THEN quorem_mod])
   316   done
   317 
   318 
   319 subsection{*Proving @{term "a div (b*c) = (a div b) div c"}*}
   320 
   321 (** first, a lemma to bound the remainder **)
   322 
   323 lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
   324   apply (cut_tac m = q and n = c in mod_less_divisor)
   325   apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
   326   apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
   327   apply (simp add: add_mult_distrib2)
   328   done
   329 
   330 lemma quorem_mult2_eq: "[| quorem ((a,b), (q,r));  0 < b;  0 < c |]
   331       ==> quorem ((a, b*c), (q div c, b*(q mod c) + r))"
   332   by (auto simp add: mult_ac quorem_def add_mult_distrib2 [symmetric] mod_lemma)
   333 
   334 lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
   335   apply (cases "b = 0", simp)
   336   apply (cases "c = 0", simp)
   337   apply (force simp add: quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_div])
   338   done
   339 
   340 lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
   341   apply (cases "b = 0", simp)
   342   apply (cases "c = 0", simp)
   343   apply (auto simp add: mult_commute quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_mod])
   344   done
   345 
   346 
   347 subsection{*Cancellation of Common Factors in Division*}
   348 
   349 lemma div_mult_mult_lemma:
   350     "[| (0::nat) < b;  0 < c |] ==> (c*a) div (c*b) = a div b"
   351   by (auto simp add: div_mult2_eq)
   352 
   353 lemma div_mult_mult1 [simp]: "(0::nat) < c ==> (c*a) div (c*b) = a div b"
   354   apply (cases "b = 0")
   355   apply (auto simp add: linorder_neq_iff [of b] div_mult_mult_lemma)
   356   done
   357 
   358 lemma div_mult_mult2 [simp]: "(0::nat) < c ==> (a*c) div (b*c) = a div b"
   359   apply (drule div_mult_mult1)
   360   apply (auto simp add: mult_commute)
   361   done
   362 
   363 
   364 subsection{*Further Facts about Quotient and Remainder*}
   365 
   366 lemma div_1 [simp]: "m div Suc 0 = m"
   367   by (induct m) (simp_all add: div_geq)
   368 
   369 lemma div_self [simp]: "0<n ==> n div n = (1::nat)"
   370   by (simp add: div_geq)
   371 
   372 lemma div_add_self2: "0<n ==> (m+n) div n = Suc (m div n)"
   373   apply (subgoal_tac "(n + m) div n = Suc ((n+m-n) div n) ")
   374    apply (simp add: add_commute)
   375   apply (subst div_geq [symmetric], simp_all)
   376   done
   377 
   378 lemma div_add_self1: "0<n ==> (n+m) div n = Suc (m div n)"
   379   by (simp add: add_commute div_add_self2)
   380 
   381 lemma div_mult_self1 [simp]: "!!n::nat. 0<n ==> (m + k*n) div n = k + m div n"
   382   apply (subst div_add1_eq)
   383   apply (subst div_mult1_eq, simp)
   384   done
   385 
   386 lemma div_mult_self2 [simp]: "0<n ==> (m + n*k) div n = k + m div (n::nat)"
   387   by (simp add: mult_commute div_mult_self1)
   388 
   389 
   390 (* Monotonicity of div in first argument *)
   391 lemma div_le_mono [rule_format (no_asm)]:
   392     "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
   393 apply (case_tac "k=0", simp)
   394 apply (induct "n" rule: nat_less_induct, clarify)
   395 apply (case_tac "n<k")
   396 (* 1  case n<k *)
   397 apply simp
   398 (* 2  case n >= k *)
   399 apply (case_tac "m<k")
   400 (* 2.1  case m<k *)
   401 apply simp
   402 (* 2.2  case m>=k *)
   403 apply (simp add: div_geq diff_le_mono)
   404 done
   405 
   406 (* Antimonotonicity of div in second argument *)
   407 lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
   408 apply (subgoal_tac "0<n")
   409  prefer 2 apply simp
   410 apply (induct_tac k rule: nat_less_induct)
   411 apply (rename_tac "k")
   412 apply (case_tac "k<n", simp)
   413 apply (subgoal_tac "~ (k<m) ")
   414  prefer 2 apply simp
   415 apply (simp add: div_geq)
   416 apply (subgoal_tac "(k-n) div n \<le> (k-m) div n")
   417  prefer 2
   418  apply (blast intro: div_le_mono diff_le_mono2)
   419 apply (rule le_trans, simp)
   420 apply (simp)
   421 done
   422 
   423 lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
   424 apply (case_tac "n=0", simp)
   425 apply (subgoal_tac "m div n \<le> m div 1", simp)
   426 apply (rule div_le_mono2)
   427 apply (simp_all (no_asm_simp))
   428 done
   429 
   430 (* Similar for "less than" *)
   431 lemma div_less_dividend [rule_format]:
   432      "!!n::nat. 1<n ==> 0 < m --> m div n < m"
   433 apply (induct_tac m rule: nat_less_induct)
   434 apply (rename_tac "m")
   435 apply (case_tac "m<n", simp)
   436 apply (subgoal_tac "0<n")
   437  prefer 2 apply simp
   438 apply (simp add: div_geq)
   439 apply (case_tac "n<m")
   440  apply (subgoal_tac "(m-n) div n < (m-n) ")
   441   apply (rule impI less_trans_Suc)+
   442 apply assumption
   443   apply (simp_all)
   444 done
   445 
   446 declare div_less_dividend [simp]
   447 
   448 text{*A fact for the mutilated chess board*}
   449 lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
   450 apply (case_tac "n=0", simp)
   451 apply (induct "m" rule: nat_less_induct)
   452 apply (case_tac "Suc (na) <n")
   453 (* case Suc(na) < n *)
   454 apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
   455 (* case n \<le> Suc(na) *)
   456 apply (simp add: linorder_not_less le_Suc_eq mod_geq)
   457 apply (auto simp add: Suc_diff_le le_mod_geq)
   458 done
   459 
   460 lemma nat_mod_div_trivial [simp]: "m mod n div n = (0 :: nat)"
   461   by (cases "n = 0") auto
   462 
   463 lemma nat_mod_mod_trivial [simp]: "m mod n mod n = (m mod n :: nat)"
   464   by (cases "n = 0") auto
   465 
   466 
   467 subsection{*The Divides Relation*}
   468 
   469 lemma dvdI [intro?]: "n = m * k ==> m dvd n"
   470   unfolding dvd_def by blast
   471 
   472 lemma dvdE [elim?]: "!!P. [|m dvd n;  !!k. n = m*k ==> P|] ==> P"
   473   unfolding dvd_def by blast
   474 
   475 lemma dvd_0_right [iff]: "m dvd (0::nat)"
   476   unfolding dvd_def by (blast intro: mult_0_right [symmetric])
   477 
   478 lemma dvd_0_left: "0 dvd m ==> m = (0::nat)"
   479   by (force simp add: dvd_def)
   480 
   481 lemma dvd_0_left_iff [iff]: "(0 dvd (m::nat)) = (m = 0)"
   482   by (blast intro: dvd_0_left)
   483 
   484 declare dvd_0_left_iff [noatp]
   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 subsection {* Code generation for div, mod and dvd on nat *}
   882 
   883 definition [code func del]:
   884   "divmod (m\<Colon>nat) n = (m div n, m mod n)"
   885 
   886 lemma divmod_zero [code]: "divmod m 0 = (0, m)"
   887   unfolding divmod_def by simp
   888 
   889 lemma divmod_succ [code]:
   890   "divmod m (Suc k) = (if m < Suc k then (0, m) else
   891     let
   892       (p, q) = divmod (m - Suc k) (Suc k)
   893     in (Suc p, q))"
   894   unfolding divmod_def Let_def split_def
   895   by (auto intro: div_geq mod_geq)
   896 
   897 lemma div_divmod [code]: "m div n = fst (divmod m n)"
   898   unfolding divmod_def by simp
   899 
   900 lemma mod_divmod [code]: "m mod n = snd (divmod m n)"
   901   unfolding divmod_def by simp
   902 
   903 instance nat :: dvd_mod
   904   by default (simp add: dvd_eq_mod_eq_0)
   905 
   906 code_modulename SML
   907   Divides Nat
   908 
   909 code_modulename OCaml
   910   Divides Nat
   911 
   912 code_modulename Haskell
   913   Divides Nat
   914 
   915 hide (open) const divmod
   916 
   917 end