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