src/HOL/Divides.thy
author haftmann
Tue Apr 28 13:34:45 2009 +0200 (2009-04-28)
changeset 31009 41fd307cab30
parent 30934 ed5377c2b0a3
child 31661 1e252b8b2334
permissions -rw-r--r--
dropped reference to class recpower and lemma duplicate
     1 (*  Title:      HOL/Divides.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Copyright   1999  University of Cambridge
     4 *)
     5 
     6 header {* The division operators div and mod *}
     7 
     8 theory Divides
     9 imports Nat Power Product_Type
    10 uses "~~/src/Provers/Arith/cancel_div_mod.ML"
    11 begin
    12 
    13 subsection {* Syntactic division operations *}
    14 
    15 class div = dvd +
    16   fixes div :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "div" 70)
    17     and mod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "mod" 70)
    18 
    19 
    20 subsection {* Abstract division in commutative semirings. *}
    21 
    22 class semiring_div = comm_semiring_1_cancel + no_zero_divisors + div +
    23   assumes mod_div_equality: "a div b * b + a mod b = a"
    24     and div_by_0 [simp]: "a div 0 = 0"
    25     and div_0 [simp]: "0 div a = 0"
    26     and div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b"
    27     and div_mult_mult1 [simp]: "c \<noteq> 0 \<Longrightarrow> (c * a) div (c * b) = a div b"
    28 begin
    29 
    30 text {* @{const div} and @{const mod} *}
    31 
    32 lemma mod_div_equality2: "b * (a div b) + a mod b = a"
    33   unfolding mult_commute [of b]
    34   by (rule mod_div_equality)
    35 
    36 lemma mod_div_equality': "a mod b + a div b * b = a"
    37   using mod_div_equality [of a b]
    38   by (simp only: add_ac)
    39 
    40 lemma div_mod_equality: "((a div b) * b + a mod b) + c = a + c"
    41   by (simp add: mod_div_equality)
    42 
    43 lemma div_mod_equality2: "(b * (a div b) + a mod b) + c = a + c"
    44   by (simp add: mod_div_equality2)
    45 
    46 lemma mod_by_0 [simp]: "a mod 0 = a"
    47   using mod_div_equality [of a zero] by simp
    48 
    49 lemma mod_0 [simp]: "0 mod a = 0"
    50   using mod_div_equality [of zero a] div_0 by simp
    51 
    52 lemma div_mult_self2 [simp]:
    53   assumes "b \<noteq> 0"
    54   shows "(a + b * c) div b = c + a div b"
    55   using assms div_mult_self1 [of b a c] by (simp add: mult_commute)
    56 
    57 lemma mod_mult_self1 [simp]: "(a + c * b) mod b = a mod b"
    58 proof (cases "b = 0")
    59   case True then show ?thesis by simp
    60 next
    61   case False
    62   have "a + c * b = (a + c * b) div b * b + (a + c * b) mod b"
    63     by (simp add: mod_div_equality)
    64   also from False div_mult_self1 [of b a c] have
    65     "\<dots> = (c + a div b) * b + (a + c * b) mod b"
    66       by (simp add: algebra_simps)
    67   finally have "a = a div b * b + (a + c * b) mod b"
    68     by (simp add: add_commute [of a] add_assoc left_distrib)
    69   then have "a div b * b + (a + c * b) mod b = a div b * b + a mod b"
    70     by (simp add: mod_div_equality)
    71   then show ?thesis by simp
    72 qed
    73 
    74 lemma mod_mult_self2 [simp]: "(a + b * c) mod b = a mod b"
    75   by (simp add: mult_commute [of b])
    76 
    77 lemma div_mult_self1_is_id [simp]: "b \<noteq> 0 \<Longrightarrow> b * a div b = a"
    78   using div_mult_self2 [of b 0 a] by simp
    79 
    80 lemma div_mult_self2_is_id [simp]: "b \<noteq> 0 \<Longrightarrow> a * b div b = a"
    81   using div_mult_self1 [of b 0 a] by simp
    82 
    83 lemma mod_mult_self1_is_0 [simp]: "b * a mod b = 0"
    84   using mod_mult_self2 [of 0 b a] by simp
    85 
    86 lemma mod_mult_self2_is_0 [simp]: "a * b mod b = 0"
    87   using mod_mult_self1 [of 0 a b] by simp
    88 
    89 lemma div_by_1 [simp]: "a div 1 = a"
    90   using div_mult_self2_is_id [of 1 a] zero_neq_one by simp
    91 
    92 lemma mod_by_1 [simp]: "a mod 1 = 0"
    93 proof -
    94   from mod_div_equality [of a one] div_by_1 have "a + a mod 1 = a" by simp
    95   then have "a + a mod 1 = a + 0" by simp
    96   then show ?thesis by (rule add_left_imp_eq)
    97 qed
    98 
    99 lemma mod_self [simp]: "a mod a = 0"
   100   using mod_mult_self2_is_0 [of 1] by simp
   101 
   102 lemma div_self [simp]: "a \<noteq> 0 \<Longrightarrow> a div a = 1"
   103   using div_mult_self2_is_id [of _ 1] by simp
   104 
   105 lemma div_add_self1 [simp]:
   106   assumes "b \<noteq> 0"
   107   shows "(b + a) div b = a div b + 1"
   108   using assms div_mult_self1 [of b a 1] by (simp add: add_commute)
   109 
   110 lemma div_add_self2 [simp]:
   111   assumes "b \<noteq> 0"
   112   shows "(a + b) div b = a div b + 1"
   113   using assms div_add_self1 [of b a] by (simp add: add_commute)
   114 
   115 lemma mod_add_self1 [simp]:
   116   "(b + a) mod b = a mod b"
   117   using mod_mult_self1 [of a 1 b] by (simp add: add_commute)
   118 
   119 lemma mod_add_self2 [simp]:
   120   "(a + b) mod b = a mod b"
   121   using mod_mult_self1 [of a 1 b] by simp
   122 
   123 lemma mod_div_decomp:
   124   fixes a b
   125   obtains q r where "q = a div b" and "r = a mod b"
   126     and "a = q * b + r"
   127 proof -
   128   from mod_div_equality have "a = a div b * b + a mod b" by simp
   129   moreover have "a div b = a div b" ..
   130   moreover have "a mod b = a mod b" ..
   131   note that ultimately show thesis by blast
   132 qed
   133 
   134 lemma dvd_eq_mod_eq_0 [code unfold]: "a dvd b \<longleftrightarrow> b mod a = 0"
   135 proof
   136   assume "b mod a = 0"
   137   with mod_div_equality [of b a] have "b div a * a = b" by simp
   138   then have "b = a * (b div a)" unfolding mult_commute ..
   139   then have "\<exists>c. b = a * c" ..
   140   then show "a dvd b" unfolding dvd_def .
   141 next
   142   assume "a dvd b"
   143   then have "\<exists>c. b = a * c" unfolding dvd_def .
   144   then obtain c where "b = a * c" ..
   145   then have "b mod a = a * c mod a" by simp
   146   then have "b mod a = c * a mod a" by (simp add: mult_commute)
   147   then show "b mod a = 0" by simp
   148 qed
   149 
   150 lemma mod_div_trivial [simp]: "a mod b div b = 0"
   151 proof (cases "b = 0")
   152   assume "b = 0"
   153   thus ?thesis by simp
   154 next
   155   assume "b \<noteq> 0"
   156   hence "a div b + a mod b div b = (a mod b + a div b * b) div b"
   157     by (rule div_mult_self1 [symmetric])
   158   also have "\<dots> = a div b"
   159     by (simp only: mod_div_equality')
   160   also have "\<dots> = a div b + 0"
   161     by simp
   162   finally show ?thesis
   163     by (rule add_left_imp_eq)
   164 qed
   165 
   166 lemma mod_mod_trivial [simp]: "a mod b mod b = a mod b"
   167 proof -
   168   have "a mod b mod b = (a mod b + a div b * b) mod b"
   169     by (simp only: mod_mult_self1)
   170   also have "\<dots> = a mod b"
   171     by (simp only: mod_div_equality')
   172   finally show ?thesis .
   173 qed
   174 
   175 lemma dvd_imp_mod_0: "a dvd b \<Longrightarrow> b mod a = 0"
   176 by (rule dvd_eq_mod_eq_0[THEN iffD1])
   177 
   178 lemma dvd_div_mult_self: "a dvd b \<Longrightarrow> (b div a) * a = b"
   179 by (subst (2) mod_div_equality [of b a, symmetric]) (simp add:dvd_imp_mod_0)
   180 
   181 lemma dvd_div_mult: "a dvd b \<Longrightarrow> (b div a) * c = b * c div a"
   182 apply (cases "a = 0")
   183  apply simp
   184 apply (auto simp: dvd_def mult_assoc)
   185 done
   186 
   187 lemma div_dvd_div[simp]:
   188   "a dvd b \<Longrightarrow> a dvd c \<Longrightarrow> (b div a dvd c div a) = (b dvd c)"
   189 apply (cases "a = 0")
   190  apply simp
   191 apply (unfold dvd_def)
   192 apply auto
   193  apply(blast intro:mult_assoc[symmetric])
   194 apply(fastsimp simp add: mult_assoc)
   195 done
   196 
   197 lemma dvd_mod_imp_dvd: "[| k dvd m mod n;  k dvd n |] ==> k dvd m"
   198   apply (subgoal_tac "k dvd (m div n) *n + m mod n")
   199    apply (simp add: mod_div_equality)
   200   apply (simp only: dvd_add dvd_mult)
   201   done
   202 
   203 text {* Addition respects modular equivalence. *}
   204 
   205 lemma mod_add_left_eq: "(a + b) mod c = (a mod c + b) mod c"
   206 proof -
   207   have "(a + b) mod c = (a div c * c + a mod c + b) mod c"
   208     by (simp only: mod_div_equality)
   209   also have "\<dots> = (a mod c + b + a div c * c) mod c"
   210     by (simp only: add_ac)
   211   also have "\<dots> = (a mod c + b) mod c"
   212     by (rule mod_mult_self1)
   213   finally show ?thesis .
   214 qed
   215 
   216 lemma mod_add_right_eq: "(a + b) mod c = (a + b mod c) mod c"
   217 proof -
   218   have "(a + b) mod c = (a + (b div c * c + b mod c)) mod c"
   219     by (simp only: mod_div_equality)
   220   also have "\<dots> = (a + b mod c + b div c * c) mod c"
   221     by (simp only: add_ac)
   222   also have "\<dots> = (a + b mod c) mod c"
   223     by (rule mod_mult_self1)
   224   finally show ?thesis .
   225 qed
   226 
   227 lemma mod_add_eq: "(a + b) mod c = (a mod c + b mod c) mod c"
   228 by (rule trans [OF mod_add_left_eq mod_add_right_eq])
   229 
   230 lemma mod_add_cong:
   231   assumes "a mod c = a' mod c"
   232   assumes "b mod c = b' mod c"
   233   shows "(a + b) mod c = (a' + b') mod c"
   234 proof -
   235   have "(a mod c + b mod c) mod c = (a' mod c + b' mod c) mod c"
   236     unfolding assms ..
   237   thus ?thesis
   238     by (simp only: mod_add_eq [symmetric])
   239 qed
   240 
   241 lemma div_add [simp]: "z dvd x \<Longrightarrow> z dvd y
   242   \<Longrightarrow> (x + y) div z = x div z + y div z"
   243 by (cases "z = 0", simp, unfold dvd_def, auto simp add: algebra_simps)
   244 
   245 text {* Multiplication respects modular equivalence. *}
   246 
   247 lemma mod_mult_left_eq: "(a * b) mod c = ((a mod c) * b) mod c"
   248 proof -
   249   have "(a * b) mod c = ((a div c * c + a mod c) * b) mod c"
   250     by (simp only: mod_div_equality)
   251   also have "\<dots> = (a mod c * b + a div c * b * c) mod c"
   252     by (simp only: algebra_simps)
   253   also have "\<dots> = (a mod c * b) mod c"
   254     by (rule mod_mult_self1)
   255   finally show ?thesis .
   256 qed
   257 
   258 lemma mod_mult_right_eq: "(a * b) mod c = (a * (b mod c)) mod c"
   259 proof -
   260   have "(a * b) mod c = (a * (b div c * c + b mod c)) mod c"
   261     by (simp only: mod_div_equality)
   262   also have "\<dots> = (a * (b mod c) + a * (b div c) * c) mod c"
   263     by (simp only: algebra_simps)
   264   also have "\<dots> = (a * (b mod c)) mod c"
   265     by (rule mod_mult_self1)
   266   finally show ?thesis .
   267 qed
   268 
   269 lemma mod_mult_eq: "(a * b) mod c = ((a mod c) * (b mod c)) mod c"
   270 by (rule trans [OF mod_mult_left_eq mod_mult_right_eq])
   271 
   272 lemma mod_mult_cong:
   273   assumes "a mod c = a' mod c"
   274   assumes "b mod c = b' mod c"
   275   shows "(a * b) mod c = (a' * b') mod c"
   276 proof -
   277   have "(a mod c * (b mod c)) mod c = (a' mod c * (b' mod c)) mod c"
   278     unfolding assms ..
   279   thus ?thesis
   280     by (simp only: mod_mult_eq [symmetric])
   281 qed
   282 
   283 lemma mod_mod_cancel:
   284   assumes "c dvd b"
   285   shows "a mod b mod c = a mod c"
   286 proof -
   287   from `c dvd b` obtain k where "b = c * k"
   288     by (rule dvdE)
   289   have "a mod b mod c = a mod (c * k) mod c"
   290     by (simp only: `b = c * k`)
   291   also have "\<dots> = (a mod (c * k) + a div (c * k) * k * c) mod c"
   292     by (simp only: mod_mult_self1)
   293   also have "\<dots> = (a div (c * k) * (c * k) + a mod (c * k)) mod c"
   294     by (simp only: add_ac mult_ac)
   295   also have "\<dots> = a mod c"
   296     by (simp only: mod_div_equality)
   297   finally show ?thesis .
   298 qed
   299 
   300 lemma div_mult_div_if_dvd:
   301   "y dvd x \<Longrightarrow> z dvd w \<Longrightarrow> (x div y) * (w div z) = (x * w) div (y * z)"
   302   apply (cases "y = 0", simp)
   303   apply (cases "z = 0", simp)
   304   apply (auto elim!: dvdE simp add: algebra_simps)
   305   apply (subst mult_assoc [symmetric])
   306   apply (simp add: no_zero_divisors)
   307   done
   308 
   309 lemma div_mult_mult2 [simp]:
   310   "c \<noteq> 0 \<Longrightarrow> (a * c) div (b * c) = a div b"
   311   by (drule div_mult_mult1) (simp add: mult_commute)
   312 
   313 lemma div_mult_mult1_if [simp]:
   314   "(c * a) div (c * b) = (if c = 0 then 0 else a div b)"
   315   by simp_all
   316 
   317 lemma mod_mult_mult1:
   318   "(c * a) mod (c * b) = c * (a mod b)"
   319 proof (cases "c = 0")
   320   case True then show ?thesis by simp
   321 next
   322   case False
   323   from mod_div_equality
   324   have "((c * a) div (c * b)) * (c * b) + (c * a) mod (c * b) = c * a" .
   325   with False have "c * ((a div b) * b + a mod b) + (c * a) mod (c * b)
   326     = c * a + c * (a mod b)" by (simp add: algebra_simps)
   327   with mod_div_equality show ?thesis by simp 
   328 qed
   329   
   330 lemma mod_mult_mult2:
   331   "(a * c) mod (b * c) = (a mod b) * c"
   332   using mod_mult_mult1 [of c a b] by (simp add: mult_commute)
   333 
   334 end
   335 
   336 lemma div_power:
   337   "(y::'a::{semiring_div,no_zero_divisors,power}) dvd x \<Longrightarrow>
   338     (x div y) ^ n = x ^ n div y ^ n"
   339 apply (induct n)
   340  apply simp
   341 apply(simp add: div_mult_div_if_dvd dvd_power_same)
   342 done
   343 
   344 class ring_div = semiring_div + comm_ring_1
   345 begin
   346 
   347 text {* Negation respects modular equivalence. *}
   348 
   349 lemma mod_minus_eq: "(- a) mod b = (- (a mod b)) mod b"
   350 proof -
   351   have "(- a) mod b = (- (a div b * b + a mod b)) mod b"
   352     by (simp only: mod_div_equality)
   353   also have "\<dots> = (- (a mod b) + - (a div b) * b) mod b"
   354     by (simp only: minus_add_distrib minus_mult_left add_ac)
   355   also have "\<dots> = (- (a mod b)) mod b"
   356     by (rule mod_mult_self1)
   357   finally show ?thesis .
   358 qed
   359 
   360 lemma mod_minus_cong:
   361   assumes "a mod b = a' mod b"
   362   shows "(- a) mod b = (- a') mod b"
   363 proof -
   364   have "(- (a mod b)) mod b = (- (a' mod b)) mod b"
   365     unfolding assms ..
   366   thus ?thesis
   367     by (simp only: mod_minus_eq [symmetric])
   368 qed
   369 
   370 text {* Subtraction respects modular equivalence. *}
   371 
   372 lemma mod_diff_left_eq: "(a - b) mod c = (a mod c - b) mod c"
   373   unfolding diff_minus
   374   by (intro mod_add_cong mod_minus_cong) simp_all
   375 
   376 lemma mod_diff_right_eq: "(a - b) mod c = (a - b mod c) mod c"
   377   unfolding diff_minus
   378   by (intro mod_add_cong mod_minus_cong) simp_all
   379 
   380 lemma mod_diff_eq: "(a - b) mod c = (a mod c - b mod c) mod c"
   381   unfolding diff_minus
   382   by (intro mod_add_cong mod_minus_cong) simp_all
   383 
   384 lemma mod_diff_cong:
   385   assumes "a mod c = a' mod c"
   386   assumes "b mod c = b' mod c"
   387   shows "(a - b) mod c = (a' - b') mod c"
   388   unfolding diff_minus using assms
   389   by (intro mod_add_cong mod_minus_cong)
   390 
   391 lemma dvd_neg_div: "y dvd x \<Longrightarrow> -x div y = - (x div y)"
   392 apply (case_tac "y = 0") apply simp
   393 apply (auto simp add: dvd_def)
   394 apply (subgoal_tac "-(y * k) = y * - k")
   395  apply (erule ssubst)
   396  apply (erule div_mult_self1_is_id)
   397 apply simp
   398 done
   399 
   400 lemma dvd_div_neg: "y dvd x \<Longrightarrow> x div -y = - (x div y)"
   401 apply (case_tac "y = 0") apply simp
   402 apply (auto simp add: dvd_def)
   403 apply (subgoal_tac "y * k = -y * -k")
   404  apply (erule ssubst)
   405  apply (rule div_mult_self1_is_id)
   406  apply simp
   407 apply simp
   408 done
   409 
   410 end
   411 
   412 
   413 subsection {* Division on @{typ nat} *}
   414 
   415 text {*
   416   We define @{const div} and @{const mod} on @{typ nat} by means
   417   of a characteristic relation with two input arguments
   418   @{term "m\<Colon>nat"}, @{term "n\<Colon>nat"} and two output arguments
   419   @{term "q\<Colon>nat"}(uotient) and @{term "r\<Colon>nat"}(emainder).
   420 *}
   421 
   422 definition divmod_rel :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat \<Rightarrow> bool" where
   423   "divmod_rel m n qr \<longleftrightarrow>
   424     m = fst qr * n + snd qr \<and>
   425       (if n = 0 then fst qr = 0 else if n > 0 then 0 \<le> snd qr \<and> snd qr < n else n < snd qr \<and> snd qr \<le> 0)"
   426 
   427 text {* @{const divmod_rel} is total: *}
   428 
   429 lemma divmod_rel_ex:
   430   obtains q r where "divmod_rel m n (q, r)"
   431 proof (cases "n = 0")
   432   case True  with that show thesis
   433     by (auto simp add: divmod_rel_def)
   434 next
   435   case False
   436   have "\<exists>q r. m = q * n + r \<and> r < n"
   437   proof (induct m)
   438     case 0 with `n \<noteq> 0`
   439     have "(0\<Colon>nat) = 0 * n + 0 \<and> 0 < n" by simp
   440     then show ?case by blast
   441   next
   442     case (Suc m) then obtain q' r'
   443       where m: "m = q' * n + r'" and n: "r' < n" by auto
   444     then show ?case proof (cases "Suc r' < n")
   445       case True
   446       from m n have "Suc m = q' * n + Suc r'" by simp
   447       with True show ?thesis by blast
   448     next
   449       case False then have "n \<le> Suc r'" by auto
   450       moreover from n have "Suc r' \<le> n" by auto
   451       ultimately have "n = Suc r'" by auto
   452       with m have "Suc m = Suc q' * n + 0" by simp
   453       with `n \<noteq> 0` show ?thesis by blast
   454     qed
   455   qed
   456   with that show thesis
   457     using `n \<noteq> 0` by (auto simp add: divmod_rel_def)
   458 qed
   459 
   460 text {* @{const divmod_rel} is injective: *}
   461 
   462 lemma divmod_rel_unique:
   463   assumes "divmod_rel m n qr"
   464     and "divmod_rel m n qr'"
   465   shows "qr = qr'"
   466 proof (cases "n = 0")
   467   case True with assms show ?thesis
   468     by (cases qr, cases qr')
   469       (simp add: divmod_rel_def)
   470 next
   471   case False
   472   have aux: "\<And>q r q' r'. q' * n + r' = q * n + r \<Longrightarrow> r < n \<Longrightarrow> q' \<le> (q\<Colon>nat)"
   473   apply (rule leI)
   474   apply (subst less_iff_Suc_add)
   475   apply (auto simp add: add_mult_distrib)
   476   done
   477   from `n \<noteq> 0` assms have "fst qr = fst qr'"
   478     by (auto simp add: divmod_rel_def intro: order_antisym dest: aux sym)
   479   moreover from this assms have "snd qr = snd qr'"
   480     by (simp add: divmod_rel_def)
   481   ultimately show ?thesis by (cases qr, cases qr') simp
   482 qed
   483 
   484 text {*
   485   We instantiate divisibility on the natural numbers by
   486   means of @{const divmod_rel}:
   487 *}
   488 
   489 instantiation nat :: semiring_div
   490 begin
   491 
   492 definition divmod :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" where
   493   [code del]: "divmod m n = (THE qr. divmod_rel m n qr)"
   494 
   495 lemma divmod_rel_divmod:
   496   "divmod_rel m n (divmod m n)"
   497 proof -
   498   from divmod_rel_ex
   499     obtain qr where rel: "divmod_rel m n qr" .
   500   then show ?thesis
   501   by (auto simp add: divmod_def intro: theI elim: divmod_rel_unique)
   502 qed
   503 
   504 lemma divmod_eq:
   505   assumes "divmod_rel m n qr" 
   506   shows "divmod m n = qr"
   507   using assms by (auto intro: divmod_rel_unique divmod_rel_divmod)
   508 
   509 definition div_nat where
   510   "m div n = fst (divmod m n)"
   511 
   512 definition mod_nat where
   513   "m mod n = snd (divmod m n)"
   514 
   515 lemma divmod_div_mod:
   516   "divmod m n = (m div n, m mod n)"
   517   unfolding div_nat_def mod_nat_def by simp
   518 
   519 lemma div_eq:
   520   assumes "divmod_rel m n (q, r)" 
   521   shows "m div n = q"
   522   using assms by (auto dest: divmod_eq simp add: divmod_div_mod)
   523 
   524 lemma mod_eq:
   525   assumes "divmod_rel m n (q, r)" 
   526   shows "m mod n = r"
   527   using assms by (auto dest: divmod_eq simp add: divmod_div_mod)
   528 
   529 lemma divmod_rel: "divmod_rel m n (m div n, m mod n)"
   530   by (simp add: div_nat_def mod_nat_def divmod_rel_divmod)
   531 
   532 lemma divmod_zero:
   533   "divmod m 0 = (0, m)"
   534 proof -
   535   from divmod_rel [of m 0] show ?thesis
   536     unfolding divmod_div_mod divmod_rel_def by simp
   537 qed
   538 
   539 lemma divmod_base:
   540   assumes "m < n"
   541   shows "divmod m n = (0, m)"
   542 proof -
   543   from divmod_rel [of m n] show ?thesis
   544     unfolding divmod_div_mod divmod_rel_def
   545     using assms by (cases "m div n = 0")
   546       (auto simp add: gr0_conv_Suc [of "m div n"])
   547 qed
   548 
   549 lemma divmod_step:
   550   assumes "0 < n" and "n \<le> m"
   551   shows "divmod m n = (Suc ((m - n) div n), (m - n) mod n)"
   552 proof -
   553   from divmod_rel have divmod_m_n: "divmod_rel m n (m div n, m mod n)" .
   554   with assms have m_div_n: "m div n \<ge> 1"
   555     by (cases "m div n") (auto simp add: divmod_rel_def)
   556   from assms divmod_m_n have "divmod_rel (m - n) n (m div n - Suc 0, m mod n)"
   557     by (cases "m div n") (auto simp add: divmod_rel_def)
   558   with divmod_eq have "divmod (m - n) n = (m div n - Suc 0, m mod n)" by simp
   559   moreover from divmod_div_mod have "divmod (m - n) n = ((m - n) div n, (m - n) mod n)" .
   560   ultimately have "m div n = Suc ((m - n) div n)"
   561     and "m mod n = (m - n) mod n" using m_div_n by simp_all
   562   then show ?thesis using divmod_div_mod by simp
   563 qed
   564 
   565 text {* The ''recursion'' equations for @{const div} and @{const mod} *}
   566 
   567 lemma div_less [simp]:
   568   fixes m n :: nat
   569   assumes "m < n"
   570   shows "m div n = 0"
   571   using assms divmod_base divmod_div_mod by simp
   572 
   573 lemma le_div_geq:
   574   fixes m n :: nat
   575   assumes "0 < n" and "n \<le> m"
   576   shows "m div n = Suc ((m - n) div n)"
   577   using assms divmod_step divmod_div_mod by simp
   578 
   579 lemma mod_less [simp]:
   580   fixes m n :: nat
   581   assumes "m < n"
   582   shows "m mod n = m"
   583   using assms divmod_base divmod_div_mod by simp
   584 
   585 lemma le_mod_geq:
   586   fixes m n :: nat
   587   assumes "n \<le> m"
   588   shows "m mod n = (m - n) mod n"
   589   using assms divmod_step divmod_div_mod by (cases "n = 0") simp_all
   590 
   591 instance proof -
   592   have [simp]: "\<And>n::nat. n div 0 = 0"
   593     by (simp add: div_nat_def divmod_zero)
   594   have [simp]: "\<And>n::nat. 0 div n = 0"
   595   proof -
   596     fix n :: nat
   597     show "0 div n = 0"
   598       by (cases "n = 0") simp_all
   599   qed
   600   show "OFCLASS(nat, semiring_div_class)" proof
   601     fix m n :: nat
   602     show "m div n * n + m mod n = m"
   603       using divmod_rel [of m n] by (simp add: divmod_rel_def)
   604   next
   605     fix m n q :: nat
   606     assume "n \<noteq> 0"
   607     then show "(q + m * n) div n = m + q div n"
   608       by (induct m) (simp_all add: le_div_geq)
   609   next
   610     fix m n q :: nat
   611     assume "m \<noteq> 0"
   612     then show "(m * n) div (m * q) = n div q"
   613     proof (cases "n \<noteq> 0 \<and> q \<noteq> 0")
   614       case False then show ?thesis by auto
   615     next
   616       case True with `m \<noteq> 0`
   617         have "m > 0" and "n > 0" and "q > 0" by auto
   618       then have "\<And>a b. divmod_rel n q (a, b) \<Longrightarrow> divmod_rel (m * n) (m * q) (a, m * b)"
   619         by (auto simp add: divmod_rel_def) (simp_all add: algebra_simps)
   620       moreover from divmod_rel have "divmod_rel n q (n div q, n mod q)" .
   621       ultimately have "divmod_rel (m * n) (m * q) (n div q, m * (n mod q))" .
   622       then show ?thesis by (simp add: div_eq)
   623     qed
   624   qed simp_all
   625 qed
   626 
   627 end
   628 
   629 text {* Simproc for cancelling @{const div} and @{const mod} *}
   630 
   631 ML {*
   632 local
   633 
   634 structure CancelDivMod = CancelDivModFun(struct
   635 
   636   val div_name = @{const_name div};
   637   val mod_name = @{const_name mod};
   638   val mk_binop = HOLogic.mk_binop;
   639   val mk_sum = Nat_Arith.mk_sum;
   640   val dest_sum = Nat_Arith.dest_sum;
   641 
   642   val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
   643 
   644   val trans = trans;
   645 
   646   val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac
   647     (@{thm monoid_add_class.add_0_left} :: @{thm monoid_add_class.add_0_right} :: @{thms add_ac}))
   648 
   649 end)
   650 
   651 in
   652 
   653 val cancel_div_mod_nat_proc = Simplifier.simproc (the_context ())
   654   "cancel_div_mod" ["(m::nat) + n"] (K CancelDivMod.proc);
   655 
   656 val _ = Addsimprocs [cancel_div_mod_nat_proc];
   657 
   658 end
   659 *}
   660 
   661 text {* code generator setup *}
   662 
   663 lemma divmod_if [code]: "divmod m n = (if n = 0 \<or> m < n then (0, m) else
   664   let (q, r) = divmod (m - n) n in (Suc q, r))"
   665 by (simp add: divmod_zero divmod_base divmod_step)
   666     (simp add: divmod_div_mod)
   667 
   668 code_modulename SML
   669   Divides Nat
   670 
   671 code_modulename OCaml
   672   Divides Nat
   673 
   674 code_modulename Haskell
   675   Divides Nat
   676 
   677 
   678 subsubsection {* Quotient *}
   679 
   680 lemma div_geq: "0 < n \<Longrightarrow>  \<not> m < n \<Longrightarrow> m div n = Suc ((m - n) div n)"
   681 by (simp add: le_div_geq linorder_not_less)
   682 
   683 lemma div_if: "0 < n \<Longrightarrow> m div n = (if m < n then 0 else Suc ((m - n) div n))"
   684 by (simp add: div_geq)
   685 
   686 lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
   687 by simp
   688 
   689 lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
   690 by simp
   691 
   692 
   693 subsubsection {* Remainder *}
   694 
   695 lemma mod_less_divisor [simp]:
   696   fixes m n :: nat
   697   assumes "n > 0"
   698   shows "m mod n < (n::nat)"
   699   using assms divmod_rel [of m n] unfolding divmod_rel_def by auto
   700 
   701 lemma mod_less_eq_dividend [simp]:
   702   fixes m n :: nat
   703   shows "m mod n \<le> m"
   704 proof (rule add_leD2)
   705   from mod_div_equality have "m div n * n + m mod n = m" .
   706   then show "m div n * n + m mod n \<le> m" by auto
   707 qed
   708 
   709 lemma mod_geq: "\<not> m < (n\<Colon>nat) \<Longrightarrow> m mod n = (m - n) mod n"
   710 by (simp add: le_mod_geq linorder_not_less)
   711 
   712 lemma mod_if: "m mod (n\<Colon>nat) = (if m < n then m else (m - n) mod n)"
   713 by (simp add: le_mod_geq)
   714 
   715 lemma mod_1 [simp]: "m mod Suc 0 = 0"
   716 by (induct m) (simp_all add: mod_geq)
   717 
   718 lemma mod_mult_distrib: "(m mod n) * (k\<Colon>nat) = (m * k) mod (n * k)"
   719   apply (cases "n = 0", simp)
   720   apply (cases "k = 0", simp)
   721   apply (induct m rule: nat_less_induct)
   722   apply (subst mod_if, simp)
   723   apply (simp add: mod_geq diff_mult_distrib)
   724   done
   725 
   726 lemma mod_mult_distrib2: "(k::nat) * (m mod n) = (k*m) mod (k*n)"
   727 by (simp add: mult_commute [of k] mod_mult_distrib)
   728 
   729 (* a simple rearrangement of mod_div_equality: *)
   730 lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
   731 by (cut_tac a = m and b = n in mod_div_equality2, arith)
   732 
   733 lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
   734   apply (drule mod_less_divisor [where m = m])
   735   apply simp
   736   done
   737 
   738 subsubsection {* Quotient and Remainder *}
   739 
   740 lemma divmod_rel_mult1_eq:
   741   "divmod_rel b c (q, r) \<Longrightarrow> c > 0
   742    \<Longrightarrow> divmod_rel (a * b) c (a * q + a * r div c, a * r mod c)"
   743 by (auto simp add: split_ifs divmod_rel_def algebra_simps)
   744 
   745 lemma div_mult1_eq:
   746   "(a * b) div c = a * (b div c) + a * (b mod c) div (c::nat)"
   747 apply (cases "c = 0", simp)
   748 apply (blast intro: divmod_rel [THEN divmod_rel_mult1_eq, THEN div_eq])
   749 done
   750 
   751 lemma divmod_rel_add1_eq:
   752   "divmod_rel a c (aq, ar) \<Longrightarrow> divmod_rel b c (bq, br) \<Longrightarrow>  c > 0
   753    \<Longrightarrow> divmod_rel (a + b) c (aq + bq + (ar + br) div c, (ar + br) mod c)"
   754 by (auto simp add: split_ifs divmod_rel_def algebra_simps)
   755 
   756 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
   757 lemma div_add1_eq:
   758   "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
   759 apply (cases "c = 0", simp)
   760 apply (blast intro: divmod_rel_add1_eq [THEN div_eq] divmod_rel)
   761 done
   762 
   763 lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
   764   apply (cut_tac m = q and n = c in mod_less_divisor)
   765   apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
   766   apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
   767   apply (simp add: add_mult_distrib2)
   768   done
   769 
   770 lemma divmod_rel_mult2_eq:
   771   "divmod_rel a b (q, r) \<Longrightarrow> 0 < b \<Longrightarrow> 0 < c
   772    \<Longrightarrow> divmod_rel a (b * c) (q div c, b *(q mod c) + r)"
   773 by (auto simp add: mult_ac divmod_rel_def add_mult_distrib2 [symmetric] mod_lemma)
   774 
   775 lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
   776   apply (cases "b = 0", simp)
   777   apply (cases "c = 0", simp)
   778   apply (force simp add: divmod_rel [THEN divmod_rel_mult2_eq, THEN div_eq])
   779   done
   780 
   781 lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
   782   apply (cases "b = 0", simp)
   783   apply (cases "c = 0", simp)
   784   apply (auto simp add: mult_commute divmod_rel [THEN divmod_rel_mult2_eq, THEN mod_eq])
   785   done
   786 
   787 
   788 subsubsection{*Further Facts about Quotient and Remainder*}
   789 
   790 lemma div_1 [simp]: "m div Suc 0 = m"
   791 by (induct m) (simp_all add: div_geq)
   792 
   793 
   794 (* Monotonicity of div in first argument *)
   795 lemma div_le_mono [rule_format (no_asm)]:
   796     "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
   797 apply (case_tac "k=0", simp)
   798 apply (induct "n" rule: nat_less_induct, clarify)
   799 apply (case_tac "n<k")
   800 (* 1  case n<k *)
   801 apply simp
   802 (* 2  case n >= k *)
   803 apply (case_tac "m<k")
   804 (* 2.1  case m<k *)
   805 apply simp
   806 (* 2.2  case m>=k *)
   807 apply (simp add: div_geq diff_le_mono)
   808 done
   809 
   810 (* Antimonotonicity of div in second argument *)
   811 lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
   812 apply (subgoal_tac "0<n")
   813  prefer 2 apply simp
   814 apply (induct_tac k rule: nat_less_induct)
   815 apply (rename_tac "k")
   816 apply (case_tac "k<n", simp)
   817 apply (subgoal_tac "~ (k<m) ")
   818  prefer 2 apply simp
   819 apply (simp add: div_geq)
   820 apply (subgoal_tac "(k-n) div n \<le> (k-m) div n")
   821  prefer 2
   822  apply (blast intro: div_le_mono diff_le_mono2)
   823 apply (rule le_trans, simp)
   824 apply (simp)
   825 done
   826 
   827 lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
   828 apply (case_tac "n=0", simp)
   829 apply (subgoal_tac "m div n \<le> m div 1", simp)
   830 apply (rule div_le_mono2)
   831 apply (simp_all (no_asm_simp))
   832 done
   833 
   834 (* Similar for "less than" *)
   835 lemma div_less_dividend [rule_format]:
   836      "!!n::nat. 1<n ==> 0 < m --> m div n < m"
   837 apply (induct_tac m rule: nat_less_induct)
   838 apply (rename_tac "m")
   839 apply (case_tac "m<n", simp)
   840 apply (subgoal_tac "0<n")
   841  prefer 2 apply simp
   842 apply (simp add: div_geq)
   843 apply (case_tac "n<m")
   844  apply (subgoal_tac "(m-n) div n < (m-n) ")
   845   apply (rule impI less_trans_Suc)+
   846 apply assumption
   847   apply (simp_all)
   848 done
   849 
   850 declare div_less_dividend [simp]
   851 
   852 text{*A fact for the mutilated chess board*}
   853 lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
   854 apply (case_tac "n=0", simp)
   855 apply (induct "m" rule: nat_less_induct)
   856 apply (case_tac "Suc (na) <n")
   857 (* case Suc(na) < n *)
   858 apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
   859 (* case n \<le> Suc(na) *)
   860 apply (simp add: linorder_not_less le_Suc_eq mod_geq)
   861 apply (auto simp add: Suc_diff_le le_mod_geq)
   862 done
   863 
   864 
   865 subsubsection {* The Divides Relation *}
   866 
   867 lemma dvd_1_left [iff]: "Suc 0 dvd k"
   868   unfolding dvd_def by simp
   869 
   870 lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)"
   871 by (simp add: dvd_def)
   872 
   873 lemma nat_dvd_1_iff_1 [simp]: "m dvd (1::nat) \<longleftrightarrow> m = 1"
   874 by (simp add: dvd_def)
   875 
   876 lemma dvd_anti_sym: "[| m dvd n; n dvd m |] ==> m = (n::nat)"
   877   unfolding dvd_def
   878   by (force dest: mult_eq_self_implies_10 simp add: mult_assoc mult_eq_1_iff)
   879 
   880 text {* @{term "op dvd"} is a partial order *}
   881 
   882 interpretation dvd: order "op dvd" "\<lambda>n m \<Colon> nat. n dvd m \<and> \<not> m dvd n"
   883   proof qed (auto intro: dvd_refl dvd_trans dvd_anti_sym)
   884 
   885 lemma nat_dvd_diff[simp]: "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)"
   886 unfolding dvd_def
   887 by (blast intro: diff_mult_distrib2 [symmetric])
   888 
   889 lemma dvd_diffD: "[| k dvd m-n; k dvd n; n\<le>m |] ==> k dvd (m::nat)"
   890   apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
   891   apply (blast intro: dvd_add)
   892   done
   893 
   894 lemma dvd_diffD1: "[| k dvd m-n; k dvd m; n\<le>m |] ==> k dvd (n::nat)"
   895 by (drule_tac m = m in nat_dvd_diff, auto)
   896 
   897 lemma dvd_reduce: "(k dvd n + k) = (k dvd (n::nat))"
   898   apply (rule iffI)
   899    apply (erule_tac [2] dvd_add)
   900    apply (rule_tac [2] dvd_refl)
   901   apply (subgoal_tac "n = (n+k) -k")
   902    prefer 2 apply simp
   903   apply (erule ssubst)
   904   apply (erule nat_dvd_diff)
   905   apply (rule dvd_refl)
   906   done
   907 
   908 lemma dvd_mod: "!!n::nat. [| f dvd m; f dvd n |] ==> f dvd m mod n"
   909   unfolding dvd_def
   910   apply (case_tac "n = 0", auto)
   911   apply (blast intro: mod_mult_distrib2 [symmetric])
   912   done
   913 
   914 lemma dvd_mod_iff: "k dvd n ==> ((k::nat) dvd m mod n) = (k dvd m)"
   915 by (blast intro: dvd_mod_imp_dvd dvd_mod)
   916 
   917 lemma dvd_mult_cancel: "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n"
   918   unfolding dvd_def
   919   apply (erule exE)
   920   apply (simp add: mult_ac)
   921   done
   922 
   923 lemma dvd_mult_cancel1: "0<m ==> (m*n dvd m) = (n = (1::nat))"
   924   apply auto
   925    apply (subgoal_tac "m*n dvd m*1")
   926    apply (drule dvd_mult_cancel, auto)
   927   done
   928 
   929 lemma dvd_mult_cancel2: "0<m ==> (n*m dvd m) = (n = (1::nat))"
   930   apply (subst mult_commute)
   931   apply (erule dvd_mult_cancel1)
   932   done
   933 
   934 lemma dvd_imp_le: "[| k dvd n; 0 < n |] ==> k \<le> (n::nat)"
   935   by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
   936 
   937 lemma dvd_mult_div_cancel: "n dvd m ==> n * (m div n) = (m::nat)"
   938   by (simp add: dvd_eq_mod_eq_0 mult_div_cancel)
   939 
   940 lemma power_dvd_imp_le:
   941   "i ^ m dvd i ^ n \<Longrightarrow> (1::nat) < i \<Longrightarrow> m \<le> n"
   942   apply (rule power_le_imp_le_exp, assumption)
   943   apply (erule dvd_imp_le, simp)
   944   done
   945 
   946 lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
   947 by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
   948 
   949 lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]
   950 
   951 (*Loses information, namely we also have r<d provided d is nonzero*)
   952 lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
   953   apply (cut_tac a = m in mod_div_equality)
   954   apply (simp only: add_ac)
   955   apply (blast intro: sym)
   956   done
   957 
   958 lemma split_div:
   959  "P(n div k :: nat) =
   960  ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
   961  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   962 proof
   963   assume P: ?P
   964   show ?Q
   965   proof (cases)
   966     assume "k = 0"
   967     with P show ?Q by simp
   968   next
   969     assume not0: "k \<noteq> 0"
   970     thus ?Q
   971     proof (simp, intro allI impI)
   972       fix i j
   973       assume n: "n = k*i + j" and j: "j < k"
   974       show "P i"
   975       proof (cases)
   976         assume "i = 0"
   977         with n j P show "P i" by simp
   978       next
   979         assume "i \<noteq> 0"
   980         with not0 n j P show "P i" by(simp add:add_ac)
   981       qed
   982     qed
   983   qed
   984 next
   985   assume Q: ?Q
   986   show ?P
   987   proof (cases)
   988     assume "k = 0"
   989     with Q show ?P by simp
   990   next
   991     assume not0: "k \<noteq> 0"
   992     with Q have R: ?R by simp
   993     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   994     show ?P by simp
   995   qed
   996 qed
   997 
   998 lemma split_div_lemma:
   999   assumes "0 < n"
  1000   shows "n * q \<le> m \<and> m < n * Suc q \<longleftrightarrow> q = ((m\<Colon>nat) div n)" (is "?lhs \<longleftrightarrow> ?rhs")
  1001 proof
  1002   assume ?rhs
  1003   with mult_div_cancel have nq: "n * q = m - (m mod n)" by simp
  1004   then have A: "n * q \<le> m" by simp
  1005   have "n - (m mod n) > 0" using mod_less_divisor assms by auto
  1006   then have "m < m + (n - (m mod n))" by simp
  1007   then have "m < n + (m - (m mod n))" by simp
  1008   with nq have "m < n + n * q" by simp
  1009   then have B: "m < n * Suc q" by simp
  1010   from A B show ?lhs ..
  1011 next
  1012   assume P: ?lhs
  1013   then have "divmod_rel m n (q, m - n * q)"
  1014     unfolding divmod_rel_def by (auto simp add: mult_ac)
  1015   with divmod_rel_unique divmod_rel [of m n]
  1016   have "(q, m - n * q) = (m div n, m mod n)" by auto
  1017   then show ?rhs by simp
  1018 qed
  1019 
  1020 theorem split_div':
  1021   "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
  1022    (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
  1023   apply (case_tac "0 < n")
  1024   apply (simp only: add: split_div_lemma)
  1025   apply simp_all
  1026   done
  1027 
  1028 lemma split_mod:
  1029  "P(n mod k :: nat) =
  1030  ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
  1031  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
  1032 proof
  1033   assume P: ?P
  1034   show ?Q
  1035   proof (cases)
  1036     assume "k = 0"
  1037     with P show ?Q by simp
  1038   next
  1039     assume not0: "k \<noteq> 0"
  1040     thus ?Q
  1041     proof (simp, intro allI impI)
  1042       fix i j
  1043       assume "n = k*i + j" "j < k"
  1044       thus "P j" using not0 P by(simp add:add_ac mult_ac)
  1045     qed
  1046   qed
  1047 next
  1048   assume Q: ?Q
  1049   show ?P
  1050   proof (cases)
  1051     assume "k = 0"
  1052     with Q show ?P by simp
  1053   next
  1054     assume not0: "k \<noteq> 0"
  1055     with Q have R: ?R by simp
  1056     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
  1057     show ?P by simp
  1058   qed
  1059 qed
  1060 
  1061 theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
  1062   apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
  1063     subst [OF mod_div_equality [of _ n]])
  1064   apply arith
  1065   done
  1066 
  1067 lemma div_mod_equality':
  1068   fixes m n :: nat
  1069   shows "m div n * n = m - m mod n"
  1070 proof -
  1071   have "m mod n \<le> m mod n" ..
  1072   from div_mod_equality have 
  1073     "m div n * n + m mod n - m mod n = m - m mod n" by simp
  1074   with diff_add_assoc [OF `m mod n \<le> m mod n`, of "m div n * n"] have
  1075     "m div n * n + (m mod n - m mod n) = m - m mod n"
  1076     by simp
  1077   then show ?thesis by simp
  1078 qed
  1079 
  1080 
  1081 subsubsection {*An ``induction'' law for modulus arithmetic.*}
  1082 
  1083 lemma mod_induct_0:
  1084   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
  1085   and base: "P i" and i: "i<p"
  1086   shows "P 0"
  1087 proof (rule ccontr)
  1088   assume contra: "\<not>(P 0)"
  1089   from i have p: "0<p" by simp
  1090   have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
  1091   proof
  1092     fix k
  1093     show "?A k"
  1094     proof (induct k)
  1095       show "?A 0" by simp  -- "by contradiction"
  1096     next
  1097       fix n
  1098       assume ih: "?A n"
  1099       show "?A (Suc n)"
  1100       proof (clarsimp)
  1101         assume y: "P (p - Suc n)"
  1102         have n: "Suc n < p"
  1103         proof (rule ccontr)
  1104           assume "\<not>(Suc n < p)"
  1105           hence "p - Suc n = 0"
  1106             by simp
  1107           with y contra show "False"
  1108             by simp
  1109         qed
  1110         hence n2: "Suc (p - Suc n) = p-n" by arith
  1111         from p have "p - Suc n < p" by arith
  1112         with y step have z: "P ((Suc (p - Suc n)) mod p)"
  1113           by blast
  1114         show "False"
  1115         proof (cases "n=0")
  1116           case True
  1117           with z n2 contra show ?thesis by simp
  1118         next
  1119           case False
  1120           with p have "p-n < p" by arith
  1121           with z n2 False ih show ?thesis by simp
  1122         qed
  1123       qed
  1124     qed
  1125   qed
  1126   moreover
  1127   from i obtain k where "0<k \<and> i+k=p"
  1128     by (blast dest: less_imp_add_positive)
  1129   hence "0<k \<and> i=p-k" by auto
  1130   moreover
  1131   note base
  1132   ultimately
  1133   show "False" by blast
  1134 qed
  1135 
  1136 lemma mod_induct:
  1137   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
  1138   and base: "P i" and i: "i<p" and j: "j<p"
  1139   shows "P j"
  1140 proof -
  1141   have "\<forall>j<p. P j"
  1142   proof
  1143     fix j
  1144     show "j<p \<longrightarrow> P j" (is "?A j")
  1145     proof (induct j)
  1146       from step base i show "?A 0"
  1147         by (auto elim: mod_induct_0)
  1148     next
  1149       fix k
  1150       assume ih: "?A k"
  1151       show "?A (Suc k)"
  1152       proof
  1153         assume suc: "Suc k < p"
  1154         hence k: "k<p" by simp
  1155         with ih have "P k" ..
  1156         with step k have "P (Suc k mod p)"
  1157           by blast
  1158         moreover
  1159         from suc have "Suc k mod p = Suc k"
  1160           by simp
  1161         ultimately
  1162         show "P (Suc k)" by simp
  1163       qed
  1164     qed
  1165   qed
  1166   with j show ?thesis by blast
  1167 qed
  1168 
  1169 lemma nat_dvd_not_less:
  1170   fixes m n :: nat
  1171   shows "0 < m \<Longrightarrow> m < n \<Longrightarrow> \<not> n dvd m"
  1172 by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
  1173 
  1174 end