src/HOL/Divides.thy
author huffman
Fri Jul 27 19:27:21 2012 +0200 (2012-07-27)
changeset 48561 12aa0cb2b447
parent 47268 262d96552e50
child 48891 c0eafbd55de3
permissions -rw-r--r--
move ML functions from nat_arith.ML to Divides.thy, which is the only place they are used
     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_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_plus = HOLogic.mk_binop @{const_name Groups.plus};
   697   val dest_plus = HOLogic.dest_bin @{const_name Groups.plus} HOLogic.natT;
   698   fun mk_sum [] = HOLogic.zero
   699     | mk_sum [t] = t
   700     | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
   701   fun dest_sum tm =
   702     if HOLogic.is_zero tm then []
   703     else
   704       (case try HOLogic.dest_Suc tm of
   705         SOME t => HOLogic.Suc_zero :: dest_sum t
   706       | NONE =>
   707           (case try dest_plus tm of
   708             SOME (t, u) => dest_sum t @ dest_sum u
   709           | NONE => [tm]));
   710 
   711   val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
   712 
   713   val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac
   714     (@{thm add_0_left} :: @{thm add_0_right} :: @{thms add_ac}))
   715 )
   716 *}
   717 
   718 simproc_setup cancel_div_mod_nat ("(m::nat) + n") = {* K Cancel_Div_Mod_Nat.proc *}
   719 
   720 
   721 subsubsection {* Quotient *}
   722 
   723 lemma div_geq: "0 < n \<Longrightarrow>  \<not> m < n \<Longrightarrow> m div n = Suc ((m - n) div n)"
   724 by (simp add: le_div_geq linorder_not_less)
   725 
   726 lemma div_if: "0 < n \<Longrightarrow> m div n = (if m < n then 0 else Suc ((m - n) div n))"
   727 by (simp add: div_geq)
   728 
   729 lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
   730 by simp
   731 
   732 lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
   733 by simp
   734 
   735 
   736 subsubsection {* Remainder *}
   737 
   738 lemma mod_less_divisor [simp]:
   739   fixes m n :: nat
   740   assumes "n > 0"
   741   shows "m mod n < (n::nat)"
   742   using assms divmod_nat_rel [of m n] unfolding divmod_nat_rel_def by auto
   743 
   744 lemma mod_less_eq_dividend [simp]:
   745   fixes m n :: nat
   746   shows "m mod n \<le> m"
   747 proof (rule add_leD2)
   748   from mod_div_equality have "m div n * n + m mod n = m" .
   749   then show "m div n * n + m mod n \<le> m" by auto
   750 qed
   751 
   752 lemma mod_geq: "\<not> m < (n\<Colon>nat) \<Longrightarrow> m mod n = (m - n) mod n"
   753 by (simp add: le_mod_geq linorder_not_less)
   754 
   755 lemma mod_if: "m mod (n\<Colon>nat) = (if m < n then m else (m - n) mod n)"
   756 by (simp add: le_mod_geq)
   757 
   758 lemma mod_1 [simp]: "m mod Suc 0 = 0"
   759 by (induct m) (simp_all add: mod_geq)
   760 
   761 (* a simple rearrangement of mod_div_equality: *)
   762 lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
   763   using mod_div_equality2 [of n m] by arith
   764 
   765 lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
   766   apply (drule mod_less_divisor [where m = m])
   767   apply simp
   768   done
   769 
   770 subsubsection {* Quotient and Remainder *}
   771 
   772 lemma divmod_nat_rel_mult1_eq:
   773   "divmod_nat_rel b c (q, r)
   774    \<Longrightarrow> divmod_nat_rel (a * b) c (a * q + a * r div c, a * r mod c)"
   775 by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
   776 
   777 lemma div_mult1_eq:
   778   "(a * b) div c = a * (b div c) + a * (b mod c) div (c::nat)"
   779 by (blast intro: divmod_nat_rel_mult1_eq [THEN div_nat_unique] divmod_nat_rel)
   780 
   781 lemma divmod_nat_rel_add1_eq:
   782   "divmod_nat_rel a c (aq, ar) \<Longrightarrow> divmod_nat_rel b c (bq, br)
   783    \<Longrightarrow> divmod_nat_rel (a + b) c (aq + bq + (ar + br) div c, (ar + br) mod c)"
   784 by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
   785 
   786 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
   787 lemma div_add1_eq:
   788   "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
   789 by (blast intro: divmod_nat_rel_add1_eq [THEN div_nat_unique] divmod_nat_rel)
   790 
   791 lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
   792   apply (cut_tac m = q and n = c in mod_less_divisor)
   793   apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
   794   apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
   795   apply (simp add: add_mult_distrib2)
   796   done
   797 
   798 lemma divmod_nat_rel_mult2_eq:
   799   "divmod_nat_rel a b (q, r)
   800    \<Longrightarrow> divmod_nat_rel a (b * c) (q div c, b *(q mod c) + r)"
   801 by (auto simp add: mult_ac divmod_nat_rel_def add_mult_distrib2 [symmetric] mod_lemma)
   802 
   803 lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
   804 by (force simp add: divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN div_nat_unique])
   805 
   806 lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
   807 by (auto simp add: mult_commute divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN mod_nat_unique])
   808 
   809 
   810 subsubsection {* Further Facts about Quotient and Remainder *}
   811 
   812 lemma div_1 [simp]: "m div Suc 0 = m"
   813 by (induct m) (simp_all add: div_geq)
   814 
   815 (* Monotonicity of div in first argument *)
   816 lemma div_le_mono [rule_format (no_asm)]:
   817     "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
   818 apply (case_tac "k=0", simp)
   819 apply (induct "n" rule: nat_less_induct, clarify)
   820 apply (case_tac "n<k")
   821 (* 1  case n<k *)
   822 apply simp
   823 (* 2  case n >= k *)
   824 apply (case_tac "m<k")
   825 (* 2.1  case m<k *)
   826 apply simp
   827 (* 2.2  case m>=k *)
   828 apply (simp add: div_geq diff_le_mono)
   829 done
   830 
   831 (* Antimonotonicity of div in second argument *)
   832 lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
   833 apply (subgoal_tac "0<n")
   834  prefer 2 apply simp
   835 apply (induct_tac k rule: nat_less_induct)
   836 apply (rename_tac "k")
   837 apply (case_tac "k<n", simp)
   838 apply (subgoal_tac "~ (k<m) ")
   839  prefer 2 apply simp
   840 apply (simp add: div_geq)
   841 apply (subgoal_tac "(k-n) div n \<le> (k-m) div n")
   842  prefer 2
   843  apply (blast intro: div_le_mono diff_le_mono2)
   844 apply (rule le_trans, simp)
   845 apply (simp)
   846 done
   847 
   848 lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
   849 apply (case_tac "n=0", simp)
   850 apply (subgoal_tac "m div n \<le> m div 1", simp)
   851 apply (rule div_le_mono2)
   852 apply (simp_all (no_asm_simp))
   853 done
   854 
   855 (* Similar for "less than" *)
   856 lemma div_less_dividend [simp]:
   857   "\<lbrakk>(1::nat) < n; 0 < m\<rbrakk> \<Longrightarrow> m div n < m"
   858 apply (induct m rule: nat_less_induct)
   859 apply (rename_tac "m")
   860 apply (case_tac "m<n", simp)
   861 apply (subgoal_tac "0<n")
   862  prefer 2 apply simp
   863 apply (simp add: div_geq)
   864 apply (case_tac "n<m")
   865  apply (subgoal_tac "(m-n) div n < (m-n) ")
   866   apply (rule impI less_trans_Suc)+
   867 apply assumption
   868   apply (simp_all)
   869 done
   870 
   871 text{*A fact for the mutilated chess board*}
   872 lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
   873 apply (case_tac "n=0", simp)
   874 apply (induct "m" rule: nat_less_induct)
   875 apply (case_tac "Suc (na) <n")
   876 (* case Suc(na) < n *)
   877 apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
   878 (* case n \<le> Suc(na) *)
   879 apply (simp add: linorder_not_less le_Suc_eq mod_geq)
   880 apply (auto simp add: Suc_diff_le le_mod_geq)
   881 done
   882 
   883 lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
   884 by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
   885 
   886 lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]
   887 
   888 (*Loses information, namely we also have r<d provided d is nonzero*)
   889 lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
   890   apply (cut_tac a = m in mod_div_equality)
   891   apply (simp only: add_ac)
   892   apply (blast intro: sym)
   893   done
   894 
   895 lemma split_div:
   896  "P(n div k :: nat) =
   897  ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
   898  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   899 proof
   900   assume P: ?P
   901   show ?Q
   902   proof (cases)
   903     assume "k = 0"
   904     with P show ?Q by simp
   905   next
   906     assume not0: "k \<noteq> 0"
   907     thus ?Q
   908     proof (simp, intro allI impI)
   909       fix i j
   910       assume n: "n = k*i + j" and j: "j < k"
   911       show "P i"
   912       proof (cases)
   913         assume "i = 0"
   914         with n j P show "P i" by simp
   915       next
   916         assume "i \<noteq> 0"
   917         with not0 n j P show "P i" by(simp add:add_ac)
   918       qed
   919     qed
   920   qed
   921 next
   922   assume Q: ?Q
   923   show ?P
   924   proof (cases)
   925     assume "k = 0"
   926     with Q show ?P by simp
   927   next
   928     assume not0: "k \<noteq> 0"
   929     with Q have R: ?R by simp
   930     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   931     show ?P by simp
   932   qed
   933 qed
   934 
   935 lemma split_div_lemma:
   936   assumes "0 < n"
   937   shows "n * q \<le> m \<and> m < n * Suc q \<longleftrightarrow> q = ((m\<Colon>nat) div n)" (is "?lhs \<longleftrightarrow> ?rhs")
   938 proof
   939   assume ?rhs
   940   with mult_div_cancel have nq: "n * q = m - (m mod n)" by simp
   941   then have A: "n * q \<le> m" by simp
   942   have "n - (m mod n) > 0" using mod_less_divisor assms by auto
   943   then have "m < m + (n - (m mod n))" by simp
   944   then have "m < n + (m - (m mod n))" by simp
   945   with nq have "m < n + n * q" by simp
   946   then have B: "m < n * Suc q" by simp
   947   from A B show ?lhs ..
   948 next
   949   assume P: ?lhs
   950   then have "divmod_nat_rel m n (q, m - n * q)"
   951     unfolding divmod_nat_rel_def by (auto simp add: mult_ac)
   952   with divmod_nat_rel_unique divmod_nat_rel [of m n]
   953   have "(q, m - n * q) = (m div n, m mod n)" by auto
   954   then show ?rhs by simp
   955 qed
   956 
   957 theorem split_div':
   958   "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
   959    (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
   960   apply (case_tac "0 < n")
   961   apply (simp only: add: split_div_lemma)
   962   apply simp_all
   963   done
   964 
   965 lemma split_mod:
   966  "P(n mod k :: nat) =
   967  ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
   968  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   969 proof
   970   assume P: ?P
   971   show ?Q
   972   proof (cases)
   973     assume "k = 0"
   974     with P show ?Q by simp
   975   next
   976     assume not0: "k \<noteq> 0"
   977     thus ?Q
   978     proof (simp, intro allI impI)
   979       fix i j
   980       assume "n = k*i + j" "j < k"
   981       thus "P j" using not0 P by(simp add:add_ac mult_ac)
   982     qed
   983   qed
   984 next
   985   assume Q: ?Q
   986   show ?P
   987   proof (cases)
   988     assume "k = 0"
   989     with Q show ?P by simp
   990   next
   991     assume not0: "k \<noteq> 0"
   992     with Q have R: ?R by simp
   993     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   994     show ?P by simp
   995   qed
   996 qed
   997 
   998 theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
   999   using mod_div_equality [of m n] by arith
  1000 
  1001 lemma div_mod_equality': "(m::nat) div n * n = m - m mod n"
  1002   using mod_div_equality [of m n] by arith
  1003 (* FIXME: very similar to mult_div_cancel *)
  1004 
  1005 
  1006 subsubsection {* An ``induction'' law for modulus arithmetic. *}
  1007 
  1008 lemma mod_induct_0:
  1009   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
  1010   and base: "P i" and i: "i<p"
  1011   shows "P 0"
  1012 proof (rule ccontr)
  1013   assume contra: "\<not>(P 0)"
  1014   from i have p: "0<p" by simp
  1015   have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
  1016   proof
  1017     fix k
  1018     show "?A k"
  1019     proof (induct k)
  1020       show "?A 0" by simp  -- "by contradiction"
  1021     next
  1022       fix n
  1023       assume ih: "?A n"
  1024       show "?A (Suc n)"
  1025       proof (clarsimp)
  1026         assume y: "P (p - Suc n)"
  1027         have n: "Suc n < p"
  1028         proof (rule ccontr)
  1029           assume "\<not>(Suc n < p)"
  1030           hence "p - Suc n = 0"
  1031             by simp
  1032           with y contra show "False"
  1033             by simp
  1034         qed
  1035         hence n2: "Suc (p - Suc n) = p-n" by arith
  1036         from p have "p - Suc n < p" by arith
  1037         with y step have z: "P ((Suc (p - Suc n)) mod p)"
  1038           by blast
  1039         show "False"
  1040         proof (cases "n=0")
  1041           case True
  1042           with z n2 contra show ?thesis by simp
  1043         next
  1044           case False
  1045           with p have "p-n < p" by arith
  1046           with z n2 False ih show ?thesis by simp
  1047         qed
  1048       qed
  1049     qed
  1050   qed
  1051   moreover
  1052   from i obtain k where "0<k \<and> i+k=p"
  1053     by (blast dest: less_imp_add_positive)
  1054   hence "0<k \<and> i=p-k" by auto
  1055   moreover
  1056   note base
  1057   ultimately
  1058   show "False" by blast
  1059 qed
  1060 
  1061 lemma mod_induct:
  1062   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
  1063   and base: "P i" and i: "i<p" and j: "j<p"
  1064   shows "P j"
  1065 proof -
  1066   have "\<forall>j<p. P j"
  1067   proof
  1068     fix j
  1069     show "j<p \<longrightarrow> P j" (is "?A j")
  1070     proof (induct j)
  1071       from step base i show "?A 0"
  1072         by (auto elim: mod_induct_0)
  1073     next
  1074       fix k
  1075       assume ih: "?A k"
  1076       show "?A (Suc k)"
  1077       proof
  1078         assume suc: "Suc k < p"
  1079         hence k: "k<p" by simp
  1080         with ih have "P k" ..
  1081         with step k have "P (Suc k mod p)"
  1082           by blast
  1083         moreover
  1084         from suc have "Suc k mod p = Suc k"
  1085           by simp
  1086         ultimately
  1087         show "P (Suc k)" by simp
  1088       qed
  1089     qed
  1090   qed
  1091   with j show ?thesis by blast
  1092 qed
  1093 
  1094 lemma div2_Suc_Suc [simp]: "Suc (Suc m) div 2 = Suc (m div 2)"
  1095   by (simp add: numeral_2_eq_2 le_div_geq)
  1096 
  1097 lemma mod2_Suc_Suc [simp]: "Suc (Suc m) mod 2 = m mod 2"
  1098   by (simp add: numeral_2_eq_2 le_mod_geq)
  1099 
  1100 lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
  1101 by (simp add: mult_2 [symmetric])
  1102 
  1103 lemma mod2_gr_0 [simp]: "0 < (m\<Colon>nat) mod 2 \<longleftrightarrow> m mod 2 = 1"
  1104 proof -
  1105   { fix n :: nat have  "(n::nat) < 2 \<Longrightarrow> n = 0 \<or> n = 1" by (cases n) simp_all }
  1106   moreover have "m mod 2 < 2" by simp
  1107   ultimately have "m mod 2 = 0 \<or> m mod 2 = 1" .
  1108   then show ?thesis by auto
  1109 qed
  1110 
  1111 text{*These lemmas collapse some needless occurrences of Suc:
  1112     at least three Sucs, since two and fewer are rewritten back to Suc again!
  1113     We already have some rules to simplify operands smaller than 3.*}
  1114 
  1115 lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
  1116 by (simp add: Suc3_eq_add_3)
  1117 
  1118 lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
  1119 by (simp add: Suc3_eq_add_3)
  1120 
  1121 lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
  1122 by (simp add: Suc3_eq_add_3)
  1123 
  1124 lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
  1125 by (simp add: Suc3_eq_add_3)
  1126 
  1127 lemmas Suc_div_eq_add3_div_numeral [simp] = Suc_div_eq_add3_div [of _ "numeral v"] for v
  1128 lemmas Suc_mod_eq_add3_mod_numeral [simp] = Suc_mod_eq_add3_mod [of _ "numeral v"] for v
  1129 
  1130 
  1131 lemma Suc_times_mod_eq: "1<k ==> Suc (k * m) mod k = 1" 
  1132 apply (induct "m")
  1133 apply (simp_all add: mod_Suc)
  1134 done
  1135 
  1136 declare Suc_times_mod_eq [of "numeral w", simp] for w
  1137 
  1138 lemma Suc_div_le_mono [simp]: "n div k \<le> (Suc n) div k"
  1139 by (simp add: div_le_mono)
  1140 
  1141 lemma Suc_n_div_2_gt_zero [simp]: "(0::nat) < n ==> 0 < (n + 1) div 2"
  1142 by (cases n) simp_all
  1143 
  1144 lemma div_2_gt_zero [simp]: assumes A: "(1::nat) < n" shows "0 < n div 2"
  1145 proof -
  1146   from A have B: "0 < n - 1" and C: "n - 1 + 1 = n" by simp_all
  1147   from Suc_n_div_2_gt_zero [OF B] C show ?thesis by simp 
  1148 qed
  1149 
  1150   (* Potential use of algebra : Equality modulo n*)
  1151 lemma mod_mult_self3 [simp]: "(k*n + m) mod n = m mod (n::nat)"
  1152 by (simp add: mult_ac add_ac)
  1153 
  1154 lemma mod_mult_self4 [simp]: "Suc (k*n + m) mod n = Suc m mod n"
  1155 proof -
  1156   have "Suc (k * n + m) mod n = (k * n + Suc m) mod n" by simp
  1157   also have "... = Suc m mod n" by (rule mod_mult_self3) 
  1158   finally show ?thesis .
  1159 qed
  1160 
  1161 lemma mod_Suc_eq_Suc_mod: "Suc m mod n = Suc (m mod n) mod n"
  1162 apply (subst mod_Suc [of m]) 
  1163 apply (subst mod_Suc [of "m mod n"], simp) 
  1164 done
  1165 
  1166 lemma mod_2_not_eq_zero_eq_one_nat:
  1167   fixes n :: nat
  1168   shows "n mod 2 \<noteq> 0 \<longleftrightarrow> n mod 2 = 1"
  1169   by simp
  1170 
  1171 
  1172 subsection {* Division on @{typ int} *}
  1173 
  1174 definition divmod_int_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" where
  1175     --{*definition of quotient and remainder*}
  1176   "divmod_int_rel a b = (\<lambda>(q, r). a = b * q + r \<and>
  1177     (if 0 < b then 0 \<le> r \<and> r < b else if b < 0 then b < r \<and> r \<le> 0 else q = 0))"
  1178 
  1179 definition adjust :: "int \<Rightarrow> int \<times> int \<Rightarrow> int \<times> int" where
  1180     --{*for the division algorithm*}
  1181     "adjust b = (\<lambda>(q, r). if 0 \<le> r - b then (2 * q + 1, r - b)
  1182                          else (2 * q, r))"
  1183 
  1184 text{*algorithm for the case @{text "a\<ge>0, b>0"}*}
  1185 function posDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  1186   "posDivAlg a b = (if a < b \<or>  b \<le> 0 then (0, a)
  1187      else adjust b (posDivAlg a (2 * b)))"
  1188 by auto
  1189 termination by (relation "measure (\<lambda>(a, b). nat (a - b + 1))")
  1190   (auto simp add: mult_2)
  1191 
  1192 text{*algorithm for the case @{text "a<0, b>0"}*}
  1193 function negDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  1194   "negDivAlg a b = (if 0 \<le>a + b \<or> b \<le> 0  then (-1, a + b)
  1195      else adjust b (negDivAlg a (2 * b)))"
  1196 by auto
  1197 termination by (relation "measure (\<lambda>(a, b). nat (- a - b))")
  1198   (auto simp add: mult_2)
  1199 
  1200 text{*algorithm for the general case @{term "b\<noteq>0"}*}
  1201 
  1202 definition divmod_int :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  1203     --{*The full division algorithm considers all possible signs for a, b
  1204        including the special case @{text "a=0, b<0"} because 
  1205        @{term negDivAlg} requires @{term "a<0"}.*}
  1206   "divmod_int a b = (if 0 \<le> a then if 0 \<le> b then posDivAlg a b
  1207                   else if a = 0 then (0, 0)
  1208                        else apsnd uminus (negDivAlg (-a) (-b))
  1209                else 
  1210                   if 0 < b then negDivAlg a b
  1211                   else apsnd uminus (posDivAlg (-a) (-b)))"
  1212 
  1213 instantiation int :: Divides.div
  1214 begin
  1215 
  1216 definition div_int where
  1217   "a div b = fst (divmod_int a b)"
  1218 
  1219 lemma fst_divmod_int [simp]:
  1220   "fst (divmod_int a b) = a div b"
  1221   by (simp add: div_int_def)
  1222 
  1223 definition mod_int where
  1224   "a mod b = snd (divmod_int a b)"
  1225 
  1226 lemma snd_divmod_int [simp]:
  1227   "snd (divmod_int a b) = a mod b"
  1228   by (simp add: mod_int_def)
  1229 
  1230 instance ..
  1231 
  1232 end
  1233 
  1234 lemma divmod_int_mod_div:
  1235   "divmod_int p q = (p div q, p mod q)"
  1236   by (simp add: prod_eq_iff)
  1237 
  1238 text{*
  1239 Here is the division algorithm in ML:
  1240 
  1241 \begin{verbatim}
  1242     fun posDivAlg (a,b) =
  1243       if a<b then (0,a)
  1244       else let val (q,r) = posDivAlg(a, 2*b)
  1245                in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
  1246            end
  1247 
  1248     fun negDivAlg (a,b) =
  1249       if 0\<le>a+b then (~1,a+b)
  1250       else let val (q,r) = negDivAlg(a, 2*b)
  1251                in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
  1252            end;
  1253 
  1254     fun negateSnd (q,r:int) = (q,~r);
  1255 
  1256     fun divmod (a,b) = if 0\<le>a then 
  1257                           if b>0 then posDivAlg (a,b) 
  1258                            else if a=0 then (0,0)
  1259                                 else negateSnd (negDivAlg (~a,~b))
  1260                        else 
  1261                           if 0<b then negDivAlg (a,b)
  1262                           else        negateSnd (posDivAlg (~a,~b));
  1263 \end{verbatim}
  1264 *}
  1265 
  1266 
  1267 subsubsection {* Uniqueness and Monotonicity of Quotients and Remainders *}
  1268 
  1269 lemma unique_quotient_lemma:
  1270      "[| b*q' + r'  \<le> b*q + r;  0 \<le> r';  r' < b;  r < b |]  
  1271       ==> q' \<le> (q::int)"
  1272 apply (subgoal_tac "r' + b * (q'-q) \<le> r")
  1273  prefer 2 apply (simp add: right_diff_distrib)
  1274 apply (subgoal_tac "0 < b * (1 + q - q') ")
  1275 apply (erule_tac [2] order_le_less_trans)
  1276  prefer 2 apply (simp add: right_diff_distrib right_distrib)
  1277 apply (subgoal_tac "b * q' < b * (1 + q) ")
  1278  prefer 2 apply (simp add: right_diff_distrib right_distrib)
  1279 apply (simp add: mult_less_cancel_left)
  1280 done
  1281 
  1282 lemma unique_quotient_lemma_neg:
  1283      "[| b*q' + r' \<le> b*q + r;  r \<le> 0;  b < r;  b < r' |]  
  1284       ==> q \<le> (q'::int)"
  1285 by (rule_tac b = "-b" and r = "-r'" and r' = "-r" in unique_quotient_lemma, 
  1286     auto)
  1287 
  1288 lemma unique_quotient:
  1289      "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r') |]  
  1290       ==> q = q'"
  1291 apply (simp add: divmod_int_rel_def linorder_neq_iff split: split_if_asm)
  1292 apply (blast intro: order_antisym
  1293              dest: order_eq_refl [THEN unique_quotient_lemma] 
  1294              order_eq_refl [THEN unique_quotient_lemma_neg] sym)+
  1295 done
  1296 
  1297 
  1298 lemma unique_remainder:
  1299      "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r') |]  
  1300       ==> r = r'"
  1301 apply (subgoal_tac "q = q'")
  1302  apply (simp add: divmod_int_rel_def)
  1303 apply (blast intro: unique_quotient)
  1304 done
  1305 
  1306 
  1307 subsubsection {* Correctness of @{term posDivAlg}, the Algorithm for Non-Negative Dividends *}
  1308 
  1309 text{*And positive divisors*}
  1310 
  1311 lemma adjust_eq [simp]:
  1312      "adjust b (q, r) = 
  1313       (let diff = r - b in  
  1314         if 0 \<le> diff then (2 * q + 1, diff)   
  1315                      else (2*q, r))"
  1316   by (simp add: Let_def adjust_def)
  1317 
  1318 declare posDivAlg.simps [simp del]
  1319 
  1320 text{*use with a simproc to avoid repeatedly proving the premise*}
  1321 lemma posDivAlg_eqn:
  1322      "0 < b ==>  
  1323       posDivAlg a b = (if a<b then (0,a) else adjust b (posDivAlg a (2*b)))"
  1324 by (rule posDivAlg.simps [THEN trans], simp)
  1325 
  1326 text{*Correctness of @{term posDivAlg}: it computes quotients correctly*}
  1327 theorem posDivAlg_correct:
  1328   assumes "0 \<le> a" and "0 < b"
  1329   shows "divmod_int_rel a b (posDivAlg a b)"
  1330   using assms
  1331   apply (induct a b rule: posDivAlg.induct)
  1332   apply auto
  1333   apply (simp add: divmod_int_rel_def)
  1334   apply (subst posDivAlg_eqn, simp add: right_distrib)
  1335   apply (case_tac "a < b")
  1336   apply simp_all
  1337   apply (erule splitE)
  1338   apply (auto simp add: right_distrib Let_def mult_ac mult_2_right)
  1339   done
  1340 
  1341 
  1342 subsubsection {* Correctness of @{term negDivAlg}, the Algorithm for Negative Dividends *}
  1343 
  1344 text{*And positive divisors*}
  1345 
  1346 declare negDivAlg.simps [simp del]
  1347 
  1348 text{*use with a simproc to avoid repeatedly proving the premise*}
  1349 lemma negDivAlg_eqn:
  1350      "0 < b ==>  
  1351       negDivAlg a b =       
  1352        (if 0\<le>a+b then (-1,a+b) else adjust b (negDivAlg a (2*b)))"
  1353 by (rule negDivAlg.simps [THEN trans], simp)
  1354 
  1355 (*Correctness of negDivAlg: it computes quotients correctly
  1356   It doesn't work if a=0 because the 0/b equals 0, not -1*)
  1357 lemma negDivAlg_correct:
  1358   assumes "a < 0" and "b > 0"
  1359   shows "divmod_int_rel a b (negDivAlg a b)"
  1360   using assms
  1361   apply (induct a b rule: negDivAlg.induct)
  1362   apply (auto simp add: linorder_not_le)
  1363   apply (simp add: divmod_int_rel_def)
  1364   apply (subst negDivAlg_eqn, assumption)
  1365   apply (case_tac "a + b < (0\<Colon>int)")
  1366   apply simp_all
  1367   apply (erule splitE)
  1368   apply (auto simp add: right_distrib Let_def mult_ac mult_2_right)
  1369   done
  1370 
  1371 
  1372 subsubsection {* Existence Shown by Proving the Division Algorithm to be Correct *}
  1373 
  1374 (*the case a=0*)
  1375 lemma divmod_int_rel_0: "divmod_int_rel 0 b (0, 0)"
  1376 by (auto simp add: divmod_int_rel_def linorder_neq_iff)
  1377 
  1378 lemma posDivAlg_0 [simp]: "posDivAlg 0 b = (0, 0)"
  1379 by (subst posDivAlg.simps, auto)
  1380 
  1381 lemma posDivAlg_0_right [simp]: "posDivAlg a 0 = (0, a)"
  1382 by (subst posDivAlg.simps, auto)
  1383 
  1384 lemma negDivAlg_minus1 [simp]: "negDivAlg -1 b = (-1, b - 1)"
  1385 by (subst negDivAlg.simps, auto)
  1386 
  1387 lemma divmod_int_rel_neg: "divmod_int_rel (-a) (-b) qr ==> divmod_int_rel a b (apsnd uminus qr)"
  1388 by (auto simp add: divmod_int_rel_def)
  1389 
  1390 lemma divmod_int_correct: "divmod_int_rel a b (divmod_int a b)"
  1391 apply (cases "b = 0", simp add: divmod_int_def divmod_int_rel_def)
  1392 by (force simp add: linorder_neq_iff divmod_int_rel_0 divmod_int_def divmod_int_rel_neg
  1393                     posDivAlg_correct negDivAlg_correct)
  1394 
  1395 lemma divmod_int_unique:
  1396   assumes "divmod_int_rel a b qr" 
  1397   shows "divmod_int a b = qr"
  1398   using assms divmod_int_correct [of a b]
  1399   using unique_quotient [of a b] unique_remainder [of a b]
  1400   by (metis pair_collapse)
  1401 
  1402 lemma divmod_int_rel_div_mod: "divmod_int_rel a b (a div b, a mod b)"
  1403   using divmod_int_correct by (simp add: divmod_int_mod_div)
  1404 
  1405 lemma div_int_unique: "divmod_int_rel a b (q, r) \<Longrightarrow> a div b = q"
  1406   by (simp add: divmod_int_rel_div_mod [THEN unique_quotient])
  1407 
  1408 lemma mod_int_unique: "divmod_int_rel a b (q, r) \<Longrightarrow> a mod b = r"
  1409   by (simp add: divmod_int_rel_div_mod [THEN unique_remainder])
  1410 
  1411 instance int :: ring_div
  1412 proof
  1413   fix a b :: int
  1414   show "a div b * b + a mod b = a"
  1415     using divmod_int_rel_div_mod [of a b]
  1416     unfolding divmod_int_rel_def by (simp add: mult_commute)
  1417 next
  1418   fix a b c :: int
  1419   assume "b \<noteq> 0"
  1420   hence "divmod_int_rel (a + c * b) b (c + a div b, a mod b)"
  1421     using divmod_int_rel_div_mod [of a b]
  1422     unfolding divmod_int_rel_def by (auto simp: algebra_simps)
  1423   thus "(a + c * b) div b = c + a div b"
  1424     by (rule div_int_unique)
  1425 next
  1426   fix a b c :: int
  1427   assume "c \<noteq> 0"
  1428   hence "\<And>q r. divmod_int_rel a b (q, r)
  1429     \<Longrightarrow> divmod_int_rel (c * a) (c * b) (q, c * r)"
  1430     unfolding divmod_int_rel_def
  1431     by - (rule linorder_cases [of 0 b], auto simp: algebra_simps
  1432       mult_less_0_iff zero_less_mult_iff mult_strict_right_mono
  1433       mult_strict_right_mono_neg zero_le_mult_iff mult_le_0_iff)
  1434   hence "divmod_int_rel (c * a) (c * b) (a div b, c * (a mod b))"
  1435     using divmod_int_rel_div_mod [of a b] .
  1436   thus "(c * a) div (c * b) = a div b"
  1437     by (rule div_int_unique)
  1438 next
  1439   fix a :: int show "a div 0 = 0"
  1440     by (rule div_int_unique, simp add: divmod_int_rel_def)
  1441 next
  1442   fix a :: int show "0 div a = 0"
  1443     by (rule div_int_unique, auto simp add: divmod_int_rel_def)
  1444 qed
  1445 
  1446 text{*Basic laws about division and remainder*}
  1447 
  1448 lemma zmod_zdiv_equality: "(a::int) = b * (a div b) + (a mod b)"
  1449   by (fact mod_div_equality2 [symmetric])
  1450 
  1451 text {* Tool setup *}
  1452 
  1453 (* FIXME: Theorem list add_0s doesn't exist, because Numeral0 has gone. *)
  1454 lemmas add_0s = add_0_left add_0_right
  1455 
  1456 ML {*
  1457 structure Cancel_Div_Mod_Int = Cancel_Div_Mod
  1458 (
  1459   val div_name = @{const_name div};
  1460   val mod_name = @{const_name mod};
  1461   val mk_binop = HOLogic.mk_binop;
  1462   val mk_sum = Arith_Data.mk_sum HOLogic.intT;
  1463   val dest_sum = Arith_Data.dest_sum;
  1464 
  1465   val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
  1466 
  1467   val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac 
  1468     (@{thm diff_minus} :: @{thms add_0s} @ @{thms add_ac}))
  1469 )
  1470 *}
  1471 
  1472 simproc_setup cancel_div_mod_int ("(k::int) + l") = {* K Cancel_Div_Mod_Int.proc *}
  1473 
  1474 lemma pos_mod_conj: "(0::int) < b \<Longrightarrow> 0 \<le> a mod b \<and> a mod b < b"
  1475   using divmod_int_correct [of a b]
  1476   by (auto simp add: divmod_int_rel_def prod_eq_iff)
  1477 
  1478 lemmas pos_mod_sign [simp] = pos_mod_conj [THEN conjunct1]
  1479    and pos_mod_bound [simp] = pos_mod_conj [THEN conjunct2]
  1480 
  1481 lemma neg_mod_conj: "b < (0::int) \<Longrightarrow> a mod b \<le> 0 \<and> b < a mod b"
  1482   using divmod_int_correct [of a b]
  1483   by (auto simp add: divmod_int_rel_def prod_eq_iff)
  1484 
  1485 lemmas neg_mod_sign [simp] = neg_mod_conj [THEN conjunct1]
  1486    and neg_mod_bound [simp] = neg_mod_conj [THEN conjunct2]
  1487 
  1488 
  1489 subsubsection {* General Properties of div and mod *}
  1490 
  1491 lemma div_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a div b = 0"
  1492 apply (rule div_int_unique)
  1493 apply (auto simp add: divmod_int_rel_def)
  1494 done
  1495 
  1496 lemma div_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a div b = 0"
  1497 apply (rule div_int_unique)
  1498 apply (auto simp add: divmod_int_rel_def)
  1499 done
  1500 
  1501 lemma div_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a div b = -1"
  1502 apply (rule div_int_unique)
  1503 apply (auto simp add: divmod_int_rel_def)
  1504 done
  1505 
  1506 (*There is no div_neg_pos_trivial because  0 div b = 0 would supersede it*)
  1507 
  1508 lemma mod_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a mod b = a"
  1509 apply (rule_tac q = 0 in mod_int_unique)
  1510 apply (auto simp add: divmod_int_rel_def)
  1511 done
  1512 
  1513 lemma mod_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a mod b = a"
  1514 apply (rule_tac q = 0 in mod_int_unique)
  1515 apply (auto simp add: divmod_int_rel_def)
  1516 done
  1517 
  1518 lemma mod_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a mod b = a+b"
  1519 apply (rule_tac q = "-1" in mod_int_unique)
  1520 apply (auto simp add: divmod_int_rel_def)
  1521 done
  1522 
  1523 text{*There is no @{text mod_neg_pos_trivial}.*}
  1524 
  1525 
  1526 subsubsection {* Laws for div and mod with Unary Minus *}
  1527 
  1528 lemma zminus1_lemma:
  1529      "divmod_int_rel a b (q, r) ==> b \<noteq> 0
  1530       ==> divmod_int_rel (-a) b (if r=0 then -q else -q - 1,  
  1531                           if r=0 then 0 else b-r)"
  1532 by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_diff_distrib)
  1533 
  1534 
  1535 lemma zdiv_zminus1_eq_if:
  1536      "b \<noteq> (0::int)  
  1537       ==> (-a) div b =  
  1538           (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
  1539 by (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN div_int_unique])
  1540 
  1541 lemma zmod_zminus1_eq_if:
  1542      "(-a::int) mod b = (if a mod b = 0 then 0 else  b - (a mod b))"
  1543 apply (case_tac "b = 0", simp)
  1544 apply (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN mod_int_unique])
  1545 done
  1546 
  1547 lemma zmod_zminus1_not_zero:
  1548   fixes k l :: int
  1549   shows "- k mod l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
  1550   unfolding zmod_zminus1_eq_if by auto
  1551 
  1552 lemma zdiv_zminus2_eq_if:
  1553      "b \<noteq> (0::int)  
  1554       ==> a div (-b) =  
  1555           (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
  1556 by (simp add: zdiv_zminus1_eq_if div_minus_right)
  1557 
  1558 lemma zmod_zminus2_eq_if:
  1559      "a mod (-b::int) = (if a mod b = 0 then 0 else  (a mod b) - b)"
  1560 by (simp add: zmod_zminus1_eq_if mod_minus_right)
  1561 
  1562 lemma zmod_zminus2_not_zero:
  1563   fixes k l :: int
  1564   shows "k mod - l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
  1565   unfolding zmod_zminus2_eq_if by auto 
  1566 
  1567 
  1568 subsubsection {* Computation of Division and Remainder *}
  1569 
  1570 lemma div_eq_minus1: "(0::int) < b ==> -1 div b = -1"
  1571 by (simp add: div_int_def divmod_int_def)
  1572 
  1573 lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1"
  1574 by (simp add: mod_int_def divmod_int_def)
  1575 
  1576 text{*a positive, b positive *}
  1577 
  1578 lemma div_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a div b = fst (posDivAlg a b)"
  1579 by (simp add: div_int_def divmod_int_def)
  1580 
  1581 lemma mod_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a mod b = snd (posDivAlg a b)"
  1582 by (simp add: mod_int_def divmod_int_def)
  1583 
  1584 text{*a negative, b positive *}
  1585 
  1586 lemma div_neg_pos: "[| a < 0;  0 < b |] ==> a div b = fst (negDivAlg a b)"
  1587 by (simp add: div_int_def divmod_int_def)
  1588 
  1589 lemma mod_neg_pos: "[| a < 0;  0 < b |] ==> a mod b = snd (negDivAlg a b)"
  1590 by (simp add: mod_int_def divmod_int_def)
  1591 
  1592 text{*a positive, b negative *}
  1593 
  1594 lemma div_pos_neg:
  1595      "[| 0 < a;  b < 0 |] ==> a div b = fst (apsnd uminus (negDivAlg (-a) (-b)))"
  1596 by (simp add: div_int_def divmod_int_def)
  1597 
  1598 lemma mod_pos_neg:
  1599      "[| 0 < a;  b < 0 |] ==> a mod b = snd (apsnd uminus (negDivAlg (-a) (-b)))"
  1600 by (simp add: mod_int_def divmod_int_def)
  1601 
  1602 text{*a negative, b negative *}
  1603 
  1604 lemma div_neg_neg:
  1605      "[| a < 0;  b \<le> 0 |] ==> a div b = fst (apsnd uminus (posDivAlg (-a) (-b)))"
  1606 by (simp add: div_int_def divmod_int_def)
  1607 
  1608 lemma mod_neg_neg:
  1609      "[| a < 0;  b \<le> 0 |] ==> a mod b = snd (apsnd uminus (posDivAlg (-a) (-b)))"
  1610 by (simp add: mod_int_def divmod_int_def)
  1611 
  1612 text {*Simplify expresions in which div and mod combine numerical constants*}
  1613 
  1614 lemma int_div_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a div b = q"
  1615   by (rule div_int_unique [of a b q r]) (simp add: divmod_int_rel_def)
  1616 
  1617 lemma int_div_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a div b = q"
  1618   by (rule div_int_unique [of a b q r],
  1619     simp add: divmod_int_rel_def)
  1620 
  1621 lemma int_mod_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a mod b = r"
  1622   by (rule mod_int_unique [of a b q r],
  1623     simp add: divmod_int_rel_def)
  1624 
  1625 lemma int_mod_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a mod b = r"
  1626   by (rule mod_int_unique [of a b q r],
  1627     simp add: divmod_int_rel_def)
  1628 
  1629 (* simprocs adapted from HOL/ex/Binary.thy *)
  1630 ML {*
  1631 local
  1632   val mk_number = HOLogic.mk_number HOLogic.intT
  1633   val plus = @{term "plus :: int \<Rightarrow> int \<Rightarrow> int"}
  1634   val times = @{term "times :: int \<Rightarrow> int \<Rightarrow> int"}
  1635   val zero = @{term "0 :: int"}
  1636   val less = @{term "op < :: int \<Rightarrow> int \<Rightarrow> bool"}
  1637   val le = @{term "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"}
  1638   val simps = @{thms arith_simps} @ @{thms rel_simps} @
  1639     map (fn th => th RS sym) [@{thm numeral_1_eq_1}]
  1640   fun prove ctxt goal = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop goal)
  1641     (K (ALLGOALS (full_simp_tac (HOL_basic_ss addsimps simps))));
  1642   fun binary_proc proc ss ct =
  1643     (case Thm.term_of ct of
  1644       _ $ t $ u =>
  1645       (case try (pairself (`(snd o HOLogic.dest_number))) (t, u) of
  1646         SOME args => proc (Simplifier.the_context ss) args
  1647       | NONE => NONE)
  1648     | _ => NONE);
  1649 in
  1650   fun divmod_proc posrule negrule =
  1651     binary_proc (fn ctxt => fn ((a, t), (b, u)) =>
  1652       if b = 0 then NONE else let
  1653         val (q, r) = pairself mk_number (Integer.div_mod a b)
  1654         val goal1 = HOLogic.mk_eq (t, plus $ (times $ u $ q) $ r)
  1655         val (goal2, goal3, rule) = if b > 0
  1656           then (le $ zero $ r, less $ r $ u, posrule RS eq_reflection)
  1657           else (le $ r $ zero, less $ u $ r, negrule RS eq_reflection)
  1658       in SOME (rule OF map (prove ctxt) [goal1, goal2, goal3]) end)
  1659 end
  1660 *}
  1661 
  1662 simproc_setup binary_int_div
  1663   ("numeral m div numeral n :: int" |
  1664    "numeral m div neg_numeral n :: int" |
  1665    "neg_numeral m div numeral n :: int" |
  1666    "neg_numeral m div neg_numeral n :: int") =
  1667   {* K (divmod_proc @{thm int_div_pos_eq} @{thm int_div_neg_eq}) *}
  1668 
  1669 simproc_setup binary_int_mod
  1670   ("numeral m mod numeral n :: int" |
  1671    "numeral m mod neg_numeral n :: int" |
  1672    "neg_numeral m mod numeral n :: int" |
  1673    "neg_numeral m mod neg_numeral n :: int") =
  1674   {* K (divmod_proc @{thm int_mod_pos_eq} @{thm int_mod_neg_eq}) *}
  1675 
  1676 lemmas posDivAlg_eqn_numeral [simp] =
  1677     posDivAlg_eqn [of "numeral v" "numeral w", OF zero_less_numeral] for v w
  1678 
  1679 lemmas negDivAlg_eqn_numeral [simp] =
  1680     negDivAlg_eqn [of "numeral v" "neg_numeral w", OF zero_less_numeral] for v w
  1681 
  1682 
  1683 text{*Special-case simplification *}
  1684 
  1685 (** The last remaining special cases for constant arithmetic:
  1686     1 div z and 1 mod z **)
  1687 
  1688 lemmas div_pos_pos_1_numeral [simp] =
  1689   div_pos_pos [OF zero_less_one, of "numeral w", OF zero_le_numeral] for w
  1690 
  1691 lemmas div_pos_neg_1_numeral [simp] =
  1692   div_pos_neg [OF zero_less_one, of "neg_numeral w",
  1693   OF neg_numeral_less_zero] for w
  1694 
  1695 lemmas mod_pos_pos_1_numeral [simp] =
  1696   mod_pos_pos [OF zero_less_one, of "numeral w", OF zero_le_numeral] for w
  1697 
  1698 lemmas mod_pos_neg_1_numeral [simp] =
  1699   mod_pos_neg [OF zero_less_one, of "neg_numeral w",
  1700   OF neg_numeral_less_zero] for w
  1701 
  1702 lemmas posDivAlg_eqn_1_numeral [simp] =
  1703     posDivAlg_eqn [of concl: 1 "numeral w", OF zero_less_numeral] for w
  1704 
  1705 lemmas negDivAlg_eqn_1_numeral [simp] =
  1706     negDivAlg_eqn [of concl: 1 "numeral w", OF zero_less_numeral] for w
  1707 
  1708 
  1709 subsubsection {* Monotonicity in the First Argument (Dividend) *}
  1710 
  1711 lemma zdiv_mono1: "[| a \<le> a';  0 < (b::int) |] ==> a div b \<le> a' div b"
  1712 apply (cut_tac a = a and b = b in zmod_zdiv_equality)
  1713 apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
  1714 apply (rule unique_quotient_lemma)
  1715 apply (erule subst)
  1716 apply (erule subst, simp_all)
  1717 done
  1718 
  1719 lemma zdiv_mono1_neg: "[| a \<le> a';  (b::int) < 0 |] ==> a' div b \<le> a div b"
  1720 apply (cut_tac a = a and b = b in zmod_zdiv_equality)
  1721 apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
  1722 apply (rule unique_quotient_lemma_neg)
  1723 apply (erule subst)
  1724 apply (erule subst, simp_all)
  1725 done
  1726 
  1727 
  1728 subsubsection {* Monotonicity in the Second Argument (Divisor) *}
  1729 
  1730 lemma q_pos_lemma:
  1731      "[| 0 \<le> b'*q' + r'; r' < b';  0 < b' |] ==> 0 \<le> (q'::int)"
  1732 apply (subgoal_tac "0 < b'* (q' + 1) ")
  1733  apply (simp add: zero_less_mult_iff)
  1734 apply (simp add: right_distrib)
  1735 done
  1736 
  1737 lemma zdiv_mono2_lemma:
  1738      "[| b*q + r = b'*q' + r';  0 \<le> b'*q' + r';   
  1739          r' < b';  0 \<le> r;  0 < b';  b' \<le> b |]   
  1740       ==> q \<le> (q'::int)"
  1741 apply (frule q_pos_lemma, assumption+) 
  1742 apply (subgoal_tac "b*q < b* (q' + 1) ")
  1743  apply (simp add: mult_less_cancel_left)
  1744 apply (subgoal_tac "b*q = r' - r + b'*q'")
  1745  prefer 2 apply simp
  1746 apply (simp (no_asm_simp) add: right_distrib)
  1747 apply (subst add_commute, rule add_less_le_mono, arith)
  1748 apply (rule mult_right_mono, auto)
  1749 done
  1750 
  1751 lemma zdiv_mono2:
  1752      "[| (0::int) \<le> a;  0 < b';  b' \<le> b |] ==> a div b \<le> a div b'"
  1753 apply (subgoal_tac "b \<noteq> 0")
  1754  prefer 2 apply arith
  1755 apply (cut_tac a = a and b = b in zmod_zdiv_equality)
  1756 apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
  1757 apply (rule zdiv_mono2_lemma)
  1758 apply (erule subst)
  1759 apply (erule subst, simp_all)
  1760 done
  1761 
  1762 lemma q_neg_lemma:
  1763      "[| b'*q' + r' < 0;  0 \<le> r';  0 < b' |] ==> q' \<le> (0::int)"
  1764 apply (subgoal_tac "b'*q' < 0")
  1765  apply (simp add: mult_less_0_iff, arith)
  1766 done
  1767 
  1768 lemma zdiv_mono2_neg_lemma:
  1769      "[| b*q + r = b'*q' + r';  b'*q' + r' < 0;   
  1770          r < b;  0 \<le> r';  0 < b';  b' \<le> b |]   
  1771       ==> q' \<le> (q::int)"
  1772 apply (frule q_neg_lemma, assumption+) 
  1773 apply (subgoal_tac "b*q' < b* (q + 1) ")
  1774  apply (simp add: mult_less_cancel_left)
  1775 apply (simp add: right_distrib)
  1776 apply (subgoal_tac "b*q' \<le> b'*q'")
  1777  prefer 2 apply (simp add: mult_right_mono_neg, arith)
  1778 done
  1779 
  1780 lemma zdiv_mono2_neg:
  1781      "[| a < (0::int);  0 < b';  b' \<le> b |] ==> a div b' \<le> a div b"
  1782 apply (cut_tac a = a and b = b in zmod_zdiv_equality)
  1783 apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
  1784 apply (rule zdiv_mono2_neg_lemma)
  1785 apply (erule subst)
  1786 apply (erule subst, simp_all)
  1787 done
  1788 
  1789 
  1790 subsubsection {* More Algebraic Laws for div and mod *}
  1791 
  1792 text{*proving (a*b) div c = a * (b div c) + a * (b mod c) *}
  1793 
  1794 lemma zmult1_lemma:
  1795      "[| divmod_int_rel b c (q, r) |]  
  1796       ==> divmod_int_rel (a * b) c (a*q + a*r div c, a*r mod c)"
  1797 by (auto simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_distrib mult_ac)
  1798 
  1799 lemma zdiv_zmult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)"
  1800 apply (case_tac "c = 0", simp)
  1801 apply (blast intro: divmod_int_rel_div_mod [THEN zmult1_lemma, THEN div_int_unique])
  1802 done
  1803 
  1804 text{*proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) *}
  1805 
  1806 lemma zadd1_lemma:
  1807      "[| divmod_int_rel a c (aq, ar);  divmod_int_rel b c (bq, br) |]  
  1808       ==> divmod_int_rel (a+b) c (aq + bq + (ar+br) div c, (ar+br) mod c)"
  1809 by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_distrib)
  1810 
  1811 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
  1812 lemma zdiv_zadd1_eq:
  1813      "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)"
  1814 apply (case_tac "c = 0", simp)
  1815 apply (blast intro: zadd1_lemma [OF divmod_int_rel_div_mod divmod_int_rel_div_mod] div_int_unique)
  1816 done
  1817 
  1818 lemma posDivAlg_div_mod:
  1819   assumes "k \<ge> 0"
  1820   and "l \<ge> 0"
  1821   shows "posDivAlg k l = (k div l, k mod l)"
  1822 proof (cases "l = 0")
  1823   case True then show ?thesis by (simp add: posDivAlg.simps)
  1824 next
  1825   case False with assms posDivAlg_correct
  1826     have "divmod_int_rel k l (fst (posDivAlg k l), snd (posDivAlg k l))"
  1827     by simp
  1828   from div_int_unique [OF this] mod_int_unique [OF this]
  1829   show ?thesis by simp
  1830 qed
  1831 
  1832 lemma negDivAlg_div_mod:
  1833   assumes "k < 0"
  1834   and "l > 0"
  1835   shows "negDivAlg k l = (k div l, k mod l)"
  1836 proof -
  1837   from assms have "l \<noteq> 0" by simp
  1838   from assms negDivAlg_correct
  1839     have "divmod_int_rel k l (fst (negDivAlg k l), snd (negDivAlg k l))"
  1840     by simp
  1841   from div_int_unique [OF this] mod_int_unique [OF this]
  1842   show ?thesis by simp
  1843 qed
  1844 
  1845 lemma zmod_eq_0_iff: "(m mod d = 0) = (EX q::int. m = d*q)"
  1846 by (simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
  1847 
  1848 (* REVISIT: should this be generalized to all semiring_div types? *)
  1849 lemmas zmod_eq_0D [dest!] = zmod_eq_0_iff [THEN iffD1]
  1850 
  1851 lemma zmod_zdiv_equality':
  1852   "(m\<Colon>int) mod n = m - (m div n) * n"
  1853   using mod_div_equality [of m n] by arith
  1854 
  1855 
  1856 subsubsection {* Proving  @{term "a div (b*c) = (a div b) div c"} *}
  1857 
  1858 (*The condition c>0 seems necessary.  Consider that 7 div ~6 = ~2 but
  1859   7 div 2 div ~3 = 3 div ~3 = ~1.  The subcase (a div b) mod c = 0 seems
  1860   to cause particular problems.*)
  1861 
  1862 text{*first, four lemmas to bound the remainder for the cases b<0 and b>0 *}
  1863 
  1864 lemma zmult2_lemma_aux1: "[| (0::int) < c;  b < r;  r \<le> 0 |] ==> b*c < b*(q mod c) + r"
  1865 apply (subgoal_tac "b * (c - q mod c) < r * 1")
  1866  apply (simp add: algebra_simps)
  1867 apply (rule order_le_less_trans)
  1868  apply (erule_tac [2] mult_strict_right_mono)
  1869  apply (rule mult_left_mono_neg)
  1870   using add1_zle_eq[of "q mod c"]apply(simp add: algebra_simps)
  1871  apply (simp)
  1872 apply (simp)
  1873 done
  1874 
  1875 lemma zmult2_lemma_aux2:
  1876      "[| (0::int) < c;   b < r;  r \<le> 0 |] ==> b * (q mod c) + r \<le> 0"
  1877 apply (subgoal_tac "b * (q mod c) \<le> 0")
  1878  apply arith
  1879 apply (simp add: mult_le_0_iff)
  1880 done
  1881 
  1882 lemma zmult2_lemma_aux3: "[| (0::int) < c;  0 \<le> r;  r < b |] ==> 0 \<le> b * (q mod c) + r"
  1883 apply (subgoal_tac "0 \<le> b * (q mod c) ")
  1884 apply arith
  1885 apply (simp add: zero_le_mult_iff)
  1886 done
  1887 
  1888 lemma zmult2_lemma_aux4: "[| (0::int) < c; 0 \<le> r; r < b |] ==> b * (q mod c) + r < b * c"
  1889 apply (subgoal_tac "r * 1 < b * (c - q mod c) ")
  1890  apply (simp add: right_diff_distrib)
  1891 apply (rule order_less_le_trans)
  1892  apply (erule mult_strict_right_mono)
  1893  apply (rule_tac [2] mult_left_mono)
  1894   apply simp
  1895  using add1_zle_eq[of "q mod c"] apply (simp add: algebra_simps)
  1896 apply simp
  1897 done
  1898 
  1899 lemma zmult2_lemma: "[| divmod_int_rel a b (q, r); 0 < c |]  
  1900       ==> divmod_int_rel a (b * c) (q div c, b*(q mod c) + r)"
  1901 by (auto simp add: mult_ac divmod_int_rel_def linorder_neq_iff
  1902                    zero_less_mult_iff right_distrib [symmetric] 
  1903                    zmult2_lemma_aux1 zmult2_lemma_aux2 zmult2_lemma_aux3 zmult2_lemma_aux4 mult_less_0_iff split: split_if_asm)
  1904 
  1905 lemma zdiv_zmult2_eq: "(0::int) < c ==> a div (b*c) = (a div b) div c"
  1906 apply (case_tac "b = 0", simp)
  1907 apply (force simp add: divmod_int_rel_div_mod [THEN zmult2_lemma, THEN div_int_unique])
  1908 done
  1909 
  1910 lemma zmod_zmult2_eq:
  1911      "(0::int) < c ==> a mod (b*c) = b*(a div b mod c) + a mod b"
  1912 apply (case_tac "b = 0", simp)
  1913 apply (force simp add: divmod_int_rel_div_mod [THEN zmult2_lemma, THEN mod_int_unique])
  1914 done
  1915 
  1916 lemma div_pos_geq:
  1917   fixes k l :: int
  1918   assumes "0 < l" and "l \<le> k"
  1919   shows "k div l = (k - l) div l + 1"
  1920 proof -
  1921   have "k = (k - l) + l" by simp
  1922   then obtain j where k: "k = j + l" ..
  1923   with assms show ?thesis by simp
  1924 qed
  1925 
  1926 lemma mod_pos_geq:
  1927   fixes k l :: int
  1928   assumes "0 < l" and "l \<le> k"
  1929   shows "k mod l = (k - l) mod l"
  1930 proof -
  1931   have "k = (k - l) + l" by simp
  1932   then obtain j where k: "k = j + l" ..
  1933   with assms show ?thesis by simp
  1934 qed
  1935 
  1936 
  1937 subsubsection {* Splitting Rules for div and mod *}
  1938 
  1939 text{*The proofs of the two lemmas below are essentially identical*}
  1940 
  1941 lemma split_pos_lemma:
  1942  "0<k ==> 
  1943     P(n div k :: int)(n mod k) = (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i j)"
  1944 apply (rule iffI, clarify)
  1945  apply (erule_tac P="P ?x ?y" in rev_mp)  
  1946  apply (subst mod_add_eq) 
  1947  apply (subst zdiv_zadd1_eq) 
  1948  apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial)  
  1949 txt{*converse direction*}
  1950 apply (drule_tac x = "n div k" in spec) 
  1951 apply (drule_tac x = "n mod k" in spec, simp)
  1952 done
  1953 
  1954 lemma split_neg_lemma:
  1955  "k<0 ==>
  1956     P(n div k :: int)(n mod k) = (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i j)"
  1957 apply (rule iffI, clarify)
  1958  apply (erule_tac P="P ?x ?y" in rev_mp)  
  1959  apply (subst mod_add_eq) 
  1960  apply (subst zdiv_zadd1_eq) 
  1961  apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial)  
  1962 txt{*converse direction*}
  1963 apply (drule_tac x = "n div k" in spec) 
  1964 apply (drule_tac x = "n mod k" in spec, simp)
  1965 done
  1966 
  1967 lemma split_zdiv:
  1968  "P(n div k :: int) =
  1969   ((k = 0 --> P 0) & 
  1970    (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i)) & 
  1971    (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i)))"
  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 x"] 
  1976                       split_neg_lemma [of concl: "%x y. P x"])
  1977 done
  1978 
  1979 lemma split_zmod:
  1980  "P(n mod k :: int) =
  1981   ((k = 0 --> P n) & 
  1982    (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P j)) & 
  1983    (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P j)))"
  1984 apply (case_tac "k=0", simp)
  1985 apply (simp only: linorder_neq_iff)
  1986 apply (erule disjE) 
  1987  apply (simp_all add: split_pos_lemma [of concl: "%x y. P y"] 
  1988                       split_neg_lemma [of concl: "%x y. P y"])
  1989 done
  1990 
  1991 text {* Enable (lin)arith to deal with @{const div} and @{const mod}
  1992   when these are applied to some constant that is of the form
  1993   @{term "numeral k"}: *}
  1994 declare split_zdiv [of _ _ "numeral k", arith_split] for k
  1995 declare split_zmod [of _ _ "numeral k", arith_split] for k
  1996 
  1997 
  1998 subsubsection {* Computing @{text "div"} and @{text "mod"} with shifting *}
  1999 
  2000 lemma pos_divmod_int_rel_mult_2:
  2001   assumes "0 \<le> b"
  2002   assumes "divmod_int_rel a b (q, r)"
  2003   shows "divmod_int_rel (1 + 2*a) (2*b) (q, 1 + 2*r)"
  2004   using assms unfolding divmod_int_rel_def by auto
  2005 
  2006 lemma neg_divmod_int_rel_mult_2:
  2007   assumes "b \<le> 0"
  2008   assumes "divmod_int_rel (a + 1) b (q, r)"
  2009   shows "divmod_int_rel (1 + 2*a) (2*b) (q, 2*r - 1)"
  2010   using assms unfolding divmod_int_rel_def by auto
  2011 
  2012 text{*computing div by shifting *}
  2013 
  2014 lemma pos_zdiv_mult_2: "(0::int) \<le> a ==> (1 + 2*b) div (2*a) = b div a"
  2015   using pos_divmod_int_rel_mult_2 [OF _ divmod_int_rel_div_mod]
  2016   by (rule div_int_unique)
  2017 
  2018 lemma neg_zdiv_mult_2: 
  2019   assumes A: "a \<le> (0::int)" shows "(1 + 2*b) div (2*a) = (b+1) div a"
  2020   using neg_divmod_int_rel_mult_2 [OF A divmod_int_rel_div_mod]
  2021   by (rule div_int_unique)
  2022 
  2023 (* FIXME: add rules for negative numerals *)
  2024 lemma zdiv_numeral_Bit0 [simp]:
  2025   "numeral (Num.Bit0 v) div numeral (Num.Bit0 w) =
  2026     numeral v div (numeral w :: int)"
  2027   unfolding numeral.simps unfolding mult_2 [symmetric]
  2028   by (rule div_mult_mult1, simp)
  2029 
  2030 lemma zdiv_numeral_Bit1 [simp]:
  2031   "numeral (Num.Bit1 v) div numeral (Num.Bit0 w) =  
  2032     (numeral v div (numeral w :: int))"
  2033   unfolding numeral.simps
  2034   unfolding mult_2 [symmetric] add_commute [of _ 1]
  2035   by (rule pos_zdiv_mult_2, simp)
  2036 
  2037 lemma pos_zmod_mult_2:
  2038   fixes a b :: int
  2039   assumes "0 \<le> a"
  2040   shows "(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)"
  2041   using pos_divmod_int_rel_mult_2 [OF assms divmod_int_rel_div_mod]
  2042   by (rule mod_int_unique)
  2043 
  2044 lemma neg_zmod_mult_2:
  2045   fixes a b :: int
  2046   assumes "a \<le> 0"
  2047   shows "(1 + 2 * b) mod (2 * a) = 2 * ((b + 1) mod a) - 1"
  2048   using neg_divmod_int_rel_mult_2 [OF assms divmod_int_rel_div_mod]
  2049   by (rule mod_int_unique)
  2050 
  2051 (* FIXME: add rules for negative numerals *)
  2052 lemma zmod_numeral_Bit0 [simp]:
  2053   "numeral (Num.Bit0 v) mod numeral (Num.Bit0 w) =  
  2054     (2::int) * (numeral v mod numeral w)"
  2055   unfolding numeral_Bit0 [of v] numeral_Bit0 [of w]
  2056   unfolding mult_2 [symmetric] by (rule mod_mult_mult1)
  2057 
  2058 lemma zmod_numeral_Bit1 [simp]:
  2059   "numeral (Num.Bit1 v) mod numeral (Num.Bit0 w) =
  2060     2 * (numeral v mod numeral w) + (1::int)"
  2061   unfolding numeral_Bit1 [of v] numeral_Bit0 [of w]
  2062   unfolding mult_2 [symmetric] add_commute [of _ 1]
  2063   by (rule pos_zmod_mult_2, simp)
  2064 
  2065 lemma zdiv_eq_0_iff:
  2066  "(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")
  2067 proof
  2068   assume ?L
  2069   have "?L \<longrightarrow> ?R" by (rule split_zdiv[THEN iffD2]) simp
  2070   with `?L` show ?R by blast
  2071 next
  2072   assume ?R thus ?L
  2073     by(auto simp: div_pos_pos_trivial div_neg_neg_trivial)
  2074 qed
  2075 
  2076 
  2077 subsubsection {* Quotients of Signs *}
  2078 
  2079 lemma div_neg_pos_less0: "[| a < (0::int);  0 < b |] ==> a div b < 0"
  2080 apply (subgoal_tac "a div b \<le> -1", force)
  2081 apply (rule order_trans)
  2082 apply (rule_tac a' = "-1" in zdiv_mono1)
  2083 apply (auto simp add: div_eq_minus1)
  2084 done
  2085 
  2086 lemma div_nonneg_neg_le0: "[| (0::int) \<le> a; b < 0 |] ==> a div b \<le> 0"
  2087 by (drule zdiv_mono1_neg, auto)
  2088 
  2089 lemma div_nonpos_pos_le0: "[| (a::int) \<le> 0; b > 0 |] ==> a div b \<le> 0"
  2090 by (drule zdiv_mono1, auto)
  2091 
  2092 text{* Now for some equivalences of the form @{text"a div b >=< 0 \<longleftrightarrow> \<dots>"}
  2093 conditional upon the sign of @{text a} or @{text b}. There are many more.
  2094 They should all be simp rules unless that causes too much search. *}
  2095 
  2096 lemma pos_imp_zdiv_nonneg_iff: "(0::int) < b ==> (0 \<le> a div b) = (0 \<le> a)"
  2097 apply auto
  2098 apply (drule_tac [2] zdiv_mono1)
  2099 apply (auto simp add: linorder_neq_iff)
  2100 apply (simp (no_asm_use) add: linorder_not_less [symmetric])
  2101 apply (blast intro: div_neg_pos_less0)
  2102 done
  2103 
  2104 lemma neg_imp_zdiv_nonneg_iff:
  2105   "b < (0::int) ==> (0 \<le> a div b) = (a \<le> (0::int))"
  2106 apply (subst div_minus_minus [symmetric])
  2107 apply (subst pos_imp_zdiv_nonneg_iff, auto)
  2108 done
  2109 
  2110 (*But not (a div b \<le> 0 iff a\<le>0); consider a=1, b=2 when a div b = 0.*)
  2111 lemma pos_imp_zdiv_neg_iff: "(0::int) < b ==> (a div b < 0) = (a < 0)"
  2112 by (simp add: linorder_not_le [symmetric] pos_imp_zdiv_nonneg_iff)
  2113 
  2114 lemma pos_imp_zdiv_pos_iff:
  2115   "0<k \<Longrightarrow> 0 < (i::int) div k \<longleftrightarrow> k \<le> i"
  2116 using pos_imp_zdiv_nonneg_iff[of k i] zdiv_eq_0_iff[of i k]
  2117 by arith
  2118 
  2119 (*Again the law fails for \<le>: consider a = -1, b = -2 when a div b = 0*)
  2120 lemma neg_imp_zdiv_neg_iff: "b < (0::int) ==> (a div b < 0) = (0 < a)"
  2121 by (simp add: linorder_not_le [symmetric] neg_imp_zdiv_nonneg_iff)
  2122 
  2123 lemma nonneg1_imp_zdiv_pos_iff:
  2124   "(0::int) <= a \<Longrightarrow> (a div b > 0) = (a >= b & b>0)"
  2125 apply rule
  2126  apply rule
  2127   using div_pos_pos_trivial[of a b]apply arith
  2128  apply(cases "b=0")apply simp
  2129  using div_nonneg_neg_le0[of a b]apply arith
  2130 using int_one_le_iff_zero_less[of "a div b"] zdiv_mono1[of b a b]apply simp
  2131 done
  2132 
  2133 lemma zmod_le_nonneg_dividend: "(m::int) \<ge> 0 ==> m mod k \<le> m"
  2134 apply (rule split_zmod[THEN iffD2])
  2135 apply(fastforce dest: q_pos_lemma intro: split_mult_pos_le)
  2136 done
  2137 
  2138 
  2139 subsubsection {* The Divides Relation *}
  2140 
  2141 lemma dvd_neg_numeral_left [simp]:
  2142   fixes y :: "'a::comm_ring_1"
  2143   shows "(neg_numeral k) dvd y \<longleftrightarrow> (numeral k) dvd y"
  2144   unfolding neg_numeral_def minus_dvd_iff ..
  2145 
  2146 lemma dvd_neg_numeral_right [simp]:
  2147   fixes x :: "'a::comm_ring_1"
  2148   shows "x dvd (neg_numeral k) \<longleftrightarrow> x dvd (numeral k)"
  2149   unfolding neg_numeral_def dvd_minus_iff ..
  2150 
  2151 lemmas dvd_eq_mod_eq_0_numeral [simp] =
  2152   dvd_eq_mod_eq_0 [of "numeral x" "numeral y"] for x y
  2153 
  2154 
  2155 subsubsection {* Further properties *}
  2156 
  2157 lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)"
  2158   using zmod_zdiv_equality[where a="m" and b="n"]
  2159   by (simp add: algebra_simps) (* FIXME: generalize *)
  2160 
  2161 lemma zdiv_int: "int (a div b) = (int a) div (int b)"
  2162 apply (subst split_div, auto)
  2163 apply (subst split_zdiv, auto)
  2164 apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in unique_quotient)
  2165 apply (auto simp add: divmod_int_rel_def of_nat_mult)
  2166 done
  2167 
  2168 lemma zmod_int: "int (a mod b) = (int a) mod (int b)"
  2169 apply (subst split_mod, auto)
  2170 apply (subst split_zmod, auto)
  2171 apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia 
  2172        in unique_remainder)
  2173 apply (auto simp add: divmod_int_rel_def of_nat_mult)
  2174 done
  2175 
  2176 lemma abs_div: "(y::int) dvd x \<Longrightarrow> abs (x div y) = abs x div abs y"
  2177 by (unfold dvd_def, cases "y=0", auto simp add: abs_mult)
  2178 
  2179 text{*Suggested by Matthias Daum*}
  2180 lemma int_power_div_base:
  2181      "\<lbrakk>0 < m; 0 < k\<rbrakk> \<Longrightarrow> k ^ m div k = (k::int) ^ (m - Suc 0)"
  2182 apply (subgoal_tac "k ^ m = k ^ ((m - Suc 0) + Suc 0)")
  2183  apply (erule ssubst)
  2184  apply (simp only: power_add)
  2185  apply simp_all
  2186 done
  2187 
  2188 text {* by Brian Huffman *}
  2189 lemma zminus_zmod: "- ((x::int) mod m) mod m = - x mod m"
  2190 by (rule mod_minus_eq [symmetric])
  2191 
  2192 lemma zdiff_zmod_left: "(x mod m - y) mod m = (x - y) mod (m::int)"
  2193 by (rule mod_diff_left_eq [symmetric])
  2194 
  2195 lemma zdiff_zmod_right: "(x - y mod m) mod m = (x - y) mod (m::int)"
  2196 by (rule mod_diff_right_eq [symmetric])
  2197 
  2198 lemmas zmod_simps =
  2199   mod_add_left_eq  [symmetric]
  2200   mod_add_right_eq [symmetric]
  2201   mod_mult_right_eq[symmetric]
  2202   mod_mult_left_eq [symmetric]
  2203   power_mod
  2204   zminus_zmod zdiff_zmod_left zdiff_zmod_right
  2205 
  2206 text {* Distributive laws for function @{text nat}. *}
  2207 
  2208 lemma nat_div_distrib: "0 \<le> x \<Longrightarrow> nat (x div y) = nat x div nat y"
  2209 apply (rule linorder_cases [of y 0])
  2210 apply (simp add: div_nonneg_neg_le0)
  2211 apply simp
  2212 apply (simp add: nat_eq_iff pos_imp_zdiv_nonneg_iff zdiv_int)
  2213 done
  2214 
  2215 (*Fails if y<0: the LHS collapses to (nat z) but the RHS doesn't*)
  2216 lemma nat_mod_distrib:
  2217   "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> nat (x mod y) = nat x mod nat y"
  2218 apply (case_tac "y = 0", simp)
  2219 apply (simp add: nat_eq_iff zmod_int)
  2220 done
  2221 
  2222 text  {* transfer setup *}
  2223 
  2224 lemma transfer_nat_int_functions:
  2225     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) div (nat y) = nat (x div y)"
  2226     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) mod (nat y) = nat (x mod y)"
  2227   by (auto simp add: nat_div_distrib nat_mod_distrib)
  2228 
  2229 lemma transfer_nat_int_function_closures:
  2230     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x div y >= 0"
  2231     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x mod y >= 0"
  2232   apply (cases "y = 0")
  2233   apply (auto simp add: pos_imp_zdiv_nonneg_iff)
  2234   apply (cases "y = 0")
  2235   apply auto
  2236 done
  2237 
  2238 declare transfer_morphism_nat_int [transfer add return:
  2239   transfer_nat_int_functions
  2240   transfer_nat_int_function_closures
  2241 ]
  2242 
  2243 lemma transfer_int_nat_functions:
  2244     "(int x) div (int y) = int (x div y)"
  2245     "(int x) mod (int y) = int (x mod y)"
  2246   by (auto simp add: zdiv_int zmod_int)
  2247 
  2248 lemma transfer_int_nat_function_closures:
  2249     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x div y)"
  2250     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x mod y)"
  2251   by (simp_all only: is_nat_def transfer_nat_int_function_closures)
  2252 
  2253 declare transfer_morphism_int_nat [transfer add return:
  2254   transfer_int_nat_functions
  2255   transfer_int_nat_function_closures
  2256 ]
  2257 
  2258 text{*Suggested by Matthias Daum*}
  2259 lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"
  2260 apply (subgoal_tac "nat x div nat k < nat x")
  2261  apply (simp add: nat_div_distrib [symmetric])
  2262 apply (rule Divides.div_less_dividend, simp_all)
  2263 done
  2264 
  2265 lemma zmod_eq_dvd_iff: "(x::int) mod n = y mod n \<longleftrightarrow> n dvd x - y"
  2266 proof
  2267   assume H: "x mod n = y mod n"
  2268   hence "x mod n - y mod n = 0" by simp
  2269   hence "(x mod n - y mod n) mod n = 0" by simp 
  2270   hence "(x - y) mod n = 0" by (simp add: mod_diff_eq[symmetric])
  2271   thus "n dvd x - y" by (simp add: dvd_eq_mod_eq_0)
  2272 next
  2273   assume H: "n dvd x - y"
  2274   then obtain k where k: "x-y = n*k" unfolding dvd_def by blast
  2275   hence "x = n*k + y" by simp
  2276   hence "x mod n = (n*k + y) mod n" by simp
  2277   thus "x mod n = y mod n" by (simp add: mod_add_left_eq)
  2278 qed
  2279 
  2280 lemma nat_mod_eq_lemma: assumes xyn: "(x::nat) mod n = y  mod n" and xy:"y \<le> x"
  2281   shows "\<exists>q. x = y + n * q"
  2282 proof-
  2283   from xy have th: "int x - int y = int (x - y)" by simp 
  2284   from xyn have "int x mod int n = int y mod int n" 
  2285     by (simp add: zmod_int [symmetric])
  2286   hence "int n dvd int x - int y" by (simp only: zmod_eq_dvd_iff[symmetric]) 
  2287   hence "n dvd x - y" by (simp add: th zdvd_int)
  2288   then show ?thesis using xy unfolding dvd_def apply clarsimp apply (rule_tac x="k" in exI) by arith
  2289 qed
  2290 
  2291 lemma nat_mod_eq_iff: "(x::nat) mod n = y mod n \<longleftrightarrow> (\<exists>q1 q2. x + n * q1 = y + n * q2)" 
  2292   (is "?lhs = ?rhs")
  2293 proof
  2294   assume H: "x mod n = y mod n"
  2295   {assume xy: "x \<le> y"
  2296     from H have th: "y mod n = x mod n" by simp
  2297     from nat_mod_eq_lemma[OF th xy] have ?rhs 
  2298       apply clarify  apply (rule_tac x="q" in exI) by (rule exI[where x="0"], simp)}
  2299   moreover
  2300   {assume xy: "y \<le> x"
  2301     from nat_mod_eq_lemma[OF H xy] have ?rhs 
  2302       apply clarify  apply (rule_tac x="0" in exI) by (rule_tac x="q" in exI, simp)}
  2303   ultimately  show ?rhs using linear[of x y] by blast  
  2304 next
  2305   assume ?rhs then obtain q1 q2 where q12: "x + n * q1 = y + n * q2" by blast
  2306   hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp
  2307   thus  ?lhs by simp
  2308 qed
  2309 
  2310 lemma div_nat_numeral [simp]:
  2311   "(numeral v :: nat) div numeral v' = nat (numeral v div numeral v')"
  2312   by (simp add: nat_div_distrib)
  2313 
  2314 lemma one_div_nat_numeral [simp]:
  2315   "Suc 0 div numeral v' = nat (1 div numeral v')"
  2316   by (subst nat_div_distrib, simp_all)
  2317 
  2318 lemma mod_nat_numeral [simp]:
  2319   "(numeral v :: nat) mod numeral v' = nat (numeral v mod numeral v')"
  2320   by (simp add: nat_mod_distrib)
  2321 
  2322 lemma one_mod_nat_numeral [simp]:
  2323   "Suc 0 mod numeral v' = nat (1 mod numeral v')"
  2324   by (subst nat_mod_distrib) simp_all
  2325 
  2326 lemma mod_2_not_eq_zero_eq_one_int:
  2327   fixes k :: int
  2328   shows "k mod 2 \<noteq> 0 \<longleftrightarrow> k mod 2 = 1"
  2329   by auto
  2330 
  2331 
  2332 subsubsection {* Tools setup *}
  2333 
  2334 text {* Nitpick *}
  2335 
  2336 lemmas [nitpick_unfold] = dvd_eq_mod_eq_0 mod_div_equality' zmod_zdiv_equality'
  2337 
  2338 
  2339 subsubsection {* Code generation *}
  2340 
  2341 definition pdivmod :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
  2342   "pdivmod k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
  2343 
  2344 lemma pdivmod_posDivAlg [code]:
  2345   "pdivmod k l = (if l = 0 then (0, \<bar>k\<bar>) else posDivAlg \<bar>k\<bar> \<bar>l\<bar>)"
  2346 by (subst posDivAlg_div_mod) (simp_all add: pdivmod_def)
  2347 
  2348 lemma divmod_int_pdivmod: "divmod_int k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else
  2349   apsnd ((op *) (sgn l)) (if 0 < l \<and> 0 \<le> k \<or> l < 0 \<and> k < 0
  2350     then pdivmod k l
  2351     else (let (r, s) = pdivmod k l in
  2352        if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
  2353 proof -
  2354   have aux: "\<And>q::int. - k = l * q \<longleftrightarrow> k = l * - q" by auto
  2355   show ?thesis
  2356     by (simp add: divmod_int_mod_div pdivmod_def)
  2357       (auto simp add: aux not_less not_le zdiv_zminus1_eq_if
  2358       zmod_zminus1_eq_if zdiv_zminus2_eq_if zmod_zminus2_eq_if)
  2359 qed
  2360 
  2361 lemma divmod_int_code [code]: "divmod_int k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else
  2362   apsnd ((op *) (sgn l)) (if sgn k = sgn l
  2363     then pdivmod k l
  2364     else (let (r, s) = pdivmod k l in
  2365       if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
  2366 proof -
  2367   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"
  2368     by (auto simp add: not_less sgn_if)
  2369   then show ?thesis by (simp add: divmod_int_pdivmod)
  2370 qed
  2371 
  2372 code_modulename SML
  2373   Divides Arith
  2374 
  2375 code_modulename OCaml
  2376   Divides Arith
  2377 
  2378 code_modulename Haskell
  2379   Divides Arith
  2380 
  2381 end