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