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