src/HOL/Divides.thy
 author paulson Tue Jun 23 16:55:28 2015 +0100 (2015-06-23) changeset 60562 24af00b010cf parent 60517 f16e4fb20652 child 60685 cb21b7022b00 permissions -rw-r--r--
Amalgamation of the class comm_semiring_1_diff_distrib into comm_semiring_1_cancel. Moving axiom le_add_diff_inverse2 from semiring_numeral_div to linordered_semidom.
 paulson@3366  1 (* Title: HOL/Divides.thy  paulson@3366  2  Author: Lawrence C Paulson, Cambridge University Computer Laboratory  paulson@6865  3  Copyright 1999 University of Cambridge  huffman@18154  4 *)  paulson@3366  5 wenzelm@58889  6 section {* The division operators div and mod *}  paulson@3366  7 nipkow@15131  8 theory Divides  haftmann@58778  9 imports Parity  nipkow@15131  10 begin  paulson@3366  11 haftmann@60429  12 subsection {* Abstract division in commutative semirings. *}  haftmann@25942  13 haftmann@60352  14 class div = dvd + divide +  haftmann@60352  15  fixes mod :: "'a \ 'a \ 'a" (infixl "mod" 70)  haftmann@25942  16 haftmann@59833  17 class semiring_div = semidom + div +  haftmann@25942  18  assumes mod_div_equality: "a div b * b + a mod b = a"  haftmann@27651  19  and div_by_0 [simp]: "a div 0 = 0"  haftmann@27651  20  and div_0 [simp]: "0 div a = 0"  haftmann@27651  21  and div_mult_self1 [simp]: "b \ 0 \ (a + c * b) div b = c + a div b"  haftmann@30930  22  and div_mult_mult1 [simp]: "c \ 0 \ (c * a) div (c * b) = a div b"  haftmann@25942  23 begin  haftmann@25942  24 haftmann@60517  25 subclass algebraic_semidom  haftmann@60353  26 proof  haftmann@60353  27  fix b a  haftmann@60353  28  assume "b \ 0"  haftmann@60353  29  then show "a * b div b = a"  haftmann@60353  30  using div_mult_self1 [of b 0 a] by (simp add: ac_simps)  haftmann@60353  31 qed simp  haftmann@58953  32 haftmann@59009  33 lemma power_not_zero: -- \FIXME cf. @{text field_power_not_zero}\  haftmann@59009  34  "a \ 0 \ a ^ n \ 0"  haftmann@59009  35  by (induct n) (simp_all add: no_zero_divisors)  haftmann@59009  36 haftmann@59009  37 lemma semiring_div_power_eq_0_iff: -- \FIXME cf. @{text power_eq_0_iff}, @{text power_eq_0_nat_iff}\  haftmann@59009  38  "n \ 0 \ a ^ n = 0 \ a = 0"  haftmann@59009  39  using power_not_zero [of a n] by (auto simp add: zero_power)  haftmann@59009  40 haftmann@60429  41 text {* @{const divide} and @{const mod} *}  haftmann@26100  42 haftmann@26062  43 lemma mod_div_equality2: "b * (a div b) + a mod b = a"  haftmann@57512  44  unfolding mult.commute [of b]  haftmann@26062  45  by (rule mod_div_equality)  haftmann@26062  46 huffman@29403  47 lemma mod_div_equality': "a mod b + a div b * b = a"  huffman@29403  48  using mod_div_equality [of a b]  haftmann@57514  49  by (simp only: ac_simps)  huffman@29403  50 haftmann@26062  51 lemma div_mod_equality: "((a div b) * b + a mod b) + c = a + c"  haftmann@30934  52  by (simp add: mod_div_equality)  haftmann@26062  53 haftmann@26062  54 lemma div_mod_equality2: "(b * (a div b) + a mod b) + c = a + c"  haftmann@30934  55  by (simp add: mod_div_equality2)  haftmann@26062  56 haftmann@27651  57 lemma mod_by_0 [simp]: "a mod 0 = a"  haftmann@30934  58  using mod_div_equality [of a zero] by simp  haftmann@27651  59 haftmann@27651  60 lemma mod_0 [simp]: "0 mod a = 0"  haftmann@30934  61  using mod_div_equality [of zero a] div_0 by simp  haftmann@27651  62 haftmann@27651  63 lemma div_mult_self2 [simp]:  haftmann@27651  64  assumes "b \ 0"  haftmann@27651  65  shows "(a + b * c) div b = c + a div b"  haftmann@57512  66  using assms div_mult_self1 [of b a c] by (simp add: mult.commute)  haftmann@26100  67 haftmann@54221  68 lemma div_mult_self3 [simp]:  haftmann@54221  69  assumes "b \ 0"  haftmann@54221  70  shows "(c * b + a) div b = c + a div b"  haftmann@54221  71  using assms by (simp add: add.commute)  haftmann@54221  72 haftmann@54221  73 lemma div_mult_self4 [simp]:  haftmann@54221  74  assumes "b \ 0"  haftmann@54221  75  shows "(b * c + a) div b = c + a div b"  haftmann@54221  76  using assms by (simp add: add.commute)  haftmann@54221  77 haftmann@27651  78 lemma mod_mult_self1 [simp]: "(a + c * b) mod b = a mod b"  haftmann@27651  79 proof (cases "b = 0")  haftmann@27651  80  case True then show ?thesis by simp  haftmann@27651  81 next  haftmann@27651  82  case False  haftmann@27651  83  have "a + c * b = (a + c * b) div b * b + (a + c * b) mod b"  haftmann@27651  84  by (simp add: mod_div_equality)  haftmann@27651  85  also from False div_mult_self1 [of b a c] have  haftmann@27651  86  "\ = (c + a div b) * b + (a + c * b) mod b"  nipkow@29667  87  by (simp add: algebra_simps)  haftmann@27651  88  finally have "a = a div b * b + (a + c * b) mod b"  haftmann@57512  89  by (simp add: add.commute [of a] add.assoc distrib_right)  haftmann@27651  90  then have "a div b * b + (a + c * b) mod b = a div b * b + a mod b"  haftmann@27651  91  by (simp add: mod_div_equality)  haftmann@27651  92  then show ?thesis by simp  haftmann@27651  93 qed  haftmann@27651  94 lp15@60562  95 lemma mod_mult_self2 [simp]:  haftmann@54221  96  "(a + b * c) mod b = a mod b"  haftmann@57512  97  by (simp add: mult.commute [of b])  haftmann@27651  98 haftmann@54221  99 lemma mod_mult_self3 [simp]:  haftmann@54221  100  "(c * b + a) mod b = a mod b"  haftmann@54221  101  by (simp add: add.commute)  haftmann@54221  102 haftmann@54221  103 lemma mod_mult_self4 [simp]:  haftmann@54221  104  "(b * c + a) mod b = a mod b"  haftmann@54221  105  by (simp add: add.commute)  haftmann@54221  106 haftmann@60353  107 lemma div_mult_self1_is_id:  haftmann@60353  108  "b \ 0 \ b * a div b = a"  haftmann@60353  109  by (fact nonzero_mult_divide_cancel_left)  haftmann@60353  110 haftmann@60353  111 lemma div_mult_self2_is_id:  haftmann@60353  112  "b \ 0 \ a * b div b = a"  haftmann@60353  113  by (fact nonzero_mult_divide_cancel_right)  haftmann@27651  114 haftmann@27651  115 lemma mod_mult_self1_is_0 [simp]: "b * a mod b = 0"  haftmann@27651  116  using mod_mult_self2 [of 0 b a] by simp  haftmann@27651  117 haftmann@27651  118 lemma mod_mult_self2_is_0 [simp]: "a * b mod b = 0"  haftmann@27651  119  using mod_mult_self1 [of 0 a b] by simp  haftmann@26062  120 haftmann@27651  121 lemma div_by_1 [simp]: "a div 1 = a"  haftmann@27651  122  using div_mult_self2_is_id [of 1 a] zero_neq_one by simp  haftmann@27651  123 haftmann@27651  124 lemma mod_by_1 [simp]: "a mod 1 = 0"  haftmann@27651  125 proof -  haftmann@27651  126  from mod_div_equality [of a one] div_by_1 have "a + a mod 1 = a" by simp  haftmann@27651  127  then have "a + a mod 1 = a + 0" by simp  haftmann@27651  128  then show ?thesis by (rule add_left_imp_eq)  haftmann@27651  129 qed  haftmann@27651  130 haftmann@27651  131 lemma mod_self [simp]: "a mod a = 0"  haftmann@27651  132  using mod_mult_self2_is_0 [of 1] by simp  haftmann@27651  133 haftmann@27676  134 lemma div_add_self1 [simp]:  haftmann@27651  135  assumes "b \ 0"  haftmann@27651  136  shows "(b + a) div b = a div b + 1"  haftmann@57512  137  using assms div_mult_self1 [of b a 1] by (simp add: add.commute)  haftmann@26062  138 haftmann@27676  139 lemma div_add_self2 [simp]:  haftmann@27651  140  assumes "b \ 0"  haftmann@27651  141  shows "(a + b) div b = a div b + 1"  haftmann@57512  142  using assms div_add_self1 [of b a] by (simp add: add.commute)  haftmann@27651  143 haftmann@27676  144 lemma mod_add_self1 [simp]:  haftmann@27651  145  "(b + a) mod b = a mod b"  haftmann@57512  146  using mod_mult_self1 [of a 1 b] by (simp add: add.commute)  haftmann@27651  147 haftmann@27676  148 lemma mod_add_self2 [simp]:  haftmann@27651  149  "(a + b) mod b = a mod b"  haftmann@27651  150  using mod_mult_self1 [of a 1 b] by simp  haftmann@27651  151 haftmann@27651  152 lemma mod_div_decomp:  haftmann@27651  153  fixes a b  haftmann@27651  154  obtains q r where "q = a div b" and "r = a mod b"  haftmann@27651  155  and "a = q * b + r"  haftmann@27651  156 proof -  haftmann@27651  157  from mod_div_equality have "a = a div b * b + a mod b" by simp  haftmann@27651  158  moreover have "a div b = a div b" ..  haftmann@27651  159  moreover have "a mod b = a mod b" ..  haftmann@27651  160  note that ultimately show thesis by blast  haftmann@27651  161 qed  haftmann@27651  162 haftmann@58834  163 lemma dvd_imp_mod_0 [simp]:  haftmann@58834  164  assumes "a dvd b"  haftmann@58834  165  shows "b mod a = 0"  haftmann@58834  166 proof -  haftmann@58834  167  from assms obtain c where "b = a * c" ..  haftmann@58834  168  then have "b mod a = a * c mod a" by simp  haftmann@58834  169  then show "b mod a = 0" by simp  haftmann@58834  170 qed  haftmann@58911  171 haftmann@58911  172 lemma mod_eq_0_iff_dvd:  haftmann@58911  173  "a mod b = 0 \ b dvd a"  haftmann@58911  174 proof  haftmann@58911  175  assume "b dvd a"  haftmann@58911  176  then show "a mod b = 0" by simp  haftmann@58911  177 next  haftmann@58911  178  assume "a mod b = 0"  haftmann@58911  179  with mod_div_equality [of a b] have "a div b * b = a" by simp  haftmann@58911  180  then have "a = b * (a div b)" by (simp add: ac_simps)  haftmann@58911  181  then show "b dvd a" ..  haftmann@58911  182 qed  haftmann@58911  183 haftmann@58834  184 lemma dvd_eq_mod_eq_0 [code]:  haftmann@58834  185  "a dvd b \ b mod a = 0"  haftmann@58911  186  by (simp add: mod_eq_0_iff_dvd)  haftmann@58911  187 haftmann@58911  188 lemma mod_div_trivial [simp]:  haftmann@58911  189  "a mod b div b = 0"  huffman@29403  190 proof (cases "b = 0")  huffman@29403  191  assume "b = 0"  huffman@29403  192  thus ?thesis by simp  huffman@29403  193 next  huffman@29403  194  assume "b \ 0"  huffman@29403  195  hence "a div b + a mod b div b = (a mod b + a div b * b) div b"  huffman@29403  196  by (rule div_mult_self1 [symmetric])  huffman@29403  197  also have "\ = a div b"  huffman@29403  198  by (simp only: mod_div_equality')  huffman@29403  199  also have "\ = a div b + 0"  huffman@29403  200  by simp  huffman@29403  201  finally show ?thesis  huffman@29403  202  by (rule add_left_imp_eq)  huffman@29403  203 qed  huffman@29403  204 haftmann@58911  205 lemma mod_mod_trivial [simp]:  haftmann@58911  206  "a mod b mod b = a mod b"  huffman@29403  207 proof -  huffman@29403  208  have "a mod b mod b = (a mod b + a div b * b) mod b"  huffman@29403  209  by (simp only: mod_mult_self1)  huffman@29403  210  also have "\ = a mod b"  huffman@29403  211  by (simp only: mod_div_equality')  huffman@29403  212  finally show ?thesis .  huffman@29403  213 qed  huffman@29403  214 haftmann@58834  215 lemma div_dvd_div [simp]:  haftmann@58834  216  assumes "a dvd b" and "a dvd c"  haftmann@58834  217  shows "b div a dvd c div a \ b dvd c"  haftmann@58834  218 using assms apply (cases "a = 0")  haftmann@58834  219 apply auto  nipkow@29925  220 apply (unfold dvd_def)  nipkow@29925  221 apply auto  haftmann@57512  222  apply(blast intro:mult.assoc[symmetric])  haftmann@57512  223 apply(fastforce simp add: mult.assoc)  nipkow@29925  224 done  nipkow@29925  225 haftmann@58834  226 lemma dvd_mod_imp_dvd:  haftmann@58834  227  assumes "k dvd m mod n" and "k dvd n"  haftmann@58834  228  shows "k dvd m"  haftmann@58834  229 proof -  haftmann@58834  230  from assms have "k dvd (m div n) * n + m mod n"  haftmann@58834  231  by (simp only: dvd_add dvd_mult)  haftmann@58834  232  then show ?thesis by (simp add: mod_div_equality)  haftmann@58834  233 qed  huffman@30078  234 huffman@29403  235 text {* Addition respects modular equivalence. *}  huffman@29403  236 huffman@29403  237 lemma mod_add_left_eq: "(a + b) mod c = (a mod c + b) mod c"  huffman@29403  238 proof -  huffman@29403  239  have "(a + b) mod c = (a div c * c + a mod c + b) mod c"  huffman@29403  240  by (simp only: mod_div_equality)  huffman@29403  241  also have "\ = (a mod c + b + a div c * c) mod c"  haftmann@57514  242  by (simp only: ac_simps)  huffman@29403  243  also have "\ = (a mod c + b) mod c"  huffman@29403  244  by (rule mod_mult_self1)  huffman@29403  245  finally show ?thesis .  huffman@29403  246 qed  huffman@29403  247 huffman@29403  248 lemma mod_add_right_eq: "(a + b) mod c = (a + b mod c) mod c"  huffman@29403  249 proof -  huffman@29403  250  have "(a + b) mod c = (a + (b div c * c + b mod c)) mod c"  huffman@29403  251  by (simp only: mod_div_equality)  huffman@29403  252  also have "\ = (a + b mod c + b div c * c) mod c"  haftmann@57514  253  by (simp only: ac_simps)  huffman@29403  254  also have "\ = (a + b mod c) mod c"  huffman@29403  255  by (rule mod_mult_self1)  huffman@29403  256  finally show ?thesis .  huffman@29403  257 qed  huffman@29403  258 huffman@29403  259 lemma mod_add_eq: "(a + b) mod c = (a mod c + b mod c) mod c"  huffman@29403  260 by (rule trans [OF mod_add_left_eq mod_add_right_eq])  huffman@29403  261 huffman@29403  262 lemma mod_add_cong:  huffman@29403  263  assumes "a mod c = a' mod c"  huffman@29403  264  assumes "b mod c = b' mod c"  huffman@29403  265  shows "(a + b) mod c = (a' + b') mod c"  huffman@29403  266 proof -  huffman@29403  267  have "(a mod c + b mod c) mod c = (a' mod c + b' mod c) mod c"  huffman@29403  268  unfolding assms ..  huffman@29403  269  thus ?thesis  huffman@29403  270  by (simp only: mod_add_eq [symmetric])  huffman@29403  271 qed  huffman@29403  272 haftmann@30923  273 lemma div_add [simp]: "z dvd x \ z dvd y  nipkow@30837  274  \ (x + y) div z = x div z + y div z"  haftmann@30923  275 by (cases "z = 0", simp, unfold dvd_def, auto simp add: algebra_simps)  nipkow@30837  276 huffman@29403  277 text {* Multiplication respects modular equivalence. *}  huffman@29403  278 huffman@29403  279 lemma mod_mult_left_eq: "(a * b) mod c = ((a mod c) * b) mod c"  huffman@29403  280 proof -  huffman@29403  281  have "(a * b) mod c = ((a div c * c + a mod c) * b) mod c"  huffman@29403  282  by (simp only: mod_div_equality)  huffman@29403  283  also have "\ = (a mod c * b + a div c * b * c) mod c"  nipkow@29667  284  by (simp only: algebra_simps)  huffman@29403  285  also have "\ = (a mod c * b) mod c"  huffman@29403  286  by (rule mod_mult_self1)  huffman@29403  287  finally show ?thesis .  huffman@29403  288 qed  huffman@29403  289 huffman@29403  290 lemma mod_mult_right_eq: "(a * b) mod c = (a * (b mod c)) mod c"  huffman@29403  291 proof -  huffman@29403  292  have "(a * b) mod c = (a * (b div c * c + b mod c)) mod c"  huffman@29403  293  by (simp only: mod_div_equality)  huffman@29403  294  also have "\ = (a * (b mod c) + a * (b div c) * c) mod c"  nipkow@29667  295  by (simp only: algebra_simps)  huffman@29403  296  also have "\ = (a * (b mod c)) mod c"  huffman@29403  297  by (rule mod_mult_self1)  huffman@29403  298  finally show ?thesis .  huffman@29403  299 qed  huffman@29403  300 huffman@29403  301 lemma mod_mult_eq: "(a * b) mod c = ((a mod c) * (b mod c)) mod c"  huffman@29403  302 by (rule trans [OF mod_mult_left_eq mod_mult_right_eq])  huffman@29403  303 huffman@29403  304 lemma mod_mult_cong:  huffman@29403  305  assumes "a mod c = a' mod c"  huffman@29403  306  assumes "b mod c = b' mod c"  huffman@29403  307  shows "(a * b) mod c = (a' * b') mod c"  huffman@29403  308 proof -  huffman@29403  309  have "(a mod c * (b mod c)) mod c = (a' mod c * (b' mod c)) mod c"  huffman@29403  310  unfolding assms ..  huffman@29403  311  thus ?thesis  huffman@29403  312  by (simp only: mod_mult_eq [symmetric])  huffman@29403  313 qed  huffman@29403  314 huffman@47164  315 text {* Exponentiation respects modular equivalence. *}  huffman@47164  316 huffman@47164  317 lemma power_mod: "(a mod b)^n mod b = a^n mod b"  huffman@47164  318 apply (induct n, simp_all)  huffman@47164  319 apply (rule mod_mult_right_eq [THEN trans])  huffman@47164  320 apply (simp (no_asm_simp))  huffman@47164  321 apply (rule mod_mult_eq [symmetric])  huffman@47164  322 done  huffman@47164  323 huffman@29404  324 lemma mod_mod_cancel:  huffman@29404  325  assumes "c dvd b"  huffman@29404  326  shows "a mod b mod c = a mod c"  huffman@29404  327 proof -  huffman@29404  328  from c dvd b obtain k where "b = c * k"  huffman@29404  329  by (rule dvdE)  huffman@29404  330  have "a mod b mod c = a mod (c * k) mod c"  huffman@29404  331  by (simp only: b = c * k)  huffman@29404  332  also have "\ = (a mod (c * k) + a div (c * k) * k * c) mod c"  huffman@29404  333  by (simp only: mod_mult_self1)  huffman@29404  334  also have "\ = (a div (c * k) * (c * k) + a mod (c * k)) mod c"  haftmann@58786  335  by (simp only: ac_simps)  huffman@29404  336  also have "\ = a mod c"  huffman@29404  337  by (simp only: mod_div_equality)  huffman@29404  338  finally show ?thesis .  huffman@29404  339 qed  huffman@29404  340 haftmann@30930  341 lemma div_mult_div_if_dvd:  haftmann@30930  342  "y dvd x \ z dvd w \ (x div y) * (w div z) = (x * w) div (y * z)"  haftmann@30930  343  apply (cases "y = 0", simp)  haftmann@30930  344  apply (cases "z = 0", simp)  haftmann@30930  345  apply (auto elim!: dvdE simp add: algebra_simps)  haftmann@57512  346  apply (subst mult.assoc [symmetric])  nipkow@30476  347  apply (simp add: no_zero_divisors)  haftmann@30930  348  done  haftmann@30930  349 haftmann@30930  350 lemma div_mult_mult2 [simp]:  haftmann@30930  351  "c \ 0 \ (a * c) div (b * c) = a div b"  haftmann@57512  352  by (drule div_mult_mult1) (simp add: mult.commute)  haftmann@30930  353 haftmann@30930  354 lemma div_mult_mult1_if [simp]:  haftmann@30930  355  "(c * a) div (c * b) = (if c = 0 then 0 else a div b)"  haftmann@30930  356  by simp_all  nipkow@30476  357 haftmann@30930  358 lemma mod_mult_mult1:  haftmann@30930  359  "(c * a) mod (c * b) = c * (a mod b)"  haftmann@30930  360 proof (cases "c = 0")  haftmann@30930  361  case True then show ?thesis by simp  haftmann@30930  362 next  haftmann@30930  363  case False  haftmann@30930  364  from mod_div_equality  haftmann@30930  365  have "((c * a) div (c * b)) * (c * b) + (c * a) mod (c * b) = c * a" .  haftmann@30930  366  with False have "c * ((a div b) * b + a mod b) + (c * a) mod (c * b)  haftmann@30930  367  = c * a + c * (a mod b)" by (simp add: algebra_simps)  lp15@60562  368  with mod_div_equality show ?thesis by simp  haftmann@30930  369 qed  lp15@60562  370 haftmann@30930  371 lemma mod_mult_mult2:  haftmann@30930  372  "(a * c) mod (b * c) = (a mod b) * c"  haftmann@57512  373  using mod_mult_mult1 [of c a b] by (simp add: mult.commute)  haftmann@30930  374 huffman@47159  375 lemma mult_mod_left: "(a mod b) * c = (a * c) mod (b * c)"  huffman@47159  376  by (fact mod_mult_mult2 [symmetric])  huffman@47159  377 huffman@47159  378 lemma mult_mod_right: "c * (a mod b) = (c * a) mod (c * b)"  huffman@47159  379  by (fact mod_mult_mult1 [symmetric])  huffman@47159  380 haftmann@59009  381 lemma dvd_times_left_cancel_iff [simp]: -- \FIXME generalize\  haftmann@59009  382  assumes "c \ 0"  haftmann@59009  383  shows "c * a dvd c * b \ a dvd b"  haftmann@59009  384 proof -  haftmann@59009  385  have "(c * b) mod (c * a) = 0 \ b mod a = 0" (is "?P \ ?Q")  haftmann@59009  386  using assms by (simp add: mod_mult_mult1)  haftmann@59009  387  then show ?thesis by (simp add: mod_eq_0_iff_dvd)  haftmann@59009  388 qed  haftmann@59009  389 haftmann@59009  390 lemma dvd_times_right_cancel_iff [simp]: -- \FIXME generalize\  haftmann@59009  391  assumes "c \ 0"  haftmann@59009  392  shows "a * c dvd b * c \ a dvd b"  haftmann@59009  393  using assms dvd_times_left_cancel_iff [of c a b] by (simp add: ac_simps)  haftmann@59009  394 huffman@31662  395 lemma dvd_mod: "k dvd m \ k dvd n \ k dvd (m mod n)"  huffman@31662  396  unfolding dvd_def by (auto simp add: mod_mult_mult1)  huffman@31662  397 huffman@31662  398 lemma dvd_mod_iff: "k dvd n \ k dvd (m mod n) \ k dvd m"  huffman@31662  399 by (blast intro: dvd_mod_imp_dvd dvd_mod)  huffman@31662  400 haftmann@31009  401 lemma div_power:  huffman@31661  402  "y dvd x \ (x div y) ^ n = x ^ n div y ^ n"  nipkow@30476  403 apply (induct n)  nipkow@30476  404  apply simp  nipkow@30476  405 apply(simp add: div_mult_div_if_dvd dvd_power_same)  nipkow@30476  406 done  nipkow@30476  407 haftmann@35367  408 lemma dvd_div_eq_mult:  lp15@60562  409  assumes "a \ 0" and "a dvd b"  haftmann@35367  410  shows "b div a = c \ b = c * a"  haftmann@35367  411 proof  haftmann@35367  412  assume "b = c * a"  haftmann@35367  413  then show "b div a = c" by (simp add: assms)  haftmann@35367  414 next  haftmann@35367  415  assume "b div a = c"  haftmann@35367  416  then have "b div a * a = c * a" by simp  haftmann@60353  417  moreover from a dvd b have "b div a * a = b" by simp  haftmann@35367  418  ultimately show "b = c * a" by simp  haftmann@35367  419 qed  lp15@60562  420 haftmann@35367  421 lemma dvd_div_div_eq_mult:  haftmann@35367  422  assumes "a \ 0" "c \ 0" and "a dvd b" "c dvd d"  haftmann@35367  423  shows "b div a = d div c \ b * c = a * d"  haftmann@60353  424  using assms by (auto simp add: mult.commute [of _ a] dvd_div_eq_mult div_mult_swap intro: sym)  haftmann@35367  425 huffman@31661  426 end  huffman@31661  427 haftmann@59833  428 class ring_div = comm_ring_1 + semiring_div  huffman@29405  429 begin  huffman@29405  430 haftmann@60353  431 subclass idom_divide ..  haftmann@36634  432 huffman@29405  433 text {* Negation respects modular equivalence. *}  huffman@29405  434 huffman@29405  435 lemma mod_minus_eq: "(- a) mod b = (- (a mod b)) mod b"  huffman@29405  436 proof -  huffman@29405  437  have "(- a) mod b = (- (a div b * b + a mod b)) mod b"  huffman@29405  438  by (simp only: mod_div_equality)  huffman@29405  439  also have "\ = (- (a mod b) + - (a div b) * b) mod b"  haftmann@57514  440  by (simp add: ac_simps)  huffman@29405  441  also have "\ = (- (a mod b)) mod b"  huffman@29405  442  by (rule mod_mult_self1)  huffman@29405  443  finally show ?thesis .  huffman@29405  444 qed  huffman@29405  445 huffman@29405  446 lemma mod_minus_cong:  huffman@29405  447  assumes "a mod b = a' mod b"  huffman@29405  448  shows "(- a) mod b = (- a') mod b"  huffman@29405  449 proof -  huffman@29405  450  have "(- (a mod b)) mod b = (- (a' mod b)) mod b"  huffman@29405  451  unfolding assms ..  huffman@29405  452  thus ?thesis  huffman@29405  453  by (simp only: mod_minus_eq [symmetric])  huffman@29405  454 qed  huffman@29405  455 huffman@29405  456 text {* Subtraction respects modular equivalence. *}  huffman@29405  457 haftmann@54230  458 lemma mod_diff_left_eq:  haftmann@54230  459  "(a - b) mod c = (a mod c - b) mod c"  haftmann@54230  460  using mod_add_cong [of a c "a mod c" "- b" "- b"] by simp  haftmann@54230  461 haftmann@54230  462 lemma mod_diff_right_eq:  haftmann@54230  463  "(a - b) mod c = (a - b mod c) mod c"  haftmann@54230  464  using mod_add_cong [of a c a "- b" "- (b mod c)"] mod_minus_cong [of "b mod c" c b] by simp  haftmann@54230  465 haftmann@54230  466 lemma mod_diff_eq:  haftmann@54230  467  "(a - b) mod c = (a mod c - b mod c) mod c"  haftmann@54230  468  using mod_add_cong [of a c "a mod c" "- b" "- (b mod c)"] mod_minus_cong [of "b mod c" c b] by simp  huffman@29405  469 huffman@29405  470 lemma mod_diff_cong:  huffman@29405  471  assumes "a mod c = a' mod c"  huffman@29405  472  assumes "b mod c = b' mod c"  huffman@29405  473  shows "(a - b) mod c = (a' - b') mod c"  haftmann@54230  474  using assms mod_add_cong [of a c a' "- b" "- b'"] mod_minus_cong [of b c "b'"] by simp  huffman@29405  475 nipkow@30180  476 lemma dvd_neg_div: "y dvd x \ -x div y = - (x div y)"  nipkow@30180  477 apply (case_tac "y = 0") apply simp  nipkow@30180  478 apply (auto simp add: dvd_def)  nipkow@30180  479 apply (subgoal_tac "-(y * k) = y * - k")  thomas@57492  480  apply (simp only:)  nipkow@30180  481  apply (erule div_mult_self1_is_id)  nipkow@30180  482 apply simp  nipkow@30180  483 done  nipkow@30180  484 nipkow@30180  485 lemma dvd_div_neg: "y dvd x \ x div -y = - (x div y)"  nipkow@30180  486 apply (case_tac "y = 0") apply simp  nipkow@30180  487 apply (auto simp add: dvd_def)  nipkow@30180  488 apply (subgoal_tac "y * k = -y * -k")  thomas@57492  489  apply (erule ssubst, rule div_mult_self1_is_id)  nipkow@30180  490  apply simp  nipkow@30180  491 apply simp  nipkow@30180  492 done  nipkow@30180  493 nipkow@59473  494 lemma div_diff[simp]:  nipkow@59380  495  "\ z dvd x; z dvd y\ \ (x - y) div z = x div z - y div z"  nipkow@59380  496 using div_add[where y = "- z" for z]  nipkow@59380  497 by (simp add: dvd_neg_div)  nipkow@59380  498 huffman@47159  499 lemma div_minus_minus [simp]: "(-a) div (-b) = a div b"  huffman@47159  500  using div_mult_mult1 [of "- 1" a b]  huffman@47159  501  unfolding neg_equal_0_iff_equal by simp  huffman@47159  502 huffman@47159  503 lemma mod_minus_minus [simp]: "(-a) mod (-b) = - (a mod b)"  huffman@47159  504  using mod_mult_mult1 [of "- 1" a b] by simp  huffman@47159  505 huffman@47159  506 lemma div_minus_right: "a div (-b) = (-a) div b"  huffman@47159  507  using div_minus_minus [of "-a" b] by simp  huffman@47159  508 huffman@47159  509 lemma mod_minus_right: "a mod (-b) = - ((-a) mod b)"  huffman@47159  510  using mod_minus_minus [of "-a" b] by simp  huffman@47159  511 huffman@47160  512 lemma div_minus1_right [simp]: "a div (-1) = -a"  huffman@47160  513  using div_minus_right [of a 1] by simp  huffman@47160  514 huffman@47160  515 lemma mod_minus1_right [simp]: "a mod (-1) = 0"  huffman@47160  516  using mod_minus_right [of a 1] by simp  huffman@47160  517 lp15@60562  518 lemma minus_mod_self2 [simp]:  haftmann@54221  519  "(a - b) mod b = a mod b"  haftmann@54221  520  by (simp add: mod_diff_right_eq)  haftmann@54221  521 lp15@60562  522 lemma minus_mod_self1 [simp]:  haftmann@54221  523  "(b - a) mod b = - a mod b"  haftmann@54230  524  using mod_add_self2 [of "- a" b] by simp  haftmann@54221  525 huffman@29405  526 end  huffman@29405  527 haftmann@58778  528 haftmann@58778  529 subsubsection {* Parity and division *}  haftmann@58778  530 lp15@60562  531 class semiring_div_parity = semiring_div + comm_semiring_1_cancel + numeral +  haftmann@54226  532  assumes parity: "a mod 2 = 0 \ a mod 2 = 1"  haftmann@58786  533  assumes one_mod_two_eq_one [simp]: "1 mod 2 = 1"  haftmann@58710  534  assumes zero_not_eq_two: "0 \ 2"  haftmann@54226  535 begin  haftmann@54226  536 haftmann@54226  537 lemma parity_cases [case_names even odd]:  haftmann@54226  538  assumes "a mod 2 = 0 \ P"  haftmann@54226  539  assumes "a mod 2 = 1 \ P"  haftmann@54226  540  shows P  haftmann@54226  541  using assms parity by blast  haftmann@54226  542 haftmann@58786  543 lemma one_div_two_eq_zero [simp]:  haftmann@58778  544  "1 div 2 = 0"  haftmann@58778  545 proof (cases "2 = 0")  haftmann@58778  546  case True then show ?thesis by simp  haftmann@58778  547 next  haftmann@58778  548  case False  haftmann@58778  549  from mod_div_equality have "1 div 2 * 2 + 1 mod 2 = 1" .  haftmann@58778  550  with one_mod_two_eq_one have "1 div 2 * 2 + 1 = 1" by simp  haftmann@58953  551  then have "1 div 2 * 2 = 0" by (simp add: ac_simps add_left_imp_eq del: mult_eq_0_iff)  haftmann@58953  552  then have "1 div 2 = 0 \ 2 = 0" by simp  haftmann@58778  553  with False show ?thesis by auto  haftmann@58778  554 qed  haftmann@58778  555 haftmann@58786  556 lemma not_mod_2_eq_0_eq_1 [simp]:  haftmann@58786  557  "a mod 2 \ 0 \ a mod 2 = 1"  haftmann@58786  558  by (cases a rule: parity_cases) simp_all  haftmann@58786  559 haftmann@58786  560 lemma not_mod_2_eq_1_eq_0 [simp]:  haftmann@58786  561  "a mod 2 \ 1 \ a mod 2 = 0"  haftmann@58786  562  by (cases a rule: parity_cases) simp_all  haftmann@58786  563 haftmann@58778  564 subclass semiring_parity  haftmann@58778  565 proof (unfold_locales, unfold dvd_eq_mod_eq_0 not_mod_2_eq_0_eq_1)  haftmann@58778  566  show "1 mod 2 = 1"  haftmann@58778  567  by (fact one_mod_two_eq_one)  haftmann@58778  568 next  haftmann@58778  569  fix a b  haftmann@58778  570  assume "a mod 2 = 1"  haftmann@58778  571  moreover assume "b mod 2 = 1"  haftmann@58778  572  ultimately show "(a + b) mod 2 = 0"  haftmann@58778  573  using mod_add_eq [of a b 2] by simp  haftmann@58778  574 next  haftmann@58778  575  fix a b  haftmann@58778  576  assume "(a * b) mod 2 = 0"  haftmann@58778  577  then have "(a mod 2) * (b mod 2) = 0"  haftmann@58778  578  by (cases "a mod 2 = 0") (simp_all add: mod_mult_eq [of a b 2])  haftmann@58778  579  then show "a mod 2 = 0 \ b mod 2 = 0"  haftmann@58778  580  by (rule divisors_zero)  haftmann@58778  581 next  haftmann@58778  582  fix a  haftmann@58778  583  assume "a mod 2 = 1"  haftmann@58778  584  then have "a = a div 2 * 2 + 1" using mod_div_equality [of a 2] by simp  haftmann@58778  585  then show "\b. a = b + 1" ..  haftmann@58778  586 qed  haftmann@58778  587 haftmann@58778  588 lemma even_iff_mod_2_eq_zero:  haftmann@58778  589  "even a \ a mod 2 = 0"  haftmann@58778  590  by (fact dvd_eq_mod_eq_0)  haftmann@58778  591 haftmann@58778  592 lemma even_succ_div_two [simp]:  haftmann@58778  593  "even a \ (a + 1) div 2 = a div 2"  haftmann@58778  594  by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)  haftmann@58778  595 haftmann@58778  596 lemma odd_succ_div_two [simp]:  haftmann@58778  597  "odd a \ (a + 1) div 2 = a div 2 + 1"  haftmann@58778  598  by (auto elim!: oddE simp add: zero_not_eq_two [symmetric] add.assoc)  haftmann@58778  599 haftmann@58778  600 lemma even_two_times_div_two:  haftmann@58778  601  "even a \ 2 * (a div 2) = a"  haftmann@58778  602  by (fact dvd_mult_div_cancel)  haftmann@58778  603 haftmann@58834  604 lemma odd_two_times_div_two_succ [simp]:  haftmann@58778  605  "odd a \ 2 * (a div 2) + 1 = a"  haftmann@58778  606  using mod_div_equality2 [of 2 a] by (simp add: even_iff_mod_2_eq_zero)  haftmann@58778  607 haftmann@54226  608 end  haftmann@54226  609 haftmann@25942  610 haftmann@53067  611 subsection {* Generic numeral division with a pragmatic type class *}  haftmann@53067  612 haftmann@53067  613 text {*  haftmann@53067  614  The following type class contains everything necessary to formulate  haftmann@53067  615  a division algorithm in ring structures with numerals, restricted  haftmann@53067  616  to its positive segments. This is its primary motiviation, and it  haftmann@53067  617  could surely be formulated using a more fine-grained, more algebraic  haftmann@53067  618  and less technical class hierarchy.  haftmann@53067  619 *}  haftmann@53067  620 lp15@60562  621 class semiring_numeral_div = semiring_div + comm_semiring_1_cancel + linordered_semidom +  haftmann@59816  622  assumes div_less: "0 \ a \ a < b \ a div b = 0"  haftmann@53067  623  and mod_less: " 0 \ a \ a < b \ a mod b = a"  haftmann@53067  624  and div_positive: "0 < b \ b \ a \ a div b > 0"  haftmann@53067  625  and mod_less_eq_dividend: "0 \ a \ a mod b \ a"  haftmann@53067  626  and pos_mod_bound: "0 < b \ a mod b < b"  haftmann@53067  627  and pos_mod_sign: "0 < b \ 0 \ a mod b"  haftmann@53067  628  and mod_mult2_eq: "0 \ c \ a mod (b * c) = b * (a div b mod c) + a mod b"  haftmann@53067  629  and div_mult2_eq: "0 \ c \ a div (b * c) = a div b div c"  haftmann@53067  630  assumes discrete: "a < b \ a + 1 \ b"  haftmann@53067  631 begin  haftmann@53067  632 haftmann@59816  633 lemma mult_div_cancel:  haftmann@59816  634  "b * (a div b) = a - a mod b"  haftmann@59816  635 proof -  haftmann@59816  636  have "b * (a div b) + a mod b = a"  haftmann@59816  637  using mod_div_equality [of a b] by (simp add: ac_simps)  haftmann@59816  638  then have "b * (a div b) + a mod b - a mod b = a - a mod b"  haftmann@59816  639  by simp  haftmann@59816  640  then show ?thesis  haftmann@59816  641  by simp  haftmann@59816  642 qed  haftmann@53067  643 haftmann@54226  644 subclass semiring_div_parity  haftmann@54226  645 proof  haftmann@54226  646  fix a  haftmann@54226  647  show "a mod 2 = 0 \ a mod 2 = 1"  haftmann@54226  648  proof (rule ccontr)  haftmann@54226  649  assume "\ (a mod 2 = 0 \ a mod 2 = 1)"  haftmann@54226  650  then have "a mod 2 \ 0" and "a mod 2 \ 1" by simp_all  haftmann@54226  651  have "0 < 2" by simp  haftmann@54226  652  with pos_mod_bound pos_mod_sign have "0 \ a mod 2" "a mod 2 < 2" by simp_all  haftmann@54226  653  with a mod 2 \ 0 have "0 < a mod 2" by simp  haftmann@54226  654  with discrete have "1 \ a mod 2" by simp  haftmann@54226  655  with a mod 2 \ 1 have "1 < a mod 2" by simp  haftmann@54226  656  with discrete have "2 \ a mod 2" by simp  haftmann@54226  657  with a mod 2 < 2 show False by simp  haftmann@54226  658  qed  haftmann@58646  659 next  haftmann@58646  660  show "1 mod 2 = 1"  haftmann@58646  661  by (rule mod_less) simp_all  haftmann@58710  662 next  haftmann@58710  663  show "0 \ 2"  haftmann@58710  664  by simp  haftmann@53067  665 qed  haftmann@53067  666 haftmann@53067  667 lemma divmod_digit_1:  haftmann@53067  668  assumes "0 \ a" "0 < b" and "b \ a mod (2 * b)"  haftmann@53067  669  shows "2 * (a div (2 * b)) + 1 = a div b" (is "?P")  haftmann@53067  670  and "a mod (2 * b) - b = a mod b" (is "?Q")  haftmann@53067  671 proof -  haftmann@53067  672  from assms mod_less_eq_dividend [of a "2 * b"] have "b \ a"  haftmann@53067  673  by (auto intro: trans)  haftmann@53067  674  with 0 < b have "0 < a div b" by (auto intro: div_positive)  haftmann@53067  675  then have [simp]: "1 \ a div b" by (simp add: discrete)  haftmann@53067  676  with 0 < b have mod_less: "a mod b < b" by (simp add: pos_mod_bound)  haftmann@53067  677  def w \ "a div b mod 2" with parity have w_exhaust: "w = 0 \ w = 1" by auto  haftmann@53067  678  have mod_w: "a mod (2 * b) = a mod b + b * w"  haftmann@53067  679  by (simp add: w_def mod_mult2_eq ac_simps)  haftmann@53067  680  from assms w_exhaust have "w = 1"  haftmann@53067  681  by (auto simp add: mod_w) (insert mod_less, auto)  haftmann@53067  682  with mod_w have mod: "a mod (2 * b) = a mod b + b" by simp  haftmann@53067  683  have "2 * (a div (2 * b)) = a div b - w"  haftmann@53067  684  by (simp add: w_def div_mult2_eq mult_div_cancel ac_simps)  haftmann@53067  685  with w = 1 have div: "2 * (a div (2 * b)) = a div b - 1" by simp  haftmann@53067  686  then show ?P and ?Q  haftmann@59816  687  by (simp_all add: div mod add_implies_diff [symmetric] le_add_diff_inverse2)  haftmann@53067  688 qed  haftmann@53067  689 haftmann@53067  690 lemma divmod_digit_0:  haftmann@53067  691  assumes "0 < b" and "a mod (2 * b) < b"  haftmann@53067  692  shows "2 * (a div (2 * b)) = a div b" (is "?P")  haftmann@53067  693  and "a mod (2 * b) = a mod b" (is "?Q")  haftmann@53067  694 proof -  haftmann@53067  695  def w \ "a div b mod 2" with parity have w_exhaust: "w = 0 \ w = 1" by auto  haftmann@53067  696  have mod_w: "a mod (2 * b) = a mod b + b * w"  haftmann@53067  697  by (simp add: w_def mod_mult2_eq ac_simps)  haftmann@53067  698  moreover have "b \ a mod b + b"  haftmann@53067  699  proof -  haftmann@53067  700  from 0 < b pos_mod_sign have "0 \ a mod b" by blast  haftmann@53067  701  then have "0 + b \ a mod b + b" by (rule add_right_mono)  haftmann@53067  702  then show ?thesis by simp  haftmann@53067  703  qed  haftmann@53067  704  moreover note assms w_exhaust  haftmann@53067  705  ultimately have "w = 0" by auto  haftmann@53067  706  with mod_w have mod: "a mod (2 * b) = a mod b" by simp  haftmann@53067  707  have "2 * (a div (2 * b)) = a div b - w"  haftmann@53067  708  by (simp add: w_def div_mult2_eq mult_div_cancel ac_simps)  haftmann@53067  709  with w = 0 have div: "2 * (a div (2 * b)) = a div b" by simp  haftmann@53067  710  then show ?P and ?Q  haftmann@53067  711  by (simp_all add: div mod)  haftmann@53067  712 qed  haftmann@53067  713 haftmann@53067  714 definition divmod :: "num \ num \ 'a \ 'a"  haftmann@53067  715 where  haftmann@53067  716  "divmod m n = (numeral m div numeral n, numeral m mod numeral n)"  haftmann@53067  717 haftmann@53067  718 lemma fst_divmod [simp]:  haftmann@53067  719  "fst (divmod m n) = numeral m div numeral n"  haftmann@53067  720  by (simp add: divmod_def)  haftmann@53067  721 haftmann@53067  722 lemma snd_divmod [simp]:  haftmann@53067  723  "snd (divmod m n) = numeral m mod numeral n"  haftmann@53067  724  by (simp add: divmod_def)  haftmann@53067  725 haftmann@53067  726 definition divmod_step :: "num \ 'a \ 'a \ 'a \ 'a"  haftmann@53067  727 where  haftmann@53067  728  "divmod_step l qr = (let (q, r) = qr  haftmann@53067  729  in if r \ numeral l then (2 * q + 1, r - numeral l)  haftmann@53067  730  else (2 * q, r))"  haftmann@53067  731 haftmann@53067  732 text {*  haftmann@53067  733  This is a formulation of one step (referring to one digit position)  haftmann@53067  734  in school-method division: compare the dividend at the current  haftmann@53070  735  digit position with the remainder from previous division steps  haftmann@53067  736  and evaluate accordingly.  haftmann@53067  737 *}  haftmann@53067  738 haftmann@53067  739 lemma divmod_step_eq [code]:  haftmann@53067  740  "divmod_step l (q, r) = (if numeral l \ r  haftmann@53067  741  then (2 * q + 1, r - numeral l) else (2 * q, r))"  haftmann@53067  742  by (simp add: divmod_step_def)  haftmann@53067  743 haftmann@53067  744 lemma divmod_step_simps [simp]:  haftmann@53067  745  "r < numeral l \ divmod_step l (q, r) = (2 * q, r)"  haftmann@53067  746  "numeral l \ r \ divmod_step l (q, r) = (2 * q + 1, r - numeral l)"  haftmann@53067  747  by (auto simp add: divmod_step_eq not_le)  haftmann@53067  748 haftmann@53067  749 text {*  haftmann@53067  750  This is a formulation of school-method division.  haftmann@53067  751  If the divisor is smaller than the dividend, terminate.  haftmann@53067  752  If not, shift the dividend to the right until termination  haftmann@53067  753  occurs and then reiterate single division steps in the  haftmann@53067  754  opposite direction.  haftmann@53067  755 *}  haftmann@53067  756 haftmann@53067  757 lemma divmod_divmod_step [code]:  haftmann@53067  758  "divmod m n = (if m < n then (0, numeral m)  haftmann@53067  759  else divmod_step n (divmod m (Num.Bit0 n)))"  haftmann@53067  760 proof (cases "m < n")  haftmann@53067  761  case True then have "numeral m < numeral n" by simp  haftmann@53067  762  then show ?thesis  haftmann@53067  763  by (simp add: prod_eq_iff div_less mod_less)  haftmann@53067  764 next  haftmann@53067  765  case False  haftmann@53067  766  have "divmod m n =  haftmann@53067  767  divmod_step n (numeral m div (2 * numeral n),  haftmann@53067  768  numeral m mod (2 * numeral n))"  haftmann@53067  769  proof (cases "numeral n \ numeral m mod (2 * numeral n)")  haftmann@53067  770  case True  haftmann@53067  771  with divmod_step_simps  haftmann@53067  772  have "divmod_step n (numeral m div (2 * numeral n), numeral m mod (2 * numeral n)) =  haftmann@53067  773  (2 * (numeral m div (2 * numeral n)) + 1, numeral m mod (2 * numeral n) - numeral n)"  haftmann@53067  774  by blast  haftmann@53067  775  moreover from True divmod_digit_1 [of "numeral m" "numeral n"]  haftmann@53067  776  have "2 * (numeral m div (2 * numeral n)) + 1 = numeral m div numeral n"  haftmann@53067  777  and "numeral m mod (2 * numeral n) - numeral n = numeral m mod numeral n"  haftmann@53067  778  by simp_all  haftmann@53067  779  ultimately show ?thesis by (simp only: divmod_def)  haftmann@53067  780  next  haftmann@53067  781  case False then have *: "numeral m mod (2 * numeral n) < numeral n"  haftmann@53067  782  by (simp add: not_le)  haftmann@53067  783  with divmod_step_simps  haftmann@53067  784  have "divmod_step n (numeral m div (2 * numeral n), numeral m mod (2 * numeral n)) =  haftmann@53067  785  (2 * (numeral m div (2 * numeral n)), numeral m mod (2 * numeral n))"  haftmann@53067  786  by blast  haftmann@53067  787  moreover from * divmod_digit_0 [of "numeral n" "numeral m"]  haftmann@53067  788  have "2 * (numeral m div (2 * numeral n)) = numeral m div numeral n"  haftmann@53067  789  and "numeral m mod (2 * numeral n) = numeral m mod numeral n"  haftmann@53067  790  by (simp_all only: zero_less_numeral)  haftmann@53067  791  ultimately show ?thesis by (simp only: divmod_def)  haftmann@53067  792  qed  haftmann@53067  793  then have "divmod m n =  haftmann@53067  794  divmod_step n (numeral m div numeral (Num.Bit0 n),  haftmann@53067  795  numeral m mod numeral (Num.Bit0 n))"  lp15@60562  796  by (simp only: numeral.simps distrib mult_1)  haftmann@53067  797  then have "divmod m n = divmod_step n (divmod m (Num.Bit0 n))"  haftmann@53067  798  by (simp add: divmod_def)  haftmann@53067  799  with False show ?thesis by simp  haftmann@53067  800 qed  haftmann@53067  801 haftmann@58953  802 lemma divmod_eq [simp]:  haftmann@58953  803  "m < n \ divmod m n = (0, numeral m)"  haftmann@58953  804  "n \ m \ divmod m n = divmod_step n (divmod m (Num.Bit0 n))"  haftmann@58953  805  by (auto simp add: divmod_divmod_step [of m n])  haftmann@58953  806 haftmann@58953  807 lemma divmod_cancel [simp, code]:  haftmann@53069  808  "divmod (Num.Bit0 m) (Num.Bit0 n) = (case divmod m n of (q, r) \ (q, 2 * r))" (is ?P)  haftmann@53069  809  "divmod (Num.Bit1 m) (Num.Bit0 n) = (case divmod m n of (q, r) \ (q, 2 * r + 1))" (is ?Q)  haftmann@53069  810 proof -  haftmann@53069  811  have *: "\q. numeral (Num.Bit0 q) = 2 * numeral q"  haftmann@53069  812  "\q. numeral (Num.Bit1 q) = 2 * numeral q + 1"  haftmann@53069  813  by (simp_all only: numeral_mult numeral.simps distrib) simp_all  haftmann@53069  814  have "1 div 2 = 0" "1 mod 2 = 1" by (auto intro: div_less mod_less)  haftmann@53069  815  then show ?P and ?Q  haftmann@53069  816  by (simp_all add: prod_eq_iff split_def * [of m] * [of n] mod_mult_mult1  haftmann@53069  817  div_mult2_eq [of _ _ 2] mod_mult2_eq [of _ _ 2] add.commute del: numeral_times_numeral)  haftmann@58953  818 qed  haftmann@58953  819 haftmann@58953  820 text {* Special case: divisibility *}  haftmann@58953  821 haftmann@58953  822 definition divides_aux :: "'a \ 'a \ bool"  haftmann@58953  823 where  haftmann@58953  824  "divides_aux qr \ snd qr = 0"  haftmann@58953  825 haftmann@58953  826 lemma divides_aux_eq [simp]:  haftmann@58953  827  "divides_aux (q, r) \ r = 0"  haftmann@58953  828  by (simp add: divides_aux_def)  haftmann@58953  829 haftmann@58953  830 lemma dvd_numeral_simp [simp]:  haftmann@58953  831  "numeral m dvd numeral n \ divides_aux (divmod n m)"  haftmann@58953  832  by (simp add: divmod_def mod_eq_0_iff_dvd)  haftmann@53069  833 haftmann@53067  834 end  haftmann@53067  835 lp15@60562  836 haftmann@26100  837 subsection {* Division on @{typ nat} *}  haftmann@26100  838 haftmann@26100  839 text {*  haftmann@60429  840  We define @{const divide} and @{const mod} on @{typ nat} by means  haftmann@26100  841  of a characteristic relation with two input arguments  haftmann@26100  842  @{term "m\nat"}, @{term "n\nat"} and two output arguments  haftmann@26100  843  @{term "q\nat"}(uotient) and @{term "r\nat"}(emainder).  haftmann@26100  844 *}  haftmann@26100  845 haftmann@33340  846 definition divmod_nat_rel :: "nat \ nat \ nat \ nat \ bool" where  haftmann@33340  847  "divmod_nat_rel m n qr \  haftmann@30923  848  m = fst qr * n + snd qr \  haftmann@30923  849  (if n = 0 then fst qr = 0 else if n > 0 then 0 \ snd qr \ snd qr < n else n < snd qr \ snd qr \ 0)"  haftmann@26100  850 haftmann@33340  851 text {* @{const divmod_nat_rel} is total: *}  haftmann@26100  852 haftmann@33340  853 lemma divmod_nat_rel_ex:  haftmann@33340  854  obtains q r where "divmod_nat_rel m n (q, r)"  haftmann@26100  855 proof (cases "n = 0")  haftmann@30923  856  case True with that show thesis  haftmann@33340  857  by (auto simp add: divmod_nat_rel_def)  haftmann@26100  858 next  haftmann@26100  859  case False  haftmann@26100  860  have "\q r. m = q * n + r \ r < n"  haftmann@26100  861  proof (induct m)  haftmann@26100  862  case 0 with n \ 0  haftmann@26100  863  have "(0\nat) = 0 * n + 0 \ 0 < n" by simp  haftmann@26100  864  then show ?case by blast  haftmann@26100  865  next  haftmann@26100  866  case (Suc m) then obtain q' r'  haftmann@26100  867  where m: "m = q' * n + r'" and n: "r' < n" by auto  haftmann@26100  868  then show ?case proof (cases "Suc r' < n")  haftmann@26100  869  case True  haftmann@26100  870  from m n have "Suc m = q' * n + Suc r'" by simp  haftmann@26100  871  with True show ?thesis by blast  haftmann@26100  872  next  haftmann@26100  873  case False then have "n \ Suc r'" by auto  haftmann@26100  874  moreover from n have "Suc r' \ n" by auto  haftmann@26100  875  ultimately have "n = Suc r'" by auto  haftmann@26100  876  with m have "Suc m = Suc q' * n + 0" by simp  haftmann@26100  877  with n \ 0 show ?thesis by blast  haftmann@26100  878  qed  haftmann@26100  879  qed  haftmann@26100  880  with that show thesis  haftmann@33340  881  using n \ 0 by (auto simp add: divmod_nat_rel_def)  haftmann@26100  882 qed  haftmann@26100  883 haftmann@33340  884 text {* @{const divmod_nat_rel} is injective: *}  haftmann@26100  885 haftmann@33340  886 lemma divmod_nat_rel_unique:  haftmann@33340  887  assumes "divmod_nat_rel m n qr"  haftmann@33340  888  and "divmod_nat_rel m n qr'"  haftmann@30923  889  shows "qr = qr'"  haftmann@26100  890 proof (cases "n = 0")  haftmann@26100  891  case True with assms show ?thesis  haftmann@30923  892  by (cases qr, cases qr')  haftmann@33340  893  (simp add: divmod_nat_rel_def)  haftmann@26100  894 next  haftmann@26100  895  case False  haftmann@26100  896  have aux: "\q r q' r'. q' * n + r' = q * n + r \ r < n \ q' \ (q\nat)"  haftmann@26100  897  apply (rule leI)  haftmann@26100  898  apply (subst less_iff_Suc_add)  haftmann@26100  899  apply (auto simp add: add_mult_distrib)  haftmann@26100  900  done  wenzelm@53374  901  from n \ 0 assms have *: "fst qr = fst qr'"  haftmann@33340  902  by (auto simp add: divmod_nat_rel_def intro: order_antisym dest: aux sym)  wenzelm@53374  903  with assms have "snd qr = snd qr'"  haftmann@33340  904  by (simp add: divmod_nat_rel_def)  wenzelm@53374  905  with * show ?thesis by (cases qr, cases qr') simp  haftmann@26100  906 qed  haftmann@26100  907 haftmann@26100  908 text {*  haftmann@26100  909  We instantiate divisibility on the natural numbers by  haftmann@33340  910  means of @{const divmod_nat_rel}:  haftmann@26100  911 *}  haftmann@25942  912 haftmann@33340  913 definition divmod_nat :: "nat \ nat \ nat \ nat" where  haftmann@37767  914  "divmod_nat m n = (THE qr. divmod_nat_rel m n qr)"  haftmann@30923  915 haftmann@33340  916 lemma divmod_nat_rel_divmod_nat:  haftmann@33340  917  "divmod_nat_rel m n (divmod_nat m n)"  haftmann@30923  918 proof -  haftmann@33340  919  from divmod_nat_rel_ex  haftmann@33340  920  obtain qr where rel: "divmod_nat_rel m n qr" .  haftmann@30923  921  then show ?thesis  haftmann@33340  922  by (auto simp add: divmod_nat_def intro: theI elim: divmod_nat_rel_unique)  haftmann@30923  923 qed  haftmann@30923  924 huffman@47135  925 lemma divmod_nat_unique:  lp15@60562  926  assumes "divmod_nat_rel m n qr"  haftmann@33340  927  shows "divmod_nat m n = qr"  haftmann@33340  928  using assms by (auto intro: divmod_nat_rel_unique divmod_nat_rel_divmod_nat)  haftmann@26100  929 haftmann@60429  930 instantiation nat :: semiring_div  haftmann@60352  931 begin  haftmann@60352  932 haftmann@60352  933 definition divide_nat where  haftmann@60429  934  div_nat_def: "m div n = fst (divmod_nat m n)"  haftmann@60352  935 haftmann@60352  936 definition mod_nat where  haftmann@60352  937  "m mod n = snd (divmod_nat m n)"  huffman@46551  938 huffman@46551  939 lemma fst_divmod_nat [simp]:  huffman@46551  940  "fst (divmod_nat m n) = m div n"  huffman@46551  941  by (simp add: div_nat_def)  huffman@46551  942 huffman@46551  943 lemma snd_divmod_nat [simp]:  huffman@46551  944  "snd (divmod_nat m n) = m mod n"  huffman@46551  945  by (simp add: mod_nat_def)  huffman@46551  946 haftmann@33340  947 lemma divmod_nat_div_mod:  haftmann@33340  948  "divmod_nat m n = (m div n, m mod n)"  huffman@46551  949  by (simp add: prod_eq_iff)  haftmann@26100  950 huffman@47135  951 lemma div_nat_unique:  lp15@60562  952  assumes "divmod_nat_rel m n (q, r)"  haftmann@26100  953  shows "m div n = q"  huffman@47135  954  using assms by (auto dest!: divmod_nat_unique simp add: prod_eq_iff)  huffman@47135  955 huffman@47135  956 lemma mod_nat_unique:  lp15@60562  957  assumes "divmod_nat_rel m n (q, r)"  haftmann@26100  958  shows "m mod n = r"  huffman@47135  959  using assms by (auto dest!: divmod_nat_unique simp add: prod_eq_iff)  haftmann@25571  960 haftmann@33340  961 lemma divmod_nat_rel: "divmod_nat_rel m n (m div n, m mod n)"  huffman@46551  962  using divmod_nat_rel_divmod_nat by (simp add: divmod_nat_div_mod)  paulson@14267  963 huffman@47136  964 lemma divmod_nat_zero: "divmod_nat m 0 = (0, m)"  huffman@47136  965  by (simp add: divmod_nat_unique divmod_nat_rel_def)  huffman@47136  966 huffman@47136  967 lemma divmod_nat_zero_left: "divmod_nat 0 n = (0, 0)"  huffman@47136  968  by (simp add: divmod_nat_unique divmod_nat_rel_def)  haftmann@25942  969 huffman@47137  970 lemma divmod_nat_base: "m < n \ divmod_nat m n = (0, m)"  huffman@47137  971  by (simp add: divmod_nat_unique divmod_nat_rel_def)  haftmann@25942  972 haftmann@33340  973 lemma divmod_nat_step:  haftmann@26100  974  assumes "0 < n" and "n \ m"  haftmann@33340  975  shows "divmod_nat m n = (Suc ((m - n) div n), (m - n) mod n)"  huffman@47135  976 proof (rule divmod_nat_unique)  huffman@47134  977  have "divmod_nat_rel (m - n) n ((m - n) div n, (m - n) mod n)"  huffman@47134  978  by (rule divmod_nat_rel)  huffman@47134  979  thus "divmod_nat_rel m n (Suc ((m - n) div n), (m - n) mod n)"  huffman@47134  980  unfolding divmod_nat_rel_def using assms by auto  haftmann@26100  981 qed  haftmann@25942  982 haftmann@60429  983 text {* The ''recursion'' equations for @{const divide} and @{const mod} *}  haftmann@26100  984 haftmann@26100  985 lemma div_less [simp]:  haftmann@26100  986  fixes m n :: nat  haftmann@26100  987  assumes "m < n"  haftmann@26100  988  shows "m div n = 0"  huffman@46551  989  using assms divmod_nat_base by (simp add: prod_eq_iff)  haftmann@25942  990 haftmann@26100  991 lemma le_div_geq:  haftmann@26100  992  fixes m n :: nat  haftmann@26100  993  assumes "0 < n" and "n \ m"  haftmann@26100  994  shows "m div n = Suc ((m - n) div n)"  huffman@46551  995  using assms divmod_nat_step by (simp add: prod_eq_iff)  paulson@14267  996 haftmann@26100  997 lemma mod_less [simp]:  haftmann@26100  998  fixes m n :: nat  haftmann@26100  999  assumes "m < n"  haftmann@26100  1000  shows "m mod n = m"  huffman@46551  1001  using assms divmod_nat_base by (simp add: prod_eq_iff)  haftmann@26100  1002 haftmann@26100  1003 lemma le_mod_geq:  haftmann@26100  1004  fixes m n :: nat  haftmann@26100  1005  assumes "n \ m"  haftmann@26100  1006  shows "m mod n = (m - n) mod n"  huffman@46551  1007  using assms divmod_nat_step by (cases "n = 0") (simp_all add: prod_eq_iff)  paulson@14267  1008 huffman@47136  1009 instance proof  huffman@47136  1010  fix m n :: nat  huffman@47136  1011  show "m div n * n + m mod n = m"  huffman@47136  1012  using divmod_nat_rel [of m n] by (simp add: divmod_nat_rel_def)  huffman@47136  1013 next  huffman@47136  1014  fix m n q :: nat  huffman@47136  1015  assume "n \ 0"  huffman@47136  1016  then show "(q + m * n) div n = m + q div n"  huffman@47136  1017  by (induct m) (simp_all add: le_div_geq)  huffman@47136  1018 next  huffman@47136  1019  fix m n q :: nat  huffman@47136  1020  assume "m \ 0"  huffman@47136  1021  hence "\a b. divmod_nat_rel n q (a, b) \ divmod_nat_rel (m * n) (m * q) (a, m * b)"  huffman@47136  1022  unfolding divmod_nat_rel_def  huffman@47136  1023  by (auto split: split_if_asm, simp_all add: algebra_simps)  huffman@47136  1024  moreover from divmod_nat_rel have "divmod_nat_rel n q (n div q, n mod q)" .  huffman@47136  1025  ultimately have "divmod_nat_rel (m * n) (m * q) (n div q, m * (n mod q))" .  huffman@47136  1026  thus "(m * n) div (m * q) = n div q" by (rule div_nat_unique)  huffman@47136  1027 next  huffman@47136  1028  fix n :: nat show "n div 0 = 0"  haftmann@33340  1029  by (simp add: div_nat_def divmod_nat_zero)  huffman@47136  1030 next  huffman@47136  1031  fix n :: nat show "0 div n = 0"  huffman@47136  1032  by (simp add: div_nat_def divmod_nat_zero_left)  haftmann@25942  1033 qed  haftmann@26100  1034 haftmann@25942  1035 end  paulson@14267  1036 haftmann@33361  1037 lemma divmod_nat_if [code]: "divmod_nat m n = (if n = 0 \ m < n then (0, m) else  haftmann@33361  1038  let (q, r) = divmod_nat (m - n) n in (Suc q, r))"  blanchet@55414  1039  by (simp add: prod_eq_iff case_prod_beta not_less le_div_geq le_mod_geq)  haftmann@33361  1040 haftmann@60429  1041 text {* Simproc for cancelling @{const divide} and @{const mod} *}  haftmann@25942  1042 wenzelm@51299  1043 ML_file "~~/src/Provers/Arith/cancel_div_mod.ML"  wenzelm@51299  1044 haftmann@30934  1045 ML {*  wenzelm@43594  1046 structure Cancel_Div_Mod_Nat = Cancel_Div_Mod  wenzelm@41550  1047 (  haftmann@60352  1048  val div_name = @{const_name divide};  haftmann@30934  1049  val mod_name = @{const_name mod};  haftmann@30934  1050  val mk_binop = HOLogic.mk_binop;  huffman@48561  1051  val mk_plus = HOLogic.mk_binop @{const_name Groups.plus};  huffman@48561  1052  val dest_plus = HOLogic.dest_bin @{const_name Groups.plus} HOLogic.natT;  huffman@48561  1053  fun mk_sum [] = HOLogic.zero  huffman@48561  1054  | mk_sum [t] = t  huffman@48561  1055  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);  huffman@48561  1056  fun dest_sum tm =  huffman@48561  1057  if HOLogic.is_zero tm then []  huffman@48561  1058  else  huffman@48561  1059  (case try HOLogic.dest_Suc tm of  huffman@48561  1060  SOME t => HOLogic.Suc_zero :: dest_sum t  huffman@48561  1061  | NONE =>  huffman@48561  1062  (case try dest_plus tm of  huffman@48561  1063  SOME (t, u) => dest_sum t @ dest_sum u  huffman@48561  1064  | NONE => [tm]));  haftmann@25942  1065 haftmann@30934  1066  val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];  paulson@14267  1067 haftmann@30934  1068  val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac  haftmann@57514  1069  (@{thm add_0_left} :: @{thm add_0_right} :: @{thms ac_simps}))  wenzelm@41550  1070 )  haftmann@25942  1071 *}  haftmann@25942  1072 wenzelm@43594  1073 simproc_setup cancel_div_mod_nat ("(m::nat) + n") = {* K Cancel_Div_Mod_Nat.proc *}  wenzelm@43594  1074 haftmann@26100  1075 haftmann@26100  1076 subsubsection {* Quotient *}  haftmann@26100  1077 haftmann@26100  1078 lemma div_geq: "0 < n \ \ m < n \ m div n = Suc ((m - n) div n)"  nipkow@29667  1079 by (simp add: le_div_geq linorder_not_less)  haftmann@26100  1080 haftmann@26100  1081 lemma div_if: "0 < n \ m div n = (if m < n then 0 else Suc ((m - n) div n))"  nipkow@29667  1082 by (simp add: div_geq)  haftmann@26100  1083 haftmann@26100  1084 lemma div_mult_self_is_m [simp]: "0 (m*n) div n = (m::nat)"  nipkow@29667  1085 by simp  haftmann@26100  1086 haftmann@26100  1087 lemma div_mult_self1_is_m [simp]: "0 (n*m) div n = (m::nat)"  nipkow@29667  1088 by simp  haftmann@26100  1089 haftmann@53066  1090 lemma div_positive:  haftmann@53066  1091  fixes m n :: nat  haftmann@53066  1092  assumes "n > 0"  haftmann@53066  1093  assumes "m \ n"  haftmann@53066  1094  shows "m div n > 0"  haftmann@53066  1095 proof -  haftmann@53066  1096  from m \ n obtain q where "m = n + q"  haftmann@53066  1097  by (auto simp add: le_iff_add)  haftmann@53066  1098  with n > 0 show ?thesis by simp  haftmann@53066  1099 qed  haftmann@53066  1100 hoelzl@59000  1101 lemma div_eq_0_iff: "(a div b::nat) = 0 \ a < b \ b = 0"  hoelzl@59000  1102  by (metis div_less div_positive div_by_0 gr0I less_numeral_extra(3) not_less)  haftmann@25942  1103 haftmann@25942  1104 subsubsection {* Remainder *}  haftmann@25942  1105 haftmann@26100  1106 lemma mod_less_divisor [simp]:  haftmann@26100  1107  fixes m n :: nat  haftmann@26100  1108  assumes "n > 0"  haftmann@26100  1109  shows "m mod n < (n::nat)"  haftmann@33340  1110  using assms divmod_nat_rel [of m n] unfolding divmod_nat_rel_def by auto  paulson@14267  1111 haftmann@51173  1112 lemma mod_Suc_le_divisor [simp]:  haftmann@51173  1113  "m mod Suc n \ n"  haftmann@51173  1114  using mod_less_divisor [of "Suc n" m] by arith  haftmann@51173  1115 haftmann@26100  1116 lemma mod_less_eq_dividend [simp]:  haftmann@26100  1117  fixes m n :: nat  haftmann@26100  1118  shows "m mod n \ m"  haftmann@26100  1119 proof (rule add_leD2)  haftmann@26100  1120  from mod_div_equality have "m div n * n + m mod n = m" .  haftmann@26100  1121  then show "m div n * n + m mod n \ m" by auto  haftmann@26100  1122 qed  haftmann@26100  1123 haftmann@26100  1124 lemma mod_geq: "\ m < (n\nat) \ m mod n = (m - n) mod n"  nipkow@29667  1125 by (simp add: le_mod_geq linorder_not_less)  paulson@14267  1126 haftmann@26100  1127 lemma mod_if: "m mod (n\nat) = (if m < n then m else (m - n) mod n)"  nipkow@29667  1128 by (simp add: le_mod_geq)  haftmann@26100  1129 paulson@14267  1130 lemma mod_1 [simp]: "m mod Suc 0 = 0"  nipkow@29667  1131 by (induct m) (simp_all add: mod_geq)  paulson@14267  1132 paulson@14267  1133 (* a simple rearrangement of mod_div_equality: *)  paulson@14267  1134 lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"  huffman@47138  1135  using mod_div_equality2 [of n m] by arith  paulson@14267  1136 nipkow@15439  1137 lemma mod_le_divisor[simp]: "0 < n \ m mod n \ (n::nat)"  wenzelm@22718  1138  apply (drule mod_less_divisor [where m = m])  wenzelm@22718  1139  apply simp  wenzelm@22718  1140  done  paulson@14267  1141 haftmann@26100  1142 subsubsection {* Quotient and Remainder *}  paulson@14267  1143 haftmann@33340  1144 lemma divmod_nat_rel_mult1_eq:  bulwahn@46552  1145  "divmod_nat_rel b c (q, r)  haftmann@33340  1146  \ divmod_nat_rel (a * b) c (a * q + a * r div c, a * r mod c)"  haftmann@33340  1147 by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)  paulson@14267  1148 haftmann@30923  1149 lemma div_mult1_eq:  haftmann@30923  1150  "(a * b) div c = a * (b div c) + a * (b mod c) div (c::nat)"  huffman@47135  1151 by (blast intro: divmod_nat_rel_mult1_eq [THEN div_nat_unique] divmod_nat_rel)  paulson@14267  1152 haftmann@33340  1153 lemma divmod_nat_rel_add1_eq:  bulwahn@46552  1154  "divmod_nat_rel a c (aq, ar) \ divmod_nat_rel b c (bq, br)  haftmann@33340  1155  \ divmod_nat_rel (a + b) c (aq + bq + (ar + br) div c, (ar + br) mod c)"  haftmann@33340  1156 by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)  paulson@14267  1157 paulson@14267  1158 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)  paulson@14267  1159 lemma div_add1_eq:  nipkow@25134  1160  "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"  huffman@47135  1161 by (blast intro: divmod_nat_rel_add1_eq [THEN div_nat_unique] divmod_nat_rel)  paulson@14267  1162 haftmann@33340  1163 lemma divmod_nat_rel_mult2_eq:  haftmann@60352  1164  assumes "divmod_nat_rel a b (q, r)"  haftmann@60352  1165  shows "divmod_nat_rel a (b * c) (q div c, b *(q mod c) + r)"  haftmann@60352  1166 proof -  lp15@60562  1167  { assume "r < b" and "0 < c"  haftmann@60352  1168  then have "b * (q mod c) + r < b * c"  haftmann@60352  1169  apply (cut_tac m = q and n = c in mod_less_divisor)  haftmann@60352  1170  apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)  haftmann@60352  1171  apply (erule_tac P = "%x. lhs < rhs x" for lhs rhs in ssubst)  haftmann@60352  1172  apply (simp add: add_mult_distrib2)  haftmann@60352  1173  done  haftmann@60352  1174  then have "r + b * (q mod c) < b * c"  haftmann@60352  1175  by (simp add: ac_simps)  haftmann@60352  1176  } with assms show ?thesis  haftmann@60352  1177  by (auto simp add: divmod_nat_rel_def algebra_simps add_mult_distrib2 [symmetric])  haftmann@60352  1178 qed  lp15@60562  1179 blanchet@55085  1180 lemma div_mult2_eq: "a div (b * c) = (a div b) div (c::nat)"  huffman@47135  1181 by (force simp add: divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN div_nat_unique])  paulson@14267  1182 blanchet@55085  1183 lemma mod_mult2_eq: "a mod (b * c) = b * (a div b mod c) + a mod (b::nat)"  haftmann@57512  1184 by (auto simp add: mult.commute divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN mod_nat_unique])  paulson@14267  1185 haftmann@58786  1186 instance nat :: semiring_numeral_div  haftmann@58786  1187  by intro_classes (auto intro: div_positive simp add: mult_div_cancel mod_mult2_eq div_mult2_eq)  haftmann@58786  1188 paulson@14267  1189 huffman@46551  1190 subsubsection {* Further Facts about Quotient and Remainder *}  paulson@14267  1191 haftmann@58786  1192 lemma div_1 [simp]:  haftmann@58786  1193  "m div Suc 0 = m"  haftmann@58786  1194  using div_by_1 [of m] by simp  paulson@14267  1195 paulson@14267  1196 (* Monotonicity of div in first argument *)  haftmann@30923  1197 lemma div_le_mono [rule_format (no_asm)]:  wenzelm@22718  1198  "\m::nat. m \ n --> (m div k) \ (n div k)"  paulson@14267  1199 apply (case_tac "k=0", simp)  paulson@15251  1200 apply (induct "n" rule: nat_less_induct, clarify)  paulson@14267  1201 apply (case_tac "n= k *)  paulson@14267  1205 apply (case_tac "m=k *)  nipkow@15439  1209 apply (simp add: div_geq diff_le_mono)  paulson@14267  1210 done  paulson@14267  1211 paulson@14267  1212 (* Antimonotonicity of div in second argument *)  paulson@14267  1213 lemma div_le_mono2: "!!m::nat. [| 0n |] ==> (k div n) \ (k div m)"  paulson@14267  1214 apply (subgoal_tac "0 (k-m) div n")  paulson@14267  1223  prefer 2  paulson@14267  1224  apply (blast intro: div_le_mono diff_le_mono2)  paulson@14267  1225 apply (rule le_trans, simp)  nipkow@15439  1226 apply (simp)  paulson@14267  1227 done  paulson@14267  1228 paulson@14267  1229 lemma div_le_dividend [simp]: "m div n \ (m::nat)"  paulson@14267  1230 apply (case_tac "n=0", simp)  paulson@14267  1231 apply (subgoal_tac "m div n \ m div 1", simp)  paulson@14267  1232 apply (rule div_le_mono2)  paulson@14267  1233 apply (simp_all (no_asm_simp))  paulson@14267  1234 done  paulson@14267  1235 wenzelm@22718  1236 (* Similar for "less than" *)  huffman@47138  1237 lemma div_less_dividend [simp]:  huffman@47138  1238  "\(1::nat) < n; 0 < m\ \ m div n < m"  huffman@47138  1239 apply (induct m rule: nat_less_induct)  paulson@14267  1240 apply (rename_tac "m")  paulson@14267  1241 apply (case_tac "m Suc(na) *)  paulson@16796  1260 apply (simp add: linorder_not_less le_Suc_eq mod_geq)  nipkow@15439  1261 apply (auto simp add: Suc_diff_le le_mod_geq)  paulson@14267  1262 done  paulson@14267  1263 paulson@14267  1264 lemma mod_eq_0_iff: "(m mod d = 0) = (\q::nat. m = d*q)"  nipkow@29667  1265 by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)  paulson@17084  1266 wenzelm@22718  1267 lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]  paulson@14267  1268 paulson@14267  1269 (*Loses information, namely we also have rq. m = r + q * d"  haftmann@57514  1274 proof -  haftmann@57514  1275  from mod_div_equality obtain q where "q * d + m mod d = m" by blast  haftmann@57514  1276  with assms have "m = r + q * d" by simp  haftmann@57514  1277  then show ?thesis ..  haftmann@57514  1278 qed  paulson@14267  1279 nipkow@13152  1280 lemma split_div:  nipkow@13189  1281  "P(n div k :: nat) =  nipkow@13189  1282  ((k = 0 \ P 0) \ (k \ 0 \ (!i. !j P i)))"  nipkow@13189  1283  (is "?P = ?Q" is "_ = (_ \ (_ \ ?R))")  nipkow@13189  1284 proof  nipkow@13189  1285  assume P: ?P  nipkow@13189  1286  show ?Q  nipkow@13189  1287  proof (cases)  nipkow@13189  1288  assume "k = 0"  haftmann@27651  1289  with P show ?Q by simp  nipkow@13189  1290  next  nipkow@13189  1291  assume not0: "k \ 0"  nipkow@13189  1292  thus ?Q  nipkow@13189  1293  proof (simp, intro allI impI)  nipkow@13189  1294  fix i j  nipkow@13189  1295  assume n: "n = k*i + j" and j: "j < k"  nipkow@13189  1296  show "P i"  nipkow@13189  1297  proof (cases)  wenzelm@22718  1298  assume "i = 0"  wenzelm@22718  1299  with n j P show "P i" by simp  nipkow@13189  1300  next  wenzelm@22718  1301  assume "i \ 0"  haftmann@57514  1302  with not0 n j P show "P i" by(simp add:ac_simps)  nipkow@13189  1303  qed  nipkow@13189  1304  qed  nipkow@13189  1305  qed  nipkow@13189  1306 next  nipkow@13189  1307  assume Q: ?Q  nipkow@13189  1308  show ?P  nipkow@13189  1309  proof (cases)  nipkow@13189  1310  assume "k = 0"  haftmann@27651  1311  with Q show ?P by simp  nipkow@13189  1312  next  nipkow@13189  1313  assume not0: "k \ 0"  nipkow@13189  1314  with Q have R: ?R by simp  nipkow@13189  1315  from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]  nipkow@13517  1316  show ?P by simp  nipkow@13189  1317  qed  nipkow@13189  1318 qed  nipkow@13189  1319 berghofe@13882  1320 lemma split_div_lemma:  haftmann@26100  1321  assumes "0 < n"  haftmann@26100  1322  shows "n * q \ m \ m < n * Suc q \ q = ((m\nat) div n)" (is "?lhs \ ?rhs")  haftmann@26100  1323 proof  haftmann@26100  1324  assume ?rhs  haftmann@26100  1325  with mult_div_cancel have nq: "n * q = m - (m mod n)" by simp  haftmann@26100  1326  then have A: "n * q \ m" by simp  haftmann@26100  1327  have "n - (m mod n) > 0" using mod_less_divisor assms by auto  haftmann@26100  1328  then have "m < m + (n - (m mod n))" by simp  haftmann@26100  1329  then have "m < n + (m - (m mod n))" by simp  haftmann@26100  1330  with nq have "m < n + n * q" by simp  haftmann@26100  1331  then have B: "m < n * Suc q" by simp  haftmann@26100  1332  from A B show ?lhs ..  haftmann@26100  1333 next  haftmann@26100  1334  assume P: ?lhs  haftmann@33340  1335  then have "divmod_nat_rel m n (q, m - n * q)"  haftmann@57514  1336  unfolding divmod_nat_rel_def by (auto simp add: ac_simps)  haftmann@33340  1337  with divmod_nat_rel_unique divmod_nat_rel [of m n]  haftmann@30923  1338  have "(q, m - n * q) = (m div n, m mod n)" by auto  haftmann@30923  1339  then show ?rhs by simp  haftmann@26100  1340 qed  berghofe@13882  1341 berghofe@13882  1342 theorem split_div':  berghofe@13882  1343  "P ((m::nat) div n) = ((n = 0 \ P 0) \  paulson@14267  1344  (\q. (n * q \ m \ m < n * (Suc q)) \ P q))"  berghofe@13882  1345  apply (case_tac "0 < n")  berghofe@13882  1346  apply (simp only: add: split_div_lemma)  haftmann@27651  1347  apply simp_all  berghofe@13882  1348  done  berghofe@13882  1349 nipkow@13189  1350 lemma split_mod:  nipkow@13189  1351  "P(n mod k :: nat) =  nipkow@13189  1352  ((k = 0 \ P n) \ (k \ 0 \ (!i. !j P j)))"  nipkow@13189  1353  (is "?P = ?Q" is "_ = (_ \ (_ \ ?R))")  nipkow@13189  1354 proof  nipkow@13189  1355  assume P: ?P  nipkow@13189  1356  show ?Q  nipkow@13189  1357  proof (cases)  nipkow@13189  1358  assume "k = 0"  haftmann@27651  1359  with P show ?Q by simp  nipkow@13189  1360  next  nipkow@13189  1361  assume not0: "k \ 0"  nipkow@13189  1362  thus ?Q  nipkow@13189  1363  proof (simp, intro allI impI)  nipkow@13189  1364  fix i j  nipkow@13189  1365  assume "n = k*i + j" "j < k"  haftmann@58786  1366  thus "P j" using not0 P by (simp add: ac_simps)  nipkow@13189  1367  qed  nipkow@13189  1368  qed  nipkow@13189  1369 next  nipkow@13189  1370  assume Q: ?Q  nipkow@13189  1371  show ?P  nipkow@13189  1372  proof (cases)  nipkow@13189  1373  assume "k = 0"  haftmann@27651  1374  with Q show ?P by simp  nipkow@13189  1375  next  nipkow@13189  1376  assume not0: "k \ 0"  nipkow@13189  1377  with Q have R: ?R by simp  nipkow@13189  1378  from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]  nipkow@13517  1379  show ?P by simp  nipkow@13189  1380  qed  nipkow@13189  1381 qed  nipkow@13189  1382 berghofe@13882  1383 theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"  huffman@47138  1384  using mod_div_equality [of m n] by arith  huffman@47138  1385 huffman@47138  1386 lemma div_mod_equality': "(m::nat) div n * n = m - m mod n"  huffman@47138  1387  using mod_div_equality [of m n] by arith  huffman@47138  1388 (* FIXME: very similar to mult_div_cancel *)  haftmann@22800  1389 noschinl@52398  1390 lemma div_eq_dividend_iff: "a \ 0 \ (a :: nat) div b = a \ b = 1"  noschinl@52398  1391  apply rule  noschinl@52398  1392  apply (cases "b = 0")  noschinl@52398  1393  apply simp_all  noschinl@52398  1394  apply (metis (full_types) One_nat_def Suc_lessI div_less_dividend less_not_refl3)  noschinl@52398  1395  done  noschinl@52398  1396 haftmann@22800  1397 huffman@46551  1398 subsubsection {* An induction'' law for modulus arithmetic. *}  paulson@14640  1399 paulson@14640  1400 lemma mod_induct_0:  paulson@14640  1401  assumes step: "\i P ((Suc i) mod p)"  paulson@14640  1402  and base: "P i" and i: "i(P 0)"  paulson@14640  1406  from i have p: "0k. 0 \ P (p-k)" (is "\k. ?A k")  paulson@14640  1408  proof  paulson@14640  1409  fix k  paulson@14640  1410  show "?A k"  paulson@14640  1411  proof (induct k)  paulson@14640  1412  show "?A 0" by simp -- "by contradiction"  paulson@14640  1413  next  paulson@14640  1414  fix n  paulson@14640  1415  assume ih: "?A n"  paulson@14640  1416  show "?A (Suc n)"  paulson@14640  1417  proof (clarsimp)  wenzelm@22718  1418  assume y: "P (p - Suc n)"  wenzelm@22718  1419  have n: "Suc n < p"  wenzelm@22718  1420  proof (rule ccontr)  wenzelm@22718  1421  assume "\(Suc n < p)"  wenzelm@22718  1422  hence "p - Suc n = 0"  wenzelm@22718  1423  by simp  wenzelm@22718  1424  with y contra show "False"  wenzelm@22718  1425  by simp  wenzelm@22718  1426  qed  wenzelm@22718  1427  hence n2: "Suc (p - Suc n) = p-n" by arith  wenzelm@22718  1428  from p have "p - Suc n < p" by arith  wenzelm@22718  1429  with y step have z: "P ((Suc (p - Suc n)) mod p)"  wenzelm@22718  1430  by blast  wenzelm@22718  1431  show "False"  wenzelm@22718  1432  proof (cases "n=0")  wenzelm@22718  1433  case True  wenzelm@22718  1434  with z n2 contra show ?thesis by simp  wenzelm@22718  1435  next  wenzelm@22718  1436  case False  wenzelm@22718  1437  with p have "p-n < p" by arith  wenzelm@22718  1438  with z n2 False ih show ?thesis by simp  wenzelm@22718  1439  qed  paulson@14640  1440  qed  paulson@14640  1441  qed  paulson@14640  1442  qed  paulson@14640  1443  moreover  paulson@14640  1444  from i obtain k where "0 i+k=p"  paulson@14640  1445  by (blast dest: less_imp_add_positive)  paulson@14640  1446  hence "0 i=p-k" by auto  paulson@14640  1447  moreover  paulson@14640  1448  note base  paulson@14640  1449  ultimately  paulson@14640  1450  show "False" by blast  paulson@14640  1451 qed  paulson@14640  1452 paulson@14640  1453 lemma mod_induct:  paulson@14640  1454  assumes step: "\i P ((Suc i) mod p)"  paulson@14640  1455  and base: "P i" and i: "ij P j" (is "?A j")  paulson@14640  1462  proof (induct j)  paulson@14640  1463  from step base i show "?A 0"  wenzelm@22718  1464  by (auto elim: mod_induct_0)  paulson@14640  1465  next  paulson@14640  1466  fix k  paulson@14640  1467  assume ih: "?A k"  paulson@14640  1468  show "?A (Suc k)"  paulson@14640  1469  proof  wenzelm@22718  1470  assume suc: "Suc k < p"  wenzelm@22718  1471  hence k: "knat) mod 2 \ m mod 2 = 1"  haftmann@33296  1496 proof -  boehmes@35815  1497  { fix n :: nat have "(n::nat) < 2 \ n = 0 \ n = 1" by (cases n) simp_all }  haftmann@33296  1498  moreover have "m mod 2 < 2" by simp  haftmann@33296  1499  ultimately have "m mod 2 = 0 \ m mod 2 = 1" .  haftmann@33296  1500  then show ?thesis by auto  haftmann@33296  1501 qed  haftmann@33296  1502 haftmann@33296  1503 text{*These lemmas collapse some needless occurrences of Suc:  haftmann@33296  1504  at least three Sucs, since two and fewer are rewritten back to Suc again!  haftmann@33296  1505  We already have some rules to simplify operands smaller than 3.*}  haftmann@33296  1506 haftmann@33296  1507 lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"  haftmann@33296  1508 by (simp add: Suc3_eq_add_3)  haftmann@33296  1509 haftmann@33296  1510 lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"  haftmann@33296  1511 by (simp add: Suc3_eq_add_3)  haftmann@33296  1512 haftmann@33296  1513 lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"  haftmann@33296  1514 by (simp add: Suc3_eq_add_3)  haftmann@33296  1515 haftmann@33296  1516 lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"  haftmann@33296  1517 by (simp add: Suc3_eq_add_3)  haftmann@33296  1518 huffman@47108  1519 lemmas Suc_div_eq_add3_div_numeral [simp] = Suc_div_eq_add3_div [of _ "numeral v"] for v  huffman@47108  1520 lemmas Suc_mod_eq_add3_mod_numeral [simp] = Suc_mod_eq_add3_mod [of _ "numeral v"] for v  haftmann@33296  1521 lp15@60562  1522 lemma Suc_times_mod_eq: "1 Suc (k * m) mod k = 1"  haftmann@33361  1523 apply (induct "m")  haftmann@33361  1524 apply (simp_all add: mod_Suc)  haftmann@33361  1525 done  haftmann@33361  1526 huffman@47108  1527 declare Suc_times_mod_eq [of "numeral w", simp] for w  haftmann@33361  1528 huffman@47138  1529 lemma Suc_div_le_mono [simp]: "n div k \ (Suc n) div k"  huffman@47138  1530 by (simp add: div_le_mono)  haftmann@33361  1531 haftmann@33361  1532 lemma Suc_n_div_2_gt_zero [simp]: "(0::nat) < n ==> 0 < (n + 1) div 2"  haftmann@33361  1533 by (cases n) simp_all  haftmann@33361  1534 boehmes@35815  1535 lemma div_2_gt_zero [simp]: assumes A: "(1::nat) < n" shows "0 < n div 2"  boehmes@35815  1536 proof -  boehmes@35815  1537  from A have B: "0 < n - 1" and C: "n - 1 + 1 = n" by simp_all  lp15@60562  1538  from Suc_n_div_2_gt_zero [OF B] C show ?thesis by simp  boehmes@35815  1539 qed  haftmann@33361  1540 haftmann@33361  1541 lemma mod_mult_self4 [simp]: "Suc (k*n + m) mod n = Suc m mod n"  haftmann@33361  1542 proof -  haftmann@33361  1543  have "Suc (k * n + m) mod n = (k * n + Suc m) mod n" by simp  lp15@60562  1544  also have "... = Suc m mod n" by (rule mod_mult_self3)  haftmann@33361  1545  finally show ?thesis .  haftmann@33361  1546 qed  haftmann@33361  1547 haftmann@33361  1548 lemma mod_Suc_eq_Suc_mod: "Suc m mod n = Suc (m mod n) mod n"  lp15@60562  1549 apply (subst mod_Suc [of m])  lp15@60562  1550 apply (subst mod_Suc [of "m mod n"], simp)  haftmann@33361  1551 done  haftmann@33361  1552 huffman@47108  1553 lemma mod_2_not_eq_zero_eq_one_nat:  huffman@47108  1554  fixes n :: nat  huffman@47108  1555  shows "n mod 2 \ 0 \ n mod 2 = 1"  haftmann@58786  1556  by (fact not_mod_2_eq_0_eq_1)  lp15@60562  1557 haftmann@58778  1558 lemma even_Suc_div_two [simp]:  haftmann@58778  1559  "even n \ Suc n div 2 = n div 2"  haftmann@58778  1560  using even_succ_div_two [of n] by simp  lp15@60562  1561 haftmann@58778  1562 lemma odd_Suc_div_two [simp]:  haftmann@58778  1563  "odd n \ Suc n div 2 = Suc (n div 2)"  haftmann@58778  1564  using odd_succ_div_two [of n] by simp  haftmann@58778  1565 haftmann@58834  1566 lemma odd_two_times_div_two_nat [simp]:  haftmann@60352  1567  assumes "odd n"  haftmann@60352  1568  shows "2 * (n div 2) = n - (1 :: nat)"  haftmann@60352  1569 proof -  haftmann@60352  1570  from assms have "2 * (n div 2) + 1 = n"  haftmann@60352  1571  by (rule odd_two_times_div_two_succ)  haftmann@60352  1572  then have "Suc (2 * (n div 2)) - 1 = n - 1"  haftmann@60352  1573  by simp  haftmann@60352  1574  then show ?thesis  haftmann@60352  1575  by simp  haftmann@60352  1576 qed  haftmann@58778  1577 haftmann@58834  1578 lemma odd_Suc_minus_one [simp]:  haftmann@58834  1579  "odd n \ Suc (n - Suc 0) = n"  haftmann@58834  1580  by (auto elim: oddE)  haftmann@58834  1581 haftmann@58778  1582 lemma parity_induct [case_names zero even odd]:  haftmann@58778  1583  assumes zero: "P 0"  haftmann@58778  1584  assumes even: "\n. P n \ P (2 * n)"  haftmann@58778  1585  assumes odd: "\n. P n \ P (Suc (2 * n))"  haftmann@58778  1586  shows "P n"  haftmann@58778  1587 proof (induct n rule: less_induct)  haftmann@58778  1588  case (less n)  haftmann@58778  1589  show "P n"  haftmann@58778  1590  proof (cases "n = 0")  haftmann@58778  1591  case True with zero show ?thesis by simp  haftmann@58778  1592  next  haftmann@58778  1593  case False  haftmann@58778  1594  with less have hyp: "P (n div 2)" by simp  haftmann@58778  1595  show ?thesis  haftmann@58778  1596  proof (cases "even n")  haftmann@58778  1597  case True  haftmann@58778  1598  with hyp even [of "n div 2"] show ?thesis  haftmann@58834  1599  by simp  haftmann@58778  1600  next  haftmann@58778  1601  case False  lp15@60562  1602  with hyp odd [of "n div 2"] show ?thesis  haftmann@58834  1603  by simp  haftmann@58778  1604  qed  haftmann@58778  1605  qed  haftmann@58778  1606 qed  haftmann@58778  1607 haftmann@33361  1608 haftmann@33361  1609 subsection {* Division on @{typ int} *}  haftmann@33361  1610 haftmann@33361  1611 definition divmod_int_rel :: "int \ int \ int \ int \ bool" where  haftmann@33361  1612  --{*definition of quotient and remainder*}  huffman@47139  1613  "divmod_int_rel a b = (\(q, r). a = b * q + r \  huffman@47139  1614  (if 0 < b then 0 \ r \ r < b else if b < 0 then b < r \ r \ 0 else q = 0))"  haftmann@33361  1615 haftmann@53067  1616 text {*  haftmann@53067  1617  The following algorithmic devlopment actually echos what has already  haftmann@53067  1618  been developed in class @{class semiring_numeral_div}. In the long  haftmann@53067  1619  run it seems better to derive division on @{typ int} just from  haftmann@53067  1620  division on @{typ nat} and instantiate @{class semiring_numeral_div}  haftmann@53067  1621  accordingly.  haftmann@53067  1622 *}  haftmann@53067  1623 haftmann@33361  1624 definition adjust :: "int \ int \ int \ int \ int" where  haftmann@33361  1625  --{*for the division algorithm*}  huffman@47108  1626  "adjust b = (\(q, r). if 0 \ r - b then (2 * q + 1, r - b)  haftmann@33361  1627  else (2 * q, r))"  haftmann@33361  1628 haftmann@33361  1629 text{*algorithm for the case @{text "a\0, b>0"}*}  haftmann@33361  1630 function posDivAlg :: "int \ int \ int \ int" where  haftmann@33361  1631  "posDivAlg a b = (if a < b \ b \ 0 then (0, a)  haftmann@33361  1632  else adjust b (posDivAlg a (2 * b)))"  haftmann@33361  1633 by auto  haftmann@33361  1634 termination by (relation "measure (\(a, b). nat (a - b + 1))")  haftmann@33361  1635  (auto simp add: mult_2)  haftmann@33361  1636 haftmann@33361  1637 text{*algorithm for the case @{text "a<0, b>0"}*}  haftmann@33361  1638 function negDivAlg :: "int \ int \ int \ int" where  haftmann@33361  1639  "negDivAlg a b = (if 0 \a + b \ b \ 0 then (-1, a + b)  haftmann@33361  1640  else adjust b (negDivAlg a (2 * b)))"  haftmann@33361  1641 by auto  haftmann@33361  1642 termination by (relation "measure (\(a, b). nat (- a - b))")  haftmann@33361  1643  (auto simp add: mult_2)  haftmann@33361  1644 haftmann@33361  1645 text{*algorithm for the general case @{term "b\0"}*}  haftmann@33361  1646 haftmann@33361  1647 definition divmod_int :: "int \ int \ int \ int" where  haftmann@33361  1648  --{*The full division algorithm considers all possible signs for a, b  lp15@60562  1649  including the special case @{text "a=0, b<0"} because  haftmann@33361  1650  @{term negDivAlg} requires @{term "a<0"}.*}  haftmann@33361  1651  "divmod_int a b = (if 0 \ a then if 0 \ b then posDivAlg a b  haftmann@33361  1652  else if a = 0 then (0, 0)  huffman@46560  1653  else apsnd uminus (negDivAlg (-a) (-b))  lp15@60562  1654  else  haftmann@33361  1655  if 0 < b then negDivAlg a b  huffman@46560  1656  else apsnd uminus (posDivAlg (-a) (-b)))"  haftmann@33361  1657 haftmann@60429  1658 instantiation int :: ring_div  haftmann@33361  1659 begin  haftmann@33361  1660 haftmann@60352  1661 definition divide_int where  haftmann@60429  1662  div_int_def: "a div b = fst (divmod_int a b)"  haftmann@60352  1663 haftmann@60352  1664 definition mod_int where  haftmann@60352  1665  "a mod b = snd (divmod_int a b)"  haftmann@60352  1666 huffman@46551  1667 lemma fst_divmod_int [simp]:  huffman@46551  1668  "fst (divmod_int a b) = a div b"  huffman@46551  1669  by (simp add: div_int_def)  huffman@46551  1670 huffman@46551  1671 lemma snd_divmod_int [simp]:  huffman@46551  1672  "snd (divmod_int a b) = a mod b"  huffman@46551  1673  by (simp add: mod_int_def)  huffman@46551  1674 haftmann@33361  1675 lemma divmod_int_mod_div:  haftmann@33361  1676  "divmod_int p q = (p div q, p mod q)"  huffman@46551  1677  by (simp add: prod_eq_iff)  haftmann@33361  1678 haftmann@33361  1679 text{*  haftmann@33361  1680 Here is the division algorithm in ML:  haftmann@33361  1681 haftmann@33361  1682 \begin{verbatim}  haftmann@33361  1683  fun posDivAlg (a,b) =  haftmann@33361  1684  if ar-b then (2*q+1, r-b) else (2*q, r)  haftmann@33361  1687  end  haftmann@33361  1688 haftmann@33361  1689  fun negDivAlg (a,b) =  haftmann@33361  1690  if 0\a+b then (~1,a+b)  haftmann@33361  1691  else let val (q,r) = negDivAlg(a, 2*b)  haftmann@33361  1692  in if 0\r-b then (2*q+1, r-b) else (2*q, r)  haftmann@33361  1693  end;  haftmann@33361  1694 haftmann@33361  1695  fun negateSnd (q,r:int) = (q,~r);  haftmann@33361  1696 lp15@60562  1697  fun divmod (a,b) = if 0\a then  lp15@60562  1698  if b>0 then posDivAlg (a,b)  haftmann@33361  1699  else if a=0 then (0,0)  haftmann@33361  1700  else negateSnd (negDivAlg (~a,~b))  lp15@60562  1701  else  haftmann@33361  1702  if 0 b*q + r; 0 \ r'; r' < b; r < b |]  haftmann@33361  1712  ==> q' \ (q::int)"  haftmann@33361  1713 apply (subgoal_tac "r' + b * (q'-q) \ r")  haftmann@33361  1714  prefer 2 apply (simp add: right_diff_distrib)  haftmann@33361  1715 apply (subgoal_tac "0 < b * (1 + q - q') ")  haftmann@33361  1716 apply (erule_tac [2] order_le_less_trans)  webertj@49962  1717  prefer 2 apply (simp add: right_diff_distrib distrib_left)  haftmann@33361  1718 apply (subgoal_tac "b * q' < b * (1 + q) ")  webertj@49962  1719  prefer 2 apply (simp add: right_diff_distrib distrib_left)  haftmann@33361  1720 apply (simp add: mult_less_cancel_left)  haftmann@33361  1721 done  haftmann@33361  1722 haftmann@33361  1723 lemma unique_quotient_lemma_neg:  lp15@60562  1724  "[| b*q' + r' \ b*q + r; r \ 0; b < r; b < r' |]  haftmann@33361  1725  ==> q \ (q'::int)"  lp15@60562  1726 by (rule_tac b = "-b" and r = "-r'" and r' = "-r" in unique_quotient_lemma,  haftmann@33361  1727  auto)  haftmann@33361  1728 haftmann@33361  1729 lemma unique_quotient:  lp15@60562  1730  "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r') |]  haftmann@33361  1731  ==> q = q'"  haftmann@33361  1732 apply (simp add: divmod_int_rel_def linorder_neq_iff split: split_if_asm)  haftmann@33361  1733 apply (blast intro: order_antisym  lp15@60562  1734  dest: order_eq_refl [THEN unique_quotient_lemma]  haftmann@33361  1735  order_eq_refl [THEN unique_quotient_lemma_neg] sym)+  haftmann@33361  1736 done  haftmann@33361  1737 haftmann@33361  1738 haftmann@33361  1739 lemma unique_remainder:  lp15@60562  1740  "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r') |]  haftmann@33361  1741  ==> r = r'"  haftmann@33361  1742 apply (subgoal_tac "q = q'")  haftmann@33361  1743  apply (simp add: divmod_int_rel_def)  haftmann@33361  1744 apply (blast intro: unique_quotient)  haftmann@33361  1745 done  haftmann@33361  1746 haftmann@33361  1747 huffman@46551  1748 subsubsection {* Correctness of @{term posDivAlg}, the Algorithm for Non-Negative Dividends *}  haftmann@33361  1749 haftmann@33361  1750 text{*And positive divisors*}  haftmann@33361  1751 haftmann@33361  1752 lemma adjust_eq [simp]:  lp15@60562  1753  "adjust b (q, r) =  lp15@60562  1754  (let diff = r - b in  lp15@60562  1755  if 0 \ diff then (2 * q + 1, diff)  haftmann@33361  1756  else (2*q, r))"  huffman@47108  1757  by (simp add: Let_def adjust_def)  haftmann@33361  1758 haftmann@33361  1759 declare posDivAlg.simps [simp del]  haftmann@33361  1760 haftmann@33361  1761 text{*use with a simproc to avoid repeatedly proving the premise*}  haftmann@33361  1762 lemma posDivAlg_eqn:  lp15@60562  1763  "0 < b ==>  haftmann@33361  1764  posDivAlg a b = (if a a" and "0 < b"  haftmann@33361  1770  shows "divmod_int_rel a b (posDivAlg a b)"  wenzelm@41550  1771  using assms  wenzelm@41550  1772  apply (induct a b rule: posDivAlg.induct)  wenzelm@41550  1773  apply auto  wenzelm@41550  1774  apply (simp add: divmod_int_rel_def)  webertj@49962  1775  apply (subst posDivAlg_eqn, simp add: distrib_left)  wenzelm@41550  1776  apply (case_tac "a < b")  wenzelm@41550  1777  apply simp_all  wenzelm@41550  1778  apply (erule splitE)  haftmann@57514  1779  apply (auto simp add: distrib_left Let_def ac_simps mult_2_right)  wenzelm@41550  1780  done  haftmann@33361  1781 haftmann@33361  1782 huffman@46551  1783 subsubsection {* Correctness of @{term negDivAlg}, the Algorithm for Negative Dividends *}  haftmann@33361  1784 haftmann@33361  1785 text{*And positive divisors*}  haftmann@33361  1786 haftmann@33361  1787 declare negDivAlg.simps [simp del]  haftmann@33361  1788 haftmann@33361  1789 text{*use with a simproc to avoid repeatedly proving the premise*}  haftmann@33361  1790 lemma negDivAlg_eqn:  lp15@60562  1791  "0 < b ==>  lp15@60562  1792  negDivAlg a b =  haftmann@33361  1793  (if 0\a+b then (-1,a+b) else adjust b (negDivAlg a (2*b)))"  haftmann@33361  1794 by (rule negDivAlg.simps [THEN trans], simp)  haftmann@33361  1795 haftmann@33361  1796 (*Correctness of negDivAlg: it computes quotients correctly  haftmann@33361  1797  It doesn't work if a=0 because the 0/b equals 0, not -1*)  haftmann@33361  1798 lemma negDivAlg_correct:  haftmann@33361  1799  assumes "a < 0" and "b > 0"  haftmann@33361  1800  shows "divmod_int_rel a b (negDivAlg a b)"  wenzelm@41550  1801  using assms  wenzelm@41550  1802  apply (induct a b rule: negDivAlg.induct)  wenzelm@41550  1803  apply (auto simp add: linorder_not_le)  wenzelm@41550  1804  apply (simp add: divmod_int_rel_def)  wenzelm@41550  1805  apply (subst negDivAlg_eqn, assumption)  wenzelm@41550  1806  apply (case_tac "a + b < (0\int)")  wenzelm@41550  1807  apply simp_all  wenzelm@41550  1808  apply (erule splitE)  haftmann@57514  1809  apply (auto simp add: distrib_left Let_def ac_simps mult_2_right)  wenzelm@41550  1810  done  haftmann@33361  1811 haftmann@33361  1812 huffman@46551  1813 subsubsection {* Existence Shown by Proving the Division Algorithm to be Correct *}  haftmann@33361  1814 haftmann@33361  1815 (*the case a=0*)  huffman@47139  1816 lemma divmod_int_rel_0: "divmod_int_rel 0 b (0, 0)"  haftmann@33361  1817 by (auto simp add: divmod_int_rel_def linorder_neq_iff)  haftmann@33361  1818 haftmann@33361  1819 lemma posDivAlg_0 [simp]: "posDivAlg 0 b = (0, 0)"  haftmann@33361  1820 by (subst posDivAlg.simps, auto)  haftmann@33361  1821 huffman@47139  1822 lemma posDivAlg_0_right [simp]: "posDivAlg a 0 = (0, a)"  huffman@47139  1823 by (subst posDivAlg.simps, auto)  huffman@47139  1824 haftmann@58410  1825 lemma negDivAlg_minus1 [simp]: "negDivAlg (- 1) b = (- 1, b - 1)"  haftmann@33361  1826 by (subst negDivAlg.simps, auto)  haftmann@33361  1827 huffman@46560  1828 lemma divmod_int_rel_neg: "divmod_int_rel (-a) (-b) qr ==> divmod_int_rel a b (apsnd uminus qr)"  huffman@47139  1829 by (auto simp add: divmod_int_rel_def)  huffman@47139  1830 huffman@47139  1831 lemma divmod_int_correct: "divmod_int_rel a b (divmod_int a b)"  huffman@47139  1832 apply (cases "b = 0", simp add: divmod_int_def divmod_int_rel_def)  haftmann@33361  1833 by (force simp add: linorder_neq_iff divmod_int_rel_0 divmod_int_def divmod_int_rel_neg  haftmann@33361  1834  posDivAlg_correct negDivAlg_correct)  haftmann@33361  1835 huffman@47141  1836 lemma divmod_int_unique:  lp15@60562  1837  assumes "divmod_int_rel a b qr"  huffman@47141  1838  shows "divmod_int a b = qr"  huffman@47141  1839  using assms divmod_int_correct [of a b]  huffman@47141  1840  using unique_quotient [of a b] unique_remainder [of a b]  huffman@47141  1841  by (metis pair_collapse)  huffman@47141  1842 huffman@47141  1843 lemma divmod_int_rel_div_mod: "divmod_int_rel a b (a div b, a mod b)"  huffman@47141  1844  using divmod_int_correct by (simp add: divmod_int_mod_div)  huffman@47141  1845 huffman@47141  1846 lemma div_int_unique: "divmod_int_rel a b (q, r) \ a div b = q"  huffman@47141  1847  by (simp add: divmod_int_rel_div_mod [THEN unique_quotient])  huffman@47141  1848 huffman@47141  1849 lemma mod_int_unique: "divmod_int_rel a b (q, r) \ a mod b = r"  huffman@47141  1850  by (simp add: divmod_int_rel_div_mod [THEN unique_remainder])  huffman@47141  1851 haftmann@60429  1852 instance  huffman@47141  1853 proof  huffman@47141  1854  fix a b :: int  huffman@47141  1855  show "a div b * b + a mod b = a"  huffman@47141  1856  using divmod_int_rel_div_mod [of a b]  haftmann@57512  1857  unfolding divmod_int_rel_def by (simp add: mult.commute)  huffman@47141  1858 next  huffman@47141  1859  fix a b c :: int  huffman@47141  1860  assume "b \ 0"  huffman@47141  1861  hence "divmod_int_rel (a + c * b) b (c + a div b, a mod b)"  huffman@47141  1862  using divmod_int_rel_div_mod [of a b]  huffman@47141  1863  unfolding divmod_int_rel_def by (auto simp: algebra_simps)  huffman@47141  1864  thus "(a + c * b) div b = c + a div b"  huffman@47141  1865  by (rule div_int_unique)  huffman@47141  1866 next  huffman@47141  1867  fix a b c :: int  huffman@47141  1868  assume "c \ 0"  huffman@47141  1869  hence "\q r. divmod_int_rel a b (q, r)  huffman@47141  1870  \ divmod_int_rel (c * a) (c * b) (q, c * r)"  huffman@47141  1871  unfolding divmod_int_rel_def  huffman@47141  1872  by - (rule linorder_cases [of 0 b], auto simp: algebra_simps  huffman@47141  1873  mult_less_0_iff zero_less_mult_iff mult_strict_right_mono  huffman@47141  1874  mult_strict_right_mono_neg zero_le_mult_iff mult_le_0_iff)  huffman@47141  1875  hence "divmod_int_rel (c * a) (c * b) (a div b, c * (a mod b))"  huffman@47141  1876  using divmod_int_rel_div_mod [of a b] .  huffman@47141  1877  thus "(c * a) div (c * b) = a div b"  huffman@47141  1878  by (rule div_int_unique)  huffman@47141  1879 next  huffman@47141  1880  fix a :: int show "a div 0 = 0"  huffman@47141  1881  by (rule div_int_unique, simp add: divmod_int_rel_def)  huffman@47141  1882 next  huffman@47141  1883  fix a :: int show "0 div a = 0"  huffman@47141  1884  by (rule div_int_unique, auto simp add: divmod_int_rel_def)  huffman@47141  1885 qed  huffman@47141  1886 haftmann@60429  1887 end  haftmann@60429  1888 haftmann@60517  1889 lemma is_unit_int:  haftmann@60517  1890  "is_unit (k::int) \ k = 1 \ k = - 1"  haftmann@60517  1891  by auto  haftmann@60517  1892 haftmann@33361  1893 text{*Basic laws about division and remainder*}  haftmann@33361  1894 haftmann@33361  1895 lemma zmod_zdiv_equality: "(a::int) = b * (a div b) + (a mod b)"  huffman@47141  1896  by (fact mod_div_equality2 [symmetric])  haftmann@33361  1897 haftmann@33361  1898 text {* Tool setup *}  haftmann@33361  1899 haftmann@33361  1900 ML {*  wenzelm@43594  1901 structure Cancel_Div_Mod_Int = Cancel_Div_Mod  wenzelm@41550  1902 (  haftmann@60352  1903  val div_name = @{const_name Rings.divide};  haftmann@33361  1904  val mod_name = @{const_name mod};  haftmann@33361  1905  val mk_binop = HOLogic.mk_binop;  haftmann@33361  1906  val mk_sum = Arith_Data.mk_sum HOLogic.intT;  haftmann@33361  1907  val dest_sum = Arith_Data.dest_sum;  haftmann@33361  1908 huffman@47165  1909  val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];  haftmann@33361  1910 lp15@60562  1911  val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac  haftmann@59556  1912  (@{thm diff_conv_add_uminus} :: @{thms add_0_left add_0_right} @ @{thms ac_simps}))  wenzelm@41550  1913 )  haftmann@33361  1914 *}  haftmann@33361  1915 wenzelm@43594  1916 simproc_setup cancel_div_mod_int ("(k::int) + l") = {* K Cancel_Div_Mod_Int.proc *}  wenzelm@43594  1917 huffman@47141  1918 lemma pos_mod_conj: "(0::int) < b \ 0 \ a mod b \ a mod b < b"  huffman@47141  1919  using divmod_int_correct [of a b]  huffman@47141  1920  by (auto simp add: divmod_int_rel_def prod_eq_iff)  haftmann@33361  1921 wenzelm@45607  1922 lemmas pos_mod_sign [simp] = pos_mod_conj [THEN conjunct1]  wenzelm@45607  1923  and pos_mod_bound [simp] = pos_mod_conj [THEN conjunct2]  haftmann@33361  1924 huffman@47141  1925 lemma neg_mod_conj: "b < (0::int) \ a mod b \ 0 \ b < a mod b"  huffman@47141  1926  using divmod_int_correct [of a b]  huffman@47141  1927  by (auto simp add: divmod_int_rel_def prod_eq_iff)  haftmann@33361  1928 wenzelm@45607  1929 lemmas neg_mod_sign [simp] = neg_mod_conj [THEN conjunct1]  wenzelm@45607  1930  and neg_mod_bound [simp] = neg_mod_conj [THEN conjunct2]  haftmann@33361  1931 haftmann@33361  1932 huffman@46551  1933 subsubsection {* General Properties of div and mod *}  haftmann@33361  1934 haftmann@33361  1935 lemma div_pos_pos_trivial: "[| (0::int) \ a; a < b |] ==> a div b = 0"  huffman@47140  1936 apply (rule div_int_unique)  haftmann@33361  1937 apply (auto simp add: divmod_int_rel_def)  haftmann@33361  1938 done  haftmann@33361  1939 haftmann@33361  1940 lemma div_neg_neg_trivial: "[| a \ (0::int); b < a |] ==> a div b = 0"  huffman@47140  1941 apply (rule div_int_unique)  haftmann@33361  1942 apply (auto simp add: divmod_int_rel_def)  haftmann@33361  1943 done  haftmann@33361  1944 haftmann@33361  1945 lemma div_pos_neg_trivial: "[| (0::int) < a; a+b \ 0 |] ==> a div b = -1"  huffman@47140  1946 apply (rule div_int_unique)  haftmann@33361  1947 apply (auto simp add: divmod_int_rel_def)  haftmann@33361  1948 done  haftmann@33361  1949 haftmann@33361  1950 (*There is no div_neg_pos_trivial because 0 div b = 0 would supersede it*)  haftmann@33361  1951 haftmann@33361  1952 lemma mod_pos_pos_trivial: "[| (0::int) \ a; a < b |] ==> a mod b = a"  huffman@47140  1953 apply (rule_tac q = 0 in mod_int_unique)  haftmann@33361  1954 apply (auto simp add: divmod_int_rel_def)  haftmann@33361  1955 done  haftmann@33361  1956 haftmann@33361  1957 lemma mod_neg_neg_trivial: "[| a \ (0::int); b < a |] ==> a mod b = a"  huffman@47140  1958 apply (rule_tac q = 0 in mod_int_unique)  haftmann@33361  1959 apply (auto simp add: divmod_int_rel_def)  haftmann@33361  1960 done  haftmann@33361  1961 haftmann@33361  1962 lemma mod_pos_neg_trivial: "[| (0::int) < a; a+b \ 0 |] ==> a mod b = a+b"  huffman@47140  1963 apply (rule_tac q = "-1" in mod_int_unique)  haftmann@33361  1964 apply (auto simp add: divmod_int_rel_def)  haftmann@33361  1965 done  haftmann@33361  1966 haftmann@33361  1967 text{*There is no @{text mod_neg_pos_trivial}.*}  haftmann@33361  1968 haftmann@33361  1969 huffman@46551  1970 subsubsection {* Laws for div and mod with Unary Minus *}  haftmann@33361  1971 haftmann@33361  1972 lemma zminus1_lemma:  huffman@47139  1973  "divmod_int_rel a b (q, r) ==> b \ 0  lp15@60562  1974  ==> divmod_int_rel (-a) b (if r=0 then -q else -q - 1,  haftmann@33361  1975  if r=0 then 0 else b-r)"  haftmann@33361  1976 by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_diff_distrib)  haftmann@33361  1977 haftmann@33361  1978 haftmann@33361  1979 lemma zdiv_zminus1_eq_if:  lp15@60562  1980  "b \ (0::int)  lp15@60562  1981  ==> (-a) div b =  haftmann@33361  1982  (if a mod b = 0 then - (a div b) else - (a div b) - 1)"  huffman@47140  1983 by (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN div_int_unique])  haftmann@33361  1984 haftmann@33361  1985 lemma zmod_zminus1_eq_if:  haftmann@33361  1986  "(-a::int) mod b = (if a mod b = 0 then 0 else b - (a mod b))"  haftmann@33361  1987 apply (case_tac "b = 0", simp)  huffman@47140  1988 apply (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN mod_int_unique])  haftmann@33361  1989 done  haftmann@33361  1990 haftmann@33361  1991 lemma zmod_zminus1_not_zero:  haftmann@33361  1992  fixes k l :: int  haftmann@33361  1993  shows "- k mod l \ 0 \ k mod l \ 0"  haftmann@33361  1994  unfolding zmod_zminus1_eq_if by auto  haftmann@33361  1995 haftmann@33361  1996 lemma zdiv_zminus2_eq_if:  lp15@60562  1997  "b \ (0::int)  lp15@60562  1998  ==> a div (-b) =  haftmann@33361  1999  (if a mod b = 0 then - (a div b) else - (a div b) - 1)"  huffman@47159  2000 by (simp add: zdiv_zminus1_eq_if div_minus_right)  haftmann@33361  2001 haftmann@33361  2002 lemma zmod_zminus2_eq_if:  haftmann@33361  2003  "a mod (-b::int) = (if a mod b = 0 then 0 else (a mod b) - b)"  huffman@47159  2004 by (simp add: zmod_zminus1_eq_if mod_minus_right)  haftmann@33361  2005 haftmann@33361  2006 lemma zmod_zminus2_not_zero:  haftmann@33361  2007  fixes k l :: int  haftmann@33361  2008  shows "k mod - l \ 0 \ k mod l \ 0"  lp15@60562  2009  unfolding zmod_zminus2_eq_if by auto  haftmann@33361  2010 haftmann@33361  2011 huffman@46551  2012 subsubsection {* Computation of Division and Remainder *}  haftmann@33361  2013 haftmann@33361  2014 lemma div_eq_minus1: "(0::int) < b ==> -1 div b = -1"  haftmann@33361  2015 by (simp add: div_int_def divmod_int_def)  haftmann@33361  2016 haftmann@33361  2017 lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1"  haftmann@33361  2018 by (simp add: mod_int_def divmod_int_def)  haftmann@33361  2019 haftmann@33361  2020 text{*a positive, b positive *}  haftmann@33361  2021 haftmann@33361  2022 lemma div_pos_pos: "[| 0 < a; 0 \ b |] ==> a div b = fst (posDivAlg a b)"  haftmann@33361  2023 by (simp add: div_int_def divmod_int_def)  haftmann@33361  2024 haftmann@33361  2025 lemma mod_pos_pos: "[| 0 < a; 0 \ b |] ==> a mod b = snd (posDivAlg a b)"  haftmann@33361  2026 by (simp add: mod_int_def divmod_int_def)  haftmann@33361  2027 haftmann@33361  2028 text{*a negative, b positive *}  haftmann@33361  2029 haftmann@33361  2030 lemma div_neg_pos: "[| a < 0; 0 < b |] ==> a div b = fst (negDivAlg a b)"  haftmann@33361  2031 by (simp add: div_int_def divmod_int_def)  haftmann@33361  2032 haftmann@33361  2033 lemma mod_neg_pos: "[| a < 0; 0 < b |] ==> a mod b = snd (negDivAlg a b)"  haftmann@33361  2034 by (simp add: mod_int_def divmod_int_def)  haftmann@33361  2035 haftmann@33361  2036 text{*a positive, b negative *}  haftmann@33361  2037 haftmann@33361  2038 lemma div_pos_neg:  huffman@46560  2039  "[| 0 < a; b < 0 |] ==> a div b = fst (apsnd uminus (negDivAlg (-a) (-b)))"  haftmann@33361  2040 by (simp add: div_int_def divmod_int_def)  haftmann@33361  2041 haftmann@33361  2042 lemma mod_pos_neg:  huffman@46560  2043  "[| 0 < a; b < 0 |] ==> a mod b = snd (apsnd uminus (negDivAlg (-a) (-b)))"  haftmann@33361  2044 by (simp add: mod_int_def divmod_int_def)  haftmann@33361  2045 haftmann@33361  2046 text{*a negative, b negative *}  haftmann@33361  2047 haftmann@33361  2048 lemma div_neg_neg:  huffman@46560  2049  "[| a < 0; b \ 0 |] ==> a div b = fst (apsnd uminus (posDivAlg (-a) (-b)))"  haftmann@33361  2050 by (simp add: div_int_def divmod_int_def)  haftmann@33361  2051 haftmann@33361  2052 lemma mod_neg_neg:  huffman@46560  2053  "[| a < 0; b \ 0 |] ==> a mod b = snd (apsnd uminus (posDivAlg (-a) (-b)))"  haftmann@33361  2054 by (simp add: mod_int_def divmod_int_def)  haftmann@33361  2055 haftmann@33361  2056 text {*Simplify expresions in which div and mod combine numerical constants*}  haftmann@33361  2057 huffman@45530  2058 lemma int_div_pos_eq: "\(a::int) = b * q + r; 0 \ r; r < b\ \ a div b = q"  huffman@47140  2059  by (rule div_int_unique [of a b q r]) (simp add: divmod_int_rel_def)  huffman@45530  2060 huffman@45530  2061 lemma int_div_neg_eq: "\(a::int) = b * q + r; r \ 0; b < r\ \ a div b = q"  huffman@47140  2062  by (rule div_int_unique [of a b q r],  bulwahn@46552  2063  simp add: divmod_int_rel_def)  huffman@45530  2064 huffman@45530  2065 lemma int_mod_pos_eq: "\(a::int) = b * q + r; 0 \ r; r < b\ \ a mod b = r"  huffman@47140  2066  by (rule mod_int_unique [of a b q r],  bulwahn@46552  2067  simp add: divmod_int_rel_def)  huffman@45530  2068 huffman@45530  2069 lemma int_mod_neg_eq: "\(a::int) = b * q + r; r \ 0; b < r\ \ a mod b = r"  huffman@47140  2070  by (rule mod_int_unique [of a b q r],  bulwahn@46552  2071  simp add: divmod_int_rel_def)  huffman@45530  2072 haftmann@53069  2073 text {*  haftmann@53069  2074  numeral simprocs -- high chance that these can be replaced  haftmann@53069  2075  by divmod algorithm from @{class semiring_numeral_div}  haftmann@53069  2076 *}  haftmann@53069  2077 haftmann@33361  2078 ML {*  haftmann@33361  2079 local  huffman@45530  2080  val mk_number = HOLogic.mk_number HOLogic.intT  huffman@45530  2081  val plus = @{term "plus :: int \ int \ int"}  huffman@45530  2082  val times = @{term "times :: int \ int \ int"}  huffman@45530  2083  val zero = @{term "0 :: int"}  huffman@45530  2084  val less = @{term "op < :: int \ int \ bool"}  huffman@45530  2085  val le = @{term "op \ :: int \ int \ bool"}  haftmann@54489  2086  val simps = @{thms arith_simps} @ @{thms rel_simps} @ [@{thm numeral_1_eq_1 [symmetric]}]  wenzelm@58847  2087  fun prove ctxt goal = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop goal)  wenzelm@58847  2088  (K (ALLGOALS (full_simp_tac (put_simpset HOL_basic_ss ctxt addsimps simps))));  wenzelm@51717  2089  fun binary_proc proc ctxt ct =  haftmann@33361  2090  (case Thm.term_of ct of  haftmann@33361  2091  _ $t$ u =>  wenzelm@59058  2092  (case try (apply2 ((snd o HOLogic.dest_number))) (t, u) of  wenzelm@51717  2093  SOME args => proc ctxt args  haftmann@33361  2094  | NONE => NONE)  haftmann@33361  2095  | _ => NONE);  haftmann@33361  2096 in  huffman@45530  2097  fun divmod_proc posrule negrule =  huffman@45530  2098  binary_proc (fn ctxt => fn ((a, t), (b, u)) =>  wenzelm@59058  2099  if b = 0 then NONE  wenzelm@59058  2100  else  wenzelm@59058  2101  let  wenzelm@59058  2102  val (q, r) = apply2 mk_number (Integer.div_mod a b)  wenzelm@59058  2103  val goal1 = HOLogic.mk_eq (t, plus $(times$ u $q)$ r)  wenzelm@59058  2104  val (goal2, goal3, rule) =  wenzelm@59058  2105  if b > 0  wenzelm@59058  2106  then (le $zero$ r, less $r$ u, posrule RS eq_reflection)  wenzelm@59058  2107  else (le $r$ zero, less $u$ r, negrule RS eq_reflection)  wenzelm@59058  2108  in SOME (rule OF map (prove ctxt) [goal1, goal2, goal3]) end)  haftmann@33361  2109 end  haftmann@33361  2110 *}  haftmann@33361  2111 huffman@47108  2112 simproc_setup binary_int_div  huffman@47108  2113  ("numeral m div numeral n :: int" |  haftmann@54489  2114  "numeral m div - numeral n :: int" |  haftmann@54489  2115  "- numeral m div numeral n :: int" |  haftmann@54489  2116  "- numeral m div - numeral n :: int") =  huffman@45530  2117  {* K (divmod_proc @{thm int_div_pos_eq} @{thm int_div_neg_eq}) *}  haftmann@33361  2118 huffman@47108  2119 simproc_setup binary_int_mod  huffman@47108  2120  ("numeral m mod numeral n :: int" |  haftmann@54489  2121  "numeral m mod - numeral n :: int" |  haftmann@54489  2122  "- numeral m mod numeral n :: int" |  haftmann@54489  2123  "- numeral m mod - numeral n :: int") =  huffman@45530  2124  {* K (divmod_proc @{thm int_mod_pos_eq} @{thm int_mod_neg_eq}) *}  haftmann@33361  2125 huffman@47108  2126 lemmas posDivAlg_eqn_numeral [simp] =  huffman@47108  2127  posDivAlg_eqn [of "numeral v" "numeral w", OF zero_less_numeral] for v w  huffman@47108  2128 huffman@47108  2129 lemmas negDivAlg_eqn_numeral [simp] =  haftmann@54489  2130  negDivAlg_eqn [of "numeral v" "- numeral w", OF zero_less_numeral] for v w  haftmann@33361  2131 haftmann@33361  2132 haftmann@55172  2133 text {* Special-case simplification: @{text "\1 div z"} and @{text "\1 mod z"} *}  haftmann@55172  2134 haftmann@55172  2135 lemma [simp]:  haftmann@55172  2136  shows div_one_bit0: "1 div numeral (Num.Bit0 v) = (0 :: int)"  haftmann@55172  2137  and mod_one_bit0: "1 mod numeral (Num.Bit0 v) = (1 :: int)"  wenzelm@55439  2138  and div_one_bit1: "1 div numeral (Num.Bit1 v) = (0 :: int)"  wenzelm@55439  2139  and mod_one_bit1: "1 mod numeral (Num.Bit1 v) = (1 :: int)"  wenzelm@55439  2140  and div_one_neg_numeral: "1 div - numeral v = (- 1 :: int)"  wenzelm@55439  2141  and mod_one_neg_numeral: "1 mod - numeral v = (1 :: int) - numeral v"  haftmann@55172  2142  by (simp_all del: arith_special  haftmann@55172  2143  add: div_pos_pos mod_pos_pos div_pos_neg mod_pos_neg posDivAlg_eqn)  wenzelm@55439  2144 haftmann@55172  2145 lemma [simp]:  haftmann@55172  2146  shows div_neg_one_numeral: "- 1 div numeral v = (- 1 :: int)"  haftmann@55172  2147  and mod_neg_one_numeral: "- 1 mod numeral v = numeral v - (1 :: int)"  haftmann@55172  2148  and div_neg_one_neg_bit0: "- 1 div - numeral (Num.Bit0 v) = (0 :: int)"  haftmann@55172  2149  and mod_neg_one_neb_bit0: "- 1 mod - numeral (Num.Bit0 v) = (- 1 :: int)"  haftmann@55172  2150  and div_neg_one_neg_bit1: "- 1 div - numeral (Num.Bit1 v) = (0 :: int)"  haftmann@55172  2151  and mod_neg_one_neb_bit1: "- 1 mod - numeral (Num.Bit1 v) = (- 1 :: int)"  haftmann@55172  2152  by (simp_all add: div_eq_minus1 zmod_minus1)  haftmann@33361  2153 haftmann@33361  2154 huffman@46551  2155 subsubsection {* Monotonicity in the First Argument (Dividend) *}  haftmann@33361  2156 haftmann@33361  2157 lemma zdiv_mono1: "[| a \ a'; 0 < (b::int) |] ==> a div b \ a' div b"  haftmann@33361  2158 apply (cut_tac a = a and b = b in zmod_zdiv_equality)  haftmann@33361  2159 apply (cut_tac a = a' and b = b in zmod_zdiv_equality)  haftmann@33361  2160 apply (rule unique_quotient_lemma)  haftmann@33361  2161 apply (erule subst)  haftmann@33361  2162 apply (erule subst, simp_all)  haftmann@33361  2163 done  haftmann@33361  2164 haftmann@33361  2165 lemma zdiv_mono1_neg: "[| a \ a'; (b::int) < 0 |] ==> a' div b \ a div b"  haftmann@33361  2166 apply (cut_tac a = a and b = b in zmod_zdiv_equality)  haftmann@33361  2167 apply (cut_tac a = a' and b = b in zmod_zdiv_equality)  haftmann@33361  2168 apply (rule unique_quotient_lemma_neg)  haftmann@33361  2169 apply (erule subst)  haftmann@33361  2170 apply (erule subst, simp_all)  haftmann@33361  2171 done  haftmann@33361  2172 haftmann@33361  2173 huffman@46551  2174 subsubsection {* Monotonicity in the Second Argument (Divisor) *}  haftmann@33361  2175 haftmann@33361  2176 lemma q_pos_lemma:  haftmann@33361  2177  "[| 0 \ b'*q' + r'; r' < b'; 0 < b' |] ==> 0 \ (q'::int)"  haftmann@33361  2178 apply (subgoal_tac "0 < b'* (q' + 1) ")  haftmann@33361  2179  apply (simp add: zero_less_mult_iff)  webertj@49962  2180 apply (simp add: distrib_left)  haftmann@33361  2181 done  haftmann@33361  2182 haftmann@33361  2183 lemma zdiv_mono2_lemma:  lp15@60562  2184  "[| b*q + r = b'*q' + r'; 0 \ b'*q' + r';  lp15@60562  2185  r' < b'; 0 \ r; 0 < b'; b' \ b |]  haftmann@33361  2186  ==> q \ (q'::int)"  lp15@60562  2187 apply (frule q_pos_lemma, assumption+)  haftmann@33361  2188 apply (subgoal_tac "b*q < b* (q' + 1) ")  haftmann@33361  2189  apply (simp add: mult_less_cancel_left)  haftmann@33361  2190 apply (subgoal_tac "b*q = r' - r + b'*q'")  haftmann@33361  2191  prefer 2 apply simp  webertj@49962  2192 apply (simp (no_asm_simp) add: distrib_left)  haftmann@57512  2193 apply (subst add.commute, rule add_less_le_mono, arith)  haftmann@33361  2194 apply (rule mult_right_mono, auto)  haftmann@33361  2195 done  haftmann@33361  2196 haftmann@33361  2197 lemma zdiv_mono2:  haftmann@33361  2198  "[| (0::int) \ a; 0 < b'; b' \ b |] ==> a div b \ a div b'"  haftmann@33361  2199 apply (subgoal_tac "b \ 0")  haftmann@33361  2200  prefer 2 apply arith  haftmann@33361  2201 apply (cut_tac a = a and b = b in zmod_zdiv_equality)  haftmann@33361  2202 apply (cut_tac a = a and b = b' in zmod_zdiv_equality)  haftmann@33361  2203 apply (rule zdiv_mono2_lemma)  haftmann@33361  2204 apply (erule subst)  haftmann@33361  2205 apply (erule subst, simp_all)  haftmann@33361  2206 done  haftmann@33361  2207 haftmann@33361  2208 lemma q_neg_lemma:  haftmann@33361  2209  "[| b'*q' + r' < 0; 0 \ r'; 0 < b' |] ==> q' \ (0::int)"  haftmann@33361  2210 apply (subgoal_tac "b'*q' < 0")  haftmann@33361  2211  apply (simp add: mult_less_0_iff, arith)  haftmann@33361  2212 done  haftmann@33361  2213 haftmann@33361  2214 lemma zdiv_mono2_neg_lemma:  lp15@60562  2215  "[| b*q + r = b'*q' + r'; b'*q' + r' < 0;  lp15@60562  2216  r < b; 0 \ r'; 0 < b'; b' \ b |]  haftmann@33361  2217  ==> q' \ (q::int)"  lp15@60562  2218 apply (frule q_neg_lemma, assumption+)  haftmann@33361  2219 apply (subgoal_tac "b*q' < b* (q + 1) ")  haftmann@33361  2220  apply (simp add: mult_less_cancel_left)  webertj@49962  2221 apply (simp add: distrib_left)  haftmann@33361  2222 apply (subgoal_tac "b*q' \ b'*q'")  haftmann@33361  2223  prefer 2 apply (simp add: mult_right_mono_neg, arith)  haftmann@33361  2224 done  haftmann@33361  2225 haftmann@33361  2226 lemma zdiv_mono2_neg:  haftmann@33361  2227  "[| a < (0::int); 0 < b'; b' \ b |] ==> a div b' \ a div b"  haftmann@33361  2228 apply (cut_tac a = a and b = b in zmod_zdiv_equality)  haftmann@33361  2229 apply (cut_tac a = a and b = b' in zmod_zdiv_equality)  haftmann@33361  2230 apply (rule zdiv_mono2_neg_lemma)  haftmann@33361  2231 apply (erule subst)  haftmann@33361  2232 apply (erule subst, simp_all)  haftmann@33361  2233 done  haftmann@33361  2234 haftmann@33361  2235 huffman@46551  2236 subsubsection {* More Algebraic Laws for div and mod *}  haftmann@33361  2237 haftmann@33361  2238 text{*proving (a*b) div c = a * (b div c) + a * (b mod c) *}  haftmann@33361  2239 haftmann@33361  2240 lemma zmult1_lemma:  lp15@60562  2241  "[| divmod_int_rel b c (q, r) |]  haftmann@33361  2242  ==> divmod_int_rel (a * b) c (a*q + a*r div c, a*r mod c)"  haftmann@57514  2243 by (auto simp add: split_ifs divmod_int_rel_def linorder_neq_iff distrib_left ac_simps)  haftmann@33361  2244 haftmann@33361  2245 lemma zdiv_zmult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)"  haftmann@33361  2246 apply (case_tac "c = 0", simp)  huffman@47140  2247 apply (blast intro: divmod_int_rel_div_mod [THEN zmult1_lemma, THEN div_int_unique])  haftmann@33361  2248 done  haftmann@33361  2249 haftmann@33361  2250 text{*proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) *}  haftmann@33361  2251 haftmann@33361  2252 lemma zadd1_lemma:  lp15@60562  2253  "[| divmod_int_rel a c (aq, ar); divmod_int_rel b c (bq, br) |]  haftmann@33361  2254  ==> divmod_int_rel (a+b) c (aq + bq + (ar+br) div c, (ar+br) mod c)"  webertj@49962  2255 by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff distrib_left)  haftmann@33361  2256 haftmann@33361  2257 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)  haftmann@33361  2258 lemma zdiv_zadd1_eq:  haftmann@33361  2259  "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)"  haftmann@33361  2260 apply (case_tac "c = 0", simp)  huffman@47140  2261 apply (blast intro: zadd1_lemma [OF divmod_int_rel_div_mod divmod_int_rel_div_mod] div_int_unique)  haftmann@33361  2262 done  haftmann@33361  2263 haftmann@33361  2264 lemma posDivAlg_div_mod:  haftmann@33361  2265  assumes "k \ 0"  haftmann@33361  2266  and "l \ 0"  haftmann@33361  2267  shows "posDivAlg k l = (k div l, k mod l)"  haftmann@33361  2268 proof (cases "l = 0")  haftmann@33361  2269  case True then show ?thesis by (simp add: posDivAlg.simps)  haftmann@33361  2270 next  haftmann@33361  2271  case False with assms posDivAlg_correct  haftmann@33361  2272  have "divmod_int_rel k l (fst (posDivAlg k l), snd (posDivAlg k l))"  haftmann@33361  2273  by simp  huffman@47140  2274  from div_int_unique [OF this] mod_int_unique [OF this]  haftmann@33361  2275  show ?thesis by simp  haftmann@33361  2276 qed  haftmann@33361  2277 haftmann@33361  2278 lemma negDivAlg_div_mod:  haftmann@33361  2279  assumes "k < 0"  haftmann@33361  2280  and "l > 0"  haftmann@33361  2281  shows "negDivAlg k l = (k div l, k mod l)"  haftmann@33361  2282 proof -  haftmann@33361  2283  from assms have "l \ 0" by simp  haftmann@33361  2284  from assms negDivAlg_correct  haftmann@33361  2285  have "divmod_int_rel k l (fst (negDivAlg k l), snd (negDivAlg k l))"  haftmann@33361  2286  by simp  huffman@47140  2287  from div_int_unique [OF this] mod_int_unique [OF this]  haftmann@33361  2288  show ?thesis by simp  haftmann@33361  2289 qed  haftmann@33361  2290 haftmann@33361  2291 lemma zmod_eq_0_iff: "(m mod d = 0) = (EX q::int. m = d*q)"  haftmann@33361  2292 by (simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)  haftmann@33361  2293 haftmann@33361  2294 (* REVISIT: should this be generalized to all semiring_div types? *)  haftmann@33361  2295 lemmas zmod_eq_0D [dest!] = zmod_eq_0_iff [THEN iffD1]  haftmann@33361  2296 huffman@47108  2297 lemma zmod_zdiv_equality':  huffman@47108  2298  "(m\int) mod n = m - (m div n) * n"  huffman@47141  2299  using mod_div_equality [of m n] by arith  huffman@47108  2300 haftmann@33361  2301 blanchet@55085  2302 subsubsection {* Proving @{term "a div (b * c) = (a div b) div c"} *}  haftmann@33361  2303 haftmann@33361  2304 (*The condition c>0 seems necessary. Consider that 7 div ~6 = ~2 but  haftmann@33361  2305  7 div 2 div ~3 = 3 div ~3 = ~1. The subcase (a div b) mod c = 0 seems  haftmann@33361  2306  to cause particular problems.*)  haftmann@33361  2307 haftmann@33361  2308 text{*first, four lemmas to bound the remainder for the cases b<0 and b>0 *}  haftmann@33361  2309 blanchet@55085  2310 lemma zmult2_lemma_aux1: "[| (0::int) < c; b < r; r \ 0 |] ==> b * c < b * (q mod c) + r"  haftmann@33361  2311 apply (subgoal_tac "b * (c - q mod c) < r * 1")  haftmann@33361  2312  apply (simp add: algebra_simps)  haftmann@33361  2313 apply (rule order_le_less_trans)  haftmann@33361  2314  apply (erule_tac [2] mult_strict_right_mono)  haftmann@33361  2315  apply (rule mult_left_mono_neg)  huffman@35216  2316  using add1_zle_eq[of "q mod c"]apply(simp add: algebra_simps)  haftmann@33361  2317  apply (simp)  haftmann@33361  2318 apply (simp)  haftmann@33361  2319 done  haftmann@33361  2320 haftmann@33361  2321 lemma zmult2_lemma_aux2:  haftmann@33361  2322  "[| (0::int) < c; b < r; r \ 0 |] ==> b * (q mod c) + r \ 0"  haftmann@33361  2323 apply (subgoal_tac "b * (q mod c) \ 0")  haftmann@33361  2324  apply arith  haftmann@33361  2325 apply (simp add: mult_le_0_iff)  haftmann@33361  2326 done  haftmann@33361  2327 haftmann@33361  2328 lemma zmult2_lemma_aux3: "[| (0::int) < c; 0 \ r; r < b |] ==> 0 \ b * (q mod c) + r"  haftmann@33361  2329 apply (subgoal_tac "0 \ b * (q mod c) ")  haftmann@33361  2330 apply arith  haftmann@33361  2331 apply (simp add: zero_le_mult_iff)  haftmann@33361  2332 done  haftmann@33361  2333 haftmann@33361  2334 lemma zmult2_lemma_aux4: "[| (0::int) < c; 0 \ r; r < b |] ==> b * (q mod c) + r < b * c"  haftmann@33361  2335 apply (subgoal_tac "r * 1 < b * (c - q mod c) ")  haftmann@33361  2336  apply (simp add: right_diff_distrib)  haftmann@33361  2337 apply (rule order_less_le_trans)  haftmann@33361  2338  apply (erule mult_strict_right_mono)  haftmann@33361  2339  apply (rule_tac [2] mult_left_mono)  haftmann@33361  2340  apply simp  huffman@35216  2341  using add1_zle_eq[of "q mod c"] apply (simp add: algebra_simps)  haftmann@33361  2342 apply simp  haftmann@33361  2343 done  haftmann@33361  2344 lp15@60562  2345 lemma zmult2_lemma: "[| divmod_int_rel a b (q, r); 0 < c |]  haftmann@33361  2346  ==> divmod_int_rel a (b * c) (q div c, b*(q mod c) + r)"  haftmann@57514  2347 by (auto simp add: mult.assoc divmod_int_rel_def linorder_neq_iff  lp15@60562  2348  zero_less_mult_iff distrib_left [symmetric]  huffman@47139  2349  zmult2_lemma_aux1 zmult2_lemma_aux2 zmult2_lemma_aux3 zmult2_lemma_aux4 mult_less_0_iff split: split_if_asm)  haftmann@33361  2350 haftmann@53068  2351 lemma zdiv_zmult2_eq:  haftmann@53068  2352  fixes a b c :: int  haftmann@53068  2353  shows "0 \ c \ a div (b * c) = (a div b) div c"  haftmann@33361  2354 apply (case_tac "b = 0", simp)  haftmann@53068  2355 apply (force simp add: le_less divmod_int_rel_div_mod [THEN zmult2_lemma, THEN div_int_unique])  haftmann@33361  2356 done  haftmann@33361  2357 haftmann@33361  2358 lemma zmod_zmult2_eq:  haftmann@53068  2359  fixes a b c :: int  haftmann@53068  2360  shows "0 \ c \ a mod (b * c) = b * (a div b mod c) + a mod b"  haftmann@33361  2361 apply (case_tac "b = 0", simp)  haftmann@53068  2362 apply (force simp add: le_less divmod_int_rel_div_mod [THEN zmult2_lemma, THEN mod_int_unique])  haftmann@33361  2363 done  haftmann@33361  2364 huffman@47108  2365 lemma div_pos_geq:  huffman@47108  2366  fixes k l :: int  huffman@47108  2367  assumes "0 < l" and "l \ k"  huffman@47108  2368  shows "k div l = (k - l) div l + 1"  huffman@47108  2369 proof -  huffman@47108  2370  have "k = (k - l) + l" by simp  huffman@47108  2371  then obtain j where k: "k = j + l" ..  huffman@47108  2372  with assms show ?thesis by simp  huffman@47108  2373 qed  huffman@47108  2374 huffman@47108  2375 lemma mod_pos_geq:  huffman@47108  2376  fixes k l :: int  huffman@47108  2377  assumes "0 < l" and "l \ k"  huffman@47108  2378  shows "k mod l = (k - l) mod l"  huffman@47108  2379 proof -  huffman@47108  2380  have "k = (k - l) + l" by simp  huffman@47108  2381  then obtain j where k: "k = j + l" ..  huffman@47108  2382  with assms show ?thesis by simp  huffman@47108  2383 qed  huffman@47108  2384 haftmann@33361  2385 huffman@46551  2386 subsubsection {* Splitting Rules for div and mod *}  haftmann@33361  2387 haftmann@33361  2388 text{*The proofs of the two lemmas below are essentially identical*}  haftmann@33361  2389 haftmann@33361  2390 lemma split_pos_lemma:  lp15@60562  2391  "0  haftmann@33361  2392  P(n div k :: int)(n mod k) = (\i j. 0\j & j P i j)"  haftmann@33361  2393 apply (rule iffI, clarify)  lp15@60562  2394  apply (erule_tac P="P x y" for x y in rev_mp)  lp15@60562  2395  apply (subst mod_add_eq)  lp15@60562  2396  apply (subst zdiv_zadd1_eq)  lp15@60562  2397  apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial)  haftmann@33361  2398 txt{*converse direction*}  lp15@60562  2399 apply (drule_tac x = "n div k" in spec)  haftmann@33361  2400 apply (drule_tac x = "n mod k" in spec, simp)  haftmann@33361  2401 done  haftmann@33361  2402 haftmann@33361  2403 lemma split_neg_lemma:  haftmann@33361  2404  "k<0 ==>  haftmann@33361  2405  P(n div k :: int)(n mod k) = (\i j. k0 & n = k*i + j --> P i j)"  haftmann@33361  2406 apply (rule iffI, clarify)  lp15@60562  2407  apply (erule_tac P="P x y" for x y in rev_mp)  lp15@60562  2408  apply (subst mod_add_eq)  lp15@60562  2409  apply (subst zdiv_zadd1_eq)  lp15@60562  2410  apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial)  haftmann@33361  2411 txt{*converse direction*}  lp15@60562  2412 apply (drule_tac x = "n div k" in spec)  haftmann@33361  2413 apply (drule_tac x = "n mod k" in spec, simp)  haftmann@33361  2414 done  haftmann@33361  2415 haftmann@33361  2416 lemma split_zdiv:  haftmann@33361  2417  "P(n div k :: int) =  lp15@60562  2418  ((k = 0 --> P 0) &  lp15@60562  2419  (0 (\i j. 0\j & j P i)) &  haftmann@33361  2420  (k<0 --> (\i j. k0 & n = k*i + j --> P i)))"  haftmann@33361  2421 apply (case_tac "k=0", simp)  haftmann@33361  2422 apply (simp only: linorder_neq_iff)  lp15@60562  2423 apply (erule disjE)  lp15@60562  2424  apply (simp_all add: split_pos_lemma [of concl: "%x y. P x"]  haftmann@33361  2425  split_neg_lemma [of concl: "%x y. P x"])  haftmann@33361  2426 done  haftmann@33361  2427 haftmann@33361  2428 lemma split_zmod:  haftmann@33361  2429  "P(n mod k :: int) =  lp15@60562  2430  ((k = 0 --> P n) &  lp15@60562  2431  (0 (\i j. 0\j & j P j)) &  haftmann@33361  2432  (k<0 --> (\i j. k0 & n = k*i + j --> P j)))"  haftmann@33361  2433 apply (case_tac "k=0", simp)  haftmann@33361  2434 apply (simp only: linorder_neq_iff)  lp15@60562  2435 apply (erule disjE)  lp15@60562  2436  apply (simp_all add: split_pos_lemma [of concl: "%x y. P y"]  haftmann@33361  2437  split_neg_lemma [of concl: "%x y. P y"])  haftmann@33361  2438 done  haftmann@33361  2439 haftmann@60429  2440 text {* Enable (lin)arith to deal with @{const divide} and @{const mod}  webertj@33730  2441  when these are applied to some constant that is of the form  huffman@47108  2442  @{term "numeral k"}: *}  huffman@47108  2443 declare split_zdiv [of _ _ "numeral k", arith_split] for k  huffman@47108  2444 declare split_zmod [of _ _ "numeral k", arith_split] for k  haftmann@33361  2445 haftmann@33361  2446 huffman@47166  2447 subsubsection {* Computing @{text "div"} and @{text "mod"} with shifting *}  huffman@47166  2448 huffman@47166  2449 lemma pos_divmod_int_rel_mult_2:  huffman@47166  2450  assumes "0 \ b"  huffman@47166  2451  assumes "divmod_int_rel a b (q, r)"  huffman@47166  2452  shows "divmod_int_rel (1 + 2*a) (2*b) (q, 1 + 2*r)"  huffman@47166  2453  using assms unfolding divmod_int_rel_def by auto  huffman@47166  2454 haftmann@54489  2455 declaration {* K (Lin_Arith.add_simps @{thms uminus_numeral_One}) *}  haftmann@54489  2456 huffman@47166  2457 lemma neg_divmod_int_rel_mult_2:  huffman@47166  2458  assumes "b \ 0"  huffman@47166  2459  assumes "divmod_int_rel (a + 1) b (q, r)"  huffman@47166  2460  shows "divmod_int_rel (1 + 2*a) (2*b) (q, 2*r - 1)"  huffman@47166  2461  using assms unfolding divmod_int_rel_def by auto  haftmann@33361  2462 haftmann@33361  2463 text{*computing div by shifting *}  haftmann@33361  2464 haftmann@33361  2465 lemma pos_zdiv_mult_2: "(0::int) \ a ==> (1 + 2*b) div (2*a) = b div a"  huffman@47166  2466  using pos_divmod_int_rel_mult_2 [OF _ divmod_int_rel_div_mod]  huffman@47166  2467  by (rule div_int_unique)  haftmann@33361  2468 lp15@60562  2469 lemma neg_zdiv_mult_2:  boehmes@35815  2470  assumes A: "a \ (0::int)" shows "(1 + 2*b) div (2*a) = (b+1) div a"  huffman@47166  2471  using neg_divmod_int_rel_mult_2 [OF A divmod_int_rel_div_mod]  huffman@47166  2472  by (rule div_int_unique)  haftmann@33361  2473 huffman@47108  2474 (* FIXME: add rules for negative numerals *)  huffman@47108  2475 lemma zdiv_numeral_Bit0 [simp]:  huffman@47108  2476  "numeral (Num.Bit0 v) div numeral (Num.Bit0 w) =  huffman@47108  2477  numeral v div (numeral w :: int)"  huffman@47108  2478  unfolding numeral.simps unfolding mult_2 [symmetric]  huffman@47108  2479  by (rule div_mult_mult1, simp)  huffman@47108  2480 huffman@47108  2481 lemma zdiv_numeral_Bit1 [simp]:  lp15@60562  2482  "numeral (Num.Bit1 v) div numeral (Num.Bit0 w) =  huffman@47108  2483  (numeral v div (numeral w :: int))"  huffman@47108  2484  unfolding numeral.simps  haftmann@57512  2485  unfolding mult_2 [symmetric] add.commute [of _ 1]  huffman@47108  2486  by (rule pos_zdiv_mult_2, simp)  haftmann@33361  2487 haftmann@33361  2488 lemma pos_zmod_mult_2:  haftmann@33361  2489  fixes a b :: int  haftmann@33361  2490  assumes "0 \ a"  haftmann@33361  2491  shows "(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)"  huffman@47166  2492  using pos_divmod_int_rel_mult_2 [OF assms divmod_int_rel_div_mod]  huffman@47166  2493  by (rule mod_int_unique)  haftmann@33361  2494 haftmann@33361  2495 lemma neg_zmod_mult_2:  haftmann@33361  2496  fixes a b :: int  haftmann@33361  2497  assumes "a \ 0"  haftmann@33361  2498  shows "(1 + 2 * b) mod (2 * a) = 2 * ((b + 1) mod a) - 1"  huffman@47166  2499  using neg_divmod_int_rel_mult_2 [OF assms divmod_int_rel_div_mod]  huffman@47166  2500  by (rule mod_int_unique)  haftmann@33361  2501 huffman@47108  2502 (* FIXME: add rules for negative numerals *) `