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