src/HOL/Divides.thy
author huffman
Tue Mar 27 15:40:11 2012 +0200 (2012-03-27)
changeset 47162 9d7d919b9fd8
parent 47160 8ada79014cb2
child 47163 248376f8881d
permissions -rw-r--r--
remove redundant lemma
     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_Numeral Nat_Transfer
    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]: "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_mult_div_cancel: "a dvd b \<Longrightarrow> a * (b div a) = b"
   182 by (drule dvd_div_mult_self) (simp add: mult_commute)
   183 
   184 lemma dvd_div_mult: "a dvd b \<Longrightarrow> (b div a) * c = b * c div a"
   185 apply (cases "a = 0")
   186  apply simp
   187 apply (auto simp: dvd_def mult_assoc)
   188 done
   189 
   190 lemma div_dvd_div[simp]:
   191   "a dvd b \<Longrightarrow> a dvd c \<Longrightarrow> (b div a dvd c div a) = (b dvd c)"
   192 apply (cases "a = 0")
   193  apply simp
   194 apply (unfold dvd_def)
   195 apply auto
   196  apply(blast intro:mult_assoc[symmetric])
   197 apply(fastforce simp add: mult_assoc)
   198 done
   199 
   200 lemma dvd_mod_imp_dvd: "[| k dvd m mod n;  k dvd n |] ==> k dvd m"
   201   apply (subgoal_tac "k dvd (m div n) *n + m mod n")
   202    apply (simp add: mod_div_equality)
   203   apply (simp only: dvd_add dvd_mult)
   204   done
   205 
   206 text {* Addition respects modular equivalence. *}
   207 
   208 lemma mod_add_left_eq: "(a + b) mod c = (a mod c + b) mod c"
   209 proof -
   210   have "(a + b) mod c = (a div c * c + a mod c + b) mod c"
   211     by (simp only: mod_div_equality)
   212   also have "\<dots> = (a mod c + b + a div c * c) mod c"
   213     by (simp only: add_ac)
   214   also have "\<dots> = (a mod c + b) mod c"
   215     by (rule mod_mult_self1)
   216   finally show ?thesis .
   217 qed
   218 
   219 lemma mod_add_right_eq: "(a + b) mod c = (a + b mod c) mod c"
   220 proof -
   221   have "(a + b) mod c = (a + (b div c * c + b mod c)) mod c"
   222     by (simp only: mod_div_equality)
   223   also have "\<dots> = (a + b mod c + b div c * c) mod c"
   224     by (simp only: add_ac)
   225   also have "\<dots> = (a + b mod c) mod c"
   226     by (rule mod_mult_self1)
   227   finally show ?thesis .
   228 qed
   229 
   230 lemma mod_add_eq: "(a + b) mod c = (a mod c + b mod c) mod c"
   231 by (rule trans [OF mod_add_left_eq mod_add_right_eq])
   232 
   233 lemma mod_add_cong:
   234   assumes "a mod c = a' mod c"
   235   assumes "b mod c = b' mod c"
   236   shows "(a + b) mod c = (a' + b') mod c"
   237 proof -
   238   have "(a mod c + b mod c) mod c = (a' mod c + b' mod c) mod c"
   239     unfolding assms ..
   240   thus ?thesis
   241     by (simp only: mod_add_eq [symmetric])
   242 qed
   243 
   244 lemma div_add [simp]: "z dvd x \<Longrightarrow> z dvd y
   245   \<Longrightarrow> (x + y) div z = x div z + y div z"
   246 by (cases "z = 0", simp, unfold dvd_def, auto simp add: algebra_simps)
   247 
   248 text {* Multiplication respects modular equivalence. *}
   249 
   250 lemma mod_mult_left_eq: "(a * b) mod c = ((a mod c) * b) mod c"
   251 proof -
   252   have "(a * b) mod c = ((a div c * c + a mod c) * b) mod c"
   253     by (simp only: mod_div_equality)
   254   also have "\<dots> = (a mod c * b + a div c * b * c) mod c"
   255     by (simp only: algebra_simps)
   256   also have "\<dots> = (a mod c * b) mod c"
   257     by (rule mod_mult_self1)
   258   finally show ?thesis .
   259 qed
   260 
   261 lemma mod_mult_right_eq: "(a * b) mod c = (a * (b mod c)) mod c"
   262 proof -
   263   have "(a * b) mod c = (a * (b div c * c + b mod c)) mod c"
   264     by (simp only: mod_div_equality)
   265   also have "\<dots> = (a * (b mod c) + a * (b div c) * c) mod c"
   266     by (simp only: algebra_simps)
   267   also have "\<dots> = (a * (b mod c)) mod c"
   268     by (rule mod_mult_self1)
   269   finally show ?thesis .
   270 qed
   271 
   272 lemma mod_mult_eq: "(a * b) mod c = ((a mod c) * (b mod c)) mod c"
   273 by (rule trans [OF mod_mult_left_eq mod_mult_right_eq])
   274 
   275 lemma mod_mult_cong:
   276   assumes "a mod c = a' mod c"
   277   assumes "b mod c = b' mod c"
   278   shows "(a * b) mod c = (a' * b') mod c"
   279 proof -
   280   have "(a mod c * (b mod c)) mod c = (a' mod c * (b' mod c)) mod c"
   281     unfolding assms ..
   282   thus ?thesis
   283     by (simp only: mod_mult_eq [symmetric])
   284 qed
   285 
   286 lemma mod_mod_cancel:
   287   assumes "c dvd b"
   288   shows "a mod b mod c = a mod c"
   289 proof -
   290   from `c dvd b` obtain k where "b = c * k"
   291     by (rule dvdE)
   292   have "a mod b mod c = a mod (c * k) mod c"
   293     by (simp only: `b = c * k`)
   294   also have "\<dots> = (a mod (c * k) + a div (c * k) * k * c) mod c"
   295     by (simp only: mod_mult_self1)
   296   also have "\<dots> = (a div (c * k) * (c * k) + a mod (c * k)) mod c"
   297     by (simp only: add_ac mult_ac)
   298   also have "\<dots> = a mod c"
   299     by (simp only: mod_div_equality)
   300   finally show ?thesis .
   301 qed
   302 
   303 lemma div_mult_div_if_dvd:
   304   "y dvd x \<Longrightarrow> z dvd w \<Longrightarrow> (x div y) * (w div z) = (x * w) div (y * z)"
   305   apply (cases "y = 0", simp)
   306   apply (cases "z = 0", simp)
   307   apply (auto elim!: dvdE simp add: algebra_simps)
   308   apply (subst mult_assoc [symmetric])
   309   apply (simp add: no_zero_divisors)
   310   done
   311 
   312 lemma div_mult_swap:
   313   assumes "c dvd b"
   314   shows "a * (b div c) = (a * b) div c"
   315 proof -
   316   from assms have "b div c * (a div 1) = b * a div (c * 1)"
   317     by (simp only: div_mult_div_if_dvd one_dvd)
   318   then show ?thesis by (simp add: mult_commute)
   319 qed
   320    
   321 lemma div_mult_mult2 [simp]:
   322   "c \<noteq> 0 \<Longrightarrow> (a * c) div (b * c) = a div b"
   323   by (drule div_mult_mult1) (simp add: mult_commute)
   324 
   325 lemma div_mult_mult1_if [simp]:
   326   "(c * a) div (c * b) = (if c = 0 then 0 else a div b)"
   327   by simp_all
   328 
   329 lemma mod_mult_mult1:
   330   "(c * a) mod (c * b) = c * (a mod b)"
   331 proof (cases "c = 0")
   332   case True then show ?thesis by simp
   333 next
   334   case False
   335   from mod_div_equality
   336   have "((c * a) div (c * b)) * (c * b) + (c * a) mod (c * b) = c * a" .
   337   with False have "c * ((a div b) * b + a mod b) + (c * a) mod (c * b)
   338     = c * a + c * (a mod b)" by (simp add: algebra_simps)
   339   with mod_div_equality show ?thesis by simp 
   340 qed
   341   
   342 lemma mod_mult_mult2:
   343   "(a * c) mod (b * c) = (a mod b) * c"
   344   using mod_mult_mult1 [of c a b] by (simp add: mult_commute)
   345 
   346 lemma mult_mod_left: "(a mod b) * c = (a * c) mod (b * c)"
   347   by (fact mod_mult_mult2 [symmetric])
   348 
   349 lemma mult_mod_right: "c * (a mod b) = (c * a) mod (c * b)"
   350   by (fact mod_mult_mult1 [symmetric])
   351 
   352 lemma dvd_mod: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd (m mod n)"
   353   unfolding dvd_def by (auto simp add: mod_mult_mult1)
   354 
   355 lemma dvd_mod_iff: "k dvd n \<Longrightarrow> k dvd (m mod n) \<longleftrightarrow> k dvd m"
   356 by (blast intro: dvd_mod_imp_dvd dvd_mod)
   357 
   358 lemma div_power:
   359   "y dvd x \<Longrightarrow> (x div y) ^ n = x ^ n div y ^ n"
   360 apply (induct n)
   361  apply simp
   362 apply(simp add: div_mult_div_if_dvd dvd_power_same)
   363 done
   364 
   365 lemma dvd_div_eq_mult:
   366   assumes "a \<noteq> 0" and "a dvd b"  
   367   shows "b div a = c \<longleftrightarrow> b = c * a"
   368 proof
   369   assume "b = c * a"
   370   then show "b div a = c" by (simp add: assms)
   371 next
   372   assume "b div a = c"
   373   then have "b div a * a = c * a" by simp
   374   moreover from `a dvd b` have "b div a * a = b" by (simp add: dvd_div_mult_self)
   375   ultimately show "b = c * a" by simp
   376 qed
   377    
   378 lemma dvd_div_div_eq_mult:
   379   assumes "a \<noteq> 0" "c \<noteq> 0" and "a dvd b" "c dvd d"
   380   shows "b div a = d div c \<longleftrightarrow> b * c = a * d"
   381   using assms by (auto simp add: mult_commute [of _ a] dvd_div_mult_self dvd_div_eq_mult div_mult_swap intro: sym)
   382 
   383 end
   384 
   385 class ring_div = semiring_div + comm_ring_1
   386 begin
   387 
   388 subclass ring_1_no_zero_divisors ..
   389 
   390 text {* Negation respects modular equivalence. *}
   391 
   392 lemma mod_minus_eq: "(- a) mod b = (- (a mod b)) mod b"
   393 proof -
   394   have "(- a) mod b = (- (a div b * b + a mod b)) mod b"
   395     by (simp only: mod_div_equality)
   396   also have "\<dots> = (- (a mod b) + - (a div b) * b) mod b"
   397     by (simp only: minus_add_distrib minus_mult_left add_ac)
   398   also have "\<dots> = (- (a mod b)) mod b"
   399     by (rule mod_mult_self1)
   400   finally show ?thesis .
   401 qed
   402 
   403 lemma mod_minus_cong:
   404   assumes "a mod b = a' mod b"
   405   shows "(- a) mod b = (- a') mod b"
   406 proof -
   407   have "(- (a mod b)) mod b = (- (a' mod b)) mod b"
   408     unfolding assms ..
   409   thus ?thesis
   410     by (simp only: mod_minus_eq [symmetric])
   411 qed
   412 
   413 text {* Subtraction respects modular equivalence. *}
   414 
   415 lemma mod_diff_left_eq: "(a - b) mod c = (a mod c - b) mod c"
   416   unfolding diff_minus
   417   by (intro mod_add_cong mod_minus_cong) simp_all
   418 
   419 lemma mod_diff_right_eq: "(a - b) mod c = (a - b mod c) mod c"
   420   unfolding diff_minus
   421   by (intro mod_add_cong mod_minus_cong) simp_all
   422 
   423 lemma mod_diff_eq: "(a - b) mod c = (a mod c - b mod c) mod c"
   424   unfolding diff_minus
   425   by (intro mod_add_cong mod_minus_cong) simp_all
   426 
   427 lemma mod_diff_cong:
   428   assumes "a mod c = a' mod c"
   429   assumes "b mod c = b' mod c"
   430   shows "(a - b) mod c = (a' - b') mod c"
   431   unfolding diff_minus using assms
   432   by (intro mod_add_cong mod_minus_cong)
   433 
   434 lemma dvd_neg_div: "y dvd x \<Longrightarrow> -x div y = - (x div y)"
   435 apply (case_tac "y = 0") apply simp
   436 apply (auto simp add: dvd_def)
   437 apply (subgoal_tac "-(y * k) = y * - k")
   438  apply (erule ssubst)
   439  apply (erule div_mult_self1_is_id)
   440 apply simp
   441 done
   442 
   443 lemma dvd_div_neg: "y dvd x \<Longrightarrow> x div -y = - (x div y)"
   444 apply (case_tac "y = 0") apply simp
   445 apply (auto simp add: dvd_def)
   446 apply (subgoal_tac "y * k = -y * -k")
   447  apply (erule ssubst)
   448  apply (rule div_mult_self1_is_id)
   449  apply simp
   450 apply simp
   451 done
   452 
   453 lemma div_minus_minus [simp]: "(-a) div (-b) = a div b"
   454   using div_mult_mult1 [of "- 1" a b]
   455   unfolding neg_equal_0_iff_equal by simp
   456 
   457 lemma mod_minus_minus [simp]: "(-a) mod (-b) = - (a mod b)"
   458   using mod_mult_mult1 [of "- 1" a b] by simp
   459 
   460 lemma div_minus_right: "a div (-b) = (-a) div b"
   461   using div_minus_minus [of "-a" b] by simp
   462 
   463 lemma mod_minus_right: "a mod (-b) = - ((-a) mod b)"
   464   using mod_minus_minus [of "-a" b] by simp
   465 
   466 lemma div_minus1_right [simp]: "a div (-1) = -a"
   467   using div_minus_right [of a 1] by simp
   468 
   469 lemma mod_minus1_right [simp]: "a mod (-1) = 0"
   470   using mod_minus_right [of a 1] by simp
   471 
   472 end
   473 
   474 
   475 subsection {* Division on @{typ nat} *}
   476 
   477 text {*
   478   We define @{const div} and @{const mod} on @{typ nat} by means
   479   of a characteristic relation with two input arguments
   480   @{term "m\<Colon>nat"}, @{term "n\<Colon>nat"} and two output arguments
   481   @{term "q\<Colon>nat"}(uotient) and @{term "r\<Colon>nat"}(emainder).
   482 *}
   483 
   484 definition divmod_nat_rel :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat \<Rightarrow> bool" where
   485   "divmod_nat_rel m n qr \<longleftrightarrow>
   486     m = fst qr * n + snd qr \<and>
   487       (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)"
   488 
   489 text {* @{const divmod_nat_rel} is total: *}
   490 
   491 lemma divmod_nat_rel_ex:
   492   obtains q r where "divmod_nat_rel m n (q, r)"
   493 proof (cases "n = 0")
   494   case True  with that show thesis
   495     by (auto simp add: divmod_nat_rel_def)
   496 next
   497   case False
   498   have "\<exists>q r. m = q * n + r \<and> r < n"
   499   proof (induct m)
   500     case 0 with `n \<noteq> 0`
   501     have "(0\<Colon>nat) = 0 * n + 0 \<and> 0 < n" by simp
   502     then show ?case by blast
   503   next
   504     case (Suc m) then obtain q' r'
   505       where m: "m = q' * n + r'" and n: "r' < n" by auto
   506     then show ?case proof (cases "Suc r' < n")
   507       case True
   508       from m n have "Suc m = q' * n + Suc r'" by simp
   509       with True show ?thesis by blast
   510     next
   511       case False then have "n \<le> Suc r'" by auto
   512       moreover from n have "Suc r' \<le> n" by auto
   513       ultimately have "n = Suc r'" by auto
   514       with m have "Suc m = Suc q' * n + 0" by simp
   515       with `n \<noteq> 0` show ?thesis by blast
   516     qed
   517   qed
   518   with that show thesis
   519     using `n \<noteq> 0` by (auto simp add: divmod_nat_rel_def)
   520 qed
   521 
   522 text {* @{const divmod_nat_rel} is injective: *}
   523 
   524 lemma divmod_nat_rel_unique:
   525   assumes "divmod_nat_rel m n qr"
   526     and "divmod_nat_rel m n qr'"
   527   shows "qr = qr'"
   528 proof (cases "n = 0")
   529   case True with assms show ?thesis
   530     by (cases qr, cases qr')
   531       (simp add: divmod_nat_rel_def)
   532 next
   533   case False
   534   have aux: "\<And>q r q' r'. q' * n + r' = q * n + r \<Longrightarrow> r < n \<Longrightarrow> q' \<le> (q\<Colon>nat)"
   535   apply (rule leI)
   536   apply (subst less_iff_Suc_add)
   537   apply (auto simp add: add_mult_distrib)
   538   done
   539   from `n \<noteq> 0` assms have "fst qr = fst qr'"
   540     by (auto simp add: divmod_nat_rel_def intro: order_antisym dest: aux sym)
   541   moreover from this assms have "snd qr = snd qr'"
   542     by (simp add: divmod_nat_rel_def)
   543   ultimately show ?thesis by (cases qr, cases qr') simp
   544 qed
   545 
   546 text {*
   547   We instantiate divisibility on the natural numbers by
   548   means of @{const divmod_nat_rel}:
   549 *}
   550 
   551 definition divmod_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" where
   552   "divmod_nat m n = (THE qr. divmod_nat_rel m n qr)"
   553 
   554 lemma divmod_nat_rel_divmod_nat:
   555   "divmod_nat_rel m n (divmod_nat m n)"
   556 proof -
   557   from divmod_nat_rel_ex
   558     obtain qr where rel: "divmod_nat_rel m n qr" .
   559   then show ?thesis
   560   by (auto simp add: divmod_nat_def intro: theI elim: divmod_nat_rel_unique)
   561 qed
   562 
   563 lemma divmod_nat_unique:
   564   assumes "divmod_nat_rel m n qr" 
   565   shows "divmod_nat m n = qr"
   566   using assms by (auto intro: divmod_nat_rel_unique divmod_nat_rel_divmod_nat)
   567 
   568 instantiation nat :: semiring_div
   569 begin
   570 
   571 definition div_nat where
   572   "m div n = fst (divmod_nat m n)"
   573 
   574 lemma fst_divmod_nat [simp]:
   575   "fst (divmod_nat m n) = m div n"
   576   by (simp add: div_nat_def)
   577 
   578 definition mod_nat where
   579   "m mod n = snd (divmod_nat m n)"
   580 
   581 lemma snd_divmod_nat [simp]:
   582   "snd (divmod_nat m n) = m mod n"
   583   by (simp add: mod_nat_def)
   584 
   585 lemma divmod_nat_div_mod:
   586   "divmod_nat m n = (m div n, m mod n)"
   587   by (simp add: prod_eq_iff)
   588 
   589 lemma div_nat_unique:
   590   assumes "divmod_nat_rel m n (q, r)" 
   591   shows "m div n = q"
   592   using assms by (auto dest!: divmod_nat_unique simp add: prod_eq_iff)
   593 
   594 lemma mod_nat_unique:
   595   assumes "divmod_nat_rel m n (q, r)" 
   596   shows "m mod n = r"
   597   using assms by (auto dest!: divmod_nat_unique simp add: prod_eq_iff)
   598 
   599 lemma divmod_nat_rel: "divmod_nat_rel m n (m div n, m mod n)"
   600   using divmod_nat_rel_divmod_nat by (simp add: divmod_nat_div_mod)
   601 
   602 lemma divmod_nat_zero: "divmod_nat m 0 = (0, m)"
   603   by (simp add: divmod_nat_unique divmod_nat_rel_def)
   604 
   605 lemma divmod_nat_zero_left: "divmod_nat 0 n = (0, 0)"
   606   by (simp add: divmod_nat_unique divmod_nat_rel_def)
   607 
   608 lemma divmod_nat_base: "m < n \<Longrightarrow> divmod_nat m n = (0, m)"
   609   by (simp add: divmod_nat_unique divmod_nat_rel_def)
   610 
   611 lemma divmod_nat_step:
   612   assumes "0 < n" and "n \<le> m"
   613   shows "divmod_nat m n = (Suc ((m - n) div n), (m - n) mod n)"
   614 proof (rule divmod_nat_unique)
   615   have "divmod_nat_rel (m - n) n ((m - n) div n, (m - n) mod n)"
   616     by (rule divmod_nat_rel)
   617   thus "divmod_nat_rel m n (Suc ((m - n) div n), (m - n) mod n)"
   618     unfolding divmod_nat_rel_def using assms by auto
   619 qed
   620 
   621 text {* The ''recursion'' equations for @{const div} and @{const mod} *}
   622 
   623 lemma div_less [simp]:
   624   fixes m n :: nat
   625   assumes "m < n"
   626   shows "m div n = 0"
   627   using assms divmod_nat_base by (simp add: prod_eq_iff)
   628 
   629 lemma le_div_geq:
   630   fixes m n :: nat
   631   assumes "0 < n" and "n \<le> m"
   632   shows "m div n = Suc ((m - n) div n)"
   633   using assms divmod_nat_step by (simp add: prod_eq_iff)
   634 
   635 lemma mod_less [simp]:
   636   fixes m n :: nat
   637   assumes "m < n"
   638   shows "m mod n = m"
   639   using assms divmod_nat_base by (simp add: prod_eq_iff)
   640 
   641 lemma le_mod_geq:
   642   fixes m n :: nat
   643   assumes "n \<le> m"
   644   shows "m mod n = (m - n) mod n"
   645   using assms divmod_nat_step by (cases "n = 0") (simp_all add: prod_eq_iff)
   646 
   647 instance proof
   648   fix m n :: nat
   649   show "m div n * n + m mod n = m"
   650     using divmod_nat_rel [of m n] by (simp add: divmod_nat_rel_def)
   651 next
   652   fix m n q :: nat
   653   assume "n \<noteq> 0"
   654   then show "(q + m * n) div n = m + q div n"
   655     by (induct m) (simp_all add: le_div_geq)
   656 next
   657   fix m n q :: nat
   658   assume "m \<noteq> 0"
   659   hence "\<And>a b. divmod_nat_rel n q (a, b) \<Longrightarrow> divmod_nat_rel (m * n) (m * q) (a, m * b)"
   660     unfolding divmod_nat_rel_def
   661     by (auto split: split_if_asm, simp_all add: algebra_simps)
   662   moreover from divmod_nat_rel have "divmod_nat_rel n q (n div q, n mod q)" .
   663   ultimately have "divmod_nat_rel (m * n) (m * q) (n div q, m * (n mod q))" .
   664   thus "(m * n) div (m * q) = n div q" by (rule div_nat_unique)
   665 next
   666   fix n :: nat show "n div 0 = 0"
   667     by (simp add: div_nat_def divmod_nat_zero)
   668 next
   669   fix n :: nat show "0 div n = 0"
   670     by (simp add: div_nat_def divmod_nat_zero_left)
   671 qed
   672 
   673 end
   674 
   675 lemma divmod_nat_if [code]: "divmod_nat m n = (if n = 0 \<or> m < n then (0, m) else
   676   let (q, r) = divmod_nat (m - n) n in (Suc q, r))"
   677   by (simp add: prod_eq_iff prod_case_beta not_less le_div_geq le_mod_geq)
   678 
   679 text {* Simproc for cancelling @{const div} and @{const mod} *}
   680 
   681 ML {*
   682 structure Cancel_Div_Mod_Nat = Cancel_Div_Mod
   683 (
   684   val div_name = @{const_name div};
   685   val mod_name = @{const_name mod};
   686   val mk_binop = HOLogic.mk_binop;
   687   val mk_sum = Nat_Arith.mk_sum;
   688   val dest_sum = Nat_Arith.dest_sum;
   689 
   690   val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
   691 
   692   val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac
   693     (@{thm add_0_left} :: @{thm add_0_right} :: @{thms add_ac}))
   694 )
   695 *}
   696 
   697 simproc_setup cancel_div_mod_nat ("(m::nat) + n") = {* K Cancel_Div_Mod_Nat.proc *}
   698 
   699 
   700 subsubsection {* Quotient *}
   701 
   702 lemma div_geq: "0 < n \<Longrightarrow>  \<not> m < n \<Longrightarrow> m div n = Suc ((m - n) div n)"
   703 by (simp add: le_div_geq linorder_not_less)
   704 
   705 lemma div_if: "0 < n \<Longrightarrow> m div n = (if m < n then 0 else Suc ((m - n) div n))"
   706 by (simp add: div_geq)
   707 
   708 lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
   709 by simp
   710 
   711 lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
   712 by simp
   713 
   714 
   715 subsubsection {* Remainder *}
   716 
   717 lemma mod_less_divisor [simp]:
   718   fixes m n :: nat
   719   assumes "n > 0"
   720   shows "m mod n < (n::nat)"
   721   using assms divmod_nat_rel [of m n] unfolding divmod_nat_rel_def by auto
   722 
   723 lemma mod_less_eq_dividend [simp]:
   724   fixes m n :: nat
   725   shows "m mod n \<le> m"
   726 proof (rule add_leD2)
   727   from mod_div_equality have "m div n * n + m mod n = m" .
   728   then show "m div n * n + m mod n \<le> m" by auto
   729 qed
   730 
   731 lemma mod_geq: "\<not> m < (n\<Colon>nat) \<Longrightarrow> m mod n = (m - n) mod n"
   732 by (simp add: le_mod_geq linorder_not_less)
   733 
   734 lemma mod_if: "m mod (n\<Colon>nat) = (if m < n then m else (m - n) mod n)"
   735 by (simp add: le_mod_geq)
   736 
   737 lemma mod_1 [simp]: "m mod Suc 0 = 0"
   738 by (induct m) (simp_all add: mod_geq)
   739 
   740 (* a simple rearrangement of mod_div_equality: *)
   741 lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
   742   using mod_div_equality2 [of n m] by arith
   743 
   744 lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
   745   apply (drule mod_less_divisor [where m = m])
   746   apply simp
   747   done
   748 
   749 subsubsection {* Quotient and Remainder *}
   750 
   751 lemma divmod_nat_rel_mult1_eq:
   752   "divmod_nat_rel b c (q, r)
   753    \<Longrightarrow> divmod_nat_rel (a * b) c (a * q + a * r div c, a * r mod c)"
   754 by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
   755 
   756 lemma div_mult1_eq:
   757   "(a * b) div c = a * (b div c) + a * (b mod c) div (c::nat)"
   758 by (blast intro: divmod_nat_rel_mult1_eq [THEN div_nat_unique] divmod_nat_rel)
   759 
   760 lemma divmod_nat_rel_add1_eq:
   761   "divmod_nat_rel a c (aq, ar) \<Longrightarrow> divmod_nat_rel b c (bq, br)
   762    \<Longrightarrow> divmod_nat_rel (a + b) c (aq + bq + (ar + br) div c, (ar + br) mod c)"
   763 by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
   764 
   765 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
   766 lemma div_add1_eq:
   767   "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
   768 by (blast intro: divmod_nat_rel_add1_eq [THEN div_nat_unique] divmod_nat_rel)
   769 
   770 lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
   771   apply (cut_tac m = q and n = c in mod_less_divisor)
   772   apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
   773   apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
   774   apply (simp add: add_mult_distrib2)
   775   done
   776 
   777 lemma divmod_nat_rel_mult2_eq:
   778   "divmod_nat_rel a b (q, r)
   779    \<Longrightarrow> divmod_nat_rel a (b * c) (q div c, b *(q mod c) + r)"
   780 by (auto simp add: mult_ac divmod_nat_rel_def add_mult_distrib2 [symmetric] mod_lemma)
   781 
   782 lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
   783 by (force simp add: divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN div_nat_unique])
   784 
   785 lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
   786 by (auto simp add: mult_commute divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN mod_nat_unique])
   787 
   788 
   789 subsubsection {* Further Facts about Quotient and Remainder *}
   790 
   791 lemma div_1 [simp]: "m div Suc 0 = m"
   792 by (induct m) (simp_all add: div_geq)
   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 [simp]:
   836   "\<lbrakk>(1::nat) < n; 0 < m\<rbrakk> \<Longrightarrow> m div n < m"
   837 apply (induct 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 text{*A fact for the mutilated chess board*}
   851 lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
   852 apply (case_tac "n=0", simp)
   853 apply (induct "m" rule: nat_less_induct)
   854 apply (case_tac "Suc (na) <n")
   855 (* case Suc(na) < n *)
   856 apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
   857 (* case n \<le> Suc(na) *)
   858 apply (simp add: linorder_not_less le_Suc_eq mod_geq)
   859 apply (auto simp add: Suc_diff_le le_mod_geq)
   860 done
   861 
   862 lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
   863 by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
   864 
   865 lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]
   866 
   867 (*Loses information, namely we also have r<d provided d is nonzero*)
   868 lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
   869   apply (cut_tac a = m in mod_div_equality)
   870   apply (simp only: add_ac)
   871   apply (blast intro: sym)
   872   done
   873 
   874 lemma split_div:
   875  "P(n div k :: nat) =
   876  ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
   877  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   878 proof
   879   assume P: ?P
   880   show ?Q
   881   proof (cases)
   882     assume "k = 0"
   883     with P show ?Q by simp
   884   next
   885     assume not0: "k \<noteq> 0"
   886     thus ?Q
   887     proof (simp, intro allI impI)
   888       fix i j
   889       assume n: "n = k*i + j" and j: "j < k"
   890       show "P i"
   891       proof (cases)
   892         assume "i = 0"
   893         with n j P show "P i" by simp
   894       next
   895         assume "i \<noteq> 0"
   896         with not0 n j P show "P i" by(simp add:add_ac)
   897       qed
   898     qed
   899   qed
   900 next
   901   assume Q: ?Q
   902   show ?P
   903   proof (cases)
   904     assume "k = 0"
   905     with Q show ?P by simp
   906   next
   907     assume not0: "k \<noteq> 0"
   908     with Q have R: ?R by simp
   909     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   910     show ?P by simp
   911   qed
   912 qed
   913 
   914 lemma split_div_lemma:
   915   assumes "0 < n"
   916   shows "n * q \<le> m \<and> m < n * Suc q \<longleftrightarrow> q = ((m\<Colon>nat) div n)" (is "?lhs \<longleftrightarrow> ?rhs")
   917 proof
   918   assume ?rhs
   919   with mult_div_cancel have nq: "n * q = m - (m mod n)" by simp
   920   then have A: "n * q \<le> m" by simp
   921   have "n - (m mod n) > 0" using mod_less_divisor assms by auto
   922   then have "m < m + (n - (m mod n))" by simp
   923   then have "m < n + (m - (m mod n))" by simp
   924   with nq have "m < n + n * q" by simp
   925   then have B: "m < n * Suc q" by simp
   926   from A B show ?lhs ..
   927 next
   928   assume P: ?lhs
   929   then have "divmod_nat_rel m n (q, m - n * q)"
   930     unfolding divmod_nat_rel_def by (auto simp add: mult_ac)
   931   with divmod_nat_rel_unique divmod_nat_rel [of m n]
   932   have "(q, m - n * q) = (m div n, m mod n)" by auto
   933   then show ?rhs by simp
   934 qed
   935 
   936 theorem split_div':
   937   "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
   938    (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
   939   apply (case_tac "0 < n")
   940   apply (simp only: add: split_div_lemma)
   941   apply simp_all
   942   done
   943 
   944 lemma split_mod:
   945  "P(n mod k :: nat) =
   946  ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
   947  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   948 proof
   949   assume P: ?P
   950   show ?Q
   951   proof (cases)
   952     assume "k = 0"
   953     with P show ?Q by simp
   954   next
   955     assume not0: "k \<noteq> 0"
   956     thus ?Q
   957     proof (simp, intro allI impI)
   958       fix i j
   959       assume "n = k*i + j" "j < k"
   960       thus "P j" using not0 P by(simp add:add_ac mult_ac)
   961     qed
   962   qed
   963 next
   964   assume Q: ?Q
   965   show ?P
   966   proof (cases)
   967     assume "k = 0"
   968     with Q show ?P by simp
   969   next
   970     assume not0: "k \<noteq> 0"
   971     with Q have R: ?R by simp
   972     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   973     show ?P by simp
   974   qed
   975 qed
   976 
   977 theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
   978   using mod_div_equality [of m n] by arith
   979 
   980 lemma div_mod_equality': "(m::nat) div n * n = m - m mod n"
   981   using mod_div_equality [of m n] by arith
   982 (* FIXME: very similar to mult_div_cancel *)
   983 
   984 
   985 subsubsection {* An ``induction'' law for modulus arithmetic. *}
   986 
   987 lemma mod_induct_0:
   988   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
   989   and base: "P i" and i: "i<p"
   990   shows "P 0"
   991 proof (rule ccontr)
   992   assume contra: "\<not>(P 0)"
   993   from i have p: "0<p" by simp
   994   have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
   995   proof
   996     fix k
   997     show "?A k"
   998     proof (induct k)
   999       show "?A 0" by simp  -- "by contradiction"
  1000     next
  1001       fix n
  1002       assume ih: "?A n"
  1003       show "?A (Suc n)"
  1004       proof (clarsimp)
  1005         assume y: "P (p - Suc n)"
  1006         have n: "Suc n < p"
  1007         proof (rule ccontr)
  1008           assume "\<not>(Suc n < p)"
  1009           hence "p - Suc n = 0"
  1010             by simp
  1011           with y contra show "False"
  1012             by simp
  1013         qed
  1014         hence n2: "Suc (p - Suc n) = p-n" by arith
  1015         from p have "p - Suc n < p" by arith
  1016         with y step have z: "P ((Suc (p - Suc n)) mod p)"
  1017           by blast
  1018         show "False"
  1019         proof (cases "n=0")
  1020           case True
  1021           with z n2 contra show ?thesis by simp
  1022         next
  1023           case False
  1024           with p have "p-n < p" by arith
  1025           with z n2 False ih show ?thesis by simp
  1026         qed
  1027       qed
  1028     qed
  1029   qed
  1030   moreover
  1031   from i obtain k where "0<k \<and> i+k=p"
  1032     by (blast dest: less_imp_add_positive)
  1033   hence "0<k \<and> i=p-k" by auto
  1034   moreover
  1035   note base
  1036   ultimately
  1037   show "False" by blast
  1038 qed
  1039 
  1040 lemma mod_induct:
  1041   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
  1042   and base: "P i" and i: "i<p" and j: "j<p"
  1043   shows "P j"
  1044 proof -
  1045   have "\<forall>j<p. P j"
  1046   proof
  1047     fix j
  1048     show "j<p \<longrightarrow> P j" (is "?A j")
  1049     proof (induct j)
  1050       from step base i show "?A 0"
  1051         by (auto elim: mod_induct_0)
  1052     next
  1053       fix k
  1054       assume ih: "?A k"
  1055       show "?A (Suc k)"
  1056       proof
  1057         assume suc: "Suc k < p"
  1058         hence k: "k<p" by simp
  1059         with ih have "P k" ..
  1060         with step k have "P (Suc k mod p)"
  1061           by blast
  1062         moreover
  1063         from suc have "Suc k mod p = Suc k"
  1064           by simp
  1065         ultimately
  1066         show "P (Suc k)" by simp
  1067       qed
  1068     qed
  1069   qed
  1070   with j show ?thesis by blast
  1071 qed
  1072 
  1073 lemma div2_Suc_Suc [simp]: "Suc (Suc m) div 2 = Suc (m div 2)"
  1074   by (simp add: numeral_2_eq_2 le_div_geq)
  1075 
  1076 lemma mod2_Suc_Suc [simp]: "Suc (Suc m) mod 2 = m mod 2"
  1077   by (simp add: numeral_2_eq_2 le_mod_geq)
  1078 
  1079 lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
  1080 by (simp add: nat_mult_2 [symmetric])
  1081 
  1082 lemma mod2_gr_0 [simp]: "0 < (m\<Colon>nat) mod 2 \<longleftrightarrow> m mod 2 = 1"
  1083 proof -
  1084   { fix n :: nat have  "(n::nat) < 2 \<Longrightarrow> n = 0 \<or> n = 1" by (cases n) simp_all }
  1085   moreover have "m mod 2 < 2" by simp
  1086   ultimately have "m mod 2 = 0 \<or> m mod 2 = 1" .
  1087   then show ?thesis by auto
  1088 qed
  1089 
  1090 text{*These lemmas collapse some needless occurrences of Suc:
  1091     at least three Sucs, since two and fewer are rewritten back to Suc again!
  1092     We already have some rules to simplify operands smaller than 3.*}
  1093 
  1094 lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
  1095 by (simp add: Suc3_eq_add_3)
  1096 
  1097 lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
  1098 by (simp add: Suc3_eq_add_3)
  1099 
  1100 lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
  1101 by (simp add: Suc3_eq_add_3)
  1102 
  1103 lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
  1104 by (simp add: Suc3_eq_add_3)
  1105 
  1106 lemmas Suc_div_eq_add3_div_numeral [simp] = Suc_div_eq_add3_div [of _ "numeral v"] for v
  1107 lemmas Suc_mod_eq_add3_mod_numeral [simp] = Suc_mod_eq_add3_mod [of _ "numeral v"] for v
  1108 
  1109 
  1110 lemma Suc_times_mod_eq: "1<k ==> Suc (k * m) mod k = 1" 
  1111 apply (induct "m")
  1112 apply (simp_all add: mod_Suc)
  1113 done
  1114 
  1115 declare Suc_times_mod_eq [of "numeral w", simp] for w
  1116 
  1117 lemma Suc_div_le_mono [simp]: "n div k \<le> (Suc n) div k"
  1118 by (simp add: div_le_mono)
  1119 
  1120 lemma Suc_n_div_2_gt_zero [simp]: "(0::nat) < n ==> 0 < (n + 1) div 2"
  1121 by (cases n) simp_all
  1122 
  1123 lemma div_2_gt_zero [simp]: assumes A: "(1::nat) < n" shows "0 < n div 2"
  1124 proof -
  1125   from A have B: "0 < n - 1" and C: "n - 1 + 1 = n" by simp_all
  1126   from Suc_n_div_2_gt_zero [OF B] C show ?thesis by simp 
  1127 qed
  1128 
  1129   (* Potential use of algebra : Equality modulo n*)
  1130 lemma mod_mult_self3 [simp]: "(k*n + m) mod n = m mod (n::nat)"
  1131 by (simp add: mult_ac add_ac)
  1132 
  1133 lemma mod_mult_self4 [simp]: "Suc (k*n + m) mod n = Suc m mod n"
  1134 proof -
  1135   have "Suc (k * n + m) mod n = (k * n + Suc m) mod n" by simp
  1136   also have "... = Suc m mod n" by (rule mod_mult_self3) 
  1137   finally show ?thesis .
  1138 qed
  1139 
  1140 lemma mod_Suc_eq_Suc_mod: "Suc m mod n = Suc (m mod n) mod n"
  1141 apply (subst mod_Suc [of m]) 
  1142 apply (subst mod_Suc [of "m mod n"], simp) 
  1143 done
  1144 
  1145 lemma mod_2_not_eq_zero_eq_one_nat:
  1146   fixes n :: nat
  1147   shows "n mod 2 \<noteq> 0 \<longleftrightarrow> n mod 2 = 1"
  1148   by simp
  1149 
  1150 
  1151 subsection {* Division on @{typ int} *}
  1152 
  1153 definition divmod_int_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" where
  1154     --{*definition of quotient and remainder*}
  1155   "divmod_int_rel a b = (\<lambda>(q, r). a = b * q + r \<and>
  1156     (if 0 < b then 0 \<le> r \<and> r < b else if b < 0 then b < r \<and> r \<le> 0 else q = 0))"
  1157 
  1158 definition adjust :: "int \<Rightarrow> int \<times> int \<Rightarrow> int \<times> int" where
  1159     --{*for the division algorithm*}
  1160     "adjust b = (\<lambda>(q, r). if 0 \<le> r - b then (2 * q + 1, r - b)
  1161                          else (2 * q, r))"
  1162 
  1163 text{*algorithm for the case @{text "a\<ge>0, b>0"}*}
  1164 function posDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  1165   "posDivAlg a b = (if a < b \<or>  b \<le> 0 then (0, a)
  1166      else adjust b (posDivAlg a (2 * b)))"
  1167 by auto
  1168 termination by (relation "measure (\<lambda>(a, b). nat (a - b + 1))")
  1169   (auto simp add: mult_2)
  1170 
  1171 text{*algorithm for the case @{text "a<0, b>0"}*}
  1172 function negDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  1173   "negDivAlg a b = (if 0 \<le>a + b \<or> b \<le> 0  then (-1, a + b)
  1174      else adjust b (negDivAlg a (2 * b)))"
  1175 by auto
  1176 termination by (relation "measure (\<lambda>(a, b). nat (- a - b))")
  1177   (auto simp add: mult_2)
  1178 
  1179 text{*algorithm for the general case @{term "b\<noteq>0"}*}
  1180 
  1181 definition divmod_int :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  1182     --{*The full division algorithm considers all possible signs for a, b
  1183        including the special case @{text "a=0, b<0"} because 
  1184        @{term negDivAlg} requires @{term "a<0"}.*}
  1185   "divmod_int a b = (if 0 \<le> a then if 0 \<le> b then posDivAlg a b
  1186                   else if a = 0 then (0, 0)
  1187                        else apsnd uminus (negDivAlg (-a) (-b))
  1188                else 
  1189                   if 0 < b then negDivAlg a b
  1190                   else apsnd uminus (posDivAlg (-a) (-b)))"
  1191 
  1192 instantiation int :: Divides.div
  1193 begin
  1194 
  1195 definition div_int where
  1196   "a div b = fst (divmod_int a b)"
  1197 
  1198 lemma fst_divmod_int [simp]:
  1199   "fst (divmod_int a b) = a div b"
  1200   by (simp add: div_int_def)
  1201 
  1202 definition mod_int where
  1203   "a mod b = snd (divmod_int a b)"
  1204 
  1205 lemma snd_divmod_int [simp]:
  1206   "snd (divmod_int a b) = a mod b"
  1207   by (simp add: mod_int_def)
  1208 
  1209 instance ..
  1210 
  1211 end
  1212 
  1213 lemma divmod_int_mod_div:
  1214   "divmod_int p q = (p div q, p mod q)"
  1215   by (simp add: prod_eq_iff)
  1216 
  1217 text{*
  1218 Here is the division algorithm in ML:
  1219 
  1220 \begin{verbatim}
  1221     fun posDivAlg (a,b) =
  1222       if a<b then (0,a)
  1223       else let val (q,r) = posDivAlg(a, 2*b)
  1224                in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
  1225            end
  1226 
  1227     fun negDivAlg (a,b) =
  1228       if 0\<le>a+b then (~1,a+b)
  1229       else let val (q,r) = negDivAlg(a, 2*b)
  1230                in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
  1231            end;
  1232 
  1233     fun negateSnd (q,r:int) = (q,~r);
  1234 
  1235     fun divmod (a,b) = if 0\<le>a then 
  1236                           if b>0 then posDivAlg (a,b) 
  1237                            else if a=0 then (0,0)
  1238                                 else negateSnd (negDivAlg (~a,~b))
  1239                        else 
  1240                           if 0<b then negDivAlg (a,b)
  1241                           else        negateSnd (posDivAlg (~a,~b));
  1242 \end{verbatim}
  1243 *}
  1244 
  1245 
  1246 subsubsection {* Uniqueness and Monotonicity of Quotients and Remainders *}
  1247 
  1248 lemma unique_quotient_lemma:
  1249      "[| b*q' + r'  \<le> b*q + r;  0 \<le> r';  r' < b;  r < b |]  
  1250       ==> q' \<le> (q::int)"
  1251 apply (subgoal_tac "r' + b * (q'-q) \<le> r")
  1252  prefer 2 apply (simp add: right_diff_distrib)
  1253 apply (subgoal_tac "0 < b * (1 + q - q') ")
  1254 apply (erule_tac [2] order_le_less_trans)
  1255  prefer 2 apply (simp add: right_diff_distrib right_distrib)
  1256 apply (subgoal_tac "b * q' < b * (1 + q) ")
  1257  prefer 2 apply (simp add: right_diff_distrib right_distrib)
  1258 apply (simp add: mult_less_cancel_left)
  1259 done
  1260 
  1261 lemma unique_quotient_lemma_neg:
  1262      "[| b*q' + r' \<le> b*q + r;  r \<le> 0;  b < r;  b < r' |]  
  1263       ==> q \<le> (q'::int)"
  1264 by (rule_tac b = "-b" and r = "-r'" and r' = "-r" in unique_quotient_lemma, 
  1265     auto)
  1266 
  1267 lemma unique_quotient:
  1268      "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r') |]  
  1269       ==> q = q'"
  1270 apply (simp add: divmod_int_rel_def linorder_neq_iff split: split_if_asm)
  1271 apply (blast intro: order_antisym
  1272              dest: order_eq_refl [THEN unique_quotient_lemma] 
  1273              order_eq_refl [THEN unique_quotient_lemma_neg] sym)+
  1274 done
  1275 
  1276 
  1277 lemma unique_remainder:
  1278      "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r') |]  
  1279       ==> r = r'"
  1280 apply (subgoal_tac "q = q'")
  1281  apply (simp add: divmod_int_rel_def)
  1282 apply (blast intro: unique_quotient)
  1283 done
  1284 
  1285 
  1286 subsubsection {* Correctness of @{term posDivAlg}, the Algorithm for Non-Negative Dividends *}
  1287 
  1288 text{*And positive divisors*}
  1289 
  1290 lemma adjust_eq [simp]:
  1291      "adjust b (q, r) = 
  1292       (let diff = r - b in  
  1293         if 0 \<le> diff then (2 * q + 1, diff)   
  1294                      else (2*q, r))"
  1295   by (simp add: Let_def adjust_def)
  1296 
  1297 declare posDivAlg.simps [simp del]
  1298 
  1299 text{*use with a simproc to avoid repeatedly proving the premise*}
  1300 lemma posDivAlg_eqn:
  1301      "0 < b ==>  
  1302       posDivAlg a b = (if a<b then (0,a) else adjust b (posDivAlg a (2*b)))"
  1303 by (rule posDivAlg.simps [THEN trans], simp)
  1304 
  1305 text{*Correctness of @{term posDivAlg}: it computes quotients correctly*}
  1306 theorem posDivAlg_correct:
  1307   assumes "0 \<le> a" and "0 < b"
  1308   shows "divmod_int_rel a b (posDivAlg a b)"
  1309   using assms
  1310   apply (induct a b rule: posDivAlg.induct)
  1311   apply auto
  1312   apply (simp add: divmod_int_rel_def)
  1313   apply (subst posDivAlg_eqn, simp add: right_distrib)
  1314   apply (case_tac "a < b")
  1315   apply simp_all
  1316   apply (erule splitE)
  1317   apply (auto simp add: right_distrib Let_def mult_ac mult_2_right)
  1318   done
  1319 
  1320 
  1321 subsubsection {* Correctness of @{term negDivAlg}, the Algorithm for Negative Dividends *}
  1322 
  1323 text{*And positive divisors*}
  1324 
  1325 declare negDivAlg.simps [simp del]
  1326 
  1327 text{*use with a simproc to avoid repeatedly proving the premise*}
  1328 lemma negDivAlg_eqn:
  1329      "0 < b ==>  
  1330       negDivAlg a b =       
  1331        (if 0\<le>a+b then (-1,a+b) else adjust b (negDivAlg a (2*b)))"
  1332 by (rule negDivAlg.simps [THEN trans], simp)
  1333 
  1334 (*Correctness of negDivAlg: it computes quotients correctly
  1335   It doesn't work if a=0 because the 0/b equals 0, not -1*)
  1336 lemma negDivAlg_correct:
  1337   assumes "a < 0" and "b > 0"
  1338   shows "divmod_int_rel a b (negDivAlg a b)"
  1339   using assms
  1340   apply (induct a b rule: negDivAlg.induct)
  1341   apply (auto simp add: linorder_not_le)
  1342   apply (simp add: divmod_int_rel_def)
  1343   apply (subst negDivAlg_eqn, assumption)
  1344   apply (case_tac "a + b < (0\<Colon>int)")
  1345   apply simp_all
  1346   apply (erule splitE)
  1347   apply (auto simp add: right_distrib Let_def mult_ac mult_2_right)
  1348   done
  1349 
  1350 
  1351 subsubsection {* Existence Shown by Proving the Division Algorithm to be Correct *}
  1352 
  1353 (*the case a=0*)
  1354 lemma divmod_int_rel_0: "divmod_int_rel 0 b (0, 0)"
  1355 by (auto simp add: divmod_int_rel_def linorder_neq_iff)
  1356 
  1357 lemma posDivAlg_0 [simp]: "posDivAlg 0 b = (0, 0)"
  1358 by (subst posDivAlg.simps, auto)
  1359 
  1360 lemma posDivAlg_0_right [simp]: "posDivAlg a 0 = (0, a)"
  1361 by (subst posDivAlg.simps, auto)
  1362 
  1363 lemma negDivAlg_minus1 [simp]: "negDivAlg -1 b = (-1, b - 1)"
  1364 by (subst negDivAlg.simps, auto)
  1365 
  1366 lemma divmod_int_rel_neg: "divmod_int_rel (-a) (-b) qr ==> divmod_int_rel a b (apsnd uminus qr)"
  1367 by (auto simp add: divmod_int_rel_def)
  1368 
  1369 lemma divmod_int_correct: "divmod_int_rel a b (divmod_int a b)"
  1370 apply (cases "b = 0", simp add: divmod_int_def divmod_int_rel_def)
  1371 by (force simp add: linorder_neq_iff divmod_int_rel_0 divmod_int_def divmod_int_rel_neg
  1372                     posDivAlg_correct negDivAlg_correct)
  1373 
  1374 lemma divmod_int_unique:
  1375   assumes "divmod_int_rel a b qr" 
  1376   shows "divmod_int a b = qr"
  1377   using assms divmod_int_correct [of a b]
  1378   using unique_quotient [of a b] unique_remainder [of a b]
  1379   by (metis pair_collapse)
  1380 
  1381 lemma divmod_int_rel_div_mod: "divmod_int_rel a b (a div b, a mod b)"
  1382   using divmod_int_correct by (simp add: divmod_int_mod_div)
  1383 
  1384 lemma div_int_unique: "divmod_int_rel a b (q, r) \<Longrightarrow> a div b = q"
  1385   by (simp add: divmod_int_rel_div_mod [THEN unique_quotient])
  1386 
  1387 lemma mod_int_unique: "divmod_int_rel a b (q, r) \<Longrightarrow> a mod b = r"
  1388   by (simp add: divmod_int_rel_div_mod [THEN unique_remainder])
  1389 
  1390 instance int :: ring_div
  1391 proof
  1392   fix a b :: int
  1393   show "a div b * b + a mod b = a"
  1394     using divmod_int_rel_div_mod [of a b]
  1395     unfolding divmod_int_rel_def by (simp add: mult_commute)
  1396 next
  1397   fix a b c :: int
  1398   assume "b \<noteq> 0"
  1399   hence "divmod_int_rel (a + c * b) b (c + a div b, a mod b)"
  1400     using divmod_int_rel_div_mod [of a b]
  1401     unfolding divmod_int_rel_def by (auto simp: algebra_simps)
  1402   thus "(a + c * b) div b = c + a div b"
  1403     by (rule div_int_unique)
  1404 next
  1405   fix a b c :: int
  1406   assume "c \<noteq> 0"
  1407   hence "\<And>q r. divmod_int_rel a b (q, r)
  1408     \<Longrightarrow> divmod_int_rel (c * a) (c * b) (q, c * r)"
  1409     unfolding divmod_int_rel_def
  1410     by - (rule linorder_cases [of 0 b], auto simp: algebra_simps
  1411       mult_less_0_iff zero_less_mult_iff mult_strict_right_mono
  1412       mult_strict_right_mono_neg zero_le_mult_iff mult_le_0_iff)
  1413   hence "divmod_int_rel (c * a) (c * b) (a div b, c * (a mod b))"
  1414     using divmod_int_rel_div_mod [of a b] .
  1415   thus "(c * a) div (c * b) = a div b"
  1416     by (rule div_int_unique)
  1417 next
  1418   fix a :: int show "a div 0 = 0"
  1419     by (rule div_int_unique, simp add: divmod_int_rel_def)
  1420 next
  1421   fix a :: int show "0 div a = 0"
  1422     by (rule div_int_unique, auto simp add: divmod_int_rel_def)
  1423 qed
  1424 
  1425 text{*Basic laws about division and remainder*}
  1426 
  1427 lemma zmod_zdiv_equality: "(a::int) = b * (a div b) + (a mod b)"
  1428   by (fact mod_div_equality2 [symmetric])
  1429 
  1430 lemma zdiv_zmod_equality: "(b * (a div b) + (a mod b)) + k = (a::int)+k"
  1431   by (fact div_mod_equality2)
  1432 
  1433 lemma zdiv_zmod_equality2: "((a div b) * b + (a mod b)) + k = (a::int)+k"
  1434   by (fact div_mod_equality)
  1435 
  1436 text {* Tool setup *}
  1437 
  1438 (* FIXME: Theorem list add_0s doesn't exist, because Numeral0 has gone. *)
  1439 lemmas add_0s = add_0_left add_0_right
  1440 
  1441 ML {*
  1442 structure Cancel_Div_Mod_Int = Cancel_Div_Mod
  1443 (
  1444   val div_name = @{const_name div};
  1445   val mod_name = @{const_name mod};
  1446   val mk_binop = HOLogic.mk_binop;
  1447   val mk_sum = Arith_Data.mk_sum HOLogic.intT;
  1448   val dest_sum = Arith_Data.dest_sum;
  1449 
  1450   val div_mod_eqs = map mk_meta_eq [@{thm zdiv_zmod_equality}, @{thm zdiv_zmod_equality2}];
  1451 
  1452   val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac 
  1453     (@{thm diff_minus} :: @{thms add_0s} @ @{thms add_ac}))
  1454 )
  1455 *}
  1456 
  1457 simproc_setup cancel_div_mod_int ("(k::int) + l") = {* K Cancel_Div_Mod_Int.proc *}
  1458 
  1459 lemma pos_mod_conj: "(0::int) < b \<Longrightarrow> 0 \<le> a mod b \<and> a mod b < b"
  1460   using divmod_int_correct [of a b]
  1461   by (auto simp add: divmod_int_rel_def prod_eq_iff)
  1462 
  1463 lemmas pos_mod_sign [simp] = pos_mod_conj [THEN conjunct1]
  1464    and pos_mod_bound [simp] = pos_mod_conj [THEN conjunct2]
  1465 
  1466 lemma neg_mod_conj: "b < (0::int) \<Longrightarrow> a mod b \<le> 0 \<and> b < a mod b"
  1467   using divmod_int_correct [of a b]
  1468   by (auto simp add: divmod_int_rel_def prod_eq_iff)
  1469 
  1470 lemmas neg_mod_sign [simp] = neg_mod_conj [THEN conjunct1]
  1471    and neg_mod_bound [simp] = neg_mod_conj [THEN conjunct2]
  1472 
  1473 
  1474 subsubsection {* General Properties of div and mod *}
  1475 
  1476 lemma div_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a div b = 0"
  1477 apply (rule div_int_unique)
  1478 apply (auto simp add: divmod_int_rel_def)
  1479 done
  1480 
  1481 lemma div_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a div b = 0"
  1482 apply (rule div_int_unique)
  1483 apply (auto simp add: divmod_int_rel_def)
  1484 done
  1485 
  1486 lemma div_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a div b = -1"
  1487 apply (rule div_int_unique)
  1488 apply (auto simp add: divmod_int_rel_def)
  1489 done
  1490 
  1491 (*There is no div_neg_pos_trivial because  0 div b = 0 would supersede it*)
  1492 
  1493 lemma mod_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a mod b = a"
  1494 apply (rule_tac q = 0 in mod_int_unique)
  1495 apply (auto simp add: divmod_int_rel_def)
  1496 done
  1497 
  1498 lemma mod_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a mod b = a"
  1499 apply (rule_tac q = 0 in mod_int_unique)
  1500 apply (auto simp add: divmod_int_rel_def)
  1501 done
  1502 
  1503 lemma mod_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a mod b = a+b"
  1504 apply (rule_tac q = "-1" in mod_int_unique)
  1505 apply (auto simp add: divmod_int_rel_def)
  1506 done
  1507 
  1508 text{*There is no @{text mod_neg_pos_trivial}.*}
  1509 
  1510 
  1511 subsubsection {* Laws for div and mod with Unary Minus *}
  1512 
  1513 lemma zminus1_lemma:
  1514      "divmod_int_rel a b (q, r) ==> b \<noteq> 0
  1515       ==> divmod_int_rel (-a) b (if r=0 then -q else -q - 1,  
  1516                           if r=0 then 0 else b-r)"
  1517 by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_diff_distrib)
  1518 
  1519 
  1520 lemma zdiv_zminus1_eq_if:
  1521      "b \<noteq> (0::int)  
  1522       ==> (-a) div b =  
  1523           (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
  1524 by (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN div_int_unique])
  1525 
  1526 lemma zmod_zminus1_eq_if:
  1527      "(-a::int) mod b = (if a mod b = 0 then 0 else  b - (a mod b))"
  1528 apply (case_tac "b = 0", simp)
  1529 apply (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN mod_int_unique])
  1530 done
  1531 
  1532 lemma zmod_zminus1_not_zero:
  1533   fixes k l :: int
  1534   shows "- k mod l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
  1535   unfolding zmod_zminus1_eq_if by auto
  1536 
  1537 lemma zdiv_zminus2_eq_if:
  1538      "b \<noteq> (0::int)  
  1539       ==> a div (-b) =  
  1540           (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
  1541 by (simp add: zdiv_zminus1_eq_if div_minus_right)
  1542 
  1543 lemma zmod_zminus2_eq_if:
  1544      "a mod (-b::int) = (if a mod b = 0 then 0 else  (a mod b) - b)"
  1545 by (simp add: zmod_zminus1_eq_if mod_minus_right)
  1546 
  1547 lemma zmod_zminus2_not_zero:
  1548   fixes k l :: int
  1549   shows "k mod - l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
  1550   unfolding zmod_zminus2_eq_if by auto 
  1551 
  1552 
  1553 subsubsection {* Computation of Division and Remainder *}
  1554 
  1555 lemma div_eq_minus1: "(0::int) < b ==> -1 div b = -1"
  1556 by (simp add: div_int_def divmod_int_def)
  1557 
  1558 lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1"
  1559 by (simp add: mod_int_def divmod_int_def)
  1560 
  1561 text{*a positive, b positive *}
  1562 
  1563 lemma div_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a div b = fst (posDivAlg a b)"
  1564 by (simp add: div_int_def divmod_int_def)
  1565 
  1566 lemma mod_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a mod b = snd (posDivAlg a b)"
  1567 by (simp add: mod_int_def divmod_int_def)
  1568 
  1569 text{*a negative, b positive *}
  1570 
  1571 lemma div_neg_pos: "[| a < 0;  0 < b |] ==> a div b = fst (negDivAlg a b)"
  1572 by (simp add: div_int_def divmod_int_def)
  1573 
  1574 lemma mod_neg_pos: "[| a < 0;  0 < b |] ==> a mod b = snd (negDivAlg a b)"
  1575 by (simp add: mod_int_def divmod_int_def)
  1576 
  1577 text{*a positive, b negative *}
  1578 
  1579 lemma div_pos_neg:
  1580      "[| 0 < a;  b < 0 |] ==> a div b = fst (apsnd uminus (negDivAlg (-a) (-b)))"
  1581 by (simp add: div_int_def divmod_int_def)
  1582 
  1583 lemma mod_pos_neg:
  1584      "[| 0 < a;  b < 0 |] ==> a mod b = snd (apsnd uminus (negDivAlg (-a) (-b)))"
  1585 by (simp add: mod_int_def divmod_int_def)
  1586 
  1587 text{*a negative, b negative *}
  1588 
  1589 lemma div_neg_neg:
  1590      "[| a < 0;  b \<le> 0 |] ==> a div b = fst (apsnd uminus (posDivAlg (-a) (-b)))"
  1591 by (simp add: div_int_def divmod_int_def)
  1592 
  1593 lemma mod_neg_neg:
  1594      "[| a < 0;  b \<le> 0 |] ==> a mod b = snd (apsnd uminus (posDivAlg (-a) (-b)))"
  1595 by (simp add: mod_int_def divmod_int_def)
  1596 
  1597 text {*Simplify expresions in which div and mod combine numerical constants*}
  1598 
  1599 lemma int_div_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a div b = q"
  1600   by (rule div_int_unique [of a b q r]) (simp add: divmod_int_rel_def)
  1601 
  1602 lemma int_div_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a div b = q"
  1603   by (rule div_int_unique [of a b q r],
  1604     simp add: divmod_int_rel_def)
  1605 
  1606 lemma int_mod_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a mod b = r"
  1607   by (rule mod_int_unique [of a b q r],
  1608     simp add: divmod_int_rel_def)
  1609 
  1610 lemma int_mod_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a mod b = r"
  1611   by (rule mod_int_unique [of a b q r],
  1612     simp add: divmod_int_rel_def)
  1613 
  1614 (* simprocs adapted from HOL/ex/Binary.thy *)
  1615 ML {*
  1616 local
  1617   val mk_number = HOLogic.mk_number HOLogic.intT
  1618   val plus = @{term "plus :: int \<Rightarrow> int \<Rightarrow> int"}
  1619   val times = @{term "times :: int \<Rightarrow> int \<Rightarrow> int"}
  1620   val zero = @{term "0 :: int"}
  1621   val less = @{term "op < :: int \<Rightarrow> int \<Rightarrow> bool"}
  1622   val le = @{term "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"}
  1623   val simps = @{thms arith_simps} @ @{thms rel_simps} @
  1624     map (fn th => th RS sym) [@{thm numeral_1_eq_1}]
  1625   fun prove ctxt goal = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop goal)
  1626     (K (ALLGOALS (full_simp_tac (HOL_basic_ss addsimps simps))));
  1627   fun binary_proc proc ss ct =
  1628     (case Thm.term_of ct of
  1629       _ $ t $ u =>
  1630       (case try (pairself (`(snd o HOLogic.dest_number))) (t, u) of
  1631         SOME args => proc (Simplifier.the_context ss) args
  1632       | NONE => NONE)
  1633     | _ => NONE);
  1634 in
  1635   fun divmod_proc posrule negrule =
  1636     binary_proc (fn ctxt => fn ((a, t), (b, u)) =>
  1637       if b = 0 then NONE else let
  1638         val (q, r) = pairself mk_number (Integer.div_mod a b)
  1639         val goal1 = HOLogic.mk_eq (t, plus $ (times $ u $ q) $ r)
  1640         val (goal2, goal3, rule) = if b > 0
  1641           then (le $ zero $ r, less $ r $ u, posrule RS eq_reflection)
  1642           else (le $ r $ zero, less $ u $ r, negrule RS eq_reflection)
  1643       in SOME (rule OF map (prove ctxt) [goal1, goal2, goal3]) end)
  1644 end
  1645 *}
  1646 
  1647 simproc_setup binary_int_div
  1648   ("numeral m div numeral n :: int" |
  1649    "numeral m div neg_numeral n :: int" |
  1650    "neg_numeral m div numeral n :: int" |
  1651    "neg_numeral m div neg_numeral n :: int") =
  1652   {* K (divmod_proc @{thm int_div_pos_eq} @{thm int_div_neg_eq}) *}
  1653 
  1654 simproc_setup binary_int_mod
  1655   ("numeral m mod numeral n :: int" |
  1656    "numeral m mod neg_numeral n :: int" |
  1657    "neg_numeral m mod numeral n :: int" |
  1658    "neg_numeral m mod neg_numeral n :: int") =
  1659   {* K (divmod_proc @{thm int_mod_pos_eq} @{thm int_mod_neg_eq}) *}
  1660 
  1661 lemmas posDivAlg_eqn_numeral [simp] =
  1662     posDivAlg_eqn [of "numeral v" "numeral w", OF zero_less_numeral] for v w
  1663 
  1664 lemmas negDivAlg_eqn_numeral [simp] =
  1665     negDivAlg_eqn [of "numeral v" "neg_numeral w", OF zero_less_numeral] for v w
  1666 
  1667 
  1668 text{*Special-case simplification *}
  1669 
  1670 (** The last remaining special cases for constant arithmetic:
  1671     1 div z and 1 mod z **)
  1672 
  1673 lemmas div_pos_pos_1_numeral [simp] =
  1674   div_pos_pos [OF zero_less_one, of "numeral w", OF zero_le_numeral] for w
  1675 
  1676 lemmas div_pos_neg_1_numeral [simp] =
  1677   div_pos_neg [OF zero_less_one, of "neg_numeral w",
  1678   OF neg_numeral_less_zero] for w
  1679 
  1680 lemmas mod_pos_pos_1_numeral [simp] =
  1681   mod_pos_pos [OF zero_less_one, of "numeral w", OF zero_le_numeral] for w
  1682 
  1683 lemmas mod_pos_neg_1_numeral [simp] =
  1684   mod_pos_neg [OF zero_less_one, of "neg_numeral w",
  1685   OF neg_numeral_less_zero] for w
  1686 
  1687 lemmas posDivAlg_eqn_1_numeral [simp] =
  1688     posDivAlg_eqn [of concl: 1 "numeral w", OF zero_less_numeral] for w
  1689 
  1690 lemmas negDivAlg_eqn_1_numeral [simp] =
  1691     negDivAlg_eqn [of concl: 1 "numeral w", OF zero_less_numeral] for w
  1692 
  1693 
  1694 subsubsection {* Monotonicity in the First Argument (Dividend) *}
  1695 
  1696 lemma zdiv_mono1: "[| a \<le> a';  0 < (b::int) |] ==> a div b \<le> a' div b"
  1697 apply (cut_tac a = a and b = b in zmod_zdiv_equality)
  1698 apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
  1699 apply (rule unique_quotient_lemma)
  1700 apply (erule subst)
  1701 apply (erule subst, simp_all)
  1702 done
  1703 
  1704 lemma zdiv_mono1_neg: "[| a \<le> a';  (b::int) < 0 |] ==> a' div b \<le> a div b"
  1705 apply (cut_tac a = a and b = b in zmod_zdiv_equality)
  1706 apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
  1707 apply (rule unique_quotient_lemma_neg)
  1708 apply (erule subst)
  1709 apply (erule subst, simp_all)
  1710 done
  1711 
  1712 
  1713 subsubsection {* Monotonicity in the Second Argument (Divisor) *}
  1714 
  1715 lemma q_pos_lemma:
  1716      "[| 0 \<le> b'*q' + r'; r' < b';  0 < b' |] ==> 0 \<le> (q'::int)"
  1717 apply (subgoal_tac "0 < b'* (q' + 1) ")
  1718  apply (simp add: zero_less_mult_iff)
  1719 apply (simp add: right_distrib)
  1720 done
  1721 
  1722 lemma zdiv_mono2_lemma:
  1723      "[| b*q + r = b'*q' + r';  0 \<le> b'*q' + r';   
  1724          r' < b';  0 \<le> r;  0 < b';  b' \<le> b |]   
  1725       ==> q \<le> (q'::int)"
  1726 apply (frule q_pos_lemma, assumption+) 
  1727 apply (subgoal_tac "b*q < b* (q' + 1) ")
  1728  apply (simp add: mult_less_cancel_left)
  1729 apply (subgoal_tac "b*q = r' - r + b'*q'")
  1730  prefer 2 apply simp
  1731 apply (simp (no_asm_simp) add: right_distrib)
  1732 apply (subst add_commute, rule add_less_le_mono, arith)
  1733 apply (rule mult_right_mono, auto)
  1734 done
  1735 
  1736 lemma zdiv_mono2:
  1737      "[| (0::int) \<le> a;  0 < b';  b' \<le> b |] ==> a div b \<le> a div b'"
  1738 apply (subgoal_tac "b \<noteq> 0")
  1739  prefer 2 apply arith
  1740 apply (cut_tac a = a and b = b in zmod_zdiv_equality)
  1741 apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
  1742 apply (rule zdiv_mono2_lemma)
  1743 apply (erule subst)
  1744 apply (erule subst, simp_all)
  1745 done
  1746 
  1747 lemma q_neg_lemma:
  1748      "[| b'*q' + r' < 0;  0 \<le> r';  0 < b' |] ==> q' \<le> (0::int)"
  1749 apply (subgoal_tac "b'*q' < 0")
  1750  apply (simp add: mult_less_0_iff, arith)
  1751 done
  1752 
  1753 lemma zdiv_mono2_neg_lemma:
  1754      "[| b*q + r = b'*q' + r';  b'*q' + r' < 0;   
  1755          r < b;  0 \<le> r';  0 < b';  b' \<le> b |]   
  1756       ==> q' \<le> (q::int)"
  1757 apply (frule q_neg_lemma, assumption+) 
  1758 apply (subgoal_tac "b*q' < b* (q + 1) ")
  1759  apply (simp add: mult_less_cancel_left)
  1760 apply (simp add: right_distrib)
  1761 apply (subgoal_tac "b*q' \<le> b'*q'")
  1762  prefer 2 apply (simp add: mult_right_mono_neg, arith)
  1763 done
  1764 
  1765 lemma zdiv_mono2_neg:
  1766      "[| a < (0::int);  0 < b';  b' \<le> b |] ==> a div b' \<le> a div b"
  1767 apply (cut_tac a = a and b = b in zmod_zdiv_equality)
  1768 apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
  1769 apply (rule zdiv_mono2_neg_lemma)
  1770 apply (erule subst)
  1771 apply (erule subst, simp_all)
  1772 done
  1773 
  1774 
  1775 subsubsection {* More Algebraic Laws for div and mod *}
  1776 
  1777 text{*proving (a*b) div c = a * (b div c) + a * (b mod c) *}
  1778 
  1779 lemma zmult1_lemma:
  1780      "[| divmod_int_rel b c (q, r) |]  
  1781       ==> divmod_int_rel (a * b) c (a*q + a*r div c, a*r mod c)"
  1782 by (auto simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_distrib mult_ac)
  1783 
  1784 lemma zdiv_zmult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)"
  1785 apply (case_tac "c = 0", simp)
  1786 apply (blast intro: divmod_int_rel_div_mod [THEN zmult1_lemma, THEN div_int_unique])
  1787 done
  1788 
  1789 lemma zmod_zmult1_eq: "(a*b) mod c = a*(b mod c) mod (c::int)"
  1790   by (fact mod_mult_right_eq) (* FIXME: delete *)
  1791 
  1792 text{*proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) *}
  1793 
  1794 lemma zadd1_lemma:
  1795      "[| divmod_int_rel a c (aq, ar);  divmod_int_rel b c (bq, br) |]  
  1796       ==> divmod_int_rel (a+b) c (aq + bq + (ar+br) div c, (ar+br) mod c)"
  1797 by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_distrib)
  1798 
  1799 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
  1800 lemma zdiv_zadd1_eq:
  1801      "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)"
  1802 apply (case_tac "c = 0", simp)
  1803 apply (blast intro: zadd1_lemma [OF divmod_int_rel_div_mod divmod_int_rel_div_mod] div_int_unique)
  1804 done
  1805 
  1806 lemma posDivAlg_div_mod:
  1807   assumes "k \<ge> 0"
  1808   and "l \<ge> 0"
  1809   shows "posDivAlg k l = (k div l, k mod l)"
  1810 proof (cases "l = 0")
  1811   case True then show ?thesis by (simp add: posDivAlg.simps)
  1812 next
  1813   case False with assms posDivAlg_correct
  1814     have "divmod_int_rel k l (fst (posDivAlg k l), snd (posDivAlg k l))"
  1815     by simp
  1816   from div_int_unique [OF this] mod_int_unique [OF this]
  1817   show ?thesis by simp
  1818 qed
  1819 
  1820 lemma negDivAlg_div_mod:
  1821   assumes "k < 0"
  1822   and "l > 0"
  1823   shows "negDivAlg k l = (k div l, k mod l)"
  1824 proof -
  1825   from assms have "l \<noteq> 0" by simp
  1826   from assms negDivAlg_correct
  1827     have "divmod_int_rel k l (fst (negDivAlg k l), snd (negDivAlg k l))"
  1828     by simp
  1829   from div_int_unique [OF this] mod_int_unique [OF this]
  1830   show ?thesis by simp
  1831 qed
  1832 
  1833 lemma zmod_eq_0_iff: "(m mod d = 0) = (EX q::int. m = d*q)"
  1834 by (simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
  1835 
  1836 (* REVISIT: should this be generalized to all semiring_div types? *)
  1837 lemmas zmod_eq_0D [dest!] = zmod_eq_0_iff [THEN iffD1]
  1838 
  1839 lemma zmod_zdiv_equality':
  1840   "(m\<Colon>int) mod n = m - (m div n) * n"
  1841   using mod_div_equality [of m n] by arith
  1842 
  1843 
  1844 subsubsection {* Proving  @{term "a div (b*c) = (a div b) div c"} *}
  1845 
  1846 (*The condition c>0 seems necessary.  Consider that 7 div ~6 = ~2 but
  1847   7 div 2 div ~3 = 3 div ~3 = ~1.  The subcase (a div b) mod c = 0 seems
  1848   to cause particular problems.*)
  1849 
  1850 text{*first, four lemmas to bound the remainder for the cases b<0 and b>0 *}
  1851 
  1852 lemma zmult2_lemma_aux1: "[| (0::int) < c;  b < r;  r \<le> 0 |] ==> b*c < b*(q mod c) + r"
  1853 apply (subgoal_tac "b * (c - q mod c) < r * 1")
  1854  apply (simp add: algebra_simps)
  1855 apply (rule order_le_less_trans)
  1856  apply (erule_tac [2] mult_strict_right_mono)
  1857  apply (rule mult_left_mono_neg)
  1858   using add1_zle_eq[of "q mod c"]apply(simp add: algebra_simps)
  1859  apply (simp)
  1860 apply (simp)
  1861 done
  1862 
  1863 lemma zmult2_lemma_aux2:
  1864      "[| (0::int) < c;   b < r;  r \<le> 0 |] ==> b * (q mod c) + r \<le> 0"
  1865 apply (subgoal_tac "b * (q mod c) \<le> 0")
  1866  apply arith
  1867 apply (simp add: mult_le_0_iff)
  1868 done
  1869 
  1870 lemma zmult2_lemma_aux3: "[| (0::int) < c;  0 \<le> r;  r < b |] ==> 0 \<le> b * (q mod c) + r"
  1871 apply (subgoal_tac "0 \<le> b * (q mod c) ")
  1872 apply arith
  1873 apply (simp add: zero_le_mult_iff)
  1874 done
  1875 
  1876 lemma zmult2_lemma_aux4: "[| (0::int) < c; 0 \<le> r; r < b |] ==> b * (q mod c) + r < b * c"
  1877 apply (subgoal_tac "r * 1 < b * (c - q mod c) ")
  1878  apply (simp add: right_diff_distrib)
  1879 apply (rule order_less_le_trans)
  1880  apply (erule mult_strict_right_mono)
  1881  apply (rule_tac [2] mult_left_mono)
  1882   apply simp
  1883  using add1_zle_eq[of "q mod c"] apply (simp add: algebra_simps)
  1884 apply simp
  1885 done
  1886 
  1887 lemma zmult2_lemma: "[| divmod_int_rel a b (q, r); 0 < c |]  
  1888       ==> divmod_int_rel a (b * c) (q div c, b*(q mod c) + r)"
  1889 by (auto simp add: mult_ac divmod_int_rel_def linorder_neq_iff
  1890                    zero_less_mult_iff right_distrib [symmetric] 
  1891                    zmult2_lemma_aux1 zmult2_lemma_aux2 zmult2_lemma_aux3 zmult2_lemma_aux4 mult_less_0_iff split: split_if_asm)
  1892 
  1893 lemma zdiv_zmult2_eq: "(0::int) < c ==> a div (b*c) = (a div b) div c"
  1894 apply (case_tac "b = 0", simp)
  1895 apply (force simp add: divmod_int_rel_div_mod [THEN zmult2_lemma, THEN div_int_unique])
  1896 done
  1897 
  1898 lemma zmod_zmult2_eq:
  1899      "(0::int) < c ==> a mod (b*c) = b*(a div b mod c) + a mod b"
  1900 apply (case_tac "b = 0", simp)
  1901 apply (force simp add: divmod_int_rel_div_mod [THEN zmult2_lemma, THEN mod_int_unique])
  1902 done
  1903 
  1904 lemma div_pos_geq:
  1905   fixes k l :: int
  1906   assumes "0 < l" and "l \<le> k"
  1907   shows "k div l = (k - l) div l + 1"
  1908 proof -
  1909   have "k = (k - l) + l" by simp
  1910   then obtain j where k: "k = j + l" ..
  1911   with assms show ?thesis by simp
  1912 qed
  1913 
  1914 lemma mod_pos_geq:
  1915   fixes k l :: int
  1916   assumes "0 < l" and "l \<le> k"
  1917   shows "k mod l = (k - l) mod l"
  1918 proof -
  1919   have "k = (k - l) + l" by simp
  1920   then obtain j where k: "k = j + l" ..
  1921   with assms show ?thesis by simp
  1922 qed
  1923 
  1924 
  1925 subsubsection {* Splitting Rules for div and mod *}
  1926 
  1927 text{*The proofs of the two lemmas below are essentially identical*}
  1928 
  1929 lemma split_pos_lemma:
  1930  "0<k ==> 
  1931     P(n div k :: int)(n mod k) = (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i j)"
  1932 apply (rule iffI, clarify)
  1933  apply (erule_tac P="P ?x ?y" in rev_mp)  
  1934  apply (subst mod_add_eq) 
  1935  apply (subst zdiv_zadd1_eq) 
  1936  apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial)  
  1937 txt{*converse direction*}
  1938 apply (drule_tac x = "n div k" in spec) 
  1939 apply (drule_tac x = "n mod k" in spec, simp)
  1940 done
  1941 
  1942 lemma split_neg_lemma:
  1943  "k<0 ==>
  1944     P(n div k :: int)(n mod k) = (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i j)"
  1945 apply (rule iffI, clarify)
  1946  apply (erule_tac P="P ?x ?y" in rev_mp)  
  1947  apply (subst mod_add_eq) 
  1948  apply (subst zdiv_zadd1_eq) 
  1949  apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial)  
  1950 txt{*converse direction*}
  1951 apply (drule_tac x = "n div k" in spec) 
  1952 apply (drule_tac x = "n mod k" in spec, simp)
  1953 done
  1954 
  1955 lemma split_zdiv:
  1956  "P(n div k :: int) =
  1957   ((k = 0 --> P 0) & 
  1958    (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i)) & 
  1959    (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i)))"
  1960 apply (case_tac "k=0", simp)
  1961 apply (simp only: linorder_neq_iff)
  1962 apply (erule disjE) 
  1963  apply (simp_all add: split_pos_lemma [of concl: "%x y. P x"] 
  1964                       split_neg_lemma [of concl: "%x y. P x"])
  1965 done
  1966 
  1967 lemma split_zmod:
  1968  "P(n mod k :: int) =
  1969   ((k = 0 --> P n) & 
  1970    (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P j)) & 
  1971    (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P j)))"
  1972 apply (case_tac "k=0", simp)
  1973 apply (simp only: linorder_neq_iff)
  1974 apply (erule disjE) 
  1975  apply (simp_all add: split_pos_lemma [of concl: "%x y. P y"] 
  1976                       split_neg_lemma [of concl: "%x y. P y"])
  1977 done
  1978 
  1979 text {* Enable (lin)arith to deal with @{const div} and @{const mod}
  1980   when these are applied to some constant that is of the form
  1981   @{term "numeral k"}: *}
  1982 declare split_zdiv [of _ _ "numeral k", arith_split] for k
  1983 declare split_zmod [of _ _ "numeral k", arith_split] for k
  1984 
  1985 
  1986 subsubsection {* Speeding up the Division Algorithm with Shifting *}
  1987 
  1988 text{*computing div by shifting *}
  1989 
  1990 lemma pos_zdiv_mult_2: "(0::int) \<le> a ==> (1 + 2*b) div (2*a) = b div a"
  1991 proof cases
  1992   assume "a=0"
  1993     thus ?thesis by simp
  1994 next
  1995   assume "a\<noteq>0" and le_a: "0\<le>a"   
  1996   hence a_pos: "1 \<le> a" by arith
  1997   hence one_less_a2: "1 < 2 * a" by arith
  1998   hence le_2a: "2 * (1 + b mod a) \<le> 2 * a"
  1999     unfolding mult_le_cancel_left
  2000     by (simp add: add1_zle_eq add_commute [of 1])
  2001   with a_pos have "0 \<le> b mod a" by simp
  2002   hence le_addm: "0 \<le> 1 mod (2*a) + 2*(b mod a)"
  2003     by (simp add: mod_pos_pos_trivial one_less_a2)
  2004   with  le_2a
  2005   have "(1 mod (2*a) + 2*(b mod a)) div (2*a) = 0"
  2006     by (simp add: div_pos_pos_trivial le_addm mod_pos_pos_trivial one_less_a2
  2007                   right_distrib) 
  2008   thus ?thesis
  2009     by (subst zdiv_zadd1_eq,
  2010         simp add: mod_mult_mult1 one_less_a2
  2011                   div_pos_pos_trivial)
  2012 qed
  2013 
  2014 lemma neg_zdiv_mult_2: 
  2015   assumes A: "a \<le> (0::int)" shows "(1 + 2*b) div (2*a) = (b+1) div a"
  2016 proof -
  2017   have R: "1 + - (2 * (b + 1)) = - (1 + 2 * b)" by simp
  2018   have "(1 + 2 * (-b - 1)) div (2 * (-a)) = (-b - 1) div (-a)"
  2019     by (rule pos_zdiv_mult_2, simp add: A)
  2020   thus ?thesis
  2021     by (simp only: R div_minus_minus diff_minus
  2022       minus_add_distrib [symmetric] mult_minus_right)
  2023 qed
  2024 
  2025 (* FIXME: add rules for negative numerals *)
  2026 lemma zdiv_numeral_Bit0 [simp]:
  2027   "numeral (Num.Bit0 v) div numeral (Num.Bit0 w) =
  2028     numeral v div (numeral w :: int)"
  2029   unfolding numeral.simps unfolding mult_2 [symmetric]
  2030   by (rule div_mult_mult1, simp)
  2031 
  2032 lemma zdiv_numeral_Bit1 [simp]:
  2033   "numeral (Num.Bit1 v) div numeral (Num.Bit0 w) =  
  2034     (numeral v div (numeral w :: int))"
  2035   unfolding numeral.simps
  2036   unfolding mult_2 [symmetric] add_commute [of _ 1]
  2037   by (rule pos_zdiv_mult_2, simp)
  2038 
  2039 
  2040 subsubsection {* Computing mod by Shifting (proofs resemble those for div) *}
  2041 
  2042 lemma pos_zmod_mult_2:
  2043   fixes a b :: int
  2044   assumes "0 \<le> a"
  2045   shows "(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)"
  2046 proof (cases "0 < a")
  2047   case False with assms show ?thesis by simp
  2048 next
  2049   case True
  2050   then have "b mod a < a" by (rule pos_mod_bound)
  2051   then have "1 + b mod a \<le> a" by simp
  2052   then have A: "2 * (1 + b mod a) \<le> 2 * a" by simp
  2053   from `0 < a` have "0 \<le> b mod a" by (rule pos_mod_sign)
  2054   then have B: "0 \<le> 1 + 2 * (b mod a)" by simp
  2055   have "((1\<Colon>int) mod ((2\<Colon>int) * a) + (2\<Colon>int) * b mod ((2\<Colon>int) * a)) mod ((2\<Colon>int) * a) = (1\<Colon>int) + (2\<Colon>int) * (b mod a)"
  2056     using `0 < a` and A
  2057     by (auto simp add: mod_mult_mult1 mod_pos_pos_trivial ring_distribs intro!: mod_pos_pos_trivial B)
  2058   then show ?thesis by (subst mod_add_eq)
  2059 qed
  2060 
  2061 lemma neg_zmod_mult_2:
  2062   fixes a b :: int
  2063   assumes "a \<le> 0"
  2064   shows "(1 + 2 * b) mod (2 * a) = 2 * ((b + 1) mod a) - 1"
  2065 proof -
  2066   from assms have "0 \<le> - a" by auto
  2067   then have "(1 + 2 * (- b - 1)) mod (2 * (- a)) = 1 + 2 * ((- b - 1) mod (- a))"
  2068     by (rule pos_zmod_mult_2)
  2069   then show ?thesis by (simp add: mod_minus_right algebra_simps)
  2070      (simp add: diff_minus add_ac)
  2071 qed
  2072 
  2073 (* FIXME: add rules for negative numerals *)
  2074 lemma zmod_numeral_Bit0 [simp]:
  2075   "numeral (Num.Bit0 v) mod numeral (Num.Bit0 w) =  
  2076     (2::int) * (numeral v mod numeral w)"
  2077   unfolding numeral_Bit0 [of v] numeral_Bit0 [of w]
  2078   unfolding mult_2 [symmetric] by (rule mod_mult_mult1)
  2079 
  2080 lemma zmod_numeral_Bit1 [simp]:
  2081   "numeral (Num.Bit1 v) mod numeral (Num.Bit0 w) =
  2082     2 * (numeral v mod numeral w) + (1::int)"
  2083   unfolding numeral_Bit1 [of v] numeral_Bit0 [of w]
  2084   unfolding mult_2 [symmetric] add_commute [of _ 1]
  2085   by (rule pos_zmod_mult_2, simp)
  2086 
  2087 lemma zdiv_eq_0_iff:
  2088  "(i::int) div k = 0 \<longleftrightarrow> k=0 \<or> 0\<le>i \<and> i<k \<or> i\<le>0 \<and> k<i" (is "?L = ?R")
  2089 proof
  2090   assume ?L
  2091   have "?L \<longrightarrow> ?R" by (rule split_zdiv[THEN iffD2]) simp
  2092   with `?L` show ?R by blast
  2093 next
  2094   assume ?R thus ?L
  2095     by(auto simp: div_pos_pos_trivial div_neg_neg_trivial)
  2096 qed
  2097 
  2098 
  2099 subsubsection {* Quotients of Signs *}
  2100 
  2101 lemma div_neg_pos_less0: "[| a < (0::int);  0 < b |] ==> a div b < 0"
  2102 apply (subgoal_tac "a div b \<le> -1", force)
  2103 apply (rule order_trans)
  2104 apply (rule_tac a' = "-1" in zdiv_mono1)
  2105 apply (auto simp add: div_eq_minus1)
  2106 done
  2107 
  2108 lemma div_nonneg_neg_le0: "[| (0::int) \<le> a; b < 0 |] ==> a div b \<le> 0"
  2109 by (drule zdiv_mono1_neg, auto)
  2110 
  2111 lemma div_nonpos_pos_le0: "[| (a::int) \<le> 0; b > 0 |] ==> a div b \<le> 0"
  2112 by (drule zdiv_mono1, auto)
  2113 
  2114 text{* Now for some equivalences of the form @{text"a div b >=< 0 \<longleftrightarrow> \<dots>"}
  2115 conditional upon the sign of @{text a} or @{text b}. There are many more.
  2116 They should all be simp rules unless that causes too much search. *}
  2117 
  2118 lemma pos_imp_zdiv_nonneg_iff: "(0::int) < b ==> (0 \<le> a div b) = (0 \<le> a)"
  2119 apply auto
  2120 apply (drule_tac [2] zdiv_mono1)
  2121 apply (auto simp add: linorder_neq_iff)
  2122 apply (simp (no_asm_use) add: linorder_not_less [symmetric])
  2123 apply (blast intro: div_neg_pos_less0)
  2124 done
  2125 
  2126 lemma neg_imp_zdiv_nonneg_iff:
  2127   "b < (0::int) ==> (0 \<le> a div b) = (a \<le> (0::int))"
  2128 apply (subst div_minus_minus [symmetric])
  2129 apply (subst pos_imp_zdiv_nonneg_iff, auto)
  2130 done
  2131 
  2132 (*But not (a div b \<le> 0 iff a\<le>0); consider a=1, b=2 when a div b = 0.*)
  2133 lemma pos_imp_zdiv_neg_iff: "(0::int) < b ==> (a div b < 0) = (a < 0)"
  2134 by (simp add: linorder_not_le [symmetric] pos_imp_zdiv_nonneg_iff)
  2135 
  2136 lemma pos_imp_zdiv_pos_iff:
  2137   "0<k \<Longrightarrow> 0 < (i::int) div k \<longleftrightarrow> k \<le> i"
  2138 using pos_imp_zdiv_nonneg_iff[of k i] zdiv_eq_0_iff[of i k]
  2139 by arith
  2140 
  2141 (*Again the law fails for \<le>: consider a = -1, b = -2 when a div b = 0*)
  2142 lemma neg_imp_zdiv_neg_iff: "b < (0::int) ==> (a div b < 0) = (0 < a)"
  2143 by (simp add: linorder_not_le [symmetric] neg_imp_zdiv_nonneg_iff)
  2144 
  2145 lemma nonneg1_imp_zdiv_pos_iff:
  2146   "(0::int) <= a \<Longrightarrow> (a div b > 0) = (a >= b & b>0)"
  2147 apply rule
  2148  apply rule
  2149   using div_pos_pos_trivial[of a b]apply arith
  2150  apply(cases "b=0")apply simp
  2151  using div_nonneg_neg_le0[of a b]apply arith
  2152 using int_one_le_iff_zero_less[of "a div b"] zdiv_mono1[of b a b]apply simp
  2153 done
  2154 
  2155 lemma zmod_le_nonneg_dividend: "(m::int) \<ge> 0 ==> m mod k \<le> m"
  2156 apply (rule split_zmod[THEN iffD2])
  2157 apply(fastforce dest: q_pos_lemma intro: split_mult_pos_le)
  2158 done
  2159 
  2160 
  2161 subsubsection {* The Divides Relation *}
  2162 
  2163 lemmas zdvd_iff_zmod_eq_0_numeral [simp] =
  2164   dvd_eq_mod_eq_0 [of "numeral x::int" "numeral y::int"]
  2165   dvd_eq_mod_eq_0 [of "numeral x::int" "neg_numeral y::int"]
  2166   dvd_eq_mod_eq_0 [of "neg_numeral x::int" "numeral y::int"]
  2167   dvd_eq_mod_eq_0 [of "neg_numeral x::int" "neg_numeral y::int"] for x y
  2168 
  2169 lemma zdvd_zmod: "f dvd m ==> f dvd (n::int) ==> f dvd m mod n"
  2170   by (rule dvd_mod) (* TODO: remove *)
  2171 
  2172 lemma zdvd_zmod_imp_zdvd: "k dvd m mod n ==> k dvd n ==> k dvd (m::int)"
  2173   by (rule dvd_mod_imp_dvd) (* TODO: remove *)
  2174 
  2175 lemmas dvd_eq_mod_eq_0_numeral [simp] =
  2176   dvd_eq_mod_eq_0 [of "numeral x" "numeral y"] for x y
  2177 
  2178 
  2179 subsubsection {* Further properties *}
  2180 
  2181 lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)"
  2182   using zmod_zdiv_equality[where a="m" and b="n"]
  2183   by (simp add: algebra_simps) (* FIXME: generalize *)
  2184 
  2185 lemma zpower_zmod: "((x::int) mod m)^y mod m = x^y mod m"
  2186 apply (induct "y", auto)
  2187 apply (rule mod_mult_right_eq [THEN trans])
  2188 apply (simp (no_asm_simp))
  2189 apply (rule mod_mult_eq [symmetric])
  2190 done (* FIXME: generalize *)
  2191 
  2192 lemma zdiv_int: "int (a div b) = (int a) div (int b)"
  2193 apply (subst split_div, auto)
  2194 apply (subst split_zdiv, auto)
  2195 apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in unique_quotient)
  2196 apply (auto simp add: divmod_int_rel_def of_nat_mult)
  2197 done
  2198 
  2199 lemma zmod_int: "int (a mod b) = (int a) mod (int b)"
  2200 apply (subst split_mod, auto)
  2201 apply (subst split_zmod, auto)
  2202 apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia 
  2203        in unique_remainder)
  2204 apply (auto simp add: divmod_int_rel_def of_nat_mult)
  2205 done
  2206 
  2207 lemma abs_div: "(y::int) dvd x \<Longrightarrow> abs (x div y) = abs x div abs y"
  2208 by (unfold dvd_def, cases "y=0", auto simp add: abs_mult)
  2209 
  2210 text{*Suggested by Matthias Daum*}
  2211 lemma int_power_div_base:
  2212      "\<lbrakk>0 < m; 0 < k\<rbrakk> \<Longrightarrow> k ^ m div k = (k::int) ^ (m - Suc 0)"
  2213 apply (subgoal_tac "k ^ m = k ^ ((m - Suc 0) + Suc 0)")
  2214  apply (erule ssubst)
  2215  apply (simp only: power_add)
  2216  apply simp_all
  2217 done
  2218 
  2219 text {* by Brian Huffman *}
  2220 lemma zminus_zmod: "- ((x::int) mod m) mod m = - x mod m"
  2221 by (rule mod_minus_eq [symmetric])
  2222 
  2223 lemma zdiff_zmod_left: "(x mod m - y) mod m = (x - y) mod (m::int)"
  2224 by (rule mod_diff_left_eq [symmetric])
  2225 
  2226 lemma zdiff_zmod_right: "(x - y mod m) mod m = (x - y) mod (m::int)"
  2227 by (rule mod_diff_right_eq [symmetric])
  2228 
  2229 lemmas zmod_simps =
  2230   mod_add_left_eq  [symmetric]
  2231   mod_add_right_eq [symmetric]
  2232   mod_mult_right_eq[symmetric]
  2233   mod_mult_left_eq [symmetric]
  2234   zpower_zmod
  2235   zminus_zmod zdiff_zmod_left zdiff_zmod_right
  2236 
  2237 text {* Distributive laws for function @{text nat}. *}
  2238 
  2239 lemma nat_div_distrib: "0 \<le> x \<Longrightarrow> nat (x div y) = nat x div nat y"
  2240 apply (rule linorder_cases [of y 0])
  2241 apply (simp add: div_nonneg_neg_le0)
  2242 apply simp
  2243 apply (simp add: nat_eq_iff pos_imp_zdiv_nonneg_iff zdiv_int)
  2244 done
  2245 
  2246 (*Fails if y<0: the LHS collapses to (nat z) but the RHS doesn't*)
  2247 lemma nat_mod_distrib:
  2248   "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> nat (x mod y) = nat x mod nat y"
  2249 apply (case_tac "y = 0", simp)
  2250 apply (simp add: nat_eq_iff zmod_int)
  2251 done
  2252 
  2253 text  {* transfer setup *}
  2254 
  2255 lemma transfer_nat_int_functions:
  2256     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) div (nat y) = nat (x div y)"
  2257     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) mod (nat y) = nat (x mod y)"
  2258   by (auto simp add: nat_div_distrib nat_mod_distrib)
  2259 
  2260 lemma transfer_nat_int_function_closures:
  2261     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x div y >= 0"
  2262     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x mod y >= 0"
  2263   apply (cases "y = 0")
  2264   apply (auto simp add: pos_imp_zdiv_nonneg_iff)
  2265   apply (cases "y = 0")
  2266   apply auto
  2267 done
  2268 
  2269 declare transfer_morphism_nat_int [transfer add return:
  2270   transfer_nat_int_functions
  2271   transfer_nat_int_function_closures
  2272 ]
  2273 
  2274 lemma transfer_int_nat_functions:
  2275     "(int x) div (int y) = int (x div y)"
  2276     "(int x) mod (int y) = int (x mod y)"
  2277   by (auto simp add: zdiv_int zmod_int)
  2278 
  2279 lemma transfer_int_nat_function_closures:
  2280     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x div y)"
  2281     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x mod y)"
  2282   by (simp_all only: is_nat_def transfer_nat_int_function_closures)
  2283 
  2284 declare transfer_morphism_int_nat [transfer add return:
  2285   transfer_int_nat_functions
  2286   transfer_int_nat_function_closures
  2287 ]
  2288 
  2289 text{*Suggested by Matthias Daum*}
  2290 lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"
  2291 apply (subgoal_tac "nat x div nat k < nat x")
  2292  apply (simp add: nat_div_distrib [symmetric])
  2293 apply (rule Divides.div_less_dividend, simp_all)
  2294 done
  2295 
  2296 lemma zmod_eq_dvd_iff: "(x::int) mod n = y mod n \<longleftrightarrow> n dvd x - y"
  2297 proof
  2298   assume H: "x mod n = y mod n"
  2299   hence "x mod n - y mod n = 0" by simp
  2300   hence "(x mod n - y mod n) mod n = 0" by simp 
  2301   hence "(x - y) mod n = 0" by (simp add: mod_diff_eq[symmetric])
  2302   thus "n dvd x - y" by (simp add: dvd_eq_mod_eq_0)
  2303 next
  2304   assume H: "n dvd x - y"
  2305   then obtain k where k: "x-y = n*k" unfolding dvd_def by blast
  2306   hence "x = n*k + y" by simp
  2307   hence "x mod n = (n*k + y) mod n" by simp
  2308   thus "x mod n = y mod n" by (simp add: mod_add_left_eq)
  2309 qed
  2310 
  2311 lemma nat_mod_eq_lemma: assumes xyn: "(x::nat) mod n = y  mod n" and xy:"y \<le> x"
  2312   shows "\<exists>q. x = y + n * q"
  2313 proof-
  2314   from xy have th: "int x - int y = int (x - y)" by simp 
  2315   from xyn have "int x mod int n = int y mod int n" 
  2316     by (simp add: zmod_int [symmetric])
  2317   hence "int n dvd int x - int y" by (simp only: zmod_eq_dvd_iff[symmetric]) 
  2318   hence "n dvd x - y" by (simp add: th zdvd_int)
  2319   then show ?thesis using xy unfolding dvd_def apply clarsimp apply (rule_tac x="k" in exI) by arith
  2320 qed
  2321 
  2322 lemma nat_mod_eq_iff: "(x::nat) mod n = y mod n \<longleftrightarrow> (\<exists>q1 q2. x + n * q1 = y + n * q2)" 
  2323   (is "?lhs = ?rhs")
  2324 proof
  2325   assume H: "x mod n = y mod n"
  2326   {assume xy: "x \<le> y"
  2327     from H have th: "y mod n = x mod n" by simp
  2328     from nat_mod_eq_lemma[OF th xy] have ?rhs 
  2329       apply clarify  apply (rule_tac x="q" in exI) by (rule exI[where x="0"], simp)}
  2330   moreover
  2331   {assume xy: "y \<le> x"
  2332     from nat_mod_eq_lemma[OF H xy] have ?rhs 
  2333       apply clarify  apply (rule_tac x="0" in exI) by (rule_tac x="q" in exI, simp)}
  2334   ultimately  show ?rhs using linear[of x y] by blast  
  2335 next
  2336   assume ?rhs then obtain q1 q2 where q12: "x + n * q1 = y + n * q2" by blast
  2337   hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp
  2338   thus  ?lhs by simp
  2339 qed
  2340 
  2341 lemma div_nat_numeral [simp]:
  2342   "(numeral v :: nat) div numeral v' = nat (numeral v div numeral v')"
  2343   by (simp add: nat_div_distrib)
  2344 
  2345 lemma one_div_nat_numeral [simp]:
  2346   "Suc 0 div numeral v' = nat (1 div numeral v')"
  2347   by (subst nat_div_distrib, simp_all)
  2348 
  2349 lemma mod_nat_numeral [simp]:
  2350   "(numeral v :: nat) mod numeral v' = nat (numeral v mod numeral v')"
  2351   by (simp add: nat_mod_distrib)
  2352 
  2353 lemma one_mod_nat_numeral [simp]:
  2354   "Suc 0 mod numeral v' = nat (1 mod numeral v')"
  2355   by (subst nat_mod_distrib) simp_all
  2356 
  2357 lemma mod_2_not_eq_zero_eq_one_int:
  2358   fixes k :: int
  2359   shows "k mod 2 \<noteq> 0 \<longleftrightarrow> k mod 2 = 1"
  2360   by auto
  2361 
  2362 
  2363 subsubsection {* Tools setup *}
  2364 
  2365 text {* Nitpick *}
  2366 
  2367 lemmas [nitpick_unfold] = dvd_eq_mod_eq_0 mod_div_equality' zmod_zdiv_equality'
  2368 
  2369 
  2370 subsubsection {* Code generation *}
  2371 
  2372 definition pdivmod :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  2373   "pdivmod k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
  2374 
  2375 lemma pdivmod_posDivAlg [code]:
  2376   "pdivmod k l = (if l = 0 then (0, \<bar>k\<bar>) else posDivAlg \<bar>k\<bar> \<bar>l\<bar>)"
  2377 by (subst posDivAlg_div_mod) (simp_all add: pdivmod_def)
  2378 
  2379 lemma divmod_int_pdivmod: "divmod_int k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else
  2380   apsnd ((op *) (sgn l)) (if 0 < l \<and> 0 \<le> k \<or> l < 0 \<and> k < 0
  2381     then pdivmod k l
  2382     else (let (r, s) = pdivmod k l in
  2383        if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
  2384 proof -
  2385   have aux: "\<And>q::int. - k = l * q \<longleftrightarrow> k = l * - q" by auto
  2386   show ?thesis
  2387     by (simp add: divmod_int_mod_div pdivmod_def)
  2388       (auto simp add: aux not_less not_le zdiv_zminus1_eq_if
  2389       zmod_zminus1_eq_if zdiv_zminus2_eq_if zmod_zminus2_eq_if)
  2390 qed
  2391 
  2392 lemma divmod_int_code [code]: "divmod_int k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else
  2393   apsnd ((op *) (sgn l)) (if sgn k = sgn l
  2394     then pdivmod k l
  2395     else (let (r, s) = pdivmod k l in
  2396       if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
  2397 proof -
  2398   have "k \<noteq> 0 \<Longrightarrow> l \<noteq> 0 \<Longrightarrow> 0 < l \<and> 0 \<le> k \<or> l < 0 \<and> k < 0 \<longleftrightarrow> sgn k = sgn l"
  2399     by (auto simp add: not_less sgn_if)
  2400   then show ?thesis by (simp add: divmod_int_pdivmod)
  2401 qed
  2402 
  2403 code_modulename SML
  2404   Divides Arith
  2405 
  2406 code_modulename OCaml
  2407   Divides Arith
  2408 
  2409 code_modulename Haskell
  2410   Divides Arith
  2411 
  2412 end